@@ -9,9 +9,13 @@ use indexmap::IndexSet;
99use itertools:: { Itertools , iproduct} ;
1010use petgraph:: Directed ;
1111use petgraph:: algo:: toposort;
12+ use petgraph:: dot:: Dot ;
1213use petgraph:: graph:: Graph ;
1314use std:: collections:: HashMap ;
1415use std:: fmt:: Display ;
16+ use std:: fs:: File ;
17+ use std:: io:: Write as IoWrite ;
18+ use std:: path:: Path ;
1519use 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`] .
209213fn 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) ]
413432mod tests {
414433 use super :: * ;
0 commit comments