Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better optimizer for initializing half-cell soc #4873

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

d-cogswell
Copy link
Contributor

@d-cogswell d-cogswell commented Feb 24, 2025

Description

scipy.opimize.root() occasionally causes numerical crashes during initialization of half-cell simulations. Switching to scipy.optimize.minimize() resolves the issue.

Fixes #4868

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

@agriyakhetarpal
Copy link
Member

Thanks, @d-cogswell – I've triggered the workflows for you.

Copy link

codecov bot commented Feb 24, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.70%. Comparing base (6f20992) to head (9de1e9e).

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #4873   +/-   ##
========================================
  Coverage    98.70%   98.70%           
========================================
  Files          304      304           
  Lines        23483    23483           
========================================
  Hits         23180    23180           
  Misses         303      303           

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

@d-cogswell d-cogswell changed the title Better optimizer for solving initializing half-cell soc Better optimizer for initializing half-cell soc Feb 25, 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.

Thanks @d-cogswell, largely this looks good and works for me as well, I've got a couple of questions below that should be addressed

@@ -31,7 +31,7 @@ def __init__(self, name="ElectrodeSOH model"):
Q_w = pybamm.InputParameter("Q_w")
T_ref = param.T_ref
U_w = param.p.prim.U
Q = Q_w * (x_100 - x_0)
Q = Q_w * (x_0 - x_100)
Copy link
Contributor

Choose a reason for hiding this comment

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

why was Q altered here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@valentinsulzer noticed that Q was negative for half-cells, and this change addresses that. I happened to be looking through this part of the code, I discovered it was defined incorrectly for a positive electrode. x_0 is the stoichiometry at 0% SOC, and x_100 at 100% SOC. For a cathode x_0 > x_100.

@@ -49,13 +49,12 @@ def __init__(self, name="ElectrodeSOH model"):
"Uw(x_100)": U_w(x_100, T_ref),
"Uw(x_0)": U_w(x_0, T_ref),
"Q_w": Q_w,
"Q_w * (x_100 - x_0)": Q_w * (x_100 - x_0),
Copy link
Contributor

Choose a reason for hiding this comment

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

why was this variable removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This expression is also negative, so I didn't see a reason to keep it. A half-cell only needs on Q variable.

}

@property
def default_solver(self):
# Use AlgebraicSolver as CasadiAlgebraicSolver gives unnecessary warnings
return pybamm.AlgebraicSolver()
return pybamm.AlgebraicSolver(method="minimize L-BFGS-B", tol=1e-7)
Copy link
Contributor

Choose a reason for hiding this comment

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

I tried this out on the example linked in your issue. Using the default tol (1e-6) seems to work reliably for me, is there a reason to tighten this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed the tolerance because a regression test was failing.

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.

[Bug]: SolverError when simulating a half-cell and setting initial_soc
4 participants