/
OMCFactory.cpp
318 lines (272 loc) · 12 KB
/
OMCFactory.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/** @addtogroup simcorefactoryOMCFactory
*
* @{
*/
#include <Core/ModelicaDefine.h>
#include <Core/Modelica.h>
#include <SimCoreFactory/OMCFactory/OMCFactory.h>
#include <Core/SimController/ISimController.h>
#include <boost/algorithm/string.hpp>
#include <boost/container/vector.hpp>
OMCFactory::OMCFactory(PATH library_path, PATH modelicasystem_path)
: _library_path(library_path)
, _modelicasystem_path(modelicasystem_path)
, _defaultLinSolver("kinsol")
, _defaultNonLinSolver("kinsol")
{
}
OMCFactory::OMCFactory()
: _library_path("")
, _modelicasystem_path("")
, _defaultLinSolver("kinsol")
, _defaultNonLinSolver("kinsol")
{
}
OMCFactory::~OMCFactory()
{
}
void OMCFactory::UnloadAllLibs(void)
{
map<string,shared_library>::iterator iter;
for(iter = _modules.begin();iter!=_modules.end();++iter)
{
UnloadLibrary(iter->second);
}
}
// parse a long option that starts with one dash, like -port=12345
static pair<string, string> checkOMEditOption(const string &s)
{
int sep = s.find("=");
if (sep > 2 && s[0] == '-' && s[1] != '-')
return make_pair(string("OMEdit"), s);
else
return make_pair(string(), string());
}
SimSettings OMCFactory::readSimulationParameter(int argc, const char* argv[])
{
int opt;
int portnum;
std::map<std::string,LogCategory> logCatMap = map_list_of("init", LC_INIT)("nls", LC_NLS)("ls",LC_LS)("solv", LC_SOLV)("output", LC_OUT)("event",LC_EVT)("model",LC_MOD)("other",LC_OTHER);
std::map<std::string,LogLevel> logLvlMap = map_list_of("error", LL_ERROR)("warning", LL_WARNING)("info", LL_INFO)("debug", LL_DEBUG);
std::map<std::string,OutputPointType> outputPointTypeMap = map_list_of("all", OPT_ALL)("step", OPT_STEP)("none", OPT_NONE);
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("emit_protected", "emits protected variables to the result file")
("nls_continue", po::bool_switch()->default_value(false),"non linear solver will continue if it can not reach the given precision")
("runtime-library,R", po::value<string>(),"path to cpp runtime libraries")
("Modelica-system-library,M", po::value<string>(), "path to Modelica library")
("results-file,r", po::value<string>(),"name of results file")
//("config-path,f", po::value< string >(), "path to xml files")
("start-time,S", po::value< double >()->default_value(0.0), "simulation start time")
("stop-time,E", po::value< double >()->default_value(1.0), "simulation stop time")
("step-size,H", po::value< double >()->default_value(0.0), "simulation step size")
("solver,I", po::value< string >()->default_value("euler"), "solver method")
("lin-solver,L", po::value< string >()->default_value(_defaultLinSolver), "linear solver method")
("non-lin-solver,N", po::value< string >()->default_value(_defaultNonLinSolver), "non linear solver method")
("number-of-intervals,G", po::value< int >()->default_value(500), "number of intervals in equidistant grid")
("tolerance,T", po::value< double >()->default_value(1e-6), "solver tolerance")
("log-settings,V", po::value< std::vector<std::string> >(), "log information: init, nls, ls, solv, output, event, model, other")
("alarm,A", po::value<unsigned int >()->default_value(360), "sets timeout in seconds for simulation")
("output-type,O", po::value< string >()->default_value("all"), "the points in time written to result file: all (output steps + events), step (just output points), none")
("OMEdit", po::value<vector<string> >(), "OMEdit options")
;
po::variables_map vm;
po::parsed_options parsed = po::command_line_parser(argc, argv)
.options(desc)
.style((po::command_line_style::default_style | po::command_line_style::allow_long_disguise) & ~po::command_line_style::allow_guessing)
.extra_parser(checkOMEditOption)
.allow_unregistered()
.run();
po::store(parsed, vm);
po::notify(vm);
// warn about unrecognized command line options, including OMEdit for now
vector<string> unrecognized = po::collect_unrecognized(parsed.options, po::include_positional);
if (vm.count("OMEdit")) {
vector<string> opts = vm["OMEdit"].as<vector<string> >();
unrecognized.insert(unrecognized.begin(), opts.begin(), opts.end());
}
if (unrecognized.size() > 0) {
std::cerr << "Warning: unrecognized command line options ";
std::copy(unrecognized.begin(), unrecognized.end(), std::ostream_iterator<string>(std::cerr, " "));
std::cerr << std::endl;
}
string runtime_lib_path;
string modelica_lib_path;
double starttime = vm["start-time"].as<double>();
double stoptime = vm["stop-time"].as<double>();
double stepsize =vm["step-size"].as<double>();
bool nlsContinueOnError = vm["nls_continue"].as<bool>();
if (!(stepsize > 0.0))
stepsize = (stoptime - starttime) / vm["number-of-intervals"].as<int>();
double tolerance =vm["tolerance"].as<double>();
string solver = vm["solver"].as<string>();
string nonLinSolver = vm["non-lin-solver"].as<string>();
string linSolver = vm["lin-solver"].as<string>();
unsigned int time_out = vm["alarm"].as<unsigned int>();;
if (vm.count("runtime-library"))
{
//cout << "runtime library path set to " << vm["runtime-library"].as<string>() << std::endl;
runtime_lib_path = vm["runtime-library"].as<string>();
}
else
{
throw ModelicaSimulationError(MODEL_FACTORY,"runtime libraries path is not set");
}
if (vm.count("Modelica-system-library"))
{
//cout << "Modelica library path set to " << vm["Modelica-system-library"].as<string>() << std::endl;
modelica_lib_path =vm["Modelica-system-library"].as<string>();
}
else
{
throw ModelicaSimulationError(MODEL_FACTORY,"Modelica library path is not set");
}
string resultsfilename;
if (vm.count("results-file"))
{
//cout << "results file: " << vm["results-file"].as<string>() << std::endl;
resultsfilename = vm["results-file"].as<string>();
}
else
{
throw ModelicaSimulationError(MODEL_FACTORY,"results-filename is not set");
}
string outputPointType_str;
OutputPointType outputPointType;
if (vm.count("output-type"))
{
//cout << "results file: " << vm["results-file"].as<string>() << std::endl;
outputPointType_str = vm["output-type"].as<string>();
outputPointType = outputPointTypeMap[outputPointType_str];
}
else
{
throw ModelicaSimulationError(MODEL_FACTORY, "output-type is not set");
}
LogSettings logSet;
if (vm.count("log-settings"))
{
std::vector<std::string> log_vec = vm["log-settings"].as<std::vector<string> >(),tmpvec;
for(unsigned i=0;i<log_vec.size();++i)
{
cout << i << ". " << log_vec[i] << std::endl;
tmpvec.clear();
boost::split(tmpvec,log_vec[i],boost::is_any_of("="));
if(tmpvec.size()>1 && logLvlMap.find(tmpvec[1]) != logLvlMap.end() && ( tmpvec[0] == "all" || logCatMap.find(tmpvec[0]) != logCatMap.end()))
{
if(tmpvec[0] == "all")
{
logSet.setAll(logLvlMap[tmpvec[1]]);
break;
}
else
logSet.modes[logCatMap[tmpvec[0]]] = logLvlMap[tmpvec[1]];
}
else
throw ModelicaSimulationError(MODEL_FACTORY,"log-settings flags not supported: " + boost::lexical_cast<std::string>(log_vec[i]) + "\n");
}
}
fs::path libraries_path = fs::path( runtime_lib_path) ;
fs::path modelica_path = fs::path( modelica_lib_path) ;
libraries_path.make_preferred();
modelica_path.make_preferred();
SimSettings settings = {solver,linSolver,nonLinSolver,starttime,stoptime,stepsize,1e-24,0.01,tolerance,resultsfilename,time_out,outputPointType,logSet,nlsContinueOnError};
_library_path = libraries_path;
_modelicasystem_path = modelica_path;
return settings;
}
std::vector<const char *> OMCFactory::preprocessArguments(int argc, const char* argv[], std::map<std::string, std::string> &opts)
{
std::map<std::string, std::string>::const_iterator oit;
std::vector<const char *> optv;
optv.push_back(argv[0]);
std::string override; // OMEdit override option
for (int i = 1; i < argc; i++) {
string arg = argv[i];
int j;
if (arg[0] == '-' && arg[1] != '-' && (j = arg.find('=')) > 0
&& (oit = opts.find(arg.substr(0, j))) != opts.end())
opts[oit->first] = arg.substr(j + 1); // split at = and override
else if ((oit = opts.find(arg)) != opts.end() && i < argc - 1)
opts[oit->first] = argv[++i]; // regular override
else if (strncmp(argv[i], "-override=", 10) == 0) {
std::map<std::string, std::string> supported = map_list_of
("startTime", "-S")("stopTime", "-E")("stepSize", "-H")
("numberOfIntervals", "-G")("solver", "-I")("tolerance", "-T")
("outputFormat", "-O");
std::vector<std::string> strs;
boost::split(strs, argv[i], boost::is_any_of(",="));
for (int j = 1; j < strs.size(); j++) {
if ((oit = supported.find(strs[j])) != supported.end()
&& j < strs.size() - 1) {
opts[oit->second] = strs[++j];
}
else {
// leave untreated overrides
if (override.size() > 0)
override += ",";
else
override = "-override=";
override += strs[j];
if (j < strs.size() - 1)
override += "=" + strs[++j];
}
}
if (override.size() > 10)
optv.push_back(override.c_str());
}
else
optv.push_back(argv[i]); // pass through
}
for (oit = opts.begin(); oit != opts.end(); oit++) {
optv.push_back(oit->first.c_str());
optv.push_back(oit->second.c_str());
}
return optv;
}
std::pair<boost::shared_ptr<ISimController>,SimSettings>
OMCFactory::createSimulation(int argc, const char* argv[],
std::map<std::string, std::string> &opts)
{
std::vector<const char *> optv = preprocessArguments(argc, argv, opts);
SimSettings settings = readSimulationParameter(optv.size(), &optv[0]);
type_map simcontroller_type_map;
PATH simcontroller_path = _library_path;
PATH simcontroller_name(SIMCONTROLLER_LIB);
simcontroller_path/=simcontroller_name;
LOADERRESULT result = LoadLibrary(simcontroller_path.string(),simcontroller_type_map);
if (result != LOADER_SUCCESS)
{
throw ModelicaSimulationError(MODEL_FACTORY,string("Failed loading SimConroller library!") + simcontroller_path.string());
}
std::map<std::string, factory<ISimController,PATH,PATH> >::iterator iter;
std::map<std::string, factory<ISimController,PATH,PATH> >& factories(simcontroller_type_map.get());
iter = factories.find("SimController");
if (iter ==factories.end())
{
throw ModelicaSimulationError(MODEL_FACTORY,"No such SimController library");
}
boost::shared_ptr<ISimController> simcontroller = boost::shared_ptr<ISimController>(iter->second.create(_library_path,_modelicasystem_path));
return std::make_pair(simcontroller,settings);
}
LOADERRESULT OMCFactory::LoadLibrary(string libName,type_map& current_map)
{
shared_library lib;
if(!load_single_library(current_map,libName,lib))
return LOADER_ERROR;
_modules.insert(std::make_pair(libName,lib));
return LOADER_SUCCESS;
}
LOADERRESULT OMCFactory::UnloadLibrary(shared_library lib)
{
if(lib.is_open())
{
if(!lib.close())
return LOADER_ERROR;
else
return LOADER_SUCCESS;
}
return LOADER_SUCCESS;
}
/** @} */ // end of simcorefactoryOMCFactory