Skip to main content

ParticleSoA

Struct ParticleSoA 

Source
pub struct ParticleSoA {
    pub x: Vec<f64>,
    pub y: Vec<f64>,
    pub z: Vec<f64>,
    pub vx: Vec<f64>,
    pub vy: Vec<f64>,
    pub vz: Vec<f64>,
    pub fx: Vec<f64>,
    pub fy: Vec<f64>,
    pub fz: Vec<f64>,
    pub mass: Vec<f64>,
    /* private fields */
}
Expand description

Cache-friendly particle storage using Structure-of-Arrays layout.

Positions, velocities, and forces are stored in separate contiguous arrays for optimal vectorization and cache utilization. This layout is ideal for tight inner loops that only touch one or two fields at a time (e.g., force accumulation only needs x, y, z, fx, fy, fz – velocities stay cold).

§Examples

use oxiphysics_core::cache_layout::ParticleSoA;

let mut soa = ParticleSoA::new(0);
soa.push([1.0, 2.0, 3.0], [0.1, 0.2, 0.3], [0.0; 3], 1.0);
assert_eq!(soa.len(), 1);
assert_eq!(soa.get_position(0).expect("valid index"), [1.0, 2.0, 3.0]);

Fields§

§x: Vec<f64>

X positions.

§y: Vec<f64>

Y positions.

§z: Vec<f64>

Z positions.

§vx: Vec<f64>

X velocities.

§vy: Vec<f64>

Y velocities.

§vz: Vec<f64>

Z velocities.

§fx: Vec<f64>

X forces.

§fy: Vec<f64>

Y forces.

§fz: Vec<f64>

Z forces.

§mass: Vec<f64>

Masses.

Implementations§

Source§

impl ParticleSoA

Source

pub fn new(capacity: usize) -> Self

Allocate a new, empty container pre-allocated for capacity particles.

Source

pub fn len(&self) -> usize

Number of particles currently stored.

Source

pub fn is_empty(&self) -> bool

Whether the container holds zero particles.

Source

pub fn capacity(&self) -> usize

Current allocated capacity (in particles).

Source

pub fn push(&mut self, pos: [f64; 3], vel: [f64; 3], force: [f64; 3], mass: f64)

Append a single particle.

Source

pub fn get_position(&self, i: usize) -> Result<[f64; 3], CacheLayoutError>

Return the position of particle i.

Returns Err if i >= len().

Source

pub fn set_position( &mut self, i: usize, pos: [f64; 3], ) -> Result<(), CacheLayoutError>

Set the position of particle i.

Returns Err if i >= len().

Source

pub fn get_velocity(&self, i: usize) -> Result<[f64; 3], CacheLayoutError>

Return the velocity of particle i.

Returns Err if i >= len().

Source

pub fn set_velocity( &mut self, i: usize, vel: [f64; 3], ) -> Result<(), CacheLayoutError>

Set the velocity of particle i.

Returns Err if i >= len().

Source

pub fn get_force(&self, i: usize) -> Result<[f64; 3], CacheLayoutError>

Return the force on particle i.

Returns Err if i >= len().

Source

pub fn set_force( &mut self, i: usize, force: [f64; 3], ) -> Result<(), CacheLayoutError>

Set the force on particle i.

Returns Err if i >= len().

Source

pub fn zero_forces(&mut self)

Zero all force components. This is a very cache-friendly operation because it streams through three contiguous arrays sequentially.

Source

pub fn from_aos(particles: &[Particle]) -> Self

Convert from an AoS slice of Particles.

Source

pub fn to_aos(&self) -> Vec<Particle>

Convert back to an AoS VecParticle`.

Source

pub fn swap(&mut self, i: usize, j: usize) -> Result<(), CacheLayoutError>

Swap two particles at indices i and j.

Returns Err if either index is out of bounds or if i == j.

Source

pub fn sort_by_morton_code( &mut self, grid_spacing: f64, ) -> Result<(), CacheLayoutError>

Sort all particles by their 3D Morton (Z-curve) code.

After sorting, particles that are spatially close share nearby memory addresses, which dramatically improves cache performance for neighbour-search algorithms.

Returns Err if grid_spacing is invalid (non-positive or non-finite). Returns Ok(()) immediately if the container has fewer than two particles.

Source

pub fn prefetch_range(&self, _start: usize, _end: usize)

Hint the CPU prefetcher to load particles in \[start, end).

On stable Rust this is a no-op; it documents the developer’s intent and may be replaced with intrinsic prefetch instructions when they stabilise.

Source

pub fn get_mass(&self, i: usize) -> Result<f64, CacheLayoutError>

Return the mass of particle i.

Returns Err if i >= len().

Source

pub fn pop(&mut self) -> Result<Particle, CacheLayoutError>

Remove the last particle (analogous to Vec::pop).

Returns the removed particle, or Err if the container is empty.

Source

pub fn clear(&mut self)

Remove all particles without deallocating.

Source

pub fn reserve(&mut self, additional: usize)

Reserve capacity for at least additional more particles.

Source

pub fn bounding_box(&self) -> Result<([f64; 3], [f64; 3]), CacheLayoutError>

Compute the axis-aligned bounding box of all particles.

Returns (min_corner, max_corner) or Err if empty.

Source

pub fn kinetic_energy(&self) -> f64

Compute the total kinetic energy: sum_i 0.5 * m_i * |v_i|^2.

Source

pub fn centre_of_mass(&self) -> Result<[f64; 3], CacheLayoutError>

Compute the centre of mass position.

Returns Err if the container is empty or total mass is zero.

Trait Implementations§

Source§

impl Clone for ParticleSoA

Source§

fn clone(&self) -> ParticleSoA

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for ParticleSoA

Source§

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

Formats the value using the given formatter. Read more
Source§

impl PartialEq for ParticleSoA

Source§

fn eq(&self, other: &ParticleSoA) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for ParticleSoA

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.
Source§

impl<T> Scalar for T
where T: 'static + Clone + PartialEq + Debug,