fmi-schema 0.7.0

XML schema support for FMI 2.0 and 3.0
Documentation
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
use crate::{Error, traits::FmiModelDescription};

use super::{
    CoSimulation, Fmi2Unit, Fmi2VariableDependency, ModelExchange, ScalarVariable, SimpleType,
};

#[derive(Default, Debug, hard_xml::XmlRead, hard_xml::XmlWrite)]
#[xml(
    tag = "fmiModelDescription",
    strict(unknown_attribute, unknown_element)
)]
pub struct Fmi2ModelDescription {
    /// Version of FMI (Clarification for FMI 2.0.2: for FMI 2.0.x revisions fmiVersion is defined
    /// as "2.0").
    #[xml(attr = "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.
    #[xml(attr = "modelName")]
    pub model_name: String,

    /// Fingerprint of xml-file content to verify that xml-file and C-functions are compatible to
    /// each other
    #[xml(attr = "guid")]
    pub guid: String,

    #[xml(attr = "description")]
    pub description: Option<String>,

    /// Version of FMU, e.g., "1.4.1"
    #[xml(attr = "version")]
    pub version: Option<String>,

    /// Information on intellectual property copyright for this FMU, such as "© MyCompany 2011"
    #[xml(attr = "copyright")]
    pub copyright: Option<String>,

    /// Information on intellectual property licensing for this FMU, such as "BSD license",
    /// "Proprietary", or "Public Domain"
    #[xml(attr = "license")]
    pub license: Option<String>,

    /// Name of the tool that generated the XML file.
    #[xml(attr = "generationTool")]
    pub generation_tool: Option<String>,

    /// time/date of database creation according to ISO 8601 (preference: YYYY-MM-DDThh:mm:ss)
    /// 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].
    #[xml(attr = "generationDateAndTime")]
    pub generation_date_and_time: Option<String>,

    /// Defines whether the variable names in `<ModelVariables>` and in `<TypeDefinitions>` follow a
    /// particular convention.
    #[xml(attr = "variableNamingConvention")]
    pub variable_naming_convention: Option<String>,

    /// FMI 2.0: Required for ModelExchange, ignored for Co-Simulation (may be absent).
    #[xml(attr = "numberOfEventIndicators")]
    pub number_of_event_indicators: Option<u32>,

    /// If present, the FMU is based on FMI for Model Exchange
    #[xml(child = "ModelExchange")]
    pub model_exchange: Option<ModelExchange>,

    /// If present, the FMU is based on FMI for Co-Simulation
    #[xml(child = "CoSimulation")]
    pub co_simulation: Option<CoSimulation>,

    #[xml(child = "LogCategories")]
    pub log_categories: Option<LogCategories>,

    #[xml(child = "DefaultExperiment")]
    pub default_experiment: Option<DefaultExperiment>,

    #[xml(child = "UnitDefinitions")]
    pub unit_definitions: Option<UnitDefinitions>,

    #[xml(child = "TypeDefinitions")]
    pub type_definitions: Option<TypeDefinitions>,

    #[xml(child = "ModelVariables", default)]
    pub model_variables: ModelVariables,

    #[xml(child = "ModelStructure", default)]
    pub model_structure: ModelStructure,
}

impl Fmi2ModelDescription {
    /// Total number of variables
    pub fn num_variables(&self) -> usize {
        self.model_variables.variables.len()
    }

    /// Get the number of continuous states (and derivatives)
    pub fn num_states(&self) -> usize {
        self.model_structure.derivatives.unknowns.len()
    }

    pub fn num_event_indicators(&self) -> usize {
        self.number_of_event_indicators.unwrap_or(0) as usize
    }

    /// Get a iterator of the SalarVariables
    pub fn get_model_variables(&self) -> impl Iterator<Item = &ScalarVariable> {
        self.model_variables.variables.iter()
    }

    #[cfg(false)]
    pub fn get_model_variable_by_vr(&self, vr: u32) -> Option<&ScalarVariable> {
        self.model_variables.map.get(&vr)
    }

    /// Turns an UnknownList into a nested Vector of ScalarVariables and their Dependencies
    #[cfg(false)]
    fn map_unknowns(
        &self,
        list: &UnknownList,
    ) -> Result<Vec<UnknownsTuple>, ModelDescriptionError> {
        list.unknowns
            .iter()
            .map(|unknown| {
                self.model_variables
                    .by_index
                    // Variable indices start at 1 in the modelDescription
                    .get(unknown.index as usize - 1)
                    .map(|vr| &self.model_variables.map[vr])
                    .ok_or_else(|| {
                        ModelDescriptionError::VariableAtIndexNotFound(
                            self.model_name.clone(),
                            unknown.index as usize,
                        )
                    })
                    .and_then(|var| {
                        let deps = unknown
                            .dependencies
                            .iter()
                            .map(|dep| {
                                self.model_variables
                                    .by_index
                                    .get(*dep as usize - 1)
                                    .map(|vr| &self.model_variables.map[vr])
                                    .ok_or_else(|| {
                                        ModelDescriptionError::VariableAtIndexNotFound(
                                            self.model_name.clone(),
                                            *dep as usize,
                                        )
                                    })
                            })
                            .collect::<Result<Vec<_>, ModelDescriptionError>>()?;

                        Ok((var, deps))
                    })
            })
            .collect()
    }

    /// Get a reference to the vector of Unknowns marked as outputs
    #[cfg(false)]
    pub fn outputs(&self) -> Result<Vec<UnknownsTuple>, ModelDescriptionError> {
        self.map_unknowns(&self.model_structure.outputs)
    }

    /// Get a reference to the vector of Unknowns marked as derivatives
    #[cfg(false)]
    pub fn derivatives(&self) -> Result<Vec<UnknownsTuple>, ModelDescriptionError> {
        self.map_unknowns(&self.model_structure.derivatives)
    }

    /// Get a reference to the vector of Unknowns marked as initial_unknowns
    #[cfg(false)]
    pub fn initial_unknowns(&self) -> Result<Vec<UnknownsTuple>, ModelDescriptionError> {
        self.map_unknowns(&self.model_structure.initial_unknowns)
    }

    /// Get a reference to the model variable with the given name
    pub fn model_variable_by_name(&self, name: &str) -> Result<&ScalarVariable, Error> {
        self.model_variables
            .variables
            .iter()
            .find(|var| var.name == name)
            .ok_or_else(|| Error::VariableNotFound(name.to_owned()))
    }

    /// This private function is used to de-reference variable indices from the UnknownList and
    /// Real{derivative}
    #[cfg(false)]
    fn model_variable_by_index(
        &self,
        idx: usize,
    ) -> Result<&ScalarVariable, ModelDescriptionError> {
        self.model_variables
            .by_index
            .get(idx - 1)
            .map(|vr| &self.model_variables.map[vr])
            .ok_or_else(|| {
                ModelDescriptionError::VariableAtIndexNotFound(
                    self.model_name.clone(),
                    idx as usize,
                )
            })
    }

    /// Return a vector of tuples `(&ScalarVariable, &ScalarVariabel)`, where the 1st is a
    /// continuous-time state, and the 2nd is its derivative.
    #[cfg(false)]
    pub fn continuous_states(
        &self,
    ) -> Result<Vec<(&ScalarVariable, &ScalarVariable)>, ModelDescriptionError> {
        self.model_structure
            .derivatives
            .unknowns
            .iter()
            .map(|unknown| {
                self.model_variable_by_index(unknown.index as usize)
                    .and_then(|der| {
                        if let ScalarVariableElement::Real { derivative, .. } = der.elem {
                            derivative
                                .ok_or_else(|| {
                                    ModelDescriptionError::VariableDerivativeMissing(
                                        der.name.clone(),
                                    )
                                })
                                .and_then(|der_idx| {
                                    self.model_variable_by_index(der_idx as usize)
                                        .map(|state| (state, der))
                                })
                        } else {
                            Err(ModelDescriptionError::VariableTypeMismatch(
                                ScalarVariableElementBase::Real,
                                ScalarVariableElementBase::from(&der.elem),
                            ))
                        }
                    })
            })
            .collect()
    }
}

impl FmiModelDescription for Fmi2ModelDescription {
    fn model_name(&self) -> &str {
        &self.model_name
    }

    fn version_string(&self) -> &str {
        &self.fmi_version
    }

    fn deserialize(xml: &str) -> Result<Self, crate::Error> {
        hard_xml::XmlRead::from_str(xml).map_err(crate::Error::XmlParse)
    }

    fn serialize(&self) -> Result<String, crate::Error> {
        hard_xml::XmlWrite::to_string(self).map_err(crate::Error::XmlParse)
    }
}

#[derive(Clone, Default, PartialEq, Debug, hard_xml::XmlRead, hard_xml::XmlWrite)]
#[xml(tag = "LogCategories", strict(unknown_attribute, unknown_element))]
pub struct LogCategories {
    #[xml(child = "Category")]
    pub categories: Vec<Category>,
}

#[derive(Clone, Default, PartialEq, Debug, hard_xml::XmlRead, hard_xml::XmlWrite)]
#[xml(tag = "Category", strict(unknown_attribute, unknown_element))]
pub struct Category {
    #[xml(attr = "name")]
    pub name: String,
    #[xml(attr = "description")]
    pub description: String,
}

#[derive(Clone, Default, PartialEq, Debug, hard_xml::XmlRead, hard_xml::XmlWrite)]
#[xml(tag = "DefaultExperiment")]
pub struct DefaultExperiment {
    /// Default start time of simulation
    #[xml(attr = "startTime")]
    pub start_time: Option<f64>,
    /// Default stop time of simulation
    #[xml(attr = "stopTime")]
    pub stop_time: Option<f64>,
    /// Default relative integration tolerance
    #[xml(attr = "tolerance")]
    pub tolerance: Option<f64>,
    /// ModelExchange: Default step size for fixed step integrators.
    /// CoSimulation: Preferred communicationStepSize.
    #[xml(attr = "stepSize")]
    pub step_size: Option<f64>,
}

impl DefaultExperiment {
    pub fn start_time(&self) -> f64 {
        self.start_time.unwrap_or(0.0)
    }

    pub fn stop_time(&self) -> f64 {
        self.stop_time.unwrap_or(10.0)
    }

    pub fn tolerance(&self) -> f64 {
        self.tolerance.unwrap_or(1e-3)
    }

    pub fn step_size(&self) -> Option<f64> {
        self.step_size
    }
}

#[derive(Default, Debug, hard_xml::XmlRead, hard_xml::XmlWrite)]
#[xml(tag = "UnitDefinitions", strict(unknown_attribute, unknown_element))]
pub struct UnitDefinitions {
    #[xml(child = "Unit")]
    pub units: Vec<Fmi2Unit>,
}

#[derive(Default, Debug, hard_xml::XmlRead, hard_xml::XmlWrite)]
#[xml(tag = "TypeDefinitions", strict(unknown_attribute, unknown_element))]
pub struct TypeDefinitions {
    #[xml(child = "SimpleType")]
    pub types: Vec<SimpleType>,
}

#[derive(Default, Debug, hard_xml::XmlRead, hard_xml::XmlWrite)]
#[xml(tag = "ModelVariables", strict(unknown_attribute, unknown_element))]
pub struct ModelVariables {
    #[xml(child = "ScalarVariable")]
    pub variables: Vec<ScalarVariable>,
}

#[derive(Default, PartialEq, Debug, hard_xml::XmlRead, hard_xml::XmlWrite)]
#[xml(tag = "ModelStructure", strict(unknown_attribute, unknown_element))]
pub struct ModelStructure {
    #[xml(child = "Outputs", default)]
    pub outputs: Outputs,

    #[xml(child = "Derivatives", default)]
    pub derivatives: Derivatives,

    #[xml(child = "InitialUnknowns", default)]
    pub initial_unknowns: InitialUnknowns,
}

#[derive(Default, PartialEq, Debug, hard_xml::XmlRead, hard_xml::XmlWrite)]
#[xml(tag = "Outputs")]
pub struct Outputs {
    #[xml(child = "Unknown")]
    pub unknowns: Vec<Fmi2VariableDependency>,
}

#[derive(Default, PartialEq, Debug, hard_xml::XmlRead, hard_xml::XmlWrite)]
#[xml(tag = "Derivatives")]
pub struct Derivatives {
    #[xml(child = "Unknown")]
    pub unknowns: Vec<Fmi2VariableDependency>,
}

#[derive(Default, PartialEq, Debug, hard_xml::XmlRead, hard_xml::XmlWrite)]
#[xml(tag = "InitialUnknowns")]
pub struct InitialUnknowns {
    #[xml(child = "Unknown")]
    pub unknowns: Vec<Fmi2VariableDependency>,
}

#[cfg(test)]
mod tests {
    use hard_xml::XmlRead;

    use super::*;

    #[test]
    fn test_model_description() {
        let s = r##"<?xml version="1.0" encoding="UTF8"?>
<fmiModelDescription
 fmiVersion="2.0"
 modelName="MyLibrary.SpringMassDamper"
 guid="{8c4e810f-3df3-4a00-8276-176fa3c9f9e0}"
 description="Rotational Spring Mass Damper System"
 version="1.0"
 generationDateAndTime="2011-09-23T16:57:33Z"
 variableNamingConvention="structured"
 numberOfEventIndicators="2">
 <ModelVariables>
    <ScalarVariable name="x[1]" valueReference="0" initial="exact"> <Real/> </ScalarVariable> <!-- idex="5" -->
    <ScalarVariable name="x[2]" valueReference="1" initial="exact"> <Real/> </ScalarVariable> <!-- index="6" -->
    <ScalarVariable name="PI.x" valueReference="46" description="State of block" causality="local" variability="continuous" initial="calculated">
        <Real relativeQuantity="false" />
    </ScalarVariable>
    <ScalarVariable name="der(PI.x)" valueReference="45" causality="local" variability="continuous" initial="calculated">
        <Real relativeQuantity="false" derivative="3" />
    </ScalarVariable>
 </ModelVariables>
 <ModelStructure>
    <Outputs><Unknown index="1" dependencies="1 2" /><Unknown index="2" /></Outputs>
    <Derivatives><Unknown index="4" dependencies="1 2" /></Derivatives>
    <InitialUnknowns />
</ModelStructure>
</fmiModelDescription>"##;
        let md = Fmi2ModelDescription::from_str(&s).unwrap();
        assert_eq!(md.fmi_version, "2.0");
        assert_eq!(md.model_name, "MyLibrary.SpringMassDamper");
        assert_eq!(md.guid, "{8c4e810f-3df3-4a00-8276-176fa3c9f9e0}");
        assert_eq!(
            md.description.as_deref(),
            Some("Rotational Spring Mass Damper System")
        );
        assert_eq!(md.version.as_deref(), Some("1.0"));
        // assert_eq!(x.generation_date_and_time, chrono::DateTime<chrono::Utc>::from)
        assert_eq!(md.variable_naming_convention, Some("structured".to_owned()));
        assert_eq!(md.number_of_event_indicators, Some(2));
        assert_eq!(md.model_variables.variables.len(), 4);

        let outputs = &md.model_structure.outputs.unknowns;
        assert_eq!(outputs.len(), 2);
        assert_eq!(outputs[0].index, 1);
        assert_eq!(outputs[0].dependencies, vec![1, 2]);
        assert_eq!(outputs[1].index, 2);
        assert!(outputs[1].dependencies.is_empty());

        let derivatives = &md.model_structure.derivatives.unknowns;
        assert_eq!(derivatives.len(), 1);
        assert_eq!(derivatives[0].index, 4);
        assert_eq!(derivatives[0].dependencies, vec![1, 2]);
    }
}