Skip to content

Commit

Permalink
core: use uom to type units
Browse files Browse the repository at this point in the history
Signed-off-by: Tristram Gräbener <[email protected]>
  • Loading branch information
Tristramg committed Dec 5, 2024
1 parent 908d2aa commit ee4ecc5
Show file tree
Hide file tree
Showing 17 changed files with 306 additions and 135 deletions.
20 changes: 20 additions & 0 deletions editoast/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions editoast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ tracing = { version = "0.1.41", default-features = false, features = [
"attributes",
"log",
] }
uom = { version = "0.36.0", features = ["serde"] }
url = { version = "2.5.4", features = ["serde"] }
urlencoding = "2.1.3"
utoipa = { version = "4.2.3", features = ["chrono", "uuid"] }
Expand Down Expand Up @@ -184,6 +185,7 @@ tracing-opentelemetry = { version = "0.28.0", default-features = false, features
"tracing-log",
] }
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
uom.workspace = true
url.workspace = true
utoipa.workspace = true
uuid.workspace = true
Expand Down
1 change: 1 addition & 0 deletions editoast/editoast_common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ rangemap.workspace = true
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
thiserror.workspace = true
uom.workspace = true
utoipa.workspace = true

[lints]
Expand Down
24 changes: 24 additions & 0 deletions editoast/editoast_common/src/hash_rounded_float.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
use std::hash::{Hash, Hasher};
use uom::si::{
acceleration::meter_per_second_squared,
f64::{Acceleration, Length, Velocity},
length::meter,
velocity::meter_per_second,
};

/// Hash a float through a rounded integer value
/// `hash_float<3,_>` means that the value is rounded to the nearest thousandth
pub fn hash_float<const T: u8, H: Hasher>(value: &f64, state: &mut H) {
((value * 10i64.pow(T as u32) as f64).round() as i64).hash(state);
}

/// Hash a length through a rounded integer value
/// `hash_length<3,_>` means that the value is rounded to the nearest thousandth
pub fn hash_length<const T: u8, H: Hasher>(value: &Length, state: &mut H) {
hash_float::<T, H>(&value.get::<meter>(), state);
}

/// Hash a acceleration through a rounded integer value
/// `hash_acceleration<3,_>` means that the value is rounded to the nearest thousandth
pub fn hash_acceleration<const T: u8, H: Hasher>(value: &Acceleration, state: &mut H) {
hash_float::<T, H>(&value.get::<meter_per_second_squared>(), state);
}

/// Hash a velocity through a rounded integer value
/// `hash_velocity<3,_>` means that the value is rounded to the nearest thousandth
pub fn hash_velocity<const T: u8, H: Hasher>(value: &Velocity, state: &mut H) {
hash_float::<T, H>(&value.get::<meter_per_second>(), state);
}

/// Hash a list of floats through a list of rounded integer value
/// `hash_float_slice<3,_>` means that the values are rounded to the nearest thousandth
pub fn hash_float_slice<const T: u8, H: Hasher>(value: &[f64], state: &mut H) {
Expand Down
3 changes: 3 additions & 0 deletions editoast/editoast_common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ mod hash_rounded_float;
pub mod rangemap_utils;
pub mod schemas;

pub use hash_rounded_float::hash_acceleration;
pub use hash_rounded_float::hash_float;
pub use hash_rounded_float::hash_float_slice;
pub use hash_rounded_float::hash_length;
pub use hash_rounded_float::hash_velocity;

schemas! {
geometry::schemas(),
Expand Down
1 change: 1 addition & 0 deletions editoast/editoast_schemas/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ serde.workspace = true
serde_json.workspace = true
strum.workspace = true
thiserror.workspace = true
uom.workspace = true
utoipa.workspace = true
uuid.workspace = true

Expand Down
11 changes: 6 additions & 5 deletions editoast/editoast_schemas/src/rolling_stock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub use towed_rolling_stock::TowedRollingStock;
use serde::Deserialize;
use serde::Serialize;
use std::collections::HashMap;
use uom::si::f64::{Acceleration, Length, Velocity};

editoast_common::schemas! {
effort_curves::schemas(),
Expand All @@ -54,14 +55,14 @@ pub struct RollingStock {
pub effort_curves: EffortCurves,
pub base_power_class: Option<String>,
/// In m
pub length: f64,
pub length: Length,
/// In m/s
pub max_speed: f64,
pub startup_time: f64,
pub max_speed: Velocity,
pub startup_time: f64, // What unit ?
/// In m/s²
pub startup_acceleration: f64,
pub startup_acceleration: Acceleration,
/// In m/s²
pub comfort_acceleration: f64,
pub comfort_acceleration: Acceleration,
// The constant gamma braking coefficient used when NOT circulating
// under ETCS/ERTMS signaling system in m/s^2
pub const_gamma: f64,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use uom::si::f64::{Acceleration, Length};

use super::RollingResistancePerWeight;

#[derive(Debug, Clone, PartialEq, serde::Deserialize, serde::Serialize)]
Expand All @@ -8,11 +10,11 @@ pub struct TowedRollingStock {
/// In kg
pub mass: f64,
/// In m
pub length: f64,
pub length: Length,
/// In m/s²
pub comfort_acceleration: f64,
pub comfort_acceleration: Acceleration,
/// In m/s²
pub startup_acceleration: f64,
pub startup_acceleration: Acceleration,
pub inertia_coefficient: f64,
pub rolling_resistance: RollingResistancePerWeight,
/// The constant gamma braking coefficient used when NOT circulating
Expand Down
Loading

0 comments on commit ee4ecc5

Please sign in to comment.