-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
--------- Co-authored-by: James Tomlinson <[email protected]>
- Loading branch information
1 parent
5f9702c
commit 100a9fd
Showing
5 changed files
with
139 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use super::PywrError; | ||
use crate::metric::Metric; | ||
use crate::model::Model; | ||
use crate::parameters::{Parameter, ParameterMeta}; | ||
use crate::scenario::ScenarioIndex; | ||
use crate::state::State; | ||
use crate::timestep::Timestep; | ||
use crate::PywrError::InvalidMetricValue; | ||
use std::any::Any; | ||
|
||
pub struct DivisionParameter { | ||
meta: ParameterMeta, | ||
numerator: Metric, | ||
denominator: Metric, | ||
} | ||
|
||
impl DivisionParameter { | ||
pub fn new(name: &str, numerator: Metric, denominator: Metric) -> Self { | ||
Self { | ||
meta: ParameterMeta::new(name), | ||
numerator, | ||
denominator, | ||
} | ||
} | ||
} | ||
|
||
impl Parameter for DivisionParameter { | ||
fn as_any_mut(&mut self) -> &mut dyn Any { | ||
self | ||
} | ||
fn meta(&self) -> &ParameterMeta { | ||
&self.meta | ||
} | ||
fn compute( | ||
&self, | ||
_timestep: &Timestep, | ||
_scenario_index: &ScenarioIndex, | ||
model: &Model, | ||
state: &State, | ||
_internal_state: &mut Option<Box<dyn Any + Send>>, | ||
) -> Result<f64, PywrError> { | ||
// TODO handle scenarios | ||
let denominator = self.denominator.get_value(model, state)?; | ||
|
||
if denominator == 0.0 { | ||
return Err(InvalidMetricValue(format!( | ||
"Division by zero creates a NaN in {}.", | ||
self.name() | ||
))); | ||
} | ||
|
||
let numerator = self.numerator.get_value(model, state)?; | ||
Ok(numerator / denominator) | ||
} | ||
} |
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