-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add support for multi-model simulation. #60
Merged
Merged
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9914339
feat: Add support for multi-model simulation.
jetuk 7fcff4a
chore: Merge branch 'main' into multi-model
jetuk 395fd4c
feat: Refactor multi-model test and improve test model description.
jetuk 15fa0bf
fix: Use consistent inter-model terminology.
jetuk 16104e6
feat: Add initial value for inter-network transfers.
jetuk f8cd45a
chore: Merge branch 'main' into multi-model
jetuk 1f7cf6b
chore: Add docstrings for the two main model types.
jetuk f4cc927
chore: Fix docstring code blocks.
jetuk 8c6901e
chore: Merge branch 'main' into multi-model
jetuk ba41a26
chore: Merge branch 'main' into multi-model
jetuk 7702f54
chore: Merge branch 'main' into multi-model
jetuk 608f983
chore: Merge branch 'main' into multi-model
jetuk 09337a1
feat: Improve design of inter-network transfers.
jetuk 494ec0b
feat: Add multi-model command to CLI.
jetuk 52d71d8
fix: Correct docstring typo in PywrMultiNetworkModel
jetuk 84deef8
fix: Apply model file parent as data path if data path not given.
jetuk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
use anyhow::{Context, Result}; | ||
use clap::{Parser, Subcommand, ValueEnum}; | ||
use pywr_core::model::Model; | ||
#[cfg(feature = "ipm-ocl")] | ||
use pywr_core::solvers::{ClIpmF32Solver, ClIpmF64Solver, ClIpmSolverSettings}; | ||
use pywr_core::solvers::{ClpSolver, ClpSolverSettings}; | ||
|
@@ -9,16 +8,13 @@ use pywr_core::solvers::{HighsSolver, HighsSolverSettings}; | |
#[cfg(feature = "ipm-simd")] | ||
use pywr_core::solvers::{SimdIpmF64Solver, SimdIpmSolverSettings}; | ||
use pywr_core::test_utils::make_random_model; | ||
use pywr_core::timestep::Timestepper; | ||
use pywr_core::tracing::setup_tracing; | ||
use pywr_core::PywrError; | ||
use pywr_schema::model::PywrModel; | ||
use pywr_schema::model::{PywrModel, PywrMultiNetworkModel}; | ||
use pywr_schema::ConversionError; | ||
use rand::SeedableRng; | ||
use rand_chacha::ChaCha8Rng; | ||
use std::fmt::{Display, Formatter}; | ||
use std::path::{Path, PathBuf}; | ||
use time::macros::date; | ||
|
||
#[derive(Copy, Clone, ValueEnum)] | ||
enum Solver { | ||
|
@@ -92,6 +88,25 @@ enum Commands { | |
#[arg(long, default_value_t = false)] | ||
debug: bool, | ||
}, | ||
RunMulti { | ||
/// Path to Pywr model JSON. | ||
model: PathBuf, | ||
/// Solver to use. | ||
#[arg(short, long, default_value_t=Solver::Clp)] | ||
solver: Solver, | ||
#[arg(short, long)] | ||
data_path: Option<PathBuf>, | ||
#[arg(short, long)] | ||
output_path: Option<PathBuf>, | ||
/// Use multiple threads for simulation. | ||
#[arg(short, long, default_value_t = false)] | ||
parallel: bool, | ||
/// The number of threads to use in parallel simulation. | ||
#[arg(short, long, default_value_t = 1)] | ||
threads: usize, | ||
#[arg(long, default_value_t = false)] | ||
debug: bool, | ||
}, | ||
RunRandom { | ||
num_systems: usize, | ||
density: usize, | ||
|
@@ -117,6 +132,15 @@ fn main() -> Result<()> { | |
threads, | ||
debug, | ||
} => run(model, solver, data_path.as_deref(), output_path.as_deref(), *debug), | ||
Commands::RunMulti { | ||
model, | ||
solver, | ||
data_path, | ||
output_path, | ||
parallel, | ||
threads, | ||
debug, | ||
} => run(model, solver, data_path.as_deref(), output_path.as_deref(), *debug), | ||
Commands::RunRandom { | ||
num_systems, | ||
density, | ||
|
@@ -173,48 +197,58 @@ fn run(path: &Path, solver: &Solver, data_path: Option<&Path>, output_path: Opti | |
let data = std::fs::read_to_string(path).unwrap(); | ||
let schema_v2: PywrModel = serde_json::from_str(data.as_str()).unwrap(); | ||
|
||
let (model, timestepper): (Model, Timestepper) = schema_v2.build_model(data_path, output_path).unwrap(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks like a cmd needs adding to run a multi-model. Something woth doing in this PR? |
||
let model = schema_v2.build_model(data_path, output_path).unwrap(); | ||
|
||
match *solver { | ||
Solver::Clp => model.run::<ClpSolver>(×tepper, &ClpSolverSettings::default()), | ||
Solver::Clp => model.run::<ClpSolver>(&ClpSolverSettings::default()), | ||
#[cfg(feature = "highs")] | ||
Solver::HIGHS => model.run::<HighsSolver>(×tepper, &HighsSolverSettings::default()), | ||
Solver::HIGHS => model.run::<HighsSolver>(&HighsSolverSettings::default()), | ||
#[cfg(feature = "ipm-ocl")] | ||
Solver::CLIPMF32 => model.run_multi_scenario::<ClIpmF32Solver>(×tepper, &ClIpmSolverSettings::default()), | ||
Solver::CLIPMF32 => model.run_multi_scenario::<ClIpmF32Solver>(&ClIpmSolverSettings::default()), | ||
#[cfg(feature = "ipm-ocl")] | ||
Solver::CLIPMF64 => model.run_multi_scenario::<ClIpmF64Solver>(×tepper, &ClIpmSolverSettings::default()), | ||
Solver::CLIPMF64 => model.run_multi_scenario::<ClIpmF64Solver>(&ClIpmSolverSettings::default()), | ||
#[cfg(feature = "ipm-simd")] | ||
Solver::IpmSimd => { | ||
model.run_multi_scenario::<SimdIpmF64Solver<4>>(×tepper, &SimdIpmSolverSettings::default()) | ||
} | ||
Solver::IpmSimd => model.run_multi_scenario::<SimdIpmF64Solver<4>>(&SimdIpmSolverSettings::default()), | ||
} | ||
.unwrap(); | ||
} | ||
|
||
fn run_multi(path: &Path, solver: &Solver, data_path: Option<&Path>, output_path: Option<&Path>, debug: bool) { | ||
setup_tracing(debug).unwrap(); | ||
|
||
let data = std::fs::read_to_string(path).unwrap(); | ||
let schema_v2: PywrMultiNetworkModel = serde_json::from_str(data.as_str()).unwrap(); | ||
|
||
let model = schema_v2.build_model(data_path, output_path).unwrap(); | ||
|
||
match *solver { | ||
Solver::Clp => model.run::<ClpSolver>(&ClpSolverSettings::default()), | ||
#[cfg(feature = "highs")] | ||
Solver::HIGHS => model.run::<HighsSolver>(&HighsSolverSettings::default()), | ||
#[cfg(feature = "ipm-ocl")] | ||
Solver::CLIPMF32 => model.run_multi_scenario::<ClIpmF32Solver>(&ClIpmSolverSettings::default()), | ||
#[cfg(feature = "ipm-ocl")] | ||
Solver::CLIPMF64 => model.run_multi_scenario::<ClIpmF64Solver>(&ClIpmSolverSettings::default()), | ||
#[cfg(feature = "ipm-simd")] | ||
Solver::IpmSimd => model.run_multi_scenario::<SimdIpmF64Solver<4>>(&SimdIpmSolverSettings::default()), | ||
} | ||
.unwrap(); | ||
} | ||
|
||
fn run_random(num_systems: usize, density: usize, num_scenarios: usize, solver: &Solver) { | ||
let timestepper = Timestepper::new(date!(2020 - 01 - 01), date!(2020 - 01 - 10), 1); | ||
let mut rng = ChaCha8Rng::seed_from_u64(0); | ||
let model = make_random_model( | ||
num_systems, | ||
density, | ||
timestepper.timesteps().len(), | ||
num_scenarios, | ||
&mut rng, | ||
) | ||
.unwrap(); | ||
let model = make_random_model(num_systems, density, num_scenarios, &mut rng).unwrap(); | ||
|
||
match *solver { | ||
Solver::Clp => model.run::<ClpSolver>(×tepper, &ClpSolverSettings::default()), | ||
Solver::Clp => model.run::<ClpSolver>(&ClpSolverSettings::default()), | ||
#[cfg(feature = "highs")] | ||
Solver::HIGHS => model.run::<HighsSolver>(×tepper, &HighsSolverSettings::default()), | ||
Solver::HIGHS => model.run::<HighsSolver>(&HighsSolverSettings::default()), | ||
#[cfg(feature = "ipm-ocl")] | ||
Solver::CLIPMF32 => model.run_multi_scenario::<ClIpmF32Solver>(×tepper, &ClIpmSolverSettings::default()), | ||
Solver::CLIPMF32 => model.run_multi_scenario::<ClIpmF32Solver>(&ClIpmSolverSettings::default()), | ||
#[cfg(feature = "ipm-ocl")] | ||
Solver::CLIPMF64 => model.run_multi_scenario::<ClIpmF64Solver>(×tepper, &ClIpmSolverSettings::default()), | ||
Solver::CLIPMF64 => model.run_multi_scenario::<ClIpmF64Solver>(&ClIpmSolverSettings::default()), | ||
#[cfg(feature = "ipm-simd")] | ||
Solver::IpmSimd => { | ||
model.run_multi_scenario::<SimdIpmF64Solver<4>>(×tepper, &SimdIpmSolverSettings::default()) | ||
} | ||
Solver::IpmSimd => model.run_multi_scenario::<SimdIpmF64Solver<4>>(&SimdIpmSolverSettings::default()), | ||
} | ||
.unwrap(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be
run_multi
?