Skip to content
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

Geometric input params #4665

Merged
merged 22 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Features

- Added symbolic mesh which allows for using InputParameters for geometric parameters ([#4665](https://github.com/pybamm-team/PyBaMM/pull/4665))
- Enhanced the `search` method to accept multiple search terms in the form of a string or a list. ([#4650](https://github.com/pybamm-team/PyBaMM/pull/4650))
- Made composite electrode model compatible with particle size distribution ([#4687](https://github.com/pybamm-team/PyBaMM/pull/4687))
- Added `Symbol.post_order()` method to return an iterable that steps through the tree in post-order fashion. ([#4684](https://github.com/pybamm-team/PyBaMM/pull/4684))
Expand Down
1 change: 1 addition & 0 deletions src/pybamm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
Chebyshev1DSubMesh,
UserSupplied1DSubMesh,
SpectralVolume1DSubMesh,
SymbolicUniform1DSubMesh,
)
from .meshes.scikit_fem_submeshes import (
ScikitSubMesh2D,
Expand Down
47 changes: 37 additions & 10 deletions src/pybamm/meshes/meshes.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ def __init__(self, geometry, submesh_types, var_pts):
if isinstance(sym, pybamm.Symbol):
try:
sym_eval = sym.evaluate()
except KeyError:
sym_eval = sym
except NotImplementedError as error:
if sym.has_symbol_of_classes(pybamm.Parameter):
raise pybamm.DiscretisationError(
Expand Down Expand Up @@ -169,7 +171,12 @@ def combine_submeshes(self, *submeshnames):
# Check that the final edge of each submesh is the same as the first edge of the
# next submesh
for i in range(len(submeshnames) - 1):
if self[submeshnames[i]].edges[-1] != self[submeshnames[i + 1]].edges[0]:
if self[submeshnames[i]].edges[-1] == self[submeshnames[i + 1]].edges[0]:
pass
elif hasattr(self[submeshnames[i + 1]], "min"):
# Circle back to this, but this should let the case of submesh i+1 be a symbolic submesh
pass
else:
raise pybamm.DomainError("submesh edges are not aligned")

coord_sys = self[submeshnames[i]].coord_sys
Expand All @@ -184,10 +191,26 @@ def combine_submeshes(self, *submeshnames):
)
coord_sys = self[submeshnames[0]].coord_sys
submesh = pybamm.SubMesh1D(combined_submesh_edges, coord_sys)
if getattr(self[submeshnames[0]], "length", None) is not None:
# Assume that the ghost cells have the same length as the first submesh
if any("ghost" in submeshname for submeshname in submeshnames):
submesh_min = self[submeshnames[0]].min
submesh_length = self[submeshnames[0]].length
# If not ghost cells, then the length is the sum of the lengths of the submeshes
else:
submesh_min = self[submeshnames[0]].min
submesh_length = sum(
[self[submeshname].length for submeshname in submeshnames]
)
submesh.length = submesh_length
submesh.min = submesh_min
# add in internal boundaries
submesh.internal_boundaries = [
self[submeshname].edges[0] for submeshname in submeshnames[1:]
]
for submeshname in submeshnames[1:]:
if getattr(self[submeshname], "length", None) is not None:
min = self[submeshname].min
else:
min = 0
submesh.internal_boundaries.append(self[submeshname].edges[0] + min)
return submesh

def add_ghost_meshes(self):
Expand All @@ -210,16 +233,20 @@ def add_ghost_meshes(self):

# left ghost cell: two edges, one node, to the left of existing submesh
lgs_edges = np.array([2 * edges[0] - edges[1], edges[0]])
self[domain[0] + "_left ghost cell"] = pybamm.SubMesh1D(
lgs_edges, submesh.coord_sys
)
lgs_submesh = pybamm.SubMesh1D(lgs_edges, submesh.coord_sys)
if getattr(submesh, "length", None) is not None:
lgs_submesh.length = submesh.length
lgs_submesh.min = submesh.min
self[domain[0] + "_left ghost cell"] = lgs_submesh

# right ghost cell: two edges, one node, to the right of
# existing submesh
rgs_edges = np.array([edges[-1], 2 * edges[-1] - edges[-2]])
self[domain[0] + "_right ghost cell"] = pybamm.SubMesh1D(
rgs_edges, submesh.coord_sys
)
rgs_submesh = pybamm.SubMesh1D(rgs_edges, submesh.coord_sys)
if getattr(submesh, "length", None) is not None:
rgs_submesh.length = submesh.length
rgs_submesh.min = submesh.min
self[domain[0] + "_right ghost cell"] = rgs_submesh

@property
def geometry(self):
Expand Down
24 changes: 24 additions & 0 deletions src/pybamm/meshes/one_dimensional_submeshes.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,30 @@ def to_json(self):
return json_dict


class SymbolicUniform1DSubMesh(SubMesh1D):
def __init__(self, lims, npts, tabs=None):
spatial_var, spatial_lims, tabs = self.read_lims(lims)
coord_sys = spatial_var.coord_sys
x0 = spatial_lims["min"]
if tabs is not None:
raise NotImplementedError("Tabs not supported for symbolic uniform submesh")
if coord_sys != "cartesian" and spatial_lims["min"] != 0:
raise pybamm.GeometryError(
"Symbolic uniform submesh with non-cartesian coordinates and non-zero minimum not supported"
)
npts = npts[spatial_var.name]
length = spatial_lims["max"] - x0
self.edges = np.linspace(0, 1, npts + 1)
self.length = length
self.min = x0
self.nodes = (self.edges[1:] + self.edges[:-1]) / 2
self.d_edges = np.diff(self.edges)
self.d_nodes = np.diff(self.nodes)
self.npts = self.nodes.size
self.coord_sys = coord_sys
self.internal_boundaries = []


class Uniform1DSubMesh(SubMesh1D):
"""
A class to generate a uniform submesh on a 1D domain
Expand Down
10 changes: 6 additions & 4 deletions src/pybamm/parameters/parameter_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,10 +604,12 @@ def process_geometry(self, geometry):

def process_and_check(sym):
new_sym = self.process_symbol(sym)
if not isinstance(new_sym, pybamm.Scalar):
raise ValueError(
"Geometry parameters must be Scalars after parameter processing"
)
# if not isinstance(new_sym, pybamm.Scalar) and not isinstance(
# new_sym, pybamm.InputParameter
# ):
# raise ValueError(
# "Geometry parameters must be Scalars or InputParameters after parameter processing"
# )
return new_sym

for domain in geometry:
Expand Down
Loading
Loading