muse2 2.1.0

A tool for running simulations of energy systems
Documentation
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//! Code for handling commodity demands. Demands may vary by region, year, and time slice.
use super::super::{format_items_with_cap, input_err_msg, read_csv};
use super::demand_slicing::{DemandSliceMap, read_demand_slices};
use crate::commodity::{Commodity, CommodityID, CommodityType, DemandMap};
use crate::id::IDCollection;
use crate::region::RegionID;
use crate::time_slice::{TimeSliceInfo, TimeSliceLevel};
use crate::units::Flow;
use anyhow::{Context, Result, ensure};
use indexmap::{IndexMap, IndexSet};
use itertools::iproduct;
use serde::Deserialize;
use std::collections::HashMap;
use std::path::Path;

const DEMAND_FILE_NAME: &str = "demand.csv";

/// Represents a single demand entry in the dataset.
#[allow(clippy::struct_field_names)]
#[derive(Debug, Clone, Deserialize, PartialEq)]
struct Demand {
    /// The commodity this demand entry refers to
    commodity_id: String,
    /// The region of the demand entry
    region_id: String,
    /// The year of the demand entry
    year: u32,
    /// Annual demand quantity
    demand: Flow,
}

/// A map relating commodity, region and year to annual demand
pub type AnnualDemandMap = HashMap<(CommodityID, RegionID, u32), (TimeSliceLevel, Flow)>;

/// A map containing references to commodities
pub type BorrowedCommodityMap<'a> = HashMap<CommodityID, &'a Commodity>;

/// Reads demand data from CSV files.
///
/// # Arguments
///
/// * `model_dir` - Folder containing model configuration files
/// * `commodity_ids` - All possible commodity IDs
/// * `region_ids` - Known region identifiers
/// * `time_slice_info` - Information about seasons and times of day
/// * `milestone_years` - Milestone years used by the model
///
/// # Returns
///
/// A `HashMap<CommodityID, DemandMap>` mapping each commodity to its `DemandMap`.
pub fn read_demand(
    model_dir: &Path,
    commodities: &IndexMap<CommodityID, Commodity>,
    region_ids: &IndexSet<RegionID>,
    time_slice_info: &TimeSliceInfo,
    milestone_years: &[u32],
) -> Result<HashMap<CommodityID, DemandMap>> {
    // Demand only applies to SVD commodities
    let svd_commodities = commodities
        .iter()
        .filter(|(_, commodity)| commodity.kind == CommodityType::ServiceDemand)
        .map(|(id, commodity)| (id.clone(), commodity))
        .collect();

    let demand = read_demand_file(model_dir, &svd_commodities, region_ids, milestone_years)?;
    let slices = read_demand_slices(model_dir, &svd_commodities, region_ids, time_slice_info)?;

    Ok(compute_demand_maps(time_slice_info, &demand, &slices))
}

/// Read the demand.csv file.
///
/// # Arguments
///
/// * `model_dir` - Folder containing model configuration files
/// * `svd_commodities` - Map of service demand commodities
/// * `region_ids` - All possible IDs for regions
/// * `milestone_years` - All milestone years
///
/// # Returns
///
/// An `AnnualDemandMap` mapping `(CommodityID, RegionID, year)` to `(TimeSliceLevel, Flow)`.
fn read_demand_file(
    model_dir: &Path,
    svd_commodities: &BorrowedCommodityMap,
    region_ids: &IndexSet<RegionID>,
    milestone_years: &[u32],
) -> Result<AnnualDemandMap> {
    let file_path = model_dir.join(DEMAND_FILE_NAME);
    let iter = read_csv(&file_path)?;
    read_demand_from_iter(iter, svd_commodities, region_ids, milestone_years)
        .with_context(|| input_err_msg(file_path))
}

/// Read the demand data from an iterator.
///
/// # Arguments
///
/// * `iter` - An iterator of [`Demand`]s
/// * `svd_commodities` - Map of service demand commodities
/// * `region_ids` - All possible IDs for regions
/// * `milestone_years` - All milestone years
///
/// # Returns
///
/// An `AnnualDemandMap` mapping `(CommodityID, RegionID, year)` to `(TimeSliceLevel, Flow)`.
fn read_demand_from_iter<I>(
    iter: I,
    svd_commodities: &BorrowedCommodityMap,
    region_ids: &IndexSet<RegionID>,
    milestone_years: &[u32],
) -> Result<AnnualDemandMap>
where
    I: Iterator<Item = Demand>,
{
    let mut map = AnnualDemandMap::new();
    for demand in iter {
        let commodity = svd_commodities
            .get(demand.commodity_id.as_str())
            .with_context(|| {
                format!(
                    "Can only provide demand data for SVD commodities. Found entry for '{}'",
                    demand.commodity_id
                )
            })?;
        let region_id = region_ids.get_id(&demand.region_id)?;

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

        ensure!(
            demand.demand.is_finite() && demand.demand >= Flow(0.0),
            "Demand must be a finite number greater than or equal to zero"
        );

        ensure!(
            map.insert(
                (commodity.id.clone(), region_id.clone(), demand.year),
                (commodity.time_slice_level, demand.demand)
            )
            .is_none(),
            "Duplicate demand entries (commodity: {}, region: {}, year: {})",
            commodity.id,
            region_id,
            demand.year
        );
    }

    // Check that demand data is specified for all combinations of commodity, region and year
    for commodity_id in svd_commodities.keys() {
        let mut missing_keys = Vec::new();
        for (region_id, year) in iproduct!(region_ids, milestone_years) {
            if !map.contains_key(&(commodity_id.clone(), region_id.clone(), *year)) {
                missing_keys.push((region_id.clone(), *year));
            }
        }
        ensure!(
            missing_keys.is_empty(),
            "Commodity {commodity_id} is missing demand data for {}",
            format_items_with_cap(&missing_keys)
        );
    }

    Ok(map)
}

/// Calculate the demand for each combination of commodity, region, year and time slice.
///
/// # Arguments
///
/// * `time_slice_info` - Information about time slices
/// * `demand` - Total annual demand for combinations of commodity, region and year
/// * `slices` - How annual demand is shared between time slices
///
/// # Returns
///
/// A `HashMap<CommodityID, DemandMap>` mapping each commodity to its `DemandMap`, which contains
/// demand values for combinations of region, year and time slice.
fn compute_demand_maps(
    time_slice_info: &TimeSliceInfo,
    demand: &AnnualDemandMap,
    slices: &DemandSliceMap,
) -> HashMap<CommodityID, DemandMap> {
    let mut map = HashMap::new();
    for ((commodity_id, region_id, year), (level, annual_demand)) in demand {
        for ts_selection in time_slice_info.iter_selections_at_level(*level) {
            let slice_key = (
                commodity_id.clone(),
                region_id.clone(),
                ts_selection.clone(),
            );

            // NB: This has already been checked, so shouldn't fail
            let demand_fraction = slices[&slice_key];

            // Get or create entry
            let map = map
                .entry(commodity_id.clone())
                .or_insert_with(DemandMap::new);

            // Add a new demand entry
            map.insert(
                (region_id.clone(), *year, ts_selection.clone()),
                *annual_demand * demand_fraction,
            );
        }
    }

    map
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fixture::{assert_error, get_svd_map, region_ids, svd_commodity};
    use rstest::rstest;
    use std::fs::File;
    use std::io::Write;
    use std::path::Path;
    use tempfile::tempdir;

    #[rstest]
    fn read_demand_from_iter_works(svd_commodity: Commodity, region_ids: IndexSet<RegionID>) {
        let svd_commodities = get_svd_map(&svd_commodity);
        let demand = [
            Demand {
                year: 2020,
                region_id: "GBR".to_string(),
                commodity_id: "commodity1".to_string(),
                demand: Flow(10.0),
            },
            Demand {
                year: 2020,
                region_id: "USA".to_string(),
                commodity_id: "commodity1".to_string(),
                demand: Flow(11.0),
            },
        ];

        // Valid
        read_demand_from_iter(demand.into_iter(), &svd_commodities, &region_ids, &[2020]).unwrap();
    }

    #[rstest]
    fn read_demand_from_iter_bad_commodity_id(
        svd_commodity: Commodity,
        region_ids: IndexSet<RegionID>,
    ) {
        // Bad commodity ID
        let svd_commodities = get_svd_map(&svd_commodity);
        let demand = [
            Demand {
                year: 2020,
                region_id: "GBR".to_string(),
                commodity_id: "commodity2".to_string(),
                demand: Flow(10.0),
            },
            Demand {
                year: 2020,
                region_id: "USA".to_string(),
                commodity_id: "commodity1".to_string(),
                demand: Flow(11.0),
            },
            Demand {
                year: 2020,
                region_id: "Spain".to_string(),
                commodity_id: "commodity3".to_string(),
                demand: Flow(0.0),
            },
        ];
        assert_error!(
            read_demand_from_iter(demand.into_iter(), &svd_commodities, &region_ids, &[2020]),
            "Can only provide demand data for SVD commodities. Found entry for 'commodity2'"
        );
    }

    #[rstest]
    fn read_demand_from_iter_bad_region_id(
        svd_commodity: Commodity,
        region_ids: IndexSet<RegionID>,
    ) {
        // Bad region ID
        let svd_commodities = get_svd_map(&svd_commodity);
        let demand = [
            Demand {
                year: 2020,
                region_id: "FRA".to_string(),
                commodity_id: "commodity1".to_string(),
                demand: Flow(10.0),
            },
            Demand {
                year: 2020,
                region_id: "USA".to_string(),
                commodity_id: "commodity1".to_string(),
                demand: Flow(11.0),
            },
        ];
        assert_error!(
            read_demand_from_iter(demand.into_iter(), &svd_commodities, &region_ids, &[2020]),
            "Unknown ID FRA found"
        );
    }

    #[rstest]
    fn read_demand_from_iter_bad_year(svd_commodity: Commodity, region_ids: IndexSet<RegionID>) {
        // Bad year
        let svd_commodities = get_svd_map(&svd_commodity);
        let demand = [
            Demand {
                year: 2010,
                region_id: "GBR".to_string(),
                commodity_id: "commodity1".to_string(),
                demand: Flow(10.0),
            },
            Demand {
                year: 2020,
                region_id: "USA".to_string(),
                commodity_id: "commodity1".to_string(),
                demand: Flow(11.0),
            },
        ];
        assert_error!(
            read_demand_from_iter(demand.into_iter(), &svd_commodities, &region_ids, &[2020]),
            "Year 2010 is not a milestone year. \
            Input of non-milestone years is currently not supported."
        );
    }

    #[rstest]
    #[case(-1.0)]
    #[case(f64::NAN)]
    #[case(f64::NEG_INFINITY)]
    #[case(f64::INFINITY)]
    fn read_demand_from_iter_bad_demand(
        svd_commodity: Commodity,
        region_ids: IndexSet<RegionID>,
        #[case] quantity: f64,
    ) {
        // Bad demand quantity
        let svd_commodities = get_svd_map(&svd_commodity);
        let demand = [Demand {
            year: 2020,
            region_id: "GBR".to_string(),
            commodity_id: "commodity1".to_string(),
            demand: Flow(quantity),
        }];
        assert_error!(
            read_demand_from_iter(demand.into_iter(), &svd_commodities, &region_ids, &[2020],),
            "Demand must be a finite number greater than or equal to zero"
        );
    }

    #[rstest]
    fn read_demand_from_iter_multiple_entries(
        svd_commodity: Commodity,
        region_ids: IndexSet<RegionID>,
    ) {
        // Multiple entries for same commodity and region
        let svd_commodities = get_svd_map(&svd_commodity);
        let demand = [
            Demand {
                year: 2020,
                region_id: "GBR".to_string(),
                commodity_id: "commodity1".to_string(),
                demand: Flow(10.0),
            },
            Demand {
                year: 2020,
                region_id: "GBR".to_string(),
                commodity_id: "commodity1".to_string(),
                demand: Flow(10.0),
            },
            Demand {
                year: 2020,
                region_id: "USA".to_string(),
                commodity_id: "commodity1".to_string(),
                demand: Flow(11.0),
            },
        ];
        assert_error!(
            read_demand_from_iter(demand.into_iter(), &svd_commodities, &region_ids, &[2020]),
            "Duplicate demand entries (commodity: commodity1, region: GBR, year: 2020)"
        );
    }

    #[rstest]
    fn read_demand_from_iter_missing_year(
        svd_commodity: Commodity,
        region_ids: IndexSet<RegionID>,
    ) {
        // Missing entry for a milestone year
        let svd_commodities = get_svd_map(&svd_commodity);
        let demand = Demand {
            year: 2020,
            region_id: "GBR".to_string(),
            commodity_id: "commodity1".to_string(),
            demand: Flow(10.0),
        };
        read_demand_from_iter(
            std::iter::once(demand),
            &svd_commodities,
            &region_ids,
            &[2020, 2030],
        )
        .unwrap_err();
    }

    /// Create an example demand file in `dir_path`
    fn create_demand_file(dir_path: &Path) {
        let file_path = dir_path.join(DEMAND_FILE_NAME);
        let mut file = File::create(file_path).unwrap();
        writeln!(
            file,
            "commodity_id,region_id,year,demand\n\
            commodity1,GBR,2020,10\n\
            commodity1,USA,2020,11\n"
        )
        .unwrap();
    }

    #[rstest]
    fn read_demand_file_works(svd_commodity: Commodity, region_ids: IndexSet<RegionID>) {
        let svd_commodities = get_svd_map(&svd_commodity);
        let dir = tempdir().unwrap();
        create_demand_file(dir.path());
        let milestone_years = [2020];
        let expected = AnnualDemandMap::from_iter([
            (
                ("commodity1".into(), "GBR".into(), 2020),
                (TimeSliceLevel::DayNight, Flow(10.0)),
            ),
            (
                ("commodity1".into(), "USA".into(), 2020),
                (TimeSliceLevel::DayNight, Flow(11.0)),
            ),
        ]);
        let demand =
            read_demand_file(dir.path(), &svd_commodities, &region_ids, &milestone_years).unwrap();
        assert_eq!(demand, expected);
    }
}