Skip to content

Commit 0612406

Browse files
Merge pull request #1678 from Saransh-cpp/plot-summary-variables
Added a function to plot "summary variables"
2 parents 261f6c4 + d077706 commit 0612406

File tree

7 files changed

+1922
-1121
lines changed

7 files changed

+1922
-1121
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Features
44

5+
- Added `plot_summary_variables` to plot and compare summary variables ([#1678](https://github.com/pybamm-team/PyBaMM/pull/1678))
56
- The DFN model can now be used directly (instead of `BasicDFNHalfCell`) to simulate a half-cell ([#1600](https://github.com/pybamm-team/PyBaMM/pull/1600))
67

78
## Breaking changes

docs/source/plotting/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ Plotting
88
plot
99
plot_2D
1010
plot_voltage_components
11+
plot_summary_variables
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Plot Summary Variables
2+
======================
3+
4+
.. autofunction:: pybamm.plot_summary_variables

examples/notebooks/simulating-long-experiments.ipynb

+1,750-1,121
Large diffs are not rendered by default.

pybamm/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ def version(formatted=False):
249249
from .plotting.plot import plot
250250
from .plotting.plot2D import plot2D
251251
from .plotting.plot_voltage_components import plot_voltage_components
252+
from .plotting.plot_summary_variables import plot_summary_variables
252253
from .plotting.dynamic_plot import dynamic_plot
253254

254255
#
+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#
2+
# Method for plotting/comparing summary variables
3+
#
4+
import numpy as np
5+
import pybamm
6+
7+
8+
def plot_summary_variables(
9+
solutions, output_variables=None, labels=None, testing=False, **kwargs_fig
10+
):
11+
"""
12+
Generate a plot showing/comparing the summary variables.
13+
14+
Parameters
15+
----------
16+
solutions : (iter of) :class:`pybamm.Solution`
17+
The solution(s) for the model(s) from which to extract summary variables.
18+
output_variables: list (optional)
19+
A list of variables to plot automatically. If None, the default ones are used.
20+
labels: list (optional)
21+
A list of labels to be added to the legend. No labels are added by default.
22+
testing : bool (optional)
23+
Whether to actually make the plot (turned off for unit tests).
24+
kwargs_fig
25+
Keyword arguments, passed to plt.subplots.
26+
27+
"""
28+
import matplotlib.pyplot as plt
29+
30+
if isinstance(solutions, pybamm.Solution):
31+
solutions = [solutions]
32+
33+
# setting a default value for figsize
34+
kwargs_fig = {"figsize": (15, 8), **kwargs_fig}
35+
36+
if output_variables is None:
37+
output_variables = [
38+
"Capacity [A.h]",
39+
"Loss of lithium inventory [%]",
40+
"Loss of capacity to SEI [A.h]",
41+
"Loss of active material in negative electrode [%]",
42+
"Loss of active material in positive electrode [%]",
43+
"x_100",
44+
"x_0",
45+
"y_100",
46+
"y_0",
47+
]
48+
49+
# find the number of subplots to be created
50+
length = len(output_variables)
51+
n = int(length // np.sqrt(length))
52+
m = int(np.ceil(length / n))
53+
54+
# create subplots
55+
fig, axes = plt.subplots(n, m, **kwargs_fig)
56+
57+
# loop through the subplots and plot the output_variables
58+
for var, ax in zip(output_variables, axes.flat):
59+
# loop through the solutions to compare output_variables
60+
for solution in solutions:
61+
# plot summary variable v/s cycle number
62+
ax.plot(
63+
solution.summary_variables["Cycle number"],
64+
solution.summary_variables[var],
65+
)
66+
# label the axes
67+
ax.set_xlabel("Cycle number")
68+
ax.set_ylabel(var)
69+
ax.set_xlim([1, solution.summary_variables["Cycle number"][-1]])
70+
71+
fig.tight_layout()
72+
73+
# add labels in legend
74+
if labels is not None: # pragma: no cover
75+
fig.legend(labels, loc="lower right")
76+
if not testing: # pragma: no cover
77+
plt.show()
78+
79+
return axes
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import pybamm
2+
import unittest
3+
import numpy as np
4+
5+
6+
class TestPlotSummaryVariables(unittest.TestCase):
7+
def test_plot(self):
8+
model = pybamm.lithium_ion.SPM({"SEI": "ec reaction limited"})
9+
parameter_values = pybamm.ParameterValues(
10+
chemistry=pybamm.parameter_sets.Mohtat2020
11+
)
12+
experiment = pybamm.Experiment(
13+
[
14+
(
15+
"Discharge at C/10 for 10 hours or until 3.3 V",
16+
"Rest for 1 hour",
17+
"Charge at 1 A until 4.1 V",
18+
"Hold at 4.1 V until 50 mA",
19+
"Rest for 1 hour",
20+
)
21+
]
22+
* 3,
23+
)
24+
output_variables = [
25+
"Capacity [A.h]",
26+
"Loss of lithium inventory [%]",
27+
"Loss of capacity to SEI [A.h]",
28+
"Loss of active material in negative electrode [%]",
29+
"Loss of active material in positive electrode [%]",
30+
"x_100",
31+
"x_0",
32+
"y_100",
33+
"y_0",
34+
]
35+
sim = pybamm.Simulation(
36+
model, experiment=experiment, parameter_values=parameter_values
37+
)
38+
sol = sim.solve(initial_soc=1)
39+
40+
axes = pybamm.plot_summary_variables(sol, testing=True)
41+
42+
axes = axes.flatten()
43+
self.assertEqual(len(axes), 9)
44+
45+
for output_var, ax in zip(output_variables, axes):
46+
self.assertEqual(ax.get_xlabel(), "Cycle number")
47+
self.assertEqual(ax.get_ylabel(), output_var)
48+
49+
cycle_number, var = ax.get_lines()[0].get_data()
50+
np.testing.assert_array_equal(
51+
cycle_number, sol.summary_variables["Cycle number"]
52+
)
53+
np.testing.assert_array_equal(var, sol.summary_variables[output_var])
54+
55+
axes = pybamm.plot_summary_variables(
56+
[sol, sol], labels=["SPM", "SPM"], testing=True
57+
)
58+
59+
axes = axes.flatten()
60+
self.assertEqual(len(axes), 9)
61+
62+
for output_var, ax in zip(output_variables, axes):
63+
self.assertEqual(ax.get_xlabel(), "Cycle number")
64+
self.assertEqual(ax.get_ylabel(), output_var)
65+
66+
cycle_number, var = ax.get_lines()[0].get_data()
67+
np.testing.assert_array_equal(
68+
cycle_number, sol.summary_variables["Cycle number"]
69+
)
70+
np.testing.assert_array_equal(var, sol.summary_variables[output_var])
71+
72+
cycle_number, var = ax.get_lines()[1].get_data()
73+
np.testing.assert_array_equal(
74+
cycle_number, sol.summary_variables["Cycle number"]
75+
)
76+
np.testing.assert_array_equal(var, sol.summary_variables[output_var])
77+
78+
79+
if __name__ == "__main__":
80+
print("Add -v for more debug output")
81+
import sys
82+
83+
if "-v" in sys.argv:
84+
debug = True
85+
pybamm.settings.debug_mode = True
86+
unittest.main()

0 commit comments

Comments
 (0)