Skip to content

Add region key to FlowMap#1311

Draft
tsmbland wants to merge 1 commit into
mainfrom
flows_map
Draft

Add region key to FlowMap#1311
tsmbland wants to merge 1 commit into
mainfrom
flows_map

Conversation

@tsmbland
Copy link
Copy Markdown
Collaborator

@tsmbland tsmbland commented May 22, 2026

Description

Adds a region field to FlowMap to allow for the possibility of flow regions differing from the region of the asset. This will be necessary for trade as we'll probably want to store trade flows in this map as well. (Or will we... this map needs an asset key, so if we wanted trade processes to exist without assets, then trade flows would need to be stored in a different map. Not sure!)

Fixes #1309

Type of change

  • Bug fix (non-breaking change to fix an issue)
  • New feature (non-breaking change to add functionality)
  • Refactoring (non-breaking, non-functional change to improve maintainability)
  • Optimization (non-breaking change to speed up the code)
  • Breaking change (whatever its nature)
  • Documentation (improve or add documentation)

Key checklist

  • All tests pass: $ cargo test
  • The documentation builds and looks OK: $ cargo doc
  • Update release notes for the latest release if this PR adds a new feature or fixes a bug
    present in the previous release

Further checks

  • Code is commented, particularly in hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

@tsmbland tsmbland changed the title Add region key to FlowMap Add region key to FlowMap May 22, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.70%. Comparing base (51590fa) to head (6723361).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1311      +/-   ##
==========================================
+ Coverage   89.69%   89.70%   +0.01%     
==========================================
  Files          58       58              
  Lines        8470     8481      +11     
  Branches     8470     8481      +11     
==========================================
+ Hits         7597     7608      +11     
  Misses        566      566              
  Partials      307      307              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tsmbland
Copy link
Copy Markdown
Collaborator Author

@alexdewar Could also go further than this and add a region field to ProcessFlow, but depends if we want to use ProcessFlow for trade "processes"

Maybe best to leave this as a draft for now and see how we get on with the rest of it

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds an explicit RegionID dimension to the optimisation FlowMap key so flows can be attributed to a (potentially different) region than the asset’s region, in preparation for cross-region/trade flow handling (Fixes #1309).

Changes:

  • Extend FlowMap key from (AssetRef, CommodityID, TimeSliceID) to (AssetRef, CommodityID, RegionID, TimeSliceID).
  • Update flow-map construction and downstream consumers (investment net demand update, flow CSV writer, and related tests) to handle the new key shape.
  • Adjust parent/child flow copying and map retention logic for the new tuple arity.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/simulation/optimisation.rs Updates FlowMap type and populates RegionID in flow keys during flow-map creation and parent→child copying.
src/simulation/investment.rs Uses the RegionID from FlowMap keys when updating the net demand map.
src/output.rs Updates flow writing/test to destructure the new FlowMap key shape (but currently ignores the region value).
Comments suppressed due to low confidence (1)

src/output.rs:637

  • write_flows destructures and then discards region_id (_region_id). If FlowMap now allows flow regions to differ from asset.region_id(), the CSV output will silently lose that information and may misattribute flows when trade is introduced. Consider adding a region_id (or flow_region_id to disambiguate) column to CommodityFlowRow and populating it from the FlowMap key, updating the corresponding CSV test expectations as well.
        for ((asset, commodity_id, _region_id, time_slice), flow) in flow_map {
            let row = CommodityFlowRow {
                milestone_year,
                asset_id: asset.id().unwrap(),
                commodity_id: commodity_id.clone(),
                time_slice: time_slice.clone(),
                flow: *flow,

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 500 to 504
fn update_net_demand_map(demand: &mut AllDemandMap, flows: &FlowMap, assets: &[AssetRef]) {
for ((asset, commodity_id, time_slice), flow) in flows {
for ((asset, commodity_id, region_id, time_slice), flow) in flows {
if assets.contains(asset) {
let key = (
commodity_id.clone(),
asset.region_id().clone(),
time_slice.clone(),
);
let key = (commodity_id.clone(), region_id.clone(), time_slice.clone());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flows map should be updated to allow flows between regions

2 participants