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
use crate::{
modeling::{
port::{Port, PortVal},
Component, InPort, OutPort,
},
simulation::Simulator,
};
use std::{collections::HashMap, sync::Arc};
pub(crate) type Coupling = (Arc<dyn Port>, Arc<dyn Port>);
/// Coupled DEVS model.
pub struct Coupled {
/// Component wrapped by the coupled model.
pub(crate) component: Component,
/// Components map. Keys are components' IDs.
comps_map: HashMap<String, usize>,
/// External input couplings map.
eic_map: HashMap<String, HashMap<String, usize>>,
/// Internal couplings map.
ic_map: HashMap<String, HashMap<String, usize>>,
/// External output couplings map.
eoc_map: HashMap<String, HashMap<String, usize>>,
/// Components of the DEVS coupled model (serialized for better performance).
pub(crate) components: Vec<Box<dyn Simulator>>,
/// External input couplings (serialized for better performance).
pub(crate) eics: Vec<Coupling>,
/// Internal couplings (serialized for better performance).
pub(crate) ics: Vec<Coupling>,
/// External output couplings (serialized for better performance).
pub(crate) eocs: Vec<Coupling>,
#[cfg(feature = "par_couplings")]
pub(crate) par_eics: Vec<Vec<Coupling>>,
#[cfg(feature = "par_couplings")]
pub(crate) par_xxcs: Vec<Vec<Coupling>>,
}
impl Coupled {
/// Creates a new coupled DEVS model with the provided name.
pub fn new(name: &str) -> Self {
Self {
component: Component::new(name),
comps_map: HashMap::new(),
eic_map: HashMap::new(),
ic_map: HashMap::new(),
eoc_map: HashMap::new(),
components: Vec::new(),
eics: Vec::new(),
ics: Vec::new(),
eocs: Vec::new(),
#[cfg(feature = "par_couplings")]
par_eics: Vec::new(),
#[cfg(feature = "par_couplings")]
par_xxcs: Vec::new(),
}
}
/// Returns the number of components in the coupled model.
#[inline]
pub fn n_components(&self) -> usize {
self.components.len()
}
/// Returns the number of external input couplings in the coupled model.
#[inline]
pub fn n_eics(&self) -> usize {
self.eic_map.values().map(|eics| eics.len()).sum()
}
/// Returns the number of internal couplings in the coupled model.
#[inline]
pub fn n_ics(&self) -> usize {
self.ic_map.values().map(|ics| ics.len()).sum()
}
/// Returns the number of external output couplings in the coupled model.
#[inline]
pub fn n_eocs(&self) -> usize {
self.eoc_map.values().map(|eocs| eocs.len()).sum()
}
/// Adds a new input port of type `T` and returns a reference to it.
///
/// It panics if there is already an input port with the same name.
#[inline]
pub fn add_in_port<T: PortVal>(&mut self, name: &str) -> InPort<T> {
self.component.add_in_port::<T>(name)
}
/// Adds a new output port of type `T` and returns a reference to it.
///
/// It panics if there is already an output port with the same name.
#[inline]
pub fn add_out_port<T: PortVal>(&mut self, name: &str) -> OutPort<T> {
self.component.add_out_port::<T>(name)
}
/// Adds a new component to the coupled model.
///
/// If there is already a component with the same name as the new component, it panics.
pub fn add_component<T: Simulator>(&mut self, component: Box<T>) {
let component_name = component.get_name();
if self.comps_map.contains_key(component_name) {
panic!("coupled model already contains component with the name provided")
}
self.comps_map
.insert(component_name.to_string(), self.components.len());
self.components.push(component);
}
/// Returns a reference to a component with the provided name.
///
/// If the coupled model does not contain any model with that name, it returns [`None`].
#[inline]
fn get_component(&self, name: &str) -> Option<&Component> {
let index = *self.comps_map.get(name)?;
Some(self.components.get(index)?.get_component())
}
/// Adds a new EIC to the model.
///
/// You must provide the input port name of the coupled model,
/// the receiving component name, and its input port name.
///
/// # Panics
///
/// This method panics if:
///
/// - The origin port does not exist.
/// - The destination component does not exist.
/// - The destination port does not exist.
/// - Ports are not compatible.
/// - Coupling already exists.
pub fn add_eic(&mut self, port_from: &str, component_to: &str, port_to: &str) {
let p_from = self
.component
.get_in_port(port_from)
.expect("port_from does not exist");
let comp_to = self
.get_component(component_to)
.expect("component_to does not exist");
let p_to = comp_to
.get_in_port(port_to)
.expect("port_to does not exist");
if !p_from.is_compatible(&*p_to) {
panic!("ports are not compatible")
}
let source_key = port_from.to_string();
let destination_key = component_to.to_string() + "-" + port_to;
let coups = self.eic_map.entry(destination_key).or_default();
if coups.contains_key(&source_key) {
panic!("coupling already exists");
}
coups.insert(source_key, self.eics.len());
self.eics.push((p_to, p_from));
}
/// Adds a new IC to the model.
///
/// You must provide the sending component name, its output port name,
/// the receiving component name, and its input port name.
///
/// # Panics
///
/// This method panics if:
///
/// - The origin component does not exist.
/// - The origin port does not exist.
/// - The destination component does not exist.
/// - The destination port does not exist.
/// - Ports are not compatible.
/// - Coupling already exists.
pub fn add_ic(
&mut self,
component_from: &str,
port_from: &str,
component_to: &str,
port_to: &str,
) {
let comp_from = self
.get_component(component_from)
.expect("component_from does not exist");
let p_from = comp_from
.get_out_port(port_from)
.expect("port_from does not exist");
let comp_to = self
.get_component(component_to)
.expect("component_to does not exist");
let p_to = comp_to
.get_in_port(port_to)
.expect("port_to does not exist");
if !p_from.is_compatible(&*p_to) {
panic!("ports are not compatible")
}
let source_key = component_from.to_string() + "-" + port_from;
let destination_key = component_to.to_string() + "-" + port_to;
let coups = self.ic_map.entry(destination_key).or_default();
if coups.contains_key(&source_key) {
panic!("coupling already exists");
}
coups.insert(source_key, self.ics.len());
self.ics.push((p_to, p_from));
}
/// Adds a new EOC to the model.
///
/// You must provide the sending component name, its output port name,
/// and the output port name of the coupled model.
///
/// # Panics
///
/// This method panics if:
///
/// - The origin component does not exist.
/// - The origin port does not exist.
/// - The destination port does not exist.
/// - Ports are not compatible.
/// - Coupling already exists.
pub fn add_eoc(&mut self, component_from: &str, port_from: &str, port_to: &str) {
let comp_from = self
.get_component(component_from)
.expect("component_from does not exist");
let p_from = comp_from
.get_out_port(port_from)
.expect("port_from does not exist");
let p_to = self
.component
.get_out_port(port_to)
.expect("port_to does not exist");
if !p_from.is_compatible(&*p_to) {
panic!("ports are not compatible")
}
let source_key = component_from.to_string() + "-" + port_from;
let destination_key = port_to.to_string();
let coups = self.eoc_map.entry(destination_key).or_default();
if coups.contains_key(&source_key) {
panic!("coupling already exists");
}
coups.insert(source_key, self.eocs.len());
self.eocs.push((p_to, p_from));
}
#[cfg(feature = "par_couplings")]
#[inline]
pub(crate) fn build_par_eics(&mut self) {
for coups in self.eic_map.values() {
let mut x = Vec::new();
for &source in coups.values() {
x.push(self.eics[source].clone());
}
self.par_eics.push(x);
}
}
#[cfg(feature = "par_couplings")]
#[inline]
pub(crate) fn build_par_ics(&mut self) {
for coups in self.ic_map.values() {
let mut x = Vec::new();
for &source in coups.values() {
x.push(self.ics[source].clone());
}
self.par_xxcs.push(x);
}
}
#[cfg(feature = "par_couplings")]
#[inline]
pub(crate) fn build_par_eocs(&mut self) {
for coups in self.eoc_map.values() {
let mut x = Vec::new();
for &source in coups.values() {
x.push(self.eocs[source].clone());
}
self.par_xxcs.push(x);
}
}
}