legendre 0.1.0

Block-structured, deterministic, scheduler-driven PDE simulation framework
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
//! Uniform block-structured Cartesian grid, generic over dimension.
//!
//! The global domain of `cells[d]` interior cells per dimension is tiled by
//! equal blocks of `block_cells[d]` cells. Every field slab for a block is
//! stored dense, dimension-0-fastest, with a ghost ring of the field's
//! declared width:
//!
//! ```text
//! linear(idx) = Σ_d (idx[d] + ghost) · stride[d],   stride[0] = 1,
//! stride[d+1] = stride[d] · (block_cells[d] + 2·ghost)
//! ```
//!
//! Views do this arithmetic inline; after monomorphization a stencil read
//! `u.get([1, 0])` is a single indexed load.

use super::grid::{BlockId, Grid};
use crate::{
    core::{
        state::{FieldHandle, State},
        storage::{Scalar, StorageBackend},
    },
    geometry::GridError,
};

/// Uniform Cartesian grid of `cells` interior cells tiled by congruent
/// blocks; see the module docs for the storage layout.
#[derive(Debug, Clone)]
pub struct CartesianGrid<const D: usize> {
    cells: [usize; D],
    block_cells: [usize; D],
    blocks_per_dim: [usize; D],
    origin: [f64; D],
    spacing: [f64; D],
}

impl<const D: usize> CartesianGrid<D> {
    /// A domain of `cells` interior cells tiled by `block_cells`-sized
    /// blocks. `cells[d]` must be divisible by `block_cells[d]` — v1 keeps
    /// all blocks congruent, which makes storage layout and halo exchange
    /// uniform.
    ///
    /// # Errors
    ///
    /// Returns a [`GridError`] if any dimension is empty, `cells[d]` is not
    /// divisible by `block_cells[d]`, or `spacing[d]` is not a positive
    /// finite number.
    pub fn new(
        cells: [usize; D],
        block_cells: [usize; D],
        origin: [f64; D],
        spacing: [f64; D],
    ) -> Result<Self, GridError> {
        let mut blocks_per_dim = [0usize; D];
        for d in 0..D {
            if !(cells[d] > 0 && block_cells[d] > 0) {
                return Err(GridError::EmptyDimension(d));
            }
            if !cells[d].is_multiple_of(block_cells[d]) {
                return Err(GridError::IndivisibleDimension {
                    dimension: d,
                    cells: cells[d],
                    block_cells: block_cells[d],
                });
            }
            if spacing[d] <= 0.0 || !spacing[d].is_finite() {
                return Err(GridError::InvalidSpacing {
                    dimension: d,
                    spacing: spacing[d],
                });
            }
            blocks_per_dim[d] = cells[d] / block_cells[d];
        }
        Ok(Self {
            cells,
            block_cells,
            blocks_per_dim,
            origin,
            spacing,
        })
    }

    /// Whole domain as one block — convenient for small runs and tests.
    ///
    /// # Errors
    ///
    /// Same conditions as [`Self::new`].
    pub fn single_block(
        cells: [usize; D],
        origin: [f64; D],
        spacing: [f64; D],
    ) -> Result<Self, GridError> {
        Self::new(cells, cells, origin, spacing)
    }

    /// Interior cells per dimension over the whole domain.
    #[must_use]
    pub const fn cells(&self) -> [usize; D] {
        self.cells
    }

    /// Interior cells per dimension of one block.
    #[must_use]
    pub const fn block_cells(&self) -> [usize; D] {
        self.block_cells
    }

    /// Number of blocks per dimension.
    #[must_use]
    pub const fn blocks_per_dim(&self) -> [usize; D] {
        self.blocks_per_dim
    }

    /// Multi-index of a block, dimension 0 fastest.
    #[allow(clippy::needless_range_loop)]
    #[must_use]
    pub fn block_coords(&self, block: BlockId) -> [usize; D] {
        let mut rem = block.index();
        let mut out = [0usize; D];
        for d in 0..D {
            out[d] = rem % self.blocks_per_dim[d];
            rem /= self.blocks_per_dim[d];
        }
        debug_assert_eq!(rem, 0, "block id out of range");
        out
    }

    /// Neighbor block across face `(dim, +1/-1)`, or `None` at the domain
    /// boundary. This is the topology query halo exchange is built on.
    #[allow(clippy::needless_range_loop)]
    #[must_use]
    pub fn face_neighbor(&self, block: BlockId, dim: usize, dir: isize) -> Option<BlockId> {
        let coords = self.block_coords(block);
        let next = coords[dim] as isize + dir.signum();
        if next < 0 || next as usize >= self.blocks_per_dim[dim] {
            return None;
        }
        let mut id = 0usize;
        let mut stride = 1usize;
        for d in 0..D {
            let c = if d == dim { next as usize } else { coords[d] };
            id += c * stride;
            stride *= self.blocks_per_dim[d];
        }
        Some(BlockId(id as u32))
    }

    fn dims_with_ghosts(&self, ghost: u32) -> [usize; D] {
        let g = ghost as usize;
        std::array::from_fn(|d| self.block_cells[d] + 2 * g)
    }
}

#[inline(always)]
fn linearize<const D: usize>(idx: [isize; D], interior: [usize; D], ghost: u32) -> usize {
    let g = ghost as isize;
    let mut offset = 0usize;
    let mut stride = 1usize;
    for d in 0..D {
        let shifted = idx[d] + g;
        debug_assert!(
            shifted >= 0 && (shifted as usize) < interior[d] + 2 * ghost as usize,
            "index {idx:?} outside block extent {interior:?} with ghost {ghost}"
        );
        offset += shifted as usize * stride;
        stride *= interior[d] + 2 * ghost as usize;
    }
    offset
}

/// Read view of one block's slab. Indices are relative to the interior
/// origin; ghosts sit at `-1..-ghost` and `n..n+ghost-1`.
#[derive(Debug, Clone, Copy)]
pub struct CartesianView<'a, T: Scalar, const D: usize> {
    data: &'a [T],
    interior: [usize; D],
    ghost: u32,
}

impl<T: Scalar, const D: usize> CartesianView<'_, T, D> {
    /// Value at `idx` (ghost cells addressable with negative indices).
    #[inline(always)]
    #[must_use]
    pub fn get(&self, idx: [isize; D]) -> T {
        self.data[linearize(idx, self.interior, self.ghost)]
    }

    /// Interior extent of the block.
    #[must_use]
    pub const fn interior(&self) -> [usize; D] {
        self.interior
    }

    /// Ghost-ring width of the viewed field.
    #[must_use]
    pub const fn ghost(&self) -> u32 {
        self.ghost
    }
}

/// Mutable view of one block's slab.
#[derive(Debug)]
pub struct CartesianViewMut<'a, T: Scalar, const D: usize> {
    data: &'a mut [T],
    interior: [usize; D],
    ghost: u32,
}

impl<T: Scalar, const D: usize> CartesianViewMut<'_, T, D> {
    /// Value at `idx` (ghost cells addressable with negative indices).
    #[inline(always)]
    #[must_use]
    pub fn get(&self, idx: [isize; D]) -> T {
        self.data[linearize(idx, self.interior, self.ghost)]
    }

    /// Write `value` at `idx`.
    #[inline(always)]
    pub fn set(&mut self, idx: [isize; D], value: T) {
        self.data[linearize(idx, self.interior, self.ghost)] = value;
    }

    /// Interior extent of the block.
    #[must_use]
    pub const fn interior(&self) -> [usize; D] {
        self.interior
    }

    /// Ghost-ring width of the viewed field.
    #[must_use]
    pub const fn ghost(&self) -> u32 {
        self.ghost
    }
}

/// Visit every interior index of a block extent, dimension 0 fastest. The
/// workhorse of concrete stencil loops.
pub fn for_each_interior<const D: usize>(interior: [usize; D], mut f: impl FnMut([isize; D])) {
    if interior.contains(&0) {
        return;
    }
    let mut idx = [0isize; D];
    loop {
        f(idx);
        let mut d = 0;
        loop {
            idx[d] += 1;
            if (idx[d] as usize) < interior[d] {
                break;
            }
            idx[d] = 0;
            d += 1;
            if d == D {
                return;
            }
        }
    }
}

/// Visit every index in the half-open box `lo..hi` (signed, so ghost ranges
/// are expressible), dimension 0 fastest.
pub fn for_each_box<const D: usize>(lo: [isize; D], hi: [isize; D], mut f: impl FnMut([isize; D])) {
    if (0..D).any(|d| lo[d] >= hi[d]) {
        return;
    }
    let mut idx = lo;
    loop {
        f(idx);
        let mut d = 0;
        loop {
            idx[d] += 1;
            if idx[d] < hi[d] {
                break;
            }
            idx[d] = lo[d];
            d += 1;
            if d == D {
                return;
            }
        }
    }
}

/// Fill one field's ghost cells: halo exchange across interior block faces,
/// mirror (no-flux) at physical boundaries.
///
/// Works dimension by dimension; each sweep `d` writes only `d`-ghost strips
/// and reads only `d`-interior strips, spanning the *full* ghost-inclusive
/// extent in the other dimensions. After all `D` sweeps, edge and corner
/// ghosts are consistent — explicit face-then-corner copies, generalized
/// to any block count and dimension. Sequential over blocks for now; the sweeps
/// are embarrassingly parallel per block if this ever shows up in profiles.
pub fn fill_ghosts_mirror<T: Scalar, S: StorageBackend<T>, const D: usize>(
    grid: &CartesianGrid<D>,
    state: &mut State<T, S>,
    handle: FieldHandle<T>,
) {
    let ghost = state.layout().ghost(handle.index());
    if ghost == 0 {
        return;
    }
    let g = ghost as isize;
    let n_cells = grid.block_cells();
    for d in 0..D {
        // Strip extent: ghost range in dimension d, full ghost-inclusive
        // range everywhere else.
        let mut lo = [0isize; D];
        let mut hi = [0isize; D];
        for e in 0..D {
            lo[e] = -g;
            hi[e] = n_cells[e] as isize + g;
        }
        let n = n_cells[d] as isize;
        for b in 0..grid.num_blocks() {
            let block = BlockId(b as u32);
            for dir in [-1isize, 1] {
                // Ghost strip on this side: idx[d] ∈ [-g, 0) or [n, n+g).
                (lo[d], hi[d]) = if dir < 0 { (-g, 0) } else { (n, n + g) };
                if let Some(nb) = grid.face_neighbor(block, d, dir) {
                    // My ghost layer k mirrors the neighbor's interior:
                    // low side: idx[d] = -1-k  ←  neighbor n-1-k
                    // high side: idx[d] = n+k  ←  neighbor k
                    let (dst, src) = state.slab_pair_mut(block, nb, handle);
                    let mut vd = grid.view_mut(block, ghost, dst);
                    let vs = grid.view(nb, ghost, src);
                    for_each_box(lo, hi, |idx| {
                        let mut sidx = idx;
                        sidx[d] = if dir < 0 { n + idx[d] } else { idx[d] - n };
                        vd.set(idx, vs.get(sidx));
                    });
                } else {
                    // Physical boundary: mirror across the face,
                    // ghost -1-k ← interior k (and n+k ← n-1-k).
                    let slab = state.slab_mut(block, handle);
                    let mut v = grid.view_mut(block, ghost, slab);
                    for_each_box(lo, hi, |idx| {
                        let mut sidx = idx;
                        sidx[d] = if dir < 0 {
                            -1 - idx[d]
                        } else {
                            2 * n - 1 - idx[d]
                        };
                        v.set(idx, v.get(sidx));
                    });
                }
            }
        }
    }
}

impl<const D: usize> Grid for CartesianGrid<D> {
    const DIM: usize = D;
    type Point = [f64; D];
    type Index = [isize; D];
    type View<'a, T: Scalar> = CartesianView<'a, T, D>;
    type ViewMut<'a, T: Scalar> = CartesianViewMut<'a, T, D>;

    fn num_blocks(&self) -> usize {
        self.blocks_per_dim.iter().product()
    }

    fn block_len(&self, _block: BlockId, ghost: u32) -> usize {
        self.dims_with_ghosts(ghost).iter().product()
    }

    fn level(&self, _block: BlockId) -> u8 {
        0
    }

    fn spacing(&self, _block: BlockId) -> [f64; D] {
        self.spacing
    }

    #[allow(clippy::needless_range_loop)]
    fn cell_center(&self, block: BlockId, idx: [isize; D]) -> [f64; D] {
        let coords = self.block_coords(block);
        let mut out = [0.0f64; D];
        for d in 0..D {
            let global = coords[d] * self.block_cells[d];
            out[d] = (global as f64 + idx[d] as f64 + 0.5).mul_add(self.spacing[d], self.origin[d]);
        }
        out
    }

    fn view<'a, T: Scalar>(
        &'a self,
        block: BlockId,
        ghost: u32,
        data: &'a [T],
    ) -> CartesianView<'a, T, D> {
        debug_assert_eq!(data.len(), self.block_len(block, ghost));
        CartesianView {
            data,
            interior: self.block_cells,
            ghost,
        }
    }

    fn view_mut<'a, T: Scalar>(
        &'a self,
        block: BlockId,
        ghost: u32,
        data: &'a mut [T],
    ) -> CartesianViewMut<'a, T, D> {
        debug_assert_eq!(data.len(), self.block_len(block, ghost));
        CartesianViewMut {
            data,
            interior: self.block_cells,
            ghost,
        }
    }
}