Skip to main content

fmi_schema/
traits.rs

1//! Common traits for FMI schema
2
3use crate::MajorVersion;
4
5pub trait DefaultExperiment {
6    fn start_time(&self) -> Option<f64>;
7    fn stop_time(&self) -> Option<f64>;
8    fn tolerance(&self) -> Option<f64>;
9    fn step_size(&self) -> Option<f64>;
10}
11
12/// A trait common between all FMI schema versions
13pub trait FmiModelDescription: Sized {
14    /// Returns the model name
15    fn model_name(&self) -> &str;
16
17    /// Returns the FMI version as a string
18    fn version_string(&self) -> &str;
19
20    /// Returns the parsed FMI version as a semver::Version
21    fn version(&self) -> Result<semver::Version, crate::Error> {
22        lenient_semver::parse(self.version_string()).map_err(|e| e.owned().into())
23    }
24
25    /// Returns the parsed FMI version as a MajorVersion
26    fn major_version(&self) -> Result<MajorVersion, crate::Error> {
27        match self.version()? {
28            v if v.major == 1 => Ok(MajorVersion::FMI1),
29            v if v.major == 2 => Ok(MajorVersion::FMI2),
30            v if v.major == 3 => Ok(MajorVersion::FMI3),
31            v => panic!("Invalid version {}", v.major),
32        }
33    }
34
35    /// Deserialize the model description from XML
36    fn deserialize(xml: &str) -> Result<Self, crate::Error>;
37
38    /// Serialize the model description to XML
39    fn serialize(&self) -> Result<String, crate::Error>;
40}
41
42/// A trait for FMI interface types (Model Exchange, Co-Simulation, Scheduled Execution) and versions
43pub trait FmiInterfaceType: Sized {
44    /// Returns the model identifier
45    fn model_identifier(&self) -> &str;
46    /// Returns true if the FMU needs an execution tool
47    fn needs_execution_tool(&self) -> Option<bool>;
48    /// Returns true if the FMU can be instantiated only once per process
49    fn can_be_instantiated_only_once_per_process(&self) -> Option<bool>;
50    /// Returns true if the FMU can get and set FMU state
51    fn can_get_and_set_fmu_state(&self) -> Option<bool>;
52    /// Returns true if the FMU can serialize FMU state
53    fn can_serialize_fmu_state(&self) -> Option<bool>;
54    /// Returns true if the FMU provides directional derivatives
55    fn provides_directional_derivatives(&self) -> Option<bool>;
56    /// Returns true if the FMU provides adjoint derivatives
57    /// (only FMI 3.0)
58    fn provides_adjoint_derivatives(&self) -> Option<bool>;
59    /// Returns true if the FMU provides per element dependencies
60    /// (only FMI 3.0)
61    fn provides_per_element_dependencies(&self) -> Option<bool>;
62}