Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ edit = "0.1.5"
erased-serde = "0.4.10"
context_manager = "0.1.3"
map-macro = "0.3.0"
rayon = "1.12.0"

[dev-dependencies]
assert_cmd = "2.2.2"
Expand Down
6 changes: 3 additions & 3 deletions src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::units::Dimensionless;
use indexmap::{IndexMap, IndexSet};
use serde::Deserialize;
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::Arc;

define_id_type! {AgentID, "agent ID"}

Expand All @@ -19,7 +19,7 @@ pub type AgentMap = IndexMap<AgentID, Agent>;
pub type AgentCommodityPortionsMap = HashMap<(CommodityID, u32), Dimensionless>;

/// A map for the agent's search space, keyed by commodity, region, and year
pub type AgentSearchSpaceMap = HashMap<(CommodityID, RegionID, u32), Rc<Vec<Rc<Process>>>>;
pub type AgentSearchSpaceMap = HashMap<(CommodityID, RegionID, u32), Arc<Vec<Arc<Process>>>>;

/// A map of objectives for an agent, keyed by year.
///
Expand Down Expand Up @@ -59,7 +59,7 @@ impl Agent {
region_id: &RegionID,
commodity_id: &CommodityID,
year: u32,
) -> impl Iterator<Item = &Rc<Process>> {
) -> impl Iterator<Item = &Arc<Process>> {
self.search_space[&(commodity_id.clone(), region_id.clone(), year)].iter()
}
}
Expand Down
56 changes: 28 additions & 28 deletions src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::cmp::Ordering;
use std::collections::VecDeque;
use std::hash::{Hash, Hasher};
use std::ops::RangeInclusive;
use std::rc::Rc;
use std::sync::Arc;

mod capacity;
pub use capacity::AssetCapacity;
Expand Down Expand Up @@ -91,13 +91,13 @@ pub struct Asset {
/// The status of the asset
state: AssetState,
/// The [`Process`] that this asset corresponds to
process: Rc<Process>,
process: Arc<Process>,
/// Activity limits for this asset
activity_limits: Rc<ActivityLimits>,
activity_limits: Arc<ActivityLimits>,
/// The commodity flows for this asset
flows: Rc<IndexMap<CommodityID, ProcessFlow>>,
flows: Arc<IndexMap<CommodityID, ProcessFlow>>,
/// The [`ProcessParameter`] corresponding to the asset's region and commission year
process_parameter: Rc<ProcessParameter>,
process_parameter: Arc<ProcessParameter>,
/// The region in which the asset is located
region_id: RegionID,
/// Capacity of asset (for candidates this is a hypothetical capacity which may be altered)
Expand All @@ -111,7 +111,7 @@ pub struct Asset {
impl Asset {
/// Create a new candidate asset
pub fn new_candidate(
process: Rc<Process>,
process: Arc<Process>,
region_id: RegionID,
capacity: Capacity,
commission_year: u32,
Expand All @@ -133,7 +133,7 @@ impl Asset {
/// `candidate_asset_capacity`, regardless of whether the underlying process is divisible or
/// not.
pub fn new_candidate_for_dispatch(
process: Rc<Process>,
process: Arc<Process>,
region_id: RegionID,
capacity: Capacity,
commission_year: u32,
Expand Down Expand Up @@ -165,7 +165,7 @@ impl Asset {
#[cfg(test)]
pub fn new_ready(
agent_id: AgentID,
process: Rc<Process>,
process: Arc<Process>,
region_id: RegionID,
capacity: Capacity,
commission_year: u32,
Expand All @@ -191,7 +191,7 @@ impl Asset {
#[cfg(test)]
pub fn new_commissioned(
agent_id: AgentID,
process: Rc<Process>,
process: Arc<Process>,
region_id: RegionID,
capacity: Capacity,
commission_year: u32,
Expand All @@ -214,7 +214,7 @@ impl Asset {
/// Private helper to create an asset with the given state
fn new_with_state(
state: AssetState,
process: Rc<Process>,
process: Arc<Process>,
region_id: RegionID,
capacity: AssetCapacity,
commission_year: u32,
Expand Down Expand Up @@ -884,7 +884,7 @@ impl UserAsset {
/// Create a new [`UserAsset`]
pub fn new(
agent_id: AgentID,
process: Rc<Process>,
process: Arc<Process>,
region_id: RegionID,
capacity: Capacity,
commission_year: u32,
Expand Down Expand Up @@ -951,12 +951,12 @@ fn log_decommissioning(asset: &Asset, num_units: u32, reason: &str) {
/// otherwise using a combination of other fields which should be unique at all the relevant points
/// in the simulation.
#[derive(Clone, Debug, derive_more::Deref, derive_more::From, derive_more::Into)]
pub struct AssetRef(#[deref(forward)] Rc<Asset>);
pub struct AssetRef(#[deref(forward)] Arc<Asset>);

impl AssetRef {
/// Make a mutable reference to the underlying [`Asset`]
pub fn make_mut(&mut self) -> &mut Asset {
Rc::make_mut(&mut self.0)
Arc::make_mut(&mut self.0)
}

/// Get a representation of this [`AssetRef`] that can be used for comparisons
Expand Down Expand Up @@ -1135,7 +1135,7 @@ impl AssetRef {

impl From<Asset> for AssetRef {
fn from(value: Asset) -> Self {
Self::from(Rc::new(value))
Self::from(Arc::new(value))
}
}

Expand Down Expand Up @@ -1235,7 +1235,7 @@ mod tests {
use indexmap::indexmap;
use itertools::assert_equal;
use rstest::{fixture, rstest};
use std::rc::Rc;
use std::sync::Arc;

/// A commissioned divisible asset with three units.
#[fixture]
Expand All @@ -1253,20 +1253,20 @@ mod tests {
time_slice: TimeSliceID,
) {
// Update the process flows using the existing commodity fixture
let commodity_rc = Rc::new(svd_commodity);
let commodity_rc = Arc::new(svd_commodity);
let process_flow = ProcessFlow {
commodity: Rc::clone(&commodity_rc),
commodity: Arc::clone(&commodity_rc),
coeff: FlowPerActivity(-2.0), // Input
kind: FlowType::Fixed,
cost: MoneyPerFlow(0.0),
};
let process_flows = indexmap! { commodity_rc.id.clone() => process_flow.clone() };
let process_flows_map = process_flows_map(process.regions.clone(), Rc::new(process_flows));
let process_flows_map = process_flows_map(process.regions.clone(), Arc::new(process_flows));
process.flows = process_flows_map;

// Create asset
let asset =
Asset::new_candidate(Rc::new(process), region_id.clone(), Capacity(1.0), 2020).unwrap();
let asset = Asset::new_candidate(Arc::new(process), region_id.clone(), Capacity(1.0), 2020)
.unwrap();

// Set input prices
let mut input_prices = PriceMap::default();
Expand Down Expand Up @@ -1299,7 +1299,7 @@ mod tests {
fn asset_with_activity_limits(process_with_activity_limits: Process) -> Asset {
Asset::new_ready(
"agent1".into(),
Rc::new(process_with_activity_limits),
Arc::new(process_with_activity_limits),
"GBR".into(),
Capacity(2.0),
2010,
Expand Down Expand Up @@ -1407,14 +1407,14 @@ mod tests {
assert_eq!(asset_subset.capacity().n_units(), Some(num_units));
assert_eq!(asset_subset.id(), asset.id());
assert_eq!(asset_subset.agent_id(), asset.agent_id());
assert_eq!(Rc::ptr_eq(&asset_subset.0, &asset.0), expect_same_asset);
assert_eq!(Arc::ptr_eq(&asset_subset.0, &asset.0), expect_same_asset);
assert_eq!(asset.capacity(), AssetCapacity::Discrete(3, Capacity(4.0)));
}

#[rstest]
fn with_subset_of_units_non_divisible_asset(asset: Asset) {
let asset = AssetRef::from(asset);
assert!(Rc::ptr_eq(
assert!(Arc::ptr_eq(
&asset.0,
&asset.clone().with_subset_of_units(1).0
));
Expand Down Expand Up @@ -1516,11 +1516,11 @@ mod tests {
// Set an addition limit of 3 for (region, year 2015)
process.investment_constraints.insert(
(region_id.clone(), 2015),
Rc::new(crate::process::ProcessInvestmentConstraint {
Arc::new(crate::process::ProcessInvestmentConstraint {
addition_limit: Some(Capacity(3.0)),
}),
);
let process_rc = Rc::new(process);
let process_rc = Arc::new(process);

// Create a candidate asset with commission year 2015
let asset =
Expand Down Expand Up @@ -1625,7 +1625,7 @@ mod tests {
let asset = commissioned_divisible.with_mothballed_units(2, Some(2020));
// Requesting the same number of mothballed units is a no-op (the year is ignored)
let same = asset.clone().with_mothballed_units(2, Some(2099));
assert!(Rc::ptr_eq(&asset.0, &same.0));
assert!(Arc::ptr_eq(&asset.0, &same.0));
}

#[rstest]
Expand Down Expand Up @@ -1681,7 +1681,7 @@ mod tests {
// `asset_divisble` has no mothballed units, so the original Rc is returned unchanged
let asset = commissioned_divisible;
let same = asset.clone().with_no_mothballed_units();
assert!(Rc::ptr_eq(&asset.0, &same.0));
assert!(Arc::ptr_eq(&asset.0, &same.0));
}

#[rstest]
Expand All @@ -1704,7 +1704,7 @@ mod tests {
.clone()
.with_decommission_mothballed(2025, 20)
.unwrap();
assert!(Rc::ptr_eq(&asset.0, &result.0));
assert!(Arc::ptr_eq(&asset.0, &result.0));
}

#[rstest]
Expand Down
24 changes: 12 additions & 12 deletions src/asset/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ mod tests {
use itertools::{Itertools, assert_equal};
use rstest::{fixture, rstest};
use std::iter;
use std::rc::Rc;
use std::sync::Arc;

#[fixture]
fn user_assets(mut process: Process) -> Vec<UserAsset> {
Expand All @@ -212,12 +212,12 @@ mod tests {
let process_parameter_map = process_parameter_map(process.regions.clone(), process_param);
process.parameters = process_parameter_map;

let rc_process = Rc::new(process);
let rc_process = Arc::new(process);
[2020, 2010]
.map(|year| {
UserAsset::new(
"agent1".into(),
Rc::clone(&rc_process),
Arc::clone(&rc_process),
"GBR".into(),
Capacity(1.0),
year,
Expand Down Expand Up @@ -346,11 +346,11 @@ mod tests {
let original_count = asset_pool.assets.len();

// Create new non-commissioned assets
let process_rc = Rc::new(process);
let process_rc = Arc::new(process);
let new_assets = vec![
Asset::new_ready(
"agent2".into(),
Rc::clone(&process_rc),
Arc::clone(&process_rc),
"GBR".into(),
Capacity(1.5),
2015,
Expand All @@ -359,7 +359,7 @@ mod tests {
.into(),
Asset::new_ready(
"agent3".into(),
Rc::clone(&process_rc),
Arc::clone(&process_rc),
"GBR".into(),
Capacity(2.5),
2020,
Expand Down Expand Up @@ -425,11 +425,11 @@ mod tests {
asset_pool.commission_new(2020, &mut user_assets);

// Create new assets that would be out of order if added at the end
let process_rc = Rc::new(process);
let process_rc = Arc::new(process);
let new_assets = vec![
Asset::new_ready(
"agent_high_id".into(),
Rc::clone(&process_rc),
Arc::clone(&process_rc),
"GBR".into(),
Capacity(1.0),
2010,
Expand All @@ -438,7 +438,7 @@ mod tests {
.into(),
Asset::new_ready(
"agent_low_id".into(),
Rc::clone(&process_rc),
Arc::clone(&process_rc),
"GBR".into(),
Capacity(1.0),
2015,
Expand Down Expand Up @@ -481,11 +481,11 @@ mod tests {
assert_eq!(asset_pool.next_id, 2); // Should be 2 after commissioning 2 assets

// Create new non-commissioned assets
let process_rc = Rc::new(process);
let process_rc = Arc::new(process);
let new_assets = vec![
Asset::new_ready(
"agent1".into(),
Rc::clone(&process_rc),
Arc::clone(&process_rc),
"GBR".into(),
Capacity(1.0),
2015,
Expand All @@ -494,7 +494,7 @@ mod tests {
.into(),
Asset::new_ready(
"agent2".into(),
Rc::clone(&process_rc),
Arc::clone(&process_rc),
"GBR".into(),
Capacity(1.0),
2020,
Expand Down
4 changes: 2 additions & 2 deletions src/commodity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use crate::units::{Flow, MoneyPerFlow};
use indexmap::IndexMap;
use serde::Deserialize;
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::Arc;

define_id_type! {CommodityID, "commodity ID"}

/// A map of [`Commodity`]s, keyed by commodity ID
pub type CommodityMap = IndexMap<CommodityID, Rc<Commodity>>;
pub type CommodityMap = IndexMap<CommodityID, Arc<Commodity>>;

/// A map of [`MoneyPerFlow`]s, keyed by region ID, year and time slice ID for a specific levy
pub type CommodityLevyMap = HashMap<(RegionID, u32, TimeSliceID), MoneyPerFlow>;
Expand Down
Loading
Loading