muse2/input/
process.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
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
//! Code for reading process-related information from CSV files.
use crate::commodity::{Commodity, CommodityType};
use crate::input::*;
use crate::process::{Process, ProcessCapacityMap, ProcessFlow, ProcessParameter};
use crate::region::RegionSelection;
use crate::time_slice::TimeSliceInfo;
use anyhow::Result;
use serde::Deserialize;
use std::collections::{HashMap, HashSet};
use std::ops::RangeInclusive;
use std::path::Path;
use std::rc::Rc;
pub mod availability;
use availability::read_process_availabilities;
pub mod flow;
use flow::read_process_flows;
pub mod parameter;
use parameter::read_process_parameters;
pub mod region;
use anyhow::bail;
use region::read_process_regions;

const PROCESSES_FILE_NAME: &str = "processes.csv";

macro_rules! define_process_id_getter {
    ($t:ty) => {
        impl HasID for $t {
            fn get_id(&self) -> &str {
                &self.process_id
            }
        }
    };
}
use define_process_id_getter;

#[derive(PartialEq, Debug, Deserialize)]
struct ProcessDescription {
    id: Rc<str>,
    description: String,
}
define_id_getter! {ProcessDescription}

/// Read process information from the specified CSV files.
///
/// # Arguments
///
/// * `model_dir` - Folder containing model configuration files
/// * `commodities` - Commodities for the model
/// * `region_ids` - All possible region IDs
/// * `time_slice_info` - Information about seasons and times of day
/// * `year_range` - The possible range of milestone years
///
/// # Returns
///
/// This function returns a map of processes, with the IDs as keys.
pub fn read_processes(
    model_dir: &Path,
    commodities: &HashMap<Rc<str>, Rc<Commodity>>,
    region_ids: &HashSet<Rc<str>>,
    time_slice_info: &TimeSliceInfo,
    year_range: &RangeInclusive<u32>,
) -> Result<HashMap<Rc<str>, Rc<Process>>> {
    let file_path = model_dir.join(PROCESSES_FILE_NAME);
    let descriptions = read_csv_id_file::<ProcessDescription>(&file_path)?;
    let process_ids = HashSet::from_iter(descriptions.keys().cloned());

    let availabilities = read_process_availabilities(model_dir, &process_ids, time_slice_info)?;
    let flows = read_process_flows(model_dir, &process_ids, commodities)?;
    let parameters = read_process_parameters(model_dir, &process_ids, year_range)?;
    let regions = read_process_regions(model_dir, &process_ids, region_ids)?;

    // Validate commodities after the flows have been read
    validate_commodities(commodities, &flows)?;

    create_process_map(
        descriptions.into_values(),
        availabilities,
        flows,
        parameters,
        regions,
    )
}

/// Perform consistency checks for commodity flows.
fn validate_commodities(
    commodities: &HashMap<Rc<str>, Rc<Commodity>>,
    flows: &HashMap<Rc<str>, Vec<ProcessFlow>>,
) -> anyhow::Result<()> {
    for (commodity_id, commodity) in commodities {
        if commodity.kind == CommodityType::SupplyEqualsDemand {
            validate_sed_commodity(commodity_id, commodity, flows)?;
        }
    }
    Ok(())
}

fn validate_sed_commodity(
    commodity_id: &Rc<str>,
    commodity: &Rc<Commodity>,
    flows: &HashMap<Rc<str>, Vec<ProcessFlow>>,
) -> Result<()> {
    let mut has_producer = false;
    let mut has_consumer = false;

    for flow in flows.values().flatten() {
        if Rc::ptr_eq(&flow.commodity, commodity) {
            if flow.flow > 0.0 {
                has_producer = true;
            } else if flow.flow < 0.0 {
                has_consumer = true;
            }

            if has_producer && has_consumer {
                return Ok(());
            }
        }
    }

    bail!(
        "Commodity {} of 'SED' type must have both producer and consumer processes",
        commodity_id
    );
}

fn create_process_map<I>(
    descriptions: I,
    mut availabilities: HashMap<Rc<str>, ProcessCapacityMap>,
    mut flows: HashMap<Rc<str>, Vec<ProcessFlow>>,
    mut parameters: HashMap<Rc<str>, ProcessParameter>,
    mut regions: HashMap<Rc<str>, RegionSelection>,
) -> Result<HashMap<Rc<str>, Rc<Process>>>
where
    I: Iterator<Item = ProcessDescription>,
{
    descriptions
        .map(|description| {
            let id = &description.id;
            let availabilities = availabilities
                .remove(id)
                .with_context(|| format!("No availabilities defined for process {id}"))?;
            let flows = flows
                .remove(id)
                .with_context(|| format!("No commodity flows defined for process {id}"))?;
            let parameter = parameters
                .remove(id)
                .with_context(|| format!("No parameters defined for process {id}"))?;

            // We've already checked that regions are defined for each process
            let regions = regions.remove(id).unwrap();

            let process = Process {
                id: Rc::clone(id),
                description: description.description,
                capacity_fractions: availabilities,
                flows,
                parameter,
                regions,
            };

            Ok((description.id, process.into()))
        })
        .process_results(|iter| iter.collect())
}

#[cfg(test)]
mod tests {
    use crate::commodity::{CommodityCostMap, DemandMap};
    use crate::process::FlowType;
    use crate::time_slice::TimeSliceLevel;

    use super::*;

    struct ProcessData {
        descriptions: Vec<ProcessDescription>,
        availabilities: HashMap<Rc<str>, ProcessCapacityMap>,
        flows: HashMap<Rc<str>, Vec<ProcessFlow>>,
        parameters: HashMap<Rc<str>, ProcessParameter>,
        regions: HashMap<Rc<str>, RegionSelection>,
    }

    /// Returns example data (without errors) for processes
    fn get_process_data() -> ProcessData {
        let descriptions = vec![
            ProcessDescription {
                id: Rc::from("process1"),
                description: "Process 1".to_string(),
            },
            ProcessDescription {
                id: Rc::from("process2"),
                description: "Process 2".to_string(),
            },
        ];

        let availabilities = ["process1", "process2"]
            .into_iter()
            .map(|id| (id.into(), ProcessCapacityMap::new()))
            .collect();

        let flows = ["process1", "process2"]
            .into_iter()
            .map(|id| (id.into(), vec![]))
            .collect();

        let parameters = ["process1", "process2"]
            .into_iter()
            .map(|id| {
                let parameter = ProcessParameter {
                    process_id: id.to_string(),
                    years: 2010..=2020,
                    capital_cost: 0.0,
                    fixed_operating_cost: 0.0,
                    variable_operating_cost: 0.0,
                    lifetime: 1,
                    discount_rate: 1.0,
                    cap2act: 0.0,
                };

                (id.into(), parameter)
            })
            .collect();

        let regions = ["process1", "process2"]
            .into_iter()
            .map(|id| (id.into(), RegionSelection::All))
            .collect();

        ProcessData {
            descriptions,
            availabilities,
            flows,
            parameters,
            regions,
        }
    }

    #[test]
    fn test_create_process_map_success() {
        let data = get_process_data();
        let result = create_process_map(
            data.descriptions.into_iter(),
            data.availabilities,
            data.flows,
            data.parameters,
            data.regions,
        )
        .unwrap();

        assert_eq!(result.len(), 2);
        assert!(result.contains_key("process1"));
        assert!(result.contains_key("process2"));
    }

    /// Generate code for a test with data missing for a given field
    macro_rules! test_missing {
        ($field:ident) => {
            let mut data = get_process_data();
            data.$field.remove("process1");

            let result = create_process_map(
                data.descriptions.into_iter(),
                data.availabilities,
                data.flows,
                data.parameters,
                data.regions,
            );
            assert!(result.is_err());
        };
    }

    #[test]
    fn test_create_process_map_missing_availabilities() {
        test_missing!(availabilities);
    }

    #[test]
    fn test_create_process_map_missing_flows() {
        test_missing!(flows);
    }

    #[test]
    fn test_create_process_map_missing_parameters() {
        test_missing!(parameters);
    }

    #[test]
    fn test_validate_commodities() {
        // Create mock commodities
        let commodity_sed = Rc::new(Commodity {
            id: "commodity_sed".into(),
            description: "SED commodity".into(),
            kind: CommodityType::SupplyEqualsDemand,
            time_slice_level: TimeSliceLevel::Annual,
            costs: CommodityCostMap::new(),
            demand: DemandMap::new(),
        });

        let commodity_non_sed = Rc::new(Commodity {
            id: "commodity_non_sed".into(),
            description: "Non-SED commodity".into(),
            kind: CommodityType::ServiceDemand,
            time_slice_level: TimeSliceLevel::Annual,
            costs: CommodityCostMap::new(),
            demand: DemandMap::new(),
        });

        let commodities: HashMap<Rc<str>, Rc<Commodity>> = [
            (Rc::clone(&commodity_sed.id), Rc::clone(&commodity_sed)),
            (
                Rc::clone(&commodity_non_sed.id),
                Rc::clone(&commodity_non_sed),
            ),
        ]
        .into_iter()
        .collect();

        // Create mock flows
        let process_flows: HashMap<Rc<str>, Vec<ProcessFlow>> = [
            (
                "process1".into(),
                vec![
                    ProcessFlow {
                        process_id: "process1".into(),
                        commodity: Rc::clone(&commodity_sed),
                        flow: 10.0,
                        flow_type: FlowType::Fixed,
                        flow_cost: 1.0,
                        is_pac: false,
                    },
                    ProcessFlow {
                        process_id: "process1".into(),
                        commodity: Rc::clone(&commodity_non_sed),
                        flow: -5.0,
                        flow_type: FlowType::Fixed,
                        flow_cost: 1.0,
                        is_pac: false,
                    },
                ],
            ),
            (
                "process2".into(),
                vec![ProcessFlow {
                    process_id: "process2".into(),
                    commodity: Rc::clone(&commodity_sed),
                    flow: -10.0,
                    flow_type: FlowType::Fixed,
                    flow_cost: 1.0,
                    is_pac: false,
                }],
            ),
        ]
        .into_iter()
        .collect();

        // Validate commodities
        assert!(validate_commodities(&commodities, &process_flows).is_ok());

        // Modify flows to make the validation fail
        let process_flows_invalid: HashMap<Rc<str>, Vec<ProcessFlow>> = [(
            "process1".into(),
            vec![ProcessFlow {
                process_id: "process1".into(),
                commodity: Rc::clone(&commodity_sed),
                flow: 10.0,
                flow_type: FlowType::Fixed,
                flow_cost: 1.0,
                is_pac: false,
            }],
        )]
        .into_iter()
        .collect();

        // Validate commodities should fail
        assert!(validate_commodities(&commodities, &process_flows_invalid).is_err());
    }
}