Skip to content

Commit 63bbd41

Browse files
authored
Merge pull request #39 from NREL/fix/change-DirT-to-enum
compiles with `DirT` replaced by an enum but 3 tests fail
2 parents 32c048f + 088a24b commit 63bbd41

File tree

8 files changed

+62
-48
lines changed

8 files changed

+62
-48
lines changed

rust/altrios-core/src/lin_search_hint.rs

+9-21
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
use crate::imports::*;
22
use crate::si;
33

4-
// Per Geordie, we could probably get rid of this `DirT` code
5-
pub type DirT = u8;
6-
7-
#[allow(non_snake_case)]
8-
pub mod Dir {
9-
pub use super::*;
10-
#[allow(non_upper_case_globals)]
11-
pub const Unk: DirT = 0;
12-
#[allow(non_upper_case_globals)]
13-
pub const Fwd: DirT = 1;
14-
#[allow(non_upper_case_globals)]
15-
pub const Bwd: DirT = 2;
4+
#[derive(PartialEq)]
5+
pub enum Dir {
6+
Unk,
7+
Fwd,
8+
Bwd,
169
}
1710

1811
/// Has method that returns offset from start of PathTpc
@@ -26,16 +19,12 @@ pub trait GetOffset {
2619
pub trait LinSearchHint {
2720
/// Calculate the index immediately before `offset` given the previous calculated index, `idx`,
2821
/// and a direction `DirT`.
29-
fn calc_idx<const DIR: DirT>(&self, offset: si::Length, idx: usize) -> anyhow::Result<usize>;
22+
fn calc_idx(&self, offset: si::Length, idx: usize, dir: &Dir) -> anyhow::Result<usize>;
3023
}
3124

3225
impl<T: GetOffset + core::fmt::Debug> LinSearchHint for &[T] {
33-
fn calc_idx<const DIR: DirT>(
34-
&self,
35-
offset: si::Length,
36-
mut idx: usize,
37-
) -> anyhow::Result<usize> {
38-
if DIR != Dir::Bwd {
26+
fn calc_idx(&self, offset: si::Length, mut idx: usize, dir: &Dir) -> anyhow::Result<usize> {
27+
if dir != &Dir::Bwd {
3928
ensure!(
4029
offset <= self.last().unwrap().get_offset(),
4130
"{}\nOffset larger than last slice offset!",
@@ -44,8 +33,7 @@ impl<T: GetOffset + core::fmt::Debug> LinSearchHint for &[T] {
4433
while self[idx + 1].get_offset() < offset {
4534
idx += 1;
4635
}
47-
}
48-
if DIR != Dir::Fwd {
36+
} else if dir != &Dir::Fwd {
4937
ensure!(
5038
self.first().unwrap().get_offset() <= offset,
5139
"{}\nOffset smaller than first slice offset!",

rust/altrios-core/src/train/braking_point.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl BrakingPoints {
8181
let mut train_res = train_res.clone();
8282
train_state.offset = path_tpc.offset_end();
8383
train_state.speed = si::Velocity::ZERO;
84-
train_res.update_res::<{ Dir::Unk }>(&mut train_state, path_tpc)?;
84+
train_res.update_res(&mut train_state, path_tpc, &Dir::Unk)?;
8585
let speed_points = path_tpc.speed_points();
8686
let mut idx = path_tpc.speed_points().len();
8787

@@ -101,9 +101,19 @@ impl BrakingPoints {
101101

102102
train_state.offset = bp_curr.offset;
103103
train_state.speed = bp_curr.speed_limit;
104-
train_res.update_res::<{ Dir::Bwd }>(&mut train_state, path_tpc)?;
104+
train_res.update_res(&mut train_state, path_tpc, &Dir::Bwd)?;
105105

106-
assert!(fric_brake.force_max + train_state.res_net() > si::Force::ZERO);
106+
ensure!(
107+
fric_brake.force_max + train_state.res_net() > si::Force::ZERO,
108+
format!(
109+
"{}\n{}\n{}",
110+
format_dbg!(
111+
fric_brake.force_max + train_state.res_net() > si::Force::ZERO
112+
),
113+
format_dbg!(fric_brake.force_max),
114+
format_dbg!(train_state.res_net()),
115+
)
116+
);
107117
let vel_change = train_state.dt
108118
* (fric_brake.force_max + train_state.res_net())
109119
/ train_state.mass_static;

rust/altrios-core/src/train/resistance/kind/path_res.rs

+28-15
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@ pub struct Point {
1414
impl Point {
1515
pub fn new(vals: &[PathResCoeff], state: &TrainState) -> anyhow::Result<Self> {
1616
Ok(Self {
17-
idx: vals.calc_idx::<{ Dir::Fwd }>(state.offset - state.length * 0.5, 0)?,
17+
idx: vals.calc_idx(state.offset - state.length * 0.5, 0, &Dir::Fwd)?,
1818
})
1919
}
20-
pub fn calc_res<const DIR: DirT>(
20+
pub fn calc_res(
2121
&mut self,
2222
vals: &[PathResCoeff],
2323
state: &TrainState,
24+
dir: &Dir,
2425
) -> anyhow::Result<si::Force> {
25-
self.idx = vals.calc_idx::<DIR>(state.offset - state.length * 0.5, self.idx)?;
26+
self.idx = vals.calc_idx(state.offset - state.length * 0.5, self.idx, dir)?;
2627
Ok(calc_res_val(vals[self.idx].res_coeff, state))
2728
}
2829
pub fn res_coeff_front(&self, vals: &[PathResCoeff]) -> si::Ratio {
@@ -61,31 +62,43 @@ impl Strap {
6162
idx_back: 0,
6263
})
6364
} else {
64-
let idx_back = vals.calc_idx::<{ Dir::Fwd }>(state.offset - state.length, 0)?;
65+
let idx_back = vals.calc_idx(state.offset - state.length, 0, &Dir::Fwd)?;
6566
Ok(Self {
6667
idx_back,
67-
idx_front: vals.calc_idx::<{ Dir::Fwd }>(state.offset, idx_back)?,
68+
idx_front: vals.calc_idx(state.offset, idx_back, &Dir::Fwd)?,
6869
})
6970
}
7071
}
71-
pub fn calc_res<const DIR: DirT>(
72+
pub fn calc_res(
7273
&mut self,
7374
vals: &[PathResCoeff],
7475
state: &TrainState,
76+
dir: &Dir,
7577
) -> anyhow::Result<si::Force> {
76-
if DIR == Dir::Fwd || DIR == Dir::Unk {
77-
self.idx_front = vals.calc_idx::<DIR>(state.offset, self.idx_front)?;
78-
}
79-
if DIR == Dir::Bwd || DIR == Dir::Unk {
80-
self.idx_back = vals.calc_idx::<DIR>(state.offset_back, self.idx_back)?;
78+
match dir {
79+
Dir::Fwd => {
80+
self.idx_front = vals.calc_idx(state.offset, self.idx_front, dir)?;
81+
}
82+
Dir::Bwd => {
83+
self.idx_back = vals.calc_idx(state.offset_back, self.idx_back, dir)?;
84+
}
85+
Dir::Unk => {
86+
self.idx_front = vals.calc_idx(state.offset, self.idx_front, dir)?;
87+
self.idx_back = vals.calc_idx(state.offset_back, self.idx_back, dir)?;
88+
}
8189
}
90+
8291
let res_coeff = if self.idx_front == self.idx_back {
8392
vals[self.idx_front].res_coeff
8493
} else {
85-
if DIR == Dir::Fwd {
86-
self.idx_back = vals.calc_idx::<DIR>(state.offset_back, self.idx_back)?;
87-
} else if DIR == Dir::Bwd {
88-
self.idx_front = vals.calc_idx::<DIR>(state.offset, self.idx_front)?;
94+
match dir {
95+
Dir::Fwd => {
96+
self.idx_back = vals.calc_idx(state.offset_back, self.idx_back, dir)?;
97+
}
98+
Dir::Bwd => {
99+
self.idx_front = vals.calc_idx(state.offset, self.idx_front, dir)?;
100+
}
101+
_ => {}
89102
}
90103
vals.calc_res_strap(self.idx_front, self.idx_back, state)
91104
};

rust/altrios-core/src/train/resistance/method/point.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,20 @@ pub struct Point {
1616
}
1717

1818
impl ResMethod for Point {
19-
fn update_res<const DIR: DirT>(
19+
fn update_res(
2020
&mut self,
2121
state: &mut TrainState,
2222
path_tpc: &PathTpc,
23+
dir: &Dir,
2324
) -> anyhow::Result<()> {
2425
state.offset_back = state.offset - state.length;
2526
state.weight_static = state.mass_static * uc::ACC_GRAV;
2627
state.res_bearing = self.bearing.calc_res();
2728
state.res_rolling = self.rolling.calc_res(state);
2829
state.res_davis_b = self.davis_b.calc_res(state);
2930
state.res_aero = self.aerodynamic.calc_res(state);
30-
state.res_grade = self.grade.calc_res::<DIR>(path_tpc.grades(), state)?;
31-
state.res_curve = self.curve.calc_res::<DIR>(path_tpc.curves(), state)?;
31+
state.res_grade = self.grade.calc_res(path_tpc.grades(), state, dir)?;
32+
state.res_curve = self.curve.calc_res(path_tpc.curves(), state, dir)?;
3233
state.grade_front = self.grade.res_coeff_front(path_tpc.grades());
3334
state.elev_front = self.grade.res_net_front(path_tpc.grades(), state);
3435
Ok(())

rust/altrios-core/src/train/resistance/method/strap.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,20 @@ impl Strap {
3535
}
3636
}
3737
impl ResMethod for Strap {
38-
fn update_res<const DIR: DirT>(
38+
fn update_res(
3939
&mut self,
4040
state: &mut TrainState,
4141
path_tpc: &PathTpc,
42+
dir: &Dir,
4243
) -> anyhow::Result<()> {
4344
state.offset_back = state.offset - state.length;
4445
state.weight_static = state.mass_static * uc::ACC_GRAV;
4546
state.res_bearing = self.bearing.calc_res();
4647
state.res_rolling = self.rolling.calc_res(state);
4748
state.res_davis_b = self.davis_b.calc_res(state);
4849
state.res_aero = self.aerodynamic.calc_res(state);
49-
state.res_grade = self.grade.calc_res::<DIR>(path_tpc.grades(), state)?;
50-
state.res_curve = self.curve.calc_res::<DIR>(path_tpc.curves(), state)?;
50+
state.res_grade = self.grade.calc_res(path_tpc.grades(), state, dir)?;
51+
state.res_curve = self.curve.calc_res(path_tpc.curves(), state, dir)?;
5152
state.grade_front = self.grade.res_coeff_front(path_tpc.grades());
5253
state.elev_front = self.grade.res_net_front(path_tpc.grades(), state);
5354
Ok(())

rust/altrios-core/src/train/resistance/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ use crate::train::TrainState;
88

99
#[enum_dispatch]
1010
pub trait ResMethod {
11-
fn update_res<const DIR: DirT>(
11+
fn update_res(
1212
&mut self,
1313
state: &mut TrainState,
1414
path_tpc: &PathTpc,
15+
dir: &Dir,
1516
) -> anyhow::Result<()>;
1617
fn fix_cache(&mut self, link_point_del: &LinkPoint);
1718
}

rust/altrios-core/src/train/set_speed_train_sim.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ impl SetSpeedTrainSim {
314314
self.loco_con
315315
.set_cur_pwr_max_out(None, self.speed_trace.dt(self.state.i))?;
316316
self.train_res
317-
.update_res::<{ Dir::Fwd }>(&mut self.state, &self.path_tpc)?;
317+
.update_res(&mut self.state, &self.path_tpc, &Dir::Fwd)?;
318318
self.solve_required_pwr(self.speed_trace.dt(self.state.i));
319319
self.loco_con.solve_energy_consumption(
320320
self.state.pwr_whl_out,

rust/altrios-core/src/train/speed_limit_train_sim.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ impl SpeedLimitTrainSim {
287287
self.loco_con.set_pwr_aux(Some(true))?;
288288
self.loco_con.set_cur_pwr_max_out(None, self.state.dt)?;
289289
self.train_res
290-
.update_res::<{ Dir::Fwd }>(&mut self.state, &self.path_tpc)?;
290+
.update_res(&mut self.state, &self.path_tpc, &Dir::Fwd)?;
291291
self.solve_required_pwr()?;
292292
self.loco_con.solve_energy_consumption(
293293
self.state.pwr_whl_out,

0 commit comments

Comments
 (0)