Using multithreading for parameter sweeps using experiments

Hi,

I am trying to use the multithreading capabilities of the IKDALU solver to run the model for different parameter sets (varying 5 parameters at the same time for each execution). I am unable to make it work when using Experiment objects. Is this a limitation of PyBamm? I can switch to multiprocessing instead, but since the multithreading is builtin I prefer to use it.

Best regards,

Oier.

Hi Oier,

You might already be aware of this document about using multithreading: Running many simulations in parallel using OpenMP — PyBaMM v25.8.1.dev96+g847723e76 Manual

It might be more helpful also if you could share your code snippet here so we can better understand the specific issue you’re encountering with Experiment objects and the IKDALU solver.

Best regards,
Kawa

Hi,

Here a minimal example

import pybamm
import matplotlib.pyplot as plt

import numpy as np

experiment = pybamm.Experiment(
[(“Charge at 1C for 1 hour or until 4.0 V”)]
)

model = pybamm.lithium_ion.DFN()
params = pybamm.ParameterValues(“Chen2020”)
params[“Positive particle diffusivity [m2.s-1]”] = “[input]”
params[“Negative particle diffusivity [m2.s-1]”] = “[input]”
solver = pybamm.IDAKLUSolver(options={“num_threads”: 4})

sim = pybamm.Simulation(model, solver=solver, experiment=experiment, parameter_values=params)

parameter_loop = [{“Positive particle diffusivity [m2.s-1]”: 1e-15, “Negative particle diffusivity [m2.s-1]”: 1e-15},
{“Positive particle diffusivity [m2.s-1]”: 2e-15, “Negative particle diffusivity [m2.s-1]”: 2e-15},
{“Positive particle diffusivity [m2.s-1]”: 3e-15, “Negative particle diffusivity [m2.s-1]”: 3e-15},
{“Positive particle diffusivity [m2.s-1]”: 4e-15, “Negative particle diffusivity [m2.s-1]”: 4e-15},
{“Positive particle diffusivity [m2.s-1]”: 5e-15, “Negative particle diffusivity [m2.s-1]”: 5e-15}]

solution = sim.solve(inputs=parameter_loop)

The error keeps being the same:

/home/oarcelus/miniconda3/envs/pybamm/lib/python3.12/site-packages/requests/__init__.py:86: RequestsDependencyWarning: Unable to find acceptable character detection dependency (chardet or charset_normalizer).
  warnings.warn(

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[1], line 66
     56 sim = pybamm.Simulation(
     57     model, solver=solver, experiment=experiment, parameter_values=params
     58 )
     60 parameter_loop = [{"Positive particle diffusivity [m2.s-1]": 1e-15, "Negative particle diffusivity [m2.s-1]": 1e-15},
     61                   {"Positive particle diffusivity [m2.s-1]": 2e-15, "Negative particle diffusivity [m2.s-1]": 2e-15},
     62                   {"Positive particle diffusivity [m2.s-1]": 3e-15, "Negative particle diffusivity [m2.s-1]": 3e-15},
     63                   {"Positive particle diffusivity [m2.s-1]": 4e-15, "Negative particle diffusivity [m2.s-1]": 4e-15},
     64                   {"Positive particle diffusivity [m2.s-1]": 5e-15, "Negative particle diffusivity [m2.s-1]": 5e-15}]
---> 66 solution = sim.solve(inputs=parameter_loop)

File ~/miniconda3/envs/pybamm/lib/python3.12/site-packages/pybamm/simulation.py:740, in Simulation.solve(self, t_eval, solver, save_at_cycles, calc_esoh, starting_solution, initial_soc, callbacks, showprogress, inputs, t_interp, **kwargs)
    737 logs["step duration"] = step.duration
    738 callbacks.on_step_start(logs)
--> 740 inputs = {
    741     **user_inputs,
    742     "start time": start_time,
    743 }
    744 # Make sure we take at least 2 timesteps
    745 t_eval, t_interp_processed = step.setup_timestepping(
    746     solver, dt, t_interp
    747 )

TypeError: 'list' object is not a mapping
1 Like

Have you tried using a loop? The issue is that solve(inputs=parameter_loop) expects a single parameter dictionary, not a list.

You might try this instead:

  solutions = []
for param_dict in parameter_loop:
    solution = sim.solve(inputs=param_dict, initial_soc=0)
    solutions.append(solution)

Best regards,
Kawa

@oarcelus: currently pybamm does not support multithreading with experiments. The error message should be better though!!

Thanks @martinjrobins, I guess that this means that experiments cannot go directly as inputs right?

Can I pass an interpolant function as an input to the solve() function in order to loop thorugh various drive-cycles?