aeon-tk 0.3.3

Toolkit for running finite difference simulations with adaptive mesh refinement
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
use std::{
    array::from_fn,
    cmp::Ordering,
    fmt::{Display, Write},
};

use super::{index::IndexWindow, Split, CartesianIter, Face, IndexSpace};

/// Denotes where the region falls on a certain axis.
#[repr(u8)]
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub enum Side {
    Left = 0,
    Middle = 1,
    Right = 2,
}

impl Side {
    pub fn reverse(self) -> Self {
        match self {
            Self::Left => Self::Right,
            Self::Right => Self::Left,
            Self::Middle => Self::Middle,
        }
    }

    pub fn from_value(val: u8) -> Self {
        assert!(val < 3);
        // Safety. We have specified the memory representation of the
        // enum and checked the value, so this should be safe.
        unsafe { std::mem::transmute(val) }
    }
}

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub struct Region<const N: usize> {
    #[serde(with = "crate::array")]
    sides: [Side; N],
}

impl<const N: usize> Region<N> {
    /// Number of different regions in a given number of dimensions.
    pub const COUNT: usize = 3usize.pow(N as u32);
    /// The default "central" region.
    pub const CENTRAL: Self = Self::new([Side::Middle; N]);

    // Builds a new region from the given sides.
    pub const fn new(sides: [Side; N]) -> Self {
        Self { sides }
    }

    pub const fn sides(&self) -> [Side; N] {
        self.sides
    }

    pub const fn side(&self, axis: usize) -> Side {
        self.sides[axis]
    }

    pub fn set_side(&mut self, axis: usize, side: Side) {
        self.sides[axis] = side
    }

    /// Reverse every side in the region.
    pub fn reverse(&self) -> Self {
        let mut result = [Side::Left; N];

        for axis in 0..N {
            result[axis] = self.sides[axis].reverse();
        }

        Self::new(result)
    }

    /// Returns number of axes that are not `Side::Middle`.
    pub fn adjacency(&self) -> usize {
        self.sides
            .into_iter()
            .filter(|&s| s != Side::Middle)
            .count()
    }

    /// Iterates over all faces one would have to move over to get to the region.
    pub fn adjacent_faces(&self) -> impl Iterator<Item = Face<N>> + '_ {
        (0..N)
            .filter(|&axis| self.side(axis) != Side::Middle)
            .map(|axis| Face {
                axis,
                side: self.side(axis) == Side::Right,
            })
    }

    /// Iterates over all splits that can touch this region.
    pub fn adjacent_splits(self) -> impl Iterator<Item = Split<N>> {
        let origin: [_; N] = from_fn(|axis| match self.side(axis) {
            Side::Left | Side::Middle => 0,
            Side::Right => 1,
        });

        let size: [_; N] = from_fn(|axis| match self.side(axis) {
            Side::Middle => 2,
            _ => 1,
        });

        IndexWindow::new(origin, size)
            .iter()
            .map(|index| Split::pack(from_fn(|axis| index[axis] != 0)))
    }

    /// Computes a split which touches the given region.
    pub fn adjacent_split(&self) -> Split<N> {
        let mut result = Split::empty();
        for axis in 0..N {
            result.set_to(axis, self.side(axis) == Side::Right)
        }
        result
    }

    /// Checks whether a given split is adjacent to the region.
    pub fn is_split_adjacent(&self, split: Split<N>) -> bool {
        for axis in 0..N {
            match (self.side(axis), split.is_set(axis)) {
                (Side::Left, true) => return false,
                (Side::Right, false) => return false,
                _ => {}
            }
        }

        true
    }

    // /// Returns an index space with the same size as the region.
    // pub fn index_space(&self, support: usize, block: [usize; N]) -> IndexSpace<N> {
    //     let mut size = [0; N];

    //     for axis in 0..N {
    //         if self.sides[axis] != Side::Middle {
    //             size[axis] = support;
    //         } else {
    //             size[axis] = block[axis]
    //         }
    //     }

    //     IndexSpace::new(size)
    // }

    // /// Iterates nodes in the given region (including ghost nodes and nodes
    // /// on faces).
    // pub fn nodes(&self, support: usize, block: [usize; N]) -> RegionNodeIter<N> {
    //     RegionNodeIter {
    //         inner: self.index_space(support, block).iter(),
    //         block,
    //         sides: self.sides,
    //     }
    // }

    // pub fn face_vertices(&self, block: [usize; N]) -> RegionFaceVertexIter<N> {
    //     let mut size = [0; N];

    //     for axis in 0..N {
    //         size[axis] = match self.sides[axis] {
    //             Side::Left | Side::Right => 1,
    //             Side::Middle => block[axis],
    //         }
    //     }

    //     RegionFaceVertexIter {
    //         inner: IndexSpace::new(size).iter(),
    //         block,
    //         sides: self.sides,
    //     }
    // }

    // pub fn offset_nodes(&self, support: usize) -> RegionOffsetNodeIter<N> {
    //     let size = self.sides.map(|side| match side {
    //         Side::Left | Side::Right => support,
    //         Side::Middle => 1,
    //     });

    //     RegionOffsetNodeIter {
    //         inner: IndexSpace::new(size).iter(),
    //         sides: self.sides,
    //     }
    // }

    // pub fn offset_dir(&self) -> [isize; N] {
    //     self.sides.map(|side| match side {
    //         Side::Left => -1,
    //         Side::Right => 1,
    //         Side::Middle => 0,
    //     })
    // }

    // /// Returns a mask for which a given axis is set if and only if `self.sides[axis] != Side::Middle`.
    // pub fn to_mask(&self) -> AxisMask<N> {
    //     let mut result = AxisMask::empty();

    //     for axis in 0..N {
    //         result.set_to(axis, self.sides[axis] != Side::Middle);
    //     }

    //     result
    // }

    // pub fn masked(&self, mask: AxisMask<N>) -> Self {
    //     let mut sides = self.sides;

    //     for i in 0..N {
    //         if !mask.is_set(i) {
    //             sides[i] = Side::Middle;
    //         }
    //     }

    //     Self::new(sides)
    // }

    // pub fn masked_by_split(&self, split: AxisMask<N>) -> Self {
    //     let mut mask = AxisMask::empty();

    //     for axis in 0..N {
    //         match self.sides[axis] {
    //             Side::Left => mask.set_to(axis, !split.is_set(axis)),
    //             Side::Middle => mask.clear(axis),
    //             Side::Right => mask.set_to(axis, split.is_set(axis)),
    //         }
    //     }

    //     self.masked(mask)
    // }

    /// Converts the region into an integer value.
    pub fn to_linear(&self) -> usize {
        let space = IndexSpace::new([3; N]);
        let index = from_fn(|axis| self.side(axis) as usize);
        space.linear_from_cartesian(index)
    }

    /// Converts an integer value into a region.
    pub fn from_linear(val: usize) -> Self {
        let space = IndexSpace::new([3; N]);
        let index = space.cartesian_from_linear(val);
        Self::new(from_fn(|axis| Side::from_value(index[axis] as u8)))
    }
}

impl<const N: usize> Ord for Region<N> {
    fn cmp(&self, other: &Self) -> Ordering {
        let space = IndexSpace::new([3; N]);
        let index = from_fn(|axis| self.side(axis) as usize);
        let oindex = from_fn(|axis| other.side(axis) as usize);

        space
            .linear_from_cartesian(index)
            .cmp(&space.linear_from_cartesian(oindex))
    }
}

impl<const N: usize> PartialOrd for Region<N> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<const N: usize> Display for Region<N> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        for axis in 0..N {
            match self.side(axis) {
                Side::Left => f.write_char('-'),
                Side::Middle => f.write_char('='),
                Side::Right => f.write_char('+'),
            }?;
        }
        Ok(())
    }
}

// /// Allows iterating the nodes in a region.
// pub struct RegionNodeIter<const N: usize> {
//     inner: CartesianIter<N>,
//     block: [usize; N],
//     sides: [Side; N],
// }

// impl<const N: usize> Iterator for RegionNodeIter<N> {
//     type Item = [isize; N];

//     fn next(&mut self) -> Option<Self::Item> {
//         let cart = self.inner.next()?;

//         let mut result = [0isize; N];

//         for axis in 0..N {
//             result[axis] = match self.sides[axis] {
//                 Side::Left => -(cart[axis] as isize),
//                 Side::Right => (self.block[axis] + cart[axis]) as isize,
//                 Side::Middle => cart[axis] as isize,
//             }
//         }

//         Some(result)
//     }
// }

// /// Allows iterating the vertices on the face of a region.
// pub struct RegionFaceVertexIter<const N: usize> {
//     inner: CartesianIter<N>,
//     block: [usize; N],
//     sides: [Side; N],
// }

// impl<const N: usize> Iterator for RegionFaceVertexIter<N> {
//     type Item = [usize; N];

//     fn next(&mut self) -> Option<Self::Item> {
//         let cart = self.inner.next()?;

//         let mut result = [0; N];

//         for axis in 0..N {
//             result[axis] = match self.sides[axis] {
//                 Side::Left => 0,
//                 Side::Right => self.block[axis] - 1,
//                 Side::Middle => cart[axis],
//             }
//         }

//         Some(result)
//     }
// }

// pub struct RegionOffsetNodeIter<const N: usize> {
//     inner: CartesianIter<N>,
//     sides: [Side; N],
// }

// impl<const N: usize> Iterator for RegionOffsetNodeIter<N> {
//     type Item = [isize; N];

//     fn next(&mut self) -> Option<Self::Item> {
//         let cart = self.inner.next()?;

//         let mut result = [0isize; N];

//         for axis in 0..N {
//             result[axis] = match self.sides[axis] {
//                 Side::Left => -(cart[axis] as isize),
//                 Side::Right => cart[axis] as isize,
//                 Side::Middle => 0,
//             }
//         }

//         Some(result)
//     }
// }

pub struct RegionIter<const N: usize> {
    inner: CartesianIter<N>,
}

impl<const N: usize> Iterator for RegionIter<N> {
    type Item = Region<N>;

    fn next(&mut self) -> Option<Self::Item> {
        Some(Region::new(self.inner.next()?.map(|idx| match idx {
            0 => Side::Left,
            1 => Side::Middle,
            2 => Side::Right,
            _ => unreachable!(),
        })))
    }
}

impl<const N: usize> ExactSizeIterator for RegionIter<N> {
    fn len(&self) -> usize {
        Region::<N>::COUNT
    }
}

/// Iterates over all regions in an N-dimensional space.
pub fn regions<const N: usize>() -> RegionIter<N> {
    RegionIter {
        inner: IndexSpace::new([3; N]).iter(),
    }
}

#[cfg(test)]
mod tests {
    use crate::geometry::{Split, Face};

    use super::{regions, Region, Side};

    #[test]
    fn region_iteration() {
        let comparison = [
            [Side::Left, Side::Left],
            [Side::Middle, Side::Left],
            [Side::Right, Side::Left],
            [Side::Left, Side::Middle],
            [Side::Middle, Side::Middle],
            [Side::Right, Side::Middle],
            [Side::Left, Side::Right],
            [Side::Middle, Side::Right],
            [Side::Right, Side::Right],
        ];

        for (region, compare) in regions().zip(comparison.into_iter()) {
            assert_eq!(region, Region::new(compare));
        }
    }

    #[test]
    fn adjacency() {
        let region = Region::new([Side::Left, Side::Right]);
        assert_eq!(region.adjacency(), 2);

        let mut faces = region.adjacent_faces();
        assert_eq!(faces.next(), Some(Face::negative(0)));
        assert_eq!(faces.next(), Some(Face::positive(1)));
        assert_eq!(faces.next(), None);

        let mut splits = region.adjacent_splits();
        assert_eq!(splits.next(), Some(Split::pack([false, true])));
        assert_eq!(splits.next(), None);

        assert_eq!(region.adjacent_split(), Split::pack([false, true]));
    }
}