|
55 | 55 | #include "OMSimulator/Types.h" |
56 | 56 | #include "Version.h" |
57 | 57 |
|
58 | | -#include <chrono> |
59 | | -#include <condition_variable> |
60 | | -#include <iostream> |
61 | 58 | #include "miniunz.h" |
62 | | -#include <mutex> |
63 | | -#include <pugixml.hpp> |
| 59 | +#include "pugixml.hpp" |
| 60 | + |
| 61 | +#include <chrono> |
| 62 | +#include <future> |
64 | 63 | #include <string> |
65 | 64 | #include <thread> |
66 | 65 |
|
@@ -1169,60 +1168,93 @@ static int do_simulation(std::string model, std::chrono::duration<double> timeou |
1169 | 1168 | return 0; |
1170 | 1169 | } |
1171 | 1170 |
|
1172 | | -oms_status_enu_t oms_RunFile(const char* filename) |
| 1171 | +oms_status_enu_t SimulateSingleFMU(const filesystem::path& path) |
1173 | 1172 | { |
1174 | 1173 | oms_status_enu_t status; |
1175 | | - filesystem::path path(filename); |
1176 | | - std::string type = path.extension().string(); |
| 1174 | + std::string modelName("model"); |
| 1175 | + std::string systemName = modelName + ".root"; |
| 1176 | + std::string fmuName = systemName + (oms::ComRef::isValidIdent(path.stem().string()) ? ("." + path.stem().string()) : ".fmu"); |
| 1177 | + oms_fmi_kind_enu_t kind; |
1177 | 1178 |
|
1178 | | - if (type == ".fmu") |
1179 | | - { |
1180 | | - std::string modelName("model"); |
1181 | | - std::string systemName = modelName + ".root"; |
1182 | | - std::string fmuName = systemName + (oms::ComRef::isValidIdent(path.stem().string()) ? ("." + path.stem().string()) : ".fmu"); |
1183 | | - oms_fmi_kind_enu_t kind; |
| 1179 | + /* intialize the defaultExperiment with defaults set in Flags.cpp setDefaults() or |
| 1180 | + * values provided from user from commandLine (e.g) OMSimulator A.fmu --stopTime=10 --stepSize=10 |
| 1181 | + */ |
| 1182 | + oms_fmu_default_experiment_settings defaultExperiment = {oms::Flags::StartTime(), oms::Flags::StopTime(), oms::Flags::Tolerance(), oms::Flags::MaximumStepSize()}; |
1184 | 1183 |
|
1185 | | - /* intialize the defaultExperiment with defaults set in Flags.cpp setDefaults() or |
1186 | | - * values provided from user from commandLine (e.g) OMSimulator A.fmu --stopTime=10 --stepSize=10 |
1187 | | - */ |
1188 | | - oms_fmu_default_experiment_settings defaultExperiment = {oms::Flags::StartTime(), oms::Flags::StopTime(), oms::Flags::Tolerance(), oms::Flags::MaximumStepSize()}; |
| 1184 | + status = oms_extractFMIKind(path.string().c_str(), &kind, &defaultExperiment); |
| 1185 | + if (oms_status_ok != status) return logError("oms_extractFMIKind failed"); |
1189 | 1186 |
|
1190 | | - status = oms_extractFMIKind(filename, &kind, &defaultExperiment); |
1191 | | - if (oms_status_ok != status) return logError("oms_extractFMIKind failed"); |
| 1187 | + status = oms_newModel(modelName.c_str()); |
| 1188 | + if (oms_status_ok != status) return logError("oms_newModel failed"); |
1192 | 1189 |
|
1193 | | - status = oms_newModel(modelName.c_str()); |
1194 | | - if (oms_status_ok != status) return logError("oms_newModel failed"); |
| 1190 | + oms::Model* model = oms::Scope::GetInstance().getModel(oms::ComRef(modelName)); |
| 1191 | + if (!model) |
| 1192 | + return logError_ModelNotInScope(oms::ComRef(modelName)); |
| 1193 | + model->setIsolatedFMUModel(); |
1195 | 1194 |
|
1196 | | - oms::Model* model = oms::Scope::GetInstance().getModel(oms::ComRef(modelName)); |
1197 | | - if (!model) |
1198 | | - return logError_ModelNotInScope(oms::ComRef(modelName)); |
1199 | | - model->setIsolatedFMUModel(); |
| 1195 | + if (kind == oms_fmi_kind_me_and_cs) |
| 1196 | + status = oms_addSystem(systemName.c_str(), oms::Flags::DefaultModeIsCS() ? oms_system_wc : oms_system_sc); |
| 1197 | + else |
| 1198 | + status = oms_addSystem(systemName.c_str(), (kind == oms_fmi_kind_cs) ? oms_system_wc : oms_system_sc); |
| 1199 | + if (oms_status_ok != status) return logError("oms_addSystem failed"); |
| 1200 | + |
| 1201 | + status = oms_addSubModel(fmuName.c_str(), path.string().c_str()); |
| 1202 | + if (oms_status_ok != status) return logError("oms_addSubModel failed"); |
| 1203 | + |
| 1204 | + if (oms::Flags::ResultFile() != "<default>") |
| 1205 | + if(oms_status_ok != oms_setResultFile(modelName.c_str(), oms::Flags::ResultFile().c_str(), 1)) |
| 1206 | + return logError("oms_setResultFile failed"); |
| 1207 | + |
| 1208 | + status = oms_setStartTime(modelName.c_str(), defaultExperiment.startTime); |
| 1209 | + if(oms_status_ok != status) return logError("oms_setStartTime failed"); |
| 1210 | + status = oms_setStopTime(modelName.c_str(), defaultExperiment.stopTime); |
| 1211 | + if(oms_status_ok != status) return logError("oms_setStopTime failed"); |
| 1212 | + status = oms_setTolerance(modelName.c_str(), defaultExperiment.tolerance, defaultExperiment.tolerance); |
| 1213 | + if(oms_status_ok != status) return logError("oms_setTolerance failed"); |
| 1214 | + // set the maximum stepSize |
| 1215 | + status = oms_setVariableStepSize(modelName.c_str(), oms::Flags::InitialStepSize(), oms::Flags::MinimumStepSize(), defaultExperiment.stepSize); |
| 1216 | + if(oms_status_ok != status) return logError("oms_setVariableStepSize failed"); |
| 1217 | + |
| 1218 | + if (kind == oms_fmi_kind_me_and_cs) |
| 1219 | + status = oms_setSolver(systemName.c_str(), oms::Flags::DefaultModeIsCS() ? oms::Flags::MasterAlgorithm() : oms::Flags::Solver()); |
| 1220 | + else |
| 1221 | + status = oms_setSolver(systemName.c_str(), (kind == oms_fmi_kind_cs) ? oms::Flags::MasterAlgorithm() : oms::Flags::Solver()); |
| 1222 | + if(oms_status_ok != status) return logError("oms_setSolver failed"); |
1200 | 1223 |
|
1201 | | - if (kind == oms_fmi_kind_me_and_cs) |
1202 | | - status = oms_addSystem(systemName.c_str(), oms::Flags::DefaultModeIsCS() ? oms_system_wc : oms_system_sc); |
1203 | | - else |
1204 | | - status = oms_addSystem(systemName.c_str(), (kind == oms_fmi_kind_cs) ? oms_system_wc : oms_system_sc); |
1205 | | - if (oms_status_ok != status) return logError("oms_addSystem failed"); |
| 1224 | + status = oms_instantiate(modelName.c_str()); |
| 1225 | + if(oms_status_ok != status) return logError("oms_instantiate failed"); |
| 1226 | + status = oms_initialize(modelName.c_str()); |
| 1227 | + if(oms_status_ok != status) return logError("oms_initialize failed"); |
| 1228 | + status = oms_simulate(modelName.c_str()); |
| 1229 | + if(oms_status_ok != status) return logError("oms_simulate failed"); |
1206 | 1230 |
|
1207 | | - status = oms_addSubModel(fmuName.c_str(), filename); |
1208 | | - if (oms_status_ok != status) return logError("oms_addSubModel failed"); |
| 1231 | + status = oms_terminate(modelName.c_str()); |
| 1232 | + if(oms_status_ok != status) return logError("oms_terminate failed"); |
| 1233 | + status = oms_delete(modelName.c_str()); |
| 1234 | + if(oms_status_ok != status) return logError("oms_delete failed"); |
1209 | 1235 |
|
1210 | | - if (oms::Flags::ResultFile() != "<default>") |
1211 | | - oms_setResultFile(modelName.c_str(), oms::Flags::ResultFile().c_str(), 1); |
1212 | | - oms_setStartTime(modelName.c_str(), defaultExperiment.startTime); |
1213 | | - oms_setStopTime(modelName.c_str(), defaultExperiment.stopTime); |
1214 | | - oms_setTolerance(modelName.c_str(), defaultExperiment.tolerance, defaultExperiment.tolerance); |
1215 | | - // set the maximum stepSize |
1216 | | - oms_setVariableStepSize(modelName.c_str(), oms::Flags::InitialStepSize(), oms::Flags::MinimumStepSize(), defaultExperiment.stepSize); |
1217 | | - |
1218 | | - if (kind == oms_fmi_kind_me_and_cs) |
1219 | | - oms_setSolver(systemName.c_str(), oms::Flags::DefaultModeIsCS() ? oms::Flags::MasterAlgorithm() : oms::Flags::Solver()); |
| 1236 | + return oms_status_ok; |
| 1237 | +} |
| 1238 | + |
| 1239 | +oms_status_enu_t oms_RunFile(const char* filename) |
| 1240 | +{ |
| 1241 | + oms_status_enu_t status; |
| 1242 | + filesystem::path path(filename); |
| 1243 | + std::string type = path.extension().string(); |
| 1244 | + |
| 1245 | + if (type == ".fmu") |
| 1246 | + { |
| 1247 | + auto future = std::async(std::launch::async, SimulateSingleFMU, path); |
| 1248 | + |
| 1249 | + if (oms::Flags::Timeout() == 0 || future.wait_for(std::chrono::seconds(int(oms::Flags::Timeout()))) == std::future_status::ready) |
| 1250 | + status = future.get(); |
1220 | 1251 | else |
1221 | | - oms_setSolver(systemName.c_str(), (kind == oms_fmi_kind_cs) ? oms::Flags::MasterAlgorithm() : oms::Flags::Solver()); |
| 1252 | + { |
| 1253 | + logError("Simulation timed out - cleaning up..."); |
| 1254 | + status = oms_status_error; |
| 1255 | + exit(1); |
| 1256 | + } |
1222 | 1257 |
|
1223 | | - status = do_simulation(modelName, std::chrono::duration<double>(oms::Flags::Timeout())) ? oms_status_error : oms_status_ok; |
1224 | | - oms_terminate(modelName.c_str()); |
1225 | | - oms_delete(modelName.c_str()); |
1226 | 1258 | return status; |
1227 | 1259 | } |
1228 | 1260 | else if (type == ".ssp") |
|
0 commit comments