Repository: pybamm-team/PyBaMM
Pull request #4054 (2024-04-30 17:20:21)

switch thermal component plot to cumulative heating
# Description

Temperature components were confusing.
New plot:

<img width="1192" alt="Screenshot 2024-04-30 at 1 19 54 PM" src="https://github.com/pybamm-team/PyBaMM/assets/20817509/f2a3f0df-65ca-40a3-9b55-b5719a292189">


## Type of change

Please add a line in the relevant section of [CHANGELOG.md](https://github.com/pybamm-team/PyBaMM/blob/develop/CHANGELOG.md) to document the change (include PR #) - note reverse order of PR #s. If necessary, also add to the list of breaking changes.

- [ ] New feature (non-breaking change which adds functionality)
- [ ] Optimization (back-end change that speeds up the code)
- [ ] Bug fix (non-breaking change which fixes an issue)

# Key checklist:

- [ ] No style issues: `$ pre-commit run` (or `$ nox -s pre-commit`) (see [CONTRIBUTING.md](https://github.com/pybamm-team/PyBaMM/blob/develop/CONTRIBUTING.md#installing-and-using-pre-commit) for how to set this up to run automatically when committing locally, in just two lines of code)
- [ ] All tests pass: `$ python run-tests.py --all` (or `$ nox -s tests`)
- [ ] The documentation builds: `$ python run-tests.py --doctest` (or `$ nox -s doctests`)

You can run integration tests, unit tests, and doctests together at once, using `$ python run-tests.py --quick` (or `$ nox -s quick`).

## Further checks:

- [ ] Code is commented, particularly in hard-to-understand areas
- [ ] Tests added that prove fix is effective or that feature works



Patch:
diff --git a/pybamm/plotting/plot_thermal_components.py b/pybamm/plotting/plot_thermal_components.py
index 9cda7f2bd..8cb99a454 100644
--- a/pybamm/plotting/plot_thermal_components.py
+++ b/pybamm/plotting/plot_thermal_components.py
@@ -52,11 +52,7 @@ def plot_thermal_components(
 
     time_s = solution["Time [s]"].entries
     time_h = time_s / 3600
-    T = solution["X-averaged cell temperature [K]"].entries
     volume = solution["Cell thermal volume [m3]"].entries
-    rho_c_p_eff_av = solution[
-        "Volume-averaged effective heat capacity [J.K-1.m-3]"
-    ].entries
 
     heating_sources = [
         "Lumped total cooling",
@@ -65,7 +61,7 @@ def plot_thermal_components(
         "Reversible heating",
     ]
     try:
-        heats_volumetric = {
+        heats = {
             name: solution[name + " [W]"].entries / volume for name in heating_sources
         }
     except KeyError as err:
@@ -73,29 +69,33 @@ def plot_thermal_components(
             "plot_thermal_components is only implemented for lumped models"
         ) from err
 
-    dTs = {
-        name: cumulative_trapezoid(heat / rho_c_p_eff_av, time_s, initial=0)
-        for name, heat in heats_volumetric.items()
+    cumul_heats = {
+        name: cumulative_trapezoid(heat, time_s, initial=0)
+        for name, heat in heats.items()
     }
 
     # Plot
     # Initialise
     total_heat = 0
-    bottom_heat = heats_volumetric["Lumped total cooling"]
-    bottom_temp = T[0] + dTs["Lumped total cooling"]
+    bottom_heat = heats["Lumped total cooling"]
+    total_cumul_heat = 0
+    bottom_cumul_heat = cumul_heats["Lumped total cooling"]
     # Plot components
     for name in heating_sources:
-        top_temp = bottom_temp + abs(dTs[name])
-        ax[0].fill_between(time_h, bottom_temp, top_temp, **kwargs_fill, label=name)
-        bottom_temp = top_temp
-
-        top_heat = bottom_heat + abs(heats_volumetric[name])
-        ax[1].fill_between(time_h, bottom_heat, top_heat, **kwargs_fill, label=name)
+        top_heat = bottom_heat + abs(heats[name])
+        ax[0].fill_between(time_h, bottom_heat, top_heat, **kwargs_fill, label=name)
         bottom_heat = top_heat
-        total_heat += heats_volumetric[name]
+        total_heat += heats[name]
+
+        top_cumul_heat = bottom_cumul_heat + abs(cumul_heats[name])
+        ax[1].fill_between(
+            time_h, bottom_cumul_heat, top_cumul_heat, **kwargs_fill, label=name
+        )
+        bottom_cumul_heat = top_cumul_heat
+        total_cumul_heat += cumul_heats[name]
 
-    ax[0].plot(time_h, T, "k--", label="Cell temperature")
-    ax[1].plot(time_h, total_heat, "k--", label="Total heating")
+    ax[0].plot(time_h, total_heat, "k--")
+    ax[1].plot(time_h, total_cumul_heat, "k--", label="Total")
 
     if show_legend:
         leg = ax[1].legend(loc="center left", bbox_to_anchor=(1.05, 0.5), frameon=True)
@@ -106,8 +106,8 @@ def plot_thermal_components(
         a.set_xlabel("Time [h]")
         a.set_xlim([time_h[0], time_h[-1]])
 
-    ax[0].set_title("Temperatures [K]")
-    ax[1].set_title("Heat sources [W/m$^3$]")
+    ax[0].set_title("Heat generation [W/m$^3$]")
+    ax[1].set_title("Cumulative heat generation [J/m$^3$]")
 
     if fig is not None:
         fig.tight_layout()


Test patch:
diff --git a/tests/unit/test_plotting/test_plot_thermal_components.py b/tests/unit/test_plotting/test_plot_thermal_components.py
index f4179da2d..99b3d40ca 100644
--- a/tests/unit/test_plotting/test_plot_thermal_components.py
+++ b/tests/unit/test_plotting/test_plot_thermal_components.py
@@ -11,15 +11,22 @@ use("Agg")
 class TestPlotThermalComponents(TestCase):
     def test_plot_with_solution(self):
         model = pybamm.lithium_ion.SPM({"thermal": "lumped"})
-        sim = pybamm.Simulation(model)
+        sim = pybamm.Simulation(
+            model, parameter_values=pybamm.ParameterValues("ORegan2022")
+        )
         sol = sim.solve([0, 3600])
         for input_data in [sim, sol]:
             _, ax = pybamm.plot_thermal_components(input_data, show_plot=False)
-            t, T = ax[0].get_lines()[-1].get_data()
+            t, cumul_heat = ax[1].get_lines()[-1].get_data()
+            np.testing.assert_array_almost_equal(t, sol["Time [h]"].data)
+            t, cumul_heat = ax[1].get_lines()[-1].get_data()
+            T_sol = sol["X-averaged cell temperature [K]"].data
             np.testing.assert_array_almost_equal(t, sol["Time [h]"].data)
-            np.testing.assert_array_almost_equal(
-                T, sol["X-averaged cell temperature [K]"].data
-            )
+            rho_c_p_eff = sol[
+                "Volume-averaged effective heat capacity [J.K-1.m-3]"
+            ].data
+            T_plot = T_sol[0] + cumul_heat / rho_c_p_eff
+            np.testing.assert_allclose(T_sol, T_plot, rtol=1e-2)
 
             _, ax = plt.subplots(1, 2)
             _, ax_out = pybamm.plot_thermal_components(sol, ax=ax, show_legend=True)