1 : |
agomez |
1 |
#include "timevariable.h" |
2 : |
|
|
#include "errorhandler.h" |
3 : |
|
|
#include "readword.h" |
4 : |
|
|
#include "gadget.h" |
5 : |
|
|
#include "global.h" |
6 : |
|
|
|
7 : |
|
|
void TimeVariable::read(CommentStream& infile,
|
8 : |
|
|
const TimeClass* const TimeInfo, Keeper* const keeper) {
|
9 : |
|
|
|
10 : |
|
|
int i, check, tmpint;
|
11 : |
|
|
char text[MaxStrLength];
|
12 : |
|
|
strncpy(text, "", MaxStrLength);
|
13 : |
|
|
|
14 : |
|
|
i = 0;
|
15 : |
|
|
while (!infile.eof()) {
|
16 : |
|
|
infile >> ws >> tmpint;
|
17 : |
|
|
years.resize(1, tmpint);
|
18 : |
|
|
if (infile.fail())
|
19 : |
|
|
handle.logFileMessage(LOGFAIL, "failed to read timevariable year");
|
20 : |
|
|
infile >> ws >> tmpint;
|
21 : |
|
|
steps.resize(1, tmpint);
|
22 : |
|
|
if (infile.fail())
|
23 : |
|
|
handle.logFileMessage(LOGFAIL, "failed to read timevariable step");
|
24 : |
|
|
|
25 : |
|
|
values.resize(1, keeper);
|
26 : |
|
|
infile >> ws >> values[i];
|
27 : |
|
|
if (infile.fail())
|
28 : |
|
|
handle.logFileMessage(LOGFAIL, "failed to read timevariable value");
|
29 : |
|
|
values[i].Inform(keeper);
|
30 : |
|
|
|
31 : |
|
|
infile >> ws;
|
32 : |
|
|
i++;
|
33 : |
|
|
}
|
34 : |
|
|
|
35 : |
|
|
handle.logMessage(LOGMESSAGE, "Read timevariable data - number of timesteps", years.Size());
|
36 : |
|
|
|
37 : |
|
|
//check if years and steps are ordered in time
|
38 : |
|
|
for (i = 0; i < years.Size() - 1; i++)
|
39 : |
|
|
if ((years[i + 1] < years[i]) ||
|
40 : |
|
|
(years[i + 1] == years[i] && steps[i + 1] <= steps[i]))
|
41 : |
|
|
handle.logMessage(LOGFAIL, "Error in timevariable - years and steps are not increasing");
|
42 : |
|
|
|
43 : |
|
|
check = -1;
|
44 : |
|
|
for (i = 0; i < years.Size(); i++)
|
45 : |
|
|
if (years[i] == TimeInfo->getFirstYear() && steps[i] == TimeInfo->getFirstStep())
|
46 : |
|
|
check = i;
|
47 : |
|
|
|
48 : |
|
|
if (check == -1)
|
49 : |
|
|
handle.logMessage(LOGFAIL, "Error in timevariable - nothing specified for first timestep of the simulation");
|
50 : |
|
|
}
|
51 : |
|
|
|
52 : |
|
|
int TimeVariable::didChange(const TimeClass* const TimeInfo) {
|
53 : |
|
|
if (TimeInfo->getTime() == 1)
|
54 : |
|
|
return 1; //return true for the first timestep
|
55 : |
|
|
|
56 : |
|
|
return ((!(isEqual(lastvalue, value))) && (time == TimeInfo->getTime()));
|
57 : |
|
|
}
|
58 : |
|
|
|
59 : |
|
|
void TimeVariable::Update(const TimeClass* const TimeInfo) {
|
60 : |
|
|
if (TimeInfo->getTime() == 1)
|
61 : |
|
|
timeid = 0;
|
62 : |
|
|
|
63 : |
|
|
int i;
|
64 : |
|
|
for (i = timeid; i < steps.Size(); i++) {
|
65 : |
|
|
if (steps[i] == TimeInfo->getStep() && years[i] == TimeInfo->getYear()) {
|
66 : |
|
|
timeid = i;
|
67 : |
|
|
time = TimeInfo->getTime();
|
68 : |
|
|
break;
|
69 : |
|
|
}
|
70 : |
|
|
}
|
71 : |
|
|
|
72 : |
|
|
lastvalue = value;
|
73 : |
|
|
value = values[timeid];
|
74 : |
|
|
}
|
75 : |
|
|
|
76 : |
|
|
void TimeVariable::Delete(Keeper* const keeper) const {
|
77 : |
|
|
int i;
|
78 : |
|
|
for (i = 0; i < values.Size(); i++)
|
79 : |
|
|
values[i].Delete(keeper);
|
80 : |
|
|
}
|
81 : |
|
|
|
82 : |
|
|
void TimeVariable::Interchange(TimeVariable& newTV, Keeper* const keeper) const {
|
83 : |
|
|
int i;
|
84 : |
|
|
newTV.value = value;
|
85 : |
|
|
newTV.lastvalue = lastvalue;
|
86 : |
|
|
newTV.timeid = timeid;
|
87 : |
|
|
newTV.time = time;
|
88 : |
|
|
newTV.years = years;
|
89 : |
|
|
newTV.steps = steps;
|
90 : |
|
|
newTV.values.resize(values.Size(), keeper);
|
91 : |
|
|
for (i = 0; i < steps.Size(); i++)
|
92 : |
|
|
values[i].Interchange(newTV.values[i], keeper);
|
93 : |
|
|
}
|