bunsen 0.22.2

bunsen is a batteries included common library for burn
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
//! # Space Primitives
use burn::{
    Tensor,
    module::Module,
    prelude::{
        Backend,
        ElementConversion,
        Int,
    },
    tensor::{
        DType,
        DType::F32,
    },
};

use crate::contracts::unpack_shape_contract;

/// D2Q9 Direction Indices
///
/// # Returns
///
/// The `[VY=3, VX=3, (VY, VX)=2]` int direction indices.
pub fn direction_indices<B: Backend>(device: &B::Device) -> Tensor<B, 3, Int> {
    Tensor::<B, 3, Int>::from_data(
        [
            [[-1, -1], [-1, 0], [-1, 1]],
            [[0, -1], [0, 0], [0, 1]],
            [[1, -1], [1, 0], [1, 1]],
        ],
        device,
    )
}

/// D2Q9 Direction Vectors
///
/// # Returns
///
/// The `[VY=3, VX=3, (VY, VX)=2]` float direction vectors.
pub fn direction_vectors<B: Backend>(device: &B::Device) -> Tensor<B, 3> {
    direction_indices(device).float()
}

/// D2Q9 Equilibrium Weight Matrix
///
/// # Returns
///
/// The `[VY=3, VX=3]` equilibrium weight matrix.
pub fn weight_matrix<B: Backend>(device: &B::Device) -> Tensor<B, 2> {
    Tensor::<B, 2>::from_data(
        [
            [1.0 / 36.0, 1.0 / 9.0, 1.0 / 36.0],
            [1.0 / 9.0, 4.0 / 9.0, 1.0 / 9.0],
            [1.0 / 36.0, 1.0 / 9.0, 1.0 / 36.0],
        ],
        device,
    )
}

/// LBM Space Constants
///
/// Bundles the static D2Q9 lattice tables: the integer direction indices
/// (`e_idx`), the float direction vectors (`e_vec`), and the equilibrium weight
/// matrix (`w`). Built via [`LbmTables::init`] (or [`LbmTables::for_dist`]) for
/// a device and dtype rather than from a Config.
///
/// Used by [`LBMD2Q9State`](super::LBMD2Q9State).
#[derive(Module, Debug)]
pub struct LbmTables<B: Backend> {
    /// `[H, W, (Y, X)=2]` integer direction indices.
    e_idx: Tensor<B, 3, Int>,

    /// `[H, W, (Y, X)=2]` float direction vectors.
    e_vec: Tensor<B, 3>,

    /// `[Y=3, X=3]` equilibrium weights
    w: Tensor<B, 2>,
}

impl<B: Backend> LbmTables<B> {
    /// Creates new space `lbm_tables`.
    pub fn init(device: &B::Device) -> Self {
        let e_idx = direction_indices(device);
        let e_vec = e_idx.clone().float();
        Self {
            e_idx,
            e_vec,
            w: weight_matrix(device),
        }
    }

    /// Returns appropriate `lbm_tables` for the distribution.
    pub fn for_dist(dist: &Tensor<B, 4>) -> Self {
        Self::init(&dist.device()).to_dtype(dist.dtype())
    }

    /// Casts the `lbm_tables` to the given dtype.
    pub fn to_dtype(
        self,
        dtype: DType,
    ) -> Self {
        Self {
            e_vec: self.e_vec.cast(dtype),
            w: self.w.cast(dtype),
            ..self
        }
    }

    /// Returns the direction indices.
    pub fn e_idx(&self) -> Tensor<B, 3, Int> {
        self.e_idx.clone()
    }

    /// Returns the direction vectors.
    pub fn e_vec(&self) -> Tensor<B, 3> {
        self.e_vec.clone()
    }

    /// Returns the equilibrium weight matrix.
    pub fn w(&self) -> Tensor<B, 2> {
        self.w.clone()
    }
}

/// Prints a distribution.
///
/// Doesn't work well on big distributions.
///
/// # Arguments
///
/// - `label`: the label to use.
/// - `dist`: the dist to print.
pub fn dbg_dist<B: Backend>(
    label: &str,
    dist: Tensor<B, 4>,
) {
    let [height, width] = unpack_shape_contract!(
        ["h", "w", "vy", "vx"],
        dist.shape().as_slice(),
        &["h", "w"],
        &[("vy", 3), ("vx", 3)]
    );

    let total_energy: f32 = dist.clone().sum().into_scalar().elem();
    println!("{label}: {total_energy:<8.2e}");

    let data = dist.cast(F32).to_data().to_vec::<f32>().unwrap();

    fn cross_bar_line(width: usize) {
        print!("+");
        for _ in 0..width {
            for _ in 0..3 {
                print!(" --------");
            }
            print!(" +");
        }
        println!();
    }

    cross_bar_line(width);

    for h in 0..height {
        for vy in 0..3 {
            print!("|");
            for w in 0..width {
                for vx in 0..3 {
                    let v = data[(h * width * 3 * 3) + (w * 3 * 3) + (vy * 3) + vx];
                    print!(" {:>8.2e}", v);
                }
                print!(" |");
            }
            println!();
        }

        cross_bar_line(width);
    }
}

/// Population Density
///
/// # Arguments
///
/// - `dist`: a `[H, W, VY=3, VX=3]` population distribution.
///
/// # Returns
///
/// A `[H, W]` population density.
pub fn density<B: Backend>(dist: Tensor<B, 4>) -> Tensor<B, 2> {
    dist.sum_dims(&[2, 3]).squeeze_dims::<2>(&[2, 3])
}

/// Computes the directional macroscopic momentum.
///
/// This is the unnormalized macroscopic momentum.
///
/// # Arguments
///
/// - `dist`: a `[H, W, VY, VX]` population distribution.
/// - `e`: the D2Q9 direction vectors.
///
/// # Returns
///
/// The `[H, W, (Y, X)=2]` momentum.
pub fn macroscopic_momentum<B: Backend>(
    dist: Tensor<B, 4>,
    e: Tensor<B, 3>,
) -> Tensor<B, 3> {
    dist.unsqueeze_dims::<5>(&[-1])
        .mul(e.unsqueeze::<5>())
        .sum_dims(&[2, 3])
        .squeeze_dims::<3>(&[2, 3])
}

/// Computes directional velocity from macroscopic momentum.
///
/// # Arguments
///
/// - `m`: `[H, W, (Y, X)=2]` macroscopic momentum.
/// - `rho`: `[H, W]` population density.
///
/// # Returns
///
/// The `[H, W, (Y, X)=2]` velocity.
pub fn normalize_velocity<B: Backend>(
    m: Tensor<B, 3>,
    rho: Tensor<B, 2>,
) -> Tensor<B, 3> {
    // TODO: div-by-zero check?
    // .clamp_min(1e-15)?
    m.div(rho.unsqueeze_dim(2))
}

/// Computes the directional macroscopic velocity.
///
/// # Arguments
///
/// - `dist`: a `[H, W, VY, VX]` population distribution.
/// - `e`: the D2Q9 direction vectors.
///
/// # Returns
/// - `[H, W, (Y, X)=2]` velocity.
pub fn macroscopic_velocity<B: Backend>(
    dist: Tensor<B, 4>,
    rho: Tensor<B, 2>,
    e: Tensor<B, 3>,
) -> Tensor<B, 3> {
    normalize_velocity(macroscopic_momentum(dist, e), rho)
}

/// Computes the squared velocity field.
///
/// # Arguments
/// - `u`: `[H, W, (Y, X)=2]` macroscopic velocity
///
/// # Returns
/// - `[H, W]` velocity magnitude squared
pub fn velocity_squared<B: Backend>(u: Tensor<B, 3>) -> Tensor<B, 2> {
    u.square().sum_dim(2).squeeze_dims::<2>(&[2])
}

/// Computes the first (density) and second (macro velocity) moments.
///
/// # Arguments
///
/// - `dist`: a `[H, W, VY, VX]` population distribution.
/// - `e`: the D2Q9 direction vectors.
///
/// # Returns
/// ``(density, velocity)`` where:
/// - `density`: `[H, W]`
/// - `velocity`: `[H, W, (Y, X)=2]`
pub fn moments<B: Backend>(
    dist: Tensor<B, 4>,
    lbm_tables: &LbmTables<B>,
) -> (Tensor<B, 2>, Tensor<B, 3>) {
    let rho = density(dist.clone());
    let u = macroscopic_velocity(dist, rho.clone(), lbm_tables.e_vec());
    (rho, u)
}

/// Folds a distribution into windows.
///
/// Note the geometry: `[H-2, W-2, VY=3, VX=3, WIN_Y=3, WIN_X=3]`
/// - ``(H, W)``: the spatial position of the "current" cell.
/// - ``(WIN_Y, WIN_X)``: the current window; with the current cell in the
///   center.
/// - ``(VY, VX)``: the 3x3 distribution in each cell.
///
/// # Arguments
/// - `dist`: a `[H, W, VY=3, VX=3]` distribution.
///
/// # Returns
/// `[H, W, VY=3, VX=3, WIN_Y=3, WIN_X=3]` folded windows.
pub fn dist_windows<B: Backend>(dist: Tensor<B, 4>) -> Tensor<B, 6> {
    dist.unfold::<5, _>(0, 3, 1).unfold::<6, _>(1, 3, 1)
}

#[cfg(test)]
mod tests {
    use burn::tensor::Tolerance;
    use serial_test::serial;

    use super::{
        super::velocity_squared,
        *,
    };
    use crate::support::testing::PerformanceBackend;

    #[test]
    #[serial]
    fn test_population_density() {
        type B = PerformanceBackend;
        let device = Default::default();

        let dist: Tensor<B, 4> = Tensor::from_data(
            [
                [
                    [[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]],
                    [[10., 20., 30.], [40., 50., 60.], [70., 80., 90.]],
                ],
                [
                    [[9., 10., 3.], [4., 5., 6.], [7., 8., 9.]],
                    [[0., -2., 0.], [0., 8., 0.], [0., 0., 0.]],
                ],
            ],
            &device,
        );

        let rho = density(dist.clone());

        rho.to_data().assert_approx_eq::<f32>(
            &Tensor::<B, 2>::from_data([[45., 450.], [61., 6.]], &device).to_data(),
            Tolerance::default(),
        )
    }

    #[test]
    #[serial]
    fn test_direction_vectors() {
        type B = PerformanceBackend;
        let device = Default::default();

        let e: Tensor<B, 3> = direction_vectors(&device);

        e.to_data().assert_eq(
            &Tensor::<B, 3>::from_data(
                [
                    [[-1., -1.], [-1., 0.], [-1., 1.]],
                    [[0., -1.], [0., 0.], [0., 1.]],
                    [[1., -1.], [1., 0.], [1., 1.]],
                ],
                &device,
            )
            .to_data(),
            false,
        );
    }

    #[test]
    #[serial]
    fn test_weight_matrix() {
        type B = PerformanceBackend;
        let device = Default::default();

        let w: Tensor<B, 2> = weight_matrix(&device);

        w.to_data().assert_eq(
            &Tensor::<B, 2>::from_data(
                [
                    [1.0 / 36.0, 1.0 / 9.0, 1.0 / 36.0],
                    [1.0 / 9.0, 4.0 / 9.0, 1.0 / 9.0],
                    [1.0 / 36.0, 1.0 / 9.0, 1.0 / 36.0],
                ],
                &device,
            )
            .to_data(),
            false,
        );
    }

    #[test]
    #[serial]
    fn test_momentum_and_velocity() {
        type B = PerformanceBackend;
        let device = Default::default();

        let dist: Tensor<B, 4> = Tensor::from_data(
            [[
                [[1., 0., 0.], [0., 10., 0.], [0., 0., 0.]],
                [[1., 2., 3.], [4., 10., 5.], [6., 7., 8.]],
            ]],
            &device,
        );

        let lbm_tables = LbmTables::for_dist(&dist);

        let momentum = macroscopic_momentum(dist.clone(), lbm_tables.e_vec());

        momentum.clone().to_data().assert_approx_eq::<f32>(
            &Tensor::<B, 3>::from_data([[[-1., -1.], [15., 5.]]], &device).to_data(),
            Tolerance::default(),
        );

        let (rho, u) = moments(dist.clone(), &lbm_tables);

        let rho_data = rho.to_data().to_vec::<f32>().unwrap();

        u.clone().to_data().assert_approx_eq::<f32>(
            &Tensor::<B, 3>::from_data(
                [[
                    [-1. / rho_data[0], -1. / rho_data[0]],
                    [15. / rho_data[1], 5. / rho_data[1]],
                ]],
                &device,
            )
            .to_data(),
            Tolerance::default(),
        );

        let v_sq = velocity_squared(u.clone());

        v_sq.clone().to_data().assert_approx_eq::<f32>(
            &Tensor::<B, 2>::from_data(
                [[
                    (1. + 1.) / rho_data[0].powi(2),
                    (15. * 15. + 5. * 5.) / rho_data[1].powi(2),
                ]],
                &device,
            )
            .to_data(),
            Tolerance::default(),
        );
    }
}