Skip to content

Commit e788e2a

Browse files
Allow sim to be pickled with output variables (#5225)
* Bugfix #5224 add output variable pkl attributes to _setup Allows pickling solver when output variables are set * Add to changelog --------- Co-authored-by: Martin Robinson <[email protected]>
1 parent d2669ee commit e788e2a

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
## Bug fixes
1313

14+
- Fixed a bug where simulations using output variables in `IDAKLUSolver` couldn't be pickled ([#5225](https://github.com/pybamm-team/PyBaMM/pull/5225))
1415
- Added explicit warning in installation docs about unmaintained Conda recipe due to pybammsolvers split (Fixes #5155). See pull request [#5206](https://github.com/pybamm-team/PyBaMM/pull/5206)
1516
- Fixed a bug where time-based Heaviside or modulo discontinuities could trigger out-of-bounds errors in time arrays. ([#5205](https://github.com/pybamm-team/PyBaMM/pull/5205))
1617
- Fixed a bug using a time-varying input with heaviside or modulo functions using the `IDAKLUSolver`. ([#4994](https://github.com/pybamm-team/PyBaMM/pull/4994))

src/pybamm/solvers/idaklu_solver.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,11 @@ def set_up(self, model, inputs=None, t_eval=None, ics_only=False):
445445
"output_variables": self.output_variables,
446446
"var_fcns": self.computed_var_fcns,
447447
"var_idaklu_fcns": self.var_idaklu_fcns,
448+
"var_idaklu_fcns_pkl": self.var_idaklu_fcns_pkl,
448449
"dvar_dy_idaklu_fcns": self.dvar_dy_idaklu_fcns,
450+
"dvar_dy_idaklu_fcns_pkl": self.dvar_dy_idaklu_fcns_pkl,
449451
"dvar_dp_idaklu_fcns": self.dvar_dp_idaklu_fcns,
452+
"dvar_dp_idaklu_fcns_pkl": self.dvar_dp_idaklu_fcns_pkl,
450453
}
451454

452455
solver = self._setup["solver_function"](
@@ -483,6 +486,8 @@ def __getstate__(self):
483486
if not hasattr(self, "_setup"):
484487
return self.__dict__
485488

489+
self.var_idaklu_fcns = []
490+
486491
for key in [
487492
"solver",
488493
"solver_function",
@@ -492,6 +497,9 @@ def __getstate__(self):
492497
"mass_action",
493498
"sensfn",
494499
"rootfn",
500+
"var_idaklu_fcns",
501+
"dvar_dy_idaklu_fcns",
502+
"dvar_dp_idaklu_fcns",
495503
]:
496504
del self._setup[key]
497505
return self.__dict__
@@ -503,6 +511,10 @@ def __setstate__(self, d):
503511
if not hasattr(self, "_setup"):
504512
return
505513

514+
self.var_idaklu_fcns = [
515+
idaklu.generate_function(f) for f in self.var_idaklu_fcns_pkl
516+
]
517+
506518
for key in [
507519
"rhs_algebraic",
508520
"jac_times_cjmass",
@@ -513,6 +525,15 @@ def __setstate__(self, d):
513525
]:
514526
self._setup[key] = idaklu.generate_function(self._setup[key + "_pkl"])
515527

528+
for key in [
529+
"var_idaklu_fcns",
530+
"dvar_dy_idaklu_fcns",
531+
"dvar_dp_idaklu_fcns",
532+
]:
533+
self._setup[key] = [
534+
idaklu.generate_function(f) for f in self._setup[key + "_pkl"]
535+
]
536+
516537
self._setup["solver_function"] = idaklu.create_casadi_solver_group
517538

518539
self._setup["solver"] = self._setup["solver_function"](

tests/unit/test_simulation.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,16 @@ def test_save_load_model(self):
736736

737737
os.remove("sim_save.json")
738738

739+
def test_save_load_outvars(self, tmp_path):
740+
filename = str(tmp_path / "test.pkl")
741+
model = pybamm.lithium_ion.SPM()
742+
solver = pybamm.IDAKLUSolver(output_variables=["Voltage [V]"])
743+
sim = pybamm.Simulation(model, solver=solver)
744+
sim.solve([0, 600])
745+
sim.save(filename)
746+
pkl_obj = pybamm.load_sim(filename)
747+
assert list(pkl_obj.solver.output_variables) == ["Voltage [V]"]
748+
739749
def test_plot(self):
740750
sim = pybamm.Simulation(pybamm.lithium_ion.SPM())
741751

0 commit comments

Comments
 (0)