Solutions#

class pybamm.Solution(all_ts, all_ys, all_models, all_inputs, t_event=None, y_event=None, termination='final time', sensitivities=False, check_solution=True)[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

  • sensitivities (bool or dict) – True if sensitivities included as the solution of the explicit forwards equations. False if no sensitivities included/wanted. Dict if sensitivities are provided as a dict of {parameter: sensitivities} pairs.

property all_models#

Model(s) used for solution

property first_state#

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#

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)

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(filename)[source]#

Save the whole solution using pickle

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#

Times at which the solution is evaluated

property t_event#

Time at which the event happens

property termination#

Reason for termination

update(variables)[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