scirs2-integrate 0.4.0

Numerical integration module for SciRS2 (scirs2-integrate)
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
//! Boundary condition handling for incompressible flow simulations
//!
//! This module provides methods for applying various boundary conditions
//! to velocity and pressure fields in incompressible fluid simulations.

use crate::error::{IntegrateError, IntegrateResult as Result};
use scirs2_core::ndarray::Array2;

use super::super::core::FluidBoundaryCondition;

/// Apply boundary conditions to velocity fields
///
/// This function applies the specified boundary conditions to the u and v velocity
/// components based on the boundary condition types for each edge.
///
/// # Arguments
///
/// * `u` - Mutable reference to u-velocity component
/// * `v` - Mutable reference to v-velocity component
/// * `bc_x` - Boundary conditions for left and right edges (x-direction)
/// * `bc_y` - Boundary conditions for bottom and top edges (y-direction)
pub fn apply_boundary_conditions_2d(
    u: &mut Array2<f64>,
    v: &mut Array2<f64>,
    bc_x: (FluidBoundaryCondition, FluidBoundaryCondition),
    bc_y: (FluidBoundaryCondition, FluidBoundaryCondition),
) -> Result<()> {
    let (ny, nx) = u.dim();

    // Left and right boundaries (x-direction)
    apply_x_boundary_left(u, v, bc_x.0, ny)?;
    apply_x_boundary_right(u, v, bc_x.1, ny, nx)?;

    // Top and bottom boundaries (y-direction)
    apply_y_boundary_bottom(u, v, bc_y.0, nx)?;
    apply_y_boundary_top(u, v, bc_y.1, nx, ny)?;

    Ok(())
}

/// Apply left boundary condition (x = 0)
fn apply_x_boundary_left(
    u: &mut Array2<f64>,
    v: &mut Array2<f64>,
    bc: FluidBoundaryCondition,
    ny: usize,
) -> Result<()> {
    match bc {
        FluidBoundaryCondition::NoSlip => {
            for j in 0..ny {
                u[[j, 0]] = 0.0;
                v[[j, 0]] = 0.0;
            }
        }
        FluidBoundaryCondition::FreeSlip => {
            for j in 0..ny {
                u[[j, 0]] = u[[j, 1]]; // Zero normal gradient for tangential velocity
                v[[j, 0]] = 0.0; // Zero normal velocity
            }
        }
        FluidBoundaryCondition::Periodic => {
            for j in 0..ny {
                u[[j, 0]] = u[[j, u.dim().1 - 2]];
                v[[j, 0]] = v[[j, v.dim().1 - 2]];
            }
        }
        FluidBoundaryCondition::Inflow(u_in, v_in) => {
            for j in 0..ny {
                u[[j, 0]] = u_in;
                v[[j, 0]] = v_in;
            }
        }
        FluidBoundaryCondition::Outflow => {
            for j in 0..ny {
                u[[j, 0]] = u[[j, 1]]; // Zero gradient
                v[[j, 0]] = v[[j, 1]];
            }
        }
    }
    Ok(())
}

/// Apply right boundary condition (x = L)
fn apply_x_boundary_right(
    u: &mut Array2<f64>,
    v: &mut Array2<f64>,
    bc: FluidBoundaryCondition,
    ny: usize,
    nx: usize,
) -> Result<()> {
    match bc {
        FluidBoundaryCondition::NoSlip => {
            for j in 0..ny {
                u[[j, nx - 1]] = 0.0;
                v[[j, nx - 1]] = 0.0;
            }
        }
        FluidBoundaryCondition::FreeSlip => {
            for j in 0..ny {
                u[[j, nx - 1]] = u[[j, nx - 2]]; // Zero normal gradient for tangential velocity
                v[[j, nx - 1]] = 0.0; // Zero normal velocity
            }
        }
        FluidBoundaryCondition::Periodic => {
            for j in 0..ny {
                u[[j, nx - 1]] = u[[j, 1]];
                v[[j, nx - 1]] = v[[j, 1]];
            }
        }
        FluidBoundaryCondition::Inflow(u_in, v_in) => {
            for j in 0..ny {
                u[[j, nx - 1]] = u_in;
                v[[j, nx - 1]] = v_in;
            }
        }
        FluidBoundaryCondition::Outflow => {
            for j in 0..ny {
                u[[j, nx - 1]] = u[[j, nx - 2]]; // Zero gradient
                v[[j, nx - 1]] = v[[j, nx - 2]];
            }
        }
    }
    Ok(())
}

/// Apply bottom boundary condition (y = 0)
fn apply_y_boundary_bottom(
    u: &mut Array2<f64>,
    v: &mut Array2<f64>,
    bc: FluidBoundaryCondition,
    nx: usize,
) -> Result<()> {
    match bc {
        FluidBoundaryCondition::NoSlip => {
            // Skip corners (i=0 and i=nx-1) to let x-direction boundaries take precedence
            for i in 1..nx - 1 {
                u[[0, i]] = 0.0;
                v[[0, i]] = 0.0;
            }
        }
        FluidBoundaryCondition::FreeSlip => {
            // Skip corners to let x-direction boundaries take precedence
            for i in 1..nx - 1 {
                u[[0, i]] = 0.0; // Zero normal velocity
                v[[0, i]] = v[[1, i]]; // Zero normal gradient for tangential velocity
            }
        }
        FluidBoundaryCondition::Periodic => {
            // Skip corners to let x-direction boundaries take precedence
            for i in 1..nx - 1 {
                u[[0, i]] = u[[u.dim().0 - 2, i]];
                v[[0, i]] = v[[v.dim().0 - 2, i]];
            }
        }
        FluidBoundaryCondition::Inflow(u_in, v_in) => {
            // Skip corners to let x-direction boundaries take precedence
            for i in 1..nx - 1 {
                u[[0, i]] = u_in;
                v[[0, i]] = v_in;
            }
        }
        FluidBoundaryCondition::Outflow => {
            // Skip corners to let x-direction boundaries take precedence
            for i in 1..nx - 1 {
                u[[0, i]] = u[[1, i]]; // Zero gradient
                v[[0, i]] = v[[1, i]];
            }
        }
    }
    Ok(())
}

/// Apply top boundary condition (y = L)
fn apply_y_boundary_top(
    u: &mut Array2<f64>,
    v: &mut Array2<f64>,
    bc: FluidBoundaryCondition,
    nx: usize,
    ny: usize,
) -> Result<()> {
    match bc {
        FluidBoundaryCondition::NoSlip => {
            // Skip corners to let x-direction boundaries take precedence
            for i in 1..nx - 1 {
                u[[ny - 1, i]] = 0.0;
                v[[ny - 1, i]] = 0.0;
            }
        }
        FluidBoundaryCondition::FreeSlip => {
            // Skip corners to let x-direction boundaries take precedence
            for i in 1..nx - 1 {
                u[[ny - 1, i]] = 0.0; // Zero normal velocity
                v[[ny - 1, i]] = v[[ny - 2, i]]; // Zero normal gradient for tangential velocity
            }
        }
        FluidBoundaryCondition::Periodic => {
            // Skip corners to let x-direction boundaries take precedence
            for i in 1..nx - 1 {
                u[[ny - 1, i]] = u[[1, i]];
                v[[ny - 1, i]] = v[[1, i]];
            }
        }
        FluidBoundaryCondition::Inflow(u_in, v_in) => {
            // Skip corners to let x-direction boundaries take precedence
            for i in 1..nx - 1 {
                u[[ny - 1, i]] = u_in;
                v[[ny - 1, i]] = v_in;
            }
        }
        FluidBoundaryCondition::Outflow => {
            // Skip corners to let x-direction boundaries take precedence
            for i in 1..nx - 1 {
                u[[ny - 1, i]] = u[[ny - 2, i]]; // Zero gradient
                v[[ny - 1, i]] = v[[ny - 2, i]];
            }
        }
    }
    Ok(())
}

/// Apply boundary conditions to pressure field
///
/// Pressure boundary conditions are typically Neumann (zero gradient) conditions
/// to ensure proper pressure-velocity coupling in the projection method.
///
/// # Arguments
///
/// * `pressure` - Mutable reference to pressure field
pub fn apply_pressure_boundary_conditions(pressure: &mut Array2<f64>) -> Result<()> {
    let (ny, nx) = pressure.dim();

    // Neumann boundary conditions (zero gradient) for pressure
    // Left and right
    for j in 0..ny {
        pressure[[j, 0]] = pressure[[j, 1]];
        pressure[[j, nx - 1]] = pressure[[j, nx - 2]];
    }

    // Top and bottom
    for i in 0..nx {
        pressure[[0, i]] = pressure[[1, i]];
        pressure[[ny - 1, i]] = pressure[[ny - 2, i]];
    }

    Ok(())
}

/// Apply boundary conditions with specified wall velocities
///
/// This is a specialized function for handling moving wall boundary conditions,
/// such as in the lid-driven cavity problem.
///
/// # Arguments
///
/// * `u` - Mutable reference to u-velocity component
/// * `v` - Mutable reference to v-velocity component
/// * `wall_velocities` - Tuple of (left, right, bottom, top) wall velocities as Option<(f64, f64)>
pub fn apply_wall_velocity_conditions(
    u: &mut Array2<f64>,
    v: &mut Array2<f64>,
    wall_velocities: (
        Option<(f64, f64)>,
        Option<(f64, f64)>,
        Option<(f64, f64)>,
        Option<(f64, f64)>,
    ),
) -> Result<()> {
    let (ny, nx) = u.dim();

    // Left wall
    if let Some((u_wall, v_wall)) = wall_velocities.0 {
        for j in 0..ny {
            u[[j, 0]] = u_wall;
            v[[j, 0]] = v_wall;
        }
    }

    // Right wall
    if let Some((u_wall, v_wall)) = wall_velocities.1 {
        for j in 0..ny {
            u[[j, nx - 1]] = u_wall;
            v[[j, nx - 1]] = v_wall;
        }
    }

    // Bottom wall
    if let Some((u_wall, v_wall)) = wall_velocities.2 {
        for i in 0..nx {
            u[[0, i]] = u_wall;
            v[[0, i]] = v_wall;
        }
    }

    // Top wall
    if let Some((u_wall, v_wall)) = wall_velocities.3 {
        for i in 0..nx {
            u[[ny - 1, i]] = u_wall;
            v[[ny - 1, i]] = v_wall;
        }
    }

    Ok(())
}

/// Apply periodic boundary conditions
///
/// This function specifically handles periodic boundary conditions where
/// the flow field wraps around from one side to the other.
///
/// # Arguments
///
/// * `u` - Mutable reference to u-velocity component
/// * `v` - Mutable reference to v-velocity component
/// * `periodic_x` - Whether to apply periodic conditions in x-direction
/// * `periodic_y` - Whether to apply periodic conditions in y-direction
pub fn apply_periodic_conditions(
    u: &mut Array2<f64>,
    v: &mut Array2<f64>,
    periodic_x: bool,
    periodic_y: bool,
) -> Result<()> {
    let (ny, nx) = u.dim();

    if periodic_x {
        for j in 0..ny {
            u[[j, 0]] = u[[j, nx - 2]];
            u[[j, nx - 1]] = u[[j, 1]];
            v[[j, 0]] = v[[j, nx - 2]];
            v[[j, nx - 1]] = v[[j, 1]];
        }
    }

    if periodic_y {
        for i in 0..nx {
            u[[0, i]] = u[[ny - 2, i]];
            u[[ny - 1, i]] = u[[1, i]];
            v[[0, i]] = v[[ny - 2, i]];
            v[[ny - 1, i]] = v[[1, i]];
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::ndarray::Array2;

    #[test]
    fn test_no_slip_boundary_conditions() {
        let mut u = Array2::ones((5, 5));
        let mut v = Array2::ones((5, 5));

        let bc_x = (
            FluidBoundaryCondition::NoSlip,
            FluidBoundaryCondition::NoSlip,
        );
        let bc_y = (
            FluidBoundaryCondition::NoSlip,
            FluidBoundaryCondition::NoSlip,
        );

        apply_boundary_conditions_2d(&mut u, &mut v, bc_x, bc_y).expect("Operation failed");

        // Check that boundaries are zero
        for i in 0..5 {
            assert_eq!(u[[0, i]], 0.0); // Bottom
            assert_eq!(u[[4, i]], 0.0); // Top
            assert_eq!(v[[0, i]], 0.0); // Bottom
            assert_eq!(v[[4, i]], 0.0); // Top
        }

        for j in 0..5 {
            assert_eq!(u[[j, 0]], 0.0); // Left
            assert_eq!(u[[j, 4]], 0.0); // Right
            assert_eq!(v[[j, 0]], 0.0); // Left
            assert_eq!(v[[j, 4]], 0.0); // Right
        }
    }

    #[test]
    fn test_pressure_boundary_conditions() {
        let mut pressure = Array2::from_shape_fn((5, 5), |(i, j)| (i + j) as f64);
        let original_interior = pressure[[2, 2]];

        apply_pressure_boundary_conditions(&mut pressure).expect("Operation failed");

        // Check that interior values are unchanged
        assert_eq!(pressure[[2, 2]], original_interior);

        // Check that boundary values are copied from interior
        assert_eq!(pressure[[0, 2]], pressure[[1, 2]]); // Bottom
        assert_eq!(pressure[[4, 2]], pressure[[3, 2]]); // Top
        assert_eq!(pressure[[2, 0]], pressure[[2, 1]]); // Left
        assert_eq!(pressure[[2, 4]], pressure[[2, 3]]); // Right
    }

    #[test]
    fn test_inflow_boundary_conditions() {
        let mut u = Array2::zeros((5, 5));
        let mut v = Array2::zeros((5, 5));

        let bc_x = (
            FluidBoundaryCondition::Inflow(1.0, 0.5),
            FluidBoundaryCondition::Outflow,
        );
        let bc_y = (
            FluidBoundaryCondition::NoSlip,
            FluidBoundaryCondition::NoSlip,
        );

        apply_boundary_conditions_2d(&mut u, &mut v, bc_x, bc_y).expect("Operation failed");

        // Check inflow boundary
        for j in 0..5 {
            assert_eq!(u[[j, 0]], 1.0);
            assert_eq!(v[[j, 0]], 0.5);
        }
    }
}