Skip to content

Commit

Permalink
fix bug, reimplement value error, make sure multiplicative is always …
Browse files Browse the repository at this point in the history
…defined
  • Loading branch information
Alexander Bills committed Dec 11, 2024
1 parent 831f97e commit d468be4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/pybamm/parameters/parameter_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +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) 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
28 changes: 27 additions & 1 deletion src/pybamm/spatial_methods/finite_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,8 @@ def boundary_value_or_flux(self, symbol, discretised_child, bcs=None):
# just use the value from the bc: f(x*)
sub_matrix = csr_matrix((1, prim_pts))
additive = bcs[child][symbol.side][0]
additive_multiplicative = pybamm.Scalar(1)
multiplicative = pybamm.Scalar(1)

elif symbol.side == "left":
if extrap_order_value == "linear":
Expand All @@ -903,13 +905,19 @@ def boundary_value_or_flux(self, symbol, discretised_child, bcs=None):
sub_matrix = csr_matrix(([1], ([0], [0])), shape=(1, prim_pts))

additive = -dx0 * bcs[child][symbol.side][0]
if hasattr(submesh, "length"):
additive_multiplicative = submesh.length

Check warning on line 909 in src/pybamm/spatial_methods/finite_volume.py

View check run for this annotation

Codecov / codecov/patch

src/pybamm/spatial_methods/finite_volume.py#L909

Added line #L909 was not covered by tests
else:
additive_multiplicative = pybamm.Scalar(1)
multiplicative = pybamm.Scalar(1)

else:
sub_matrix = csr_matrix(
([1 + (dx0 / dx1), -(dx0 / dx1)], ([0, 0], [0, 1])),
shape=(1, prim_pts),
)
additive = pybamm.Scalar(0)
additive_multiplicative = pybamm.Scalar(1)
multiplicative = pybamm.Scalar(1)

elif extrap_order_value == "quadratic":
Expand All @@ -924,6 +932,10 @@ def boundary_value_or_flux(self, symbol, discretised_child, bcs=None):
([a, b], ([0, 0], [0, 1])), shape=(1, prim_pts)
)
additive = alpha * bcs[child][symbol.side][0]
if hasattr(submesh, "length"):
additive_multiplicative = submesh.length

Check warning on line 936 in src/pybamm/spatial_methods/finite_volume.py

View check run for this annotation

Codecov / codecov/patch

src/pybamm/spatial_methods/finite_volume.py#L936

Added line #L936 was not covered by tests
else:
additive_multiplicative = pybamm.Scalar(1)
multiplicative = pybamm.Scalar(1)

else:
Expand All @@ -936,6 +948,7 @@ def boundary_value_or_flux(self, symbol, discretised_child, bcs=None):
)

additive = pybamm.Scalar(0)
additive_multiplicative = pybamm.Scalar(1)
multiplicative = pybamm.Scalar(1)

else:
Expand All @@ -954,8 +967,10 @@ def boundary_value_or_flux(self, symbol, discretised_child, bcs=None):
additive = dxN * bcs[child][symbol.side][0]
if hasattr(submesh, "length"):
multiplicative = submesh.length
additive_multiplicative = submesh.length

Check warning on line 970 in src/pybamm/spatial_methods/finite_volume.py

View check run for this annotation

Codecov / codecov/patch

src/pybamm/spatial_methods/finite_volume.py#L969-L970

Added lines #L969 - L970 were not covered by tests
else:
multiplicative = pybamm.Scalar(1)
additive_multiplicative = pybamm.Scalar(1)
else:
# to find value at x* use formula:
# f(x*) = f_N - (dxN / dxNm1) (f_N - f_Nm1)
Expand All @@ -967,6 +982,7 @@ def boundary_value_or_flux(self, symbol, discretised_child, bcs=None):
shape=(1, prim_pts),
)
additive = pybamm.Scalar(0)
additive_multiplicative = pybamm.Scalar(1)
multiplicative = pybamm.Scalar(1)
elif extrap_order_value == "quadratic":
if use_bcs and pybamm.has_bc_of_form(
Expand All @@ -981,6 +997,10 @@ def boundary_value_or_flux(self, symbol, discretised_child, bcs=None):
)

additive = alpha * bcs[child][symbol.side][0]
if hasattr(submesh, "length"):
additive_multiplicative = submesh.length

Check warning on line 1001 in src/pybamm/spatial_methods/finite_volume.py

View check run for this annotation

Codecov / codecov/patch

src/pybamm/spatial_methods/finite_volume.py#L1001

Added line #L1001 was not covered by tests
else:
additive_multiplicative = pybamm.Scalar(1)
multiplicative = pybamm.Scalar(1)
else:
a = (
Expand All @@ -999,6 +1019,7 @@ def boundary_value_or_flux(self, symbol, discretised_child, bcs=None):
shape=(1, prim_pts),
)
additive = pybamm.Scalar(0)
additive_multiplicative = pybamm.Scalar(1)
multiplicative = pybamm.Scalar(1)
else:
raise NotImplementedError
Expand All @@ -1008,6 +1029,7 @@ def boundary_value_or_flux(self, symbol, discretised_child, bcs=None):
# just use the value from the bc: f'(x*)
sub_matrix = csr_matrix((1, prim_pts))
additive = bcs[child][symbol.side][0]
additive_multiplicative = pybamm.Scalar(1)
multiplicative = pybamm.Scalar(1)

Check warning on line 1033 in src/pybamm/spatial_methods/finite_volume.py

View check run for this annotation

Codecov / codecov/patch

src/pybamm/spatial_methods/finite_volume.py#L1032-L1033

Added lines #L1032 - L1033 were not covered by tests

elif symbol.side == "left":
Expand All @@ -1017,6 +1039,7 @@ def boundary_value_or_flux(self, symbol, discretised_child, bcs=None):
([-1, 1], ([0, 0], [0, 1])), shape=(1, prim_pts)
)
additive = pybamm.Scalar(0)
additive_multiplicative = pybamm.Scalar(1)
if hasattr(submesh, "length"):
multiplicative = 1 / submesh.length

Check warning on line 1044 in src/pybamm/spatial_methods/finite_volume.py

View check run for this annotation

Codecov / codecov/patch

src/pybamm/spatial_methods/finite_volume.py#L1044

Added line #L1044 was not covered by tests
else:
Expand All @@ -1031,6 +1054,7 @@ def boundary_value_or_flux(self, symbol, discretised_child, bcs=None):
([a, b, c], ([0, 0, 0], [0, 1, 2])), shape=(1, prim_pts)
)
additive = pybamm.Scalar(0)
additive_multiplicative = pybamm.Scalar(1)
if hasattr(submesh, "length"):
multiplicative = 1 / submesh.length

Check warning on line 1059 in src/pybamm/spatial_methods/finite_volume.py

View check run for this annotation

Codecov / codecov/patch

src/pybamm/spatial_methods/finite_volume.py#L1059

Added line #L1059 was not covered by tests
else:
Expand All @@ -1048,6 +1072,7 @@ def boundary_value_or_flux(self, symbol, discretised_child, bcs=None):
shape=(1, prim_pts),
)
additive = pybamm.Scalar(0)
additive_multiplicative = pybamm.Scalar(1)
if hasattr(submesh, "length"):
multiplicative = 1 / submesh.length

Check warning on line 1077 in src/pybamm/spatial_methods/finite_volume.py

View check run for this annotation

Codecov / codecov/patch

src/pybamm/spatial_methods/finite_volume.py#L1077

Added line #L1077 was not covered by tests
else:
Expand All @@ -1066,6 +1091,7 @@ def boundary_value_or_flux(self, symbol, discretised_child, bcs=None):
shape=(1, prim_pts),
)
additive = pybamm.Scalar(0)
additive_multiplicative = pybamm.Scalar(1)
if hasattr(submesh, "length"):
multiplicative = 1 / submesh.length

Check warning on line 1096 in src/pybamm/spatial_methods/finite_volume.py

View check run for this annotation

Codecov / codecov/patch

src/pybamm/spatial_methods/finite_volume.py#L1096

Added line #L1096 was not covered by tests
else:
Expand All @@ -1086,7 +1112,7 @@ def boundary_value_or_flux(self, symbol, discretised_child, bcs=None):
boundary_value.copy_domains(symbol)

additive.copy_domains(symbol)
boundary_value += additive
boundary_value += additive * additive_multiplicative

return boundary_value

Expand Down
25 changes: 25 additions & 0 deletions tests/unit/test_meshes/test_one_dimensional_submesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,31 @@ def test_symmetric_mesh_creation_no_parameters(self, r, geometry):
)


class TestSymbolicUniform1DSubMesh:
def test_exceptions(self):
lims = {"a": 1, "b": 2}
with pytest.raises(pybamm.GeometryError):
pybamm.SymbolicUniform1DSubMesh(lims, None)

def test_symmetric_mesh_creation_no_parameters(self, r, geometry):
submesh_types = {"negative particle": pybamm.Uniform1DSubMesh}
var_pts = {r: 20}

# create mesh
mesh = pybamm.Mesh(geometry, submesh_types, var_pts)

# check boundary locations
assert mesh["negative particle"].edges[0] == 0
assert mesh["negative particle"].edges[-1] == 1

# 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
)


class TestExponential1DSubMesh:
def test_symmetric_mesh_creation_no_parameters_even(self, r, geometry):
submesh_params = {"side": "symmetric"}
Expand Down

0 comments on commit d468be4

Please sign in to comment.