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
//! Mixed-type section storage with tagged scalar types.

use crate::data::atlas::Atlas;
use crate::data::section::Section;
use crate::data::storage::VecStorage;
use crate::mesh_error::MeshSieveError;
use std::collections::BTreeMap;

/// Scalar type tag for mixed sections.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ScalarType {
    F64,
    F32,
    I32,
    I64,
    U32,
    U64,
}

impl ScalarType {
    /// Returns a stable string label for the scalar type.
    pub fn as_str(self) -> &'static str {
        match self {
            ScalarType::F64 => "f64",
            ScalarType::F32 => "f32",
            ScalarType::I32 => "i32",
            ScalarType::I64 => "i64",
            ScalarType::U32 => "u32",
            ScalarType::U64 => "u64",
        }
    }

    /// Parse a scalar type from a string label.
    pub fn parse(tag: &str) -> Option<Self> {
        match tag {
            "f64" => Some(ScalarType::F64),
            "f32" => Some(ScalarType::F32),
            "i32" => Some(ScalarType::I32),
            "i64" => Some(ScalarType::I64),
            "u32" => Some(ScalarType::U32),
            "u64" => Some(ScalarType::U64),
            _ => None,
        }
    }
}

/// Tagged, type-erased section storage for mixed scalar types.
#[derive(Clone, Debug)]
pub enum TaggedSection {
    F64(Section<f64, VecStorage<f64>>),
    F32(Section<f32, VecStorage<f32>>),
    I32(Section<i32, VecStorage<i32>>),
    I64(Section<i64, VecStorage<i64>>),
    U32(Section<u32, VecStorage<u32>>),
    U64(Section<u64, VecStorage<u64>>),
}

impl TaggedSection {
    /// Return the scalar type tag for this section.
    pub fn scalar_type(&self) -> ScalarType {
        match self {
            TaggedSection::F64(_) => ScalarType::F64,
            TaggedSection::F32(_) => ScalarType::F32,
            TaggedSection::I32(_) => ScalarType::I32,
            TaggedSection::I64(_) => ScalarType::I64,
            TaggedSection::U32(_) => ScalarType::U32,
            TaggedSection::U64(_) => ScalarType::U64,
        }
    }

    /// Return the atlas backing this tagged section.
    pub fn atlas(&self) -> &Atlas {
        match self {
            TaggedSection::F64(section) => section.atlas(),
            TaggedSection::F32(section) => section.atlas(),
            TaggedSection::I32(section) => section.atlas(),
            TaggedSection::I64(section) => section.atlas(),
            TaggedSection::U32(section) => section.atlas(),
            TaggedSection::U64(section) => section.atlas(),
        }
    }

    /// Gather values from this tagged section in atlas insertion order.
    pub fn gather_in_order(&self) -> TaggedSectionBuffer {
        match self {
            TaggedSection::F64(section) => TaggedSectionBuffer::F64(section.gather_in_order()),
            TaggedSection::F32(section) => TaggedSectionBuffer::F32(section.gather_in_order()),
            TaggedSection::I32(section) => TaggedSectionBuffer::I32(section.gather_in_order()),
            TaggedSection::I64(section) => TaggedSectionBuffer::I64(section.gather_in_order()),
            TaggedSection::U32(section) => TaggedSectionBuffer::U32(section.gather_in_order()),
            TaggedSection::U64(section) => TaggedSectionBuffer::U64(section.gather_in_order()),
        }
    }

    /// Scatter values into this tagged section in atlas insertion order.
    pub fn try_scatter_in_order(
        &mut self,
        buf: &TaggedSectionBuffer,
    ) -> Result<(), MeshSieveError> {
        match (self, buf) {
            (TaggedSection::F64(section), TaggedSectionBuffer::F64(data)) => {
                section.try_scatter_in_order(data)
            }
            (TaggedSection::F32(section), TaggedSectionBuffer::F32(data)) => {
                section.try_scatter_in_order(data)
            }
            (TaggedSection::I32(section), TaggedSectionBuffer::I32(data)) => {
                section.try_scatter_in_order(data)
            }
            (TaggedSection::I64(section), TaggedSectionBuffer::I64(data)) => {
                section.try_scatter_in_order(data)
            }
            (TaggedSection::U32(section), TaggedSectionBuffer::U32(data)) => {
                section.try_scatter_in_order(data)
            }
            (TaggedSection::U64(section), TaggedSectionBuffer::U64(data)) => {
                section.try_scatter_in_order(data)
            }
            (section, buf) => Err(MeshSieveError::TaggedSectionTypeMismatch {
                expected: section.scalar_type(),
                found: buf.scalar_type(),
            }),
        }
    }
}

/// Typed buffer for tagged section scatter/gather operations.
#[derive(Clone, Debug)]
pub enum TaggedSectionBuffer {
    F64(Vec<f64>),
    F32(Vec<f32>),
    I32(Vec<i32>),
    I64(Vec<i64>),
    U32(Vec<u32>),
    U64(Vec<u64>),
}

impl TaggedSectionBuffer {
    /// Scalar type tag for this buffer.
    pub fn scalar_type(&self) -> ScalarType {
        match self {
            TaggedSectionBuffer::F64(_) => ScalarType::F64,
            TaggedSectionBuffer::F32(_) => ScalarType::F32,
            TaggedSectionBuffer::I32(_) => ScalarType::I32,
            TaggedSectionBuffer::I64(_) => ScalarType::I64,
            TaggedSectionBuffer::U32(_) => ScalarType::U32,
            TaggedSectionBuffer::U64(_) => ScalarType::U64,
        }
    }

    /// Length of the underlying flat buffer.
    pub fn len(&self) -> usize {
        match self {
            TaggedSectionBuffer::F64(data) => data.len(),
            TaggedSectionBuffer::F32(data) => data.len(),
            TaggedSectionBuffer::I32(data) => data.len(),
            TaggedSectionBuffer::I64(data) => data.len(),
            TaggedSectionBuffer::U32(data) => data.len(),
            TaggedSectionBuffer::U64(data) => data.len(),
        }
    }

    /// Return true if the buffer is empty.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

/// Trait to map scalar types to tagged sections for typed accessors.
pub trait MixedScalar: Sized + 'static {
    /// Scalar type tag for this concrete type.
    const SCALAR_TYPE: ScalarType;

    /// Wrap a typed section into a tagged container.
    fn wrap(section: Section<Self, VecStorage<Self>>) -> TaggedSection;
    /// Borrow a typed section if the tag matches.
    fn unwrap(section: &TaggedSection) -> Option<&Section<Self, VecStorage<Self>>>;
    /// Mutably borrow a typed section if the tag matches.
    fn unwrap_mut(section: &mut TaggedSection) -> Option<&mut Section<Self, VecStorage<Self>>>;
}

impl MixedScalar for f64 {
    const SCALAR_TYPE: ScalarType = ScalarType::F64;

    fn wrap(section: Section<Self, VecStorage<Self>>) -> TaggedSection {
        TaggedSection::F64(section)
    }

    fn unwrap(section: &TaggedSection) -> Option<&Section<Self, VecStorage<Self>>> {
        if let TaggedSection::F64(section) = section {
            Some(section)
        } else {
            None
        }
    }

    fn unwrap_mut(section: &mut TaggedSection) -> Option<&mut Section<Self, VecStorage<Self>>> {
        if let TaggedSection::F64(section) = section {
            Some(section)
        } else {
            None
        }
    }
}

impl MixedScalar for f32 {
    const SCALAR_TYPE: ScalarType = ScalarType::F32;

    fn wrap(section: Section<Self, VecStorage<Self>>) -> TaggedSection {
        TaggedSection::F32(section)
    }

    fn unwrap(section: &TaggedSection) -> Option<&Section<Self, VecStorage<Self>>> {
        if let TaggedSection::F32(section) = section {
            Some(section)
        } else {
            None
        }
    }

    fn unwrap_mut(section: &mut TaggedSection) -> Option<&mut Section<Self, VecStorage<Self>>> {
        if let TaggedSection::F32(section) = section {
            Some(section)
        } else {
            None
        }
    }
}

impl MixedScalar for i32 {
    const SCALAR_TYPE: ScalarType = ScalarType::I32;

    fn wrap(section: Section<Self, VecStorage<Self>>) -> TaggedSection {
        TaggedSection::I32(section)
    }

    fn unwrap(section: &TaggedSection) -> Option<&Section<Self, VecStorage<Self>>> {
        if let TaggedSection::I32(section) = section {
            Some(section)
        } else {
            None
        }
    }

    fn unwrap_mut(section: &mut TaggedSection) -> Option<&mut Section<Self, VecStorage<Self>>> {
        if let TaggedSection::I32(section) = section {
            Some(section)
        } else {
            None
        }
    }
}

impl MixedScalar for i64 {
    const SCALAR_TYPE: ScalarType = ScalarType::I64;

    fn wrap(section: Section<Self, VecStorage<Self>>) -> TaggedSection {
        TaggedSection::I64(section)
    }

    fn unwrap(section: &TaggedSection) -> Option<&Section<Self, VecStorage<Self>>> {
        if let TaggedSection::I64(section) = section {
            Some(section)
        } else {
            None
        }
    }

    fn unwrap_mut(section: &mut TaggedSection) -> Option<&mut Section<Self, VecStorage<Self>>> {
        if let TaggedSection::I64(section) = section {
            Some(section)
        } else {
            None
        }
    }
}

impl MixedScalar for u32 {
    const SCALAR_TYPE: ScalarType = ScalarType::U32;

    fn wrap(section: Section<Self, VecStorage<Self>>) -> TaggedSection {
        TaggedSection::U32(section)
    }

    fn unwrap(section: &TaggedSection) -> Option<&Section<Self, VecStorage<Self>>> {
        if let TaggedSection::U32(section) = section {
            Some(section)
        } else {
            None
        }
    }

    fn unwrap_mut(section: &mut TaggedSection) -> Option<&mut Section<Self, VecStorage<Self>>> {
        if let TaggedSection::U32(section) = section {
            Some(section)
        } else {
            None
        }
    }
}

impl MixedScalar for u64 {
    const SCALAR_TYPE: ScalarType = ScalarType::U64;

    fn wrap(section: Section<Self, VecStorage<Self>>) -> TaggedSection {
        TaggedSection::U64(section)
    }

    fn unwrap(section: &TaggedSection) -> Option<&Section<Self, VecStorage<Self>>> {
        if let TaggedSection::U64(section) = section {
            Some(section)
        } else {
            None
        }
    }

    fn unwrap_mut(section: &mut TaggedSection) -> Option<&mut Section<Self, VecStorage<Self>>> {
        if let TaggedSection::U64(section) = section {
            Some(section)
        } else {
            None
        }
    }
}

/// Store named sections with mixed scalar types.
#[derive(Clone, Debug, Default)]
pub struct MixedSectionStore {
    sections: BTreeMap<String, TaggedSection>,
}

impl MixedSectionStore {
    /// Create an empty mixed section store.
    pub fn new() -> Self {
        Self::default()
    }

    /// Insert a typed section into the store.
    pub fn insert<T: MixedScalar>(
        &mut self,
        name: impl Into<String>,
        section: Section<T, VecStorage<T>>,
    ) -> Option<TaggedSection> {
        self.sections.insert(name.into(), T::wrap(section))
    }

    /// Insert a tagged section into the store.
    pub fn insert_tagged(
        &mut self,
        name: impl Into<String>,
        section: TaggedSection,
    ) -> Option<TaggedSection> {
        self.sections.insert(name.into(), section)
    }

    /// Retrieve a typed section by name.
    pub fn get<T: MixedScalar>(&self, name: &str) -> Option<&Section<T, VecStorage<T>>> {
        self.sections.get(name).and_then(T::unwrap)
    }

    /// Retrieve a mutable typed section by name.
    pub fn get_mut<T: MixedScalar>(
        &mut self,
        name: &str,
    ) -> Option<&mut Section<T, VecStorage<T>>> {
        self.sections.get_mut(name).and_then(T::unwrap_mut)
    }

    /// Retrieve a tagged section by name.
    pub fn get_tagged(&self, name: &str) -> Option<&TaggedSection> {
        self.sections.get(name)
    }

    /// Iterate over all named tagged sections.
    pub fn iter(&self) -> impl Iterator<Item = (&String, &TaggedSection)> {
        self.sections.iter()
    }

    /// Mutably iterate over all named tagged sections.
    pub fn iter_mut(&mut self) -> impl Iterator<Item = (&String, &mut TaggedSection)> {
        self.sections.iter_mut()
    }

    /// Return true if the store is empty.
    pub fn is_empty(&self) -> bool {
        self.sections.is_empty()
    }

    /// Gather all tagged sections into flat buffers in atlas insertion order.
    pub fn gather_in_order(&self) -> BTreeMap<String, TaggedSectionBuffer> {
        self.sections
            .iter()
            .map(|(name, section)| (name.clone(), section.gather_in_order()))
            .collect()
    }

    /// Scatter all tagged sections from flat buffers in atlas insertion order.
    pub fn try_scatter_in_order(
        &mut self,
        buffers: &BTreeMap<String, TaggedSectionBuffer>,
    ) -> Result<(), MeshSieveError> {
        for (name, section) in &mut self.sections {
            let buf = buffers
                .get(name)
                .ok_or_else(|| MeshSieveError::MissingSectionName { name: name.clone() })?;
            section.try_scatter_in_order(buf)?;
        }
        Ok(())
    }
}