Skip to content

Commit

Permalink
#2304 make sure experiment creates multiple solvers
Browse files Browse the repository at this point in the history
  • Loading branch information
martinjrobins committed Nov 21, 2022
1 parent 178b385 commit f3f26c1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
8 changes: 7 additions & 1 deletion pybamm/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def __init__(
self._built_model = None
self._built_initial_soc = None
self.op_conds_to_built_models = None
self.op_conds_to_built_solvers = None
self._mesh = None
self._disc = None
self._solution = None
Expand Down Expand Up @@ -383,6 +384,7 @@ def set_initial_soc(self, initial_soc):
self._model_with_set_params = None
self._built_model = None
self.op_conds_to_built_models = None
self.op_conds_to_built_solvers = None

c_n_init = self.parameter_values[
"Initial concentration in negative electrode [mol.m-3]"
Expand Down Expand Up @@ -443,7 +445,7 @@ def build(self, check_model=True, initial_soc=None):
def build_for_experiment(self, check_model=True, initial_soc=None):
"""
Similar to :meth:`Simulation.build`, but for the case of simulating an
experiment, where there may be several models to build
experiment, where there may be several models and solvers to build.
"""
if initial_soc is not None:
self.set_initial_soc(initial_soc)
Expand All @@ -461,12 +463,15 @@ def build_for_experiment(self, check_model=True, initial_soc=None):
self._disc = pybamm.Discretisation(self._mesh, self._spatial_methods)
# Process all the different models
self.op_conds_to_built_models = {}
self.op_conds_to_built_solvers= {}
for op_cond, model_with_set_params in self.op_string_to_model.items():
# It's ok to modify the model with set parameters in place as it's
# not returned anywhere
built_model = self._disc.process_model(
model_with_set_params, inplace=True, check_model=check_model
)
solver = self.solver.copy()
self.op_conds_to_built_solvers[op_cond] = solver
self.op_conds_to_built_models[op_cond] = built_model

def solve(
Expand Down Expand Up @@ -707,6 +712,7 @@ def solve(
dt = op_conds["time"]
op_conds_str = op_conds["string"]
model = self.op_conds_to_built_models[op_conds_str]
solver = self.op_conds_to_built_solvers[op_conds_str]

logs["step number"] = (step_num, cycle_length)
logs["step operating conditions"] = op_conds_str
Expand Down
14 changes: 8 additions & 6 deletions pybamm/solvers/base_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ def set_up(self, model, inputs=None, t_eval=None, ics_only=False):
"""
inputs = inputs or {}

if len(self.models_set_up) > 0:
existing_model = next(iter(self.models_set_up))
raise RuntimeError(
'This solver has already been initialised for model '
f'"{existing_model.name}". Please create a separate '
'solver for this model'
)

if ics_only:
pybamm.logger.info("Start solver set-up, initial_conditions only")
else:
Expand Down Expand Up @@ -784,12 +792,6 @@ def solve(
# Set up (if not done already)
timer = pybamm.Timer()
if model not in self.models_set_up:
if len(self.models_set_up) > 0:
existing_model = next(iter(self.models_set_up))
raise RuntimeError(
f'This solver has already been initialised for model "{existing_model.name}". '
'Please create a separate solver for this model'
)
# It is assumed that when len(inputs_list) > 1, model set
# up (initial condition, time-scale and length-scale) does
# not depend on input parameters. Thefore only `ext_and_inputs[0]`
Expand Down

0 comments on commit f3f26c1

Please sign in to comment.