Skip to main content

mesh_sieve/io/
mod.rs

1//! Mesh I/O helpers for sieve topology and sections.
2//!
3//! This module provides trait-based readers and writers for loading and
4//! saving `Sieve` topologies together with associated `Section` data.
5
6pub mod bundle;
7pub mod cgns;
8pub mod exodus;
9pub mod fluent;
10pub mod gmsh;
11pub mod hdf5;
12pub mod partitioned;
13pub mod petsc_hdf5;
14pub mod ply;
15pub mod vtk;
16pub mod xdmf;
17
18use crate::data::coordinates::Coordinates;
19use crate::data::discretization::Discretization;
20use crate::data::mixed_section::MixedSectionStore;
21use crate::data::section::Section;
22use crate::data::storage::Storage;
23use crate::mesh_error::MeshSieveError;
24use crate::topology::cell_type::CellType;
25use crate::topology::labels::LabelSet;
26use crate::topology::sieve::Sieve;
27use std::collections::BTreeMap;
28use std::io::{Read, Write};
29
30pub use bundle::MeshBundle;
31pub use partitioned::{
32    GatherPolicy, NeighborLinks, OverlapLink, OverlapMetadata, PartitionedMeshData,
33    PartitionedMeshMetadata, PartitionedMeshWriteOutcome, read_partitioned_mesh,
34    write_local_piece_with_metadata, write_partitioned_mesh,
35};
36
37/// Combined sieve and section data returned by I/O readers.
38#[derive(Debug)]
39pub struct MeshData<S, V, St, CtSt>
40where
41    St: Storage<V>,
42    CtSt: Storage<CellType>,
43{
44    /// The mesh topology as a sieve.
45    pub sieve: S,
46    /// Optional coordinate section wrapper.
47    pub coordinates: Option<Coordinates<V, St>>,
48    /// Named sections keyed by user-provided identifiers.
49    pub sections: BTreeMap<String, Section<V, St>>,
50    /// Tagged sections with mixed scalar types.
51    pub mixed_sections: MixedSectionStore,
52    /// Optional integer labels associated with mesh points.
53    pub labels: Option<LabelSet>,
54    /// Optional cell type section over mesh points.
55    pub cell_types: Option<Section<CellType, CtSt>>,
56    /// Optional discretization metadata keyed by regions.
57    pub discretization: Option<Discretization>,
58}
59
60impl<S, V, St, CtSt> MeshData<S, V, St, CtSt>
61where
62    St: Storage<V>,
63    CtSt: Storage<CellType>,
64{
65    /// Create an empty container with a sieve and no sections.
66    pub fn new(sieve: S) -> Self {
67        Self {
68            sieve,
69            coordinates: None,
70            sections: BTreeMap::new(),
71            mixed_sections: MixedSectionStore::default(),
72            labels: None,
73            cell_types: None,
74            discretization: None,
75        }
76    }
77}
78
79/// Trait for mesh readers that produce sieve + section data.
80pub trait SieveSectionReader {
81    /// Sieve implementation returned by the reader.
82    type Sieve: Sieve;
83    /// Scalar value stored in sections.
84    type Value;
85    /// Storage backend for section data.
86    type Storage: Storage<Self::Value>;
87    /// Storage backend for cell type data.
88    type CellStorage: Storage<CellType>;
89
90    /// Parse mesh data from a reader.
91    fn read<R: Read>(
92        &self,
93        reader: R,
94    ) -> Result<MeshData<Self::Sieve, Self::Value, Self::Storage, Self::CellStorage>, MeshSieveError>;
95}
96
97/// Trait for mesh writers that serialize sieve + section data.
98pub trait SieveSectionWriter {
99    /// Sieve implementation expected by the writer.
100    type Sieve: Sieve;
101    /// Scalar value stored in sections.
102    type Value;
103    /// Storage backend for section data.
104    type Storage: Storage<Self::Value>;
105    /// Storage backend for cell type data.
106    type CellStorage: Storage<CellType>;
107
108    /// Write mesh data to a writer.
109    fn write<W: Write>(
110        &self,
111        writer: W,
112        mesh: &MeshData<Self::Sieve, Self::Value, Self::Storage, Self::CellStorage>,
113    ) -> Result<(), MeshSieveError>;
114}