Skip to content

Commit

Permalink
Merge pull request #88 from NREL/fix/feature-gate-logging
Browse files Browse the repository at this point in the history
feature gated logging functionality
  • Loading branch information
calbaker authored Aug 19, 2024
2 parents 7ea083e + df5505a commit 12201d6
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 4 deletions.
16 changes: 16 additions & 0 deletions rust/Cargo.lock

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

1 change: 1 addition & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ polars-lazy = { version = "0.32" }
pyo3-polars = { version = "0.6" }
uom = { version = "0.35.0", features = ["use_serde"] }
eng_fmt = "0.1.2"
document-features = "0.2.10"

# local crates in this workspace
altrios-core = { path = "./altrios-core" }
Expand Down
7 changes: 6 additions & 1 deletion rust/altrios-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ altrios-proc-macros = { workspace = true }
argmin = "0.5.1"
rayon = "1.5.3"
bincode = "1.3.3"
log = "0.4.17"
log = { version = "0.4.17", optional = true }
document-features = { workspace = true }
anyhow = { workspace = true }
readonly = "0.2.3"
duplicate = "0.4.1"
Expand Down Expand Up @@ -50,7 +51,11 @@ directories = "5.0.1"
tempfile = "3.10.1"

[features]
## Exposes ALTRIOS structs, methods, and functions to Python.
pyo3 = ["dep:pyo3"]
## Enables logging messages that can be passed to Python if `pyo3` is also
## enabled.
logging = ["dep:log"]

[lints.rust]
# `'cfg(debug_advance_rewind)'` is expected for debugging in `advance_rewind.rs`
Expand Down
1 change: 1 addition & 0 deletions rust/altrios-core/src/consist/consist_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ 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() {
#[cfg(feature = "logging")]
log::info!(
"Solving locomotive #{}\n`pwr_out: `{} MW",
i,
Expand Down
1 change: 1 addition & 0 deletions rust/altrios-core/src/consist/locomotive/loco_sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ impl LocomotiveSimulationVec {
.par_iter_mut()
.enumerate()
.try_for_each(|(i, loco_sim)| {
#[cfg(feature = "logging")]
log::info!("Solving locomotive #{i}");
loco_sim
.walk()
Expand Down
2 changes: 2 additions & 0 deletions rust/altrios-core/src/consist/locomotive/locomotive_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ impl Mass for Locomotive {
Some(new_mass) => {
if let Some(dm) = derived_mass {
if dm != new_mass {
#[cfg(feature = "logging")]
log::warn!(
"Derived mass does not match provided mass, setting `{}` consituent mass fields to `None`",
stringify!(Locomotive));
Expand All @@ -661,6 +662,7 @@ impl Mass for Locomotive {
)
})?),
};
#[cfg(feature = "logging")]
log::info!("Updating `force_max` to correspond to new mass.");
self.force_max = self
.mu()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ impl Mass for FuelConverter {
let derived_mass = self.derived_mass().with_context(|| format_dbg!())?;
if let (Some(derived_mass), Some(new_mass)) = (derived_mass, new_mass) {
if derived_mass != new_mass {
#[cfg(feature = "logging")]
log::info!(
"Derived mass from `self.specific_pwr` and `self.pwr_out_max` does not match {}",
"provided mass. Updating based on `side_effect`"
Expand All @@ -147,6 +148,7 @@ impl Mass for FuelConverter {
}
}
} else if new_mass.is_none() {
#[cfg(feature = "logging")]
log::debug!("Provided mass is None, setting `self.specific_pwr` to None");
self.specific_pwr = None;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ impl Mass for Generator {
let derived_mass = self.derived_mass().with_context(|| format_dbg!())?;
if let (Some(derived_mass), Some(new_mass)) = (derived_mass, new_mass) {
if derived_mass != new_mass {
#[cfg(feature = "logging")]
log::info!(
"Derived mass from `self.specific_pwr` and `self.pwr_out_max` does not match {}",
"provided mass. Updating based on `side_effect`"
Expand All @@ -161,6 +162,7 @@ impl Mass for Generator {
}
}
} else if new_mass.is_none() {
#[cfg(feature = "logging")]
log::debug!("Provided mass is None, setting `self.specific_pwr` to None");
self.specific_pwr = None;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ impl Mass for ReversibleEnergyStorage {
let derived_mass = self.derived_mass().with_context(|| format_dbg!())?;
if let (Some(derived_mass), Some(new_mass)) = (derived_mass, new_mass) {
if derived_mass != new_mass {
#[cfg(feature = "logging")]
log::info!(
"Derived mass from `self.specific_energy` and `self.energy_capacity` does not match {}",
"provided mass. Updating based on `side_effect`"
Expand All @@ -232,6 +233,7 @@ impl Mass for ReversibleEnergyStorage {
}
}
} else if new_mass.is_none() {
#[cfg(feature = "logging")]
log::debug!("Provided mass is None, setting `self.specific_energy` to None");
self.specific_energy = None;
}
Expand Down
4 changes: 2 additions & 2 deletions rust/altrios-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
//! handful of standard data formats as strings or file read/write operations using
//! [traits::SerdeAPI].
//!
//! # Features:
//! - pyo3: enable this feature to expose ALTRIOS structs, methods, and functions to Python
//! # Feature Flags
#![doc = document_features::document_features!()]

#[macro_use]
pub mod macros;
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
#[cfg(feature = "logging")]
log::debug!("{}", format_dbg!("Push initial fake nodes."));
est_times.push(EstTime {
idx_next: 1,
Expand All @@ -503,6 +504,7 @@ pub fn make_est_times<N: AsRef<[Link]>>(
});

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

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

// Fix distances for different origins
#[cfg(feature = "logging")]
log::debug!("{}", format_dbg!("Fix distances for different origins"));
{
let mut est_idx_fix = 1;
Expand All @@ -585,6 +590,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
#[cfg(feature = "logging")]
log::debug!("{}", format_dbg!("Iterate and process all saved sims"));
while let Some(mut sim) = saved_sims.pop() {
let mut has_split = false;
Expand Down
4 changes: 4 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 @@ -295,6 +295,7 @@ impl SpeedLimitTrainSim {
self.train_res
.update_res(&mut self.state, &self.path_tpc, &Dir::Fwd)?;
self.solve_required_pwr()?;
#[cfg(feature = "logging")]
log::debug!(
"{}\ntime step: {}",
format_dbg!(),
Expand Down Expand Up @@ -325,6 +326,7 @@ impl SpeedLimitTrainSim {
|| (self.state.offset < self.path_tpc.offset_end()
&& self.state.speed != si::Velocity::ZERO)
{
#[cfg(feature = "logging")]
log::debug!(
"{}",
format_dbg!(
Expand Down Expand Up @@ -362,6 +364,7 @@ impl SpeedLimitTrainSim {
let mut idx_prev = 0;
while idx_prev != timed_path.len() - 1 {
let mut idx_next = idx_prev + 1;
#[cfg(feature = "logging")]
log::debug!("Solving idx: {}", idx_next);
while idx_next + 1 < timed_path.len() - 1 && timed_path[idx_next].time < self.state.time
{
Expand Down Expand Up @@ -457,6 +460,7 @@ impl SpeedLimitTrainSim {
.min(pwr_pos_max / speed_target.min(v_max));
// Verify that train has sufficient power to move
if self.state.speed < uc::MPH * 0.1 && f_pos_max <= res_net {
#[cfg(feature = "logging")]
log::debug!("{}", format_dbg!(self.path_tpc));
bail!(
"{}\nTrain does not have sufficient power to move!\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}", // ,\nlink={:?}
Expand Down
2 changes: 2 additions & 0 deletions rust/altrios-core/src/train/train_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ impl TrainSimBuilder {
.mass()
.with_context(|| format_dbg!())?
.unwrap_or_else(|| {
#[cfg(feature = "logging")]
log::warn!(
"Consist has no mass set so train dynamics don't include consist mass."
);
Expand Down Expand Up @@ -407,6 +408,7 @@ impl TrainSimBuilder {
let res_aero =
res_kind::aerodynamic::Basic::new(match &self.train_config.drag_coeff_vec {
Some(dcv) => {
#[cfg(feature = "logging")]
log::info!("Using `drag_coeff_vec` to calculate aero resistance.");
dcv.iter()
.fold(0. * uc::M2, |acc, dc| *dc * veh.drag_area_loaded + acc)
Expand Down
5 changes: 4 additions & 1 deletion rust/altrios-py/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ version = "0.1.0"
[dependencies]
altrios-core = { workspace = true, features = ["pyo3"] }
pyo3 = { workspace = true, features = ["extension-module", "anyhow"] }
pyo3-log = { workspace = true }
pyo3-log = { workspace = true, optional=true }
polars = { workspace = true }
polars-lazy = { workspace = true }
pyo3-polars = { workspace = true }
Expand All @@ -21,3 +21,6 @@ pyo3-polars = { workspace = true }
[lib]
name = "altrios_pyo3"
crate-type = ["cdylib"]

[features]
logging = ["altrios-core/logging", "dep:pyo3-log"]
1 change: 1 addition & 0 deletions rust/altrios-py/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub use pyo3_polars::PyDataFrame;

#[pymodule]
fn altrios_pyo3(_py: Python, m: &PyModule) -> PyResult<()> {
#[cfg(feature = "logging")]
pyo3_log::init();
m.add_class::<FuelConverter>()?;
m.add_class::<FuelConverterState>()?;
Expand Down

0 comments on commit 12201d6

Please sign in to comment.