Log In | Get Help   
Home My Page Projects Code Snippets Project Openings Mareframe
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files
[mareframe] Annotation of /trunk/gadget/prey.cc
[mareframe] / trunk / gadget / prey.cc Repository:
ViewVC logotype

Annotation of /trunk/gadget/prey.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download)

1 : agomez 1 #include "prey.h"
2 :     #include "errorhandler.h"
3 :     #include "readword.h"
4 :     #include "readaggregation.h"
5 :     #include "keeper.h"
6 :     #include "mathfunc.h"
7 :     #include "gadget.h"
8 :     #include "global.h"
9 :    
10 :     Prey::Prey(CommentStream& infile, const IntVector& Areas,
11 :     const char* givenname, const TimeClass* const TimeInfo, Keeper* const keeper)
12 :     : HasName(givenname), LivesOnAreas(Areas), CI(0) {
13 :    
14 :     char text[MaxStrLength];
15 :     strncpy(text, "", MaxStrLength);
16 :     int i;
17 :    
18 :     //read the prey length group data
19 :     DoubleVector preylengths;
20 :     CharPtrVector preylenindex;
21 :     char aggfilename[MaxStrLength];
22 :     strncpy(aggfilename, "", MaxStrLength);
23 :     ifstream datafile;
24 :     CommentStream subdata(datafile);
25 :    
26 :     readWordAndValue(infile, "preylengths", aggfilename);
27 :     datafile.open(aggfilename, ios::in);
28 :     handle.checkIfFailure(datafile, aggfilename);
29 :     handle.Open(aggfilename);
30 :     i = readLengthAggregation(subdata, preylengths, preylenindex);
31 :     handle.Close();
32 :     datafile.close();
33 :     datafile.clear();
34 :    
35 :     LgrpDiv = new LengthGroupDivision(preylengths);
36 :     if (LgrpDiv->Error())
37 :     handle.logMessage(LOGFAIL, "Error in prey - failed to create length group");
38 :    
39 :     //read the energy content of this prey
40 :     infile >> ws;
41 :     char c = infile.peek();
42 :     if ((c == 'e') || (c == 'E'))
43 :     readWordAndModelVariable(infile, "energycontent", energy, TimeInfo, keeper);
44 :     else
45 :     energy.setValue(1.0);
46 :    
47 :     //read from file - initialise things
48 :     int numlen = LgrpDiv->numLengthGroups();
49 :     int numarea = areas.Size();
50 :     PopInfo nullpop;
51 :    
52 :     preynumber.AddRows(numarea, numlen, nullpop);
53 :     biomass.AddRows(numarea, numlen, 0.0);
54 :     cons.AddRows(numarea, numlen, 0.0);
55 :     consumption.AddRows(numarea, numlen, 0.0);
56 :     isoverconsumption.resize(numarea, 0);
57 :     total.resize(numarea, 0.0);
58 :     ratio.AddRows(numarea, numlen, 0.0);
59 :     useratio.AddRows(numarea, numlen, 0.0);
60 :     consratio.AddRows(numarea, numlen, 0.0);
61 :     overconsumption.AddRows(numarea, numlen, 0.0);
62 :    
63 :     //preylenindex is not required - free up memory
64 :     for (i = 0; i < preylenindex.Size(); i++)
65 :     delete[] preylenindex[i];
66 :     }
67 :    
68 :     Prey::Prey(CommentStream& infile, const char* givenname,
69 :     const IntVector& Areas, const TimeClass* const TimeInfo, Keeper* const keeper)
70 :     : HasName(givenname), LivesOnAreas(Areas) {
71 :    
72 :     char text[MaxStrLength];
73 :     strncpy(text, "", MaxStrLength);
74 :    
75 :     //read the length information
76 :     DoubleVector lengths(2, 0.0);
77 :     infile >> text >> ws;
78 :     if (strcasecmp(text, "lengths") != 0)
79 :     handle.logFileUnexpected(LOGFAIL, "lengths", text);
80 :     infile >> lengths[0] >> lengths[1] >> ws;
81 :    
82 :     LgrpDiv = new LengthGroupDivision(lengths);
83 :     if (LgrpDiv->Error())
84 :     handle.logMessage(LOGFAIL, "Error in prey - failed to create length group");
85 :     CI = new ConversionIndex(LgrpDiv, LgrpDiv);
86 :     if (CI->Error())
87 :     handle.logMessage(LOGFAIL, "Error in prey - error when checking length structure");
88 :    
89 :     //read the energy content of this prey
90 :     infile >> ws;
91 :     char c = infile.peek();
92 :     if ((c == 'e') || (c == 'E'))
93 :     readWordAndModelVariable(infile, "energycontent", energy, TimeInfo, keeper);
94 :     else
95 :     energy.setValue(1.0);
96 :    
97 :     int numlen = LgrpDiv->numLengthGroups();
98 :     int numarea = areas.Size();
99 :     PopInfo nullpop;
100 :    
101 :     preynumber.AddRows(numarea, numlen, nullpop);
102 :     biomass.AddRows(numarea, numlen, 0.0);
103 :     cons.AddRows(numarea, numlen, 0.0);
104 :     consumption.AddRows(numarea, numlen, 0.0);
105 :     isoverconsumption.resize(numarea, 0);
106 :     total.resize(numarea, 0.0);
107 :     ratio.AddRows(numarea, numlen, 0.0);
108 :     useratio.AddRows(numarea, numlen, 0.0);
109 :     consratio.AddRows(numarea, numlen, 0.0);
110 :     overconsumption.AddRows(numarea, numlen, 0.0);
111 :     }
112 :    
113 :     Prey::~Prey() {
114 :     delete CI;
115 :     delete LgrpDiv;
116 :     }
117 :    
118 :     void Prey::setCI(const LengthGroupDivision* const GivenLDiv) {
119 :     if (!checkLengthGroupStructure(GivenLDiv, LgrpDiv))
120 :     handle.logMessage(LOGFAIL, "Error in prey - invalid length group structure for consumption of", this->getName());
121 :     if (GivenLDiv->minLength() < LgrpDiv->minLength())
122 :     handle.logMessage(LOGFAIL, "Error in prey - invalid minimum length group for consumption of", this->getName());
123 :     if (!isSmall(LgrpDiv->minLength() - GivenLDiv->minLength()))
124 :     handle.logMessage(LOGWARN, "Warning in prey - minimum lengths don't match for consumption of", this->getName());
125 :     if (GivenLDiv->maxLength() > LgrpDiv->maxLength())
126 :     handle.logMessage(LOGFAIL, "Error in prey - invalid maximum length group for consumption of", this->getName());
127 :     if (!isSmall(LgrpDiv->maxLength() - GivenLDiv->maxLength()))
128 :     handle.logMessage(LOGWARN, "Warning in prey - maximum lengths don't match for consumption of", this->getName());
129 :    
130 :     CI = new ConversionIndex(GivenLDiv, LgrpDiv);
131 :     if (CI->Error())
132 :     handle.logMessage(LOGFAIL, "Error in prey - error when checking length structure for", this->getName());
133 :     }
134 :    
135 :     void Prey::Print(ofstream& outfile) const {
136 :     int i, area;
137 :     outfile << "\nPrey\n\tName " << this->getName() << "\n\tEnergy content " << energy << "\n\t";
138 :     LgrpDiv->Print(outfile);
139 :     for (area = 0; area < areas.Size(); area++) {
140 :     outfile << "\tNumber of prey on internal area " << areas[area] << ":\n\t";
141 :     for (i = 0; i < LgrpDiv->numLengthGroups(); i++)
142 :     outfile << setw(smallwidth) << setprecision(smallprecision) << preynumber[area][i].N << sep;
143 :     outfile << "\n\tWeight of prey on internal area " << areas[area] << ":\n\t";
144 :     for (i = 0; i < LgrpDiv->numLengthGroups(); i++)
145 :     outfile << setw(smallwidth) << setprecision(smallprecision) << preynumber[area][i].W << sep;
146 :     outfile << "\n\tConsumption of prey on internal area " << areas[area] << ":\n\t";
147 :     for (i = 0; i < LgrpDiv->numLengthGroups(); i++)
148 :     outfile << setw(smallwidth) << setprecision(smallprecision) << consumption[area][i] << sep;
149 :     outfile << "\n\tOverconsumption of prey on internal area " << areas[area] << ":\n\t";
150 :     for (i = 0; i < LgrpDiv->numLengthGroups(); i++)
151 :     outfile << setw(smallwidth) << setprecision(smallprecision) << overconsumption[area][i] << sep;
152 :     outfile << endl;
153 :     }
154 :     }
155 :    
156 :     //reduce the population of the stock by the consumption
157 :     void Prey::Subtract(AgeBandMatrix& Alkeys, int area) {
158 :     Alkeys.Subtract(consratio[this->areaNum(area)], *CI);
159 :     }
160 :    
161 :     //adds the consumption by biomass
162 :     void Prey::addBiomassConsumption(int area, const DoubleVector& predcons) {
163 :     int i, inarea = this->areaNum(area);
164 :     if (predcons.Size() != cons[inarea].Size())
165 :     handle.logMessage(LOGFAIL, "Error in consumption - cannot add different size vectors");
166 :     for (i = 0; i < predcons.Size(); i++)
167 :     cons[inarea][i] += predcons[i];
168 :     }
169 :    
170 :     //adds the consumption by numbers
171 :     void Prey::addNumbersConsumption(int area, const DoubleVector& predcons) {
172 :     int i, inarea = this->areaNum(area);
173 :     if (predcons.Size() != cons[inarea].Size())
174 :     handle.logMessage(LOGFAIL, "Error in consumption - cannot add different size vectors");
175 :     for (i = 0; i < predcons.Size(); i++)
176 :     cons[inarea][i] += (predcons[i] * preynumber[inarea][i].W);
177 :     }
178 :    
179 :     //check if more is consumed of prey than was available. If this is
180 :     //the case a flag is set. Changed 22 - May 1997 so that only 95% of a prey
181 :     //in an area can be eaten in one timestep. This is to avoid problems
182 :     //with survey indices etc.
183 :     void Prey::checkConsumption(int area, const TimeClass* const TimeInfo) {
184 :     int i, over = 0;
185 :     int inarea = this->areaNum(area);
186 :    
187 :     double timeratio = 1.0;
188 :     double maxRatio = TimeInfo->getMaxRatioConsumed();
189 :     if (TimeInfo->numSubSteps() != 1) {
190 :     timeratio = 1.0 / TimeInfo->getSubStep();
191 :     for (i = 0; i < LgrpDiv->numLengthGroups(); i++)
192 :     useratio[inarea][i] *= (1.0 - timeratio);
193 :     }
194 :    
195 :     for (i = 0; i < LgrpDiv->numLengthGroups(); i++) {
196 :     if (isZero(biomass[inarea][i])) {
197 :     //no prey biomass available to consume
198 :     ratio[inarea][i] = 0.0;
199 :     consratio[inarea][i] = 0.0;
200 :     if (!(isZero(cons[inarea][i]))) {
201 :     //consumption required but no prey exists
202 :     over = 1;
203 :     overconsumption[inarea][i] += cons[inarea][i];
204 :     }
205 :    
206 :     } else {
207 :     //prey available to consume so only need to check overconsumption
208 :     ratio[inarea][i] = cons[inarea][i] / biomass[inarea][i];
209 :     if (ratio[inarea][i] > maxRatio) {
210 :     over = 1;
211 :     overconsumption[inarea][i] += (ratio[inarea][i] - maxRatio) * biomass[inarea][i];
212 :     consratio[inarea][i] = 1.0 - maxRatio;
213 :     useratio[inarea][i] += (timeratio * maxRatio);
214 :     cons[inarea][i] = biomass[inarea][i] * maxRatio;
215 :     } else {
216 :     consratio[inarea][i] = 1.0 - ratio[inarea][i];
217 :     useratio[inarea][i] += (timeratio * ratio[inarea][i]);
218 :     }
219 :     //finally add the consumption
220 :     consumption[inarea][i] += cons[inarea][i];
221 :     }
222 :     }
223 :    
224 :     //JMB changed to deal with substeps a little better
225 :     if (over)
226 :     isoverconsumption[inarea] = over;
227 :     }
228 :    
229 :     void Prey::Reset(const TimeClass* const TimeInfo) {
230 :     consumption.setToZero();
231 :     overconsumption.setToZero();
232 :     useratio.setToZero();
233 :     isoverconsumption.setToZero();
234 :    
235 :     energy.Update(TimeInfo);
236 :     if (isZero(energy))
237 :     handle.logMessage(LOGWARN, "Warning in prey - energy content should be non-zero");
238 :    
239 :     if (handle.getLogLevel() >= LOGMESSAGE)
240 :     handle.logMessage(LOGMESSAGE, "Reset consumption data for prey", this->getName());
241 :     }
242 :    
243 :     int Prey::isPreyArea(int area) {
244 :     if (this->isInArea(area) == 0)
245 :     return 0;
246 :     if (total[this->areaNum(area)] < 0.0)
247 :     handle.logMessage(LOGWARN, "Warning in prey - negative amount consumed for", this->getName());
248 :     if (isZero(total[this->areaNum(area)]))
249 :     return 0;
250 :     return 1;
251 :     }
252 :    
253 :     int Prey::isOverConsumption(int area) {
254 :     if (this->isInArea(area) == 0)
255 :     return 0;
256 :     return isoverconsumption[this->areaNum(area)];
257 :     }
258 :    
259 :     double Prey::getTotalOverConsumption(int area) const {
260 :     int inarea = this->areaNum(area);
261 :     if (inarea == -1)
262 :     return 0.0;
263 :    
264 :     int i;
265 :     double total = 0.0;
266 :     for (i = 0; i < LgrpDiv->numLengthGroups(); i++)
267 :     total += overconsumption[inarea][i];
268 :     return total;
269 :     }

root@forge.cesga.es
ViewVC Help
Powered by ViewVC 1.0.0  

Powered By FusionForge