-
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 pywr-book listings to workspace.
- Loading branch information
Showing
8 changed files
with
112 additions
and
65 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
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,11 @@ | ||
[package] | ||
name = "adding-a-parameter" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
pywr-core = { path = "../../../pywr-core" } | ||
pywr-schema = { path = "../../../pywr-schema" } | ||
pywr-schema-macros = { path = "../../../pywr-schema-macros" } | ||
serde = { workspace = true } | ||
schemars = { workspace = true } |
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,81 @@ | ||
use pywr_core::metric::MetricF64; | ||
use pywr_core::network::Network; | ||
use pywr_core::parameters::{Parameter, ParameterMeta}; | ||
use pywr_core::scenario::ScenarioIndex; | ||
use pywr_core::state::{ParameterState, State}; | ||
use pywr_core::timestep::Timestep; | ||
use pywr_core::PywrError; | ||
|
||
// ANCHOR: parameter | ||
pub struct MaxParameter { | ||
meta: ParameterMeta, | ||
metric: MetricF64, | ||
threshold: f64, | ||
} | ||
// ANCHOR_END: parameter | ||
// ANCHOR: impl-new | ||
impl MaxParameter { | ||
pub fn new(name: &str, metric: MetricF64, threshold: f64) -> Self { | ||
Self { | ||
meta: ParameterMeta::new(name), | ||
metric, | ||
threshold, | ||
} | ||
} | ||
} | ||
// ANCHOR_END: impl-new | ||
// ANCHOR: impl-parameter | ||
impl Parameter<f64> for MaxParameter { | ||
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> { | ||
// Current value | ||
let x = self.metric.get_value(model, state)?; | ||
Ok(x.max(self.threshold)) | ||
} | ||
} | ||
// ANCHOR_END: impl-parameter | ||
mod schema { | ||
use pywr_schema::metric::Metric; | ||
use pywr_schema::parameters::ParameterMeta; | ||
use schemars::JsonSchema; | ||
|
||
// ANCHOR: schema | ||
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, JsonSchema)] | ||
pub struct MaxParameter { | ||
#[serde(flatten)] | ||
pub meta: ParameterMeta, | ||
pub parameter: Metric, | ||
pub threshold: Option<f64>, | ||
} | ||
|
||
// ANCHOR_END: schema | ||
// ANCHOR: schema-impl | ||
#[cfg(feature = "core")] | ||
impl MaxParameter { | ||
pub fn add_to_model( | ||
&self, | ||
network: &mut pywr_core::network::Network, | ||
args: &LoadArgs, | ||
) -> Result<ParameterIndex<f64>, SchemaError> { | ||
let idx = self.parameter.load(network, args)?; | ||
let threshold = self.threshold.unwrap_or(0.0); | ||
|
||
let p = pywr_core::parameters::MaxParameter::new(&self.meta.name, idx, threshold); | ||
Ok(network.add_parameter(Box::new(p))?) | ||
} | ||
} | ||
// ANCHOR_END: schema-impl | ||
} | ||
|
||
fn main() { | ||
println!("Hello, world!"); | ||
} |
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