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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
/// This module implements the ModelDescription datamodel and provides
/// attributes to serde_xml_rs to generate an XML deserializer.
use derive_more::Display;
use failure::{format_err, Error, ResultExt};
use serde::{de, Deserialize, Deserializer};
use std::str::FromStr;

// Re-exports
pub use serde_xml_rs::from_reader;

/// Generic parsing function
fn t_from_str<'de, T, D>(deser: D) -> Result<T, D::Error>
where
    D: Deserializer<'de>,
    T: FromStr,
    <T as std::str::FromStr>::Err: std::fmt::Display,
{
    let s = <String>::deserialize(deser)?;
    T::from_str(&s).map_err(de::Error::custom)
}

/// Parse a string list into a Vec<T>
fn vec_from_str<'de, T, D>(deser: D) -> Result<Vec<T>, D::Error>
where
    D: Deserializer<'de>,
    T: FromStr,
    <T as std::str::FromStr>::Err: std::fmt::Display,
{
    let s = <String>::deserialize(deser)?;
    if s.len() == 0 {
        return Ok(Vec::<T>::new());
    }
    s.split(" ")
        .map(|i| T::from_str(&i).map_err(de::Error::custom))
        .collect()
}

fn dtparse_from_str<'de, D>(deser: D) -> Result<chrono::DateTime<chrono::Utc>, D::Error>
where
    D: Deserializer<'de>,
{
    use chrono::{DateTime, Utc};

    let s = <String>::deserialize(deser)?;
    dtparse::parse(&s)
        .map_err(|e| de::Error::custom(format!("{:?}", e)))
        .and_then(|dt| Ok(DateTime::<Utc>::from_utc(dt.0, Utc)))
    //( chrono::naive::NaiveDateTime, Option<chrono::offset::FixedOffset>,)
}

/* fmiModelDescription */
#[derive(Debug, Deserialize)]
#[serde(rename = "fmiModelDescription", rename_all = "camelCase")]
pub struct ModelDescription {
    pub fmi_version: String,
    pub model_name: String,
    pub guid: String,

    #[serde(default)]
    pub description: String,

    #[serde(default)]
    pub version: String,

    /*
    #[serde(with = "parse_util::odr_dateformat", default = "Header::default_date")]
    */
    /// time/date of database creation according to ISO 8601 (preference: YYYY-MM-DDThh:mm:ss)
    #[serde(deserialize_with = "dtparse_from_str")]
    pub generation_date_and_time: chrono::DateTime<chrono::Utc>,

    #[serde(default)]
    pub generation_tool: String,

    pub variable_naming_convention: String,

    #[serde(deserialize_with = "t_from_str")]
    pub number_of_event_indicators: u32,

    #[serde(rename = "ModelExchange")]
    pub model_exchange: Option<ModelExchange>,

    #[serde(rename = "CoSimulation")]
    pub co_simulation: Option<CoSimulation>,

    #[serde(rename = "LogCategories")]
    pub log_categories: Option<LogCategories>,

    #[serde(rename = "DefaultExperiment")]
    pub default_experiment: Option<DefaultExperiment>,

    #[serde(rename = "UnitDefinitions")]
    pub unit_definitions: Option<UnitDefinitions>,

    #[serde(rename = "TypeDefinitions")]
    pub type_definitions: Option<TypeDefinitions>,

    #[serde(rename = "ModelVariables")]
    model_variables: ModelVariables,

    #[serde(rename = "ModelStructure")]
    model_structure: ModelStructure,
}

pub type ScalarVariableMap<'a> = std::collections::HashMap<String, &'a ScalarVariable>;
pub type UnknownsTuple<'a> = (&'a ScalarVariable, Vec<&'a ScalarVariable>);

#[derive(Debug, Default)]
pub struct Counts {
    pub num_constants: usize,
    pub num_parameters: usize,
    pub num_discrete: usize,
    pub num_continuous: usize,
    pub num_inputs: usize,
    pub num_outputs: usize,
    pub num_local: usize,
    pub num_independent: usize,
    pub num_calculated_parameters: usize,
    pub num_real_vars: usize,
    pub num_integer_vars: usize,
    pub num_enum_vars: usize,
    pub num_bool_vars: usize,
    pub num_string_vars: usize,
}

impl ModelDescription {
    /// The model name
    pub fn model_name(&self) -> &str {
        &self.model_name
    }

    /*
    pub fn model_identifier(&self) -> &str {
        &self.model_exchange
    }
    */

    /// Collect counts of variables in the model
    pub fn model_counts(&self) -> Counts {
        self.model_variables
            .variables
            .iter()
            .fold(Counts::default(), |mut cts, ref sv| {
                match sv.variability {
                    Variability::Constant => {
                        cts.num_constants += 1;
                    }
                    Variability::Continuous => {
                        cts.num_continuous += 1;
                    }
                    Variability::Discrete => {
                        cts.num_discrete += 1;
                    }
                    _ => {}
                }
                match sv.causality {
                    Causality::CalculatedParameter => {
                        cts.num_calculated_parameters += 1;
                    }
                    Causality::Parameter => {
                        cts.num_parameters += 1;
                    }
                    Causality::Input => {
                        cts.num_inputs += 1;
                    }
                    Causality::Output => {
                        cts.num_outputs += 1;
                    }
                    Causality::Local => {
                        cts.num_local += 1;
                    }
                    Causality::Independent => {
                        cts.num_independent += 1;
                    }
                    _ => {}
                }
                match sv.elem {
                    ScalarVariableElement::Real { .. } => {
                        cts.num_real_vars += 1;
                    }
                    ScalarVariableElement::Integer { .. } => {
                        cts.num_integer_vars += 1;
                    }
                    ScalarVariableElement::Enumeration { .. } => {
                        cts.num_enum_vars += 1;
                    }
                    ScalarVariableElement::Boolean { .. } => {
                        cts.num_bool_vars += 1;
                    }
                    ScalarVariableElement::String { .. } => {
                        cts.num_string_vars += 1;
                    }
                }
                cts
            })
    }

    /// 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 as usize
    }

    /// Get a reference to the Map of SalarVariables
    pub fn model_variables(&self) -> impl Iterator<Item = (&str, &ScalarVariable)> {
        self.model_variables
            .variables
            .iter()
            .map(|row| (row.name.as_ref(), row))
    }

    /// Turns an UnknownList into a nested Vector of ScalarVariables and their Dependencies
    fn map_unknowns(&self, list: &UnknownList) -> Result<Vec<UnknownsTuple>, Error> {
        list.unknowns
            .iter()
            .map(|unknown| {
                self.model_variables
                    .variables
                    // Variable indices start at 1 in the modelDescription
                    .get(unknown.index as usize - 1)
                    .ok_or(format_err!("Variable not found"))
                    .context(format!("{} var index {}", self.model_name, unknown.index))
                    .map_err(|e| -> Error { e.into() })
                    .and_then(|var| {
                        let deps = unknown
                            .dependencies
                            .iter()
                            .map(|dep| {
                                self.model_variables
                                    .variables
                                    .get(*dep as usize - 1)
                                    .ok_or(format_err!("Dependency not found"))
                                    .context(format!(
                                        "{} var index {}",
                                        self.model_name, *dep as usize
                                    ))
                                    .map_err(|e| -> Error { e.into() })
                            })
                            .collect::<Result<Vec<_>, Error>>()?;

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

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

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

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

    fn model_variable_by_index(&self, idx: usize) -> Result<&ScalarVariable, Error> {
        self.model_variables
            .variables
            .get(idx - 1)
            .ok_or(format_err!("Variable not found"))
    }

    /// Return a vector of tuples `(&ScalarVariable, &ScalarVariabel)`, where the 1st is a
    /// continuous-time state, and the 2nd is its derivative.
    pub fn continuous_states(&self) -> Result<Vec<(&ScalarVariable, &ScalarVariable)>, Error> {
        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(format_err!("Derivative index missing"))
                                .and_then(|der_idx| {
                                    self.model_variable_by_index(der_idx as usize)
                                        .map(|state| (state, der))
                                })
                        } else {
                            Err(format_err!("Referenced state variable not a Real"))
                        }
                    })
            })
            .collect()
    }
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ModelExchange {
    // Short class name according to C-syntax
    pub model_identifier: String,

    #[serde(default)]
    // If true, a tool is needed to execute the model and the FMU just contains the communication to this tool.
    pub needs_execution_tool: bool,

    #[serde(default)]
    pub completed_integrator_step_not_needed: bool,

    #[serde(default)]
    pub can_be_instantiated_only_once_per_process: bool,

    #[serde(default)]
    pub can_not_use_memory_management_functions: bool,

    #[serde(default, rename = "canGetAndSetFMUState")]
    pub can_get_and_set_fmu_state: bool,

    #[serde(default, rename = "canSerializeFMUState")]
    pub can_serialize_fmu_state: bool,

    #[serde(default)]
    // If true, the directional derivative of the equations can be computed with fmi2GetDirectionalDerivative
    pub provides_directional_derivative: bool,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CoSimulation {
    // Short class name according to C-syntax
    pub model_identifier: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LogCategories {
    #[serde(default, rename = "$value")]
    pub categories: Vec<Category>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Category {
    pub name: String,
    pub description: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Unit {
    pub name: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UnitDefinitions {
    #[serde(default, rename = "$value")]
    pub units: Vec<Unit>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SimpleType {}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TypeDefinitions {
    #[serde(default, rename = "$value")]
    pub types: Vec<SimpleType>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DefaultExperiment {
    #[serde(default, deserialize_with = "t_from_str")]
    pub start_time: f64,

    #[serde(default = "default_stop_time", deserialize_with = "t_from_str")]
    pub stop_time: f64,

    #[serde(default = "default_tolerance", deserialize_with = "t_from_str")]
    pub tolerance: f64,
}

fn default_stop_time() -> f64 {
    10.0
}
fn default_tolerance() -> f64 {
    1e-3
}

#[derive(Debug, Display, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub enum Causality {
    Parameter,
    CalculatedParameter,
    Input,
    Output,
    Local,
    Independent,
    Unknown,
}

impl Default for Causality {
    fn default() -> Causality {
        Causality::Unknown
    }
}

/// Enumeration that defines the time dependency of the variable
#[derive(Debug, Display, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub enum Variability {
    Constant,
    Fixed,
    Tunable,
    Discrete,
    Continuous,
    Unknown,
}

impl Default for Variability {
    fn default() -> Variability {
        Variability::Unknown
    }
}

#[derive(Debug, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub enum Initial {
    Exact,
    Approx,
    Calculated,
}

impl Default for Initial {
    fn default() -> Initial {
        Initial::Exact
    }
}

#[derive(Debug, Deserialize, Display, Clone)]
#[serde(rename_all = "camelCase")]
#[display(
    fmt = "ScalarVariable {} {{{}, {}, {}}}",
    elem,
    name,
    causality,
    variability
)]
pub struct ScalarVariable {
    /// The full, unique name of the variable.
    pub name: String,

    /// A handle of the variable to efficiently identify the variable value in the model interface.
    #[serde(deserialize_with = "t_from_str")]
    pub value_reference: u64,

    /// An optional description string describing the meaning of the variable.
    #[serde(default)]
    pub description: String,

    /// Enumeration that defines the causality of the variable.
    #[serde(default)]
    pub causality: Causality,

    /// Enumeration that defines the time dependency of the variable, in other words it defines the
    /// time instants when a variable can change its value.
    #[serde(default)]
    pub variability: Variability,

    /// Enumeration that defines how the variable is initialized. It is not allowed to provide a
    /// value for initial if `causality`=`Input` or `Independent`.
    #[serde(default)]
    pub initial: Initial,

    #[serde(rename = "$value")]
    pub elem: ScalarVariableElement,
}

impl PartialEq for ScalarVariable {
    fn eq(&self, other: &ScalarVariable) -> bool {
        self.value_reference == other.value_reference
    }
}

impl Eq for ScalarVariable {}

impl std::hash::Hash for ScalarVariable {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.value_reference.hash(state);
    }
}

impl ScalarVariable {
    pub fn is_continuous_input(&self) -> bool {
        match (&self.elem, &self.causality) {
            (ScalarVariableElement::Real { .. }, Causality::Input) => true,
            _ => false
        }
    }
}

#[derive(Debug, Deserialize, Display, PartialEq, Clone)]
pub enum ScalarVariableElement {
    #[serde(rename_all = "camelCase")]
    #[display(fmt = "Real({:?},{})", declared_type, start)]
    Real {
        declared_type: Option<String>,

        #[serde(default, deserialize_with = "t_from_str")]
        start: f64,

        #[serde(default, deserialize_with = "t_from_str")]
        relative_quantity: bool,

        //#[serde(default, deserialize_with = "deser_opt")]
        #[serde(default)]
        derivative: Option<u32>,
    },
    #[serde(rename_all = "camelCase")]
    #[display(fmt = "Int({},{})", declared_type, start)]
    Integer {
        #[serde(default)]
        declared_type: String,
        #[serde(default, deserialize_with = "t_from_str")]
        start: i64,
    },
    #[serde(rename_all = "camelCase")]
    #[display(fmt = "Bool({},{})", declared_type, start)]
    Boolean {
        #[serde(default)]
        declared_type: String,
        #[serde(default, deserialize_with = "t_from_str")]
        start: bool,
    },
    #[serde(rename_all = "camelCase")]
    #[display(fmt = "String({},{})", declared_type, start)]
    String {
        #[serde(default)]
        declared_type: String,
        start: String,
    },
    #[serde(rename_all = "camelCase")]
    #[display(fmt = "Enum({},{})", declared_type, start)]
    Enumeration {
        #[serde(default)]
        declared_type: String,
        #[serde(default, deserialize_with = "t_from_str")]
        start: u32,
    },
}

#[derive(Debug, Deserialize)]
pub struct ModelVariables {
    #[serde(default, rename = "$value")]
    variables: Vec<ScalarVariable>,
}

#[derive(Debug, Deserialize)]
#[serde(rename = "Unknown")]
pub struct Unknown {
    #[serde(deserialize_with = "t_from_str")]
    pub index: u32,
    #[serde(default, deserialize_with = "vec_from_str")]
    pub dependencies: Vec<u32>,
}

#[derive(Debug, Deserialize)]
pub struct UnknownList {
    #[serde(default, rename = "$value")]
    pub unknowns: Vec<Unknown>,
}
impl Default for UnknownList {
    fn default() -> UnknownList {
        UnknownList {
            unknowns: Vec::<Unknown>::new(),
        }
    }
}

#[derive(Debug, Deserialize)]
#[serde(rename = "ModelStructure", rename_all = "PascalCase")]
struct ModelStructure {
    #[serde(default)]
    pub outputs: UnknownList,

    #[serde(default)]
    pub derivatives: UnknownList,

    #[serde(default)]
    pub initial_unknowns: UnknownList,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_model_exchange() {
        let s = r##"<ModelExchange modelIdentifier="MyLibrary_SpringMassDamper"/>"##;
        let x: ModelExchange = serde_xml_rs::from_reader(s.as_bytes()).unwrap();
        assert!(x.model_identifier == "MyLibrary_SpringMassDamper");
    }

    #[test]
    fn test_default_experiment() {
        let s = r##"<DefaultExperiment stopTime="3.0" tolerance="0.0001"/>"##;
        let x: DefaultExperiment = serde_xml_rs::from_reader(s.as_bytes()).unwrap();
        assert_eq!(x.start_time, 0.0);
        assert_eq!(x.stop_time, 3.0);
        assert_eq!(x.tolerance, 0.0001);

        let s = r#"<DefaultExperiment startTime = "0.10000000000000000e+00" stopTime  = "1.50000000000000000e+00" tolerance = "0.0001"/>"#;
        let x: DefaultExperiment = serde_xml_rs::from_reader(s.as_bytes()).unwrap();
        assert_eq!(x.start_time, 0.1);
        assert_eq!(x.stop_time, 1.5);
        assert_eq!(x.tolerance, 0.0001);
    }

    #[test]
    fn test_scalar_variable() {
        let s =r##"
<ScalarVariable name="inertia1.J" valueReference="1073741824" description="Moment of load inertia" causality="parameter" variability="fixed">
<Real declaredType="Modelica.SIunits.Inertia" start="1"/>
</ScalarVariable>
        "##;
        let x: ScalarVariable = serde_xml_rs::from_reader(s.as_bytes()).unwrap();
        assert_eq!(x.name, "inertia1.J");
        assert_eq!(x.value_reference, 1073741824);
        assert_eq!(x.description, "Moment of load inertia");
        assert_eq!(x.causality, Causality::Parameter);
        assert_eq!(x.variability, Variability::Fixed);
        assert_eq!(
            x.elem,
            ScalarVariableElement::Real {
                declared_type: Some("Modelica.SIunits.Inertia".to_string()),
                start: 1.0,
                relative_quantity: false,
                derivative: None
            }
        );
    }

    #[test]
    fn test_model_variables() {
        let s = r##"
            <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="der(x[1])" valueReference="2"> <Real derivative="5"/> </ScalarVariable> <!-- index="7" -->
                <ScalarVariable name="der(x[2])" valueReference="3"> <Real derivative="6"/> </ScalarVariable> <!-- index="8" -->
            </ModelVariables>
        "##;
        let x: ModelVariables = serde_xml_rs::from_reader(s.as_bytes()).unwrap();
        assert_eq!(x.variables.len(), 4);
        assert_eq!(
            x.variables
                .iter()
                .map(|v| &v.name)
                .collect::<Vec<_>>()
                .sort(),
            vec!["x[1]", "x[2]", "der(x[1])", "der(x[2])"].sort()
        );
    }

    #[test]
    fn test_model_structure() {
        let s = r##"
            <ModelStructure>
                <Outputs> <Unknown index="3" /> <Unknown index="4" /> </Outputs>
                <Derivatives> <Unknown index="7" /> <Unknown index="8" /> </Derivatives>
                <InitialUnknowns> <Unknown index="3" /> <Unknown index="4" /> <Unknown index="7" dependencies="5 2" /> <Unknown index="8" dependencies="5 6" /> </InitialUnknowns>
            </ModelStructure>
        "##;
        let x: ModelStructure = serde_xml_rs::from_reader(s.as_bytes()).unwrap();
        assert_eq!(x.outputs.unknowns[0].index, 3);
        assert_eq!(x.outputs.unknowns[1].index, 4);
        assert_eq!(x.derivatives.unknowns[0].index, 7);
        assert_eq!(x.derivatives.unknowns[1].index, 8);
        assert_eq!(x.initial_unknowns.unknowns[0].index, 3);
        assert_eq!(x.initial_unknowns.unknowns[1].index, 4);
        assert_eq!(x.initial_unknowns.unknowns[2].index, 7);
        assert_eq!(x.initial_unknowns.unknowns[2].dependencies, vec! {5, 2});
        assert_eq!(x.initial_unknowns.unknowns[3].dependencies, vec! {5, 6});
    }

    #[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 x: ModelDescription = serde_xml_rs::from_str(s).expect("hello");
        assert_eq!(x.fmi_version, "2.0");
        assert_eq!(x.model_name, "MyLibrary.SpringMassDamper");
        assert_eq!(x.guid, "{8c4e810f-3df3-4a00-8276-176fa3c9f9e0}");
        assert_eq!(x.description, "Rotational Spring Mass Damper System");
        assert_eq!(x.version, "1.0");
        //assert_eq!(x.generation_date_and_time, chrono::DateTime<chrono::Utc>::from)
        assert_eq!(x.variable_naming_convention, "structured");
        assert_eq!(x.number_of_event_indicators, 2);
        assert_eq!(x.model_variables.variables.len(), 4);

        let outputs = x.outputs().unwrap();
        assert_eq!(outputs[0].0.name, "x[1]");
        assert_eq!(outputs[0].1.len(), 2);
        assert_eq!(outputs[0].1[0].name, "x[1]");
        assert_eq!(outputs[1].0.name, "x[2]");
        assert_eq!(outputs[1].1.len(), 0);

        let derivatives = x.derivatives().unwrap();
        assert_eq!(derivatives[0].0.name, "der(PI.x)");
        assert_eq!(
            derivatives[0].0.elem,
            ScalarVariableElement::Real {
                declared_type: None,
                start: 0.0,
                relative_quantity: false,
                derivative: Some(3)
            }
        );

        let states = x.continuous_states().unwrap();
        assert_eq!(
            states
                .iter()
                .map(|(der, state)| (der.name.as_str(), state.name.as_str()))
                .collect::<Vec<(_, _)>>(),
            vec![("PI.x", "der(PI.x)")]
        );
    }

    /*
    #[test]
    fn test_file() {
        let file = std::fs::File::open("modelDescription.xml").unwrap();
        let file = std::io::BufReader::new(file);
        let x: ModelDescription = serde_xml_rs::deserialize(file).unwrap();
        println!("{:#?}", x);
    }
    */

}