Skip to content

Commit

Permalink
stdcm: pr comments ans refacto
Browse files Browse the repository at this point in the history
Signed-off-by: Egor Berezovskiy <egor@berezify.fr>
  • Loading branch information
Wadjetz committed Oct 22, 2024
1 parent 07b8f13 commit 5571056
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 157 deletions.
225 changes: 115 additions & 110 deletions editoast/src/core/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,52 +67,135 @@ pub struct PhysicsRollingStock {
pub raise_pantograph_time: Option<u64>,
}

#[derive(Debug, Default)]
pub struct SimulationParameters {
#[derive(Debug, Clone, Default)]
pub struct PhysicsConsist {
/// In kg
pub total_mass: Option<f64>,
/// In m
pub total_length: Option<f64>,
/// In m/s
pub max_speed: Option<f64>,
pub towed_rolling_stock: Option<TowedRollingStock>,
}

impl PhysicsRollingStock {
pub fn new(
traction_engine: RollingStock,
towed_rolling_stock: Option<TowedRollingStock>,
params: SimulationParameters,
) -> Self {
let traction_engine_length = traction_engine.length * 1000.0;

let towed_rolling_stock_length = towed_rolling_stock
impl PhysicsConsist {
pub fn compute_length(&self, traction_engine: &RollingStock) -> u64 {
let towed_rolling_stock_length = self
.towed_rolling_stock
.as_ref()
.map(|trs| trs.length)
.unwrap_or(0.0);
let length = params

let traction_engine_length = traction_engine.length * 1000.0;

let length = self
.total_length
.map(|tl| tl * 1000.0)
.unwrap_or_else(|| traction_engine_length + towed_rolling_stock_length)
.round() as u64;
.unwrap_or_else(|| traction_engine_length + towed_rolling_stock_length);

length.round() as u64
}

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
}
}

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
}
}

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
}
}

pub fn compute_inertia_coefficient(&self, traction_engine: &RollingStock) -> f64 {
if let (Some(towed_rolling_stock), Some(total_mass)) =
(self.towed_rolling_stock.as_ref(), self.total_mass)
{
let mass_carriage = total_mass - traction_engine.mass;
let traction_engine_inertia =
traction_engine.mass * traction_engine.inertia_coefficient;
let towed_inertia = mass_carriage * towed_rolling_stock.inertia_coefficient;
(traction_engine_inertia + towed_inertia) / total_mass
} else {
traction_engine.inertia_coefficient
}
}

pub fn compute_consist_mass(&self, traction_engine: &RollingStock) -> u64 {
let traction_engine_mass = traction_engine.mass;
let towed_rolling_stock_mass = towed_rolling_stock.as_ref().map(|trs| trs.mass);
let mass = params
let towed_rolling_stock_mass = self
.towed_rolling_stock
.as_ref()
.map(|trs| trs.mass)
.unwrap_or(0.0);
let mass = self
.total_mass
.unwrap_or_else(|| traction_engine_mass + towed_rolling_stock_mass.unwrap_or(0.0))
.round() as u64;

let max_speed = f64::min(
traction_engine.max_speed,
params.max_speed.unwrap_or(traction_engine.max_speed),
);

let comfort_acceleration =
compute_comfort_acceleration(&traction_engine, &towed_rolling_stock);
let startup_acceleration =
compute_startup_acceleration(&traction_engine, &towed_rolling_stock);
let inertia_coefficient =
compute_inertia_coefficient(&traction_engine, &towed_rolling_stock, params.total_mass);
let rolling_resistance =
compute_rolling_resistance(&traction_engine, &towed_rolling_stock, params.total_mass);
.unwrap_or_else(|| traction_engine_mass + towed_rolling_stock_mass);
mass.round() as u64
}

pub fn compute_rolling_resistance(&self, traction_engine: &RollingStock) -> RollingResistance {
if let (Some(towed_rolling_stock), Some(total_mass)) =
(self.towed_rolling_stock.as_ref(), self.total_mass)
{
let traction_engine_rr = &traction_engine.rolling_resistance;
let towed_rs_rr = &towed_rolling_stock.rolling_resistance;
let traction_engine_mass = traction_engine.mass; // kg

let mass_carriage = total_mass - traction_engine_mass; // kg

let rav_a_te = traction_engine_rr.A * 1000.0; // convert from kN to N
let rav_b_te = traction_engine_rr.B * 1000.0 * 3.6; // convert from kN/(km/h) to N/(m/s)
let rav_c_te = traction_engine_rr.C * 1000.0 * 3.6 * 3.6; // convert from kN/(km/h)² to N/(m/s)²

let rav_a_towed = towed_rs_rr.A * 1e-2 * mass_carriage; // convert from daN/t to N
let rav_b_towed = towed_rs_rr.B * 1e-2 * mass_carriage * 3.6; // convert from (daN/t)/(km/h) to N/(m/s)
let rav_c_towed = towed_rs_rr.C * 1e-2 * mass_carriage * 3.6 * 3.6; // convert from (daN/t)/(km/h)² to N/(m/s)²

let rav_a = rav_a_te + rav_a_towed;
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,
)
} else {
traction_engine.rolling_resistance.clone()
}
}
}

impl PhysicsRollingStock {
pub fn new(traction_engine: RollingStock, physics_consist: PhysicsConsist) -> Self {
let length = physics_consist.compute_length(&traction_engine);
let max_speed = physics_consist.compute_max_speed(&traction_engine);
let startup_acceleration = physics_consist.compute_startup_acceleration(&traction_engine);
let comfort_acceleration = physics_consist.compute_comfort_acceleration(&traction_engine);
let inertia_coefficient = physics_consist.compute_inertia_coefficient(&traction_engine);
let mass = physics_consist.compute_consist_mass(&traction_engine);
let rolling_resistance = physics_consist.compute_rolling_resistance(&traction_engine);

Self {
effort_curves: traction_engine.effort_curves,
Expand All @@ -137,84 +220,6 @@ impl PhysicsRollingStock {
}
}

fn compute_rolling_resistance(
traction_engine: &RollingStock,
towed_rolling_stock: &Option<TowedRollingStock>,
total_mass: Option<f64>,
) -> RollingResistance {
if let (Some(towed_rolling_stock), Some(total_mass)) = (towed_rolling_stock, total_mass) {
let traction_engine_rr = &traction_engine.rolling_resistance;
let towed_rs_rr = &towed_rolling_stock.rolling_resistance;
let traction_engine_mass = traction_engine.mass; // kg

let mass_carriage = total_mass - traction_engine_mass; // kg

let rav_a_te = traction_engine_rr.A * 1000.0; // N
let rav_b_te = traction_engine_rr.B * 1000.0 * 3.6; // N/(m/s)
let rav_c_te = traction_engine_rr.C * 1000.0 * 3.6 * 3.6; // N/(m/s)²

let rav_a_towed = towed_rs_rr.A * 1e-2 * mass_carriage; // N
let rav_b_towed = towed_rs_rr.B * 1e-2 * mass_carriage * 3.6; // N/(m/s)
let rav_c_towed = towed_rs_rr.C * 1e-2 * mass_carriage * 3.6 * 3.6; // N/(m/s)²

let rav_a = rav_a_te + rav_a_towed;
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,
)
} else {
traction_engine.rolling_resistance.clone()
}
}

fn compute_inertia_coefficient(
traction_engine: &RollingStock,
towed_rolling_stock: &Option<TowedRollingStock>,
total_mass: Option<f64>,
) -> f64 {
if let (Some(towed_rolling_stock), Some(total_mass)) = (towed_rolling_stock, total_mass) {
let mass_carriage = total_mass - traction_engine.mass;
let traction_engine_inertia = traction_engine.mass * traction_engine.inertia_coefficient;
let towed_inertia = mass_carriage * towed_rolling_stock.inertia_coefficient;
(traction_engine_inertia + towed_inertia) / total_mass
} else {
traction_engine.inertia_coefficient
}
}

fn compute_startup_acceleration(
traction_engine: &RollingStock,
towed_rolling_stock: &Option<TowedRollingStock>,
) -> f64 {
if let Some(towed_rolling_stock) = towed_rolling_stock {
f64::max(
traction_engine.startup_acceleration,
towed_rolling_stock.startup_acceleration,
)
} else {
traction_engine.startup_acceleration
}
}

fn compute_comfort_acceleration(
traction_engine: &RollingStock,
towed_rolling_stock: &Option<TowedRollingStock>,
) -> f64 {
if let Some(towed_rolling_stock) = towed_rolling_stock {
f64::min(
traction_engine.comfort_acceleration,
towed_rolling_stock.comfort_acceleration,
)
} else {
traction_engine.comfort_acceleration
}
}

#[derive(Debug, Clone, Hash, PartialEq, Serialize, Deserialize, ToSchema)]
pub struct ZoneUpdate {
pub zone: String,
Expand Down
Loading

0 comments on commit 5571056

Please sign in to comment.