mesh-sieve 4.0.1

Modular, high-performance Rust library for mesh and data management, designed for scientific computing and PDE codes.
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
//! Orientation-aware closure DOF extraction in the style of PETSc DMPlex.
//!
//! A [`ClosureIndexCache`] stores reusable closure indices keyed by
//! `(cell, section_version, topology_version)`.  The indices flatten all DOFs
//! in a cell closure once, so repeated element assembly can call
//! [`get_closure`], [`set_closure`], or [`add_closure`] without re-traversing the
//! topology DAG.

use crate::data::section::Section;
use crate::data::storage::Storage;
use crate::mesh_error::MeshSieveError;
use crate::topology::point::PointId;
use crate::topology::sieve::{Orientation, OrientedSieve, Sieve};
use std::collections::{HashMap, HashSet, VecDeque};
use std::hash::Hash;
use std::ops::AddAssign;

/// Monotonic version supplied by the caller for topology changes.
pub type TopologyVersion = u64;

/// Stable cache key for closure indices.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ClosureIndexKey {
    /// Seed cell for the transitive closure.
    pub cell: PointId,
    /// [`Atlas`](crate::data::atlas::Atlas) version backing the section.
    pub section_version: u64,
    /// Caller-maintained topology version.
    pub topology_version: TopologyVersion,
}

/// Ordering policy for points in a cell closure.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ClosureOrder {
    /// Breadth-first DMPlex-style order: cell, cone, cone-of-cone, ... .
    BreadthFirstDmpLex,
    /// Deterministic lexicographic tensor order.  `dims` describes the tensor
    /// grid shape when known; this implementation uses stable point ids as the
    /// lexicographic coordinate surrogate for topological closures.
    LexicographicTensor {
        /// Tensor grid dimensions used by callers to describe the lexicographic shape.
        dims: Vec<usize>,
    },
    /// User-provided point order.  Points not listed are appended in
    /// breadth-first order.
    Custom(Vec<PointId>),
}

impl Default for ClosureOrder {
    fn default() -> Self {
        Self::BreadthFirstDmpLex
    }
}

/// One point-sized span inside a flattened closure vector.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ClosurePointIndex<O> {
    /// Topology point contributing these DOFs.
    pub point: PointId,
    /// Accumulated orientation from the closure seed to `point`.
    pub orientation: O,
    /// Offset in the flattened closure vector.
    pub offset: usize,
    /// Number of DOFs contributed by this point.
    pub len: usize,
    /// Maps closure-local DOF slots to section-local indices for this point.
    pub permutation: Vec<usize>,
}

/// Reusable flattened index for a closure.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ClosureIndex<O> {
    /// Cache key used to build this index.
    pub key: ClosureIndexKey,
    /// Point spans in flattened closure order.
    pub points: Vec<ClosurePointIndex<O>>,
    /// Total number of flattened DOFs.
    pub len: usize,
}

impl<O> ClosureIndex<O> {
    /// Closure points in flattened order.
    pub fn point_order(&self) -> impl Iterator<Item = PointId> + '_ {
        self.points.iter().map(|entry| entry.point)
    }
}

/// PetscSectionSym-like per-point orientation symmetries.
///
/// The returned permutation maps each closure-local DOF slot to the source
/// index within that point's section slice. Returning `None` means identity.
pub trait SectionSym<O> {
    /// Return the orientation-dependent DOF permutation for `point`.
    fn permutation(&self, point: PointId, orientation: O, dof_count: usize) -> Option<Vec<usize>>;
}

/// Identity section symmetry.
#[derive(Clone, Copy, Debug, Default)]
pub struct IdentitySectionSym;

impl<O: Copy> SectionSym<O> for IdentitySectionSym {
    #[inline]
    fn permutation(
        &self,
        _point: PointId,
        _orientation: O,
        _dof_count: usize,
    ) -> Option<Vec<usize>> {
        None
    }
}

/// Table-backed section symmetry keyed by `(point, orientation)`.
#[derive(Clone, Debug, Default)]
pub struct TableSectionSym<O> {
    by_point_orientation: HashMap<(PointId, O), Vec<usize>>,
    by_orientation: HashMap<O, Vec<usize>>,
}

impl<O> TableSectionSym<O>
where
    O: Copy + Eq + Hash,
{
    /// Create an empty table.
    pub fn new() -> Self {
        Self {
            by_point_orientation: HashMap::new(),
            by_orientation: HashMap::new(),
        }
    }

    /// Set a permutation used for a specific point and orientation.
    pub fn insert_point(&mut self, point: PointId, orientation: O, permutation: Vec<usize>) {
        self.by_point_orientation
            .insert((point, orientation), permutation);
    }

    /// Set a default permutation for an orientation, independent of point.
    pub fn insert_orientation(&mut self, orientation: O, permutation: Vec<usize>) {
        self.by_orientation.insert(orientation, permutation);
    }
}

impl<O> SectionSym<O> for TableSectionSym<O>
where
    O: Copy + Eq + Hash,
{
    fn permutation(&self, point: PointId, orientation: O, _dof_count: usize) -> Option<Vec<usize>> {
        self.by_point_orientation
            .get(&(point, orientation))
            .or_else(|| self.by_orientation.get(&orientation))
            .cloned()
    }
}

/// User-provided symmetry closure.
pub struct FnSectionSym<F>(pub F);

impl<O, F> SectionSym<O> for FnSectionSym<F>
where
    F: Fn(PointId, O, usize) -> Option<Vec<usize>>,
{
    fn permutation(&self, point: PointId, orientation: O, dof_count: usize) -> Option<Vec<usize>> {
        (self.0)(point, orientation, dof_count)
    }
}

/// Cache for repeated closure-index construction.
#[derive(Clone, Debug, Default)]
pub struct ClosureIndexCache<O> {
    entries: HashMap<ClosureIndexKey, ClosureIndex<O>>,
}

impl<O> ClosureIndexCache<O>
where
    O: Orientation + Eq + Hash,
{
    /// Create an empty cache.
    pub fn new() -> Self {
        Self {
            entries: HashMap::new(),
        }
    }

    /// Drop all cached indices.
    pub fn clear(&mut self) {
        self.entries.clear();
    }

    /// Return a cached index or build and insert it.
    pub fn get_or_build<T, V, Sct, Sym>(
        &mut self,
        topology: &T,
        section: &Section<V, Sct>,
        cell: PointId,
        topology_version: TopologyVersion,
        order: &ClosureOrder,
        sym: &Sym,
    ) -> Result<&ClosureIndex<O>, MeshSieveError>
    where
        T: OrientedSieve<Point = PointId, Orient = O>,
        Sct: Storage<V>,
        Sym: SectionSym<O>,
    {
        let key = ClosureIndexKey {
            cell,
            section_version: section.atlas().version(),
            topology_version,
        };
        if let std::collections::hash_map::Entry::Vacant(e) = self.entries.entry(key) {
            let index = build_closure_index(topology, section, cell, topology_version, order, sym)?;
            e.insert(index);
        }
        Ok(self.entries.get(&key).expect("inserted or present"))
    }
}

impl ClosureIndexCache<()> {
    /// Return a cached non-oriented index or build and insert it.
    pub fn get_or_build_unoriented<T, V, Sct, Sym>(
        &mut self,
        topology: &T,
        section: &Section<V, Sct>,
        cell: PointId,
        topology_version: TopologyVersion,
        order: &ClosureOrder,
        sym: &Sym,
    ) -> Result<&ClosureIndex<()>, MeshSieveError>
    where
        T: Sieve<Point = PointId>,
        Sct: Storage<V>,
        Sym: SectionSym<()>,
    {
        let key = ClosureIndexKey {
            cell,
            section_version: section.atlas().version(),
            topology_version,
        };
        if let std::collections::hash_map::Entry::Vacant(e) = self.entries.entry(key) {
            let index = build_closure_index_unoriented(
                topology,
                section,
                cell,
                topology_version,
                order,
                sym,
            )?;
            e.insert(index);
        }
        Ok(self.entries.get(&key).expect("inserted or present"))
    }
}

/// Build an orientation-aware closure index without caching.
pub fn build_closure_index<T, V, Sct, O, Sym>(
    topology: &T,
    section: &Section<V, Sct>,
    cell: PointId,
    topology_version: TopologyVersion,
    order: &ClosureOrder,
    sym: &Sym,
) -> Result<ClosureIndex<O>, MeshSieveError>
where
    T: OrientedSieve<Point = PointId, Orient = O>,
    Sct: Storage<V>,
    O: Orientation + Eq + Hash,
    Sym: SectionSym<O>,
{
    let key = ClosureIndexKey {
        cell,
        section_version: section.atlas().version(),
        topology_version,
    };
    let oriented_points = ordered_oriented_closure(topology, cell, order);
    let mut offset = 0usize;
    let mut points = Vec::with_capacity(oriented_points.len());
    for (point, orientation) in oriented_points {
        let len = match section.atlas().get(point) {
            Some((_, len)) => len,
            None => continue,
        };
        let permutation =
            normalized_permutation(point, len, sym.permutation(point, orientation, len))?;
        points.push(ClosurePointIndex {
            point,
            orientation,
            offset,
            len,
            permutation,
        });
        offset += len;
    }
    Ok(ClosureIndex {
        key,
        points,
        len: offset,
    })
}

/// Build a non-oriented closure index for a plain [`Sieve`].
pub fn build_closure_index_unoriented<T, V, Sct, Sym>(
    topology: &T,
    section: &Section<V, Sct>,
    cell: PointId,
    topology_version: TopologyVersion,
    order: &ClosureOrder,
    sym: &Sym,
) -> Result<ClosureIndex<()>, MeshSieveError>
where
    T: Sieve<Point = PointId>,
    Sct: Storage<V>,
    Sym: SectionSym<()>,
{
    let key = ClosureIndexKey {
        cell,
        section_version: section.atlas().version(),
        topology_version,
    };
    let points_only = ordered_closure(topology, cell, order);
    let mut offset = 0usize;
    let mut points = Vec::with_capacity(points_only.len());
    for point in points_only {
        let len = match section.atlas().get(point) {
            Some((_, len)) => len,
            None => continue,
        };
        let permutation = normalized_permutation(point, len, sym.permutation(point, (), len))?;
        points.push(ClosurePointIndex {
            point,
            orientation: (),
            offset,
            len,
            permutation,
        });
        offset += len;
    }
    Ok(ClosureIndex {
        key,
        points,
        len: offset,
    })
}

/// Extract closure DOFs using a precomputed index.
pub fn get_closure<V, Sct, O>(
    section: &Section<V, Sct>,
    index: &ClosureIndex<O>,
) -> Result<Vec<V>, MeshSieveError>
where
    V: Clone,
    Sct: Storage<V>,
{
    validate_section_version(section, index)?;
    let mut out = Vec::with_capacity(index.len);
    for entry in &index.points {
        let slice = section.try_restrict(entry.point)?;
        for &src in &entry.permutation {
            out.push(slice[src].clone());
        }
    }
    Ok(out)
}

/// Set closure DOFs using a precomputed index.
pub fn set_closure<V, Sct, O>(
    section: &mut Section<V, Sct>,
    index: &ClosureIndex<O>,
    values: &[V],
) -> Result<(), MeshSieveError>
where
    V: Clone,
    Sct: Storage<V>,
{
    validate_section_version(section, index)?;
    validate_closure_len(index, values.len())?;
    for entry in &index.points {
        let slice = section.try_restrict_mut(entry.point)?;
        for (local_slot, &section_slot) in entry.permutation.iter().enumerate() {
            slice[section_slot] = values[entry.offset + local_slot].clone();
        }
    }
    Ok(())
}

/// Add closure DOFs into a section using a precomputed index.
pub fn add_closure<V, Sct, O>(
    section: &mut Section<V, Sct>,
    index: &ClosureIndex<O>,
    values: &[V],
) -> Result<(), MeshSieveError>
where
    V: Clone + AddAssign<V>,
    Sct: Storage<V>,
{
    validate_section_version(section, index)?;
    validate_closure_len(index, values.len())?;
    for entry in &index.points {
        let slice = section.try_restrict_mut(entry.point)?;
        for (local_slot, &section_slot) in entry.permutation.iter().enumerate() {
            slice[section_slot] += values[entry.offset + local_slot].clone();
        }
    }
    Ok(())
}

/// Build an index and extract closure DOFs in one call.
pub fn get_closure_oriented<T, V, Sct, O, Sym>(
    topology: &T,
    section: &Section<V, Sct>,
    cell: PointId,
    topology_version: TopologyVersion,
    order: &ClosureOrder,
    sym: &Sym,
) -> Result<Vec<V>, MeshSieveError>
where
    T: OrientedSieve<Point = PointId, Orient = O>,
    V: Clone,
    Sct: Storage<V>,
    O: Orientation + Eq + Hash,
    Sym: SectionSym<O>,
{
    let index = build_closure_index(topology, section, cell, topology_version, order, sym)?;
    get_closure(section, &index)
}

fn validate_section_version<V, Sct, O>(
    section: &Section<V, Sct>,
    index: &ClosureIndex<O>,
) -> Result<(), MeshSieveError>
where
    Sct: Storage<V>,
{
    let current = section.atlas().version();
    if current != index.key.section_version {
        return Err(MeshSieveError::AtlasPlanStale {
            expected: index.key.section_version,
            found: current,
        });
    }
    Ok(())
}

fn validate_closure_len<O>(index: &ClosureIndex<O>, found: usize) -> Result<(), MeshSieveError> {
    if index.len != found {
        return Err(MeshSieveError::ScatterLengthMismatch {
            expected: index.len,
            found,
        });
    }
    Ok(())
}

fn normalized_permutation(
    point: PointId,
    len: usize,
    permutation: Option<Vec<usize>>,
) -> Result<Vec<usize>, MeshSieveError> {
    let perm = permutation.unwrap_or_else(|| (0..len).collect());
    if perm.len() != len {
        return Err(MeshSieveError::SliceLengthMismatch {
            point,
            expected: len,
            found: perm.len(),
        });
    }
    let mut seen = vec![false; len];
    for &idx in &perm {
        if idx >= len || seen[idx] {
            return Err(MeshSieveError::InvalidPermutation(format!(
                "invalid closure permutation for {point:?}: {perm:?}"
            )));
        }
        seen[idx] = true;
    }
    Ok(perm)
}

fn ordered_closure<T>(topology: &T, cell: PointId, order: &ClosureOrder) -> Vec<PointId>
where
    T: Sieve<Point = PointId>,
{
    let bfs = bfs_closure(topology, cell);
    apply_order(bfs, order)
}

fn ordered_oriented_closure<T, O>(
    topology: &T,
    cell: PointId,
    order: &ClosureOrder,
) -> Vec<(PointId, O)>
where
    T: OrientedSieve<Point = PointId, Orient = O>,
    O: Orientation + Eq + Hash,
{
    let bfs = bfs_oriented_closure(topology, cell);
    let ordered_points = apply_order(bfs.iter().map(|(point, _)| *point).collect(), order);
    let orientations: HashMap<PointId, O> = bfs.into_iter().collect();
    ordered_points
        .into_iter()
        .filter_map(|point| orientations.get(&point).copied().map(|o| (point, o)))
        .collect()
}

fn apply_order(mut bfs: Vec<PointId>, order: &ClosureOrder) -> Vec<PointId> {
    match order {
        ClosureOrder::BreadthFirstDmpLex => bfs,
        ClosureOrder::LexicographicTensor { .. } => {
            if let Some((&cell, rest)) = bfs.split_first() {
                let mut out = vec![cell];
                let mut rest = rest.to_vec();
                rest.sort_unstable();
                out.extend(rest);
                out
            } else {
                bfs
            }
        }
        ClosureOrder::Custom(custom) => {
            let bfs_set: HashSet<_> = bfs.iter().copied().collect();
            let mut used = HashSet::new();
            let mut out = Vec::with_capacity(bfs.len());
            for &point in custom {
                if bfs_set.contains(&point) && used.insert(point) {
                    out.push(point);
                }
            }
            for point in bfs.drain(..) {
                if used.insert(point) {
                    out.push(point);
                }
            }
            out
        }
    }
}

fn bfs_closure<T>(topology: &T, cell: PointId) -> Vec<PointId>
where
    T: Sieve<Point = PointId>,
{
    let mut seen = HashSet::new();
    let mut queue = VecDeque::from([cell]);
    let mut out = Vec::new();
    while let Some(point) = queue.pop_front() {
        if !seen.insert(point) {
            continue;
        }
        out.push(point);
        for child in topology.cone_points(point) {
            if !seen.contains(&child) {
                queue.push_back(child);
            }
        }
    }
    out
}

fn bfs_oriented_closure<T, O>(topology: &T, cell: PointId) -> Vec<(PointId, O)>
where
    T: OrientedSieve<Point = PointId, Orient = O>,
    O: Orientation + Eq + Hash,
{
    let mut seen = HashSet::new();
    let mut queue = VecDeque::from([(cell, O::default())]);
    let mut out = Vec::new();
    while let Some((point, orientation)) = queue.pop_front() {
        if !seen.insert(point) {
            continue;
        }
        out.push((point, orientation));
        for (child, edge_orientation) in topology.cone_o(point) {
            if !seen.contains(&child) {
                queue.push_back((child, O::compose(orientation, edge_orientation)));
            }
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::data::atlas::Atlas;
    use crate::data::storage::VecStorage;
    use crate::topology::orientation::Sign;
    use crate::topology::sieve::{InMemoryOrientedSieve, InMemorySieve};

    fn p(id: u64) -> PointId {
        PointId::new(id).unwrap()
    }

    #[test]
    fn get_set_add_unoriented_closure() {
        let mut topo = InMemorySieve::<PointId, ()>::default();
        topo.add_arrow(p(1), p(2), ());
        topo.add_arrow(p(1), p(3), ());

        let mut atlas = Atlas::default();
        atlas.try_insert(p(1), 1).unwrap();
        atlas.try_insert(p(2), 2).unwrap();
        atlas.try_insert(p(3), 1).unwrap();
        let mut section = Section::<i32, VecStorage<i32>>::new(atlas);
        section.try_scatter_in_order(&[10, 20, 21, 30]).unwrap();

        let index = build_closure_index_unoriented(
            &topo,
            &section,
            p(1),
            0,
            &ClosureOrder::BreadthFirstDmpLex,
            &IdentitySectionSym,
        )
        .unwrap();
        assert_eq!(get_closure(&section, &index).unwrap(), vec![10, 20, 21, 30]);

        set_closure(&mut section, &index, &[1, 2, 3, 4]).unwrap();
        add_closure(&mut section, &index, &[10, 20, 30, 40]).unwrap();
        assert_eq!(get_closure(&section, &index).unwrap(), vec![11, 22, 33, 44]);

        let mut cache = ClosureIndexCache::new();
        let cached = cache
            .get_or_build_unoriented(
                &topo,
                &section,
                p(1),
                0,
                &ClosureOrder::BreadthFirstDmpLex,
                &IdentitySectionSym,
            )
            .unwrap();
        assert_eq!(cached.key.cell, p(1));
    }

    #[test]
    fn oriented_symmetry_permutates_point_dofs() {
        let mut topo = InMemoryOrientedSieve::<PointId, (), Sign>::default();
        topo.add_arrow_o(p(1), p(2), (), Sign(true));

        let mut atlas = Atlas::default();
        atlas.try_insert(p(2), 3).unwrap();
        let mut section = Section::<i32, VecStorage<i32>>::new(atlas);
        section.try_scatter_in_order(&[1, 2, 3]).unwrap();

        let mut sym = TableSectionSym::new();
        sym.insert_orientation(Sign(true), vec![2, 1, 0]);
        let index = build_closure_index(
            &topo,
            &section,
            p(1),
            7,
            &ClosureOrder::BreadthFirstDmpLex,
            &sym,
        )
        .unwrap();
        assert_eq!(index.key.topology_version, 7);
        assert_eq!(get_closure(&section, &index).unwrap(), vec![3, 2, 1]);
    }

    #[test]
    fn custom_order_appends_unlisted_points() {
        let mut topo = InMemorySieve::<PointId, ()>::default();
        topo.add_arrow(p(1), p(2), ());
        topo.add_arrow(p(1), p(3), ());
        let ordered = ordered_closure(&topo, p(1), &ClosureOrder::Custom(vec![p(3)]));
        assert_eq!(ordered, vec![p(3), p(1), p(2)]);
    }
}