|
| 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