From e4aa0decb5ad2e0261ec15b3e556d2a1c5f7c4de Mon Sep 17 00:00:00 2001 From: Diego Alonso Alvarez Date: Thu, 9 Jul 2026 14:29:50 +0100 Subject: [PATCH 1/4] Don't create unmet demand file unless needed. --- src/output.rs | 31 ++++++++++++++++++++---- tests/data/simple/debug_unmet_demand.csv | 0 2 files changed, 26 insertions(+), 5 deletions(-) delete mode 100644 tests/data/simple/debug_unmet_demand.csv diff --git a/src/output.rs b/src/output.rs index 9479e942e..a77e2ce9b 100644 --- a/src/output.rs +++ b/src/output.rs @@ -280,8 +280,9 @@ struct AppraisalResultsTimeSliceRow { /// For writing extra debug information about the model struct DebugDataWriter { context: Option, + unmet_demand_file_path: PathBuf, commodity_balance_duals_writer: csv::Writer, - unmet_demand_writer: csv::Writer, + unmet_demand_writer: Option>, solver_values_writer: csv::Writer, appraisal_results_writer: csv::Writer, appraisal_results_time_slice_writer: csv::Writer, @@ -302,8 +303,9 @@ impl DebugDataWriter { Ok(Self { context: None, + unmet_demand_file_path: output_path.join(UNMET_DEMAND_FILE_NAME), commodity_balance_duals_writer: new_writer(COMMODITY_BALANCE_DUALS_FILE_NAME)?, - unmet_demand_writer: new_writer(UNMET_DEMAND_FILE_NAME)?, + unmet_demand_writer: None, solver_values_writer: new_writer(SOLVER_VALUES_FILE_NAME)?, appraisal_results_writer: new_writer(APPRAISAL_RESULTS_FILE_NAME)?, appraisal_results_time_slice_writer: new_writer( @@ -439,7 +441,21 @@ impl DebugDataWriter { where I: Iterator, { - for (commodity_id, region_id, time_slice, value) in iter { + let rows: Vec<(&CommodityID, &RegionID, &TimeSliceID, Flow)> = + iter.collect::>(); + + if rows.is_empty() { + return Ok(()); + } + + // If the unmet demand writer already exist, we return with an error, as it should not happen + ensure!( + self.unmet_demand_writer.is_none(), + "Unmet demand file already exists!" + ); + + self.unmet_demand_writer = Some(csv::Writer::from_path(&self.unmet_demand_file_path)?); + for (commodity_id, region_id, time_slice, value) in rows { let row = UnmetDemandRow { milestone_year, run_description: self.with_context(run_description), @@ -448,7 +464,10 @@ impl DebugDataWriter { time_slice: time_slice.clone(), value, }; - self.unmet_demand_writer.serialize(row)?; + self.unmet_demand_writer + .as_mut() + .expect("Writer does not exist (cannot not happen)") + .serialize(row)?; } Ok(()) @@ -529,8 +548,10 @@ impl DebugDataWriter { /// Flush the underlying streams fn flush(&mut self) -> Result<()> { + if let Some(wrt) = &mut self.unmet_demand_writer { + wrt.flush()?; + } self.commodity_balance_duals_writer.flush()?; - self.unmet_demand_writer.flush()?; self.solver_values_writer.flush()?; self.appraisal_results_writer.flush()?; self.appraisal_results_time_slice_writer.flush()?; diff --git a/tests/data/simple/debug_unmet_demand.csv b/tests/data/simple/debug_unmet_demand.csv deleted file mode 100644 index e69de29bb..000000000 From 75a05f3c5a071f6e2464330c37662398b3d40a1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Alonso=20=C3=81lvarez?= <6095790+dalonsoa@users.noreply.github.com> Date: Fri, 10 Jul 2026 11:58:15 +0100 Subject: [PATCH 2/4] Change error handling to panic for unmet demand writer --- src/output.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/output.rs b/src/output.rs index a77e2ce9b..8e31cd969 100644 --- a/src/output.rs +++ b/src/output.rs @@ -449,10 +449,9 @@ impl DebugDataWriter { } // If the unmet demand writer already exist, we return with an error, as it should not happen - ensure!( - self.unmet_demand_writer.is_none(), - "Unmet demand file already exists!" - ); + if self.unmet_demand_writer.is_some() { + panic!("Unmet demand file already exists!"); + } self.unmet_demand_writer = Some(csv::Writer::from_path(&self.unmet_demand_file_path)?); for (commodity_id, region_id, time_slice, value) in rows { From 95f7ba7ee27de81dd8f741d03895f07ae3f78ced Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Alonso=20=C3=81lvarez?= <6095790+dalonsoa@users.noreply.github.com> Date: Fri, 10 Jul 2026 12:02:08 +0100 Subject: [PATCH 3/4] Change panic to assert for unmet demand writer check Replaced panic with assert to handle existing unmet demand writer. --- src/output.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/output.rs b/src/output.rs index 8e31cd969..5d393509e 100644 --- a/src/output.rs +++ b/src/output.rs @@ -449,9 +449,7 @@ impl DebugDataWriter { } // If the unmet demand writer already exist, we return with an error, as it should not happen - if self.unmet_demand_writer.is_some() { - panic!("Unmet demand file already exists!"); - } + assert!(self.unmet_demand_writer.is_none(), "Unmet demand file already exists!"); self.unmet_demand_writer = Some(csv::Writer::from_path(&self.unmet_demand_file_path)?); for (commodity_id, region_id, time_slice, value) in rows { From 4724b9966c4bd3b275f21acd745a83089968aef1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Fri, 10 Jul 2026 11:03:11 +0000 Subject: [PATCH 4/4] [pre-commit.ci lite] apply automatic fixes --- src/output.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/output.rs b/src/output.rs index 5d393509e..7940e3888 100644 --- a/src/output.rs +++ b/src/output.rs @@ -449,7 +449,10 @@ impl DebugDataWriter { } // If the unmet demand writer already exist, we return with an error, as it should not happen - assert!(self.unmet_demand_writer.is_none(), "Unmet demand file already exists!"); + assert!( + self.unmet_demand_writer.is_none(), + "Unmet demand file already exists!" + ); self.unmet_demand_writer = Some(csv::Writer::from_path(&self.unmet_demand_file_path)?); for (commodity_id, region_id, time_slice, value) in rows {