diff --git a/pywr-core/src/lib.rs b/pywr-core/src/lib.rs index 2b3ed952..764a5988 100644 --- a/pywr-core/src/lib.rs +++ b/pywr-core/src/lib.rs @@ -5,8 +5,9 @@ extern crate core; use crate::derived_metric::DerivedMetricIndex; use crate::models::MultiNetworkTransferIndex; use crate::node::NodeIndex; -use crate::parameters::{IndexParameterIndex, InterpolationError, MultiValueParameterIndex, ParameterIndex}; +use crate::parameters::{InterpolationError, ParameterIndex}; use crate::recorders::{AggregationError, MetricSetIndex, RecorderIndex}; +use crate::state::MultiValue; use crate::virtual_storage::VirtualStorageIndex; use pyo3::exceptions::{PyException, PyRuntimeError}; use pyo3::{create_exception, PyErr}; @@ -44,11 +45,11 @@ pub enum PywrError { #[error("virtual storage index {0} not found")] VirtualStorageIndexNotFound(VirtualStorageIndex), #[error("parameter index {0} not found")] - ParameterIndexNotFound(ParameterIndex), + ParameterIndexNotFound(ParameterIndex), #[error("index parameter index {0} not found")] - IndexParameterIndexNotFound(IndexParameterIndex), + IndexParameterIndexNotFound(ParameterIndex), #[error("multi1 value parameter index {0} not found")] - MultiValueParameterIndexNotFound(MultiValueParameterIndex), + MultiValueParameterIndexNotFound(ParameterIndex), #[error("multi1 value parameter key {0} not found")] MultiValueParameterKeyNotFound(String), #[error("inter-network parameter state not initialised")] @@ -72,9 +73,9 @@ pub enum PywrError { #[error("node name `{0}` already exists")] NodeNameAlreadyExists(String), #[error("parameter name `{0}` already exists at index {1}")] - ParameterNameAlreadyExists(String, ParameterIndex), + ParameterNameAlreadyExists(String, ParameterIndex), #[error("index parameter name `{0}` already exists at index {1}")] - IndexParameterNameAlreadyExists(String, IndexParameterIndex), + IndexParameterNameAlreadyExists(String, ParameterIndex), #[error("metric set name `{0}` already exists")] MetricSetNameAlreadyExists(String), #[error("recorder name `{0}` already exists at index {1}")] @@ -158,7 +159,7 @@ pub enum PywrError { #[error("parameters do not provide an initial value")] ParameterNoInitialValue, #[error("parameter state not found for parameter index {0}")] - ParameterStateNotFound(ParameterIndex), + ParameterStateNotFound(ParameterIndex), #[error("Could not create timestep range due to following error: {0}")] TimestepRangeGenerationError(String), #[error("Could not create timesteps for frequency '{0}'")] diff --git a/pywr-core/src/metric.rs b/pywr-core/src/metric.rs index 82e88ea6..daa7ad0a 100644 --- a/pywr-core/src/metric.rs +++ b/pywr-core/src/metric.rs @@ -5,8 +5,8 @@ use crate::edge::EdgeIndex; use crate::models::MultiNetworkTransferIndex; use crate::network::Network; use crate::node::NodeIndex; -use crate::parameters::{IndexParameterIndex, MultiValueParameterIndex, ParameterIndex}; -use crate::state::State; +use crate::parameters::ParameterIndex; +use crate::state::{MultiValue, State}; use crate::virtual_storage::VirtualStorageIndex; use crate::PywrError; #[derive(Clone, Debug, PartialEq)] @@ -18,8 +18,8 @@ pub enum Metric { AggregatedNodeOutFlow(AggregatedNodeIndex), AggregatedNodeVolume(AggregatedStorageNodeIndex), EdgeFlow(EdgeIndex), - ParameterValue(ParameterIndex), - MultiParameterValue((MultiValueParameterIndex, String)), + ParameterValue(ParameterIndex), + MultiParameterValue((ParameterIndex, String)), VirtualStorageVolume(VirtualStorageIndex), MultiNodeInFlow { indices: Vec, name: String }, MultiNodeOutFlow { indices: Vec, name: String }, @@ -87,7 +87,7 @@ impl Metric { #[derive(Clone, Debug, PartialEq)] pub enum IndexMetric { - IndexParameterValue(IndexParameterIndex), + IndexParameterValue(ParameterIndex), Constant(usize), } diff --git a/pywr-core/src/network.rs b/pywr-core/src/network.rs index de960951..0d6a3782 100644 --- a/pywr-core/src/network.rs +++ b/pywr-core/src/network.rs @@ -5,14 +5,14 @@ use crate::edge::{EdgeIndex, EdgeVec}; use crate::metric::Metric; use crate::models::ModelDomain; use crate::node::{ConstraintValue, Node, NodeVec, StorageInitialVolume}; -use crate::parameters::{MultiValueParameterIndex, ParameterType, VariableConfig}; +use crate::parameters::{ParameterType, VariableConfig}; use crate::recorders::{MetricSet, MetricSetIndex, MetricSetState}; use crate::scenario::ScenarioIndex; use crate::solvers::{MultiStateSolver, Solver, SolverFeatures, SolverTimings}; 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}; +use crate::{parameters, recorders, NodeIndex, ParameterIndex, PywrError, RecorderIndex}; use rayon::prelude::*; use std::any::Any; use std::collections::HashSet; @@ -1089,7 +1089,7 @@ 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, PywrError> { match self.parameters.get(*index.deref()) { Some(p) => Ok(p.as_ref()), None => Err(PywrError::ParameterIndexNotFound(*index)), @@ -1099,7 +1099,7 @@ impl Network { /// Get a `Parameter` from a parameter's name pub fn get_mut_parameter( &mut self, - index: &ParameterIndex, + index: &ParameterIndex, ) -> Result<&mut dyn parameters::Parameter, PywrError> { match self.parameters.get_mut(*index.deref()) { Some(p) => Ok(p.as_mut()), @@ -1116,7 +1116,7 @@ impl Network { } /// Get a `ParameterIndex` from a parameter's name - pub fn get_parameter_index_by_name(&self, name: &str) -> Result { + pub fn get_parameter_index_by_name(&self, name: &str) -> Result, PywrError> { match self.parameters.iter().position(|p| p.name() == name) { Some(idx) => Ok(ParameterIndex::new(idx)), None => Err(PywrError::ParameterNotFound(name.to_string())), @@ -1132,17 +1132,20 @@ impl Network { } /// Get a `IndexParameterIndex` from a parameter's name - pub fn get_index_parameter_index_by_name(&self, name: &str) -> Result { + pub fn get_index_parameter_index_by_name(&self, name: &str) -> Result, PywrError> { match self.index_parameters.iter().position(|p| p.name() == name) { - Some(idx) => Ok(IndexParameterIndex::new(idx)), + Some(idx) => Ok(ParameterIndex::new(idx)), None => Err(PywrError::ParameterNotFound(name.to_string())), } } /// Get a `MultiValueParameterIndex` from a parameter's name - pub fn get_multi_valued_parameter_index_by_name(&self, name: &str) -> Result { + pub fn get_multi_valued_parameter_index_by_name( + &self, + name: &str, + ) -> Result, PywrError> { match self.multi_parameters.iter().position(|p| p.name() == name) { - Some(idx) => Ok(MultiValueParameterIndex::new(idx)), + Some(idx) => Ok(ParameterIndex::new(idx)), None => Err(PywrError::ParameterNotFound(name.to_string())), } } @@ -1317,7 +1320,7 @@ impl Network { pub fn add_parameter( &mut self, parameter: Box>, - ) -> Result { + ) -> Result, PywrError> { if let Ok(idx) = self.get_parameter_index_by_name(¶meter.meta().name) { return Err(PywrError::ParameterNameAlreadyExists( parameter.meta().name.to_string(), @@ -1339,7 +1342,7 @@ impl Network { pub fn add_index_parameter( &mut self, index_parameter: Box>, - ) -> Result { + ) -> Result, PywrError> { if let Ok(idx) = self.get_index_parameter_index_by_name(&index_parameter.meta().name) { return Err(PywrError::IndexParameterNameAlreadyExists( index_parameter.meta().name.to_string(), @@ -1347,7 +1350,7 @@ impl Network { )); } - let parameter_index = IndexParameterIndex::new(self.index_parameters.len()); + let parameter_index = ParameterIndex::new(self.index_parameters.len()); self.index_parameters.push(index_parameter); // .. and add it to the resolve order @@ -1360,7 +1363,7 @@ impl Network { pub fn add_multi_value_parameter( &mut self, parameter: Box>, - ) -> Result { + ) -> Result, PywrError> { if let Ok(idx) = self.get_parameter_index_by_name(¶meter.meta().name) { return Err(PywrError::ParameterNameAlreadyExists( parameter.meta().name.to_string(), @@ -1368,7 +1371,7 @@ impl Network { )); } - let parameter_index = MultiValueParameterIndex::new(self.multi_parameters.len()); + let parameter_index = ParameterIndex::new(self.multi_parameters.len()); // Add the parameter ... self.multi_parameters.push(parameter); @@ -1457,7 +1460,7 @@ impl Network { /// This will update the internal state of the parameter with the new values for all scenarios. pub fn set_f64_parameter_variable_values( &self, - parameter_index: ParameterIndex, + parameter_index: ParameterIndex, values: &[f64], variable_config: &dyn VariableConfig, state: &mut NetworkState, @@ -1487,7 +1490,7 @@ impl Network { /// Only the internal state of the parameter for the given scenario will be updated. pub fn set_f64_parameter_variable_values_for_scenario( &self, - parameter_index: ParameterIndex, + parameter_index: ParameterIndex, scenario_index: ScenarioIndex, values: &[f64], variable_config: &dyn VariableConfig, @@ -1511,7 +1514,7 @@ impl Network { /// Return a vector of the current values of active variable parameters. pub fn get_f64_parameter_variable_values_for_scenario( &self, - parameter_index: ParameterIndex, + parameter_index: ParameterIndex, scenario_index: ScenarioIndex, state: &NetworkState, ) -> Result>, PywrError> { @@ -1533,7 +1536,7 @@ impl Network { pub fn get_f64_parameter_variable_values( &self, - parameter_index: ParameterIndex, + parameter_index: ParameterIndex, state: &NetworkState, ) -> Result>>, PywrError> { match self.parameters.get(*parameter_index.deref()) { @@ -1563,7 +1566,7 @@ impl Network { /// This will update the internal state of the parameter with the new values for scenarios. pub fn set_u32_parameter_variable_values( &self, - parameter_index: ParameterIndex, + parameter_index: ParameterIndex, values: &[u32], variable_config: &dyn VariableConfig, state: &mut NetworkState, @@ -1593,7 +1596,7 @@ impl Network { /// Only the internal state of the parameter for the given scenario will be updated. pub fn set_u32_parameter_variable_values_for_scenario( &self, - parameter_index: ParameterIndex, + parameter_index: ParameterIndex, scenario_index: ScenarioIndex, values: &[u32], variable_config: &dyn VariableConfig, @@ -1617,7 +1620,7 @@ impl Network { /// Return a vector of the current values of active variable parameters. pub fn get_u32_parameter_variable_values_for_scenario( &self, - parameter_index: ParameterIndex, + parameter_index: ParameterIndex, scenario_index: ScenarioIndex, state: &NetworkState, ) -> Result>, PywrError> { diff --git a/pywr-core/src/parameters/mod.rs b/pywr-core/src/parameters/mod.rs index 289b8fc6..5243758b 100644 --- a/pywr-core/src/parameters/mod.rs +++ b/pywr-core/src/parameters/mod.rs @@ -28,7 +28,7 @@ pub use self::rhai::RhaiParameter; use super::PywrError; use crate::network::Network; use crate::scenario::ScenarioIndex; -use crate::state::{ParameterState, State}; +use crate::state::{MultiValue, ParameterState, State}; use crate::timestep::Timestep; pub use activation_function::ActivationFunction; pub use aggregated::{AggFunc, AggregatedParameter}; @@ -59,76 +59,59 @@ pub use profiles::{ pub use py::PyParameter; use std::fmt; use std::fmt::{Display, Formatter}; +use std::marker::PhantomData; use std::ops::Deref; pub use threshold::{Predicate, ThresholdParameter}; pub use vector::VectorParameter; -#[derive(Debug, Copy, Clone, Eq, PartialEq)] -pub struct ParameterIndex(usize); - -#[derive(Debug, Copy, Clone, Eq, PartialEq)] -pub struct IndexParameterIndex(usize); - -#[derive(Debug, Copy, Clone, Eq, PartialEq)] -pub struct MultiValueParameterIndex(usize); - -impl ParameterIndex { - pub fn new(idx: usize) -> Self { - Self(idx) - } -} - -impl IndexParameterIndex { - pub fn new(idx: usize) -> Self { - Self(idx) - } +/// Generic parameter index. +/// +/// This is a wrapper around usize that is used to index parameters in the state. It is +/// generic over the type of the value that the parameter returns. +#[derive(Debug)] +pub struct ParameterIndex { + idx: usize, + phantom: PhantomData, } -impl MultiValueParameterIndex { - pub fn new(idx: usize) -> Self { - Self(idx) +// These implementations are required because the derive macro does not work well with PhantomData. +// See issue: https://github.com/rust-lang/rust/issues/26925 +impl Clone for ParameterIndex { + fn clone(&self) -> Self { + *self } } -impl Deref for ParameterIndex { - type Target = usize; +impl Copy for ParameterIndex {} - fn deref(&self) -> &Self::Target { - &self.0 +impl PartialEq for ParameterIndex { + fn eq(&self, other: &Self) -> bool { + self.idx == other.idx } } -impl Deref for IndexParameterIndex { - type Target = usize; +impl Eq for ParameterIndex {} - fn deref(&self) -> &Self::Target { - &self.0 +impl ParameterIndex { + pub fn new(idx: usize) -> Self { + Self { + idx, + phantom: PhantomData, + } } } -impl Deref for MultiValueParameterIndex { +impl Deref for ParameterIndex { type Target = usize; fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl Display for ParameterIndex { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.0) - } -} - -impl Display for IndexParameterIndex { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.0) + &self.idx } } -impl Display for MultiValueParameterIndex { +impl Display for ParameterIndex { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.0) + write!(f, "{}", self.idx) } } @@ -271,7 +254,7 @@ pub trait Parameter: Send + Sync { #[derive(Copy, Clone)] pub enum IndexValue { Constant(usize), - Dynamic(IndexParameterIndex), + Dynamic(ParameterIndex), } impl IndexValue { @@ -284,9 +267,9 @@ impl IndexValue { } pub enum ParameterType { - Parameter(ParameterIndex), - Index(IndexParameterIndex), - Multi(MultiValueParameterIndex), + Parameter(ParameterIndex), + Index(ParameterIndex), + Multi(ParameterIndex), } /// A parameter that can be optimised. diff --git a/pywr-core/src/state.rs b/pywr-core/src/state.rs index 6f896b6e..194beeae 100644 --- a/pywr-core/src/state.rs +++ b/pywr-core/src/state.rs @@ -3,7 +3,7 @@ use crate::edge::{Edge, EdgeIndex}; use crate::models::MultiNetworkTransferIndex; use crate::network::Network; use crate::node::{Node, NodeIndex}; -use crate::parameters::{IndexParameterIndex, MultiValueParameterIndex, ParameterIndex}; +use crate::parameters::ParameterIndex; use crate::timestep::Timestep; use crate::virtual_storage::VirtualStorageIndex; use crate::PywrError; @@ -271,27 +271,30 @@ impl ParameterStates { } } - pub fn get_value_state(&self, index: ParameterIndex) -> Option<&Option>> { + pub fn get_value_state(&self, index: ParameterIndex) -> Option<&Option>> { self.values.get(*index.deref()) } - pub fn get_mut_value_state(&mut self, index: ParameterIndex) -> Option<&mut Option>> { + pub fn get_mut_value_state(&mut self, index: ParameterIndex) -> Option<&mut Option>> { self.values.get_mut(*index.deref()) } - pub fn get_mut_index_state(&mut self, index: IndexParameterIndex) -> Option<&mut Option>> { + pub fn get_mut_index_state( + &mut self, + index: ParameterIndex, + ) -> Option<&mut Option>> { self.indices.get_mut(*index.deref()) } pub fn get_mut_multi_state( &mut self, - index: MultiValueParameterIndex, + index: ParameterIndex, ) -> Option<&mut Option>> { self.multi.get_mut(*index.deref()) } } -#[derive(Debug, Default, Clone)] +#[derive(Debug, Default, Clone, PartialEq)] pub struct MultiValue { values: HashMap, indices: HashMap, @@ -328,14 +331,14 @@ impl ParameterValues { } } - fn get_value(&self, idx: ParameterIndex) -> Result { + fn get_value(&self, idx: ParameterIndex) -> Result { match self.values.get(*idx.deref()) { Some(s) => Ok(*s), None => Err(PywrError::ParameterIndexNotFound(idx)), } } - fn set_value(&mut self, idx: ParameterIndex, value: f64) -> Result<(), PywrError> { + fn set_value(&mut self, idx: ParameterIndex, value: f64) -> Result<(), PywrError> { match self.values.get_mut(*idx.deref()) { Some(s) => { *s = value; @@ -345,14 +348,14 @@ impl ParameterValues { } } - fn get_index(&self, idx: IndexParameterIndex) -> Result { + fn get_index(&self, idx: ParameterIndex) -> Result { match self.indices.get(*idx.deref()) { Some(s) => Ok(*s), None => Err(PywrError::IndexParameterIndexNotFound(idx)), } } - fn set_index(&mut self, idx: IndexParameterIndex, value: usize) -> Result<(), PywrError> { + fn set_index(&mut self, idx: ParameterIndex, value: usize) -> Result<(), PywrError> { match self.indices.get_mut(*idx.deref()) { Some(s) => { *s = value; @@ -362,7 +365,7 @@ impl ParameterValues { } } - fn get_multi_value(&self, idx: MultiValueParameterIndex, key: &str) -> Result { + fn get_multi_value(&self, idx: ParameterIndex, key: &str) -> Result { match self.multi_values.get(*idx.deref()) { Some(s) => match s.get_value(key) { Some(v) => Ok(*v), @@ -372,7 +375,7 @@ impl ParameterValues { } } - fn set_multi_value(&mut self, idx: MultiValueParameterIndex, value: MultiValue) -> Result<(), PywrError> { + fn set_multi_value(&mut self, idx: ParameterIndex, value: MultiValue) -> Result<(), PywrError> { match self.multi_values.get_mut(*idx.deref()) { Some(s) => { *s = value; @@ -382,7 +385,7 @@ impl ParameterValues { } } - fn get_multi_index(&self, idx: MultiValueParameterIndex, key: &str) -> Result { + fn get_multi_index(&self, idx: ParameterIndex, key: &str) -> Result { match self.multi_values.get(*idx.deref()) { Some(s) => match s.get_index(key) { Some(v) => Ok(*v), @@ -637,35 +640,35 @@ impl State { &mut self.network } - pub fn get_parameter_value(&self, idx: ParameterIndex) -> Result { + pub fn get_parameter_value(&self, idx: ParameterIndex) -> Result { self.parameters.get_value(idx) } - pub fn set_parameter_value(&mut self, idx: ParameterIndex, value: f64) -> Result<(), PywrError> { + pub fn set_parameter_value(&mut self, idx: ParameterIndex, value: f64) -> Result<(), PywrError> { self.parameters.set_value(idx, value) } - pub fn get_parameter_index(&self, idx: IndexParameterIndex) -> Result { + pub fn get_parameter_index(&self, idx: ParameterIndex) -> Result { self.parameters.get_index(idx) } - pub fn set_parameter_index(&mut self, idx: IndexParameterIndex, value: usize) -> Result<(), PywrError> { + pub fn set_parameter_index(&mut self, idx: ParameterIndex, value: usize) -> Result<(), PywrError> { self.parameters.set_index(idx, value) } - pub fn get_multi_parameter_value(&self, idx: MultiValueParameterIndex, key: &str) -> Result { + pub fn get_multi_parameter_value(&self, idx: ParameterIndex, key: &str) -> Result { self.parameters.get_multi_value(idx, key) } pub fn set_multi_parameter_value( &mut self, - idx: MultiValueParameterIndex, + idx: ParameterIndex, value: MultiValue, ) -> Result<(), PywrError> { self.parameters.set_multi_value(idx, value) } - pub fn get_multi_parameter_index(&self, idx: MultiValueParameterIndex, key: &str) -> Result { + pub fn get_multi_parameter_index(&self, idx: ParameterIndex, key: &str) -> Result { self.parameters.get_multi_index(idx, key) } diff --git a/pywr-schema/src/parameters/aggregated.rs b/pywr-schema/src/parameters/aggregated.rs index 945a1543..022449c6 100644 --- a/pywr-schema/src/parameters/aggregated.rs +++ b/pywr-schema/src/parameters/aggregated.rs @@ -6,7 +6,7 @@ use crate::parameters::{ TryIntoV2Parameter, }; use pywr_core::models::ModelDomain; -use pywr_core::parameters::{IndexParameterIndex, ParameterIndex}; +use pywr_core::parameters::ParameterIndex; use pywr_v1_schema::parameters::{ AggFunc as AggFuncV1, AggregatedIndexParameter as AggregatedIndexParameterV1, AggregatedParameter as AggregatedParameterV1, IndexAggFunc as IndexAggFuncV1, @@ -99,7 +99,7 @@ impl AggregatedParameter { tables: &LoadedTableCollection, data_path: Option<&Path>, inter_network_transfers: &[PywrMultiNetworkTransfer], - ) -> Result { + ) -> Result, SchemaError> { let metrics = self .metrics .iter() @@ -206,7 +206,7 @@ impl AggregatedIndexParameter { tables: &LoadedTableCollection, data_path: Option<&Path>, inter_network_transfers: &[PywrMultiNetworkTransfer], - ) -> Result { + ) -> Result, SchemaError> { let parameters = self .parameters .iter() diff --git a/pywr-schema/src/parameters/asymmetric_switch.rs b/pywr-schema/src/parameters/asymmetric_switch.rs index 554b4506..3943e2c3 100644 --- a/pywr-schema/src/parameters/asymmetric_switch.rs +++ b/pywr-schema/src/parameters/asymmetric_switch.rs @@ -5,7 +5,7 @@ use crate::parameters::{ DynamicFloatValueType, DynamicIndexValue, IntoV2Parameter, ParameterMeta, TryFromV1Parameter, TryIntoV2Parameter, }; use pywr_core::models::ModelDomain; -use pywr_core::parameters::IndexParameterIndex; +use pywr_core::parameters::ParameterIndex; use pywr_v1_schema::parameters::AsymmetricSwitchIndexParameter as AsymmetricSwitchIndexParameterV1; use std::collections::HashMap; use std::path::Path; @@ -34,7 +34,7 @@ impl AsymmetricSwitchIndexParameter { tables: &LoadedTableCollection, data_path: Option<&Path>, inter_network_transfers: &[PywrMultiNetworkTransfer], - ) -> Result { + ) -> Result, SchemaError> { let on_index_parameter = self.on_index_parameter .load(network, schema, domain, tables, data_path, inter_network_transfers)?; diff --git a/pywr-schema/src/parameters/control_curves.rs b/pywr-schema/src/parameters/control_curves.rs index 9e3477b2..a7f722d0 100644 --- a/pywr-schema/src/parameters/control_curves.rs +++ b/pywr-schema/src/parameters/control_curves.rs @@ -6,7 +6,7 @@ use crate::parameters::{ DynamicFloatValue, IntoV2Parameter, NodeReference, ParameterMeta, TryFromV1Parameter, TryIntoV2Parameter, }; use pywr_core::models::ModelDomain; -use pywr_core::parameters::{IndexParameterIndex, ParameterIndex}; +use pywr_core::parameters::ParameterIndex; use pywr_v1_schema::parameters::{ ControlCurveIndexParameter as ControlCurveIndexParameterV1, ControlCurveInterpolatedParameter as ControlCurveInterpolatedParameterV1, @@ -33,7 +33,7 @@ impl ControlCurveInterpolatedParameter { tables: &LoadedTableCollection, data_path: Option<&Path>, inter_network_transfers: &[PywrMultiNetworkTransfer], - ) -> Result { + ) -> Result, SchemaError> { let metric = self.storage_node.load(network, schema)?; let control_curves = self @@ -136,7 +136,7 @@ impl ControlCurveIndexParameter { tables: &LoadedTableCollection, data_path: Option<&Path>, inter_network_transfers: &[PywrMultiNetworkTransfer], - ) -> Result { + ) -> Result, SchemaError> { let metric = self.storage_node.load(network, schema)?; let control_curves = self @@ -247,7 +247,7 @@ impl ControlCurveParameter { tables: &LoadedTableCollection, data_path: Option<&Path>, inter_network_transfers: &[PywrMultiNetworkTransfer], - ) -> Result { + ) -> Result, SchemaError> { let metric = self.storage_node.load(network, schema)?; let control_curves = self @@ -341,7 +341,7 @@ impl ControlCurvePiecewiseInterpolatedParameter { tables: &LoadedTableCollection, data_path: Option<&Path>, inter_network_transfers: &[PywrMultiNetworkTransfer], - ) -> Result { + ) -> Result, SchemaError> { let metric = self.storage_node.load(network, schema)?; let control_curves = self diff --git a/pywr-schema/src/parameters/core.rs b/pywr-schema/src/parameters/core.rs index a2f0c456..7eac46e0 100644 --- a/pywr-schema/src/parameters/core.rs +++ b/pywr-schema/src/parameters/core.rs @@ -170,7 +170,7 @@ impl ConstantParameter { &self, network: &mut pywr_core::network::Network, tables: &LoadedTableCollection, - ) -> Result { + ) -> Result, SchemaError> { let p = pywr_core::parameters::ConstantParameter::new(&self.meta.name, self.value.load(tables)?); Ok(network.add_parameter(Box::new(p))?) } @@ -226,7 +226,7 @@ impl MaxParameter { tables: &LoadedTableCollection, data_path: Option<&Path>, inter_network_transfers: &[PywrMultiNetworkTransfer], - ) -> Result { + ) -> Result, SchemaError> { let idx = self .parameter .load(network, schema, domain, tables, data_path, inter_network_transfers)?; @@ -301,7 +301,7 @@ impl DivisionParameter { tables: &LoadedTableCollection, data_path: Option<&Path>, inter_network_transfers: &[PywrMultiNetworkTransfer], - ) -> Result { + ) -> Result, SchemaError> { let n = self .numerator .load(network, schema, domain, tables, data_path, inter_network_transfers)?; @@ -376,7 +376,7 @@ impl MinParameter { tables: &LoadedTableCollection, data_path: Option<&Path>, inter_network_transfers: &[PywrMultiNetworkTransfer], - ) -> Result { + ) -> Result, SchemaError> { let idx = self .parameter .load(network, schema, domain, tables, data_path, inter_network_transfers)?; @@ -433,7 +433,7 @@ impl NegativeParameter { tables: &LoadedTableCollection, data_path: Option<&Path>, inter_network_transfers: &[PywrMultiNetworkTransfer], - ) -> Result { + ) -> Result, SchemaError> { let idx = self .parameter .load(network, schema, domain, tables, data_path, inter_network_transfers)?; diff --git a/pywr-schema/src/parameters/data_frame.rs b/pywr-schema/src/parameters/data_frame.rs index 57be6a7d..d0ce6a9e 100644 --- a/pywr-schema/src/parameters/data_frame.rs +++ b/pywr-schema/src/parameters/data_frame.rs @@ -75,7 +75,7 @@ impl DataFrameParameter { network: &mut pywr_core::network::Network, domain: &ModelDomain, data_path: Option<&Path>, - ) -> Result { + ) -> Result, SchemaError> { // Handle the case of an optional data path with a relative url. let pth = if let Some(dp) = data_path { if self.url.is_relative() { diff --git a/pywr-schema/src/parameters/delay.rs b/pywr-schema/src/parameters/delay.rs index 79021b85..de891c36 100644 --- a/pywr-schema/src/parameters/delay.rs +++ b/pywr-schema/src/parameters/delay.rs @@ -39,7 +39,7 @@ impl DelayParameter { tables: &LoadedTableCollection, data_path: Option<&Path>, inter_network_transfers: &[PywrMultiNetworkTransfer], - ) -> Result { + ) -> Result, SchemaError> { let metric = self .metric .load(network, schema, domain, tables, data_path, inter_network_transfers)?; diff --git a/pywr-schema/src/parameters/discount_factor.rs b/pywr-schema/src/parameters/discount_factor.rs index bef78112..0ab9a321 100644 --- a/pywr-schema/src/parameters/discount_factor.rs +++ b/pywr-schema/src/parameters/discount_factor.rs @@ -40,7 +40,7 @@ impl DiscountFactorParameter { tables: &LoadedTableCollection, data_path: Option<&Path>, inter_network_transfers: &[PywrMultiNetworkTransfer], - ) -> Result { + ) -> Result, SchemaError> { let discount_rate = self.discount_rate .load(network, schema, domain, tables, data_path, inter_network_transfers)?; diff --git a/pywr-schema/src/parameters/indexed_array.rs b/pywr-schema/src/parameters/indexed_array.rs index 3534db62..af1fb583 100644 --- a/pywr-schema/src/parameters/indexed_array.rs +++ b/pywr-schema/src/parameters/indexed_array.rs @@ -42,7 +42,7 @@ impl IndexedArrayParameter { tables: &LoadedTableCollection, data_path: Option<&Path>, inter_network_transfers: &[PywrMultiNetworkTransfer], - ) -> Result { + ) -> Result, SchemaError> { let index_parameter = self.index_parameter .load(network, schema, domain, tables, data_path, inter_network_transfers)?; diff --git a/pywr-schema/src/parameters/interpolated.rs b/pywr-schema/src/parameters/interpolated.rs index 68bf67d2..b8de7960 100644 --- a/pywr-schema/src/parameters/interpolated.rs +++ b/pywr-schema/src/parameters/interpolated.rs @@ -58,7 +58,7 @@ impl InterpolatedParameter { tables: &LoadedTableCollection, data_path: Option<&Path>, inter_network_transfers: &[PywrMultiNetworkTransfer], - ) -> Result { + ) -> Result, SchemaError> { let x = self .x .load(network, schema, domain, tables, data_path, inter_network_transfers)?; diff --git a/pywr-schema/src/parameters/mod.rs b/pywr-schema/src/parameters/mod.rs index af362eed..83e313d0 100644 --- a/pywr-schema/src/parameters/mod.rs +++ b/pywr-schema/src/parameters/mod.rs @@ -53,7 +53,7 @@ use crate::parameters::interpolated::InterpolatedParameter; pub use offset::OffsetParameter; use pywr_core::metric::Metric; use pywr_core::models::{ModelDomain, MultiNetworkTransferIndex}; -use pywr_core::parameters::{IndexParameterIndex, IndexValue, ParameterType}; +use pywr_core::parameters::{IndexValue, ParameterIndex, ParameterType}; use pywr_v1_schema::parameters::{ CoreParameter, ExternalDataRef as ExternalDataRefV1, Parameter as ParameterV1, ParameterMeta as ParameterMetaV1, ParameterValue as ParameterValueV1, TableIndex as TableIndexV1, TableIndexEntry as TableIndexEntryV1, @@ -718,7 +718,7 @@ impl ParameterIndexValue { tables: &LoadedTableCollection, data_path: Option<&Path>, inter_network_transfers: &[PywrMultiNetworkTransfer], - ) -> Result { + ) -> Result, SchemaError> { match self { Self::Reference(name) => { // This should be an existing parameter diff --git a/pywr-schema/src/parameters/offset.rs b/pywr-schema/src/parameters/offset.rs index 43d58440..3e35df93 100644 --- a/pywr-schema/src/parameters/offset.rs +++ b/pywr-schema/src/parameters/offset.rs @@ -55,7 +55,7 @@ impl OffsetParameter { tables: &LoadedTableCollection, data_path: Option<&Path>, inter_network_transfers: &[PywrMultiNetworkTransfer], - ) -> Result { + ) -> Result, SchemaError> { let idx = self .metric .load(network, schema, domain, tables, data_path, inter_network_transfers)?; diff --git a/pywr-schema/src/parameters/polynomial.rs b/pywr-schema/src/parameters/polynomial.rs index 377058b3..674c35aa 100644 --- a/pywr-schema/src/parameters/polynomial.rs +++ b/pywr-schema/src/parameters/polynomial.rs @@ -23,7 +23,7 @@ impl Polynomial1DParameter { HashMap::new() } - pub fn add_to_model(&self, network: &mut pywr_core::network::Network) -> Result { + pub fn add_to_model(&self, network: &mut pywr_core::network::Network) -> Result, SchemaError> { let metric = network.get_storage_node_metric(&self.storage_node, None, self.use_proportional_volume.unwrap_or(true))?; diff --git a/pywr-schema/src/parameters/profiles.rs b/pywr-schema/src/parameters/profiles.rs index 0a24b895..78bc1c89 100644 --- a/pywr-schema/src/parameters/profiles.rs +++ b/pywr-schema/src/parameters/profiles.rs @@ -31,7 +31,7 @@ impl DailyProfileParameter { &self, network: &mut pywr_core::network::Network, tables: &LoadedTableCollection, - ) -> Result { + ) -> Result, SchemaError> { let values = &self.values.load(tables)?[..366]; let p = pywr_core::parameters::DailyProfileParameter::new(&self.meta.name, values.try_into().expect("")); Ok(network.add_parameter(Box::new(p))?) @@ -101,7 +101,7 @@ impl MonthlyProfileParameter { &self, network: &mut pywr_core::network::Network, tables: &LoadedTableCollection, - ) -> Result { + ) -> Result, SchemaError> { let values = &self.values.load(tables)?[..12]; let p = pywr_core::parameters::MonthlyProfileParameter::new( &self.meta.name, @@ -175,7 +175,7 @@ impl UniformDrawdownProfileParameter { &self, network: &mut pywr_core::network::Network, tables: &LoadedTableCollection, - ) -> Result { + ) -> Result, SchemaError> { let reset_day = match &self.reset_day { Some(v) => v.load(tables)? as u32, None => 1, @@ -363,7 +363,7 @@ impl RbfProfileParameter { HashMap::new() } - pub fn add_to_model(&self, network: &mut pywr_core::network::Network) -> Result { + pub fn add_to_model(&self, network: &mut pywr_core::network::Network) -> Result, SchemaError> { let function = self.function.into_core_rbf(&self.points)?; let p = pywr_core::parameters::RbfProfileParameter::new(&self.meta.name, self.points.clone(), function); @@ -545,7 +545,7 @@ impl WeeklyProfileParameter { &self, network: &mut pywr_core::network::Network, tables: &LoadedTableCollection, - ) -> Result { + ) -> Result, SchemaError> { let p = pywr_core::parameters::WeeklyProfileParameter::new( &self.meta.name, WeeklyProfileValues::try_from(self.values.load(tables)?.as_slice()).map_err( diff --git a/pywr-schema/src/parameters/tables.rs b/pywr-schema/src/parameters/tables.rs index 6fe16f8b..5e9da5d8 100644 --- a/pywr-schema/src/parameters/tables.rs +++ b/pywr-schema/src/parameters/tables.rs @@ -33,7 +33,7 @@ impl TablesArrayParameter { network: &mut pywr_core::network::Network, domain: &ModelDomain, data_path: Option<&Path>, - ) -> Result { + ) -> Result, SchemaError> { // 1. Load the file from the HDF5 file (NB this is not Pandas format). // Handle the case of an optional data path with a relative url. diff --git a/pywr-schema/src/parameters/thresholds.rs b/pywr-schema/src/parameters/thresholds.rs index e24ba041..3770c6d0 100644 --- a/pywr-schema/src/parameters/thresholds.rs +++ b/pywr-schema/src/parameters/thresholds.rs @@ -5,7 +5,7 @@ use crate::parameters::{ DynamicFloatValue, DynamicFloatValueType, IntoV2Parameter, ParameterMeta, TryFromV1Parameter, TryIntoV2Parameter, }; use pywr_core::models::ModelDomain; -use pywr_core::parameters::IndexParameterIndex; +use pywr_core::parameters::ParameterIndex; use pywr_v1_schema::parameters::{ ParameterThresholdParameter as ParameterThresholdParameterV1, Predicate as PredicateV1, }; @@ -77,7 +77,7 @@ impl ParameterThresholdParameter { tables: &LoadedTableCollection, data_path: Option<&Path>, inter_network_transfers: &[PywrMultiNetworkTransfer], - ) -> Result { + ) -> Result, SchemaError> { let metric = self .parameter .load(network, schema, domain, tables, data_path, inter_network_transfers)?;