gemlab 2.0.0

Geometry and meshes laboratory for finite element analyses
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
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#[cfg(test)]
pub mod aux {
    use crate::shapes::{GeoKind, Scratchpad};
    use plotpy::{Canvas, Curve, Plot};
    use russell_lab::generate2d;
    use russell_lab::math::PI;
    use russell_lab::Vector;

    pub const RMIN: f64 = 5.0;
    pub const RMAX: f64 = 10.0;
    pub const AMIN: f64 = 30.0 * PI / 180.0;
    pub const AMAX: f64 = 60.0 * PI / 180.0;
    pub const ZMIN: f64 = 0.0;
    pub const ZMAX: f64 = 4.0;

    /// Maps point coordinates
    ///
    /// * The shape is the area indicated with "?" or the edge with "%".
    /// * If class == Tri, the shape is the half (bottom) of the highlighted wedge.
    /// * In 3D, an extrusion is applied along the out-of-plane direction.
    ///
    /// ```text
    ///   |            /
    ///   |           / αmax
    ///   ***=---__  /
    ///   |         % _
    ///   |        % ? *._          ,
    ///   |       % ????? *.     ,-'
    ///   ***=-_ % ???????? *.,-' αmin
    ///   |     % - ?????? ,-'*
    ///   |    /    *.? ,-'    *
    ///   |   /      ,*'        *
    ///   |  /    ,-'  *         *
    ///   | /  ,-'      *         *
    ///   |/.-'         #         #
    ///   o ----------- # ------- # --> r
    ///               rmin       rmax
    /// ```
    ///
    /// Intermediary mapping:
    ///
    /// r(ξ₀,ξ₁,ξ₂) = rmin + (ξ₀ - ξ₀min) · Δr / Δξ₀
    /// α(ξ₀,ξ₁,ξ₂) = αmin + (ξ₁ - ξ₁min) · Δα / Δξ₁
    /// z(ξ₀,ξ₁,ξ₂) = ξ₂
    ///
    /// Cylindrical coordinates:
    ///
    /// x₀ := r · cos(α)
    /// x₁ := r · sin(α)
    /// x₂ := z
    pub fn map_point_coords(x: &mut Vector, ksi: &[f64], ksi_min: f64, ksi_del: f64) {
        assert_eq!(x.dim(), ksi.len());
        let r = RMIN + (ksi[0] - ksi_min) * (RMAX - RMIN) / ksi_del;
        let a = AMIN + (ksi[1] - ksi_min) * (AMAX - AMIN) / ksi_del;
        x[0] = r * f64::cos(a);
        x[1] = r * f64::sin(a);
        if x.dim() == 3 {
            x[2] = ZMIN + (ksi[2] - ksi_min) * (ZMAX - ZMIN) / ksi_del;
        }
    }

    /// Returns a new scratchpad with coordinates set
    ///
    /// Notes:
    ///
    /// * For cables, the line will be along AMAX
    /// * For shells, the surface will be at ZMAX
    pub fn gen_scratchpad_with_coords(space_ndim: usize, kind: GeoKind) -> Scratchpad {
        let geo_ndim = kind.ndim();
        let nnode = kind.nnode();
        let mut x = Vector::new(space_ndim);
        let mut ksi_aux = vec![0.0; space_ndim];
        let mut pad = Scratchpad::new(space_ndim, kind).unwrap();
        let (ksi_min, ksi_del) = kind.ksi_min_ksi_del();
        for m in 0..nnode {
            let ksi = kind.reference_coords(m);
            if geo_ndim == space_ndim {
                // SOLID case: 2D or 3D
                map_point_coords(&mut x, ksi, ksi_min, ksi_del);
            } else if geo_ndim == 1 && space_ndim == 2 {
                // CABLE case: line in 2D
                ksi_aux[0] = ksi[0];
                ksi_aux[1] = 1.0;
                map_point_coords(&mut x, &ksi_aux, ksi_min, ksi_del);
            } else if geo_ndim == 1 && space_ndim == 3 {
                // CABLE in 3D
                ksi_aux[0] = ksi[0];
                ksi_aux[1] = 1.0;
                ksi_aux[2] = 1.0;
                map_point_coords(&mut x, &ksi_aux, ksi_min, ksi_del);
            } else {
                // SHELL in 3D
                ksi_aux[0] = ksi[0];
                ksi_aux[1] = ksi[1];
                ksi_aux[2] = 1.0;
                map_point_coords(&mut x, &ksi_aux, ksi_min, ksi_del);
            }
            for j in 0..space_ndim {
                pad.set_xx(m, j, x[j])
            }
        }
        pad
    }

    /// Returns a new scratchpad with coordinates such that the shape has edges aligned to x-y-z
    ///
    /// **Important:** This function works with geo_ndim = 2 or 3 only (SOLID).
    ///
    /// Notes:
    ///
    /// * For triangles (tetrahedra), one edge (face) will cut the diagonal of the x-y(-z) space.
    /// * Qua and Hex will have real coordinates equal to the natural coordinates
    /// * Tri and Tet will be scaled using the natural coordinates
    /// * All edges parallel to the x,y,z axes will have lengths equal to 2.0
    /// * The cell will also be shifted such that the first vertex will be at (0.0,0.0,0.0)
    pub fn gen_scratchpad_with_coords_aligned(kind: GeoKind) -> Scratchpad {
        let geo_ndim = kind.ndim();
        assert!(geo_ndim > 1);
        let space_ndim = geo_ndim;
        let mut pad = Scratchpad::new(space_ndim, kind).unwrap();
        let (shift, scale) = if kind.is_tri_or_tet() { (0.0, 2.0) } else { (1.0, 1.0) };
        for m in 0..kind.nnode() {
            let ksi = kind.reference_coords(m);
            for j in 0..space_ndim {
                pad.set_xx(m, j, shift + scale * ksi[j])
            }
        }
        pad
    }

    /// Extracts edge 'e' with coordinates set from the parent shape
    ///
    /// # Panics
    ///
    /// This function panics if:
    ///
    /// 1. geo_ndim = 1 (shape does not have edges)
    /// 2. space_ndim is not 2 or 3
    /// 3. geo_ndim is greater than geo_ndim (impossible situation)
    pub fn extract_edge(e: usize, pad: &Scratchpad) -> Scratchpad {
        let (space_ndim, geo_ndim) = pad.jacobian.dims();
        assert_ne!(geo_ndim, 1);
        let mut pad_edge = Scratchpad::new(space_ndim, pad.kind.edge_kind().unwrap()).unwrap();
        for i in 0..pad.kind.edge_nnode() {
            let m = pad.kind.edge_node_id(e, i);
            for j in 0..space_ndim {
                pad_edge.set_xx(i, j, pad.xxt.get(j, m));
            }
        }
        pad_edge
    }

    /// Extracts face 'f' with coordinates set from the parent shape
    ///
    /// # Panics
    ///
    /// This function panics if:
    ///
    /// 1. geo_ndim = 1 or 2 (shape does not have faces)
    /// 2. space_ndim is not 2 or 3
    /// 3. geo_ndim is greater than geo_ndim (impossible situation)
    pub fn extract_face(f: usize, pad: &Scratchpad) -> Scratchpad {
        let (space_ndim, geo_ndim) = pad.jacobian.dims();
        assert_eq!(geo_ndim, 3);
        let mut pad_face = Scratchpad::new(space_ndim, pad.kind.face_kind().unwrap()).unwrap();
        for i in 0..pad.kind.face_nnode() {
            let m = pad.kind.face_node_id(f, i);
            for j in 0..space_ndim {
                pad_face.set_xx(i, j, pad.xxt.get(j, m));
            }
        }
        pad_face
    }

    // ------ internal functions for testing ----------------------------------------------------------

    /// Generates the Canvas for the mapping used in tests
    pub fn gen_canvas_mapping() -> Canvas {
        let mut canvas = Canvas::new();
        let color = "#bfbfbf";
        canvas
            .set_face_color("None")
            .set_edge_color(color)
            .set_line_width(2.0)
            .draw_circle(0.0, 0.0, RMIN);
        canvas.draw_circle(0.0, 0.0, RMAX);
        canvas.set_edge_color(color).set_line_width(2.0);
        canvas.draw_polyline(&[[0.0, 0.0], [RMAX * f64::cos(AMIN), RMAX * f64::sin(AMIN)]], false);
        canvas.draw_polyline(&[[0.0, 0.0], [RMAX * f64::cos(AMAX), RMAX * f64::sin(AMAX)]], false);
        canvas
    }

    /// Draws the points in the natural (right) and real (left) spaces
    pub fn draw_point_coords_2d(ksi_min: f64, ksi_del: f64) -> Plot {
        const N: usize = 11;
        let mut natural_space = Curve::new();
        natural_space.set_line_style("None").set_marker_style("o");
        let mut real_space = Curve::new();
        real_space.set_line_style("None").set_marker_style("o");
        let ksi_max = ksi_min + ksi_del;
        let (ksi_0, ksi_1) = generate2d(ksi_min, ksi_max, ksi_min, ksi_max, N, N);
        let mut x = Vector::new(2);
        natural_space.points_begin();
        real_space.points_begin();
        for i in 0..N {
            for j in 0..N {
                natural_space.points_add(ksi_0.get(i, j), ksi_1.get(i, j));
                map_point_coords(&mut x, &[ksi_0.get(i, j), ksi_1.get(i, j)], ksi_min, ksi_del);
                real_space.points_add(x[0], x[1]);
            }
        }
        natural_space.points_end();
        real_space.points_end();
        let mut plot = Plot::new();
        plot.set_subplot(1, 2, 1)
            .add(&real_space)
            .add(&gen_canvas_mapping())
            .set_equal_axes(true)
            .set_range(-0.1, RMAX + 0.1, -0.1, RMAX + 0.1)
            .set_ticks_x(1.0, 0.0, "")
            .set_ticks_y(1.0, 0.0, "")
            .grid_and_labels("x", "y");
        plot.set_subplot(1, 2, 2)
            .add(&natural_space)
            .set_equal_axes(true)
            .set_labels("ξ0", "ξ1");
        plot
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#[cfg(test)]
mod tests {
    use super::aux::{
        self, draw_point_coords_2d, extract_edge, extract_face, gen_scratchpad_with_coords,
        gen_scratchpad_with_coords_aligned,
    };
    use crate::shapes::{GeoKind, Scratchpad};
    use crate::StrError;
    use russell_lab::{approx_eq, deriv1_approx_eq};

    const SAVE_FIGURE: bool = false;

    #[test]
    #[allow(unused_variables, unused_mut)]
    fn draw_point_coords_2d_works() {
        let mut plot = draw_point_coords_2d(0.0, 1.0);
        if SAVE_FIGURE {
            plot.set_figure_size_points(1000.0, 600.0)
                .save("/tmp/gemlab/test_draw_point_coords_2d.svg")
                .unwrap();
        }
    }

    #[test]
    fn gen_scratchpad_with_coords_works() {
        // SOLID in 2D
        let pad = gen_scratchpad_with_coords(2, GeoKind::Qua4);
        approx_eq(pad.xxt.get(0, 0), aux::RMIN * f64::cos(aux::AMIN), 1e-15);
        approx_eq(pad.xxt.get(1, 0), aux::RMIN * f64::sin(aux::AMIN), 1e-15);
        approx_eq(pad.xxt.get(0, 1), aux::RMAX * f64::cos(aux::AMIN), 1e-15);
        approx_eq(pad.xxt.get(1, 1), aux::RMAX * f64::sin(aux::AMIN), 1e-15);
        approx_eq(pad.xxt.get(0, 2), aux::RMAX * f64::cos(aux::AMAX), 1e-15);
        approx_eq(pad.xxt.get(1, 2), aux::RMAX * f64::sin(aux::AMAX), 1e-15);
        approx_eq(pad.xxt.get(0, 3), aux::RMIN * f64::cos(aux::AMAX), 1e-15);
        approx_eq(pad.xxt.get(1, 3), aux::RMIN * f64::sin(aux::AMAX), 1e-15);

        // SOLID in 3D
        let pad = gen_scratchpad_with_coords(3, GeoKind::Hex8);
        approx_eq(pad.xxt.get(0, 0), aux::RMIN * f64::cos(aux::AMIN), 1e-15);
        approx_eq(pad.xxt.get(1, 0), aux::RMIN * f64::sin(aux::AMIN), 1e-15);
        approx_eq(pad.xxt.get(2, 0), aux::ZMIN, 1e-15);
        approx_eq(pad.xxt.get(0, 1), aux::RMAX * f64::cos(aux::AMIN), 1e-15);
        approx_eq(pad.xxt.get(1, 1), aux::RMAX * f64::sin(aux::AMIN), 1e-15);
        approx_eq(pad.xxt.get(2, 1), aux::ZMIN, 1e-15);
        approx_eq(pad.xxt.get(0, 2), aux::RMAX * f64::cos(aux::AMAX), 1e-15);
        approx_eq(pad.xxt.get(1, 2), aux::RMAX * f64::sin(aux::AMAX), 1e-15);
        approx_eq(pad.xxt.get(2, 2), aux::ZMIN, 1e-15);
        approx_eq(pad.xxt.get(0, 3), aux::RMIN * f64::cos(aux::AMAX), 1e-15);
        approx_eq(pad.xxt.get(1, 3), aux::RMIN * f64::sin(aux::AMAX), 1e-15);
        approx_eq(pad.xxt.get(2, 3), aux::ZMIN, 1e-15);

        approx_eq(pad.xxt.get(0, 4), aux::RMIN * f64::cos(aux::AMIN), 1e-15);
        approx_eq(pad.xxt.get(1, 4), aux::RMIN * f64::sin(aux::AMIN), 1e-15);
        approx_eq(pad.xxt.get(2, 4), aux::ZMAX, 1e-15);
        approx_eq(pad.xxt.get(0, 5), aux::RMAX * f64::cos(aux::AMIN), 1e-15);
        approx_eq(pad.xxt.get(1, 5), aux::RMAX * f64::sin(aux::AMIN), 1e-15);
        approx_eq(pad.xxt.get(2, 5), aux::ZMAX, 1e-15);
        approx_eq(pad.xxt.get(0, 6), aux::RMAX * f64::cos(aux::AMAX), 1e-15);
        approx_eq(pad.xxt.get(1, 6), aux::RMAX * f64::sin(aux::AMAX), 1e-15);
        approx_eq(pad.xxt.get(2, 6), aux::ZMAX, 1e-15);
        approx_eq(pad.xxt.get(0, 7), aux::RMIN * f64::cos(aux::AMAX), 1e-15);
        approx_eq(pad.xxt.get(1, 7), aux::RMIN * f64::sin(aux::AMAX), 1e-15);
        approx_eq(pad.xxt.get(2, 7), aux::ZMAX, 1e-15);

        // CABLE in 2D
        let pad = gen_scratchpad_with_coords(2, GeoKind::Lin2);
        approx_eq(pad.xxt.get(0, 0), aux::RMIN * f64::cos(aux::AMAX), 1e-15);
        approx_eq(pad.xxt.get(1, 0), aux::RMIN * f64::sin(aux::AMAX), 1e-15);
        approx_eq(pad.xxt.get(0, 1), aux::RMAX * f64::cos(aux::AMAX), 1e-15);
        approx_eq(pad.xxt.get(1, 1), aux::RMAX * f64::sin(aux::AMAX), 1e-15);

        // CABLE in 3D
        let pad = gen_scratchpad_with_coords(3, GeoKind::Lin2);
        approx_eq(pad.xxt.get(0, 0), aux::RMIN * f64::cos(aux::AMAX), 1e-15);
        approx_eq(pad.xxt.get(1, 0), aux::RMIN * f64::sin(aux::AMAX), 1e-15);
        approx_eq(pad.xxt.get(2, 0), aux::ZMAX, 1e-15);
        approx_eq(pad.xxt.get(0, 1), aux::RMAX * f64::cos(aux::AMAX), 1e-15);
        approx_eq(pad.xxt.get(1, 1), aux::RMAX * f64::sin(aux::AMAX), 1e-15);
        approx_eq(pad.xxt.get(2, 1), aux::ZMAX, 1e-15);

        // SHELL in 3D
        let pad = gen_scratchpad_with_coords(3, GeoKind::Tri3);
        approx_eq(pad.xxt.get(0, 0), aux::RMIN * f64::cos(aux::AMIN), 1e-15);
        approx_eq(pad.xxt.get(1, 0), aux::RMIN * f64::sin(aux::AMIN), 1e-15);
        approx_eq(pad.xxt.get(2, 0), aux::ZMAX, 1e-15);
        approx_eq(pad.xxt.get(0, 1), aux::RMAX * f64::cos(aux::AMIN), 1e-15);
        approx_eq(pad.xxt.get(1, 1), aux::RMAX * f64::sin(aux::AMIN), 1e-15);
        approx_eq(pad.xxt.get(2, 1), aux::ZMAX, 1e-15);
        approx_eq(pad.xxt.get(0, 2), aux::RMIN * f64::cos(aux::AMAX), 1e-15);
        approx_eq(pad.xxt.get(1, 2), aux::RMIN * f64::sin(aux::AMAX), 1e-15);
        approx_eq(pad.xxt.get(2, 2), aux::ZMAX, 1e-15);
    }

    #[test]
    fn gen_scratchpad_with_coords_aligned_works() {
        let pad = gen_scratchpad_with_coords_aligned(GeoKind::Tri3);
        assert_eq!(pad.xxt.get(0, 0), 0.0);
        assert_eq!(pad.xxt.get(1, 0), 0.0);
        assert_eq!(pad.xxt.get(0, 1), 2.0);
        assert_eq!(pad.xxt.get(1, 1), 0.0);
        assert_eq!(pad.xxt.get(0, 2), 0.0);
        assert_eq!(pad.xxt.get(1, 2), 2.0);

        let pad = gen_scratchpad_with_coords_aligned(GeoKind::Qua4);
        assert_eq!(pad.xxt.get(0, 0), 0.0);
        assert_eq!(pad.xxt.get(1, 0), 0.0);
        assert_eq!(pad.xxt.get(0, 1), 2.0);
        assert_eq!(pad.xxt.get(1, 1), 0.0);
        assert_eq!(pad.xxt.get(0, 2), 2.0);
        assert_eq!(pad.xxt.get(1, 2), 2.0);
        assert_eq!(pad.xxt.get(0, 3), 0.0);
        assert_eq!(pad.xxt.get(1, 3), 2.0);

        let pad = gen_scratchpad_with_coords_aligned(GeoKind::Hex8);
        assert_eq!(pad.xxt.get(0, 0), 0.0);
        assert_eq!(pad.xxt.get(1, 0), 0.0);
        assert_eq!(pad.xxt.get(2, 0), 0.0);
        assert_eq!(pad.xxt.get(0, 1), 2.0);
        assert_eq!(pad.xxt.get(1, 1), 0.0);
        assert_eq!(pad.xxt.get(2, 1), 0.0);
        assert_eq!(pad.xxt.get(0, 2), 2.0);
        assert_eq!(pad.xxt.get(1, 2), 2.0);
        assert_eq!(pad.xxt.get(2, 2), 0.0);
        assert_eq!(pad.xxt.get(0, 3), 0.0);
        assert_eq!(pad.xxt.get(1, 3), 2.0);
        assert_eq!(pad.xxt.get(2, 3), 0.0);

        assert_eq!(pad.xxt.get(0, 4), 0.0);
        assert_eq!(pad.xxt.get(1, 4), 0.0);
        assert_eq!(pad.xxt.get(2, 4), 2.0);
        assert_eq!(pad.xxt.get(0, 5), 2.0);
        assert_eq!(pad.xxt.get(1, 5), 0.0);
        assert_eq!(pad.xxt.get(2, 5), 2.0);
        assert_eq!(pad.xxt.get(0, 6), 2.0);
        assert_eq!(pad.xxt.get(1, 6), 2.0);
        assert_eq!(pad.xxt.get(2, 6), 2.0);
        assert_eq!(pad.xxt.get(0, 7), 0.0);
        assert_eq!(pad.xxt.get(1, 7), 2.0);
        assert_eq!(pad.xxt.get(2, 7), 2.0);
    }

    #[test]
    fn extract_edge_works() {
        let pad = gen_scratchpad_with_coords(2, GeoKind::Qua4);
        let pad_edge = extract_edge(0, &pad);
        assert_eq!(pad_edge.kind, GeoKind::Lin2);
        approx_eq(pad_edge.xxt.get(0, 0), aux::RMAX * f64::cos(aux::AMIN), 1e-15);
        approx_eq(pad_edge.xxt.get(1, 0), aux::RMAX * f64::sin(aux::AMIN), 1e-15);
        approx_eq(pad_edge.xxt.get(0, 1), aux::RMIN * f64::cos(aux::AMIN), 1e-15);
        approx_eq(pad_edge.xxt.get(1, 1), aux::RMIN * f64::sin(aux::AMIN), 1e-15);
    }

    #[test]
    fn extract_face_works() {
        let pad = gen_scratchpad_with_coords(3, GeoKind::Hex8);
        let pad_face = extract_face(0, &pad);
        assert_eq!(pad_face.kind, GeoKind::Qua4);
        approx_eq(pad_face.xxt.get(0, 0), aux::RMIN * f64::cos(aux::AMIN), 1e-15);
        approx_eq(pad_face.xxt.get(1, 0), aux::RMIN * f64::sin(aux::AMIN), 1e-15);
        approx_eq(pad_face.xxt.get(2, 0), aux::ZMIN, 1e-15);
        approx_eq(pad_face.xxt.get(0, 1), aux::RMIN * f64::cos(aux::AMIN), 1e-15);
        approx_eq(pad_face.xxt.get(1, 1), aux::RMIN * f64::sin(aux::AMIN), 1e-15);
        approx_eq(pad_face.xxt.get(2, 1), aux::ZMAX, 1e-15);
        approx_eq(pad_face.xxt.get(0, 2), aux::RMIN * f64::cos(aux::AMAX), 1e-15);
        approx_eq(pad_face.xxt.get(1, 2), aux::RMIN * f64::sin(aux::AMAX), 1e-15);
        approx_eq(pad_face.xxt.get(2, 2), aux::ZMAX, 1e-15);
        approx_eq(pad_face.xxt.get(0, 3), aux::RMIN * f64::cos(aux::AMAX), 1e-15);
        approx_eq(pad_face.xxt.get(1, 3), aux::RMIN * f64::sin(aux::AMAX), 1e-15);
        approx_eq(pad_face.xxt.get(2, 3), aux::ZMIN, 1e-15);
    }

    #[test]
    fn fn_interp_works() {
        // loop over shapes
        for kind in GeoKind::VALUES {
            // scratchpad with coordinates
            let geo_ndim = kind.ndim();
            let space_ndim = usize::max(2, geo_ndim);
            let mut pad = gen_scratchpad_with_coords(space_ndim, kind);

            // loop over nodes
            const TOL: f64 = 1e-15;
            let nnode = kind.nnode();
            for m in 0..nnode {
                // get ξᵐ corresponding to node m
                let ksi = kind.reference_coords(m);

                // compute interpolation function Nⁿ(ξᵐ)
                pad.calc_interp(ksi);

                // check: Nⁿ(ξᵐ) = 1 if m==n; 0 otherwise
                for n in 0..nnode {
                    if m == n {
                        approx_eq(pad.interp[n], 1.0, TOL);
                    } else {
                        approx_eq(pad.interp[n], 0.0, TOL);
                    }
                }
            }
        }
    }

    // Holds arguments for numerical differentiation of N with respect to ξ => L (deriv) matrix
    struct ArgsNumDeriv {
        pad: Scratchpad,  // scratchpad
        at_ksi: Vec<f64>, // at reference coord value
        ksi: Vec<f64>,    // temporary reference coord
        m: usize,         // node index from 0 to nnode
        j: usize,         // dimension index from 0 to geom_ndim
    }

    // Computes Nᵐ(ξ) with variable v := ξⱼ
    fn nn_given_ksi(v: f64, args: &mut ArgsNumDeriv) -> Result<f64, StrError> {
        args.ksi.copy_from_slice(&args.at_ksi);
        args.ksi[args.j] = v;
        (args.pad.fn_interp)(&mut args.pad.interp, &args.ksi);
        Ok(args.pad.interp[args.m])
    }

    #[test]
    fn calc_deriv_works() {
        // kind and tolerances
        let problem = vec![
            // Lin
            (GeoKind::Lin2, 1e-13),
            (GeoKind::Lin3, 1e-13),
            (GeoKind::Lin4, 1e-10),
            (GeoKind::Lin5, 1e-10),
            // Tri
            (GeoKind::Tri3, 1e-12),
            (GeoKind::Tri6, 1e-12),
            (GeoKind::Tri10, 1e-10),
            (GeoKind::Tri15, 1e-9),
            // Qua
            (GeoKind::Qua4, 1e-13),
            (GeoKind::Qua8, 1e-12),
            (GeoKind::Qua9, 1e-13),
            (GeoKind::Qua12, 1e-10),
            (GeoKind::Qua16, 1e-10),
            (GeoKind::Qua17, 1e-10),
            // Tet
            (GeoKind::Tet4, 1e-12),
            (GeoKind::Tet10, 1e-12),
            (GeoKind::Tet20, 1e-10),
            // Hex
            (GeoKind::Hex8, 1e-13),
            (GeoKind::Hex20, 1e-12),
            (GeoKind::Hex32, 1e-10),
        ];

        // loop over shapes
        for (kind, tol) in problem {
            // scratchpad with coordinates
            let geo_ndim = kind.ndim();
            let space_ndim = usize::max(2, geo_ndim);
            let mut pad = aux::gen_scratchpad_with_coords(space_ndim, kind);

            // set ξ within reference space
            let at_ksi = vec![0.25; geo_ndim];

            // compute all derivatives of interpolation functions with respect to ξ
            (pad.fn_deriv)(&mut pad.deriv, &at_ksi);

            // set arguments for numerical integration
            let args = &mut ArgsNumDeriv {
                pad: pad.clone(),
                at_ksi,
                ksi: vec![0.0; geo_ndim],
                m: 0,
                j: 0,
            };

            // check Lᵐ(ξ) = dNᵐ(ξ)/dξ
            for m in 0..kind.nnode() {
                args.m = m;
                for j in 0..geo_ndim {
                    args.j = j;
                    // Lᵐⱼ := dNᵐ/dξⱼ
                    deriv1_approx_eq(pad.deriv.get(m, j), args.at_ksi[j], args, tol, nn_given_ksi);
                }
            }
        }
    }
}