Skip to content
Projects

Edge space

Download notebookView on GitHubRun when the site was built.

CompositeEdges is the space of an electric field next to the potential’s nodes: one value per edge of the leaf cells, the line integral of the field along it. It is the field space of an implicit, energy-conserving particle-in-cell scheme on patches: the gradient maps the nodes’ potential into it exactly, its mass matrix is the field energy, and the gather of the field at markers has the current deposit as its transpose.

import numpy as np
from patchwork import (
AMRHierarchy,
CompositeEdges,
CompositeNodes,
Dirichlet,
Neumann,
NodalPoisson,
plot_patches,
)

Inside a leaf cell the field is the lowest-order edge interpolant: the x component varies linearly in y between the cell’s lower and upper x-edges, the y component linearly in x between its left and right y-edges. A coarse edge on the edge of a patch is two fine half-edges; they hang, and each carries half the coarse edge’s line integral (edges.reduction).

mesh = AMRHierarchy(base_shape=(16, 16), periodic=(False, True))
mesh.add_patch(level=1, box=((8, 24), (8, 24)))
nodes = CompositeNodes(mesh)
edges = CompositeEdges(nodes)
print(
f"{nodes.num_nodes} nodes ({nodes.hanging.sum()} hanging), "
f"{edges.num_edges} edges ({edges.hanging.sum()} hanging)"
)
fig, ax = plot_patches(mesh)
480 nodes (32 hanging), 960 edges (64 hanging)
Notebook output

edges.gradient is the difference of the potential along every edge. For a potential with its hanging nodes filled (nodes.reduction @ phi) the result is an edge field whose hanging edges are filled too: the constraints commute with the gradient. The field it gathers at any point is the gradient of the bilinear interpolant of the potential.

x, y = nodes.coordinates.T
phi = nodes.reduction @ (np.sin(2 * np.pi * x) * np.cos(2 * np.pi * y))
e = edges.gradient @ phi
print("hanging edges filled:", np.allclose(edges.reduction @ e, e))
points = np.array([[0.3, 0.52], [0.7, 0.45]])
ex, ey = (matrix @ e for matrix in edges.evaluation(points))
step = 1e-7
dx = (
nodes.evaluation(points + [[step], [0]]) @ phi
- nodes.evaluation(points - [[step], [0]]) @ phi
) / (2 * step)
print("E_x from the edges:", ex, " d(phi)/dx of the bilinear potential:", dx)
hanging edges filled: True
E_x from the edges: [0.56021877 5.90978111] d(phi)/dx of the bilinear potential: [0.56021877 5.90978111]

edges.mass(lumped=True) integrates |E|^2 with the vertex quadrature: it is diagonal, and G^T M1 G is NodalPoisson’s operator, its five-point stencil inside a patch. mass(lumped=False) is the exact integral; both give the same energy for a uniform field.

poisson = NodalPoisson(
mesh, faces={"x_lower": Dirichlet(0.0), "x_upper": Neumann(0.0)}, nodes=nodes
)
gradient = edges.gradient @ nodes.reduction
stiffness = gradient.T @ edges.mass(lumped=True) @ gradient
free = poisson.free
print(
"difference to NodalPoisson:",
abs(stiffness.tocsr()[free][:, free] - poisson.matrix).max(),
)
print(
"field energy, lumped and exact:",
e @ edges.mass() @ e,
e @ edges.mass(lumped=False) @ e,
)
difference to NodalPoisson: 0.0
field energy, lumped and exact: 19.528190687419027 19.123954894746248

The lumped and the consistent mass are the only difference between the finite-difference and the finite-element (Q1) version of the operator: NodalPoisson(mass="consistent") assembles G^T M1 G with the consistent edge mass and integrates the source with the consistent node mass (nodes.mass(lumped=False)), from the same per-cell blocks. The Q1 scheme is exact for a bilinear potential, across the patch edge too; the five-point stencil is not where nodes hang.

box = AMRHierarchy(base_shape=(16, 16))
box.add_patch(level=1, box=((8, 24), (8, 24)))
def bilinear(x, y):
return 1.0 + 2.0 * x - y + 3.0 * x * y
sides = ("x_lower", "x_upper", "y_lower", "y_upper")
faces = {side: Dirichlet(bilinear) for side in sides}
for mass in ("lumped", "consistent"):
solver = NodalPoisson(box, faces=faces, mass=mass)
edges_box = CompositeEdges(solver.nodes)
g = edges_box.gradient @ solver.nodes.reduction
m1 = edges_box.mass(lumped=mass == "lumped")
operator = (g.T @ m1 @ g).tocsr()[solver.free][:, solver.free]
x, y = solver.nodes.coordinates.T
error = np.abs(solver.solve(np.zeros_like(x)) - bilinear(x, y)).max()
print(
f"{mass:>10}: |A - G^T M1 G| = {abs(operator - solver.matrix).max():.0e},"
f" error of a bilinear potential {error:.1e}"
)
lumped: |A - G^T M1 G| = 0e+00, error of a bilinear potential 1.5e-03
consistent: |A - G^T M1 G| = 9e-16, error of a bilinear potential 2.3e-14

edges.deposit(points, vectors) puts vectors at points on the edges with the weights of the gather, whose transpose it is: the work of the gathered field on the markers’ currents is the current’s work on the edges.

rng = np.random.default_rng(1)
points = np.vstack((rng.random(500), rng.random(500)))
current = rng.normal(size=(2, 500))
gathered = np.array([matrix @ e for matrix in edges.evaluation(points)])
print(edges.deposit(points, current) @ e, np.sum(current * gathered))
print("compiled gather:", np.abs(edges.gather(points, e) - gathered).max())
-60.77032960324005 -60.77032960324002
compiled gather: 8.881784197001252e-16

edges.deposit_paths(start, end, charges) integrates the current of charges moving along straight paths against the edge functions, each path split at every cell face it crosses, patch edges included. The charge is then conserved exactly: the divergence of the current, (G T)^T j, is the change of the charge deposited on the nodes. It runs mpifield’s compiled path deposit (mpifield.WHITNEY) on every patch, on the parts of the paths in the patch’s leaf cells; edges.gather(points, e) is evaluation the same way, with mpifield’s compiled edge gather.

start = np.vstack((rng.random(500), rng.random(500)))
end = start + rng.normal(scale=0.1, size=start.shape)
end[0] = np.clip(end[0], 0.0, 1.0) # bounded in x, periodic in y
charges = rng.normal(size=500)
current = edges.deposit_paths(start, end, charges)
divergence = gradient.T @ current
change = nodes.reduction.T @ (
nodes.deposit(end, charges) - nodes.deposit(start, charges)
)
print("largest difference:", np.abs(divergence - change).max())
largest difference: 9.325873406851315e-15