From b1b7ffc01f4bb0e92e9d49f6e55090d7830c41ab Mon Sep 17 00:00:00 2001 From: Robert Timms Date: Thu, 7 Nov 2019 11:40:47 +0000 Subject: [PATCH 1/3] #711 fix bc bug in 1plus1D --- pybamm/discretisations/discretisation.py | 16 +++++---- .../test_finite_volume.py | 35 +++++++++++++++++++ 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/pybamm/discretisations/discretisation.py b/pybamm/discretisations/discretisation.py index 0c1ab0c6ab..51806be9ee 100644 --- a/pybamm/discretisations/discretisation.py +++ b/pybamm/discretisations/discretisation.py @@ -381,19 +381,21 @@ def check_tab_conditions(self, symbol, bcs): symbol, domain ) ) - # Replace keys with "left" and "right" as appropriate for 1D meshes if isinstance(mesh, pybamm.SubMesh1D): # replace negative and/or positive tab for tab in ["negative tab", "positive tab"]: if any(tab in side for side in list(bcs.keys())): bcs[mesh.tabs[tab]] = bcs.pop(tab) - # replace no tab - if any("no tab" in side for side in list(bcs.keys())): - if "left" in list(bcs.keys()): - bcs["right"] = bcs.pop("no tab") # tab at bottom - else: - bcs["left"] = bcs.pop("no tab") # tab at top + # if both left and right are set we are done + if all(side in list(bcs.keys()) for side in ["left", "right"]): + pass + # if only left is set, then set right to no tab + elif "left" in list(bcs.keys()): + bcs["right"] = bcs.pop("no tab") # tab at bottom + # otherwise left is no tab + else: + bcs["left"] = bcs.pop("no tab") # tab at top return bcs diff --git a/tests/unit/test_spatial_methods/test_finite_volume.py b/tests/unit/test_spatial_methods/test_finite_volume.py index cdee8c22e2..99e4cec573 100644 --- a/tests/unit/test_spatial_methods/test_finite_volume.py +++ b/tests/unit/test_spatial_methods/test_finite_volume.py @@ -1459,6 +1459,41 @@ def test_grad_div_with_bcs_on_tab(self): div_eqn_disc = disc.process_symbol(div_eqn) div_eqn_disc.evaluate(None, y_test) + def test_neg_pos_bcs(self): + # 2d macroscale + mesh = get_1p1d_mesh_for_testing() + spatial_methods = { + "macroscale": pybamm.FiniteVolume, + "negative particle": pybamm.FiniteVolume, + "positive particle": pybamm.FiniteVolume, + "current collector": pybamm.FiniteVolume, + } + disc = pybamm.Discretisation(mesh, spatial_methods) + + # var + var = pybamm.Variable("var", domain="current collector") + disc.set_variable_slices([var]) + # grad + grad_eqn = pybamm.grad(var) + + # bcs (on each tab) + boundary_conditions = { + var.id: { + "negative tab": (pybamm.Scalar(1), "Dirichlet"), + "positive tab": (pybamm.Scalar(0), "Neumann"), + "no tab": (pybamm.Scalar(8), "Dirichlet"), + } + } + disc.bcs = boundary_conditions + + # check after disc that negative tab goes to left and positive tab goes + # to right + disc.process_symbol(grad_eqn) + self.assertEqual(disc.bcs[var.id]["left"][0].id, pybamm.Scalar(1).id) + self.assertEqual(disc.bcs[var.id]["left"][1], "Dirichlet") + self.assertEqual(disc.bcs[var.id]["right"][0].id, pybamm.Scalar(0).id) + self.assertEqual(disc.bcs[var.id]["right"][1], "Neumann") + if __name__ == "__main__": print("Add -v for more debug output") From 434f0a14c7017eb98a14ed5f12e40c0370658e33 Mon Sep 17 00:00:00 2001 From: Robert Timms Date: Thu, 7 Nov 2019 11:42:38 +0000 Subject: [PATCH 2/3] #711 changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ffd77754d4..416e41446d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ ## Bug fixes +- Fix a bug where boundary conditions were sometimes handled incorrectly in 1+1D models ([#713](https://github.com/pybamm-team/PyBaMM/pull/713)) - Correct a sign error in Dirichlet boundary conditions in the Finite Element Method ([#706](https://github.com/pybamm-team/PyBaMM/pull/706)) - Pass the correct dimensional temperature to open circuit potential ([#702](https://github.com/pybamm-team/PyBaMM/pull/702)) - Adds missing temperature dependence in electrolyte and interface submodels ([#698](https://github.com/pybamm-team/PyBaMM/pull/698)) From 9e3cf5e30caf8c91af747885ea3af52f4bce3696 Mon Sep 17 00:00:00 2001 From: Robert Timms Date: Fri, 8 Nov 2019 16:40:13 +0000 Subject: [PATCH 3/3] #711 improve comments --- pybamm/discretisations/discretisation.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/pybamm/discretisations/discretisation.py b/pybamm/discretisations/discretisation.py index 51806be9ee..1e1653a8a0 100644 --- a/pybamm/discretisations/discretisation.py +++ b/pybamm/discretisations/discretisation.py @@ -383,19 +383,24 @@ def check_tab_conditions(self, symbol, bcs): ) # Replace keys with "left" and "right" as appropriate for 1D meshes if isinstance(mesh, pybamm.SubMesh1D): - # replace negative and/or positive tab + # send boundary conditions applied on the tabs to "left" or "right" + # depending on the tab location stored in the mesh for tab in ["negative tab", "positive tab"]: if any(tab in side for side in list(bcs.keys())): bcs[mesh.tabs[tab]] = bcs.pop(tab) - # if both left and right are set we are done + # if there was a tab at either end, then the boundary conditions + # have now been set on "left" and "right" as required by the spatial + # method, so there is no need to further modify the bcs dict if all(side in list(bcs.keys()) for side in ["left", "right"]): pass - # if only left is set, then set right to no tab + # if both tabs are located at z=0 then the "right" boundary condition + # (at z=1) is the condition for "no tab" elif "left" in list(bcs.keys()): - bcs["right"] = bcs.pop("no tab") # tab at bottom - # otherwise left is no tab + bcs["right"] = bcs.pop("no tab") + # else if both tabs are located at z=1, the "left" boundary condition + # (at z=0) is the condition for "no tab" else: - bcs["left"] = bcs.pop("no tab") # tab at top + bcs["left"] = bcs.pop("no tab") return bcs