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/stockpredator.cc
[mareframe] / trunk / gadget / stockpredator.cc Repository:
ViewVC logotype

Annotation of /trunk/gadget/stockpredator.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download)

1 : agomez 1 #include "stockpredator.h"
2 :     #include "keeper.h"
3 :     #include "errorhandler.h"
4 :     #include "readfunc.h"
5 :     #include "prey.h"
6 :     #include "areatime.h"
7 :     #include "suits.h"
8 :     #include "readword.h"
9 :     #include "gadget.h"
10 :     #include "global.h"
11 :    
12 :     StockPredator::StockPredator(CommentStream& infile, const char* givenname, const IntVector& Areas,
13 :     const LengthGroupDivision* const OtherLgrpDiv, const LengthGroupDivision* const GivenLgrpDiv,
14 :     int minage, int numage, const TimeClass* const TimeInfo, Keeper* const keeper)
15 :     : PopPredator(givenname, Areas, OtherLgrpDiv, GivenLgrpDiv) {
16 :    
17 :     type = STOCKPREDATOR;
18 :     functionnumber = 0;
19 :     keeper->addString("predator");
20 :     keeper->addString(givenname);
21 :    
22 :     //first read in the suitability parameters
23 :     this->readSuitability(infile, TimeInfo, keeper);
24 :    
25 :     //now we read in the prey preference parameters - should be one for each prey
26 :     int i, check, count = 0;
27 :     char text[MaxStrLength];
28 :     strncpy(text, "", MaxStrLength);
29 :    
30 :     keeper->addString("preypreference");
31 :     infile >> text >> ws;
32 :     while (!infile.eof() && (((strcasecmp(text, "maxconsumption") != 0)) && (strcasecmp(text, "whaleconsumption") != 0))) {
33 :     check = 0;
34 :     for (i = 0; i < preference.Size(); i++) {
35 :     if (strcasecmp(text, this->getPreyName(i)) == 0) {
36 :     infile >> preference[i] >> ws;
37 :     count++;
38 :     check = 1;
39 :     }
40 :     }
41 :     if (!check)
42 :     handle.logMessage(LOGWARN, "Warning in stockpredator - failed to match prey", text);
43 :     infile >> text >> ws;
44 :     }
45 :     if (count != preference.Size())
46 :     handle.logMessage(LOGFAIL, "Error in stockpredator - missing prey preference data");
47 :     preference.Inform(keeper);
48 :     keeper->clearLast();
49 :    
50 :     //then read in the maximum consumption parameters
51 :     keeper->addString("consumption");
52 :     if (strcasecmp(text, "maxconsumption") == 0) {
53 :     functionnumber = 1;
54 :     consParam.resize(5, keeper);
55 :     for (i = 0; i < 4; i++)
56 :     if (!(infile >> consParam[i]))
57 :     handle.logFileMessage(LOGFAIL, "invalid format for maxconsumption vector");
58 :    
59 :     readWordAndVariable(infile, "halffeedingvalue", consParam[4]);
60 :    
61 :     } else if (strcasecmp(text, "whaleconsumption") == 0) {
62 :     functionnumber = 2;
63 :     consParam.resize(16, keeper);
64 :     for (i = 0; i < 15; i++)
65 :     if (!(infile >> consParam[i]))
66 :     handle.logFileMessage(LOGFAIL, "invalid format for whaleconsumption vector");
67 :    
68 :     readWordAndVariable(infile, "halffeedingvalue", consParam[15]);
69 :    
70 :     } else
71 :     handle.logFileUnexpected(LOGFAIL, "maxconsumption", text);
72 :    
73 :     consParam.Inform(keeper);
74 :     keeper->clearLast();
75 :    
76 :     //everything has been read from infile ... resize objects
77 :     int numlength = LgrpDiv->numLengthGroups();
78 :     int numarea = areas.Size();
79 :     IntVector lower(numage, 0);
80 :     IntVector agesize(numage, numlength);
81 :     predAlkeys.resize(numarea, minage, lower, agesize);
82 :     for (i = 0; i < predAlkeys.Size(); i++)
83 :     predAlkeys[i].setToZero();
84 :     maxcons.AddRows(numarea, numlength, 0.0);
85 :     Phi.AddRows(numarea, numlength, 0.0);
86 :     fphi.AddRows(numarea, numlength, 0.0);
87 :     subfphi.AddRows(numarea, numlength, 0.0);
88 :    
89 :     keeper->clearLast();
90 :     keeper->clearLast();
91 :     }
92 :    
93 :     void StockPredator::Print(ofstream& outfile) const {
94 :     int i, area;
95 :     outfile << "\nStock predator\n";
96 :     PopPredator::Print(outfile);
97 :     outfile << "\n\tPredator age length keys\n";
98 :     for (area = 0; area < areas.Size(); area++) {
99 :     outfile << "\tInternal area " << areas[area] << "\n\tNumbers\n";
100 :     predAlkeys[area].printNumbers(outfile);
101 :     outfile << "\tMean weights\n";
102 :     predAlkeys[area].printWeights(outfile);
103 :     }
104 :     outfile << "\n\tConsumption information\n";
105 :     for (area = 0; area < areas.Size(); area++) {
106 :     outfile << "\tPhi by length on internal area " << areas[area] << ":\n\t";
107 :     for (i = 0; i < fphi.Ncol(area); i++)
108 :     outfile << setw(smallwidth) << setprecision(smallprecision) << fphi[area][i] << sep;
109 :     outfile << "\n\tMaximum consumption by length on internal area " << areas[area] << ":\n\t";
110 :     for (i = 0; i < maxcons.Ncol(area); i++)
111 :     outfile << setw(smallwidth) << setprecision(smallprecision) << maxcons[area][i] << sep;
112 :     outfile << endl;
113 :     }
114 :     outfile << endl;
115 :     }
116 :    
117 :     void StockPredator::Sum(const AgeBandMatrix& stockAlkeys, int area) {
118 :     int inarea = this->areaNum(area);
119 :     predAlkeys[inarea].setToZero();
120 :     predAlkeys[inarea].Add(stockAlkeys, *CI);
121 :     predAlkeys[inarea].sumColumns(prednumber[inarea]);
122 :     }
123 :    
124 :     void StockPredator::Reset(const TimeClass* const TimeInfo) {
125 :     PopPredator::Reset(TimeInfo);
126 :    
127 :     //check that the various parameters that can be estimated are sensible
128 :     if ((handle.getLogLevel() >= LOGWARN) && (TimeInfo->getTime() == 1)) {
129 :     int i, check;
130 :     if (functionnumber == 1)
131 :     for (i = 0; i < consParam.Size(); i++)
132 :     if (consParam[i] < 0.0)
133 :     handle.logMessage(LOGWARN, "Warning in stockpredator - negative consumption parameter", consParam[i]);
134 :     check = 0;
135 :     if (preference.Size() > 1)
136 :     for (i = 1; i < preference.Size(); i++)
137 :     if (!(isEqual(preference[0], preference[i])))
138 :     check++;
139 :     if (check != 0)
140 :     handle.logMessage(LOGWARN, "Warning in stockpredator - preference parameters differ for", this->getName());
141 :     }
142 :     }
143 :    
144 :     void StockPredator::Eat(int area, const AreaClass* const Area, const TimeClass* const TimeInfo) {
145 :    
146 :     int prey, predl, preyl, check;
147 :     int inarea = this->areaNum(area);
148 :     double tmp;
149 :    
150 :     if (TimeInfo->getSubStep() == 1) {
151 :     //this is the first substep of the timestep so need to reset things
152 :     for (predl = 0; predl < LgrpDiv->numLengthGroups(); predl++) {
153 :     Phi[inarea][predl] = 0.0;
154 :     fphi[inarea][predl] = 0.0;
155 :     }
156 :    
157 :     if (functionnumber == 1) {
158 :     double temperature = Area->getTemperature(area, TimeInfo->getTime());
159 :     tmp = exp(temperature * (consParam[1] - temperature * temperature * consParam[2]))
160 :     * consParam[0] * TimeInfo->getTimeStepLength() / TimeInfo->numSubSteps();
161 :     for (predl = 0; predl < LgrpDiv->numLengthGroups(); predl++)
162 :     maxcons[inarea][predl] = tmp * pow(LgrpDiv->meanLength(predl), consParam[3]);
163 :    
164 :     } else if (functionnumber == 2) {
165 :     double max1, max2, max3, l;
166 :     for (predl = 0; predl < LgrpDiv->numLengthGroups(); predl++) {
167 :     l = LgrpDiv->meanLength(predl);
168 :     max1 = max(0.0, consParam[6] * (consParam[7] + consParam[8] * l));
169 :     max2 = max(0.0, consParam[9] * (consParam[10] + consParam[11] * l));
170 :     max3 = max(0.0, consParam[12] * (consParam[13] + consParam[14] * l));
171 :     tmp = consParam[2] * pow(prednumber[inarea][predl].W, consParam[3])
172 :     + consParam[4] * pow(l, consParam[5]) + max1 + max2 + max3;
173 :     maxcons[inarea][predl] = consParam[0] * consParam[1] * tmp;
174 :     }
175 :    
176 :     } else
177 :     handle.logMessage(LOGWARN, "Warning in stockpredator - unrecognised consumption format");
178 :    
179 :     } else {
180 :     //this is not the first substep of the timestep so only reset Phi
181 :     for (predl = 0; predl < LgrpDiv->numLengthGroups(); predl++)
182 :     Phi[inarea][predl] = 0.0;
183 :     }
184 :    
185 :     //Now maxcons contains the maximum consumption by length
186 :     //Calculating Phi(L) and O(l,L,prey) based on energy requirements
187 :     for (prey = 0; prey < this->numPreys(); prey++) {
188 :     check = 0;
189 :     if (isEqual(preference[prey], 1.0))
190 :     check = 1;
191 :    
192 :     if ((this->getPrey(prey)->isPreyArea(area)) && (!(isZero(this->getPrey(prey)->getEnergy())))) {
193 :     for (predl = 0; predl < LgrpDiv->numLengthGroups(); predl++) {
194 :     for (preyl = 0; preyl < (*cons[inarea][prey])[predl].Size(); preyl++) {
195 :     tmp = this->getSuitability(prey)[predl][preyl] * this->getPrey(prey)->getEnergy()
196 :     * this->getPrey(prey)->getBiomass(area, preyl);
197 :    
198 :     //JMB - dont take the power if we dont have to
199 :     if (!check)
200 :     tmp = pow(tmp, preference[prey]);
201 :     (*cons[inarea][prey])[predl][preyl] = tmp;
202 :     Phi[inarea][predl] += tmp;
203 :     }
204 :     }
205 :    
206 :     } else {
207 :     for (predl = 0; predl < LgrpDiv->numLengthGroups(); predl++)
208 :     for (preyl = 0; preyl < (*cons[inarea][prey])[predl].Size(); preyl++)
209 :     (*cons[inarea][prey])[predl][preyl] = 0.0;
210 :     }
211 :     }
212 :    
213 :     tmp = TimeInfo->getTimeStepLength() / TimeInfo->numSubSteps();
214 :     if (functionnumber == 1)
215 :     tmp *= consParam[4];
216 :     else if (functionnumber == 2)
217 :     tmp *= consParam[15];
218 :     else
219 :     handle.logMessage(LOGWARN, "Warning in stockpredator - unrecognised consumption format");
220 :    
221 :     //Calculating fphi(L) and totalcons of predator in area
222 :     for (predl = 0; predl < LgrpDiv->numLengthGroups(); predl++) {
223 :     if (isZero(tmp)) {
224 :     subfphi[inarea][predl] = 1.0;
225 :     totalcons[inarea][predl] = maxcons[inarea][predl] * prednumber[inarea][predl].N;
226 :     } else if (isZero(Phi[inarea][predl]) || isZero(Phi[inarea][predl] + tmp)) {
227 :     subfphi[inarea][predl] = 0.0;
228 :     totalcons[inarea][predl] = 0.0;
229 :     } else {
230 :     subfphi[inarea][predl] = Phi[inarea][predl] / (Phi[inarea][predl] + tmp);
231 :     totalcons[inarea][predl] = subfphi[inarea][predl]
232 :     * maxcons[inarea][predl] * prednumber[inarea][predl].N;
233 :     }
234 :     }
235 :    
236 :     //Distributing the total consumption on the preys and converting to biomass
237 :     for (prey = 0; prey < this->numPreys(); prey++) {
238 :     if ((this->getPrey(prey)->isPreyArea(area)) && (!(isZero(this->getPrey(prey)->getEnergy())))) {
239 :     for (predl = 0; predl < LgrpDiv->numLengthGroups(); predl++) {
240 :     if (!(isZero(Phi[inarea][predl]))) {
241 :     tmp = totalcons[inarea][predl] / (Phi[inarea][predl] * this->getPrey(prey)->getEnergy());
242 :     for (preyl = 0; preyl < (*cons[inarea][prey])[predl].Size(); preyl++)
243 :     (*cons[inarea][prey])[predl][preyl] *= tmp;
244 :    
245 :     //set the multiplicative constant
246 :     (*predratio[inarea])[prey][predl] += tmp;
247 :     }
248 :     }
249 :     }
250 :     }
251 :    
252 :     //Add the calculated consumption to the preys in question
253 :     for (prey = 0; prey < this->numPreys(); prey++)
254 :     if (this->getPrey(prey)->isPreyArea(area))
255 :     for (predl = 0; predl < LgrpDiv->numLengthGroups(); predl++)
256 :     this->getPrey(prey)->addBiomassConsumption(area, (*cons[inarea][prey])[predl]);
257 :     }
258 :    
259 :     //Check if any of the preys of the predator are eaten up.
260 :     //adjust the consumption according to that.
261 :     void StockPredator::adjustConsumption(int area, const TimeClass* const TimeInfo) {
262 :     int inarea = this->areaNum(area);
263 :     int numlen = LgrpDiv->numLengthGroups();
264 :     int preyl, predl, prey;
265 :     double maxRatio, tmp;
266 :    
267 :     maxRatio = TimeInfo->getMaxRatioConsumed();
268 :     for (predl = 0; predl < numlen; predl++)
269 :     overcons[inarea][predl] = 0.0;
270 :    
271 :     for (prey = 0; prey < this->numPreys(); prey++) {
272 :     if (this->getPrey(prey)->isOverConsumption(area)) {
273 :     hasoverconsumption[inarea] = 1;
274 :     DoubleVector ratio = this->getPrey(prey)->getRatio(area);
275 :     for (predl = 0; predl < numlen; predl++) {
276 :     for (preyl = 0; preyl < (*cons[inarea][prey])[predl].Size(); preyl++) {
277 :     if (ratio[preyl] > maxRatio) {
278 :     tmp = maxRatio / ratio[preyl];
279 :     overcons[inarea][predl] += (1.0 - tmp) * (*cons[inarea][prey])[predl][preyl];
280 :     (*cons[inarea][prey])[predl][preyl] *= tmp;
281 :     (*usesuit[inarea][prey])[predl][preyl] *= tmp;
282 :     }
283 :     }
284 :     }
285 :     }
286 :     }
287 :    
288 :     if (hasoverconsumption[inarea]) {
289 :     for (predl = 0; predl < numlen; predl++) {
290 :     overconsumption[inarea][predl] += overcons[inarea][predl];
291 :     if (totalcons[inarea][predl] > verysmall) {
292 :     tmp = 1.0 - (overcons[inarea][predl] / totalcons[inarea][predl]);
293 :     subfphi[inarea][predl] *= tmp;
294 :     totalcons[inarea][predl] -= overcons[inarea][predl];
295 :     }
296 :     }
297 :     }
298 :    
299 :     for (predl = 0; predl < numlen; predl++)
300 :     totalconsumption[inarea][predl] += totalcons[inarea][predl];
301 :    
302 :     if (TimeInfo->numSubSteps() != 1) {
303 :     double ratio1, ratio2;
304 :     ratio2 = 1.0 / TimeInfo->getSubStep();
305 :     ratio1 = 1.0 - ratio2;
306 :     for (predl = 0; predl < numlen; predl++)
307 :     fphi[inarea][predl] = (ratio2 * subfphi[inarea][predl]) + (ratio1 * fphi[inarea][predl]);
308 :    
309 :     } else
310 :     for (predl = 0; predl < numlen; predl++)
311 :     fphi[inarea][predl] = subfphi[inarea][predl];
312 :    
313 :     for (prey = 0; prey < this->numPreys(); prey++)
314 :     if (this->getPrey(prey)->isPreyArea(area))
315 :     for (predl = 0; predl < numlen; predl++)
316 :     for (preyl = 0; preyl < (*cons[inarea][prey])[predl].Size(); preyl++)
317 :     (*consumption[inarea][prey])[predl][preyl] += (*cons[inarea][prey])[predl][preyl];
318 :     }

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

Powered By FusionForge