coral
A C++ library for distributed co-simulation
model.hpp
Go to the documentation of this file.
1 
10 #ifndef CORAL_MODEL_HPP
11 #define CORAL_MODEL_HPP
12 
13 #include <cstdint>
14 #include <iterator>
15 #include <limits>
16 #include <map>
17 #include <string>
18 #include <vector>
19 
20 #include <boost/range/adaptor/map.hpp>
21 #include <boost/variant.hpp>
22 
23 #include <coral/config.h>
24 
25 
26 namespace coral
27 {
29 namespace model
30 {
31 
32 
35 
36 
38 const StepID INVALID_STEP_ID = -1;
39 
40 
42 typedef double TimePoint;
43 
44 
47 
48 
56 typedef double TimeDuration;
57 
58 
61 
62 
64 const SlaveID INVALID_SLAVE_ID = 0;
65 
66 
69 
70 
73 {
74  REAL_DATATYPE = 1,
75  INTEGER_DATATYPE = 1 << 1,
76  BOOLEAN_DATATYPE = 1 << 2,
77  STRING_DATATYPE = 1 << 3,
78 };
79 
80 
83 {
84  PARAMETER_CAUSALITY = 1,
85  CALCULATED_PARAMETER_CAUSALITY = 1 << 1,
86  INPUT_CAUSALITY = 1 << 2,
87  OUTPUT_CAUSALITY = 1 << 3,
88  LOCAL_CAUSALITY = 1 << 4,
89 };
90 
91 
94 {
95  CONSTANT_VARIABILITY = 1,
96  FIXED_VARIABILITY = 1 << 1,
97  TUNABLE_VARIABILITY = 1 << 2,
98  DISCRETE_VARIABILITY = 1 << 3,
99  CONTINUOUS_VARIABILITY = 1 << 4,
100 };
101 
102 
105 {
106 public:
109  const std::string& name,
110  coral::model::DataType dataType,
111  coral::model::Causality causality,
112  coral::model::Variability variability);
113 
121 
127  const std::string& Name() const;
128 
131 
134 
137 
138 private:
139  VariableID m_id;
140  std::string m_name;
141  coral::model::DataType m_dataType;
142  coral::model::Causality m_causality;
143  coral::model::Variability m_variability;
144 };
145 
146 
149 {
151 public:
153  typedef boost::select_second_const_range<VariablesMap>
155 
156  // Construction/destruction
157  SlaveTypeDescription() CORAL_NOEXCEPT;
158 
159  template<typename VariableDescriptionRange>
161  const std::string& name,
162  const std::string& uuid,
163  const std::string& description,
164  const std::string& author,
165  const std::string& version,
166  const VariableDescriptionRange& variables);
167 
168  ~SlaveTypeDescription() = default;
169 
170  // Copy
171  SlaveTypeDescription(const SlaveTypeDescription&) = default;
172  SlaveTypeDescription& operator=(const SlaveTypeDescription&) = default;
173 
174  // Move
175  CORAL_DEFINE_DEFAULT_MOVE(SlaveTypeDescription,
176  m_name, m_uuid, m_description, m_author, m_version, m_variables)
177 
179  const std::string& Name() const;
180 
182  const std::string& UUID() const;
183 
185  const std::string& Description() const;
186 
188  const std::string& Author() const;
189 
191  const std::string& Version() const;
192 
194  ConstVariablesRange Variables() const;
195 
202  const VariableDescription& Variable(VariableID id) const;
203 
204 private:
205  std::string m_name;
206  std::string m_uuid;
207  std::string m_description;
208  std::string m_author;
209  std::string m_version;
210  VariablesMap m_variables;
211 };
212 
213 
216 {
217 public:
218  // Construction/destruction
219  explicit SlaveDescription(
220  SlaveID id = INVALID_SLAVE_ID,
221  const std::string& name = std::string(),
222  const SlaveTypeDescription& typeDescription = SlaveTypeDescription());
223 
224  ~SlaveDescription() = default;
225 
226  // Copy
227  SlaveDescription(const SlaveDescription&) = default;
228  SlaveDescription& operator=(const SlaveDescription&) = default;
229 
230  // Move
231  CORAL_DEFINE_DEFAULT_MOVE(SlaveDescription, m_id, m_name, m_typeDescription)
232 
233 
234  SlaveID ID() const;
235 
237  void SetID(SlaveID value);
238 
240  const std::string& Name() const;
241 
243  void SetName(const std::string& value);
244 
246  const SlaveTypeDescription& TypeDescription() const;
247 
249  void SetTypeDescription(const SlaveTypeDescription& value);
250 
251 private:
252  SlaveID m_id;
253  std::string m_name;
254  SlaveTypeDescription m_typeDescription;
255 };
256 
257 
259 typedef boost::variant<double, int, bool, std::string> ScalarValue;
260 
261 
263 DataType DataTypeOf(const ScalarValue& v);
264 
265 
270 class Variable
271 {
272 public:
273  explicit Variable(SlaveID slave = INVALID_SLAVE_ID, VariableID id = 0)
274  : m_slave(slave), m_id(id) { }
275 
276  coral::model::SlaveID Slave() const CORAL_NOEXCEPT { return m_slave; }
277  coral::model::VariableID ID() const CORAL_NOEXCEPT { return m_id; }
278  bool Empty() const CORAL_NOEXCEPT { return m_slave == INVALID_SLAVE_ID; }
279 
280 private:
281  coral::model::SlaveID m_slave;
283 };
284 
285 
292 bool operator==(const Variable& a, const Variable& b);
293 
295 bool operator!=(const Variable& a, const Variable& b);
296 
297 
303 {
304 public:
307  VariableID variable,
308  const ScalarValue& value);
309 
312  VariableID inputVar,
313  const coral::model::Variable& outputVar);
314 
320  VariableID inputVar,
321  const ScalarValue& value,
322  const coral::model::Variable& outputVar);
323 
325  VariableID Variable() const CORAL_NOEXCEPT;
326 
328  bool HasValue() const CORAL_NOEXCEPT;
329 
334  const ScalarValue& Value() const;
335 
337  bool IsConnected() const CORAL_NOEXCEPT;
338 
343  const coral::model::Variable& ConnectedOutput() const;
344 
345 private:
346  VariableID m_variable;
347  bool m_hasValue;
348  ScalarValue m_value;
349  coral::model::Variable m_connectedOutput;
350 };
351 
352 
359 bool IsValidSlaveName(const std::string& s);
360 
361 
362 // =============================================================================
363 // Function template definitions
364 // =============================================================================
365 
366 template<typename VariableDescriptionRange>
367 SlaveTypeDescription::SlaveTypeDescription(
368  const std::string& name,
369  const std::string& uuid,
370  const std::string& description,
371  const std::string& author,
372  const std::string& version,
373  const VariableDescriptionRange& variables)
374  : m_name(name),
375  m_uuid(uuid),
376  m_description(description),
377  m_author(author),
378  m_version(version)
379 {
380  for (const auto& v : variables) {
381  m_variables.insert(std::make_pair(v.ID(), v));
382  }
383 }
384 
385 
386 }} // namespace
387 #endif // header guard
DataType DataTypeOf(const ScalarValue &v)
Returns the type of data stored in the given ScalarValue.
An object that identifies a variable in a simulation, and which consists of a slave ID and a variable...
Definition: model.hpp:270
boost::select_second_const_range< VariablesMap > ConstVariablesRange
The return type of the Variables() function.
Definition: model.hpp:154
coral::model::VariableID ID() const
An identifier which uniquely refers to this variable in the context of a single slave type...
STL namespace.
boost::variant< double, int, bool, std::string > ScalarValue
An algebraic type that can hold values of all supported data types.
Definition: model.hpp:259
coral::model::Variability Variability() const
The variable&#39;s variability.
STL class.
std::int32_t StepID
A number that uniquely identifies a time step in an execution.
Definition: model.hpp:34
A description of a slave type.
Definition: model.hpp:148
std::uint16_t SlaveID
Unsigned integer type used for slave identifiers.
Definition: model.hpp:60
const StepID INVALID_STEP_ID
A number which will never be used for an actual time step ID.
Definition: model.hpp:38
Definition: variable_io.hpp:28
bool IsValidSlaveName(const std::string &s)
Returns whether s contains a valid slave name.
Causality
Variable causalities. These correspond to FMI causality definitions.
Definition: model.hpp:82
T make_pair(T...args)
coral::model::DataType DataType() const
The variable&#39;s data type.
T infinity(T...args)
double TimeDuration
The type used to specify (simulation) time durations.
Definition: model.hpp:56
const TimePoint ETERNITY
A special TimePoint value that lies infinitely far in the future.
Definition: model.hpp:46
std::uint32_t VariableID
Unsigned integer type used for variable identifiers.
Definition: model.hpp:68
Variability
Variable variabilities. These correspond to FMI variability definitions.
Definition: model.hpp:93
A description of a specific slave.
Definition: model.hpp:215
const std::string & Name() const
A human-readable name for the variable.
DataType
Variable data types.
Definition: model.hpp:72
bool operator==(const Variable &a, const Variable &b)
Equality comparison for Variable objects.
bool operator!=(const Variable &a, const Variable &b)
Inequality comparison for Variable objects, defined as !(a==b).
const SlaveID INVALID_SLAVE_ID
An invalid slave identifier.
Definition: model.hpp:64
A description of a single variable.
Definition: model.hpp:104
double TimePoint
The type used to specify (simulation) time points.
Definition: model.hpp:42
An object which represents the action of assigning an initial value to a variable, or to connect it to another variable.
Definition: model.hpp:302
coral::model::Causality Causality() const
The variable&#39;s causality.