OMCompiler/SimulationRuntime/cpp/Core/SimController/SimController.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file belongs to the OpenModelica Run-Time System | ||
| 3 | * | ||
| 4 | * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC), c/o Linköpings | ||
| 5 | * universitet, Department of Computer and Information Science, SE-58183 Linköping, Sweden. All rights | ||
| 6 | * reserved. | ||
| 7 | * | ||
| 8 | * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE | ||
| 9 | * AGPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. ANY | ||
| 10 | * USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S | ||
| 11 | * ACCEPTANCE OF THE BSD NEW LICENSE OR THE OSMC PUBLIC LICENSE OR THE AGPL | ||
| 12 | * VERSION 3, ACCORDING TO RECIPIENTS CHOICE. | ||
| 13 | * | ||
| 14 | * The OpenModelica software and the OSMC (Open Source Modelica Consortium) Public License | ||
| 15 | * (OSMC-PL) are obtained from OSMC, either from the above address, from the URLs: | ||
| 16 | * http://www.openmodelica.org or https://github.com/OpenModelica/ or | ||
| 17 | * http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica distribution. GNU | ||
| 18 | * AGPL version 3 is obtained from: https://www.gnu.org/licenses/licenses.html#GPL. The BSD NEW | ||
| 19 | * License is obtained from: http://www.opensource.org/licenses/BSD-3-Clause. | ||
| 20 | * | ||
| 21 | * This program is distributed WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY | ||
| 23 | * SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF | ||
| 24 | * OSMC-PL. | ||
| 25 | * | ||
| 26 | */ | ||
| 27 | |||
| 28 | /** @addtogroup coreSimcontroller | ||
| 29 | * | ||
| 30 | * @{ | ||
| 31 | */ | ||
| 32 | |||
| 33 | |||
| 34 | #include <Core/ModelicaDefine.h> | ||
| 35 | #include <Core/Modelica.h> | ||
| 36 | #ifdef USE_REDUCE_DAE | ||
| 37 | #include <Core/ReduceDAE/IReduceDAE.h> | ||
| 38 | #include <core/ReduceDAE/ReduceDAESettings.h> | ||
| 39 | #include <core/ReduceDAE/Ranking.h> | ||
| 40 | #include <core/ReduceDAE/Reduction.h> | ||
| 41 | #include <core/ReduceDAE/com/ModelicaCompiler.h> | ||
| 42 | #endif | ||
| 43 | |||
| 44 | #include <Core/SimController/ISimController.h> | ||
| 45 | #include <Core/SimController/SimController.h> | ||
| 46 | #include <Core/SimController/Configuration.h> | ||
| 47 | #include <Core/SimController/SimObjects.h> | ||
| 48 | #include <Core/SimController/FactoryExport.h> | ||
| 49 | #include <Core/Utils/extension/logger.hpp> | ||
| 50 | |||
| 51 | #if defined(OMC_BUILD) || defined(SIMSTER_BUILD) | ||
| 52 | #include "LibrariesConfig.h" | ||
| 53 | #endif | ||
| 54 | |||
| 55 | |||
| 56 | 40 | SimController::SimController(PATH library_path, PATH modelicasystem_path) | |
| 57 |
4/10✓ Branch 2 taken 40 times.
✓ Branch 4 taken 40 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 40 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 40 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
|
200 | : SimControllerPolicy(library_path, modelicasystem_path, library_path) |
| 58 | { | ||
| 59 |
6/13✓ Branch 1 taken 40 times.
✓ Branch 4 taken 40 times.
✓ Branch 6 taken 40 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 40 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 40 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 40 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
|
200 | _config = shared_ptr<Configuration>(new Configuration(_library_path, _config_path, modelicasystem_path)); |
| 60 |
6/15✓ Branch 1 taken 40 times.
✓ Branch 4 taken 40 times.
✓ Branch 7 taken 40 times.
✓ Branch 9 taken 40 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 40 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 40 times.
✗ Branch 14 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
|
160 | _sim_objects = shared_ptr<ISimObjects>(new SimObjects(_library_path,modelicasystem_path,_config->getGlobalSettings().get())); |
| 61 | |||
| 62 | #ifdef RUNTIME_PROFILING | ||
| 63 | measuredFunctionStartValues = NULL; | ||
| 64 | measuredFunctionEndValues = NULL; | ||
| 65 | |||
| 66 | if(MeasureTime::getInstance() != NULL) | ||
| 67 | { | ||
| 68 | measureTimeFunctionsArray = new std::vector<MeasureTimeData*>(2, NULL); //0 initialize //1 solveInitialSystem | ||
| 69 | (*measureTimeFunctionsArray)[0] = new MeasureTimeData("initialize"); | ||
| 70 | (*measureTimeFunctionsArray)[1] = new MeasureTimeData("solveInitialSystem"); | ||
| 71 | |||
| 72 | measuredFunctionStartValues = MeasureTime::getZeroValues(); | ||
| 73 | measuredFunctionEndValues = MeasureTime::getZeroValues(); | ||
| 74 | } | ||
| 75 | else | ||
| 76 | { | ||
| 77 | measureTimeFunctionsArray = new std::vector<MeasureTimeData*>(); | ||
| 78 | } | ||
| 79 | #endif | ||
| 80 | 40 | } | |
| 81 | |||
| 82 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 34 times.
|
80 | SimController::~SimController() |
| 83 | { | ||
| 84 | #ifdef RUNTIME_PROFILING | ||
| 85 | if(measuredFunctionStartValues) | ||
| 86 | delete measuredFunctionStartValues; | ||
| 87 | if(measuredFunctionEndValues) | ||
| 88 | delete measuredFunctionEndValues; | ||
| 89 | #endif | ||
| 90 | 80 | } | |
| 91 | |||
| 92 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
|
40 | weak_ptr<IMixedSystem> SimController::LoadSystem(string modelLib,string modelKey) |
| 93 | { | ||
| 94 | //if the model is already loaded | ||
| 95 | std::map<string,shared_ptr<IMixedSystem> >::iterator iter = _systems.find(modelKey); | ||
| 96 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
|
40 | if(iter != _systems.end()) |
| 97 | { | ||
| 98 | //recreate data and vars | ||
| 99 | ✗ | shared_ptr<ISimVars> sv = _sim_objects->getSimVars(modelKey); | |
| 100 | ✗ | _sim_objects->LoadSimVars(modelKey, sv->getDimReal(), sv->getDimInt(), sv->getDimBool(), sv->getDimString(), | |
| 101 | ✗ | sv->getDimPreVars(), sv->getDimStateVars(), sv->getStateVectorIndex()); | |
| 102 | ✗ | _sim_objects->LoadSimData(modelKey); | |
| 103 | //destroy system | ||
| 104 | _systems.erase(iter); | ||
| 105 | } | ||
| 106 | //create system | ||
| 107 |
6/14✓ Branch 2 taken 40 times.
✓ Branch 5 taken 40 times.
✓ Branch 7 taken 6 times.
✓ Branch 8 taken 34 times.
✓ Branch 9 taken 40 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 40 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
|
166 | shared_ptr<IMixedSystem> system = createSystem(modelLib, modelKey, _config->getGlobalSettings().get(), _sim_objects); |
| 108 |
1/1✓ Branch 1 taken 40 times.
|
40 | _systems[modelKey] = system; |
| 109 |
1/1✓ Branch 1 taken 40 times.
|
40 | _modelLib = modelLib; |
| 110 |
1/1✓ Branch 1 taken 40 times.
|
40 | _modelKey = modelKey; |
| 111 | 40 | return system; | |
| 112 | } | ||
| 113 | |||
| 114 | ✗ | weak_ptr<IMixedSystem> SimController::LoadModelicaSystem(PATH modelica_path,string modelKey) | |
| 115 | { | ||
| 116 | ✗ | if(_use_modelica_compiler) | |
| 117 | { | ||
| 118 | // if the modell is already loaded | ||
| 119 | std::map<string,shared_ptr<IMixedSystem> >::iterator iter = _systems.find(modelKey); | ||
| 120 | ✗ | if(iter != _systems.end()) | |
| 121 | { | ||
| 122 | ✗ | _sim_objects->eraseSimData(modelKey); | |
| 123 | ✗ | _sim_objects->eraseSimVars(modelKey); | |
| 124 | // destroy system | ||
| 125 | _systems.erase(iter); | ||
| 126 | } | ||
| 127 | |||
| 128 | ✗ | shared_ptr<IMixedSystem> system = createModelicaSystem(modelica_path, modelKey, _config->getGlobalSettings().get(),_sim_objects); | |
| 129 | _systems[modelKey] = system; | ||
| 130 | return system; | ||
| 131 | } | ||
| 132 | else | ||
| 133 | ✗ | throw ModelicaSimulationError(SIMMANAGER,"No Modelica Compiler configured"); | |
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | 40 | shared_ptr<ISimObjects> SimController::getSimObjects() | |
| 138 | { | ||
| 139 | |||
| 140 | 40 | return _sim_objects; | |
| 141 | |||
| 142 | } | ||
| 143 | |||
| 144 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | shared_ptr<IMixedSystem> SimController::getSystem(string modelname) |
| 145 | { | ||
| 146 | std::map<string,shared_ptr<IMixedSystem> >::iterator iter = _systems.find(modelname); | ||
| 147 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | if(iter!=_systems.end()) |
| 148 | { | ||
| 149 | 40 | return iter->second; | |
| 150 | } | ||
| 151 | else | ||
| 152 | { | ||
| 153 | ✗ | string error = string("Simulation data was not found for model: ") + modelname; | |
| 154 | ✗ | throw ModelicaSimulationError(SIMMANAGER,error); | |
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | 40 | void SimController::Start(SimSettings simsettings, string modelKey) | |
| 159 | { | ||
| 160 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | for (int i = 0; i < simsettings.nonlinear_solver_names.size(); i++) |
| 161 | { | ||
| 162 | string nls = simsettings.nonlinear_solver_names[i]; | ||
| 163 | try { | ||
| 164 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
|
40 | if (i > 0) |
| 165 | ✗ | LOGGER_WRITE("SimController: Recovering with nonlinear solver " + nls, LC_SOLVER, LL_ERROR); | |
| 166 |
7/13✓ Branch 1 taken 40 times.
✓ Branch 4 taken 40 times.
✓ Branch 7 taken 40 times.
✓ Branch 10 taken 6 times.
✓ Branch 11 taken 34 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 40 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 40 times.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
|
86 | Start(simsettings, modelKey, nls); |
| 167 | break; | ||
| 168 | } | ||
| 169 | ✗ | catch(ModelicaSimulationError & ex) | |
| 170 | { | ||
| 171 | ✗ | LOGGER_WRITE("SimController: Simulation failed using nonlinear solver " + nls, LC_SOLVER, LL_ERROR); | |
| 172 | ✗ | if (i < simsettings.nonlinear_solver_names.size() - 1) { | |
| 173 | // load system again to get all variables re-initialized | ||
| 174 | ✗ | LoadSystem(_modelLib, _modelKey); | |
| 175 | } | ||
| 176 | else { | ||
| 177 | ✗ | throw; | |
| 178 | } | ||
| 179 | ✗ | } | |
| 180 | } | ||
| 181 | 40 | } | |
| 182 | |||
| 183 | 40 | void SimController::Start(SimSettings simsettings, string modelKey, string nls) | |
| 184 | { | ||
| 185 | try | ||
| 186 | { | ||
| 187 | #ifdef RUNTIME_PROFILING | ||
| 188 | MEASURETIME_REGION_DEFINE(simControllerInitializeHandler, "SimControllerInitialize"); | ||
| 189 | MEASURETIME_REGION_DEFINE(simControllerSolveInitialSystemHandler, "SimControllerSolveInitialSystem"); | ||
| 190 | if(MeasureTime::getInstance() != NULL) | ||
| 191 | { | ||
| 192 | MEASURETIME_START(measuredFunctionStartValues, simControllerInitializeHandler, "CVodeWriteOutput"); | ||
| 193 | } | ||
| 194 | #endif | ||
| 195 |
3/3✓ Branch 1 taken 40 times.
✓ Branch 4 taken 40 times.
✓ Branch 7 taken 40 times.
|
120 | shared_ptr<IMixedSystem> mixedsystem = getSystem(modelKey); |
| 196 | |||
| 197 |
1/1✓ Branch 1 taken 40 times.
|
40 | shared_ptr<IGlobalSettings> global_settings = _config->getGlobalSettings(); |
| 198 | |||
| 199 |
1/1✓ Branch 1 taken 40 times.
|
40 | global_settings->setStartTime(simsettings.start_time); |
| 200 |
1/1✓ Branch 1 taken 40 times.
|
40 | global_settings->setEndTime(simsettings.end_time); |
| 201 |
1/1✓ Branch 1 taken 40 times.
|
40 | global_settings->sethOutput(simsettings.step_size); |
| 202 |
1/1✓ Branch 1 taken 40 times.
|
40 | global_settings->setTolerance(simsettings.tolerance); |
| 203 |
3/3✓ Branch 1 taken 40 times.
✓ Branch 4 taken 40 times.
✓ Branch 7 taken 40 times.
|
120 | global_settings->setResultsFileName(simsettings.outputfile_name); |
| 204 |
3/3✓ Branch 1 taken 40 times.
✓ Branch 4 taken 40 times.
✓ Branch 7 taken 40 times.
|
120 | global_settings->setSelectedLinSolver(simsettings.linear_solver_name); |
| 205 |
3/3✓ Branch 1 taken 40 times.
✓ Branch 4 taken 40 times.
✓ Branch 7 taken 40 times.
|
120 | global_settings->setSelectedNonLinSolver(nls); |
| 206 |
3/3✓ Branch 1 taken 40 times.
✓ Branch 4 taken 40 times.
✓ Branch 7 taken 40 times.
|
120 | global_settings->setSelectedSolver(simsettings.solver_name); |
| 207 |
3/3✓ Branch 1 taken 40 times.
✓ Branch 4 taken 40 times.
✓ Branch 7 taken 40 times.
|
80 | global_settings->setLogSettings(simsettings.logSettings); |
| 208 |
1/1✓ Branch 1 taken 40 times.
|
40 | global_settings->setAlarmTime(simsettings.timeOut); |
| 209 |
1/1✓ Branch 1 taken 40 times.
|
40 | global_settings->setOutputPointType(simsettings.outputPointType); |
| 210 |
1/1✓ Branch 1 taken 40 times.
|
40 | global_settings->setOutputFormat(simsettings.outputFormat); |
| 211 |
1/1✓ Branch 1 taken 40 times.
|
40 | global_settings->setEmitResults(simsettings.emitResults); |
| 212 |
1/1✓ Branch 1 taken 40 times.
|
40 | global_settings->setVariableFilter(simsettings.variableFilter); |
| 213 |
1/1✓ Branch 1 taken 40 times.
|
40 | global_settings->setNonLinearSolverContinueOnError(simsettings.nonLinearSolverContinueOnError); |
| 214 |
1/1✓ Branch 1 taken 40 times.
|
40 | global_settings->setSolverThreads(simsettings.solverThreads); |
| 215 |
3/3✓ Branch 1 taken 40 times.
✓ Branch 4 taken 40 times.
✓ Branch 7 taken 40 times.
|
120 | global_settings->setInputPath(simsettings.inputPath); |
| 216 |
2/2✓ Branch 1 taken 40 times.
✓ Branch 4 taken 40 times.
|
120 | global_settings->setOutputPath(simsettings.outputPath); |
| 217 | |||
| 218 |
4/7✓ Branch 1 taken 40 times.
✓ Branch 4 taken 40 times.
✓ Branch 6 taken 40 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 40 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
|
120 | /*shared_ptr<SimManager>*/ _simMgr = shared_ptr<SimManager>(new SimManager(mixedsystem, _config.get())); |
| 219 | |||
| 220 |
1/1✓ Branch 1 taken 40 times.
|
40 | ISolverSettings* solver_settings = _config->getSolverSettings(); |
| 221 |
1/1✓ Branch 1 taken 40 times.
|
40 | solver_settings->setLowerLimit(simsettings.lower_limit); |
| 222 |
1/1✓ Branch 1 taken 40 times.
|
40 | solver_settings->sethInit(simsettings.lower_limit); |
| 223 |
1/1✓ Branch 1 taken 40 times.
|
40 | solver_settings->setUpperLimit(simsettings.upper_limit); |
| 224 |
1/1✓ Branch 1 taken 40 times.
|
40 | solver_settings->setRTol(simsettings.tolerance); |
| 225 |
1/1✓ Branch 1 taken 40 times.
|
40 | solver_settings->setATol(simsettings.tolerance); |
| 226 | #ifdef RUNTIME_PROFILING | ||
| 227 | if(MeasureTime::getInstance() != NULL) | ||
| 228 | { | ||
| 229 | MEASURETIME_END(measuredFunctionStartValues, measuredFunctionEndValues, (*measureTimeFunctionsArray)[0], simControllerInitializeHandler); | ||
| 230 | measuredFunctionStartValues->reset(); | ||
| 231 | measuredFunctionEndValues->reset(); | ||
| 232 | MEASURETIME_START(measuredFunctionStartValues, simControllerSolveInitialSystemHandler, "SolveInitialSystem"); | ||
| 233 | } | ||
| 234 | #endif | ||
| 235 | |||
| 236 |
1/1✓ Branch 1 taken 40 times.
|
40 | _simMgr->initialize(); |
| 237 | |||
| 238 | #ifdef RUNTIME_PROFILING | ||
| 239 | if(MeasureTime::getInstance() != NULL) | ||
| 240 | { | ||
| 241 | MEASURETIME_END(measuredFunctionStartValues, measuredFunctionEndValues, (*measureTimeFunctionsArray)[1], simControllerSolveInitialSystemHandler); | ||
| 242 | MeasureTime::addResultContentBlock(mixedsystem->getModelName(),"simController",measureTimeFunctionsArray); | ||
| 243 | } | ||
| 244 | #endif | ||
| 245 | |||
| 246 |
1/1✓ Branch 1 taken 40 times.
|
40 | _simMgr->runSimulation(); |
| 247 | |||
| 248 |
2/3✓ Branch 1 taken 40 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 40 times.
|
40 | if(global_settings->getOutputFormat() == BUFFER) |
| 249 | { | ||
| 250 | ✗ | shared_ptr<IWriteOutput> writeoutput_system = dynamic_pointer_cast<IWriteOutput>(mixedsystem); | |
| 251 | |||
| 252 | ✗ | shared_ptr<ISimData> simData = _sim_objects->getSimData(modelKey); | |
| 253 | ✗ | simData->clearResults(); | |
| 254 | //get history object to query simulation results | ||
| 255 | ✗ | IHistory* history = writeoutput_system->getHistory(); | |
| 256 | //simulation results (output variables) | ||
| 257 | ublas::matrix<double> Ro; | ||
| 258 | //query simulation result outputs | ||
| 259 | ✗ | history->getOutputResults(Ro); | |
| 260 | vector<string> output_names; | ||
| 261 | ✗ | history->getOutputNames(output_names); | |
| 262 | int j=0; | ||
| 263 | |||
| 264 | ✗ | FOREACH(string& name, output_names) | |
| 265 | { | ||
| 266 | ublas::vector<double> o_j(Ro.size2()); | ||
| 267 | ✗ | for (size_t k = 0; k < Ro.size2(); k++) | |
| 268 | ✗ | o_j(k) = Ro(j, k); | |
| 269 | ✗ | simData->addOutputResults(name,o_j); | |
| 270 | ✗ | j++; | |
| 271 | } | ||
| 272 | |||
| 273 | ✗ | vector<double> time_values = history->getTimeEntries(); | |
| 274 | ✗ | simData->addTimeEntries(time_values); | |
| 275 | ✗ | } | |
| 276 | } | ||
| 277 | ✗ | catch(ModelicaSimulationError & ex) | |
| 278 | { | ||
| 279 | ✗ | string error = add_error_info(string("Simulation failed for ") + simsettings.outputfile_name,ex.what(),ex.getErrorID()); | |
| 280 | ✗ | throw ModelicaSimulationError(SIMMANAGER, error, "", ex.isSuppressed()); | |
| 281 | ✗ | } | |
| 282 | 40 | } | |
| 283 | |||
| 284 | ✗ | void SimController::StartReduceDAE(SimSettings simsettings,string modelPath, string modelKey,bool loadMSL, bool loadPackage) | |
| 285 | { | ||
| 286 | #ifdef USE_REDUCE_DAE | ||
| 287 | try | ||
| 288 | { | ||
| 289 | |||
| 290 | #ifdef RUNTIME_PROFILING | ||
| 291 | MEASURETIME_REGION_DEFINE(simControllerInitializeHandler, "SimControllerInitialize"); | ||
| 292 | MEASURETIME_REGION_DEFINE(simControllerSolveInitialSystemHandler, "SimControllerSolveInitialSystem"); | ||
| 293 | if(MeasureTime::getInstance() != NULL) | ||
| 294 | { | ||
| 295 | MEASURETIME_START(measuredFunctionStartValues, simControllerInitializeHandler, "CVodeWriteOutput"); | ||
| 296 | } | ||
| 297 | #endif | ||
| 298 | shared_ptr<IMixedSystem> mixedsystem = getSystem(modelKey); | ||
| 299 | |||
| 300 | shared_ptr<IGlobalSettings> global_settings = _config->getGlobalSettings(); | ||
| 301 | |||
| 302 | global_settings->setStartTime(simsettings.start_time); | ||
| 303 | global_settings->setEndTime(simsettings.end_time); | ||
| 304 | global_settings->sethOutput(simsettings.step_size); | ||
| 305 | global_settings->setTolerance(simsettings.tolerance); | ||
| 306 | global_settings->setResultsFileName(simsettings.outputfile_name); | ||
| 307 | global_settings->setSelectedLinSolver(simsettings.linear_solver_name); | ||
| 308 | global_settings->setSelectedNonLinSolver(simsettings.nonlinear_solver_name); | ||
| 309 | global_settings->setSelectedSolver(simsettings.solver_name); | ||
| 310 | global_settings->setLogSettings(simsettings.logSettings); | ||
| 311 | global_settings->setAlarmTime(simsettings.timeOut); | ||
| 312 | // global_settings->setAlarmTime(2); | ||
| 313 | global_settings->setOutputPointType(simsettings.outputPointType); | ||
| 314 | global_settings->setOutputFormat(simsettings.outputFormat); | ||
| 315 | global_settings->setEmitResults(simsettings.emitResults); | ||
| 316 | global_settings->setVariableFilter(simsettings.variableFilter); | ||
| 317 | global_settings->setNonLinearSolverContinueOnError(simsettings.nonLinearSolverContinueOnError); | ||
| 318 | global_settings->setSolverThreads(simsettings.solverThreads); | ||
| 319 | /*shared_ptr<SimManager>*/ _simMgr = shared_ptr<SimManager>(new SimManager(mixedsystem, _config.get())); | ||
| 320 | |||
| 321 | ISolverSettings* solver_settings = _config->getSolverSettings(); | ||
| 322 | solver_settings->setLowerLimit(simsettings.lower_limit); | ||
| 323 | solver_settings->sethInit(simsettings.lower_limit); | ||
| 324 | solver_settings->setUpperLimit(simsettings.upper_limit); | ||
| 325 | solver_settings->setRTol(simsettings.tolerance); | ||
| 326 | solver_settings->setATol(simsettings.tolerance); | ||
| 327 | #ifdef RUNTIME_PROFILING | ||
| 328 | if(MeasureTime::getInstance() != NULL) | ||
| 329 | { | ||
| 330 | MEASURETIME_END(measuredFunctionStartValues, measuredFunctionEndValues, (*measureTimeFunctionsArray)[0], simControllerInitializeHandler); | ||
| 331 | measuredFunctionStartValues->reset(); | ||
| 332 | measuredFunctionEndValues->reset(); | ||
| 333 | MEASURETIME_START(measuredFunctionStartValues, simControllerSolveInitialSystemHandler, "SolveInitialSystem"); | ||
| 334 | } | ||
| 335 | #endif | ||
| 336 | //read reduced settings | ||
| 337 | ReduceDAESettings reduce_settings(global_settings.get()); | ||
| 338 | reduce_settings.load("ReduceDAESettings.xml"); | ||
| 339 | Ranking ranking(mixedsystem,&reduce_settings); | ||
| 340 | Reduction reduction(mixedsystem,&reduce_settings); | ||
| 341 | #ifdef USE_CHRONO | ||
| 342 | auto startSim1 = high_resolution_clock::now(); | ||
| 343 | #endif | ||
| 344 | _simMgr->initialize(); | ||
| 345 | _simMgr->SetCheckTimeout(true); | ||
| 346 | #ifdef RUNTIME_PROFILING | ||
| 347 | if(MeasureTime::getInstance() != NULL) | ||
| 348 | { | ||
| 349 | MEASURETIME_END(measuredFunctionStartValues, measuredFunctionEndValues, (*measureTimeFunctionsArray)[1], simControllerSolveInitialSystemHandler); | ||
| 350 | MeasureTime::addResultContentBlock(mixedsystem->getModelName(),"simController",measureTimeFunctionsArray); | ||
| 351 | } | ||
| 352 | #endif | ||
| 353 | |||
| 354 | |||
| 355 | _simMgr->runSimulation(); | ||
| 356 | #ifdef USE_CHRONO | ||
| 357 | auto endSim1 = high_resolution_clock::now(); | ||
| 358 | double timeout= duration_cast<std::chrono::duration<double>>(endSim1-startSim1).count(); | ||
| 359 | std::cout <<" time of first simulation: "<< timeout << " seconds" << std::endl; | ||
| 360 | #endif | ||
| 361 | IReduceDAE* reduce_dae = dynamic_cast<IReduceDAE*>(mixedsystem.get()); | ||
| 362 | if(reduce_dae==NULL) | ||
| 363 | { | ||
| 364 | throw std::runtime_error("Modelica System is not of type IReduceDAE!!!"); | ||
| 365 | } | ||
| 366 | |||
| 367 | |||
| 368 | |||
| 369 | //get history object to query simulation results | ||
| 370 | IHistory* history = reduce_dae->getHistory(); | ||
| 371 | vector<double> time_values = history->getTimeEntries(); | ||
| 372 | cout << "time_values: " << time_values.size() << std::endl; | ||
| 373 | |||
| 374 | //simulation results (algebraic and state variables) | ||
| 375 | ublas::matrix<double> R; | ||
| 376 | //simulation results (derivative variables) | ||
| 377 | ublas::matrix<double> dR; | ||
| 378 | //simulation results (residues) | ||
| 379 | ublas::matrix<double> Re; | ||
| 380 | //simulation results (output variables) | ||
| 381 | ublas::matrix<double> Ro; | ||
| 382 | //query simulation results | ||
| 383 | history->getSimResults(R,dR,Re); | ||
| 384 | |||
| 385 | cout << "number of derivatives: " << dR.size1() << std::endl; | ||
| 386 | cout << "number of variables: " << R.size1() << std::endl; | ||
| 387 | cout << "number of residual: " << Re.size1() << std::endl; | ||
| 388 | history->getOutputResults(Ro); | ||
| 389 | cout << "number of output " << Ro.size1() << std::endl; | ||
| 390 | vector<string> output_names; | ||
| 391 | history->getOutputNames(output_names); | ||
| 392 | |||
| 393 | |||
| 394 | label_list_type labels; | ||
| 395 | |||
| 396 | |||
| 397 | //---------------------------------------------------------------------------------------------------------------- | ||
| 398 | //start ranking | ||
| 399 | |||
| 400 | |||
| 401 | if(reduce_settings.getRankingMethod()==IReduceDAESettings::RESIDUEN) | ||
| 402 | { | ||
| 403 | #ifdef USE_CHRONO | ||
| 404 | auto start = high_resolution_clock::now(); | ||
| 405 | #endif | ||
| 406 | //start residue ranking | ||
| 407 | labels =ranking.residuenRanking(R,dR,Re,time_values); | ||
| 408 | #ifdef USE_CHRONO | ||
| 409 | auto end = high_resolution_clock::now(); | ||
| 410 | std::cout <<" time of residual ranking: "<< std::chrono::duration_cast<std::chrono::milliseconds>(end-start).count() << " milliseconds" << std::endl; | ||
| 411 | #endif | ||
| 412 | } | ||
| 413 | |||
| 414 | |||
| 415 | else if(reduce_settings.getRankingMethod()==IReduceDAESettings::PERFECT) | ||
| 416 | { | ||
| 417 | #ifdef USE_CHRONO | ||
| 418 | //start perfect ranking | ||
| 419 | auto start = high_resolution_clock::now(); | ||
| 420 | #endif | ||
| 421 | labels = ranking.perfectRanking(Ro,mixedsystem,&reduce_settings,simsettings,modelKey,output_names,timeout,this); | ||
| 422 | #ifdef USE_CHRONO | ||
| 423 | auto end = high_resolution_clock::now(); | ||
| 424 | std::cout <<" time of perfect ranking: "<< std::chrono::duration_cast<std::chrono::milliseconds>(end-start).count() << " milliseconds" << std::endl; | ||
| 425 | #endif | ||
| 426 | } | ||
| 427 | |||
| 428 | |||
| 429 | else if(reduce_settings.getRankingMethod()==IReduceDAESettings::NORANKING) | ||
| 430 | { | ||
| 431 | //without ranking, get all labels without any sorting and pass it to the redction | ||
| 432 | labels=reduce_dae->getLabels(); | ||
| 433 | } | ||
| 434 | |||
| 435 | |||
| 436 | std::cout<<"sorted labels: "<<"\n"; | ||
| 437 | FOREACH(label_type label, labels) | ||
| 438 | { | ||
| 439 | std::cout<<"label "<< get<0>(label)<<"\n"; | ||
| 440 | |||
| 441 | } | ||
| 442 | |||
| 443 | |||
| 444 | |||
| 445 | //---------------------------------------------------------------------------------------------------------------- | ||
| 446 | //start reduction | ||
| 447 | std::vector<unsigned int> terms = reduction.cancelTerms(labels,Ro,mixedsystem,&reduce_settings,simsettings,modelKey,output_names,timeout,this); | ||
| 448 | |||
| 449 | if(terms.size()>0) | ||
| 450 | { | ||
| 451 | |||
| 452 | //------------------------------------------------------------------------------------ | ||
| 453 | string packageName; | ||
| 454 | size_t found; | ||
| 455 | std::cout << "modelPath "<< modelPath << std::endl; | ||
| 456 | found=modelPath.find(modelKey); | ||
| 457 | //std::cout << "found "<< found << std::endl; | ||
| 458 | |||
| 459 | if (found != std::string::npos && found!=0) | ||
| 460 | packageName=modelPath.substr(0, found-1); | ||
| 461 | else | ||
| 462 | packageName=""; | ||
| 463 | std::cout << "package name "<< packageName<< std::endl; | ||
| 464 | string fileName = modelKey; | ||
| 465 | // when a model from MSL is used, then LoadFile doesn't need to be called | ||
| 466 | // still there is problem with calling reducedTerms with model from MSL, because | ||
| 467 | // for example for Modelica.Electrical.Analog.Examples.CauerLowPassSC, the modelPath only gives Modelica.CauerLowPassSC | ||
| 468 | ModelicaCompiler compiler(modelKey,fileName,packageName,!loadMSL,loadPackage); | ||
| 469 | compiler.reduceTerms(terms,simsettings.start_time,simsettings.end_time); | ||
| 470 | //----------------------------------------------------------------------------------------- | ||
| 471 | |||
| 472 | |||
| 473 | /*_simMgr->runSimulation(); | ||
| 474 | }*/ | ||
| 475 | |||
| 476 | } | ||
| 477 | |||
| 478 | else | ||
| 479 | std::cout << "list of labels for reduction is empty, so model remained as original." << std::endl; | ||
| 480 | |||
| 481 | |||
| 482 | } | ||
| 483 | catch(ModelicaSimulationError & ex) | ||
| 484 | { | ||
| 485 | string error = add_error_info(string("Simulation failed for ") + simsettings.outputfile_name,ex.what(),ex.getErrorID()); | ||
| 486 | throw ModelicaSimulationError(SIMMANAGER, error, "", ex.isSuppressed()); | ||
| 487 | } | ||
| 488 | #else | ||
| 489 | ✗ | throw ModelicaSimulationError(SIMMANAGER,"The reduction algorithm is no supported for used compiler"); | |
| 490 | #endif | ||
| 491 | } | ||
| 492 | |||
| 493 | ✗ | void SimController::Stop() | |
| 494 | { | ||
| 495 | ✗ | if(_simMgr) | |
| 496 | ✗ | _simMgr->stopSimulation(); | |
| 497 | ✗ | } | |
| 498 | /** @} */ // end of coreSimcontroller | ||
| 499 |