Skip to content

Combine region graphs into single graph for each year - #1569

Open
tsmbland wants to merge 6 commits into
mainfrom
graph_multi_region
Open

tsmbland wants to merge 6 commits into
mainfrom
graph_multi_region

Conversation

@tsmbland

@tsmbland tsmbland commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

Description

We currently build separate commodity graphs for each region, which we then combine into a single investment graph to get the investment order. This works ok for now, as there aren't any edges between regions (that would imply trade), but this is surely something that we'll want. The way I see it, there's no reason to have multi-region models if we don't have trade, so either way building separate graphs for different regions is a bit dumb. Not a high priority, but I think there will be some changes incoming to the graph code, and I wanted to get this out of the way. I think it's also a bit more consistent with how the rest of the code works.

I first tried this when we were discussing trade (https://github.com/EnergySystemsModellingLab/MUSE2/tree/rough_trade_implementation), and I've mostly just copied over some of the changes from that. There are some other changes in that branch about allowing flows between regions, and prettifying the graph, which I haven't copied over, but these can come later.

Copilot has flagged some performance issues, which I think I agree with, although they're probably minor.

Fixes # (issue)

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

@codecov

codecov Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.42857% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.90%. Comparing base (1b9a848) to head (44a754e).
⚠️ Report is 8 commits behind head on main.

Files with missing lines Patch % Lines
src/graph.rs 88.46% 3 Missing and 3 partials ⚠️
src/graph/validate.rs 86.66% 1 Missing and 1 partial ⚠️
src/graph/investment.rs 97.29% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1569      +/-   ##
==========================================
+ Coverage   90.88%   90.90%   +0.01%     
==========================================
  Files          61       61              
  Lines        9121     9116       -5     
  Branches     9121     9116       -5     
==========================================
- Hits         8290     8287       -3     
+ Misses        509      507       -2     
  Partials      322      322              

☔ View full report in Codecov by Harness.
📢 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.

@github-actions github-actions Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.30.

Benchmark suite Current: ee7882f Previous: 6a19604 Ratio
example_run/two_outputs example 639063963.5 ns 485146523.5 ns 1.32
select_best_assets_parallel/01 13654279.702380951 ns 9132896.765151516 ns 1.50
select_best_assets_parallel/05 38994977.875 ns 26819628.777777776 ns 1.45
select_best_assets_parallel/10 66406262.33333333 ns 45113674.75 ns 1.47
select_best_assets_parallel/15 96065833.75 ns 65351435.33333333 ns 1.47
select_best_assets_parallel/20 129487808.75 ns 85097739.5 ns 1.52
select_best_assets_sequential/01 13742472.66025641 ns 9107675.635526314 ns 1.51
select_best_assets_sequential/05 65061327.5 ns 43859325.75 ns 1.48
select_best_assets_sequential/10 129810393.25 ns 87553632.5 ns 1.48
select_best_assets_sequential/15 193924851.5 ns 131658938.5 ns 1.47
select_best_assets_sequential/20 258691192 ns 175652280.5 ns 1.47

This comment was automatically generated by workflow using github-action-benchmark.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Repeated demand-map and graph-node scans introduce validation scaling regressions as model size increases.

2 open issues
What changed in this PR

Combines regional commodity graphs into one graph per year, simplifying graph handling and preparing MUSE2 for future inter-region trade.

Changes:

  • Adds region identities to commodity nodes and process edges.
  • Adapts validation and investment ordering to year-keyed graphs.
  • Saves one DOT file per year and updates affected tests.
File Description
tests/​cli.rs Expects the year-only DOT filename.
src/​input.rs Returns commodity graphs keyed by year.
src/​graph/​validate.rs Uses regional edge availability and demand in combined graphs.
src/​graph/​investment.rs Derives investment order from a single yearly graph.
src/​graph.rs Builds region-qualified yearly graphs and updates DOT output.

Review effort: Balanced

Comment thread src/graph/validate.rs
Comment on lines +49 to +50
for ((region_id, dem_year, dem_ts_sel), &flow) in &commodity.demand {
if *dem_year != year || dem_ts_sel != time_slice_selection || flow <= Flow(0.0) {
Comment thread src/graph/validate.rs
Comment on lines 54 to +57
let commodity_node_index = filtered_graph
.node_indices()
.find(|&idx| filtered_graph.node_weight(idx) == Some(&commodity_node))
.unwrap_or_else(|| {
filtered_graph.add_node(GraphNode::Commodity(commodity_id.clone()))
});
.unwrap_or_else(|| filtered_graph.add_node(commodity_node));
@tsmbland tsmbland mentioned this pull request Nov 3, 2026
1 of 11 tasks
@tsmbland
tsmbland marked this pull request as ready for review November 3, 2026 09:03
@tsmbland
tsmbland requested a review from alexdewar November 3, 2026 09:04

This branch has not been deployed

No deployments
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.

2 participants