Skip to content

Commit

Permalink
editoast: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Wadjetz committed Oct 23, 2024
1 parent 8c20cd1 commit 9529da3
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 57 deletions.
10 changes: 2 additions & 8 deletions editoast/editoast_schemas/src/rolling_stock/gamma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ editoast_common::schemas! {
#[derivative(Hash)]
pub struct Gamma {
#[serde(rename = "type")]
gamma_type: String,
pub gamma_type: String,
#[derivative(Hash(hash_with = "editoast_common::hash_float::<3,_>"))]
value: f64,
}

impl Gamma {
pub fn new(gamma_type: String, value: f64) -> Self {
Self { gamma_type, value }
}
pub value: f64,
}
11 changes: 0 additions & 11 deletions editoast/editoast_schemas/src/rolling_stock/rolling_resistance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,3 @@ pub struct RollingResistance {
#[derivative(Hash(hash_with = "editoast_common::hash_float::<5,_>"))]
pub C: f64,
}

impl RollingResistance {
pub fn new(rolling_resistance_type: String, a: f64, b: f64, c: f64) -> Self {
Self {
rolling_resistance_type,
A: a,
B: b,
C: c,
}
}
}
61 changes: 33 additions & 28 deletions editoast/src/core/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,33 +99,33 @@ impl PhysicsConsist {
}

pub fn compute_max_speed(&self, traction_engine: &RollingStock) -> f64 {
if let Some(max_speed_parameter) = self.max_speed {
f64::min(traction_engine.max_speed, max_speed_parameter)
} else {
traction_engine.max_speed
}
self.max_speed
.map(|max_speed_parameter| f64::min(traction_engine.max_speed, max_speed_parameter))
.unwrap_or(traction_engine.max_speed)
}

pub fn compute_startup_acceleration(&self, traction_engine: &RollingStock) -> f64 {
if let Some(towed_rolling_stock) = self.towed_rolling_stock.as_ref() {
f64::max(
traction_engine.startup_acceleration,
towed_rolling_stock.startup_acceleration,
)
} else {
traction_engine.startup_acceleration
}
self.towed_rolling_stock
.as_ref()
.map(|towed_rolling_stock| {
f64::max(
traction_engine.startup_acceleration,
towed_rolling_stock.startup_acceleration,
)
})
.unwrap_or(traction_engine.startup_acceleration)
}

pub fn compute_comfort_acceleration(&self, traction_engine: &RollingStock) -> f64 {
if let Some(towed_rolling_stock) = self.towed_rolling_stock.as_ref() {
f64::min(
traction_engine.comfort_acceleration,
towed_rolling_stock.comfort_acceleration,
)
} else {
traction_engine.comfort_acceleration
}
self.towed_rolling_stock
.as_ref()
.map(|towed_rolling_stock| {
f64::min(
traction_engine.comfort_acceleration,
towed_rolling_stock.comfort_acceleration,
)
})
.unwrap_or(traction_engine.comfort_acceleration)
}

pub fn compute_inertia_coefficient(&self, traction_engine: &RollingStock) -> f64 {
Expand Down Expand Up @@ -177,12 +177,12 @@ impl PhysicsConsist {
let rav_b = rav_b_te + rav_b_towed;
let rav_c = rav_c_te + rav_c_towed;

RollingResistance::new(
traction_engine_rr.rolling_resistance_type.clone(),
rav_a,
rav_b,
rav_c,
)
RollingResistance {
rolling_resistance_type: traction_engine_rr.rolling_resistance_type.clone(),
A: rav_a,
B: rav_b,
C: rav_c,
}
} else {
traction_engine.rolling_resistance.clone()
}
Expand Down Expand Up @@ -596,7 +596,12 @@ mod tests {

assert_eq!(
physics_consist.compute_rolling_resistance(&traction_engine),
RollingResistance::new("davis".to_string(), 1350.0, 48.6, 7.387200000000001)
RollingResistance {
rolling_resistance_type: "davis".to_string(),
A: 1350.0,
B: 48.6,
C: 7.387200000000001
}
);

physics_consist.towed_rolling_stock = None;
Expand Down
30 changes: 23 additions & 7 deletions editoast/src/models/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,22 @@ pub async fn create_rolling_stock_livery(

pub fn create_towed_rolling_stock() -> TowedRollingStock {
TowedRollingStock {
name: "TOWED ROLLING STOCK".to_string(),
mass: 50000_f64,
length: 30_f64, // m
name: "SIMPLE_ROLLING_STOCK".to_string(),
mass: 50000.0,
length: 30.0, // m
comfort_acceleration: 0.2,
startup_acceleration: 0.06,
inertia_coefficient: 1.05,
rolling_resistance: RollingResistance::new("davis".to_string(), 1.0, 0.01, 0.0002),
gamma: Gamma::new("CONST".to_string(), 1.0),
rolling_resistance: RollingResistance {
rolling_resistance_type: "davis".to_string(),
A: 1.0,
B: 0.01,
C: 0.0002,
},
gamma: Gamma {
gamma_type: "CONST".to_string(),
value: 1.0,
},
railjson_version: "3.4".to_string(),
}
}
Expand All @@ -249,12 +257,20 @@ pub fn create_simple_rolling_stock() -> RollingStock {
electrical_power_startup_time: None,
raise_pantograph_time: None,
energy_sources: vec![],
gamma: Gamma::new("CONST".to_string(), 1.0),
gamma: Gamma {
gamma_type: "CONST".to_string(),
value: 1.0,
},
locked: false,
metadata: None,
power_restrictions: HashMap::new(),
railjson_version: "12".to_string(),
rolling_resistance: RollingResistance::new("davis".to_string(), 1.0, 0.01, 0.0005),
rolling_resistance: RollingResistance {
rolling_resistance_type: "davis".to_string(),
A: 1.0,
B: 0.01,
C: 0.0005,
},
length: 140.0, // m
mass: 15000.0, // kg
max_speed: 20.0, // m/s
Expand Down
15 changes: 12 additions & 3 deletions editoast/src/views/timetable/stdcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,8 +705,12 @@ mod tests {
rolling_stock.inertia_coefficient = 1.10; // m/s²
rolling_stock.comfort_acceleration = 0.1;
rolling_stock.startup_acceleration = 0.04; // m/s²
rolling_stock.rolling_resistance =
RollingResistance::new("davis".to_string(), 1.0, 0.01, 0.0005);
rolling_stock.rolling_resistance = RollingResistance {
rolling_resistance_type: "davis".to_string(),
A: 1.0,
B: 0.01,
C: 0.0005,
};

let towed_rolling_stock = create_towed_rolling_stock();

Expand All @@ -728,7 +732,12 @@ mod tests {

assert_eq!(
physics_rolling_stock.rolling_resistance,
RollingResistance::new("davis".to_string(), 2000.0, 72.0, 9.072000000000001)
RollingResistance {
rolling_resistance_type: "davis".to_string(),
A: 2000.0,
B: 72.0,
C: 9.072000000000001
}
);
}

Expand Down

0 comments on commit 9529da3

Please sign in to comment.