1 : |
agomez |
1 |
#include "grower.h" |
2 : |
|
|
#include "errorhandler.h" |
3 : |
|
|
#include "readfunc.h" |
4 : |
|
|
#include "readword.h" |
5 : |
|
|
#include "keeper.h" |
6 : |
|
|
#include "areatime.h" |
7 : |
|
|
#include "growthcalc.h" |
8 : |
|
|
#include "gadget.h" |
9 : |
|
|
#include "global.h" |
10 : |
|
|
|
11 : |
|
|
Grower::Grower(CommentStream& infile, const LengthGroupDivision* const OtherLgrpDiv,
|
12 : |
|
|
const LengthGroupDivision* const GivenLgrpDiv, const IntVector& Areas,
|
13 : |
|
|
const TimeClass* const TimeInfo, Keeper* const keeper, const char* refWeight,
|
14 : |
|
|
const char* givenname, const AreaClass* const Area, const CharPtrVector& lenindex)
|
15 : |
|
|
: HasName(givenname), LivesOnAreas(Areas), LgrpDiv(0), CI(0), growthcalc(0) {
|
16 : |
|
|
|
17 : |
|
|
char text[MaxStrLength];
|
18 : |
|
|
strncpy(text, "", MaxStrLength);
|
19 : |
|
|
int i;
|
20 : |
|
|
|
21 : |
|
|
keeper->addString("grower");
|
22 : |
|
|
fixedweights = 0;
|
23 : |
|
|
functionnumber = 0;
|
24 : |
|
|
LgrpDiv = new LengthGroupDivision(*GivenLgrpDiv);
|
25 : |
|
|
if (LgrpDiv->Error())
|
26 : |
|
|
handle.logMessage(LOGFAIL, "Error in grower - failed to create length group");
|
27 : |
|
|
CI = new ConversionIndex(OtherLgrpDiv, LgrpDiv, 1);
|
28 : |
|
|
if (CI->Error())
|
29 : |
|
|
handle.logMessage(LOGFAIL, "Error in grower - error when checking length structure");
|
30 : |
|
|
|
31 : |
|
|
readWordAndValue(infile, "growthfunction", text);
|
32 : |
|
|
if (strcasecmp(text, "multspec") == 0) {
|
33 : |
|
|
functionnumber = 1;
|
34 : |
|
|
growthcalc = new GrowthCalcA(infile, areas, TimeInfo, keeper);
|
35 : |
|
|
} else if (strcasecmp(text, "fromfile") == 0) {
|
36 : |
|
|
functionnumber = 2;
|
37 : |
|
|
growthcalc = new GrowthCalcB(infile, areas, TimeInfo, keeper, Area, lenindex);
|
38 : |
|
|
} else if (strcasecmp(text, "weightvb") == 0) {
|
39 : |
|
|
functionnumber = 3;
|
40 : |
|
|
growthcalc = new GrowthCalcC(infile, areas, TimeInfo, LgrpDiv, keeper, refWeight);
|
41 : |
|
|
} else if (strcasecmp(text, "weightjones") == 0) {
|
42 : |
|
|
functionnumber = 4;
|
43 : |
|
|
growthcalc = new GrowthCalcD(infile, areas, TimeInfo, LgrpDiv, keeper, refWeight);
|
44 : |
|
|
} else if (strcasecmp(text, "weightvbexpanded") == 0) {
|
45 : |
|
|
functionnumber = 5;
|
46 : |
|
|
growthcalc = new GrowthCalcE(infile, areas, TimeInfo, LgrpDiv, keeper, refWeight);
|
47 : |
|
|
} else if (strcasecmp(text, "lengthvb") == 0) {
|
48 : |
|
|
functionnumber = 6;
|
49 : |
|
|
fixedweights = 1;
|
50 : |
|
|
growthcalc = new GrowthCalcF(infile, areas, TimeInfo, keeper, Area, lenindex);
|
51 : |
|
|
} else if (strcasecmp(text, "lengthpower") == 0) {
|
52 : |
|
|
functionnumber = 7;
|
53 : |
|
|
fixedweights = 1;
|
54 : |
|
|
growthcalc = new GrowthCalcG(infile, areas, TimeInfo, keeper, Area, lenindex);
|
55 : |
|
|
} else if (strcasecmp(text, "lengthvbsimple") == 0) {
|
56 : |
|
|
functionnumber = 8;
|
57 : |
|
|
growthcalc = new GrowthCalcH(infile, areas, TimeInfo, keeper);
|
58 : |
|
|
} else if (strcasecmp(text, "weightjonessimple") == 0) {
|
59 : |
|
|
functionnumber = 9;
|
60 : |
|
|
growthcalc = new GrowthCalcI(infile, areas, TimeInfo, keeper);
|
61 : |
|
|
} else if (strcasecmp(text, "lengthvpsimplet0") == 0) {
|
62 : |
|
|
functionnumber = 10;
|
63 : |
|
|
growthcalc = new GrowthCalcJ(infile, areas, TimeInfo, keeper);
|
64 : |
|
|
} else if (strcasecmp(text, "lengthgompertz") == 0) {
|
65 : |
|
|
functionnumber = 11;
|
66 : |
|
|
growthcalc = new GrowthCalcK(infile, areas, TimeInfo, keeper);
|
67 : |
|
|
} else {
|
68 : |
|
|
handle.logFileMessage(LOGFAIL, "unrecognised growth function", text);
|
69 : |
|
|
}
|
70 : |
|
|
|
71 : |
|
|
infile >> ws >> text;
|
72 : |
|
|
if (strcasecmp(text, "beta") == 0) {
|
73 : |
|
|
//Beta binomial growth distribution code is used
|
74 : |
|
|
infile >> beta;
|
75 : |
|
|
beta.Inform(keeper);
|
76 : |
|
|
readWordAndVariable(infile, "maxlengthgroupgrowth", maxlengthgroupgrowth);
|
77 : |
|
|
|
78 : |
|
|
if (maxlengthgroupgrowth > LgrpDiv->numLengthGroups()) {
|
79 : |
|
|
handle.logMessage(LOGWARN, "Warning in grower - maxlengthgroupgrowth is greater than number of length groups");
|
80 : |
|
|
maxlengthgroupgrowth = LgrpDiv->numLengthGroups();
|
81 : |
|
|
}
|
82 : |
|
|
|
83 : |
|
|
part1.resize(maxlengthgroupgrowth + 1, 0.0);
|
84 : |
|
|
part2.resize(maxlengthgroupgrowth + 1, 0.0);
|
85 : |
|
|
part4.resize(maxlengthgroupgrowth + 1, 0.0);
|
86 : |
|
|
|
87 : |
|
|
} else if (strcasecmp(text, "meanvarianceparameters") == 0) {
|
88 : |
|
|
handle.logFileMessage(LOGFAIL, "\nThe mean variance parameters implementation of the growth is no longer supported\nUse the beta-binomial distribution implementation of the growth instead");
|
89 : |
|
|
|
90 : |
|
|
} else
|
91 : |
|
|
handle.logFileUnexpected(LOGFAIL, "beta", text);
|
92 : |
|
|
|
93 : |
|
|
//Finished reading from input files.
|
94 : |
|
|
keeper->clearLast();
|
95 : |
|
|
int noareas = areas.Size();
|
96 : |
|
|
int len = LgrpDiv->numLengthGroups();
|
97 : |
|
|
int otherlen = OtherLgrpDiv->numLengthGroups();
|
98 : |
|
|
PopInfo nullpop;
|
99 : |
|
|
numGrow.AddRows(noareas, len, nullpop);
|
100 : |
|
|
|
101 : |
|
|
//setting storage spaces for growth
|
102 : |
|
|
calcLengthGrowth.AddRows(noareas, len, 0.0);
|
103 : |
|
|
calcWeightGrowth.AddRows(noareas, len, 0.0);
|
104 : |
|
|
interpLengthGrowth.AddRows(noareas, otherlen, 0.0);
|
105 : |
|
|
interpWeightGrowth.AddRows(noareas, otherlen, 0.0);
|
106 : |
|
|
dummyfphi.resize(len, 0.0);
|
107 : |
|
|
for (i = 0; i < noareas; i++) {
|
108 : |
|
|
lgrowth.resize(new DoubleMatrix(maxlengthgroupgrowth + 1, otherlen, 0.0));
|
109 : |
|
|
wgrowth.resize(new DoubleMatrix(maxlengthgroupgrowth + 1, otherlen, 0.0));
|
110 : |
|
|
}
|
111 : |
|
|
}
|
112 : |
|
|
|
113 : |
|
|
Grower::~Grower() {
|
114 : |
|
|
int i;
|
115 : |
|
|
for (i = 0; i < lgrowth.Size(); i++) {
|
116 : |
|
|
delete lgrowth[i];
|
117 : |
|
|
delete wgrowth[i];
|
118 : |
|
|
}
|
119 : |
|
|
delete CI;
|
120 : |
|
|
delete LgrpDiv;
|
121 : |
|
|
delete growthcalc;
|
122 : |
|
|
}
|
123 : |
|
|
|
124 : |
|
|
void Grower::Print(ofstream& outfile) const {
|
125 : |
|
|
int i, j, area;
|
126 : |
|
|
|
127 : |
|
|
outfile << "\nGrower\n\t";
|
128 : |
|
|
LgrpDiv->Print(outfile);
|
129 : |
|
|
for (area = 0; area < areas.Size(); area++) {
|
130 : |
|
|
outfile << "\tLength increase on internal area " << areas[area] << ":\n\t";
|
131 : |
|
|
for (i = 0; i < calcLengthGrowth.Ncol(area); i++)
|
132 : |
|
|
outfile << sep << calcLengthGrowth[area][i];
|
133 : |
|
|
outfile << "\n\tWeight increase on internal area " << areas[area] << ":\n\t";
|
134 : |
|
|
for (i = 0; i < calcWeightGrowth.Ncol(area); i++)
|
135 : |
|
|
outfile << sep << calcWeightGrowth[area][i];
|
136 : |
|
|
outfile << "\n\tDistributed length increase on internal area " << areas[area] << ":\n";
|
137 : |
|
|
for (i = 0; i < lgrowth[area]->Nrow(); i++) {
|
138 : |
|
|
outfile << TAB;
|
139 : |
|
|
for (j = 0; j < lgrowth[area]->Ncol(i); j++)
|
140 : |
|
|
outfile << sep << (*lgrowth[area])[i][j];
|
141 : |
|
|
outfile << endl;
|
142 : |
|
|
}
|
143 : |
|
|
outfile << "\tDistributed weight increase on internal area " << areas[area] << ":\n";
|
144 : |
|
|
for (i = 0; i < wgrowth[area]->Nrow(); i++) {
|
145 : |
|
|
outfile << TAB;
|
146 : |
|
|
for (j = 0; j < wgrowth[area]->Ncol(i); j++)
|
147 : |
|
|
outfile << sep << (*wgrowth[area])[i][j];
|
148 : |
|
|
outfile << endl;
|
149 : |
|
|
}
|
150 : |
|
|
}
|
151 : |
|
|
}
|
152 : |
|
|
|
153 : |
|
|
void Grower::Sum(const PopInfoVector& NumberInArea, int area) {
|
154 : |
|
|
numGrow[this->areaNum(area)].Sum(&NumberInArea, *CI);
|
155 : |
|
|
}
|
156 : |
|
|
|
157 : |
|
|
void Grower::calcGrowth(int area,
|
158 : |
|
|
const AreaClass* const Area, const TimeClass* const TimeInfo) {
|
159 : |
|
|
|
160 : |
|
|
this->calcGrowth(area, Area, TimeInfo, dummyfphi, dummyfphi);
|
161 : |
|
|
}
|
162 : |
|
|
|
163 : |
|
|
void Grower::calcGrowth(int area,
|
164 : |
|
|
const AreaClass* const Area, const TimeClass* const TimeInfo,
|
165 : |
|
|
const DoubleVector& FPhi, const DoubleVector& MaxCon) {
|
166 : |
|
|
|
167 : |
|
|
int inarea = this->areaNum(area);
|
168 : |
|
|
growthcalc->calcGrowth(area, calcLengthGrowth[inarea], calcWeightGrowth[inarea],
|
169 : |
|
|
numGrow[inarea], Area, TimeInfo, FPhi, MaxCon, LgrpDiv);
|
170 : |
|
|
|
171 : |
|
|
CI->interpolateLengths(interpLengthGrowth[inarea], calcLengthGrowth[inarea]);
|
172 : |
|
|
switch (functionnumber) {
|
173 : |
|
|
case 1:
|
174 : |
|
|
case 2:
|
175 : |
|
|
case 3:
|
176 : |
|
|
case 4:
|
177 : |
|
|
case 5:
|
178 : |
|
|
case 6:
|
179 : |
|
|
case 7:
|
180 : |
|
|
case 9:
|
181 : |
|
|
CI->interpolateLengths(interpWeightGrowth[inarea], calcWeightGrowth[inarea]);
|
182 : |
|
|
break;
|
183 : |
|
|
case 8:
|
184 : |
|
|
case 10:
|
185 : |
|
|
case 11:
|
186 : |
|
|
break;
|
187 : |
|
|
default:
|
188 : |
|
|
handle.logMessage(LOGFAIL, "Error in grower - unrecognised growth function", functionnumber);
|
189 : |
|
|
break;
|
190 : |
|
|
}
|
191 : |
|
|
}
|
192 : |
|
|
|
193 : |
|
|
void Grower::Reset() {
|
194 : |
|
|
int i, j, area;
|
195 : |
|
|
double factorialx, tmppart, tmpmax;
|
196 : |
|
|
|
197 : |
|
|
calcLengthGrowth.setToZero();
|
198 : |
|
|
calcWeightGrowth.setToZero();
|
199 : |
|
|
interpLengthGrowth.setToZero();
|
200 : |
|
|
for (area = 0; area < areas.Size(); area++) {
|
201 : |
|
|
(*lgrowth[area]).setToZero();
|
202 : |
|
|
for (i = 0; i < LgrpDiv->numLengthGroups(); i++)
|
203 : |
|
|
numGrow[area][i].setToZero();
|
204 : |
|
|
}
|
205 : |
|
|
|
206 : |
|
|
switch (functionnumber) {
|
207 : |
|
|
case 1:
|
208 : |
|
|
case 2:
|
209 : |
|
|
case 3:
|
210 : |
|
|
case 4:
|
211 : |
|
|
case 5:
|
212 : |
|
|
case 6:
|
213 : |
|
|
case 7:
|
214 : |
|
|
case 9:
|
215 : |
|
|
interpWeightGrowth.setToZero();
|
216 : |
|
|
for (area = 0; area < areas.Size(); area++)
|
217 : |
|
|
(*wgrowth[area]).setToZero();
|
218 : |
|
|
break;
|
219 : |
|
|
case 8:
|
220 : |
|
|
case 10:
|
221 : |
|
|
case 11:
|
222 : |
|
|
break;
|
223 : |
|
|
default:
|
224 : |
|
|
handle.logMessage(LOGFAIL, "Error in grower - unrecognised growth function", functionnumber);
|
225 : |
|
|
break;
|
226 : |
|
|
}
|
227 : |
|
|
|
228 : |
|
|
tmpmax = double(maxlengthgroupgrowth);
|
229 : |
|
|
part1[0] = 1.0;
|
230 : |
|
|
part1[1] = tmpmax;
|
231 : |
|
|
factorialx = 1.0;
|
232 : |
|
|
tmppart = tmpmax;
|
233 : |
|
|
if (maxlengthgroupgrowth > 1) {
|
234 : |
|
|
for (i = 2; i < maxlengthgroupgrowth + 1; i++) {
|
235 : |
|
|
tmppart *= (tmpmax - i + 1);
|
236 : |
|
|
factorialx *= double(i);
|
237 : |
|
|
part1[i] = (tmppart / factorialx);
|
238 : |
|
|
}
|
239 : |
|
|
}
|
240 : |
|
|
|
241 : |
|
|
part2[maxlengthgroupgrowth] = 1.0;
|
242 : |
|
|
part2[maxlengthgroupgrowth - 1] = beta;
|
243 : |
|
|
if (maxlengthgroupgrowth > 1)
|
244 : |
|
|
for (i = maxlengthgroupgrowth - 2; i >= 0; i--)
|
245 : |
|
|
part2[i] = part2[i + 1] * (beta + tmpmax - i - 1);
|
246 : |
|
|
|
247 : |
|
|
//JMB this will never change so we can set it once
|
248 : |
|
|
part4[0] = 1.0;
|
249 : |
|
|
|
250 : |
|
|
if (handle.getLogLevel() >= LOGMESSAGE)
|
251 : |
|
|
handle.logMessage(LOGMESSAGE, "Reset grower data for stock", this->getName());
|
252 : |
|
|
}
|