Skip to content

Commit a75962c

Browse files
Rishab87rtimms
andauthored
2D & 3D Heatmaps (#5130)
* added plotting for 3D thermal , 2d slices and 3d heatmap * adding asymmetric cooling egs * adding unit tests * adding docs * formatting * adding mesh overlay in plots * minor changes in egs * minor changes in egs * Update src/pybamm/plotting/plot_3d_heatmap.py Co-authored-by: Robert Timms <[email protected]> * addressed reviews * changing parameter in prismatic eg * name changes * minor change * making matplotlib import optional * minor change in doc * updating changelog * removing uncessary else case * changed the file name --------- Co-authored-by: Robert Timms <[email protected]>
1 parent 0b92287 commit a75962c

File tree

12 files changed

+1348
-1
lines changed

12 files changed

+1348
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# [Unreleased](https://github.com/pybamm-team/PyBaMM/)
22

33
## Features
4+
- Added `plot_3d_cross_section` & `plot_3d_heatmap` functions to support plotting for 3D thermal simulations. ([#5130](https://github.com/pybamm-team/PyBaMM/pull/5130))
45
- Added a `Basic3DThermalSPM` with two way coupling. ([#5112](https://github.com/pybamm-team/PyBaMM/pull/5112))
56
- Enables the passing of `inputs` throughout `set_initial_soc`. ([#5122](https://github.com/pybamm-team/PyBaMM/pull/5122))
67
- Adds `on_failure` option to `BaseSolver` with options for `"warn"`, `"ignore"`, and `"raise"` to change behaviour on solver failure. Defaults to "raise" to retain historic functionality. ([#5105](https://github.com/pybamm-team/PyBaMM/pull/5105))

docs/source/api/plotting/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ Plotting
88
plot_2D
99
plot_voltage_components
1010
plot_summary_variables
11+
plot_3d_cross_section
12+
plot_3d_heatmap
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Plot Cross Section
2+
==================
3+
4+
.. autofunction:: pybamm.plot_3d_cross_section
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Plot 3D Heatmap
2+
===============
3+
4+
.. autofunction:: pybamm.plot_3d_heatmap
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import pybamm
2+
3+
print("Setting up asymmetric cooling simulation...")
4+
model_3d = pybamm.lithium_ion.Basic3DThermalSPM(
5+
options={"cell geometry": "pouch", "dimensionality": 3}
6+
)
7+
8+
parameter_values = pybamm.ParameterValues("Ecker2015")
9+
10+
# Define our cooling scenario
11+
h_cooling = 20 # W.m-2.K-1 -> A cooling plate on one side
12+
h_insulation = 0.1 # W.m-2.K-1 -> Good insulation on other sides
13+
14+
parameter_values.update(
15+
{
16+
# Apply strong cooling to the RIGHT face of the cell
17+
"Right face heat transfer coefficient [W.m-2.K-1]": h_cooling,
18+
# Insulate all other faces
19+
"Left face heat transfer coefficient [W.m-2.K-1]": h_insulation,
20+
"Front face heat transfer coefficient [W.m-2.K-1]": h_insulation,
21+
"Back face heat transfer coefficient [W.m-2.K-1]": h_insulation,
22+
"Bottom face heat transfer coefficient [W.m-2.K-1]": h_insulation,
23+
"Top face heat transfer coefficient [W.m-2.K-1]": h_insulation,
24+
},
25+
check_already_exists=False,
26+
)
27+
28+
# Use a high discharge rate to generate significant heat
29+
experiment = pybamm.Experiment([("Discharge at 4C until 2.5V")])
30+
31+
var_pts = {
32+
"x_n": 20,
33+
"x_s": 20,
34+
"x_p": 20,
35+
"r_n": 30,
36+
"r_p": 30,
37+
"x": None,
38+
"y": None,
39+
"z": None,
40+
}
41+
42+
submesh_types = model_3d.default_submesh_types
43+
submesh_types["cell"] = pybamm.ScikitFemGenerator3D("pouch", h="0.01") # very fine mesh
44+
45+
sim = pybamm.Simulation(
46+
model_3d,
47+
parameter_values=parameter_values,
48+
var_pts=var_pts,
49+
experiment=experiment,
50+
submesh_types=submesh_types,
51+
)
52+
print("Solving... (this may take a minute)")
53+
solution = sim.solve()
54+
print("Solve complete.")
55+
56+
print("Generating plots...")
57+
final_time = solution.t[-1]
58+
59+
# First, plot the overall voltage and average temperature
60+
solution.plot(["Voltage [V]", "Volume-averaged cell temperature [K]"])
61+
62+
# Now, create detailed heatmaps at the final timestep
63+
print(f"\n--- Displaying heatmaps at t={final_time:.0f}s ---")
64+
65+
# Plot a slice through the center, showing the gradient from left (hot) to right (cool)
66+
pybamm.plot_3d_cross_section(
67+
solution,
68+
plane="xz",
69+
variable="Cell temperature [K]",
70+
t=None, # Last time stamp
71+
position=0.5,
72+
show_mesh=True,
73+
mesh_color="white",
74+
mesh_alpha=0.4,
75+
mesh_linewidth=0.7,
76+
)
77+
78+
# Plot a slice showing the temperature distribution across the face of the cell
79+
pybamm.plot_3d_cross_section(
80+
solution,
81+
plane="yz",
82+
position=0.5,
83+
t=None,
84+
variable="Cell temperature [K]",
85+
show_mesh=True,
86+
mesh_color="white",
87+
mesh_alpha=0.4,
88+
mesh_linewidth=0.7,
89+
)
90+
91+
# Plot a 3D heatmap of the temperature distribution
92+
pybamm.plot_3d_heatmap(
93+
solution, t=final_time, marker_size=5, variable="Cell temperature [K]"
94+
)
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import pybamm
2+
3+
print("Setting up asymmetric radial cooling simulation for a cylinder...")
4+
model_3d = pybamm.lithium_ion.Basic3DThermalSPM(
5+
options={"cell geometry": "cylindrical", "dimensionality": 3}
6+
)
7+
8+
# Use parameters for a cylindrical 18650 cell
9+
parameter_values = pybamm.ParameterValues("Chen2020")
10+
11+
# Define our cooling scenario
12+
h_cooling = 20 # W.m-2.K-1 -> Cooling on the outside surface
13+
h_insulation = 0.1 # W.m-2.K-1 -> Insulation on other surfaces
14+
15+
parameter_values.update(
16+
{
17+
"Inner cell radius [m]": 0.005,
18+
"Outer cell radius [m]": 0.018,
19+
# Apply cooling to the outer radial surface
20+
"Outer radius heat transfer coefficient [W.m-2.K-1]": h_cooling,
21+
# Insulate the inner radius and the flat top/bottom faces
22+
"Inner radius heat transfer coefficient [W.m-2.K-1]": h_insulation,
23+
"Bottom face heat transfer coefficient [W.m-2.K-1]": h_insulation,
24+
"Top face heat transfer coefficient [W.m-2.K-1]": h_insulation,
25+
},
26+
check_already_exists=False,
27+
)
28+
29+
# Use a high discharge rate to generate significant heat
30+
experiment = pybamm.Experiment([("Discharge at 4C until 2.5V")])
31+
32+
var_pts = {
33+
"x_n": 20,
34+
"x_s": 20,
35+
"x_p": 20,
36+
"r_n": 30,
37+
"r_p": 30,
38+
"r_macro": None,
39+
"z": None,
40+
}
41+
submesh_types = model_3d.default_submesh_types
42+
submesh_types["cell"] = pybamm.ScikitFemGenerator3D(
43+
"cylinder", h="0.01"
44+
) # very fine mesh
45+
46+
sim = pybamm.Simulation(
47+
model_3d,
48+
parameter_values=parameter_values,
49+
var_pts=var_pts,
50+
experiment=experiment,
51+
submesh_types=submesh_types,
52+
)
53+
print("Solving... (this may take a minute)")
54+
solution = sim.solve()
55+
print("Solve complete.")
56+
57+
print("Generating plots...")
58+
final_time = solution.t[-1]
59+
60+
# Plot the overall voltage and average temperature
61+
solution.plot(["Voltage [V]", "Volume-averaged cell temperature [K]"])
62+
63+
# Create detailed heatmaps at the final timestep
64+
print(f"\n--- Displaying heatmaps at t={final_time:.0f}s ---")
65+
66+
# Plot the r-z plane to show the radial gradient (hot core, cool edge)
67+
pybamm.plot_3d_cross_section(
68+
solution,
69+
"Cell temperature [K]",
70+
None, # Use the last time step
71+
plane="rz",
72+
position=0.5,
73+
show_mesh=True,
74+
mesh_color="white",
75+
mesh_alpha=0.4,
76+
mesh_linewidth=0.7,
77+
)
78+
79+
# Plot the polar xy-plane to show the temperature at a mid-height slice
80+
pybamm.plot_3d_cross_section(
81+
solution,
82+
"Cell temperature [K]",
83+
None, # Use the last time step
84+
plane="xy",
85+
position=0.5,
86+
show_mesh=True,
87+
mesh_color="white",
88+
mesh_alpha=0.4,
89+
mesh_linewidth=0.7,
90+
)
91+
92+
# Plot a 3D heatmap of the temperature distribution
93+
pybamm.plot_3d_heatmap(
94+
solution, t=final_time, marker_size=5, variable="Cell temperature [K]"
95+
)

src/pybamm/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@
192192
from .plotting.plot_thermal_components import plot_thermal_components
193193
from .plotting.plot_summary_variables import plot_summary_variables
194194
from .plotting.dynamic_plot import dynamic_plot
195+
from .plotting.plot_3d_cross_section import plot_3d_cross_section
196+
from .plotting.plot_3d_heatmap import plot_3d_heatmap
195197

196198
# Simulation
197199
from .simulation import Simulation, load_sim, is_notebook

src/pybamm/plotting/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
__all__ = ['dynamic_plot', 'plot', 'plot2D', 'plot_summary_variables',
2-
'plot_thermal_components', 'plot_voltage_components', 'quick_plot']
2+
'plot_thermal_components', 'plot_voltage_components', 'quick_plot', 'plot_3d_cross_section', 'plot_3d_heatmap']

0 commit comments

Comments
 (0)