Algorithmcore
Boundary element method
Also called BEM, boundary integral equation method, boundary integral method.
Reformulates a linear PDE as an integral equation on the boundary and discretizes only the boundary, giving a dense system with fewer unknowns.
- Solves
- Poisson equation
- Time
- Θ(m^3 + p m) worst
- Space
- Θ(m^2) extra
- Where
- m = the number of boundary elements; p = the number of interior points where the solution is evaluated
- Model
- Real RAM
- Properties
- dense systemdeterministicnot iterativeneeds fundamental solutionunknowns on boundary only
- Evidence
- Proof sketchCode in 1 languages
- Origin
- M. A. Jaswon, G. T. Symm, F. J. Rizzo, C. A. Brebbia, 1963
The boundary element method solves a linear partial differential equation with a known fundamental solution, such as the Laplace equation, by working only on the boundary of the domain. Green's identity turns the equation into an integral equation that relates the boundary values and the normal derivatives . The method cuts the boundary into elements, takes and as constant (or polynomial) on each, and enforces the integral equation at one point per element (collocation) or in the mean (Galerkin). Where is given it solves for and the other way round. The values inside the domain then come from the same integrals. In two dimensions the unknowns are on a curve, so a mesh with elements does the work of an area mesh with about elements. The price is a dense matrix with weakly singular entries, which costs memory and time for direct elimination unless a fast method such as the fast multipole method applies it.
In plain words
To know the temperature everywhere inside a metal plate at rest, it is enough to know the temperature and the heat flow along its edge. A formula, which comes from the way heat spreads from a single point, gives the inside from the edge. The boundary element method uses this formula backwards. On each piece of the edge, either the temperature or the heat flow is known, and the formula, applied at the edge itself, gives one equation per piece for the unknown values. Solving these equations completes the information on the edge. After that, the formula gives the temperature at any inside point you ask for. Only the edge is cut into pieces, never the inside.
The problem
Input. A bounded domain with a polygonal boundary , traversed counterclockwise, and for each part of either the value (Dirichlet) or the outward normal derivative (Neumann). A list of interior points.
Output. Approximations of the missing boundary data and of at the interior points, where is the solution of in with the given data.
The integral equation. With the fundamental solution of , Green's second identity gives for
and for at a smooth point of
The first integral on the right is the single layer, and the integral on the left is the double layer. A nonzero source term in the Poisson equation adds a domain integral of , or a particular solution can absorb it.
The algorithm
BEM-LAPLACE-2D(boundary, data)- 1
cut the boundary into m straight elements; x_i = midpoint of element i - 2
for i = 1 to m, for j = 1 to m - 3
G[i, j] <- integral over element j of G(x_i, y) ds_y ▷ closed form, weakly singular if i = j - 4
H[i, j] <- integral over element j of dG/dn_y(x_i, y) ds_y + 1/2 [i = j] - 5
in H u = G q, move the columns of the known values to the right side: A z = b - 6
solve A z = b by Gaussian elimination - 7
for each interior point x: u(x) <- sum over j of g_j(x) q_j - h_j(x) u_j - 8
return u and q on the boundary and the interior values
Closed forms for a straight element. Let the element run from to with length , unit tangent and outward normal . Write and . Then
with when . The double layer integral is times the signed angle under which the element is seen from , and it is zero when is on the line of the element. So line L3 is exact also for , where it gives , and the diagonal of line L4 is exactly .
Collocation at midpoints. The midpoint of a straight element is a smooth point of , so the factor is right there, also on a polygon with corners. At a corner with interior angle the factor would be .
Example
One element per side. Take the unit square, one element per side in the order bottom, right, top, left, and . The midpoints are , , and , so the given values are . Line L3 gives the diagonal , for two adjacent sides and for opposite sides. From the midpoint of a side, an adjacent side is seen under the angle , and the opposite side under . So line L4 gives , for adjacent sides and for opposite sides. Each row of sums to .
By symmetry is zero on the bottom and the top and . The row of the right side then reads
so . The exact flux is . The interior value at the centre is exactly , by symmetry.
Convergence on the square. For with Dirichlet data on all four sides, the error at the centre is , , and with 4, 8, 16 and 32 elements per side, which is second order. With given on the bottom and the top and on the two other sides, the centre error is with 16 elements per side, and it also falls at a rate close to 2. On the elements in the middle third of each side, the flux error is below with 32 elements per side. On the element next to a corner it stays near for every mesh: the flux jumps at the corner, and constant elements with collocation at the midpoints do not resolve this.
Correctness
The proofs are in the correctness proof.
- The boundary integral equation holds for every harmonic (theorem). It follows from Green's second identity on minus a small disc around , with the factor from the half disc at a smooth boundary point.
- The closed forms of lines L3 and L4 are exact (theorem). They come from the antiderivative of along a line and from the angle formula for the double layer.
- Each row of sums to zero (theorem). The angles under which the other elements are seen from a midpoint add up to . So a constant gives , and if is nonsingular the computed flux of a constant is exactly zero.
- Solvability and convergence (cited). In two dimensions the single layer operator is elliptic on the right space when the diameter of is less than 1. Then Galerkin boundary elements converge with the optimal rates (Steinbach 2008). For midpoint collocation with constant elements the analysis is more delicate. The second order at interior points above is a measurement.
Proof: The boundary integral equation, the exact element integrals and the discrete Gauss lawpartial
Setting. is a bounded domain whose boundary is a simple closed polygon, traversed counterclockwise, with outward unit normal . , so . The function is harmonic in and continuously differentiable up to , and . The status is partial because the convergence of midpoint collocation is cited, not proved (Remark 2).
Lemma 1 (representation formula). For , .
Proof. Let be the disc of radius around , inside , and . On both and are harmonic, so Green's second identity gives . The boundary of is and the circle , with the normal pointing toward on the circle. On the circle and (the normal points toward , where grows). So the circle contributes . The first term is because is bounded, and the second tends to by continuity. Letting gives .
Lemma 2 (the boundary equation). For on in the interior of a straight side, , where both integrals exist as ordinary integrals.
Proof. Near the side is a line through , and for on it, , so vanishes there. Away from the kernel is bounded. So the double layer integral is an ordinary integral, and the single layer integral exists because is integrable on a line. Repeat the proof of Lemma 1 with for this boundary point. For small the part of the circle inside is a half circle, so the term over it tends to , and the logarithmic term tends to 0. The part of cut out by the disc has length , and its contribution tends to 0 because the double layer kernel is zero there and .
Lemma 3 (closed forms). Let a straight element run from to with length , unit tangent , normal , and let and . Then with as on the page, and , where is the angle at between and , taken with the sign of .
Proof. With we have , so . Put . For , . For , , and the integral is improper but finite at . For the second integral, , so it equals . The bracket is the angle swept by the direction of as moves from to . Its absolute value is the angle between and , which is less than , and its sign is that of . For both sides are zero.
The reference code uses exactly these formulas, with , so lines L3 and L4 are exact up to rounding. At the midpoint of its own element, , , and .
Theorem 1 (discrete Gauss law). Let be the midpoint of element . Then . So applied to a constant vector is zero, and if is nonsingular, the computed for constant Dirichlet data is zero.
Proof. Apply Lemma 2 to , which is harmonic with : . The integral over is the sum of the integrals over the elements, which are the off diagonal by Lemma 3 (and zero for ). So , which is since . Note that this is an identity of exact integrals, not an approximation: the double layer of a constant is computed exactly. With constant data , the system of line L5 is , so if is nonsingular.
Corollary 1. At an interior point , the same argument with Lemma 1 gives , so line L7 returns exactly for constant data. The reference tests check both identities to .
Remark 1 (the kernel of the single layer). On the circle of radius , for (the mean value of over a circle through ). So the constant density lies in the kernel of the single layer for . Scaling the domain so that its diameter is less than 1 makes the single layer operator elliptic on (Steinbach 2008).
Remark 2 (convergence, cited). For the Galerkin method with the single layer operator and piecewise constants, ellipticity and Cea's lemma give quasi optimal convergence in (Steinbach 2008). The midpoint collocation of the code is the classical method of Symm. Its error at interior points, second order in the element length on the square, is measured by the reference tests and is not proved here.
Complexity
In the real-RAM model, lines L2 to L4 compute entries of two matrices, each in constant time, so time and space. Line L6 is dense Gaussian elimination, time. Line L7 costs per interior point. In total, time and extra space. An iterative solver such as GMRES with iterations costs , and with the fast multipole method one matrix vector product costs about for a fixed accuracy, so large problems are solved in nearly linear time.
Properties
- One dimension less. Only the boundary is meshed. For exterior problems, such as the field around a body in all of space, no truncation of the domain is needed, because already satisfies the condition at infinity.
- Dense and not symmetric. Collocation gives a dense nonsymmetric matrix. Galerkin boundary elements give a symmetric system for the single layer equation, at the price of double integrals.
- Exact interior representation. Once and are known on , line L7 gives and its derivatives at any interior point with no interpolation, and the result is harmonic.
- Needs a fundamental solution. The method works for the Laplace, Helmholtz, Stokes and linear elasticity equations with constant coefficients. Variable coefficients and nonlinear terms bring back domain integrals.
Variants and improvements
- Galerkin boundary elements. Test the integral equation with the basis functions. This gives the full stability and convergence theory, symmetric systems for the symmetric formulation, and better accuracy near corners.
- Higher order elements. Linear or quadratic and on curved elements. At corners, two values of per node (double nodes) handle the jump of the normal.
- Indirect method. Write as a single or double layer potential of an unknown density, solve for the density, then evaluate. A second kind equation with the double layer is well conditioned.
- Fast methods. The fast multipole method, hierarchical matrices and panel clustering apply or compress the dense matrix in nearly linear time and memory.
- Coupling with finite elements. Finite elements in a bounded region with nonlinear material and boundary elements for the linear exterior.
When to use it
Use it for linear problems with constant coefficients on domains whose boundary is much smaller than their volume: exterior acoustics and electromagnetics, potential flow around bodies, electrostatics, and fracture and contact problems where only surface values matter. Use finite elements for nonlinear materials, variable coefficients or when the whole field is needed on a fine grid. For more than a few thousand elements, a fast method is necessary, because the dense matrix grows with .
Pitfalls
- The 2D single layer at scale 1. On the unit circle, for on , so the constant density is in the kernel of the single layer and is nearly singular. In general this happens when the logarithmic capacity of is 1. Scale the domain so that its diameter is less than 1. The reference test shows the row sums of are below on the unit circle and about on the circle of radius .
- Near singular integrals. For interior points close to the boundary, or for thin domains, the kernels vary fast along nearby elements and low order quadrature fails. Use the closed forms or adaptive quadrature.
- Corners. The normal jumps at a corner, so is discontinuous there. Constant elements on each side handle this, but the flux next to a corner converges slowly or not at all in the maximum norm.
- Orientation. The sign of the double layer depends on the direction of the normal. A clockwise boundary flips every with , and the solution is wrong with no error message. The row sum test catches it.
- Cost of the dense matrix. With elements the matrix has entries. Without a fast method the method is limited to small problems.
Implementations
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/boundary-element-method/. The authors are listed in prov/03WMHA.jsonl.
"""Direct boundary element method for the 2D Laplace equation, constant elements.
The boundary is a closed polygon, traversed counterclockwise, cut into M
straight elements. On each element u and the outward normal derivative
q = du/dn are constant, and the boundary integral equation
(1/2) u(x) + int_G dG/dn_y(x, y) u(y) ds_y = int_G G(x, y) q(y) ds_y,
G(x, y) = -(1 / (2 pi)) ln |x - y|,
is collocated at the element midpoints. The integrals over straight elements
are computed in closed form, including the weakly singular one.
"""
import math
import scimat
def segment_integrals(x, a, b):
"""Return (g, h) = (int G(x, y) ds_y, int dG/dn_y(x, y) ds_y) over the segment a b.
The normal of the segment is the outward normal of a counterclockwise
boundary: n = (t_y, -t_x) for the unit tangent t from a to b.
The formula holds for any point x, also on the segment.
"""
length = math.hypot(b[0] - a[0], b[1] - a[1])
tx, ty = (b[0] - a[0]) / length, (b[1] - a[1]) / length
nx, ny = ty, -tx
rx, ry = x[0] - a[0], x[1] - a[1]
s0 = rx * tx + ry * ty # projection of x on the line
d = rx * nx + ry * ny # signed distance of x from the line
def prim(u):
# antiderivative of ln sqrt(u^2 + d^2) in u
if d == 0:
return u * math.log(abs(u)) - u if u != 0 else 0.0
return 0.5 * u * math.log(u * u + d * d) - u + d * math.atan(u / d)
ln_int = prim(length - s0) - prim(-s0)
g = -ln_int / (2 * math.pi)
# int (y - x) . n / r^2 ds is the signed angle that the segment subtends at x
ax, ay = a[0] - x[0], a[1] - x[1]
bx, by = b[0] - x[0], b[1] - x[1]
if d == 0:
angle = 0.0
else:
angle = math.atan2(ax * by - ay * bx, ax * bx + ay * by)
# the kernel (y - x) . n / r^2 equals -d / r^2, so the sign is that of -d
angle = -abs(angle) if d > 0 else abs(angle)
h = -angle / (2 * math.pi)
return g, h
def polygon_elements(vertices, per_side):
"""Cut a counterclockwise polygon into per_side equal elements per side."""
elems = []
nv = len(vertices)
for k in range(nv):
p, r = vertices[k], vertices[(k + 1) % nv]
for j in range(per_side):
s, t = j / per_side, (j + 1) / per_side
elems.append(((p[0] + s * (r[0] - p[0]), p[1] + s * (r[1] - p[1])), (p[0] + t * (r[0] - p[0]), p[1] + t * (r[1] - p[1]))))
return elems
def midpoint(e):
return ((e[0][0] + e[1][0]) / 2, (e[0][1] + e[1][1]) / 2)
def outward_normal(e):
(ax, ay), (bx, by) = e
length = math.hypot(bx - ax, by - ay)
return ((by - ay) / length, -(bx - ax) / length)
def assemble(elems):
"""Return the M by M matrices G and H of the collocation system (H includes 1/2 I)."""
m = len(elems)
g = scimat.zeros(m)
h = scimat.zeros(m)
for i in range(m): # @line L2
xi = midpoint(elems[i])
for j in range(m):
gij, hij = segment_integrals(xi, elems[j][0], elems[j][1])
g[i][j] = gij # @line L3
h[i][j] = hij + (0.5 if i == j else 0.0) # @line L4
return g, h
def bem_solve(elems, kinds, values):
"""Solve the mixed problem. kinds[j] is 'u' (u given) or 'q' (q given) on element j.
Return (u, q, (G, H)): the boundary values of u and q on all elements.
"""
m = len(elems) # @line L1
g, h = assemble(elems)
# H u = G q; move the known values to the right side
a = scimat.zeros(m)
rhs = [0.0] * m
for j in range(m): # @line L5
for i in range(m):
if kinds[j] == "u":
a[i][j] = -g[i][j]
rhs[i] -= h[i][j] * values[j]
else:
a[i][j] = h[i][j]
rhs[i] += g[i][j] * values[j]
z = scimat.solve(a, rhs) # @line L6
u = [values[j] if kinds[j] == "u" else z[j] for j in range(m)]
q = [z[j] if kinds[j] == "u" else values[j] for j in range(m)]
return u, q, (g, h)
def interior_value(x, elems, u, q):
"""Return u(x) = sum_j (g_j(x) q_j - h_j(x) u_j) at a point x inside the domain."""
total = 0.0
for e, uj, qj in zip(elems, u, q):
gj, hj = segment_integrals(x, e[0], e[1])
total += gj * qj - hj * uj # @line L7
return total
def dirichlet(vertices, per_side, u_exact, points):
"""Solve the Dirichlet problem with boundary data u_exact; return (q, values at points)."""
elems = polygon_elements(vertices, per_side)
u_bc = [u_exact(*midpoint(e)) for e in elems]
u, q, _ = bem_solve(elems, ["u"] * len(elems), u_bc)
return q, [interior_value(p, elems, u, q) for p in points] # @line L8
scimat.py 227 lines
# SPDX-License-Identifier: MIT
# Shared helper of the scientific-computing pages of https://algorithm.tamnd.com/. The authors are listed in prov/.
"""Small dense linear algebra and error norms for the scientific-computing pages.
A matrix is a list of rows and a vector is a list. Entries may be int,
float or fractions.Fraction. The routines that do not take a square root
(solve, inv, det and the arithmetic) keep Fraction entries exact.
cholesky, cholesky_solve and norm2 take square roots and return floats.
API (fixed; other pages copy this file byte for byte):
zeros(m, n=None) m by n zero matrix (n = m if not given)
eye(n) n by n identity
add(A, B), sub(A, B) entrywise sum and difference (vectors or matrices)
scale(c, A) c times A (vector or matrix)
matmul(A, B) matrix product
matvec(A, x) matrix times vector
transpose(A) transpose
dot(x, y) inner product of two vectors
norm2(x) Euclidean norm of a vector, as a float
outer(x, y) outer product x y^T
solve(A, b) Gaussian elimination with partial pivoting;
b is a vector or a matrix of right sides
inv(A) inverse of A
det(A) determinant of A
cholesky(A) lower triangular L with A = L L^T (floats)
cholesky_solve(L, b) solve L L^T x = b for a vector b
err_l1(e, dx=1) sum |e_j| dx
err_l2(e, dx=1) sqrt(sum e_j^2 dx), as a float
err_linf(e, dx=1) max |e_j| (dx is not used)
observed_order(errors) [log2(E_i / E_(i+1)) for successive errors]
"""
import math
def zeros(m, n=None):
"""Return an m by n matrix of zeros (n = m if n is None)."""
if n is None:
n = m
return [[0] * n for _ in range(m)]
def eye(n):
"""Return the n by n identity matrix."""
out = zeros(n, n)
for i in range(n):
out[i][i] = 1
return out
def _is_matrix(a):
return len(a) > 0 and isinstance(a[0], list)
def add(a, b):
"""Return a + b for two vectors or two matrices of the same shape."""
if _is_matrix(a):
return [[x + y for x, y in zip(ra, rb)] for ra, rb in zip(a, b)]
return [x + y for x, y in zip(a, b)]
def sub(a, b):
"""Return a - b for two vectors or two matrices of the same shape."""
if _is_matrix(a):
return [[x - y for x, y in zip(ra, rb)] for ra, rb in zip(a, b)]
return [x - y for x, y in zip(a, b)]
def scale(c, a):
"""Return c times a, for a vector or a matrix a."""
if _is_matrix(a):
return [[c * x for x in row] for row in a]
return [c * x for x in a]
def transpose(a):
"""Return the transpose of the matrix a."""
return [list(col) for col in zip(*a)]
def matmul(a, b):
"""Return the matrix product a b."""
bt = transpose(b)
return [[sum(x * y for x, y in zip(row, col)) for col in bt] for row in a]
def matvec(a, x):
"""Return the product of the matrix a and the vector x."""
return [sum(aij * xj for aij, xj in zip(row, x)) for row in a]
def dot(x, y):
"""Return the inner product of the vectors x and y."""
return sum(a * b for a, b in zip(x, y))
def norm2(x):
"""Return the Euclidean norm of the vector x, as a float."""
return math.sqrt(float(dot(x, x)))
def outer(x, y):
"""Return the outer product x y^T as a matrix."""
return [[a * b for b in y] for a in x]
def _eliminate(a, rhs):
"""Reduce a to upper triangular form with partial pivoting.
a is a copy of the square matrix, rhs a list of right side rows (one
row per row of a) that receives the same row operations. Return the
number of row swaps. Raise ValueError if a pivot is zero.
"""
n = len(a)
swaps = 0
for k in range(n):
p = max(range(k, n), key=lambda i: abs(a[i][k]))
if a[p][k] == 0:
raise ValueError("matrix is singular")
if p != k:
a[k], a[p] = a[p], a[k]
rhs[k], rhs[p] = rhs[p], rhs[k]
swaps += 1
for i in range(k + 1, n):
f = a[i][k] / a[k][k]
if f == 0:
continue
for j in range(k, n):
a[i][j] -= f * a[k][j]
for j in range(len(rhs[i])):
rhs[i][j] -= f * rhs[k][j]
return swaps
def solve(a, b):
"""Solve a x = b by Gaussian elimination with partial pivoting.
b is a vector (the result is a vector) or a matrix with one column per
right side (the result is a matrix). Fraction entries stay exact.
Raise ValueError if a is singular.
"""
n = len(a)
work = [list(row) for row in a]
vector = not _is_matrix(b)
rhs = [[v] for v in b] if vector else [list(row) for row in b]
_eliminate(work, rhs)
m = len(rhs[0]) if n else 0
x = [[0] * m for _ in range(n)]
for i in range(n - 1, -1, -1):
for j in range(m):
s = rhs[i][j]
for k in range(i + 1, n):
s -= work[i][k] * x[k][j]
x[i][j] = s / work[i][i]
if vector:
return [row[0] for row in x]
return x
def inv(a):
"""Return the inverse of the square matrix a."""
return solve(a, eye(len(a)))
def det(a):
"""Return the determinant of the square matrix a (0 if singular)."""
work = [list(row) for row in a]
rhs = [[] for _ in a]
try:
swaps = _eliminate(work, rhs)
except ValueError:
return 0
d = -1 if swaps % 2 else 1
for i in range(len(a)):
d *= work[i][i]
return d
def cholesky(a):
"""Return the lower triangular L with a = L L^T, in floats.
Raise ValueError if a is not symmetric positive definite.
"""
n = len(a)
low = [[0.0] * n for _ in range(n)]
for j in range(n):
s = float(a[j][j]) - sum(low[j][k] ** 2 for k in range(j))
if s <= 0.0:
raise ValueError("matrix is not positive definite")
low[j][j] = math.sqrt(s)
for i in range(j + 1, n):
t = float(a[i][j]) - sum(low[i][k] * low[j][k] for k in range(j))
low[i][j] = t / low[j][j]
return low
def cholesky_solve(low, b):
"""Solve L L^T x = b, with L from cholesky, for a vector b."""
n = len(low)
y = [0.0] * n
for i in range(n):
y[i] = (float(b[i]) - sum(low[i][k] * y[k] for k in range(i))) / low[i][i]
x = [0.0] * n
for i in range(n - 1, -1, -1):
x[i] = (y[i] - sum(low[k][i] * x[k] for k in range(i + 1, n))) / low[i][i]
return x
def err_l1(e, dx=1):
"""Return the discrete L1 norm sum |e_j| dx."""
return sum(abs(v) for v in e) * dx
def err_l2(e, dx=1):
"""Return the discrete L2 norm sqrt(sum e_j^2 dx), as a float."""
return math.sqrt(float(sum(v * v for v in e) * dx))
def err_linf(e, dx=1):
"""Return the max norm max |e_j|. dx is accepted for a uniform call and not used."""
return max(abs(v) for v in e)
def observed_order(errors):
"""Return [log2(E_i / E_(i+1))] for a list of errors on meshes h, h/2, h/4, ..."""
return [math.log2(float(errors[i]) / float(errors[i + 1])) for i in range(len(errors) - 1)]
test_boundary_element_method.py 126 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/boundary-element-method/. The authors are listed in prov/03WMHA.jsonl.
import math
import scimat
from boundary_element_method import (
assemble,
bem_solve,
dirichlet,
interior_value,
midpoint,
outward_normal,
polygon_elements,
segment_integrals,
)
SQUARE = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)]
def u_exp(x, y):
return math.exp(x) * math.sin(y)
def q_exp(e):
(x, y), (nx, ny) = midpoint(e), outward_normal(e)
return math.exp(x) * (math.sin(y) * nx + math.cos(y) * ny)
def brute_force(x, a, b, n=20000):
length = math.hypot(b[0] - a[0], b[1] - a[1])
nx, ny = outward_normal((a, b))
g = h = 0.0
for k in range(n):
s = (k + 0.5) / n
rx, ry = a[0] + s * (b[0] - a[0]) - x[0], a[1] + s * (b[1] - a[1]) - x[1]
r2 = rx * rx + ry * ry
g += -math.log(r2) / (4 * math.pi) * length / n
h += -(rx * nx + ry * ny) / r2 / (2 * math.pi) * length / n
return g, h
def test_segment_integrals_against_quadrature():
a, b = (0.2, 0.1), (0.9, 0.5)
for x in [(0.4, 0.6), (0.6, 0.2), (-1.0, 3.0), (2.0, 0.0)]:
g, h = segment_integrals(x, a, b)
g2, h2 = brute_force(x, a, b)
assert abs(g - g2) < 1e-8 and abs(h - h2) < 1e-8
def test_self_integral_closed_form():
# x at the midpoint of its own element: int ln r ds = L ln(L / 2) - L, and the double layer is 0
a, b = (0.0, 0.0), (0.3, 0.4)
g, h = segment_integrals(midpoint((a, b)), a, b)
assert abs(g + (0.5 * math.log(0.25) - 0.5) / (2 * math.pi)) < 1e-15
assert h == 0.0
def test_gauss_law_for_the_double_layer():
elems = polygon_elements(SQUARE, 5)
_, h = assemble(elems)
# with the 1/2 on the diagonal, every row of H sums to zero: constants are in its kernel
assert max(abs(sum(row)) for row in h) < 1e-14
# at an interior point the double layer of the constant 1 is -1
total = sum(segment_integrals((0.3, 0.6), e[0], e[1])[1] for e in elems)
assert abs(total + 1) < 1e-14
def test_constant_solution_has_zero_flux():
elems = polygon_elements(SQUARE, 8)
u, q, _ = bem_solve(elems, ["u"] * len(elems), [1.0] * len(elems))
assert max(abs(v) for v in q) < 1e-12
assert abs(interior_value((0.4, 0.7), elems, u, q) - 1) < 1e-13
def test_dirichlet_interior_values_converge_at_second_order():
errs = []
for per_side in (4, 8, 16, 32):
_, vals = dirichlet(SQUARE, per_side, u_exp, [(0.5, 0.5)])
errs.append(abs(vals[0] - u_exp(0.5, 0.5)))
assert errs[-1] < 2e-6
assert all(r > 1.9 for r in scimat.observed_order(errs))
def test_flux_away_from_corners_and_at_corners():
per_side = 32
elems = polygon_elements(SQUARE, per_side)
u, q, _ = bem_solve(elems, ["u"] * len(elems), [u_exp(*midpoint(e)) for e in elems])
err = [abs(qq - q_exp(e)) for e, qq in zip(elems, q)]
# elements in the middle third of each side
middle = [err[k * per_side + j] for k in range(4) for j in range(per_side // 3, 2 * per_side // 3)]
assert max(middle) < 2e-3
# the element next to the corner (1, 0) keeps an error of order one with constant elements
assert err[per_side - 1] > 0.1
def test_mixed_problem():
errs = []
for per_side in (4, 8, 16):
elems = polygon_elements(SQUARE, per_side)
kinds, values = [], []
for e in elems:
(x, y), (nx, ny) = midpoint(e), outward_normal(e)
if abs(ny) > 0.5: # bottom and top: u given
kinds.append("u")
values.append(u_exp(x, y))
else: # left and right: q given
kinds.append("q")
values.append(q_exp(e))
u, q, _ = bem_solve(elems, kinds, values)
errs.append(abs(interior_value((0.5, 0.5), elems, u, q) - u_exp(0.5, 0.5)))
assert errs[-1] < 2e-4
assert all(r > 1.5 for r in scimat.observed_order(errs))
def test_unit_circle_makes_the_single_layer_nearly_singular():
# On a circle of radius R, int ln |x - y| ds_y = 2 pi R ln R for x on the circle,
# so for R = 1 the constant density is (almost) in the kernel of G.
results = {}
for radius in (1.0, 0.5):
n = 64
verts = [(radius * math.cos(2 * math.pi * k / n), radius * math.sin(2 * math.pi * k / n)) for k in range(n)]
g, _ = assemble(polygon_elements(verts, 1))
results[radius] = max(abs(sum(row)) for row in g)
assert results[1.0] < 2e-3
assert abs(results[0.5] - (-0.5 * math.log(0.5))) < 1e-3
History
Jaswon and Symm in 1963 wrote potential problems as boundary integral equations and solved them on a computer with piecewise constant densities. Rizzo in 1967 did the same for elastostatics with the direct formulation, which uses the boundary values themselves as unknowns. Brebbia in 1978 named it the boundary element method and showed its relation to finite elements. The fast multipole method of Greengard and Rokhlin from 1987 removed the quadratic cost of the dense matrix and made large problems possible.
References
- M. A. Jaswon, Integral equation methods in potential theory. I, Proceedings of the Royal Society of London A 275(1360) (1963) 23-32, the boundary integral equations of potential theory with the single and double layer
- G. T. Symm, Integral equation methods in potential theory. II, Proceedings of the Royal Society of London A 275(1360) (1963) 33-46, the numerical solution with piecewise constant densities and collocation at boundary points
- F. J. Rizzo, An integral equation approach to boundary value problems of classical elastostatics, Quarterly of Applied Mathematics 25(1) (1967) 83-95, the direct boundary integral formulation of elastostatics, with the boundary displacements and tractions as unknowns
- Olaf Steinbach, Numerical Approximation Methods for Elliptic Boundary Value Problems, Finite and Boundary Elements, Springer, 2008, the representation formula, the boundary integral operators and their mapping properties, the ellipticity of the 2D single layer operator after scaling, and the boundary element methods
- L. Greengard and V. Rokhlin, A fast algorithm for particle simulations, Journal of Computational Physics 73(2) (1987) 325-348, the fast multipole method, which applies the dense boundary element matrix in nearly linear time
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag 03WMHA Edit this page History Report an error Markdown