Skip to content

Commit

Permalink
test mesh
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Bills committed Dec 20, 2024
1 parent 8da3481 commit 037d4b4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/pybamm/meshes/one_dimensional_submeshes.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def __init__(self, lims, npts, tabs=None):
if tabs is not None:
raise NotImplementedError("Tabs not supported for symbolic uniform submesh")
if coord_sys != "cartesian" and spatial_lims["min"] != 0:
raise NotImplementedError(
raise pybamm.GeometryError(
"Symbolic uniform submesh with non-cartesian coordinates and non-zero minimum not supported"
)
npts = npts[spatial_var.name]
Expand Down
49 changes: 45 additions & 4 deletions tests/unit/test_meshes/test_one_dimensional_submesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ def r():
return r


@pytest.fixture()
def x():
return pybamm.SpatialVariable(
"x", domain=["negative electrode"], coord_sys="cartesian"
)


@pytest.fixture()
def geometry(r):
geometry = {
Expand Down Expand Up @@ -90,18 +97,34 @@ def test_symmetric_mesh_creation_no_parameters(self, r, geometry):


class TestSymbolicUniform1DSubMesh:
def test_exceptions(self):
def test_exceptions(self, r):
lims = {"a": 1, "b": 2}
with pytest.raises(pybamm.GeometryError):
pybamm.SymbolicUniform1DSubMesh(lims, None)
lims = {"x_n": {"min": 0, "max": 1}}
npts = {"x_n": 10}
tabs = {"negative": {"z_centre": 0}, "positive": {"z_centre": 1}}
lims["tabs"] = tabs

with pytest.raises(NotImplementedError):
pybamm.SymbolicUniform1DSubMesh(lims, 20, tabs=tabs)
pybamm.SymbolicUniform1DSubMesh(lims, npts, tabs=tabs)

def test_symmetric_mesh_creation_no_parameters(self, r, geometry):
submesh_types = {"negative particle": pybamm.Uniform1DSubMesh}
submesh_types = {"negative particle": pybamm.SymbolicUniform1DSubMesh}
var_pts = {r: 20}
geometry = {
"negative particle": {
r: {"min": pybamm.InputParameter("min"), "max": pybamm.Scalar(2)}
}
}
with pytest.raises(pybamm.GeometryError):
pybamm.Mesh(geometry, submesh_types, var_pts)

def test_mesh_creation(self, r, x):
submesh_types = {"negative particle": pybamm.SymbolicUniform1DSubMesh}
var_pts = {r: 20}
geometry = {
"negative particle": {r: {"min": pybamm.Scalar(0), "max": pybamm.Scalar(2)}}
}

# create mesh
mesh = pybamm.Mesh(geometry, submesh_types, var_pts)
Expand All @@ -110,13 +133,31 @@ def test_symmetric_mesh_creation_no_parameters(self, r, geometry):
assert mesh["negative particle"].edges[0] == 0
assert mesh["negative particle"].edges[-1] == 1

# check scaling and min/max
assert mesh["negative particle"].length == 2
assert mesh["negative particle"].min == 0

# check number of edges and nodes
assert len(mesh["negative particle"].nodes) == var_pts[r]
assert (
len(mesh["negative particle"].edges)
== len(mesh["negative particle"].nodes) + 1
)

# Check that length and min are scaled correctly
submesh_types = {"negative electrode": pybamm.SymbolicUniform1DSubMesh}
var_pts = {x: 20}
geometry = {
"negative electrode": {
x: {"min": pybamm.InputParameter("min"), "max": pybamm.Scalar(2)}
}
}
mesh = pybamm.Mesh(geometry, submesh_types, var_pts)
assert mesh["negative electrode"].length == pybamm.Scalar(
2
) - pybamm.InputParameter("min")
assert mesh["negative electrode"].min == pybamm.InputParameter("min")


class TestExponential1DSubMesh:
def test_symmetric_mesh_creation_no_parameters_even(self, r, geometry):
Expand Down
6 changes: 0 additions & 6 deletions tests/unit/test_parameters/test_parameter_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,12 +914,6 @@ def test_process_complex_expression(self):
exp_param = param.process_symbol(expression)
assert exp_param == 3.0 * (2.0**var2) / ((-4.0 + var1) + var2)

def test_process_geometry(self):
var = pybamm.Variable("var")
geometry = {"negative electrode": {"x": {"min": 0, "max": var}}}
with pytest.raises(ValueError, match="Geometry parameters must be Scalars"):
pybamm.ParameterValues({}).process_geometry(geometry)

def test_process_model(self):
model = pybamm.BaseModel()
a = pybamm.Parameter("a")
Expand Down

0 comments on commit 037d4b4

Please sign in to comment.