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

Annotation of /trunk/gadget/stock.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download)

1 : agomez 1 #include "stock.h"
2 :     #include "keeper.h"
3 :     #include "areatime.h"
4 :     #include "naturalm.h"
5 :     #include "grower.h"
6 :     #include "stockprey.h"
7 :     #include "stockpredator.h"
8 :     #include "initialcond.h"
9 :     #include "migration.h"
10 :     #include "readfunc.h"
11 :     #include "errorhandler.h"
12 :     #include "maturity.h"
13 :     #include "renewal.h"
14 :     #include "transition.h"
15 :     #include "spawner.h"
16 :     #include "stray.h"
17 :     #include "readword.h"
18 :     #include "readaggregation.h"
19 :     #include "gadget.h"
20 :     #include "global.h"
21 :    
22 :     Stock::Stock(CommentStream& infile, const char* givenname,
23 :     const AreaClass* const Area, const TimeClass* const TimeInfo, Keeper* const keeper)
24 :     : BaseClass(givenname), stray(0), spawner(0), renewal(0), maturity(0), transition(0),
25 :     migration(0), prey(0), predator(0), initial(0), LgrpDiv(0), grower(0), naturalm(0) {
26 :    
27 :     doesgrow = doeseat = iseaten = doesmigrate = istagged = 0;
28 :     doesmove = doesrenew = doesmature = doesspawn = doesstray = 0;
29 :     int i, tmpint = 0;
30 :     char c;
31 :     char text[MaxStrLength];
32 :     strncpy(text, "", MaxStrLength);
33 :     char filename[MaxStrLength];
34 :     strncpy(filename, "", MaxStrLength);
35 :     IntVector tmpareas;
36 :    
37 :     ifstream datafile;
38 :     CommentStream subdata(datafile);
39 :     keeper->setString(this->getName());
40 :    
41 :     //read the area data
42 :     infile >> text >> ws;
43 :     if (strcasecmp(text, "livesonareas") != 0)
44 :     handle.logFileUnexpected(LOGFAIL, "livesonareas", text);
45 :    
46 :     c = infile.peek();
47 :     while (isdigit(c) && !infile.eof()) {
48 :     infile >> tmpint >> ws;
49 :     tmpareas.resize(1, Area->getInnerArea(tmpint));
50 :     c = infile.peek();
51 :     }
52 :     this->storeAreas(tmpareas);
53 :    
54 :     //read the stock age and length data
55 :     int minage, maxage, numage;
56 :     double minlength, maxlength, dl;
57 :     readWordAndVariable(infile, "minage", minage);
58 :     readWordAndVariable(infile, "maxage", maxage);
59 :     numage = maxage - minage + 1;
60 :     if (minage < 0)
61 :     handle.logMessage(LOGFAIL, "Error in stock - stock age must be positive");
62 :     if (numage < 1)
63 :     handle.logMessage(LOGFAIL, "Error in stock - failed to create age groups");
64 :    
65 :     readWordAndVariable(infile, "minlength", minlength);
66 :     readWordAndVariable(infile, "maxlength", maxlength);
67 :     readWordAndVariable(infile, "dl", dl);
68 :     if (minlength < 0.0)
69 :     handle.logMessage(LOGFAIL, "Error in stock - stock length must be positive");
70 :     LgrpDiv = new LengthGroupDivision(minlength, maxlength, dl);
71 :     if (LgrpDiv->Error() || isZero(dl))
72 :     handle.logMessage(LOGFAIL, "Error in stock - failed to create length group");
73 :    
74 :     //JMB need to read the location of the reference weights file
75 :     char refweight[MaxStrLength];
76 :     strncpy(refweight, "", MaxStrLength);
77 :     readWordAndValue(infile, "refweightfile", refweight);
78 :    
79 :     //JMB need to set the lowerlgrp and size vectors to a default
80 :     //value to allow the whole range of lengths to be calculated
81 :     IntVector lower(numage, 0);
82 :     IntVector agesize(numage, LgrpDiv->numLengthGroups());
83 :     Alkeys.resize(areas.Size(), minage, lower, agesize);
84 :     for (i = 0; i < Alkeys.Size(); i++)
85 :     Alkeys[i].setToZero();
86 :    
87 :     //read the growth length group data
88 :     DoubleVector grlengths;
89 :     CharPtrVector grlenindex;
90 :    
91 :     readWordAndValue(infile, "growthandeatlengths", filename);
92 :     datafile.open(filename, ios::in);
93 :     handle.checkIfFailure(datafile, filename);
94 :     handle.Open(filename);
95 :     i = readLengthAggregation(subdata, grlengths, grlenindex);
96 :     handle.Close();
97 :     datafile.close();
98 :     datafile.clear();
99 :    
100 :     LengthGroupDivision* GrowLgrpDiv = new LengthGroupDivision(grlengths);
101 :     if (GrowLgrpDiv->Error())
102 :     handle.logMessage(LOGFAIL, "Error in stock - failed to create growth length group for", this->getName());
103 :    
104 :     //Check the growth length groups cover the stock length groups
105 :     if (!checkLengthGroupStructure(LgrpDiv, GrowLgrpDiv))
106 :     handle.logMessage(LOGFAIL, "Error in stock - invalid length group structure for growth of", this->getName());
107 :     if (LgrpDiv->minLength() < GrowLgrpDiv->minLength())
108 :     handle.logMessage(LOGFAIL, "Error in stock - invalid minimum length group for growth of", this->getName());
109 :     if (!isSmall(LgrpDiv->minLength() - GrowLgrpDiv->minLength()))
110 :     handle.logMessage(LOGWARN, "Warning in stock - minimum lengths don't match for growth of", this->getName());
111 :     if (LgrpDiv->maxLength() > GrowLgrpDiv->maxLength())
112 :     handle.logMessage(LOGFAIL, "Error in stock - invalid maximum length group for growth of", this->getName());
113 :     if (!isSmall(LgrpDiv->maxLength() - GrowLgrpDiv->maxLength()))
114 :     handle.logMessage(LOGWARN, "Warning in stock - maximum lengths don't match for growth of", this->getName());
115 :    
116 :     //JMB check that the growth is defined on evenly spaced length groups
117 :     if (isZero(GrowLgrpDiv->dl()))
118 :     handle.logMessage(LOGWARN, "Warning in stock - growth length structure not evenly spaced for", this->getName());
119 :     handle.logMessage(LOGMESSAGE, "Read basic stock data for stock", this->getName());
120 :    
121 :     //read the growth function data
122 :     PopInfo nullpop;
123 :     tmpPopulation.AddRows(areas.Size(), LgrpDiv->numLengthGroups(), nullpop);
124 :     readWordAndVariable(infile, "doesgrow", doesgrow);
125 :     if (doesgrow) {
126 :     grower = new Grower(infile, LgrpDiv, GrowLgrpDiv, areas, TimeInfo, keeper, refweight, this->getName(), Area, grlenindex);
127 :    
128 :     } else
129 :     grower = 0;
130 :     handle.logMessage(LOGMESSAGE, "Read growth data for stock", this->getName());
131 :    
132 :     //read the natural mortality data
133 :     infile >> text;
134 :     if (strcasecmp(text, "naturalmortality") != 0)
135 :     handle.logFileUnexpected(LOGFAIL, "naturalmortality", text);
136 :     naturalm = new NaturalMortality(infile, minage, numage, this->getName(), areas, TimeInfo, keeper);
137 :     handle.logMessage(LOGMESSAGE, "Read natural mortality data for stock", this->getName());
138 :    
139 :     //read the prey data
140 :     readWordAndVariable(infile, "iseaten", iseaten);
141 :     if (iseaten) {
142 :     prey = new StockPrey(infile, areas, this->getName(), minage, numage, TimeInfo, keeper);
143 :    
144 :     } else
145 :     prey = 0;
146 :     handle.logMessage(LOGMESSAGE, "Read prey data for stock", this->getName());
147 :    
148 :     //read the predator data
149 :     readWordAndVariable(infile, "doeseat", doeseat);
150 :     if (doeseat) {
151 :     predator = new StockPredator(infile, this->getName(), areas, LgrpDiv,
152 :     GrowLgrpDiv, minage, numage, TimeInfo, keeper);
153 :    
154 :     } else
155 :     predator = 0;
156 :     handle.logMessage(LOGMESSAGE, "Read predator data for stock", this->getName());
157 :    
158 :     //read the initial conditions
159 :     infile >> text;
160 :     if (strcasecmp(text, "initialconditions") != 0)
161 :     handle.logFileUnexpected(LOGFAIL, "initialconditions", text);
162 :     initial = new InitialCond(infile, areas, keeper, refweight, this->getName(), Area, dl);
163 :     handle.logMessage(LOGMESSAGE, "Read initial conditions data for stock", this->getName());
164 :    
165 :     //read the migration data
166 :     readWordAndVariable(infile, "doesmigrate", doesmigrate);
167 :     if (doesmigrate) {
168 :     tmpMigrate.resizeBlank(areas.Size());
169 :     infile >> ws;
170 :     c = infile.peek();
171 :     if ((c == 'y') || (c == 'Y')) {
172 :     //about to read the word yearstepfile
173 :     migration = new MigrationNumbers(infile, areas, Area, TimeInfo, this->getName(), keeper);
174 :     } else if ((c == 'd') || (c == 'D')) {
175 :     //about to read the word diffusion
176 :     migration = new MigrationFunction(infile, areas, Area, TimeInfo, this->getName(), keeper);
177 :     } else {
178 :     infile >> text >> ws;
179 :     handle.logFileUnexpected(LOGFAIL, "migrationnumbers or migrationfunction", text);
180 :     }
181 :    
182 :     } else
183 :     migration = 0;
184 :     handle.logMessage(LOGMESSAGE, "Read migration data for stock", this->getName());
185 :    
186 :     //read the maturation data
187 :     readWordAndVariable(infile, "doesmature", doesmature);
188 :     if (doesmature) {
189 :     readWordAndValue(infile, "maturityfunction", text);
190 :     readWordAndValue(infile, "maturityfile", filename);
191 :     ifstream subfile;
192 :     subfile.open(filename, ios::in);
193 :     CommentStream subcomment(subfile);
194 :     handle.checkIfFailure(subfile, filename);
195 :     handle.Open(filename);
196 :    
197 :     if (strcasecmp(text, "continuous") == 0) {
198 :     maturity = new MaturityA(subcomment, TimeInfo, keeper, minage, numage, areas, this->getName(), LgrpDiv);
199 :    
200 :     } else if (strcasecmp(text, "fixedlength") == 0) {
201 :     maturity = new MaturityB(subcomment, TimeInfo, keeper, minage, numage, areas, this->getName(), LgrpDiv);
202 :    
203 :     } else if (strcasecmp(text, "newconstant") == 0) {
204 :     maturity = new MaturityC(subcomment, TimeInfo, keeper, minage, numage, areas, this->getName(), LgrpDiv, 4);
205 :    
206 :     } else if (strcasecmp(text, "newconstantweight") == 0) {
207 :     maturity = new MaturityD(subcomment, TimeInfo, keeper, minage, numage, areas, this->getName(), LgrpDiv, 6, refweight);
208 :    
209 :     } else if (strcasecmp(text, "ageandlength") == 0) {
210 :     handle.logMessage(LOGFAIL, "\nThe ageandlength maturity function is no longer supported");
211 :    
212 :     } else if (strcasecmp(text, "constant") == 0) {
213 :     handle.logMessage(LOGFAIL, "\nThe constant maturity function is no longer supported\nUse the newconstant maturity function instead\nNote that this function has had a factor of 4 removed from the source code");
214 :    
215 :     } else if (strcasecmp(text, "constantweight") == 0) {
216 :     handle.logMessage(LOGFAIL, "\nThe constantweight maturity function is no longer supported\nUse the newconstantweight maturity function instead\nNote that this function has had a factor of 4 removed from the source code");
217 :    
218 :     } else
219 :     handle.logMessage(LOGFAIL, "unrecognised maturity function", text);
220 :    
221 :     handle.Close();
222 :     subfile.close();
223 :     subfile.clear();
224 :    
225 :     if (!doesgrow)
226 :     handle.logMessage(LOGFAIL, "Error in stock - maturation without growth is not implemented");
227 :    
228 :     } else
229 :     maturity = 0;
230 :     handle.logMessage(LOGMESSAGE, "Read maturity data for stock", this->getName());
231 :    
232 :     //read the movement data
233 :     readWordAndVariable(infile, "doesmove", doesmove);
234 :     if (doesmove) {
235 :     //transition handles the movements of the age group maxage
236 :     transition = new Transition(infile, areas, maxage, LgrpDiv, this->getName(), TimeInfo, keeper);
237 :    
238 :     } else
239 :     transition = 0;
240 :     handle.logMessage(LOGMESSAGE, "Read transition data for stock", this->getName());
241 :    
242 :     //read the renewal data
243 :     readWordAndVariable(infile, "doesrenew", doesrenew);
244 :     if (doesrenew) {
245 :     renewal = new RenewalData(infile, areas, Area, TimeInfo, keeper, refweight, this->getName(), minage, maxage, dl);
246 :    
247 :     } else
248 :     renewal = 0;
249 :     handle.logMessage(LOGMESSAGE, "Read renewal data for stock", this->getName());
250 :    
251 :     //read the spawning data
252 :     readWordAndVariable(infile, "doesspawn", doesspawn);
253 :     if (doesspawn) {
254 :     readWordAndValue(infile, "spawnfile", filename);
255 :     ifstream subfile;
256 :     subfile.open(filename, ios::in);
257 :     CommentStream subcomment(subfile);
258 :     handle.checkIfFailure(subfile, filename);
259 :     handle.Open(filename);
260 :     spawner = new SpawnData(subcomment, maxage, LgrpDiv, areas, Area, this->getName(), TimeInfo, keeper);
261 :     handle.Close();
262 :     subfile.close();
263 :     subfile.clear();
264 :    
265 :     } else
266 :     spawner = 0;
267 :     handle.logMessage(LOGMESSAGE, "Read spawning data for stock", this->getName());
268 :    
269 :     infile >> ws;
270 :     if (!infile.eof()) {
271 :     //read the optional straying data
272 :     readWordAndVariable(infile, "doesstray", doesstray);
273 :     if (doesstray) {
274 :     readWordAndValue(infile, "strayfile", filename);
275 :     ifstream subfile;
276 :     subfile.open(filename, ios::in);
277 :     CommentStream subcomment(subfile);
278 :     handle.checkIfFailure(subfile, filename);
279 :     handle.Open(filename);
280 :     stray = new StrayData(subcomment, LgrpDiv, areas, Area, this->getName(), TimeInfo, keeper);
281 :     handle.Close();
282 :     subfile.close();
283 :     subfile.clear();
284 :    
285 :     } else
286 :     stray = 0;
287 :     handle.logMessage(LOGMESSAGE, "Read straying data for stock", this->getName());
288 :     }
289 :    
290 :     //set the birthday for the stock
291 :     birthdate = TimeInfo->numSteps();
292 :    
293 :     //finished reading from infile
294 :     delete GrowLgrpDiv;
295 :     for (i = 0; i < grlenindex.Size(); i++)
296 :     delete[] grlenindex[i];
297 :     keeper->clearAll();
298 :     }
299 :    
300 :     Stock::Stock(const char* givenname)
301 :     : BaseClass(givenname), stray(0), spawner(0), renewal(0), maturity(0), transition(0),
302 :     migration(0), prey(0), predator(0), initial(0), LgrpDiv(0), grower(0), naturalm(0) {
303 :    
304 :     doesgrow = doeseat = iseaten = doesmigrate = istagged = 0;
305 :     doesmove = doesrenew = doesmature = doesspawn = doesstray = 0;
306 :     }
307 :    
308 :     Stock::~Stock() {
309 :     if (migration != 0)
310 :     delete migration;
311 :     if (prey != 0)
312 :     delete prey;
313 :     if (predator != 0)
314 :     delete predator;
315 :     if (initial != 0)
316 :     delete initial;
317 :     if (LgrpDiv != 0)
318 :     delete LgrpDiv;
319 :     if (grower != 0)
320 :     delete grower;
321 :     if (naturalm != 0)
322 :     delete naturalm;
323 :     if (transition != 0)
324 :     delete transition;
325 :     if (renewal != 0)
326 :     delete renewal;
327 :     if (maturity != 0)
328 :     delete maturity;
329 :     if (spawner != 0)
330 :     delete spawner;
331 :     if (stray != 0)
332 :     delete stray;
333 :     }
334 :    
335 :     void Stock::Reset(const TimeClass* const TimeInfo) {
336 :     naturalm->Reset(TimeInfo);
337 :     if (doeseat)
338 :     predator->Reset(TimeInfo);
339 :     if (doesmature)
340 :     maturity->Reset(TimeInfo);
341 :     if (doesspawn)
342 :     spawner->Reset(TimeInfo);
343 :     if (doesstray)
344 :     stray->Reset(TimeInfo);
345 :     if (iseaten)
346 :     prey->Reset(TimeInfo);
347 :    
348 :     if (TimeInfo->getTime() == 1) {
349 :     initial->Initialise(Alkeys);
350 :     if (doesrenew)
351 :     renewal->Reset();
352 :     if (doesgrow)
353 :     grower->Reset();
354 :     if (doesmigrate)
355 :     migration->Reset();
356 :     if (doesmove)
357 :     transition->Reset();
358 :     }
359 :    
360 :     if (handle.getLogLevel() >= LOGMESSAGE)
361 :     handle.logMessage(LOGMESSAGE, "Reset stock data for stock", this->getName());
362 :     }
363 :    
364 :     void Stock::setStock(StockPtrVector& stockvec) {
365 :    
366 :     initial->setCI(LgrpDiv);
367 :     if (iseaten)
368 :     prey->setCI(LgrpDiv);
369 :     if (doesrenew)
370 :     renewal->setCI(LgrpDiv);
371 :    
372 :     //JMB for the stock variable stuff to work we need to initialise the population
373 :     //very early in the simulation so we do it here and then do it again in the
374 :     //reset() function that gets called later (as part of the optimisation runs)
375 :     initial->Initialise(Alkeys);
376 :    
377 :     int i;
378 :     StockPtrVector tmpStockVector;
379 :     if (doesmature) {
380 :     maturity->setStock(stockvec);
381 :     tmpStockVector = maturity->getMatureStocks();
382 :     for (i = 0; i < tmpStockVector.Size(); i++)
383 :     if (strcasecmp(tmpStockVector[i]->getName(), this->getName()) == 0)
384 :     handle.logMessage(LOGWARN, "Warning in stock - stock maturing into itself", this->getName());
385 :     }
386 :     if (doesmove) {
387 :     transition->setStock(stockvec);
388 :     tmpStockVector = transition->getTransitionStocks();
389 :     for (i = 0; i < tmpStockVector.Size(); i++)
390 :     if (strcasecmp(tmpStockVector[i]->getName(), this->getName()) == 0)
391 :     handle.logMessage(LOGWARN, "Warning in stock - stock moving into itself", this->getName());
392 :     }
393 :     if (doesstray) {
394 :     stray->setStock(stockvec);
395 :     tmpStockVector = stray->getStrayStocks();
396 :     for (i = 0; i < tmpStockVector.Size(); i++)
397 :     if (strcasecmp(tmpStockVector[i]->getName(), this->getName()) == 0)
398 :     handle.logMessage(LOGWARN, "Warning in stock - stock straying into itself", this->getName());
399 :     }
400 :     if (doesspawn)
401 :     spawner->setStock(stockvec);
402 :     }
403 :    
404 :     void Stock::setTagged() {
405 :     //resize tagAlkeys to be the same size as Alkeys
406 :     int i, minage, maxage;
407 :     minage = Alkeys[0].minAge();
408 :     maxage = Alkeys[0].maxAge();
409 :     IntVector lower(maxage - minage + 1, 0);
410 :     IntVector size(maxage - minage + 1, LgrpDiv->numLengthGroups());
411 :     tagAlkeys.resize(areas.Size(), minage, lower, size);
412 :     for (i = 0; i < tagAlkeys.Size(); i++)
413 :     tagAlkeys[i].setToZero();
414 :    
415 :     if (doesmature)
416 :     maturity->setTagged();
417 :     if (doesmove)
418 :     transition->setTagged();
419 :     if (doesstray)
420 :     stray->setTagged();
421 :     }
422 :    
423 :     void Stock::Print(ofstream& outfile) const {
424 :    
425 :     outfile << "\nStock\nName " << this->getName() << "\nLives on internal areas";
426 :    
427 :     int i;
428 :     for (i = 0; i < areas.Size(); i++)
429 :     outfile << sep << areas[i];
430 :     outfile << endl;
431 :    
432 :     outfile << "\ndoes grow " << doesgrow << "\nis eaten " << iseaten
433 :     << "\ndoes eat " << doeseat << "\ndoes migrate " << doesmigrate
434 :     << "\ndoes mature " << doesmature << "\ndoes move " << doesmove
435 :     << "\ndoes renew " << doesrenew << "\ndoes spawn " << doesspawn
436 :     << "\ndoes stray " << doesstray << "\nis tagged " << istagged << endl << endl;
437 :    
438 :     LgrpDiv->Print(outfile);
439 :     initial->Print(outfile);
440 :     naturalm->Print(outfile);
441 :     if (doesmature)
442 :     maturity->Print(outfile);
443 :     if (iseaten)
444 :     prey->Print(outfile);
445 :     if (doeseat)
446 :     predator->Print(outfile);
447 :     if (doesmove)
448 :     transition->Print(outfile);
449 :     if (doesrenew)
450 :     renewal->Print(outfile);
451 :     if (doesgrow)
452 :     grower->Print(outfile);
453 :     if (doesmigrate)
454 :     migration->Print(outfile);
455 :     if (doesspawn)
456 :     spawner->Print(outfile);
457 :     if (doesstray)
458 :     stray->Print(outfile);
459 :    
460 :     outfile << "\nAge length keys\n";
461 :     for (i = 0; i < areas.Size(); i++) {
462 :     outfile << "\tInternal area " << areas[i] << "\n\tNumber\n";
463 :     Alkeys[i].printNumbers(outfile);
464 :     outfile << "\tMean weight\n";
465 :     Alkeys[i].printWeights(outfile);
466 :     }
467 :     }
468 :    
469 :     int Stock::isBirthday(const TimeClass* const TimeInfo) const {
470 :     return (TimeInfo->getStep() == birthdate);
471 :     }
472 :    
473 :     StockPrey* Stock::getPrey() const {
474 :     if (!iseaten)
475 :     handle.logMessage(LOGFAIL, "Error in stock - no prey defined for", this->getName());
476 :     return prey;
477 :     }
478 :    
479 :     Migration* Stock::getMigration() const {
480 :     if (!doesmigrate)
481 :     handle.logMessage(LOGFAIL, "Error in stock - no migration defined for", this->getName());
482 :     return migration;
483 :     }
484 :    
485 :     PopPredator* Stock::getPredator() const {
486 :     if (!doeseat)
487 :     handle.logMessage(LOGFAIL, "Error in stock - no predator defined for", this->getName());
488 :     return predator;
489 :     }
490 :    
491 :     const StockPtrVector& Stock::getMatureStocks() {
492 :     if (!doesmature)
493 :     handle.logMessage(LOGFAIL, "Error in stock - no mature stocks defined for", this->getName());
494 :     return maturity->getMatureStocks();
495 :     }
496 :    
497 :     const StockPtrVector& Stock::getTransitionStocks() {
498 :     if (!doesmove)
499 :     handle.logMessage(LOGFAIL, "Error in stock - no transition stocks defined for", this->getName());
500 :     return transition->getTransitionStocks();
501 :     }
502 :    
503 :     const StockPtrVector& Stock::getStrayStocks() {
504 :     if (!doesstray)
505 :     handle.logMessage(LOGFAIL, "Error in stock - no straying stocks defined for", this->getName());
506 :     return stray->getStrayStocks();
507 :     }
508 :    
509 :     AgeBandMatrix& Stock::getConsumptionALK(int area) {
510 :     // if (!iseaten)
511 :     // handle.logMessage(LOGWARN, "Error in stock - no prey for", this->getName());
512 :     // return prey->getConsumptionALK(area);
513 :     return Alkeys[this->areaNum(area)];
514 :     }

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

Powered By FusionForge