-
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.
feat: Initial commit of DerivedMetric (#68)
A derived metric is updated after solve and is for values that could be dependent on state AND paramter values. These need updating and storing for use between time-steps. Adds a test for piecewise storage model that shows how the volume is updated at the end of time-step, and the derived proportional volume is calculated and retained for use in the next time-step despite the max volumes being updated. Fixes #63.
- Loading branch information
Showing
18 changed files
with
442 additions
and
143 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use crate::aggregated_storage_node::AggregatedStorageNodeIndex; | ||
use crate::model::Model; | ||
use crate::node::NodeIndex; | ||
use crate::state::State; | ||
use crate::timestep::Timestep; | ||
use crate::virtual_storage::VirtualStorageIndex; | ||
use crate::PywrError; | ||
use std::fmt; | ||
use std::fmt::{Display, Formatter}; | ||
use std::ops::Deref; | ||
|
||
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug)] | ||
pub struct DerivedMetricIndex(usize); | ||
|
||
impl Deref for DerivedMetricIndex { | ||
type Target = usize; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl DerivedMetricIndex { | ||
pub fn new(idx: usize) -> Self { | ||
Self(idx) | ||
} | ||
} | ||
|
||
impl Display for DerivedMetricIndex { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { | ||
write!(f, "{}", self.0) | ||
} | ||
} | ||
|
||
/// Derived metrics are updated after the model is solved. | ||
/// | ||
/// These metrics are "derived" from node states (e.g. volume, flow) and must be updated | ||
/// after those states have been updated. This should happen after the model is solved. The values | ||
/// are then available in this state for the next time-step. | ||
#[derive(Clone, Debug, PartialEq)] | ||
pub enum DerivedMetric { | ||
NodeInFlowDeficit(NodeIndex), | ||
NodeProportionalVolume(NodeIndex), | ||
AggregatedNodeProportionalVolume(AggregatedStorageNodeIndex), | ||
VirtualStorageProportionalVolume(VirtualStorageIndex), | ||
} | ||
|
||
impl DerivedMetric { | ||
pub fn before(&self, timestep: &Timestep, model: &Model, state: &State) -> Result<Option<f64>, PywrError> { | ||
// On the first time-step set the initial value | ||
if timestep.is_first() { | ||
self.compute(model, state).map(|v| Some(v)) | ||
} else { | ||
Ok(None) | ||
} | ||
} | ||
|
||
pub fn compute(&self, model: &Model, state: &State) -> Result<f64, PywrError> { | ||
match self { | ||
Self::NodeProportionalVolume(idx) => { | ||
let max_volume = model.get_node(idx)?.get_current_max_volume(model, state)?; | ||
Ok(state | ||
.get_network_state() | ||
.get_node_proportional_volume(idx, max_volume)?) | ||
} | ||
Self::VirtualStorageProportionalVolume(idx) => { | ||
let max_volume = model.get_virtual_storage_node(idx)?.get_max_volume(model, state)?; | ||
Ok(state | ||
.get_network_state() | ||
.get_virtual_storage_proportional_volume(idx, max_volume)?) | ||
} | ||
Self::AggregatedNodeProportionalVolume(idx) => { | ||
let node = model.get_aggregated_storage_node(idx)?; | ||
let volume: f64 = node | ||
.nodes | ||
.iter() | ||
.map(|idx| state.get_network_state().get_node_volume(idx)) | ||
.sum::<Result<_, _>>()?; | ||
|
||
let max_volume: f64 = node | ||
.nodes | ||
.iter() | ||
.map(|idx| model.get_node(idx)?.get_current_max_volume(model, state)) | ||
.sum::<Result<_, _>>()?; | ||
// TODO handle divide by zero | ||
Ok(volume / max_volume) | ||
} | ||
Self::NodeInFlowDeficit(idx) => { | ||
let node = model.get_node(idx)?; | ||
let flow = state.get_network_state().get_node_in_flow(idx)?; | ||
let max_flow = node.get_current_max_flow(model, state)?; | ||
Ok(max_flow - flow) | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.