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

Annotation of /trunk/gadget/quotapredator.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download)

1 : agomez 1 #include "quotapredator.h"
2 :     #include "keeper.h"
3 :     #include "prey.h"
4 :     #include "mathfunc.h"
5 :     #include "errorhandler.h"
6 :     #include "gadget.h"
7 :     #include "global.h"
8 :    
9 :     QuotaPredator::QuotaPredator(CommentStream& infile, const char* givenname,
10 :     const IntVector& Areas, const TimeClass* const TimeInfo, Keeper* const keeper, Formula multscaler)
11 :     : LengthPredator(givenname, Areas, keeper, multscaler) {
12 :    
13 :     type = QUOTAPREDATOR;
14 :     keeper->addString("predator");
15 :     keeper->addString(givenname);
16 :     //first read in the suitability parameters
17 :     this->readSuitability(infile, TimeInfo, keeper);
18 :    
19 :     int i, j;
20 :     double tmp;
21 :     char text[MaxStrLength];
22 :     strncpy(text, "", MaxStrLength);
23 :    
24 :     functionnumber = 0;
25 :     functionname = new char[MaxStrLength];
26 :     strncpy(functionname, "", MaxStrLength);
27 :    
28 :     //the next entry in the input file should be the name of the quota function
29 :     infile >> functionname >> ws;
30 :     if (strcasecmp(functionname, "simple") == 0)
31 :     functionnumber = 1;
32 :     else if (strcasecmp(functionname, "simplesum") == 0)
33 :     functionnumber = 2;
34 :     else if (strcasecmp(functionname, "simpleselect") == 0)
35 :     functionnumber = 3;
36 :     else if (strcasecmp(functionname, "annual") == 0)
37 :     functionnumber = 4;
38 :     else if (strcasecmp(functionname, "annualsum") == 0)
39 :     functionnumber = 5;
40 :     else if (strcasecmp(functionname, "annualselect") == 0)
41 :     functionnumber = 6;
42 :     else
43 :     handle.logFileMessage(LOGFAIL, "\nError in quotapredator - unrecognised function", functionname);
44 :    
45 :     //now read in the stock biomass levels
46 :     infile >> text >> ws;
47 :     if (strcasecmp(text, "biomasslevel") != 0)
48 :     handle.logFileUnexpected(LOGFAIL, "biomasslevel", text);
49 :     while (isdigit(infile.peek()) && !infile.eof()) {
50 :     infile >> tmp >> ws;
51 :     biomasslevel.resize(1, tmp);
52 :     }
53 :    
54 :     if (biomasslevel.Size() == 0)
55 :     handle.logMessage(LOGFAIL, "Error in quotapredator - missing quota data");
56 :    
57 :     //check the data to make sure that it is continuous and positive
58 :     if (biomasslevel[0] < 0.0)
59 :     handle.logFileMessage(LOGFAIL, "biomass levels must be positive");
60 :     if (biomasslevel.Size() != 1)
61 :     for (i = 1; i < biomasslevel.Size(); i++)
62 :     if ((biomasslevel[i] - biomasslevel[i - 1]) < verysmall)
63 :     handle.logFileMessage(LOGFAIL, "biomass levels must be strictly increasing");
64 :    
65 :     //finally read in the quota parameters
66 :     infile >> text >> ws;
67 :     if (strcasecmp(text, "quotalevel") != 0)
68 :     handle.logFileUnexpected(LOGFAIL, "quotalevel", text);
69 :    
70 :     infile >> ws;
71 :     keeper->addString("quota");
72 :     quotalevel.resize(biomasslevel.Size() + 1, keeper);
73 :     for (i = 0; i < quotalevel.Size(); i++)
74 :     if (!(infile >> quotalevel[i]))
75 :     handle.logFileMessage(LOGFAIL, "invalid format of quotalevel vector");
76 :     quotalevel.Inform(keeper);
77 :     keeper->clearLast();
78 :    
79 :     calcquota.resize(preference.Size(), 0.0);
80 :     selectprey.resize(preference.Size(), 0); //default to not using the prey
81 :    
82 :     //if we need to select the stocks used to calculate the fishing quota
83 :     infile >> text >> ws;
84 :     if ((functionnumber == 3) || (functionnumber == 6)) {
85 :     if (strcasecmp(text, "selectstocks") != 0)
86 :     handle.logFileUnexpected(LOGFAIL, "selectstocks", text);
87 :    
88 :     i = 0;
89 :     CharPtrVector preynames;
90 :     infile >> text >> ws;
91 :     while (!infile.eof() && (strcasecmp(text, "amount") != 0)) {
92 :     preynames.resize(new char[strlen(text) + 1]);
93 :     strcpy(preynames[i++], text);
94 :     infile >> text >> ws;
95 :     }
96 :    
97 :     if (preynames.Size() == 0)
98 :     handle.logFileMessage(LOGFAIL, "no stocks selected to calculate quota");
99 :    
100 :     //check that the prey names are unique
101 :     for (i = 0; i < preynames.Size(); i++)
102 :     for (j = 0; j < preynames.Size(); j++)
103 :     if ((strcasecmp(preynames[i], preynames[j]) == 0) && (i != j))
104 :     handle.logFileMessage(LOGFAIL, "repeated stock", preynames[i]);
105 :    
106 :     //work out which preys are needed to calculate the fishing quota
107 :     int check = 0;
108 :     for (i = 0; i < preynames.Size(); i++) {
109 :     check = 0;
110 :     for (j = 0; j < preference.Size(); j++) {
111 :     if (strcasecmp(preynames[i], this->getPreyName(j)) == 0) {
112 :     selectprey[j] = 1;
113 :     check = 1;
114 :     }
115 :     }
116 :     if (check == 0)
117 :     handle.logFileMessage(LOGFAIL, "failed to match stock", preynames[i]);
118 :     }
119 :    
120 :     //finally free the memory since the prey names are no longer needed
121 :     for (i = 0; i < preynames.Size(); i++)
122 :     delete[] preynames[i];
123 :     }
124 :    
125 :     if (strcasecmp(text, "amount") != 0)
126 :     handle.logFileUnexpected(LOGFAIL, "amount", text);
127 :    
128 :     keeper->clearLast();
129 :     keeper->clearLast();
130 :     }
131 :    
132 :     QuotaPredator::~QuotaPredator() {
133 :     delete[] functionname;
134 :     }
135 :    
136 :     void QuotaPredator::Eat(int area, const AreaClass* const Area, const TimeClass* const TimeInfo) {
137 :    
138 :     int inarea = this->areaNum(area);
139 :     int prey, preyl;
140 :     int predl = 0; //JMB there is only ever one length group ...
141 :     double tmp, bio;
142 :    
143 :     totalcons[inarea][predl] = 0.0;
144 :     tmp = prednumber[inarea][predl].N * multi * TimeInfo->getTimeStepSize() / TimeInfo->numSubSteps();
145 :     if (isZero(tmp)) { //JMB no predation takes place on this timestep
146 :     for (prey = 0; prey < this->numPreys(); prey++)
147 :     (*predratio[inarea])[prey][predl] = 0.0;
148 :     return;
149 :     }
150 :    
151 :     switch (functionnumber) {
152 :     case 1:
153 :     //Calculate the fishing level based on the available biomass of each stock
154 :     for (prey = 0; prey < this->numPreys(); prey++) {
155 :     if (this->getPrey(prey)->isPreyArea(area)) {
156 :     (*predratio[inarea])[prey][predl] = tmp * calcQuota(this->getPrey(prey)->getTotalBiomass(area));
157 :     if ((*predratio[inarea])[prey][predl] > 10.0) //JMB arbitrary value here ...
158 :     handle.logMessage(LOGWARN, "Warning in quotapredator - excessive consumption required");
159 :    
160 :     } else
161 :     (*predratio[inarea])[prey][predl] = 0.0;
162 :     }
163 :     break;
164 :    
165 :     case 2:
166 :     //Calculate the fishing level based on the available biomass of all stocks
167 :     bio = 0.0;
168 :     for (prey = 0; prey < this->numPreys(); prey++)
169 :     if (this->getPrey(prey)->isPreyArea(area))
170 :     bio += this->getPrey(prey)->getTotalBiomass(area);
171 :    
172 :     tmp *= calcQuota(bio);
173 :     if (tmp > 10.0) //JMB arbitrary value here ...
174 :     handle.logMessage(LOGWARN, "Warning in quotapredator - excessive consumption required");
175 :     for (prey = 0; prey < this->numPreys(); prey++) {
176 :     if (this->getPrey(prey)->isPreyArea(area))
177 :     (*predratio[inarea])[prey][predl] = tmp;
178 :     else
179 :     (*predratio[inarea])[prey][predl] = 0.0;
180 :     }
181 :     break;
182 :    
183 :     case 3:
184 :     //Calculate the fishing level based on the available biomass of the selected stocks
185 :     bio = 0.0;
186 :     for (prey = 0; prey < this->numPreys(); prey++)
187 :     if ((this->getPrey(prey)->isPreyArea(area)) && (selectprey[prey]))
188 :     bio += this->getPrey(prey)->getTotalBiomass(area);
189 :    
190 :     tmp *= calcQuota(bio);
191 :     if (tmp > 10.0) //JMB arbitrary value here ...
192 :     handle.logMessage(LOGWARN, "Warning in quotapredator - excessive consumption required");
193 :     for (prey = 0; prey < this->numPreys(); prey++) {
194 :     if (this->getPrey(prey)->isPreyArea(area))
195 :     (*predratio[inarea])[prey][predl] = tmp;
196 :     else
197 :     (*predratio[inarea])[prey][predl] = 0.0;
198 :     }
199 :     break;
200 :    
201 :     case 4:
202 :     //Calculate the annual fishing level based on the available biomass of each stock
203 :     for (prey = 0; prey < this->numPreys(); prey++) {
204 :     if (this->getPrey(prey)->isPreyArea(area)) {
205 :     if (TimeInfo->getStep() == 1)
206 :     calcquota[prey] = calcQuota(this->getPrey(prey)->getTotalBiomass(area));
207 :    
208 :     //JMB this doesnt work if there is more than one stock since the value of quota will have changed
209 :     (*predratio[inarea])[prey][predl] = calcquota[prey] * tmp;
210 :     if ((*predratio[inarea])[prey][predl] > 10.0) //JMB arbitrary value here ...
211 :     handle.logMessage(LOGWARN, "Warning in quotapredator - excessive consumption required");
212 :    
213 :     } else
214 :     (*predratio[inarea])[prey][predl] = 0.0;
215 :     }
216 :     break;
217 :    
218 :     case 5:
219 :     //Calculate the annual fishing level based on the available biomass of all stocks
220 :     if (TimeInfo->getStep() == 1) {
221 :     bio = 0.0;
222 :     for (prey = 0; prey < this->numPreys(); prey++)
223 :     if (this->getPrey(prey)->isPreyArea(area))
224 :     bio += this->getPrey(prey)->getTotalBiomass(area);
225 :    
226 :     calcquota[0] = calcQuota(bio); //JMB all quotas are the same
227 :     }
228 :    
229 :     tmp *= calcquota[0];
230 :     if (tmp > 10.0) //JMB arbitrary value here ...
231 :     handle.logMessage(LOGWARN, "Warning in quotapredator - excessive consumption required");
232 :     for (prey = 0; prey < this->numPreys(); prey++) {
233 :     if (this->getPrey(prey)->isPreyArea(area))
234 :     (*predratio[inarea])[prey][predl] = tmp;
235 :     else
236 :     (*predratio[inarea])[prey][predl] = 0.0;
237 :     }
238 :     break;
239 :    
240 :     case 6:
241 :     //Calculate the annual fishing level based on the available biomass of the selected stocks
242 :     if (TimeInfo->getStep() == 1) {
243 :     bio = 0.0;
244 :     for (prey = 0; prey < this->numPreys(); prey++)
245 :     if ((this->getPrey(prey)->isPreyArea(area)) && (selectprey[prey]))
246 :     bio += this->getPrey(prey)->getTotalBiomass(area);
247 :    
248 :     calcquota[0] = calcQuota(bio); //JMB all quotas are the same
249 :     }
250 :    
251 :     tmp *= calcquota[0];
252 :     if (tmp > 10.0) //JMB arbitrary value here ...
253 :     handle.logMessage(LOGWARN, "Warning in quotapredator - excessive consumption required");
254 :     for (prey = 0; prey < this->numPreys(); prey++) {
255 :     if (this->getPrey(prey)->isPreyArea(area))
256 :     (*predratio[inarea])[prey][predl] = tmp;
257 :     else
258 :     (*predratio[inarea])[prey][predl] = 0.0;
259 :     }
260 :     break;
261 :    
262 :     default:
263 :     handle.logMessage(LOGWARN, "Warning in quotapredator - unrecognised function", functionname);
264 :     break;
265 :     }
266 :    
267 :     for (prey = 0; prey < this->numPreys(); prey++) {
268 :     if (isZero((*predratio[inarea])[prey][predl])) {
269 :     for (preyl = 0; preyl < (*cons[inarea][prey])[predl].Size(); preyl++)
270 :     (*cons[inarea][prey])[predl][preyl] = 0.0;
271 :    
272 :     } else {
273 :     for (preyl = 0; preyl < (*cons[inarea][prey])[predl].Size(); preyl++) {
274 :     (*cons[inarea][prey])[predl][preyl] = (*predratio[inarea])[prey][predl] *
275 :     this->getSuitability(prey)[predl][preyl] * this->getPrey(prey)->getBiomass(area, preyl);
276 :     totalcons[inarea][predl] += (*cons[inarea][prey])[predl][preyl];
277 :     }
278 :     //inform the preys of the consumption
279 :     this->getPrey(prey)->addBiomassConsumption(area, (*cons[inarea][prey])[predl]);
280 :     }
281 :     }
282 :     }
283 :    
284 :     void QuotaPredator::adjustConsumption(int area, const TimeClass* const TimeInfo) {
285 :     int prey, preyl;
286 :     int inarea = this->areaNum(area);
287 :     int predl = 0; //JMB there is only ever one length group ...
288 :     overcons[inarea][predl] = 0.0;
289 :    
290 :     if (isZero(totalcons[inarea][predl])) //JMB no predation takes place on this timestep
291 :     return;
292 :    
293 :     double maxRatio, tmp;
294 :     maxRatio = TimeInfo->getMaxRatioConsumed();
295 :    
296 :     for (prey = 0; prey < this->numPreys(); prey++) {
297 :     if (this->getPrey(prey)->isOverConsumption(area)) {
298 :     hasoverconsumption[inarea] = 1;
299 :     DoubleVector ratio = this->getPrey(prey)->getRatio(area);
300 :     for (preyl = 0; preyl < (*cons[inarea][prey])[predl].Size(); preyl++) {
301 :     if (ratio[preyl] > maxRatio) {
302 :     tmp = maxRatio / ratio[preyl];
303 :     overcons[inarea][predl] += (1.0 - tmp) * (*cons[inarea][prey])[predl][preyl];
304 :     (*cons[inarea][prey])[predl][preyl] *= tmp;
305 :     (*usesuit[inarea][prey])[predl][preyl] *= tmp;
306 :     }
307 :     }
308 :     }
309 :     }
310 :    
311 :     if (hasoverconsumption[inarea]) {
312 :     totalcons[inarea][predl] -= overcons[inarea][predl];
313 :     overconsumption[inarea][predl] += overcons[inarea][predl];
314 :     }
315 :    
316 :     totalconsumption[inarea][predl] += totalcons[inarea][predl];
317 :     for (prey = 0; prey < this->numPreys(); prey++)
318 :     if (this->getPrey(prey)->isPreyArea(area))
319 :     for (preyl = 0; preyl < (*cons[inarea][prey])[predl].Size(); preyl++)
320 :     (*consumption[inarea][prey])[predl][preyl] += (*cons[inarea][prey])[predl][preyl];
321 :     }
322 :    
323 :     void QuotaPredator::Print(ofstream& outfile) const {
324 :     outfile << "QuotaPredator\n";
325 :     PopPredator::Print(outfile);
326 :     }
327 :    
328 :     double QuotaPredator::calcQuota(double biomass) {
329 :     int i;
330 :     double quota = 0.0;
331 :     if (biomass < biomasslevel[0]) {
332 :     quota = quotalevel[0];
333 :     } else if (biomass > biomasslevel[biomasslevel.Size() - 1]) {
334 :     quota = quotalevel[biomasslevel.Size()];
335 :     } else {
336 :     for (i = 1; i < biomasslevel.Size(); i++)
337 :     if ((biomasslevel[i - 1] < biomass) && (biomass < biomasslevel[i]))
338 :     quota = quotalevel[i];
339 :     }
340 :    
341 :     if (quota < 0.0)
342 :     handle.logMessage(LOGWARN, "Warning in quotapredator - negative quota", quota);
343 :     return quota;
344 :     }
345 :    

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

Powered By FusionForge