Solutions#

class pybamm.Solution(all_ts, all_ys, all_models, all_inputs, t_event=None, y_event=None, termination='final time', all_sensitivities=None, all_yps=None, all_t_evals=None, variables_returned=False, check_solution=True, options=None)[source]#

Class containing the solution of, and various attributes associated with, a PyBaMM model.

Parameters:
  • all_ts (numpy.array, size (n,) (or list of these)) – A one-dimensional array containing the times at which the solution is evaluated. A list of times can be provided instead to initialize a solution with sub-solutions.

  • all_ys (numpy.array, size (m, n) (or list of these)) – A two-dimensional array containing the values of the solution. y[i, :] is the vector of solutions at time t[i]. A list of ys can be provided instead to initialize a solution with sub-solutions.

  • all_models (pybamm.BaseModel) – The model that was used to calculate the solution. A list of models can be provided instead to initialize a solution with sub-solutions that have been calculated using those models.

  • all_inputs (dict (or list of these)) – The inputs that were used to calculate the solution A list of inputs can be provided instead to initialize a solution with sub-solutions.

  • t_event (numpy.array, size (1,)) – A zero-dimensional array containing the time at which the event happens.

  • y_event (numpy.array, size (m,)) – A one-dimensional array containing the value of the solution at the time when the event happens.

  • termination (str) – String to indicate why the solution terminated

  • all_sensitivities (dict of lists) – sensitivities are provided as a dict of {parameter: [sensitivities]} pairs.

  • all_t_evals (numpy.array (or list of these), optional) – Evaluation time breakpoints for each segment, one array per entry in all_ts. When provided by the solver these contain the t_eval times (including merged discontinuity times). If None (the default), the property returns all_ts for each segment, since each point is asssumed to be an evaluation time.

  • variables_returned (bool) – Bool to indicate if all_ys contains the full state vector, or is empty because only requested variables have been returned. True if output_variables is used with a solver, otherwise False.

  • check_solution (bool) – Bool to indicate if the solution should be checked for large y values.

  • options (dict, optional) – Overrides for process_casadi_var(). Unspecified keys fall back to _DEFAULT_SOLUTION_OPTIONS – the casadi Function flags and the compile flag (True = AOT, False = in-process VM). Solvers should forward the user’s selection so observed variables use the same backend as the integration.

Extends: pybamm.solvers.solution.SolutionBase

property all_models#

Model(s) used for solution

property data#

Dict-like view of all computed variable data.

property first_state[source]#

A Solution object that only contains the first state. This is faster to evaluate than the full solution when only the first state is needed (e.g. to initialize a model with the solution)

get_data_dict(variables=None, short_names=None, cycles_and_steps=True)[source]#

Construct a (standard python) dictionary of the solution data containing the variables in variables. If variables is None then all variables are returned. Any variable names in short_names are replaced with the corresponding short name.

If the solution has cycles, then the cycle numbers and step numbers are also returned in the dictionary.

Parameters:
  • variables (list, optional) – List of variables to return. If None, returns all variables in solution.data

  • short_names (dict, optional) – Dictionary of shortened names to use when saving.

  • cycles_and_steps (bool, optional) – Whether to include the cycle numbers and step numbers in the dictionary

Returns:

A dictionary of the solution data

Return type:

dict

property last_state[source]#

A Solution object that only contains the final state. This is faster to evaluate than the full solution when only the final state is needed (e.g. to initialize a model with the solution)

observe(symbol: Symbol) ProcessedVariable[source]#

Observe a pybamm.Symbol object from the solution.

Parameters:

symbol (pybamm.Symbol) – The symbol to observe.

Returns:

The observed variable.

Return type:

pybamm.ProcessedVariable

plot(output_variables=None, **kwargs)[source]#

A method to quickly plot the outputs of the solution. Creates a pybamm.QuickPlot object (with keyword arguments ‘kwargs’) and then calls pybamm.QuickPlot.dynamic_plot().

Parameters:
plot_voltage_components(ax=None, show_legend=True, split_by_electrode=False, show_plot=True, **kwargs_fill)[source]#

Generate a plot showing the component overpotentials that make up the voltage

Parameters:
  • ax (matplotlib Axis, optional) – The axis on which to put the plot. If None, a new figure and axis is created.

  • show_legend (bool, optional) – Whether to display the legend. Default is True.

  • split_by_electrode (bool, optional) – Whether to show the overpotentials for the negative and positive electrodes separately. Default is False.

  • show_plot (bool, optional) – Whether to show the plots. Default is True. Set to False if you want to only display the plot after plt.show() has been called.

  • kwargs_fill – Keyword arguments, passed to ax.fill_between.

save_data(filename=None, variables=None, to_format='pickle', short_names=None)[source]#

Save solution data only (raw arrays)

Parameters:
  • filename (str, optional) – The name of the file to save data to. If None, then a str is returned

  • variables (list, optional) – List of variables to save. If None, saves all of the variables that have been created so far

  • to_format (str, optional) –

    The format to save to. Options are:

    • ’pickle’ (default): creates a pickle file with the data dictionary

    • ’matlab’: creates a .mat file, for loading in matlab

    • ’csv’: creates a csv file (0D variables only)

    • ’json’: creates a json file

  • short_names (dict, optional) – Dictionary of shortened names to use when saving. This may be necessary when saving to MATLAB, since no spaces or special characters are allowed in MATLAB variable names. Note that not all the variables need to be given a short name.

Returns:

data – str if ‘csv’ or ‘json’ is chosen and filename is None, otherwise None

Return type:

str, optional

property sensitivities#

np_array

Type:

Values of the sensitivities. Returns a dict of param_name

property sub_solutions#

List of sub solutions that have been concatenated to form the full solution

property t: ndarray#

Times at which the solution is evaluated

property t_eval: ndarray#

Evaluation time breakpoints for each segment

property t_event#

Time at which the event happens

property termination#

Reason for termination

update(variables: str | list[str])[source]#

Add ProcessedVariables to the dictionary of variables in the solution

property y#

Values of the solution

property y_event#

Value of the solution at the time of the event

property yp#

Time derivatives of the solution