Boundary conditions

A boundary condition is an equation that holds on a wall: on a face of a box or of a round domain, or on a curve of a shape in the plane. It gives the value of the unknown there, its slope, or a mix of the two.

import numpy as np
import sympy as sp
import pdemagic as pm
from pdemagic import curves

x, y = sp.symbols("x y", real=True)
u = sp.Function("u")(x, y)
v = sp.Function("v")(x)
box = pm.Box({x: (0, 1), y: (0, 1)})
rod = pm.Box({x: (0, 1)})

The value

The simplest condition gives the value of the unknown, which is called a Dirichlet condition. When the same equation holds on every face, pass it alone as boundary. A membrane that is held all around and sags under an even load:

membrane = pm.Problem(sp.Eq(-pm.laplacian(u), 1), domain=box, boundary=sp.Eq(u, 0))
pm.solve(membrane).plot()
The figure that the example above draws. The figure that the example above draws.

When the faces differ, pass a mapping from each face to its equation. A face is the coordinate that is constant on it and the end it lies at: (x, 0) is the face where x is 0. On a region the keys are the curves or their pieces. Every face and every piece needs a condition.

The slope, and a mix of both

A condition can give the derivative of the unknown across the face in place of its value, which is called a Neumann condition, or a combination of both, a Robin condition. On a face, write the derivative with respect to the coordinate of the face:

heated_rod = pm.Problem(
    sp.Eq(v.diff(x, 2), 2),
    domain=rod,
    boundary={
        (x, 0): sp.Eq(v, 1),  # the value
        (x, 1): sp.Eq(v.diff(x), 3),  # the slope
    },
)
along_the_rod = pm.solve(heated_rod)
(xs,) = along_the_rod.axes
assert abs(along_the_rod[v] - (xs**2 + xs + 1)).max() < 1e-12

This derivative is the one along the coordinate, not along the outward normal. At an upper face the two agree. At a lower face they differ in sign: heat that leaves through the face x = 0 at the rate q is sp.Eq(v.diff(x), q), and through the face x = 1 it is sp.Eq(v.diff(x), -q).

The slope along the normal

pm.normal_derivative(u) is the slope along the normal that points out of the domain. On the face x = 0 it stands for -u.diff(x), on the face x = 1 for u.diff(x), and on the rim of a disk for the derivative along the radius. With it one condition can stand for all faces. A rod that is heated evenly and loses heat through both ends in proportion to its temperature there:

losing = pm.Problem(
    sp.Eq(-v.diff(x, 2), 1),
    domain=rod,
    boundary=sp.Eq(v + pm.normal_derivative(v), 0),
)
lost = pm.solve(losing)
(xs,) = lost.axes
assert abs(lost[v] - (xs * (1 - xs) / 2 + 0.5)).max() < 1e-12

On a shape in the plane, whose boundary follows no coordinate, it is the only way to write the slope. At the edge of a hole the normal points towards the inside of the hole. A plate that is kept cold along its rim and has a hole through which nothing flows:

rim = curves.Circle((0, 0), 1, name="rim")
hole = curves.Circle((0.4, 0.2), 0.25, name="hole")

plate = pm.Problem(
    sp.Eq(-pm.laplacian(u), 1),
    domain=pm.Region(x, y, rim, hole),
    boundary={
        rim: sp.Eq(u, 0),  # the value
        hole: sp.Eq(pm.normal_derivative(u), 0),  # no flux
    },
)
pm.solve(plate).plot()
The figure that the example above draws. The figure that the example above draws.

What is given

A condition has the form \(a\,u + b\,\partial u/\partial n = g\). The coefficients \(a\) and \(b\) are numbers. What is given, \(g\), is a number, a formula in the coordinates, or an array. On a round domain it may change along the rim, as in sp.Eq(s, sp.cos(3 * phi) / 4). In a problem in time it may depend on the time.

In a medium, pm.normal_derivative(u) is still the slope of the unknown. If you know the flux through a piece of the boundary, which is the conductivity times that slope, divide it by the conductivity.

Complex coefficients: a wall that lets waves out

Coefficients may be complex. With \(\partial u/\partial x - i k u = 0\) a wave of the wave number \(k\) leaves through the face and is not reflected:

w = sp.Function("w")(x)
k = 7
tube = pm.Problem(
    sp.Eq(w.diff(x, 2) + k**2 * w, 0),
    domain=rod,
    boundary={(x, 0): sp.Eq(w, 1), (x, 1): sp.Eq(w.diff(x) - sp.I * k * w, 0)},
)
leaving = pm.solve(tube)
(xs,) = leaving.axes
assert abs(leaving[w] - np.exp(1j * k * xs)).max() < 1e-9

The solution is then complex. More about such walls is on the page about waves of one frequency.

When every face gives the slope

If no face fixes a value, and the equation has no term with the unknown itself, the solution is determined only up to a constant. pdemagic returns the one whose mean is zero.

Such a problem has a solution only if the source balances what flows through the faces. If it does not, pdemagic takes the excess off the source, as it does for periodic boxes, and reports what it took:

insulated = pm.Problem(
    sp.Eq(pm.laplacian(u), sp.cos(sp.pi * x) + 0.5),
    domain=box,
    boundary=sp.Eq(pm.normal_derivative(u), 0),
)
settled = pm.solve(insulated)
assert abs(settled.diagnostics["removed_source_mean"] - 0.5) < 1e-9
assert abs(settled.integral()) < 1e-12

The plan says in a note that the constant was left open.

More than one condition

An equation of fourth order takes two conditions on each face: a list of two equations stands where one stood, as in [sp.Eq(u, 0), sp.Eq(pm.normal_derivative(u), 0)] for a plate that is clamped. See Beams and plates.

A system has one condition for each unknown, also as a list, on a region, on a box and on a round domain. Each condition holds one unknown.

What does not work yet

  • Coefficients \(a\) and \(b\) that vary over a face, and derivatives along the face.

  • A condition with a derivative in time. A wall that lets waves out without reflection is therefore not available for problems in time.

  • An array for one face of a box alone: it is given on the whole grid.

See also