From 6c918270a406fc3839c38d1ad8a4058fa0d05ed8 Mon Sep 17 00:00:00 2001 From: Ferran Brosa Planella Date: Tue, 22 Sep 2020 10:24:16 +0200 Subject: [PATCH 1/6] #1174 added error message and test --- pybamm/discretisations/discretisation.py | 9 ++++ .../test_discretisation.py | 52 +++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/pybamm/discretisations/discretisation.py b/pybamm/discretisations/discretisation.py index 5c792767fe..d27cf7d03f 100644 --- a/pybamm/discretisations/discretisation.py +++ b/pybamm/discretisations/discretisation.py @@ -492,6 +492,15 @@ def process_boundary_conditions(self, model): for key, bcs in model.boundary_conditions.items(): processed_bcs[key.id] = {} + for subdomain in key.domain: + if self.mesh[subdomain].coord_sys == "spherical polar": + if bcs["left"][0].value != 0 or bcs["left"][1] != "Neumann": + raise pybamm.ModelError( + """Boundary condition at r = 0 must be a homogeneous Neumann condition for {} coordinates""".format( + self.mesh[subdomain].coord_sys + ) + ) + # Handle any boundary conditions applied on the tabs if any("tab" in side for side in list(bcs.keys())): bcs = self.check_tab_conditions(key, bcs) diff --git a/tests/unit/test_discretisations/test_discretisation.py b/tests/unit/test_discretisations/test_discretisation.py index 7ceff8746c..527ecc167c 100644 --- a/tests/unit/test_discretisations/test_discretisation.py +++ b/tests/unit/test_discretisations/test_discretisation.py @@ -1237,6 +1237,58 @@ def test_process_input_variable(self): n = disc.mesh.combine_submeshes(*a.domain).npts self.assertEqual(a_disc._expected_size, n) + def test_bc_symmetry(self): + # define model + model = pybamm.BaseModel() + c = pybamm.Variable("Concentration", domain="negative particle") + N = -pybamm.grad(c) + dcdt = -pybamm.div(N) + model.rhs = {c: dcdt} + + # initial conditions + model.initial_conditions = {c: pybamm.Scalar(1)} + + # define geometry + r = pybamm.SpatialVariable( + "r", domain=["negative particle"], coord_sys="spherical polar" + ) + geometry = { + "negative particle": {r: {"min": pybamm.Scalar(0), "max": pybamm.Scalar(1)}} + } + + # mesh + submesh_types = { + "negative particle": pybamm.MeshGenerator(pybamm.Uniform1DSubMesh) + } + var_pts = {r: 20} + mesh = pybamm.Mesh(geometry, submesh_types, var_pts) + + spatial_methods = {"negative particle": pybamm.FiniteVolume()} + + # boundary conditions (Dirichlet) + lbc = pybamm.Scalar(0) + rbc = pybamm.Scalar(2) + model.boundary_conditions = { + c: {"left": (lbc, "Dirichlet"), "right": (rbc, "Neumann")} + } + + # discretise + disc = pybamm.Discretisation(mesh, spatial_methods) + with self.assertRaisesRegex(pybamm.ModelError, "Boundary condition at r = 0"): + disc.process_model(model) + + # boundary conditions (non-homog Neumann) + lbc = pybamm.Scalar(0) + rbc = pybamm.Scalar(2) + model.boundary_conditions = { + c: {"left": (rbc, "Neumann"), "right": (rbc, "Neumann")} + } + + # discretise + disc = pybamm.Discretisation(mesh, spatial_methods) + with self.assertRaisesRegex(pybamm.ModelError, "Boundary condition at r = 0"): + disc.process_model(model) + if __name__ == "__main__": print("Add -v for more debug output") From bdf3b95505b9158c8c69430e7ae1cf0678c6dadb Mon Sep 17 00:00:00 2001 From: Ferran Brosa Planella Date: Tue, 22 Sep 2020 10:28:32 +0200 Subject: [PATCH 2/6] flake8 --- pybamm/discretisations/discretisation.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pybamm/discretisations/discretisation.py b/pybamm/discretisations/discretisation.py index d27cf7d03f..02758d5747 100644 --- a/pybamm/discretisations/discretisation.py +++ b/pybamm/discretisations/discretisation.py @@ -496,7 +496,8 @@ def process_boundary_conditions(self, model): if self.mesh[subdomain].coord_sys == "spherical polar": if bcs["left"][0].value != 0 or bcs["left"][1] != "Neumann": raise pybamm.ModelError( - """Boundary condition at r = 0 must be a homogeneous Neumann condition for {} coordinates""".format( + """Boundary condition at r = 0 must be a homogeneous + Neumann condition for {} coordinates""".format( self.mesh[subdomain].coord_sys ) ) From 00785da474b5162e92b0c454227666c8979bdba7 Mon Sep 17 00:00:00 2001 From: Ferran Brosa Planella Date: Tue, 22 Sep 2020 12:30:15 +0200 Subject: [PATCH 3/6] #1174 fix tests --- pybamm/discretisations/discretisation.py | 17 +++++++++-------- .../test_finite_volume/test_finite_volume.py | 2 +- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/pybamm/discretisations/discretisation.py b/pybamm/discretisations/discretisation.py index 02758d5747..9064f87f77 100644 --- a/pybamm/discretisations/discretisation.py +++ b/pybamm/discretisations/discretisation.py @@ -492,15 +492,16 @@ def process_boundary_conditions(self, model): for key, bcs in model.boundary_conditions.items(): processed_bcs[key.id] = {} - for subdomain in key.domain: - if self.mesh[subdomain].coord_sys == "spherical polar": - if bcs["left"][0].value != 0 or bcs["left"][1] != "Neumann": - raise pybamm.ModelError( - """Boundary condition at r = 0 must be a homogeneous - Neumann condition for {} coordinates""".format( - self.mesh[subdomain].coord_sys + if key not in model.external_variables: + for subdomain in key.domain: + if self.mesh[subdomain].coord_sys == "spherical polar": + if bcs["left"][0].value != 0 or bcs["left"][1] != "Neumann": + raise pybamm.ModelError( + "Boundary condition at r = 0 must be a homogeneous " + "Neumann condition for {} coordinates".format( + self.mesh[subdomain].coord_sys + ) ) - ) # Handle any boundary conditions applied on the tabs if any("tab" in side for side in list(bcs.keys())): diff --git a/tests/unit/test_spatial_methods/test_finite_volume/test_finite_volume.py b/tests/unit/test_spatial_methods/test_finite_volume/test_finite_volume.py index b6d4f4ed8f..1fe5487b68 100644 --- a/tests/unit/test_spatial_methods/test_finite_volume/test_finite_volume.py +++ b/tests/unit/test_spatial_methods/test_finite_volume/test_finite_volume.py @@ -1348,7 +1348,7 @@ def test_p2d_mass_matrix_shape(self): model.rhs = {c: pybamm.div(N)} model.initial_conditions = {c: pybamm.Scalar(0)} model.boundary_conditions = { - c: {"left": (0, "Dirichlet"), "right": (0, "Dirichlet")} + c: {"left": (0, "Neumann"), "right": (0, "Dirichlet")} } model.variables = {"c": c, "N": N} mesh = get_p2d_mesh_for_testing() From 31e8ca58139b77f232d009146285c9c6253466bd Mon Sep 17 00:00:00 2001 From: Ferran Brosa Planella Date: Tue, 22 Sep 2020 13:05:23 +0200 Subject: [PATCH 4/6] #1174 add comment --- pybamm/discretisations/discretisation.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pybamm/discretisations/discretisation.py b/pybamm/discretisations/discretisation.py index 9064f87f77..38592dccb5 100644 --- a/pybamm/discretisations/discretisation.py +++ b/pybamm/discretisations/discretisation.py @@ -492,6 +492,8 @@ def process_boundary_conditions(self, model): for key, bcs in model.boundary_conditions.items(): processed_bcs[key.id] = {} + # check if the boundary condition at the origin for sphere domains is other + # than no flux if key not in model.external_variables: for subdomain in key.domain: if self.mesh[subdomain].coord_sys == "spherical polar": From 43d5492bb05fcddf10662df57160058a02996308 Mon Sep 17 00:00:00 2001 From: Ferran Brosa Planella Date: Tue, 22 Sep 2020 13:05:44 +0200 Subject: [PATCH 5/6] #1174 add fix bcs sphere --- CHANGELOG.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c588edab5..6b6276e79e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,18 @@ +# [Unreleased](https://github.com/pybamm-team/PyBaMM) + +## Features + + +## Optimizations + + +## Bug fixes + +- Raise error if the boundary condition at the origin in a spherical domain is other than no-flux ([#1175](https://github.com/pybamm-team/PyBaMM/pull/1175)) + +## Breaking changes + + # [v0.2.4](https://github.com/pybamm-team/PyBaMM/tree/v0.2.4) - 2020-09-07 This release adds new operators for more complex models, some basic sensitivity analysis, and a spectral volumes spatial method, as well as some small bug fixes. From e5ce7a1ecd19dbd58c8a8638679ffcc3a183460f Mon Sep 17 00:00:00 2001 From: Ferran Brosa Planella Date: Tue, 22 Sep 2020 16:47:01 +0200 Subject: [PATCH 6/6] #1173 edit CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b6276e79e..f7997a9899 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ## Bug fixes - Raise error if the boundary condition at the origin in a spherical domain is other than no-flux ([#1175](https://github.com/pybamm-team/PyBaMM/pull/1175)) +- Fix boundary conditions at r = 0 for Creating Models notebooks ([#1173](https://github.com/pybamm-team/PyBaMM/pull/1173)) ## Breaking changes