Skip to content

Commit 9d70b7f

Browse files
committed
Refactor main simulation loop
1 parent 95c610a commit 9d70b7f

1 file changed

Lines changed: 26 additions & 15 deletions

File tree

src/simulation.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,17 @@ pub fn run(
2929
) -> Result<()> {
3030
let mut writer = DataWriter::create(output_path, debug_model)?;
3131

32-
let mut opt_results = None; // all results of dispatch optimisation
33-
for year in model.iter_years() {
34-
info!("Milestone year: {year}");
35-
36-
// Assets that have been decommissioned cannot be selected by agents
37-
assets.decommission_old(year);
32+
// Iterate over milestone years
33+
let mut year_iter = model.iter_years();
34+
let mut year = year_iter.next().unwrap(); // NB: There will be at least one year
3835

39-
// NB: Agent investment is not carried out in first milestone year
40-
if let Some((flow_map, prices)) = opt_results {
41-
perform_agent_investment(&model, &flow_map, &prices, &mut assets);
36+
// There shouldn't be assets already commissioned, but let's do this just in case
37+
assets.decommission_old(year);
4238

43-
// **TODO:** Remove this when we implement at least some of the agent investment code
44-
// See: https://github.com/EnergySystemsModellingLab/MUSE_2.0/issues/304
45-
error!("Agent investment is not yet implemented. Exiting...");
46-
return Ok(());
47-
}
39+
// **TODO:** Remove annotation when the loop actually loops
40+
#[allow(clippy::never_loop)]
41+
loop {
42+
info!("Milestone year: {year}");
4843

4944
// Newly commissioned assets will be included in optimisation for at least one milestone
5045
// year before agents have the option of decommissioning them
@@ -64,7 +59,23 @@ pub fn run(
6459
writer.write_flows(year, &flow_map)?;
6560
writer.write_prices(year, &prices)?;
6661

67-
opt_results = Some((flow_map, prices));
62+
if let Some(next_year) = year_iter.next() {
63+
year = next_year;
64+
65+
// NB: Agent investment is not carried out in first milestone year
66+
perform_agent_investment(&model, &flow_map, &prices, &mut assets);
67+
68+
// Decommission assets whose lifetime has passed
69+
assets.decommission_old(year);
70+
} else {
71+
// No more milestone years. Simulation is finished.
72+
break;
73+
}
74+
75+
// **TODO:** Remove this when we implement at least some of the agent investment code
76+
// See: https://github.com/EnergySystemsModellingLab/MUSE_2.0/issues/304
77+
error!("Agent investment is not yet implemented. Exiting...");
78+
break;
6879
}
6980

7081
writer.flush()?;

0 commit comments

Comments
 (0)