Skip to content

Conversation

rtimms
Copy link
Contributor

@rtimms rtimms commented Jun 20, 2025

Description

Adds a new Constant symbol for named constants. This is a subclass of pybamm.Scalar and is used to represent named constants such as the gas constant. This avoids constants being simplified out when printing expressions (but they are still simplified when evaluated). For example, pybamm.constants.R + 2 should print 2+R and evaluate to 10.314462618. Counterintuitively, the "is_constant" method is set to False, so that named constants are not simplified when constructed. This is maybe a little confusing, but this is the cleanest way of avoiding simplifications without checking for instances of Constant everywhere.

Fixes # (issue)

Type of change

Please add a line in the relevant section of CHANGELOG.md to document the change (include PR #)

Important checks:

Please confirm the following before marking the PR as ready for review:

  • No style issues: nox -s pre-commit
  • All tests pass: nox -s tests
  • The documentation builds: nox -s doctests
  • Code is commented for hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

@rtimms rtimms requested a review from a team as a code owner June 20, 2025 09:26
Copy link

codecov bot commented Jun 20, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.58%. Comparing base (75755a1) to head (173f57a).
Report is 1 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #5070   +/-   ##
========================================
  Coverage    98.58%   98.58%           
========================================
  Files          304      304           
  Lines        23731    23740    +9     
========================================
+ Hits         23395    23404    +9     
  Misses         336      336           

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

Copy link
Contributor

@martinjrobins martinjrobins left a comment

Choose a reason for hiding this comment

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

thanks @rtimms, this looks very useful. Presumably these nodes should be replaced by scalars during discretisation, so that they are then simplified? Is this the desired behaviour? If so I think adding a test to make sure this happens would be good.

@rtimms
Copy link
Contributor Author

rtimms commented Jun 20, 2025

Yes, good point, I think this is what happens since they get simplified on evaluation, but I'll add a test to confirm this behaviour

martinjrobins
martinjrobins previously approved these changes Jun 22, 2025
Copy link
Contributor

@martinjrobins martinjrobins left a comment

Choose a reason for hiding this comment

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

looks great, thanks @rtimms

@rtimms rtimms merged commit ec4bcb8 into develop Jun 22, 2025
22 checks passed
@rtimms rtimms deleted the constant-symbol branch June 22, 2025 17:08
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