-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulationSetup.cpp
More file actions
231 lines (197 loc) · 9.42 KB
/
SimulationSetup.cpp
File metadata and controls
231 lines (197 loc) · 9.42 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
230
231
//
// Created by jan on 29/10/18.
//
#include "SimulationSetup.h"
#include "src/io/ConfigFileInterpreter.h"
#include "src/simulator/SimulatorSsa.h"
#include "src/simulator/SimulatorOde.h"
SimulationSetup::SimulationSetup(options::SimulationOptions &options) : GeneralSetup(options), _sim_options(options) {}
SimulationSetup::~SimulationSetup() {}
void SimulationSetup::setUp() {
_readSettingsfromFile();
rng = std::make_shared<base::RandomNumberGenerator>(time(NULL));
for (std::string experiment : experiments) {
models::FullModel_ptr full_model = std::make_shared<models::FullModel>(model_settings, rng, experiment);
full_models.push_back(full_model);
simulator::Simulator_ptr simulator = _createSimulator(full_model->dynamics);
simulator->setDiscontTimes(full_model->getDiscontTimes());
simulators.push_back(simulator);
}
_createParameterVector();
_createOutputTimes();
}
void SimulationSetup::_readSettingsfromFile() {
GeneralSetup::_readSettingsfromFile();
_readSimulationSettings();
for (std::string experiment : experiments) {
std::vector<models::InputData_ptr> datas = _getInputDatasForExperiment(experiment, final_time);
if (!datas.empty()) { model_settings.input_datas[experiment] = datas; }
}
if (model_settings.input_datas.size() < experiments.size() - 1) {
std::cerr << "Warning: " << experiments.size() << " different experiments provided, but only "
<< model_settings.input_datas.size()
<< " different inputs provided. Experiments may be indistinguishible from one another!" << std::endl;
}
}
std::vector<std::string> SimulationSetup::_readExperiments() {
try {
return interpreter.getExperimentsForSimulations();
} catch (const std::exception &e) {
std::cerr << "Failed to read experiments for simulation:\n\t" << e.what() << std::endl;
std::cerr << "Dummy experiment '0' will be simulated." << std::endl;
return {"0"};
}
}
void SimulationSetup::printSettings(std::ostream &os) {
GeneralSetup::printSettings(os);
os << "\n---------- Simulation Settings ----------" << std::endl;
if (!parameter_file.empty()) {
os << "Parameters will be read from " << parameter_file << std::endl;
} else {
os << "\nParameters for simulation:" << std::endl;
}
std::size_t max_name_length = 0;
for (std::string ¶m_name : sim_param_names) {
if (max_name_length < param_name.size()) { max_name_length = param_name.size(); }
}
std::size_t max_value_length = 16 + 8;
os << std::setw(max_name_length) << "Name ";
if (parameter_file.empty()) { os << std::setw(max_value_length) << "simulation value"; }
os << std::endl;
int index = 0;
for (std::string ¶m_name : sim_param_names) {
os << std::setw(max_name_length) << param_name;
std::stringstream value_str;
if (parameter_file.empty()) {
value_str << parameter[index++];
os << std::setw(max_value_length) << value_str.str();
}
os << std::endl;
}
os << std::endl;
os << "Experiments for simulation: ";
for (std::string &experiment: experiments) { os << experiment << ", "; }
os << std::endl;
os << "\n---------- Model Settings ----------" << std::endl;
model_settings.print(os);
if (model_settings.model_type == models::MODEL_TYPE::HYBRID ||
model_settings.model_type == models::MODEL_TYPE::ODE) {
simulator::OdeSettings ode_settings = _readOdeSettings();
os << "Settings for ODE simulator:" << std::endl;
os << std::setw(30) << std::left << "Minimal step size:" << ode_settings.min_step_size << std::endl;
os << std::setw(30) << std::left << "Maximal number of steps:" << ode_settings.max_num_steps << std::endl;
os << std::setw(30) << std::left << "Maximal error fails:" << ode_settings.max_error_fails << std::endl;
os << std::setw(30) << std::left << "Absolute tolerance:" << ode_settings.abs_tol << std::endl;
os << std::setw(30) << std::left << "Relative tolerance:" << ode_settings.rel_tol << std::endl;
}
os << std::endl;
if (full_models.empty()) { std::cerr << "No models created!!" << std::endl; }
else { full_models.back()->printInfo(os); }
}
void SimulationSetup::_readSimulationSettings() {
sim_param_names = model_settings.getUnfixedParameters();
if (_sim_options.vm.count("parameter") > 0) { parameter = _sim_options.params; }
else if (_sim_options.vm.count("parameter-file") > 0) { parameter_file = _sim_options.param_file; }
else {
parameter_file = interpreter.getParameterFileforSimulation();
parameter = interpreter.getParamForSimulation();
if (parameter.size() > 0 && parameter_file.size() > 0) {
std::stringstream ss;
ss << "In config file only a parameter vector or a parameter file can be provided, but not both!"
<< std::endl;
throw std::runtime_error(ss.str());
}
if (!sim_param_names.empty() && parameter.size() == 0 && parameter_file.size() == 0) {
std::stringstream ss;
ss
<< "For simulation either a parameter vector (either with -p through the command line or 'Simulation.parameter' through config file) or a file with parameters (either with -P through the command line or 'Simulation.parameter_file' through config file) needs to be provided"
<< std::endl;
throw std::runtime_error(ss.str());
}
}
if (sim_param_names.empty()) {
if (!parameter.empty()) {
std::cerr
<< "Warning: No parameters needed for simulation, but simulation parameters provided! Provided parameters will be ignored!"
<< std::endl;
parameter.clear();
}
if (!parameter_file.empty()) {
std::cerr
<< "Warning: No parameters needed for simulation, but simulation parameter file provided! Provided parameters will be ignored!"
<< std::endl;
parameter_file.clear();
}
}
if (!parameter.empty()) {
if (parameter.size() != sim_param_names.size()) {
std::stringstream ss;
ss << "Number of simulation parameters are not consistent. Number of parameters for simulation:"
<< sim_param_names.size();
ss << ", but " << parameter.size() << " parameters for simulation provided!"
<< std::endl;
ss << "Parameters for simulation: " << std::endl;
for (std::string param_name: sim_param_names) { ss << param_name << std::endl; }
ss << "Additional fixed model parameters: " << std::endl;
std::map<std::string, double>::iterator it;
for (it = model_settings.fixed_parameters.begin(); it != model_settings.fixed_parameters.end(); it++) {
ss << it->first << ":\t" << it->second << std::endl;
}
throw std::runtime_error(ss.str());
}
}
if (_sim_options.num_simulations > 0) { number_simulations = _sim_options.num_simulations; }
else {
int n = interpreter.getNForSimulation();
if (n > 0) { number_simulations = n; }
else {
std::cerr
<< "No number of simulations (either with -n through command line or 'Simulation.num_simulations') provided, assume default value of "
<< number_simulations << std::endl;
}
}
if (_sim_options.initialTimeProvided()) {
initial_time = _sim_options.initial_time;
final_time = _sim_options.final_time;
interval = _sim_options.interval;
} else if (_sim_options.timepointsProvided()) {
times = _sim_options.time_points;
} else {
try { initial_time = interpreter.getInitialTimeForSimulation(); } catch (
const std::exception &e) {
std::cerr
<< "Failed to read initial time for simulation (either provided in command line with -I or in the config file with 'Simulation.initialtime'. Assume default value of "
<< initial_time << std::endl;
}
try { final_time = interpreter.getFinalTimeForSimulation(); } catch (
const std::exception &e) {
std::cerr
<< "Failed to read final time for simulation (either provided in command line with -F or in the config file with 'Simulation.finaltime'. Assume default value of "
<< final_time << std::endl;
}
try { interval = interpreter.getIntervalForSimulation(); } catch (const std::exception &e) {
std::cerr
<< "Failed to read interval for for simulation readout (either provided in command line with -i or in the config file with 'Simulation.interval'. Assume default value of "
<< interval << std::endl;
}
}
}
void SimulationSetup::_createOutputTimes() {
if (times.empty()) {
double t = initial_time;
while (t <= final_time) {
times.push_back(t);
t += interval;
}
}
}
void SimulationSetup::_createParameterVector() {
if (sim_param_names.empty()) {
parameters.push_back(std::vector<double>());
return; }
if (!parameter.empty()) {
parameters.push_back(parameter);
} else {
parameters = base::IoUtils::readVectorOfVectors(parameter_file);
}
}