lmb_engine_simulator 0.1.0

The lmb_engine_simulator library provides an easy way to simulate internal combustion engines
Documentation
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use crate::base::constants::MAX_ARRAY_LEN;
use crate::engine::engine::Engine;
use crate::{BasicProperties, FlowRatio};
use crate::{ObjectInfo, ObjectType};
use ndarray::*;
use std::f64::consts::PI;
use std::time::Instant;

// Core Traits
// use crate::core::traits::Connector;
// use crate::core::traits::OneDim;
use crate::core::traits::SaveData;
use crate::core::traits::ZeroDim;

// Types
type IndexOutput = Result<Vec<(ObjectType, usize)>, String>;

// Super Traits
use crate::core::traits::Conn;
use crate::core::traits::OneD;
use crate::core::traits::ZeroD;

pub struct System {
    objs_info: Vec<ObjectInfo>,
    engine: Option<Engine>,
    zero_dim: Vec<Box<dyn ZeroD>>,
    one_dim: Vec<Box<dyn OneD>>,
    connector: Vec<Box<dyn Conn>>,
    engine_connectors_index: Vec<Vec<(ObjectType, usize)>>,
    zero_dim_connectors_index: Vec<Vec<(ObjectType, usize)>>,
    one_dim_connectors_index: Vec<Vec<(ObjectType, usize)>>,
    connector_objects_index: Vec<Vec<(ObjectType, usize)>>,
    cycle_start: usize,
    iterations_counter: usize,
    time: Array2<f64>,
}

impl System {
    pub fn new(
        objs_info: Vec<ObjectInfo>,
        engine: Option<Engine>,
        zero_dim: Vec<Box<dyn ZeroD>>,
        one_dim: Vec<Box<dyn OneD>>,
        connector: Vec<Box<dyn Conn>>,
    ) -> Result<System, String> {
        let mut system = System {
            objs_info,
            engine,
            zero_dim,
            one_dim,
            connector,
            engine_connectors_index: Vec::new(),
            zero_dim_connectors_index: Vec::new(),
            one_dim_connectors_index: Vec::new(),
            connector_objects_index: Vec::new(),
            cycle_start: 0,
            iterations_counter: 0,
            time: Array::from_elem((MAX_ARRAY_LEN, 1), 0.),
        };

        system = system.setup_indexes()?;

        Ok(system)
    }

    /// Advance all objects in System by `dt`. The state of the objects is stored.
    pub fn advance<'a>(&'a mut self, dt: f64) -> &'a mut Self {
        // Advancing ZeroDim objects
        self.zero_dim.iter_mut().for_each(|zd| zd.advance(dt));

        // Advancing OneDim objects

        // Advacing Engine
        if let Some(eng) = &mut self.engine {
            eng.advance(dt);
        }

        // check if masses less than zero

        // Update connectors `flow_ratio`: require `basic_properties` of DimElements
        for (connector, obj_index_list) in self
            .connector
            .iter_mut()
            .zip(self.connector_objects_index.iter())
        {
            let mut basic_properties: Vec<BasicProperties> =
                Vec::with_capacity(connector.connecting().len());
            for (obj_type, i) in obj_index_list.iter() {
                match obj_type {
                    ObjectType::ZeroDim => basic_properties.push(self.zero_dim[*i].get_state()),
                    ObjectType::Cylinder => basic_properties
                        .push(self.engine.as_ref().unwrap().cylinders()[*i].get_state()),
                    _ => panic!("Error at `System::advance()`\n Object of unknown type!"),
                }
            }
            connector.update_flow_ratio(basic_properties, dt);
        }

        // Update ZeroDim objects: require `flow_ratio` and `name `from connectors
        for (zero_dim, conn_index_list) in self
            .zero_dim
            .iter_mut()
            .zip(self.zero_dim_connectors_index.iter())
        {
            let mut total_flow_ratio: Vec<(&str, &FlowRatio)> =
                Vec::with_capacity(conn_index_list.len());
            for (_, i) in conn_index_list.iter() {
                let (flow_ratio, name) = match self.connector[*i].get_flow_ratio(zero_dim.name()) {
                    Ok(flow) => (flow, self.connector[*i].name()),
                    Err(err) => {
                        println!("Error at 'System::advance()': \n {}", err);
                        std::process::exit(1);
                    }
                };

                total_flow_ratio.push((name, flow_ratio));
            }
            zero_dim.update_flow_ratio(total_flow_ratio);
        }

        // Updating OneDim objects

        // Updating Engine objects
        if let Some(engine) = &mut self.engine {
            let mut cylinders_total_flow: Vec<Vec<(&str, &FlowRatio)>> =
                Vec::with_capacity(engine.cylinders().len());
            for (cyl, conn_index_list) in engine
                .cylinders()
                .iter()
                .zip(self.engine_connectors_index.iter())
            {
                let mut total_flow_ratio: Vec<(&str, &FlowRatio)> =
                    Vec::with_capacity(conn_index_list.len());
                for (_, i) in conn_index_list.iter() {
                    let (flow_ratio, name) = match self.connector[*i].get_flow_ratio(cyl.name()) {
                        Ok(flow) => (flow, self.connector[*i].name()),
                        Err(err) => {
                            println!("Error at 'System::advance()': \n {}", err);
                            std::process::exit(1);
                        }
                    };
                    total_flow_ratio.push((name, flow_ratio));
                }
                cylinders_total_flow.push(total_flow_ratio);
            }
            engine.update_cylinders_flow_ratio(cylinders_total_flow)
        }

        for data in self.objs_info.iter_mut() {
            match data.obj_type {
                ObjectType::ZeroDim => {
                    data.stored_data
                        .add_data(self.zero_dim[data.index].get_storable_data());
                }
                ObjectType::OneDim => {
                    data.stored_data
                        .add_data(self.one_dim[data.index].get_storable_data());
                }
                ObjectType::Connector => {
                    data.stored_data
                        .add_data(self.connector[data.index].get_storable_data());
                }
                ObjectType::Cylinder => {
                    if let Some(eng) = &self.engine {
                        data.stored_data
                            .add_data(eng.cylinders()[data.index].get_storable_data());
                    }
                }
            }
        }

        self
    }

    /// Advance all objects in System until steady state or until a max number of iterations/cycles is reached.
    /// All the stored data of objects are reseted
    pub fn advance_to_steady_state<'a>(&'a mut self) -> &'a mut Self {
        let now = Instant::now(); // measuring time

        // reseting StoredData from all objects:
        self.objs_info
            .iter_mut()
            .for_each(|obj| obj.stored_data.reset_data());
        self.cycle_start = 0;
        self.iterations_counter = 0;

        let mut time = 0.0;
        let mut cycle = 0;
        let mut total_angle = 0.0;
        let max_cycles = 10;
        let max_time = 5.0; // sec
        let step: f64;
        let step_calculator: Box<dyn Fn(&System) -> f64>;

        // if no `one_dim` elements exist step is constant (estimated by engine or assumed arbitrarily),
        // otherwise the step is determined by `one_dim` objects with CFL condition
        if self.one_dim.is_empty() {
            // estimate by engine
            match &self.engine {
                Some(engine) => {
                    let d_angle = 0.05;
                    step = (d_angle * PI / 180.0) / engine.sec_to_rad();
                    step_calculator = Box::new(|_: &System| -> f64 { step })
                }
                None => {
                    println!("WARNING: At `advance_to_steady_state`!");
                    println!("WARNING: No one-dimensional nor engine objects were found!");
                    println!("WARNING: Time step assumed as 0.1ms");
                    step = 1e-4;
                    step_calculator = Box::new(|_: &System| -> f64 { step })
                }
            }
        } else {
            step_calculator = Box::new(System::get_time_step);
        }

        // stop conditions are: cycle > max_cycles or time > max_time
        loop {
            let step = step_calculator(self);
            match &self.engine {
                Some(e) => {
                    total_angle += step * e.sec_to_rad();
                    if total_angle > 4.0 * PI {
                        total_angle -= 4.0 * PI;
                        cycle += 1;
                        if cycle > max_cycles {
                            break;
                        }
                        self.cycle_start = self.iterations_counter;
                    }
                }
                None => {}
            }
            if time > max_time {
                break;
            }
            self.advance(step);
            time += step;
            self.time[[self.iterations_counter, 0]] = time;
            self.iterations_counter += 1;
        }

        println!("\n\t\tSystem successfully advanced to steady state!");
        println!("number of iterations: {}", self.iterations_counter);
        println!("time taken: {:?}", now.elapsed());

        if let Some(engine) = &mut self.engine {
            let mut pressure: Vec<ArrayView1<f64>> = Vec::new();
            let mut volume: Vec<ArrayView1<f64>> = Vec::new();
            let mut data: Vec<Array2<f64>> = Vec::new();
            let range = (self.cycle_start, self.iterations_counter);
            let p_index: usize = 1;
            let v_index: usize = 3;
            for info in &self.objs_info {
                match info.obj_type {
                    ObjectType::Cylinder => {
                        data.push(info.stored_data.get_data(range, vec![p_index, v_index]));
                    }
                    _ => {}
                }
            }
            data.iter_mut().for_each(|d| {
                pressure.push(d.column(0));
                volume.push(d.column(1))
            });

            engine.calc_operational_param(pressure, volume);

            println!("Engine performance:{}", engine.operat_param());
        }
        self
    }

    /// Write the stored data from a object, `obj_name`, into a file `file_name`.
    /// If `Engine` has been added, only the data stored in the last cycle will be written as default.
    /// `_range` can be used to set the first and last index of the writable data.
    pub fn write_to_file(&self, file_name: &str, obj_name: &str, _range: Option<(usize, usize)>) {
        let range: (usize, usize);
        if let Some(r) = _range {
            range = r;
        } else {
            range = (self.cycle_start, self.iterations_counter);
        }
        let time = self.time.slice(s![range.0..range.1, ..]);

        if let Some(obj_info) = self.objs_info.iter().find(|info| info.name == obj_name) {
            obj_info.stored_data.write_to_file(
                file_name,
                range,
                Some(("time [s]\t".to_string(), time)),
            )
        } else {
            println!("Error at 'write_to_file'");
            println!(" {} was not found", obj_name);
            std::process::exit(1)
        }
    }

    pub fn engine<'a>(&'a self) -> Option<&'a Engine> {
        match &self.engine {
            Some(eng) => Some(eng),
            None => None,
        }
    }

    pub fn engine_mut<'a>(&'a mut self) -> Option<&'a mut Engine> {
        match &mut self.engine {
            Some(eng) => Some(eng),
            None => None,
        }
    }

    fn setup_indexes(mut self) -> Result<Self, String> {
        // finding which connectors are connected 0D objects
        let mut zero_dim_connectors_index: Vec<Vec<(ObjectType, usize)>> = Vec::new();
        for zero in self.zero_dim.iter() {
            let indexes = match self.get_connectors_index(zero.name().to_string()) {
                Ok(x) => x,
                Err(err) => {
                    return Err(format!("{}", err));
                }
            };
            zero_dim_connectors_index.push(indexes);
        }

        // finding which connectors are connected 1D objects
        let mut one_dim_connectors_index: Vec<Vec<(ObjectType, usize)>> = Vec::new();
        for one in self.one_dim.iter() {
            let indexes = match self.get_connectors_index(one.name().to_string()) {
                Ok(x) => x,
                Err(err) => {
                    return Err(format!("{}", err));
                }
            };
            one_dim_connectors_index.push(indexes);
        }

        // finding which objects are connected to the connectors
        let mut connector_objects_index: Vec<Vec<(ObjectType, usize)>> = Vec::new();
        for conn in self.connector.iter() {
            let indexes = match self.get_dim_obj_index(conn.connecting()) {
                Ok(x) => x,
                Err(err) => {
                    return Err(format!("{}", err));
                }
            };
            connector_objects_index.push(indexes);
        }

        // finding which connectors are connected the engine's cylinders
        if self.engine.is_some() {
            let mut engine_connectors_index: Vec<Vec<(ObjectType, usize)>> = Vec::new();
            for cyl in self.engine.as_ref().unwrap().cylinders() {
                let indexes = match self.get_connectors_index(cyl.name().to_string()) {
                    Ok(x) => x,
                    Err(err) => {
                        return Err(format!("{}", err));
                    }
                };
                engine_connectors_index.push(indexes);
            }
            self.engine_connectors_index = engine_connectors_index;
        }

        self.zero_dim_connectors_index = zero_dim_connectors_index;
        self.one_dim_connectors_index = one_dim_connectors_index;
        self.connector_objects_index = connector_objects_index;
        Ok(self)
    }

    fn get_connectors_index(&self, obj_name: String) -> IndexOutput {
        let mut conn_indexes: Vec<(ObjectType, usize)> = Vec::new();
        // searching connector vector
        for (index, conn) in self.connector.iter().enumerate() {
            if conn.connecting().iter().any(|name| *name == obj_name) {
                conn_indexes.push((ObjectType::Connector, index));
            }
        }
        if conn_indexes.is_empty() {
            let msg = format!("Object '{}' is not connected to anything.", obj_name);
            return Err(msg);
        }
        Ok(conn_indexes)
    }

    fn get_dim_obj_index(&self, conn_list: &Vec<String>) -> IndexOutput {
        let mut objs_indexes: Vec<(ObjectType, usize)> = Vec::new();
        for obj in conn_list.iter() {
            if self.zero_dim.iter().any(|z| z.name() == obj) {
                let index = self.zero_dim.iter().position(|z| z.name() == obj).unwrap();
                objs_indexes.push((ObjectType::ZeroDim, index));
            } else if self.one_dim.iter().any(|z| z.name() == obj) {
                let index = self.zero_dim.iter().position(|z| z.name() == obj).unwrap();
                objs_indexes.push((ObjectType::OneDim, index));
            } else if self.engine.is_some() {
                let index = self
                    .engine
                    .as_ref()
                    .unwrap()
                    .cylinders()
                    .iter()
                    .position(|z| z.name() == obj)
                    .unwrap();
                objs_indexes.push((ObjectType::Cylinder, index));
            } else {
                let msg = format!("Object '{}' was not found in the arrays", obj);
                return Err(msg);
            }
        }
        Ok(objs_indexes)
    }

    fn get_time_step(_system: &System) -> f64 {
        println!("using get_time_step function!");
        0.0
    }

    pub fn _store_composition_of(&mut self, _obj_name: &str) {
        // match self._objs_info.iter().find(|(name,_,_)| name == obj_name) {
        //     Some((name, obj_type, i)) => {
        //         match obj_type {
        //         ObjectType::ZeroDim => self.zero_dim[*index].write_to_file(
        //             file_name,
        //             (self.cycle_start, self.iterations_counter),
        //             Some(("time [s]\t".to_string(), time)),
        //         ),
        //         _ =>
        //     }
        //     },
        //     None => {},
        // }
    }
}