muse2/input/commodity/
levy.rs

1//! Code for reading in the commodity levies CSV file.
2use super::super::{input_err_msg, read_csv_optional, try_insert};
3use crate::commodity::{BalanceType, CommodityID, CommodityLevy, CommodityLevyMap};
4use crate::id::IDCollection;
5use crate::region::{RegionID, parse_region_str};
6use crate::time_slice::TimeSliceInfo;
7use crate::units::MoneyPerFlow;
8use crate::year::parse_year_str;
9use anyhow::{Context, Result, ensure};
10use indexmap::IndexSet;
11use log::warn;
12use serde::Deserialize;
13use std::collections::HashMap;
14use std::path::Path;
15
16const COMMODITY_LEVIES_FILE_NAME: &str = "commodity_levies.csv";
17
18/// Cost parameters for each commodity
19#[derive(PartialEq, Debug, Deserialize, Clone)]
20struct CommodityLevyRaw {
21    /// Unique identifier for the commodity (e.g. "ELC")
22    commodity_id: String,
23    /// The region(s) to which the levy applies.
24    regions: String,
25    /// Type of balance for application of cost.
26    balance_type: BalanceType,
27    /// The year(s) to which the cost applies.
28    years: String,
29    /// The time slice to which the cost applies.
30    time_slice: String,
31    /// Cost per unit commodity
32    value: MoneyPerFlow,
33}
34
35/// Read costs associated with each commodity from levies CSV file.
36///
37/// # Arguments
38///
39/// * `model_dir` - Folder containing model configuration files
40/// * `commodity_ids` - All possible commodity IDs
41/// * `region_ids` - All possible region IDs
42/// * `time_slice_info` - Information about time slices
43/// * `milestone_years` - All milestone years
44///
45/// # Returns
46///
47/// A map containing levies, grouped by commodity ID or an error.
48pub fn read_commodity_levies(
49    model_dir: &Path,
50    commodity_ids: &IndexSet<CommodityID>,
51    region_ids: &IndexSet<RegionID>,
52    time_slice_info: &TimeSliceInfo,
53    milestone_years: &[u32],
54) -> Result<HashMap<CommodityID, CommodityLevyMap>> {
55    let file_path = model_dir.join(COMMODITY_LEVIES_FILE_NAME);
56    let commodity_levies_csv = read_csv_optional(&file_path)?;
57    read_commodity_levies_iter(
58        commodity_levies_csv,
59        commodity_ids,
60        region_ids,
61        time_slice_info,
62        milestone_years,
63    )
64    .with_context(|| input_err_msg(&file_path))
65}
66
67/// Read costs associated with each commodity from an iterator over raw cost entries.
68///
69/// # Arguments
70///
71/// * `iter` - An iterator over raw commodity levy entries
72/// * `commodity_ids` - All possible commodity IDs
73/// * `region_ids` - All possible region IDs
74/// * `time_slice_info` - Information about time slices
75/// * `milestone_years` - All milestone years
76///
77/// # Returns
78///
79/// A map containing levies, grouped by commodity ID.
80fn read_commodity_levies_iter<I>(
81    iter: I,
82    commodity_ids: &IndexSet<CommodityID>,
83    region_ids: &IndexSet<RegionID>,
84    time_slice_info: &TimeSliceInfo,
85    milestone_years: &[u32],
86) -> Result<HashMap<CommodityID, CommodityLevyMap>>
87where
88    I: Iterator<Item = CommodityLevyRaw>,
89{
90    let mut map = HashMap::new();
91
92    // Keep track of commodity/region combinations specified. We will check that all years and
93    // time slices are covered for each commodity/region combination.
94    let mut commodity_regions: HashMap<CommodityID, IndexSet<RegionID>> = HashMap::new();
95
96    for cost in iter {
97        let commodity_id = commodity_ids.get_id(&cost.commodity_id)?;
98        let regions = parse_region_str(&cost.regions, region_ids)?;
99        let years = parse_year_str(&cost.years, milestone_years)?;
100        let ts_selection = time_slice_info.get_selection(&cost.time_slice)?;
101
102        // Get or create CommodityLevyMap for this commodity
103        let map = map
104            .entry(commodity_id.clone())
105            .or_insert_with(CommodityLevyMap::new);
106
107        // Create CommodityLevy
108        let cost = CommodityLevy {
109            balance_type: cost.balance_type,
110            value: cost.value,
111        };
112
113        // Insert cost into map for each region/year/time slice
114        for region in &regions {
115            commodity_regions
116                .entry(commodity_id.clone())
117                .or_default()
118                .insert(region.clone());
119            for year in &years {
120                for (time_slice, _) in ts_selection.iter(time_slice_info) {
121                    try_insert(
122                        map,
123                        &(region.clone(), *year, time_slice.clone()),
124                        cost.clone(),
125                    )?;
126                }
127            }
128        }
129    }
130
131    // Validate map and complete with missing regions/years/time slices
132    for (commodity_id, regions) in &commodity_regions {
133        let map = map.get_mut(commodity_id).unwrap();
134        validate_commodity_levy_map(map, regions, milestone_years, time_slice_info)
135            .with_context(|| format!("Missing costs for commodity {commodity_id}"))?;
136
137        for region_id in region_ids.difference(regions) {
138            add_missing_region_to_commodity_levy_map(
139                map,
140                region_id,
141                milestone_years,
142                time_slice_info,
143            );
144            warn!(
145                "No levy specified for commodity {commodity_id} in region {region_id}. Assuming zero levy."
146            );
147        }
148    }
149
150    Ok(map)
151}
152
153/// Add missing region to commodity levy map with zero cost for all years and time slices.
154///
155/// # Arguments
156///
157/// * `map` - The commodity levy map to update
158/// * `region_id` - The region ID to add
159/// * `milestone_years` - All milestone years
160/// * `time_slice_info` - Information about time slices
161fn add_missing_region_to_commodity_levy_map(
162    map: &mut CommodityLevyMap,
163    region_id: &RegionID,
164    milestone_years: &[u32],
165    time_slice_info: &TimeSliceInfo,
166) {
167    for year in milestone_years {
168        for time_slice in time_slice_info.iter_ids() {
169            map.insert(
170                (region_id.clone(), *year, time_slice.clone()),
171                CommodityLevy {
172                    balance_type: BalanceType::Net,
173                    value: MoneyPerFlow(0.0),
174                },
175            );
176        }
177    }
178}
179
180/// Validate that the commodity levy map contains entries for all regions, years and time slices.
181///
182/// # Arguments
183///
184/// * `map` - The commodity levy map to validate
185/// * `regions` - The set of regions that should be covered
186/// * `milestone_years` - All milestone years
187/// * `time_slice_info` - Information about time slices
188///
189/// # Returns
190///
191/// Nothing if the map is valid. An error if the map is missing any entries.
192fn validate_commodity_levy_map(
193    map: &CommodityLevyMap,
194    regions: &IndexSet<RegionID>,
195    milestone_years: &[u32],
196    time_slice_info: &TimeSliceInfo,
197) -> Result<()> {
198    // Check that all regions, years and time slices are covered
199    for region_id in regions {
200        for year in milestone_years {
201            for time_slice in time_slice_info.iter_ids() {
202                ensure!(
203                    map.contains_key(&(region_id.clone(), *year, time_slice.clone())),
204                    "Missing cost for region {region_id}, year {year}, time slice {time_slice}"
205                );
206            }
207        }
208    }
209    Ok(())
210}
211
212#[cfg(test)]
213mod tests {
214    use super::*;
215    use crate::fixture::{assert_error, region_id, time_slice, time_slice_info};
216    use crate::time_slice::TimeSliceID;
217    use crate::units::Year;
218    use rstest::{fixture, rstest};
219
220    #[fixture]
221    fn region_ids(region_id: RegionID) -> IndexSet<RegionID> {
222        IndexSet::from([region_id])
223    }
224
225    #[fixture]
226    fn cost_map(time_slice: TimeSliceID) -> CommodityLevyMap {
227        let cost = CommodityLevy {
228            balance_type: BalanceType::Net,
229            value: MoneyPerFlow(1.0),
230        };
231
232        let mut map = CommodityLevyMap::new();
233        map.insert(("GBR".into(), 2020, time_slice.clone()), cost.clone());
234        map
235    }
236
237    #[rstest]
238    fn test_validate_commodity_levies_map_valid(
239        cost_map: CommodityLevyMap,
240        time_slice_info: TimeSliceInfo,
241        region_ids: IndexSet<RegionID>,
242    ) {
243        // Valid map
244        assert!(
245            validate_commodity_levy_map(&cost_map, &region_ids, &[2020], &time_slice_info).is_ok()
246        );
247    }
248
249    #[rstest]
250    fn test_validate_commodity_levies_map_invalid_missing_region(
251        cost_map: CommodityLevyMap,
252        time_slice_info: TimeSliceInfo,
253    ) {
254        // Missing region
255        let region_ids = IndexSet::from(["GBR".into(), "FRA".into()]);
256        assert_error!(
257            validate_commodity_levy_map(&cost_map, &region_ids, &[2020], &time_slice_info),
258            "Missing cost for region FRA, year 2020, time slice winter.day"
259        );
260    }
261
262    #[rstest]
263    fn test_validate_commodity_levies_map_invalid_missing_year(
264        cost_map: CommodityLevyMap,
265        time_slice_info: TimeSliceInfo,
266        region_ids: IndexSet<RegionID>,
267    ) {
268        // Missing year
269        assert_error!(
270            validate_commodity_levy_map(&cost_map, &region_ids, &[2020, 2030], &time_slice_info),
271            "Missing cost for region GBR, year 2030, time slice winter.day"
272        );
273    }
274
275    #[rstest]
276    fn test_validate_commodity_levies_map_invalid(
277        cost_map: CommodityLevyMap,
278        region_ids: IndexSet<RegionID>,
279    ) {
280        // Missing time slice
281        let time_slice = TimeSliceID {
282            season: "winter".into(),
283            time_of_day: "night".into(),
284        };
285        let time_slice_info = TimeSliceInfo {
286            seasons: [("winter".into(), Year(1.0))].into(),
287            times_of_day: ["day".into(), "night".into()].into(),
288            time_slices: [
289                (time_slice.clone(), Year(0.5)),
290                (time_slice.clone(), Year(0.5)),
291            ]
292            .into(),
293        };
294        assert_error!(
295            validate_commodity_levy_map(&cost_map, &region_ids, &[2020], &time_slice_info),
296            "Missing cost for region GBR, year 2020, time slice winter.night"
297        );
298    }
299
300    #[rstest]
301    fn test_add_missing_region_to_commodity_levy_map(
302        cost_map: CommodityLevyMap,
303        time_slice_info: TimeSliceInfo,
304        region_id: RegionID,
305    ) {
306        let mut cost_map = cost_map;
307        add_missing_region_to_commodity_levy_map(
308            &mut cost_map,
309            &region_id,
310            &[2020],
311            &time_slice_info,
312        );
313
314        // Check that costs have been added for the new region
315        for time_slice in time_slice_info.iter_ids() {
316            assert_eq!(
317                cost_map.get(&(region_id.clone(), 2020, time_slice.clone())),
318                Some(&CommodityLevy {
319                    balance_type: BalanceType::Net,
320                    value: MoneyPerFlow(0.0)
321                })
322            );
323        }
324    }
325}