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