Skip to content

Refactor to use PathSim solver#2

Merged
DavidMStraub merged 11 commits intomasterfrom
fix_after_refactor
May 4, 2026
Merged

Refactor to use PathSim solver#2
DavidMStraub merged 11 commits intomasterfrom
fix_after_refactor

Conversation

@DavidMStraub
Copy link
Copy Markdown
Collaborator

@milanofthe except for lint and formatter fixes, the relevant change here is the override of set_solver in _CellBase. Is this allowed? If not, what's the right way to make it work?

PS: regarding formatting, I think it makes sense to use ruff, but you can tune the settings to match your preferred style.

@milanofthe
Copy link
Copy Markdown
Member

Usually set_solver should never be overriden or touched. Its automatically called depending on whether a block has an internal ode component and needs an ode solver. The step method of the block is tied to that and will be called at every minor step i.e. for runge-kutta solvers.

@DavidMStraub
Copy link
Copy Markdown
Collaborator Author

Thanks, I realized it's actually not needed! Removed it. Good to merge from your side?

@milanofthe
Copy link
Copy Markdown
Member

give me a moment, I am currently reviewing it again, missed some things about how pybamm works

@milanofthe
Copy link
Copy Markdown
Member

So I did some digging now.

The current implementation wraps pybamm.Simulation.step() which runs pybamms own CasadiSolver internally. This bypasses pathsims integration engine. step() in pathsim is a solver stage call (called multiple times per timestep for multi-stage methods), not a "once per timestep" callback.

The correct approach is to extract pybamms discretized ODE/DAE after sim.build() and expose the RHS to pathsims engine directly, the same way ODE and DynamicalSystem blocks work. pathsim handles the time integration. pybamm provides the equations.

The set_solver override in the PR would also prevent step() from ever being called since the block wouldnt land in _blocks_dyn.

Sorry for the confusion.

@DavidMStraub DavidMStraub changed the title CI fixes after refactoring Refactor to use PathSim solver Mar 30, 2026
@DavidMStraub
Copy link
Copy Markdown
Collaborator Author

Thanks, I got it! I was able to refactor it using PyBaMMs own export_casadi_objects feature.

Can you have another look please?

If there is no show stopper from your side, after merging I'd reach out to the PyBaMM dev to give feedback directly, whether they think this is the right approach.

(For curiosity: one of the PyBaMM main devs demonstrated here https://github.com/FaradayInstitution/pybamm_simulink_example long ago how to call PyBaMM from Simulink, and it also uses export_casadi_objects.)

@milanofthe
Copy link
Copy Markdown
Member

I'll check! :)

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

This PR refactors the PyBaMM-based cell blocks to integrate with PathSim by exporting CasADi dynamics at construction time (instead of stepping PyBaMM internally), and updates tests/docs to reflect the new integration approach.

Changes:

  • Refactor _CellBase to subclass DynamicalSystem and expose CasADi RHS/Jacobian + output functions for PathSim integration.
  • Update cell integration tests to run via pathsim.Simulation (Constants + Connections) instead of calling buffer/step/update directly.
  • Minor formatting/lint cleanups and README updates describing immediate model discretisation.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
src/pathsim_batt/cells/pybamm_cell.py Replaces internal PyBaMM stepping with CasADi-exported dynamics for PathSim solvers.
tests/cells/test_pybamm_cell.py Updates tests to exercise PathSim-driven integration and validates CasADi setup.
src/pathsim_batt/thermal/lumped.py Small formatting/tidy changes; no substantive logic change.
README.md Updates integration description from lazy build to immediate discretisation.

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

Comment thread src/pathsim_batt/cells/pybamm_cell.py Outdated
Comment thread src/pathsim_batt/cells/pybamm_cell.py
Comment thread src/pathsim_batt/cells/pybamm_cell.py
Comment thread README.md
Comment thread src/pathsim_batt/cells/pybamm_cell.py
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

Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.


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

Comment thread src/pathsim_batt/cells/pybamm_cell.py Outdated
Comment thread src/pathsim_batt/cells/pybamm_cell.py Outdated
Comment thread src/pathsim_batt/cells/pybamm_cell.py Outdated
Comment thread src/pathsim_batt/cells/pybamm_cell.py
Comment thread src/pathsim_batt/cells/pybamm_cell.py
Comment thread README.md
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

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.


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

Comment thread src/pathsim_batt/cells/pybamm_cell.py
Comment thread src/pathsim_batt/cells/pybamm_cell.py Outdated
Comment thread src/pathsim_batt/cells/pybamm_cell.py
@DavidMStraub
Copy link
Copy Markdown
Collaborator Author

@milanofthe I think now it's worth to have another look 🙂

@milanofthe
Copy link
Copy Markdown
Member

@DavidMStraub do you know how intertwined casadi is into pybamm?

And sorry for the late reply, I will review it this weekend.

@milanofthe
Copy link
Copy Markdown
Member

@DavidMStraub looks much better now! :)

A few small last things:

  • I dont quite understand the extension of the update method in _CellBase, what is that for?
  • You mention in the base class _CellBase that stiff solvers should be used with these blocks. I think it would be nice to also add this as a docstring note to the derivative blocks such that its immediately clear for users

Besides that, looks good!

@DavidMStraub
Copy link
Copy Markdown
Collaborator Author

Thanks for the feedback!

You're right to wonder about the patched update method. This wasn't a great design and I removed it now.

PyBaMM calculates lots of other observables (not just the ones I wired into the output ports) and this was a way to make them accessible, but a hacky way. Is there a way to expose (a potentially large number of) additional variables without making them output ports?

Also updated the doc string as suggested.

I will merge this now. As a next step, I'll ping the PyBaMM devs via the project's Slack channel and ask them for feedback. Let me know if I should transfer the repo to the pathsim org now.

@DavidMStraub DavidMStraub merged commit 9e2e29e into master May 4, 2026
5 checks passed
@milanofthe
Copy link
Copy Markdown
Member

@DavidMStraub great! Since the pybamm object lives inside of the block, its attributes should still be accessible directly that way. If you want something more direct Id say add a somple getter method to the block thatmis specifically for retrieving these values.

Otherwise I think you can transfer the repo.

@DavidMStraub
Copy link
Copy Markdown
Collaborator Author

Github says

You don’t have the permission to create public repositories on pathsim

Can you add me to the pathsim org with the permissions required? After transfer, also maintainer access to the repo. Create repository access can be removed again after the migration.

@milanofthe
Copy link
Copy Markdown
Member

@DavidMStraub you can just transfer the repo, this works through the dangerous section. I will add you as maintainer to the repo afer that.

@DavidMStraub
Copy link
Copy Markdown
Collaborator Author

That's what I meant - I can't do that, error message above.

It makes sense that org non-members cannot just move arbitrary repos into an org.

@milanofthe
Copy link
Copy Markdown
Member

gocha, give me a minute

@milanofthe
Copy link
Copy Markdown
Member

i have sent an invite

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.

3 participants