Fluid Simulation¶
Overview¶
NovaPhy implements Position Based Fluids (PBF) following Macklin & Muller 2013, with two-way rigid-fluid coupling via the Akinci et al. 2012 boundary particle method.
PBF Solver¶
The PBF pipeline:
- Predict — Apply gravity, predict particle positions
- Neighbor Search — Spatial hash grid for O(1) average neighbor lookup
- Density Constraint — Iteratively project particles to satisfy rest density
- Corrections — XSPH viscosity, tensile instability correction, vorticity confinement
- Update — Finalize positions and velocities
Creating a Fluid Simulation¶
import numpy as np
import novaphy
builder = novaphy.ModelBuilder()
builder.add_ground_plane(y=0.0)
# Define a fluid block
fluid_block = novaphy.FluidBlockDef()
fluid_block.lower = np.array([0.0, 0.0, 0.0])
fluid_block.upper = np.array([1.0, 1.0, 1.0])
fluid_block.particle_spacing = 0.05
# Configure PBF solver
settings = novaphy.PBFSettings()
settings.kernel_radius = 4.0 * 0.05 # Kernel radius = 4x spacing
settings.solver_iterations = 4
# Create fluid world
fluid_world = novaphy.FluidWorld(builder.build(), [fluid_block], pbf_settings=settings)
for _ in range(500):
fluid_world.step(1.0 / 120.0)
SPH Kernels¶
NovaPhy provides standard SPH kernel functions:
- Poly6 kernel — for density estimation
- Spiky kernel gradient — for pressure forces
- Viscosity kernel Laplacian — for viscosity forces
Rigid-Fluid Coupling (Akinci)¶
Two-way coupling between rigid bodies and fluid particles:
- Rigid body surfaces are sampled with boundary particles
- Boundary particles contribute to fluid density computation
- Fluid pressure forces are fed back to rigid bodies
# Coupling happens automatically when FluidWorld contains both
# rigid bodies (from ModelBuilder) and fluid blocks
fluid_world = novaphy.FluidWorld(builder.build(), [fluid_block], pbf_settings=settings)
Demos¶
| Demo | Description |
|---|---|
demo_dam_break.py |
Rectangular fluid block collapse |
demo_fluid_box.py |
~8000 particles sloshing in a moving box |
demo_ball_in_water.py |
Ball dropping into water |
demo_fluid_coupling.py |
Boxes and spheres splashing into water |