OMCompiler/SimulationRuntime/cpp/Core/Solver/SolverDefaultImplementation.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 coreSolver | ||
| 29 | * | ||
| 30 | * @{ | ||
| 31 | */ | ||
| 32 | #include <Core/ModelicaDefine.h> | ||
| 33 | #include <Core/Modelica.h> | ||
| 34 | #include <Core/Solver/FactoryExport.h> | ||
| 35 | #include <Core/Solver/SolverDefaultImplementation.h> | ||
| 36 | #include <Core/Solver/SolverSettings.h> | ||
| 37 | #include <Core/SimulationSettings/IGlobalSettings.h> | ||
| 38 | #include <Core/Math/Constants.h> | ||
| 39 | #include <Core/System/FactoryExport.h> | ||
| 40 | #include <Core/Utils/extension/logger.hpp> | ||
| 41 | |||
| 42 | 40 | SolverDefaultImplementation::SolverDefaultImplementation(IMixedSystem* system, ISolverSettings* settings) | |
| 43 | : SimulationMonitor() | ||
| 44 | 40 | , _system (system) | |
| 45 | 40 | , _settings (settings) | |
| 46 | |||
| 47 | 40 | , _tInit (0.0) | |
| 48 | 40 | , _tCurrent (0.0) | |
| 49 | 40 | , _tEnd (0.0) | |
| 50 | 40 | , _tLastSuccess (0.0) | |
| 51 | 40 | , _tLastUnsucess (0.0) | |
| 52 | 40 | , _tLargeStep (0.0) | |
| 53 | 40 | , _h (0.0) | |
| 54 | |||
| 55 | 40 | , _firstCall (false) | |
| 56 | 40 | , _firstStep (true) | |
| 57 | |||
| 58 | 40 | , _totStps (0) | |
| 59 | 40 | , _accStps (0) | |
| 60 | 40 | , _rejStps (0) | |
| 61 | 40 | , _zeroStps (0) | |
| 62 | 40 | , _zeros (0) | |
| 63 | 40 | , _dimSys (0) | |
| 64 | 40 | , _zeroStatus (ISolver::UNCHANGED_SIGN) | |
| 65 | 40 | , _zeroValInit (NULL) | |
| 66 | 40 | , _dimZeroFunc (0) | |
| 67 | 40 | , _zeroVal (NULL) | |
| 68 | 40 | , _zeroValLastSuccess (NULL) | |
| 69 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | , _events (NULL) |
| 70 | 40 | , _solverStatus (ISolver::UNDEF_STATUS) | |
| 71 | 40 | , _outputCommand (IWriteOutput::WRITEOUT) | |
| 72 |
1/2✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
|
40 | , _writeoutput_system (NULL) |
| 73 | { | ||
| 74 |
2/4✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 40 times.
✗ Branch 5 not taken.
|
40 | _state_selection = shared_ptr<SystemStateSelection>(new SystemStateSelection(system)); |
| 75 | |||
| 76 | #ifdef RUNTIME_PROFILING | ||
| 77 | if(MeasureTime::getInstance() != NULL) | ||
| 78 | { | ||
| 79 | measureTimeFunctionsArray = new std::vector<MeasureTimeData*>(1, NULL); //0 write output | ||
| 80 | (*measureTimeFunctionsArray)[0] = new MeasureTimeData("writeOutput"); | ||
| 81 | |||
| 82 | MeasureTime::addResultContentBlock(system->getModelName(),"solver",measureTimeFunctionsArray); | ||
| 83 | writeFunctionStartValues = MeasureTime::getZeroValues(); | ||
| 84 | writeFunctionEndValues = MeasureTime::getZeroValues(); | ||
| 85 | } | ||
| 86 | else | ||
| 87 | { | ||
| 88 | measureTimeFunctionsArray = new std::vector<MeasureTimeData*>(); | ||
| 89 | writeFunctionStartValues = NULL; | ||
| 90 | writeFunctionEndValues = NULL; | ||
| 91 | } | ||
| 92 | #endif | ||
| 93 | 40 | } | |
| 94 | |||
| 95 | 40 | SolverDefaultImplementation::~SolverDefaultImplementation() | |
| 96 | { | ||
| 97 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 22 times.
|
40 | if(_zeroVal) |
| 98 | 18 | delete [] _zeroVal; | |
| 99 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 22 times.
|
40 | if(_zeroValInit) |
| 100 | 18 | delete [] _zeroValInit; | |
| 101 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 22 times.
|
40 | if(_zeroValLastSuccess) |
| 102 | 18 | delete [] _zeroValLastSuccess; | |
| 103 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 22 times.
|
40 | if(_events) |
| 104 | 18 | delete [] _events; | |
| 105 | |||
| 106 | #ifdef RUNTIME_PROFILING | ||
| 107 | if(writeFunctionStartValues) | ||
| 108 | delete writeFunctionStartValues; | ||
| 109 | if(writeFunctionEndValues) | ||
| 110 | delete writeFunctionEndValues; | ||
| 111 | #endif | ||
| 112 | 40 | } | |
| 113 | |||
| 114 | 3803 | void SolverDefaultImplementation::setStartTime(const double& t) | |
| 115 | { | ||
| 116 | 3803 | _tCurrent = t; | |
| 117 | 3803 | }; | |
| 118 | |||
| 119 | 1933 | void SolverDefaultImplementation::setEndTime(const double& t) | |
| 120 | { | ||
| 121 | 1933 | _tEnd = t; | |
| 122 | 1933 | }; | |
| 123 | |||
| 124 | 1853 | void SolverDefaultImplementation::setInitStepSize(const double& h) | |
| 125 | { | ||
| 126 | 1853 | _h = h; | |
| 127 | 1853 | }; | |
| 128 | |||
| 129 | 1870 | const ISolver::SOLVERSTATUS SolverDefaultImplementation::getSolverStatus() | |
| 130 | { | ||
| 131 | 1870 | return _solverStatus; | |
| 132 | }; | ||
| 133 | |||
| 134 | 323648 | bool SolverDefaultImplementation::stateSelection() | |
| 135 | { | ||
| 136 | 323648 | return _state_selection->stateSelection(1); | |
| 137 | } | ||
| 138 | |||
| 139 | 40 | void SolverDefaultImplementation::initialize() | |
| 140 | { | ||
| 141 | 40 | SimulationMonitor::initialize(); | |
| 142 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | IContinuous* continous_system = dynamic_cast<IContinuous*>(_system); |
| 143 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | IEvent* event_system = dynamic_cast<IEvent*>(_system); |
| 144 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | ITime* timeevent_system = dynamic_cast<ITime*>(_system); |
| 145 | |||
| 146 | // Set current start time to the system | ||
| 147 | 40 | timeevent_system->setTime(_tCurrent); | |
| 148 | |||
| 149 | // Allocate array with values of zero functions | ||
| 150 |
2/2✓ Branch 1 taken 18 times.
✓ Branch 2 taken 22 times.
|
40 | if (_dimZeroFunc != event_system->getDimZeroFunc()) |
| 151 | { | ||
| 152 | // Number (dimension) of zero functions | ||
| 153 | 18 | _dimZeroFunc = event_system->getDimZeroFunc(); | |
| 154 | |||
| 155 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | if(_zeroVal) |
| 156 | ✗ | delete [] _zeroVal; | |
| 157 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | if(_zeroValInit) |
| 158 | ✗ | delete [] _zeroValInit; | |
| 159 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | if(_zeroValLastSuccess) |
| 160 | ✗ | delete [] _zeroValLastSuccess; | |
| 161 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | if(_events) |
| 162 | ✗ | delete [] _events; | |
| 163 | |||
| 164 |
1/2✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
|
18 | _zeroVal = new double[_dimZeroFunc]; |
| 165 |
1/2✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
|
18 | _zeroValLastSuccess = new double[_dimZeroFunc]; |
| 166 | 18 | _events = new bool[_dimZeroFunc]; | |
| 167 |
1/2✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
|
18 | _zeroValInit = new double[_dimZeroFunc]; |
| 168 | 18 | continous_system->evaluateZeroFuncs(IContinuous::CONTINUOUS); | |
| 169 | 18 | event_system->getZeroFunc(_zeroVal); | |
| 170 | 18 | memcpy(_zeroValLastSuccess,_zeroVal,_dimZeroFunc*sizeof(double)); | |
| 171 | 18 | memcpy(_zeroValInit,_zeroVal,_dimZeroFunc*sizeof(double)); | |
| 172 | 18 | memset(_events,false,_dimZeroFunc*sizeof(bool)); | |
| 173 | } | ||
| 174 | |||
| 175 | // Set flags | ||
| 176 | 40 | _firstCall = true; | |
| 177 | 40 | _firstStep = true; | |
| 178 | |||
| 179 | // Reset counter | ||
| 180 | 40 | _totStps = 0; | |
| 181 | 40 | _accStps = 0; | |
| 182 | 40 | _rejStps = 0; | |
| 183 | 40 | _zeroStps = 0; | |
| 184 | 40 | _zeros = 0; | |
| 185 | |||
| 186 | // Set initial step size | ||
| 187 | //_h = _settings->_globalSettings->_hOutput; | ||
| 188 | 40 | } | |
| 189 | |||
| 190 | ✗ | void SolverDefaultImplementation::setZeroState() | |
| 191 | { | ||
| 192 | // Reset Zero-State | ||
| 193 | ✗ | _zeroStatus = ISolver::UNCHANGED_SIGN;; | |
| 194 | |||
| 195 | // Alle Elemente im ZeroFunction-Array durchgehen | ||
| 196 | ✗ | for (int i=0; i<_dimZeroFunc; ++i) | |
| 197 | { | ||
| 198 | // Überprüfung auf Vorzeichenwechsel | ||
| 199 | ✗ | if ((_zeroVal[i] < 0 && _zeroValLastSuccess[i] > 0) || (_zeroVal[i] > 0 && _zeroValLastSuccess[i] < 0)) | |
| 200 | { | ||
| 201 | // Vorzeichenwechsel, aber Eintrag ist größer (oder kleiner) als Toleranzbereich | ||
| 202 | ✗ | _zeroStatus = ISolver::EQUAL_ZERO; | |
| 203 | |||
| 204 | // Rest ZeroSign | ||
| 205 | ✗ | _events[i] = true; | |
| 206 | |||
| 207 | // Zeitpunkt des letzten verworfenen Schrittes abspeichern | ||
| 208 | ✗ | _tLastUnsucess = _tCurrent; | |
| 209 | ✗ | break; | |
| 210 | } | ||
| 211 | else | ||
| 212 | ✗ | _events[i] = false; | |
| 213 | } | ||
| 214 | |||
| 215 | ✗ | } | |
| 216 | |||
| 217 | 340419 | void SolverDefaultImplementation::writeToFile(const int& stp, const double& t, const double& h) | |
| 218 | { | ||
| 219 | #ifdef RUNTIME_PROFILING | ||
| 220 | MEASURETIME_REGION_DEFINE(solverWriteOutputHandler, "solverWriteOutput"); | ||
| 221 | if(MeasureTime::getInstance() != NULL) | ||
| 222 | { | ||
| 223 | MEASURETIME_START(writeFunctionStartValues, solverWriteOutputHandler, "solverWriteOutput"); | ||
| 224 | } | ||
| 225 | #endif | ||
| 226 | |||
| 227 |
1/2✓ Branch 0 taken 340419 times.
✗ Branch 1 not taken.
|
340419 | LOGGER_STATUS("Running", t, h); |
| 228 | |||
| 229 |
1/2✓ Branch 2 taken 340419 times.
✗ Branch 3 not taken.
|
340419 | if (_settings->getGlobalSettings()->getOutputPointType() != OPT_NONE) |
| 230 | { | ||
| 231 | // write header on first call | ||
| 232 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 340379 times.
|
340419 | if (_writeoutput_system == NULL) |
| 233 | { | ||
| 234 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | _writeoutput_system = dynamic_cast<IWriteOutput*>(_system); |
| 235 | 40 | _writeoutput_system->writeOutput(IWriteOutput::HEAD_LINE); | |
| 236 | } | ||
| 237 | // write values | ||
| 238 |
1/2✓ Branch 0 taken 340419 times.
✗ Branch 1 not taken.
|
340419 | if (_outputCommand & IWriteOutput::WRITEOUT) |
| 239 | { | ||
| 240 | 340419 | _writeoutput_system->writeOutput(_outputCommand); | |
| 241 | } | ||
| 242 | } | ||
| 243 | 340419 | checkTimeout(); | |
| 244 | |||
| 245 | #ifdef RUNTIME_PROFILING | ||
| 246 | if(MeasureTime::getInstance() != NULL) | ||
| 247 | { | ||
| 248 | MEASURETIME_END(writeFunctionStartValues, writeFunctionEndValues, (*measureTimeFunctionsArray)[0], solverWriteOutputHandler); | ||
| 249 | } | ||
| 250 | #endif | ||
| 251 | 340419 | } | |
| 252 | |||
| 253 | ✗ | void SolverDefaultImplementation::updateEventState() | |
| 254 | { | ||
| 255 | ✗ | dynamic_cast<IEvent*>(_system)->getZeroFunc(_zeroVal); | |
| 256 | ✗ | setZeroState(); | |
| 257 | ✗ | if (_zeroStatus == ISolver::ZERO_CROSSING) // An event triggered an other event | |
| 258 | { | ||
| 259 | ✗ | _tLastSuccess = _tCurrent; // Concurrently occured events are in the time tollerance | |
| 260 | ✗ | setZeroState(); // Upate status of events vector | |
| 261 | } | ||
| 262 | ✗ | } | |
| 263 | /** @} */ // end of coreSolver | ||
| 264 |