mesh_sieve/data/
discretization.rs1use crate::topology::cell_type::CellType;
4use std::collections::{BTreeMap, HashMap};
5
6#[derive(Clone, Debug, PartialEq, Eq, Hash)]
8pub enum RegionKey {
9 Label { name: String, value: i32 },
11 CellType(CellType),
13}
14
15impl RegionKey {
16 pub fn label(name: impl Into<String>, value: i32) -> Self {
18 Self::Label {
19 name: name.into(),
20 value,
21 }
22 }
23
24 pub fn cell_type(cell_type: CellType) -> Self {
26 Self::CellType(cell_type)
27 }
28}
29
30#[derive(Clone, Debug, PartialEq)]
32pub struct DiscretizationMetadata {
33 pub basis: String,
35 pub basis_order: Option<usize>,
37 pub shape_functions: Vec<String>,
39 pub quadrature: String,
41 pub quadrature_order: Option<usize>,
43 pub quadrature_points: Vec<Vec<f64>>,
45 pub quadrature_weights: Vec<f64>,
47}
48
49impl DiscretizationMetadata {
50 pub fn new(basis: impl Into<String>, quadrature: impl Into<String>) -> Self {
52 Self {
53 basis: basis.into(),
54 basis_order: None,
55 shape_functions: Vec::new(),
56 quadrature: quadrature.into(),
57 quadrature_order: None,
58 quadrature_points: Vec::new(),
59 quadrature_weights: Vec::new(),
60 }
61 }
62
63 pub fn with_basis_metadata(
65 mut self,
66 order: usize,
67 shape_functions: impl IntoIterator<Item = impl Into<String>>,
68 ) -> Self {
69 self.basis_order = Some(order);
70 self.shape_functions = shape_functions.into_iter().map(Into::into).collect();
71 self
72 }
73
74 pub fn with_quadrature_metadata(
76 mut self,
77 order: usize,
78 points: Vec<Vec<f64>>,
79 weights: Vec<f64>,
80 ) -> Self {
81 self.quadrature_order = Some(order);
82 self.quadrature_points = points;
83 self.quadrature_weights = weights;
84 self
85 }
86
87 pub fn has_quadrature_data(&self) -> bool {
89 !self.quadrature_points.is_empty()
90 && self.quadrature_points.len() == self.quadrature_weights.len()
91 }
92}
93
94#[derive(Clone, Debug, Default)]
96pub struct FieldDiscretization {
97 metadata: HashMap<RegionKey, DiscretizationMetadata>,
98}
99
100impl FieldDiscretization {
101 pub fn new() -> Self {
103 Self::default()
104 }
105
106 pub fn set_metadata(
108 &mut self,
109 region: RegionKey,
110 metadata: DiscretizationMetadata,
111 ) -> Option<DiscretizationMetadata> {
112 self.metadata.insert(region, metadata)
113 }
114
115 pub fn set_label_metadata(
117 &mut self,
118 name: impl Into<String>,
119 value: i32,
120 metadata: DiscretizationMetadata,
121 ) -> Option<DiscretizationMetadata> {
122 self.set_metadata(RegionKey::label(name, value), metadata)
123 }
124
125 pub fn set_cell_type_metadata(
127 &mut self,
128 cell_type: CellType,
129 metadata: DiscretizationMetadata,
130 ) -> Option<DiscretizationMetadata> {
131 self.set_metadata(RegionKey::cell_type(cell_type), metadata)
132 }
133
134 pub fn metadata_for(&self, region: &RegionKey) -> Option<&DiscretizationMetadata> {
136 self.metadata.get(region)
137 }
138
139 pub fn iter(&self) -> impl Iterator<Item = (&RegionKey, &DiscretizationMetadata)> {
141 self.metadata.iter()
142 }
143}
144
145#[derive(Clone, Debug, Default)]
147pub struct Discretization {
148 fields: BTreeMap<String, FieldDiscretization>,
149}
150
151impl Discretization {
152 pub fn new() -> Self {
154 Self::default()
155 }
156
157 pub fn insert_field(
159 &mut self,
160 name: impl Into<String>,
161 field: FieldDiscretization,
162 ) -> Option<FieldDiscretization> {
163 self.fields.insert(name.into(), field)
164 }
165
166 pub fn field(&self, name: &str) -> Option<&FieldDiscretization> {
168 self.fields.get(name)
169 }
170
171 pub fn field_mut(&mut self, name: &str) -> &mut FieldDiscretization {
173 self.fields.entry(name.to_string()).or_default()
174 }
175
176 pub fn iter(&self) -> impl Iterator<Item = (&str, &FieldDiscretization)> {
178 self.fields
179 .iter()
180 .map(|(name, field)| (name.as_str(), field))
181 }
182}