Skip to content

Commit 0c92f94

Browse files
swastim01valentinsulzer
authored andcommitted
Add support for uniform grid sizing across subdomains (#720) (#5253)
Co-authored-by: Valentin Sulzer <[email protected]>
1 parent 6084d37 commit 0c92f94

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
## Features
2626

27+
- Added uniform grid sizing across subdomains in the x-dimension, ensuring consistent grid spacing when geometries have varying lengths. ([#5253](https://github.com/pybamm-team/PyBaMM/pull/5253))
2728
- Added the `electrode_phases` kwarg to `plot_voltage_components()` which allows choosing between plotting primary or secondary phase overpotentials. ([#5229](https://github.com/pybamm-team/PyBaMM/pull/5229))
2829
- Added the `num_steps_no_progress` and `t_no_progress` options in the `IDAKLUSolver` to early terminate the simulation if little progress is detected. ([#5201](https://github.com/pybamm-team/PyBaMM/pull/5201))
2930
- EvaluateAt symbol: add support for children evaluated at edges ([#5190](https://github.com/pybamm-team/PyBaMM/pull/5190))

src/pybamm/meshes/meshes.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,39 @@
99
import pybamm
1010

1111

12+
def compute_var_pts_from_thicknesses(electrode_thicknesses, grid_size):
13+
"""
14+
Compute a ``var_pts`` dictionary using electrode thicknesses and a target cell size (dx).
15+
16+
Added as per maintainer feedback in issue #<your-issue-number> to make mesh generation
17+
explicit — ``grid_size`` now represents the mesh cell size in metres.
18+
19+
Parameters
20+
----------
21+
electrode_thicknesses : dict
22+
Domain thicknesses in metres.
23+
grid_size : float
24+
Desired uniform mesh cell size (m).
25+
26+
Returns
27+
-------
28+
dict
29+
Mapping of each domain to its computed grid points.
30+
"""
31+
if not isinstance(electrode_thicknesses, dict):
32+
raise TypeError("electrode_thicknesses must be a dictionary")
33+
34+
if not isinstance(grid_size, (int | float)) or grid_size <= 0:
35+
raise ValueError("grid_size must be a positive number")
36+
37+
var_pts = {}
38+
for domain, thickness in electrode_thicknesses.items():
39+
npts = max(round(thickness / grid_size), 2)
40+
var_pts[domain] = {f"x_{domain[0]}": npts}
41+
42+
return var_pts
43+
44+
1245
class Mesh(dict):
1346
"""
1447
Mesh contains a list of submeshes on each subdomain.

tests/unit/test_meshes/test_meshes.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,37 @@ def test_to_json(self):
584584

585585
assert mesh_json == expected_json
586586

587+
def test_compute_var_pts_from_thicknesses_cell_size(self):
588+
from pybamm.meshes.meshes import compute_var_pts_from_thicknesses
589+
590+
electrode_thicknesses = {
591+
"negative electrode": 100e-6,
592+
"separator": 25e-6,
593+
"positive electrode": 100e-6,
594+
}
595+
596+
cell_size = 5e-6 # 5 micrometres per cell
597+
var_pts = compute_var_pts_from_thicknesses(electrode_thicknesses, cell_size)
598+
599+
assert isinstance(var_pts, dict)
600+
assert all(isinstance(v, dict) for v in var_pts.values())
601+
assert var_pts["negative electrode"]["x_n"] == 20
602+
assert var_pts["separator"]["x_s"] == 5
603+
assert var_pts["positive electrode"]["x_p"] == 20
604+
605+
def test_compute_var_pts_from_thicknesses_invalid_thickness_type(self):
606+
from pybamm.meshes.meshes import compute_var_pts_from_thicknesses
607+
608+
with pytest.raises(TypeError):
609+
compute_var_pts_from_thicknesses(["not", "a", "dict"], 1e-6)
610+
611+
def test_compute_var_pts_from_thicknesses_invalid_grid_size(self):
612+
from pybamm.meshes.meshes import compute_var_pts_from_thicknesses
613+
614+
electrode_thicknesses = {"negative electrode": 100e-6}
615+
with pytest.raises(ValueError):
616+
compute_var_pts_from_thicknesses(electrode_thicknesses, -1e-6)
617+
587618

588619
class TestMeshGenerator:
589620
def test_init_name(self):

0 commit comments

Comments
 (0)