Skip to content

Commit c435a1e

Browse files
committed
#704 converted to using options in finite volume
1 parent cb1e5d0 commit c435a1e

File tree

2 files changed

+44
-48
lines changed

2 files changed

+44
-48
lines changed

pybamm/discretisations/discretisation.py

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
def has_bc_condition_of_form(symbol, side, bcs, form):
1111

1212
if symbol.id in bcs:
13-
return True
1413
if bcs[symbol.id][side][1] == form:
1514
return True
1615
else:

pybamm/spatial_methods/finite_volume.py

+44-47
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def __init__(self, mesh):
3636
super().__init__(mesh)
3737

3838
# there is no way to set this at the moment
39-
self.extrapolation = "quadratic"
39+
self.options = {"extrapolation": {"order": "quadratic", "use bcs": True}}
4040

4141
# add npts_for_broadcast to mesh domains for this particular discretisation
4242
for dom in mesh.keys():
@@ -611,13 +611,23 @@ def boundary_value_or_flux(self, symbol, discretised_child, bcs=None):
611611

612612
child = symbol.child
613613

614-
if symbol.side == "left":
614+
extrap_order = self.options["extrapolation"]["order"]
615+
use_bcs = self.options["extrapolation"]["use bcs"]
615616

616-
if self.extrapolation == "linear":
617+
if use_bcs and pybamm.has_bc_condition_of_form(
618+
child, symbol.side, bcs, "Dirichlet"
619+
):
620+
# just use the value from the bc: f(x*)
621+
sub_matrix = csr_matrix((1, prim_pts))
622+
additive = bcs[child.id][symbol.side][0]
623+
624+
elif symbol.side == "left":
625+
626+
if extrap_order == "linear":
617627
# to find value at x* use formula:
618628
# f(x*) = f_1 - (dx0 / dx1) (f_2 - f_1)
619629

620-
if pybamm.has_bc_condition_of_form(
630+
if use_bcs and pybamm.has_bc_condition_of_form(
621631
child, symbol.side, bcs, "Neumann"
622632
):
623633
sub_matrix = csr_matrix(([1], ([0], [0])), shape=(1, prim_pts),)
@@ -631,9 +641,9 @@ def boundary_value_or_flux(self, symbol, discretised_child, bcs=None):
631641
)
632642
additive = pybamm.Scalar(0)
633643

634-
elif self.extrapolation == "quadratic":
644+
elif extrap_order == "quadratic":
635645

636-
if pybamm.has_bc_condition_of_form(
646+
if use_bcs and pybamm.has_bc_condition_of_form(
637647
child, symbol.side, bcs, "Neumann"
638648
):
639649
a = (dx0 + dx1) ** 2 / (dx1 * (2 * dx0 + dx1))
@@ -660,9 +670,9 @@ def boundary_value_or_flux(self, symbol, discretised_child, bcs=None):
660670

661671
elif symbol.side == "right":
662672

663-
if self.extrapolation == "linear":
673+
if extrap_order == "linear":
664674

665-
if pybamm.has_bc_condition_of_form(
675+
if use_bcs and pybamm.has_bc_condition_of_form(
666676
child, symbol.side, bcs, "Neumann"
667677
):
668678
# use formula:
@@ -672,13 +682,6 @@ def boundary_value_or_flux(self, symbol, discretised_child, bcs=None):
672682
)
673683
additive = dxN * bcs[child.id][symbol.side][0]
674684

675-
elif pybamm.has_bc_condition_of_form(
676-
child, symbol.side, bcs, "Dirichlet"
677-
):
678-
# just use the value from the bc: f(x*)
679-
sub_matrix = csr_matrix((1, prim_pts))
680-
additive = bcs[child.id][symbol.side][0]
681-
682685
else:
683686
# to find value at x* use formula:
684687
# f(x*) = f_N - (dxN / dxNm1) (f_N - f_Nm1)
@@ -690,9 +693,9 @@ def boundary_value_or_flux(self, symbol, discretised_child, bcs=None):
690693
shape=(1, prim_pts),
691694
)
692695
additive = pybamm.Scalar(0)
693-
elif self.extrapolation == "quadratic":
696+
elif extrap_order == "quadratic":
694697

695-
if pybamm.has_bc_condition_of_form(
698+
if use_bcs and pybamm.has_bc_condition_of_form(
696699
child, symbol.side, bcs, "Neumann"
697700
):
698701
a = (dxN + dxNm1) ** 2 / (dxNm1 * (2 * dxN + dxNm1))
@@ -726,24 +729,24 @@ def boundary_value_or_flux(self, symbol, discretised_child, bcs=None):
726729
raise NotImplementedError
727730

728731
elif isinstance(symbol, pybamm.BoundaryGradient):
729-
if symbol.side == "left":
730732

731-
if self.extrapolation == "linear":
733+
if use_bcs and pybamm.has_bc_condition_of_form(
734+
child, symbol.side, bcs, "Neumann"
735+
):
736+
# just use the value from the bc: f'(x*)
737+
sub_matrix = csr_matrix((1, prim_pts))
738+
additive = bcs[child.id][symbol.side][0]
732739

733-
if pybamm.has_bc_condition_of_form(
734-
child, symbol.side, bcs, "Neumann"
735-
):
736-
# just use the value from the bc: f'(x*)
737-
sub_matrix = csr_matrix((1, prim_pts))
738-
additive = bcs[child.id][symbol.side][0]
739-
else:
740-
# use formula:
741-
# f'(x*) = (f_2 - f_1) / dx1
742-
sub_matrix = (1 / dx1) * csr_matrix(
743-
([-1, 1], ([0, 0], [0, 1])), shape=(1, prim_pts)
744-
)
745-
additive = pybamm.Scalar(0)
746-
elif self.extrapolation == "quadratic":
740+
elif symbol.side == "left":
741+
742+
if extrap_order == "linear":
743+
# f'(x*) = (f_2 - f_1) / dx1
744+
sub_matrix = (1 / dx1) * csr_matrix(
745+
([-1, 1], ([0, 0], [0, 1])), shape=(1, prim_pts)
746+
)
747+
additive = pybamm.Scalar(0)
748+
749+
elif extrap_order == "quadratic":
747750

748751
a = -(2 * dx0 + 2 * dx1 + dx2) / (dx1 ** 2 + dx1 * dx2)
749752
b = (2 * dx0 + dx1 + dx2) / (dx1 * dx2)
@@ -759,20 +762,13 @@ def boundary_value_or_flux(self, symbol, discretised_child, bcs=None):
759762
elif symbol.side == "right":
760763

761764
if self.extrapolation == "linear":
762-
if pybamm.has_bc_condition_of_form(
763-
child, symbol.side, bcs, "Neumann"
764-
):
765-
# just use the value from the bc: f'(x*)
766-
sub_matrix = csr_matrix((1, prim_pts))
767-
additive = bcs[child.id][symbol.side][0]
768-
else:
769-
# use formula:
770-
# f'(x*) = (f_N - f_Nm1) / dxNm1
771-
sub_matrix = (1 / dxNm1) * csr_matrix(
772-
([-1, 1], ([0, 0], [prim_pts - 2, prim_pts - 1])),
773-
shape=(1, prim_pts),
774-
)
775-
additive = pybamm.Scalar(0)
765+
# use formula:
766+
# f'(x*) = (f_N - f_Nm1) / dxNm1
767+
sub_matrix = (1 / dxNm1) * csr_matrix(
768+
([-1, 1], ([0, 0], [prim_pts - 2, prim_pts - 1])),
769+
shape=(1, prim_pts),
770+
)
771+
additive = pybamm.Scalar(0)
776772

777773
elif self.extrapolation == "quadratic":
778774
a = (2 * dxN + 2 * dxNm1 + dxNm2) / (dxNm1 ** 2 + dxNm1 * dxNm2)
@@ -787,6 +783,7 @@ def boundary_value_or_flux(self, symbol, discretised_child, bcs=None):
787783
shape=(1, prim_pts),
788784
)
789785
additive = pybamm.Scalar(0)
786+
790787
else:
791788
raise NotImplementedError
792789

0 commit comments

Comments
 (0)