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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! # Navier-Stokes Fluid Simulation
//!
//! This module provides Computational Fluid Dynamics (CFD) solvers for incompressible, viscous fluid flow
//! governed by the Navier-Stokes equations. It focuses on 2D simulations using standard numerical techniques
//! for pressure-velocity coupling.
//!
//! # Overview
//!
//! The simulation engine primarily uses the projection method (Chorin, 1968) to decouple the velocity
//! and pressure fields. This involves an intermediate velocity prediction followed by a pressure correction step
//! to enforce the incompressibility constraint (continuity equation).
//!
//! Key features include:
//! - **Stable Solvers**: Implements the projection method for stable time-stepping.
//! - **Multigrid Acceleration**: Utilizes a geometric multigrid solver (V-cycles) for the pressure Poisson equation, critical for performance on fine grids.
//! - **Parallel Computation**: Leverages `rayon` for parallelized grid operations (advection, diffusion updates).
//! - **Diverse Scenarios**:
//!     - **Channel Flow**: Simulates flow past obstacles with configurable Reynolds numbers.
//!     - **Lid-Driven Cavity**: A classic CFD benchmark problem for testing internal flow and vortex formation.
//!
//! ![refer to this image](https://raw.githubusercontent.com/Apich-Organization/rssn/refs/heads/dev/doc/karman_velocity_mag.png)

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

use crate::output::io::write_npy_file;
use crate::physics::physics_mtm::solve_poisson_2d_multigrid;

/// Parameters for the Navier-Stokes simulation.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct NavierStokesParameters {
    /// Number of grid points in the x-direction.
    pub nx: usize,
    /// Number of grid points in the y-direction.
    pub ny: usize,
    /// Reynolds number.
    pub re: f64,
    /// Time step size.
    pub dt: f64,
    /// Number of simulation iterations.
    pub n_iter: usize,
    /// Velocity of the lid (driving force).
    pub lid_velocity: f64,
}

/// Type of `NavierStokesOutput`.
pub type NavierStokesOutput = Result<(Array2<f64>, Array2<f64>, Array2<f64>), String>;

/// Solves the 2D Navier-Stokes equations for channel flow with an obstacle.
/// Uses the projection method (Chorin 1968).
///
/// # Arguments
/// * `nx` - Grid width.
/// * `ny` - Grid height.
/// * `re` - Reynolds number.
/// * `dt` - Time step.
/// * `n_iter` - Number of iterations.
/// * `obstacle_mask` - A boolean mask where true indicates an obstacle (u=0).
///
/// # Returns
/// Tuple of (u, v, p) arrays.
///
/// # Panics
/// Panics if `nx` or `ny` is less than 2.
///
/// # Errors
/// Returns an error if the obstacle mask is not the same size as the grid.
pub fn run_channel_flow(
    nx: usize,
    _ny: usize,
    re: f64,
    dt: f64,
    n_iter: usize,
    obstacle_mask: &Array2<bool>,
) -> NavierStokesOutput {
    let n = nx;

    let h = 1.0 / (n as f64 - 1.0);

    let nu = 1.0 / re;

    let mut u = Array2::<f64>::zeros((n, n));

    let mut v = Array2::<f64>::zeros((n, n));

    let mut p = Array2::<f64>::zeros((n, n));

    // Helper for indexing
    let idx = |i: usize, j: usize| j * n + i;

    // Multigrid size
    let mg_size = n;

    for _iter in 0..n_iter {
        let u_old = u.clone();

        let v_old = v.clone();

        // 1. Advection-Diffusion (Explicit) -> Intermediate Velocity (u*, v*)
        // u_star = u_old + dt * ( - (u grad) u + nu laplacian u )
        let mut u_star = u.clone();

        let mut v_star = v.clone();

        u_star
            .as_slice_mut()
            .expect("Contiguous array")
            .par_iter_mut()
            .zip(
                v_star
                    .as_slice_mut()
                    .expect("Contiguous array")
                    .par_iter_mut(),
            )
            .enumerate()
            .for_each(|(id, (u_val, v_val))| {
                let i = id % n;
                let j = id / n;

                // Skip boundaries and obstacles for now
                if i == 0 || i == n - 1 || j == 0 || j == n - 1 || obstacle_mask[[j, i]] {
                    return;
                }

                // Upwind Advection
                let u_curr = u_old[[j, i]];
                let v_curr = v_old[[j, i]];

                let du_dx = if u_curr > 0.0 {
                    u_curr - u_old[[j, i - 1]]
                } else {
                    u_old[[j, i + 1]] - u_curr
                } / h;

                let du_dy = if v_curr > 0.0 {
                    u_curr - u_old[[j - 1, i]]
                } else {
                    u_old[[j + 1, i]] - u_curr
                } / h;

                let dv_dx = if u_curr > 0.0 {
                    v_curr - v_old[[j, i - 1]]
                } else {
                    v_old[[j, i + 1]] - v_curr
                } / h;

                let dv_dy = if v_curr > 0.0 {
                    v_curr - v_old[[j - 1, i]]
                } else {
                    v_old[[j + 1, i]] - v_curr
                } / h;

                // Diffusion (Central Difference)
                let lap_u = (2.0f64.mul_add(
                    -u_curr,
                    2.0f64.mul_add(-u_curr, u_old[[j, i + 1]])
                        + u_old[[j, i - 1]]
                        + u_old[[j + 1, i]],
                ) + u_old[[j - 1, i]])
                    / (h * h);

                let lap_v = (2.0f64.mul_add(
                    -v_curr,
                    2.0f64.mul_add(-v_curr, v_old[[j, i + 1]])
                        + v_old[[j, i - 1]]
                        + v_old[[j + 1, i]],
                ) + v_old[[j - 1, i]])
                    / (h * h);

                *u_val = dt.mul_add(-u_curr.mul_add(du_dx, v_curr * du_dy) + nu * lap_u, u_curr);
                *v_val = dt.mul_add(-u_curr.mul_add(dv_dx, v_curr * dv_dy) + nu * lap_v, v_curr);
            });

        // Apply BCs to u_star, v_star
        // Inflow (Left)
        for j in 0..n {
            u_star[[j, 0]] = 1.0;

            v_star[[j, 0]] = 0.0;
        }

        // Outflow (Right) - Zero Gradient
        for j in 0..n {
            u_star[[j, n - 1]] = u_star[[j, n - 2]];

            v_star[[j, n - 1]] = v_star[[j, n - 2]];
        }

        // Walls (Top/Bottom) - We do Slip for channel here
        for i in 0..n {
            u_star[[0, i]] = u_star[[1, i]]; // Slip
            v_star[[0, i]] = 0.0;

            u_star[[n - 1, i]] = u_star[[n - 2, i]]; // Slip
            v_star[[n - 1, i]] = 0.0;
        }

        // Obstacle - No Slip
        for j in 0..n {
            for i in 0..n {
                if obstacle_mask[[j, i]] {
                    u_star[[j, i]] = 0.0;

                    v_star[[j, i]] = 0.0;
                }
            }
        }

        // 2. Pressure Correction (Poisson Step)
        // laplacian p = div(u*) / dt
        let mut rhs_vec = vec![0.0; n * n];

        // Calculate Div U*
        for j in 1..n - 1 {
            for i in 1..n - 1 {
                if obstacle_mask[[j, i]] {
                    continue;
                }

                let div = (u_star[[j, i + 1]] - u_star[[j, i - 1]]) / (2.0 * h)
                    + (v_star[[j + 1, i]] - v_star[[j - 1, i]]) / (2.0 * h);

                rhs_vec[idx(i, j)] = div / dt;
            }
        }

        // Solve Poisson
        // Note: Our MG solver expects "f" where "-laplacian u = f".
        // Our equation is "laplacian p = R". So we pass "-R" as f.
        for val in &mut rhs_vec {
            *val = -*val;
        }

        let p_flat = solve_poisson_2d_multigrid(mg_size, &rhs_vec, 2)?; // 2 V-cycles

        // Copy back to P array
        for j in 0..n {
            for i in 0..n {
                p[[j, i]] = p_flat[idx(i, j)];
            }
        }

        // 3. Velocity Correction
        // u_new = u* - dt * grad p
        for j in 1..n - 1 {
            for i in 1..n - 1 {
                if obstacle_mask[[j, i]] {
                    u[[j, i]] = 0.0;

                    v[[j, i]] = 0.0;

                    continue;
                }

                let dp_dx = (p[[j, i + 1]] - p[[j, i - 1]]) / (2.0 * h);

                let dp_dy = (p[[j + 1, i]] - p[[j - 1, i]]) / (2.0 * h);

                u[[j, i]] = dt.mul_add(-dp_dx, u_star[[j, i]]);

                v[[j, i]] = dt.mul_add(-dp_dy, v_star[[j, i]]);
            }
        }

        // Re-apply BCs for u, v
        // Inflow (Left)
        for j in 0..n {
            u[[j, 0]] = 1.0;

            v[[j, 0]] = 0.0;
        }

        // Outflow (Right)
        for j in 0..n {
            u[[j, n - 1]] = u[[j, n - 2]];

            v[[j, n - 1]] = v[[j, n - 2]];
        }

        // Walls (Top/Bottom)
        for i in 0..n {
            u[[0, i]] = u[[1, i]];

            v[[0, i]] = 0.0;

            u[[n - 1, i]] = u[[n - 2, i]];

            v[[n - 1, i]] = 0.0;
        }
    }

    Ok((u, v, p))
}

/// Main solver for the 2D lid-driven cavity problem.
///
/// # Errors
///
/// This function will return an error if the underlying Poisson solver fails,
/// or if there are issues reshaping the pressure correction array.
pub fn run_lid_driven_cavity(params: &NavierStokesParameters) -> NavierStokesOutput {
    let (nx, ny, _re, dt) = (params.nx, params.ny, params.re, params.dt);

    let hx = 1.0 / (nx - 1) as f64;

    let hy = 1.0 / (ny - 1) as f64;

    let mut u = Array2::<f64>::zeros((ny, nx + 1));

    let mut v = Array2::<f64>::zeros((ny + 1, nx));

    let mut p = Array2::<f64>::zeros((ny, nx));

    // Boundary conditions: lid velocity
    for j in 0..=nx {
        u[[ny - 1, j]] = params.lid_velocity;
    }

    let mg_size_k = (((nx.max(ny) - 1) as f64).log2().ceil() as i64)
        .try_into()
        .unwrap_or(0);

    let mg_size = 2_usize.pow(mg_size_k) + 1;

    for _ in 0..params.n_iter {
        let u_old = u.clone();

        let v_old = v.clone();

        // Calculate RHS in parallel
        let mut rhs_padded = vec![0.0; mg_size * mg_size];

        let rhs_ptr = rhs_padded.as_mut_ptr() as usize;

        (1..ny - 1).into_par_iter().for_each(|j| {
            for i in 1..nx - 1 {
                let div_u_star = ((u_old[[j, i + 1]] - u_old[[j, i]]) / hx)
                    + ((v_old[[j + 1, i]] - v_old[[j, i]]) / hy);

                unsafe {
                    *(rhs_ptr as *mut f64).add(j * mg_size + i) = div_u_star / dt;
                }
            }
        });

        // Solve Poisson for pressure correction
        let p_corr_vec = solve_poisson_2d_multigrid(mg_size, &rhs_padded, 10)?; // More V-cycles for accuracy
        let p_corr =
            Array2::from_shape_vec((mg_size, mg_size), p_corr_vec).map_err(|e| e.to_string())?;

        // Update pressure and velocities in parallel
        let p_ptr = p.as_mut_ptr() as usize;

        let u_ptr = u.as_mut_ptr() as usize;

        let v_ptr = v.as_mut_ptr() as usize;

        // Update P
        (0..ny).into_par_iter().for_each(|j| {
            for i in 0..nx {
                unsafe {
                    *(p_ptr as *mut f64).add(j * nx + i) += 0.7 * p_corr[[j, i]];
                }
            }
        });

        // Update U
        (1..ny - 1).into_par_iter().for_each(|j| {
            for i in 1..nx {
                unsafe {
                    *(u_ptr as *mut f64).add(j * (nx + 1) + i) -=
                        dt / hx * (p_corr[[j, i]] - p_corr[[j, i - 1]]);
                }
            }
        });

        // Update V
        (1..ny).into_par_iter().for_each(|j| {
            for i in 1..nx - 1 {
                unsafe {
                    *(v_ptr as *mut f64).add(j * nx + i) -=
                        dt / hy * (p_corr[[j, i]] - p_corr[[j - 1, i]]);
                }
            }
        });
    }

    // ... centering ...
    let mut u_centered = Array2::<f64>::zeros((ny, nx));

    let mut v_centered = Array2::<f64>::zeros((ny, nx));

    let uc_ptr = u_centered.as_mut_ptr() as usize;

    let vc_ptr = v_centered.as_mut_ptr() as usize;

    (0..ny).into_par_iter().for_each(|j| {
        for i in 0..nx {
            unsafe {
                *(uc_ptr as *mut f64).add(j * nx + i) = 0.5 * (u[[j, i]] + u[[j, i + 1]]);

                *(vc_ptr as *mut f64).add(j * nx + i) = 0.5 * (v[[j, i]] + v[[j + 1, i]]);
            }
        }
    });

    Ok((u_centered, v_centered, p))
}

/// An example scenario for the lid-driven cavity simulation.
pub fn simulate_lid_driven_cavity_scenario() {
    const K: usize = 6;

    const N: usize = 2_usize.pow(K as u32) + 1;

    println!(
        "Running 2D Lid-Driven Cavity \
         simulation..."
    );

    let params = NavierStokesParameters {
        nx: N,
        ny: N,
        re: 100.0,
        dt: 0.01,
        n_iter: 200,
        lid_velocity: 1.0,
    };

    match run_lid_driven_cavity(&params) {
        | Ok((u, v, p)) => {
            println!(
                "Simulation finished. \
                 Saving results..."
            );

            let save_result = (|| -> Result<(), String> {
                write_npy_file("cavity_u_velocity.npy", &u)?;

                write_npy_file("cavity_v_velocity.npy", &v)?;

                write_npy_file("cavity_pressure.npy", &p)?;

                Ok(())
            })();

            if let Err(e) = save_result {
                eprintln!(
                    "Failed to save \
                     results: {e}"
                );
            } else {
                println!(
                    "Results saved to \
                     .npy files."
                );
            }
        },
        | Err(e) => {
            eprintln!(
                "An error occurred \
                 during simulation: \
                 {e}"
            );
        },
    }
}