-
-
Notifications
You must be signed in to change notification settings - Fork 529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added a function to plot "summary variables" #1678
Merged
valentinsulzer
merged 9 commits into
pybamm-team:develop
from
Saransh-cpp:plot-summary-variables
Sep 17, 2021
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
816dac9
Create a function to plot summary variables
Saransh-cpp 78b1444
Add documentation for `plot_summary_variables`
Saransh-cpp 0061398
Update `simulating-long-experiments` notebook
Saransh-cpp dccac19
Add tests for `plot_summary_variables`
Saransh-cpp 0133bbb
Update default summary variables from 8 to 9
Saransh-cpp d4f6f81
Add some more tests for `plot_summary_variables`
Saransh-cpp 832af69
Update CHANGELOG.md for plot_summary_variables
Saransh-cpp 7a88c30
Merge branch 'develop' of https://github.com/pybamm-team/PyBaMM into …
Saransh-cpp d077706
Allow `plot_summary_variables` to accept solution not in a list
Saransh-cpp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ Plotting | |
plot | ||
plot_2D | ||
plot_voltage_components | ||
plot_summary_variables |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Plot Summary Variables | ||
====================== | ||
|
||
.. autofunction:: pybamm.plot_summary_variables |
2,871 changes: 1,750 additions & 1,121 deletions
2,871
examples/notebooks/simulating-long-experiments.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# | ||
# Method for plotting/comparing summary variables | ||
# | ||
import numpy as np | ||
import pybamm | ||
|
||
|
||
def plot_summary_variables( | ||
solutions, output_variables=None, labels=None, testing=False, **kwargs_fig | ||
): | ||
""" | ||
Generate a plot showing/comparing the summary variables. | ||
|
||
Parameters | ||
---------- | ||
solutions : (iter of) :class:`pybamm.Solution` | ||
The solution(s) for the model(s) from which to extract summary variables. | ||
output_variables: list (optional) | ||
A list of variables to plot automatically. If None, the default ones are used. | ||
labels: list (optional) | ||
A list of labels to be added to the legend. No labels are added by default. | ||
testing : bool (optional) | ||
Whether to actually make the plot (turned off for unit tests). | ||
kwargs_fig | ||
Keyword arguments, passed to plt.subplots. | ||
|
||
""" | ||
import matplotlib.pyplot as plt | ||
|
||
if isinstance(solutions, pybamm.Solution): | ||
solutions = [solutions] | ||
|
||
# setting a default value for figsize | ||
kwargs_fig = {"figsize": (15, 8), **kwargs_fig} | ||
|
||
if output_variables is None: | ||
output_variables = [ | ||
"Capacity [A.h]", | ||
"Loss of lithium inventory [%]", | ||
"Loss of capacity to SEI [A.h]", | ||
"Loss of active material in negative electrode [%]", | ||
"Loss of active material in positive electrode [%]", | ||
"x_100", | ||
"x_0", | ||
"y_100", | ||
"y_0", | ||
] | ||
|
||
# find the number of subplots to be created | ||
length = len(output_variables) | ||
n = int(length // np.sqrt(length)) | ||
m = int(np.ceil(length / n)) | ||
|
||
# create subplots | ||
fig, axes = plt.subplots(n, m, **kwargs_fig) | ||
|
||
# loop through the subplots and plot the output_variables | ||
for var, ax in zip(output_variables, axes.flat): | ||
# loop through the solutions to compare output_variables | ||
for solution in solutions: | ||
# plot summary variable v/s cycle number | ||
ax.plot( | ||
solution.summary_variables["Cycle number"], | ||
solution.summary_variables[var], | ||
) | ||
# label the axes | ||
ax.set_xlabel("Cycle number") | ||
ax.set_ylabel(var) | ||
ax.set_xlim([1, solution.summary_variables["Cycle number"][-1]]) | ||
|
||
fig.tight_layout() | ||
|
||
# add labels in legend | ||
if labels is not None: # pragma: no cover | ||
fig.legend(labels, loc="lower right") | ||
if not testing: # pragma: no cover | ||
plt.show() | ||
|
||
return axes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import pybamm | ||
import unittest | ||
import numpy as np | ||
|
||
|
||
class TestPlotSummaryVariables(unittest.TestCase): | ||
def test_plot(self): | ||
model = pybamm.lithium_ion.SPM({"SEI": "ec reaction limited"}) | ||
parameter_values = pybamm.ParameterValues( | ||
chemistry=pybamm.parameter_sets.Mohtat2020 | ||
) | ||
experiment = pybamm.Experiment( | ||
[ | ||
( | ||
"Discharge at C/10 for 10 hours or until 3.3 V", | ||
"Rest for 1 hour", | ||
"Charge at 1 A until 4.1 V", | ||
"Hold at 4.1 V until 50 mA", | ||
"Rest for 1 hour", | ||
) | ||
] | ||
* 3, | ||
) | ||
output_variables = [ | ||
"Capacity [A.h]", | ||
"Loss of lithium inventory [%]", | ||
"Loss of capacity to SEI [A.h]", | ||
"Loss of active material in negative electrode [%]", | ||
"Loss of active material in positive electrode [%]", | ||
"x_100", | ||
"x_0", | ||
"y_100", | ||
"y_0", | ||
] | ||
sim = pybamm.Simulation( | ||
model, experiment=experiment, parameter_values=parameter_values | ||
) | ||
sol = sim.solve(initial_soc=1) | ||
|
||
axes = pybamm.plot_summary_variables(sol, testing=True) | ||
|
||
axes = axes.flatten() | ||
self.assertEqual(len(axes), 9) | ||
|
||
for output_var, ax in zip(output_variables, axes): | ||
self.assertEqual(ax.get_xlabel(), "Cycle number") | ||
self.assertEqual(ax.get_ylabel(), output_var) | ||
|
||
cycle_number, var = ax.get_lines()[0].get_data() | ||
np.testing.assert_array_equal( | ||
cycle_number, sol.summary_variables["Cycle number"] | ||
) | ||
np.testing.assert_array_equal(var, sol.summary_variables[output_var]) | ||
|
||
axes = pybamm.plot_summary_variables( | ||
[sol, sol], labels=["SPM", "SPM"], testing=True | ||
) | ||
|
||
axes = axes.flatten() | ||
self.assertEqual(len(axes), 9) | ||
|
||
for output_var, ax in zip(output_variables, axes): | ||
self.assertEqual(ax.get_xlabel(), "Cycle number") | ||
self.assertEqual(ax.get_ylabel(), output_var) | ||
|
||
cycle_number, var = ax.get_lines()[0].get_data() | ||
np.testing.assert_array_equal( | ||
cycle_number, sol.summary_variables["Cycle number"] | ||
) | ||
np.testing.assert_array_equal(var, sol.summary_variables[output_var]) | ||
|
||
cycle_number, var = ax.get_lines()[1].get_data() | ||
np.testing.assert_array_equal( | ||
cycle_number, sol.summary_variables["Cycle number"] | ||
) | ||
np.testing.assert_array_equal(var, sol.summary_variables[output_var]) | ||
|
||
|
||
if __name__ == "__main__": | ||
print("Add -v for more debug output") | ||
import sys | ||
|
||
if "-v" in sys.argv: | ||
debug = True | ||
pybamm.settings.debug_mode = True | ||
unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can this be made to work if only a single solution is provided not in a list, e.g.