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 <cvode/cvode.h> /* prototypes for CVODE fcts., consts. */
44#include <nvector/nvector_serial.h> /* serial N_Vector types, fcts., macros */
45#include <sunlinsol/sunlinsol_dense.h> /* Default dense linear solver */
46
47 namespace oms
48{
49 class Model;
50 class ComponentFMUME;
51 class Component;
52 int cvode_rhs(realtype t, N_Vector y, N_Vector ydot, void* user_data);
53 int cvode_rhs_algebraic(realtype t, N_Vector y, N_Vector ydot, void* user_data);
54 int cvode_roots(realtype t, N_Vector y, realtype *gout, void* user_data);
55
56 class SystemSC : public System
57 {
58 public:
59 ~SystemSC();
60
61 static System* NewSystem(const oms::ComRef& cref, Model* parentModel, System* parentSystem);
62 oms_status_enu_t exportToSSD_SimulationInformation(pugi::xml_node& node) const;
63 oms_status_enu_t importFromSSD_SimulationInformation(const pugi::xml_node& node, const std::string& sspVersion);
64
65 oms_status_enu_t instantiate();
66 oms_status_enu_t initialize();
67 oms_status_enu_t terminate();
68 oms_status_enu_t reset();
69 oms_status_enu_t doStep();
70 oms_status_enu_t stepUntil(double stopTime);
71
72 oms_status_enu_t updateInputs(DirectedGraph& graph);
73
74 std::string getSolverName() const;
75 oms_status_enu_t setSolverMethod(std::string);
76
77 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;}
78
79 private:
80 oms_status_enu_t doStepEuler();
81 oms_status_enu_t doStepCVODE();
82
83 protected:
84 SystemSC(const ComRef& cref, Model* parentModel, System* parentSystem);
85
86 // stop the compiler generating methods copying the object
87 SystemSC(SystemSC const& copy);
88 SystemSC& operator=(SystemSC const& copy);
89
90 private:
91 std::vector<Component*> fmus; // use Component Base class to support FMI 2 ME and FMI 3 ME
92
93 bool* callEventUpdate = new bool[fmus.size()](); //initialize with false
94 bool* terminateSimulation = new bool[fmus.size()](); //initialize with false
95
96 std::vector<size_t> nStates;
97 std::vector<size_t> nEventIndicators;
98
99 std::vector<double*> states;
100 std::vector<double*> states_der;
101 std::vector<double*> states_nominal;
102 std::vector<double*> event_indicators;
103 std::vector<double*> event_indicators_prev;
104
105 bool algebraic = false;
106
107 struct SolverDataEuler_t
108 {
109 };
110
111 struct SolverDataCVODE_t
112 {
113 void *mem;
114 N_Vector y;
115 SUNLinearSolver linSol; /* linear solver object */
116 SUNMatrix J; /* Matrix used by linear solver */
117 N_Vector liny; /* Vector used by linear solver */
118 N_Vector abstol;
119 };
120
121 union SolverData_t
122 {
123 SolverDataEuler_t euler;
124 SolverDataCVODE_t cvode;
125 } solverData;
126
127 friend int oms::cvode_rhs(realtype t, N_Vector y, N_Vector ydot, void* user_data);
128 friend int oms::cvode_rhs_algebraic(realtype t, N_Vector y, N_Vector ydot, void* user_data);
129 friend int oms::cvode_roots(realtype t, N_Vector y, realtype *gout, void* user_data);
130 };
131}
132
133#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:57
oms::SystemSC::importFromSSD_SimulationInformation
oms_status_enu_t importFromSSD_SimulationInformation(const pugi::xml_node &node, const std::string &sspVersion)
Definition SystemSC.cpp:214
oms::SystemSC::setSolverMethod
oms_status_enu_t setSolverMethod(std::string)
Definition SystemSC.cpp:181
oms::SystemSC::doStepEuler
oms_status_enu_t doStepEuler()
Definition SystemSC.cpp:593
oms::SystemSC::event_indicators
std::vector< double * > event_indicators
Definition SystemSC.h:102
oms::SystemSC::algebraic
bool algebraic
Definition SystemSC.h:105
oms::SystemSC::getSolverName
std::string getSolverName() const
Definition SystemSC.cpp:168
oms::SystemSC::terminate
oms_status_enu_t terminate()
Definition SystemSC.cpp:447
oms::SystemSC::NewSystem
static System * NewSystem(const oms::ComRef &cref, Model *parentModel, System *parentSystem)
Definition SystemSC.cpp:150
oms::SystemSC::reset
oms_status_enu_t reset()
Definition SystemSC.cpp:512
oms::SystemSC::setSolver
oms_status_enu_t setSolver(oms_solver_enu_t solver)
Definition SystemSC.h:77
oms::SystemSC::exportToSSD_SimulationInformation
oms_status_enu_t exportToSSD_SimulationInformation(pugi::xml_node &node) const
Definition SystemSC.cpp:193
oms::SystemSC::solverData
union oms::SystemSC::SolverData_t solverData
oms::SystemSC::~SystemSC
~SystemSC()
Definition SystemSC.cpp:146
oms::SystemSC::event_indicators_prev
std::vector< double * > event_indicators_prev
Definition SystemSC.h:103
oms::SystemSC::nStates
std::vector< size_t > nStates
Definition SystemSC.h:96
oms::SystemSC::fmus
std::vector< Component * > fmus
Definition SystemSC.h:91
oms::SystemSC::states
std::vector< double * > states
Definition SystemSC.h:99
oms::SystemSC::SystemSC
SystemSC(SystemSC const &copy)
not implemented
oms::SystemSC::callEventUpdate
bool * callEventUpdate
Definition SystemSC.h:93
oms::SystemSC::operator=
SystemSC & operator=(SystemSC const &copy)
not implemented
oms::SystemSC::terminateSimulation
bool * terminateSimulation
Definition SystemSC.h:94
oms::SystemSC::doStepCVODE
oms_status_enu_t doStepCVODE()
Definition SystemSC.cpp:814
oms::SystemSC::initialize
oms_status_enu_t initialize()
Definition SystemSC.cpp:264
oms::SystemSC::instantiate
oms_status_enu_t instantiate()
Definition SystemSC.cpp:241
oms::SystemSC::states_nominal
std::vector< double * > states_nominal
Definition SystemSC.h:101
oms::SystemSC::updateInputs
oms_status_enu_t updateInputs(DirectedGraph &graph)
Definition SystemSC.cpp:1002
oms::SystemSC::stepUntil
oms_status_enu_t stepUntil(double stopTime)
Definition SystemSC.cpp:976
oms::SystemSC::states_der
std::vector< double * > states_der
Definition SystemSC.h:100
oms::SystemSC::doStep
oms_status_enu_t doStep()
Definition SystemSC.cpp:578
oms::SystemSC::nEventIndicators
std::vector< size_t > nEventIndicators
Definition SystemSC.h:97
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:49
oms::cvode_rhs_algebraic
int cvode_rhs_algebraic(realtype t, N_Vector y, N_Vector ydot, void *user_data)
Definition SystemSC.cpp:99
oms::cvode_roots
int cvode_roots(realtype t, N_Vector y, realtype *gout, void *user_data)
Definition SystemSC.cpp:111
oms::cvode_rhs
int cvode_rhs(realtype t, N_Vector y, N_Vector ydot, void *user_data)
Definition SystemSC.cpp:51
oms::SystemSC::SolverDataCVODE_t
Definition SystemSC.h:112
oms::SystemSC::SolverDataCVODE_t::J
SUNMatrix J
Definition SystemSC.h:116
oms::SystemSC::SolverDataCVODE_t::linSol
SUNLinearSolver linSol
Definition SystemSC.h:115
oms::SystemSC::SolverDataCVODE_t::y
N_Vector y
Definition SystemSC.h:114
oms::SystemSC::SolverDataCVODE_t::abstol
N_Vector abstol
Definition SystemSC.h:118
oms::SystemSC::SolverDataCVODE_t::mem
void * mem
Definition SystemSC.h:113
oms::SystemSC::SolverDataCVODE_t::liny
N_Vector liny
Definition SystemSC.h:117
oms::SystemSC::SolverDataEuler_t
Definition SystemSC.h:108
oms::SystemSC::SolverData_t
Definition SystemSC.h:122
oms::SystemSC::SolverData_t::euler
SolverDataEuler_t euler
Definition SystemSC.h:123
oms::SystemSC::SolverData_t::cvode
SolverDataCVODE_t cvode
Definition SystemSC.h:124