euv-core 0.57.0

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Composable solid collider enforcing particle containment or obstacle exclusion with friction and restitution.
#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
pub struct SphSolidCollider {
    /// Underlying geometric collider primitive.
    #[get(type(copy))]
    pub(crate) shape: SphColliderShape,
    /// Tangential wall friction factor in `[0.0, 1.0]`.
    #[get(type(copy))]
    pub(crate) friction: f32,
    /// Normal bounce restitution factor in `[0.0, 1.0]`.
    #[get(type(copy))]
    pub(crate) restitution: f32,
    /// Whether this collider participates in simulation steps.
    #[get(type(copy))]
    pub(crate) enabled: bool,
}

/// Continuous particle fluid emitter spawning new liquid particles at a configured rate.
#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
pub struct SphFluidEmitter {
    /// World-space emission origin.
    #[get(type(copy))]
    pub(crate) position: Vec3,
    /// Initial emitted particle velocity in meters per second.
    #[get(type(copy))]
    pub(crate) velocity: Vec3,
    /// Nozzle disk/sphere spawn jitter radius in meters.
    #[get(type(copy))]
    pub(crate) radius: f32,
    /// Emission rate in particles per second.
    #[get(type(copy))]
    pub(crate) rate_per_sec: f32,
    /// Fractional emission accumulator carried across frames.
    #[get(type(copy))]
    pub(crate) accumulator: f32,
    /// Whether this emitter is actively spawning particles.
    #[get(type(copy))]
    pub(crate) enabled: bool,
}

/// Spherical fluid drain sink removing particles entering its capture radius.
#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
pub struct SphFluidDrain {
    /// World-space drain center.
    #[get(type(copy))]
    pub(crate) position: Vec3,
    /// Capture radius in meters within which particles are removed.
    #[get(type(copy))]
    pub(crate) radius: f32,
    /// Whether this drain is active.
    #[get(type(copy))]
    pub(crate) enabled: bool,
}

/// Declarative configuration for the 2D/3D Position-Based Fluids `SphLiquidSolver3D`.
#[derive(Clone, Copy, Data, Debug, PartialEq)]
pub struct SphSolverConfig {
    /// Simulation dimensionality (`Dim2` or `Dim3`).
    #[get(type(copy))]
    pub(crate) domain_mode: FluidDomainMode,
    /// Outer safety domain minimum corner `(min_x, min_y, min_z)`.
    #[get(type(copy))]
    pub(crate) domain_min: Vec3,
    /// Outer safety domain maximum corner `(max_x, max_y, max_z)`.
    #[get(type(copy))]
    pub(crate) domain_max: Vec3,
    /// Kernel smoothing support radius `h` in meters.
    #[get(type(copy))]
    pub(crate) smoothing_radius: f32,
    /// Target fluid rest density `rho_0` in `kg/m^3`.
    #[get(type(copy))]
    pub(crate) rest_density: f32,
    /// Mass of each individual fluid particle in kg (`<= 0.0` auto-calibrates from spacing).
    #[get(type(copy))]
    pub(crate) particle_mass: f32,
    /// Constant external body acceleration vector (default `(0, -9.80665, 0)`).
    #[get(type(copy))]
    pub(crate) gravity: Vec3,
    /// Number of PBF substeps executed per `step(dt)` call.
    #[get(type(copy))]
    pub(crate) substeps: u32,
    /// Number of Jacobi density constraint iterations per substep.
    #[get(type(copy))]
    pub(crate) solver_iterations: u32,
    /// Dimensionless Constraint Force Mixing regularization parameter.
    #[get(type(copy))]
    pub(crate) cfm_epsilon: f32,
    /// Artificial tensile-instability pressure strength `k` (`s_corr`, Macklin & Mueller).
    #[get(type(copy))]
    pub(crate) scorr_k: f32,
    /// Artificial tensile-instability reference distance fraction `delta_q / h`.
    #[get(type(copy))]
    pub(crate) scorr_dq_ratio: f32,
    /// Artificial tensile-instability kernel ratio exponent `n` (typically `4`).
    #[get(type(copy))]
    pub(crate) scorr_n: u32,
    /// XSPH artificial viscosity smoothing factor.
    #[get(type(copy))]
    pub(crate) xsph_viscosity: f32,
    /// Vorticity confinement eddy restoration strength.
    #[get(type(copy))]
    pub(crate) vorticity_epsilon: f32,
    /// Akinci cohesion surface tension gain.
    #[get(type(copy))]
    pub(crate) surface_tension_cohesion: f32,
    /// Akinci surface-curvature minimization gain.
    #[get(type(copy))]
    pub(crate) surface_tension_curvature: f32,
    /// Pre-allocated maximum active particle capacity.
    #[get(type(copy))]
    pub(crate) max_particles: usize,
    /// Power-of-two spatial hash table bucket count.
    #[get(type(copy))]
    pub(crate) hash_table_size: usize,
    /// Maximum particle speed clamp in meters per second.
    #[get(type(copy))]
    pub(crate) max_speed: f32,
    /// Deterministic PRNG seed for spawning and emission jitter.
    #[get(type(copy))]
    pub(crate) seed: u64,
}

/// Single simulated PBF/SPH liquid particle carrying kinematics, density, and Akinci surface normal.
#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq)]
pub struct SphParticle {
    /// Current world-space position in meters (`z == 0.0` in `Dim2`).
    #[get(type(copy))]
    pub(crate) position: Vec3,
    /// Current world-space velocity in meters per second (`z == 0.0` in `Dim2`).
    #[get(type(copy))]
    pub(crate) velocity: Vec3,
    /// Latest Poly6 kernel density estimate `rho_i` in `kg/m^3`.
    #[get(type(copy))]
    pub(crate) density: f32,
    /// Akinci color-field surface normal vector `n_i` pointing into liquid interior.
    #[get(type(copy))]
    pub(crate) color_normal: Vec3,
}

/// Read-only diagnostics snapshot summarizing density convergence, energy, and total mass.
#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq)]
pub struct SphSolverStats {
    /// Active simulated particle count.
    #[get(type(copy))]
    pub(crate) particle_count: usize,
    /// Mean particle density across all active particles.
    #[get(type(copy))]
    pub(crate) mean_density: f32,
    /// Maximum instantaneous particle density across all active particles.
    #[get(type(copy))]
    pub(crate) max_density: f32,
    /// Mean positive over-compression density error `mean(max(0, rho / rho_0 - 1))`.
    #[get(type(copy))]
    pub(crate) mean_compression_error: f32,
    /// Total fluid kinetic energy `0.5 * sum(m * |v|^2)` in joules.
    #[get(type(copy))]
    pub(crate) kinetic_energy: f32,
    /// Total conserved fluid mass `particle_count * particle_mass` in kg.
    #[get(type(copy))]
    pub(crate) total_mass: f32,
    /// Total completed simulation ticks.
    #[get(type(copy))]
    pub(crate) step_count: u64,
}

/// Pre-allocated scratch buffers enabling zero steady-state heap allocations during `SphLiquidSolver3D::step`.
#[derive(Clone, Data, Debug, Default, PartialEq)]
pub(crate) struct SphScratchBuffers {
    /// Predicted particle positions for the active PBF substep.
    pub(crate) predicted_positions: Vec<Vec3>,
    /// Per-particle Lagrange density multipliers `lambda_i`.
    pub(crate) lambdas: Vec<f32>,
    /// Per-particle Jacobi position correction deltas `delta_p_i`.
    pub(crate) delta_positions: Vec<Vec3>,
    /// Per-particle curl/vorticity vectors `omega_i`.
    pub(crate) vorticities: Vec<Vec3>,
    /// Per-particle XSPH velocity smoothing deltas.
    pub(crate) xsph_deltas: Vec<Vec3>,
    /// Per-particle Akinci cohesion + curvature velocity impulses.
    pub(crate) tension_deltas: Vec<Vec3>,
    /// Per-bucket counting-sort particle counts.
    pub(crate) cell_counts: Vec<u32>,
    /// Prefix-summed spatial hash bucket start offsets (`hash_table_size + 1`).
    pub(crate) cell_offsets: Vec<u32>,
    /// Mutable counting-sort write cursors per bucket.
    pub(crate) cell_cursors: Vec<u32>,
    /// Cached hash bucket index per particle.
    pub(crate) particle_hashes: Vec<u32>,
    /// Counting-sorted particle indices grouped by hash bucket.
    pub(crate) sorted_indices: Vec<u32>,
    /// Neighbor count cached for each particle.
    pub(crate) neighbor_counts: Vec<u16>,
    /// Flat neighbor index matrix (`max_particles * SPH_MAX_NEIGHBORS`).
    pub(crate) neighbor_indices: Vec<u32>,
}

/// Precomputed dimension-specific SPH kernel normalization coefficients.
#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq)]
pub(crate) struct SphKernelConstants {
    /// Smoothing support radius `h`.
    #[get(type(copy))]
    pub(crate) h: f32,
    /// Squared smoothing radius `h^2`.
    #[get(type(copy))]
    pub(crate) h2: f32,
    /// Poly6 normalization scalar.
    #[get(type(copy))]
    pub(crate) poly6_coeff: f32,
    /// Spiky gradient normalization scalar (negative).
    #[get(type(copy))]
    pub(crate) spiky_grad_coeff: f32,
    /// Poly6 gradient normalization scalar (negative) for Akinci color-field normal.
    #[get(type(copy))]
    pub(crate) poly6_grad_coeff: f32,
    /// Akinci cohesion spline normalization scalar.
    #[get(type(copy))]
    pub(crate) akinci_cohesion_coeff: f32,
    /// Poly6 kernel value at tensile reference distance `delta_q`.
    #[get(type(copy))]
    pub(crate) w_dq_inv: f32,
}

/// Production Position-Based Fluids (PBF/SPH) 2D & 3D particle liquid engine with zero steady-state allocations.
#[derive(Clone, Data, Debug, PartialEq)]
pub struct SphLiquidSolver3D {
    /// Sanitized solver physical configuration.
    #[get(type(copy))]
    pub(crate) config: SphSolverConfig,
    /// Dense active particle buffer pre-allocated to `config.max_particles`.
    pub(crate) particles: Vec<SphParticle>,
    /// Registered solid boundary and obstacle colliders.
    pub(crate) colliders: Vec<SphSolidCollider>,
    /// Registered continuous liquid emitters.
    pub(crate) emitters: Vec<SphFluidEmitter>,
    /// Registered spherical particle drains.
    pub(crate) drains: Vec<SphFluidDrain>,
    /// Pre-allocated O(N) spatial hash and solver scratch buffers.
    pub(crate) scratch: SphScratchBuffers,
    /// Deterministic PRNG state.
    #[get(type(copy))]
    pub(crate) rng_state: u64,
    /// Total completed `step` invocations.
    #[get(type(copy))]
    pub(crate) step_count: u64,
    /// Accumulated simulation time in seconds.
    #[get(type(copy))]
    pub(crate) sim_time_secs: f32,
}