fmi_export/fmi3/traits/
mod.rs1use std::{fmt::Display, path::PathBuf, str::FromStr};
2
3use fmi::{
4 EventFlags,
5 fmi3::{Fmi3Error, Fmi3Res, Fmi3Status, binding},
6 schema::fmi3::AppendToModelVariables,
7};
8
9use crate::fmi3::ModelState;
10
11mod model_get_set;
12mod wrappers;
13
14pub use model_get_set::{ModelGetSet, ModelGetSetStates};
15pub use wrappers::{Fmi3CoSimulation, Fmi3Common, Fmi3ModelExchange, Fmi3ScheduledExecution};
16
17pub trait Context<M: UserModel> {
19 fn logging_on(&self, category: M::LoggingCategory) -> bool;
21
22 fn set_logging(&mut self, category: M::LoggingCategory, enabled: bool);
24
25 fn log(&self, status: Fmi3Status, category: M::LoggingCategory, args: std::fmt::Arguments<'_>);
27
28 fn resource_path(&self) -> &PathBuf;
30
31 fn initialize(&mut self, start_time: f64, stop_time: Option<f64>);
32
33 fn time(&self) -> f64;
35
36 fn set_time(&mut self, time: f64);
38
39 fn stop_time(&self) -> Option<f64>;
41
42 fn early_return_allowed(&self) -> bool {
44 false
45 }
46
47 fn as_any_mut(&mut self) -> &mut dyn std::any::Any;
48}
49
50pub trait Model: Default {
55 const MODEL_NAME: &'static str;
56 const INSTANTIATION_TOKEN: &'static str;
57
58 const MAX_EVENT_INDICATORS: usize;
60
61 const SUPPORTS_MODEL_EXCHANGE: bool;
63
64 const SUPPORTS_CO_SIMULATION: bool;
66
67 const SUPPORTS_SCHEDULED_EXECUTION: bool;
69
70 fn build_metadata(
75 variables: &mut fmi::schema::fmi3::ModelVariables,
76 model_structure: &mut fmi::schema::fmi3::ModelStructure,
77 vr_offset: u32,
78 prefix: Option<&str>,
79 ) -> u32;
80
81 fn build_toplevel_metadata() -> (
83 fmi::schema::fmi3::ModelVariables,
84 fmi::schema::fmi3::ModelStructure,
85 ) {
86 let mut variables = fmi::schema::fmi3::ModelVariables::default();
87 let time = fmi::schema::fmi3::FmiFloat64::new(
88 "time".to_string(),
89 0,
90 None,
91 fmi::schema::fmi3::Causality::Independent,
92 fmi::schema::fmi3::Variability::Continuous,
93 None,
94 None,
95 );
96 AppendToModelVariables::append_to_variables(time, &mut variables);
97 let mut structure = fmi::schema::fmi3::ModelStructure::default();
98 let _num_vars = Self::build_metadata(&mut variables, &mut structure, 1, None);
99 (variables, structure)
100 }
101
102 fn set_start_values(&mut self);
104
105 fn validate_variable_setting(
109 vr: binding::fmi3ValueReference,
110 state: &ModelState,
111 ) -> Result<(), &'static str> {
112 let _ = (vr, state);
115 Ok(())
116 }
117}
118
119pub trait ModelLoggingCategory: Display + FromStr + Ord + Copy + Default {
120 fn all_categories() -> impl Iterator<Item = Self>;
122 fn trace_category() -> Self;
124 fn error_category() -> Self;
126}
127
128#[derive(Debug, Clone, Copy, Default)]
130pub struct CSDoStepResult {
131 pub event_handling_needed: bool,
132 pub terminate_simulation: bool,
133 pub early_return: bool,
134 pub last_successful_time: f64,
135}
136
137impl CSDoStepResult {
138 pub fn completed(last_successful_time: f64) -> Self {
139 Self {
140 event_handling_needed: false,
141 terminate_simulation: false,
142 early_return: false,
143 last_successful_time,
144 }
145 }
146}
147
148pub trait UserModel: Sized {
152 type LoggingCategory: ModelLoggingCategory + 'static;
156
157 fn configurate(&mut self, _context: &dyn Context<Self>) -> Result<(), Fmi3Error> {
160 Ok(())
161 }
162
163 fn calculate_values(&mut self, _context: &dyn Context<Self>) -> Result<Fmi3Res, Fmi3Error> {
166 Ok(Fmi3Res::OK)
167 }
168
169 fn event_update(
178 &mut self,
179 _context: &dyn Context<Self>,
180 event_flags: &mut EventFlags,
181 ) -> Result<Fmi3Res, Fmi3Error> {
182 event_flags.reset();
183 Ok(Fmi3Res::OK)
184 }
185
186 fn get_event_indicators(
195 &mut self,
196 _context: &dyn Context<Self>,
197 indicators: &mut [f64],
198 ) -> Result<bool, Fmi3Error> {
199 for indicator in indicators.iter_mut() {
201 *indicator = 0.0;
202 }
203 Ok(true)
204 }
205
206 fn do_step(
210 &mut self,
211 context: &mut dyn Context<Self>,
212 current_communication_point: f64,
213 communication_step_size: f64,
214 _no_set_fmu_state_prior_to_current_point: bool,
215 ) -> Result<CSDoStepResult, Fmi3Error> {
216 let target_time = current_communication_point + communication_step_size;
217 context.set_time(target_time);
218 Ok(CSDoStepResult::completed(target_time))
219 }
220}