You are on page 1of 6

Differential operators and boundary conditions in ‘py-pde‘

David Zwicker
(Dated: August 18, 2022)
We here document the differential operators and the associated boundary conditions that are used
in the finite difference approximation. We also derive special versions of the Laplace operator that
conserves the total mass.

CONTENTS see problems that can arise, consider a vector field vα


with two components, so that the field has a normal and
I. General considerations 1 a tangential component at each boundary point. How
A. Scalar fields 1 many components need to be specified of the ghost cell?
B. Vector fields 1 The answer depends on the operator that will be applied
to the field: Only the normal components need to be
II. Boundary conditions for differential operators 2 specified to calculate the divergence of the vector field,
A. Laplace operator 2 while all components are required to calculate the gradi-
B. Gradient operator 2 ent tensor or to interpolate the vector field. Generally,
C. Divergence operator 2 normal boundary conditions are sufficient when the ap-
D. Vector Laplacian 2 plied operator reduces the rank of the field, e.g., turns a
E. Vector gradient operator 2 vector field into a scalar field.
F. Tensor divergence operator 2
G. Operators not implemented 2
A. Scalar fields
III. Differential operators on Cartesian grids 2
A. Lower boundary 3 The simplest fields in ‘py-pde‘ are scalar fields, which
B. Upper boundary 3 associate a single value to each grid point. In the contin-
uum, a scalar field c(r) often has the following boundary
IV. Differential operators on cylindrical grids 3 conditions:
A. Boundary condition at the origin 4
B. Boundary condition at the outer side 4 value: c=A (1a)
normal derivative: nα ∂α c = B (1b)
V. Differential operators on spherical grid 4
A. Boundary condition at the origin 4 where nα is the outward normal of the boundary. These
B. Boundary condition at the outer side 4 two conditions are also often known as Dirichlet and Neu-
C. Conservative Laplace operator 4 mann boundary conditions, respectively. These options
D. Conservative divergence operator 5 are implemented as value and derivative in ‘py-pde‘.
E. Conservative tensor divergence operator 5
F. Conservative double divergence operator 6
B. Vector fields

I. GENERAL CONSIDERATIONS For a vector field vα (r), which associate a vector with
each grid point, possible boundary conditions are
The main objects in ‘py-pde‘ are fields that are ex-
pressed on discretized grids. The package provides many value: vα = Aα (2a)
functions to manipulate these fields. Some functions normal component: n α vα =A (2b)
(e.g., addition, multiplication, integrals) do only use the normal derivative: nβ ∂β vα = Bα (2c)
actual field values, while others (e.g., differential opera- normal comp. of normal deriv.: nα nβ ∂β vα =B (2d)
tors and interpolation) might require information on the
behavior of the field at the boundary. This documents There are now many more options, where the first two
specifies details for how boundary conditions are handled. specify values (Dirichlet conditions) and the last two
Generally, boundary conditions are imposed using so- specify derivatives (Neumann conditions). Additionally,
called ghost cells (or virtual points), which specify the the conditions differ in that they either specify the full
value of the fields one support point beyond the bound- vector (conditions 1 and 3) or just the normal component
ary. The idea is that the values of the ghost cells are (conditions 2 and 4). The latter choice obviously contains
set such that the boundary condition between the ghost less information, but is sufficient for differential operators
cell and the first real cell is imposed exactly. While this that reduce the field rank (e.g., divergence). Conversely,
is quite straight-forward for scalar fields, fields of higher specifying the full vector using the first or third condi-
rank (i.e., vector and tensor fields) require more care. To tion is necessary for interpolating vector fields correctly.
2

Note also that the Dirichlet condition imposes the dot Note that the Dirichlet condition in this case imposes
product between the normal vector and the vector field, the dot product between the normal vector and the vec-
which in a simple 1d system implies v1 (x = xL ) = −A tor field, which in a simple 1d system implies v1 (x =
and v1 (x = xR ) = A for the lower and upper boundary, xL ) = −A and v1 (x = xR ) = A for the lower and up-
respectively. Similarly, the Neumann condition evaluates per boundary, respectively. Similarly, the Neumann con-
the directional derivative of the vector field in the normal dition evaluates the directional derivative of the vector
direction nβ of the boundary. field in the normal direction nβ of the boundary. This
implies that the tangential components of the vector field
do not affect the boundary condition.
II. BOUNDARY CONDITIONS FOR
DIFFERENTIAL OPERATORS
D. Vector Laplacian
We start with general considerations that are indepen-
dent of the grid type. Here, we consider differential op- The Vector Laplacian is a simple generalization of the
erators that act on fields defined in a domain Ω. In par- ordinary Laplace operator to a vector field vα (r). Possi-
ticular, we discuss the boundary conditions that can be ble boundary conditions are
enforced with each type of operator. We here use a com-
value: v α = Aα (6a)
ponent notation with implicit Einstein summation to de-
note contractions explicitly. For instance, ∂α vα denotes normal derivative: nβ ∂β vα = Bα (6b)
the divergence of a vector field vα (r), and tαα (r) is the
which are applied at positions r ∈ ∂Ω.
trace of a tensor field tαβ (r).

E. Vector gradient operator


A. Laplace operator
The vector gradient operator ∂β vα acts on a vector
The ordinary Laplace operator ∂α2 c is defined to act on field vα (r) and yields a tensorial field. This operator can
scalar fields c(r). Possible boundary conditions are be though of as a gradient applied to each component of
vα and the boundary conditions thus are also applied to
value: c=A (3a) each component,
normal derivative: nα ∂α c = B (3b)
value: v α = Aα (7a)
applied at positions r ∈ ∂Ω. Consequently, either the derivative: nβ ∂β vα = Bα (7b)
value A of the field (Dirichlet condition c(r) = A at
r ∈ ∂Ω) or the normal derivative (Neumann condition
nα ∂α c = B) is specified, where nα is the outwards ori- F. Tensor divergence operator
ented normal vector at the boundary.
The vector gradient operator ∂β tαβ acts on a tensor
field tαβ (r) and yields a vector field. The boundary con-
B. Gradient operator ditions thus are applied to each component,
value: tαβ nβ = Aα (8a)
The gradient operator ∂α c acts on a scalar field c(r)
yielding a vector field. Possible boundary conditions are derivative: ∂β tαβ = Bα (8b)

value: c=A (4a) G. Operators not implemented


derivative: nα ∂α c = B (4b)
The following operators are not yet implemented, but
where nα is again the outward normal of the boundary. might be useful in the future:
Curl: ϵαβγ ∂β vγ (9a)
C. Divergence operator Material derivative (Advection): wα ∂α vβ (9b)

The divergence operator ∂α vα acts on a vector field III. DIFFERENTIAL OPERATORS ON


vα (r) with possible boundary conditions CARTESIAN GRIDS

normal component: n α vα = A (5a)


We consider a linear domain of length L discretized by
derivative: nα nβ ∂β vα = B (5b) N support points. We place these points equidistantly
3

at xn = (n + 21 )∆x for n = 0, 1, . . . , N − 1 using the


TABLE I. Coefficients for lower cartesian boundary
discretization ∆x = L/N . Any function y = f (x) is then
represented by discretized values yn = f (xn ). Boundary condition Offset αL Pre-factor βL Index kL
In the finite difference schemes we discuss, we require Periodic 0 1 N −1
differential operators of first and second order. Targeting
Dirichlet 2a −1 0
second order accuracy, we obtain these as
Neumann b∆x 1 0
yn+1 − yn−1 Robin 2b∆x 2−c∆x
0
f ′ (xn ) ∼ (10a) 2+c∆x 2+c∆x
2∆x
′′ yn+1 − 2yn + yn−1
f (xn ) ∼ (10b)
∆x2 TABLE II. Coefficients for upper cartesian boundary
where ∼ denotes the finite difference approximation. Boundary condition Offset αR Pre-factor βR Index kR
These derivatives can only be evaluated directly for Periodic 0 1 0
n = 1, 2, . . . , n − 2, while the boundary points n = 0 and
Dirichlet 2a −1 N −1
n = N −1 require knowledge of the value at the respective
Neumann b∆x 1 N −1
virtual points x−1 and xN outside the domain. We can 2b∆x 2−c∆x
derive expressions for the associated function values y−1 Robin 2+c∆x 2+c∆x
N −1
and yN taking the boundary conditions in to account,
which typically specify the value at the boundary or the
(outward) derivative. A particular simple case are pe- B. Upper boundary
riodic boundary conditions, where we have y−1 = yN −1
and yN = y0 , allowing the above formula to be used ev- The upper boundary at x = L can be treated analo-
erywhere. For more complicated boundary conditions, gously to the lower one by introducing a virtual point at
we need to treat both sides separately. x = xN . Consequently, we find the following conditions
for the virtual support point yN :
a. Dirichlet (Constant value): Assuming the
A. Lower boundary boundary condition f (L) = a, we obtain yN = 2a−yN −1 .
b. Neumann (Constant derivative): Here, we con-
Let us first consider conditions at the lower boundary, sider the boundary condition f ′ (L) = b, which specifies
which we place at x = 0. We thus need to evaluate the the outward derivative to equal the value b. We obtain
virtual support point y−1 from the boundary condition. yN = yN −1 + b∆x.
Using estimates for the value and the derivative at the Taken together, we express the value at the virtual
boundary, support point as
y0 + y−1 y0 − y−1 yN = αR + βR ykR , (14)
f (0) ∼ f ′ (0) ∼ (11)
2 ∆x
where the coefficients αR , βR , and kR are taken from
we can now handle several boundary conditions. Tab. II.
a. Dirichlet (Constant value): Assuming the
boundary condition f (0) = a, we obtain y−1 = 2a − y0 .
b. Neumann (Constant derivative): Here, we con- IV. DIFFERENTIAL OPERATORS ON
sider the boundary condition −f ′ (0) = b, which specifies CYLINDRICAL GRIDS
the outward derivative to equal the value b. We obtain
y−1 = y0 + b∆x. We here consider fields in 3 dimensions that possess
c. Robin (Mixed): Assuming the boundary condi- angular symmetry and thus only depend on the radial
tion −f ′ (0) + cf (0) = b, we obtain y−1 = A − By0 where coordinate r and the axial coordinate z. Clearly, the
axial coordinate behaves exactly as the Cartesian ones
2∆x c∆x − 2 described above, so we here only have to deal with the
A= b B= (12)
c∆x + 2 c∆x + 2 polar symmetry. Similar to Cartesian grids, we discretize
As a sanity check, we obtain Dirichlet conditions in the the associated radial coordinate as rn = r0 + (n + 21 )∆r,
limit b → ∞ with b/c = a, where A = 2a, B = 1. Like- where r0 is the inner radius, which often will be zero.
wise, we recover Neumann condition for c = 0. The radial part of the gradient operator in cylin-
d. Summary: Taken together, we can express the drical coordinates is simply given by ∂r f (r) and thus
value of the support point as obeys the same discretization as in a Cartesian grid.
The divergence of a vector field vα (r) = ρ(r)er reads
y−1 = αL + βL ykL , (13) ∂α vα (r) = ρ′ (r) + ρ(r)
r , implying the discretized version

where the coefficients αL , βL , and kL are taken from yn+1 − yn−1 yn


∂α vα ∼ + (15)
Tab. I. 2∆r rn
4

Moreover, the Laplace operator in spherical coordinates A. Boundary condition at the origin

reads ∇2 f (r) = f ′′ (r) + f r(r) , which in the discretized
version becomes Due to the symmetry, only vanishing derivatives are
allowed at the inner boundary at the origin, implying the
yn+1 − 2yn + yn−1 yn+1 − yn−1
∂α2 f ∼ + (16) virtual support point y−1 = y0 . Hence, the differential
∆r2 2rn ∆r operators become
y1 − y0 4y0
A. Boundary condition at the origin ∂α vα (r)|r=0 ∼ + (20a)
2∆r ∆r
2
y 1 − y0
∂α f (r) r=0 ∼ 3 (20b)
Due to the symmetry, only vanishing derivatives are ∆r2
allowed at the inner boundary at the origin, implying the
virtual support point y−1 = y0 . Hence, the differential Note that the latter expression applies to both the non-
operators become conservative and the conservative form of the Laplacian.

y1 − y0 2y0
∂α vα (r)|r=0 ∼ + (17a) B. Boundary condition at the outer side
2∆r ∆r
y1 − y 0
∂α2 f (r) r=0 ∼ 2

(17b)
∆r2 At the outer boundary, we can impose boundary con-
ditions similar to the Cartesian grid.
a. Dirichlet (Constant value): Assuming the
B. Boundary condition at the outer side boundary condition f (R) = a, we obtain yN = 2a−yN −1 .
b. Neumann (Constant derivative): Here, we con-
At the outer boundary, we can impose boundary con- sider the boundary condition f ′ (L) = b, which specifies
ditions similar to the Cartesian grid. the outward derivative to equal the value b. We obtain
a. Dirichlet (Constant value): Assuming the yn = yn−1 + b∆x.
boundary condition f (R) = a, we obtain yN = 2a−yN −1 .
b. Neumann (Constant derivative): Here, we con-
sider the boundary condition f ′ (L) = b, which specifies C. Conservative Laplace operator
the outward derivative to equal the value b. We obtain
yn = yn−1 + b∆x. We call a discrete Laplace operator conservative when
it preserves the desirable conservation equation
Z I
V. DIFFERENTIAL OPERATORS ON ∂α2 f dV = ∂α f nα dS . (21)
SPHERICAL GRID Ω ∂Ω

A conservative discrete operator can be derived by inte-


We here consider fields that a spherically symmetric
grating the definition of the Laplace operator in spherical
in 3 dimensions and thus only depend on the radial co-
coordinates over one spherical shell from r = rn − ∆r/2
ordinate r. Similar to Cartesian grids, we discretize the
to r = rn + ∆r/2,
radial coordinate as rn = (n + 12 )∆r.
The radial part fo the gradient operator in spherical Z r 1
n+
2
Z r 1
n+
2
r2 ∂α2 f dr = ∂r r2 ∂r f (r) dr ,

coordinates is simply given by ∂r f (r) and thus obeys the (22)
same discretization as in a Cartesian grid. The diver- rn− 1 rn− 1
2 2
gence of a vector field vα (r) = ρ(r)er reads ∂α vα (r) =
where we skipped the factor 4π stemming from the angle
ρ′ (r) + 2ρ(r)
r , implying the discretized version integration. Assuming that the quantity ∂α2 f is constant
yn+1 − yn−1 2yn across the discretization cell, we thus find
∂α vα ∼ + (18)
2∆r rn h irn +∆r/2
Vn ∂α2 f ∼ r2 ∂r f (r) (23)
Moreover, the naive implementation of Laplace operator rn −∆r/2

in spherical coordinates reads ∇2 f (r) = f ′′ (r) + 2f r(r) , where we introduced the (scaled) shell volumes
which in the discretized version becomes
3 3
rn+ 1 − r ∆r2
 
yn+1 − 2yn + yn−1 yn+1 − yn−1 n− 1
∂α2 f ∼ + (19) Vn = 2 2
= ∆r rn2 + , (24)
∆r2 rn ∆r 3 12

Note that this form of the Laplace operator is notRconser- which also skip the factor 4π. Eq. (23) provides an ex-
vative, i. e., the discretized version of the integral ∂α2 f dr pression for the operator, which relies on the values on
does not vanish. the adjacent boundaries. Since integrating the operator
5

over the entire domain corresponds to summing all con- where the derivatives f ′ (r) are evaluated at the midpoints
tributions from individual shells, this formulation ensures and can thus be represented as
that adjacent shells have opposite contributions, so that
n+1 − yn
  y
finally only the outer boundaries contribute, ensuring the f ′ rn+ 12 ∼ (26a)
conservative property. Taken together we find ∆r
  y −y
n n−1
f ′ rn− 21 ∼ . (26b)
∆r
In the special case where the spherical grid does not have
a hole (r0 = 0), we then arrive at

3 (n + 1)2 (yn+1 − yn ) − n2 (yn − yn−1 )


∂α2 f ∼ ,
∆r2 (n + 1)3 − n3
(27)

which is a conservative discretization of the spherical


Laplacian by construction.
    Similarly, we can consider a spherical grid where fields
2
rn+ ′ 2
rn+ 12 − rn− ′ depend on the polar angle θ while still keeping azimuthal
1f 1f rn− 12
∂α2 f ∼ 2 2
(25) symmetry, i.e., assuming no dependence on ϕ. The con-
Vn dition for a conservative Laplace operator then reads

ZZ ZZ  
r2 sin θ ∂α2 f drdθ = sin θ∂r r2 ∂r f (r, θ) + ∂θ sin θ∂θ f (r, θ) drdθ
   
cell(n,m) cell(n,m)
 Z rn+ 1 Z θm+ 1
2 2
∂r r2 ∂r f (r, θ) dr + ∆r
   
= cos θm− 21 − cos θm+ 12 ∂θ sin θ∂θ f (r, θ) dθ
rn− 1 θm− 1
2 2
  r 1 θm+ 1
− cos θm+ 21 r2 ∂r f (r, θ) rn+ 21 + ∆r sin θ∂θ f (r, θ) θ 21

= cos θm− 21 (28)
n− m−
2 2

Here we assumed that we can neglect angular depen- implementation of the operator, we again integrate over
dencies in the radial integral and vice versa. Using the a single shell,
(scaled) volume of a grid cell, Z r 1
n+ r 1
2
∂α vα r2 dr = r2 vr (r) rn+ 21

3 3 (32)
 rn+ 1 − r
n− 12 n−

rn− 1 2
Vn,m = cos θm− 12 − cos θm+ 12 2
, (29) 2
3
Consequently, we find
we can write this as
2 2
rn+ 1 vr (rn+ 1 ) − r v (r 1 )
n− 1 r n− 2
  r 1
Vn,m ∂α2 f = cos θm− 12 − cos θm+ 12 r2 ∂r f (r, θ) rn+ 21 ∂α vα ∼ 2 2 2
(33)
n−
2 Vn
 θm+ 1
+ ∆r sin θ∂θ f (r, θ) θ 21 (30) where Vn is given by Eq. (24).
m−
2

D. Conservative divergence operator E. Conservative tensor divergence operator

The divergence of a spherically-symmetric vector field The divergence of a spherically-symmetric vector ten-
reads sor field reads

2vr (r) 2 [trr (r) − tϕϕ (r)]


∂α vα = vr′ (r) + (31) ∂β trβ = + t′rr (r) (34)
r r
where vr is the radial part of the vector field and primes where primes denote derivatives with respect to r. Note
denote derivatives with respect to r. For a conservative that the angular components of the resulting vector field
6

vanish. The integral over the shell reads where we skipped the factor 4π stemming from the an-
Z r 1 gle integration. Assuming that the quantity Dϕ (tαβ ) is
n+ r 1
2
∂β trβ r2 dr = r2 vr (r) rn+ 21

(35) constant across the discretization cell, we thus find
rn− 1 n−
2
2 h irn +∆r/2
Vn Dϕ (tαβ ) ∼ 2rtϕϕ (r) (40)
and we find rn −∆r/2

2 2
rn+ 1 vr (rn+ 1 ) − r v (r 1 ) where Vn is given by Eq. (24). Consequently,
2 n− 1 r n− 2
∂ α vα ∼ 2 2
(36)
Vn 2 h i
Dϕ (tαβ ) ∼ rn+ 21 tϕϕ (rn+ 21 ) − rn− 21 tϕϕ (rn− 12 ) (41)
where Vn is given by Eq. (24). Vn

where the derivatives tϕϕ (r) are evaluated at the mid-


F. Conservative double divergence operator points by the weighted average,

tϕϕ (rn ) + tϕϕ (rn+1 )


We next consider the double divergence of a rank-2 tϕϕ (rn+ 12 ) = . (42)
tensor field on spherical coordinates. In general, we have 2
Taken together, we have a conservative definition of the
∂α ∂β tαβ = Dr (tαβ ) − Dϕ (tαβ ) (37) angular part of the double divergence.
The radial part follows analogously using
where we defined the two differential operators
Z r 1 Z r 1
n+ n+
2trr 4t′rr 2 2 2 
2trr + 4rt′rr + r2 t′′rr dr

Dr (tαβ ) = + + t′′rr (38a) Dr (tαβ )r dr = 2
r2 r rn− 1
2
rn− 1
2
2tϕϕ 2t′ϕϕ (43)
Dϕ (tαβ ) = 2 + (38b)
r r
resulting in
and used tϕϕ = tθθ , which holds in spherically symmetric
systems. Here, primes denote derivatives with respect to h irn +∆r/2
the radial coordinate r. We next analyze these operators Vn Dr (tαβ ) ∼ 2rtrr (r) + r2 t′rr (r) (44)
rn −∆r/2
individually.
A conservative operator can be derived by integrating where
its definition in spherical coordinates over one spherical
shell from r = rn − ∆r/2 to r = rn + ∆r/2,  trr (rn+1 ) − trr (rn )
t′rr rn+ 21 = (45)
Z r 1 Z r 1 ∆r
n+ n+
tϕϕ + rt′ϕϕ dr , (39)
2 2 2  
Dϕ (tαβ )r dr = 2 so that Dr (tαβ ) can be expressed easily. Taken together,
rn− 1 rn− 1
2 2 the double divergence operator reads

h i h i

2
2rn+ 12 trr (rn+ 21 ) − tϕϕ (rn+ 21 ) + rn+ 1 trr (rn+ 1 ) − 2rn− 1
2 2
trr (rn− 1 ) − tϕϕ (r
2 n− 1) − r
2
2
t′ (r 1 )
n− 1 rr n− 2
2 2
∂α ∂β tαβ ∼
Vn
(46)

You might also like