1 : |
agomez |
1 |
#include "transition.h" |
2 : |
|
|
#include "errorhandler.h" |
3 : |
|
|
#include "keeper.h" |
4 : |
|
|
#include "readword.h" |
5 : |
|
|
#include "gadget.h" |
6 : |
|
|
#include "global.h" |
7 : |
|
|
|
8 : |
|
|
Transition::Transition(CommentStream& infile, const IntVector& areas, int Age,
|
9 : |
|
|
const LengthGroupDivision* const lgrpdiv, const char* givenname,
|
10 : |
|
|
const TimeClass* const TimeInfo, Keeper* const keeper)
|
11 : |
|
|
: HasName(givenname), LivesOnAreas(areas), age(Age) {
|
12 : |
|
|
|
13 : |
|
|
int i = 0;
|
14 : |
|
|
istagged = 0;
|
15 : |
|
|
ratioscale = 1.0; //JMB used to scale the ratios to ensure that they sum to 1
|
16 : |
|
|
char text[MaxStrLength];
|
17 : |
|
|
strncpy(text, "", MaxStrLength);
|
18 : |
|
|
keeper->addString("transition");
|
19 : |
|
|
|
20 : |
|
|
LgrpDiv = new LengthGroupDivision(*lgrpdiv);
|
21 : |
|
|
if (LgrpDiv->Error())
|
22 : |
|
|
handle.logMessage(LOGFAIL, "Error in transition - failed to create length group");
|
23 : |
|
|
|
24 : |
|
|
infile >> text >> ws;
|
25 : |
|
|
if (strcasecmp(text, "transitionstocksandratios") != 0)
|
26 : |
|
|
handle.logFileUnexpected(LOGFAIL, "transitionstocksandratios", text);
|
27 : |
|
|
|
28 : |
|
|
infile >> text >> ws;
|
29 : |
|
|
while (strcasecmp(text, "transitionstep") != 0 && !infile.eof()) {
|
30 : |
|
|
transitionStockNames.resize(new char[strlen(text) + 1]);
|
31 : |
|
|
strcpy(transitionStockNames[i], text);
|
32 : |
|
|
transitionRatio.resize(1, keeper);
|
33 : |
|
|
if (!(infile >> transitionRatio[i]))
|
34 : |
|
|
handle.logFileMessage(LOGFAIL, "invalid format for transition ratio");
|
35 : |
|
|
transitionRatio[i].Inform(keeper);
|
36 : |
|
|
|
37 : |
|
|
infile >> text >> ws;
|
38 : |
|
|
i++;
|
39 : |
|
|
}
|
40 : |
|
|
|
41 : |
|
|
if (infile.eof())
|
42 : |
|
|
handle.logFileEOFMessage(LOGFAIL);
|
43 : |
|
|
infile >> transitionStep >> ws;
|
44 : |
|
|
if (transitionStep < 1 || transitionStep > TimeInfo->numSteps())
|
45 : |
|
|
handle.logFileMessage(LOGFAIL, "invalid transition step", transitionStep);
|
46 : |
|
|
keeper->clearLast();
|
47 : |
|
|
}
|
48 : |
|
|
|
49 : |
|
|
Transition::~Transition() {
|
50 : |
|
|
int i;
|
51 : |
|
|
for (i = 0; i < transitionStockNames.Size(); i++)
|
52 : |
|
|
delete[] transitionStockNames[i];
|
53 : |
|
|
for (i = 0; i < CI.Size(); i++)
|
54 : |
|
|
delete CI[i];
|
55 : |
|
|
delete LgrpDiv;
|
56 : |
|
|
}
|
57 : |
|
|
|
58 : |
|
|
void Transition::setStock(StockPtrVector& stockvec) {
|
59 : |
|
|
int i, j, index;
|
60 : |
|
|
|
61 : |
|
|
for (i = 0; i < transitionStockNames.Size(); i++)
|
62 : |
|
|
for (j = 0; j < transitionStockNames.Size(); j++)
|
63 : |
|
|
if ((strcasecmp(transitionStockNames[i], transitionStockNames[j]) == 0) && (i != j))
|
64 : |
|
|
handle.logMessage(LOGFAIL, "Error in transition - repeated stock", transitionStockNames[i]);
|
65 : |
|
|
|
66 : |
|
|
for (i = 0; i < stockvec.Size(); i++)
|
67 : |
|
|
for (j = 0; j < transitionStockNames.Size(); j++)
|
68 : |
|
|
if (strcasecmp(stockvec[i]->getName(), transitionStockNames[j]) == 0)
|
69 : |
|
|
transitionStocks.resize(stockvec[i]);
|
70 : |
|
|
|
71 : |
|
|
if (transitionStocks.Size() != transitionStockNames.Size()) {
|
72 : |
|
|
handle.logMessage(LOGWARN, "Error in transition - failed to match transition stocks");
|
73 : |
|
|
for (i = 0; i < stockvec.Size(); i++)
|
74 : |
|
|
handle.logMessage(LOGWARN, "Error in transition - found stock", stockvec[i]->getName());
|
75 : |
|
|
for (i = 0; i < transitionStockNames.Size(); i++)
|
76 : |
|
|
handle.logMessage(LOGWARN, "Error in transition - looking for stock", transitionStockNames[i]);
|
77 : |
|
|
handle.logMessage(LOGFAIL, ""); //JMB this will exit gadget
|
78 : |
|
|
}
|
79 : |
|
|
|
80 : |
|
|
//JMB ensure that the ratio vector is indexed in the right order
|
81 : |
|
|
ratioindex.resize(transitionStocks.Size(), 0);
|
82 : |
|
|
for (i = 0; i < transitionStocks.Size(); i++)
|
83 : |
|
|
for (j = 0; j < transitionStockNames.Size(); j++)
|
84 : |
|
|
if (strcasecmp(transitionStocks[i]->getName(), transitionStockNames[j]) == 0)
|
85 : |
|
|
ratioindex[i] = j;
|
86 : |
|
|
|
87 : |
|
|
double mlength = 9999.0;
|
88 : |
|
|
for (i = 0; i < transitionStocks.Size(); i++) {
|
89 : |
|
|
CI.resize(new ConversionIndex(LgrpDiv, transitionStocks[i]->getLengthGroupDiv()));
|
90 : |
|
|
if (CI[i]->Error())
|
91 : |
|
|
handle.logMessage(LOGFAIL, "Error in transition - error when checking length structure");
|
92 : |
|
|
index = 0;
|
93 : |
|
|
for (j = 0; j < areas.Size(); j++)
|
94 : |
|
|
if (!transitionStocks[i]->isInArea(areas[j]))
|
95 : |
|
|
index++;
|
96 : |
|
|
|
97 : |
|
|
if (index != 0)
|
98 : |
|
|
handle.logMessage(LOGWARN, "Warning in transition - transition stock isnt defined on all areas");
|
99 : |
|
|
|
100 : |
|
|
if (transitionStocks[i]->getLengthGroupDiv()->minLength() < mlength)
|
101 : |
|
|
mlength = transitionStocks[i]->getLengthGroupDiv()->minLength();
|
102 : |
|
|
}
|
103 : |
|
|
|
104 : |
|
|
minTransitionLength = LgrpDiv->numLengthGroup(mlength);
|
105 : |
|
|
IntVector minlv(2, 0);
|
106 : |
|
|
IntVector sizev(2, LgrpDiv->numLengthGroups());
|
107 : |
|
|
Storage.resize(areas.Size(), age, minlv, sizev);
|
108 : |
|
|
for (i = 0; i < Storage.Size(); i++)
|
109 : |
|
|
Storage[i].setToZero();
|
110 : |
|
|
}
|
111 : |
|
|
|
112 : |
|
|
void Transition::Print(ofstream& outfile) const {
|
113 : |
|
|
int i;
|
114 : |
|
|
outfile << "\nTransition\n\tNames of transition stocks:";
|
115 : |
|
|
for (i = 0; i < transitionStockNames.Size(); i++)
|
116 : |
|
|
outfile << sep << transitionStockNames[i];
|
117 : |
|
|
outfile << "\n\tRatio moving into each stock:";
|
118 : |
|
|
for (i = 0; i < transitionRatio.Size(); i++)
|
119 : |
|
|
outfile << sep << (transitionRatio[ratioindex[i]] * ratioscale);
|
120 : |
|
|
outfile << "\n\tTransition step " << transitionStep << endl;
|
121 : |
|
|
}
|
122 : |
|
|
|
123 : |
|
|
void Transition::storeTransitionStock(int area, AgeBandMatrix& Alkeys, const TimeClass* const TimeInfo) {
|
124 : |
|
|
|
125 : |
|
|
int len, inarea = this->areaNum(area);
|
126 : |
|
|
for (len = Storage[inarea].minLength(age); len < Storage[inarea].maxLength(age); len++) {
|
127 : |
|
|
Storage[inarea][age][len].N = Alkeys[age][len].N;
|
128 : |
|
|
Storage[inarea][age][len].W = Alkeys[age][len].W;
|
129 : |
|
|
|
130 : |
|
|
if (len >= minTransitionLength)
|
131 : |
|
|
Alkeys[age][len].setToZero();
|
132 : |
|
|
}
|
133 : |
|
|
}
|
134 : |
|
|
|
135 : |
|
|
void Transition::storeTransitionStock(int area, AgeBandMatrix& Alkeys,
|
136 : |
|
|
AgeBandMatrixRatio& TagAlkeys, const TimeClass* const TimeInfo) {
|
137 : |
|
|
|
138 : |
|
|
if (!istagged)
|
139 : |
|
|
handle.logMessage(LOGFAIL, "Error in transition - invalid tagging experiment");
|
140 : |
|
|
|
141 : |
|
|
int len, tag;
|
142 : |
|
|
int inarea = this->areaNum(area);
|
143 : |
|
|
double tagnumber;
|
144 : |
|
|
|
145 : |
|
|
for (len = Storage[inarea].minLength(age); len < Storage[inarea].maxLength(age); len++) {
|
146 : |
|
|
Storage[inarea][age][len].N = Alkeys[age][len].N;
|
147 : |
|
|
Storage[inarea][age][len].W = Alkeys[age][len].W;
|
148 : |
|
|
|
149 : |
|
|
if (len >= minTransitionLength)
|
150 : |
|
|
Alkeys[age][len].setToZero();
|
151 : |
|
|
|
152 : |
|
|
for (tag = 0; tag < TagAlkeys.numTagExperiments(); tag++) {
|
153 : |
|
|
tagnumber = *(TagAlkeys[age][len][tag].N);
|
154 : |
|
|
if (tagnumber < verysmall)
|
155 : |
|
|
*(tagStorage[inarea][age][len][tag].N) = 0.0;
|
156 : |
|
|
else
|
157 : |
|
|
*(tagStorage[inarea][age][len][tag].N) = tagnumber;
|
158 : |
|
|
|
159 : |
|
|
if (len >= minTransitionLength) {
|
160 : |
|
|
*(TagAlkeys[age][len][tag].N) = 0.0;
|
161 : |
|
|
TagAlkeys[age][len][tag].R = 0.0;
|
162 : |
|
|
}
|
163 : |
|
|
}
|
164 : |
|
|
}
|
165 : |
|
|
}
|
166 : |
|
|
|
167 : |
|
|
//area in the call to this routine is not in the local area numbering of the stock.
|
168 : |
|
|
void Transition::Move(int area, const TimeClass* const TimeInfo) {
|
169 : |
|
|
|
170 : |
|
|
int i, inarea = this->areaNum(area);
|
171 : |
|
|
double ratio;
|
172 : |
|
|
for (i = 0; i < transitionStocks.Size(); i++) {
|
173 : |
|
|
if (!transitionStocks[i]->isInArea(area))
|
174 : |
|
|
handle.logMessage(LOGFAIL, "Error in transition - transition stock doesnt live on area", area);
|
175 : |
|
|
|
176 : |
|
|
if (transitionStocks[i]->isBirthday(TimeInfo)) {
|
177 : |
|
|
Storage[inarea].IncrementAge();
|
178 : |
|
|
if (istagged && tagStorage.numTagExperiments() > 0)
|
179 : |
|
|
tagStorage[inarea].IncrementAge(Storage[inarea]);
|
180 : |
|
|
}
|
181 : |
|
|
|
182 : |
|
|
ratio = transitionRatio[ratioindex[i]] * ratioscale;
|
183 : |
|
|
transitionStocks[i]->Add(Storage[inarea], CI[i], area, ratio);
|
184 : |
|
|
if (istagged && tagStorage.numTagExperiments() > 0)
|
185 : |
|
|
transitionStocks[i]->Add(tagStorage, CI[i], area, ratio);
|
186 : |
|
|
}
|
187 : |
|
|
|
188 : |
|
|
Storage[inarea].setToZero();
|
189 : |
|
|
if (istagged && tagStorage.numTagExperiments() > 0)
|
190 : |
|
|
tagStorage[inarea].setToZero();
|
191 : |
|
|
}
|
192 : |
|
|
|
193 : |
|
|
void Transition::Reset() {
|
194 : |
|
|
int i;
|
195 : |
|
|
|
196 : |
|
|
//JMB check that the sum of the ratios is 1
|
197 : |
|
|
ratioscale = 0.0;
|
198 : |
|
|
for (i = 0; i < transitionRatio.Size(); i++ )
|
199 : |
|
|
ratioscale += transitionRatio[i];
|
200 : |
|
|
|
201 : |
|
|
if (isZero(ratioscale)) {
|
202 : |
|
|
handle.logMessage(LOGWARN, "Warning in transition - specified ratios are zero");
|
203 : |
|
|
ratioscale = 1.0;
|
204 : |
|
|
} else if (isEqual(ratioscale, 1.0)) {
|
205 : |
|
|
// do nothing
|
206 : |
|
|
} else {
|
207 : |
|
|
handle.logMessage(LOGWARN, "Warning in transition - scaling ratios using", ratioscale);
|
208 : |
|
|
ratioscale = 1.0 / ratioscale;
|
209 : |
|
|
}
|
210 : |
|
|
|
211 : |
|
|
if (handle.getLogLevel() >= LOGMESSAGE)
|
212 : |
|
|
handle.logMessage(LOGMESSAGE, "Reset transition data for stock", this->getName());
|
213 : |
|
|
}
|
214 : |
|
|
|
215 : |
|
|
const StockPtrVector& Transition::getTransitionStocks() {
|
216 : |
|
|
return transitionStocks;
|
217 : |
|
|
}
|
218 : |
|
|
|
219 : |
|
|
void Transition::setTagged() {
|
220 : |
|
|
istagged = 1;
|
221 : |
|
|
//resize tagStorage to be the same size as Storage
|
222 : |
|
|
int i;
|
223 : |
|
|
IntVector lower(2, 0);
|
224 : |
|
|
IntVector size(2, LgrpDiv->numLengthGroups());
|
225 : |
|
|
tagStorage.resize(areas.Size(), age, lower, size);
|
226 : |
|
|
for (i = 0; i < tagStorage.Size(); i++)
|
227 : |
|
|
tagStorage[i].setToZero();
|
228 : |
|
|
}
|
229 : |
|
|
|
230 : |
|
|
void Transition::addTransitionTag(const char* tagname) {
|
231 : |
|
|
if (!istagged)
|
232 : |
|
|
handle.logMessage(LOGFAIL, "Error in transition - invalid tagging experiment", tagname);
|
233 : |
|
|
tagStorage.addTag(tagname);
|
234 : |
|
|
}
|
235 : |
|
|
|
236 : |
|
|
void Transition::deleteTransitionTag(const char* tagname) {
|
237 : |
|
|
if (!istagged)
|
238 : |
|
|
handle.logMessage(LOGFAIL, "Error in transition - invalid tagging experiment", tagname);
|
239 : |
|
|
|
240 : |
|
|
int minage, maxage, age, len, a;
|
241 : |
|
|
int id = tagStorage.getTagID(tagname);
|
242 : |
|
|
|
243 : |
|
|
if (id >= 0) {
|
244 : |
|
|
minage = tagStorage[0].minAge();
|
245 : |
|
|
maxage = tagStorage[0].maxAge();
|
246 : |
|
|
//free memory allocated for tagging experiment
|
247 : |
|
|
for (a = 0; a < tagStorage.Size(); a++) {
|
248 : |
|
|
for (age = minage; age <= maxage; age++) {
|
249 : |
|
|
for (len = tagStorage[a].minLength(age); len < tagStorage[a].maxLength(age); len++) {
|
250 : |
|
|
delete[] (tagStorage[a][age][len][id].N);
|
251 : |
|
|
(tagStorage[a][age][len][id].N) = NULL;
|
252 : |
|
|
}
|
253 : |
|
|
}
|
254 : |
|
|
}
|
255 : |
|
|
tagStorage.deleteTag(tagname);
|
256 : |
|
|
|
257 : |
|
|
} else
|
258 : |
|
|
handle.logMessage(LOGWARN, "Warning in transition - failed to delete tagging experiment", tagname);
|
259 : |
|
|
}
|
260 : |
|
|
|
261 : |
|
|
int Transition::isTransitionStep(const TimeClass* const TimeInfo) {
|
262 : |
|
|
if (TimeInfo->getStep() == transitionStep)
|
263 : |
|
|
return 1;
|
264 : |
|
|
return 0;
|
265 : |
|
|
}
|