Skip to content

Commit 962359d

Browse files
committed
Apply suggestions
1 parent 7fb11f0 commit 962359d

5 files changed

Lines changed: 29 additions & 38 deletions

File tree

src/cli.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! The command line interface for the simulation.
2+
use crate::graph::save_commodity_graphs_for_model;
23
use crate::input::{load_commodity_graphs, load_model};
34
use crate::log;
4-
use crate::output::graph::save_commodity_graphs_for_model;
55
use crate::output::{create_output_directory, get_graphs_dir, get_output_dir};
66
use crate::settings::Settings;
77
use ::log::{info, warn};
@@ -207,17 +207,12 @@ pub fn handle_graph_command(
207207
settings: Option<Settings>,
208208
) -> Result<()> {
209209
// Load program settings, if not provided
210-
let mut settings = if let Some(settings) = settings {
210+
let settings = if let Some(settings) = settings {
211211
settings
212212
} else {
213213
Settings::load().context("Failed to load settings.")?
214214
};
215215

216-
// These settings can be overridden by command-line arguments
217-
if opts.overwrite {
218-
settings.overwrite = true;
219-
}
220-
221216
// Get path to output folder
222217
let pathbuf: PathBuf;
223218
let output_path = if let Some(p) = opts.output_dir.as_deref() {
@@ -246,7 +241,7 @@ pub fn handle_graph_command(
246241
// Load commodity flow graphs and save to file
247242
let commodity_graphs = load_commodity_graphs(model_path).context("Failed to build graphs.")?;
248243
save_commodity_graphs_for_model(&commodity_graphs, output_path)?;
249-
info!("Graphs saved to file");
244+
info!("Graphs saved to: {}", output_path.display());
250245

251246
Ok(())
252247
}

src/graph.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ use indexmap::IndexSet;
99
use itertools::{Itertools, iproduct};
1010
use petgraph::Directed;
1111
use petgraph::algo::toposort;
12+
use petgraph::dot::Dot;
1213
use petgraph::graph::Graph;
1314
use std::collections::HashMap;
1415
use std::fmt::Display;
16+
use std::fs::File;
17+
use std::io::Write as IoWrite;
18+
use std::path::Path;
1519
use strum::IntoEnumIterator;
1620

1721
/// A graph of commodity flows for a given region and year
@@ -127,7 +131,7 @@ fn create_commodities_graph_for_region_year(
127131
graph
128132
}
129133

130-
/// Prepares a graph for validation with `validate_commodities_graph`.
134+
/// Prepares a graph for validation with [`validate_commodities_graph`].
131135
///
132136
/// It takes a base graph produced by `create_commodities_graph_for_region_year`, and modifies it to
133137
/// account for process availabilities and commodity demands within the given time slice selection,
@@ -205,7 +209,7 @@ fn prepare_commodities_graph_for_validation(
205209
/// The validation is only performed for commodities with the specified time slice level. For full
206210
/// validation of all commodities in the model, we therefore need to run this function for all time
207211
/// slice selections at all time slice levels. This is handled by
208-
/// `validate_commodity_graphs_for_model`.
212+
/// [`validate_commodity_graphs_for_model`].
209213
fn validate_commodities_graph(
210214
graph: &CommoditiesGraph,
211215
commodities: &CommodityMap,
@@ -409,6 +413,21 @@ pub fn validate_commodity_graphs_for_model(
409413
Ok(commodity_order)
410414
}
411415

416+
/// Saves commodity graphs to file
417+
///
418+
/// The graphs are saved as DOT files to the specified output path
419+
pub fn save_commodity_graphs_for_model(
420+
commodity_graphs: &HashMap<(RegionID, u32), CommoditiesGraph>,
421+
output_path: &Path,
422+
) -> Result<()> {
423+
for ((region_id, year), graph) in commodity_graphs {
424+
let dot = Dot::new(&graph);
425+
let mut file = File::create(output_path.join(format!("{region_id}_{year}.dot")))?;
426+
write!(file, "{dot}")?;
427+
}
428+
Ok(())
429+
}
430+
412431
#[cfg(test)]
413432
mod tests {
414433
use super::*;

src/output.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use std::fs;
1717
use std::fs::File;
1818
use std::path::{Path, PathBuf};
1919

20-
pub mod graph;
2120
pub mod metadata;
2221
use metadata::write_metadata;
2322

src/output/graph.rs

Lines changed: 0 additions & 24 deletions
This file was deleted.

tests/graph.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ fn test_handle_graph_command() {
2121

2222
// Save results to non-existent directory to check that directory creation works
2323
let tempdir = tempdir().unwrap();
24-
let output_dir = tempdir.path().join("results");
24+
let output_dir = tempdir.path().join("graphs");
2525
let opts = GraphOpts {
26-
output_dir: Some(output_dir),
26+
output_dir: Some(output_dir.clone()),
2727
overwrite: false,
2828
};
2929
handle_graph_command(&get_model_dir(), &opts, Some(Settings::default())).unwrap();
30-
3130
assert!(is_logger_initialised());
31+
32+
// Check that at least one DOT file was created
33+
assert!(output_dir.join("GBR_2020.dot").exists());
3234
}

0 commit comments

Comments
 (0)