Skip to content
Projects

Overview

patchwork is a block-structured adaptive mesh in one or two dimensions: nested rectangular patches of cells, each finer by the refinement ratio rr (2 by default), each patch an mpiarray array on its own ranks. On the composite mesh, the leaf cells that no finer patch covers, it solves elliptic problems for a particle-in-cell or fluid code: a node-centred Poisson equation for the potential and a cell-centred operator with variable coefficients, in the Cartesian (x,y)(x, y) or the axisymmetric (r,z)(r, z) plane. Around them are the pieces a time-dependent code needs: halo exchange, restriction, regridding, refluxing, and the deposit and gather of markers, with an edge space whose current deposit conserves the charge exactly.

Each equation on these pages is declared on the code that implements it, as a method card, and checked by the tests: ten closed forms against SymPy rules and four discretisations against their order of convergence on manufactured solutions. The pages of this section list the cards module by module; this one shows how they fit together.

  1. Hierarchy. Level LL has the spacing hL=h0/rLh_L = h_0 / r^L; patch boxes are aligned to the level below and covered by it. In (r,z)(r, z) a cell is a ring with its exact volume and face areas. Markers are deposited with cloud-in-cell weights (or B-splines of degree 2 or 3, splines=, folded at the patch edges) in the finest patch that holds them, and the copies of a node are summed by the deposit accumulation.
  2. Halos and restriction. A halo takes the values of the patches of its level, else the bilinear interpolation of the level below; restriction gives a coarse cell the volume-weighted mean of its fine cells. How the values travel between the ranks is the subject of the MPI communication guide.
  3. Regridding. Cells are tagged where a field changes by more than a threshold, covered with boxes by the Berger-Rigoutsos clustering, and the fields moved to the new hierarchy, conservatively where they must be.
  4. Nodal Poisson. −∇2ϕ=f-\nabla^2\phi = f on the nodes of the leaf cells: assembled cell by cell, with the hanging nodes of coarse-fine interfaces eliminated by their interpolation, and Shortley-Weller rows at embedded surfaces. With mass="consistent" the same assembly gives the Q1 finite elements instead of the five-point stencil (mass matrices):

NodalPoisson

classsource

Solve −∇2ϕ=f-\nabla^2\phi = f on the nodes of a patch hierarchy.

problem
−∇2ϕ=f in Ω,ϕ=ϕD or ∂nϕ=g on faces,ϕ=Uk in bodies Bk-\nabla^2 \phi = f \ \text{in } \Omega, \qquad \phi = \phi_D \ \text{or} \ \partial_n \phi = g \ \text{on faces}, \qquad \phi = U_k \ \text{in bodies } B_k
stencil
App+=g, Aqq+=g, Apq=Aqp−=gper leaf-cell edge pq along a,g=h⊥2ha (2D), 1h (1D)A_{pp} \mathrel{+}= g,\ A_{qq} \mathrel{+}= g,\ A_{pq} = A_{qp} \mathrel{-}= g \quad \text{per leaf-cell edge } pq \text{ along } a, \qquad g = \frac{h_\perp}{2 h_a} \ \text{(2D)}, \ \frac{1}{h} \ \text{(1D)}
uniform
App=2hyhx+2hxhy,Ap,x±=−hyhx,Ap,y±=−hxhyA_{pp} = \frac{2h_y}{h_x} + \frac{2h_x}{h_y}, \qquad A_{p,x\pm} = -\frac{h_y}{h_x}, \qquad A_{p,y\pm} = -\frac{h_x}{h_y}
reduced
(T⊤AT)FF ϕF=T⊤Vf−(T⊤AT)FD ϕD+T⊤bN+WU,V=diag(Vp)(T^\top A T)_{FF}\,\phi_F = T^\top V f - (T^\top A T)_{FD}\,\phi_D + T^\top b_N + W U, \qquad V = \mathrm{diag}(V_p)
charge
T⊤Vf→T⊤q(solve(charge=q))T^\top V f \to T^\top q \quad (\texttt{solve(charge=q)})
consistent
A=T⊤G⊤M1G T,V→M0(consistent mass: the Q1 elements; lumped M1 and V: the stencil above)A = T^\top G^\top M_1 G\, T, \qquad V \to M_0 \quad \text{(consistent mass: the Q1 elements; lumped } M_1 \text{ and } V \text{: the stencil above)}
neumann
bN,p+=12∣e∣ gfor each end p of a boundary edge eb_{N,p} \mathrel{+}= \tfrac12 |e|\, g \quad \text{for each end } p \text{ of a boundary edge } e
cut edge
App+=gθ,bp+=gθ Uk,θ=max⁡(θfound,θmin)A_{pp} \mathrel{+}= \frac{g}{\theta}, \qquad b_p \mathrel{+}= \frac{g}{\theta}\, U_k, \qquad \theta = \max(\theta_\mathrm{found}, \theta_\mathrm{min})
shortley weller
App=hxhy∑axes(2a−(a−+a+)+2a+(a−+a+)),Ap,±=− hxhy 2a±(a−+a+)A_{pp} = h_x h_y \sum_\mathrm{axes} \Big(\frac{2}{a_-(a_- + a_+)} + \frac{2}{a_+(a_- + a_+)}\Big), \qquad A_{p,\pm} = -\,h_x h_y\,\frac{2}{a_\pm(a_- + a_+)}
cylindrical
g=2π rˉ h⊥2ha,rˉ=12(re+rK),Vp=∑K∋p2π 12(rp+rK) ∣K∣4,Ap,r±=−Vp(2a±(a−+a+)±1rp(a−+a+))g = 2\pi\,\bar r\,\frac{h_\perp}{2 h_a}, \quad \bar r = \tfrac12(r_e + r_K), \qquad V_p = \sum_{K \ni p} 2\pi\,\tfrac12(r_p + r_K)\,\frac{|K|}{4}, \qquad A_{p,r\pm} = -V_p\Big(\frac{2}{a_\pm(a_- + a_+)} \pm \frac{1}{r_p (a_- + a_+)}\Big)
reaction
(A+T⊤diag(Vc) T) ϕ=…(A + T^\top \mathrm{diag}(V c)\, T)\,\phi = \ldots
Order
2
Assumes
  • unlisted faces are grounded; no permittivity: for a charge pass ρ/ε0\rho / \varepsilon_0
  • lumped and consistent mass share one assembly from the edge mass blocks (patchwork.spaces.mass); the consistent one takes no bodies and keeps the lumped Neumann load
  • Cartesian control volumes and conductances by default, also on an (r,z)(r, z) hierarchy; metric='cylindrical' weights each conductance by 2π2\pi times the mean radius of its half cell and each control volume by 2π2\pi times that of its quarter cells (the finite volumes of −1r∂r(r∂rϕ)−∂z2ϕ-\frac1r\partial_r(r\partial_r\phi) - \partial_z^2\phi, lumped), with the 1r ∂rϕ\frac{1}{r}\,\partial_r\phi term in the Shortley-Weller rows
  • a reaction c ϕc\,\phi (set_reaction) adds the lumped Vp cpV_p\, c_p to the diagonal, e.g. a Newton Jacobian
  • the Shortley-Weller rows at regular cut nodes (four equal cells, not hanging), the diagonal-only cut elsewhere; they make the matrix unsymmetric
  • θ\theta by 48 bisections of inside, or the body's fraction; θmin\theta_\mathrm{min} = min_fraction (10−310^{-3})
  • the order is in the volume-weighted L2L_2 norm; in the maximum norm about 1.86 on nested patches (an O(1)O(1) truncation error on the interface nodes)
References
Built from
  1. Cell operator. −∇⋅(a∇u)+c u=f-\nabla\cdot(a\nabla u) + c\,u = f on the leaf cells, assembled from an energy so that it stays symmetric (or, with conservative coarse-fine faces, conservative face by face), with the curl-curl of an azimuthal vector potential in (r,z)(r, z) and fixed or floating bodies.
  2. Edge space. The lowest-order (Whitney) edge interpolant on the leaf cells, whose lumped (consistent) mass matrix gives back the nodal operator’s five-point stencil (its Q1 stiffness), and the current deposit along straight paths that conserves the charge exactly:

deposit_paths

functionsource

The current of charges moving along straight paths, integrated over the paths: (n1,).

pieces
jlower+=q Δxhx(1−ηmid),jupper+=q Δxhx ηmid,likewise for the y-edgesj_\mathrm{lower} \mathrel{+}= q\,\frac{\Delta x}{h_x}(1 - \eta_\mathrm{mid}), \qquad j_\mathrm{upper} \mathrel{+}= q\,\frac{\Delta x}{h_x}\,\eta_\mathrm{mid}, \qquad \text{likewise for the } y\text{-edges}
continuity
(GT)⊤j=T⊤(Q(x1)−Q(x0))(G T)^\top j = T^\top\big(Q(\mathbf{x}_1) - Q(\mathbf{x}_0)\big)
product
a1b1−a0b0=Δa bˉ+aˉ Δbfor linear a(t),b(t)a_1 b_1 - a_0 b_0 = \Delta a\,\bar b + \bar a\,\Delta b \quad \text{for linear } a(t), b(t)
work
j⊤e=∑kqk∫x0x1E⋅dlj^\top e = \sum_k q_k \int_{\mathbf{x}_0}^{\mathbf{x}_1} \mathbf{E}\cdot d\mathbf{l}
Assumes
  • straight paths, clipped to the leaf cells of every patch and integrated on the patch's block by mpifield's compiled path deposit, which cuts them at its cell faces and integrates the edge functions (linear along a piece in 2D, quadratic in 3D) exactly; _deposit_pieces (Simpson's rule on the finest-lattice pieces) is the NumPy reference
  • QQ the bilinear node deposit (CompositeNodes.deposit); conserved across patch edges and periodic seams to round-off
  • pieces outside the domain deposit nothing
References
Built from
  • deposit
  • mpifield.edges.EdgeWeighting
  1. Face space. The lowest-order Raviart–Thomas faces (fluxes), with the divergence to the leaf cells and, in 3D, the curl from the edges: C G = 0 and D C = 0 exactly, also across patch faces.
  2. Curl–curl. ∇×(ν∇×A)+βA=J\nabla\times(\nu\nabla\times\mathbf{A}) + \beta\mathbf{A} = \mathbf{J} on the edges, lumped or consistent, solved by LU or by conjugate gradients with hypre’s AMS.
  3. Refluxing. For an explicit, conservative time step, the coarse cells next to finer levels take the fine fluxes instead of their own (Berger and Colella), with or without subcycling.

The assembled operators go to a solver backend: a direct LU on every rank, fast transforms on a uniform mesh, or the distributed solve with PETSc’s Krylov methods and algebraic multigrid. The legacy cell Poisson solve is kept for its users.

The operators carry no physical constants: for a charge density pass ρ/ε0\rho / \varepsilon_0 as the source, and μ0\mu_0 goes into the coefficients of the cell operator. Fields are per unit length out of the plane in (x,y)(x, y), totals over the ring in (r,z)(r, z), and per unit area on a line. A node-centred deposit holds charges, not densities. Face conditions are named x_lower, x_upper, y_lower, y_upper and inner (the edges of a domain that does not fill its rectangle); an unlisted face is grounded.

  • No time integration or particle push: the fluxes of refluxing and the markers are the caller’s.
  • The cell operator has the LU only, on every rank, and is 1D and 2D only.
  • No per-axis or per-level refinement ratios, no non-rectangular patches.
  • The edge space is Cartesian only (1D, 2D, 3D); the nodal Poisson solve uses Cartesian control volumes also on an (r,z)(r, z) hierarchy.
  • The Shortley-Weller rows are used only at regular nodes; elsewhere, and for the cell operator, an embedded surface is a two-point flux (about first order in the maximum norm).

Besides the checks of the cards, the tests reproduce linear potentials exactly across patches, compare the FFT and PETSc backends with SuperLU, solve a current loop and a conducting column in a solenoid against their closed forms in (r,z)(r, z), conserve the integral to round-off through refluxing, restriction and regridding, and conserve the charge of 500 random paths per patch layout through the edge deposit.