-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmodel_description.hpp
228 lines (179 loc) · 5.44 KB
/
model_description.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* \file
* Model-descriptive types and constants.
*
* \copyright
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#ifndef COSIM_MODEL_HPP
#define COSIM_MODEL_HPP
#include <cstdint>
#include <optional>
#include <ostream>
#include <string>
#include <string_view>
#include <variant>
#include <vector>
namespace cosim
{
/// Unsigned integer type used for variable identifiers.
using value_reference = std::uint32_t;
/// Variable data types.
enum class variable_type
{
real,
integer,
boolean,
string,
enumeration
};
/// Variable causalities. These correspond to FMI causality definitions.
enum variable_causality
{
parameter,
calculated_parameter,
input,
output,
local
};
/// Variable variabilities. These correspond to FMI variability definitions.
enum variable_variability
{
constant,
fixed,
tunable,
discrete,
continuous
};
/// A list of simulator capabilities
struct simulator_capabilities
{
bool can_save_state = false;
bool can_export_state = false;
};
/// Returns a textual representation of `v`.
constexpr const char* to_text(variable_type v)
{
switch (v) {
case variable_type::real: return "real";
case variable_type::integer: return "integer";
case variable_type::boolean: return "boolean";
case variable_type::string: return "string";
case variable_type::enumeration: return "enumeration";
default: return "NULL";
}
}
/// Returns a textual representation of `v`.
constexpr const char* to_text(variable_causality v)
{
switch (v) {
case variable_causality::parameter: return "parameter";
case variable_causality::calculated_parameter: return "calculated_parameter";
case variable_causality::input: return "input";
case variable_causality::output: return "output";
case variable_causality::local: return "local";
default: return "NULL";
}
}
/// Returns a textual representation of `v`.
constexpr const char* to_text(variable_variability v)
{
switch (v) {
case variable_variability::constant: return "constant";
case variable_variability::fixed: return "fixed";
case variable_variability::tunable: return "tunable";
case variable_variability::discrete: return "discrete";
case variable_variability::continuous: return "continuous";
default: return "NULL";
}
}
/// Writes a textual representation of `v` to `stream`.
inline std::ostream& operator<<(std::ostream& stream, variable_type v)
{
return stream << to_text(v);
}
/// Writes a textual representation of `v` to `stream`.
inline std::ostream& operator<<(std::ostream& stream, variable_causality v)
{
return stream << to_text(v);
}
/// Writes a textual representation of `v` to `stream`.
inline std::ostream& operator<<(std::ostream& stream, variable_variability v)
{
return stream << to_text(v);
}
/**
* An algebraic type that can hold a scalar value of one of the supported
* variable types.
*/
using scalar_value = std::variant<double, int, bool, std::string>;
/**
* An algebraic type that can hold a (possibly) non-owning, read-only view
* of a scalar value of one of the supported variable types.
*
* In practice, it's only for strings that this type is a view; for all
* other types it holds a copy.
*/
using scalar_value_view = std::variant<double, int, bool, std::string_view>;
/// A description of a model variable.
struct variable_description
{
/**
* A textual identifier for the variable.
*
* The name must be unique within the model.
*/
std::string name;
/**
* A numerical identifier for the value the variable refers to.
*
* The variable reference must be unique within the model and data type.
* That is, a real variable and an integer variable may have the same
* value reference, and they will be considered different. If two
* variables of the same type have the same value reference, they will
* be considered as aliases of each other.
*/
value_reference reference;
/// The variable's data type.
variable_type type;
/// The variable's causality.
variable_causality causality;
/// The variable's variability.
variable_variability variability;
/// The variable's start value.
std::optional<scalar_value> start;
};
/// A description of a model.
struct model_description
{
/// The model name.
std::string name;
/// A universally unique identifier (UUID) for the model.
std::string uuid;
/// A human-readable description of the model.
std::string description;
/// Author information.
std::string author;
/// Version information.
std::string version;
/// Variable descriptions.
std::vector<variable_description> variables;
/// Simulator capabilities
simulator_capabilities capabilities;
};
/// Getter for returning a variable description.
std::optional<variable_description> find_variable(const model_description& description, const std::string& variable_name);
/// Possible outcomes of a subsimulator time step
enum class step_result
{
/// Step completed
complete,
/// Step failed, but can be retried with a shorter step size
failed,
/// Step canceled
canceled
};
} // namespace cosim
#endif // COSIM_MODEL_HPP