dvcompute_dist/simulation/
specs.rs

1// Copyright (c) 2020-2022  David Sorokin <davsor@mail.ru>, based in Yoshkar-Ola, Russia
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
7use crate::simulation::generator::GeneratorType;
8
9/// It represents the simulation specs.
10#[derive(Clone)]
11pub struct Specs {
12
13    /// The start simulation time.
14    pub start_time: f64,
15
16    /// The final simulation time.
17    pub stop_time: f64,
18
19    /// The integration time step.
20    pub dt: f64,
21
22    /// The type of random number generator.
23    pub generator_type: GeneratorType
24}
25
26impl Specs {
27
28    /// Return the indexed time value in the grid by the specified index and size,
29    /// where the index changes within `[0, size)`. Index 0 corresponds to the start time,
30    /// but the index with value `size-1` corresponds to the stop time.
31    #[inline]
32    pub fn grid_time(&self, index: usize, size: usize) -> f64 {
33        let t0 = self.start_time;
34        let t2 = self.stop_time;
35        let n2 = if size <= 1 { 1 } else { size - 1 };
36        let dt2 = (t2 - t0) / (n2 as f64);
37
38        if index == 0 { t0 } else {
39            if index == n2 { t2 } else {
40                t0 + (index as f64) * dt2
41            }
42        }
43    }
44
45    /// Return the grid index that would correspond the specified modeling time.
46    #[inline]
47    pub fn grid_index(&self, t: f64, size: usize) -> usize {
48        let t0 = self.start_time;
49        let t2 = self.stop_time;
50        let n2 = if size <= 1 { 1 } else { size - 1 };
51
52        if t == t0 {
53            0
54        } else if t == t2 {
55            n2
56        } else {
57            let dt = (t2 - t0) / (n2 as f64);
58            ((t - t0) / dt).floor() as usize
59        }
60    }
61}
62
63/// It represents the simulation specs representation.
64#[repr(C)]
65#[derive(Clone)]
66pub struct SpecsRepr {
67
68    /// The start simulation time.
69    pub start_time: f64,
70
71    /// The final simulation time.
72    pub stop_time: f64,
73
74    /// The integration time step.
75    pub dt: f64
76}
77
78impl From<Specs> for SpecsRepr {
79
80    fn from(specs: Specs) -> Self {
81        SpecsRepr {
82            start_time: specs.start_time,
83            stop_time: specs.stop_time,
84            dt: specs.dt
85        }
86    }
87}
88
89impl SpecsRepr {
90
91    /// Return the indexed time value in the grid by the specified index and size,
92    /// where the index changes within `[0, size)`. Index 0 corresponds to the start time,
93    /// but the index with value `size-1` corresponds to the stop time.
94    #[inline]
95    pub fn grid_time(&self, index: usize, size: usize) -> f64 {
96        let t0 = self.start_time;
97        let t2 = self.stop_time;
98        let n2 = if size <= 1 { 1 } else { size - 1 };
99        let dt2 = (t2 - t0) / (n2 as f64);
100
101        if index == 0 { t0 } else {
102            if index == n2 { t2 } else {
103                t0 + (index as f64) * dt2
104            }
105        }
106    }
107
108    /// Return the grid index that would correspond the specified modeling time.
109    #[inline]
110    pub fn grid_index(&self, t: f64, size: usize) -> usize {
111        let t0 = self.start_time;
112        let t2 = self.stop_time;
113        let n2 = if size <= 1 { 1 } else { size - 1 };
114
115        if t == t0 {
116            0
117        } else if t == t2 {
118            n2
119        } else {
120            let dt = (t2 - t0) / (n2 as f64);
121            ((t - t0) / dt).floor() as usize
122        }
123    }
124}