-
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: Add Turbine node and parameter (#144)
* Added HydropowerTargetParameter * Added PowerFromNodeFlow derived metric * Added TurbineNode in pywr-schema
- Loading branch information
1 parent
04bc7d4
commit 5f50093
Showing
11 changed files
with
582 additions
and
4 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
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,109 @@ | ||
use crate::metric::MetricF64; | ||
use crate::network::Network; | ||
use crate::parameters::{Parameter, ParameterMeta}; | ||
use crate::scenario::ScenarioIndex; | ||
use crate::state::{ParameterState, State}; | ||
use crate::timestep::Timestep; | ||
use crate::utils::inverse_hydropower_calculation; | ||
use crate::PywrError; | ||
|
||
pub struct HydropowerTargetData { | ||
pub target: MetricF64, | ||
pub elevation: Option<f64>, | ||
pub min_head: Option<f64>, | ||
pub max_flow: Option<MetricF64>, | ||
pub min_flow: Option<MetricF64>, | ||
pub efficiency: Option<f64>, | ||
pub water_elevation: Option<MetricF64>, | ||
pub water_density: Option<f64>, | ||
pub flow_unit_conversion: Option<f64>, | ||
pub energy_unit_conversion: Option<f64>, | ||
} | ||
|
||
pub struct HydropowerTargetParameter { | ||
pub meta: ParameterMeta, | ||
pub target: MetricF64, | ||
pub max_flow: Option<MetricF64>, | ||
pub min_flow: Option<MetricF64>, | ||
pub turbine_min_head: f64, | ||
pub turbine_elevation: f64, | ||
pub turbine_efficiency: f64, | ||
pub water_elevation: Option<MetricF64>, | ||
pub water_density: f64, | ||
pub flow_unit_conversion: f64, | ||
pub energy_unit_conversion: f64, | ||
} | ||
|
||
impl HydropowerTargetParameter { | ||
pub fn new(name: &str, turbine_data: HydropowerTargetData) -> Self { | ||
Self { | ||
meta: ParameterMeta::new(name), | ||
target: turbine_data.target, | ||
water_elevation: turbine_data.water_elevation, | ||
turbine_elevation: turbine_data.elevation.unwrap_or(0.0), | ||
turbine_min_head: turbine_data.min_head.unwrap_or(0.0), | ||
turbine_efficiency: turbine_data.efficiency.unwrap_or(1.0), | ||
max_flow: turbine_data.max_flow, | ||
min_flow: turbine_data.min_flow, | ||
water_density: turbine_data.water_density.unwrap_or(1000.0), | ||
flow_unit_conversion: turbine_data.flow_unit_conversion.unwrap_or(1.0), | ||
energy_unit_conversion: turbine_data.energy_unit_conversion.unwrap_or(1e-6), | ||
} | ||
} | ||
} | ||
|
||
impl Parameter<f64> for HydropowerTargetParameter { | ||
fn meta(&self) -> &ParameterMeta { | ||
&self.meta | ||
} | ||
fn compute( | ||
&self, | ||
_timestep: &Timestep, | ||
_scenario_index: &ScenarioIndex, | ||
model: &Network, | ||
state: &State, | ||
_internal_state: &mut Option<Box<dyn ParameterState>>, | ||
) -> Result<f64, PywrError> { | ||
// Calculate the head | ||
let mut head = if let Some(water_elevation) = &self.water_elevation { | ||
water_elevation.get_value(model, state)? - self.turbine_elevation | ||
} else { | ||
self.turbine_elevation | ||
}; | ||
|
||
// the head may be negative | ||
head = head.max(0.0); | ||
|
||
// apply the minimum head threshold | ||
if head <= self.turbine_min_head { | ||
return Ok(0.0); | ||
} | ||
|
||
// Get the flow from the current node | ||
let power = self.target.get_value(model, state)?; | ||
let mut q = inverse_hydropower_calculation( | ||
power, | ||
head, | ||
self.turbine_efficiency, | ||
self.flow_unit_conversion, | ||
self.energy_unit_conversion, | ||
self.water_density, | ||
); | ||
|
||
// Bound the flow if required | ||
if let Some(max_flow) = &self.max_flow { | ||
q = q.min(max_flow.get_value(model, state)?); | ||
} | ||
if let Some(min_flow) = &self.min_flow { | ||
q = q.max(min_flow.get_value(model, state)?); | ||
} | ||
|
||
if q < 0.0 { | ||
return Err(PywrError::InternalParameterError(format!( | ||
"The calculated flow in the hydro power parameter named {} is negative", | ||
self.name() | ||
))); | ||
} | ||
Ok(q) | ||
} | ||
} |
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,23 @@ | ||
/// Calculate the flow required to produce power using the hydropower equation | ||
pub fn inverse_hydropower_calculation( | ||
power: f64, | ||
head: f64, | ||
efficiency: f64, | ||
flow_unit_conversion: f64, | ||
energy_unit_conversion: f64, | ||
density: f64, | ||
) -> f64 { | ||
power / (energy_unit_conversion * density * 9.81 * head * efficiency * flow_unit_conversion) | ||
} | ||
|
||
/// Calculate the produced power from the flow using the hydropower equation | ||
pub fn hydropower_calculation( | ||
flow: f64, | ||
head: f64, | ||
efficiency: f64, | ||
flow_unit_conversion: f64, | ||
energy_unit_conversion: f64, | ||
density: f64, | ||
) -> f64 { | ||
flow * (energy_unit_conversion * density * 9.81 * head * efficiency * flow_unit_conversion) | ||
} |
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.