Skip to main content

fmi_export/fmi3/
mod.rs

1//! ## Architecture
2//!
3//! The [`crate::export_fmu`] macro generates the necessary C-API bindings for the exported FMU.
4//! Many of these bindings operate on a [`::fmi::fmi3::binding::fmi3Instance`], which is an opaque
5//! pointer to an instance of [`instance::ModelInstance`].
6//!
7//! [`instance::ModelInstance`] implements the [`crate::fmi3::Fmi3Common`] trait, which provides
8//! the actual implementation of the FMI 3.0 API. All user-model-specific functions are delegated
9//! to the [`Model`] trait,
10//! which the user model must implement.
11
12// Disable coverage for the export module. Due to some bug in llvm-cov, the generated C functions
13// are never covered. This is ok since they are just thin wrappers around the Rust functions.
14#[cfg_attr(coverage_nightly, coverage(off))]
15mod export;
16
17mod instance;
18mod traits;
19mod types;
20mod variable_builder;
21
22use std::{fmt::Display, str::FromStr};
23
24// Re-exports
25pub use instance::{ModelInstance, context::BasicContext};
26pub use traits::{
27    CSDoStepResult, Context, Fmi3CoSimulation, Fmi3Common, Fmi3ModelExchange,
28    Fmi3ScheduledExecution, Model, ModelGetSet, ModelGetSetStates, ModelLoggingCategory,
29    ModelMetadata, TerminalProvider, UserModel,
30};
31pub use types::{Binary, Clock, InitializeFromStart};
32pub use variable_builder::{FmiVariableBuilder, VariableBuilder};
33
34/// Specifies how Co-Simulation is implemented for a model
35/// Represents the current state of the model instance
36#[derive(Debug)]
37pub enum ModelState {
38    StartAndEnd,
39    ConfigurationMode,
40    /// In the state `Instantiated` the FMU can do one-time initializations and allocate memory.
41    ///
42    /// See <https://fmi-standard.org/docs/3.0.1/#Instantiated>
43    Instantiated,
44    /// The `InitializationMode` is used by the simulation algorithm to compute consistent initial
45    /// conditions for the overall system. Equations are active to determine the initial FMU state,
46    /// as well as all outputs (and optionally other variables exposed by the exporting tool).
47    /// Artificial or real algebraic loops over connected FMUs in Initialization Mode may be handled
48    /// by using appropriate numerical algorithms.
49    ///
50    /// See <https://fmi-standard.org/docs/3.0.1/#InitializationMode>
51    InitializationMode,
52    /// In `EventMode` all continuous-time, discrete-time equations and active model partitions are
53    /// evaluated. Algebraic loops active during Event Mode are solved by event iteration.
54    ///
55    /// See <https://fmi-standard.org/docs/3.0.1/#EventMode>
56    EventMode,
57    ContinuousTimeMode,
58    StepMode,
59    ClockActivationMode,
60    StepDiscarded,
61    ReconfigurationMode,
62    IntermediateUpdateMode,
63    Terminated,
64}
65
66/// Simple default logging category for models that don't need custom logging
67#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
68pub enum DefaultLoggingCategory {
69    #[default]
70    LogAll,
71    /// Trace FMI API calls
72    Trace,
73}
74
75impl Display for DefaultLoggingCategory {
76    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77        match self {
78            DefaultLoggingCategory::LogAll => f.write_str("logAll"),
79            DefaultLoggingCategory::Trace => f.write_str("trace"),
80        }
81    }
82}
83
84impl FromStr for DefaultLoggingCategory {
85    type Err = String;
86    fn from_str(s: &str) -> Result<Self, Self::Err> {
87        match s {
88            "logAll" => Ok(Self::LogAll),
89            "trace" => Ok(Self::Trace),
90            _ => Err(format!("Unknown logging category: {}", s)),
91        }
92    }
93}
94
95impl ModelLoggingCategory for DefaultLoggingCategory {
96    fn all_categories() -> impl Iterator<Item = Self> {
97        [Self::LogAll, Self::Trace].into_iter()
98    }
99    fn trace_category() -> Self {
100        Self::Trace
101    }
102    fn error_category() -> Self {
103        Self::LogAll
104    }
105}