Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/example/patches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,26 @@ static PATCHES: LazyLock<PatchMap> = LazyLock::new(get_all_patches);

/// Get all patches
fn get_all_patches() -> PatchMap {
[(
// The simple example with gas boiler process made divisible
"simple_divisible",
vec![
FilePatch::new("processes.csv")
.with_deletion("RGASBR,Gas boiler,all,RSHEAT,2020,2040,1.0,")
.with_addition("RGASBR,Gas boiler,all,RSHEAT,2020,2040,1.0,1000"),
],
)]
[
(
// The simple example with gas boiler process made divisible
"simple_divisible",
vec![
FilePatch::new("processes.csv")
.with_deletion("RGASBR,Gas boiler,all,RSHEAT,2020,2040,1.0,")
.with_addition("RGASBR,Gas boiler,all,RSHEAT,2020,2040,1.0,1000"),
],
),
// The simple example with objective type set to NPV for one agent
(
"simple_npv",
vec![
FilePatch::new("agent_objectives.csv")
.with_deletion("A0_RES,all,lcox,,")
.with_addition("A0_RES,all,npv,,"),
],
),
]
.into_iter()
.collect()
}
Expand Down
21 changes: 0 additions & 21 deletions src/input/agent/objective.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
//! Code for reading agent objectives from a CSV file.
use super::super::{input_err_msg, read_csv, try_insert};
use crate::ISSUES_URL;
use crate::agent::{AgentID, AgentMap, AgentObjectiveMap, DecisionRule, ObjectiveType};
use crate::model::{ALLOW_BROKEN_OPTION_NAME, broken_model_options_allowed};
use crate::units::Dimensionless;
use crate::year::parse_year_str;
use anyhow::{Context, Result, ensure};
use itertools::Itertools;
use log::warn;
use serde::Deserialize;
use std::collections::HashMap;
use std::path::Path;
Expand Down Expand Up @@ -93,24 +90,6 @@ where
missing_years.is_empty(),
"Agent {agent_id} is missing objectives for the following milestone years: {missing_years:?}"
);

let npv_years = milestone_years
.iter()
.copied()
.filter(|year| agent_objectives[year] == ObjectiveType::NetPresentValue)
.collect_vec();
if !npv_years.is_empty() {
ensure!(
broken_model_options_allowed(),
"The NPV option is BROKEN and should not be used. See: {ISSUES_URL}/716.\n\
If you are sure that you want to enable it anyway, you need to set the \
{ALLOW_BROKEN_OPTION_NAME} option to true."
);
warn!(
"Agent {agent_id} is using NPV in years {npv_years:?}. \
The NPV option is BROKEN and should not be used. See: {ISSUES_URL}/716."
);
}
}

Ok(all_objectives)
Expand Down
13 changes: 9 additions & 4 deletions src/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,22 @@ impl ModelPatch {
}
}

/// Represents all rows and columns of a CSV file.
///
/// Assumes that each row is unique (as it should be for all MUSE2 input files).
type CSVTable = IndexSet<Vec<String>>;

/// Structure to hold patches for a model csv file.
#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct FilePatch {
/// The file that this patch applies to (e.g. "agents.csv")
filename: String,
/// The header row (optional). If `None`, the header is not checked against base files.
header_row: Option<Vec<String>>,
/// Rows to delete (each row is a vector of fields)
to_delete: IndexSet<Vec<String>>,
to_delete: CSVTable,
/// Rows to add (each row is a vector of fields)
to_add: IndexSet<Vec<String>>,
to_add: CSVTable,
}

impl FilePatch {
Expand Down Expand Up @@ -229,7 +234,7 @@ fn modify_base_with_patch(base: &str, patch: &FilePatch) -> Result<String> {
}

// Read all rows from the base, preserving order and checking for duplicates
let mut base_rows: IndexSet<Vec<String>> = IndexSet::new();
let mut base_rows: CSVTable = CSVTable::new();
for result in reader.records() {
let record = result?;

Expand Down
17 changes: 17 additions & 0 deletions tests/data/simple_npv/assets.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
asset_id,process_id,region_id,agent_id,group_id,commission_year,decommission_year,capacity
0,GASDRV,GBR,A0_GEX,,2020,2030,4002.26
1,GASPRC,GBR,A0_GPR,,2020,2030,3782.13
2,WNDFRM,GBR,A0_ELC,,2020,,3.964844
3,GASCGT,GBR,A0_ELC,,2020,,2.43
4,RGASBR,GBR,A0_RES,,2020,2035,2900.0
5,RELCHP,GBR,A0_RES,,2020,2035,399.98
6,RELCHP,GBR,A0_RES,,2030,,3290.2365652888325
7,RGASBR,GBR,A0_RES,,2030,,3655.8184058764805
8,GASCGT,GBR,A0_ELC,,2030,,33.820477802912976
9,GASPRC,GBR,A0_GPR,,2030,,879.1648830751317
10,GASDRV,GBR,A0_GEX,,2030,,923.1231272288879
11,RGASBR,GBR,A0_RES,,2040,,4011.65737547648
12,RELCHP,GBR,A0_RES,,2040,,802.3314750952961
13,GASCGT,GBR,A0_ELC,,2040,,3.7231090668357614
14,GASPRC,GBR,A0_GPR,,2040,,94.9477829022087
15,GASDRV,GBR,A0_GEX,,2040,,99.6951720473196
Loading