1 : |
agomez |
1 |
#include "initialcond.h" |
2 : |
|
|
#include "errorhandler.h" |
3 : |
|
|
#include "readfunc.h" |
4 : |
|
|
#include "readword.h" |
5 : |
|
|
#include "mathfunc.h" |
6 : |
|
|
#include "gadget.h" |
7 : |
|
|
#include "global.h" |
8 : |
|
|
|
9 : |
|
|
void InitialCond::readNormalConditionData(CommentStream& infile, Keeper* const keeper,
|
10 : |
|
|
int numage, int minage, const AreaClass* const Area) {
|
11 : |
|
|
|
12 : |
|
|
int noareas = areas.Size();
|
13 : |
|
|
int i, age, area, ageid, areaid, tmparea, keepdata, count, reject;
|
14 : |
|
|
char c;
|
15 : |
|
|
|
16 : |
|
|
//Resize the matrices to hold the data
|
17 : |
|
|
areaFactor.AddRows(noareas, numage, 0.0);
|
18 : |
|
|
ageFactor.AddRows(noareas, numage, 0.0);
|
19 : |
|
|
meanLength.AddRows(noareas, numage, 0.0);
|
20 : |
|
|
sdevLength.AddRows(noareas, numage, 0.0);
|
21 : |
|
|
relCond.AddRows(noareas, numage, 0.0);
|
22 : |
|
|
|
23 : |
|
|
//Find the start of the data in the following format
|
24 : |
|
|
//age - area - agedist - areadist - meanlen - standdev - relcond
|
25 : |
|
|
infile >> ws;
|
26 : |
|
|
if (countColumns(infile) != 7)
|
27 : |
|
|
handle.logFileMessage(LOGFAIL, "wrong number of columns in inputfile - should be 7");
|
28 : |
|
|
|
29 : |
|
|
area = age = ageid = count = reject = 0;
|
30 : |
|
|
keeper->addString("meandata");
|
31 : |
|
|
while (!infile.eof()) {
|
32 : |
|
|
//crude check to see if something has gone wrong and avoid infinite loops
|
33 : |
|
|
if (!(isdigit(infile.peek())))
|
34 : |
|
|
handle.logFileMessage(LOGFAIL, "failed to read data from file");
|
35 : |
|
|
|
36 : |
|
|
keepdata = 1;
|
37 : |
|
|
infile >> age >> area >> ws;
|
38 : |
|
|
|
39 : |
|
|
//crude age data check - perhaps there should be a better check?
|
40 : |
|
|
if ((age < minage) || (age >= (numage + minage)))
|
41 : |
|
|
keepdata = 0;
|
42 : |
|
|
else
|
43 : |
|
|
ageid = age - minage;
|
44 : |
|
|
|
45 : |
|
|
//crude area data check
|
46 : |
|
|
areaid = -1;
|
47 : |
|
|
tmparea = Area->getInnerArea(area);
|
48 : |
|
|
for (i = 0; i < noareas; i++)
|
49 : |
|
|
if (areas[i] == tmparea)
|
50 : |
|
|
areaid = i;
|
51 : |
|
|
|
52 : |
|
|
if (areaid == -1)
|
53 : |
|
|
keepdata = 0;
|
54 : |
|
|
|
55 : |
|
|
if (keepdata == 1) {
|
56 : |
|
|
//initial data is required, so store it
|
57 : |
|
|
count++;
|
58 : |
|
|
infile >> ageFactor[areaid][ageid] >> ws;
|
59 : |
|
|
infile >> areaFactor[areaid][ageid] >> ws;
|
60 : |
|
|
infile >> meanLength[areaid][ageid] >> ws;
|
61 : |
|
|
infile >> sdevLength[areaid][ageid] >> ws;
|
62 : |
|
|
infile >> relCond[areaid][ageid] >> ws;
|
63 : |
|
|
|
64 : |
|
|
} else { //initial data not required - skip rest of line
|
65 : |
|
|
reject++;
|
66 : |
|
|
infile.get(c);
|
67 : |
|
|
while (c != '\n' && !infile.eof())
|
68 : |
|
|
infile.get(c);
|
69 : |
|
|
infile >> ws;
|
70 : |
|
|
}
|
71 : |
|
|
}
|
72 : |
|
|
|
73 : |
|
|
if (count == 0)
|
74 : |
|
|
handle.logMessage(LOGWARN, "Warning in initial conditions - found no data in the data file");
|
75 : |
|
|
else if (count < (noareas * numage))
|
76 : |
|
|
handle.logMessage(LOGWARN, "Warning in initial conditions - missing entries from data file");
|
77 : |
|
|
else if (count > (noareas * numage))
|
78 : |
|
|
handle.logMessage(LOGWARN, "Warning in initial conditions - repeated entries in data file");
|
79 : |
|
|
|
80 : |
|
|
if (reject != 0)
|
81 : |
|
|
handle.logMessage(LOGMESSAGE, "Discarded invalid initial conditions data - number of invalid entries", reject);
|
82 : |
|
|
|
83 : |
|
|
handle.logMessage(LOGMESSAGE, "Read initial conditions data file - number of entries", count);
|
84 : |
|
|
areaFactor.Inform(keeper);
|
85 : |
|
|
ageFactor.Inform(keeper);
|
86 : |
|
|
meanLength.Inform(keeper);
|
87 : |
|
|
sdevLength.Inform(keeper);
|
88 : |
|
|
relCond.Inform(keeper);
|
89 : |
|
|
keeper->clearLast();
|
90 : |
|
|
}
|
91 : |
|
|
|
92 : |
|
|
void InitialCond::readNormalParameterData(CommentStream& infile, Keeper* const keeper,
|
93 : |
|
|
int numage, int minage, const AreaClass* const Area) {
|
94 : |
|
|
|
95 : |
|
|
int noareas = areas.Size();
|
96 : |
|
|
int i, age, area, ageid, areaid, tmparea, keepdata, count, reject;
|
97 : |
|
|
char c;
|
98 : |
|
|
|
99 : |
|
|
//Resize the matrices to hold the data
|
100 : |
|
|
areaFactor.AddRows(noareas, numage, 0.0);
|
101 : |
|
|
ageFactor.AddRows(noareas, numage, 0.0);
|
102 : |
|
|
meanLength.AddRows(noareas, numage, 0.0);
|
103 : |
|
|
sdevLength.AddRows(noareas, numage, 0.0);
|
104 : |
|
|
alpha.AddRows(noareas, numage, 0.0);
|
105 : |
|
|
beta.AddRows(noareas, numage, 0.0);
|
106 : |
|
|
|
107 : |
|
|
//Find the start of the data in the following format
|
108 : |
|
|
//age - area - agedist - areadist - meanlen - standdev - alpha - beta
|
109 : |
|
|
infile >> ws;
|
110 : |
|
|
if (countColumns(infile) != 8)
|
111 : |
|
|
handle.logFileMessage(LOGFAIL, "wrong number of columns in inputfile - should be 8");
|
112 : |
|
|
|
113 : |
|
|
area = age = ageid = count = reject = 0;
|
114 : |
|
|
keeper->addString("meandata");
|
115 : |
|
|
while (!infile.eof()) {
|
116 : |
|
|
//crude check to see if something has gone wrong and avoid infinite loops
|
117 : |
|
|
if (!(isdigit(infile.peek())))
|
118 : |
|
|
handle.logFileMessage(LOGFAIL, "failed to read data from file");
|
119 : |
|
|
|
120 : |
|
|
keepdata = 1;
|
121 : |
|
|
infile >> age >> area >> ws;
|
122 : |
|
|
|
123 : |
|
|
//crude age data check - perhaps there should be a better check?
|
124 : |
|
|
if ((age < minage) || (age >= (numage + minage)))
|
125 : |
|
|
keepdata = 0;
|
126 : |
|
|
else
|
127 : |
|
|
ageid = age - minage;
|
128 : |
|
|
|
129 : |
|
|
//crude area data check
|
130 : |
|
|
areaid = -1;
|
131 : |
|
|
tmparea = Area->getInnerArea(area);
|
132 : |
|
|
for (i = 0; i < noareas; i++)
|
133 : |
|
|
if (areas[i] == tmparea)
|
134 : |
|
|
areaid = i;
|
135 : |
|
|
|
136 : |
|
|
if (areaid == -1)
|
137 : |
|
|
keepdata = 0;
|
138 : |
|
|
|
139 : |
|
|
if (keepdata == 1) {
|
140 : |
|
|
//initial data is required, so store it
|
141 : |
|
|
count++;
|
142 : |
|
|
infile >> ageFactor[areaid][ageid] >> ws;
|
143 : |
|
|
infile >> areaFactor[areaid][ageid] >> ws;
|
144 : |
|
|
infile >> meanLength[areaid][ageid] >> ws;
|
145 : |
|
|
infile >> sdevLength[areaid][ageid] >> ws;
|
146 : |
|
|
infile >> alpha[areaid][ageid] >> ws;
|
147 : |
|
|
infile >> beta[areaid][ageid] >> ws;
|
148 : |
|
|
|
149 : |
|
|
} else { //initial data not required - skip rest of line
|
150 : |
|
|
reject++;
|
151 : |
|
|
infile.get(c);
|
152 : |
|
|
while (c != '\n' && !infile.eof())
|
153 : |
|
|
infile.get(c);
|
154 : |
|
|
infile >> ws;
|
155 : |
|
|
}
|
156 : |
|
|
}
|
157 : |
|
|
|
158 : |
|
|
if (count == 0)
|
159 : |
|
|
handle.logMessage(LOGWARN, "Warning in initial conditions - found no data in the data file");
|
160 : |
|
|
else if (count < (noareas * numage))
|
161 : |
|
|
handle.logMessage(LOGWARN, "Warning in initial conditions - missing entries from data file");
|
162 : |
|
|
else if (count > (noareas * numage))
|
163 : |
|
|
handle.logMessage(LOGWARN, "Warning in initial conditions - repeated entries in data file");
|
164 : |
|
|
|
165 : |
|
|
if (reject != 0)
|
166 : |
|
|
handle.logMessage(LOGMESSAGE, "Discarded invalid initial conditions data - number of invalid entries", reject);
|
167 : |
|
|
|
168 : |
|
|
handle.logMessage(LOGMESSAGE, "Read initial conditions data file - number of entries", count);
|
169 : |
|
|
areaFactor.Inform(keeper);
|
170 : |
|
|
ageFactor.Inform(keeper);
|
171 : |
|
|
meanLength.Inform(keeper);
|
172 : |
|
|
sdevLength.Inform(keeper);
|
173 : |
|
|
alpha.Inform(keeper);
|
174 : |
|
|
beta.Inform(keeper);
|
175 : |
|
|
keeper->clearLast();
|
176 : |
|
|
}
|
177 : |
|
|
|
178 : |
|
|
void InitialCond::readNumberData(CommentStream& infile, Keeper* const keeper,
|
179 : |
|
|
int numage, int minage, const AreaClass* const Area) {
|
180 : |
|
|
|
181 : |
|
|
int i, age, area, tmparea, count, reject;
|
182 : |
|
|
int keepdata, areaid, lengthid;
|
183 : |
|
|
double length;
|
184 : |
|
|
char c;
|
185 : |
|
|
int noareas = areas.Size();
|
186 : |
|
|
int numlen = LgrpDiv->numLengthGroups();
|
187 : |
|
|
|
188 : |
|
|
//initialise things
|
189 : |
|
|
for (areaid = 0; areaid < noareas; areaid++) {
|
190 : |
|
|
initialNumber.resize(new FormulaMatrix(numage, numlen, 0.0));
|
191 : |
|
|
initialPop[areaid].setToZero();
|
192 : |
|
|
}
|
193 : |
|
|
|
194 : |
|
|
//Find the start of the data in the following format
|
195 : |
|
|
//area - age - meanlength - number - weight
|
196 : |
|
|
infile >> ws;
|
197 : |
|
|
if (countColumns(infile) != 5)
|
198 : |
|
|
handle.logFileMessage(LOGFAIL, "wrong number of columns in inputfile - should be 5");
|
199 : |
|
|
|
200 : |
|
|
area = age = count = reject = 0;
|
201 : |
|
|
keeper->addString("numberdata");
|
202 : |
|
|
while (!infile.eof()) {
|
203 : |
|
|
//crude check to see if something has gone wrong and avoid infinite loops
|
204 : |
|
|
if (!(isdigit(infile.peek())))
|
205 : |
|
|
handle.logFileMessage(LOGFAIL, "failed to read data from file");
|
206 : |
|
|
|
207 : |
|
|
keepdata = 1;
|
208 : |
|
|
infile >> area >> age >> length >> ws;
|
209 : |
|
|
|
210 : |
|
|
//crude area data check
|
211 : |
|
|
areaid = -1;
|
212 : |
|
|
tmparea = Area->getInnerArea(area);
|
213 : |
|
|
for (i = 0; i < noareas; i++)
|
214 : |
|
|
if (areas[i] == tmparea)
|
215 : |
|
|
areaid = i;
|
216 : |
|
|
|
217 : |
|
|
if (areaid == -1)
|
218 : |
|
|
keepdata = 0;
|
219 : |
|
|
|
220 : |
|
|
//crude age data check - perhaps there should be a better check?
|
221 : |
|
|
if ((age < minage) || (age >= (numage + minage)))
|
222 : |
|
|
keepdata = 0;
|
223 : |
|
|
|
224 : |
|
|
//crude length data check
|
225 : |
|
|
lengthid = -1;
|
226 : |
|
|
for (i = 0; i < numlen; i++)
|
227 : |
|
|
if (isEqual(length, LgrpDiv->minLength(i)))
|
228 : |
|
|
lengthid = i;
|
229 : |
|
|
|
230 : |
|
|
//OK the length doesnt match a minimum length so find the length group it is in
|
231 : |
|
|
if ((lengthid == -1) && (length > LgrpDiv->minLength()) && (length < LgrpDiv->maxLength())) {
|
232 : |
|
|
for (i = 1; i < numlen; i++) {
|
233 : |
|
|
if (length < LgrpDiv->minLength(i)) {
|
234 : |
|
|
lengthid = i - 1;
|
235 : |
|
|
break;
|
236 : |
|
|
}
|
237 : |
|
|
}
|
238 : |
|
|
if (lengthid == -1)
|
239 : |
|
|
lengthid = numlen - 1; //then this must be the last length group
|
240 : |
|
|
}
|
241 : |
|
|
|
242 : |
|
|
if (lengthid == -1)
|
243 : |
|
|
keepdata = 0;
|
244 : |
|
|
|
245 : |
|
|
if (keepdata == 1) {
|
246 : |
|
|
//initial data is required, so store it
|
247 : |
|
|
infile >> (*initialNumber[areaid])[age - minage][lengthid] >> ws;
|
248 : |
|
|
infile >> initialPop[areaid][age][lengthid].W >> ws;
|
249 : |
|
|
count++;
|
250 : |
|
|
|
251 : |
|
|
} else { //initial data not required - skip rest of line
|
252 : |
|
|
reject++;
|
253 : |
|
|
infile.get(c);
|
254 : |
|
|
while (c != '\n' && !infile.eof())
|
255 : |
|
|
infile.get(c);
|
256 : |
|
|
infile >> ws;
|
257 : |
|
|
}
|
258 : |
|
|
}
|
259 : |
|
|
|
260 : |
|
|
for (i = 0; i < initialNumber.Size(); i++)
|
261 : |
|
|
(*initialNumber[i]).Inform(keeper);
|
262 : |
|
|
|
263 : |
|
|
if (count == 0)
|
264 : |
|
|
handle.logMessage(LOGWARN, "Warning in initial conditions - found no data in the data file");
|
265 : |
|
|
else if (count < (noareas * numage * numlen))
|
266 : |
|
|
handle.logMessage(LOGWARN, "Warning in initial conditions - missing entries from data file");
|
267 : |
|
|
else if (count > (noareas * numage * numlen))
|
268 : |
|
|
handle.logMessage(LOGWARN, "Warning in initial conditions - repeated entries in data file");
|
269 : |
|
|
|
270 : |
|
|
if (reject != 0)
|
271 : |
|
|
handle.logMessage(LOGMESSAGE, "Discarded invalid initial conditions data - number of invalid entries", reject);
|
272 : |
|
|
|
273 : |
|
|
handle.logMessage(LOGMESSAGE, "Read initial conditions data file - number of entries", count);
|
274 : |
|
|
keeper->clearLast();
|
275 : |
|
|
}
|
276 : |
|
|
|
277 : |
|
|
InitialCond::InitialCond(CommentStream& infile, const IntVector& Areas,
|
278 : |
|
|
Keeper* const keeper, const char* refWeightFile,
|
279 : |
|
|
const char* givenname, const AreaClass* const Area, double DL)
|
280 : |
|
|
: HasName(givenname), LivesOnAreas(Areas), LgrpDiv(0), CI(0) {
|
281 : |
|
|
|
282 : |
|
|
ifstream subfile;
|
283 : |
|
|
CommentStream subcomment(subfile);
|
284 : |
|
|
char text[MaxStrLength];
|
285 : |
|
|
strncpy(text, "", MaxStrLength);
|
286 : |
|
|
|
287 : |
|
|
int i, j, k;
|
288 : |
|
|
int minage, maxage;
|
289 : |
|
|
char c;
|
290 : |
|
|
double minlength, maxlength, dl;
|
291 : |
|
|
|
292 : |
|
|
keeper->addString("initialcond");
|
293 : |
|
|
|
294 : |
|
|
infile >> ws;
|
295 : |
|
|
c = infile.peek();
|
296 : |
|
|
if ((c == 'n') || (c == 'N'))
|
297 : |
|
|
infile >> text >> ws; //read in the word 'numbers' if it exists
|
298 : |
|
|
|
299 : |
|
|
readWordAndVariable(infile, "minage", minage);
|
300 : |
|
|
readWordAndVariable(infile, "maxage", maxage);
|
301 : |
|
|
readWordAndVariable(infile, "minlength", minlength);
|
302 : |
|
|
readWordAndVariable(infile, "maxlength", maxlength);
|
303 : |
|
|
int numage = maxage - minage + 1;
|
304 : |
|
|
|
305 : |
|
|
//JMB - changed to make the reading of dl optional
|
306 : |
|
|
//if it isnt specifed here, it will default to the dl value of the stock
|
307 : |
|
|
infile >> ws;
|
308 : |
|
|
c = infile.peek();
|
309 : |
|
|
if ((c == 'd') || (c == 'D'))
|
310 : |
|
|
readWordAndVariable(infile, "dl", dl);
|
311 : |
|
|
else
|
312 : |
|
|
dl = DL;
|
313 : |
|
|
|
314 : |
|
|
LgrpDiv = new LengthGroupDivision(minlength, maxlength, dl);
|
315 : |
|
|
if (LgrpDiv->Error())
|
316 : |
|
|
handle.logMessage(LOGFAIL, "Error in initial conditions - failed to create length group");
|
317 : |
|
|
|
318 : |
|
|
//read the standard deviation multiplier - default to 1.0
|
319 : |
|
|
keeper->addString("sdevmultiplier");
|
320 : |
|
|
infile >> ws;
|
321 : |
|
|
c = infile.peek();
|
322 : |
|
|
if ((c == 's') || (c == 'S'))
|
323 : |
|
|
readWordAndVariable(infile, "sdev", sdevMult);
|
324 : |
|
|
else
|
325 : |
|
|
sdevMult.setValue(1.0);
|
326 : |
|
|
sdevMult.Inform(keeper);
|
327 : |
|
|
keeper->clearLast();
|
328 : |
|
|
|
329 : |
|
|
//create the initialPop object of the correct size
|
330 : |
|
|
PopInfo tmppop;
|
331 : |
|
|
tmppop.N = 1.0;
|
332 : |
|
|
PopInfoMatrix popmatrix(numage, LgrpDiv->numLengthGroups(), tmppop);
|
333 : |
|
|
initialPop.resize(areas.Size(), minage, 0, popmatrix);
|
334 : |
|
|
|
335 : |
|
|
infile >> text >> ws;
|
336 : |
|
|
if ((strcasecmp(text, "initstockfile") == 0) || (strcasecmp(text, "normalcondfile") == 0)) {
|
337 : |
|
|
//read initial data in mean length format, using the reference weight file
|
338 : |
|
|
readoption = 0;
|
339 : |
|
|
infile >> text >> ws;
|
340 : |
|
|
subfile.open(text, ios::in);
|
341 : |
|
|
handle.checkIfFailure(subfile, text);
|
342 : |
|
|
handle.Open(text);
|
343 : |
|
|
this->readNormalConditionData(subcomment, keeper, numage, minage, Area);
|
344 : |
|
|
handle.Close();
|
345 : |
|
|
subfile.close();
|
346 : |
|
|
subfile.clear();
|
347 : |
|
|
|
348 : |
|
|
//read information on reference weights.
|
349 : |
|
|
DoubleMatrix tmpRefW;
|
350 : |
|
|
keeper->addString("referenceweights");
|
351 : |
|
|
subfile.open(refWeightFile, ios::in);
|
352 : |
|
|
handle.checkIfFailure(subfile, refWeightFile);
|
353 : |
|
|
handle.Open(refWeightFile);
|
354 : |
|
|
readRefWeights(subcomment, tmpRefW);
|
355 : |
|
|
handle.Close();
|
356 : |
|
|
subfile.close();
|
357 : |
|
|
subfile.clear();
|
358 : |
|
|
|
359 : |
|
|
//Interpolate the reference weights. First there are some error checks.
|
360 : |
|
|
if (LgrpDiv->meanLength(0) < tmpRefW[0][0] ||
|
361 : |
|
|
LgrpDiv->meanLength(LgrpDiv->numLengthGroups() - 1) > tmpRefW[tmpRefW.Nrow() - 1][0])
|
362 : |
|
|
handle.logFileMessage(LOGFAIL, "lengths for reference weights must span the range of initial condition lengths");
|
363 : |
|
|
|
364 : |
|
|
//Aggregate the reference weight data to be the same format
|
365 : |
|
|
double ratio, tmplen;
|
366 : |
|
|
refWeight.resize(LgrpDiv->numLengthGroups(), 0.0);
|
367 : |
|
|
int pos = 0;
|
368 : |
|
|
for (j = 0; j < LgrpDiv->numLengthGroups(); j++) {
|
369 : |
|
|
tmplen = LgrpDiv->meanLength(j);
|
370 : |
|
|
for (i = pos; i < tmpRefW.Nrow() - 1; i++) {
|
371 : |
|
|
if (((tmplen > tmpRefW[i][0]) || (isEqual(tmplen, tmpRefW[i][0]))) &&
|
372 : |
|
|
((tmplen < tmpRefW[i + 1][0]) || (isEqual(tmplen, tmpRefW[i + 1][0])))) {
|
373 : |
|
|
|
374 : |
|
|
ratio = (tmplen - tmpRefW[i][0]) / (tmpRefW[i + 1][0] - tmpRefW[i][0]);
|
375 : |
|
|
refWeight[j] = tmpRefW[i][1] + ratio * (tmpRefW[i + 1][1] - tmpRefW[i][1]);
|
376 : |
|
|
pos = i;
|
377 : |
|
|
}
|
378 : |
|
|
}
|
379 : |
|
|
}
|
380 : |
|
|
keeper->clearLast();
|
381 : |
|
|
|
382 : |
|
|
} else if ((strcasecmp(text, "normalparamfile") == 0)) {
|
383 : |
|
|
//read initial data in mean length format, using a length weight relationship
|
384 : |
|
|
readoption = 1;
|
385 : |
|
|
infile >> text >> ws;
|
386 : |
|
|
subfile.open(text, ios::in);
|
387 : |
|
|
handle.checkIfFailure(subfile, text);
|
388 : |
|
|
handle.Open(text);
|
389 : |
|
|
this->readNormalParameterData(subcomment, keeper, numage, minage, Area);
|
390 : |
|
|
handle.Close();
|
391 : |
|
|
subfile.close();
|
392 : |
|
|
subfile.clear();
|
393 : |
|
|
|
394 : |
|
|
} else if ((strcasecmp(text, "numberfile") == 0)) {
|
395 : |
|
|
//read initial data in number format
|
396 : |
|
|
readoption = 2;
|
397 : |
|
|
infile >> text >> ws;
|
398 : |
|
|
subfile.open(text, ios::in);
|
399 : |
|
|
handle.checkIfFailure(subfile, text);
|
400 : |
|
|
handle.Open(text);
|
401 : |
|
|
this->readNumberData(subcomment, keeper, numage, minage, Area);
|
402 : |
|
|
handle.Close();
|
403 : |
|
|
subfile.close();
|
404 : |
|
|
subfile.clear();
|
405 : |
|
|
|
406 : |
|
|
} else
|
407 : |
|
|
handle.logFileMessage(LOGFAIL, "unrecognised initial conditions format", text);
|
408 : |
|
|
|
409 : |
|
|
keeper->clearLast();
|
410 : |
|
|
}
|
411 : |
|
|
|
412 : |
|
|
InitialCond::~InitialCond() {
|
413 : |
|
|
int i;
|
414 : |
|
|
delete LgrpDiv;
|
415 : |
|
|
delete CI;
|
416 : |
|
|
for (i = 0; i < initialNumber.Size(); i++)
|
417 : |
|
|
delete initialNumber[i];
|
418 : |
|
|
}
|
419 : |
|
|
|
420 : |
|
|
void InitialCond::setCI(const LengthGroupDivision* const GivenLDiv) {
|
421 : |
|
|
if (!checkLengthGroupStructure(GivenLDiv, LgrpDiv))
|
422 : |
|
|
handle.logMessage(LOGFAIL, "Error in initial conditions - invalid length group structure for stock", this->getName());
|
423 : |
|
|
//check minimum and maximum lengths
|
424 : |
|
|
if (LgrpDiv->minLength() < GivenLDiv->minLength())
|
425 : |
|
|
handle.logMessage(LOGWARN, "Warning in initial conditions - minimum length less than stock length for stock", this->getName());
|
426 : |
|
|
if (LgrpDiv->maxLength() > GivenLDiv->maxLength())
|
427 : |
|
|
handle.logMessage(LOGWARN, "Warning in initial conditions - maximum length greater than stock length for stock"), this->getName();
|
428 : |
|
|
CI = new ConversionIndex(LgrpDiv, GivenLDiv);
|
429 : |
|
|
if (CI->Error())
|
430 : |
|
|
handle.logMessage(LOGFAIL, "Error in initial conditions - error when checking length structure for stock", this->getName());
|
431 : |
|
|
}
|
432 : |
|
|
|
433 : |
|
|
void InitialCond::Print(ofstream& outfile) const {
|
434 : |
|
|
int i, j;
|
435 : |
|
|
|
436 : |
|
|
outfile << "\nInitial conditions\n";
|
437 : |
|
|
for (i = 0; i < areas.Size(); i++) {
|
438 : |
|
|
outfile << "\tInternal area " << areas[i] << endl;
|
439 : |
|
|
initialPop[i].printNumbers(outfile);
|
440 : |
|
|
}
|
441 : |
|
|
|
442 : |
|
|
if ((readoption == 0) || (readoption == 1)) {
|
443 : |
|
|
//JMB print the multipliers that are used to scale the population
|
444 : |
|
|
outfile << "\tArea multipliers used to scale the population\n";
|
445 : |
|
|
for (i = 0; i < areaFactor.Nrow(); i++) {
|
446 : |
|
|
for (j = 0; j < areaFactor.Ncol(i); j++)
|
447 : |
|
|
outfile << TAB << areaFactor[i][j];
|
448 : |
|
|
outfile << endl;
|
449 : |
|
|
}
|
450 : |
|
|
outfile << "\tAge multipliers used to scale the population\n";
|
451 : |
|
|
for (i = 0; i < ageFactor.Nrow(); i++) {
|
452 : |
|
|
for (j = 0; j < ageFactor.Ncol(i); j++)
|
453 : |
|
|
outfile << TAB << ageFactor[i][j];
|
454 : |
|
|
outfile << endl;
|
455 : |
|
|
}
|
456 : |
|
|
}
|
457 : |
|
|
|
458 : |
|
|
outfile << endl;
|
459 : |
|
|
outfile.flush();
|
460 : |
|
|
}
|
461 : |
|
|
|
462 : |
|
|
void InitialCond::Initialise(AgeBandMatrixPtrVector& Alkeys) {
|
463 : |
|
|
|
464 : |
|
|
int area, age, l;
|
465 : |
|
|
int minage, maxage;
|
466 : |
|
|
double mult, scaler, dnorm;
|
467 : |
|
|
|
468 : |
|
|
if (readoption == 0) {
|
469 : |
|
|
if (isZero(sdevMult)) //JMB this should never happen ...
|
470 : |
|
|
handle.logMessage(LOGFAIL, "Error in initial conditions - multiplier for standard deviation is zero");
|
471 : |
|
|
|
472 : |
|
|
for (area = 0; area < areas.Size(); area++) {
|
473 : |
|
|
minage = initialPop[area].minAge();
|
474 : |
|
|
maxage = initialPop[area].maxAge();
|
475 : |
|
|
for (age = minage; age <= maxage; age++) {
|
476 : |
|
|
|
477 : |
|
|
//JMB check that the length data is valid
|
478 : |
|
|
if (isZero(meanLength[area][age - minage]) || sdevLength[area][age - minage] < 0.04) {
|
479 : |
|
|
//JMB the limit has been set at 0.04 to keep the exponential calculation sane
|
480 : |
|
|
handle.logMessage(LOGWARN, "Warning in initial conditions - invalid length data for stock", this->getName());
|
481 : |
|
|
|
482 : |
|
|
//JMB set the population to zero
|
483 : |
|
|
for (l = initialPop[area].minLength(age); l < initialPop[area].maxLength(age); l++)
|
484 : |
|
|
initialPop[area][age][l].setToZero();
|
485 : |
|
|
|
486 : |
|
|
} else {
|
487 : |
|
|
//JMB check that the mean length is within the length group range
|
488 : |
|
|
if (meanLength[area][age - minage] < LgrpDiv->minLength())
|
489 : |
|
|
handle.logMessage(LOGWARN, "Warning in initial conditions - mean length is less than minimum length for stock", this->getName());
|
490 : |
|
|
if (meanLength[area][age - minage] > LgrpDiv->maxLength())
|
491 : |
|
|
handle.logMessage(LOGWARN, "Warning in initial conditions - mean length is greater than maximum length for stock", this->getName());
|
492 : |
|
|
|
493 : |
|
|
scaler = 0.0;
|
494 : |
|
|
mult = 1.0 / (sdevLength[area][age - minage] * sdevMult);
|
495 : |
|
|
for (l = initialPop[area].minLength(age); l < initialPop[area].maxLength(age); l++) {
|
496 : |
|
|
dnorm = (LgrpDiv->meanLength(l) - meanLength[area][age - minage]) * mult;
|
497 : |
|
|
initialPop[area][age][l].N = exp(-(dnorm * dnorm) * 0.5);
|
498 : |
|
|
scaler += initialPop[area][age][l].N;
|
499 : |
|
|
}
|
500 : |
|
|
|
501 : |
|
|
if (isZero(scaler)) {
|
502 : |
|
|
handle.logMessage(LOGWARN, "Warning in initial population - calculated zero population");
|
503 : |
|
|
for (l = initialPop[area].minLength(age); l < initialPop[area].maxLength(age); l++)
|
504 : |
|
|
initialPop[area][age][l].setToZero();
|
505 : |
|
|
|
506 : |
|
|
} else {
|
507 : |
|
|
scaler = 10000.0 / scaler;
|
508 : |
|
|
for (l = initialPop[area].minLength(age); l < initialPop[area].maxLength(age); l++) {
|
509 : |
|
|
initialPop[area][age][l].N *= scaler;
|
510 : |
|
|
initialPop[area][age][l].W = refWeight[l] * relCond[area][age - minage];
|
511 : |
|
|
if ((handle.getLogLevel() >= LOGWARN) && (isZero(initialPop[area][age][l].W)) && (initialPop[area][age][l].N > 0.0))
|
512 : |
|
|
handle.logMessage(LOGWARN, "Warning in initial conditions - zero mean weight for stock", this->getName());
|
513 : |
|
|
}
|
514 : |
|
|
}
|
515 : |
|
|
}
|
516 : |
|
|
}
|
517 : |
|
|
}
|
518 : |
|
|
|
519 : |
|
|
} else if (readoption == 1) {
|
520 : |
|
|
if (isZero(sdevMult)) //JMB this should never happen ...
|
521 : |
|
|
handle.logMessage(LOGFAIL, "Error in initial conditions - multiplier for standard deviation is zero for stock", this->getName());
|
522 : |
|
|
|
523 : |
|
|
for (area = 0; area < areas.Size(); area++) {
|
524 : |
|
|
minage = initialPop[area].minAge();
|
525 : |
|
|
maxage = initialPop[area].maxAge();
|
526 : |
|
|
for (age = minage; age <= maxage; age++) {
|
527 : |
|
|
|
528 : |
|
|
//JMB check that the length data is valid
|
529 : |
|
|
if (isZero(meanLength[area][age - minage]) || sdevLength[area][age - minage] < 0.04) {
|
530 : |
|
|
//JMB the limit has been set at 0.04 to keep the exponential calculation sane
|
531 : |
|
|
handle.logMessage(LOGWARN, "Warning in initial conditions - invalid length data");
|
532 : |
|
|
|
533 : |
|
|
//JMB set the population to zero
|
534 : |
|
|
for (l = initialPop[area].minLength(age); l < initialPop[area].maxLength(age); l++)
|
535 : |
|
|
initialPop[area][age][l].setToZero();
|
536 : |
|
|
|
537 : |
|
|
} else {
|
538 : |
|
|
//JMB check that the mean length is within the length group range
|
539 : |
|
|
if (meanLength[area][age - minage] < LgrpDiv->minLength())
|
540 : |
|
|
handle.logMessage(LOGWARN, "Warning in initial conditions - mean length is less than minimum length for stock", this->getName());
|
541 : |
|
|
if (meanLength[area][age - minage] > LgrpDiv->maxLength())
|
542 : |
|
|
handle.logMessage(LOGWARN, "Warning in initial conditions - mean length is greater than maximum length for stock", this->getName());
|
543 : |
|
|
|
544 : |
|
|
scaler = 0.0;
|
545 : |
|
|
mult = 1.0 / (sdevLength[area][age - minage] * sdevMult);
|
546 : |
|
|
for (l = initialPop[area].minLength(age); l < initialPop[area].maxLength(age); l++) {
|
547 : |
|
|
dnorm = (LgrpDiv->meanLength(l) - meanLength[area][age - minage]) * mult;
|
548 : |
|
|
initialPop[area][age][l].N = exp(-(dnorm * dnorm) * 0.5);
|
549 : |
|
|
scaler += initialPop[area][age][l].N;
|
550 : |
|
|
}
|
551 : |
|
|
|
552 : |
|
|
if (isZero(scaler)) {
|
553 : |
|
|
handle.logMessage(LOGWARN, "Warning in initial population - calculated zero population for stock", this->getName());
|
554 : |
|
|
for (l = initialPop[area].minLength(age); l < initialPop[area].maxLength(age); l++)
|
555 : |
|
|
initialPop[area][age][l].setToZero();
|
556 : |
|
|
|
557 : |
|
|
} else {
|
558 : |
|
|
scaler = 10000.0 / scaler;
|
559 : |
|
|
for (l = initialPop[area].minLength(age); l < initialPop[area].maxLength(age); l++) {
|
560 : |
|
|
initialPop[area][age][l].N *= scaler;
|
561 : |
|
|
initialPop[area][age][l].W = alpha[area][age - minage] * pow(LgrpDiv->meanLength(l), beta[area][age - minage]);
|
562 : |
|
|
if ((handle.getLogLevel() >= LOGWARN) && (isZero(initialPop[area][age][l].W)) && (initialPop[area][age][l].N > 0.0))
|
563 : |
|
|
handle.logMessage(LOGWARN, "Warning in initial conditions - zero mean weight for stock", this->getName());
|
564 : |
|
|
}
|
565 : |
|
|
}
|
566 : |
|
|
}
|
567 : |
|
|
}
|
568 : |
|
|
}
|
569 : |
|
|
|
570 : |
|
|
} else if (readoption == 2) {
|
571 : |
|
|
for (area = 0; area < areas.Size(); area++) {
|
572 : |
|
|
minage = initialPop[area].minAge();
|
573 : |
|
|
maxage = initialPop[area].maxAge();
|
574 : |
|
|
for (age = minage; age <= maxage; age++) {
|
575 : |
|
|
for (l = initialPop[area].minLength(age); l < initialPop[area].maxLength(age); l++) {
|
576 : |
|
|
initialPop[area][age][l].N = (*initialNumber[area])[age - minage][l];
|
577 : |
|
|
if (handle.getLogLevel() >= LOGWARN) {
|
578 : |
|
|
if (initialPop[area][age][l].N < 0.0)
|
579 : |
|
|
handle.logMessage(LOGWARN, "Warning in initial conditions - negative initial population for stock", this->getName());
|
580 : |
|
|
if ((isZero(initialPop[area][age][l].W)) && (initialPop[area][age][l].N > 0.0))
|
581 : |
|
|
handle.logMessage(LOGWARN, "Warning in initial conditions - zero mean weight for stock", this->getName());
|
582 : |
|
|
}
|
583 : |
|
|
}
|
584 : |
|
|
}
|
585 : |
|
|
}
|
586 : |
|
|
|
587 : |
|
|
} else
|
588 : |
|
|
handle.logMessage(LOGFAIL, "Error in initial conditions - unrecognised data format");
|
589 : |
|
|
|
590 : |
|
|
mult = 1.0;
|
591 : |
|
|
for (area = 0; area < areas.Size(); area++) {
|
592 : |
|
|
Alkeys[area].setToZero();
|
593 : |
|
|
//check minimum and maximum ages
|
594 : |
|
|
if (handle.getLogLevel() >= LOGWARN) {
|
595 : |
|
|
if (initialPop[area].minAge() < Alkeys[area].minAge())
|
596 : |
|
|
handle.logMessage(LOGWARN, "Warning in initial conditions - minimum age less than stock age for stock", this->getName());
|
597 : |
|
|
if (initialPop[area].maxAge() > Alkeys[area].maxAge())
|
598 : |
|
|
handle.logMessage(LOGWARN, "Warning in initial conditions - maximum age greater than stock age for stock", this->getName());
|
599 : |
|
|
}
|
600 : |
|
|
|
601 : |
|
|
minage = max(Alkeys[area].minAge(), initialPop[area].minAge());
|
602 : |
|
|
maxage = min(Alkeys[area].maxAge(), initialPop[area].maxAge());
|
603 : |
|
|
if (maxage < minage)
|
604 : |
|
|
handle.logMessage(LOGFAIL, "Error in initial conditions - maximum age less than minimum age for stock", this->getName());
|
605 : |
|
|
|
606 : |
|
|
for (age = minage; age <= maxage; age++) {
|
607 : |
|
|
if ((readoption == 0) || (readoption == 1))
|
608 : |
|
|
mult = areaFactor[area][age - minage] * ageFactor[area][age - minage];
|
609 : |
|
|
|
610 : |
|
|
if (mult < 0.0) {
|
611 : |
|
|
handle.logMessage(LOGWARN, "Warning in initial conditions - negative stock multiplier", mult);
|
612 : |
|
|
mult = -mult;
|
613 : |
|
|
}
|
614 : |
|
|
|
615 : |
|
|
Alkeys[area][age].Add(initialPop[area][age], *CI, mult);
|
616 : |
|
|
}
|
617 : |
|
|
}
|
618 : |
|
|
|
619 : |
|
|
if (handle.getLogLevel() >= LOGMESSAGE)
|
620 : |
|
|
handle.logMessage(LOGMESSAGE, "Calculated initial condition data for stock", this->getName());
|
621 : |
|
|
}
|