Skip to main content

PySphSimulation

Struct PySphSimulation 

Source
pub struct PySphSimulation { /* private fields */ }
Expand description

A 3-D weakly-compressible SPH fluid simulation.

Particles are integrated with forward Euler and use Tait’s equation of state (p = k * (ρ/ρ₀ - 1)) for pressure. Supports an optional flat floor boundary.

Implementations§

Source§

impl PySphSimulation

Source

pub fn new(config: PySphConfig) -> Self

Create an empty SPH simulation with the given configuration.

Source

pub fn add_particle(&mut self, pos: [f64; 3]) -> usize

Add a particle at pos with zero velocity. Returns the particle index.

Source

pub fn add_particle_with_velocity( &mut self, pos: [f64; 3], vel: [f64; 3], ) -> usize

Add a particle at pos with the given initial velocity.

Source

pub fn add_particle_block( &mut self, origin: [f64; 3], nx: usize, ny: usize, nz: usize, dx: f64, )

Add nx * ny * nz particles in a 3-D lattice starting at origin, with inter-particle spacing dx.

Source

pub fn particle_count(&self) -> usize

Number of particles.

Source

pub fn position(&self, i: usize) -> Option<[f64; 3]>

Position of particle i, or None if out of bounds.

Source

pub fn velocity(&self, i: usize) -> Option<[f64; 3]>

Velocity of particle i, or None if out of bounds.

Source

pub fn density(&self, i: usize) -> Option<f64>

Density of particle i, or None if out of bounds.

Source

pub fn time(&self) -> f64

Accumulated simulation time.

Source

pub fn step_count(&self) -> u64

Number of completed steps.

Source

pub fn all_positions(&self) -> Vec<f64>

Return all positions as a flat Vecf64` (length = 3 * particle_count).

Source

pub fn all_velocities(&self) -> Vec<f64>

Return all velocities as a flat Vecf64` (length = 3 * particle_count).

Source

pub fn get_density_field(&self) -> Vec<f64>

Return all densities as a Vecf64` (length = particle_count).

Source

pub fn get_pressure_field(&self) -> Vec<f64>

Return all pressures as a Vecf64`.

Source

pub fn mean_density(&self) -> f64

Average density over all particles.

Source

pub fn step(&mut self, dt: f64)

Advance the simulation by dt seconds using forward Euler integration.

Uses WCSPH:

  1. Density via poly6 kernel.
  2. Pressure from Tait equation.
  3. Forces: pressure gradient (spiky) + viscosity (Laplacian) + gravity.
  4. Euler integration.
  5. Optional floor boundary reflection.
Source§

impl PySphSimulation

Source

pub fn neighbors(&self, i: usize, r: f64) -> Option<Vec<usize>>

Return the indices of all particles within distance r of particle i.

Returns None if i is out of bounds.

Source

pub fn neighbor_count(&self, i: usize, r: f64) -> Option<usize>

Count of neighbors of particle i within radius r.

Source

pub fn closest_neighbor(&self, i: usize) -> Option<(usize, f64)>

Return the index and distance of the closest neighbor to particle i.

Returns None if i is out of bounds or there are no other particles.

Source

pub fn center_of_mass(&self) -> [f64; 3]

Return the center of mass of all particles.

Source

pub fn max_speed(&self) -> f64

Maximum particle speed.

Source

pub fn kinetic_energy(&self) -> f64

Total kinetic energy of the particle system.

Source

pub fn max_density(&self) -> f64

Maximum density among all particles.

Source

pub fn min_density(&self) -> f64

Minimum density among all particles.

Source

pub fn particle_aabb(&self) -> Option<[f64; 6]>

Return the AABB of all particle positions as [xmin, ymin, zmin, xmax, ymax, zmax].

Returns None if there are no particles.

Source

pub fn remove_below_y(&mut self, y_min: f64) -> usize

Remove all particles below a given y-coordinate.

Returns the number of particles removed.

Source§

impl PySphSimulation

Source

pub fn collect_stats(&self) -> SphStats

Collect per-step statistics into a SphStats struct.

Source§

impl PySphSimulation

Source

pub fn adaptive_h(&self, i: usize, k: usize) -> Option<f64>

Estimate an adaptive smoothing length for particle i based on the distance to its k-th nearest neighbor (default k=8).

Returns None if i is out of bounds or there are fewer than k other particles.

Source

pub fn global_adaptive_h(&self, k: usize) -> Option<f64>

Estimate a global adaptive h as the median of all per-particle adaptive h values.

Source§

impl PySphSimulation

Source

pub fn detect_surface_particles(&self, r: f64, threshold: usize) -> Vec<bool>

Detect surface particles using the color-field divergence method.

A particle i is classified as a surface particle if the number of neighbors within radius r is below threshold. Returns a Vecboolof lengthparticle_count()`.

Source

pub fn surface_particle_indices(&self, r: f64, threshold: usize) -> Vec<usize>

Indices of surface particles (using detect_surface_particles).

Source§

impl PySphSimulation

Source

pub fn add_particles(&mut self, positions: &[[f64; 3]]) -> Range<usize>

Bulk-add a list of positions, returning the range of newly added indices.

Source

pub fn set_velocity(&mut self, i: usize, vel: [f64; 3])

Set velocity of particle i. No-op if out of bounds.

Source

pub fn apply_global_impulse(&mut self, dv: [f64; 3])

Apply a uniform velocity impulse \[dvx, dvy, dvz\] to all particles.

Source

pub fn clamp_velocities(&mut self, max_speed: f64)

Clamp all particle velocities to max_speed.

Source

pub fn total_momentum(&self) -> [f64; 3]

Total linear momentum \[px, py, pz\] of all particles.

Source

pub fn density_variance(&self) -> f64

Variance of the density field (measure of compressibility error).

Trait Implementations§

Source§

impl Clone for PySphSimulation

Source§

fn clone(&self) -> PySphSimulation

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PySphSimulation

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.