stroemung 0.1.2

A Computational Fluid Dynamics (CFD) simulator in Rust
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
use ndarray::ArrayView2;

pub type Real = f64;

/// Calculate du^2/dx (the derivative of u^2 over x)
///
/// This function uses the same basic algebra rearrangement that the
/// NaSt2D code does. This makes the function easier to compare against
/// NaSt2D (and reduces the number of divisions in the calculation).
///
/// # Arguments
///
/// * `u_view` - A 3x3-element ArrayView2 representing
///   u[(i-1) to (i+1), (j-1) to (j+1)]. This function only uses
///   the three values on the row where j is 0 (index 1), but takes a 3x3
///   ArrayView2 to be easier to combine with other functions.
/// * `delx` - "delta x," the physical width of the cell
/// * `gamma` - Greek letter gamma, the upwind discretization parameter
pub fn du2dx(u_view: ArrayView2<Real>, delx: Real, gamma: Real) -> Real {
    let u_i_m1 = u_view[(0, 1)]; // u[(i-1, j)]  "u[i minus 1]" -> u_i_m1
    let u_i = u_view[(1, 1)]; // u[(i, j)]  "u[i]" -> "u_i"
    let u_i_p1 = u_view[(2, 1)]; // u[(i+1, j)]  "u[i plus 1]" -> u_i_p1

    let inner_left1 = (u_i + u_i_p1).powi(2);
    let inner_right1 = (u_i_m1 + u_i).powi(2);

    let left_side = inner_left1 - inner_right1;

    let inner_left2 = (u_i + u_i_p1).abs() * (u_i - u_i_p1);
    let inner_right2 = (u_i_m1 + u_i).abs() * (u_i_m1 - u_i);

    (left_side + (gamma * (inner_left2 - inner_right2))) / (4.0 * delx)
}

/// Calculate duv/dx (the derivative of u*v over x)
///
/// This function uses the same basic algebra rearrangement that the
/// NaSt2D code does. This makes the function easier to compare against
/// NaSt2D (and reduces the number of divisions in the calculation).
///
/// # Arguments
///
/// * `u_view` - A 3x3-element ArrayView2 representing
///   u[(i-1) to (i+1), (j-1) to (j+1)]. This function only uses the four
///   "lower left" values (the four combinarions of i-1, i, j, and j+1), but
///   takes a 3x3 ArrayView2 to be easier to combine with other functions.
/// * `v_view` - A 3x3-element ArrayView2 representing
///   v[(i-1) to (i+1), (j-1) to (j+1)]. This function only uses
///   the three values on the row where j is 0 (index 1), but takes a 3x3
///   ArrayView2 to be easier to combine with other functions.
/// * `delx` - "delta x," the physical width of the cell
/// * `gamma` - Greek letter gamma, the upwind discretization parameter
pub fn duvdx(
    u_view: ArrayView2<Real>,
    v_view: ArrayView2<Real>,
    delx: Real,
    gamma: Real,
) -> Real {
    let u_i_j = u_view[(1, 1)]; // u[(i, j)] -> u_i_j
    let u_i_j_p1 = u_view[(1, 2)]; // u[(i, j+1)]  "u[i][j plus 1]" -> u_i_j_p1
    let u_i_m1_j = u_view[(0, 1)]; // "u[(i-1, j)]" "u[i minus 1][j]" -> u_i_m1_j
    let u_i_m1_j_p1 = u_view[(0, 2)]; // "u[(i-1, j+1)]" "u[i-1][j+1]" -> u_i_m1_j_p1

    let v_i_j = v_view[(1, 1)]; // "v[(i, j)]" -> v_i_j
    let v_i_p1_j = v_view[(2, 1)]; // "v[(i+1, j)]" -> "v[i plus 1][j]" -> v_i_p1_j
    let v_i_m1_j = v_view[(0, 1)]; // "v[(i-1, j)]" -> "v[i-1][j]" -> v_i_m1_j

    let inner_left1 = (u_i_j + u_i_j_p1) * (v_i_j + v_i_p1_j);
    let inner_right1 = (u_i_m1_j + u_i_m1_j_p1) * (v_i_m1_j + v_i_j);

    let left_side = inner_left1 - inner_right1;

    let inner_left2 = (u_i_j + u_i_j_p1).abs() * (v_i_j - v_i_p1_j);
    let inner_right2 = (u_i_m1_j + u_i_m1_j_p1).abs() * (v_i_m1_j - v_i_j);

    (left_side + (gamma * (inner_left2 - inner_right2))) / (4.0 * delx)
}

/// Calculate duv/dy (the derivative of u*v over y)
///
/// This function uses the same basic algebra rearrangement that the
/// NaSt2D code does. This makes the function easier to compare against
/// NaSt2D (and reduces the number of divisions in the calculation).
///
/// # Arguments
///
/// * `u_view` - A 3x3-element ArrayView2 representing
///   u[(i-1) to (i+1), (j-1) to (j+1)]. This function only uses
///   the three values on the column where i is 0 (index 1), but takes a 3x3
///   ArrayView2 to be easier to combine with other functions.
/// * `v_view` - A 3x3-element ArrayView2 representing
///   v[(i-1) to (i+1), (j-1) to (j+1)]. This function only uses
///   "upper right" values (the four combinarions of i, i+1, j-1, and j), but
///   takes a 3x3 ArrayView2 to be easier to combine with other functions.
/// * `dely` - "delta y," the physical height of the cell
/// * `gamma` - Greek letter gamma, the upwind discretization parameter
pub fn duvdy(
    u_view: ArrayView2<Real>,
    v_view: ArrayView2<Real>,
    dely: Real,
    gamma: Real,
) -> Real {
    let u_i_j = u_view[(1, 1)]; // u[(i, j)] -> u_i_j
    let u_i_j_m1 = u_view[(1, 0)]; // u[(i, j-1)] -> "u[i][j minus 1]" -> u_i_j_m1
    let u_i_j_p1 = u_view[(1, 2)]; // u[(i, j+1)] -> "u[i][j plus 1]" -> u_i_j_p1

    let v_i_j = v_view[(1, 1)]; // v[(i, j)] -> v_i_j
    let v_i_j_m1 = v_view[(1, 0)]; // v[(i, j-1)] -> "v[i][j minus 1]" -> v_i_j_m1
    let v_i_p1_j = v_view[(2, 1)]; // v[(i+1, j)] -> "v[i plus 1][j]" -> v_i_p1_j
    let v_i_p1_j_m1 = v_view[(2, 0)]; // v[(i+1, j-1)] -> "v[i plus 1][j minus 1]" -> v_i_p1_j_m1

    let inner_left1 = (v_i_j + v_i_p1_j) * (u_i_j + u_i_j_p1);
    let inner_right1 = (v_i_j_m1 + v_i_p1_j_m1) * (u_i_j_m1 + u_i_j);
    let left_side = inner_left1 - inner_right1;

    let inner_left2 = (v_i_j + v_i_p1_j).abs() * (u_i_j - u_i_j_p1);
    let inner_right2 = (v_i_j_m1 + v_i_p1_j_m1).abs() * (u_i_j_m1 - u_i_j);

    (left_side + (gamma * (inner_left2 - inner_right2))) / (4.0 * dely)
}

/// Calculate dv^2/dy (the derivative of v^2 over y)
///
/// This function uses the same basic algebra rearrangement that the
/// NaSt2D code does. This makes the function easier to compare against
/// NaSt2D (and reduces the number of divisions in the calculation).
///
/// # Arguments
///
/// * `v_view` - A 3x3-element ArrayView2 representing
///   v[(i-1) to (i+1), (j-1) to (j+1)]. This function only uses
///   the three values on the column where i is 0 (index 1), but takes a 3x3
///   ArrayView2 to be easier to combine with other functions.
/// * `dely` - "delta y," the physical width of the cell
/// * `gamma` - Greek letter gamma, the upwind discretization parameter
pub fn dv2dy(v_view: ArrayView2<Real>, dely: Real, gamma: Real) -> Real {
    let v_i_j = v_view[(1, 1)]; // v[(i, j)] -> v_i_j
    let v_i_j_p1 = v_view[(1, 2)]; // v[(i, j+1)] -> "v[i][j plus 1]" -> v_i_j_p1
    let v_i_j_m1 = v_view[(1, 0)]; // v[(i, j-1)] -> "v[i][j minus 1]" -> v_i_j_m1

    let inner_left1 = (v_i_j + v_i_j_p1).powi(2);
    let inner_right1 = (v_i_j_m1 + v_i_j).powi(2);

    let left_side = inner_left1 - inner_right1;

    let inner_left2 = (v_i_j + v_i_j_p1).abs() * (v_i_j - v_i_j_p1);
    let inner_right2 = (v_i_j_m1 + v_i_j).abs() * (v_i_j_m1 - v_i_j);

    (left_side + (gamma * (inner_left2 - inner_right2))) / (4.0 * dely)
}

/// Calculate the discrete Laplacian using a five-point stencil
///
/// # Arguments
///
/// * `view` - A 3x3-element ArrayView2 representing the stencil of the array
///   to calculate the Laplacian over. This will most likely be u or v, meaning
///   view would represent u[(i-1) to (i+1), (j-1) to (j+1)] or
///   v[(i-1) to (i+1), (j-1) to (j+1)] respectively.
/// * `delx` - "delta x," the physical width of the cell
/// * `dely` - "delta y," the physical width of the cell
pub fn laplacian(view: ArrayView2<Real>, delx: Real, dely: Real) -> Real {
    // Since the view could be of u or v, we use "e" here to denote "element."
    let e_i_j = view[(1, 1)]; // e[(i, j)] -> e_i_j
    let e_i_j_m1 = view[(1, 0)]; // e[(i, j-1)] -> "e[i][j minus 1]" -> e_i_j_m1
    let e_i_j_p1 = view[(1, 2)]; // e[(i, j+1)] -> "e[i][j plus 1]" -> e_i_j_p1
    let e_i_m1_j = view[(0, 1)]; // e[(i-1, j)] -> "e[i minus 1][j]" -> e_i_m1_j
    let e_i_p1_j = view[(2, 1)]; // e[(i+1, j)] -> "e[i plus 1][j]" -> e_i_p1_j

    let d2edx2 = (e_i_p1_j - (2. * e_i_j) + e_i_m1_j) / delx.powi(2);
    let d2edy2 = (e_i_j_p1 - (2. * e_i_j) + e_i_j_m1) / dely.powi(2);

    d2edx2 + d2edy2
}

pub fn residual(p_view: ArrayView2<Real>, delx: Real, dely: Real, rhs: Real) -> Real {
    let p_i_p1_j = p_view[(2, 1)];
    let p_i_j = p_view[(1, 1)];
    let p_i_m1_j = p_view[(0, 1)];
    let p_i_j_p1 = p_view[(1, 2)];
    let p_i_j_m1 = p_view[(1, 0)];

    let part1 = ((p_i_p1_j - p_i_j) - (p_i_j - p_i_m1_j)) / delx.powi(2);
    let part2 = ((p_i_j_p1 - p_i_j) - (p_i_j - p_i_j_m1)) / dely.powi(2);
    part1 + part2 - rhs
}
#[cfg(test)]
mod tests {
    use super::*;
    use ndarray::{array, ArrayView2};

    #[test]
    fn test_du2dx() {
        // These don't have any particular significance, just some random data.
        let test_cases = [
            (
                array![[0., 1., 0.], [0., 2., 0.], [0., 3., 0.]],
                1.,
                1.7,
                3.15,
            ),
            (
                array![[0., -1., 0.], [0., 2., 0.], [0., 3., 0.]],
                1.,
                1.7,
                5.15,
            ),
            (
                array![[0., 1., 0.], [0., 1., 0.], [0., 1., 0.]],
                1.,
                1.7,
                0.,
            ),
            (
                array![[0., 1., 0.], [0., 2., 0.], [0., 3., 0.]],
                1.,
                1.3,
                3.35,
            ),
            (
                array![[0., 1., 0.], [0., 2., 0.], [0., 3., 0.]],
                1.5,
                1.7,
                2.1,
            ),
            (
                array![[0., 10., 0.], [0., 20., 0.], [0., 30., 0.]],
                1.5,
                1.7,
                210.,
            ),
        ];
        for (u, delx, gamma, expected) in test_cases {
            assert_eq!(du2dx(ArrayView2::from(&u), delx, gamma), expected);
        }
    }

    #[test]
    fn test_duvdx() {
        // These don't have any particular significance, just some random data.
        let test_cases = [
            (
                array![[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]],
                array![[8., 9., 10.], [11., 12., 13.], [14., 15., 16.]],
                1.,
                1.7,
                40.35,
            ),
            (
                array![[1., 2., 3.], [4., 5., -6.], [-7., 8., 9.]],
                array![[8., 9., 10.], [11., -12., 13.], [14., 15., -16.]],
                1.,
                1.7,
                -53.1,
            ),
            (
                array![[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]],
                array![[8., 9., 10.], [11., 12., 13.], [14., 15., 16.]],
                1.6,
                1.7,
                25.218750,
            ),
            (
                array![[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]],
                array![[8., 9., 10.], [11., 12., 13.], [14., 15., 16.]],
                1.,
                1.5,
                41.25,
            ),
        ];
        for (u, v, delx, gamma, expected) in test_cases {
            assert_eq!(
                duvdx(ArrayView2::from(&u), ArrayView2::from(&v), delx, gamma),
                expected
            );
        }
    }

    #[test]
    fn test_duvdy() {
        // These don't have any particular significance, just some random data.
        let test_cases = [
            (
                array![[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]],
                array![[8., 9., 10.], [11., 12., 13.], [14., 15., 16.]],
                1.,
                1.7,
                17.15,
            ),
            (
                array![[1., 2., 3.], [4., 5., -6.], [-7., 8., 9.]],
                array![[8., 9., 10.], [11., -12., 13.], [14., 15., -16.]],
                1.,
                1.7,
                -32.35,
            ),
            (
                array![[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]],
                array![[8., 9., 10.], [11., 12., 13.], [14., 15., 16.]],
                1.6,
                1.7,
                10.718749999999998,
            ),
            (
                array![[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]],
                array![[8., 9., 10.], [11., 12., 13.], [14., 15., 16.]],
                1.,
                1.5,
                17.25,
            ),
        ];
        for (u, v, dely, gamma, expected) in test_cases {
            assert_eq!(
                duvdy(ArrayView2::from(&u), ArrayView2::from(&v), dely, gamma),
                expected
            );
        }
    }

    #[test]
    fn test_dv2dy() {
        // These don't have any particular significance, just some random data.
        let test_cases = [
            (
                array![[0., 0., 0.], [1., 2., 3.], [0., 0., 0.]],
                1.,
                1.7,
                3.15,
            ),
            (
                array![[0., 0., 0.], [-1., 2., 3.], [0., 0., 0.]],
                1.,
                1.7,
                5.15,
            ),
            (
                array![[0., 0., 0.], [1., 1., 1.], [0., 0., 0.]],
                1.,
                1.7,
                0.,
            ),
            (
                array![[0., 0., 0.], [1., 2., 3.], [0., 0., 0.]],
                1.,
                1.3,
                3.35,
            ),
            (
                array![[0., 0., 0.], [1., 2., 3.], [0., 0., 0.]],
                1.5,
                1.7,
                2.1,
            ),
            (
                array![[0., 0., 0.], [10., 20., 30.], [0., 0., 0.]],
                1.5,
                1.7,
                210.,
            ),
        ];
        for (v, dely, gamma, expected) in test_cases {
            assert_eq!(dv2dy(ArrayView2::from(&v), dely, gamma), expected);
        }
    }

    #[test]
    fn test_lapacian() {
        // These don't have any particular significance, just some random data.
        let test_cases = [
            (
                array![[1., 4., 1.], [1., 5., 3.], [2., 1., 1.]],
                1.,
                1.,
                -11.,
            ),
            (
                array![[-1., -1., -1.], [-1., -5., -1.], [-1., -1., -1.]],
                1.,
                1.,
                16.,
            ),
            (
                array![[1., 4., 1.], [1., 5., 3.], [2., 1., 1.]],
                1.6,
                1.3,
                // NaSt2D actually has a slight difference in the least
                // significant digit. Not sure if that's due to differences in
                // calculation or imprecision in representing binary numbers in
                // decimal.
                -5.503420857988164,
            ),
            (
                array![[1., 4., 1.], [1., 5., 3.], [2., 1., 1.]],
                1.3,
                0.7,
                -15.20347784084048,
            ),
        ];
        for (e, delx, dely, expected) in test_cases {
            assert_eq!(laplacian(ArrayView2::from(&e), delx, dely), expected);
        }
    }
}