Skip to content

Commit 0b50a24

Browse files
committed
Revert "Allow multiple regions in demand input files"
This reverts commit 211c0e9.
1 parent e6261a7 commit 0b50a24

4 files changed

Lines changed: 63 additions & 68 deletions

File tree

examples/simple/demand.csv

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
commodity_id,regions,years,demand
2-
RSHEAT,all,2020,927.38
3-
RSHEAT,all,2030,927.38
1+
commodity_id,region_id,years,demand
2+
RSHEAT,GBR,2020,927.38
3+
RSHEAT,GBR,2030,927.38

examples/simple/demand_slicing.csv

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
commodity_id,regions,time_slice,fraction
2-
RSHEAT,all,winter.night,0.065498087
3-
RSHEAT,all,winter.day,0.226148097
4-
RSHEAT,all,winter.peak,0.111199678
5-
RSHEAT,all,winter.evening,0.076688397
6-
RSHEAT,all,peak.night,0.045882959
7-
RSHEAT,all,peak.day,0.121987853
8-
RSHEAT,all,peak.peak,0.068135438
9-
RSHEAT,all,peak.evening,0.041334529
10-
RSHEAT,all,summer.night,0.005698366
11-
RSHEAT,all,summer.day,0.009494171
12-
RSHEAT,all,summer.peak,0.003126411
13-
RSHEAT,all,summer.evening,0.002327863
14-
RSHEAT,all,autumn.night,0.037269058
15-
RSHEAT,all,autumn.day,0.098086985
16-
RSHEAT,all,autumn.peak,0.054500569
17-
RSHEAT,all,autumn.evening,0.032621538
1+
commodity_id,region_id,time_slice,fraction
2+
RSHEAT,GBR,winter.night,0.065498087
3+
RSHEAT,GBR,winter.day,0.226148097
4+
RSHEAT,GBR,winter.peak,0.111199678
5+
RSHEAT,GBR,winter.evening,0.076688397
6+
RSHEAT,GBR,peak.night,0.045882959
7+
RSHEAT,GBR,peak.day,0.121987853
8+
RSHEAT,GBR,peak.peak,0.068135438
9+
RSHEAT,GBR,peak.evening,0.041334529
10+
RSHEAT,GBR,summer.night,0.005698366
11+
RSHEAT,GBR,summer.day,0.009494171
12+
RSHEAT,GBR,summer.peak,0.003126411
13+
RSHEAT,GBR,summer.evening,0.002327863
14+
RSHEAT,GBR,autumn.night,0.037269058
15+
RSHEAT,GBR,autumn.day,0.098086985
16+
RSHEAT,GBR,autumn.peak,0.054500569
17+
RSHEAT,GBR,autumn.evening,0.032621538

src/input/commodity/demand.rs

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::super::*;
44
use super::demand_slicing::{read_demand_slices, DemandSliceMap};
55
use crate::commodity::{Commodity, CommodityID, CommodityType, DemandMap};
66
use crate::id::IDCollection;
7-
use crate::region::{parse_region_str, RegionID};
7+
use crate::region::RegionID;
88
use crate::time_slice::TimeSliceInfo;
99
use crate::year::parse_year_str;
1010
use anyhow::{ensure, Result};
@@ -19,8 +19,8 @@ const DEMAND_FILE_NAME: &str = "demand.csv";
1919
struct Demand {
2020
/// The commodity this demand entry refers to
2121
commodity_id: String,
22-
/// The region(s) of the demand entry
23-
regions: String,
22+
/// The region of the demand entry
23+
region_id: String,
2424
/// The year(s) of the demand entry
2525
years: String,
2626
/// Annual demand quantity
@@ -118,21 +118,19 @@ where
118118
demand.commodity_id
119119
)
120120
})?;
121+
let region_id = region_ids.get_id_by_str(&demand.region_id)?;
121122

122123
ensure!(
123124
demand.demand.is_normal() && demand.demand > 0.0,
124125
"Demand must be a valid number greater than zero"
125126
);
126127

127-
let years = parse_year_str(&demand.years, milestone_years)?;
128-
for region_id in parse_region_str(&demand.regions, region_ids)? {
129-
for &year in years.iter() {
130-
try_insert(
131-
&mut map,
132-
(commodity_id.clone(), region_id.clone(), year),
133-
demand.demand,
134-
)?;
135-
}
128+
for year in parse_year_str(&demand.years, milestone_years)? {
129+
try_insert(
130+
&mut map,
131+
(commodity_id.clone(), region_id.clone(), year),
132+
demand.demand,
133+
)?;
136134
}
137135
}
138136

@@ -217,13 +215,13 @@ mod tests {
217215
let demand = [
218216
Demand {
219217
years: "2020".into(),
220-
regions: "GBR".to_string(),
218+
region_id: "GBR".to_string(),
221219
commodity_id: "commodity1".to_string(),
222220
demand: 10.0,
223221
},
224222
Demand {
225223
years: "2020".into(),
226-
regions: "USA".to_string(),
224+
region_id: "USA".to_string(),
227225
commodity_id: "commodity1".to_string(),
228226
demand: 11.0,
229227
},
@@ -244,13 +242,13 @@ mod tests {
244242
let demand = [
245243
Demand {
246244
years: "2020".into(),
247-
regions: "GBR".to_string(),
245+
region_id: "GBR".to_string(),
248246
commodity_id: "commodity2".to_string(),
249247
demand: 10.0,
250248
},
251249
Demand {
252250
years: "2020".into(),
253-
regions: "USA".to_string(),
251+
region_id: "USA".to_string(),
254252
commodity_id: "commodity1".to_string(),
255253
demand: 11.0,
256254
},
@@ -270,13 +268,13 @@ mod tests {
270268
let demand = [
271269
Demand {
272270
years: "2020".into(),
273-
regions: "FRA".to_string(),
271+
region_id: "FRA".to_string(),
274272
commodity_id: "commodity1".to_string(),
275273
demand: 10.0,
276274
},
277275
Demand {
278276
years: "2020".into(),
279-
regions: "USA".to_string(),
277+
region_id: "USA".to_string(),
280278
commodity_id: "commodity1".to_string(),
281279
demand: 11.0,
282280
},
@@ -296,13 +294,13 @@ mod tests {
296294
let demand = [
297295
Demand {
298296
years: "2010".into(),
299-
regions: "GBR".to_string(),
297+
region_id: "GBR".to_string(),
300298
commodity_id: "commodity1".to_string(),
301299
demand: 10.0,
302300
},
303301
Demand {
304302
years: "2020".into(),
305-
regions: "USA".to_string(),
303+
region_id: "USA".to_string(),
306304
commodity_id: "commodity1".to_string(),
307305
demand: 11.0,
308306
},
@@ -327,7 +325,7 @@ mod tests {
327325
// Bad demand quantity
328326
let demand = [Demand {
329327
years: "2020".into(),
330-
regions: "GBR".to_string(),
328+
region_id: "GBR".to_string(),
331329
commodity_id: "commodity1".to_string(),
332330
demand: quantity,
333331
}];
@@ -346,19 +344,19 @@ mod tests {
346344
let demand = [
347345
Demand {
348346
years: "2020".into(),
349-
regions: "GBR".to_string(),
347+
region_id: "GBR".to_string(),
350348
commodity_id: "commodity1".to_string(),
351349
demand: 10.0,
352350
},
353351
Demand {
354352
years: "2020".into(),
355-
regions: "GBR".to_string(),
353+
region_id: "GBR".to_string(),
356354
commodity_id: "commodity1".to_string(),
357355
demand: 10.0,
358356
},
359357
Demand {
360358
years: "2020".into(),
361-
regions: "USA".to_string(),
359+
region_id: "USA".to_string(),
362360
commodity_id: "commodity1".to_string(),
363361
demand: 11.0,
364362
},
@@ -378,21 +376,21 @@ mod tests {
378376
let demand = [
379377
Demand {
380378
years: "2020".into(),
381-
regions: "GBR".to_string(),
379+
region_id: "GBR".to_string(),
382380
commodity_id: "commodity1".to_string(),
383381
demand: 10.0,
384382
},
385383
Demand {
386384
years: "2020".into(),
387-
regions: "USA".to_string(),
385+
region_id: "USA".to_string(),
388386
commodity_id: "commodity1".to_string(),
389387
demand: 11.0,
390388
},
391389
Demand {
392390
years: "2030".into(),
393-
regions: "USA".to_string(),
391+
region_id: "GBR".to_string(),
394392
commodity_id: "commodity1".to_string(),
395-
demand: 5.0,
393+
demand: 10.0,
396394
},
397395
];
398396
assert_error!(
@@ -402,7 +400,7 @@ mod tests {
402400
&region_ids,
403401
&[2020, 2030]
404402
),
405-
"Commodity commodity1 is missing demand data for [(RegionID(\"GBR\"), 2030)]"
403+
"Commodity commodity1 is missing demand data for [(RegionID(\"USA\"), 2030)]"
406404
);
407405
}
408406

@@ -412,7 +410,7 @@ mod tests {
412410
let mut file = File::create(file_path).unwrap();
413411
writeln!(
414412
file,
415-
"commodity_id,regions,years,demand\n\
413+
"commodity_id,region_id,years,demand\n\
416414
commodity1,GBR,2020,10\n\
417415
commodity1,USA,all,11\n"
418416
)

src/input/commodity/demand_slicing.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use super::super::*;
33
use crate::commodity::CommodityID;
44
use crate::id::IDCollection;
5-
use crate::region::{parse_region_str, RegionID};
5+
use crate::region::RegionID;
66
use crate::time_slice::{TimeSliceID, TimeSliceInfo};
77
use anyhow::{ensure, Context, Result};
88
use itertools::Itertools;
@@ -15,7 +15,7 @@ const DEMAND_SLICING_FILE_NAME: &str = "demand_slicing.csv";
1515
#[derive(Clone, Deserialize)]
1616
struct DemandSlice {
1717
commodity_id: String,
18-
regions: String,
18+
region_id: String,
1919
time_slice: String,
2020
#[serde(deserialize_with = "deserialise_proportion_nonzero")]
2121
fraction: f64,
@@ -71,7 +71,7 @@ where
7171
slice.commodity_id
7272
)
7373
})?;
74-
let regions = parse_region_str(&slice.regions, region_ids)?;
74+
let region_id = region_ids.get_id_by_str(&slice.region_id)?;
7575

7676
// We need to know how many time slices are covered by the current demand slice entry and
7777
// how long they are relative to one another so that we can divide up the demand for this
@@ -80,13 +80,10 @@ where
8080
for (ts, demand_fraction) in time_slice_info.calculate_share(&ts_selection, slice.fraction)
8181
{
8282
// Share demand between the time slices in proportion to duration
83-
for region_id in regions.iter() {
84-
let key = (commodity_id.clone(), region_id.clone(), ts.clone());
85-
ensure!(demand_slices.insert(key, demand_fraction).is_none(),
83+
ensure!(demand_slices.insert((commodity_id.clone(), region_id.clone(), ts.clone()), demand_fraction).is_none(),
8684
"Duplicate demand slicing entry (or same time slice covered by more than one entry) \
8785
(commodity: {commodity_id}, region: {region_id}, time slice: {ts})"
8886
);
89-
}
9087
}
9188
}
9289

@@ -159,7 +156,7 @@ mod tests {
159156
// Valid
160157
let demand_slice = DemandSlice {
161158
commodity_id: "commodity1".into(),
162-
regions: "GBR".into(),
159+
region_id: "GBR".into(),
163160
time_slice: "winter".into(),
164161
fraction: 1.0,
165162
};
@@ -225,13 +222,13 @@ mod tests {
225222
let demand_slices = [
226223
DemandSlice {
227224
commodity_id: "commodity1".into(),
228-
regions: "GBR".into(),
225+
region_id: "GBR".into(),
229226
time_slice: "winter".into(),
230227
fraction: 0.5,
231228
},
232229
DemandSlice {
233230
commodity_id: "commodity1".into(),
234-
regions: "GBR".into(),
231+
region_id: "GBR".into(),
235232
time_slice: "summer".into(),
236233
fraction: 0.5,
237234
},
@@ -321,7 +318,7 @@ mod tests {
321318
// Bad commodity
322319
let demand_slice = DemandSlice {
323320
commodity_id: "commodity2".into(),
324-
regions: "GBR".into(),
321+
region_id: "GBR".into(),
325322
time_slice: "winter.day".into(),
326323
fraction: 1.0,
327324
};
@@ -345,7 +342,7 @@ mod tests {
345342
// Bad region
346343
let demand_slice = DemandSlice {
347344
commodity_id: "commodity1".into(),
348-
regions: "FRA".into(),
345+
region_id: "FRA".into(),
349346
time_slice: "winter.day".into(),
350347
fraction: 1.0,
351348
};
@@ -369,7 +366,7 @@ mod tests {
369366
// Bad time slice selection
370367
let demand_slice = DemandSlice {
371368
commodity_id: "commodity1".into(),
372-
regions: "GBR".into(),
369+
region_id: "GBR".into(),
373370
time_slice: "summer".into(),
374371
fraction: 1.0,
375372
};
@@ -414,7 +411,7 @@ mod tests {
414411
};
415412
let demand_slice = DemandSlice {
416413
commodity_id: "commodity1".into(),
417-
regions: "GBR".into(),
414+
region_id: "GBR".into(),
418415
time_slice: "winter".into(),
419416
fraction: 1.0,
420417
};
@@ -438,7 +435,7 @@ mod tests {
438435
// Same time slice twice
439436
let demand_slice = DemandSlice {
440437
commodity_id: "commodity1".into(),
441-
regions: "GBR".into(),
438+
region_id: "GBR".into(),
442439
time_slice: "winter.day".into(),
443440
fraction: 0.5,
444441
};
@@ -463,13 +460,13 @@ mod tests {
463460
// Whole season and single time slice conflicting
464461
let demand_slice = DemandSlice {
465462
commodity_id: "commodity1".into(),
466-
regions: "GBR".into(),
463+
region_id: "GBR".into(),
467464
time_slice: "winter.day".into(),
468465
fraction: 0.5,
469466
};
470467
let demand_slice_season = DemandSlice {
471468
commodity_id: "commodity1".into(),
472-
regions: "GBR".into(),
469+
region_id: "GBR".into(),
473470
time_slice: "winter".into(),
474471
fraction: 0.5,
475472
};
@@ -494,7 +491,7 @@ mod tests {
494491
// Fractions don't sum to one
495492
let demand_slice = DemandSlice {
496493
commodity_id: "commodity1".into(),
497-
regions: "GBR".into(),
494+
region_id: "GBR".into(),
498495
time_slice: "winter".into(),
499496
fraction: 0.5,
500497
};

0 commit comments

Comments
 (0)