pyrucast 0.8.0

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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
//! Bring a gmsh mesh into pyrucast — from a `.msh` file, or straight from
//! the arrays a live gmsh session hands over.
//!
//! Supported on-disk formats: **MSH 2.2** and **MSH 4.1**, in both their
//! **ASCII** (`$MeshFormat … 0 …`) and **binary** (`… 1 …`) variants. The
//! binary endianness is taken from the one-int marker gmsh writes in
//! `$MeshFormat`, so little- and big-endian files both read. The element
//! types pyrucast knows are mapped from their gmsh codes:
//!
//! | gmsh code | [`ElementType`] |
//! |---|---|
//! | `1`  | `SEG2` |
//! | `2`  | `TRI3` |
//! | `3`  | `QUA4` |
//! | `4`  | `TET4` |
//! | `5`  | `HEX8` |
//! | `6`  | `PENTA6` |
//! | `7`  | `PYRA5` |
//! | `15` | `POI1` |
//! | `8`  | `SEG3` |
//! | `9`  | `TRI6` |
//! | `16` | `QUA8` |
//! | `10` | `QUA9` |
//! | `11` | `TET10` |
//! | `17` | `HEX20` |
//! | `18` | `PENTA15` |
//! | `12` | `HEX27` |
//!
//! Any other gmsh element type (order 3+, …) is an error. For most
//! types the local node ordering already matches pyrucast's reference frame
//! (see [`crate::atoms::ElementType`]) and the connectivity is
//! copied verbatim; the quadratic **volumes** (`TET10`, `HEX20`, `PENTA15`,
//! `HEX27`) have their mid-edge / face nodes **reordered** to pyrucast's
//! (VTK) order — see `gmsh_node_permutation`.
//!
//! # Grouping
//!
//! The result is **one [`Mesh`] per gmsh physical group**, returned as a
//! list of `(group name, Mesh)` pairs in order of first appearance. Inside
//! a group's mesh there is **one [`SubMesh`] per element type**. All meshes
//! share a **single [`Coords`]**, so a node shared between two groups (e.g.
//! a boundary node belonging both to a surface and to its bounding line) is
//! the same node on both sides — handy for posing boundary conditions on a
//! named region read straight from the file. The Python binding turns the
//! list into a `dict[str, Mesh]`.
//!
//! Elements that carry no physical group land under the synthetic name
//! `"<ungrouped>"`. Only nodes actually **referenced** by an element are
//! materialized in the `Coords`; isolated nodes listed in the file but used
//! by no element are skipped.
//!
//! # Dimension
//!
//! The caller supplies the `Coords` to read into, so its dimension decides
//! how many coordinates are kept: gmsh always stores three per node, of
//! which the first `coords.dim()` are taken (a 2-D `Coords` flattens the
//! mesh onto its `xy` projection). The `Coords` may already hold geometry —
//! the import is merged into it.
//!
//! # From a file, or from memory
//!
//! Two front-ends share one back-end. [`read_gmsh`] (and its `_str` / `_bytes`
//! siblings) parses the on-disk format; [`from_gmsh_arrays`] takes the node
//! table and the element blocks **already in memory**, in exactly the shape
//! `gmsh.model.mesh.getNodes()` and `getElements()` return them — which is
//! also pyrucast's own shape, since a [`SubMesh`] holds one element type and
//! a flat connectivity. The Python layer builds on the latter: the pure-Python
//! `pyrucast.mesh.from_gmsh(coords)` reads the current gmsh model and feeds it
//! here, so a script can mesh in gmsh and compute in pyrucast without a file
//! ever touching the disk.
//!
//! Both go through `GroupBuilder`, so the grouping, the node sharing and the
//! quadratic reordering are the same code either way — a mesh read from a file
//! and the same mesh taken from memory come out identical, cell for cell.

use crate::aggregate::Aggregate;
use crate::atoms::{ElementType, NodeId};
use crate::containers::mesh::{Mesh, SubMesh};
use crate::coords::Coords;
use crate::error::{PyrucastError, Result};
use crate::handle::Handle;
use std::collections::HashMap;
use std::path::Path;

/// Synthetic group name for elements without a physical group.
const UNGROUPED: &str = "<ungrouped>";

fn err(msg: impl Into<String>) -> PyrucastError {
    PyrucastError::Message(msg.into())
}

/// Map a gmsh element-type code to a pyrucast [`ElementType`].
fn element_type_from_gmsh(code: u32) -> Result<ElementType> {
    ElementType::ALL
        .iter()
        .copied()
        .find(|et| et.as_kind().gmsh_code() == code)
        .ok_or_else(|| {
            let known: Vec<String> = ElementType::ALL
                .iter()
                .map(|et| format!("{}={et}", et.as_kind().gmsh_code()))
                .collect();
            err(format!(
                "gmsh: unsupported element type {code} (supported: {})",
                known.join(", ")
            ))
        })
}

/// Permutation mapping a gmsh element's node order to pyrucast's (VTK) order:
/// `pyrucast[i] = gmsh[perm[i]]`. Returns `None` when the two orders already
/// coincide (all linear types, plus `SEG3`/`TRI6`/`QUA8`/`QUA9`).
///
/// gmsh numbers the mid-edge nodes of `TET10`, `HEX20`, `PENTA15` and `HEX27`
/// (and the face nodes of `HEX27`) in a different order than VTK; these tables
/// realign them (same convention as meshio).
fn gmsh_node_permutation(et: ElementType) -> Option<&'static [usize]> {
    et.as_kind().gmsh_permutation()
}

// ─── Parsed (pure) representation ────────────────────────────────────────────

/// One element as read from the file: its pyrucast type, the gmsh node tags
/// (in file order), and the physical-group names it belongs to.
struct ParsedElement {
    element_type: ElementType,
    groups: Vec<String>,
    nodes: Vec<u64>,
}

/// The pure result of parsing — coordinates by gmsh node tag and the list of
/// elements. No store access happens here.
struct Parsed {
    coords: HashMap<u64, [f64; 3]>,
    elements: Vec<ParsedElement>,
}

// ─── Token cursor over a section's lines ─────────────────────────────────────

/// Whitespace-token cursor over the (flattened) lines of one `$Section`.
/// gmsh sections are token streams whose structure is driven by leading
/// counts, so line boundaries don't matter once the counts are known.
struct Cursor<'a> {
    toks: Vec<&'a str>,
    i: usize,
}

impl<'a> Cursor<'a> {
    fn new(lines: &[&'a str]) -> Self {
        let toks = lines.iter().flat_map(|l| l.split_whitespace()).collect();
        Self { toks, i: 0 }
    }

    fn next_tok(&mut self) -> Result<&'a str> {
        let t = self
            .toks
            .get(self.i)
            .copied()
            .ok_or_else(|| err("gmsh: unexpected end of section"))?;
        self.i += 1;
        Ok(t)
    }

    fn u64(&mut self) -> Result<u64> {
        let t = self.next_tok()?;
        t.parse()
            .map_err(|_| err(format!("gmsh: expected an integer, got {t:?}")))
    }

    fn i64(&mut self) -> Result<i64> {
        let t = self.next_tok()?;
        t.parse()
            .map_err(|_| err(format!("gmsh: expected an integer, got {t:?}")))
    }

    fn usize(&mut self) -> Result<usize> {
        Ok(self.u64()? as usize)
    }

    fn f64(&mut self) -> Result<f64> {
        let t = self.next_tok()?;
        t.parse()
            .map_err(|_| err(format!("gmsh: expected a number, got {t:?}")))
    }
}

/// Split the file text into `name -> body lines`, one entry per `$Name …
/// $EndName` block. Unknown sections are kept too (and simply never read).
fn split_sections(text: &str) -> HashMap<String, Vec<&str>> {
    let mut map = HashMap::new();
    let mut lines = text.lines();
    while let Some(line) = lines.next() {
        let t = line.trim();
        let Some(name) = t.strip_prefix('$') else {
            continue;
        };
        if name.starts_with("End") {
            continue;
        }
        let end = format!("$End{name}");
        let mut body = Vec::new();
        for l in lines.by_ref() {
            if l.trim() == end {
                break;
            }
            body.push(l);
        }
        map.insert(name.to_string(), body);
    }
    map
}

/// Parse `$PhysicalNames` into `(dim, physical tag) -> name`.
fn parse_physical_names(map: &HashMap<String, Vec<&str>>) -> Result<HashMap<(u8, i64), String>> {
    let mut names = HashMap::new();
    let Some(lines) = map.get("PhysicalNames") else {
        return Ok(names);
    };
    let mut it = lines.iter().map(|l| l.trim()).filter(|l| !l.is_empty());
    let count: usize = it
        .next()
        .ok_or_else(|| err("gmsh: empty $PhysicalNames"))?
        .parse()
        .map_err(|_| err("gmsh: bad $PhysicalNames count"))?;
    for _ in 0..count {
        let line = it
            .next()
            .ok_or_else(|| err("gmsh: truncated $PhysicalNames"))?;
        parse_physical_name_line(line, &mut names)?;
    }
    Ok(names)
}

/// Parse one `$PhysicalNames` entry — `dim tag "name"` — into `names`.
fn parse_physical_name_line(line: &str, names: &mut HashMap<(u8, i64), String>) -> Result<()> {
    let q1 = line
        .find('"')
        .ok_or_else(|| err("gmsh: $PhysicalNames entry without a quoted name"))?;
    let q2 = line
        .rfind('"')
        .filter(|&q| q > q1)
        .ok_or_else(|| err("gmsh: $PhysicalNames entry without a closing quote"))?;
    let name = line[q1 + 1..q2].to_string();
    let mut head = line[..q1].split_whitespace();
    let dim: u8 = head
        .next()
        .and_then(|s| s.parse().ok())
        .ok_or_else(|| err("gmsh: $PhysicalNames entry without a dimension"))?;
    let tag: i64 = head
        .next()
        .and_then(|s| s.parse().ok())
        .ok_or_else(|| err("gmsh: $PhysicalNames entry without a tag"))?;
    names.insert((dim, tag), name);
    Ok(())
}

/// Resolve a list of physical tags (of the given dimension) to group names,
/// falling back to `"<ungrouped>"` when there is none.
fn groups_of(dim: u8, phys: &[i64], names: &HashMap<(u8, i64), String>) -> Vec<String> {
    if phys.is_empty() {
        return vec![UNGROUPED.to_string()];
    }
    phys.iter()
        .map(|&p| {
            names
                .get(&(dim, p))
                .cloned()
                .unwrap_or_else(|| format!("physical {p}"))
        })
        .collect()
}

// ─── MSH 2.2 ─────────────────────────────────────────────────────────────────

fn parse_v2(
    map: &HashMap<String, Vec<&str>>,
    names: &HashMap<(u8, i64), String>,
) -> Result<Parsed> {
    let nodes_sec = map
        .get("Nodes")
        .ok_or_else(|| err("gmsh: missing $Nodes"))?;
    let mut c = Cursor::new(nodes_sec);
    let n = c.usize()?;
    let mut coords = HashMap::with_capacity(n);
    for _ in 0..n {
        let tag = c.u64()?;
        let xyz = [c.f64()?, c.f64()?, c.f64()?];
        coords.insert(tag, xyz);
    }

    let elems_sec = map
        .get("Elements")
        .ok_or_else(|| err("gmsh: missing $Elements"))?;
    let mut c = Cursor::new(elems_sec);
    let ne = c.usize()?;
    let mut elements = Vec::with_capacity(ne);
    for _ in 0..ne {
        let _id = c.u64()?;
        let et = element_type_from_gmsh(c.u64()? as u32)?;
        let ntags = c.usize()?;
        // In MSH 2.2 the first tag is the physical group, the second the
        // elementary (geometric) entity; the rest are partition tags.
        let mut phys: Vec<i64> = Vec::new();
        for k in 0..ntags {
            let t = c.i64()?;
            if k == 0 && t != 0 {
                phys.push(t);
            }
        }
        let npc = et.nodes_per_cell();
        let mut nodes = Vec::with_capacity(npc);
        for _ in 0..npc {
            nodes.push(c.u64()?);
        }
        let groups = groups_of(et.topological_dim() as u8, &phys, names);
        elements.push(ParsedElement {
            element_type: et,
            groups,
            nodes,
        });
    }
    Ok(Parsed { coords, elements })
}

// ─── MSH 4.1 ─────────────────────────────────────────────────────────────────

/// Parse `$Entities` into `(dim, entity tag) -> [physical tags]`.
fn parse_entities_v4(map: &HashMap<String, Vec<&str>>) -> Result<HashMap<(u8, i64), Vec<i64>>> {
    let mut entity_phys = HashMap::new();
    let Some(sec) = map.get("Entities") else {
        return Ok(entity_phys);
    };
    let mut c = Cursor::new(sec);
    let np = c.usize()?;
    let nc = c.usize()?;
    let ns = c.usize()?;
    let nv = c.usize()?;
    // Points: tag, 3 coords, numPhysical, physical tags.
    for _ in 0..np {
        let tag = c.i64()?;
        for _ in 0..3 {
            c.f64()?;
        }
        let k = c.usize()?;
        let mut phys = Vec::with_capacity(k);
        for _ in 0..k {
            phys.push(c.i64()?);
        }
        entity_phys.insert((0u8, tag), phys);
    }
    // Curves / surfaces / volumes: tag, 6-coord bbox, numPhysical, physical
    // tags, numBounding, bounding entity tags.
    for (dim, count) in [(1u8, nc), (2u8, ns), (3u8, nv)] {
        for _ in 0..count {
            let tag = c.i64()?;
            for _ in 0..6 {
                c.f64()?;
            }
            let k = c.usize()?;
            let mut phys = Vec::with_capacity(k);
            for _ in 0..k {
                phys.push(c.i64()?);
            }
            let nb = c.usize()?;
            for _ in 0..nb {
                c.i64()?;
            }
            entity_phys.insert((dim, tag), phys);
        }
    }
    Ok(entity_phys)
}

fn parse_v4(
    map: &HashMap<String, Vec<&str>>,
    names: &HashMap<(u8, i64), String>,
) -> Result<Parsed> {
    let entity_phys = parse_entities_v4(map)?;

    // $Nodes: blocks of (entityDim, entityTag, parametric, count), then the
    // node tags, then the coordinate triples.
    let nodes_sec = map
        .get("Nodes")
        .ok_or_else(|| err("gmsh: missing $Nodes"))?;
    let mut c = Cursor::new(nodes_sec);
    let nblocks = c.usize()?;
    let total = c.usize()?;
    let _min = c.u64()?;
    let _max = c.u64()?;
    let mut coords = HashMap::with_capacity(total);
    for _ in 0..nblocks {
        let _edim = c.i64()?;
        let _etag = c.i64()?;
        let parametric = c.usize()?;
        let cnt = c.usize()?;
        if parametric != 0 {
            return Err(err("gmsh: parametric nodes are not supported"));
        }
        let mut tags = Vec::with_capacity(cnt);
        for _ in 0..cnt {
            tags.push(c.u64()?);
        }
        for &tag in &tags {
            let xyz = [c.f64()?, c.f64()?, c.f64()?];
            coords.insert(tag, xyz);
        }
    }

    // $Elements: blocks of (entityDim, entityTag, elementType, count), then
    // one line `elemTag node…` per element.
    let elems_sec = map
        .get("Elements")
        .ok_or_else(|| err("gmsh: missing $Elements"))?;
    let mut c = Cursor::new(elems_sec);
    let nblocks = c.usize()?;
    let total = c.usize()?;
    let _min = c.u64()?;
    let _max = c.u64()?;
    let mut elements = Vec::with_capacity(total);
    for _ in 0..nblocks {
        let edim = c.i64()?;
        let etag = c.i64()?;
        let et = element_type_from_gmsh(c.u64()? as u32)?;
        let cnt = c.usize()?;
        let dim = edim as u8;
        let phys = entity_phys.get(&(dim, etag)).cloned().unwrap_or_default();
        let groups = groups_of(dim, &phys, names);
        let npc = et.nodes_per_cell();
        for _ in 0..cnt {
            let _id = c.u64()?;
            let mut nodes = Vec::with_capacity(npc);
            for _ in 0..npc {
                nodes.push(c.u64()?);
            }
            elements.push(ParsedElement {
                element_type: et,
                groups: groups.clone(),
                nodes,
            });
        }
    }
    Ok(Parsed { coords, elements })
}

// ─── Top-level parse + build ─────────────────────────────────────────────────

/// Parse the full text of a gmsh `.msh` file into the pure [`Parsed`] form.
fn parse_gmsh_text(text: &str) -> Result<Parsed> {
    let map = split_sections(text);
    let fmt = map
        .get("MeshFormat")
        .ok_or_else(|| err("gmsh: missing $MeshFormat (is this a .msh file?)"))?;
    // file-type is handled by the binary/ASCII dispatch in `parse_gmsh`;
    // here we only need the version to pick the v2/v4 section layout.
    let mut c = Cursor::new(fmt);
    let version = c.next_tok()?.to_string();
    let names = parse_physical_names(&map)?;
    match version.split('.').next() {
        Some("2") => parse_v2(&map, &names),
        Some("4") => parse_v4(&map, &names),
        _ => Err(err(format!(
            "gmsh: unsupported MSH version {version:?} (supported: 2.2, 4.1)"
        ))),
    }
}

// ─── Binary MSH ──────────────────────────────────────────────────────────────

/// Byte cursor over a binary `.msh` file. Reads ASCII lines (section
/// markers and the count lines that gmsh keeps textual) and fixed-width
/// little-/big-endian scalars (the binary payloads).
struct ByteReader<'a> {
    buf: &'a [u8],
    pos: usize,
    le: bool,
}

impl<'a> ByteReader<'a> {
    /// Next `\n`-terminated line, trimmed of surrounding ASCII whitespace
    /// (incl. `\r`). `None` at end of buffer.
    fn try_line(&mut self) -> Option<&'a [u8]> {
        if self.pos >= self.buf.len() {
            return None;
        }
        let rest = &self.buf[self.pos..];
        let line = match rest.iter().position(|&b| b == b'\n') {
            Some(i) => {
                self.pos += i + 1;
                &rest[..i]
            }
            None => {
                self.pos = self.buf.len();
                rest
            }
        };
        Some(line.trim_ascii())
    }

    fn line(&mut self) -> Result<&'a [u8]> {
        self.try_line()
            .ok_or_else(|| err("gmsh: unexpected end of file"))
    }

    fn take(&mut self, n: usize) -> Result<&'a [u8]> {
        let end = self
            .pos
            .checked_add(n)
            .filter(|&e| e <= self.buf.len())
            .ok_or_else(|| err("gmsh: truncated binary data"))?;
        let s = &self.buf[self.pos..end];
        self.pos = end;
        Ok(s)
    }

    fn i32(&mut self) -> Result<i32> {
        let b: [u8; 4] = self.take(4)?.try_into().unwrap();
        Ok(if self.le {
            i32::from_le_bytes(b)
        } else {
            i32::from_be_bytes(b)
        })
    }

    /// A gmsh `size_t` (8 bytes).
    fn size_t(&mut self) -> Result<u64> {
        let b: [u8; 8] = self.take(8)?.try_into().unwrap();
        Ok(if self.le {
            u64::from_le_bytes(b)
        } else {
            u64::from_be_bytes(b)
        })
    }

    fn f64(&mut self) -> Result<f64> {
        let b: [u8; 8] = self.take(8)?.try_into().unwrap();
        Ok(if self.le {
            f64::from_le_bytes(b)
        } else {
            f64::from_be_bytes(b)
        })
    }

    /// Consume lines until one equals `marker` (the section's `$End…`).
    fn skip_to(&mut self, marker: &[u8]) -> Result<()> {
        loop {
            if self.line()? == marker {
                return Ok(());
            }
        }
    }
}

/// Parse a non-empty ASCII line as an integer count.
fn ascii_count(line: &[u8]) -> Result<usize> {
    std::str::from_utf8(line)
        .ok()
        .and_then(|s| s.trim().parse().ok())
        .ok_or_else(|| err("gmsh: expected an integer count"))
}

/// Read the version major and file-type from `$MeshFormat` without decoding
/// the rest — enough to choose the ASCII or binary path.
fn peek_format(buf: &[u8]) -> Result<(u32, u64)> {
    let mut r = ByteReader {
        buf,
        pos: 0,
        le: true,
    };
    loop {
        if r.line()? == b"$MeshFormat" {
            break;
        }
    }
    let line = std::str::from_utf8(r.line()?).map_err(|_| err("gmsh: bad $MeshFormat line"))?;
    let mut it = line.split_whitespace();
    let version = it.next().ok_or_else(|| err("gmsh: empty $MeshFormat"))?;
    let filetype: u64 = it
        .next()
        .and_then(|s| s.parse().ok())
        .ok_or_else(|| err("gmsh: $MeshFormat without a file-type"))?;
    let major: u32 = version
        .split('.')
        .next()
        .and_then(|s| s.parse().ok())
        .ok_or_else(|| err(format!("gmsh: unparsable MSH version {version:?}")))?;
    Ok((major, filetype))
}

/// Parse a binary `.msh` (`file-type 1`), MSH major version 2 or 4.
fn parse_binary(buf: &[u8], major: u32) -> Result<Parsed> {
    let mut r = ByteReader {
        buf,
        pos: 0,
        le: true,
    };
    // Re-reach $MeshFormat, skip the version line, then read the endianness
    // marker: an int written as `1` in the file's own byte order.
    loop {
        if r.line()? == b"$MeshFormat" {
            break;
        }
    }
    r.line()?; // version / file-type / data-size
    let marker = r.take(4)?.try_into().unwrap();
    r.le = i32::from_le_bytes(marker) == 1;
    if !r.le && i32::from_be_bytes(marker) != 1 {
        return Err(err("gmsh: unrecognized binary endianness marker"));
    }
    r.skip_to(b"$EndMeshFormat")?;

    let mut names: HashMap<(u8, i64), String> = HashMap::new();
    let mut entity_phys: HashMap<(u8, i64), Vec<i64>> = HashMap::new();
    let mut parsed = Parsed {
        coords: HashMap::new(),
        elements: Vec::new(),
    };

    while let Some(line) = r.try_line() {
        if line.is_empty() {
            continue;
        }
        match line {
            b"$PhysicalNames" => {
                let count = ascii_count(r.line()?)?;
                for _ in 0..count {
                    let l =
                        std::str::from_utf8(r.line()?).map_err(|_| err("gmsh: bad name line"))?;
                    parse_physical_name_line(l, &mut names)?;
                }
                r.skip_to(b"$EndPhysicalNames")?;
            }
            b"$Entities" if major == 4 => {
                entity_phys = parse_entities_binary(&mut r)?;
            }
            b"$Nodes" if major == 2 => parse_nodes_v2_binary(&mut r, &mut parsed.coords)?,
            b"$Nodes" => parse_nodes_v4_binary(&mut r, &mut parsed.coords)?,
            b"$Elements" if major == 2 => {
                parse_elements_v2_binary(&mut r, &names, &mut parsed.elements)?
            }
            b"$Elements" => {
                parse_elements_v4_binary(&mut r, &entity_phys, &names, &mut parsed.elements)?
            }
            other if other.starts_with(b"$") && !other.starts_with(b"$End") => {
                // Unknown section: skip to its matching $End… marker.
                let mut end = b"$End".to_vec();
                end.extend_from_slice(&other[1..]);
                r.skip_to(&end)?;
            }
            _ => {}
        }
    }
    Ok(parsed)
}

fn parse_entities_binary(r: &mut ByteReader) -> Result<HashMap<(u8, i64), Vec<i64>>> {
    let mut entity_phys = HashMap::new();
    let np = r.size_t()?;
    let nc = r.size_t()?;
    let ns = r.size_t()?;
    let nv = r.size_t()?;
    for _ in 0..np {
        let tag = r.i32()? as i64;
        for _ in 0..3 {
            r.f64()?;
        }
        let k = r.size_t()?;
        let phys = (0..k)
            .map(|_| r.i32().map(|t| t as i64))
            .collect::<Result<_>>()?;
        entity_phys.insert((0u8, tag), phys);
    }
    for (dim, count) in [(1u8, nc), (2u8, ns), (3u8, nv)] {
        for _ in 0..count {
            let tag = r.i32()? as i64;
            for _ in 0..6 {
                r.f64()?;
            }
            let k = r.size_t()?;
            let phys = (0..k)
                .map(|_| r.i32().map(|t| t as i64))
                .collect::<Result<_>>()?;
            let nb = r.size_t()?;
            for _ in 0..nb {
                r.i32()?;
            }
            entity_phys.insert((dim, tag), phys);
        }
    }
    r.skip_to(b"$EndEntities")?;
    Ok(entity_phys)
}

fn parse_nodes_v2_binary(r: &mut ByteReader, coords: &mut HashMap<u64, [f64; 3]>) -> Result<()> {
    let n = ascii_count(r.line()?)?;
    for _ in 0..n {
        let tag = r.i32()? as u64;
        let xyz = [r.f64()?, r.f64()?, r.f64()?];
        coords.insert(tag, xyz);
    }
    r.skip_to(b"$EndNodes")
}

fn parse_elements_v2_binary(
    r: &mut ByteReader,
    names: &HashMap<(u8, i64), String>,
    elements: &mut Vec<ParsedElement>,
) -> Result<()> {
    let total = ascii_count(r.line()?)?;
    let mut done = 0;
    while done < total {
        // Block header: element type, count in block, number of tags.
        let et = element_type_from_gmsh(r.i32()? as u32)?;
        let num_in_block = r.i32()? as usize;
        let num_tags = r.i32()? as usize;
        let npc = et.nodes_per_cell();
        for _ in 0..num_in_block {
            let _id = r.i32()?;
            let mut phys: Vec<i64> = Vec::new();
            for k in 0..num_tags {
                let t = r.i32()?;
                if k == 0 && t != 0 {
                    phys.push(t as i64);
                }
            }
            let nodes = (0..npc)
                .map(|_| r.i32().map(|v| v as u64))
                .collect::<Result<_>>()?;
            let groups = groups_of(et.topological_dim() as u8, &phys, names);
            elements.push(ParsedElement {
                element_type: et,
                groups,
                nodes,
            });
        }
        done += num_in_block;
    }
    r.skip_to(b"$EndElements")
}

fn parse_nodes_v4_binary(r: &mut ByteReader, coords: &mut HashMap<u64, [f64; 3]>) -> Result<()> {
    let nblocks = r.size_t()?;
    let _total = r.size_t()?;
    let _min = r.size_t()?;
    let _max = r.size_t()?;
    for _ in 0..nblocks {
        let _edim = r.i32()?;
        let _etag = r.i32()?;
        let parametric = r.i32()?;
        let cnt = r.size_t()? as usize;
        if parametric != 0 {
            return Err(err("gmsh: parametric nodes are not supported"));
        }
        let tags: Vec<u64> = (0..cnt).map(|_| r.size_t()).collect::<Result<_>>()?;
        for &tag in &tags {
            let xyz = [r.f64()?, r.f64()?, r.f64()?];
            coords.insert(tag, xyz);
        }
    }
    r.skip_to(b"$EndNodes")
}

fn parse_elements_v4_binary(
    r: &mut ByteReader,
    entity_phys: &HashMap<(u8, i64), Vec<i64>>,
    names: &HashMap<(u8, i64), String>,
    elements: &mut Vec<ParsedElement>,
) -> Result<()> {
    let nblocks = r.size_t()?;
    let _total = r.size_t()?;
    let _min = r.size_t()?;
    let _max = r.size_t()?;
    for _ in 0..nblocks {
        let edim = r.i32()?;
        let etag = r.i32()? as i64;
        let et = element_type_from_gmsh(r.i32()? as u32)?;
        let cnt = r.size_t()? as usize;
        let dim = edim as u8;
        let phys = entity_phys.get(&(dim, etag)).cloned().unwrap_or_default();
        let groups = groups_of(dim, &phys, names);
        let npc = et.nodes_per_cell();
        for _ in 0..cnt {
            let _tag = r.size_t()?;
            let nodes = (0..npc).map(|_| r.size_t()).collect::<Result<_>>()?;
            elements.push(ParsedElement {
                element_type: et,
                groups: groups.clone(),
                nodes,
            });
        }
    }
    r.skip_to(b"$EndElements")
}

/// Parse a gmsh `.msh` file (ASCII or binary) into the pure [`Parsed`] form.
fn parse_gmsh(buf: &[u8]) -> Result<Parsed> {
    let (major, filetype) = peek_format(buf)?;
    match filetype {
        0 => {
            let text = std::str::from_utf8(buf)
                .map_err(|_| err("gmsh: file declared ASCII but is not valid UTF-8"))?;
            parse_gmsh_text(text)
        }
        1 => parse_binary(buf, major),
        other => Err(err(format!(
            "gmsh: unsupported file-type {other} (0 = ASCII, 1 = binary)"
        ))),
    }
}

// ─── Shared back-end: cells in, one `Mesh` per group out ─────────────────────

/// The half of the import that neither front-end owns: it takes cells — each
/// with its pyrucast element type, its node ids in **gmsh** order and the
/// physical groups it belongs to — and lays them out as one [`Mesh`] per group,
/// one [`SubMesh`] per element type inside a group.
///
/// Both `read_gmsh` (from a file) and [`from_gmsh_arrays`] (from memory) drive
/// it, which is what makes them agree cell for cell. Only the way a node tag is
/// turned into coordinates differs between them, and that is the caller's
/// closure in [`GroupBuilder::node_of`].
/// What one physical group has accumulated: its element types in first-seen
/// order, and the flat connectivity gathered for each of them.
type GroupCells = (Vec<ElementType>, HashMap<ElementType, Vec<NodeId>>);

struct GroupBuilder {
    coords: Handle<Coords>,
    /// How many of gmsh's three coordinates to keep, from the `Coords` itself.
    dim: usize,
    /// gmsh node tag → **rank** of the node in creation order. The nodes
    /// themselves do not exist yet: their coordinates pile up in `positions`
    /// and the whole cloud is created in one locked pass by `finish`, so an
    /// import costs one `Coords` write instead of one per node.
    node_map: HashMap<u64, u32>,
    /// Coordinates of the nodes to create, rank by rank, flat.
    positions: Vec<f64>,
    /// Group names in order of first appearance — the order of the result.
    order: Vec<String>,
    groups: HashMap<String, GroupCells>,
    /// One cell's connectivity, permuted. Reused across cells so the hot loop
    /// allocates nothing.
    scratch: Vec<NodeId>,
}

impl GroupBuilder {
    fn new(coords: Handle<Coords>) -> Self {
        let dim = coords.read().dim() as usize;
        Self {
            coords,
            dim,
            node_map: HashMap::new(),
            positions: Vec::new(),
            order: Vec::new(),
            groups: HashMap::new(),
            scratch: Vec::new(),
        }
    }

    /// Resolve a gmsh node tag to the **rank** its node will have, recording
    /// its coordinates at the tag's **first** appearance. `xyz` is only called
    /// then, so a front-end pays for its coordinate lookup once per node
    /// rather than once per occurrence. The rank becomes a real `NodeId` in
    /// `finish`, once the whole cloud is created at once.
    fn node_of(&mut self, tag: u64, xyz: impl FnOnce() -> Result<[f64; 3]>) -> Result<NodeId> {
        if let Some(&rank) = self.node_map.get(&tag) {
            return Ok(NodeId(rank));
        }
        let p = xyz()?;
        self.positions.extend_from_slice(&p[..self.dim]);
        let rank = self.node_map.len() as u32;
        self.node_map.insert(tag, rank);
        Ok(NodeId(rank))
    }

    /// Add one cell to every group it belongs to. `ids` is in **gmsh** order;
    /// the realignment to pyrucast's (VTK) order happens here, once, so no
    /// front-end can forget it.
    fn push_cell(&mut self, et: ElementType, ids: &[NodeId], names: &[String]) -> Result<()> {
        self.scratch.clear();
        match gmsh_node_permutation(et) {
            Some(perm) if perm.len() == ids.len() => {
                self.scratch.extend(perm.iter().map(|&p| ids[p]));
            }
            _ => self.scratch.extend_from_slice(ids),
        }

        // Disjoint field borrows: `order` is pushed to from inside the closure
        // that `groups`' entry API runs, so the two cannot go through `self`.
        let Self {
            order,
            groups,
            scratch,
            ..
        } = self;
        for g in names {
            let entry = groups.entry(g.clone()).or_insert_with(|| {
                order.push(g.clone());
                (Vec::new(), HashMap::new())
            });
            if !entry.1.contains_key(&et) {
                entry.0.push(et);
                entry.1.insert(et, Vec::new());
            }
            entry.1.get_mut(&et).unwrap().extend_from_slice(scratch);
        }
        Ok(())
    }

    fn finish(self) -> Result<Vec<(String, Mesh)>> {
        let Self {
            coords,
            node_map,
            positions,
            order,
            mut groups,
            ..
        } = self;
        // The whole cloud in one locked pass; the ranks parked in the
        // connectivities become ids by a shift.
        let first = coords.write().add_nodes(&positions)?.start;
        let mut out = Vec::with_capacity(order.len());
        for name in order {
            let (types, mut by_type) = groups.remove(&name).unwrap();
            let mut mesh = Mesh::empty();
            for et in types {
                // Shifted in place, then handed over: the connectivity is
                // never copied.
                let mut conn = by_type.remove(&et).unwrap();
                for slot in &mut conn {
                    *slot = NodeId(first + slot.0);
                }
                mesh.add_sub(Handle::new(SubMesh::from_connectivity(
                    coords.clone(),
                    et,
                    conn,
                )?))?;
            }
            out.push((name, mesh));
        }
        // The groups own the nodes now; hand back the unit `add_nodes` gave.
        // A node no group referenced cannot exist — every one of them was
        // created for a cell.
        let owned: Vec<NodeId> = (first..first + node_map.len() as u32).map(NodeId).collect();
        coords.write().decref_all(&owned)?;
        Ok(out)
    }
}

/// Build the per-group meshes from the parsed data into the **caller's**
/// `coords`. The coordinate dimension is the one already carried by
/// `coords`: gmsh always stores three coordinates per node, of which the
/// first `coords.dim()` are kept (so a 2-D `Coords` flattens onto `xy`).
/// Groups come out in order of first appearance; within a group, submeshes
/// are ordered by the first cell of each element type.
fn build_groups(parsed: &Parsed, coords: Handle<Coords>) -> Result<Vec<(String, Mesh)>> {
    let mut builder = GroupBuilder::new(coords);
    let mut ids: Vec<NodeId> = Vec::new();
    for el in &parsed.elements {
        ids.clear();
        for &tag in &el.nodes {
            ids.push(builder.node_of(tag, || {
                parsed
                    .coords
                    .get(&tag)
                    .copied()
                    .ok_or_else(|| err(format!("gmsh: element references unknown node {tag}")))
            })?);
        }
        builder.push_cell(el.element_type, &ids, &el.groups)?;
    }
    builder.finish()
}

// ─── In-memory front-end ─────────────────────────────────────────────────────

/// gmsh node tag → row in the coordinate array.
///
/// gmsh numbers its nodes `1..=n` after meshing, so a dense table is both
/// smaller and faster than hashing. A model whose entities were deleted and
/// re-created can leave holes; past a factor four of waste it hashes instead.
enum TagIndex {
    /// `rows[tag - min]`, with `u32::MAX` for a tag nothing occupies.
    Dense {
        min: u64,
        rows: Vec<u32>,
    },
    Sparse(HashMap<u64, u32>),
}

impl TagIndex {
    fn new(tags: &[u64]) -> Self {
        let Some((&first, rest)) = tags.split_first() else {
            return Self::Sparse(HashMap::new());
        };
        let (min, max) = rest
            .iter()
            .fold((first, first), |(lo, hi), &t| (lo.min(t), hi.max(t)));
        // `NodeId` is a `u32`, so a mesh past 4 G nodes cannot exist anyway;
        // the row index shares that ceiling. `checked_add` because the span of
        // a garbage tag list can reach `u64::MAX`, and a panic is not the way
        // to greet bad input — it hashes instead.
        let span = (max - min).checked_add(1);
        if let Some(span) = span
            && span <= 4 * tags.len() as u64
        {
            let mut rows = vec![u32::MAX; span as usize];
            for (row, &tag) in tags.iter().enumerate() {
                rows[(tag - min) as usize] = row as u32;
            }
            Self::Dense { min, rows }
        } else {
            Self::Sparse(
                tags.iter()
                    .enumerate()
                    .map(|(row, &tag)| (tag, row as u32))
                    .collect(),
            )
        }
    }

    fn row(&self, tag: u64) -> Option<usize> {
        match self {
            Self::Dense { min, rows } => match rows.get(tag.checked_sub(*min)? as usize) {
                Some(&r) if r != u32::MAX => Some(r as usize),
                _ => None,
            },
            Self::Sparse(map) => map.get(&tag).map(|&r| r as usize),
        }
    }
}

/// One homogeneous block of elements, as a live gmsh session hands it over:
/// a gmsh element-type code, the node tags **flat** (`nodes_per_cell` of them
/// per element) and the physical groups the block belongs to.
///
/// It mirrors one `(elementType, nodeTags)` pair of
/// `gmsh.model.mesh.getElements()`, for one entity.
///
/// ```
/// # use pyrucast::ops::mesh::GmshBlock;
/// # use pyrucast::atoms::ElementType;
/// // Deux triangles partageant une arête, dans le groupe physique « plaque ».
/// let plaque = ["plaque".to_string()];
/// let bloc = GmshBlock {
///     element_type: 2, // le code gmsh de TRI3
///     node_tags: &[1, 2, 3, 2, 4, 3],
///     groups: &plaque,
/// };
/// // La connectivité est à plat : sa longueur dit le nombre de mailles.
/// let npc = ElementType::TRI3.nodes_per_cell();
/// assert_eq!(bloc.node_tags.len() / npc, 2);
/// ```
pub struct GmshBlock<'a> {
    /// gmsh's element-type code — `2` for a triangle, `4` for a tetrahedron, …
    /// See the table in the module documentation.
    pub element_type: u32,
    /// Flat connectivity: element `i` occupies `[i*npc, (i+1)*npc)`.
    pub node_tags: &'a [u64],
    /// Every physical-group name this block belongs to. An entity can carry
    /// several, and then its cells land in each of them; an empty list is not
    /// the same as no group — pass `<ungrouped>` for that, as the file
    /// front-end does.
    pub groups: &'a [String],
}

/// Convert a gmsh mesh **already in memory** — the node table and the element
/// blocks — into one [`Mesh`] per physical group, adding the nodes to the
/// **caller's** `coords`.
///
/// This is the in-memory twin of [`read_gmsh`], and obeys exactly its rules:
/// the dimension of `coords` decides how many of gmsh's three coordinates are
/// kept, every returned mesh shares that one `Coords`, only nodes actually
/// referenced by a cell are materialized, and groups come out in order of first
/// appearance. `node_coords` holds three values per entry of `node_tags`, in
/// the same order.
///
/// Errors if a block's length is not a whole number of cells, if a cell names
/// a node tag absent from `node_tags`, or if a gmsh element type has no
/// pyrucast counterpart.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::ops::mesh::{self, GmshBlock};
/// // Ce que gmsh tend : les tags des nœuds, leurs trois coordonnées chacun,
/// // et un bloc par type d'élément dont la connectivité est à plat.
/// let tags = [1_u64, 2, 3, 4];
/// let xyz = [
///     0.0, 0.0, 0.0, //
///     1.0, 0.0, 0.0, //
///     0.0, 1.0, 0.0, //
///     1.0, 1.0, 0.0,
/// ];
/// let plaque = ["plaque".to_string()];
/// let blocs = [GmshBlock {
///     element_type: 2, // TRI3
///     node_tags: &[1, 2, 3, 2, 4, 3],
///     groups: &plaque,
/// }];
///
/// // Le `Coords` est 2-D : la troisième coordonnée de gmsh tombe.
/// let coords = Handle::new(Coords::new(2)?);
/// let regions = mesh::from_gmsh_arrays(coords.clone(), &tags, &xyz, &blocs)?;
/// assert_eq!(regions.len(), 1);
/// assert_eq!(regions[0].0, "plaque");
/// assert_eq!(regions[0].1.cell_count(), 2);
/// assert_eq!(coords.read().node_count(), 4);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn from_gmsh_arrays(
    coords: Handle<Coords>,
    node_tags: &[u64],
    node_coords: &[f64],
    blocks: &[GmshBlock<'_>],
) -> Result<Vec<(String, Mesh)>> {
    if node_coords.len() != 3 * node_tags.len() {
        return Err(err(format!(
            "gmsh: {} node tags call for {} coordinates (three each), got {}",
            node_tags.len(),
            3 * node_tags.len(),
            node_coords.len()
        )));
    }
    let index = TagIndex::new(node_tags);
    let mut builder = GroupBuilder::new(coords);
    let mut ids: Vec<NodeId> = Vec::new();

    for block in blocks {
        let et = element_type_from_gmsh(block.element_type)?;
        let npc = et.nodes_per_cell();
        if !block.node_tags.len().is_multiple_of(npc) {
            return Err(err(format!(
                "gmsh: a {et} block holds {} node tags, not a whole number of cells of {npc}",
                block.node_tags.len()
            )));
        }
        for cell in block.node_tags.chunks(npc) {
            ids.clear();
            for &tag in cell {
                ids.push(builder.node_of(tag, || {
                    let row = index.row(tag).ok_or_else(|| {
                        err(format!("gmsh: element references unknown node {tag}"))
                    })?;
                    Ok([
                        node_coords[3 * row],
                        node_coords[3 * row + 1],
                        node_coords[3 * row + 2],
                    ])
                })?);
            }
            builder.push_cell(et, &ids, block.groups)?;
        }
    }
    builder.finish()
}

/// Read a gmsh `.msh` file (ASCII or binary, MSH 2.2 or 4.1) into one
/// [`Mesh`] per physical group, adding the nodes to the **caller's**
/// `coords`. See the module docs for the format and grouping rules.
///
/// The coordinate dimension is the one of `coords`: the first
/// `coords.dim()` of gmsh's three coordinates are kept. The nodes land in
/// `coords` (which may already hold geometry — the import is merged in), so
/// the caller keeps the handle it needs to pose boundary conditions etc.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::ops::mesh;
/// # let maillage_gmsh = "\
/// # $MeshFormat\n2.2 0 8\n$EndMeshFormat\n\
/// # $Nodes\n3\n1 0 0 0\n2 1 0 0\n3 0 1 0\n$EndNodes\n\
/// # $Elements\n1\n1 2 2 7 1 1 2 3\n$EndElements\n";
/// // Les nœuds atterrissent dans le `Coords` fourni — qui peut déjà porter
/// // de la géométrie, l'import s'y fondant — de sorte que l'appelant garde
/// // la poignée dont il a besoin pour poser ses conditions aux limites.
/// # let chemin = std::env::temp_dir()
/// #     .join(format!("pyrucast_gmsh_{}.msh", std::process::id()));
/// # std::fs::write(&chemin, maillage_gmsh)?;
/// let coords = Handle::new(Coords::new(2)?);
/// assert_eq!(coords.read().node_count(), 0);
/// let regions = mesh::read_gmsh(coords.clone(), &chemin)?;
/// assert_eq!(regions[0].1.cell_count(), 1);
/// assert_eq!(coords.read().node_count(), 3);
/// # let _ = std::fs::remove_file(&chemin);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn read_gmsh(coords: Handle<Coords>, path: &Path) -> Result<Vec<(String, Mesh)>> {
    let bytes = std::fs::read(path)?;
    read_gmsh_bytes(coords, &bytes)
}

/// Like [`read_gmsh`] but parsing **ASCII** file contents already held in a
/// string. For binary content use [`read_gmsh_bytes`].
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::ops::mesh;
/// # let maillage_gmsh = "\
/// # $MeshFormat\n2.2 0 8\n$EndMeshFormat\n\
/// # $Nodes\n3\n1 0 0 0\n2 1 0 0\n3 0 1 0\n$EndNodes\n\
/// # $Elements\n1\n1 2 2 7 1 1 2 3\n$EndElements\n";
/// // Les mailles reviennent **groupées** par nom de région physique.
/// let coords = Handle::new(Coords::new(2)?);
/// let regions = mesh::read_gmsh_str(coords.clone(), maillage_gmsh)?;
/// assert_eq!(regions.len(), 1);
/// assert_eq!(regions[0].1.cell_count(), 1);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn read_gmsh_str(coords: Handle<Coords>, text: &str) -> Result<Vec<(String, Mesh)>> {
    read_gmsh_bytes(coords, text.as_bytes())
}

/// Like [`read_gmsh`] but parsing the raw file bytes already held in memory
/// (ASCII or binary).
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::ops::mesh;
/// # let maillage_gmsh = "\
/// # $MeshFormat\n2.2 0 8\n$EndMeshFormat\n\
/// # $Nodes\n3\n1 0 0 0\n2 1 0 0\n3 0 1 0\n$EndNodes\n\
/// # $Elements\n1\n1 2 2 7 1 1 2 3\n$EndElements\n";
/// // La même chose depuis des octets bruts — ASCII **ou** binaire.
/// let coords = Handle::new(Coords::new(2)?);
/// let regions = mesh::gmsh::read_gmsh_bytes(coords.clone(), maillage_gmsh.as_bytes())?;
/// assert_eq!(regions[0].1.cell_count(), 1);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn read_gmsh_bytes(coords: Handle<Coords>, bytes: &[u8]) -> Result<Vec<(String, Mesh)>> {
    let parsed = parse_gmsh(bytes)?;
    build_groups(&parsed, coords)
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Fresh `Coords` of the given dimension, to read into.
    fn coords(dim: u8) -> Handle<Coords> {
        Handle::new(Coords::new(dim).unwrap())
    }

    // A unit square split into two TRI3, with a named surface "plate" and a
    // named bottom edge "bottom" (one SEG2). MSH 2.2 ASCII.
    const SQUARE_V2: &str = "\
$MeshFormat
2.2 0 8
$EndMeshFormat
$PhysicalNames
2
1 1 \"bottom\"
2 2 \"plate\"
$EndPhysicalNames
$Nodes
4
1 0 0 0
2 1 0 0
3 1 1 0
4 0 1 0
$EndNodes
$Elements
3
1 1 2 1 1 1 2
2 2 2 2 2 1 2 3
3 2 2 2 2 1 3 4
$EndElements
";

    #[test]
    fn v2_groups_and_types() {
        let groups = read_gmsh_str(coords(2), SQUARE_V2).unwrap();
        let names: Vec<&str> = groups.iter().map(|(n, _)| n.as_str()).collect();
        assert_eq!(names, vec!["bottom", "plate"]);

        let (_, bottom) = &groups[0];
        assert_eq!(bottom.element_types().unwrap(), vec![ElementType::SEG2]);
        assert_eq!(bottom.cell_count(), 1);

        let (_, plate) = &groups[1];
        assert_eq!(plate.element_types().unwrap(), vec![ElementType::TRI3]);
        assert_eq!(plate.cell_count(), 2);
    }

    #[test]
    fn reads_into_the_given_coords_shared_by_all_groups() {
        let c = coords(2);
        let groups = read_gmsh_str(c.clone(), SQUARE_V2).unwrap();
        // Nodes landed in the caller's Coords (the square's 4 corners).
        assert_eq!(c.read().node_count(), 4);
        // Every group hangs off that very same Coords slot.
        for (_, mesh) in &groups {
            let mc = mesh.coords().unwrap();
            assert!(mc.same_object(&c));
        }
    }

    // The same square in MSH 4.1: surface entity 1 → physical 2 ("plate"),
    // curve entity 1 → physical 1 ("bottom").
    const SQUARE_V4: &str = "\
$MeshFormat
4.1 0 8
$EndMeshFormat
$PhysicalNames
2
1 1 \"bottom\"
2 2 \"plate\"
$EndPhysicalNames
$Entities
0 1 1 0
1 0 0 0 1 0 0 1 1 0
1 0 0 0 1 1 0 1 2 0
$EndEntities
$Nodes
2 4 1 4
1 1 0 2
1
2
0 0 0
1 0 0
2 1 0 2
3
4
1 1 0
0 1 0
$EndNodes
$Elements
2 3 1 3
1 1 1 1
1 1 2
2 1 2 2
2 1 2 3
3 1 3 4
$EndElements
";

    #[test]
    fn v4_groups_and_types() {
        let groups = read_gmsh_str(coords(2), SQUARE_V4).unwrap();
        let names: Vec<&str> = groups.iter().map(|(n, _)| n.as_str()).collect();
        // "bottom" (curve) appears first in the elements block.
        assert_eq!(names, vec!["bottom", "plate"]);

        let (_, plate) = groups.iter().find(|(n, _)| n == "plate").unwrap();
        assert_eq!(plate.element_types().unwrap(), vec![ElementType::TRI3]);
        assert_eq!(plate.cell_count(), 2);

        let (_, bottom) = groups.iter().find(|(n, _)| n == "bottom").unwrap();
        assert_eq!(bottom.element_types().unwrap(), vec![ElementType::SEG2]);
    }

    #[test]
    fn coords_dimension_decides_kept_coordinates() {
        let mesh = "\
$MeshFormat
2.2 0 8
$EndMeshFormat
$Nodes
2
1 0 0 5
2 1 0 7
$EndNodes
$Elements
1
1 1 2 0 1 1 2
$EndElements
";
        // A 3-D Coords keeps z.
        let g3 = read_gmsh_str(coords(3), mesh).unwrap();
        assert_eq!(
            g3[0].1.node(0, 0, 0).unwrap().position().unwrap(),
            vec![0.0, 0.0, 5.0]
        );
        // A 2-D Coords keeps only x, y — z is dropped.
        let g2 = read_gmsh_str(coords(2), mesh).unwrap();
        assert_eq!(
            g2[0].1.node(0, 0, 0).unwrap().position().unwrap(),
            vec![0.0, 0.0]
        );
    }

    #[test]
    fn ungrouped_elements_bucket() {
        let mesh = "\
$MeshFormat
2.2 0 8
$EndMeshFormat
$Nodes
2
1 0 0 0
2 1 0 0
$EndNodes
$Elements
1
1 1 2 0 1 1 2
$EndElements
";
        let groups = read_gmsh_str(coords(2), mesh).unwrap();
        assert_eq!(groups.len(), 1);
        assert_eq!(groups[0].0, "<ungrouped>");
    }

    #[test]
    fn unsupported_element_type_errors() {
        let mesh = "\
$MeshFormat
2.2 0 8
$EndMeshFormat
$Nodes
3
1 0 0 0
2 1 0 0
3 2 0 0
$EndNodes
$Elements
1
1 21 2 0 1 1 2 3
$EndElements
";
        // gmsh type 21 = 10-node third-order triangle, which pyrucast has no
        // element for.
        let err = read_gmsh_str(coords(2), mesh).unwrap_err();
        assert!(
            format!("{err}").contains("unsupported element type 21"),
            "{err}"
        );
    }

    #[test]
    fn reads_a_pyramid() {
        // gmsh type 7 = 5-node pyramid: square base then apex, the same order
        // pyrucast uses.
        let mesh = "\
$MeshFormat
2.2 0 8
$EndMeshFormat
$Nodes
5
1 -1 -1 0
2 1 -1 0
3 1 1 0
4 -1 1 0
5 0 0 1
$EndNodes
$Elements
1
1 7 2 0 1 1 2 3 4 5
$EndElements
";
        let groups = read_gmsh_str(coords(3), mesh).unwrap();
        assert_eq!(groups.len(), 1);
        let m = &groups[0].1;
        assert_eq!(m.element_types().unwrap(), vec![ElementType::PYRA5]);
        assert_eq!(m.cell_count(), 1);
        assert_eq!(
            m.node(0, 0, 4).unwrap().position().unwrap(),
            vec![0.0, 0.0, 1.0]
        );
    }

    #[test]
    fn reads_quadratic_tet10_with_node_permutation() {
        // A single TET10 (gmsh type 11) on the unit tetrahedron, its 6
        // mid-edge nodes at the exact edge midpoints, listed in **gmsh**
        // node order (edges (0,1),(1,2),(2,0),(0,3),(2,3),(1,3)). After the
        // gmsh→pyrucast permutation, local node 8 must be the (1,3) midpoint
        // and node 9 the (2,3) midpoint.
        let mesh = "\
$MeshFormat
2.2 0 8
$EndMeshFormat
$Nodes
10
1 0 0 0
2 1 0 0
3 0 1 0
4 0 0 1
5 0.5 0 0
6 0.5 0.5 0
7 0 0.5 0
8 0 0 0.5
9 0 0.5 0.5
10 0.5 0 0.5
$EndNodes
$Elements
1
1 11 2 0 1 1 2 3 4 5 6 7 8 9 10
$EndElements
";
        let g = read_gmsh_str(coords(3), mesh).unwrap();
        let (_, m) = &g[0];
        assert_eq!(m.element_types().unwrap(), vec![ElementType::TET10]);
        assert_eq!(m.cell_count(), 1);
        // Local node 8 = edge (1,3) midpoint, node 9 = edge (2,3) midpoint.
        assert_eq!(
            m.node(0, 0, 8).unwrap().position().unwrap(),
            vec![0.5, 0.0, 0.5]
        );
        assert_eq!(
            m.node(0, 0, 9).unwrap().position().unwrap(),
            vec![0.0, 0.5, 0.5]
        );
    }

    #[test]
    fn reads_quadratic_hex27_with_node_permutation() {
        // A single HEX27 (gmsh type 12) on the unit cube, every mid-edge /
        // face / center node at its exact geometric position, listed in
        // **gmsh** node order. After the gmsh→pyrucast permutation the face
        // centers (local 20..25) and body center (26) must land correctly.
        let mesh = "\
$MeshFormat
2.2 0 8
$EndMeshFormat
$Nodes
27
1 0 0 0
2 1 0 0
3 1 1 0
4 0 1 0
5 0 0 1
6 1 0 1
7 1 1 1
8 0 1 1
9 0.5 0 0
10 0 0.5 0
11 0 0 0.5
12 1 0.5 0
13 1 0 0.5
14 0.5 1 0
15 1 1 0.5
16 0 1 0.5
17 0.5 0 1
18 0 0.5 1
19 1 0.5 1
20 0.5 1 1
21 0.5 0.5 0
22 0.5 0 0.5
23 0 0.5 0.5
24 1 0.5 0.5
25 0.5 1 0.5
26 0.5 0.5 1
27 0.5 0.5 0.5
$EndNodes
$Elements
1
1 12 2 0 1 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
$EndElements
";
        let g = read_gmsh_str(coords(3), mesh).unwrap();
        let (_, m) = &g[0];
        assert_eq!(m.element_types().unwrap(), vec![ElementType::HEX27]);
        // Faces x-, x+, y-, y+, z-, z+ at local 20..25; body center at 26.
        assert_eq!(
            m.node(0, 0, 20).unwrap().position().unwrap(),
            vec![0.0, 0.5, 0.5]
        );
        assert_eq!(
            m.node(0, 0, 21).unwrap().position().unwrap(),
            vec![1.0, 0.5, 0.5]
        );
        assert_eq!(
            m.node(0, 0, 22).unwrap().position().unwrap(),
            vec![0.5, 0.0, 0.5]
        );
        assert_eq!(
            m.node(0, 0, 23).unwrap().position().unwrap(),
            vec![0.5, 1.0, 0.5]
        );
        assert_eq!(
            m.node(0, 0, 24).unwrap().position().unwrap(),
            vec![0.5, 0.5, 0.0]
        );
        assert_eq!(
            m.node(0, 0, 25).unwrap().position().unwrap(),
            vec![0.5, 0.5, 1.0]
        );
        assert_eq!(
            m.node(0, 0, 26).unwrap().position().unwrap(),
            vec![0.5, 0.5, 0.5]
        );
    }

    // ─── Binary round-trips ──────────────────────────────────────────────────
    //
    // The same unit square as SQUARE_V2 / SQUARE_V4, hand-encoded in
    // little-endian binary, must read identically to its ASCII twin.

    // Endianness-parametrized scalar encoders, so the same fixtures exercise
    // both the little- and big-endian read paths.
    fn i32e(b: &mut Vec<u8>, x: i32, le: bool) {
        b.extend_from_slice(&if le { x.to_le_bytes() } else { x.to_be_bytes() });
    }
    fn u64e(b: &mut Vec<u8>, x: u64, le: bool) {
        b.extend_from_slice(&if le { x.to_le_bytes() } else { x.to_be_bytes() });
    }
    fn f64e(b: &mut Vec<u8>, x: f64, le: bool) {
        b.extend_from_slice(&if le { x.to_le_bytes() } else { x.to_be_bytes() });
    }

    fn square_v2_binary(le: bool) -> Vec<u8> {
        let mut b = Vec::new();
        b.extend_from_slice(b"$MeshFormat\n2.2 1 8\n");
        i32e(&mut b, 1, le); // endianness marker
        b.extend_from_slice(b"\n$EndMeshFormat\n");
        b.extend_from_slice(
            b"$PhysicalNames\n2\n1 1 \"bottom\"\n2 2 \"plate\"\n$EndPhysicalNames\n",
        );
        b.extend_from_slice(b"$Nodes\n4\n");
        for (tag, x, y) in [(1, 0.0, 0.0), (2, 1.0, 0.0), (3, 1.0, 1.0), (4, 0.0, 1.0)] {
            i32e(&mut b, tag, le);
            f64e(&mut b, x, le);
            f64e(&mut b, y, le);
            f64e(&mut b, 0.0, le);
        }
        b.extend_from_slice(b"\n$EndNodes\n");
        b.extend_from_slice(b"$Elements\n3\n");
        // SEG2 block (type 1, 1 element, 2 tags): elem 1, phys 1, geom 1, nodes 1-2.
        i32e(&mut b, 1, le);
        i32e(&mut b, 1, le);
        i32e(&mut b, 2, le);
        for v in [1, 1, 1, 1, 2] {
            i32e(&mut b, v, le);
        }
        // TRI3 block (type 2, 2 elements, 2 tags).
        i32e(&mut b, 2, le);
        i32e(&mut b, 2, le);
        i32e(&mut b, 2, le);
        for v in [2, 2, 2, 1, 2, 3] {
            i32e(&mut b, v, le);
        }
        for v in [3, 2, 2, 1, 3, 4] {
            i32e(&mut b, v, le);
        }
        b.extend_from_slice(b"\n$EndElements\n");
        b
    }

    fn square_v4_binary(le: bool) -> Vec<u8> {
        let mut b = Vec::new();
        b.extend_from_slice(b"$MeshFormat\n4.1 1 8\n");
        i32e(&mut b, 1, le);
        b.extend_from_slice(b"\n$EndMeshFormat\n");
        b.extend_from_slice(
            b"$PhysicalNames\n2\n1 1 \"bottom\"\n2 2 \"plate\"\n$EndPhysicalNames\n",
        );
        // Entities: 0 points, 1 curve, 1 surface, 0 volumes.
        b.extend_from_slice(b"$Entities\n");
        for n in [0u64, 1, 1, 0] {
            u64e(&mut b, n, le);
        }
        // curve tag 1: bbox(6 f64), 1 physical (1), 0 bounding.
        i32e(&mut b, 1, le);
        for _ in 0..6 {
            f64e(&mut b, 0.0, le);
        }
        u64e(&mut b, 1, le);
        i32e(&mut b, 1, le);
        u64e(&mut b, 0, le);
        // surface tag 1: bbox(6 f64), 1 physical (2), 0 bounding.
        i32e(&mut b, 1, le);
        for _ in 0..6 {
            f64e(&mut b, 0.0, le);
        }
        u64e(&mut b, 1, le);
        i32e(&mut b, 2, le);
        u64e(&mut b, 0, le);
        b.extend_from_slice(b"\n$EndEntities\n");
        // Nodes: 2 blocks, 4 nodes total.
        b.extend_from_slice(b"$Nodes\n");
        for n in [2u64, 4, 1, 4] {
            u64e(&mut b, n, le);
        }
        // block curve(dim1,tag1), parametric 0, 2 nodes (1,2).
        i32e(&mut b, 1, le);
        i32e(&mut b, 1, le);
        i32e(&mut b, 0, le);
        u64e(&mut b, 2, le);
        u64e(&mut b, 1, le);
        u64e(&mut b, 2, le);
        for (x, y) in [(0.0, 0.0), (1.0, 0.0)] {
            f64e(&mut b, x, le);
            f64e(&mut b, y, le);
            f64e(&mut b, 0.0, le);
        }
        // block surface(dim2,tag1), parametric 0, 2 nodes (3,4).
        i32e(&mut b, 2, le);
        i32e(&mut b, 1, le);
        i32e(&mut b, 0, le);
        u64e(&mut b, 2, le);
        u64e(&mut b, 3, le);
        u64e(&mut b, 4, le);
        for (x, y) in [(1.0, 1.0), (0.0, 1.0)] {
            f64e(&mut b, x, le);
            f64e(&mut b, y, le);
            f64e(&mut b, 0.0, le);
        }
        b.extend_from_slice(b"\n$EndNodes\n");
        // Elements: 2 blocks, 3 elements total.
        b.extend_from_slice(b"$Elements\n");
        for n in [2u64, 3, 1, 3] {
            u64e(&mut b, n, le);
        }
        // SEG2 block on curve(dim1,tag1): 1 element, tag 1, nodes 1-2.
        i32e(&mut b, 1, le);
        i32e(&mut b, 1, le);
        i32e(&mut b, 1, le);
        u64e(&mut b, 1, le);
        u64e(&mut b, 1, le);
        u64e(&mut b, 1, le);
        u64e(&mut b, 2, le);
        // TRI3 block on surface(dim2,tag1): 2 elements.
        i32e(&mut b, 2, le);
        i32e(&mut b, 1, le);
        i32e(&mut b, 2, le);
        u64e(&mut b, 2, le);
        u64e(&mut b, 2, le);
        for v in [1u64, 2, 3] {
            u64e(&mut b, v, le);
        }
        u64e(&mut b, 3, le);
        for v in [1u64, 3, 4] {
            u64e(&mut b, v, le);
        }
        b.extend_from_slice(b"\n$EndElements\n");
        b
    }

    /// Shared assertions: the binary square reads to the same groups/types
    /// as its ASCII twin, with coordinates decoded correctly.
    fn assert_square(groups: &[(String, Mesh)]) {
        let names: Vec<&str> = groups.iter().map(|(n, _)| n.as_str()).collect();
        assert_eq!(names, vec!["bottom", "plate"]);
        let (_, plate) = groups.iter().find(|(n, _)| n == "plate").unwrap();
        assert_eq!(plate.element_types().unwrap(), vec![ElementType::TRI3]);
        assert_eq!(plate.cell_count(), 2);
        let (_, bottom) = groups.iter().find(|(n, _)| n == "bottom").unwrap();
        assert_eq!(bottom.element_types().unwrap(), vec![ElementType::SEG2]);
        assert_eq!(bottom.cell_count(), 1);
        // node 3 sits at (1, 1) — checks the f64 payload decoded right.
        assert_eq!(
            plate.node(0, 0, 2).unwrap().position().unwrap(),
            vec![1.0, 1.0]
        );
    }

    #[test]
    fn binary_v2_matches_ascii() {
        assert_square(&read_gmsh_bytes(coords(2), &square_v2_binary(true)).unwrap());
    }

    #[test]
    fn binary_v4_matches_ascii() {
        assert_square(&read_gmsh_bytes(coords(2), &square_v4_binary(true)).unwrap());
    }

    #[test]
    fn binary_big_endian_round_trips() {
        // Same fixtures encoded big-endian; the marker drives detection.
        assert_square(&read_gmsh_bytes(coords(2), &square_v2_binary(false)).unwrap());
        assert_square(&read_gmsh_bytes(coords(2), &square_v4_binary(false)).unwrap());
    }

    // ─── In-memory front-end ─────────────────────────────────────────────

    /// One zone, flattened: its element type and its raw connectivity.
    type Zone = (ElementType, Vec<NodeId>);

    /// Everything a mesh *is*, flattened for comparison: the groups in order,
    /// and inside each the element type and raw connectivity of every zone.
    /// Two imports that agree on this agree cell for cell, node for node.
    fn shape(groups: &[(String, Mesh)]) -> Vec<(String, Vec<Zone>)> {
        groups
            .iter()
            .map(|(name, mesh)| {
                let zones = (0..mesh.len())
                    .map(|i| {
                        let handle = mesh.get(i).unwrap();
                        let sub = handle.read();
                        (sub.element_type(), sub.connectivity().to_vec())
                    })
                    .collect();
                (name.clone(), zones)
            })
            .collect()
    }

    /// `SQUARE_V2` again, but as the arrays a live gmsh session hands over:
    /// the node table, then one block per (entity, element type).
    fn square_blocks() -> (Vec<u64>, Vec<f64>, Vec<String>, Vec<String>) {
        let tags = vec![1_u64, 2, 3, 4];
        #[rustfmt::skip]
        let xyz = vec![
            0.0, 0.0, 0.0,
            1.0, 0.0, 0.0,
            1.0, 1.0, 0.0,
            0.0, 1.0, 0.0,
        ];
        (
            tags,
            xyz,
            vec!["bottom".to_string()],
            vec!["plate".to_string()],
        )
    }

    #[test]
    fn arrays_and_file_agree_cell_for_cell() {
        // The blocks are given in the file's element order, so both paths meet
        // the node tags in the same order and hand out the same `NodeId`s.
        let (tags, xyz, bottom, plate) = square_blocks();
        let blocks = [
            GmshBlock {
                element_type: 1, // SEG2
                node_tags: &[1, 2],
                groups: &bottom,
            },
            GmshBlock {
                element_type: 2, // TRI3
                node_tags: &[1, 2, 3, 1, 3, 4],
                groups: &plate,
            },
        ];

        let from_memory = from_gmsh_arrays(coords(2), &tags, &xyz, &blocks).unwrap();
        let from_file = read_gmsh_str(coords(2), SQUARE_V2).unwrap();
        assert_eq!(shape(&from_memory), shape(&from_file));
    }

    #[test]
    fn arrays_share_one_coords_and_keep_only_used_nodes() {
        let (mut tags, mut xyz, _, plate) = square_blocks();
        // A fifth node nothing references: it must not land in the `Coords`.
        tags.push(9);
        xyz.extend_from_slice(&[5.0, 5.0, 0.0]);

        let c = coords(2);
        let blocks = [GmshBlock {
            element_type: 2,
            node_tags: &[1, 2, 3],
            groups: &plate,
        }];
        let groups = from_gmsh_arrays(c.clone(), &tags, &xyz, &blocks).unwrap();
        assert_eq!(c.read().node_count(), 3);
        assert!(groups[0].1.coords().unwrap().same_object(&c));
    }

    #[test]
    fn arrays_apply_the_quadratic_volume_permutation() {
        // TET10: gmsh swaps the last two mid-edge nodes. Ten tags in gmsh
        // order must come out in pyrucast's, i.e. `[.., 9, 8]` swapped back.
        let tags: Vec<u64> = (1..=10).collect();
        let xyz: Vec<f64> = (0..10).flat_map(|i| [i as f64, 0.0, 0.0]).collect();
        let names = vec!["bloc".to_string()];
        let blocks = [GmshBlock {
            element_type: 11, // TET10
            node_tags: &tags,
            groups: &names,
        }];

        let groups = from_gmsh_arrays(coords(3), &tags, &xyz, &blocks).unwrap();
        let handle = groups[0].1.get(0).unwrap();
        let conn = handle.read().connectivity().to_vec();
        let perm = gmsh_node_permutation(ElementType::TET10).unwrap();
        let expected: Vec<NodeId> = perm.iter().map(|&p| NodeId(p as u32)).collect();
        assert_eq!(conn, expected);
    }

    #[test]
    fn arrays_place_a_block_in_each_of_its_groups() {
        // An entity carrying two physical groups: its cells belong to both,
        // exactly as MSH 4.1 allows.
        let (tags, xyz, _, _) = square_blocks();
        let names = vec!["plate".to_string(), "everything".to_string()];
        let blocks = [GmshBlock {
            element_type: 2,
            node_tags: &[1, 2, 3],
            groups: &names,
        }];
        let groups = from_gmsh_arrays(coords(2), &tags, &xyz, &blocks).unwrap();
        let got: Vec<&str> = groups.iter().map(|(n, _)| n.as_str()).collect();
        assert_eq!(got, vec!["plate", "everything"]);
        // One cell, counted in both — the node is shared, not duplicated.
        assert_eq!(groups[0].1.cell_count(), 1);
        assert_eq!(groups[1].1.cell_count(), 1);
    }

    #[test]
    fn arrays_read_sparse_tags() {
        // Tags far apart: `TagIndex` hashes instead of tabulating, and the
        // result must be the same.
        let tags = vec![1_u64, 5_000_000, 9_000_000];
        let xyz = vec![0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0];
        let names = vec!["plate".to_string()];
        let blocks = [GmshBlock {
            element_type: 2,
            node_tags: &[1, 5_000_000, 9_000_000],
            groups: &names,
        }];
        let groups = from_gmsh_arrays(coords(2), &tags, &xyz, &blocks).unwrap();
        assert_eq!(groups[0].1.cell_count(), 1);
    }

    #[test]
    fn arrays_unknown_node_errors() {
        let (tags, xyz, _, plate) = square_blocks();
        let blocks = [GmshBlock {
            element_type: 2,
            node_tags: &[1, 2, 77],
            groups: &plate,
        }];
        let e = from_gmsh_arrays(coords(2), &tags, &xyz, &blocks).unwrap_err();
        assert!(matches!(e, PyrucastError::Message(m) if m.contains("unknown node 77")));
    }

    #[test]
    fn arrays_ragged_block_errors() {
        let (tags, xyz, _, plate) = square_blocks();
        let blocks = [GmshBlock {
            element_type: 2, // TRI3 wants a multiple of three
            node_tags: &[1, 2, 3, 4],
            groups: &plate,
        }];
        let e = from_gmsh_arrays(coords(2), &tags, &xyz, &blocks).unwrap_err();
        assert!(matches!(e, PyrucastError::Message(m) if m.contains("whole number of cells")));
    }

    #[test]
    fn arrays_unsupported_element_type_errors() {
        let (tags, xyz, _, plate) = square_blocks();
        let blocks = [GmshBlock {
            element_type: 21, // TRI10, third order — pyrucast has no such type
            node_tags: &[],
            groups: &plate,
        }];
        let e = from_gmsh_arrays(coords(2), &tags, &xyz, &blocks).unwrap_err();
        assert!(
            matches!(e, PyrucastError::Message(m) if m.contains("unsupported element type 21"))
        );
    }

    #[test]
    fn arrays_coordinate_count_mismatch_errors() {
        let (tags, _, _, plate) = square_blocks();
        let blocks = [GmshBlock {
            element_type: 2,
            node_tags: &[1, 2, 3],
            groups: &plate,
        }];
        // Two coordinates per node instead of three.
        let flat = vec![0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0];
        let e = from_gmsh_arrays(coords(2), &tags, &flat, &blocks).unwrap_err();
        assert!(matches!(e, PyrucastError::Message(m) if m.contains("three each")));
    }

    #[test]
    fn binary_bad_endianness_marker_errors() {
        let mut bad = square_v2_binary(true);
        // Corrupt the endianness marker (the int right after "2.2 1 8\n").
        let at = bad.windows(8).position(|w| w == b"2.2 1 8\n").unwrap() + 8;
        bad[at] = 9; // neither 1 LE nor 1 BE
        let e = read_gmsh_bytes(coords(2), &bad).unwrap_err();
        assert!(matches!(e, PyrucastError::Message(m) if m.contains("endianness")));
    }
}