Skip to content

Commit

Permalink
Merge pull request #71 from NREL/more-logging
Browse files Browse the repository at this point in the history
added several `log::debug`
  • Loading branch information
calbaker authored Jun 20, 2024
2 parents da95447 + ce29fad commit 0253217
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 16 deletions.
10 changes: 7 additions & 3 deletions rust/altrios-core/src/consist/consist_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl Consist {
|f_sum, (i, loco)| -> anyhow::Result<si::Force> {
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)
},
)
Expand Down Expand Up @@ -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::<si::megawatt>().format_eng(None)
);
loco.solve_energy_consumption(*pwr_out, dt, engine_on)
.map_err(|err| {
err.context(format!(
Expand Down Expand Up @@ -500,7 +504,7 @@ impl Mass for Consist {
|m_acc, (i, loco)| -> anyhow::Result<si::Mass> {
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)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)?;
Expand Down
2 changes: 1 addition & 1 deletion rust/altrios-core/src/consist/locomotive/hybrid_loco.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl LocoTrait for Box<HybridLoco> {
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,
)?;
Expand Down
9 changes: 5 additions & 4 deletions rust/altrios-core/src/consist/locomotive/locomotive_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,13 @@ impl LocoParams {
fn from_hash(mut params: HashMap<&str, f64>) -> anyhow::Result<Self> {
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(),
Expand Down Expand Up @@ -655,7 +655,8 @@ impl Locomotive {
}

pub fn force_max(&self) -> anyhow::Result<Option<si::Force>> {
self.check_force_max()?;
self.check_force_max()
.with_context(|| anyhow!(format_dbg!()))?;
Ok(self.force_max)
}

Expand Down
6 changes: 6 additions & 0 deletions rust/altrios-core/src/meet_pass/est_times/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ pub fn make_est_times<N: AsRef<[Link]>>(
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()
Expand All @@ -502,6 +503,7 @@ pub fn make_est_times<N: AsRef<[Link]>>(
});

// Add origin estimated times
log::debug!("{}", format_dbg!("Add origin estimated times."));
for orig in origs {
ensure!(
orig.offset == si::Length::ZERO,
Expand All @@ -520,6 +522,7 @@ pub fn make_est_times<N: AsRef<[Link]>>(
..Default::default()
};

log::debug!("{}", format_dbg!());
insert_est_time(
&mut est_times,
&mut est_alt,
Expand All @@ -535,6 +538,7 @@ pub fn make_est_times<N: AsRef<[Link]>>(
..Default::default()
},
);
log::debug!("{}", format_dbg!());
insert_est_time(
&mut est_times,
&mut est_alt,
Expand Down Expand Up @@ -565,6 +569,7 @@ pub fn make_est_times<N: AsRef<[Link]>>(
}

// 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 {
Expand All @@ -580,6 +585,7 @@ pub fn make_est_times<N: AsRef<[Link]>>(
let mut est_idxs_end = Vec::<EstIdx>::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!(
Expand Down
10 changes: 6 additions & 4 deletions rust/altrios-core/src/track/link/link_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion rust/altrios-core/src/track/path_track/path_tpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions rust/altrios-core/src/train/speed_limit_train_sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<si::second>().format_eng(Some(9))
);
self.loco_con.solve_energy_consumption(
self.state.pwr_whl_out,
self.state.dt,
Expand All @@ -318,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(())
Expand Down
4 changes: 2 additions & 2 deletions rust/altrios-core/src/train/train_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(),
Expand All @@ -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!(),
Expand Down

0 comments on commit 0253217

Please sign in to comment.