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

[Bug]: Error when pickling simulation #2982

Closed
brosaplanella opened this issue May 24, 2023 · 4 comments · Fixed by #2960
Closed

[Bug]: Error when pickling simulation #2982

brosaplanella opened this issue May 24, 2023 · 4 comments · Fixed by #2960
Labels
bug Something isn't working difficulty: easy A good issue for someone new. Can be done in a few hours priority: high To be resolved as soon as possible

Comments

@brosaplanella
Copy link
Member

brosaplanella commented May 24, 2023

PyBaMM Version

23.4.1

Python Version

3.9.16

Describe the bug

When trying to pickle a simulation with experiment I get an error related to a SwigPy object that can't be pickled. I suspect it is a problem with Casadi, but haven't been able to trace the origin of such object.

Steps to Reproduce

Run

import pybamm
model = pybamm.lithium_ion.SPMe()

experiment = pybamm.Experiment(
    [
        (
            "Discharge at 1C until 2.5 V",
            "Charge at C/3 until 4.2 V",
            "Hold at 4.2 V until C/20",
        )
    ]
)

sim = pybamm.Simulation(
    model,
    experiment=experiment,
)
sim.solve()
sim.save("test.pkl")

If instead you replace the last few lines by

sim = pybamm.Simulation(
    model,
)
sim.solve([0, 3600])
sim.save("test.pkl")

it works fine.

Relevant log output

Traceback (most recent call last):
  File "/home/brosaplanella/support-pybamm/mwe_swigpy.py", line 19, in <module>
    sim.save("test.pkl")
  File "/home/brosaplanella/support-pybamm/env/lib/python3.9/site-packages/pybamm/simulation.py", line 1096, in save
    pickle.dump(self, f, pickle.HIGHEST_PROTOCOL)
TypeError: cannot pickle 'SwigPyObject' object
@brosaplanella brosaplanella added the bug Something isn't working label May 24, 2023
@valentinsulzer
Copy link
Member

Pickling simulations is a pain and simulations are super cheap to set up. Do we want to continue allowing this, or suggest that people set up configuration files instead?

@valentinsulzer
Copy link
Member

This also kind of relates to #2787

@brosaplanella
Copy link
Member Author

I think that some optimisers pickle (or do something similar to pickling) when running in parallel. It was not where this issues came up, but had similar issues in the past with pybamm-param. I think for that it would be enough to be able to access an unsolved version.

@brosaplanella
Copy link
Member Author

brosaplanella commented Jun 5, 2023

Ok, I found the issue. It is with op_conds_to_build_solvers which keeps a copy of the solver which is not processed for pickling. It should be an easy fix, we only need to process the solvers in op_conds_to_build_solvers similarly as we do in

PyBaMM/pybamm/simulation.py

Lines 1090 to 1094 in a1e64c7

if (
isinstance(self._solver, pybamm.CasadiSolver)
and self._solver.integrator_specs != {}
):
self._solver.integrator_specs = {}

Note that this issue appears only after merging #2960. I labelled it as high priority as ideally it would be good to fix that jointly with #2960.

@brosaplanella brosaplanella added difficulty: easy A good issue for someone new. Can be done in a few hours priority: high To be resolved as soon as possible labels Jun 5, 2023
@brosaplanella brosaplanella linked a pull request Jun 7, 2023 that will close this issue
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working difficulty: easy A good issue for someone new. Can be done in a few hours priority: high To be resolved as soon as possible
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants