Callbacks#

class pybamm.callbacks.Callback[source]#

Base class for callbacks, for documenting callback methods.

Callbacks are used to perform actions (e.g. logging, saving) at certain points in the simulation. Each callback method is named on_<event>, where <event> describes the point at which the callback is called. For example, the callback on_experiment_start is called at the start of an experiment simulation. In general, callbacks take a single argument, logs, which is a dictionary of information about the simulation. Each callback method should return None (the output of the method is ignored).

EXPERIMENTAL - this class is experimental and the callback interface may change in future releases.

on_cycle_end(logs)[source]#

Called at the end of each cycle in an experiment simulation.

on_cycle_start(logs)[source]#

Called at the start of each cycle in an experiment simulation.

on_experiment_end(logs)[source]#

Called at the end of an experiment simulation.

on_experiment_error(logs)[source]#

Called when a SolverError occurs during an experiment simulation.

For example, this could be used to send an error alert with a bug report when running batch simulations in the cloud.

on_experiment_infeasible(logs)[source]#

Called when an experiment simulation is infeasible.

on_experiment_start(logs)[source]#

Called at the start of an experiment simulation.

on_step_end(logs)[source]#

Called at the end of each step in an experiment simulation.

on_step_start(logs)[source]#

Called at the start of each step in an experiment simulation.

class pybamm.callbacks.CallbackList(callbacks)[source]#

Container abstracting a list of callbacks, so that they can be called in a single step e.g. callbacks.on_simulation_end(…).

This is done without having to redefine the method each time by using the callback_loop_decorator decorator, which is applied to every method that starts with on_, using the inspect module. See https://stackoverflow.com/questions/1367514/how-to-decorate-a-method-inside-a-class.

If better control over how the callbacks are called is required, it might be better to be more explicit with the for loop.

Extends: pybamm.callbacks.Callback

on_cycle_end(*args, **kwargs)#

Called at the end of each cycle in an experiment simulation.

on_cycle_start(*args, **kwargs)#

Called at the start of each cycle in an experiment simulation.

on_experiment_end(*args, **kwargs)#

Called at the end of an experiment simulation.

on_experiment_error(*args, **kwargs)#

Called when a SolverError occurs during an experiment simulation.

For example, this could be used to send an error alert with a bug report when running batch simulations in the cloud.

on_experiment_infeasible(*args, **kwargs)#

Called when an experiment simulation is infeasible.

on_experiment_start(*args, **kwargs)#

Called at the start of an experiment simulation.

on_step_end(*args, **kwargs)#

Called at the end of each step in an experiment simulation.

on_step_start(*args, **kwargs)#

Called at the start of each step in an experiment simulation.

class pybamm.callbacks.LoggingCallback(logfile=None)[source]#

Logging callback, implements methods to log progress of the simulation.

Parameters:

logfile (str, optional) – Where to send the log output. If None, uses pybamm’s logger.

Extends: pybamm.callbacks.Callback

on_cycle_end(logs)[source]#

Called at the end of each cycle in an experiment simulation.

on_cycle_start(logs)[source]#

Called at the start of each cycle in an experiment simulation.

on_experiment_end(logs)[source]#

Called at the end of an experiment simulation.

on_experiment_error(logs)[source]#

Called when a SolverError occurs during an experiment simulation.

For example, this could be used to send an error alert with a bug report when running batch simulations in the cloud.

on_experiment_infeasible(logs)[source]#

Called when an experiment simulation is infeasible.

on_experiment_start(logs)[source]#

Called at the start of an experiment simulation.

on_step_end(logs)[source]#

Called at the end of each step in an experiment simulation.

on_step_start(logs)[source]#

Called at the start of each step in an experiment simulation.

pybamm.callbacks.setup_callbacks(callbacks)[source]#