forked from ChristophKirst/SimKernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim.cpp
266 lines (214 loc) · 5.27 KB
/
sim.cpp
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/***************************************************************************
sim.cpp - interface between parameter expressions
and simulation kernel
Christoph Kirst
Max Planck Institue for Dynamics and Self-Organisation Göttingen
HU Berlin, BCCN Göttingen & Berlin (2008)
****************************************************************************/
#include "sim.h"
#include "sim_signals.h"
#ifdef SIM_MPI
#include "sim_mpi.h"
#endif
Sim::Sim() : io(this), err(this), msg(this), global(ExprNullPtr()), proc(-1), iter(-1), n_iters(0)
{
SIM_DEBUG("Sim::creator() global=", global<< " proc =" << proc << " global.ptr=" << global.ptr )
};
Sim::Sim(const ExprPtrT& g) : io(this), err(this), msg(this), global(g), proc(-1), iter(-1), n_iters(0)
{
};
void Sim::clear()
{
scope.clear();
global->clear();
proc = -1;
iter = -1;
n_iters = 0;
};
bool Sim::init(const ExprPtrT& g, std::string& err, int p)
{
SIM_DEBUG("Sim::init() g=", g)
SIM_DEBUG("Sim::init() p=", p)
SIM_DEBUG("Sim::init() proc=", proc)
SIM_DEBUG("Sim::init() global=", global)
global = g;
proc = p;
iter = 0;
try
{
global->evaluate(&scope);
n_iters = ExprIterator::count_iterations(&scope);
}
catch (const ExpressionError& exprerr)
{
SIM_DEBUG("Sim::init() error while evaluating global:", exprerr.what())
err = std::string("Parameters could not be initialized: " + exprerr.what());
return false;
};
SIM_DEBUG("Sim::init() sucess! n_iters=", n_iters)
SIM_DEBUG("Sim::init() sucess! scope=", scope.info())
return true;
};
#ifndef SIM_MPI
void Sim::init_io(SimIOManager* iomngr_)
{
iomngr = iomngr_;
};
#endif
bool Sim::next_iteration()
{
iter++;
io.release(); err.release(); msg.release();
try
{
return ExprIterator::next_iteration(&scope);
}
catch (const ExpressionError& exprerr)
{
iter = -1;
io << "Expression error while increasing iteration: " << exprerr.what() << Exit;
return false;
};
};
int Sim::n_iterations()
{
return n_iters;
};
int Sim::iteration()
{
return iter;
};
int Sim::process()
{
return proc;
};
#ifdef SIM_MPI
void Sim::signal(const SimSignal& sig, const std::string& str)
{
SIM_DEBUG("proc="<<proc << " iter="<<iter, "signal="<<sig<<" msg="<<str)
sim_send_signal(sig, iter, SIM_MPI_CNTRL);
if (is_io_signal(sig))
{
sim_send_message(str, SIM_MPI_CNTRL);
};
if (is_throw_signal(sig))
{
throw sig;
};
};
void Sim::output_level(const SimSignal& sig)
{
int s = to_output_signal(sig);
sim_send_signal(s, iter, SIM_MPI_CNTRL);
};
#else //no MPI
void Sim::signal(const SimSignal& sig, const std::string& str)
{
SIM_DEBUG("no MPI! proc="<<proc << " iter="<<iter, "signal="<<sig<<" msg="<<str)
// todo
if (is_io_signal(sig))
{
iomngr->message(str, sig, iter);
};
SIM_DEBUG("is_throw_signal", is_throw_signal(sig) )
if (is_throw_signal(sig))
{
SIM_DEBUG("throwing sig=", sig)
throw sig;
};
};
void Sim::output_level(const SimSignal& sig)
{
iomngr->set_output_level(to_output_signal(sig));
};
#endif
bool Sim::defined(const ExprNameT& name)
{
return (scope.defined(name));
};
bool Sim::defined_as(const ExprNameT& name, const ExprNameT& name2)
{
ExprPtrT e = scope.match(name);
if (e == ExprNoMatchPtr()) return false;
return (e->name() == name2);
};
/*
bool Sim::defined_as_creator(const ExprNameT& name)
{
return defined_as(name, ExprCreator::Name);
};
*/
std::string Sim::info()
{
SIM_DEBUG("Sim.info()", "")
std::stringstream str;
str << "Sim: process = " << process() << " iteration = " << iteration() <<
str << " n_iterations = " << n_iterations() << std::endl;
str << "global = " << global << std::endl;
str << "scope:" << std::endl << scope.info();
return str.str();
};
bool Sim::get_expr(const ExprNameT& n, ExprPtrT& e, const SimSignal& sig)
{
bool eo;
if (!get_expr_nothrow(n,e, sig, eo))
{
if (eo) err << sig;
return false;
};
return true;
};
bool Sim::get(const ExprNameT& n, ExprPtrT& e, const SimSignal& sig)
{
return get_expr(n,e,sig);
};
bool Sim::get_definition(const ExprNameT& n, ExprPtrT& e, const SimSignal& sig)
{
bool eo = is_io_signal(sig);
e = scope.match(n);
if (e == ExprNoMatchPtr())
{
if (eo) err << "Cannot get definition for " << n << "!" << sig;
e = ExprNullPtr();
return false;
}
return true;
};
bool Sim::define(const ExprNameT& name, const ExprPtrT& expr)
{
scope.define(name, expr);
return true;
};
// private member functions
// inline
bool Sim::get_expr_nothrow(const ExprNameT& n, ExprPtrT& e, const SimSignal& sig, bool& eo)
{
eo = is_io_signal(sig);
try
{
e = scope.match(n);
if (e == ExprNoMatchPtr())
{
if (eo) err << "Cannot get value for " << n << "! Not Defined!";
e = ExprNullPtr();
return false;
}
e = e->evaluate(&scope);
}
catch (const ExpressionError& exprerr)
{
if (eo) err << exprerr.what();
e = ExprNullPtr();
return false;
};
return true;
};
std::string Sim::file_extension()
{
std::ostringstream str;
str.width(4);
str.fill('0');
str << iteration();
return std::string(".") + str.str();
};