OMCompiler/SimulationRuntime/cpp/Solver/IDA/IDA.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 | |||
| 30 | #include "FactoryExport.h" | ||
| 31 | |||
| 32 | #include <Core/Solver/SolverDefaultImplementation.h> | ||
| 33 | #include <sundials/sundials_context.h> | ||
| 34 | #include <sundials/sundials_logger.h> | ||
| 35 | #include <idas/idas.h> | ||
| 36 | #include <nvector/nvector_serial.h> | ||
| 37 | #include <sunlinsol/sunlinsol_dense.h> /* Default dense linear solver */ | ||
| 38 | |||
| 39 | |||
| 40 | #ifdef RUNTIME_PROFILING | ||
| 41 | #include <Core/Utils/extension/measure_time.hpp> | ||
| 42 | #endif | ||
| 43 | |||
| 44 | #include <Core/Utils/extension/logger.hpp> | ||
| 45 | |||
| 46 | /*****************************************************************************/ | ||
| 47 | // IDA aus dem SUNDIALS-Package | ||
| 48 | // BDF-Verfahren für steife und nicht-steife ODEs | ||
| 49 | // Dokumentation siehe offizielle IDA Doku | ||
| 50 | |||
| 51 | |||
| 52 | class Ida | ||
| 53 | : public ISolver, public SolverDefaultImplementation | ||
| 54 | { | ||
| 55 | public: | ||
| 56 | |||
| 57 | Ida(IMixedSystem* system, ISolverSettings* settings); | ||
| 58 | |||
| 59 | virtual ~Ida(); | ||
| 60 | |||
| 61 | // geerbt von Object (in SolverDefaultImplementation) | ||
| 62 | //--------------------------------------- | ||
| 63 | /// Spezielle Solvereinstellungen setzten (default oder user defined) | ||
| 64 | virtual void initialize(); | ||
| 65 | |||
| 66 | |||
| 67 | // geerbt von ISolver | ||
| 68 | //--------------------------------------- | ||
| 69 | /// Setzen der Startzeit für die numerische Lösung | ||
| 70 | 4 | virtual void setStartTime(const double& time) | |
| 71 | { | ||
| 72 | 4 | SolverDefaultImplementation::setStartTime(time); | |
| 73 | 4 | }; | |
| 74 | |||
| 75 | /// Setzen der Endzeit für die numerische Lösung | ||
| 76 | 3 | virtual void setEndTime(const double& time) | |
| 77 | { | ||
| 78 | 3 | SolverDefaultImplementation::setEndTime(time); | |
| 79 | 3 | }; | |
| 80 | |||
| 81 | /// Setzen der initialen Schrittweite (z.B. auch nach Nullstelle) | ||
| 82 | 1 | virtual void setInitStepSize(const double& stepSize) | |
| 83 | { | ||
| 84 | 1 | SolverDefaultImplementation::setInitStepSize(stepSize); | |
| 85 | 1 | }; | |
| 86 | |||
| 87 | /// Berechung der numerischen Lösung innerhalb eines gegebenen Zeitintervalls | ||
| 88 | virtual void solve(const SOLVERCALL command = UNDEF_CALL); | ||
| 89 | |||
| 90 | /// Liefert den Status des Solvers nach Beendigung der Simulation | ||
| 91 | 1 | virtual ISolver::SOLVERSTATUS getSolverStatus() | |
| 92 | { | ||
| 93 | 1 | return (SolverDefaultImplementation::getSolverStatus()); | |
| 94 | }; | ||
| 95 | |||
| 96 | //// Ausgabe von statistischen Informationen (wird vom SimManager nach Abschluß der Simulation aufgerufen) | ||
| 97 | virtual void writeSimulationInfo(); | ||
| 98 | virtual void setTimeOut(unsigned int time_out); | ||
| 99 | |||
| 100 | virtual void stop(); | ||
| 101 | |||
| 102 | virtual int reportErrorMessage(std::ostream& messageStream); | ||
| 103 | virtual bool stateSelection(); | ||
| 104 | private: | ||
| 105 | |||
| 106 | // Solveraufruf | ||
| 107 | void IDACore(); | ||
| 108 | |||
| 109 | /// Kapselung der Berechnung der rechten Seite | ||
| 110 | int calcFunction(const double& time, const double* y,double *yp, double* res); | ||
| 111 | |||
| 112 | // Callback für die rechte Seite | ||
| 113 | static int rhsFunctionCB(double t, N_Vector y, N_Vector ydot, N_Vector, void *user_data); | ||
| 114 | static void errOutputIDA(int line, const char *func, const char *file, const char *msg, | ||
| 115 | SUNErrCode err_code, void *err_user_data, SUNContext sunctx); | ||
| 116 | // Checks error flags of SUNDIALS | ||
| 117 | int check_flag(void *flagvalue, const char *funcname, int opt); | ||
| 118 | |||
| 119 | // Nulltellenfunktion | ||
| 120 | void giveZeroVal(const double &t,const double *y,const double *yp,double *zeroValue); | ||
| 121 | void writeIDAOutput(const double &time,const double &h,const int &stp); | ||
| 122 | bool isInterrupted(); | ||
| 123 | // Callback der Nullstellenfunktion | ||
| 124 | static int zeroFunctionCB(double t, N_Vector y, N_Vector yp, double *zeroval, void *user_data); | ||
| 125 | |||
| 126 | // Functions for Coloured Jacobian | ||
| 127 | static int jacobianFunctionCB(long int N, sunrealtype t, N_Vector y, N_Vector fy, SUNMatrix Jac,void *user_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3); | ||
| 128 | int calcJacobian(double t, long int N, N_Vector fHelp, N_Vector errorWeight, N_Vector jthcol, double* y, N_Vector fy, SUNMatrix Jac); | ||
| 129 | |||
| 130 | |||
| 131 | |||
| 132 | |||
| 133 | ISolverSettings | ||
| 134 | *_idasettings; ///< Input - Solver settings | ||
| 135 | |||
| 136 | void | ||
| 137 | *_idaMem, ///< Temp - Memory for the solver | ||
| 138 | *_data; ///< Temp - User data. Contains pointer to ida | ||
| 139 | |||
| 140 | long int | ||
| 141 | _dimSys, ///< Input - (total) Dimension of system (=number of ODE) | ||
| 142 | _dimStates, | ||
| 143 | _dimAE, | ||
| 144 | _idid, ///< Input, Output - Status Flag | ||
| 145 | _locStps, ///< Output - Number of Steps between two events | ||
| 146 | _cv_rt; ///< Temp - ida return flag | ||
| 147 | |||
| 148 | int | ||
| 149 | _outStps, ///< Output - Total number of output-steps | ||
| 150 | *_zeroSign; | ||
| 151 | |||
| 152 | |||
| 153 | double | ||
| 154 | /*old state vector | ||
| 155 | *_z, ///< Output - (Current) State vector | ||
| 156 | *_zInit, ///< Temp - Initial state vector | ||
| 157 | *_zWrite, ///< Temp - Zustand den das System rausschreibt | ||
| 158 | */ | ||
| 159 | *_absTol, /// - Vektor für absolute Toleranzen | ||
| 160 | *_delta, | ||
| 161 | *_deltaInv, | ||
| 162 | *_ysave, | ||
| 163 | *_y, ///< Output - (Current) State vector and dae vars vector | ||
| 164 | *_yInit, | ||
| 165 | *_yWrite, | ||
| 166 | *_ypWrite, | ||
| 167 | *_yp, ///< Output - (Current) Derivatives for State vector and dae vars vector | ||
| 168 | *_dae_res; | ||
| 169 | double | ||
| 170 | _hOut; ///< Temp - Ouput step size for dense output | ||
| 171 | |||
| 172 | unsigned int | ||
| 173 | _event_n; | ||
| 174 | double | ||
| 175 | _tLastEvent; | ||
| 176 | |||
| 177 | double | ||
| 178 | _tOut, ///< Output - Time for dense output | ||
| 179 | _tZero, ///< Temp - Nullstelle | ||
| 180 | _tLastWrite; ///< Temp - Letzter Ausgabezeitpunkt | ||
| 181 | |||
| 182 | bool | ||
| 183 | _bWritten, ///< Temp - Is output already written | ||
| 184 | _zeroFound; | ||
| 185 | |||
| 186 | |||
| 187 | |||
| 188 | |||
| 189 | N_Vector | ||
| 190 | _CV_y0, ///< Temp - Initial values in the ida Format | ||
| 191 | _CV_y, ///< Temp - State in ida Format | ||
| 192 | _CV_yp, ///<Temp - Stateders in ida Format | ||
| 193 | _CV_yWrite, ///< Temp - Vector for dense out | ||
| 194 | _CV_ypWrite, | ||
| 195 | _CV_absTol, | ||
| 196 | _ida_ySolver; ///< Temp - Vector templated used by linear solver | ||
| 197 | |||
| 198 | SUNLinearSolver | ||
| 199 | _ida_linSol; ///< Temp - Linear solver object used by IDA | ||
| 200 | |||
| 201 | SUNMatrix | ||
| 202 | _ida_J; ///< Temp - Matrix template for cloning matrices needed within linear solver | ||
| 203 | |||
| 204 | /* SUNDIALS simulation context. Owned by this solver instance. */ | ||
| 205 | SUNContext _sunctx; | ||
| 206 | |||
| 207 | // Variables for Coloured Jacobians | ||
| 208 | int* _colorOfColumn; | ||
| 209 | int _maxColors; | ||
| 210 | matrix_t _jacobianA; | ||
| 211 | int _jacobianANonzeros; | ||
| 212 | int const* _jacobianAIndex; | ||
| 213 | int const* _jacobianALeadindex; | ||
| 214 | |||
| 215 | |||
| 216 | bool _ida_initialized; | ||
| 217 | |||
| 218 | |||
| 219 | ISystemProperties* _properties; | ||
| 220 | IContinuous* _continuous_system; | ||
| 221 | IEvent* _event_system; | ||
| 222 | IMixedSystem* _mixed_system; | ||
| 223 | ITime* _time_system; | ||
| 224 | |||
| 225 | #ifdef RUNTIME_PROFILING | ||
| 226 | std::vector<MeasureTimeData*> *measureTimeFunctionsArray; | ||
| 227 | MeasureTimeValues *measuredFunctionStartValues, *measuredFunctionEndValues, *solveFunctionStartValues, *solveFunctionEndValues; | ||
| 228 | MeasureTimeValuesSolver *solverValues; | ||
| 229 | #endif | ||
| 230 | }; | ||
| 231 |