Skip to content

Add 2D/3D particle surface reconstruction and callback - #1329

Draft
svchb wants to merge 13 commits into
drafty_draftfrom
surface-reconstruction-pr1293
Draft

svchb wants to merge 13 commits into
drafty_draftfrom
surface-reconstruction-pr1293

Conversation

@svchb

@svchb svchb commented Sep 25, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Add a reusable 2D/3D SurfaceReconstruction API and dimension-aware SurfaceMesh: area-corrected marching-squares contours in 2D and volume-corrected marching-cubes triangle surfaces in 3D, with closed boundary/structure geometry support.
  • Add a multi-fluid SurfaceReconstructionCallback with VTP/PVD output, optional PLY, surface statistics, and vertex interpolation. Float32 CUDA states are transferred to CPU for reconstruction; GPU neighborhood-search data is transferred only when interpolation requires it.
  • Add a CPU/GPU example, reference-value and independent correctness tests, and opt-in production-scale benchmarks grouped in benchmark/surface_reconstruction/.
  • Document the algorithm and its limitations with generated 2D and 3D illustrations embedded in the manual page. Test comments explain the non-obvious numerical and GPU-cache invariants.
  • Share dimension-specialized grid, particle-stencil accounting, domain-distance, lattice-lookup and enclosure helpers. Retain the measured faster 3D scalar stencil scan and the dimension-specific deposition/filter traversals and topology algorithms.
  • Reuse seven-region triangle projection and geometry measure primitives across packing, imported geometry and reconstruction. Share VTK filename/PVD lifecycle and add a reusable CPU system-state transfer that deliberately drops GPU neighborhood searches. Remove particle-ID helpers and a component-volume wrapper with no production callers.
  • Extend the existing geometry closure interface to planar and spatial SurfaceMesh, with invalid-index checks and closure diagnostics. Share solution/surface save-schedule validation and postprocess/surface JSON/CSV persistence while keeping endpoint policies and numerical reconstruction separate.

Planar reconstruction supports adaptive/fixed grids, polygon and tracked-lattice boundaries, holes/islands, area-equivalent sparse circles, offline/system/solution entry points, interpolated vertex quantities, VTK lines and PLY edges. A two-component tank_size selects 2D; use ndims=2 for reusable adaptive reconstruction. In 2D the existing volume-tolerance option controls area; statistics expose area/perimeter aliases and the callback writes area/perimeter series. The new 2D example runs to its default final time successfully.

The 3D production default uses classic marching cubes. A manufactured saddle motivates topology-aware contouring, but MarchingCubes.jl 0.1.11 can produce nonmanifold results for low-amplitude inputs; switching that default needs further work. Planar contours use a Float64 bilinear asymptotic decider and relative zero-node tie handling. The documentation states the numerical and topology limitations.

The final mathematical-review follow-up clarified that particle-lattice boundary meshes pass through outermost particle centers, which can lie h/2 inside a physical wall; boundary_clearance is measured from that mesh, not inferred from the physical wall. The manual also distinguishes the exact-zero/tie convention and 2D gross versus 3D net cavity statistics. These are documentation/docstring fixes only: the default, reconstruction arithmetic, and recorded 2D/3D output hashes are unchanged.

SplashSurf comparison: why the sparse image has two shapes

The sparse input is a 343-particle block plus one isolated particle at (0.9, 0.9, 0.9). Consequently, both reconstructions show a large block and a small, separate droplet—two components, not duplicate renders. Our small shape is a volume-equivalent sparse-fallback sphere; SplashSurf directly contours the isolated particle. The comparison comment now includes the full threshold table and diagnostic images for both ~1M and ~10M synthetic waves, including a higher-amplitude four-wave variant. The full 3D surfaces nearly coincide; the cross-sections, crest close-ups and distance histograms are more informative than side-by-side full-scene renders.

Stack

Depends on #1293 (drafty_draft). This branch starts at its head and contains only surface-reconstruction commits. It does not include the later #1325–#1327 FSI stack. The example uses the positional wall-model constructor supported on #1293.

Validation on this base

  • Bounds-checked CPU tests: core 328, numerical references 66, API 204, independent correctness 724, planar 107, callback 104, plus packing signed-distance 66, winding 9, particle VTK 24; Aqua passes. Existing 3D reference results are unchanged. Added Float32/Float64 stencil-boundary and inference checks for the shared helpers.
  • Offline reconstruction contracts: historical frames of systems with particle buffers are rejected (saved solutions lack per-frame activity); raw SummationDensity inputs document cache synchronization with an opt-in refresh_caches recompute; planar closure validation counts indexed connectivity like the analysis. Default outputs are unchanged.
  • Follow-up contract fixes: refresh_caches performs the complete time-aware update (a density-only recompute silently dropped Shepard correction, measured +1.35% target drift) and requires the state time; buffered solutions require the final simulation state to be saved (save_end=false leaves it unsaved); non-integer frame indices are normalized before the guard. Callback 104→109; suites and Aqua green.
  • Shared closure/callback follow-up: geometry 16,502, solution saving 70, postprocess 17, and the surface suites pass locally; the final geometry/surface run used bounds checking and included Aqua.
  • Callback performance was checked against the pre-extraction parent. Removed eager error-message and disabled-output path allocations introduced during extraction; allocation counts now match the parent. Constructor and full writer timings are comparable in alternating tests. Reconstruction allocations and fingerprints also match; pinned 8-thread old/new frame medians were 15.87/15.47 ms (2D ~100k particles) and 212.45/212.87 ms (3D 1.1M particles), within baseline variation.
  • CPU examples 3/3 each; the default 2D example also completes to t=0.2. CUDA 3D example 5/5, moving-particle GPU cache/interpolation test 31/31, and Float32 planar callback/offline/output checks 11/11 on an RTX 5090.
  • All four default workloads pass from benchmark/surface_reconstruction/; fixed-bucket 3D reconstruction fingerprints agree at 1 and 24 threads and match the pre-2D results, including a case above the 40,000-face reduction threshold.
  • JuliaFormatter and git diff --check pass; the workstation-specific benchmark baseline is excluded.
  • Same-process old/new helper and frame comparisons preserve mesh hashes, isovalues and enclosed measures in both dimensions. On a pinned 8-thread workstation, sharing removed planar allocation overhead: ~100k-particle frames measured 20.24→14.92 ms (48.71→19.76 MiB allocated). The 1.1M-particle 3D case was comparable at 201.58→198.76 ms with identical allocations. These are workload-specific measurements, not portable performance thresholds.
  • Closest-triangle kernels match the previous packing and BVH values byte-for-byte on 6,000 sampled queries; alternating old/new timings show comparable performance. Spatial/planar VTP and PVD files are byte-identical to the pre-refactor versions; 2D/3D frame hashes, isovalues and measures match as well. Solid-angle numerator formulas and caller summation orders remain separate intentionally.
  • The surface manual page renders the generated illustrations, including the new planar contour, without page-specific warnings. Full documentation/doctests are blocked by an existing Drafty draft #1293 bug: its BoundaryModelDummyParticles(initial_condition; fluid_system=...) forwards the unsupported correction keyword to a positional constructor using separate correction keywords. This breaks existing examples and their doctest setups (e.g. hydrostatic_water_column_2d.jl). A warning-tolerant full render confirmed the failures are outside the new surface page; the parent fix belongs in Drafty draft #1293.
  • The latest full Ubuntu/Julia 1.10 CI run reached 25,290 passing checks, zero assertion failures, and nine errors in the parent's correction-API migration: boundary constructors and existing WCSPH/EDAC correction tests still pass the removed correction keyword. The new closure/callback checks and reconstruction suites passed in that run. The relevant failing constructor/tests are unchanged relative to Drafty draft #1293.

@svchb svchb changed the title Add 3D particle surface reconstruction and callback Add 2D/3D particle surface reconstruction and callback Sep 25, 2026
@svchb

svchb commented Sep 26, 2026 •

Copy link
Copy Markdown
Collaborator Author

SplashSurf comparison — 1M and 10M particles (synthetic frames, 20 CPU threads)

Both methods receive the same particles for each case. Julia uses the production reconstruction configuration (voxel size h/2, Gaussian sigma 0.9h, cold-start volume correction to 0.1%). SplashSurf 0.14.1 uses reconstruction_pipeline with particle radius h/2, smoothing length 2.0 radii, cube size 1.0 radius (h/2), and an isosurface-threshold sweep 0.2, 0.3125, 0.4, 0.5, 0.6. The table shows 0.6, closest to the volume target among these tested thresholds. Different smoothing kernels mean equal voxel size does not imply equivalent surfaces.

Wave fixtures have mean water height 0.18 m in a 2 m × 1 m footprint. “Smooth” has amplitude 0.05 m and two waves across 2 m; “choppy” has amplitude 0.10 m and four waves. The 1M fixtures contain about 1.12M particles (h=0.006885 m); the 10M fixtures contain about 10.01M (h=0.00331 m). These are generated snapshots, not simulated frames.

Times exclude compilation, mesh file I/O, and quality auditing: ours is the minimum of warmed reconstruction repeats with initial isovalue reset to 0.5; SplashSurf is one pipeline run per candidate threshold. Both use 20 CPU threads on the same workstation. Sub-millisecond small-case times are close to timer noise; the large cases are more informative.

Input Method Reconstruction Vertices / triangles Volume error Surface area (m²)
Block, 343 particles Ours 0.001 s 1,152 / 2,300 −0.062% 2.510
SplashSurf t=0.6 0.001 s 1,542 / 3,080 −2.869% 2.667
Block + isolated droplet, 344 particles Ours 0.002 s 1,634 / 3,260 −0.0004% 2.560
SplashSurf t=0.6 0.001 s 1,572 / 3,136 −2.975% 2.705
Smooth wave, 1,120,850 particles Ours 0.188 s 463,290 / 926,576 +0.065% 5.095
SplashSurf t=0.6 0.411 s 519,076 / 1,038,148 −0.102% 5.190
Choppy wave, 1,118,965 particles Ours 0.289 s 560,756 / 1,121,508 +0.092% 5.656
SplashSurf t=0.6 0.445 s 581,820 / 1,163,636 +0.040% 5.746
Smooth wave, 10,012,810 particles Ours 1.301 s 1,999,857 / 3,999,710 −0.029% 5.120
SplashSurf t=0.6 3.660 s 2,211,350 / 4,422,696 −0.047% 5.206
Choppy wave, 10,017,944 particles Ours 2.440 s 2,432,922 / 4,865,840 +0.045% 5.706
SplashSurf t=0.6 3.851 s 2,490,390 / 4,980,776 +0.021% 5.775

Full results: threshold-by-threshold CSV · metrics and methodology JSON. Volume error is computed from the finished, oriented mesh and the relevant retained particle-volume target. The previous −200% figure for our choppy fixtures came from an export script mistakenly measuring raw, inward-ordered triangles; the VTP files were correctly oriented and their independently recomputed volumes agree with the reconstruction statistics.

Small fixtures

343-particle block, particles and both reconstructed meshes

Block plus isolated droplet, particles and both reconstructed meshes

The sparse fixture intentionally contains two shapes: the block and a single isolated particle at (0.9, 0.9, 0.9). Both methods produce two disconnected components. Our small blue sphere is the volume-equivalent fallback for that particle; SplashSurf contours it directly (orange).

Higher-amplitude wave — 1M particles

Four-wave, 1.12M-particle comparison: surface, mesh-to-mesh distance, profile, crest close-up and histogram

Same higher-amplitude wave — 10M particles

Four-wave, 10.02M-particle comparison at finer resolution with matched cross-section and crest close-up

The full 3D surfaces nearly overlap; the wave-profile cross-section and crest close-up expose the small geometric offsets, while the nearest-vertex distance histogram measures them. Each figure labels its physical-distance colorbar; do not compare colors across resolutions without reading that scale. The histograms query sampled vertices against the full other mesh, not a sparse approximation.

Lower-amplitude baseline waves (1M and 10M) — images

Smooth wave, 1.12M particles

Smooth wave, 10.01M particles

Limits: SplashSurf's threshold was selected post hoc from a five-point sweep; our correction certifies ≤0.1% only on successfully returned meshes. The small cases' topology was audited (two components for the sparse case; no boundary/nonmanifold edges). Large-case complete edge-manifoldness has not been independently audited here; at t=0.6 the choppy 10M SplashSurf mesh contains two triangles below the 2×10⁻¹⁴ m² doubled-area degeneracy cutoff. SplashSurf has no counterpart for our boundary constraints or enclosed-particle exclusion, so the boundary-only fixture is in the CSV without a SplashSurf row.

Figures, CSV and JSON are hosted on assets/splashsurf-comparison-1329, outside this PR's code diff. Comparison scripts and raw fixtures are in /tmp/opencode/splash_compare/ on the benchmarking workstation.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant