AMR tagging
What it does
An immersed-boundary solver on an octree or a quadtree rebuilds its geometry after every regrid. Tagging by sampling the level set φ needs the field over whole blocks before it can decide where to refine. FOSSIL answers the tagging questions from the BVH alone, before any field exists:
- does this block, or this cell, meet the geometry?
- does the geometry come within r of it?
- if neither, is it inside or outside the solid?
- how many cells is each cell from the nearest cut cell (a tag band)?
| Query | 3-D (surface_stl_object) | 2-D (curve_object) |
|---|---|---|
| Classify boxes: cut, near, inside, outside | box_classify_many, box_classify_many_device | box_classify_many, box_classify_many_device |
| Does one box meet the geometry? | box_intersects_surface | box_intersects_curve |
| Signed tag bands on a block of cells | tag_block(x, y, z, band, tags) | tag_block(x, y, band, tags) |
One box_classify_many batch over the layer gives every code at once, with no field computed. The near ring is radius = half a box side wide; the inside boxes are the ones whose centre the fast winding number puts in the body. box_intersects_surface asks the same cut question for a single box:
$ fig_ref_amr_tagging_codes
boxes 1600: cut 179, near 222, inside 46, outside 1153
box 588 alone: box_intersects_surface = T
box 508 alone: box_intersects_surface = FExample
The NACA 0012 section (the wing cut at z = 0.5, chord from −0.25 to 0.75) in 2-D: a quadtree built level by level from box_classify_many, then tag bands on one stretched block. Tutorial chapter 9 classifies one level of blocks and tags a uniform block around the dragon; the cookbook tags the cells near a cube. Here the classification drives a whole hierarchy, and the block is stretched. The 3-D calls are the same, with lo(3, n), hi(3, n) and a z edge array.
use fossil, only : surface_stl_object, curve_object, slice_surface, BOX_CUT, BOX_NEAR, BOX_INSIDE, BOX_OUTSIDE, &
STATUS_OK
use penf, only : I4P, R8P
use vecfor, only : vector_R8P
implicit none
integer(I4P), parameter :: LEVELS = 6
type(surface_stl_object) :: wing
type(curve_object) :: section
real(R8P), allocatable :: lo(:,:), hi(:,:), next_lo(:,:), next_hi(:,:), leaf_lo(:,:), leaf_hi(:,:)
real(R8P), allocatable :: x(:), y(:)
integer(I4P), allocatable :: codes(:), leaf_code(:), tags(:,:), ftags(:,:)
real(R8P) :: side
integer(I4P) :: level, n, nl, i, j, c, status
call wing%load_from_file(file_name='naca0012-ascii.stl', guess_format=.true.)
call slice_surface(wing, 3_I4P, 0.5_R8P, section) ! the airfoil in the plane z = 0.5
lo = reshape([-0.5_R8P, -0.75_R8P], [2, 1]) ; hi = reshape([1._R8P, 0.75_R8P], [2, 1]) ! the root box; chord -0.25..0.75
allocate(leaf_lo(2, 4000), leaf_hi(2, 4000), leaf_code(4000))
nl = 0
do level = 0, LEVELS
n = size(lo, 2)
side = hi(1, 1) - lo(1, 1)
allocate(codes(n))
! refine where |phi| may be below the children's diagonal (Matsushita & Aoki): cut or near within it
call section%box_classify_many(lo=lo, hi=hi, codes=codes, radius=0.5_R8P * side * sqrt(2._R8P), status=status)
if (status /= STATUS_OK) error stop 'box_classify_many failed'
print '(A,I0,A,F8.5,A,I5,A,I4,A,I4,A,I4,A,I4)', 'level ', level, ', side ', side, ': boxes', n, ', cut', &
count(codes == BOX_CUT), ', near', count(codes == BOX_NEAR), ', inside', count(codes == BOX_INSIDE), &
', outside', count(codes == BOX_OUTSIDE)
allocate(next_lo(2, 4 * n), next_hi(2, 4 * n))
c = 0
do i = 1, n
if (level < LEVELS .and. (codes(i) == BOX_CUT .or. codes(i) == BOX_NEAR)) then
do j = 0, 3 ! the four children
c = c + 1
next_lo(:, c) = lo(:, i) + 0.5_R8P * side * [mod(j, 2), j / 2]
next_hi(:, c) = next_lo(:, c) + 0.5_R8P * side
enddo
else
nl = nl + 1 ! a leaf
leaf_lo(:, nl) = lo(:, i) ; leaf_hi(:, nl) = hi(:, i) ; leaf_code(nl) = codes(i)
endif
enddo
lo = next_lo(:, 1:c) ; hi = next_hi(:, 1:c)
deallocate(codes, next_lo, next_hi)
enddo
print '(A,I0,A,I0,A,I0)', 'leaves ', nl, ', finest cut ', count(leaf_code(1:nl) == BOX_CUT), &
', uniform grid of the finest size ', 4**LEVELS- Each level is one
box_classify_manybatch over all its boxes. A box is refined when it isBOX_CUT, orBOX_NEARwithinradius= the diagonal of its children: the criterion of Matsushita & Aoki (2021), decided from the tree before any field exists. - The rest become leaves. Their code says what they are: outside or inside (one fast-winding-number query at the centre), or, at the last level, cut and near.
! a block of 48 x 24 cells around the airfoil, stretched away from y = 0 by 1.1
x = [(-0.35_R8P + 1.2_R8P * i / 48._R8P, i = 0, 48)]
allocate(y(25)) ; y(13) = 0._R8P
do j = 1, 12
y(13 + j) = y(12 + j) + 0.006_R8P * 1.1_R8P**(j - 1)
y(13 - j) = -y(13 + j)
enddo
allocate(tags(48, 24), ftags(48, 24))
call section%tag_block(x=x, y=y, band=3, tags=tags, feature_band=2, feature_tags=ftags, status=status)
if (status /= STATUS_OK) error stop 'tag_block failed'
do i = -4, 4
print '(A,I3,A,I4)', 'tag', i, ': cells', count(tags == i)
enddo
print '(A,I0,A,I0)', 'cells holding a corner ', count(ftags == 0), ', within 2 cells of one ', count(ftags <= 2)tag_blocktakes the cell edgesx(49),y(25); the y spacing starts at 0.006 at the chord line and grows by 1.1. The tags count cells, not lengths, so a band of 3 is thin where the cells are thin.feature_band=andfeature_tags=add the second field: the Chebyshev cell distance to the cells holding a corner of the curve, a vertex where it turns by more thanfeature_angle(default 30°). The only one here is the trailing edge.
$ ref_amr_tagging
level 0, side 1.50000: boxes 1, cut 1, near 0, inside 0, outside 0
level 1, side 0.75000: boxes 4, cut 4, near 0, inside 0, outside 0
level 2, side 0.37500: boxes 16, cut 8, near 0, inside 0, outside 8
level 3, side 0.18750: boxes 32, cut 12, near 8, inside 0, outside 12
level 4, side 0.09375: boxes 80, cut 24, near 24, inside 0, outside 32
level 5, side 0.04688: boxes 192, cut 48, near 40, inside 0, outside 104
level 6, side 0.02344: boxes 352, cut 96, near 126, inside 48, outside 82
leaves 508, finest cut 96, uniform grid of the finest size 4096
tag -4: cells 112
tag -3: cells 74
tag -2: cells 86
tag -1: cells 98
tag 0: cells 112
tag 1: cells 120
tag 2: cells 128
tag 3: cells 136
tag 4: cells 286
cells holding a corner 4, within 2 cells of one 36- Six levels end with 508 leaves; the uniform grid of the finest size (side 0.0234) has 4096 cells. No box is inside before level 6: none of side 0.047 or more fits in an airfoil 0.12 thick.
- The leaves of the last level are 96 cut, 126 near and 48 inside boxes: the near ring is wider than the cut ring because the radius is the children's diagonal, 0.0166, against a box side of 0.0234.
- On the block, 112 cells are cut; the counts per ring grow outward with the stretched cells, and 286 cells beyond the band get the cap, +4. Inside, 112 cells get −4: the airfoil is more than three of the thin cells thick there.
- 4 cells hold the trailing edge, and 36 are within 2 cells of it.
Definitions
Cut.
- 3-D: the separating-axis test of a box against each triangle (Akenine-Möller 2001: the 3 box normals, the triangle normal, the 9 edge cross products). It runs in floating point on the box inflated outward by
box_tolerance, 10⁻¹² of the larger of the geometry's diagonal and coordinate magnitude. So it is conservative: it never misses a box that touches the surface, and may call a box closer than that tolerance cut. For tagging this is the safe direction: a false positive costs one refined cell, a false negative loses a cut cell. - 2-D: exact. A segment meets a closed box if an endpoint lies in it or it shares a point with a box edge, decided with the exact
orient2d. The segments come from the curve's own vertices, so no rounding enters.
- 3-D: the separating-axis test of a box against each triangle (Akenine-Möller 2001: the 3 box normals, the triangle normal, the 9 edge cross products). It runs in floating point on the box inflated outward by
Near (
BOX_NEAR, withradius> 0): not cut, but the geometry comes within the radius.- 3-D: a triangle meets the box inflated by the radius. It never misses a triangle within the radius, and may report one up to √3·radius away, beyond a box corner.
- 2-D: the segment–box distance is at most the radius (the distance in floating point, the cut test still exact).
Inside / outside. A box that meets no facet lies on one side of a closed surface, so one point query at its centre decides it, with
sign_algorithm.- The default is
SIGN_FAST_WINDING_NUMBER(issue #39): the benchmark found it the most robust on dirty geometry. - With one query per box or component, its cost does not matter.
- Pass
SIGN_PSEUDO_NORMALfor the previous behaviour. - The optional
inside(:)output also gives the centre sign of near boxes.
- The default is
Tag band. On a rectilinear block given its cell edges (stretched allowed):
- 0 on a cut cell;
- otherwise the Chebyshev cell distance d to the nearest cut cell, capped at band + 1, negative inside (−d) and positive outside (+d).
band = 4gives the cut, neighbour (±1), extension (±3) and reinitialisation (±4) rings of Hoppe et al. (2022).
How it works
- Box queries. A BVH traversal prunes every node whose box does not meet the query box inflated by the reach, and stops at the first cut. The kernels (
bvh_box_contact_oac,curve_box_contact_oac) are shared by the CPU (OpenMP over the boxes) and the device (OpenACC, afterenter_device). On an octree the 3-D query falls back to brute force. - Tag bands.
- A block that meets nothing costs one point query: ±(band + 1) everywhere.
- Otherwise each cell gets the cut test, and a breadth-first search over the 26 neighbours (8 in 2-D) gives the Chebyshev distances.
- Each connected component of uncut cells is signed by one point query. Two closed cells that share a point and are both uncut lie on the same side of a closed surface: a path between their centres through the shared point stays inside the two cells, which the surface does not meet.
On this block the breadth-first search gives every count, and four point queries give every sign: the 180 cells outside the ring, the 112 of its solid, the 32 of the hole and the 4 of the island's solid are one component each. The distances are capped at band + 1 = 3, which only the outer columns reach.
$ fig_ref_amr_tagging_bands
tag -3: cells 0
tag -2: cells 20
tag -1: cells 96
tag 0: cells 104
tag 1: cells 100
tag 2: cells 76
tag 3: cells 36
component 1: 180 cells, first cell ( 1, 1), outside
component 2: 112 cells, first cell ( 5, 4), inside
component 3: 32 cells, first cell ( 9, 7), outside
component 4: 4 cells, first cell ( 12, 9), inside
uncut cells in 4 components, one point query eachRefinement criteria from the literature
| Criterion | With FOSSIL |
|---|---|
| Refine every cut cell to the finest level (Natarajan) | box_classify_many on the cells: refine BOX_CUT |
| Refine where the smallest |φ| in a cell is below the diagonal of its children (Matsushita & Aoki 2021) | radius = child diagonal: refine BOX_CUT and BOX_NEAR |
| Keep coarse–fine boundaries away from cut cells (Giuliani et al. 2022) | tag_block: refine where |tag| ≤ the required buffer |
| Tag bands for cut cells, neighbours, extension, reinitialisation (Hoppe et al. 2022) | tag_block(..., band=4) |
The quadtree of the example uses the second criterion: every box is refined while it is cut or near within its children's diagonal, so the finest level wraps the airfoil in a ring of near cells.
The same criterion level by level on the section of the dragon: no box is inside before level 5, when the boxes become smaller than the body's cross-section; the 6 levels end with 709 leaves where the uniform grid of the finest size has 4096 cells.
Results
| Case | Result |
|---|---|
| cube-02 (2000 boxes), bunny (400 boxes) | codes = brute force over all facets; all four codes occur |
| NACA 0012, annulus with an island (3000 boxes each) | codes = brute force over all segments |
| uncut boxes, cube-02 and curves | all corners on the side of the centre |
| cube-02 on the octree | codes = BVH codes |
| touching: box on a cube face plane; 2-D box on a side | cut; 10⁻⁶ off (3-D) or one ulp off (2-D) not cut |
| device path vs CPU, 3-D and 2-D, both signs | 0 mismatches (device path run on the host) |
| tag bands: cube-02 uniform 16³ and stretched 12³, bunny 12³, blocks inside and outside | = brute-force reference (sign checked at every cell centre) |
| tag bands: NACA 0012 stretched 48 × 20, 200-gon 32 × 32 | = brute-force reference |
Refining along feature edges
Sharp edges and corners need more resolution than the smooth wall. surface%build_features followed by tag_block(..., feature_band=, feature_tags=) adds a second tag field: the Chebyshev cell distance to the nearest cell touched by a feature edge (issue #61). box_feature_many(_device) answers the same question for any list of boxes. See feature edges.
680 cells meet the cylinder, but only the 88 along the two rims meet a feature edge: the smooth side is resolved by the surface tags alone, and the rims get the extra refinement. box_feature_many on the same 4096 cells marks the same 88:
$ fig_ref_amr_tagging_features
cells 4096: cut 680, touching a feature edge 88
cells one cell from a feature edge 704
box_feature_many on the same cells: 88 touched, same cells as the tags TRefining thin walls and gaps
A wall or a gap thinner than the cell breaks an immersed boundary even where the surface is tagged. After surface%thickness and surface%build_thin, tag_block(..., thin_factor=, thin_band=, thin_tags=) adds the Chebyshev cell distance to the nearest cell that meets a facet whose local size (min of thickness and gap) is below thin_factor times the cell's largest side (issue #62). Each block compares with its own cell size, so refinement stops where the cells are fine enough. box_thin_many(_device) answers the same question for any list of boxes. See thickness and gaps.
The local size of a facet is the smaller of its thickness and its gap: 0.043 on the plate (the ray cone's bias over 0.04) and 0.064 on the box face across the gap. With thin_factor=1 the cells of side 0.1 that meet those facets are tagged, 154 of them; on a block of side 0.03 none is, because every local size is above the cell:
$ fig_ref_amr_tagging_thin
local size min(thickness, gap): plate 0.043, box face across the gap 0.064
cells of side 0.10: 1638, cut 519, thin 154
box_thin_many on the same cells: 154 touched, same cells as the tags T
cells of side 0.03: 60630, cut 10058, thin 0Limitations
- GPU not validated yet.
box_classify_many_device(3-D and 2-D) runs on the host intests-gnu. The GPU benches printbox device vs serial mismatchesfor the manual gate, which has not run yet. - Tag bands see one block. A cut cell in a neighbouring block does not count, so bands that must cross block faces need the block padded with ghost cells.
- The sign needs a closed geometry. The default fast winding number tolerates holes and gaps. Even so, the one-query-per-component rule assumes the uncut cells of a component are on one side.
- No device
tag_block. The breadth-first search runs on the CPU, once per block; the per-cell cut tests can come frombox_classify_many_device.