rssn 0.2.9

A comprehensive scientific computing library for Rust, aiming for feature parity with NumPy and SymPy.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use std::ops::Add;
use std::ops::Div;
use std::ops::Mul;
use std::ops::Sub;

use rayon::prelude::*;
use serde::Deserialize;
use serde::Serialize;

/// A 2D vector.
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
pub struct Vector2D {
    /// The x component of the vector.
    pub x: f64,
    /// The y component of the vector.
    pub y: f64,
}

impl Vector2D {
    /// Creates a new 2D vector.
    #[must_use]
    pub const fn new(
        x: f64,
        y: f64,
    ) -> Self {
        Self { x, y }
    }

    pub(crate) fn norm_sq(&self) -> f64 {
        self.x.mul_add(self.x, self.y * self.y)
    }
}

impl Add for Vector2D {
    type Output = Self;

    fn add(
        self,
        rhs: Self,
    ) -> Self {
        Self {
            x: self.x + rhs.x,
            y: self.y + rhs.y,
        }
    }
}

impl Sub for Vector2D {
    type Output = Self;

    fn sub(
        self,
        rhs: Self,
    ) -> Self {
        Self {
            x: self.x - rhs.x,
            y: self.y - rhs.y,
        }
    }
}

impl Mul<f64> for Vector2D {
    type Output = Self;

    fn mul(
        self,
        rhs: f64,
    ) -> Self {
        Self {
            x: self.x * rhs,
            y: self.y * rhs,
        }
    }
}

impl Div<f64> for Vector2D {
    type Output = Self;

    fn div(
        self,
        rhs: f64,
    ) -> Self {
        Self {
            x: self.x / rhs,
            y: self.y / rhs,
        }
    }
}

/// cbindgen:ignore
/// A particle in the SPH simulation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Particle {
    /// The position of the particle.
    pub pos: Vector2D,
    /// The velocity of the particle.
    pub vel: Vector2D,
    /// The force acting on the particle.
    pub force: Vector2D,
    /// The density of the particle.
    pub density: f64,
    /// The pressure of the particle.
    pub pressure: f64,
    /// The mass of the particle.
    pub mass: f64,
}

/// The Poly6 kernel for SPH simulations.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Poly6Kernel {
    /// The squared smoothing radius.
    pub h_sq: f64,
    /// The normalization factor.
    pub factor: f64,
}

impl Poly6Kernel {
    /// Creates a new Poly6 kernel.
    #[must_use]
    pub fn new(h: f64) -> Self {
        Self {
            h_sq: h * h,
            factor: 315.0 / (64.0 * std::f64::consts::PI * h.powi(9)),
        }
    }

    pub(crate) fn value(
        &self,
        r_sq: f64,
    ) -> f64 {
        if r_sq >= self.h_sq {
            return 0.0;
        }

        let diff = self.h_sq - r_sq;

        self.factor * diff * diff * diff
    }
}

/// The spiky kernel for SPH simulations.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SpikyKernel {
    /// The smoothing radius.
    pub h: f64,
    /// The normalization factor.
    pub factor: f64,
}

impl SpikyKernel {
    /// Creates a new spiky kernel.
    #[must_use]
    pub fn new(h: f64) -> Self {
        Self {
            h,
            factor: -45.0 / (std::f64::consts::PI * h.powi(6)),
        }
    }

    pub(crate) fn gradient(
        &self,
        r_vec: Vector2D,
        r_norm: f64,
    ) -> Vector2D {
        if r_norm >= self.h || r_norm == 0.0 {
            return Vector2D::default();
        }

        let diff = self.h - r_norm;

        r_vec * (self.factor * diff * diff / r_norm)
    }
}

/// The SPH system.
#[derive(Debug, Clone, Serialize)]
pub struct SPHSystem {
    /// The particles in the system.
    pub particles: Vec<Particle>,
    /// The Poly6 kernel.
    pub poly6: Poly6Kernel,
    /// The spiky kernel.
    pub spiky: SpikyKernel,
    /// The gravity vector.
    pub gravity: Vector2D,
    /// The viscosity of the fluid.
    pub viscosity: f64,
    /// The gas constant.
    pub gas_const: f64,
    /// The rest density of the fluid.
    pub rest_density: f64,
    /// The bounds of the simulation.
    pub bounds: Vector2D,
}

impl<'de> Deserialize<'de> for SPHSystem {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(Deserialize)]
        struct SPHSystemData {
            particles: Vec<Particle>,
            poly6: Poly6Kernel,
            spiky: SpikyKernel,
            gravity: Vector2D,
            viscosity: f64,
            gas_const: f64,
            rest_density: f64,
            bounds: Vector2D,
        }

        let data = SPHSystemData::deserialize(deserializer)?;

        Ok(Self {
            particles: data.particles,
            poly6: data.poly6,
            spiky: data.spiky,
            gravity: data.gravity,
            viscosity: data.viscosity,
            gas_const: data.gas_const,
            rest_density: data.rest_density,
            bounds: data.bounds,
        })
    }
}

impl SPHSystem {
    /// Computes the density and pressure of each particle.
    pub fn compute_density_pressure(&mut self) {
        let poly6 = &self.poly6;

        let gas_const = self.gas_const;

        let rest_density = self.rest_density;

        // Use a raw pointer to provide an immutable view of all particles to each thread
        // to avoid conflicts with the mutable iterator.
        let particles_ptr = self.particles.as_ptr() as usize;

        let n = self.particles.len();

        self.particles.par_iter_mut().for_each(|p_i| {
            let particles_ref =
                unsafe { std::slice::from_raw_parts(particles_ptr as *const Particle, n) };

            let mut density = 0.0;

            for p_j in particles_ref {
                let r_vec = p_i.pos - p_j.pos;

                density += p_j.mass * poly6.value(r_vec.norm_sq());
            }

            p_i.density = density;

            p_i.pressure = gas_const * (density - rest_density).max(0.0);
        });
    }

    /// Computes the forces acting on each particle.
    pub fn compute_forces(&mut self) {
        let spiky = &self.spiky;

        let poly6 = &self.poly6;

        let viscosity = self.viscosity;

        let gravity = self.gravity;

        let particles_ptr = self.particles.as_ptr() as usize;

        let n = self.particles.len();

        self.particles
            .par_iter_mut()
            .enumerate()
            .for_each(|(i, p_i)| {
                let particles_ref =
                    unsafe { std::slice::from_raw_parts(particles_ptr as *const Particle, n) };

                let mut force = Vector2D::default();

                for (j, p_j) in particles_ref.iter().enumerate() {
                    if i == j {
                        continue;
                    }

                    let r_vec = p_i.pos - p_j.pos;

                    let r_norm = (r_vec.norm_sq()).sqrt();

                    if r_norm < spiky.h {
                        let avg_pressure = f64::midpoint(p_i.pressure, p_j.pressure);

                        force =
                            force - spiky.gradient(r_vec, r_norm) * (avg_pressure / p_j.density);

                        let vel_diff = p_j.vel - p_i.vel;

                        force = force + vel_diff * (viscosity * poly6.value(r_vec.norm_sq()));
                    }
                }

                p_i.force = force + gravity * p_i.density;
            });
    }

    /// Integrates the equations of motion for each particle.
    pub fn integrate(
        &mut self,
        dt: f64,
    ) {
        let bounds = self.bounds;

        self.particles.par_iter_mut().for_each(|p| {
            p.vel = p.vel + p.force * (dt / p.density);

            p.pos = p.pos + p.vel * dt;

            if p.pos.x < 0.0 {
                p.vel.x *= -0.5;

                p.pos.x = 0.0;
            }

            if p.pos.x > bounds.x {
                p.vel.x *= -0.5;

                p.pos.x = bounds.x;
            }

            if p.pos.y < 0.0 {
                p.vel.y *= -0.5;

                p.pos.y = 0.0;
            }

            if p.pos.y > bounds.y {
                p.vel.y *= -0.5;

                p.pos.y = bounds.y;
            }
        });
    }

    /// Updates the SPH system by one time step.
    pub fn update(
        &mut self,
        dt: f64,
    ) {
        self.compute_density_pressure();

        self.compute_forces();

        self.integrate(dt);
    }
}

/// Simulates a 2D dam break scenario using Smoothed-Particle Hydrodynamics (SPH).
///
/// This function initializes a block of fluid particles (the "dam") and simulates
/// its collapse and flow under gravity. It demonstrates the SPH method's ability
/// to model fluid dynamics without a fixed mesh.
///
/// # Returns
/// A `Vec` of tuples `(x, y)` representing the final positions of the particles.
#[must_use]
pub fn simulate_dam_break_2d_scenario() -> Vec<(f64, f64)> {
    let h = 0.1;

    let mut system = SPHSystem {
        particles: Vec::new(),
        poly6: Poly6Kernel::new(h),
        spiky: SpikyKernel::new(h),
        gravity: Vector2D::new(0.0, -9.8),
        viscosity: 0.01,
        gas_const: 2000.0,
        rest_density: 1000.0,
        bounds: Vector2D::new(4.0, 4.0),
    };

    let particle_mass = 1.0;

    for y in (0..20).map(|v| f64::from(v) * h * 0.8) {
        for x in (0..10).map(|v| f64::from(v) * h * 0.8) {
            system.particles.push(Particle {
                pos: Vector2D::new(x, y + 0.1),
                vel: Vector2D::default(),
                force: Vector2D::default(),
                density: 0.0,
                pressure: 0.0,
                mass: particle_mass,
            });
        }
    }

    let dt = 0.005;

    for _ in 0..200 {
        system.update(dt);
    }

    system
        .particles
        .iter()
        .map(|p| (p.pos.x, p.pos.y))
        .collect()
}