Skip to content

Commit bf934db

Browse files
committed
Use default muse2_graphs folder
1 parent e4272e0 commit bf934db

3 files changed

Lines changed: 65 additions & 12 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ settings.toml
77

88
# Simulation output files
99
**/muse2_results
10+
**/muse2_graphs
1011

1112
# Generated by Cargo
1213
# will have compiled files and executables

src/cli.rs

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use crate::input::{load_commodity_graphs, load_model};
33
use crate::log;
44
use crate::output::graph::save_commodity_graphs_for_model;
5-
use crate::output::{create_output_directory, get_output_dir};
5+
use crate::output::{create_output_directory, get_graphs_dir, get_output_dir};
66
use crate::settings::Settings;
77
use ::log::{info, warn};
88
use anyhow::{Context, Result};
@@ -67,8 +67,9 @@ enum Commands {
6767
BuildCommodityGraphs {
6868
/// The path to the model directory.
6969
model_dir: PathBuf,
70-
/// Output path
71-
output_dir: PathBuf,
70+
/// Other options (uses the same settings as the `run` command)
71+
#[command(flatten)]
72+
opts: RunOpts,
7273
},
7374
/// Manage settings file.
7475
Settings {
@@ -85,10 +86,9 @@ impl Commands {
8586
Self::Run { model_dir, opts } => handle_run_command(&model_dir, &opts, None),
8687
Self::Example { subcommand } => subcommand.execute(),
8788
Self::Validate { model_dir } => handle_validate_command(&model_dir, None),
88-
Self::BuildCommodityGraphs {
89-
model_dir,
90-
output_dir,
91-
} => handle_build_commodity_graphs_command(&model_dir, &output_dir, None),
89+
Self::BuildCommodityGraphs { model_dir, opts } => {
90+
handle_build_commodity_graphs_command(&model_dir, &opts, None)
91+
}
9292
Self::Settings { subcommand } => subcommand.execute(),
9393
}
9494
}
@@ -194,21 +194,52 @@ pub fn handle_validate_command(model_path: &Path, settings: Option<Settings>) ->
194194
/// Handle the `build-commodity-graphs` command.
195195
pub fn handle_build_commodity_graphs_command(
196196
model_path: &Path,
197-
output_path: &Path,
197+
opts: &RunOpts,
198198
settings: Option<Settings>,
199199
) -> Result<()> {
200200
// Load program settings, if not provided
201-
let settings = if let Some(settings) = settings {
201+
let mut settings = if let Some(settings) = settings {
202202
settings
203203
} else {
204204
Settings::load().context("Failed to load settings.")?
205205
};
206206

207-
// Initialise program logger (we won't save log files when running the validate command)
207+
// These settings can be overridden by command-line arguments
208+
if opts.debug_model {
209+
settings.debug_model = true;
210+
}
211+
if opts.overwrite {
212+
settings.overwrite = true;
213+
}
214+
215+
// Get path to output folder
216+
let pathbuf: PathBuf;
217+
let output_path = if let Some(p) = opts.output_dir.as_deref() {
218+
p
219+
} else {
220+
pathbuf = get_graphs_dir(model_path)?;
221+
&pathbuf
222+
};
223+
224+
let overwrite =
225+
create_output_directory(output_path, settings.overwrite).with_context(|| {
226+
format!(
227+
"Failed to create commodity flow graphs directory: {}",
228+
output_path.display()
229+
)
230+
})?;
231+
232+
// Initialise program logger (we won't save log files when running this command
208233
log::init(&settings.log_level, None).context("Failed to initialise logging.")?;
209234

235+
// NB: We have to wait until the logger is initialised to display this warning
236+
if overwrite {
237+
warn!("Commodity flow graphs directory will be overwritten");
238+
}
239+
210240
// Load commodity flow graphs and save to file
211-
let commodity_graphs = load_commodity_graphs(model_path)?;
241+
let commodity_graphs =
242+
load_commodity_graphs(model_path).context("Failed to build commodity flow graphs.")?;
212243
save_commodity_graphs_for_model(&commodity_graphs, output_path)?;
213244
info!("Commodity flow graphs saved to file");
214245

src/output.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ const SOLVER_VALUES_FILE_NAME: &str = "debug_solver.csv";
4848
/// The output file name for appraisal results
4949
const APPRAISAL_RESULTS_FILE_NAME: &str = "debug_appraisal_results.csv";
5050

51-
/// Get the model name from the specified directory path
51+
/// The root folder in which commodity flow graphs will be created
52+
const GRAPHS_DIRECTORY_ROOT: &str = "muse2_graphs";
53+
54+
/// Get the default output directory for the model
5255
pub fn get_output_dir(model_dir: &Path) -> Result<PathBuf> {
5356
// Get the model name from the dir path. This ends up being convoluted because we need to check
5457
// for all possible errors. Ugh.
@@ -66,6 +69,24 @@ pub fn get_output_dir(model_dir: &Path) -> Result<PathBuf> {
6669
Ok([OUTPUT_DIRECTORY_ROOT, model_name].iter().collect())
6770
}
6871

72+
/// Get the default output directory for commodity flow graphs for the model
73+
pub fn get_graphs_dir(model_dir: &Path) -> Result<PathBuf> {
74+
// Get the model name from the dir path. This ends up being convoluted because we need to check
75+
// for all possible errors. Ugh.
76+
let model_dir = model_dir
77+
.canonicalize() // canonicalise in case the user has specified "."
78+
.context("Could not resolve path to model")?;
79+
80+
let model_name = model_dir
81+
.file_name()
82+
.context("Model cannot be in root folder")?
83+
.to_str()
84+
.context("Invalid chars in model dir name")?;
85+
86+
// Construct path
87+
Ok([GRAPHS_DIRECTORY_ROOT, model_name].iter().collect())
88+
}
89+
6990
/// Create a new output directory for the model, optionally overwriting existing data
7091
///
7192
/// # Arguments

0 commit comments

Comments
 (0)