Skip to content

Commit

Permalink
refactor: Make the Parameter trait generic over the calculated value …
Browse files Browse the repository at this point in the history
…type. (#134)

This allows removing `IndexParameter` and `MultiValueParameter` traits, reducing code duplication.
  • Loading branch information
jetuk authored Mar 11, 2024
1 parent f9fd14c commit 4c86c45
Show file tree
Hide file tree
Showing 33 changed files with 94 additions and 126 deletions.
28 changes: 17 additions & 11 deletions pywr-core/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::parameters::{MultiValueParameterIndex, ParameterType, VariableConfig}
use crate::recorders::{MetricSet, MetricSetIndex, MetricSetState};
use crate::scenario::ScenarioIndex;
use crate::solvers::{MultiStateSolver, Solver, SolverFeatures, SolverTimings};
use crate::state::{ParameterStates, State, StateBuilder};
use crate::state::{MultiValue, ParameterStates, State, StateBuilder};
use crate::timestep::Timestep;
use crate::virtual_storage::{VirtualStorage, VirtualStorageIndex, VirtualStorageReset, VirtualStorageVec};
use crate::{parameters, recorders, IndexParameterIndex, NodeIndex, ParameterIndex, PywrError, RecorderIndex};
Expand Down Expand Up @@ -201,9 +201,9 @@ pub struct Network {
aggregated_nodes: AggregatedNodeVec,
aggregated_storage_nodes: AggregatedStorageNodeVec,
virtual_storage_nodes: VirtualStorageVec,
parameters: Vec<Box<dyn parameters::Parameter>>,
index_parameters: Vec<Box<dyn parameters::IndexParameter>>,
multi_parameters: Vec<Box<dyn parameters::MultiValueParameter>>,
parameters: Vec<Box<dyn parameters::Parameter<f64>>>,
index_parameters: Vec<Box<dyn parameters::Parameter<usize>>>,
multi_parameters: Vec<Box<dyn parameters::Parameter<MultiValue>>>,
derived_metrics: Vec<DerivedMetric>,
metric_sets: Vec<MetricSet>,
resolve_order: Vec<ComponentType>,
Expand Down Expand Up @@ -1089,23 +1089,26 @@ impl Network {
}

/// Get a `Parameter` from a parameter's name
pub fn get_parameter(&self, index: &ParameterIndex) -> Result<&dyn parameters::Parameter, PywrError> {
pub fn get_parameter(&self, index: &ParameterIndex) -> Result<&dyn parameters::Parameter<f64>, PywrError> {
match self.parameters.get(*index.deref()) {
Some(p) => Ok(p.as_ref()),
None => Err(PywrError::ParameterIndexNotFound(*index)),
}
}

/// Get a `Parameter` from a parameter's name
pub fn get_mut_parameter(&mut self, index: &ParameterIndex) -> Result<&mut dyn parameters::Parameter, PywrError> {
pub fn get_mut_parameter(
&mut self,
index: &ParameterIndex,
) -> Result<&mut dyn parameters::Parameter<f64>, PywrError> {
match self.parameters.get_mut(*index.deref()) {
Some(p) => Ok(p.as_mut()),
None => Err(PywrError::ParameterIndexNotFound(*index)),
}
}

/// Get a `Parameter` from a parameter's name
pub fn get_parameter_by_name(&self, name: &str) -> Result<&dyn parameters::Parameter, PywrError> {
pub fn get_parameter_by_name(&self, name: &str) -> Result<&dyn parameters::Parameter<f64>, PywrError> {
match self.parameters.iter().find(|p| p.name() == name) {
Some(parameter) => Ok(parameter.as_ref()),
None => Err(PywrError::ParameterNotFound(name.to_string())),
Expand All @@ -1121,7 +1124,7 @@ impl Network {
}

/// Get a `IndexParameter` from a parameter's name
pub fn get_index_parameter_by_name(&self, name: &str) -> Result<&dyn parameters::IndexParameter, PywrError> {
pub fn get_index_parameter_by_name(&self, name: &str) -> Result<&dyn parameters::Parameter<usize>, PywrError> {
match self.index_parameters.iter().find(|p| p.name() == name) {
Some(parameter) => Ok(parameter.as_ref()),
None => Err(PywrError::ParameterNotFound(name.to_string())),
Expand Down Expand Up @@ -1311,7 +1314,10 @@ impl Network {
}

/// Add a `parameters::Parameter` to the network
pub fn add_parameter(&mut self, parameter: Box<dyn parameters::Parameter>) -> Result<ParameterIndex, PywrError> {
pub fn add_parameter(
&mut self,
parameter: Box<dyn parameters::Parameter<f64>>,
) -> Result<ParameterIndex, PywrError> {
if let Ok(idx) = self.get_parameter_index_by_name(&parameter.meta().name) {
return Err(PywrError::ParameterNameAlreadyExists(
parameter.meta().name.to_string(),
Expand All @@ -1332,7 +1338,7 @@ impl Network {
/// Add a `parameters::IndexParameter` to the network
pub fn add_index_parameter(
&mut self,
index_parameter: Box<dyn parameters::IndexParameter>,
index_parameter: Box<dyn parameters::Parameter<usize>>,
) -> Result<IndexParameterIndex, PywrError> {
if let Ok(idx) = self.get_index_parameter_index_by_name(&index_parameter.meta().name) {
return Err(PywrError::IndexParameterNameAlreadyExists(
Expand All @@ -1353,7 +1359,7 @@ impl Network {
/// Add a `parameters::MultiValueParameter` to the network
pub fn add_multi_value_parameter(
&mut self,
parameter: Box<dyn parameters::MultiValueParameter>,
parameter: Box<dyn parameters::Parameter<MultiValue>>,
) -> Result<MultiValueParameterIndex, PywrError> {
if let Ok(idx) = self.get_parameter_index_by_name(&parameter.meta().name) {
return Err(PywrError::ParameterNameAlreadyExists(
Expand Down
2 changes: 1 addition & 1 deletion pywr-core/src/parameters/aggregated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl AggregatedParameter {
}
}

impl Parameter for AggregatedParameter {
impl Parameter<f64> for AggregatedParameter {
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
Expand Down
9 changes: 7 additions & 2 deletions pywr-core/src/parameters/aggregated_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
///
use super::PywrError;
use crate::network::Network;
use crate::parameters::{IndexParameter, IndexValue, ParameterMeta};
use crate::parameters::{IndexValue, Parameter, ParameterMeta};
use crate::scenario::ScenarioIndex;
use crate::state::{ParameterState, State};
use crate::timestep::Timestep;
use std::any::Any;
use std::str::FromStr;

pub enum AggIndexFunc {
Expand Down Expand Up @@ -49,7 +50,11 @@ impl AggregatedIndexParameter {
}
}

impl IndexParameter for AggregatedIndexParameter {
impl Parameter<usize> for AggregatedIndexParameter {
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}

fn meta(&self) -> &ParameterMeta {
&self.meta
}
Expand Down
4 changes: 2 additions & 2 deletions pywr-core/src/parameters/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Array1Parameter {
}
}

impl Parameter for Array1Parameter {
impl Parameter<f64> for Array1Parameter {
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
Expand Down Expand Up @@ -66,7 +66,7 @@ impl Array2Parameter {
}
}

impl Parameter for Array2Parameter {
impl Parameter<f64> for Array2Parameter {
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
Expand Down
9 changes: 7 additions & 2 deletions pywr-core/src/parameters/asymmetric.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::network::Network;
use crate::parameters::{downcast_internal_state_mut, IndexParameter, IndexValue, ParameterMeta};
use crate::parameters::{downcast_internal_state_mut, IndexValue, Parameter, ParameterMeta};
use crate::scenario::ScenarioIndex;
use crate::state::{ParameterState, State};
use crate::timestep::Timestep;
use crate::PywrError;
use std::any::Any;

pub struct AsymmetricSwitchIndexParameter {
meta: ParameterMeta,
Expand All @@ -21,7 +22,11 @@ impl AsymmetricSwitchIndexParameter {
}
}

impl IndexParameter for AsymmetricSwitchIndexParameter {
impl Parameter<usize> for AsymmetricSwitchIndexParameter {
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}

fn meta(&self) -> &ParameterMeta {
&self.meta
}
Expand Down
2 changes: 1 addition & 1 deletion pywr-core/src/parameters/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl ConstantParameter {
}
}

impl Parameter for ConstantParameter {
impl Parameter<f64> for ConstantParameter {
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
Expand Down
9 changes: 7 additions & 2 deletions pywr-core/src/parameters/control_curves/apportion.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::metric::Metric;
use crate::network::Network;
use crate::parameters::{MultiValueParameter, ParameterMeta};
use crate::parameters::{Parameter, ParameterMeta};
use crate::scenario::ScenarioIndex;
use crate::state::{MultiValue, ParameterState, State};
use crate::timestep::Timestep;
use crate::PywrError;
use std::any::Any;
use std::collections::HashMap;

/// A parameter which divides a apportions a metric to an upper and lower amount based
Expand All @@ -31,7 +32,11 @@ impl ApportionParameter {
}
}

impl MultiValueParameter for ApportionParameter {
impl Parameter<MultiValue> for ApportionParameter {
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}

fn meta(&self) -> &ParameterMeta {
&self.meta
}
Expand Down
9 changes: 7 additions & 2 deletions pywr-core/src/parameters/control_curves/index.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::metric::Metric;
use crate::network::Network;
use crate::parameters::{IndexParameter, ParameterMeta};
use crate::parameters::{Parameter, ParameterMeta};
use crate::scenario::ScenarioIndex;
use crate::state::{ParameterState, State};
use crate::timestep::Timestep;
use crate::PywrError;
use std::any::Any;

pub struct ControlCurveIndexParameter {
meta: ParameterMeta,
Expand All @@ -22,7 +23,11 @@ impl ControlCurveIndexParameter {
}
}

impl IndexParameter for ControlCurveIndexParameter {
impl Parameter<usize> for ControlCurveIndexParameter {
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}

fn meta(&self) -> &ParameterMeta {
&self.meta
}
Expand Down
2 changes: 1 addition & 1 deletion pywr-core/src/parameters/control_curves/interpolated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl ControlCurveInterpolatedParameter {
}
}

impl Parameter for ControlCurveInterpolatedParameter {
impl Parameter<f64> for ControlCurveInterpolatedParameter {
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
Expand Down
2 changes: 1 addition & 1 deletion pywr-core/src/parameters/control_curves/piecewise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl PiecewiseInterpolatedParameter {
}
}

impl Parameter for PiecewiseInterpolatedParameter {
impl Parameter<f64> for PiecewiseInterpolatedParameter {
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
Expand Down
2 changes: 1 addition & 1 deletion pywr-core/src/parameters/control_curves/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl ControlCurveParameter {
}
}

impl Parameter for ControlCurveParameter {
impl Parameter<f64> for ControlCurveParameter {
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
Expand Down
2 changes: 1 addition & 1 deletion pywr-core/src/parameters/control_curves/volume_between.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl VolumeBetweenControlCurvesParameter {
}
}

impl Parameter for VolumeBetweenControlCurvesParameter {
impl Parameter<f64> for VolumeBetweenControlCurvesParameter {
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
Expand Down
2 changes: 1 addition & 1 deletion pywr-core/src/parameters/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl DelayParameter {
}
}

impl Parameter for DelayParameter {
impl Parameter<f64> for DelayParameter {
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
Expand Down
2 changes: 1 addition & 1 deletion pywr-core/src/parameters/discount_factor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl DiscountFactorParameter {
}
}

impl Parameter for DiscountFactorParameter {
impl Parameter<f64> for DiscountFactorParameter {
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
Expand Down
2 changes: 1 addition & 1 deletion pywr-core/src/parameters/division.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl DivisionParameter {
}
}

impl Parameter for DivisionParameter {
impl Parameter<f64> for DivisionParameter {
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
Expand Down
2 changes: 1 addition & 1 deletion pywr-core/src/parameters/indexed_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl IndexedArrayParameter {
}
}

impl Parameter for IndexedArrayParameter {
impl Parameter<f64> for IndexedArrayParameter {
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
Expand Down
2 changes: 1 addition & 1 deletion pywr-core/src/parameters/interpolated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl InterpolatedParameter {
}
}

impl Parameter for InterpolatedParameter {
impl Parameter<f64> for InterpolatedParameter {
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
Expand Down
2 changes: 1 addition & 1 deletion pywr-core/src/parameters/max.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl MaxParameter {
}
}

impl Parameter for MaxParameter {
impl Parameter<f64> for MaxParameter {
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
Expand Down
2 changes: 1 addition & 1 deletion pywr-core/src/parameters/min.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl MinParameter {
}
}

impl Parameter for MinParameter {
impl Parameter<f64> for MinParameter {
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
Expand Down
Loading

0 comments on commit 4c86c45

Please sign in to comment.