Skip to content

Commit b6f3546

Browse files
committed
#704 removed the option in other functions
1 parent 22dc55c commit b6f3546

File tree

5 files changed

+54
-38
lines changed

5 files changed

+54
-38
lines changed

pybamm/discretisations/discretisation.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -707,9 +707,7 @@ def _process_symbol(self, symbol):
707707
mesh = self.mesh[symbol.children[0].domain[0]][0]
708708
if isinstance(mesh, pybamm.SubMesh1D):
709709
symbol.side = mesh.tabs[symbol.side]
710-
return child_spatial_method.boundary_value_or_flux(
711-
symbol, disc_child, symbol.extrapolation
712-
)
710+
return child_spatial_method.boundary_value_or_flux(symbol, disc_child)
713711

714712
else:
715713
return symbol._unary_new_copy(disc_child)

pybamm/expression_tree/unary_operators.py

+7-17
Original file line numberDiff line numberDiff line change
@@ -738,15 +738,12 @@ class BoundaryValue(BoundaryOperator):
738738
The variable whose boundary value to take
739739
side : str
740740
Which side to take the boundary value on ("left" or "right")
741-
extrapolation : str
742-
What extrapolation method to use for this variable ("linear" or "quadratic")
743741
744742
**Extends:** :class:`BoundaryOperator`
745743
"""
746744

747-
def __init__(self, child, side, extrapolation="quadratic"):
745+
def __init__(self, child, side):
748746
super().__init__("boundary value", child, side)
749-
self.extrapolation = extrapolation
750747

751748

752749
class BoundaryGradient(BoundaryOperator):
@@ -758,15 +755,12 @@ class BoundaryGradient(BoundaryOperator):
758755
The variable whose boundary flux to take
759756
side : str
760757
Which side to take the boundary flux on ("left" or "right")
761-
extrapolation : str
762-
What extrapolation method to use on this variable ("linear" or "quadratic")
763758
764759
**Extends:** :class:`BoundaryOperator`
765760
"""
766761

767-
def __init__(self, child, side, extrapolation="linear"):
762+
def __init__(self, child, side):
768763
super().__init__("boundary flux", child, side)
769-
self.extrapolation = extrapolation
770764

771765

772766
#
@@ -856,7 +850,7 @@ def grad_squared(expression):
856850
#
857851

858852

859-
def surf(symbol, set_domain=False, extrapolation="quadratic"):
853+
def surf(symbol, set_domain=False):
860854
"""convenience function for creating a right :class:`BoundaryValue`, usually in the
861855
spherical geometry
862856
@@ -865,8 +859,6 @@ def surf(symbol, set_domain=False, extrapolation="quadratic"):
865859
866860
symbol : :class:`pybamm.Symbol`
867861
the surface value of this symbol will be returned
868-
extrapolation : str
869-
The type of extrapolation method to use ("linear" or "quadratic")
870862
871863
Returns
872864
-------
@@ -876,10 +868,10 @@ def surf(symbol, set_domain=False, extrapolation="quadratic"):
876868
if symbol.domain in [["negative electrode"], ["positive electrode"]] and isinstance(
877869
symbol, pybamm.PrimaryBroadcast
878870
):
879-
child_surf = boundary_value(symbol.orphans[0], "right", extrapolation)
871+
child_surf = boundary_value(symbol.orphans[0], "right")
880872
out = pybamm.PrimaryBroadcast(child_surf, symbol.domain)
881873
else:
882-
out = boundary_value(symbol, "right", extrapolation)
874+
out = boundary_value(symbol, "right")
883875
if set_domain:
884876
if symbol.domain == ["negative particle"]:
885877
out.domain = ["negative electrode"]
@@ -1023,7 +1015,7 @@ def yz_average(symbol):
10231015
return Integral(symbol, [y, z]) / (l_y * l_z)
10241016

10251017

1026-
def boundary_value(symbol, side, extrapolation="quadratic"):
1018+
def boundary_value(symbol, side):
10271019
"""convenience function for creating a :class:`pybamm.BoundaryValue`
10281020
10291021
Parameters
@@ -1032,8 +1024,6 @@ def boundary_value(symbol, side, extrapolation="quadratic"):
10321024
The symbol whose boundary value to take
10331025
side : str
10341026
Which side to take the boundary value on ("left" or "right")
1035-
extrapolation : str
1036-
The type of extrapolation method to use ("linear" or "quadratic")
10371027
10381028
Returns
10391029
-------
@@ -1050,7 +1040,7 @@ def boundary_value(symbol, side, extrapolation="quadratic"):
10501040
return symbol.orphans[0]
10511041
# Otherwise, calculate boundary value
10521042
else:
1053-
return BoundaryValue(symbol, side, extrapolation)
1043+
return BoundaryValue(symbol, side)
10541044

10551045

10561046
def r_average(symbol):

pybamm/spatial_methods/finite_volume.py

+44-16
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ class FiniteVolume(pybamm.SpatialMethod):
3434

3535
def __init__(self, mesh):
3636
super().__init__(mesh)
37+
38+
# there is no way to set this at the moment
39+
self.extrapolation = "linear"
40+
3741
# add npts_for_broadcast to mesh domains for this particular discretisation
3842
for dom in mesh.keys():
3943
for i in range(len(mesh[dom])):
@@ -572,7 +576,7 @@ def add_ghost_nodes(self, symbol, discretised_symbol, bcs):
572576

573577
return pybamm.Matrix(matrix) @ discretised_symbol + bcs_vector
574578

575-
def boundary_value_or_flux(self, symbol, discretised_child, extrapolation="linear"):
579+
def boundary_value_or_flux(self, symbol, discretised_child):
576580
"""
577581
Uses linear extrapolation to get the boundary value or flux of a variable in the
578582
Finite Volume Method.
@@ -592,13 +596,17 @@ def boundary_value_or_flux(self, symbol, discretised_child, extrapolation="linea
592596
nodes = submesh_list[0].nodes
593597
edges = submesh_list[0].edges
594598

595-
# assumes uniform!
599+
dx0 = nodes[0] - edges[0]
600+
dx1 = submesh_list[0].d_nodes[0]
601+
dx2 = submesh_list[0].d_nodes[1]
602+
603+
dxN = edges[-1] - nodes[-1]
604+
dxNm1 = submesh_list[0].d_nodes[-1]
605+
dxNm2 = submesh_list[0].d_nodes[-2]
606+
596607
if symbol.side == "left":
597-
dx0 = nodes[0] - edges[0]
598-
dx1 = submesh_list[0].d_nodes[0]
599-
dx2 = submesh_list[0].d_nodes[1]
600608

601-
if extrapolation == "linear":
609+
if self.extrapolation == "linear":
602610
# to find value at x* use formula:
603611
# f(x*) = f_1 - (dx0 / dx1) (f_2 - f_1)
604612
# where
@@ -609,7 +617,7 @@ def boundary_value_or_flux(self, symbol, discretised_child, extrapolation="linea
609617
([1 + (dx0 / dx1), -(dx0 / dx1)], ([0, 0], [0, 1])),
610618
shape=(1, prim_pts),
611619
)
612-
elif extrapolation == "quadratic":
620+
elif self.extrapolation == "quadratic":
613621
# to find value at x* use formula:
614622
# see mathematica notebook at:
615623
# https://github.com/Scottmar93/extrapolation-coefficents/tree/master
@@ -626,11 +634,8 @@ def boundary_value_or_flux(self, symbol, discretised_child, extrapolation="linea
626634
raise NotImplementedError
627635

628636
elif symbol.side == "right":
629-
dxN = edges[-1] - nodes[-1]
630-
dxNm1 = submesh_list[0].d_nodes[-1]
631-
dxNm2 = submesh_list[0].d_nodes[-2]
632637

633-
if extrapolation == "linear":
638+
if self.extrapolation == "linear":
634639
# to find value at x* use formula:
635640
# f(x*) = f_N - (dxN / dxNm1) (f_N - f_Nm1)
636641
# where
@@ -644,7 +649,7 @@ def boundary_value_or_flux(self, symbol, discretised_child, extrapolation="linea
644649
),
645650
shape=(1, prim_pts),
646651
)
647-
elif extrapolation == "quadratic":
652+
elif self.extrapolation == "quadratic":
648653
# to find value at x* use formula:
649654
# see mathematica notebook at:
650655
# https://github.com/Scottmar93/extrapolation-coefficents/tree/master
@@ -670,23 +675,46 @@ def boundary_value_or_flux(self, symbol, discretised_child, extrapolation="linea
670675

671676
elif isinstance(symbol, pybamm.BoundaryGradient):
672677
if symbol.side == "left":
673-
dx = submesh_list[0].d_nodes[0]
678+
# use formula:
679+
# f'(x*) = (f_2 - f_1) / dx1
680+
# where dx1 = x_2 - x_1
674681

675-
if extrapolation == "linear":
676-
sub_matrix = (1 / dx) * csr_matrix(
682+
if self.extrapolation == "linear":
683+
sub_matrix = (1 / dx1) * csr_matrix(
677684
([-1, 1], ([0, 0], [0, 1])), shape=(1, prim_pts)
678685
)
686+
elif self.extrapolation == "quadratic":
687+
688+
a = -(2 * dx0 + 2 * dx1 + dx2) / (dx1 ** 2 + dx1 * dx2)
689+
b = (2 * dx0 + dx1 + dx2) / (dx1 * dx2)
690+
c = -(2 * dx0 + dx1) / (dx1 * dx2 + dx2 ** 2)
691+
692+
sub_matrix = csr_matrix(
693+
([a, b, c], ([0, 0, 0], [0, 1, 2])), shape=(1, prim_pts)
694+
)
679695
else:
680696
raise NotImplementedError
681697

682698
elif symbol.side == "right":
683699
dx = submesh_list[0].d_nodes[-1]
684700

685-
if extrapolation == "linear":
701+
if self.extrapolation == "linear":
686702
sub_matrix = (1 / dx) * csr_matrix(
687703
([-1, 1], ([0, 0], [prim_pts - 2, prim_pts - 1])),
688704
shape=(1, prim_pts),
689705
)
706+
elif self.extrapolation == "quadratic":
707+
a = (2 * dxN + 2 * dxNm1 + dxNm2) / (dxNm1 ** 2 + dxNm1 * dxNm2)
708+
b = -(2 * dxN + dxNm1 + dxNm2) / (dxNm1 * dxNm2)
709+
c = (2 * dxN + dxNm1) / (dxNm1 * dxNm2 + dxNm2 ** 2)
710+
711+
sub_matrix = csr_matrix(
712+
(
713+
[c, b, a],
714+
([0, 0, 0], [prim_pts - 3, prim_pts - 2, prim_pts - 1]),
715+
),
716+
shape=(1, prim_pts),
717+
)
690718
else:
691719
raise NotImplementedError
692720

pybamm/spatial_methods/scikit_finite_element.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ def integral_form(v, dv, w):
322322

323323
return pybamm.Matrix(integration_vector[np.newaxis, :])
324324

325-
def boundary_value_or_flux(self, symbol, discretised_child, extrapolation="linear"):
325+
def boundary_value_or_flux(self, symbol, discretised_child):
326326
"""
327327
Returns the average value of the symbol over the negative tab ("negative tab")
328328
or the positive tab ("positive tab") in the Finite Element Method.

pybamm/spatial_methods/spatial_method.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ def internal_neumann_condition(
278278

279279
raise NotImplementedError
280280

281-
def boundary_value_or_flux(self, symbol, discretised_child, extrapolation="linear"):
281+
def boundary_value_or_flux(self, symbol, discretised_child):
282282
"""
283283
Returns the boundary value or flux using the approriate expression for the
284284
spatial method. To do this, we create a sparse vector 'bv_vector' that extracts

0 commit comments

Comments
 (0)