-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dummy PR: changed to drag_coef: VehicleField<f64>
#43
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
//! Module for utility functions that support the vehicle struct. | ||
|
||
// rust crates | ||
use argmin::core::{CostFunction, Error, Executor, OptimizationResult, State}; | ||
use argmin::solver::neldermead::NelderMead; | ||
use ndarray::{array, Array1}; | ||
use polynomial::Polynomial; | ||
use validator::ValidationError; | ||
|
||
// local | ||
use crate::air::*; | ||
use crate::cycle::RustCycle; | ||
use crate::imports::*; | ||
|
@@ -96,7 +99,7 @@ pub fn abc_to_drag_coeffs( | |
wheel_rr_coef = a_newton / veh.veh_kg / props.a_grav_mps2; | ||
} | ||
|
||
veh.drag_coef = drag_coef; | ||
veh.drag_coef.value = drag_coef; | ||
veh.wheel_rr_coef = wheel_rr_coef; | ||
|
||
(drag_coef, wheel_rr_coef) | ||
|
@@ -131,7 +134,7 @@ pub fn get_error_val(model: Array1<f64>, test: Array1<f64>, time_steps: Array1<f | |
err += 0.5 * (time_steps[index + 1] - time_steps[index]) * (y[index] + y[index + 1]); | ||
} | ||
|
||
return err / (time_steps.last().unwrap() - time_steps[0]); | ||
return err / (time_steps.last().unwrap() - time_steps.first().unwrap()); | ||
} | ||
|
||
struct GetError<'a> { | ||
|
@@ -149,7 +152,7 @@ impl CostFunction for GetError<'_> { | |
let cyc: RustCycle = self.cycle.clone(); | ||
let dyno_func_lb: Polynomial<f64> = self.dyno_func_lb.clone(); | ||
|
||
veh.drag_coef = x[0]; | ||
veh.drag_coef.value = x[0]; | ||
veh.wheel_rr_coef = x[1]; | ||
|
||
let mut sd_coast: RustSimDrive = RustSimDrive::new(self.cycle.clone(), veh); | ||
|
@@ -180,6 +183,130 @@ impl CostFunction for GetError<'_> { | |
} | ||
} | ||
|
||
macro_rules! array_and_scalar_ops { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kylecarow , this is where I've implemented the |
||
($vft: ty) => { | ||
impl std::ops::Mul for VehicleField<$vft> { | ||
type Output = $vft; | ||
fn mul(self, rhs: Self) -> Self::Output { | ||
self.value * rhs.value | ||
} | ||
} | ||
impl std::ops::Mul<$vft> for VehicleField<$vft> { | ||
type Output = $vft; | ||
fn mul(self, rhs: $vft) -> Self::Output { | ||
self.value * rhs | ||
} | ||
} | ||
impl std::ops::Add for VehicleField<$vft> { | ||
type Output = $vft; | ||
fn add(self, rhs: Self) -> Self::Output { | ||
self.value + rhs.value | ||
} | ||
} | ||
impl std::ops::Add<$vft> for VehicleField<$vft> { | ||
type Output = $vft; | ||
fn add(self, rhs: $vft) -> Self::Output { | ||
self.value + rhs | ||
} | ||
} | ||
impl std::ops::Div for VehicleField<$vft> { | ||
type Output = $vft; | ||
fn div(self, rhs: Self) -> Self::Output { | ||
self.value / rhs.value | ||
} | ||
} | ||
impl std::ops::Div<$vft> for VehicleField<$vft> { | ||
type Output = $vft; | ||
fn div(self, rhs: $vft) -> Self::Output { | ||
self.value / rhs | ||
} | ||
} | ||
impl std::ops::Sub for VehicleField<$vft> { | ||
type Output = $vft; | ||
fn sub(self, rhs: Self) -> Self::Output { | ||
self.value - rhs.value | ||
} | ||
} | ||
impl std::ops::Sub<$vft> for VehicleField<$vft> { | ||
type Output = $vft; | ||
fn sub(self, rhs: $vft) -> Self::Output { | ||
self.value - rhs | ||
} | ||
} | ||
impl std::ops::Rem for VehicleField<$vft> { | ||
type Output = $vft; | ||
fn rem(self, rhs: Self) -> Self::Output { | ||
self.value % rhs.value | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl std::cmp::PartialEq<VehicleField<f64>> for VehicleField<f64> { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.value == other.value | ||
} | ||
} | ||
|
||
impl std::cmp::PartialEq<f64> for VehicleField<f64> { | ||
fn eq(&self, other: &f64) -> bool { | ||
&self.value == other | ||
} | ||
} | ||
|
||
#[derive(Default, Serialize, Deserialize, Clone, Debug)] | ||
pub struct VehicleField<T> | ||
where | ||
T: PartialEq, | ||
{ | ||
value: T, | ||
doc: Option<String>, | ||
orphaned: bool, | ||
} | ||
|
||
impl<T> ApproxEq for VehicleField<T> | ||
where | ||
T: ApproxEq + PartialEq, | ||
{ | ||
/// Returns `true` if `value` fields are approximately equal | ||
fn approx_eq(&self, other: &Self, tol: f64) -> bool { | ||
self.value.approx_eq(&other.value, tol) | ||
} | ||
} | ||
|
||
impl<T> VehicleField<T> | ||
where | ||
T: PartialOrd + PartialEq + Default, | ||
{ | ||
pub fn new(value: T) -> Self { | ||
Self { | ||
value, | ||
..Default::default() | ||
} | ||
} | ||
} | ||
|
||
array_and_scalar_ops!(f64); | ||
array_and_scalar_ops!(u32); | ||
array_and_scalar_ops!(Array1<f64>); | ||
|
||
impl core::ops::Index<usize> for VehicleField<Array1<f64>> { | ||
type Output = f64; | ||
fn index(&self, index: usize) -> &Self::Output { | ||
&self.value[index] | ||
} | ||
} | ||
|
||
/// Validate range of f64 | ||
pub(crate) fn validate_greater_than_zero_f64( | ||
value: &VehicleField<f64>, | ||
) -> Result<(), ValidationError> { | ||
match value.value { | ||
v if v > 0. => Ok(()), | ||
_ => Err(ValidationError::new("Value not in specified range.")), | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod vehicle_utils_tests { | ||
use super::*; | ||
|
@@ -219,7 +346,7 @@ mod vehicle_utils_tests { | |
|
||
assert!(drag_coef.approx_eq(&0.24676817210529464, 1e-5)); | ||
assert!(wheel_rr_coef.approx_eq(&0.0068603812443132645, 1e-6)); | ||
assert_eq!(drag_coef, veh.drag_coef); | ||
assert_eq!(veh.drag_coef, drag_coef); | ||
assert_eq!(wheel_rr_coef, veh.wheel_rr_coef); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kylecarow ,
VehiclField<f64>
can be multiplied by f64 but not the other order. Because types that are used in math operations normally implement thestd::markers::Copy
trait andVehicleField<f64>
cannot implement that trait due to fields that can't do it, we need to use.clone()
a lot, which in my mind negates the benefit of thestd::ops
implementations.