From 142ebb2ec7e2b4297b55ee1a620ec8fe4aad8eab Mon Sep 17 00:00:00 2001 From: Chad Baker Date: Wed, 22 May 2024 08:47:16 -0600 Subject: [PATCH 1/2] added several `log::debug` --- rust/altrios-core/src/consist/consist_model.rs | 6 +++++- rust/altrios-core/src/meet_pass/est_times/mod.rs | 6 ++++++ rust/altrios-core/src/train/speed_limit_train_sim.rs | 5 +++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/rust/altrios-core/src/consist/consist_model.rs b/rust/altrios-core/src/consist/consist_model.rs index ed5a7d39..2d866f07 100644 --- a/rust/altrios-core/src/consist/consist_model.rs +++ b/rust/altrios-core/src/consist/consist_model.rs @@ -331,7 +331,11 @@ impl Consist { // maybe put logic for toggling `engine_on` here for (i, (loco, pwr_out)) in self.loco_vec.iter_mut().zip(pwr_out_vec.iter()).enumerate() { - log::info!("Solving locomotive #{}", i); + log::info!( + "Solving locomotive #{}\n`pwr_out: `{} MW", + i, + pwr_out.get::().format_eng(None) + ); loco.solve_energy_consumption(*pwr_out, dt, engine_on) .map_err(|err| { err.context(format!( diff --git a/rust/altrios-core/src/meet_pass/est_times/mod.rs b/rust/altrios-core/src/meet_pass/est_times/mod.rs index 1ec4e406..aa653fd8 100644 --- a/rust/altrios-core/src/meet_pass/est_times/mod.rs +++ b/rust/altrios-core/src/meet_pass/est_times/mod.rs @@ -491,6 +491,7 @@ pub fn make_est_times>( let time_depart = speed_limit_train_sim.state.time; // Push initial fake nodes + log::debug!("{}", format_dbg!("Push initial fake nodes.")); est_times.push(EstTime { idx_next: 1, ..Default::default() @@ -502,6 +503,7 @@ pub fn make_est_times>( }); // Add origin estimated times + log::debug!("{}", format_dbg!("Add origin estimated times.")); for orig in origs { ensure!( orig.offset == si::Length::ZERO, @@ -520,6 +522,7 @@ pub fn make_est_times>( ..Default::default() }; + log::debug!("{}", format_dbg!()); insert_est_time( &mut est_times, &mut est_alt, @@ -535,6 +538,7 @@ pub fn make_est_times>( ..Default::default() }, ); + log::debug!("{}", format_dbg!()); insert_est_time( &mut est_times, &mut est_alt, @@ -565,6 +569,7 @@ pub fn make_est_times>( } // Fix distances for different origins + log::debug!("{}", format_dbg!("Fix distances for different origins")); { let mut est_idx_fix = 1; while est_idx_fix != EST_IDX_NA { @@ -580,6 +585,7 @@ pub fn make_est_times>( let mut est_idxs_end = Vec::::with_capacity(8); // Iterate and process all saved sims + log::debug!("{}", format_dbg!("Iterate and process all saved sims")); while let Some(mut sim) = saved_sims.pop() { let mut has_split = false; ensure!( diff --git a/rust/altrios-core/src/train/speed_limit_train_sim.rs b/rust/altrios-core/src/train/speed_limit_train_sim.rs index 942c447e..561209df 100644 --- a/rust/altrios-core/src/train/speed_limit_train_sim.rs +++ b/rust/altrios-core/src/train/speed_limit_train_sim.rs @@ -293,6 +293,11 @@ impl SpeedLimitTrainSim { self.train_res .update_res(&mut self.state, &self.path_tpc, &Dir::Fwd)?; self.solve_required_pwr()?; + log::debug!( + "{}\ntime step: {}", + format_dbg!(), + self.state.time.get::().format_eng(None) + ); self.loco_con.solve_energy_consumption( self.state.pwr_whl_out, self.state.dt, From b64197a3806eee385f8376cf90e562a05d7f7b82 Mon Sep 17 00:00:00 2001 From: Chad Baker Date: Wed, 22 May 2024 18:06:08 -0600 Subject: [PATCH 2/2] changed to `with_context` from `ok_or...` --- rust/altrios-core/src/consist/consist_model.rs | 4 ++-- .../src/consist/locomotive/battery_electric_loco.rs | 2 +- .../altrios-core/src/consist/locomotive/hybrid_loco.rs | 2 +- .../src/consist/locomotive/locomotive_model.rs | 9 +++++---- rust/altrios-core/src/track/link/link_impl.rs | 10 ++++++---- rust/altrios-core/src/track/path_track/path_tpc.rs | 2 +- rust/altrios-core/src/train/speed_limit_train_sim.rs | 10 +++++++++- rust/altrios-core/src/train/train_config.rs | 4 ++-- 8 files changed, 27 insertions(+), 16 deletions(-) diff --git a/rust/altrios-core/src/consist/consist_model.rs b/rust/altrios-core/src/consist/consist_model.rs index 2d866f07..32e7b6ba 100644 --- a/rust/altrios-core/src/consist/consist_model.rs +++ b/rust/altrios-core/src/consist/consist_model.rs @@ -182,7 +182,7 @@ impl Consist { |f_sum, (i, loco)| -> anyhow::Result { Ok(loco .force_max()? - .ok_or_else(|| anyhow!("Locomotive {i} does not have `force_max` set"))? + .with_context(|| anyhow!("Locomotive {i} does not have `force_max` set"))? + f_sum) }, ) @@ -504,7 +504,7 @@ impl Mass for Consist { |m_acc, (i, loco)| -> anyhow::Result { let loco_mass = loco .mass()? - .ok_or_else(|| anyhow!("Locomotive {i} does not have `mass` set"))?; + .with_context(|| anyhow!("Locomotive {i} does not have `mass` set"))?; let new_mass: si::Mass = loco_mass + m_acc; Ok(new_mass) }, diff --git a/rust/altrios-core/src/consist/locomotive/battery_electric_loco.rs b/rust/altrios-core/src/consist/locomotive/battery_electric_loco.rs index f363c330..e522f6ec 100644 --- a/rust/altrios-core/src/consist/locomotive/battery_electric_loco.rs +++ b/rust/altrios-core/src/consist/locomotive/battery_electric_loco.rs @@ -80,7 +80,7 @@ impl LocoTrait for BatteryElectricLoco { ) -> anyhow::Result<()> { // TODO: proposed interface location to feed in the catenary self.res.set_cur_pwr_out_max( - pwr_aux.ok_or(anyhow!(format_dbg!("`pwr_aux` not provided")))?, + pwr_aux.with_context(|| anyhow!(format_dbg!("`pwr_aux` not provided")))?, None, None, )?; diff --git a/rust/altrios-core/src/consist/locomotive/hybrid_loco.rs b/rust/altrios-core/src/consist/locomotive/hybrid_loco.rs index cd8ff3a1..538cb697 100644 --- a/rust/altrios-core/src/consist/locomotive/hybrid_loco.rs +++ b/rust/altrios-core/src/consist/locomotive/hybrid_loco.rs @@ -97,7 +97,7 @@ impl LocoTrait for Box { dt: si::Time, ) -> anyhow::Result<()> { self.res.set_cur_pwr_out_max( - pwr_aux.ok_or(anyhow!(format_dbg!("`pwr_aux` not provided")))?, + pwr_aux.with_context(|| anyhow!(format_dbg!("`pwr_aux` not provided")))?, None, None, )?; diff --git a/rust/altrios-core/src/consist/locomotive/locomotive_model.rs b/rust/altrios-core/src/consist/locomotive/locomotive_model.rs index 32167d13..69311774 100644 --- a/rust/altrios-core/src/consist/locomotive/locomotive_model.rs +++ b/rust/altrios-core/src/consist/locomotive/locomotive_model.rs @@ -171,13 +171,13 @@ impl LocoParams { fn from_hash(mut params: HashMap<&str, f64>) -> anyhow::Result { let pwr_aux_offset_watts = params .remove("pwr_aux_offset_watts") - .ok_or_else(|| anyhow!("Must provide 'pwr_aux_offset_watts'."))?; + .with_context(|| anyhow!("Must provide 'pwr_aux_offset_watts'."))?; let pwr_aux_traction_coeff_ratio = params .remove("pwr_aux_traction_coeff_ratio") - .ok_or_else(|| anyhow!("Must provide 'pwr_aux_traction_coeff_ratio'."))?; + .with_context(|| anyhow!("Must provide 'pwr_aux_traction_coeff_ratio'."))?; let force_max_newtons = params .remove("force_max_newtons") - .ok_or_else(|| anyhow!("Must provide 'force_max_newtons'."))?; + .with_context(|| anyhow!("Must provide 'force_max_newtons'."))?; let mass_kg = params.remove("mass_kg"); ensure!( params.is_empty(), @@ -655,7 +655,8 @@ impl Locomotive { } pub fn force_max(&self) -> anyhow::Result> { - self.check_force_max()?; + self.check_force_max() + .with_context(|| anyhow!(format_dbg!()))?; Ok(self.force_max) } diff --git a/rust/altrios-core/src/track/link/link_impl.rs b/rust/altrios-core/src/track/link/link_impl.rs index c95ab7a4..eafc80dd 100644 --- a/rust/altrios-core/src/track/link/link_impl.rs +++ b/rust/altrios-core/src/track/link/link_impl.rs @@ -69,10 +69,12 @@ impl Link { self.speed_set = Some( self.speed_sets .get(&train_type) - .ok_or(anyhow!( - "No value found for train_type: {:?} in `speed_sets`.", - train_type - ))? + .with_context(|| { + anyhow!( + "No value found for train_type: {:?} in `speed_sets`.", + train_type + ) + })? .clone(), ); self.speed_sets = HashMap::new(); diff --git a/rust/altrios-core/src/track/path_track/path_tpc.rs b/rust/altrios-core/src/track/path_track/path_tpc.rs index 2a600b9c..9662c43d 100644 --- a/rust/altrios-core/src/track/path_track/path_tpc.rs +++ b/rust/altrios-core/src/track/path_track/path_tpc.rs @@ -383,7 +383,7 @@ fn extract_speed_set<'a>( speed_sets .iter() .find(|&sps| sps.0 == &train_params.train_type) - .ok_or_else(|| { + .with_context(|| { anyhow!( "`speed_set` is `None` and `train_params.train_type` {:?} not found in `speed_sets.keys()` {:?}", train_params.train_type, diff --git a/rust/altrios-core/src/train/speed_limit_train_sim.rs b/rust/altrios-core/src/train/speed_limit_train_sim.rs index 561209df..ac8ff669 100644 --- a/rust/altrios-core/src/train/speed_limit_train_sim.rs +++ b/rust/altrios-core/src/train/speed_limit_train_sim.rs @@ -296,7 +296,7 @@ impl SpeedLimitTrainSim { log::debug!( "{}\ntime step: {}", format_dbg!(), - self.state.time.get::().format_eng(None) + self.state.time.get::().format_eng(Some(9)) ); self.loco_con.solve_energy_consumption( self.state.pwr_whl_out, @@ -323,6 +323,14 @@ impl SpeedLimitTrainSim { || (self.state.offset < self.path_tpc.offset_end() && self.state.speed != si::Velocity::ZERO) { + log::debug!( + "{}", + format_dbg!( + self.state.offset < self.path_tpc.offset_end() - 1000.0 * uc::FT + || (self.state.offset < self.path_tpc.offset_end() + && self.state.speed != si::Velocity::ZERO) + ) + ); self.step()?; } Ok(()) diff --git a/rust/altrios-core/src/train/train_config.rs b/rust/altrios-core/src/train/train_config.rs index 647fb94d..04bcd190 100644 --- a/rust/altrios-core/src/train/train_config.rs +++ b/rust/altrios-core/src/train/train_config.rs @@ -426,7 +426,7 @@ impl TrainSimBuilder { // `self.origin_id` verified to be `Some` earlier location_map .get(self.origin_id.as_ref().unwrap()) - .ok_or_else(|| { + .with_context(|| { anyhow!(format!( "{}\n`origin_id`: \"{}\" not found in `location_map` keys: {:?}", format_dbg!(), @@ -437,7 +437,7 @@ impl TrainSimBuilder { // `self.destination_id` verified to be `Some` earlier location_map .get(self.destination_id.as_ref().unwrap()) - .ok_or_else(|| { + .with_context(|| { anyhow!(format!( "{}\n`destination_id`: \"{}\" not found in `location_map` keys: {:?}", format_dbg!(),