Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
5fbcff8
Fix: Don't reuse mutable demand object between tests
alexdewar Nov 14, 2024
754af64
Rename: `DemandHashMap` => `CommodityDemandMap`
alexdewar Nov 25, 2024
c0f5935
Make bespoke `DemandMap` type with `get` method
alexdewar Nov 25, 2024
f90f1a7
Require demand entry year to be milestone year
alexdewar Nov 25, 2024
181d3d1
Check demand quantity is valid
alexdewar Nov 14, 2024
e260ac2
Move check that fractions sum to one to input.rs
alexdewar Nov 12, 2024
422ec1d
Compute demand for each time slice when loading data
alexdewar Dec 12, 2024
b15b63a
Remove unnecessarily verbose error-checking
alexdewar Dec 17, 2024
49c180a
Require demand entries for all milestone years
alexdewar Dec 12, 2024
f9cc83c
Check for demand slicing provided without corresponding entry in dema…
alexdewar Dec 17, 2024
b88758b
Merge branch 'main' into demand-time-slice-map
alexdewar Jan 13, 2025
b91fc83
Remove outdated comment
alexdewar Jan 13, 2025
04522d5
Move demand CSV code into submodule of `input`
alexdewar Jan 13, 2025
48ba07c
Rename: mod test to mod tests
alexdewar Jan 13, 2025
10aa75c
Move demand slicing code into own submodule
alexdewar Jan 13, 2025
bcf643f
Rename: DEMAND_SLICES_FILE_NAME => DEMAND_SLICING_FILE_NAME
alexdewar Jan 13, 2025
1fa3f1e
Remove redundant check
alexdewar Jan 13, 2025
941292c
Add a longer doc comment for `compute_demand_map`
alexdewar Jan 13, 2025
de1b4a6
Rename: `compute_demand_map` => `compute_demand_maps`
alexdewar Jan 13, 2025
1cbee62
TimeSliceInfo: iter{_selection}: Return fraction along with TimeSliceID
alexdewar Jan 14, 2025
69aa5d6
Put calculation of time slice share into separate function
alexdewar Jan 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/simple/demand.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
commodity_id,region_id,year,demand
RSHEAT,GBR,2020,927.38
RSHEAT,GBR,2100,927.38
61 changes: 59 additions & 2 deletions src/commodity.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![allow(missing_docs)]
use crate::demand::Demand;
use crate::input::*;
use crate::time_slice::{TimeSliceID, TimeSliceLevel};
use serde::Deserialize;
Expand All @@ -23,7 +22,7 @@ pub struct Commodity {
#[serde(skip)]
pub costs: CommodityCostMap,
#[serde(skip)]
pub demand_by_region: HashMap<Rc<str>, Demand>,
pub demand: DemandMap,
}
define_id_getter! {Commodity}

Expand Down Expand Up @@ -110,10 +109,68 @@ pub enum CommodityType {
OutputCommodity,
}

/// A map relating region, year and time slice to demand (in real units, not a fraction).
///
/// This data type is exported as this is the way in we want to look up demand outside of this
/// module.
#[derive(PartialEq, Debug, Clone, Default)]
pub struct DemandMap(HashMap<DemandMapKey, f64>);

/// The key for a [`DemandMap`]
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
struct DemandMapKey {
region_id: Rc<str>,
year: u32,
time_slice: TimeSliceID,
}

impl DemandMap {
/// Create a new, empty [`DemandMap`]
pub fn new() -> DemandMap {
DemandMap::default()
}

/// Retrieve the demand for the specified region, year and time slice
pub fn get(&self, region_id: Rc<str>, year: u32, time_slice: TimeSliceID) -> Option<f64> {
self.0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why .0?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DemandMap is what's called a newtype. Essentially it's a struct with an unnamed HashMap inside it, and self.0 is how you access it.

.get(&DemandMapKey {
region_id,
year,
time_slice,
})
.copied()
}

/// Insert a new demand entry for the specified region, year and time slice
pub fn insert(&mut self, region_id: Rc<str>, year: u32, time_slice: TimeSliceID, demand: f64) {
self.0.insert(
DemandMapKey {
region_id,
year,
time_slice,
},
demand,
);
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_demand_map() {
let time_slice = TimeSliceID {
season: "all-year".into(),
time_of_day: "all-day".into(),
};
let value = 0.25;
let mut map = DemandMap::new();
map.insert("North".into(), 2020, time_slice.clone(), value);

assert_eq!(map.get("North".into(), 2020, time_slice).unwrap(), value)
}

#[test]
fn test_commodity_cost_map() {
let ts = TimeSliceID {
Expand Down
Loading
Loading