-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathquadrature_table.rs
More file actions
485 lines (438 loc) · 16.2 KB
/
quadrature_table.rs
File metadata and controls
485 lines (438 loc) · 16.2 KB
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
use crate::nalgebra::allocator::Allocator;
use crate::nalgebra::{DefaultAllocator, DimName, OPoint, Scalar};
use crate::quadrature::QuadraturePair;
use crate::util::NestedVec;
use crate::SmallDim;
use itertools::izip;
use nalgebra::{U1, U2, U3};
use serde::{Deserialize, Serialize};
/// Lookup table mapping elements to quadrature rules.
pub trait QuadratureTable<T, GeometryDim>
where
T: Scalar,
GeometryDim: SmallDim,
DefaultAllocator: Allocator<T, GeometryDim>,
{
type Data: Default + Clone;
fn element_quadrature_size(&self, element_index: usize) -> usize;
fn populate_element_data(&self, element_index: usize, data: &mut [Self::Data]);
fn populate_element_quadrature(
&self,
element_index: usize,
points: &mut [OPoint<T, GeometryDim>],
weights: &mut [T],
);
fn populate_element_quadrature_and_data(
&self,
element_index: usize,
points: &mut [OPoint<T, GeometryDim>],
weights: &mut [T],
data: &mut [Self::Data],
) {
self.populate_element_quadrature(element_index, points, weights);
self.populate_element_data(element_index, data);
}
}
/// Trait alias for a one-dimensional quadrature table.
pub trait QuadratureTable1d<T: Scalar>: QuadratureTable<T, U1> {}
/// Trait alias for a two-dimensional quadrature table.
pub trait QuadratureTable2d<T: Scalar>: QuadratureTable<T, U2> {}
/// Trait alias for a three-dimensional quadrature table.
pub trait QuadratureTable3d<T: Scalar>: QuadratureTable<T, U3> {}
impl<T: Scalar, Table: QuadratureTable<T, U1>> QuadratureTable1d<T> for Table {}
impl<T: Scalar, Table: QuadratureTable<T, U2>> QuadratureTable2d<T> for Table {}
impl<T: Scalar, Table: QuadratureTable<T, U3>> QuadratureTable3d<T> for Table {}
/// A quadrature table that keeps a separate quadrature rule per element.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct GeneralQuadratureTable<T, GeometryDim, Data = ()>
where
T: Scalar,
GeometryDim: DimName,
DefaultAllocator: Allocator<T, GeometryDim>,
{
#[serde(bound(serialize = "OPoint<T, GeometryDim>: Serialize"))]
#[serde(bound(deserialize = "OPoint<T, GeometryDim>: Deserialize<'de>"))]
points: NestedVec<OPoint<T, GeometryDim>>,
weights: NestedVec<T>,
data: NestedVec<Data>,
}
fn unit_data_table_for_weights<T>(points: &NestedVec<T>) -> NestedVec<()> {
let mut data = NestedVec::new();
for i in 0..points.len() {
data.push(&vec![(); points.get(i).unwrap().len()]);
}
data
}
impl<T, GeometryDim> GeneralQuadratureTable<T, GeometryDim>
where
T: Scalar,
GeometryDim: DimName,
DefaultAllocator: Allocator<T, GeometryDim>,
{
pub fn from_points_and_weights(points: NestedVec<OPoint<T, GeometryDim>>, weights: NestedVec<T>) -> Self {
let data = unit_data_table_for_weights(&weights);
Self::from_points_weights_and_data(points, weights, data)
}
}
/// Checks that the provided quadrature rules are consistent, in the sense that
/// the number of elements for each table is identical, and that each rule has
/// consistent numbers of points, weights and data entries.
fn check_rules_consistency<T, D, Data>(points: &NestedVec<OPoint<T, D>>, weights: &NestedVec<T>, data: &NestedVec<Data>)
where
T: Scalar,
D: DimName,
DefaultAllocator: Allocator<T, D>,
{
assert_eq!(
points.len(),
weights.len(),
"Quadrature point and weight tables must have the same number of rules."
);
assert_eq!(
points.len(),
data.len(),
"Quadrature point and data tables must have the same number of rules."
);
// Ensure that each element has a consistent quadrature rule
let iter = izip!(points.iter(), weights.iter(), data.iter());
for (element_index, (element_points, element_weights, element_data)) in iter.enumerate() {
assert_eq!(
element_points.len(),
element_weights.len(),
"Element {} has mismatched number of points and weights.",
element_index
);
assert_eq!(
element_points.len(),
element_data.len(),
"Element {} has mismatched number of points and data.",
element_index
);
}
}
impl<T, GeometryDim, Data> GeneralQuadratureTable<T, GeometryDim, Data>
where
T: Scalar,
GeometryDim: DimName,
DefaultAllocator: Allocator<T, GeometryDim>,
{
pub fn from_points_weights_and_data(
points: NestedVec<OPoint<T, GeometryDim>>,
weights: NestedVec<T>,
data: NestedVec<Data>,
) -> Self {
check_rules_consistency(&points, &weights, &data);
Self { points, weights, data }
}
pub fn into_parts(self) -> GeneralQuadratureParts<T, GeometryDim, Data> {
GeneralQuadratureParts {
points: self.points,
weights: self.weights,
data: self.data,
}
}
/// Replaces the data of the quadrature table with the given data.
pub fn with_data<NewData>(self, data: NestedVec<NewData>) -> GeneralQuadratureTable<T, GeometryDim, NewData> {
GeneralQuadratureTable::from_points_weights_and_data(self.points, self.weights, data)
}
/// Replaces the data of the quadrature table by calling the given closure with every quadrature
/// point in reference coordinates and its element index.
pub fn with_data_from_fn<NewData>(
self,
mut data_fn: impl FnMut(usize, &OPoint<T, GeometryDim>, &Data) -> NewData,
) -> GeneralQuadratureTable<T, GeometryDim, NewData> {
let mut data = NestedVec::new();
for (element_index, (points, datas)) in self.points.iter().zip(self.data.iter()).enumerate() {
let mut arr = data.begin_array();
for (point, data) in points.iter().zip(datas.iter()) {
arr.push_single(data_fn(element_index, point, data));
}
}
self.with_data(data)
}
}
pub struct GeneralQuadratureParts<T, GeometryDim, Data>
where
T: Scalar,
GeometryDim: DimName,
DefaultAllocator: Allocator<T, GeometryDim>,
{
pub points: NestedVec<OPoint<T, GeometryDim>>,
pub weights: NestedVec<T>,
pub data: NestedVec<Data>,
}
impl<T, GeometryDim, Data> QuadratureTable<T, GeometryDim> for GeneralQuadratureTable<T, GeometryDim, Data>
where
T: Scalar,
GeometryDim: SmallDim,
Data: Clone + Default,
DefaultAllocator: Allocator<T, GeometryDim>,
{
type Data = Data;
fn element_quadrature_size(&self, element_index: usize) -> usize {
// TODO: Should we rather return results from all these methods? It seems that currently
// we are just panicking if the quadrature table size doesn't match the number of elements
// in the finite element space. This seems bad.
self.weights
.get(element_index)
.expect("Element index out of bounds")
.len()
}
fn populate_element_data(&self, element_index: usize, data: &mut [Self::Data]) {
let data_for_element = self
.data
.get(element_index)
.expect("Element index out of bounds");
assert_eq!(data_for_element.len(), data.len());
data.clone_from_slice(&data_for_element);
}
fn populate_element_quadrature(
&self,
element_index: usize,
points: &mut [OPoint<T, GeometryDim>],
weights: &mut [T],
) {
let points_for_element = self
.points
.get(element_index)
.expect("Element index out of bounds");
let weights_for_element = self
.weights
.get(element_index)
.expect("Element index out of bounds");
assert_eq!(points_for_element.len(), points.len());
assert_eq!(weights_for_element.len(), weights.len());
points.clone_from_slice(&points_for_element);
weights.clone_from_slice(&weights_for_element);
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct UniformQuadratureTable<T, GeometryDim, Data = ()>
where
T: Scalar,
GeometryDim: DimName,
DefaultAllocator: Allocator<T, GeometryDim>,
{
#[serde(bound(serialize = "OPoint<T, GeometryDim>: Serialize"))]
#[serde(bound(deserialize = "OPoint<T, GeometryDim>: Deserialize<'de>"))]
points: Vec<OPoint<T, GeometryDim>>,
weights: Vec<T>,
data: Vec<Data>,
}
impl<T, GeometryDim> UniformQuadratureTable<T, GeometryDim>
where
T: Scalar,
GeometryDim: DimName,
DefaultAllocator: Allocator<T, GeometryDim>,
{
pub fn from_points_and_weights(points: Vec<OPoint<T, GeometryDim>>, weights: Vec<T>) -> Self {
let data = vec![(); points.len()];
Self::from_points_weights_and_data(points, weights, data)
}
pub fn from_quadrature(quadrature: QuadraturePair<T, GeometryDim>) -> Self {
Self::from_quadrature_and_uniform_data(quadrature, ())
}
}
impl<T, GeometryDim, Data> UniformQuadratureTable<T, GeometryDim, Data>
where
T: Scalar,
GeometryDim: DimName,
DefaultAllocator: Allocator<T, GeometryDim>,
{
pub fn from_points_weights_and_data(points: Vec<OPoint<T, GeometryDim>>, weights: Vec<T>, data: Vec<Data>) -> Self {
let msg = "Points, weights and data must have the same length.";
assert_eq!(points.len(), weights.len(), "{}", msg);
assert_eq!(points.len(), data.len(), "{}", msg);
Self { points, weights, data }
}
pub fn from_quadrature_and_uniform_data(quadrature: QuadraturePair<T, GeometryDim>, data: Data) -> Self
where
Data: Clone,
{
let (weights, points) = quadrature;
let data = vec![data; weights.len()];
Self::from_points_weights_and_data(points, weights, data)
}
pub fn with_uniform_data<Data2: Clone>(self, data: Data2) -> UniformQuadratureTable<T, GeometryDim, Data2> {
UniformQuadratureTable::from_quadrature_and_uniform_data((self.weights, self.points), data)
}
}
impl<T, GeometryDim, Data> UniformQuadratureTable<T, GeometryDim, Data>
where
T: Scalar,
GeometryDim: DimName,
Data: Clone,
DefaultAllocator: Allocator<T, GeometryDim>,
{
pub fn to_general(&self, num_elements: usize) -> GeneralQuadratureTable<T, GeometryDim, Data> {
let mut points = NestedVec::new();
let mut weights = NestedVec::new();
let mut data = NestedVec::new();
for _ in 0..num_elements {
points.push(&self.points);
weights.push(&self.weights);
data.push(&self.data);
}
GeneralQuadratureTable::from_points_weights_and_data(points, weights, data)
}
}
impl<T, GeometryDim, Data> QuadratureTable<T, GeometryDim> for UniformQuadratureTable<T, GeometryDim, Data>
where
T: Scalar,
GeometryDim: SmallDim,
Data: Clone + Default,
DefaultAllocator: Allocator<T, GeometryDim>,
{
type Data = Data;
fn element_quadrature_size(&self, _element_index: usize) -> usize {
self.points.len()
}
fn populate_element_data(&self, _element_index: usize, data: &mut [Self::Data]) {
assert_eq!(data.len(), self.data.len());
data.clone_from_slice(&self.data);
}
fn populate_element_quadrature(
&self,
_element_index: usize,
points: &mut [OPoint<T, GeometryDim>],
weights: &mut [T],
) {
assert_eq!(points.len(), self.points.len());
assert_eq!(weights.len(), self.weights.len());
points.clone_from_slice(&self.points);
weights.clone_from_slice(&self.weights);
}
}
/// A general quadrature table that avoids duplication of identical rules.
///
/// In a nutshell, [`CompactQuadratureTable`] sits in between [`UniformQuadratureTable`]
/// and [`GeneralQuadratureTable`]. Like [`GeneralQuadratureTable`], it can store an arbitrary
/// rule per element, but it uses a layer of indirection so that `M` quadrature rules
/// are associated with `N` elements.
///
/// This can be useful in settings where many elements share the same quadrature data, such
/// as a finite element space with elements of different degrees, or the common case
/// where the elements are the same but different quadrature data is needed in different
/// regions of the mesh.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CompactQuadratureTable<T, D, Data = ()>
where
T: Scalar,
D: DimName,
DefaultAllocator: Allocator<T, D>,
{
#[serde(bound(serialize = "OPoint<T, D>: Serialize"))]
#[serde(bound(deserialize = "OPoint<T, D>: Deserialize<'de>"))]
points: NestedVec<OPoint<T, D>>,
weights: NestedVec<T>,
data: NestedVec<Data>,
element_to_rule_map: Vec<usize>,
}
impl<T, D> CompactQuadratureTable<T, D>
where
T: Scalar,
D: DimName,
DefaultAllocator: Allocator<T, D>,
{
pub fn from_points_weights_and_map(
points: NestedVec<OPoint<T, D>>,
weights: NestedVec<T>,
element_to_rule_map: Vec<usize>,
) -> Self {
let data = unit_data_table_for_weights(&weights);
Self::from_quadrature_rules_and_map(points, weights, data, element_to_rule_map)
}
}
impl<T, D, Data> CompactQuadratureTable<T, D, Data>
where
T: Scalar,
D: DimName,
DefaultAllocator: Allocator<T, D>,
{
/// Construct a new table from the given quadrature rules and a map from elements
/// to quadrature rules.
///
/// # Panics
///
/// Panics if `points`, `weights` and `data` are not consistent with each other.
///
/// Panics if the mapping from elements to quadrature rules contains indices that are
/// out of bounds with respect to the number of quadrature rules.
pub fn from_quadrature_rules_and_map(
points: NestedVec<OPoint<T, D>>,
weights: NestedVec<T>,
data: NestedVec<Data>,
element_to_rule_map: Vec<usize>,
) -> Self {
check_rules_consistency(&points, &weights, &data);
let num_rules = points.len();
let rule_indices_in_bounds = element_to_rule_map
.iter()
.all(|rule_index| rule_index < &num_rules);
assert!(
rule_indices_in_bounds,
"Each rule index must correspond to a provided quadrature rule."
);
Self {
element_to_rule_map,
points,
weights,
data,
}
}
fn rule_index_for_element(&self, element_index: usize) -> usize {
self.element_to_rule_map[element_index]
}
}
impl<T, D, Data> QuadratureTable<T, D> for CompactQuadratureTable<T, D, Data>
where
T: Scalar,
D: SmallDim,
Data: Default + Clone,
DefaultAllocator: Allocator<T, D>,
{
type Data = Data;
fn element_quadrature_size(&self, element_index: usize) -> usize {
let rule_index = self.rule_index_for_element(element_index);
self.points
.get(rule_index)
.expect("Internal error: Rule index out of bounds")
.len()
}
fn populate_element_data(&self, element_index: usize, data: &mut [Self::Data]) {
let rule_index = self.rule_index_for_element(element_index);
let data_array = self
.data
.get(rule_index)
.expect("Internal error: Rule index out of bounds");
assert_eq!(
data.len(),
data_array.len(),
"Length mismatch in data array: Stored quadrature data array has different length than output array."
);
data.clone_from_slice(data_array);
}
fn populate_element_quadrature(&self, element_index: usize, points: &mut [OPoint<T, D>], weights: &mut [T]) {
let rule_index = self.rule_index_for_element(element_index);
let points_array = self
.points
.get(rule_index)
.expect("Internal error: Rule index out of bounds");
let weights_array = self
.weights
.get(rule_index)
.expect("Internal error: Rule index out of bounds");
assert_eq!(
points.len(),
points_array.len(),
"Length mismatch in points array: Stored points array has different length than output array."
);
assert_eq!(
weights.len(),
weights_array.len(),
"Length mismatch in points array: Stored points array has different length than output array."
);
points.clone_from_slice(points_array);
weights.clone_from_slice(weights_array);
}
}