Skip to content

Commit

Permalink
add a few more corrections, be smarter about errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Bills committed Dec 11, 2024
1 parent e4fff00 commit 831f97e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
12 changes: 8 additions & 4 deletions src/pybamm/meshes/one_dimensional_submeshes.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,19 @@ def to_json(self):
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 ValueError("Tabs not supported for symbolic uniform submesh")
raise NotImplementedError("Tabs not supported for symbolic uniform submesh")
if coord_sys != "cartesian" and spatial_lims["min"] != 0:
raise NotImplementedError(
"Symbolic uniform submesh with non-cartesian coordinates and non-zero minimum not supported"
)
npts = npts[spatial_var.name]
length = spatial_lims["max"] - spatial_lims["min"]
x0 = spatial_lims["min"]
length = spatial_lims["max"] - x0
self.edges = np.linspace(0, 1, npts + 1)
self.length = length
self.min = x0
coord_sys = spatial_var.coord_sys
self.nodes = (self.edges[1:] + self.edges[:-1]) / 2
self.d_edges = np.diff(self.edges)
self.d_nodes = np.diff(self.nodes)
Expand Down
28 changes: 15 additions & 13 deletions src/pybamm/spatial_methods/finite_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,19 +210,9 @@ def divergence_matrix(self, domains):
matrix = csr_matrix(kron(eye(second_dim_repeats), sub_matrix))
if getattr(submesh, "length", None) is not None:
if submesh.coord_sys == "spherical polar":
if submesh.min != 0:
raise AssertionError(
"Spherical polar symbolic meshes must have min r = 0"
)
else:
return pybamm.Matrix(matrix) * (1 / submesh.length**3)
return pybamm.Matrix(matrix) * (1 / submesh.length**3)
elif submesh.coord_sys == "cylindrical polar":
if submesh.min != 0:
raise AssertionError(
"Cylindrical polar symbolic meshes must have min r = 0"
)
else:
return pybamm.Matrix(matrix) * (1 / (submesh.length**2))
return pybamm.Matrix(matrix) * (1 / (submesh.length**2))
else:
return pybamm.Matrix(matrix) * (1 / submesh.length)
else:
Expand Down Expand Up @@ -336,7 +326,15 @@ def definite_integral_matrix(
# not supported by the default kron format
# Note that this makes column-slicing inefficient, but this should not be an
# issue
return pybamm.Matrix(csr_matrix(matrix))
matrix = pybamm.Matrix(csr_matrix(matrix))
if hasattr(submesh, "length"):
if submesh.coord_sys == "spherical polar":
matrix = matrix * submesh.length**3
elif submesh.coord_sys == "cylindrical polar":
matrix = matrix * submesh.length**2
else:
matrix = matrix * submesh.length
return matrix

def indefinite_integral(self, child, discretised_child, direction):
"""Implementation of the indefinite integral operator."""
Expand Down Expand Up @@ -463,6 +461,8 @@ def indefinite_integral_matrix_edges(self, domains, direction):
# add a column of zeros at each end
zero_col = csr_matrix((n, 1))
sub_matrix = hstack([zero_col, sub_matrix, zero_col])
if hasattr(submesh, "length"):
sub_matrix = sub_matrix * submesh.length
# Convert to csr_matrix so that we can take the index (row-slicing), which is
# not supported by the default kron format
# Note that this makes column-slicing inefficient, but this should not be an
Expand Down Expand Up @@ -508,6 +508,8 @@ def indefinite_integral_matrix_nodes(self, domains, direction):
# Note that this makes column-slicing inefficient, but this should not be an
# issue
matrix = csr_matrix(kron(eye(second_dim_repeats), sub_matrix))
if hasattr(submesh, "length"):
matrix = matrix * submesh.length

return pybamm.Matrix(matrix)

Expand Down

0 comments on commit 831f97e

Please sign in to comment.