forked from ChristophKirst/SimKernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim_control.h
More file actions
229 lines (192 loc) · 5.85 KB
/
sim_control.h
File metadata and controls
229 lines (192 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/***********************************************************************
sim_control.h - Simulation Flow Control
Christoph Kirst
christoph@nld.ds.mpg.de
Max Planck Institue for Dynamics and Self-Organisation
HU Berlin, BCCN Göttingen & Berlin (2008)
************************************************************************/
#ifndef SIM_CONTROL_H
#define SIM_CONTROL_H
#include <sstream>
#include <string.h>
#include "expression_parser.h"
#include "sim_io_manager.h"
#include "sim.h"
#ifndef SIM_PRE
#define SIM_PRE "Sim: "
#endif
template<typename KernelT>
class SimControl
{
private:
// io
SimIOManager io;
// parameter
Sim sim;
// parser
ExprParser parser;
public:
SimControl() {};
int simulate(int argc, char* argv[])
{
io.init(true, SIM_PRE, true, false, "sim.log");
sim.init_io(&io);
if (argc<2)
{
io.message("No input file specified!", Exit);
io.close();
return int(Exit);
};
std::string err;
if (!parser.parse_file(argv[1], err))
{
io.message(err);
io.message("Simulation initialization failed!", Exit);
io.close();
return int(Exit);
};
if (!sim.init(parser.result, err))
{
io.message(err);
io.message("Simulation initialization failed!", Exit);
io.close();
return int(Exit);
};
io.set_iterations(sim.n_iterations());
long itstart = 1;
long itend = sim.n_iterations();
if (argc==3)
{
if (strcmp(argv[2], "iterations")==0) { // return number of iterations to stdout
std::cout << sim.n_iterations() << std::endl;
return int(Success);
} else {
std::stringstream str(argv[2]);
str >> itstart;
itend = itstart;
}
}
if (argc==4)
{
std::stringstream strs(argv[2]);
strs >> itstart;
std::stringstream stre(argv[3]);
stre >> itend;
if (itend<0) itend = sim.n_iterations()+1+itend;
}
if (itstart <= 0 || itstart > sim.n_iterations() || itend<=0 || itend > sim.n_iterations()) {
io.message("Invalid Iteration number!");
io.close();
return int(Exit);
}
SimSignal reterr = Success;
while (sim.next_iteration())
{
if (itstart <= sim.iteration() && sim.iteration() <= itend) {
try {
std::stringstream str;
str << "Starting Simulation iteration: " << (sim.iteration());
str << "/" << sim.n_iterations() << std::endl;
io.message(str.str());
KernelT kernel;
kernel.initialize(sim);
kernel.execute(sim);
kernel.finalize(sim);
std::stringstream str2;
str2 << "Simulation iteration: " << (sim.iteration());
str2 << "/" << sim.n_iterations() << " done!" << std::endl;
io.message(str2.str());
}
catch (const SimSignal& e)
{
SIM_DEBUG("catch sig=", e)
switch (e)
{
case Abort:
io.message("Simulation run aborted due to error!");
reterr = Abort;
break;
case Exit:
default:
io.message("Simulation aborted due to fatal error!");
io.summary();
io.close();
return int(Exit);
}
}
} // check for iteration range
} // while
io.message("Simulation done!\n");
io.error_summary();
io.message("Bye!");
io.close();
return int(reterr);
};
int simulate_online(int argc, char* argv[])
{
int in = 1;
io.init(true, SIM_PRE, true, false, "sim.log");
sim.init_io(&io);
std::string err;
ExprPtrT g = ExprPtrT(new ExprGlobal());
if (!sim.init(g, err))
{
io.message(err);
io.message("Sim initialization failed!", Exit);
io.close();
return int(Exit);
};
std::string cmd;
err = "";
cout << "In["<<in<<"]:";
//cin >> cmd;
getline(cin, cmd);
while (cmd != "Quit[]" && cmd != "q")
{
if (!parser.parse(cmd, err))
{
io.message("Cannot parse command: " + cmd);
io.message(err);
err = "";
} else {
ExprPtrT res, resprt;
//cout << parser.result << std::endl;
resprt = new ExprBlock();
resprt->arg = parser.result->arg;
cout << "Parse[" <<in <<"]:" << resprt << std::endl;
cout << "Out["<<in<<"]:"; // for intermediate ouputs such as Print etc
try {
ExprArgT::iterator it = resprt->arg.begin();
while (it != resprt->arg.end() )
{
res = it->get()->evaluate(&sim.scope);
it++;
};
// make global header to block
//cout << parser.result << std::endl;
//res = resprt->evaluate(&sim.scope);
}
catch (const SimSignal& e)
{
io.message("evaluation error!");
switch (e)
{
case Abort:
case Exit:
cmd = "q"; break;
default:
cmd = ""; break;
}
}
cout << std::endl << "Result["<<in<<"]:" << res << std::endl;
}
in++;
cout << "In["<<in<<"]:";
getline(cin, cmd);
}; // while
io.message("Bye!");
io.close();
return 0;
};
};
#endif