7. Curves in 2-D
A 2-D run of the same case needs the body as a curve in a plane, not as triangles. slice_surface cuts the dragon by a plane, and the section is a curve_object with the same queries as a surface, in two dimensions. This chapter slices, queries the section and builds its distance field.
Slicing the surface
use fossil, only : surface_stl_object, curve_object, slice_surface, CURVE_VALID
use penf, only : I4P, R8P
use vecfor, only : vector_R8P, ex_R8P, ey_R8P
implicit none
type(surface_stl_object) :: surface
type(curve_object) :: section
integer(I4P) :: open_loops, status, crossings, s, u
integer(I4P), allocatable :: pairs(:,:)
type(vector_R8P) :: a, b, c
character(80) :: caption
call surface%load_from_file(file_name='dragon-placed.stl', guess_format=.true.)
call slice_surface(surface, 3_I4P, 0._R8P, section, merge_collinear=.true., open_loops=open_loops, status=status)
print '(A,I0,A,I0,A,I0,A)', 'section z=0: ', section%get_loops_number(), ' loops, ', section%get_vertices_number(), &
' vertices, ', open_loops, ' open'
print '(A,F9.6,A,F9.6,A,L1)', 'area ', section%get_area(), ' perimeter ', section%get_perimeter(), &
' valid ', section%check_validity() == CURVE_VALID
call section%self_intersections(pairs, crossings)
print '(A,I0)', 'self-intersecting segment pairs ', crossings
call section%save_into_file(file_name='dragon-section.dat')slice_surface(surface, normal_axis, level, curve)cuts by the plane normal to axisnormal_axis(1, 2, 3) at coordinatelevel. The curve lives in that plane with in-plane coordinates (u, v), the other two axes in cyclic order: fornormal_axis = 3they are (x, y). The crossings are exact constructions, with no tolerance: a vertex on the plane counts as above it.merge_collinear=.true.drops vertices between collinear segments.open_loopscounts the chains that did not close, as happens on a surface with holes; such a curve can be closed byrepair(tolerance=).- The solid is on the left of every segment: outer loops run counter-clockwise, holes clockwise.
check_validity()returnsCURVE_VALIDor the first defect found (CURVE_OPEN_LOOP,CURVE_WRONG_ORIENTATION, ...). get_areaandget_perimeterare the 2-D metrics.save_into_filewrites the ASCII loop format, oneu vper line, read back byload_from_file. Chapters 10 and 11 readdragon-section.dat.- A curve can also be built from arrays:
adopt_loops(u=, v=, loop_sizes=).
$ tut07_a_slice
section z=0: 2 loops, 152 vertices, 0 open
area 0.072609 perimeter 1.885961 valid T
self-intersecting segment pairs 1
crossing segments 11 and 13The section has two closed loops, 152 vertices and an area of 0.072609.
Each level gives another curve. The second program slices the dragon by 13 planes, from z = -0.30 to 0.30:
$ tut07_b_planes
z = -0.30: loops 8, area 0.1675, 0 open
z = -0.25: loops 6, area 0.1553, 0 open
z = -0.20: loops 4, area 0.1262, 0 open
z = -0.15: loops 4, area 0.0976, 0 open
z = -0.10: loops 5, area 0.0775, 0 open
z = -0.05: loops 4, area 0.0719, 0 open
z = 0.00: loops 2, area 0.0726, 0 open
z = 0.05: loops 2, area 0.0709, 0 open
z = 0.10: loops 4, area 0.0928, 0 open
z = 0.15: loops 4, area 0.0390, 0 open
z = 0.20: loops 1, area 0.0515, 0 open
z = 0.25: loops 2, area 0.0297, 0 open
z = 0.30: loops 2, area 0.0035, 0 openNo section is open: sanitize left no boundary edge (chapter 2). The areas, summed times the spacing 0.05, give 0.053, close to the volume 0.054283 of chapter 3.
A fold of the section
check_validity() does not look for crossings by default; check_validity(check_self_intersections=.true.) does (CURVE_SELF_INTERSECTING), and self_intersections(pairs, n) returns the pairs of segments that cross, touch or overlap, decided exactly. The section at z = 0 is valid and still crosses itself once, at a fold of the non-manifold dragon: segment 12 doubles back along segment 11, and 13 crosses 11.
The distance queries do not mind the fold; the cut cells and the swept geometry do (chapter 11 shows how the swept-geometry residual detects it).
Distance to a curve
use fossil, only : curve_object
use penf, only : I4P, R8P
use vecfor, only : vector_R8P, ex_R8P, ey_R8P
implicit none
real(R8P), parameter :: PI = 4._R8P * atan(1._R8P)
type(curve_object) :: section
real(R8P) :: cp(2), d, ring(2, 12), dr(12), uv(2), cpk(2)
integer(I4P) :: segment, vertex, k, s, u
type(vector_R8P) :: a, b
call section%load_from_file(file_name='dragon-section.dat')
call section%compute_distance(p=[0._R8P, 0._R8P], distance=d, closest_point=cp, closest_segment=segment, &
closest_vertex=vertex)
print '(A,F10.6,A,2F10.6,A,I0,A,I0)', 'd(0,0) ', d, ' closest ', cp, ' segment ', segment, ' vertex ', vertex
do k = 1, 12 ! an ellipse about the origin
ring(:, k) = [0.12_R8P * cos(2._R8P * PI * k / 12._R8P), 0.4_R8P * sin(2._R8P * PI * k / 12._R8P)]
enddo
call section%distance_many(points=ring, distances=dr)
print '(A,I0,A,F9.5,A,F9.5)', 'ellipse: inside ', count(dr < 0._R8P), ' of 12, d from ', minval(dr), ' to ', maxval(dr)- Points are
p = [u, v].compute_distancereturns the signed distance, negative inside. Unlike the surface queries, curve distances are signed and not squared by default (is_signed=.false.for the unsigned one). It comes with the closest point,closest_segment, andclosest_vertex(0 when the closest point is inside a segment). distance(p)andis_point_inside(p)are the short forms.distance_many(points=, distances=)is the batch, with the points as columns of a(2, n)array.
$ tut07_c_query
d(0,0) -0.015933 closest 0.015329 -0.004344 segment 20 vertex 0
ellipse: inside 3 of 12, d from -0.01445 to 0.09997The origin is inside, 0.015933 from the wall, its closest point inside segment 20. Three of the twelve points of the ellipse are inside too.
A 2-D field
use fossil, only : curve_object, distance_field_object, STATUS_OK
use penf, only : I4P, R8P
implicit none
type(curve_object) :: section
type(distance_field_object) :: field
real(R8P) :: x(81)
integer(I4P) :: i, status
call section%load_from_file(file_name='dragon-section.dat')
x = [(-0.6_R8P + 0.015_R8P * i, i = 0, 80)]
call field%initialize(x=x, y=x) ! no z: a 2-D field
call field%compute(curve=section, status=status)
if (status /= STATUS_OK) error stop '2-D field failed'
print '(A,I0,A,I0)', '2-D field: band cells ', field%get_band_cells(), ', inside ', count(field%phi < 0._R8P)
print '(A,F8.5,A,F8.5)', 'inside area ', count(field%phi < 0._R8P) * 0.015_R8P**2, ', curve area ', section%get_area()initialize(x=, y=)withoutzmakes a 2-D field;compute(curve=)runs the same band, sign and sweep stages as in chapter 6.
$ tut07_d_field
2-D field: band cells 465, inside 327
inside area 0.07358, curve area 0.07261The 2-D field has 327 nodes inside on a spacing of 0.015, an area of 0.0736, against 0.0726 for the curve.
The colour saturates beyond about 0.1 from the wall; the field itself runs to the edges of the domain.
What you learned
slice_surface turns a surface into a curve_object; curves have the signed distance, the inside test and the distance field of surfaces, in (u, v) coordinates, and self_intersections finds where a section folds. Reference: 2-D geometry (curves), curve_object.
Next: 8. Immersed-boundary queries.