Skip to content

Commit

Permalink
Merge pull request #254 from NREL/rjf/soc-bug
Browse files Browse the repository at this point in the history
Rjf/soc bug
  • Loading branch information
robfitzgerald authored Sep 5, 2024
2 parents 45d47fe + 52df10e commit 7ca47d1
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

use crate::model::access::access_model_error::AccessModelError;
use serde::{Deserialize, Serialize};

Expand All @@ -14,9 +16,9 @@ pub enum Turn {
UTurn,
}

impl ToString for Turn {
fn to_string(&self) -> String {
serde_json::to_string(self).unwrap_or_else(|_| String::from("<internal error>"))
impl Display for Turn {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", serde_json::to_string(self).unwrap_or_default())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl TurnDelayAccessModelEngine {
let turn = Turn::from_angle(angle)?;
let delay = table.get(&turn).ok_or_else(|| {
let name = String::from("tabular discrete turn delay model");
let error = format!("table missing entry for turn {}", turn.to_string());
let error = format!("table missing entry for turn {}", turn);
AccessModelError::RuntimeError { name, error }
})?;
Ok((*delay, time_unit))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub enum VehicleCostRate {
Factor {
factor: f64,
},
///
/// shift a value by some amount
Offset {
offset: f64,
},
Expand Down
11 changes: 7 additions & 4 deletions rust/routee-compass-core/src/model/state/unit_codec_name.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
Expand All @@ -9,13 +11,14 @@ pub enum UnitCodecType {
Boolean,
}

impl ToString for UnitCodecType {
fn to_string(&self) -> String {
match self {
impl Display for UnitCodecType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let msg = match self {
UnitCodecType::FloatingPoint => String::from("floating_point"),
UnitCodecType::SignedInteger => String::from("signed_integer"),
UnitCodecType::UnsignedInteger => String::from("unsigned_integer"),
UnitCodecType::Boolean => String::from("boolean"),
}
};
write!(f, "{}", msg)
}
}
2 changes: 1 addition & 1 deletion rust/routee-compass-core/src/util/fs/read_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ where
/// inspects the file to determine if it should read as a raw or gzip stream.
/// the row index (starting from zero) is passed to the deserialization op
/// as in most cases, the row number is an id.
pub fn read_raw_file<'a, F: AsRef<Path>, T>(
pub fn read_raw_file<'a, F, T>(
filepath: F,
op: impl Fn(usize, String) -> Result<T, io::Error>,
row_callback: Option<Box<dyn FnMut() + 'a>>,
Expand Down
25 changes: 11 additions & 14 deletions rust/routee-compass-powertrain/src/routee/energy_traversal_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,18 @@ impl EnergyTraversalModel {
})?
.to_string();

let vehicle = match energy_model_service
let vehicle_lookup = energy_model_service
.vehicle_library
.get(&prediction_model_name)
{
None => {
let model_names: Vec<&String> =
energy_model_service.vehicle_library.keys().collect();
Err(TraversalModelError::BuildError(format!(
"No vehicle found with model_name = '{}', try one of: {:?}",
prediction_model_name, model_names
)))
}
Some(mr) => Ok(mr.clone()),
}?
.update_from_query(conf)?;
.get(&prediction_model_name);
let vehicle_initial = vehicle_lookup.cloned().ok_or_else(|| {
let model_names: Vec<&String> = energy_model_service.vehicle_library.keys().collect();
TraversalModelError::BuildError(format!(
"No vehicle found with model_name = '{}', try one of: {:?}",
prediction_model_name, model_names
))
})?;
// allow user to customize this vehicle instance if applicable
let vehicle = vehicle_initial.update_from_query(conf)?;

Ok(EnergyTraversalModel {
energy_model_service,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl VehicleType for BEV {
}

let starting_battery_energy =
Energy::new(starting_soc_percent * self.battery_capacity.as_f64());
Energy::new(0.01 * starting_soc_percent * self.battery_capacity.as_f64());

let new_bev = BEV {
name: self.name.clone(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl VehicleType for PHEV {
));
}
let starting_battery_energy =
Energy::new(starting_soc_percent * self.battery_capacity.as_f64());
Energy::new(0.01 * starting_soc_percent * self.battery_capacity.as_f64());

let new_phev = PHEV {
name: self.name.clone(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn update_soc_percent(
/// the remaining battery as a percentage [0, 100] %
pub fn as_soc_percent(remaining_battery: &Energy, max_battery: &Energy) -> f64 {
let percent_remaining = (remaining_battery.as_f64() / max_battery.as_f64()) * 100.0;
percent_remaining.max(0.0).min(100.0)
percent_remaining.clamp(0.0, 100.0)
}

/// a capacitated vehicle's state of charge (SOC) is the inverse of the
Expand All @@ -70,5 +70,5 @@ pub fn soc_from_battery_and_delta(
) -> f64 {
let current_energy = *start_battery - *energy_used;
let percent_remaining = (current_energy.as_f64() / max_battery.as_f64()) * 100.0;
percent_remaining.max(0.0).min(100.0)
percent_remaining.clamp(0.0, 100.0)
}
1 change: 1 addition & 0 deletions rust/routee-compass/src/app/search/search_app_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::app::compass::config::config_json_extension::ConfigJsonExtensions;
/// 1. from the traversal model
/// 2. from the access model
/// 3. optionally from the query itself
///
/// using the order above, each new source optionally overwrites any existing feature
/// by name (tuple index 0) as long as they match in StateFeature::get_feature_name and
/// StateFeature::get_feature_unit_name.
Expand Down

0 comments on commit 7ca47d1

Please sign in to comment.