fmi_sim/sim/
params.rs

1use fmi::schema::traits::DefaultExperiment;
2
3use crate::options::CommonOptions;
4
5#[derive(Debug, Clone)]
6pub struct SimParams {
7    /// Start time of the simulation
8    pub start_time: f64,
9    /// Stop time of the simulation
10    pub stop_time: f64,
11    /// Output interval
12    pub output_interval: f64,
13    /// Tolerance
14    pub tolerance: Option<f64>,
15    /// Use event mode
16    pub event_mode_used: bool,
17    /// Support early-return in Co-Simulation.
18    pub early_return_allowed: bool,
19}
20
21impl SimParams {
22    /// Create a new `SimParams` from the given `SimOptions` and `DefaultExperiment`.
23    ///
24    /// Values from `SimOptions` take precedence over values from `DefaultExperiment`.
25    pub fn new_from_options<DE>(
26        options: &CommonOptions,
27        default_experiment: &DE,
28        event_mode_used: bool,
29        early_return_allowed: bool,
30    ) -> Self
31    where
32        DE: DefaultExperiment,
33    {
34        let start_time = options
35            .start_time
36            .or(default_experiment.start_time())
37            .unwrap_or(0.0);
38
39        let stop_time = options
40            .stop_time
41            .or(default_experiment.stop_time())
42            .unwrap_or(1.0);
43
44        let output_interval = options
45            .output_interval
46            .or(default_experiment.step_size())
47            .unwrap_or_else(|| (stop_time - start_time) / 500.0);
48
49        if output_interval <= 0.0 {
50            panic!("`output_interval` must be positive.");
51        }
52
53        let tolerance = options.tolerance.or(default_experiment.tolerance());
54
55        Self {
56            start_time,
57            stop_time,
58            output_interval,
59            tolerance,
60            event_mode_used,
61            early_return_allowed,
62        }
63    }
64}