1 : |
agomez |
1 |
#include "modelvariable.h" |
2 : |
|
|
#include "errorhandler.h" |
3 : |
|
|
#include "readword.h" |
4 : |
|
|
#include "gadget.h" |
5 : |
|
|
#include "global.h" |
6 : |
|
|
|
7 : |
|
|
ModelVariable::ModelVariable() {
|
8 : |
|
|
value = 0.0;
|
9 : |
|
|
mvtype = MVFORMULA;
|
10 : |
|
|
}
|
11 : |
|
|
|
12 : |
|
|
void ModelVariable::setValue(double newValue) {
|
13 : |
|
|
if (mvtype != MVFORMULA)
|
14 : |
|
|
handle.logMessage(LOGFAIL, "Error in modelvariable - cannot set value for entries read from file");
|
15 : |
|
|
|
16 : |
|
|
init.setValue(newValue);
|
17 : |
|
|
value = newValue;
|
18 : |
|
|
}
|
19 : |
|
|
|
20 : |
|
|
void ModelVariable::read(CommentStream& infile,
|
21 : |
|
|
const TimeClass* const TimeInfo, Keeper* const keeper) {
|
22 : |
|
|
|
23 : |
|
|
keeper->addString("modelvariable");
|
24 : |
|
|
ifstream subfile;
|
25 : |
|
|
CommentStream subcomment(subfile);
|
26 : |
|
|
char text[MaxStrLength];
|
27 : |
|
|
strncpy(text, "", MaxStrLength);
|
28 : |
|
|
streampos readPos;
|
29 : |
|
|
readPos = infile.tellg();
|
30 : |
|
|
infile >> text;
|
31 : |
|
|
subfile.open(text, ios::in);
|
32 : |
|
|
if (subfile.fail()) {
|
33 : |
|
|
// this is a single value that is read as a formula in case
|
34 : |
|
|
infile.seekg(readPos);
|
35 : |
|
|
if (!(infile >> init))
|
36 : |
|
|
handle.logFileMessage(LOGFAIL, "Error in size of vector - didnt expect to find", text);
|
37 : |
|
|
init.Inform(keeper);
|
38 : |
|
|
|
39 : |
|
|
} else {
|
40 : |
|
|
// this is either a timevariable or a stockvariable that is read from file
|
41 : |
|
|
handle.Open(text);
|
42 : |
|
|
|
43 : |
|
|
subcomment >> text >> ws; // description of model variable
|
44 : |
|
|
keeper->addString(text);
|
45 : |
|
|
|
46 : |
|
|
char c = subcomment.peek();
|
47 : |
|
|
int numcoeff = 0;
|
48 : |
|
|
if ((c == 'n') || (c == 'N')) {
|
49 : |
|
|
readWordAndVariable(subcomment, "nrofcoeff", numcoeff);
|
50 : |
|
|
subcomment >> ws;
|
51 : |
|
|
c = subcomment.peek();
|
52 : |
|
|
if (numcoeff > 0)
|
53 : |
|
|
handle.logFileMessage(LOGWARN, "The modelmatrix timevariable formulation is no longer supported");
|
54 : |
|
|
}
|
55 : |
|
|
|
56 : |
|
|
// JMB optional multiplier
|
57 : |
|
|
if ((c == 'm') || (c == 'M'))
|
58 : |
|
|
readWordAndVariable(subcomment, "multiplier", init);
|
59 : |
|
|
else
|
60 : |
|
|
init.setValue(1.0);
|
61 : |
|
|
init.Inform(keeper);
|
62 : |
|
|
|
63 : |
|
|
subcomment >> ws >> text;
|
64 : |
|
|
if ((strcasecmp(text, "timedata") == 0) || (strcasecmp(text, "data") == 0)) {
|
65 : |
|
|
// JMB this is a timevariable
|
66 : |
|
|
mvtype = MVTIME;
|
67 : |
|
|
TV.read(subcomment, TimeInfo, keeper);
|
68 : |
|
|
|
69 : |
|
|
} else if (strcasecmp(text, "stockdata") == 0) {
|
70 : |
|
|
// JMB this is a stockvariable
|
71 : |
|
|
mvtype = MVSTOCK;
|
72 : |
|
|
SV.read(subcomment);
|
73 : |
|
|
|
74 : |
|
|
} else
|
75 : |
|
|
handle.logFileUnexpected(LOGFAIL, "timedata or stockdata", text);
|
76 : |
|
|
|
77 : |
|
|
keeper->clearLast();
|
78 : |
|
|
handle.Close();
|
79 : |
|
|
subfile.close();
|
80 : |
|
|
subfile.clear();
|
81 : |
|
|
}
|
82 : |
|
|
keeper->clearLast();
|
83 : |
|
|
}
|
84 : |
|
|
|
85 : |
|
|
int ModelVariable::didChange(const TimeClass* const TimeInfo) {
|
86 : |
|
|
if (TimeInfo->getTime() == 1)
|
87 : |
|
|
return 1; //return true for the first timestep
|
88 : |
|
|
|
89 : |
|
|
switch (mvtype) {
|
90 : |
|
|
case MVFORMULA:
|
91 : |
|
|
return 0; //return false if the values were not read from file
|
92 : |
|
|
break;
|
93 : |
|
|
case MVTIME:
|
94 : |
|
|
return TV.didChange(TimeInfo); //return based on time step
|
95 : |
|
|
break;
|
96 : |
|
|
case MVSTOCK:
|
97 : |
|
|
return 1; //return true if this is based on the stock population
|
98 : |
|
|
break;
|
99 : |
|
|
default:
|
100 : |
|
|
handle.logMessage(LOGWARN, "Warning in modelvariable - unrecognised type", mvtype);
|
101 : |
|
|
return 0;
|
102 : |
|
|
break;
|
103 : |
|
|
}
|
104 : |
|
|
}
|
105 : |
|
|
|
106 : |
|
|
void ModelVariable::Update(const TimeClass* const TimeInfo) {
|
107 : |
|
|
switch (mvtype) {
|
108 : |
|
|
case MVFORMULA:
|
109 : |
|
|
value = init;
|
110 : |
|
|
break;
|
111 : |
|
|
case MVTIME:
|
112 : |
|
|
TV.Update(TimeInfo);
|
113 : |
|
|
value = TV.getValue();
|
114 : |
|
|
value *= init; // JMB multiply by scaling factor
|
115 : |
|
|
break;
|
116 : |
|
|
case MVSTOCK:
|
117 : |
|
|
SV.Update();
|
118 : |
|
|
value = SV.getValue();
|
119 : |
|
|
value *= init; // JMB multiply by scaling factor
|
120 : |
|
|
handle.logMessage(LOGDETAIL, "Value calculated in stock variable", value);
|
121 : |
|
|
break;
|
122 : |
|
|
default:
|
123 : |
|
|
handle.logMessage(LOGWARN, "Warning in modelvariable - unrecognised type", mvtype);
|
124 : |
|
|
break;
|
125 : |
|
|
}
|
126 : |
|
|
}
|
127 : |
|
|
|
128 : |
|
|
void ModelVariable::Delete(Keeper* const keeper) const {
|
129 : |
|
|
init.Delete(keeper);
|
130 : |
|
|
if (mvtype == MVTIME)
|
131 : |
|
|
TV.Delete(keeper);
|
132 : |
|
|
if (mvtype == MVSTOCK)
|
133 : |
|
|
SV.Delete();
|
134 : |
|
|
}
|
135 : |
|
|
|
136 : |
|
|
void ModelVariable::Interchange(ModelVariable& newMV, Keeper* const keeper) const {
|
137 : |
|
|
newMV.value = value;
|
138 : |
|
|
newMV.mvtype = mvtype;
|
139 : |
|
|
init.Interchange(newMV.init, keeper);
|
140 : |
|
|
if (mvtype == MVTIME)
|
141 : |
|
|
TV.Interchange(newMV.TV, keeper);
|
142 : |
|
|
if (mvtype == MVSTOCK)
|
143 : |
|
|
SV.Interchange(newMV.SV);
|
144 : |
|
|
}
|