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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
use std::collections::BTreeMap;
use libc::size_t;
use slotmap::{new_key_type, SlotMap};
use yaserde_derive::YaDeserialize;
new_key_type! {
pub struct UnitKey;
pub struct TypeKey;
pub struct VariableKey;
}
#[derive(Debug)]
pub enum Type {
Float64Type {
name: String,
description: Option<String>,
quantity: Option<String>,
unit: Option<UnitKey>,
min: Option<f64>,
max: Option<f64>,
nominal: Option<f64>,
},
}
impl Type {
fn new(raw_type: raw::Type, unit_map: &BTreeMap<String, UnitKey>) -> Self {
match raw_type {
raw::Type::Float32Type { name, description } => todo!(),
raw::Type::Float64Type {
name,
description,
quantity,
unit,
min,
max,
nominal,
} => Type::Float64Type {
name,
description,
quantity,
unit: unit.map(|ref unit_name| unit_map[unit_name]),
min,
max,
nominal,
},
}
}
}
#[derive(Debug)]
pub struct ModelDescription {
/// Version of FMI that was used to generate the XML file.
pub fmi_version: String,
/// The name of the model as used in the modeling environment that generated the XML file, such as Modelica.Mechanics.Rotational.Examples.CoupledClutches.
pub model_name: String,
/// Optional string with a brief description of the model.
pub description: String,
/// The instantiationToken is a string that can be used by the FMU to check that the XML file is compatible with the implementation of the FMU.
pub instantiation_token: String,
/// String with the name and organization of the model author.
pub author: Option<String>,
/// Version of the model [for example 1.0].
pub version: Option<String>,
/// Information on the intellectual property copyright for this FMU [for example © My Company 2011].
pub copyright: Option<String>,
/// Information on the intellectual property licensing for this FMU [for example BSD license <license text or link to license>].
pub license: Option<String>,
/// Name of the tool that generated the XML file.
pub generation_tool: Option<String>,
/// Date and time when the XML file was generated. The format is a subset of dateTime and should be: YYYY-MM-DDThh:mm:ssZ (with one T between date and time; Z characterizes the Zulu time zone, in other words, Greenwich meantime) [for example 2009-12-08T14:33:22Z].
pub generation_date_and_time: Option<String>,
/// Defines whether the variable names in <ModelVariables> and in <TypeDefinitions> follow a particular convention.
pub variable_naming_convention: raw::VariableNamingConvention,
units: SlotMap<UnitKey, raw::Unit>,
types: SlotMap<TypeKey, Type>,
}
impl From<raw::ModelDescription> for ModelDescription {
fn from(model_description: raw::ModelDescription) -> Self {
let raw::ModelDescription {
fmi_version,
model_name,
description,
instantiation_token,
author,
version,
copyright,
license,
generation_tool,
generation_date_and_time,
variable_naming_convention,
unit_definitions,
type_definitions,
model_exchange,
co_simulation,
scheduled_execution,
default_experiment,
} = model_description;
let mut units: SlotMap<UnitKey, _> = SlotMap::with_key();
let mut types: SlotMap<TypeKey, _> = SlotMap::with_key();
let unit_map: BTreeMap<_, _> = unit_definitions
.map(|unit_definitions| {
unit_definitions
.units
.into_iter()
.map(|unit| (unit.name.clone(), units.insert(unit)))
.collect()
})
.unwrap_or_default();
let type_map: BTreeMap<_, _> = type_definitions
.map(|type_definition| {
type_definition
.types
.into_iter()
.map(|ty| (ty.name().to_owned(), types.insert(Type::new(ty, &unit_map))))
.collect()
})
.unwrap_or_default();
ModelDescription {
fmi_version,
model_name,
description,
instantiation_token,
author,
version,
copyright,
license,
generation_tool,
generation_date_and_time,
variable_naming_convention,
units,
types,
}
}
}
mod raw {
use super::*;
#[derive(PartialEq, Debug, YaDeserialize)]
pub enum VariableNamingConvention {
/// A list of strings (the default).
#[yaserde(rename = "flat")]
Flat,
/// Hierarchical names with . as hierarchy separator, and with array elements and derivative characterization.
#[yaserde(rename = "structured")]
Structured,
}
impl Default for VariableNamingConvention {
fn default() -> Self {
Self::Flat
}
}
#[derive(Default, PartialEq, Debug, YaDeserialize)]
#[yaserde(rename = "fmiModelDescription")]
pub struct ModelDescription {
/// Version of FMI that was used to generate the XML file.
#[yaserde(attribute, rename = "fmiVersion")]
pub fmi_version: String,
/// The name of the model as used in the modeling environment that generated the XML file, such as Modelica.Mechanics.Rotational.Examples.CoupledClutches.
#[yaserde(attribute, rename = "modelName")]
pub model_name: String,
/// Optional string with a brief description of the model.
#[yaserde(attribute)]
pub description: String,
/// The instantiationToken is a string that can be used by the FMU to check that the XML file is compatible with the implementation of the FMU.
#[yaserde(attribute, rename = "instantiationToken")]
pub instantiation_token: String,
/// String with the name and organization of the model author.
#[yaserde(attribute)]
pub author: Option<String>,
/// Version of the model [for example 1.0].
#[yaserde(attribute)]
pub version: Option<String>,
/// Information on the intellectual property copyright for this FMU [for example © My Company 2011].
#[yaserde(attribute)]
pub copyright: Option<String>,
/// Information on the intellectual property licensing for this FMU [for example BSD license <license text or link to license>].
#[yaserde(attribute)]
pub license: Option<String>,
/// Name of the tool that generated the XML file.
#[yaserde(attribute, rename = "generationTool")]
pub generation_tool: Option<String>,
/// Date and time when the XML file was generated. The format is a subset of dateTime and should be: YYYY-MM-DDThh:mm:ssZ (with one T between date and time; Z characterizes the Zulu time zone, in other words, Greenwich meantime) [for example 2009-12-08T14:33:22Z].
#[yaserde(attribute, rename = "generationDateAndTime")]
pub generation_date_and_time: Option<String>,
/// Defines whether the variable names in <ModelVariables> and in <TypeDefinitions> follow a particular convention.
#[yaserde(attribute, rename = "variableNamingConvention")]
pub variable_naming_convention: VariableNamingConvention,
/// If present, the FMU is based on FMI for Model Exchange
#[yaserde(child, rename = "ModelExchange")]
pub model_exchange: Option<ModelExchange>,
/// If present, the FMU is based on FMI for Co-Simulation
#[yaserde(child, rename = "CoSimulation")]
pub co_simulation: Option<CoSimulation>,
/// If present, the FMU is based on FMI for Scheduled Execution
#[yaserde(child, rename = "ScheduledExecution")]
pub scheduled_execution: Option<ScheduledExecution>,
/// Providing default settings for the integrator, such as stop time and relative tolerance.
#[yaserde(child, rename = "DefaultExperiment")]
pub default_experiment: Option<DefaultExperiment>,
/// A global list of unit and display unit definitions
#[yaserde(child, rename = "UnitDefinitions")]
pub unit_definitions: Option<UnitDefinitions>,
/// A global list of type definitions that are utilized in `ModelVariables`
#[yaserde(child, rename = "TypeDefinitions")]
pub type_definitions: Option<TypeDefinitions>,
}
#[derive(Default, PartialEq, Debug, YaDeserialize)]
pub struct ModelExchange {
#[yaserde(attribute, rename = "modelIdentifier")]
pub model_identifier: String,
#[yaserde(attribute, rename = "canGetAndSetFMUState")]
pub can_get_and_set_fmu_state: bool,
#[yaserde(attribute, rename = "canSerializeFMUState")]
pub can_serialize_fmu_state: bool,
}
#[derive(Default, PartialEq, Debug, YaDeserialize)]
pub struct CoSimulation {
#[yaserde(attribute, rename = "modelIdentifier")]
pub model_identifier: String,
#[yaserde(attribute, rename = "canGetAndSetFMUState")]
pub can_get_and_set_fmu_state: bool,
#[yaserde(attribute, rename = "canSerializeFMUState")]
pub can_serialize_fmu_state: bool,
#[yaserde(attribute, rename = "canHandleVariableCommunicationStepSize")]
pub can_handle_variable_communication_step_size: bool,
#[yaserde(attribute, rename = "providesIntermediateUpdate")]
pub provides_intermediate_update: bool,
#[yaserde(attribute, rename = "canReturnEarlyAfterIntermediateUpdate")]
pub can_return_early_after_intermediate_update: bool,
#[yaserde(attribute, rename = "fixedInternalStepSize")]
pub fixed_internal_step_size: f64,
}
#[derive(Default, PartialEq, Debug, YaDeserialize)]
pub struct ScheduledExecution {
#[yaserde(attribute, rename = "modelIdentifier")]
pub model_identifier: String,
}
#[derive(Default, PartialEq, Debug, YaDeserialize)]
pub struct DefaultExperiment {
#[yaserde(attribute, rename = "startTime")]
pub start_time: Option<f64>,
#[yaserde(attribute, rename = "stopTime")]
pub stop_time: Option<f64>,
#[yaserde(attribute, rename = "tolerance")]
pub tolerange: Option<f64>,
#[yaserde(attribute, rename = "stepSize")]
pub step_size: Option<f64>,
}
/// https://fmi-standard.org/docs/3.0-dev/#_definition_of_units
#[derive(Default, PartialEq, Debug, YaDeserialize)]
pub struct UnitDefinitions {
#[yaserde(child, rename = "Unit")]
pub units: Vec<Unit>,
}
#[derive(Default, PartialEq, Debug, YaDeserialize)]
pub struct Unit {
#[yaserde(attribute)]
pub name: String,
#[yaserde(child, rename = "BaseUnit")]
pub base_unit: BaseUnit,
}
fn default_factor() -> f64 {
1.0
}
/// The Unit definition consists of the exponents of the 7 SI base units kg, m, s, A, K, mol, cd, the exponent of the SI derived unit rad, and optionally a factor and an offset.
#[derive(Default, PartialEq, Debug, YaDeserialize)]
pub struct BaseUnit {
#[yaserde(attribute)]
pub kg: i32,
#[yaserde(attribute)]
pub m: i32,
#[yaserde(attribute)]
pub s: i32,
#[yaserde(attribute)]
pub A: i32,
#[yaserde(attribute)]
pub K: i32,
#[yaserde(attribute)]
pub mol: i32,
#[yaserde(attribute)]
pub cd: i32,
#[yaserde(attribute)]
pub rad: i32,
#[yaserde(attribute, default = "default_factor")]
pub factor: f64,
#[yaserde(attribute)]
pub offset: f64,
}
#[derive(Default, PartialEq, Debug, YaDeserialize)]
pub struct TypeDefinitions {
#[yaserde(child = "Float64Type")]
pub types: Vec<Type>,
}
#[derive(PartialEq, Debug, YaDeserialize)]
pub enum Type {
Float32Type {
#[yaserde(attribute)]
name: String,
#[yaserde(attribute)]
description: Option<String>,
},
#[yaserde(tag = "Float64Type")]
Float64Type {
#[yaserde(attribute)]
name: String,
#[yaserde(attribute)]
description: Option<String>,
#[yaserde(attribute)]
quantity: Option<String>,
#[yaserde(attribute)]
unit: Option<String>,
#[yaserde(attribute)]
min: Option<f64>,
#[yaserde(attribute)]
max: Option<f64>,
#[yaserde(attribute)]
nominal: Option<f64>,
},
}
impl Type {
pub fn name(&self) -> &str {
match self {
Type::Float32Type { name, description } => todo!(),
Type::Float64Type { name, .. } => &name,
}
}
}
impl Default for Type {
fn default() -> Self {
todo!()
}
}
#[derive(PartialEq, Debug, YaDeserialize)]
struct ModelVariables {
pub variables: Vec<Variable>,
}
enum Causality {
/// A data value that is constant during the simulation
Parameter,
/// A data value that is constant during the simulation and is computed during initialization or when tunable parameters change.
CalculatedParameter,
/// The variable value can be provided by the importer.
Input,
/// The variable value can be used by the importer.
Output,
Local,
/// The independent variable (usually time [but could also be, for example, angle]).
Independent,
/// The variable value can only be changed in Configuration Mode or Reconfiguration Mode.
StructuralParameter,
}
impl Default for Causality {
/// The default of causality is local.
fn default() -> Self {
Self::Local
}
}
struct Poo {
/// The full, unique name of the variable.
name: String,
/// A handle of the variable to efficiently identify the variable value in the model interface and for references within the modelDescription.xml
valueReference: u32,
/// An optional description string describing the meaning of the variable.
description: Option<String>,
/// Enumeration that defines the causality of the variable.
causality: Causality,
}
#[derive(PartialEq, Debug, YaDeserialize)]
enum Variable {
Float32 {},
Float64 {},
Int8,
UInt8,
Int16,
UInt16,
Int32,
UInt32,
Int64,
Uint64,
Boolean,
String,
Binary,
Enumeration,
Clock,
}
impl Default for Variable {
fn default() -> Self {
todo!()
}
}
}
#[cfg(test)]
mod tests {
use super::{raw, ModelDescription};
#[test]
fn test_model_descr() {
let meta_content = std::env::current_dir()
.map(|path| path.join("tests/data/FMI3.xml"))
.and_then(std::fs::read_to_string)
.unwrap();
let meta: raw::ModelDescription = yaserde::de::from_str(&meta_content).unwrap();
let meta2 = ModelDescription::from(meta);
dbg!(meta2);
}
}