9. AMR tagging
An adaptive grid decides where to refine before any field exists. FOSSIL answers the tagging questions from the BVH alone: does a box meet the wall, does the wall come near it, and if neither, is it inside or outside. This chapter classifies one level of blocks around the placed dragon of chapters 2–3, builds an octree from the same call, tags the cells of a block by their distance to the wall, does the same on the section of chapter 7, and adds two more fields: the cells near a sharp edge and the cells near a wall thinner than a cell.
Classifying blocks
The domain [−0.6, 0.6]³ is cut into 8³ blocks of side 0.15. One batch call classifies them all.
use fossil, only : surface_stl_object, BOX_CUT, BOX_NEAR, BOX_INSIDE, BOX_OUTSIDE
use penf, only : I4P, R8P
implicit none
integer(I4P), parameter :: nb = 8 ! blocks per direction
type(surface_stl_object) :: surface
real(R8P) :: lo(3, nb**3), hi(3, nb**3), hb
integer(I4P) :: codes(nb**3), i, j, k, b, u
call surface%load_from_file(file_name='dragon-placed.stl', guess_format=.true.)
hb = 1.2_R8P / nb ! the domain [-0.6, 0.6]^3 cut into 8^3 blocks of side 0.15
b = 0
do k = 1, nb ; do j = 1, nb ; do i = 1, nb
b = b + 1
lo(:, b) = -0.6_R8P + [i - 1, j - 1, k - 1] * hb
hi(:, b) = lo(:, b) + hb
enddo ; enddo ; enddo
call surface%box_classify_many(lo=lo, hi=hi, codes=codes) ! cut, or inside / outside
print '(A,I0,A,I0,A,I0)', 'radius 0: cut ', count(codes == BOX_CUT), ', inside ', count(codes == BOX_INSIDE), &
', outside ', count(codes == BOX_OUTSIDE)
call surface%box_classify_many(lo=lo, hi=hi, codes=codes, radius=0.05_R8P) ! and near within 0.05
print '(A,I0,A,I0,A,I0,A,I0)', 'radius 0.05: cut ', count(codes == BOX_CUT), ', near ', count(codes == BOX_NEAR), &
', inside ', count(codes == BOX_INSIDE), ', outside ', count(codes == BOX_OUTSIDE)box_classify_many(lo=, hi=, codes=)classifies n boxes given their cornerslo(3, n)andhi(3, n):BOX_CUT: the box meets the surface, by a separating-axis test against each candidate triangle, run on the box inflated by 1e-12 of the problem size. The test never misses a contact; a box closer than that may count as cut.BOX_NEAR(only withradius=> 0): not cut, but a triangle comes withinradiusof the box.BOX_INSIDE/BOX_OUTSIDE: neither; one point query at the box centre decides the side.
- The centre query uses
sign_algorithm=, whose default here isSIGN_FAST_WINDING_NUMBER, not the pseudo-normal of the point queries.inside(n)optionally returns the centre side of every box that is not cut, near boxes included.
b = findloc(codes, BOX_CUT, dim=1) ! the first cut block
print '(A,I0,A,L1)', 'block ', b, ' meets the dragon: ', surface%box_intersects_surface(lo(:, b), hi(:, b))
print '(A,L1)', 'its lower half meets it: ', &
surface%box_intersects_surface(lo(:, b), [hi(1, b), hi(2, b), lo(3, b) + 0.5_R8P * hb])box_intersects_surface(lo, hi)answers for one box, with the same test. Here the first cut block meets the dragon and its lower half does not.
$ tut09_a_blocks
radius 0: cut 87, inside 0, outside 425
radius 0.05: cut 87, near 62, inside 0, outside 363
block 75 meets the dragon: T
its lower half meets it: F- 87 of the 512 blocks meet the dragon. With
radius=0.05, 62 of the 425 uncut blocks become near; the cut count does not change. No block of side 0.15 fits inside the thin dragon, so none is inside.
Refining level by level
An octree is the same call applied level by level: classify every box of a level in one batch, split the boxes that must be refined into eight children, and keep the others as leaves.
use fossil, only : surface_stl_object, BOX_CUT, BOX_NEAR, BOX_INSIDE, BOX_OUTSIDE
use penf, only : I4P, R8P
implicit none
integer(I4P), parameter :: LEVELS = 5 ! finest side 1.2 / 2^5 = 0.0375
type(surface_stl_object) :: surface
real(R8P), allocatable :: lo(:,:), hi(:,:), next_lo(:,:), next_hi(:,:)
integer(I4P), allocatable :: codes(:)
integer(I4P) :: level, n, c, i, j, leaves, leaves_in, leaves_out
real(R8P) :: side
call surface%load_from_file(file_name='dragon-placed.stl', guess_format=.true.)
lo = reshape([-0.6_R8P, -0.6_R8P, -0.6_R8P], [3, 1]) ; hi = -lo ! the root box, the domain [-0.6, 0.6]^3
leaves = 0 ; leaves_in = 0 ; leaves_out = 0
print '(A)', 'level side boxes cut near inside outside'
do level = 0, LEVELS
n = size(lo, 2) ; side = hi(1, 1) - lo(1, 1)
allocate(codes(n))
call surface%box_classify_many(lo=lo, hi=hi, codes=codes, radius=0.5_R8P * side * sqrt(3._R8P)) ! child diagonal
print '(I5,F8.4,I10,4I7)', level, side, n, count(codes == BOX_CUT), count(codes == BOX_NEAR), &
count(codes == BOX_INSIDE), count(codes == BOX_OUTSIDE)
leaves_in = leaves_in + count(codes == BOX_INSIDE) ; leaves_out = leaves_out + count(codes == BOX_OUTSIDE)
if (level == LEVELS) exit
allocate(next_lo(3, 8 * n), next_hi(3, 8 * n))
c = 0
do i = 1, n
if (codes(i) /= BOX_CUT .and. codes(i) /= BOX_NEAR) cycle ! inside or outside: a leaf
do j = 0, 7 ! the eight children
c = c + 1
next_lo(:, c) = lo(:, i) + 0.5_R8P * side * [mod(j, 2), mod(j / 2, 2), j / 4]
next_hi(:, c) = next_lo(:, c) + 0.5_R8P * side
enddo
enddo
leaves = leaves + n - c / 8
lo = next_lo(:, 1:c) ; hi = next_hi(:, 1:c)
deallocate(codes, next_lo, next_hi)
enddo
leaves = leaves + n
print '(A,I0,A,I0,A,I0,A,I0)', 'leaves ', leaves, ' (inside ', leaves_in, ', outside ', leaves_out, &
'); uniform grid of the finest side: ', 8**LEVELS- A box is refined when it is
BOX_CUT, orBOX_NEARwithin the diagonal of its children,radius= side·√3/2: where the level set may be smaller than the next level's cells (the criterion of Matsushita & Aoki 2021), decided before any level set exists. - Inside and outside boxes become leaves at once: the wall does not come near them, so nothing finer is needed there.
$ tut09_b_octree
level side boxes cut near inside outside
0 1.2000 1 1 0 0 0
1 0.6000 8 8 0 0 0
2 0.3000 64 26 37 0 1
3 0.1500 504 87 195 0 222
4 0.0750 2256 374 571 0 1311
5 0.0375 7560 1689 2230 13 3628
leaves 9094 (inside 13, outside 5162); uniform grid of the finest side: 32768- Every box of levels 0 and 1 is cut. From level 2 on, the outside boxes drop out; the near ring is wider than the cut ring at every level, because the radius is the children's diagonal and not the box's.
- The first inside boxes appear at level 5, side 0.0375: 13 of them. Coarser boxes do not fit in the dragon.
- Six levels end with 9094 leaves against 32768 cells of the uniform grid of the finest side. The 1689 cut boxes of level 5 are the cells of a uniform 32³ grid that meet the dragon, the count the next section finds cell by cell.
Tag bands on a block
A solver that refines block by block needs more than cut or not: how far each cell is from the wall, in cells. One block of 32³ cells over the whole domain:
use fossil, only : surface_stl_object, curve_object, slice_surface, STATUS_OK
use penf, only : I4P, R8P
implicit none
integer(I4P), parameter :: nc = 32 ! cells per direction of the tagged block
integer(I4P), parameter :: kc = 16 ! the layer compared with the 2-D twin
type(surface_stl_object) :: surface
type(curve_object) :: section
real(R8P) :: x(nc + 1), zc
integer(I4P) :: tags(nc, nc, nc), tags2(nc, nc), i, status
call surface%load_from_file(file_name='dragon-placed.stl', guess_format=.true.)
x = [(-0.6_R8P + 1.2_R8P * i / nc, i = 0, nc)] ! cell edges of one 32^3 block over the domain
call surface%tag_block(x=x, y=x, z=x, band=4, tags=tags, status=status)
if (status /= STATUS_OK) error stop 'tag_block failed'
print '(A)', 'tag -5 -4 -3 -2 -1 0 1 2 3 4 5'
print '(A,11I7)', '3-D ', (count(tags == i), i = -5, 5)tag_block(x=, y=, z=, band=, tags=)takes the cell edges of a rectilinear block (stretched allowed) and returns one tag per cell: 0 on a cut cell, otherwise the Chebyshev cell distance to the nearest cut cell, capped atband + 1, negative inside and positive outside.band = 4gives the cut, neighbour, extension and reinitialisation rings an IB solver tags.- Each connected group of uncut cells is signed by one point query, with the same default sign algorithm.
- The block sees only itself: a cut cell in a neighbouring block does not count, so a band that must cross block faces needs the block padded with ghost cells.
The tags are in the first line of the run below: 1689 cut cells, the count of the octree's finest level. Inside, no cell is more than 2 cells from a cut cell (7 cells at −2): the dragon is at most a few cells thick at this spacing. Outside, the 19531 cells beyond the band of 4 get the cap, +5. A cell of the layer is cut if the wall crosses it anywhere in its height, so the cream ring follows the wall over the whole layer, not only the yellow section at its mid-plane.
The section in 2-D
The 2-D twin works on a curve_object: the same calls without the z coordinates. Here the dragon is cut at the mid-plane of layer 16 and the section tagged on the same 32 × 32 cells.
zc = 0.5_R8P * (x(kc) + x(kc + 1)) ! the mid-plane of layer 16
call slice_surface(surface, 3_I4P, zc, section, status=status)
call section%tag_block(x=x, y=x, band=4, tags=tags2, status=status) ! the same 32 x 32 cells, in the plane
print '(A,11I7)', '2-D ', (count(tags2 == i), i = -5, 5)
print '(A,I0,A,I0)', 'layer 16: cut cells in 3-D ', count(tags(:, :, kc) == 0), ', on its mid-plane section ', &
count(tags2 == 0)
print '(A,I0)', 'cells cut in 2-D but not in the layer: ', count(tags2 == 0 .and. tags(:, :, kc) /= 0)curve_object%tag_block(x=, y=, band=, tags=)is the 2-D twin, with 8-connected components; the cut test is exact (orient2don the curve's own vertices).box_classify_manyandbox_intersects_curveare the twins of the block calls above, withlo(2, n)andhi(2, n).
$ tut09_c_tags
tag -5 -4 -3 -2 -1 0 1 2 3 4 5
3-D 0 0 0 7 352 1689 2217 2602 3009 3361 19531
2-D 0 0 0 0 22 58 66 68 74 82 654
layer 16: cut cells in 3-D 80, on its mid-plane section 58
cells cut in 2-D but not in the layer: 0- On its mid-plane the layer has 58 cut cells, against 80 for the layer in 3-D, and every cell the section cuts is cut in the layer: the 3-D tags see the wall over the cell's height, the 2-D tags only in the plane.
- The section's inside is thinner still: 22 cells at −1 and none deeper.
Feature edges and thin walls
Two parts of a body need cells finer than the rest of the wall: sharp edges, where the level set has a kink, and walls or gaps thinner than a cell, where it cannot resolve both sides. tag_block gives one more tag field for each, on the same block.
use fossil, only : surface_stl_object, STATUS_OK
use penf, only : I4P, R8P
implicit none
integer(I4P), parameter :: nc = 32 ! cells per direction of the tagged block
type(surface_stl_object) :: surface
real(R8P), allocatable :: thickness(:), gap(:)
real(R8P) :: x(nc + 1)
integer(I4P) :: tags(nc, nc, nc), ftags(nc, nc, nc), ttags(nc, nc, nc), i, j, k, status, u
call surface%load_from_file(file_name='dragon-placed.stl', guess_format=.true.)
x = [(-0.6_R8P + 1.2_R8P * i / nc, i = 0, nc)]
call surface%build_features(angle=60._R8P, status=status) ! creases above 60 degrees, boundary, non-manifold
if (status /= STATUS_OK) error stop 'build_features failed'
call surface%thickness(thickness=thickness, gap=gap, rays=9) ! per facet: shape diameter and gap, 9 rays
call surface%build_thin(thickness=thickness, gap=gap, status=status)
if (status /= STATUS_OK) error stop 'build_thin failed'
call surface%tag_block(x=x, y=x, z=x, band=4, tags=tags, feature_band=2, feature_tags=ftags, &
thin_factor=1._R8P, thin_band=2, thin_tags=ttags, status=status)
if (status /= STATUS_OK) error stop 'tag_block failed'
print '(A,I0,A,I0)', 'facets ', surface%get_facets_number(), ', thinner than a cell side (0.0375): ', &
count(min(thickness, gap) < x(2) - x(1))
print '(A)', 'cells at 0 1 2 beyond'
print '(A,4I7)', 'cut ', count(tags == 0), count(abs(tags) == 1), count(abs(tags) == 2), count(abs(tags) > 2)
print '(A,4I7)', 'feature ', (count(ftags == i), i = 0, 3)
print '(A,4I7)', 'thin ', (count(ttags == i), i = 0, 3)
print '(A,I0,A,I0,A,I0)', 'cut cells: on a feature edge ', count(tags == 0 .and. ftags == 0), &
', on a thin wall only ', count(tags == 0 .and. ftags /= 0 .and. ttags == 0), &
', neither ', count(tags == 0 .and. ftags /= 0 .and. ttags /= 0)build_features(angle=)stores the feature edges (creases sharper thanangle, default 30°; boundary and non-manifold edges) in a tree of their own. Thenfeature_band=andfeature_tags=give the Chebyshev cell distance to the nearest cell a feature edge touches, capped atfeature_band + 1. On the scanned dragon most of the wall has creases sharper than 30°; 60° keeps the strongest, on the horns, the claws and the tail.thickness(thickness=, gap=)measures each facet (thickness and gaps),build_thinstores the local size min(thickness, gap). Thenthin_factor=,thin_band=andthin_tags=give the cell distance to the nearest cell that meets a facet thinner thanthin_factortimes the cell's largest side.- Both stores are explicit, because the queries take the surface
intent(in);analyzedrops them.
$ tut09_d_features
facets 6586, thinner than a cell side (0.0375): 2468
cells at 0 1 2 beyond
cut 1689 2569 2609 25901
feature 867 2888 2778 26235
thin 707 2512 2910 26639
cut cells: on a feature edge 867, on a thin wall only 56, neither 766- 2468 of the 6586 facets are thinner than a cell side, 0.0375.
- Of the 1689 cut cells, 867 hold a feature edge, 56 more meet a thin wall, and 766 are neither: refining on the three fields together puts the finest cells on half the wall.
What you learned
box_classify_many sorts boxes into cut, near, inside and outside from the tree alone, and builds an octree one level per batch; tag_block gives signed distance-to-cut bands on a block of cells, in 3-D and on a curve, plus feature and thin-wall fields after build_features and build_thin. Reference: AMR tagging, feature edges, thickness and gaps. The device batch, box_classify_many_device, is in chapter 16.
Next: 10. Cut cells.