fenris 0.0.33

A library for advanced finite element computations in Rust
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
use fenris::assembly::global::gather_global_to_local;
use fenris::assembly::local::GeneralQuadratureTable;
use fenris::connectivity::Connectivity;
use fenris::element::{ElementConnectivity, Tet20Element, Tet4Element, VolumetricFiniteElement};
use fenris::error::{
    estimate_H1_seminorm_error, estimate_L2_error, estimate_element_H1_seminorm_error,
    estimate_element_H1_seminorm_error_squared, estimate_element_L2_error, estimate_element_L2_error_squared,
};
use fenris::integrate::IntegrationWorkspace;
use fenris::mesh::procedural::create_unit_box_uniform_hex_mesh_3d;
use fenris::nalgebra::coordinates::XYZ;
use fenris::nalgebra::{DVector, DVectorView, OVector, Point3, Vector1, Vector2, U3};
use fenris::quadrature;
use fenris::quadrature::{Quadrature, QuadraturePair3d};
use fenris::util::NestedVec;
use matrixcompare::assert_scalar_eq;
use nalgebra::{Matrix3x2, Vector3};
use std::ops::Deref;
use util::flatten_vertically;

// TODO: Port this to the library proper?
fn transform_quadrature_to_physical_domain<Element>(
    element: &Element,
    weights: &[f64],
    points: &[Point3<f64>],
) -> QuadraturePair3d<f64>
where
    Element: VolumetricFiniteElement<f64, GeometryDim = U3>,
{
    weights
        .iter()
        .zip(points)
        .map(|(w, xi)| {
            let j_det = element.reference_jacobian(xi).determinant().abs();
            (w * j_det, element.map_reference_coords(xi))
        })
        .unzip()
}

fn arbitrary_tet20_element() -> Tet20Element<f64> {
    let a = Point3::new(2.0, 0.0, 1.0);
    let b = Point3::new(3.0, 4.0, 1.0);
    let c = Point3::new(1.0, 1.0, 2.0);
    let d = Point3::new(3.0, 1.0, 4.0);
    let tet4_element = Tet4Element::from_vertices([a, b, c, d]);
    Tet20Element::from(&tet4_element)
}

#[test]
#[allow(non_snake_case)]
fn test_element_L2_error_scalar() {
    // We define two functions u1 and u2. u1 is a high order polynomial and u2 is a polynomial
    // that can be exactly represented in some element (Tet20 in this case).
    // Then, given a quadrature rule on the element, we can compute the integral of the function
    //  ||e||^2       e = u1 - u2
    // in two ways:
    //  1. directly with a high order quadrature
    //  2. by computing the L2 error with u = u1 and u_h = u2
    // This allows us to test the L2 error computation routine.
    let element = arbitrary_tet20_element();
    let u_h_element = DVector::from_vec(element.vertices().iter().map(u2_scalar).collect());

    // Use a quadrature rule with sufficient strength such that it can exactly capture the error
    // (since we compute a squared norm, we need double the polynomial degree)
    let (weights, points) = quadrature::total_order::tetrahedron(10).unwrap();
    let L2_error_computed = estimate_element_L2_error(
        &element,
        &|x: &Point3<_>| Vector1::new(u1_scalar(x)),
        DVectorView::from(&u_h_element),
        &weights,
        &points,
        &mut IntegrationWorkspace::default(),
    );

    let L2_error_expected = {
        let (weights, points) = transform_quadrature_to_physical_domain(&element, &weights, &points);
        let u_squared_norm = |x: &Point3<f64>| u_scalar(x).powi(2);
        (weights, points).integrate(u_squared_norm).sqrt()
    };

    assert_scalar_eq!(L2_error_computed, L2_error_expected, comp = abs, tol = 1e-12);
}

#[test]
#[allow(non_snake_case)]
fn test_element_L2_error_vector() {
    // This test is completely analogous to the scalar test, it just tests vector-valued
    // functions intead

    let element = arbitrary_tet20_element();
    let u_h_element = flatten_vertically(&element.vertices().iter().map(u2_vector).collect::<Vec<_>>()).unwrap();

    // Use a quadrature rule with sufficient strength such that it can exactly capture the error
    // (since we compute a squared norm, we need double the polynomial degree)
    let (weights, points) = quadrature::total_order::tetrahedron(10).unwrap();
    let L2_error_computed = estimate_element_L2_error(
        &element,
        &u1_vector,
        DVectorView::from(&u_h_element),
        &weights,
        &points,
        &mut IntegrationWorkspace::default(),
    );

    let L2_error_expected = {
        let (weights, points) = transform_quadrature_to_physical_domain(&element, &weights, &points);
        let u_squared_norm = |x: &Point3<f64>| u_vector(x).norm_squared();
        (weights, points).integrate(u_squared_norm).sqrt()
    };

    assert_scalar_eq!(L2_error_computed, L2_error_expected, comp = abs, tol = 1e-12);
}

#[test]
#[allow(non_snake_case)]
fn test_element_H1_seminorm_error_scalar() {
    // We take the same approach as in the L2 tests, except in this case of course we're
    // dealing with gradients of `u` rather than `u` itself.
    // See those tests for comments on what is going on here.
    let element = arbitrary_tet20_element();
    let u_h_element = DVector::from_vec(element.vertices().iter().map(u2_scalar).collect());

    // Use a quadrature rule with sufficient strength such that it can exactly capture the error
    // (since we compute a squared norm, we need double the polynomial degree of the gradient
    // polynomial order)
    let (weights, points) = quadrature::total_order::tetrahedron(8).unwrap();
    let H1_seminorm_computed = estimate_element_H1_seminorm_error(
        &element,
        &u1_scalar_grad,
        DVectorView::from(&u_h_element),
        &weights,
        &points,
        &mut IntegrationWorkspace::default(),
    );

    let H1_seminorm_expected = {
        let (weights, points) = transform_quadrature_to_physical_domain(&element, &weights, &points);
        let u_squared_norm = |x: &Point3<f64>| u_scalar_grad(x).norm_squared();
        (weights, points).integrate(u_squared_norm).sqrt()
    };

    assert_scalar_eq!(H1_seminorm_computed, H1_seminorm_expected, comp = abs, tol = 1e-12);
}

#[test]
#[allow(non_snake_case)]
fn test_element_H1_seminorm_error_vector() {
    // This test is completely analogous to the scalar test, it just tests vector-valued
    // functions instead
    let element = arbitrary_tet20_element();
    let u_h_element = flatten_vertically(&element.vertices().iter().map(u2_vector).collect::<Vec<_>>()).unwrap();

    // Use a quadrature rule with sufficient strength such that it can exactly capture the error
    // (since we compute a squared norm, we need double the polynomial degree)
    let (weights, points) = quadrature::total_order::tetrahedron(10).unwrap();
    let H1_seminorm_computed = estimate_element_H1_seminorm_error(
        &element,
        &u1_vector_grad,
        DVectorView::from(&u_h_element),
        &weights,
        &points,
        &mut IntegrationWorkspace::default(),
    );

    let H1_seminorm_expected = {
        let (weights, points) = transform_quadrature_to_physical_domain(&element, &weights, &points);
        let u_squared_norm = |x: &Point3<f64>| u_vector_grad(x).norm_squared();
        (weights, points).integrate(u_squared_norm).sqrt()
    };

    assert_scalar_eq!(H1_seminorm_computed, H1_seminorm_expected, comp = abs, tol = 1e-12);
}

#[test]
#[allow(non_snake_case)]
fn test_estimate_L2_error_on_mesh() {
    // Test that the "global" error estimate is consistent with what we get from local estimates
    let mesh = create_unit_box_uniform_hex_mesh_3d(2);

    // Use a different quadrature rule per element
    // This ensures us that the error estimator actually takes into account
    // the correct quadrature rule for a given element
    let mut error_quadrature_points = NestedVec::new();
    let mut error_quadrature_weights = NestedVec::new();
    for i in 0..mesh.connectivity().len() {
        let (weights, points) = quadrature::tensor::hexahedron_gauss(i + 1);
        error_quadrature_weights.push(&weights);
        error_quadrature_points.push(&points);
    }
    let quadrature_table = GeneralQuadratureTable::from_points_and_weights(
        error_quadrature_points.clone(),
        error_quadrature_weights.clone(),
    );

    // Use an arbitrary function projected onto the nodes to make sure we get some non-zero error
    // for higher-order quadratures
    let g = |x: &Point3<f64>| {
        let &XYZ { x, y, z } = x.deref();
        Vector2::new(3.0 * x + 2.0 * y * z.powi(3), 4.0 * x.powi(2) + 2.0 * y + z)
    };
    let u_h = flatten_vertically(&mesh.vertices().iter().map(g).collect::<Vec<_>>()).unwrap();
    let computed_L2_error = estimate_L2_error(&mesh, &u_vector, &u_h, &quadrature_table).unwrap();

    // Compute the error "manually" element-by-element for comparison
    let expected_L2_error = {
        mesh.connectivity()
            .iter()
            .enumerate()
            .map(|(i, conn)| {
                let element = conn.element(mesh.vertices()).unwrap();
                let mut u_h_element = OVector::from([0.0; 2 * 8]);
                gather_global_to_local(&u_h, &mut u_h_element, conn.vertex_indices(), 2);
                let weights = error_quadrature_weights.get(i).unwrap();
                let points = error_quadrature_points.get(i).unwrap();
                estimate_element_L2_error_squared(
                    &element,
                    &u_vector,
                    DVectorView::from(&u_h_element),
                    weights,
                    points,
                    &mut IntegrationWorkspace::default(),
                )
            })
            .sum::<f64>()
            .sqrt()
    };

    assert_scalar_eq!(computed_L2_error, expected_L2_error, comp = abs, tol = 1e-12);
}

#[test]
#[allow(non_snake_case)]
fn test_estimate_H1_seminorm_error_on_mesh() {
    // Test that the "global" error estimate is consistent with what we get from local estimates
    let mesh = create_unit_box_uniform_hex_mesh_3d(2);

    // Use a different quadrature rule per element
    // This ensures us that the error estimator actually takes into account
    // the correct quadrature rule for a given element
    let mut error_quadrature_points = NestedVec::new();
    let mut error_quadrature_weights = NestedVec::new();
    for i in 0..mesh.connectivity().len() {
        let (weights, points) = quadrature::tensor::hexahedron_gauss(i + 1);
        error_quadrature_weights.push(&weights);
        error_quadrature_points.push(&points);
    }
    let quadrature_table = GeneralQuadratureTable::from_points_and_weights(
        error_quadrature_points.clone(),
        error_quadrature_weights.clone(),
    );

    // Use an arbitrary function projected onto the nodes to make sure we get some non-zero error
    // for higher-order quadratures
    let g = |x: &Point3<f64>| {
        let &XYZ { x, y, z } = x.deref();
        Vector2::new(3.0 * x + 2.0 * y * z.powi(3), 4.0 * x.powi(2) + 2.0 * y + z)
    };
    let u_h = flatten_vertically(&mesh.vertices().iter().map(g).collect::<Vec<_>>()).unwrap();
    let computed_H1_seminorm_error =
        estimate_H1_seminorm_error(&mesh, &u_vector_grad, &u_h, &quadrature_table).unwrap();

    // Compute the error "manually" element-by-element for comparison
    let expected_H1_seminorm_error = {
        mesh.connectivity()
            .iter()
            .enumerate()
            .map(|(i, conn)| {
                let element = conn.element(mesh.vertices()).unwrap();
                let mut u_h_element = OVector::from([0.0; 2 * 8]);
                gather_global_to_local(&u_h, &mut u_h_element, conn.vertex_indices(), 2);
                let weights = error_quadrature_weights.get(i).unwrap();
                let points = error_quadrature_points.get(i).unwrap();
                estimate_element_H1_seminorm_error_squared(
                    &element,
                    &u_vector_grad,
                    DVectorView::from(&u_h_element),
                    weights,
                    points,
                    &mut IntegrationWorkspace::default(),
                )
            })
            .sum::<f64>()
            .sqrt()
    };

    assert_scalar_eq!(
        computed_H1_seminorm_error,
        expected_H1_seminorm_error,
        comp = abs,
        tol = 1e-12
    );
}

/// An arbitrary multi-variate scalar function used in tests.
fn u1_scalar(x: &Point3<f64>) -> f64 {
    let &XYZ { x, y, z } = x.deref();
    // A polynomial of total order 5
    6.0 * x.powi(5) + 2.0 * y.powi(5) - 2.0 * z.powi(5) + 3.0 * x * y.powi(3) * z - 2.0 * y.powi(3)
        + x.powi(2) * y.powi(2)
        + 3.0 * x
        + 2.0 * y
        - 3.0 * z
        - 6.0
}

/// An arbitrary multi-variate scalar function used in tests.
fn u2_scalar(x: &Point3<f64>) -> f64 {
    let &XYZ { x, y, z } = x.deref();
    // A polynomial of total order 3
    6.0 * x.powi(3) - 2.0 * y.powi(3) + 4.0 * z.powi(3) + 2.0 * x.powi(2) * y + 4.0 * y.powi(2) * z + x.powi(2)
        - y.powi(3)
        + 5.0 * x * y * z
        + 2.0 * x
        + 3.0 * y
        - 5.0 * z
        + 2.0
}

/// An arbitrary multi-variate scalar function used in tests.
fn u_scalar(x: &Point3<f64>) -> f64 {
    u1_scalar(x) - u2_scalar(x)
}

/// An arbitrary multi-variate vector function used in tests.
fn u1_vector(x: &Point3<f64>) -> Vector2<f64> {
    let &XYZ { x, y, z } = x.deref();
    // A polynomial of total order 5
    let u1_1 = 6.0 * x.powi(5) + 2.0 * y.powi(5) - 2.0 * z.powi(5) + 3.0 * x * y.powi(3) * z - 2.0 * y.powi(3)
        + x.powi(2) * y.powi(2)
        + 3.0 * x
        + 2.0 * y
        - 3.0 * z
        - 6.0;
    let u1_2 = 3.0 * x.powi(5) - 3.0 * y.powi(5) + 2.0 * z.powi(5) + 3.0 * x.powi(3) * y * z + 4.0 * x + 2.0 * y + 15.0;
    Vector2::new(u1_1, u1_2)
}

/// An arbitrary multi-variate vector function used in tests.
fn u2_vector(x: &Point3<f64>) -> Vector2<f64> {
    let &XYZ { x, y, z } = x.deref();
    // A polynomial of total order 3
    let u2_1 =
        6.0 * x.powi(3) - 2.0 * y.powi(3) + 4.0 * z.powi(3) + 2.0 * x.powi(2) * y + 4.0 * y.powi(2) * z + x.powi(2)
            - y.powi(3)
            + 5.0 * x * y * z
            + 2.0 * x
            + 3.0 * y
            - 5.0 * z
            + 2.0;
    let u2_2 = 3.0 * x.powi(3) - 4.0 * y.powi(3) + 2.0 * z.powi(3) + 2.0 * x.powi(2) * z + 3.0 * y.powi(2) - 2.0 * x
        + 3.0 * y
        - 5.0 * z
        + 9.0;
    Vector2::new(u2_1, u2_2)
}

/// An arbitrary multi-variate vector function used in tests.
fn u_vector(x: &Point3<f64>) -> Vector2<f64> {
    u1_vector(x) - u2_vector(x)
}

fn u1_scalar_grad(x: &Point3<f64>) -> Vector3<f64> {
    let &XYZ { x, y, z } = x.deref();
    let u1_x = 30.0 * x.powi(5) + 3.0 * y.powi(3) * z + 2.0 * x + 3.0;
    let u1_y = 10.0 * y.powi(4) + 9.0 * x * y.powi(2) * z - 6.0 * y + 2.0 * x.powi(2) * y + 2.0;
    let u1_z = -10.0 * z.powi(4) + 3.0 * x * y.powi(3) - 3.0;
    Vector3::new(u1_x, u1_y, u1_z)
}

fn u2_scalar_grad(x: &Point3<f64>) -> Vector3<f64> {
    let &XYZ { x, y, z } = x.deref();
    let u2_x = 18.0 * x.powi(2) + 4.0 * y * x + 2.0 * x + 5.0 * y * z + 2.0;
    let u2_y = -6.0 * y.powi(2) + 2.0 * x.powi(2) + 8.0 * z * y - 3.0 * y.powi(2) + 5.0 * x * z + 3.0;
    let u2_z = 12.0 * z.powi(2) + 4.0 * y.powi(2) + 5.0 * x * y - 5.0;
    Vector3::new(u2_x, u2_y, u2_z)
}

fn u_scalar_grad(x: &Point3<f64>) -> Vector3<f64> {
    u1_scalar_grad(x) - u2_scalar_grad(x)
}

fn u1_vector_grad(x: &Point3<f64>) -> Matrix3x2<f64> {
    let &XYZ { x, y, z } = x.deref();
    let u1_1x = 30.0 * x.powi(4) + 3.0 * y.powi(3) * z + 2.0 * x * y.powi(2) + 3.0;
    let u1_1y = 10.0 * y.powi(4) + 9.0 * x * z * y.powi(2) - 6.0 * y.powi(2) + 2.0 * x.powi(2) * y + 2.0;
    let u1_1z = -10.0 * z.powi(4) + 3.0 * x * y.powi(3) - 3.0;

    let u1_2x = 15.0 * x.powi(4) + 9.0 * y * z * x.powi(2) + 4.0;
    let u1_2y = -15.0 * y.powi(4) + 3.0 * x.powi(3) * z + 2.0;
    let u1_2z = 10.0 * z.powi(4) + 3.0 * x.powi(3) * y;

    let u1_1_grad = Vector3::new(u1_1x, u1_1y, u1_1z);
    let u1_2_grad = Vector3::new(u1_2x, u1_2y, u1_2z);

    Matrix3x2::from_columns(&[u1_1_grad, u1_2_grad])
}

fn u2_vector_grad(x: &Point3<f64>) -> Matrix3x2<f64> {
    let &XYZ { x, y, z } = x.deref();
    let u2_1x = 18.0 * x.powi(2) + 4.0 * y * x + 2.0 * x + 5.0 * y * z + 2.0;
    let u2_1y = -6.0 * y.powi(2) + 2.0 * x.powi(2) + 8.0 * z * y - 3.0 * y.powi(2) + 5.0 * x * z + 3.0;
    let u2_1z = 12.0 * z.powi(2) + 4.0 * y.powi(2) + 5.0 * x * y - 5.0;

    let u2_2x = 9.0 * x.powi(2) + 4.0 * x * z - 2.0;
    let u2_2y = -12.0 * y.powi(2) + 6.0 * y + 3.0;
    let u2_2z = 6.0 * z.powi(2) + 2.0 * x.powi(2) - 5.0;

    let u2_1_grad = Vector3::new(u2_1x, u2_1y, u2_1z);
    let u2_2_grad = Vector3::new(u2_2x, u2_2y, u2_2z);

    Matrix3x2::from_columns(&[u2_1_grad, u2_2_grad])
}

fn u_vector_grad(x: &Point3<f64>) -> Matrix3x2<f64> {
    u1_vector_grad(x) - u2_vector_grad(x)
}