Skip to content

Commit c4b3d76

Browse files
committed
AssetPool: Represent assets with AssetRefs
1 parent e241c01 commit c4b3d76

4 files changed

Lines changed: 22 additions & 25 deletions

File tree

src/asset.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl Asset {
143143
///
144144
/// An [`AssetRef`] is guaranteed to have been commissioned at some point, though it may
145145
/// subsequently have been decommissioned.
146-
#[derive(Clone)]
146+
#[derive(Clone, Debug)]
147147
pub struct AssetRef(Rc<Asset>);
148148

149149
impl From<Rc<Asset>> for AssetRef {
@@ -199,7 +199,7 @@ impl Hash for AssetRef {
199199
/// A pool of [`Asset`]s
200200
pub struct AssetPool {
201201
/// The pool of active assets
202-
active: Vec<Rc<Asset>>,
202+
active: Vec<AssetRef>,
203203
/// Assets that have not yet been commissioned, sorted by commission year
204204
future: Vec<Asset>,
205205
/// Next available asset ID number
@@ -245,9 +245,9 @@ impl AssetPool {
245245
///
246246
/// # Returns
247247
///
248-
/// Reference to an [`Asset`] if found, else `None`. The asset may not be found if it has
249-
/// already been decommissioned.
250-
pub fn get(&self, id: AssetID) -> Option<&Rc<Asset>> {
248+
/// An [`AssetRef`] if found, else `None`. The asset may not be found if it has already been
249+
/// decommissioned.
250+
pub fn get(&self, id: AssetID) -> Option<&AssetRef> {
251251
// The assets in `active` are in order of ID
252252
let idx = self
253253
.active
@@ -258,15 +258,15 @@ impl AssetPool {
258258
}
259259

260260
/// Iterate over active assets
261-
pub fn iter(&self) -> impl Iterator<Item = &Rc<Asset>> {
261+
pub fn iter(&self) -> impl Iterator<Item = &AssetRef> {
262262
self.active.iter()
263263
}
264264

265265
/// Iterate over active assets for a particular region
266266
pub fn iter_for_region<'a>(
267267
&'a self,
268268
region_id: &'a RegionID,
269-
) -> impl Iterator<Item = &'a Rc<Asset>> {
269+
) -> impl Iterator<Item = &'a AssetRef> {
270270
self.iter().filter(|asset| asset.region_id == *region_id)
271271
}
272272

@@ -276,7 +276,7 @@ impl AssetPool {
276276
&'a self,
277277
region_id: &'a RegionID,
278278
commodity_id: &'a CommodityID,
279-
) -> impl Iterator<Item = &'a Rc<Asset>> {
279+
) -> impl Iterator<Item = &'a AssetRef> {
280280
self.iter_for_region(region_id).filter(|asset| {
281281
asset.process.contains_commodity_flow(
282282
commodity_id,
@@ -294,7 +294,7 @@ impl AssetPool {
294294
let new_pool = assets.into_iter().map(|asset| {
295295
if asset.id != AssetID::INVALID {
296296
// Already commissioned
297-
asset
297+
asset.into()
298298
} else {
299299
// Newly created from process. We need to assign an ID.
300300
let mut asset = asset.as_ref().clone();
@@ -514,7 +514,7 @@ mod tests {
514514
fn test_asset_pool_replace_active_pool_existing(mut asset_pool: AssetPool) {
515515
asset_pool.commission_new(2020);
516516
assert_eq!(asset_pool.active.len(), 2);
517-
asset_pool.replace_active_pool(iter::once(asset_pool.active[1].clone()));
517+
asset_pool.replace_active_pool(iter::once(asset_pool.active[1].clone().into()));
518518
assert_eq!(asset_pool.active.len(), 1);
519519
assert_eq!(asset_pool.active[0].id, AssetID(1));
520520
}

src/output.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use serde::{Deserialize, Serialize};
1313
use std::fs;
1414
use std::fs::File;
1515
use std::path::{Path, PathBuf};
16-
use std::rc::Rc;
1716

1817
/// The root folder in which model-specific output folders will be created
1918
const OUTPUT_DIRECTORY_ROOT: &str = "muse2_results";
@@ -259,7 +258,7 @@ impl DataWriter {
259258
/// Write assets to a CSV file
260259
pub fn write_assets<'a, I>(&mut self, milestone_year: u32, assets: I) -> Result<()>
261260
where
262-
I: Iterator<Item = &'a Rc<Asset>>,
261+
I: Iterator<Item = &'a AssetRef>,
263262
{
264263
for asset in assets {
265264
let row = AssetRow::new(milestone_year, asset);
@@ -329,30 +328,28 @@ impl DataWriter {
329328
mod tests {
330329
use super::*;
331330
use crate::asset::AssetPool;
332-
use crate::fixture::{asset, assets, commodity_id, region_id, time_slice};
331+
use crate::fixture::{assets, commodity_id, region_id, time_slice};
333332
use crate::time_slice::TimeSliceID;
334333
use itertools::{assert_equal, Itertools};
335334
use rstest::rstest;
336335
use std::iter;
337336
use tempfile::tempdir;
338337

339338
#[rstest]
340-
fn test_write_assets(asset: Asset) {
341-
let asset = asset.into();
339+
fn test_write_assets(assets: AssetPool) {
342340
let milestone_year = 2020;
343341
let dir = tempdir().unwrap();
344342

345343
// Write an asset
346344
{
347345
let mut writer = DataWriter::create(dir.path(), false).unwrap();
348-
writer
349-
.write_assets(milestone_year, iter::once(&asset))
350-
.unwrap();
346+
writer.write_assets(milestone_year, assets.iter()).unwrap();
351347
writer.flush().unwrap();
352348
}
353349

354350
// Read back and compare
355-
let expected = AssetRow::new(milestone_year, &asset);
351+
let asset = assets.iter().next().unwrap();
352+
let expected = AssetRow::new(milestone_year, asset);
356353
let records: Vec<AssetRow> = csv::Reader::from_path(dir.path().join(ASSETS_FILE_NAME))
357354
.unwrap()
358355
.into_deserialize()
@@ -364,8 +361,8 @@ mod tests {
364361
#[rstest]
365362
fn test_write_flows(assets: AssetPool, commodity_id: CommodityID, time_slice: TimeSliceID) {
366363
let milestone_year = 2020;
367-
let asset = assets.iter().next().unwrap().into();
368-
let flow_item = (&asset, &commodity_id, &time_slice, 42.0);
364+
let asset = assets.iter().next().unwrap();
365+
let flow_item = (asset, &commodity_id, &time_slice, 42.0);
369366

370367
// Write a flow
371368
let dir = tempdir().unwrap();
@@ -469,13 +466,13 @@ mod tests {
469466
let milestone_year = 2020;
470467
let value = 0.5;
471468
let dir = tempdir().unwrap();
472-
let asset = assets.iter().next().unwrap().into();
469+
let asset = assets.iter().next().unwrap();
473470

474471
// Write capacity dual
475472
{
476473
let mut writer = DebugDataWriter::create(dir.path()).unwrap();
477474
writer
478-
.write_capacity_duals(milestone_year, iter::once((&asset, &time_slice, value)))
475+
.write_capacity_duals(milestone_year, iter::once((asset, &time_slice, value)))
479476
.unwrap();
480477
writer.flush().unwrap();
481478
}

src/simulation/investment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub fn perform_agent_investment(
2424
let mut new_pool = Vec::new();
2525
for asset in assets.iter() {
2626
// **TODO**: Implement agent investment. For now, just keep all assets.
27-
new_pool.push(asset.clone());
27+
new_pool.push(asset.clone().into());
2828
}
2929

3030
assets.replace_active_pool(new_pool);

src/simulation/optimisation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ fn add_variables(
184184
for time_slice in model.time_slice_info.iter_ids() {
185185
let coeff = calculate_cost_coefficient(asset, year, time_slice);
186186
let var = problem.add_column(coeff, 0.0..);
187-
let key = (asset.into(), time_slice.clone());
187+
let key = (asset.clone(), time_slice.clone());
188188
let existing = variables.0.insert(key, var).is_some();
189189
assert!(!existing, "Duplicate entry for var");
190190
}

0 commit comments

Comments
 (0)