pyrucast 0.4.1

Finite-element library in Rust, inspired by cast3m, exposed to Python
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
//! Hexahedron-dominant volume meshing: a boundary layer grown inward from a
//! closed surface, closed onto a tetrahedral core.
//!
//! Where [`triangulate_volume`](fn@super::triangulate_volume) fills a solid with
//! tetrahedra throughout, `pave_volume` puts **hexahedra where they matter** —
//! in the layer against the boundary, where stress and flux gradients are
//! steepest and where an element's shape decides the accuracy — and leaves the
//! interior, where the field is smooth, to tetrahedra.
//!
//! ```text
//! skin (QUA4 / TRI3)  ──►  offset inward  ──►  HEX8 / PENTA6   (boundary layer)
//!                                                    │
//!                       inner square faces ──► PYRA5 │          (the junction)
//!                                                    ▼
//!                              a void bounded by triangles only
//!                                                    │
//!                                       triangulate_volume  ──►  TET4
//! ```
//!
//! ## Why the pyramids are not optional
//!
//! The layer's inner faces are **squares**, and a tetrahedron has none. Split
//! each square into two triangles and the hexahedron on the other side still
//! sees one square face, with a node hanging in the middle of nothing: the
//! mesh is no longer conforming, and no solver can assemble across that face.
//! A pyramid is the one element that presents a square on one side and
//! triangles on the other, so it is exactly what the junction needs — see
//! [`PYRA5`](crate::atoms::ElementType::PYRA5), whose shape
//! functions reduce to `QUA4`'s on the base and stay linear along the edges to
//! the apex, which is what makes the continuity hold.
//!
//! Once every square face is capped, what is left of the void is bounded by
//! triangles alone, and the existing tetrahedral mesher takes it from there —
//! in **strict** mode, so it reuses those triangles verbatim and the two parts
//! of the mesh meet node for node.

use crate::aggregate::Aggregate;
use crate::atoms::{ElementType, Node, NodeId, Point3};
use crate::containers::mesh::{Mesh, SubMesh};
use crate::coords::Coords;
use crate::error::{PyrucastError, Result};
use crate::handle::Handle;
use crate::interrupt::{Cancel, NoCancel};
use crate::ops::mesh::plaster::front::Front;
use crate::ops::mesh::plaster::shell::{Facet, Shell};
use crate::ops::mesh::plaster::{self, smooth};
use std::collections::HashMap;

/// How deep a capping pyramid's apex sits below its base, as a fraction of the
/// base's mean edge.
///
/// Shallower than it looks like it should be, and deliberately. A pyramid half
/// its base wide is the better-shaped cell, but it juts far enough into the
/// void that neighbouring pyramids start cutting through one another and the
/// core mesher is handed a surface with no well-defined inside. A quarter
/// keeps the void close to the shape the layer left, which is what the core
/// mesher can actually fill.
const APEX_DEPTH: f64 = 0.25;

/// Two apexes closer than this many mean edges are the same point, and get the
/// same node.
const APEX_WELD: f64 = 1e-7;

/// Smoothing sweeps run over the layer once the front has stopped.
const SMOOTH_SWEEPS: usize = 12;

/// Mesh the inside of a closed `QUA4`/`TRI3` envelope with a hexahedral
/// boundary layer over a tetrahedral core.
///
/// `layers` boundary layers are grown inward, each `thickness` deep;
/// `thickness` defaults to the envelope's mean edge length, which gives
/// roughly cube-shaped cells. `core_size` is the target element size for the
/// tetrahedral core, and follows [`triangulate_volume`](fn@super::triangulate_volume)'s
/// convention.
///
/// The envelope's normals must point **out of the material**, exactly as for
/// [`triangulate_volume`](fn@super::triangulate_volume); its nodes are reused as
/// they are. The result carries a `HEX8` submesh (from the quadrangular
/// facets), a `PENTA6` one (from the triangular facets), a `PYRA5` one (the
/// junction) and a `TET4` one (the core), each present only if non-empty.
///
/// This is the uninterruptible convenience form; for a long mesh a caller may
/// want to stop early, use [`pave_volume_cancellable`].
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::ops::mesh;
/// # let coords = Handle::new(Coords::new(3).unwrap());
/// # let base: Vec<Node> = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [1.0, 1.0, 0.0], [0.0, 1.0, 0.0]]
/// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
/// # let mut carre = SubMesh::new(coords.clone(), ElementType::QUA4);
/// # carre.add_cell(&base.iter().map(|n| n.id()).collect::<Vec<_>>()).unwrap();
/// # let cube = mesh::sweep_solid(&Mesh::from_submesh(carre),
/// #     &mesh::translate(&Mesh::from_submesh({
/// #         let mut c = SubMesh::new(coords.clone(), ElementType::QUA4);
/// #         c.add_cell(&base.iter().map(|n| n.id()).collect::<Vec<_>>()).unwrap();
/// #         c
/// #     }), &[0.0, 0.0, 1.0]).unwrap(), 1).unwrap();
/// # let enveloppe = mesh::convert(&mesh::skin(&cube, None).unwrap(),
/// #                               ElementType::TRI3).unwrap();
/// // Couche limite hexaédrique, raccord pyramidal, cœur tétraédrique :
/// // trois types d'éléments dans le même maillage.
/// let v = mesh::pave_volume(&enveloppe, 1, Some(0.2), Some(0.5))?;
/// assert!(v.cell_count()? > 0);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn pave_volume(
    envelope: &Mesh,
    layers: usize,
    thickness: Option<f64>,
    core_size: Option<f64>,
) -> Result<Mesh> {
    pave_volume_cancellable(envelope, layers, thickness, core_size, &NoCancel)
}

/// Like [`pave_volume`], but polls `cancel` while meshing the core so a long
/// run can be stopped early (returning [`PyrucastError::Interrupted`]).
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::ops::mesh;
/// # let coords = Handle::new(Coords::new(3).unwrap());
/// # let base: Vec<Node> = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [1.0, 1.0, 0.0], [0.0, 1.0, 0.0]]
/// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
/// # let mut carre = SubMesh::new(coords.clone(), ElementType::QUA4);
/// # carre.add_cell(&base.iter().map(|n| n.id()).collect::<Vec<_>>()).unwrap();
/// # let cube = mesh::sweep_solid(&Mesh::from_submesh(carre),
/// #     &mesh::translate(&Mesh::from_submesh({
/// #         let mut c = SubMesh::new(coords.clone(), ElementType::QUA4);
/// #         c.add_cell(&base.iter().map(|n| n.id()).collect::<Vec<_>>()).unwrap();
/// #         c
/// #     }), &[0.0, 0.0, 1.0]).unwrap(), 1).unwrap();
/// # let enveloppe = mesh::convert(&mesh::skin(&cube, None).unwrap(),
/// #                               ElementType::TRI3).unwrap();
/// # use std::sync::atomic::{AtomicBool, Ordering};
/// // Le jeton est sondé aux points de contrôle du mailleur : armé d'avance,
/// // l'appel s'arrête au premier d'entre eux.
/// let stop = AtomicBool::new(true);
/// assert!(mesh::pave_volume_cancellable(
///     &enveloppe, 1, Some(0.2), Some(0.5), &stop).is_err());
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn pave_volume_cancellable(
    envelope: &Mesh,
    layers: usize,
    thickness: Option<f64>,
    core_size: Option<f64>,
    cancel: &dyn Cancel,
) -> Result<Mesh> {
    if layers == 0 {
        return Err(PyrucastError::Message(
            "pave_volume: layers must be at least 1; for an all-tetrahedron mesh \
             use triangulate_volume"
                .into(),
        ));
    }
    if let Some(t) = thickness
        && (t <= 0.0 || t.is_nan())
    {
        return Err(PyrucastError::Message(format!(
            "pave_volume: thickness must be > 0, got {t}"
        )));
    }
    let coords = envelope.coords()?;
    let shell = Shell::extract(envelope, "pave_volume")?;
    let mut front = Front::new(shell, coords.clone());
    if front.volume() <= 0.0 {
        return Err(PyrucastError::Message(format!(
            "pave_volume: the envelope encloses a signed volume of {:.3e}, so its normals \
             point into the material rather than out of it. Run `invert` on it.",
            front.volume()
        )));
    }
    let step = thickness.unwrap_or_else(|| front.mean_edge());

    // ── The advancing front ───────────────────────────────────────────────
    for layer in 0..layers {
        cancel.check()?;
        if !front.advance(step)? {
            return Err(PyrucastError::Message(format!(
                "pave_volume: layer {} has nowhere to go — every step, down to a fraction of \
                 the one asked for, would turn a cell inside out. Ask for fewer layers, a \
                 thinner one, or refine the envelope.",
                layer + 1
            )));
        }
        // Where the front has closed on itself, seam it shut before going on.
        front.weld();
        if front.facets.is_empty() {
            break;
        }
    }

    // ── Smoothing ─────────────────────────────────────────────────────────
    {
        let patch = smooth::Patch {
            hexes: &front.fab.hexes,
            prisms: &front.fab.prisms,
            movable: &front.fab.movable,
        };
        let inc = smooth::Incidence::build(&patch, front.fab.pts.len());
        let mut pts = std::mem::take(&mut front.fab.pts);
        smooth::smooth(&mut pts, &patch, &inc, SMOOTH_SWEEPS);
        front.fab.pts = pts;
    }
    // Before anything reads the mesh back out — the core mesher
    // does, from the node coordinates — the smoothed positions have to be
    // there. Cap the front from one set of positions and tetrahedralise
    // against another and the two surfaces simply do not match.
    write_positions(&coords, &front.fab)?;

    // ── The junction, and the void it leaves ──────────────────────────────
    //
    // Two apexes can land on the same point, and not by accident: where three
    // faces meet at a convex corner, the corner cell of each pushes its apex
    // toward the same place. Two pyramids sharing an apex node is perfectly
    // sound — two more cells round one vertex. Two *nodes* at the same
    // position is not, so apexes are looked up by position before being made.
    let id = |v: u32| front.fab.ids[v as usize];
    let tol = front.mean_edge().max(f64::MIN_POSITIVE) * APEX_WELD;
    let mut apexes: HashMap<[i64; 3], NodeId> = HashMap::new();
    let mut kept: Vec<Node> = Vec::new();
    let mut pyramids: Vec<[NodeId; 5]> = Vec::new();
    let mut void: Vec<[NodeId; 3]> = Vec::new();
    for f in &front.facets {
        match f {
            Facet::Quad(q) => {
                let base: Vec<Point3> = q.iter().map(|&i| front.fab.pts[i as usize]).collect();
                let centre = Point3::from(
                    base.iter()
                        .fold(Point3::origin(), |a, p| a + p.coords)
                        .coords
                        / 4.0,
                );
                let edge = (0..4)
                    .map(|i| (base[(i + 1) % 4] - base[i]).norm())
                    .sum::<f64>()
                    / 4.0;
                let apex = centre - quad_normal(&base) * (edge * APEX_DEPTH);
                let key = [
                    (apex.x / tol).round() as i64,
                    (apex.y / tol).round() as i64,
                    (apex.z / tol).round() as i64,
                ];
                let apex_id = match apexes.get(&key) {
                    Some(&i) => i,
                    None => {
                        let a = Node::create_in(coords.clone(), &[apex.x, apex.y, apex.z])?;
                        let i = a.id();
                        kept.push(a);
                        apexes.insert(key, i);
                        i
                    }
                };
                // Seen from the apex the base runs the other way round, which
                // is the winding `PYRA5` asks for.
                let b = [id(q[0]), id(q[3]), id(q[2]), id(q[1])];
                pyramids.push([b[0], b[1], b[2], b[3], apex_id]);
                // The void's boundary at a pyramid is the pyramid's own
                // triangles, the other way round: the void's outward normal
                // points *into* the pyramid.
                for i in 0..4 {
                    void.push([b[(i + 1) % 4], b[i], apex_id]);
                }
            }
            // A triangular facet already faces the void the right way.
            Facet::Tri(t) => void.push([id(t[0]), id(t[1]), id(t[2])]),
        }
    }

    // Two pyramids that share an apex also share the triangle along their
    // common base edge, once from each side. That face is *between* them, not
    // on the void, so the pair cancels out.
    let mut times: HashMap<[u32; 3], usize> = HashMap::new();
    for t in &void {
        let mut k = [t[0].0, t[1].0, t[2].0];
        k.sort_unstable();
        *times.entry(k).or_insert(0) += 1;
    }
    void.retain(|t| {
        let mut k = [t[0].0, t[1].0, t[2].0];
        k.sort_unstable();
        times[&k] == 1
    });

    // ── The core ──────────────────────────────────────────────────────────
    let core = if void.is_empty() {
        Mesh::empty()
    } else {
        let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
        for t in &void {
            sm.add_cell(t)?;
        }
        let env = Mesh::from_submesh(sm);
        // Strict: the core has to reuse these triangles verbatim, or it would
        // not meet the pyramids node for node.
        super::triangulate_volume_cancellable(&env, core_size, false, cancel).map_err(|e| {
            PyrucastError::Message(format!(
                "pave_volume: the layer went in cleanly but its inner surface could not be \
                 filled with tetrahedra ({e}). The void left by the layer is too awkward \
                 for the core mesher: try a thinner layer or a finer envelope."
            ))
        })?
    };

    let hexes: Vec<[NodeId; 8]> = front.fab.hexes.iter().map(|c| c.map(id)).collect();
    let prisms: Vec<[NodeId; 6]> = front.fab.prisms.iter().map(|c| c.map(id)).collect();
    let out = materialize(&coords, hexes, prisms, pyramids, core);
    drop(kept);
    out
}

/// Push the fabric's smoothed positions back into the `Coords`.
fn write_positions(coords: &Handle<Coords>, fab: &plaster::front::Fabric) -> Result<()> {
    let mut c = coords.write();
    for (i, p) in fab.pts.iter().enumerate() {
        if fab.movable[i] {
            c.set_position(fab.ids[i], &[p.x, p.y, p.z])?;
        }
    }
    Ok(())
}

/// Outward unit normal of a quadrangular facet, by Newell's method so a
/// slightly non-planar quadrangle still gets a sensible answer.
fn quad_normal(p: &[Point3]) -> crate::atoms::Vector3 {
    let mut n = crate::atoms::Vector3::zeros();
    for i in 0..p.len() {
        let (a, b) = (p[i], p[(i + 1) % p.len()]);
        n.x += (a.y - b.y) * (a.z + b.z);
        n.y += (a.z - b.z) * (a.x + b.x);
        n.z += (a.x - b.x) * (a.y + b.y);
    }
    let len = n.norm();
    if len == 0.0 {
        n
    } else {
        n / len
    }
}

fn materialize(
    coords: &Handle<Coords>,
    hexes: Vec<[NodeId; 8]>,
    prisms: Vec<[NodeId; 6]>,
    pyramids: Vec<[NodeId; 5]>,
    core: Mesh,
) -> Result<Mesh> {
    let mut mesh = Mesh::empty();
    let mut push = |et: ElementType, cells: &[&[NodeId]]| -> Result<()> {
        if cells.is_empty() {
            return Ok(());
        }
        let mut sm = SubMesh::new(coords.clone(), et);
        for c in cells {
            sm.add_cell(c)?;
        }
        mesh.add_sub(Handle::new(sm))
    };
    let h: Vec<&[NodeId]> = hexes.iter().map(|c| c.as_slice()).collect();
    let p: Vec<&[NodeId]> = prisms.iter().map(|c| c.as_slice()).collect();
    let y: Vec<&[NodeId]> = pyramids.iter().map(|c| c.as_slice()).collect();
    push(ElementType::HEX8, &h)?;
    push(ElementType::PENTA6, &p)?;
    push(ElementType::PYRA5, &y)?;
    for sub in &core {
        mesh.add_sub(sub.clone())?;
    }
    if mesh.is_empty() {
        return Err(PyrucastError::Message(
            "pave_volume: produced no cell".into(),
        ));
    }
    let _ = coords.read();
    Ok(mesh)
}

// ─── Unit tests ─────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::coords::Coords;
    use std::collections::HashMap;

    /// The skin of a `nx × ny × nz` box of hexahedra: a closed QUA4 shell with
    /// outward normals, built with the operators rather than by hand.
    fn box_skin_sized(nx: usize, ny: usize, nz: usize, height: f64) -> Mesh {
        let coords = Handle::new(Coords::new(3).unwrap());
        let corner = |x: f64, z: f64| Node::create_in(coords.clone(), &[x, 0.0, z]).unwrap();
        let (a, b) = (corner(0.0, 0.0), corner(1.0, 0.0));
        let (c, d) = (corner(1.0, 1.0), corner(0.0, 1.0));
        let line = |p: &Node, q: &Node, n: usize| {
            crate::ops::mesh::line(p, q, n, ElementType::SEG2).unwrap()
        };
        // Wound so the face's normal is +y, the direction it is extruded in:
        // otherwise every hexahedron comes out inside-out and so does the skin.
        let mut ring = line(&a, &d, nz);
        for m in [line(&d, &c, nx), line(&c, &b, nz), line(&b, &a, nx)] {
            ring = ring.union(&m).unwrap();
        }
        let contour = crate::ops::mesh::consolidate(&ring).unwrap();
        let face = super::super::pave_surface(
            &contour,
            ElementType::QUA4,
            None,
            true,
            crate::ops::mesh::FrontRelax::Free,
        )
        .unwrap();
        let solid = crate::ops::mesh::extrude(&face, &[0.0, height, 0.0], ny).unwrap();
        crate::ops::mesh::skin(&solid, None).unwrap()
    }

    fn box_skin(nx: usize, ny: usize, nz: usize) -> Mesh {
        box_skin_sized(nx, ny, nz, 1.0)
    }

    /// The skin of an L-shaped solid: a re-entrant corner, which is where a
    /// front has to hold some facets back while others carry on.
    fn ell_skin(n: usize, height: f64, layers: usize) -> Mesh {
        let coords = Handle::new(Coords::new(3).unwrap());
        let pt = |x: f64, z: f64| Node::create_in(coords.clone(), &[x, 0.0, z]).unwrap();
        // Walk the L so the face's normal is +y.
        let corners = [
            (0.0, 0.0),
            (0.0, 1.0),
            (0.4, 1.0),
            (0.4, 0.4),
            (1.0, 0.4),
            (1.0, 0.0),
        ];
        let nodes: Vec<Node> = corners.iter().map(|&(x, z)| pt(x, z)).collect();
        let mut ring: Option<Mesh> = None;
        for i in 0..nodes.len() {
            let a = &nodes[i];
            let b = &nodes[(i + 1) % nodes.len()];
            let d = ((b.position().unwrap()[0] - a.position().unwrap()[0]).powi(2)
                + (b.position().unwrap()[2] - a.position().unwrap()[2]).powi(2))
            .sqrt();
            let k = ((d * n as f64).round() as usize).max(1);
            let seg = crate::ops::mesh::line(a, b, k, ElementType::SEG2).unwrap();
            ring = Some(match ring {
                None => seg,
                Some(m) => m.union(&seg).unwrap(),
            });
        }
        let contour = crate::ops::mesh::consolidate(&ring.unwrap()).unwrap();
        let face = super::super::pave_surface(
            &contour,
            ElementType::QUA4,
            None,
            true,
            crate::ops::mesh::FrontRelax::Free,
        )
        .unwrap();
        let solid = crate::ops::mesh::extrude(&face, &[0.0, height, 0.0], layers).unwrap();
        crate::ops::mesh::skin(&solid, None).unwrap()
    }

    /// Cells per element type.
    fn kinds(mesh: &Mesh) -> HashMap<ElementType, usize> {
        mesh.element_types()
            .unwrap()
            .into_iter()
            .zip(mesh.cell_counts().unwrap())
            .collect()
    }

    /// Every facet of every cell, counted. A conforming closed mesh uses each
    /// interior facet twice and each boundary facet once.
    fn facet_use(mesh: &Mesh) -> HashMap<Vec<NodeId>, usize> {
        let mut count = HashMap::new();
        for sm in mesh {
            let s = sm.read();
            let et = s.element_type();
            let npc = et.nodes_per_cell();
            for cell in s.connectivity().chunks(npc) {
                for f in et.as_kind().facets() {
                    let mut key: Vec<NodeId> = f.corners().iter().map(|&i| cell[i]).collect();
                    key.sort_unstable_by_key(|n| n.0);
                    *count.entry(key).or_insert(0) += 1;
                }
            }
        }
        count
    }

    #[test]
    fn a_box_gets_a_hexahedral_layer_over_a_tetrahedral_core() {
        let skin = box_skin(3, 3, 3);
        let mesh = pave_volume(&skin, 1, Some(0.15), Some(0.4)).unwrap();
        let k = kinds(&mesh);
        // Six faces of 3 × 3 quadrangles, one hexahedron and one pyramid each.
        assert_eq!(k.get(&ElementType::HEX8), Some(&54), "{k:?}");
        assert_eq!(k.get(&ElementType::PYRA5), Some(&54), "{k:?}");
        assert!(k.get(&ElementType::TET4).is_some_and(|&n| n > 0), "{k:?}");
        assert!(!k.contains_key(&ElementType::PENTA6), "{k:?}");
    }

    #[test]
    fn the_result_is_conforming_and_its_boundary_is_the_envelope() {
        let skin = box_skin(3, 3, 3);
        let mesh = pave_volume(&skin, 1, Some(0.15), Some(0.4)).unwrap();
        let used = facet_use(&mesh);
        assert!(
            used.values().all(|&n| n <= 2),
            "a facet is shared by more than two cells"
        );
        // The boundary facets are exactly the envelope's own.
        let boundary: usize = used.values().filter(|&&n| n == 1).count();
        assert_eq!(
            boundary, 54,
            "the mesh boundary should be the 54 skin quads"
        );
    }

    #[test]
    fn every_cell_has_a_positive_volume() {
        let skin = box_skin(3, 3, 3);
        let mesh = pave_volume(&skin, 1, Some(0.15), Some(0.4)).unwrap();
        let coords = mesh.coords().unwrap();
        for sub in &mesh {
            let space = crate::containers::finite_element_space::SubFiniteElementSpace::new(
                sub.clone(),
                crate::atoms::Interpolation::Lagrange1,
                crate::atoms::QuadratureRule::Gauss,
            )
            .unwrap();
            for c in 0..sub.read().cell_count() {
                for g in 0..space.gauss_count() {
                    let det = space.det_jacobian(c, g).unwrap();
                    assert!(
                        det > 0.0,
                        "cell {c} of {} has |J| = {det}",
                        space.element_type().unwrap()
                    );
                }
            }
        }
        let _ = coords;
    }

    #[test]
    fn several_layers_stack_inward() {
        let skin = box_skin(3, 3, 3);
        let mesh = pave_volume(&skin, 2, Some(0.08), Some(0.4)).unwrap();
        let k = kinds(&mesh);
        assert_eq!(k.get(&ElementType::HEX8), Some(&108), "{k:?}");
        assert_eq!(k.get(&ElementType::PYRA5), Some(&54), "{k:?}");
    }

    #[test]
    fn an_envelope_turned_inside_out_is_named_as_such() {
        let skin = crate::ops::mesh::invert(&box_skin(2, 2, 2)).unwrap();
        let err = pave_volume(&skin, 1, Some(0.15), Some(0.4)).unwrap_err();
        assert!(
            format!("{err}").contains("normals point into the material"),
            "{err}"
        );
    }

    #[test]
    fn an_over_thick_layer_is_clamped_by_the_room_rather_than_refused() {
        // Twice the width of the solid. A front that offsets the whole
        // envelope by one distance can only give up here; one that asks how
        // much room it has at each node simply takes what there is.
        let skin = box_skin(3, 3, 3);
        let mesh = pave_volume(&skin, 1, Some(2.0), Some(0.4)).unwrap();
        let k = kinds(&mesh);
        assert_eq!(k.get(&ElementType::HEX8), Some(&54), "{k:?}");
        // And the layer stayed inside the box, so no cell is inside out.
        let coords = mesh.coords().unwrap();
        let c = coords.read();
        for sub in &mesh {
            let s = sub.read();
            for &n in s.connectivity() {
                for x in c.position(n).unwrap() {
                    assert!(
                        (-1e-9..=1.0 + 1e-9).contains(x),
                        "a node left the box at {x}"
                    );
                }
            }
        }
    }

    /// Corner edge triples whose determinant is positive on the reference
    /// element, per type — the scaled Jacobian is read off these.
    fn corner_table(et: ElementType) -> &'static [[usize; 4]] {
        match et {
            ElementType::HEX8 => &[
                [0, 1, 3, 4],
                [1, 2, 0, 5],
                [2, 3, 1, 6],
                [3, 0, 2, 7],
                [4, 7, 5, 0],
                [5, 4, 6, 1],
                [6, 5, 7, 2],
                [7, 6, 4, 3],
            ],
            ElementType::PENTA6 => &[
                [0, 1, 2, 3],
                [1, 2, 0, 4],
                [2, 0, 1, 5],
                [3, 5, 4, 0],
                [4, 3, 5, 1],
                [5, 4, 3, 2],
            ],
            // The pyramid is judged on its four base corners; the apex has
            // four neighbours and no three of them frame it.
            ElementType::PYRA5 => &[[0, 1, 3, 4], [1, 2, 0, 4], [2, 3, 1, 4], [3, 0, 2, 4]],
            ElementType::TET4 => &[[0, 1, 2, 3], [1, 2, 0, 3], [2, 0, 1, 3], [3, 1, 0, 2]],
            _ => &[],
        }
    }

    /// Scaled Jacobian of every cell of a mesh, by element type.
    fn qualities(mesh: &Mesh) -> HashMap<ElementType, Vec<f64>> {
        let coords = mesh.coords().unwrap();
        let c = coords.read();
        let at = |n: NodeId| {
            let v = c.position(n).unwrap();
            Point3::new(v[0], v[1], v[2])
        };
        let mut out: HashMap<ElementType, Vec<f64>> = HashMap::new();
        for sm in mesh {
            let s = sm.read();
            let et = s.element_type();
            let table = corner_table(et);
            let npc = et.nodes_per_cell();
            for cell in s.connectivity().chunks(npc) {
                let p: Vec<Point3> = cell.iter().map(|&n| at(n)).collect();
                let mut worst = f64::INFINITY;
                for k in table {
                    let o = p[k[0]];
                    let e = [p[k[1]] - o, p[k[2]] - o, p[k[3]] - o];
                    let l: f64 = e.iter().map(|v| v.norm()).product();
                    worst = worst.min(if l == 0.0 {
                        0.0
                    } else {
                        e[0].cross(&e[1]).dot(&e[2]) / l
                    });
                }
                out.entry(et).or_default().push(worst);
            }
        }
        out
    }

    fn pct(v: &mut [f64], q: f64) -> f64 {
        v.sort_by(|a, b| a.partial_cmp(b).unwrap());
        v[((v.len() - 1) as f64 * q) as usize]
    }

    fn report(name: &str, skin: &Mesh, layers: usize, thickness: Option<f64>, core: Option<f64>) {
        let t0 = std::time::Instant::now();
        let mesh = match pave_volume(skin, layers, thickness, core) {
            Ok(m) => m,
            Err(e) => {
                println!(
                    "│  {name:<34} ÉCHEC — {}",
                    format!("{e}").chars().skip(90).take(96).collect::<String>()
                );
                return;
            }
        };
        let dt = t0.elapsed();
        let q = qualities(&mesh);
        let total: usize = q.values().map(|v| v.len()).sum();
        let all: Vec<f64> = q.values().flatten().copied().collect();
        let worst = pct(&mut all.clone(), 0.0);
        let p1 = pct(&mut all.clone(), 0.01);
        let med = pct(&mut all.clone(), 0.5);
        let inverted = all.iter().filter(|&&x| x <= 0.0).count();
        let mut kinds: Vec<(ElementType, usize)> = q.iter().map(|(k, v)| (*k, v.len())).collect();
        kinds.sort_by_key(|(k, _)| k.name());
        let composition: Vec<String> = kinds.iter().map(|(k, n)| format!("{k} {n}")).collect();
        println!(
            "│  {name:<34} {total:>7} mailles  {:>6.2} s  {:>8.0}/s",
            dt.as_secs_f64(),
            total as f64 / dt.as_secs_f64()
        );
        println!("│      {}", composition.join(", "));
        println!(
            "│      jacobien normalisé  min {worst:.3}  p1 {p1:.3}  médiane {med:.3}  inversées {inverted}"
        );
        for (k, _) in kinds {
            let mut vals = q[&k].clone();
            println!(
                "│        {k:<7} min {:.3}  médiane {:.3}",
                pct(&mut vals.clone(), 0.0),
                pct(&mut vals, 0.5)
            );
        }
    }

    /// Quality and throughput on named cases. Run explicitly:
    /// `cargo test --release -- --ignored volume_report --nocapture`.
    #[test]
    #[ignore = "reporting run, not an assertion"]
    fn volume_report() {
        println!("\n┌─ pave_volume");
        println!("│");

        // A. Cube, une couche puis deux — le cas de référence.
        let cube = box_skin(6, 6, 6);
        report("cube 6³, 1 couche", &cube, 1, Some(0.08), Some(0.2));
        report("cube 6³, 2 couches", &cube, 2, Some(0.06), Some(0.2));
        report("cube 6³, épaisseur libre", &cube, 1, None, Some(0.2));

        // B. Plaque mince : la place disponible borne le pas, la couture ferme.
        let plaque = box_skin_sized(8, 1, 8, 0.08);
        report("plaque mince 1×0,08×1", &plaque, 1, Some(1.0), Some(0.2));

        // C. Barreau allongé : une direction bien plus fine que les autres.
        let barreau = box_skin_sized(10, 2, 2, 0.25);
        report("barreau 1×0,25×1", &barreau, 1, Some(0.1), Some(0.25));

        // D. Solide rentrant : le coin concave est ce qui force le front à
        //    retenir des facettes pendant que les autres avancent.
        let ell = ell_skin(6, 0.6, 4);
        report("L rentrant, 1 couche", &ell, 1, Some(0.08), Some(0.2));
        report("L rentrant, 2 couches", &ell, 2, Some(0.06), Some(0.2));

        // E. Montée en taille sur le cube.
        for n in [8, 12, 16] {
            let s = box_skin(n, n, n);
            report(
                &format!("cube {n}³, 1 couche"),
                &s,
                1,
                Some(0.5 / n as f64),
                Some(1.5 / n as f64),
            );
        }
        println!("└─");
    }
}