OMSimulatorLib The OMSimulator project is a FMI-based co-simulation environment.

Loading...
Searching...
No Matches
SystemSC.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenModelica.
3 *
4 * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC),
5 * c/o Linköpings universitet, Department of Computer and Information Science,
6 * SE-58183 Linköping, Sweden.
7 *
8 * All rights reserved.
9 *
10 * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF AGPL VERSION 3 LICENSE OR
11 * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8.
12 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
13 * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GNU AGPL
14 * VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
15 *
16 * The OpenModelica software and the OSMC (Open Source Modelica Consortium)
17 * Public License (OSMC-PL) are obtained from OSMC, either from the above
18 * address, from the URLs:
19 * http://www.openmodelica.org or
20 * https://github.com/OpenModelica/ or
21 * http://www.ida.liu.se/projects/OpenModelica,
22 * and in the OpenModelica distribution.
23 *
24 * GNU AGPL version 3 is obtained from:
25 * https://www.gnu.org/licenses/licenses.html#GPL
26 *
27 * This program is distributed WITHOUT ANY WARRANTY; without
28 * even the implied warranty of MERCHANTABILITY or FITNESS
29 * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
30 * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
31 *
32 * See the full OSMC Public License conditions for more details.
33 *
34 */
35
36#ifndef _OMS_SYSTEM_SC_H_
37#define _OMS_SYSTEM_SC_H_
38
39#include "ComRef.h"
40#include "System.h"
41#include "OMSimulator/Types.h"
42
43#include <memory>
44
45#include <sundials/sundials_context.h> /* SUNContext */
46#include <sundials/sundials_logger.h> /* SUNLogger */
47#include <cvode/cvode.h> /* prototypes for CVODE fcts., consts. */
48#include <nvector/nvector_serial.h> /* serial N_Vector types, fcts., macros */
49#include <sunlinsol/sunlinsol_dense.h> /* Default dense linear solver */
50
51 namespace oms
52{
53 class Model;
54 class ComponentFMUME;
55 class Component;
56 int cvode_rhs(sunrealtype t, N_Vector y, N_Vector ydot, void* user_data);
57 int cvode_rhs_algebraic(sunrealtype t, N_Vector y, N_Vector ydot, void* user_data);
58 int cvode_roots(sunrealtype t, N_Vector y, sunrealtype *gout, void* user_data);
59
60 class SystemSC : public System
61 {
62 public:
63 ~SystemSC();
64
65 static System* NewSystem(const oms::ComRef& cref, Model* parentModel, System* parentSystem);
66 oms_status_enu_t exportToSSD_SimulationInformation(pugi::xml_node& node) const;
67 oms_status_enu_t importFromSSD_SimulationInformation(const pugi::xml_node& node, const std::string& sspVersion);
68
69 oms_status_enu_t instantiate();
70 oms_status_enu_t initialize();
71 oms_status_enu_t terminate();
72 oms_status_enu_t reset();
73 oms_status_enu_t doStep();
74 oms_status_enu_t stepUntil(double stopTime);
75
76 oms_status_enu_t updateInputs(DirectedGraph& graph);
77
78 std::string getSolverName() const;
79 oms_status_enu_t setSolverMethod(std::string);
80
81 oms_status_enu_t setSolver(oms_solver_enu_t solver) {if (solver > oms_solver_sc_min && solver < oms_solver_sc_max) {solverMethod=solver; return oms_status_ok;} return oms_status_error;}
82
83 private:
84 oms_status_enu_t doStepEuler();
85 oms_status_enu_t doStepCVODE();
86
87 protected:
88 SystemSC(const ComRef& cref, Model* parentModel, System* parentSystem);
89
90 // stop the compiler generating methods copying the object
91 SystemSC(SystemSC const& copy);
92 SystemSC& operator=(SystemSC const& copy);
93
94 private:
95 std::vector<Component*> fmus; // use Component Base class to support FMI 2 ME and FMI 3 ME
96
97 // One entry per FMU, allocated by initialize() once fmus is filled.
98 std::unique_ptr<bool[]> callEventUpdate; //initialized with false
99 std::unique_ptr<bool[]> terminateSimulation; //initialized with false
100
101 std::vector<size_t> nStates;
102 std::vector<size_t> nEventIndicators;
103
104 std::vector<double*> states;
105 std::vector<double*> states_der;
106 std::vector<double*> states_nominal;
107 std::vector<double*> event_indicators;
108 std::vector<double*> event_indicators_prev;
109
110 bool algebraic = false;
111
112 struct SolverDataEuler_t
113 {
114 };
115
116 struct SolverDataCVODE_t
117 {
118 SUNContext sunctx; /* SUNDIALS simulation context */
119 void *mem;
120 N_Vector y;
121 SUNLinearSolver linSol; /* linear solver object */
122 SUNMatrix J; /* Matrix used by linear solver */
123 N_Vector liny; /* Vector used by linear solver */
124 N_Vector abstol;
125 };
126
127 union SolverData_t
128 {
129 SolverDataEuler_t euler;
130 SolverDataCVODE_t cvode;
131 } solverData;
132
133 friend int oms::cvode_rhs(sunrealtype t, N_Vector y, N_Vector ydot, void* user_data);
134 friend int oms::cvode_rhs_algebraic(sunrealtype t, N_Vector y, N_Vector ydot, void* user_data);
135 friend int oms::cvode_roots(sunrealtype t, N_Vector y, sunrealtype *gout, void* user_data);
136 };
137}
138
139#endif
ComRef.h
System.h
oms::ComRef
ComRef - component reference.
Definition ComRef.h:51
oms::DirectedGraph
Definition DirectedGraph.h:71
oms::Model
Definition Model.h:57
oms::SystemSC
Definition SystemSC.h:61
oms::SystemSC::callEventUpdate
std::unique_ptr< bool[]> callEventUpdate
Definition SystemSC.h:98
oms::SystemSC::importFromSSD_SimulationInformation
oms_status_enu_t importFromSSD_SimulationInformation(const pugi::xml_node &node, const std::string &sspVersion)
Definition SystemSC.cpp:228
oms::SystemSC::setSolverMethod
oms_status_enu_t setSolverMethod(std::string)
Definition SystemSC.cpp:195
oms::SystemSC::doStepEuler
oms_status_enu_t doStepEuler()
Definition SystemSC.cpp:632
oms::SystemSC::event_indicators
std::vector< double * > event_indicators
Definition SystemSC.h:107
oms::SystemSC::algebraic
bool algebraic
Definition SystemSC.h:110
oms::SystemSC::getSolverName
std::string getSolverName() const
Definition SystemSC.cpp:182
oms::SystemSC::terminate
oms_status_enu_t terminate()
Definition SystemSC.cpp:482
oms::SystemSC::NewSystem
static System * NewSystem(const oms::ComRef &cref, Model *parentModel, System *parentSystem)
Definition SystemSC.cpp:164
oms::SystemSC::reset
oms_status_enu_t reset()
Definition SystemSC.cpp:548
oms::SystemSC::setSolver
oms_status_enu_t setSolver(oms_solver_enu_t solver)
Definition SystemSC.h:81
oms::SystemSC::exportToSSD_SimulationInformation
oms_status_enu_t exportToSSD_SimulationInformation(pugi::xml_node &node) const
Definition SystemSC.cpp:207
oms::SystemSC::solverData
union oms::SystemSC::SolverData_t solverData
oms::SystemSC::~SystemSC
~SystemSC()
Definition SystemSC.cpp:160
oms::SystemSC::event_indicators_prev
std::vector< double * > event_indicators_prev
Definition SystemSC.h:108
oms::SystemSC::nStates
std::vector< size_t > nStates
Definition SystemSC.h:101
oms::SystemSC::fmus
std::vector< Component * > fmus
Definition SystemSC.h:95
oms::SystemSC::states
std::vector< double * > states
Definition SystemSC.h:104
oms::SystemSC::SystemSC
SystemSC(SystemSC const &copy)
not implemented
oms::SystemSC::operator=
SystemSC & operator=(SystemSC const &copy)
not implemented
oms::SystemSC::terminateSimulation
std::unique_ptr< bool[]> terminateSimulation
Definition SystemSC.h:99
oms::SystemSC::doStepCVODE
oms_status_enu_t doStepCVODE()
Definition SystemSC.cpp:855
oms::SystemSC::initialize
oms_status_enu_t initialize()
Definition SystemSC.cpp:278
oms::SystemSC::instantiate
oms_status_enu_t instantiate()
Definition SystemSC.cpp:255
oms::SystemSC::states_nominal
std::vector< double * > states_nominal
Definition SystemSC.h:106
oms::SystemSC::updateInputs
oms_status_enu_t updateInputs(DirectedGraph &graph)
Definition SystemSC.cpp:1046
oms::SystemSC::stepUntil
oms_status_enu_t stepUntil(double stopTime)
Definition SystemSC.cpp:1020
oms::SystemSC::states_der
std::vector< double * > states_der
Definition SystemSC.h:105
oms::SystemSC::doStep
oms_status_enu_t doStep()
Definition SystemSC.cpp:617
oms::SystemSC::nEventIndicators
std::vector< size_t > nEventIndicators
Definition SystemSC.h:102
oms::System
Definition System.h:66
oms::System::cref
ComRef cref
Definition System.h:232
oms::System::solverMethod
oms_solver_enu_t solverMethod
Definition System.h:221
oms::System::parentSystem
System * parentSystem
Definition System.h:235
oms::System::parentModel
Model * parentModel
Definition System.h:234
oms
Definition AlgLoop.h:52
oms::cvode_rhs_algebraic
int cvode_rhs_algebraic(sunrealtype t, N_Vector y, N_Vector ydot, void *user_data)
Definition SystemSC.cpp:113
oms::cvode_rhs
int cvode_rhs(sunrealtype t, N_Vector y, N_Vector ydot, void *user_data)
Definition SystemSC.cpp:65
oms::cvode_roots
int cvode_roots(sunrealtype t, N_Vector y, sunrealtype *gout, void *user_data)
Definition SystemSC.cpp:125
oms::SystemSC::SolverDataCVODE_t
Definition SystemSC.h:117
oms::SystemSC::SolverDataCVODE_t::sunctx
SUNContext sunctx
Definition SystemSC.h:118
oms::SystemSC::SolverDataCVODE_t::J
SUNMatrix J
Definition SystemSC.h:122
oms::SystemSC::SolverDataCVODE_t::linSol
SUNLinearSolver linSol
Definition SystemSC.h:121
oms::SystemSC::SolverDataCVODE_t::y
N_Vector y
Definition SystemSC.h:120
oms::SystemSC::SolverDataCVODE_t::abstol
N_Vector abstol
Definition SystemSC.h:124
oms::SystemSC::SolverDataCVODE_t::mem
void * mem
Definition SystemSC.h:119
oms::SystemSC::SolverDataCVODE_t::liny
N_Vector liny
Definition SystemSC.h:123
oms::SystemSC::SolverDataEuler_t
Definition SystemSC.h:113
oms::SystemSC::SolverData_t
Definition SystemSC.h:128
oms::SystemSC::SolverData_t::euler
SolverDataEuler_t euler
Definition SystemSC.h:129
oms::SystemSC::SolverData_t::cvode
SolverDataCVODE_t cvode
Definition SystemSC.h:130