fastsim_core/
params.rs

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
//! Module containing FASTSim parameters.

use crate::imports::*;
use crate::proc_macros::{add_pyo3_api, ApproxEq};
#[cfg(feature = "pyo3")]
use crate::pyo3imports::*;

use std::collections::HashMap;

/// Unit conversions that should NEVER change
pub const MPH_PER_MPS: f64 = 2.2369;
pub const M_PER_MI: f64 = 1609.00;
// Newtons per pound-force
pub const N_PER_LBF: f64 = 4.448;
pub const L_PER_GAL: f64 = 3.78541;
pub const HP_PER_KW: f64 = 1.34102;
pub const IN_PER_M: f64 = 39.3701;
pub const LBS_PER_KG: f64 = 2.20462;

/// Misc Constants
pub const MODERN_MAX: f64 = 0.95;

/// Struct containing time trace data
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, ApproxEq)]
#[allow(non_snake_case)]
#[add_pyo3_api(
    #[allow(non_snake_case)]
    #[new]
    pub fn __new__() -> Self {
        Self::default()
    }

    #[pyo3(name = "get_fuel_lhv_kj_per_kg")]
    pub fn get_fuel_lhv_kj_per_kg_py(&self) -> f64 {
        self.get_fuel_lhv_kj_per_kg()
    }

    #[pyo3(name = "set_fuel_lhv_kj_per_kg")]
    pub fn set_fuel_lhv_kj_per_kg_py(&mut self, fuel_lhv_kj_per_kg: f64) {
        self.set_fuel_lhv_kj_per_kg(fuel_lhv_kj_per_kg);
    }

    pub fn __getnewargs__(&self) {
        todo!();
    }
)]
pub struct RustPhysicalProperties {
    pub air_density_kg_per_m3: f64, // = 1.2, Sea level air density at approximately 20C
    pub a_grav_mps2: f64,           // = 9.81
    pub kwh_per_gge: f64,           // = 33.7 # kWh per gallon of gasoline
    pub fuel_rho_kg__L: f64, // = 0.75 # gasoline density in kg/L https://inchem.org/documents/icsc/icsc/eics1400.htm
    pub fuel_afr_stoich: f64, // = 14.7 # gasoline stoichiometric air-fuel ratio https://en.wikipedia.org/wiki/Air%E2%80%93fuel_ratio
    #[serde(skip)]
    pub orphaned: bool,
}

impl SerdeAPI for RustPhysicalProperties {}

impl Default for RustPhysicalProperties {
    fn default() -> Self {
        let air_density_kg_per_m3 = 1.2;
        let a_grav_mps2 = 9.81;
        let kwh_per_gge = 33.7;
        #[allow(non_snake_case)]
        let fuel_rho_kg__L = 0.75;
        let fuel_afr_stoich = 14.7;
        Self {
            air_density_kg_per_m3,
            a_grav_mps2,
            kwh_per_gge,
            fuel_rho_kg__L,
            fuel_afr_stoich,
            orphaned: false,
        }
    }
}

impl RustPhysicalProperties {
    pub fn get_fuel_lhv_kj_per_kg(&self) -> f64 {
        // fuel_lhv_kj_per_kg = kWhPerGGE / 3.785 [L/gal] / fuel_rho_kg_per_L [kg/L] * 3_600 [s/hr] = [kJ/kg]
        self.kwh_per_gge / 3.785 / self.fuel_rho_kg__L * 3.6e3
    }

    pub fn set_fuel_lhv_kj_per_kg(&mut self, fuel_lhv_kj_per_kg: f64) {
        // kWhPerGGE = fuel_lhv_kj_per_kg * fuel_rho_kg_per_L [kg/L] * 3.785 [L/gal] / 3_600 [s/hr] = [kJ/kg]
        self.kwh_per_gge = fuel_lhv_kj_per_kg * 3.785 * self.fuel_rho_kg__L / 3.6e3;
    }
}

// Vehicle model parameters that should be changed only by advanced users

/// Relatively continuous power out percentages for assigning FC efficiencies
pub const FC_PERC_OUT_ARRAY: [f64; 100] = [
    0., 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.011, 0.012, 0.013,
    0.014, 0.015, 0.016, 0.017, 0.018, 0.019, 0.02, 0.021, 0.022, 0.023, 0.024, 0.025, 0.026,
    0.027, 0.028, 0.029, 0.03, 0.035, 0.04, 0.045, 0.05, 0.055, 0.06, 0.065, 0.07, 0.08, 0.09, 0.1,
    0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2, 0.21, 0.22, 0.23, 0.24, 0.25, 0.26,
    0.27, 0.28, 0.29, 0.3, 0.31, 0.32, 0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.4, 0.41, 0.42,
    0.43, 0.44, 0.45, 0.46, 0.47, 0.48, 0.49, 0.5, 0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58,
    0.59, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1.,
];

pub const MC_PERC_OUT_ARRAY: [f64; 101] = [
    0., 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.11, 0.12, 0.13, 0.14, 0.15,
    0.16, 0.17, 0.18, 0.19, 0.2, 0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28, 0.29, 0.3, 0.31,
    0.32, 0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.4, 0.41, 0.42, 0.43, 0.44, 0.45, 0.46, 0.47,
    0.48, 0.49, 0.5, 0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59, 0.6, 0.61, 0.62, 0.63,
    0.64, 0.65, 0.66, 0.67, 0.68, 0.69, 0.7, 0.71, 0.72, 0.73, 0.74, 0.75, 0.76, 0.77, 0.78, 0.79,
    0.8, 0.81, 0.82, 0.83, 0.84, 0.85, 0.86, 0.87, 0.88, 0.89, 0.9, 0.91, 0.92, 0.93, 0.94, 0.95,
    0.96, 0.97, 0.98, 0.99, 1.,
];

pub const SMALL_MOTOR_POWER_KW: f64 = 7.5;
pub const LARGE_MOTOR_POWER_KW: f64 = 75.0;

pub const LARGE_BASELINE_EFF: [f64; 11] = [
    0.83, 0.85, 0.87, 0.89, 0.90, 0.91, 0.93, 0.94, 0.94, 0.93, 0.92,
];

pub const SMALL_BASELINE_EFF: [f64; 11] = [
    0.12, 0.16, 0.21, 0.29, 0.35, 0.42, 0.75, 0.92, 0.93, 0.93, 0.92,
];

pub const CHG_EFF: f64 = 0.86; // charger efficiency for PEVs, this should probably not be hard coded long term

#[add_pyo3_api]
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, ApproxEq)]
pub struct RustLongParams {
    #[serde(rename = "rechgFreqMiles")]
    pub rechg_freq_miles: Vec<f64>,
    #[serde(rename = "ufArray")]
    pub uf_array: Vec<f64>,
    #[serde(rename = "LD_FE_Adj_Coef")]
    pub ld_fe_adj_coef: AdjCoefMap,
}

impl SerdeAPI for RustLongParams {}

#[add_pyo3_api]
#[derive(Default, Debug, Serialize, Deserialize, PartialEq, Clone, ApproxEq)]
pub struct AdjCoefMap {
    #[serde(flatten)]
    pub adj_coef_map: HashMap<String, AdjCoef>,
}

impl SerdeAPI for AdjCoefMap {}

#[add_pyo3_api]
#[derive(Default, Debug, Serialize, Deserialize, PartialEq, Clone, ApproxEq)]
pub struct AdjCoef {
    #[serde(rename = "City Intercept")]
    pub city_intercept: f64,
    #[serde(rename = "City Slope")]
    pub city_slope: f64,
    #[serde(rename = "Highway Intercept")]
    pub hwy_intercept: f64,
    #[serde(rename = "Highway Slope")]
    pub hwy_slope: f64,
}
impl SerdeAPI for AdjCoef {}

impl Default for RustLongParams {
    fn default() -> Self {
        let long_params_str = include_str!("../resources/longparams.json");
        let long_params = Self::from_json(long_params_str, false).unwrap();
        long_params
    }
}

#[cfg(test)]
mod params_test {
    use super::*;

    #[test]
    fn test_get_long_params() {
        let long_params = RustLongParams::default();

        let adj_coef_2008 = AdjCoef {
            city_intercept: 0.003259,
            city_slope: 1.1805,
            hwy_intercept: 0.001376,
            hwy_slope: 1.3466,
        };
        let adj_coef_2017 = AdjCoef {
            city_intercept: 0.004091,
            city_slope: 1.1601,
            hwy_intercept: 0.003191,
            hwy_slope: 1.2945,
        };

        let mut adj_coef_map = HashMap::new();
        adj_coef_map.insert(String::from("2008"), adj_coef_2008);
        adj_coef_map.insert(String::from("2017"), adj_coef_2017);

        assert_eq!(long_params.rechg_freq_miles.len(), 602);
        assert_eq!(long_params.uf_array.len(), 602);
        assert_eq!(long_params.ld_fe_adj_coef.adj_coef_map, adj_coef_map);
    }
}