OMCompiler/SimulationRuntime/cpp/Core/System/SystemDefaultImplementation.h
| 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 | #pragma once | ||
| 29 | /** @defgroup coreSystem Core.System | ||
| 30 | * Core module for all algebraic and ode systems | ||
| 31 | * @{ | ||
| 32 | */ | ||
| 33 | /*****************************************************************************/ | ||
| 34 | /** | ||
| 35 | |||
| 36 | Services, which can be used by systems. | ||
| 37 | Implementation of standart functions (e.g. giveRHS(...), etc.). | ||
| 38 | Provision of member variables used by all systems. | ||
| 39 | |||
| 40 | Note: | ||
| 41 | The order of variables in the extended state vector perserved (see: "Sorting | ||
| 42 | variables by using the index" in "Design proposal for a general solver interface | ||
| 43 | for Open Modelica", September, 10 th, 2008 | ||
| 44 | |||
| 45 | |||
| 46 | \date October, 1st, 2008 | ||
| 47 | \author | ||
| 48 | |||
| 49 | */ | ||
| 50 | |||
| 51 | |||
| 52 | #define MODELICA_TERMINATE(msg) Terminate(msg) | ||
| 53 | |||
| 54 | //typedef unordered_map<std::string, boost::any> SValuesMap; | ||
| 55 | |||
| 56 | template <class T> | ||
| 57 |
1/2✓ Branch 3 taken 43 times.
✗ Branch 4 not taken.
|
43 | class InitVars |
| 58 | { | ||
| 59 | public: | ||
| 60 | void setStartValue(T& variable,T val,bool overwriteOldValue); | ||
| 61 | T& getGetStartValue(T& variable); | ||
| 62 | |||
| 63 | private: | ||
| 64 | unordered_map<T*, T> _start_values; | ||
| 65 | }; | ||
| 66 | |||
| 67 | class BOOST_EXTENSION_SYSTEM_DECL SystemDefaultImplementation | ||
| 68 | : public IContinuous | ||
| 69 | , public IEvent | ||
| 70 | , public IStepEvent | ||
| 71 | , public ITime | ||
| 72 | , public ISystemProperties | ||
| 73 | #ifdef USE_REDUCE_DAE | ||
| 74 | , public IReduceDAE | ||
| 75 | #endif | ||
| 76 | { | ||
| 77 | public: | ||
| 78 | SystemDefaultImplementation(IGlobalSettings* globalSettings, shared_ptr<ISimObjects> sim_objects, string modelName); | ||
| 79 | SystemDefaultImplementation(SystemDefaultImplementation &instance); | ||
| 80 | virtual ~SystemDefaultImplementation(); | ||
| 81 | |||
| 82 | /// Provide number (dimension) of boolean variables | ||
| 83 | virtual int getDimBoolean() const; | ||
| 84 | |||
| 85 | /// Provide number (dimension) of states | ||
| 86 | virtual int getDimContinuousStates() const; | ||
| 87 | virtual int getDimAE() const; | ||
| 88 | /// Provide number (dimension) of integer variables | ||
| 89 | virtual int getDimInteger() const; | ||
| 90 | |||
| 91 | /// Provide number (dimension) of real variables | ||
| 92 | virtual int getDimReal() const; | ||
| 93 | |||
| 94 | /// Provide number (dimension) of string variables | ||
| 95 | virtual int getDimString() const; | ||
| 96 | |||
| 97 | /// Provide number (dimension) of clocks | ||
| 98 | virtual int getDimClock() const; | ||
| 99 | |||
| 100 | /// Provide number (dimension) of right hand sides (equations and/or residuals) according to the index | ||
| 101 | virtual int getDimRHS() const; | ||
| 102 | |||
| 103 | /// Provide boolean variables | ||
| 104 | virtual void getBoolean(bool* z); | ||
| 105 | |||
| 106 | /// Provide boolean variables | ||
| 107 | virtual void getContinuousStates(double* z); | ||
| 108 | |||
| 109 | /// Provide integer variables | ||
| 110 | virtual void getInteger(int* z); | ||
| 111 | |||
| 112 | /// Provide real variables | ||
| 113 | virtual void getReal(double* z); | ||
| 114 | |||
| 115 | /// Provide string variables | ||
| 116 | virtual void getString(std::string* z); | ||
| 117 | |||
| 118 | /// Provide clocks | ||
| 119 | virtual void getClock(bool* z); | ||
| 120 | |||
| 121 | /// Provide clock intervals | ||
| 122 | virtual double *clockInterval(); | ||
| 123 | |||
| 124 | /// Provide clock shifts | ||
| 125 | virtual double *clockShift(); | ||
| 126 | |||
| 127 | /// Provide the right hand side | ||
| 128 | virtual void getRHS(double* f); | ||
| 129 | virtual void getResidual(double* f); | ||
| 130 | virtual void setConditions(bool* c); | ||
| 131 | virtual void getConditions(bool* c); | ||
| 132 | virtual void getClockConditions(bool* c); | ||
| 133 | /// Provide boolean variables | ||
| 134 | virtual void setBoolean(const bool* z); | ||
| 135 | |||
| 136 | /// Provide boolean variables | ||
| 137 | virtual void setContinuousStates(const double* z); | ||
| 138 | |||
| 139 | /// Provide integer variables | ||
| 140 | virtual void setInteger(const int* z); | ||
| 141 | |||
| 142 | /// Provide real variables | ||
| 143 | virtual void setReal(const double* z); | ||
| 144 | |||
| 145 | /// Provide string variables | ||
| 146 | virtual void setString(const std::string* z); | ||
| 147 | |||
| 148 | /// Provide clocks | ||
| 149 | virtual void setClock(const bool* tick, const bool* subactive); | ||
| 150 | |||
| 151 | /// Provide the right hand side | ||
| 152 | virtual void setStateDerivatives(const double* f); | ||
| 153 | |||
| 154 | /// (Re-) initialize the system of equations | ||
| 155 | void initialize(); | ||
| 156 | /// Set current integration time | ||
| 157 | void setTime(double t); | ||
| 158 | double getTime(); | ||
| 159 | /// Set tolerance for zero crossings | ||
| 160 | void setZeroTol(double dt); | ||
| 161 | double getZeroTol(); | ||
| 162 | |||
| 163 | /// Set modification status of independent variables | ||
| 164 | // (exploited by linear equation systems in Jacobians) | ||
| 165 | void setFreeVariablesLock(bool freeVariablesLock); | ||
| 166 | bool getFreeVariablesLock(); | ||
| 167 | |||
| 168 | IGlobalSettings* getGlobalSettings(); | ||
| 169 | |||
| 170 | shared_ptr<ISimObjects> getSimObjects() const; | ||
| 171 | string getModelName() const; | ||
| 172 | |||
| 173 | shared_ptr<ISimData> getSimData(); | ||
| 174 | shared_ptr<ISimVars> getSimVars(); | ||
| 175 | |||
| 176 | double computeNextTimeEvents(double currTime, std::pair<double, double>* timeEventPairs); | ||
| 177 | void computeTimeEventConditions(double currTime); | ||
| 178 | void setIntervalInTimEventData(int clockIdx, double interval); | ||
| 179 | void resetTimeConditions(); | ||
| 180 | |||
| 181 | virtual double& getRealStartValue(double& var); | ||
| 182 | virtual bool& getBoolStartValue(bool& var); | ||
| 183 | virtual int& getIntStartValue(int& var); | ||
| 184 | virtual string& getStringStartValue(string& var); | ||
| 185 | virtual void setRealStartValue(double& var, double val, bool overwriteOldValue = false); | ||
| 186 | virtual void setRealStartValue(BaseArray<double>& avar, double val, bool overwriteOldValue = false); | ||
| 187 | virtual void setRealStartValue(BaseArray<double>& avar, const BaseArray<double>& aval, bool overwriteOldValue = false); | ||
| 188 | virtual void setBoolStartValue(bool& var, bool val, bool overwriteOldValue = false); | ||
| 189 | virtual void setBoolStartValue(BaseArray<bool>& avar, bool val, bool overwriteOldValue = false); | ||
| 190 | virtual void setBoolStartValue(BaseArray<bool>& avar, const BaseArray<bool>& aval, bool overwriteOldValue = false); | ||
| 191 | virtual void setIntStartValue(int& var,int val, bool overwriteOldValue = false); | ||
| 192 | virtual void setIntStartValue(BaseArray<int>& avar, int val, bool overwriteOldValue = false); | ||
| 193 | virtual void setIntStartValue(BaseArray<int>& avar, const BaseArray<int>& aval, bool overwriteOldValue = false); | ||
| 194 | virtual void setStringStartValue(string& var, string val, bool overwriteOldValue = false); | ||
| 195 | virtual void setStringStartValue(BaseArray<string>& avar, string val, bool overwriteOldValue = true); | ||
| 196 | virtual void setStringStartValue(BaseArray<string>& avar, const BaseArray<string>& aval, bool overwriteOldValue = true); | ||
| 197 | protected: | ||
| 198 | void Assert(bool cond, const string& msg); | ||
| 199 | void Terminate(string msg); | ||
| 200 | void intDelay(vector<unsigned int> expr,vector<double> delay_max); | ||
| 201 | void storeDelay(unsigned int expr_id,double expr_value,double time); | ||
| 202 | void storeTime(double time); | ||
| 203 | double delay(unsigned int expr_id,double expr_value, double delayTime, double delayMax); | ||
| 204 | bool isConsistent(); | ||
| 205 | |||
| 206 | shared_ptr<ISimObjects> _simObjects; | ||
| 207 | |||
| 208 | double | ||
| 209 | _simTime, ///< current simulation time (given by the solver) | ||
| 210 | _zeroTol; ///< tolerance for zero crossings (given by the solver) | ||
| 211 | |||
| 212 | bool | ||
| 213 | * _conditions, ///< External conditions changed by the solver | ||
| 214 | * _conditions0, | ||
| 215 | * _time_conditions; | ||
| 216 | |||
| 217 | int | ||
| 218 | _dimContinuousStates, | ||
| 219 | _dimRHS, ///< Dimension der rechten Seite | ||
| 220 | _dimReal, ///< Anzahl der reelwertigen Variablen | ||
| 221 | _dimInteger, ///< Anzahl der integerwertigen Variablen | ||
| 222 | _dimBoolean, ///< Anzahl der boolwertigen Variablen | ||
| 223 | _dimString, ///< Anzahl der stringwertigen Variablen | ||
| 224 | _dimZeroFunc, ///< Dimension (=Anzahl) Nullstellenfunktion | ||
| 225 | _dimTimeEvent, ///< Dimension (=Anzahl) Time event (start zeit und frequenz) | ||
| 226 | _dimClock, ///< Dimension (=Anzahl) Clocks (active) | ||
| 227 | _dimAE; ///< Number (dimension) of algebraic equations (e.g. constraints from an algebraic loop) | ||
| 228 | |||
| 229 | std::pair<double, double>* | ||
| 230 | _timeEventData; | ||
| 231 | double* _currTimeEvents; | ||
| 232 | |||
| 233 | double *_clockInterval; ///< time interval between clock ticks | ||
| 234 | double *_clockShift; ///< time before first activation | ||
| 235 | double *_clockTime; ///< time of clock ticks | ||
| 236 | bool *_clockEventBased; ///< boolean event clock | ||
| 237 | bool *_clockCondition; ///< clock tick active | ||
| 238 | bool *_clockStart; ///< only active at clock start | ||
| 239 | bool *_clockSubactive; ///< don't update states | ||
| 240 | std::ostream *_outputStream; ///< Output stream for results | ||
| 241 | |||
| 242 | IContinuous::UPDATETYPE _callType; | ||
| 243 | |||
| 244 | bool _initial; | ||
| 245 | bool _terminal; | ||
| 246 | bool _terminate; | ||
| 247 | |||
| 248 | //SValuesMap _start_values; | ||
| 249 | InitVars<double> _real_start_values; | ||
| 250 | InitVars<int> _int_start_values; | ||
| 251 | InitVars<bool> _bool_start_values; | ||
| 252 | InitVars<string> _string_start_values; | ||
| 253 | double | ||
| 254 | *__z, ///< "Extended state vector", containing all states and algebraic variables of all types | ||
| 255 | *__zDot, ///< "Extended vector of derivatives", containing all right hand sides of differential and algebraic equations | ||
| 256 | *__daeResidual; | ||
| 257 | typedef std::deque<double> buffer_type; | ||
| 258 | typedef std::iterator_traits<buffer_type::iterator>::difference_type difference_type; | ||
| 259 | map<unsigned int, buffer_type> _delay_buffer; | ||
| 260 | buffer_type _time_buffer; | ||
| 261 | double _delay_max; | ||
| 262 | double _start_time; | ||
| 263 | IGlobalSettings* _global_settings; //this should be a reference, but this is not working if the libraries are linked statically | ||
| 264 | IEvent* _event_system; //this pointer to event system | ||
| 265 | string _modelName; | ||
| 266 | |||
| 267 | bool _sparse; | ||
| 268 | bool _useAnalyticalJacobian; | ||
| 269 | |||
| 270 | bool _freeVariablesLock; ///< modification status of independent variables | ||
| 271 | }; | ||
| 272 | |||
| 273 | /// Mark free variables unchanged. | ||
| 274 | /// Automatically release lock upon destruction, covering exceptions as well. | ||
| 275 | class SystemLockFreeVariables { | ||
| 276 | public: | ||
| 277 | SystemLockFreeVariables(SystemDefaultImplementation *system, bool lock = true) { | ||
| 278 | system->setFreeVariablesLock(lock); | ||
| 279 | _system = system; | ||
| 280 | } | ||
| 281 | |||
| 282 | ~SystemLockFreeVariables() { | ||
| 283 | _system->setFreeVariablesLock(false); | ||
| 284 | } | ||
| 285 | |||
| 286 | private: | ||
| 287 | SystemDefaultImplementation *_system; | ||
| 288 | }; | ||
| 289 | /** @} */ // end of coreSystem | ||
| 290 |