11. Moving bodies
The dragon now moves. Over one time step it pitches by 10 degrees about y and moves 0.02 along x. A solver that rebuilds the search tree at every step pays for a full build each time, and that cost is what this chapter avoids. FOSSIL queries the body in its own frame, or moves a copy and refits its tree. It also computes what a moving cut-cell update needs: the cells that change state, the geometry the wall sweeps in a step, the body's inertia and the gap to another body. The chapter takes these one step at a time, one short program per step, all reading the placed dragon of chapters 2 and 3.
use fossil, only : surface_stl_object, rigid_pose_t, pose_identity, pose_from_axis_angle, SIGN_FAST_WINDING_NUMBER
use fossil_fig, only : fig_open, fig_surface, fig_points, fig_segments, fig_close
use penf, only : I4P, R8P
use vecfor, only : vector_R8P, ex_R8P, ey_R8P, ez_R8P
implicit none
integer(I4P), parameter :: n = 24 ! cell centres per direction
real(R8P), parameter :: pi = 4._R8P * atan(1._R8P)
type(surface_stl_object) :: surface, moved
type(rigid_pose_t) :: p0, p1
type(vector_R8P), allocatable :: centres(:)
real(R8P), allocatable :: d_posed(:), d_moved(:), foot_posed(:,:), foot_moved(:,:)
real(R8P) :: h
integer(I4P) :: i, j, k, c
call surface%load_from_file(file_name='dragon-placed.stl', guess_format=.true.) ! chapters 2-3
h = 1.2_R8P / n
allocate(centres(n**3), d_posed(n**3), d_moved(n**3), foot_posed(3, n**3), foot_moved(3, n**3))
c = 0
do k = 1, n ; do j = 1, n ; do i = 1, n
c = c + 1
centres(c) = (-0.6_R8P + (i - 0.5_R8P) * h) * ex_R8P + (-0.6_R8P + (j - 0.5_R8P) * h) * ey_R8P + &
(-0.6_R8P + (k - 0.5_R8P) * h) * ez_R8P
enddo ; enddo ; enddo
p0 = pose_identity() ! the rest pose
p1 = pose_from_axis_angle(axis=[0._R8P, 1._R8P, 0._R8P], angle=pi / 18._R8P, & ! 10 degrees about y
translation=[0.02_R8P, 0._R8P, 0._R8P]) ! and 0.02 along x
print '(A,3F8.4)', 'pose1: R row 1', p1%r(1, :)
print '(A,3F8.4)', ' t ', p1%t- A
rigid_pose_tholds a rotationr(3, 3)and a translationt(3), with x_world = R x_body + t.pose_identity()is the rest pose.pose_from_axis_angle(axis=, angle=, translation=)builds a pose from an axis, an angle in radians and a translation (default 0).pose_from_quaternion,pose_compose,pose_inverseandpose_interpolate(the screw motion between two poses) complete the algebra. - The 24³ cell centres span the cube [-0.6, 0.6]³, which holds the placed dragon. Every step below uses the same grid and the same step.
Querying the body at a pose
A query at a pose can be answered in two ways. One maps the points into the body frame and leaves the body alone. The other moves the body.
call surface%query_many(points=centres, distances=d_posed, closest_points=foot_posed, &
sign_algorithm=SIGN_FAST_WINDING_NUMBER, pose=p1) ! the tree stays as built
moved = surface
call moved%move(p1) ! a moved copy, tree refitted
call moved%query_many(points=centres, distances=d_moved, closest_points=foot_moved, &
sign_algorithm=SIGN_FAST_WINDING_NUMBER)
print '(A,ES9.2)', 'distances: posed - moved, max ', maxval(abs(d_posed - d_moved))
print '(A,ES9.2)', 'closest points: posed - moved, max ', maxval(abs(foot_posed - foot_moved))
print '(A,I0,A,I0)', 'centres inside at pose1: ', count(d_posed < 0._R8P), ' of ', size(centres)pose=onquery_many(also ondistance_many,is_point_insideand the device batches) maps each world point into the body frame, x̃ = Rᵀ (x − t), and queries the tree as built: one 3 × 3 product per point. The outputs (closest points, normals) come back in the world frame.move(pose)moves the facets of a copy and refits its BVH: node boxes recomputed bottom-up, topology kept, O(N) instead of a rebuild. Queries aligned with the grid (box tagging, line crossings, cut cells) work on the facets themselves and need the moved copy.
$ tut11_1_posed
pose1: R row 1 0.9848 0.0000 0.1736
t 0.0200 0.0000 0.0000
distances: posed - moved, max 2.22E-16
closest points: posed - moved, max 2.83E-15
centres inside at pose1: 441 of 13824The two ways agree to round-off: 2.2e-16 on the distances, 2.8e-15 on the closest points. At the new pose, 441 of the 13824 centres lie inside the dragon.
Cells that change state
A cut-cell solver must handle the cells that the wall uncovers during the step (fresh: solid to fluid) and the ones it covers (dead: fluid to solid). It can merge them for the step (Bayyuk et al. 1993), integrate a fresh cell from the instant it appears (Murman et al. 2003), or fill it from its neighbours (Natarajan et al. 2022).
call surface%state_changes(pose0=p0, pose1=p1, points=centres, fresh=fresh, dead=dead, &
sign_algorithm=SIGN_FAST_WINDING_NUMBER, checked=checked)
print '(A,I0,A,I0)', 'fresh (solid to fluid) ', count(fresh), ', dead (fluid to solid) ', count(dead)
print '(A,I0,A,I0,A)', 'second query needed for ', checked, ' of ', size(centres), ' centres'state_changes(pose0=, pose1=, points=, fresh=, dead=)flags the points whose side changes between two poses.- A point farther from the wall than the wall moves during the step cannot change state, so the routine skips its second query.
checkedcounts the points that needed that query. The sign algorithm defaults to the pseudo-normal, as for the point queries.
$ tut11_2_state
fresh (solid to fluid) 96, dead (fluid to solid) 98
second query needed for 992 of 13824 centres
plane y = -0.1250: fresh 9, dead 10The step makes 96 cells fresh and 98 dead. Only 992 of the 13824 centres needed the second query, about 7 %. The figure shows the plane of centres y = −0.125, which has the most changes (9 fresh, 10 dead). The fresh cells lie where the wall left and the dead ones where it arrived.
Over many steps, the fresh and dead cells form two fronts that travel with the body. The animation moves a decimated copy of the dragon through a plane of cells along a path from pose_interpolate, and calls state_changes between consecutive poses.
Swept geometry of a translation
A conservative cut-cell update needs, per cell, the change of its fluid volume over the step, and per face, the fluid aperture averaged over the step. The first test case is the section of chapter 7 translated by 0.02 along x, 0.8 of a cell.
use fossil, only : curve_object, rigid_pose_2d_t, pose_2d, swept_cells_2d_t, swept_cells_2d
use fossil_fig, only : fig_open, fig_zoom, fig_quad, fig_segments, fig_box, fig_label, fig_close
use penf, only : I4P, R8P
use vecfor, only : vector_R8P
implicit none
type(curve_object) :: section
type(rigid_pose_2d_t) :: q0, q1
type(swept_cells_2d_t) :: sw
real(R8P) :: x(49)
integer(I4P) :: i
call section%load_from_file(file_name='dragon-section.dat') ! chapter 7: the section z = 0
x = [(-0.6_R8P + 0.025_R8P * i, i = 0, 48)] ! 48 x 48 cells of side 0.025
q0 = pose_2d(angle=0._R8P)
q1 = pose_2d(angle=0._R8P, translation=[0.02_R8P, 0._R8P]) ! a pure translation, 0.8 of a cell
call swept_cells_2d(section, q0, q1, x, x, sw)
print '(A,L1,A,I0,A,I0,A,I0)', 'translation: exact ', sw%exact, ', cells swept ', count(sw%swept /= 0._R8P), &
', cleared ', count(sw%cleared), ', covered ', count(sw%covered)
print '(A,ES9.2)', ' sum of the swept areas ', sum(sw%swept)
print '(A,I0,A,ES9.2,A,2I3)', ' cells with residual > 1e-14: ', count(abs(sw%residual) > 1.e-14_R8P), &
', largest ', maxval(abs(sw%residual)), ' in cell', maxloc(abs(sw%residual))swept_cells_2d(curve, pose0, pose1, x, y, sw)(andswept_cells_3d(surface, pose0, pose1, x, y, z, sw)) are free routines with the swept geometry of a step on a fixed grid. Arigid_pose_2d_tcomes frompose_2d(angle=, translation=).sw%sweptis the change of fluid area of each cell, (α₁ − α₀)|cell|, from the exact cut cells at the two poses.sw%face_uandsw%face_vare the fluid lengths of the faces averaged over the step.sw%wallis the averaged projected wall.sw%clearedandsw%coveredflag the cells with no fluid at one pose and fluid at the other.sw%residualis the volume balance of the rigid velocity field in each cell. It is round-off when the face averages are exact and the wall does not cross itself.
- A pure translation takes the exact path (
sw%exact). The face lengths are piecewise linear in time, and they are integrated between the instants when a vertex crosses a grid line or a node crosses a segment.
call swept_cells_2d(section, q0, q1, x, x, sw, check_input=.true.) ! the section crosses itself (chapter 7)
print '(A,L1)', ' check_input: self-intersecting ', sw%self_intersecting$ tut11_3_swept
translation: exact T, cells swept 131, cleared 16, covered 16
sum of the swept areas -9.65E-18
cells with residual > 1e-14: 2, largest 5.30E-06 in cell 23 22
WARNING: swept_cells_2d: the curve crosses itself (1 segment pairs): residuals around the knots are not round-off
check_input: self-intersecting T- The step sweeps 131 cells. 16 of them are cleared (no fluid before, fluid after) and 16 are covered. The swept areas sum to −9.7e-18, since a rigid translation keeps the area.
- The residual is round-off in every cell but two, and reaches 5.3e-6 in cell (23, 22). That cell holds the knot of chapter 7, where the section crosses itself once. The swept areas come from the cut cells (Green's theorem, which counts a region by its winding number), the face averages from line crossings (parity), and the two count the knot differently. The residual is where they disagree.
check_input=.true.checks the curve first:sw%self_intersecting, and a warning on stderr. You can also callcheck_validity(check_self_intersections=.true.)once, before the time loop.
Swept geometry of a rotation
A rotation is the general case: a wall vertex moves along a circular arc, and a face's fluid length is no longer piecewise linear in time. The test case is a section of the placed dragon that does not cross itself (z = −0.2), turned by 10 degrees about the origin. Then comes a 3-D body, the unit cube turned by 15 degrees about its vertical axis.
use fossil, only : surface_stl_object, curve_object, rigid_pose_t, rigid_pose_2d_t, pose_identity, &
pose_from_axis_angle, pose_2d, slice_surface, swept_cells_2d_t, swept_cells_3d_t, &
swept_cells_2d, swept_cells_3d
use fossil_fig, only : fig_open, fig_zoom, fig_quad, fig_segments, fig_anim, fig_frame, fig_close
use penf, only : I4P, R8P
use vecfor, only : vector_R8P
implicit none
real(R8P), parameter :: pi = 4._R8P * atan(1._R8P)
type(surface_stl_object) :: surface
type(curve_object) :: section
type(swept_cells_2d_t) :: sw
real(R8P) :: x(49)
integer(I4P) :: i, n, status
integer(I4P), allocatable :: pairs(:,:)
call surface%load_from_file(file_name='dragon-placed.stl', guess_format=.true.)
call slice_surface(surface, 3_I4P, -0.2_R8P, section, merge_collinear=.true., status=status) ! z = -0.2
call section%self_intersections(pairs, n)
print '(A,I0,A,I0)', 'section z = -0.2: ', section%get_vertices_number(), ' segments, crossing pairs ', n
x = [(-0.6_R8P + 0.025_R8P * i, i = 0, 48)]
call swept_cells_2d(section, pose_2d(angle=0._R8P), pose_2d(angle=pi / 18._R8P), x, x, sw) ! 10 degrees
print '(A,L1,A,I0,A,I0)', 'rotation: exact ', sw%exact, ', cells swept ', count(sw%swept /= 0._R8P), &
', largest quadrature nodes per cell ', sw%evaluations
print '(A,ES9.2,A,ES9.2)', ' sum of the swept areas ', sum(sw%swept), ', largest |residual| ', maxval(abs(sw%residual))type(surface_stl_object) :: cube
type(swept_cells_3d_t) :: s3
type(rigid_pose_t) :: p1
real(R8P) :: g(11), c(3)
call cube%load_from_file(file_name='cube-01.stl', guess_format=.true.) ! [0, 1]^3
g = [(-0.25_R8P + 0.15_R8P * i, i = 0, 10)] ! 10^3 cells of side 0.15
c = 0.5_R8P ! the centre stays: t = c - R c
p1 = pose_from_axis_angle(axis=[0._R8P, 0._R8P, 1._R8P], angle=pi / 12._R8P)
p1 = pose_from_axis_angle(axis=[0._R8P, 0._R8P, 1._R8P], angle=pi / 12._R8P, translation=c - matmul(p1%r, c))
call swept_cells_3d(cube, pose_identity(), p1, g, g, g, s3)
print '(A,L1,A,I0,A,I0)', 'cube, 15 degrees: exact ', s3%exact, ', cells swept ', count(s3%swept /= 0._R8P), &
', cleared ', count(s3%cleared)
print '(A,ES9.2,A,ES9.2)', ' sum of the swept volumes ', sum(s3%swept), ', largest |residual| ', &
maxval(abs(s3%residual))- A rotation takes the exact path too (
exact=.true.is the default). For each face, the routine finds the instants when the combinatorics change (a vertex on the face's line, a node on a segment) as certified zeros of a function of time. Between them it applies Gauss-Legendre, doubling the nodes until the result stops changing at round-off.sw%evaluationsis the largest number of evaluations one face needed. When a wall side lies almost along a face for a while, the evaluations themselves carry amplified round-off; at 64 nodes the routine then asks only for the round-off of one evaluation (issue #86). swept_cells_3ddoes the same per grid plane: a mesh vertex on the moving plane, a mesh edge on a grid line, a grid node on a triangle.exact=.false.falls back tonqfixed Gauss nodes (default 4), andsw%error_estimatecompares them with 2 nq.
$ tut11_4_rotation
section z = -0.2: 229 segments, crossing pairs 0
rotation: exact T, cells swept 225, largest quadrature nodes per cell 466
sum of the swept areas 5.26E-17, largest |residual| 7.05E-18
cube, 15 degrees: exact T, cells swept 327, cleared 48
sum of the swept volumes 1.40E-16, largest |residual| 4.72E-17
step 1: exact T, largest |residual| 7.05E-18
step 2: exact T, largest |residual| 1.45E-17
step 3: exact T, largest |residual| 2.16E-17
step 4: exact T, largest |residual| 1.50E-17
step 5: exact T, largest |residual| 1.83E-17
step 6: exact T, largest |residual| 2.38E-17
step 7: exact T, largest |residual| 9.42E-18
step 8: exact T, largest |residual| 1.14E-17
step 9: exact T, largest |residual| 8.54E-18The section has 229 segments and no crossing pair, so both paths are exact. Turning it by 10 degrees sweeps 225 cells, and one cell needed at most 466 quadrature nodes. The swept areas sum to 5.3e-17 and the largest residual is 7.0e-18. The animation turns it through nine steps of 10 degrees, every one exact, with residuals of at most 2.4e-17. The cube turned by 15 degrees sweeps 327 cells and clears 48, with the swept volumes summing to 1.4e-16 and the largest residual 4.7e-17.
Mass and inertia
A body moved by the fluid, rather than along prescribed poses, needs its mass properties to integrate the Newton–Euler equations.
use fossil, only : surface_stl_object, rigid_pose_t, pose_from_axis_angle, inertia_to_world, STATUS_OK
use fossil_fig, only : fig_open, fig_surface, fig_segment, fig_label, fig_points, fig_close
use penf, only : I4P, R8P
use vecfor, only : vector_R8P
implicit none
real(R8P), parameter :: pi = 4._R8P * atan(1._R8P)
type(surface_stl_object) :: surface, wrap, rest
type(rigid_pose_t) :: p1
real(R8P) :: mass, volume, centre(3), inertia(3,3), inertia1(3,3), moments(3), axes(3,3)
integer(I4P) :: status
call surface%load_from_file(file_name='dragon-placed.stl', guess_format=.true.)
call surface%mass_properties(mass=mass, status=status)
print '(A,L1)', 'the placed dragon is a closed body: ', status == STATUS_OK ! chapter 2: it is not
call surface%alpha_wrap(alpha=0.06_R8P, offset=0.006_R8P, wrapped=wrap, status=status) ! a closed surrogate
print '(A,I0,A,L1)', 'alpha wrap: ', wrap%get_facets_number(), ' facets, watertight ', wrap%is_watertight()
call wrap%mass_properties(mass=mass, volume=volume, center=centre, inertia=inertia, principal_moments=moments, &
principal_axes=axes, density=1000._R8P, status=status) ! a body of density 1000
if (status /= STATUS_OK) error stop 'the wrap is not closed'
print '(A,ES11.4,A,ES11.4)', 'volume ', volume, ', mass ', mass
print '(A,3F8.4)', 'centre of mass ', centre
print '(A,3ES11.4)', 'principal moments', momentsmass_properties(mass=, volume=, center=, inertia=, principal_moments=, principal_axes=, density=, about=, status=)gives the mass, the centre of mass, the inertia tensor about the centre (or aboutabout) and its principal moments (ascending) and axes. The integrals are exact; see mass properties.- It needs a closed body, and the repaired dragon is not one (chapter 2), so the call returns a status other than
STATUS_OK. Its alpha wrap is closed by construction and serves as the surrogate. It is offset 0.006 outside the surface, and a coarseralphamakes it fatter, so the inertia is the wrap's.
p1 = pose_from_axis_angle(axis=[0._R8P, 1._R8P, 0._R8P], angle=pi / 18._R8P, translation=[0.02_R8P, 0._R8P, 0._R8P])
call wrap%move(p1)
call wrap%mass_properties(inertia=inertia1, density=1000._R8P)
print '(A,ES9.2)', 'moved wrap against inertia_to_world, relative ', &
maxval(abs(inertia1 - inertia_to_world(inertia, p1))) / maxval(abs(inertia))inertia_to_world(inertia, pose)gives the tensor at a pose, R I Rᵀ, without moving anything.
$ tut11_5_inertia
the placed dragon is a closed body: F
alpha wrap: 2128 facets, watertight T
volume 6.8960E-02, mass 6.8960E+01
centre of mass -0.0192 -0.0337 -0.0958
principal moments 2.3413E+00 4.1358E+00 5.7928E+00
moved wrap against inertia_to_world, relative 3.08E-16The wrap has 2128 facets and is watertight. At density 1000 its mass is 68.96, its centre of mass is (−0.0192, −0.0337, −0.0958), and its principal moments are 2.34, 4.14 and 5.79. The moved wrap's tensor equals inertia_to_world to 3.1e-16 relative.
Distance between bodies
Two bodies in the same flow need the gap between them: for contact, for a lubrication correction when the gap falls to a few cells, and for collision.
use fossil, only : surface_stl_object, rigid_pose_t, pose_from_axis_angle
use fossil_fig, only : fig_open, fig_surface, fig_segment, fig_points, fig_label, fig_close
use penf, only : I4P, R8P
use vecfor, only : vector_R8P
implicit none
real(R8P), parameter :: pi = 4._R8P * atan(1._R8P)
type(surface_stl_object) :: surface, moved, box
type(rigid_pose_t) :: p1
real(R8P) :: gap, gap1, ps(3), po(3)
integer(I4P) :: fa, fb
logical :: hit
call surface%load_from_file(file_name='dragon-placed.stl', guess_format=.true.)
p1 = pose_from_axis_angle(axis=[0._R8P, 1._R8P, 0._R8P], angle=pi / 18._R8P, translation=[0.02_R8P, 0._R8P, 0._R8P])
call box%load_from_file(file_name='cube-01.stl', guess_format=.true.) ! a unit cube beside the dragon
call box%translate(x=0.7_R8P, y=-0.5_R8P, z=-0.5_R8P)
call box%analyze
call surface%distance_to(box, distance=gap, point_self=ps, point_other=po, facet_self=fa, facet_other=fb, &
intersecting=hit, pose_self=p1)
print '(A,F8.5,A,I0,A,I0,A,L1)', 'gap to the cube ', gap, ' (facets ', fa, ', ', fb, '), touching ', hit
print '(A,3F8.4,A,3F8.4)', 'closest pair', ps, ' -', podistance_to(other, distance=, point_self=, point_other=, facet_self=, facet_other=, intersecting=)is the least distance over all pairs of facets of the two bodies, with the pair that realizes it. It returns 0 andintersectingwhen the bodies touch or cross, decided exactly. The two trees are descended together, best first, and the result is bit-identical to a loop over all pairs.pose_self=andpose_other=place the bodies wheremovewould put them, using the trees as built.
moved = surface
call moved%move(p1)
call moved%distance_to(box, distance=gap1)
print '(A,L1)', 'pose_self = moved copy, bit for bit: ', gap == gap1$ tut11_6_gap
gap to the cube 0.50539 (facets 1077, 48), touching F
closest pair 0.1946 -0.2762 -0.2950 - 0.7000 -0.2762 -0.2950
pose_self = moved copy, bit for bit: TThe gap is 0.50539, between facet 1077 of the dragon and facet 48 of the cube. The posed call and the moved copy give the same gap bit for bit. proximity_pairs(cutoff=) lists the pairs of bodies closer than a cutoff, and chapter 14 follows two bodies to the instant they touch. See body–body distance.
What you learned
pose= queries a moving body without touching its tree. move refits a copy for the queries aligned with the grid. state_changes finds fresh and dead cells. swept_cells_2d and swept_cells_3d give the exact swept geometry of a translation or a rotation. mass_properties and inertia_to_world give the inertia, and distance_to gives the gap between two bodies. Reference: Moving bodies, Body–body distance.