Skip to content

Commit e894bb5

Browse files
authored
Merge pull request #659 from EnergySystemsModellingLab/more-unit-tweaks
More tweaks to unit code
2 parents 8c08999 + 4d25bca commit e894bb5

10 files changed

Lines changed: 158 additions & 173 deletions

File tree

src/commodity.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use crate::id::{define_id_getter, define_id_type};
33
use crate::region::RegionID;
44
use crate::time_slice::{TimeSliceID, TimeSliceLevel, TimeSliceSelection};
5-
use crate::units::{Energy, MoneyPerEnergy};
5+
use crate::units::{Flow, MoneyPerFlow};
66
use indexmap::IndexMap;
77
use serde::Deserialize;
88
use serde_string_enum::DeserializeLabeledStringEnum;
@@ -18,7 +18,7 @@ pub type CommodityMap = IndexMap<CommodityID, Rc<Commodity>>;
1818
pub type CommodityLevyMap = HashMap<(RegionID, u32, TimeSliceID), CommodityLevy>;
1919

2020
/// A map of demand values, keyed by region ID, year and time slice selection
21-
pub type DemandMap = HashMap<(RegionID, u32, TimeSliceSelection), Energy>;
21+
pub type DemandMap = HashMap<(RegionID, u32, TimeSliceSelection), Flow>;
2222

2323
/// A commodity within the simulation.
2424
///
@@ -75,7 +75,7 @@ pub struct CommodityLevy {
7575
/// Type of balance for application of cost
7676
pub balance_type: BalanceType,
7777
/// Cost per unit commodity
78-
pub value: MoneyPerEnergy,
78+
pub value: MoneyPerFlow,
7979
}
8080

8181
/// Commodity balance type
@@ -105,7 +105,7 @@ mod tests {
105105
season: "all-year".into(),
106106
time_of_day: "all-day".into(),
107107
});
108-
let value = Energy(0.25);
108+
let value = Flow(0.25);
109109
let mut map = DemandMap::new();
110110
map.insert(("North".into(), 2020, ts_selection.clone()), value);
111111

@@ -123,7 +123,7 @@ mod tests {
123123
};
124124
let value = CommodityLevy {
125125
balance_type: BalanceType::Consumption,
126-
value: MoneyPerEnergy(0.5),
126+
value: MoneyPerFlow(0.5),
127127
};
128128
let mut map = CommodityLevyMap::new();
129129
assert!(map

src/input/commodity/demand.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::commodity::{Commodity, CommodityID, CommodityType, DemandMap};
66
use crate::id::IDCollection;
77
use crate::region::RegionID;
88
use crate::time_slice::{TimeSliceInfo, TimeSliceLevel};
9-
use crate::units::Energy;
9+
use crate::units::Flow;
1010
use anyhow::{ensure, Result};
1111
use itertools::iproduct;
1212
use serde::Deserialize;
@@ -25,11 +25,11 @@ struct Demand {
2525
/// The year of the demand entry
2626
year: u32,
2727
/// Annual demand quantity
28-
demand: Energy,
28+
demand: Flow,
2929
}
3030

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

3434
/// A map containing a references to commodities
3535
pub type BorrowedCommodityMap<'a> = HashMap<CommodityID, &'a Commodity>;
@@ -133,7 +133,7 @@ where
133133
);
134134

135135
ensure!(
136-
demand.demand.is_normal() && demand.demand > Energy(0.0),
136+
demand.demand.is_normal() && demand.demand > Flow(0.0),
137137
"Demand must be a valid number greater than zero"
138138
);
139139

@@ -232,13 +232,13 @@ mod tests {
232232
year: 2020,
233233
region_id: "GBR".to_string(),
234234
commodity_id: "commodity1".to_string(),
235-
demand: Energy(10.0),
235+
demand: Flow(10.0),
236236
},
237237
Demand {
238238
year: 2020,
239239
region_id: "USA".to_string(),
240240
commodity_id: "commodity1".to_string(),
241-
demand: Energy(11.0),
241+
demand: Flow(11.0),
242242
},
243243
];
244244

@@ -261,13 +261,13 @@ mod tests {
261261
year: 2020,
262262
region_id: "GBR".to_string(),
263263
commodity_id: "commodity2".to_string(),
264-
demand: Energy(10.0),
264+
demand: Flow(10.0),
265265
},
266266
Demand {
267267
year: 2020,
268268
region_id: "USA".to_string(),
269269
commodity_id: "commodity1".to_string(),
270-
demand: Energy(11.0),
270+
demand: Flow(11.0),
271271
},
272272
];
273273
assert_error!(
@@ -288,13 +288,13 @@ mod tests {
288288
year: 2020,
289289
region_id: "FRA".to_string(),
290290
commodity_id: "commodity1".to_string(),
291-
demand: Energy(10.0),
291+
demand: Flow(10.0),
292292
},
293293
Demand {
294294
year: 2020,
295295
region_id: "USA".to_string(),
296296
commodity_id: "commodity1".to_string(),
297-
demand: Energy(11.0),
297+
demand: Flow(11.0),
298298
},
299299
];
300300
assert_error!(
@@ -315,13 +315,13 @@ mod tests {
315315
year: 2010,
316316
region_id: "GBR".to_string(),
317317
commodity_id: "commodity1".to_string(),
318-
demand: Energy(10.0),
318+
demand: Flow(10.0),
319319
},
320320
Demand {
321321
year: 2020,
322322
region_id: "USA".to_string(),
323323
commodity_id: "commodity1".to_string(),
324-
demand: Energy(11.0),
324+
demand: Flow(11.0),
325325
},
326326
];
327327
assert_error!(
@@ -348,7 +348,7 @@ mod tests {
348348
year: 2020,
349349
region_id: "GBR".to_string(),
350350
commodity_id: "commodity1".to_string(),
351-
demand: Energy(quantity),
351+
demand: Flow(quantity),
352352
}];
353353
assert_error!(
354354
read_demand_from_iter(demand.into_iter(), &svd_commodities, &region_ids, &[2020],),
@@ -368,19 +368,19 @@ mod tests {
368368
year: 2020,
369369
region_id: "GBR".to_string(),
370370
commodity_id: "commodity1".to_string(),
371-
demand: Energy(10.0),
371+
demand: Flow(10.0),
372372
},
373373
Demand {
374374
year: 2020,
375375
region_id: "GBR".to_string(),
376376
commodity_id: "commodity1".to_string(),
377-
demand: Energy(10.0),
377+
demand: Flow(10.0),
378378
},
379379
Demand {
380380
year: 2020,
381381
region_id: "USA".to_string(),
382382
commodity_id: "commodity1".to_string(),
383-
demand: Energy(11.0),
383+
demand: Flow(11.0),
384384
},
385385
];
386386
assert_error!(
@@ -400,7 +400,7 @@ mod tests {
400400
year: 2020,
401401
region_id: "GBR".to_string(),
402402
commodity_id: "commodity1".to_string(),
403-
demand: Energy(10.0),
403+
demand: Flow(10.0),
404404
};
405405
assert!(read_demand_from_iter(
406406
std::iter::once(demand),
@@ -433,11 +433,11 @@ mod tests {
433433
let expected = AnnualDemandMap::from_iter([
434434
(
435435
("commodity1".into(), "GBR".into(), 2020),
436-
(TimeSliceLevel::DayNight, Energy(10.0)),
436+
(TimeSliceLevel::DayNight, Flow(10.0)),
437437
),
438438
(
439439
("commodity1".into(), "USA".into(), 2020),
440-
(TimeSliceLevel::DayNight, Energy(11.0)),
440+
(TimeSliceLevel::DayNight, Flow(11.0)),
441441
),
442442
]);
443443
let demand =

src/input/commodity/levy.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::commodity::{BalanceType, CommodityID, CommodityLevy, CommodityLevyMap
44
use crate::id::IDCollection;
55
use crate::region::{parse_region_str, RegionID};
66
use crate::time_slice::TimeSliceInfo;
7-
use crate::units::MoneyPerEnergy;
7+
use crate::units::MoneyPerFlow;
88
use crate::year::parse_year_str;
99
use anyhow::{ensure, Context, Result};
1010
use serde::Deserialize;
@@ -27,7 +27,7 @@ struct CommodityLevyRaw {
2727
/// The time slice to which the cost applies.
2828
time_slice: String,
2929
/// Cost per unit commodity
30-
value: MoneyPerEnergy,
30+
value: MoneyPerFlow,
3131
}
3232

3333
/// Read costs associated with each commodity from levies CSV file.
@@ -163,7 +163,7 @@ mod tests {
163163
fn cost_map(time_slice: TimeSliceID) -> CommodityLevyMap {
164164
let cost = CommodityLevy {
165165
balance_type: BalanceType::Net,
166-
value: MoneyPerEnergy(1.0),
166+
value: MoneyPerFlow(1.0),
167167
};
168168

169169
let mut map = CommodityLevyMap::new();

src/input/process.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::process::{
66
};
77
use crate::region::{parse_region_str, RegionID};
88
use crate::time_slice::{TimeSliceInfo, TimeSliceSelection};
9-
use crate::units::{Energy, EnergyPerActivity};
9+
use crate::units::{Flow, FlowPerActivity};
1010
use anyhow::{ensure, Context, Ok, Result};
1111
use itertools::iproduct;
1212
use serde::Deserialize;
@@ -200,7 +200,7 @@ fn validate_other_commodity(
200200
let mut is_producer = None;
201201
for flows in flows.values().flat_map(|flows| flows.values()) {
202202
if let Some(flow) = flows.get(commodity_id) {
203-
let cur_is_producer = flow.coeff > EnergyPerActivity(0.0);
203+
let cur_is_producer = flow.coeff > FlowPerActivity(0.0);
204204
if let Some(is_producer) = is_producer {
205205
ensure!(
206206
is_producer == cur_is_producer,
@@ -233,9 +233,9 @@ fn validate_sed_commodity(
233233
for flows in flows.values() {
234234
let flows = flows.get(&(region_id.clone(), year)).unwrap();
235235
if let Some(flow) = flows.get(&commodity_id.clone()) {
236-
if flow.coeff > EnergyPerActivity(0.0) {
236+
if flow.coeff > FlowPerActivity(0.0) {
237237
has_producer = true;
238-
} else if flow.coeff < EnergyPerActivity(0.0) {
238+
} else if flow.coeff < FlowPerActivity(0.0) {
239239
has_consumer = true;
240240
}
241241
}
@@ -266,7 +266,7 @@ fn validate_svd_commodity(
266266
.demand
267267
.get(&(region_id.clone(), year, ts_selection.clone()))
268268
.unwrap();
269-
if demand <= Energy(0.0) {
269+
if demand <= Flow(0.0) {
270270
return Ok(());
271271
}
272272

@@ -279,7 +279,7 @@ fn validate_svd_commodity(
279279
continue;
280280
};
281281
ensure!(
282-
flow.coeff > EnergyPerActivity(0.0),
282+
flow.coeff > FlowPerActivity(0.0),
283283
"SVD commodity {} is consumed by process {}. \
284284
SVD commodities can only be produced, not consumed.",
285285
commodity.id,
@@ -315,7 +315,7 @@ mod tests {
315315
use crate::fixture::{assert_error, time_slice, time_slice_info};
316316
use crate::process::{FlowType, ProcessFlow};
317317
use crate::time_slice::{TimeSliceID, TimeSliceLevel};
318-
use crate::units::{Dimensionless, EnergyPerActivity, MoneyPerEnergy};
318+
use crate::units::{Dimensionless, FlowPerActivity, MoneyPerFlow};
319319
use indexmap::indexmap;
320320
use rstest::{fixture, rstest};
321321
use std::iter;
@@ -338,9 +338,9 @@ mod tests {
338338
("GBR".into(), 2010),
339339
indexmap! { commodity_sed.id.clone() => ProcessFlow {
340340
commodity: commodity_sed.into(),
341-
coeff: EnergyPerActivity(-10.0),
341+
coeff: FlowPerActivity(-10.0),
342342
kind: FlowType::Fixed,
343-
cost: MoneyPerEnergy(1.0),
343+
cost: MoneyPerFlow(1.0),
344344
is_primary_output: false,
345345
}},
346346
)])
@@ -352,9 +352,9 @@ mod tests {
352352
("GBR".into(), 2010),
353353
indexmap! {commodity_sed.id.clone()=>ProcessFlow {
354354
commodity: commodity_sed.into(),
355-
coeff: EnergyPerActivity(10.0),
355+
coeff: FlowPerActivity(10.0),
356356
kind: FlowType::Fixed,
357-
cost: MoneyPerEnergy(1.0),
357+
cost: MoneyPerFlow(1.0),
358358
is_primary_output: false,
359359
}},
360360
)])
@@ -399,8 +399,7 @@ mod tests {
399399

400400
#[fixture]
401401
fn commodity_svd(time_slice: TimeSliceID) -> Commodity {
402-
let demand =
403-
DemandMap::from_iter([(("GBR".into(), 2010, time_slice.into()), Energy(10.0))]);
402+
let demand = DemandMap::from_iter([(("GBR".into(), 2010, time_slice.into()), Flow(10.0))]);
404403

405404
Commodity {
406405
id: "commodity_svd".into(),
@@ -420,9 +419,9 @@ mod tests {
420419
("GBR".into(), 2010),
421420
indexmap! { commodity_svd.id.clone() => ProcessFlow {
422421
commodity: commodity_svd.into(),
423-
coeff: EnergyPerActivity(10.0),
422+
coeff: FlowPerActivity(10.0),
424423
kind: FlowType::Fixed,
425-
cost: MoneyPerEnergy(1.0),
424+
cost: MoneyPerFlow(1.0),
426425
is_primary_output: false,
427426
}},
428427
)]),
@@ -505,9 +504,9 @@ mod tests {
505504
("GBR".into(), 2010),
506505
indexmap! { commodity_other.id.clone() => ProcessFlow {
507506
commodity: commodity_other.into(),
508-
coeff: EnergyPerActivity(10.0),
507+
coeff: FlowPerActivity(10.0),
509508
kind: FlowType::Fixed,
510-
cost: MoneyPerEnergy(1.0),
509+
cost: MoneyPerFlow(1.0),
511510
is_primary_output: false,
512511
}},
513512
)])
@@ -519,9 +518,9 @@ mod tests {
519518
("GBR".into(), 2010),
520519
indexmap! { commodity_other.id.clone() => ProcessFlow {
521520
commodity: commodity_other.into(),
522-
coeff: EnergyPerActivity(-10.0),
521+
coeff: FlowPerActivity(-10.0),
523522
kind: FlowType::Fixed,
524-
cost: MoneyPerEnergy(1.0),
523+
cost: MoneyPerFlow(1.0),
525524
is_primary_output: false,
526525
}},
527526
)])
@@ -596,9 +595,9 @@ mod tests {
596595
(region_id.clone(), 2010),
597596
indexmap! { commodity_svd.id.clone() => ProcessFlow {
598597
commodity: Rc::clone(&commodity_svd),
599-
coeff: EnergyPerActivity(-10.0),
598+
coeff: FlowPerActivity(-10.0),
600599
kind: FlowType::Fixed,
601-
cost: MoneyPerEnergy(1.0),
600+
cost: MoneyPerFlow(1.0),
602601
is_primary_output: false,
603602
}},
604603
)]),

0 commit comments

Comments
 (0)