Skip to content
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 16 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 63 additions & 29 deletions pywr-cli/src/main.rs
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};
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be run_multi?

Commands::RunRandom {
num_systems,
density,
Expand Down Expand Up @@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The 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>(&timestepper, &ClpSolverSettings::default()),
Solver::Clp => model.run::<ClpSolver>(&ClpSolverSettings::default()),
#[cfg(feature = "highs")]
Solver::HIGHS => model.run::<HighsSolver>(&timestepper, &HighsSolverSettings::default()),
Solver::HIGHS => model.run::<HighsSolver>(&HighsSolverSettings::default()),
#[cfg(feature = "ipm-ocl")]
Solver::CLIPMF32 => model.run_multi_scenario::<ClIpmF32Solver>(&timestepper, &ClIpmSolverSettings::default()),
Solver::CLIPMF32 => model.run_multi_scenario::<ClIpmF32Solver>(&ClIpmSolverSettings::default()),
#[cfg(feature = "ipm-ocl")]
Solver::CLIPMF64 => model.run_multi_scenario::<ClIpmF64Solver>(&timestepper, &ClIpmSolverSettings::default()),
Solver::CLIPMF64 => model.run_multi_scenario::<ClIpmF64Solver>(&ClIpmSolverSettings::default()),
#[cfg(feature = "ipm-simd")]
Solver::IpmSimd => {
model.run_multi_scenario::<SimdIpmF64Solver<4>>(&timestepper, &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>(&timestepper, &ClpSolverSettings::default()),
Solver::Clp => model.run::<ClpSolver>(&ClpSolverSettings::default()),
#[cfg(feature = "highs")]
Solver::HIGHS => model.run::<HighsSolver>(&timestepper, &HighsSolverSettings::default()),
Solver::HIGHS => model.run::<HighsSolver>(&HighsSolverSettings::default()),
#[cfg(feature = "ipm-ocl")]
Solver::CLIPMF32 => model.run_multi_scenario::<ClIpmF32Solver>(&timestepper, &ClIpmSolverSettings::default()),
Solver::CLIPMF32 => model.run_multi_scenario::<ClIpmF32Solver>(&ClIpmSolverSettings::default()),
#[cfg(feature = "ipm-ocl")]
Solver::CLIPMF64 => model.run_multi_scenario::<ClIpmF64Solver>(&timestepper, &ClIpmSolverSettings::default()),
Solver::CLIPMF64 => model.run_multi_scenario::<ClIpmF64Solver>(&ClIpmSolverSettings::default()),
#[cfg(feature = "ipm-simd")]
Solver::IpmSimd => {
model.run_multi_scenario::<SimdIpmF64Solver<4>>(&timestepper, &SimdIpmSolverSettings::default())
}
Solver::IpmSimd => model.run_multi_scenario::<SimdIpmF64Solver<4>>(&SimdIpmSolverSettings::default()),
}
.unwrap();
}
Loading