|
1 | 1 | //! Common routines for handling input data. |
2 | 2 | use crate::asset::AssetPool; |
3 | | -use crate::graph::{build_commodity_graphs_for_model, validate_commodity_graphs_for_model}; |
| 3 | +use crate::graph::{ |
| 4 | + CommoditiesGraph, build_commodity_graphs_for_model, validate_commodity_graphs_for_model, |
| 5 | +}; |
4 | 6 | use crate::id::{HasID, IDLike}; |
5 | 7 | use crate::model::{Model, ModelParameters}; |
| 8 | +use crate::region::RegionID; |
6 | 9 | use crate::units::UnitType; |
7 | 10 | use anyhow::{Context, Result, bail, ensure}; |
8 | 11 | use float_cmp::approx_eq; |
@@ -233,12 +236,46 @@ pub fn load_model<P: AsRef<Path>>(model_dir: P) -> Result<(Model, AssetPool)> { |
233 | 236 | processes, |
234 | 237 | time_slice_info, |
235 | 238 | regions, |
236 | | - commodity_graphs, |
237 | 239 | commodity_order, |
238 | 240 | }; |
239 | 241 | Ok((model, AssetPool::new(assets))) |
240 | 242 | } |
241 | 243 |
|
| 244 | +/// Load commodity flow graphs for a model. |
| 245 | +/// |
| 246 | +/// This begins by reading time slice, region, year, commodity and process data which is necessary |
| 247 | +/// to build the graphs. Other model data (agents and assets) that is not necessary for graph |
| 248 | +/// building is not read. |
| 249 | +/// |
| 250 | +/// Once data is loaded (and assuming the validation checks for this data pass), this will create a |
| 251 | +/// graph of commodity flows for each region and year, where nodes are commodities and edges are |
| 252 | +/// processes. |
| 253 | +/// |
| 254 | +/// Graphs validation is NOT performed. This ensures that graphs can be generated even when |
| 255 | +/// validation would fail, which may be helpful for debugging. |
| 256 | +pub fn load_commodity_graphs<P: AsRef<Path>>( |
| 257 | + model_dir: P, |
| 258 | +) -> Result<HashMap<(RegionID, u32), CommoditiesGraph>> { |
| 259 | + let model_params = ModelParameters::from_path(&model_dir)?; |
| 260 | + |
| 261 | + let time_slice_info = read_time_slice_info(model_dir.as_ref())?; |
| 262 | + let regions = read_regions(model_dir.as_ref())?; |
| 263 | + let region_ids = regions.keys().cloned().collect(); |
| 264 | + let years = &model_params.milestone_years; |
| 265 | + |
| 266 | + let commodities = read_commodities(model_dir.as_ref(), ®ion_ids, &time_slice_info, years)?; |
| 267 | + let processes = read_processes( |
| 268 | + model_dir.as_ref(), |
| 269 | + &commodities, |
| 270 | + ®ion_ids, |
| 271 | + &time_slice_info, |
| 272 | + years, |
| 273 | + )?; |
| 274 | + |
| 275 | + let commodity_graphs = build_commodity_graphs_for_model(&processes, ®ion_ids, years)?; |
| 276 | + Ok(commodity_graphs) |
| 277 | +} |
| 278 | + |
242 | 279 | #[cfg(test)] |
243 | 280 | mod tests { |
244 | 281 | use super::*; |
|
0 commit comments