Skip to content
Projects

Overview

simgeometry is the geometry of the simulation codes, nothing physical: shapes with an inside test, a signed distance and a normal; the first crossing of a particle’s step with them; the cut-cell data of a grid; and the uniform grids and the MPI process grid that splits them. Every array query takes component-major points pp of shape (ndim, n) and runs on NumPy or, through cunumpy, on CuPy; the hot loops are compiled Pyccel kernels.

Each equation on these pages is declared on the code that implements it, as a method card, and checked by the tests: 20 closed forms (the level sets of the shapes, the Boolean and affine level sets, the analytic crossings, the segment-triangle test) against SymPy rules, five discretisations against their order of convergence. The pages of the section list the cards module by module; this one shows how they fit together.

A shape is a set SS with a membership test (contains_points) and, where it has an analytic description, a level set ϕ\phi: negative inside, zero on the boundary, the exact Euclidean signed distance where level_set_exact is true and a bound on it otherwise. An inverted shape is the complement, with the sign of ϕ\phi flipped (shape conventions). The level sets are the standard exact distances of boxes, segments, capped cones and tori:

Rectangle

classsource

Represent a rectangular geometry object.

inside
p∈S  ⟺  l≤p<u(f=0),p∈S  ⟺  ϕ(p)≤0(f>0)p \in S \iff l \le p < u \quad (f = 0), \qquad p \in S \iff \phi(p) \le 0 \quad (f > 0)
level set
ϕ(p)=∣max⁡(d,0)∣+min⁡(max⁡kdk,0)−f,d=∣p−c∣−(h−f)\phi(p) = \big|\max(d, 0)\big| + \min\big(\max_k d_k, 0\big) - f, \qquad d = |p - c| - (h - f)
Assumes
  • axis-aligned, centre cc, half sides hh; ∣⋅∣|\cdot| and max⁡(⋅,0)\max(\cdot, 0) of dd componentwise
  • the fillet f≤min⁡khkf \le \min_k h_k rounds the corners: the box shrunk by ff, widened by a disc of radius ff
  • without a fillet the inside test is the half-open box of BoundingBox.contains
  • ϕ\phi is the exact signed distance, negative inside
References

2D shapes also give their boundary as counter-clockwise loops (outlines, second order in the spacing for curved boundaries) and as boundary panels for boundary-element solvers.

A particle pusher tests the whole step a→ba \to b of every marker, not only its end, so a fast particle cannot pass through a thin plate. First crossings are analytic for balls, boxes, polygons, slanted tubes and their affine images, and exact for triangle meshes (segment-triangle test in the cells of a grid, compiled) and Fourier surfaces (their triangles, then Newton steps onto the surface); other shapes are sampled along the step and refined by Illinois steps on the level set, or bisected on membership without one.

crossings

functionsource

Return the first boundary crossing of each segment start[:, i] -> end[:, i].

ball
t=−β∓β2−αγα,α=d⋅d,β=(a−c)⋅d,γ=∣a−c∣2−r2t = \frac{-\beta \mp \sqrt{\beta^2 - \alpha\gamma}}{\alpha}, \qquad \alpha = d\cdot d, \quad \beta = (a - c)\cdot d, \quad \gamma = |a - c|^2 - r^2
slab
tk±=lk−akdk,uk−akdk,t0=max⁡kmin⁡(tk−,tk+),t1=min⁡kmax⁡(tk−,tk+)t_k^\pm = \frac{l_k - a_k}{d_k}, \frac{u_k - a_k}{d_k}, \qquad t_0 = \max_k \min(t_k^-, t_k^+), \quad t_1 = \min_k \max(t_k^-, t_k^+)
first
t={t0t0∈(10−12,1] (entering)t1t1∈(10−12,1] (leaving),none if t0≥t1t = \begin{cases} t_0 & t_0 \in (10^{-12}, 1] \text{ (entering)} \\ t_1 & t_1 \in (10^{-12}, 1] \text{ (leaving)} \end{cases}, \qquad \text{none if } t_0 \ge t_1
polygon
t=w×ed×e,u=w×dd×e,w=vj−a,e=vi−vj,a×b=axby−aybxt = \frac{w \times e}{d \times e}, \quad u = \frac{w \times d}{d \times e}, \qquad w = v_j - a, \quad e = v_i - v_j, \quad a \times b = a_x b_y - a_y b_x
transformed
a′=M−1(a−o),d′=M−1d,n=M−Tn′∣M−Tn′∣a' = M^{-1}(a - o), \quad d' = M^{-1} d, \qquad n = \frac{M^{-T} n'}{|M^{-T} n'|}
bisection
[tlo,thi]∋ks first membership change,halved m times,t=12(tlo+thi)[t_\mathrm{lo}, t_\mathrm{hi}] \ni \tfrac{k}{s} \text{ first membership change}, \quad \text{halved } m \text{ times}, \quad t = \tfrac12 (t_\mathrm{lo} + t_\mathrm{hi})
illinois
t=tafb−tbfafb−fa,f=ϕ(a+td),fa←fa/2 if a is kept twice in a rowt = \frac{t_a f_b - t_b f_a}{f_b - f_a}, \qquad f = \phi(a + t d), \quad f_a \leftarrow f_a / 2 \text{ if } a \text{ is kept twice in a row}
normal
n=∇ϕ∣∇ϕ∣,∂kϕ≈ϕ(p+δek)−ϕ(p−δek)2δn = \frac{\nabla\phi}{|\nabla\phi|}, \qquad \partial_k \phi \approx \frac{\phi(p + \delta e_k) - \phi(p - \delta e_k)}{2\delta}
Order
1
Assumes
  • each step a→ba \to b, d=b−ad = b - a; the first crossing t∈(0,1]t \in (0, 1], entering or leaving; crossings at t≤10−12t \le 10^{-12} are ignored, at t=1t = 1 counted; grazing contacts and zero steps are not crossings
  • analytic (exact): balls, boxes and rounded boxes (the hull of the intervals of the inner box widened along one axis and the discs, edge cylinders and corner balls of radius ff), polygonal 2D shapes (∣d×e∣>10−14∣d∣∣e∣|d \times e| > 10^{-14}|d||e|, u∈[−10−12,1+10−12]u \in [-10^{-12}, 1 + 10^{-12}]), slanted tubes and affine images of them, triangle meshes (Möller–Trumbore in the cells of a grid) and Fourier surfaces whose hits were refined onto the surface
  • sampled otherwise: membership at s+1s + 1 points t=k/st = k/s (samples s=64s = 64), then Illinois steps on the level set to 2−m2^{-m} if it changes sign, else mm bisections on membership (iterations m=40m = 40)
  • normals point out of the region where contains is true: radial for balls, ∓sign⁡(dk) ek\mp\operatorname{sign}(d_k)\,e_k for boxes, (ey,−ex)/∣e∣(e_y, -e_x)/|e| for counter-clockwise edges; the level-set gradient (δ=10−7\delta = 10^{-7} times the extent) or 64 (2D) or 128 (3D) membership probes for sampled shapes; flipped for inverted shapes
  • a GeometryCollection: each object on its own path, the earliest crossing wins, the first listed on a tie
  • the order is that of the bisection on membership in the final bracket width 2−m2^{-m}; the analytic paths are exact and Illinois converges superlinearly (about 1.44)
References

The compiled swept absorption of 2D markers runs the same segment-edge test against packed polygon objects in one pass and returns the object each marker hit first.

Edge crossings locate where the edges of a grid enter a shape (Illinois on the level set, bisection otherwise); cut edges collect them per grid edge for embedded-boundary finite differences. Cut-cell fractions give the inside part of every cell by midpoint quadrature, and of every edge and face of a Yee grid, with the trace of the surface, for conformal field solvers.

A grid has uniform nodes per axis, the first and last on the bounds (one spacing short of the upper bound on a periodic axis). The domain decomposition splits it over a process grid with row-major ranks: one near-even split of the nodes, shared by the field arrays (mpiarray) and the markers, whose owner follows from the subdomain edges. The particle boundaries of the box kill, reflect, wrap or reinject markers per axis.

Lengths are in the units of the caller. Shapes and grids are axis-aligned unless an axis or a matrix is given. Normals have unit length and point out of the region where contains is true, so they flip for inverted shapes. Fractions along a step lie in (0,1](0, 1]: a crossing at the very start (t≤10−12t \le 10^{-12}) is not reported, one exactly at the end is. Boundary conventions of the inside tests differ: Cuboid, Line and the balls are closed; Rectangle, Polygon, Triangle and Aperture3D use the half-open bounding box l≤p<ul \le p < u.

  • No winding-number test: self-intersecting polygons follow the even-odd rule.
  • The absorption kernel reports the object only, not the hit point, edge or normal, and tests the end points of long (wrapped) steps only.
  • Sampled crossings miss features thinner than a step over samples; Boolean, section and collection level sets are bounds, so signed_distance falls back to a sampled mesh for them.
  • Aperture3D and function objects have no level set and no analytic crossing; a section of a 3D shape only bounds the in-plane distance.
  • Halo exchange, ghost cells and field layouts are mpiarray’s; simgeometry hands over the process grid and the halo width.
  • The process-grid search does not balance the axes and never fails.
  • The cut-cell fractions have no single order: the Yee fractions are exact for planes and second order or better for curved surfaces, the midpoint fractions are multiples of n−dn^{-d}.

Besides the checks of the cards, the tests compare every vectorised inside test with the scalar one, the compiled kernels (Pyccel, NumPy, CUDA in emulation) with each other and with the shapes, the level sets with exact distances and the Boolean bounds, the analytic crossings with dense sampling, the Illinois roots with bisection (fewer than 10 level-set evaluations per edge against 50 membership tests), the Yee fractions with planes (exact) and with a circle and a sphere (area, perimeter and volume), and the decomposition with brute-force owners and with runs on 4 and 6 MPI ranks.