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
use crate::{
modeling::{Atomic, Component, Coupled},
DynRef,
};
#[cfg(feature = "par_any")]
use rayon::prelude::*;
use std::ops::{Deref, DerefMut};
#[cfg(feature = "rt")]
pub mod rt;
/// Interface for simulating DEVS models. All DEVS models must implement this trait.
pub trait Simulator: DynRef {
/// Returns reference to inner [`Component`].
fn get_component(&self) -> &Component;
/// Returns mutable reference to inner [`Component`].
fn get_component_mut(&mut self) -> &mut Component;
/// Returns the name of the inner DEVS [`Component`].
#[inline]
fn get_name(&self) -> &str {
self.get_component().get_name()
}
/// Returns the time for the last state transition of the inner DEVS [`Component`].
#[inline]
fn get_t_last(&self) -> f64 {
self.get_component().get_t_last()
}
/// Returns the time for the next state transition of the inner DEVS [`Component`].
#[inline]
fn get_t_next(&self) -> f64 {
self.get_component().get_t_next()
}
/// Sets the tine for the last and next state transitions of the inner DEVS [`Component`].
#[inline]
fn set_sim_t(&mut self, t_last: f64, t_next: f64) {
self.get_component_mut().set_sim_t(t_last, t_next);
}
/// Clears input messages from the inner DEVS [`Component`]s.
#[inline]
fn clear_input(&mut self) {
// Safety: simulator clearing its input
unsafe { self.get_component_mut().clear_input() };
}
/// Clears output messages from the inner DEVS [`Component`]s.
#[inline]
fn clear_output(&mut self) {
// Safety: simulator clearing its output
unsafe { self.get_component_mut().clear_output() };
}
/// Removes all the messages from all the ports.
#[inline]
fn clear(&mut self) {
let component = self.get_component_mut();
// Safety: simulator clearing its ports
unsafe {
component.clear_input();
component.clear_output();
}
}
/// It starts the simulation, setting the initial time to t_start.
fn start(&mut self, t_start: f64) -> f64;
/// It stops the simulation, setting the last time to t_stop.
fn stop(&mut self, t_stop: f64);
/// Executes output functions and propagates messages according to ICs and EOCs.
fn collection(&mut self, t: f64);
/// Propagates messages according to EICs and executes model transition functions.
fn transition(&mut self, t: f64) -> f64;
}
impl<T: Atomic + DynRef> Simulator for T {
#[inline]
fn get_component(&self) -> &Component {
Atomic::get_component(self)
}
#[inline]
fn get_component_mut(&mut self) -> &mut Component {
Atomic::get_component_mut(self)
}
#[inline]
fn start(&mut self, t_start: f64) -> f64 {
Atomic::start(self);
let t_next = t_start + self.ta();
self.set_sim_t(t_start, t_next);
t_next
}
#[inline]
fn stop(&mut self, t_stop: f64) {
self.set_sim_t(t_stop, f64::INFINITY);
Atomic::stop(self);
}
#[inline]
fn collection(&mut self, t: f64) {
if t >= self.get_t_next() {
Atomic::lambda(self)
}
}
#[inline]
fn transition(&mut self, t: f64) -> f64 {
let t_next = self.get_t_next();
// Safety: simulator executing its transition function
if !unsafe { self.get_component().is_input_empty() } {
if t == t_next {
Atomic::delta_conf(self);
self.clear_output();
} else {
let e = t - self.get_t_last();
Atomic::delta_ext(self, e);
}
self.clear_input();
} else if t == t_next {
Atomic::delta_int(self);
self.clear_output();
} else {
return t_next;
}
let t_next = t + Atomic::ta(self);
self.set_sim_t(t, t_next);
t_next
}
}
impl Simulator for Coupled {
#[inline]
fn get_component(&self) -> &Component {
&self.component
}
#[inline]
fn get_component_mut(&mut self) -> &mut Component {
&mut self.component
}
/// Iterates over all the subcomponents to call their [`Simulator::start`]
/// method and obtain the next simulation time.
///
/// If the feature `par_start` is activated, the iteration is parallelized.
///
/// If the feature `par_couplings` is activated, the EICs, EOCs, and ICs are built
/// for enabling parallel event propagation.
#[inline]
fn start(&mut self, t_start: f64) -> f64 {
#[cfg(feature = "par_start")]
let iter = self.components.par_iter_mut();
#[cfg(not(feature = "par_start"))]
let iter = self.components.iter_mut();
// we obtain the minimum next time of all the subcomponents
let t_next = iter
.map(|c| c.start(t_start))
.min_by(|a, b| a.total_cmp(b))
.unwrap_or(f64::INFINITY);
// and set the inner component's last and next times
self.set_sim_t(t_start, t_next);
#[cfg(feature = "par_couplings")]
{
self.build_par_eics();
self.build_par_eocs();
self.build_par_ics();
}
t_next
}
/// Iterates over all the subcomponents to call their [`Simulator::stop`]
/// method and obtain the next simulation time.
///
/// If the feature `par_stop` is activated, the iteration is parallelized.
#[inline]
fn stop(&mut self, t_stop: f64) {
#[cfg(feature = "par_stop")]
let iter = self.components.par_iter_mut();
#[cfg(not(feature = "par_stop"))]
let iter = self.components.iter_mut();
iter.for_each(|c| c.stop(t_stop));
// we set the inner component's last and next times accordingly
self.set_sim_t(t_stop, f64::INFINITY);
}
/// Iterates over all the subcomponents to call their [`Simulator::collection`] method.
/// Then, it iterates over all the EOCs and ICs and propagates messages accordingly.
///
/// If the feature `par_collection` is activated, the iteration over subcomponents is parallelized.
/// If the feature `par_couplings` is activated, the iteration is over couplings is parallelized.
#[inline]
fn collection(&mut self, t: f64) {
if t >= self.get_t_next() {
#[cfg(feature = "par_collection")]
let iter = self.components.par_iter_mut();
#[cfg(not(feature = "par_collection"))]
let iter = self.components.iter_mut();
iter.for_each(|c| c.collection(t));
#[cfg(feature = "par_couplings")]
self.par_xxcs.par_iter().for_each(|coups| {
coups.iter().for_each(|(port_to, port_from)| {
// Safety: coupled model propagating messages
unsafe { port_from.propagate(&**port_to) };
});
});
#[cfg(not(feature = "par_couplings"))]
{
self.eocs.iter().for_each(|(port_to, port_from)| {
// Safety: coupled model propagating messages
unsafe { port_from.propagate(&**port_to) };
});
self.ics.iter().for_each(|(port_to, port_from)| {
// Safety: coupled model propagating messages
unsafe { port_from.propagate(&**port_to) };
});
}
}
}
/// Iterates over all the EICs and propagates messages accordingly.
/// Then, it iterates over all the subcomponents to:
/// 1. Call their [`Simulator::transition`] method
/// 2. Clear their ports
/// 3. Obtain their next simulation time.
///
/// If the feature `par_couplings` is activated, the iteration over EICs is parallelized.
/// If the feature `par_transition` is activated, the iteration over subcomponents is parallelized.
#[inline]
fn transition(&mut self, t: f64) -> f64 {
// Safety: simulator checking if its input is empty
let is_external = !unsafe { self.get_component().is_input_empty() };
// Propagate messages according to EICs only if there are messages in the input ports
if is_external {
#[cfg(feature = "par_couplings")]
self.par_eics.par_iter().for_each(|coups| {
coups.iter().for_each(|(port_to, port_from)| {
// Safety: coupled model propagating messages
unsafe { port_from.propagate(&**port_to) };
});
});
#[cfg(not(feature = "par_couplings"))]
self.eics.iter().for_each(|(port_to, port_from)| {
// Safety: coupled model propagating messages
unsafe { port_from.propagate(&**port_to) };
});
self.clear_input();
}
let is_internal = t >= self.get_t_next();
if is_internal {
self.clear_output();
}
// Nested call only if there are messages in the input ports or if the time has come
if is_external || is_internal {
#[cfg(feature = "par_transition")]
let iterator = self.components.par_iter_mut();
#[cfg(not(feature = "par_transition"))]
let iterator = self.components.iter_mut();
let t_next = iterator
.map(|c| c.transition(t))
.min_by(|a, b| a.total_cmp(b))
.unwrap_or(f64::INFINITY);
self.set_sim_t(t, t_next);
}
self.get_t_next()
}
}
#[macro_export]
macro_rules! impl_coupled {
($struct_name:ident) => {
$crate::impl_coupled!($struct_name, coupled);
};
($struct_name:ident, $coupled_name:ident) => {
impl $crate::simulation::Simulator for $struct_name {
#[inline]
fn get_component(&self) -> &$crate::modeling::Component {
self.$coupled_name.get_component()
}
#[inline]
fn get_component_mut(&mut self) -> &mut $crate::modeling::Component {
self.$coupled_name.get_component_mut()
}
#[inline]
fn start(&mut self, t_start: f64) -> f64 {
self.$coupled_name.start(t_start)
}
#[inline]
fn stop(&mut self, t_stop: f64) {
self.$coupled_name.stop(t_stop)
}
#[inline]
fn collection(&mut self, t: f64) {
self.$coupled_name.collection(t)
}
#[inline]
fn transition(&mut self, t: f64) -> f64 {
self.$coupled_name.transition(t)
}
}
impl From<$struct_name> for $crate::modeling::Coupled {
fn from(coupled: $struct_name) -> Self {
coupled.coupled
}
}
};
}
/// Root coordinator for sequential simulations of DEVS models.
#[repr(transparent)]
pub struct RootCoordinator<T>(T);
impl<T: Simulator> RootCoordinator<T> {
/// Creates a new root coordinator from a DEVS-compliant model.
pub fn new(model: T) -> Self {
Self(model)
}
/// Runs a simulation for a given period of time.
pub fn simulate(&mut self, t_stop: f64) {
let mut t_next = self.start(0.);
while t_next < t_stop {
self.collection(t_next);
t_next = self.transition(t_next);
}
self.stop(t_next);
}
}
impl<T> Deref for RootCoordinator<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for RootCoordinator<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}