muse2/input/commodity/
cost.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//! Code for reading in the commodity cost CSV file.
use crate::commodity::{BalanceType, CommodityCost, CommodityCostMap};
use crate::input::*;
use crate::time_slice::TimeSliceInfo;
use anyhow::{ensure, Context, Result};
use serde::Deserialize;
use std::collections::{HashMap, HashSet};
use std::path::Path;
use std::rc::Rc;

const COMMODITY_COSTS_FILE_NAME: &str = "commodity_costs.csv";

/// Cost parameters for each commodity
#[derive(PartialEq, Debug, Deserialize, Clone)]
struct CommodityCostRaw {
    /// Unique identifier for the commodity (e.g. "ELC")
    pub commodity_id: String,
    /// The region to which the commodity cost applies.
    pub region_id: String,
    /// Type of balance for application of cost.
    pub balance_type: BalanceType,
    /// The year to which the cost applies.
    pub year: u32,
    /// The time slice to which the cost applies.
    pub time_slice: String,
    /// Cost per unit commodity. For example, if a CO2 price is specified in input data, it can be applied to net CO2 via this value.
    pub value: f64,
}

/// Read costs associated with each commodity from commodity costs CSV file.
///
/// # Arguments
///
/// * `model_dir` - Folder containing model configuration files
/// * `commodity_ids` - All possible commodity IDs
/// * `region_ids` - All possible region IDs
/// * `time_slice_info` - Information about time slices
/// * `milestone_years` - All milestone years
///
/// # Returns
///
/// A map containing commodity costs, grouped by commodity ID.
pub fn read_commodity_costs(
    model_dir: &Path,
    commodity_ids: &HashSet<Rc<str>>,
    region_ids: &HashSet<Rc<str>>,
    time_slice_info: &TimeSliceInfo,
    milestone_years: &[u32],
) -> Result<HashMap<Rc<str>, CommodityCostMap>> {
    let file_path = model_dir.join(COMMODITY_COSTS_FILE_NAME);
    let commodity_costs_csv = read_csv::<CommodityCostRaw>(&file_path)?;
    read_commodity_costs_iter(
        commodity_costs_csv,
        commodity_ids,
        region_ids,
        time_slice_info,
        milestone_years,
    )
    .with_context(|| input_err_msg(&file_path))
}

fn read_commodity_costs_iter<I>(
    iter: I,
    commodity_ids: &HashSet<Rc<str>>,
    region_ids: &HashSet<Rc<str>>,
    time_slice_info: &TimeSliceInfo,
    milestone_years: &[u32],
) -> Result<HashMap<Rc<str>, CommodityCostMap>>
where
    I: Iterator<Item = CommodityCostRaw>,
{
    let mut map = HashMap::new();

    // Keep track of milestone years used for each commodity + region combo. If a user provides an
    // entry with a given commodity + region combo for one milestone year, they must also provide
    // entries for all the other milestone years.
    let mut used_milestone_years = HashMap::new();

    for cost in iter {
        let commodity_id = commodity_ids.get_id(&cost.commodity_id)?;
        let region_id = region_ids.get_id(&cost.region_id)?;
        let ts_selection = time_slice_info.get_selection(&cost.time_slice)?;

        ensure!(
            milestone_years.binary_search(&cost.year).is_ok(),
            "Year {} is not a milestone year. \
                Input of non-milestone years is currently not supported.",
            cost.year
        );

        // Get or create CommodityCostMap for this commodity
        let map = map
            .entry(commodity_id.clone())
            .or_insert_with(CommodityCostMap::new);

        for (time_slice, _) in time_slice_info.iter_selection(&ts_selection) {
            let value = CommodityCost {
                balance_type: cost.balance_type.clone(),
                value: cost.value,
            };

            ensure!(
                map.insert(Rc::clone(&region_id), cost.year, time_slice.clone(), value)
                    .is_none(),
                "Commodity cost entry covered by more than one time slice \
                (region: {}, year: {}, time slice: {})",
                region_id,
                cost.year,
                time_slice
            );
        }

        // Keep track of milestone years used for each commodity + region combo
        used_milestone_years
            .entry((commodity_id, region_id))
            .or_insert_with(|| HashSet::with_capacity(1))
            .insert(cost.year);
    }

    let milestone_years = HashSet::from_iter(milestone_years.iter().cloned());
    for ((commodity_id, region_id), years) in used_milestone_years.iter() {
        ensure!(
            years == &milestone_years,
            "Commodity costs missing for some milestone years (commodity: {}, region: {})",
            commodity_id,
            region_id
        );
    }

    Ok(map)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::time_slice::TimeSliceID;
    use std::iter;

    #[test]
    fn test_read_commodity_costs_iter() {
        let commodity_ids = ["commodity".into()].into_iter().collect();
        let region_ids = ["GBR".into(), "FRA".into()].into_iter().collect();
        let slices = [
            TimeSliceID {
                season: "winter".into(),
                time_of_day: "day".into(),
            },
            TimeSliceID {
                season: "summer".into(),
                time_of_day: "night".into(),
            },
        ];
        let time_slice_info = TimeSliceInfo {
            seasons: ["winter".into(), "summer".into()].into_iter().collect(),
            times_of_day: ["day".into(), "night".into()].into_iter().collect(),
            fractions: [(slices[0].clone(), 0.5), (slices[1].clone(), 0.5)]
                .into_iter()
                .collect(),
        };
        let time_slice = time_slice_info
            .get_time_slice_id_from_str("winter.day")
            .unwrap();
        let milestone_years = [2010];

        // Valid
        let cost1 = CommodityCostRaw {
            commodity_id: "commodity".into(),
            region_id: "GBR".into(),
            balance_type: BalanceType::Consumption,
            year: 2010,
            time_slice: "winter.day".into(),
            value: 0.5,
        };
        let cost2 = CommodityCostRaw {
            commodity_id: "commodity".into(),
            region_id: "FRA".into(),
            balance_type: BalanceType::Production,
            year: 2010,
            time_slice: "winter.day".into(),
            value: 0.5,
        };
        let value1 = CommodityCost {
            balance_type: cost1.balance_type.clone(),
            value: cost1.value,
        };
        let value2 = CommodityCost {
            balance_type: cost2.balance_type.clone(),
            value: cost2.value,
        };
        let mut map = CommodityCostMap::new();
        map.insert("GBR".into(), cost1.year, time_slice.clone(), value1);
        map.insert("FRA".into(), cost2.year, time_slice.clone(), value2);
        let expected = HashMap::from_iter([("commodity".into(), map)]);
        assert_eq!(
            read_commodity_costs_iter(
                [cost1.clone(), cost2].into_iter(),
                &commodity_ids,
                &region_ids,
                &time_slice_info,
                &milestone_years,
            )
            .unwrap(),
            expected
        );

        // Invalid: Overlapping time slices
        let cost2 = CommodityCostRaw {
            commodity_id: "commodity".into(),
            region_id: "GBR".into(),
            balance_type: BalanceType::Production,
            year: 2010,
            time_slice: "winter".into(), // NB: Covers all winter
            value: 0.5,
        };
        assert!(read_commodity_costs_iter(
            [cost1.clone(), cost2].into_iter(),
            &commodity_ids,
            &region_ids,
            &time_slice_info,
            &milestone_years,
        )
        .is_err());

        // Invalid: Bad commodity
        let cost = CommodityCostRaw {
            commodity_id: "commodity2".into(),
            region_id: "GBR".into(),
            balance_type: BalanceType::Production,
            year: 2010,
            time_slice: "winter.day".into(),
            value: 0.5,
        };
        assert!(read_commodity_costs_iter(
            iter::once(cost),
            &commodity_ids,
            &region_ids,
            &time_slice_info,
            &milestone_years,
        )
        .is_err());

        // Invalid: Bad region
        let cost = CommodityCostRaw {
            commodity_id: "commodity".into(),
            region_id: "USA".into(),
            balance_type: BalanceType::Production,
            year: 2010,
            time_slice: "winter.day".into(),
            value: 0.5,
        };
        assert!(read_commodity_costs_iter(
            iter::once(cost),
            &commodity_ids,
            &region_ids,
            &time_slice_info,
            &milestone_years,
        )
        .is_err());

        // Invalid: Bad time slice selection
        let cost = CommodityCostRaw {
            commodity_id: "commodity".into(),
            region_id: "GBR".into(),
            balance_type: BalanceType::Production,
            year: 2010,
            time_slice: "summer.evening".into(),
            value: 0.5,
        };
        assert!(read_commodity_costs_iter(
            iter::once(cost),
            &commodity_ids,
            &region_ids,
            &time_slice_info,
            &milestone_years,
        )
        .is_err());

        // Invalid: non-milestone year
        let cost2 = CommodityCostRaw {
            commodity_id: "commodity".into(),
            region_id: "GBR".into(),
            balance_type: BalanceType::Consumption,
            year: 2011, // NB: Non-milestone year
            time_slice: "winter.day".into(),
            value: 0.5,
        };
        assert!(read_commodity_costs_iter(
            [cost1, cost2].into_iter(),
            &commodity_ids,
            &region_ids,
            &time_slice_info,
            &milestone_years,
        )
        .is_err());

        // Invalid: Milestone year 2020 is not covered
        let milestone_years = [2010, 2020];
        let cost = CommodityCostRaw {
            commodity_id: "commodity".into(),
            region_id: "GBR".into(),
            balance_type: BalanceType::Consumption,
            year: 2010,
            time_slice: "winter.day".into(),
            value: 0.5,
        };
        assert!(read_commodity_costs_iter(
            iter::once(cost),
            &commodity_ids,
            &region_ids,
            &time_slice_info,
            &milestone_years,
        )
        .is_err());
    }
}