OMSimulatorLib
The OMSimulator project is a FMI-based co-simulation environment that supports ordinary (i.e., non-delayed) and TLM connections.
Model.h
Go to the documentation of this file.
1 /*
2  * This file is part of OpenModelica.
3  *
4  * Copyright (c) 1998-CurrentYear, 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 GPL VERSION 3 LICENSE OR
11  * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
12  * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
13  * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
14  * ACCORDING TO RECIPIENTS CHOICE.
15  *
16  * The OpenModelica software and the Open Source Modelica
17  * Consortium (OSMC) Public License (OSMC-PL) are obtained
18  * from OSMC, either from the above address,
19  * from the URLs: http://www.ida.liu.se/projects/OpenModelica or
20  * http://www.openmodelica.org, and in the OpenModelica distribution.
21  * GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
22  *
23  * This program is distributed WITHOUT ANY WARRANTY; without
24  * even the implied warranty of MERCHANTABILITY or FITNESS
25  * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
26  * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
27  *
28  * See the full OSMC Public License conditions for more details.
29  *
30  */
31 
32 #ifndef _OMS_MODEL_H_
33 #define _OMS_MODEL_H_
34 
35 #include "Clock.h"
36 #include "ComRef.h"
37 #include "Element.h"
38 #include "ResultWriter.h"
39 #include "Types.h"
40 #include <assert.h>
41 #include <pugixml.hpp>
42 
43 #if (BOOST_VERSION >= 105300)
44 #include <ctpl.h>
45 #else // use the standard queue
46 #include <ctpl_stl.h>
47 #endif
48 
49 namespace oms
50 {
51  class Component;
52  class System;
53 
54  class Model
55  {
56  public:
57  ~Model();
58 
63  static Model* NewModel(const ComRef& cref);
64  const ComRef& getCref() const {return cref;}
65  System* getSystem(const ComRef& cref);
66  oms_status_enu_t delete_(const ComRef& cref);
67  Component* getComponent(const ComRef& cref);
68  System* getTopLevelSystem() const {return system;}
69  std::string getTempDirectory() const {return tempDir;}
70  oms_status_enu_t rename(const ComRef& cref);
71  oms_status_enu_t list(const ComRef& cref, char** contents);
73  oms_status_enu_t exportToSSD(pugi::xml_node& node, pugi::xml_node& ssvNode) const;
74  oms_status_enu_t exportSnapshot(const ComRef& cref, char** contents);
75  oms_status_enu_t exportSSVTemplate(const ComRef& cref, const std::string& filename);
76  oms_status_enu_t exportSSMTemplate(const ComRef& cref, const std::string& filename);
77  oms_status_enu_t importFromSSD(const pugi::xml_node& node);
78  oms_status_enu_t importSnapshot(const char* snapshot);
79  oms_status_enu_t exportToFile(const std::string& filename) const;
80  oms_system_enu_t getSystemType(const pugi::xml_node& node, const std::string& sspVersion);
81  oms_system_enu_t getSystemTypeHelper(const pugi::xml_node& node, const std::string& sspVersion);
82  oms_status_enu_t updateParameterBindingsToSSD(pugi::xml_node& node, pugi::xml_node& ssvNode, bool isTopSystemOrModel) const;
83  void copyResources(bool copy_resources) {this->copy_resources = copy_resources;}
84  bool copyResources() {return copy_resources;}
85 
86  oms::Element** getElements() {return &elements[0];}
87  oms_status_enu_t getAllResources(std::vector<std::string>& resources) const;
88 
91  oms_status_enu_t simulate_asynchronous(void (*cb)(const char* cref, double time, oms_status_enu_t status));
96 
98 
99  oms_status_enu_t setStartTime(double value);
100  double getStartTime() const {return startTime;}
101  oms_status_enu_t setStopTime(double value);
102  double getStopTime() const {return stopTime;}
103 
105  double getLoggingInterval() const {return loggingInterval;}
106  oms_status_enu_t setResultFile(const std::string& filename, int bufferSize);
107  oms_status_enu_t getResultFile(char** filename, int* bufferSize);
108  oms_status_enu_t emit(double time, bool force=false, bool* emitted=NULL);
109  oms_status_enu_t addSignalsToResults(const char* regex);
110  oms_status_enu_t removeSignalsFromResults(const char* regex);
111  oms_status_enu_t setSignalFilter(const std::string& regex);
112  oms_status_enu_t getSignalFilter(char** regex);
113 
115  bool cancelSimulation() const {return cancelSim;}
116 
117  bool validState(int validStates) const {return (modelState & validStates);}
118 
119  bool isIsolatedFMUModel() const {return isolatedFMU;}
121 
122  bool useThreadPool() {return (pool != nullptr);}
123  ctpl::thread_pool& getThreadPool() {assert(pool); return *pool;}
124 
125  oms_status_enu_t loadSnapshot(const char* snapshot);
126 
127  pugi::xml_node getSnapshot() {return snapShot;}
128 
129  private:
130  Model(const ComRef& cref, const std::string& tempDir);
131 
132  // stop the compiler generating methods copying the object
133  Model(Model const& copy);
134  Model& operator=(Model const& copy);
135 
137 
138  private:
140  System* system = NULL;
141  std::string tempDir;
142 
143  std::vector<oms::Element*> elements;
144  bool copy_resources = true;
145 
147 
148  // ssd:DefaultExperiment
149  double startTime = 0.0;
150  double stopTime = 1.0;
151 
153  double lastEmit;
154  double loggingInterval = 0.0;
155  int bufferSize = 10;
156  std::string resultFilename;
158 
159  std::string signalFilter = ".*";
160 
161  pugi::xml_node snapShot;
162 
163  bool cancelSim;
164  bool isolatedFMU = false;
165 
166  ctpl::thread_pool* pool = nullptr;
167  };
168 }
169 
170 #endif
oms_status_enu_t importSnapshot(const char *snapshot)
Definition: Model.cpp:168
oms_status_enu_t setResultFile(const std::string &filename, int bufferSize)
Definition: Model.cpp:1127
oms_status_enu_t list(const ComRef &cref, char **contents)
Definition: Model.cpp:264
oms_status_enu_t removeSignalsFromResults(const char *regex)
Definition: Model.cpp:1212
oms_status_enu_t emit(double time, bool force=false, bool *emitted=NULL)
Definition: Model.cpp:1104
System * getTopLevelSystem() const
Definition: Model.h:68
oms_status_enu_t initialize()
Definition: Model.cpp:906
Definition: ResultWriter.h:68
Definition: Model.h:54
Definition: Clock.h:38
oms_status_enu_t exportToSSD(pugi::xml_node &node, pugi::xml_node &ssvNode) const
Definition: Model.cpp:560
Model & operator=(Model const &copy)
not implemented
oms_status_enu_t setLoggingInterval(double loggingInterval)
Definition: Model.cpp:1085
double getLoggingInterval() const
Definition: Model.h:105
Model(const ComRef &cref, const std::string &tempDir)
Definition: Model.cpp:46
oms_status_enu_t loadSnapshot(const char *snapshot)
Definition: Model.cpp:124
oms_modelState_enu_t modelState
Definition: Model.h:146
oms_status_enu_t setStopTime(double value)
Definition: Model.cpp:870
ComRef cref
Definition: Model.h:139
bool validState(int validStates) const
Definition: Model.h:117
pugi::xml_node snapShot
top level snapshot node which contains ssd, ssv and ssm as child nodes
Definition: Model.h:161
~Model()
Definition: Model.cpp:56
std::string getTempDirectory() const
Definition: Model.h:69
std::string tempDir
Definition: Model.h:141
int bufferSize
Definition: Model.h:155
oms_status_enu_t
Definition: Types.h:43
oms_status_enu_t addSignalsToResults(const char *regex)
Definition: Model.cpp:1204
System * system
Definition: Model.h:140
oms_status_enu_t getSignalFilter(char **regex)
Definition: Model.cpp:1178
oms_status_enu_t setSignalFilter(const std::string &regex)
Definition: Model.cpp:1185
oms_modelState_enu_t getModelState() const
Definition: Model.h:97
ctpl::thread_pool * pool
Definition: Model.h:166
oms_modelState_enu_t
Definition: Types.h:52
System * getSystem(const ComRef &cref)
Definition: Model.cpp:215
oms_status_enu_t terminate()
Definition: Model.cpp:1034
double stopTime
Definition: Model.h:150
oms_status_enu_t simulate()
Definition: Model.cpp:992
void setIsolatedFMUModel()
Definition: Model.h:120
oms_status_enu_t exportSnapshot(const ComRef &cref, char **contents)
Definition: Model.cpp:343
double getStartTime() const
Definition: Model.h:100
oms_system_enu_t getSystemType(const pugi::xml_node &node, const std::string &sspVersion)
Definition: Model.cpp:675
oms_status_enu_t setStartTime(double value)
Definition: Model.cpp:861
oms_status_enu_t addSystem(const ComRef &cref, oms_system_enu_t type)
Definition: Model.cpp:534
double startTime
Definition: Model.h:149
ComRef - component reference.
Definition: ComRef.h:43
oms_status_enu_t cancelSimulation_asynchronous()
Definition: Model.cpp:1220
oms_system_enu_t
Definition: Types.h:121
oms_status_enu_t simulate_asynchronous(void(*cb)(const char *cref, double time, oms_status_enu_t status))
Definition: Model.cpp:980
Definition: Component.h:56
double loggingInterval
Definition: Model.h:154
pugi::xml_node getSnapshot()
Definition: Model.h:127
oms_status_enu_t stepUntil(double stopTime)
Definition: Model.cpp:1013
Component * getComponent(const ComRef &cref)
Definition: Model.cpp:232
oms_status_enu_t rename(const ComRef &cref)
Definition: Model.cpp:115
oms_status_enu_t exportSSMTemplate(const ComRef &cref, const std::string &filename)
Definition: Model.cpp:460
std::string signalFilter
default
Definition: Model.h:159
bool isolatedFMU
Definition: Model.h:164
bool useThreadPool()
Definition: Model.h:122
Clock clock
Definition: Model.h:157
bool cancelSim
Definition: Model.h:163
std::string resultFilename
default <name>_res.mat
Definition: Model.h:156
bool copy_resources
Definition: Model.h:144
oms_status_enu_t registerSignalsForResultFile()
Definition: Model.cpp:1093
double getStopTime() const
Definition: Model.h:102
Definition: AlgLoop.h:44
oms_status_enu_t importFromSSD(const pugi::xml_node &node)
Definition: Model.cpp:599
std::vector< oms::Element * > elements
Definition: Model.h:143
ctpl::thread_pool & getThreadPool()
Definition: Model.h:123
oms_status_enu_t getAllResources(std::vector< std::string > &resources) const
Definition: Model.cpp:853
oms_status_enu_t exportSSVTemplate(const ComRef &cref, const std::string &filename)
Definition: Model.cpp:414
void copyResources(bool copy_resources)
Definition: Model.h:83
oms_status_enu_t reset()
Definition: Model.cpp:1064
oms_system_enu_t getSystemTypeHelper(const pugi::xml_node &node, const std::string &sspVersion)
Definition: Model.cpp:726
bool isIsolatedFMUModel() const
Definition: Model.h:119
bool cancelSimulation() const
Definition: Model.h:115
Element.
Definition: Element.h:52
oms_status_enu_t getResultFile(char **filename, int *bufferSize)
Definition: Model.cpp:1170
static Model * NewModel(const ComRef &cref)
Definition: Model.cpp:81
Definition: Types.h:53
const ComRef & getCref() const
Definition: Model.h:64
ResultWriter * resultFile
Definition: Model.h:152
oms_status_enu_t updateParameterBindingsToSSD(pugi::xml_node &node, pugi::xml_node &ssvNode, bool isTopSystemOrModel) const
Definition: Model.cpp:510
Definition: System.h:67
oms_status_enu_t instantiate()
Definition: Model.cpp:879
oms::Element ** getElements()
Definition: Model.h:86
oms_status_enu_t delete_(const ComRef &cref)
Definition: Model.cpp:246
bool copyResources()
Definition: Model.h:84
oms_status_enu_t exportToFile(const std::string &filename) const
Definition: Model.cpp:749
double lastEmit
Definition: Model.h:153