Skip to content

Add commodity constraints input and validation - #1487

Draft
dc2917 wants to merge 4 commits into
mainfrom
add-commodity-constraints-input-and-validation
Draft

Add commodity constraints input and validation#1487
dc2917 wants to merge 4 commits into
mainfrom
add-commodity-constraints-input-and-validation

Conversation

@dc2917

@dc2917 dc2917 commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Description

This PR adds a new module for reading and validating an input csv file for commodity constraints.

Commodity constraints are stored in a new CommodityConstraint struct, and the set of constraints provided are read and stored in a CommodityConstraintsMap type, which maps CommodityConstraints by region ID and year.

CommodityConstraints have a single field, limits, which stores the range of values to which the commodity is constrained.

An example commodity_constraints.csv file has been added to the "simple" setup for demonstration, and a corresponding schema has been added.

Fixes #1448

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

@dc2917

dc2917 commented Aug 14, 2026

Copy link
Copy Markdown
Contributor Author

@tsmbland I've taken a stab at this but some feedback would be helpful before I add tests

@tsmbland tsmbland left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Good start, but a few comments

Comment thread src/input/commodity/constraints.rs Outdated
Comment thread src/input/commodity/constraints.rs Outdated
Comment thread src/input/commodity/constraints.rs Outdated
Comment thread src/commodity.rs Outdated
Comment thread src/commodity.rs Outdated
///
/// A `HashMap<CommodityID, CommodityConstraintsMap>` mapping commodity IDs to their
/// commodity-constraints maps, or an error.
pub fn read_commodity_constraints(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We also want to disallow SVD commodities from having commodity constraints, so you'll need to pass in the map of the commodities rather than just the IDs (i.e. &IndexMap<CommodityID, Commodity>)

Another slightly tricky thing is that, since OTH commodities can either be consumed or produced (but not both), we don't want users to supply production constraints for OTH commodities that are consumed, and vice-versa. Probably worth opening an issue about this rather than attempting this here, as we may have to do this in the graph validation stage

dc2917 added 3 commits August 25, 2026 17:23
- Cleaner parsing of region_id
- Change units of limits from money to flow
- Store time slice selection and balance type in `CommodityConstraint` struct
- Store vectors of `CommodityConstraint`s in `CommodityConstraintMap`
@dc2917
dc2917 force-pushed the add-commodity-constraints-input-and-validation branch from c872f0a to 273b323 Compare August 25, 2026 16:23
@dc2917
dc2917 force-pushed the add-commodity-constraints-input-and-validation branch from 273b323 to 328243d Compare August 25, 2026 16:28
@codecov

codecov Bot commented Aug 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.94118% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.38%. Comparing base (236974e) to head (328243d).

Files with missing lines Patch % Lines
src/input/commodity/constraints.rs 92.68% 1 Missing and 11 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1487      +/-   ##
==========================================
+ Coverage   90.31%   90.38%   +0.07%     
==========================================
  Files          60       61       +1     
  Lines        8663     8833     +170     
  Branches     8663     8833     +170     
==========================================
+ Hits         7824     7984     +160     
- Misses        525      526       +1     
- Partials      314      323       +9     

☔ 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.

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.

Add file reading and validation code for commodity constraints

2 participants