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
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
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
//! Per-physics parallel drivers — the bridge that lifts parallelism **above**
//! the constitutive/element kernels.
//!
//! A physics implementation never mentions rayon, the store, or a lock. It
//! supplies two kinds of **pure, sequential kernels**:
//!
//! - a **point kernel** at one Gauss point, driven either over an element-field
//!   input ([`element_pointwise`]) or a nodal-field input (`nodal_pointwise`).
//!   The constitutive law is one such kernel — read the deformation (+ `VAR0`)
//!   and material, write the flux/stress (+ `VAR1`) — but the same driver also
//!   powers point maps like the thermal strain;
//! - an **element kernel** ([`assemble_block`]): the local stiffness matrix of
//!   one cell from its geometry and material. It receives **one [`CellGeom`] per
//!   FE subspace** of the block — a single one for a plain volumetric physics, or
//!   several (sharing one mesh, differing by quadrature) for a multi-quadrature
//!   element such as a shear-deformable beam or a shell.
//!
//! These drivers own the fan-out (rayon), the zero-copy borrowing of store data
//! (read guards held across the parallel region, slices borrowed in place — no
//! per-cell snapshot copies), and the deterministic write-back / scatter. See
//! the book chapter *« Parallélisme »* and *« Ajouter une physique »*.
//!
//! # Integration measure
//!
//! Every kernel takes its quadrature weight from [`CellGeom::det_j_w`], which is
//! therefore the **single place** the geometric measure is decided. On an
//! [axisymmetric](crate::coords::Coords::axisymmetric) geometry it
//! returns `2πr |J| w` instead of `|J| w`, so stiffness, mass, conductivity,
//! distributed flux, volumes and internal forces all integrate over the full
//! ring with no per-physics change. What a physics *does* own is its operator:
//! only mechanics gains a term (the hoop strain `ε_θθ = u_r / r`), which is why
//! [`CellGeom::axisymmetric`] and [`CellGeom::radius`] are exposed.
//!
//! # Determinism
//!
//! [`element_pointwise`] writes each output slot exactly once
//! (`par_chunks_mut` over cells). [`assemble_block`] computes cell-local
//! matrices in parallel and scatters them into the COO **serially in cell
//! order**, so the assembled matrix is bit-for-bit identical to a sequential
//! run regardless of `RAYON_NUM_THREADS`.
//!
//! [`scatter_to_nodes`] (the shared nodal integrate-and-scatter driver behind the
//! internal forces, the weak divergence
//! [`crate::ops::node_field::divergence`](fn@crate::ops::node_field::divergence) and the
//! distributed flux load [`crate::ops::node_field::flux`](fn@crate::ops::node_field::flux))
//! instead builds each cell's local vector and scatters it in the **same parallel
//! pass**, by **cell colouring** (colours = node-disjoint cells): every node
//! accumulates in a fixed colour order, so the result is reproducible for any
//! `RAYON_NUM_THREADS` — though, summed in colour order rather than cell order, not
//! bit-for-bit with a sequential run (via [`crate::parallel::colored_scatter`]).

use crate::atoms::NodeId;
use crate::containers::element_field::SubElementField;
use crate::containers::field::SubField;
use crate::containers::finite_element_space::{
    build_dn_dx, build_jacobian, jacobian_measure, SubFiniteElementSpace,
};
use crate::containers::matrix::{DofOrdering, SubMatrix};
use crate::containers::mesh::SubMesh;
use crate::containers::node_field::{NodeFieldView, SubNodeField};
use crate::coords::Coords;
use crate::error::{PyrucastError, Result};
use crate::handle::Handle;
use crate::ops::coloring;
use crate::parallel::*;
use nalgebra_sparse::CooMatrix;
use std::collections::HashMap;

/// Reference-element data of an FE subspace, snapshotted **once** before the
/// parallel loop (it would otherwise re-read the store on every call — through
/// `element_type()` — and serialise all threads on the global store lock).
/// Shape values, reference derivatives and weights at each Gauss point, plus the
/// fixed dimensions. Shared read-only across threads.
struct RefData {
    n_nodes: usize,
    n_gauss: usize,
    space_dim: usize,
    ref_dim: usize,
    /// Whether the geometry is a body of revolution — the `2πr` factor of
    /// [`CellGeom::det_j_w`].
    axisymmetric: bool,
    /// Reference coordinates `ξ_g` of each Gauss point.
    gauss_xi: Vec<Vec<f64>>,
    /// Shape values `N_i(ξ_g)` per Gauss point.
    n_ref: Vec<Vec<f64>>,
    /// Reference derivatives `∂N_i/∂ξ_k(ξ_g)` per Gauss point.
    dn_ref: Vec<Vec<f64>>,
    /// Gauss weights.
    weights: Vec<f64>,
    /// Number of **field** shape functions — `n_nodes` except under a C¹
    /// interpolation, where each node carries a value *and* a slope.
    shape_count: usize,
    /// Field `N_i(ξ_g)` and `∂²N_i/∂ξ²(ξ_g)` per Gauss point. Empty unless the
    /// field basis differs from the geometric one, so a Lagrange space
    /// snapshots exactly what it did before.
    field_n_ref: Vec<Vec<f64>>,
    field_d2n_ref: Vec<Vec<f64>>,
    /// Whether the space declares **no** field basis (the formulation owns it).
    /// Distinct from "the field tables are empty because they equal the
    /// geometric ones" — hence a flag rather than a length test.
    model_embedded: bool,
}

impl RefData {
    fn snapshot(fe: &SubFiniteElementSpace) -> Result<Self> {
        let n_gauss = fe.gauss_count();
        let mut gauss_xi = Vec::with_capacity(n_gauss);
        let mut n_ref = Vec::with_capacity(n_gauss);
        let mut dn_ref = Vec::with_capacity(n_gauss);
        let mut weights = Vec::with_capacity(n_gauss);
        for g in 0..n_gauss {
            gauss_xi.push(fe.gauss_xi(g)?.to_vec());
            n_ref.push(fe.n_at_g(g)?.to_vec());
            dn_ref.push(fe.dn_at_g(g)?.to_vec());
            weights.push(fe.gauss_weight(g)?);
        }
        // A C¹ space carries a second basis; a Lagrange one leaves these empty
        // and the accessors fall back to the geometric tables.
        let (mut field_n_ref, mut field_d2n_ref) = (Vec::new(), Vec::new());
        if fe.interpolation().is_hermite() {
            for g in 0..n_gauss {
                field_n_ref.push(fe.field_n_at_g(g)?.to_vec());
                field_d2n_ref.push(fe.field_d2n_at_g(g)?.to_vec());
            }
        }
        Ok(Self {
            n_nodes: fe.nodes_per_cell()?,
            n_gauss,
            space_dim: fe.space_dim(),
            ref_dim: fe.ref_dim()?,
            axisymmetric: fe.is_axisymmetric(),
            gauss_xi,
            n_ref,
            dn_ref,
            weights,
            shape_count: fe.shape_count()?,
            field_n_ref,
            field_d2n_ref,
            model_embedded: fe.interpolation().is_model_embedded(),
        })
    }
}

/// Multiply the **slope** slots of a C¹ basis row by the Jacobian.
///
/// The basis alternates value, slope, value, slope — one pair per node — so the
/// odd indices are the ones whose reference degree of freedom is `∂w/∂ξ`.
fn scale_slope_slots(row: &[f64], j: f64) -> Vec<f64> {
    row.iter()
        .enumerate()
        .map(|(i, v)| if i % 2 == 1 { v * j } else { *v })
        .collect()
}

/// Geometry of one cell, computed **without touching the store**: coordinates
/// come from a held `Coords` guard, reference data is shared. Every accessor is
/// a pure local computation, so a kernel may call them while the driver runs the
/// cells in parallel — the kernel sees only this, never rayon or the store.
///
/// The cell's node coordinates are gathered **lazily** (only `dn_dx` / `det_j_w`
/// need them), so a point-local kernel that never asks for geometry (most
/// behaviour integrands) pays nothing. `CellGeom` is created and used within one
/// rayon task, so the interior-mutable cache is single-threaded.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::matrix::DofOrdering;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::kernel::{assemble_block, CellGeom};
/// # let coords = Handle::new(Coords::new(2).unwrap());
/// # let n: Vec<Node> = [[0.0, 0.0], [2.0, 0.0], [0.0, 2.0]]
/// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
/// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
/// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
/// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let zone = fes.get(0).unwrap();
/// # let support = zone.read().submesh().read().to_poi1().unwrap();
/// # // `CellGeom` n'existe qu'à l'intérieur d'un pilote : on en obtient un en
/// # // passant un noyau d'élément à `assemble_block`, exactement comme le fait
/// # // une physique.
/// # let noyau = |verifier: &(dyn Fn(&CellGeom) -> pyrucast::Result<()> + Sync)| {
/// #     assemble_block(
/// #         std::slice::from_ref(&zone), &support, &support,
/// #         vec!["q".into()], vec!["T".into()], DofOrdering::NodesThenVars, true,
/// #         None, None,
/// #         |geoms, _m, _s, _ke| verifier(&geoms[0]),
/// #     ).map(|_| ())
/// # };
/// // La géométrie d'une maille, sans jamais toucher au magasin : le noyau
/// // ne voit que ça — ni rayon, ni verrou.
/// noyau(&|geom| {
///     assert_eq!(geom.n_nodes, 3);
///     assert_eq!(geom.space_dim, 2);
///     assert!(!geom.axisymmetric);
///     Ok(())
/// })?;
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub struct CellGeom<'a> {
    rd: &'a RefData,
    coords: &'a Coords,
    conn: &'a [NodeId],
    cell_coords: std::cell::RefCell<Option<Vec<f64>>>,
    /// Index of this cell within the FE subspace.
    pub cell: usize,
    /// Nodes per cell.
    pub n_nodes: usize,
    /// Gauss points per cell.
    pub n_gauss: usize,
    /// Spatial dimension.
    pub space_dim: usize,
    /// Whether the geometry is the meridian plane of a body of revolution
    /// (`x = r`, `y = z`). [`det_j_w`](Self::det_j_w) already carries the `2πr`,
    /// so a kernel only reads this when its **operator** differs — mechanics,
    /// for the hoop strain `ε_θθ = u_r / r`.
    pub axisymmetric: bool,
}

impl<'a> CellGeom<'a> {
    fn new(rd: &'a RefData, coords: &'a Coords, conn: &'a [NodeId], cell: usize) -> Result<Self> {
        Ok(Self {
            rd,
            coords,
            conn,
            cell_coords: std::cell::RefCell::new(None),
            cell,
            n_nodes: rd.n_nodes,
            n_gauss: rd.n_gauss,
            space_dim: rd.space_dim,
            axisymmetric: rd.axisymmetric,
        })
    }

    /// Global node ids of this cell, in connectivity order.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::DofOrdering;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::models::kernel::{assemble_block, CellGeom};
    /// # let coords = Handle::new(Coords::new(2).unwrap());
    /// # let n: Vec<Node> = [[0.0, 0.0], [2.0, 0.0], [0.0, 2.0]]
    /// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
    /// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
    /// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
    /// # let zone = fes.get(0).unwrap();
    /// # let support = zone.read().submesh().read().to_poi1().unwrap();
    /// # // `CellGeom` n'existe qu'à l'intérieur d'un pilote : on en obtient un en
    /// # // passant un noyau d'élément à `assemble_block`, exactement comme le fait
    /// # // une physique.
    /// # let noyau = |verifier: &(dyn Fn(&CellGeom) -> pyrucast::Result<()> + Sync)| {
    /// #     assemble_block(
    /// #         std::slice::from_ref(&zone), &support, &support,
    /// #         vec!["q".into()], vec!["T".into()], DofOrdering::NodesThenVars, true,
    /// #         None, None,
    /// #         |geoms, _m, _s, _ke| verifier(&geoms[0]),
    /// #     ).map(|_| ())
    /// # };
    /// noyau(&|geom| {
    ///     assert_eq!(geom.node_ids().len(), 3); // dans l'ordre de connectivité
    ///     Ok(())
    /// })?;
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn node_ids(&self) -> &'a [NodeId] {
        &self.conn[self.cell * self.n_nodes..(self.cell + 1) * self.n_nodes]
    }

    /// Coordinates of local node `local` (0-based within the cell). Read straight
    /// from the held `Coords` (no gather).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::DofOrdering;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::models::kernel::{assemble_block, CellGeom};
    /// # let coords = Handle::new(Coords::new(2).unwrap());
    /// # let n: Vec<Node> = [[0.0, 0.0], [2.0, 0.0], [0.0, 2.0]]
    /// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
    /// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
    /// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
    /// # let zone = fes.get(0).unwrap();
    /// # let support = zone.read().submesh().read().to_poi1().unwrap();
    /// # // `CellGeom` n'existe qu'à l'intérieur d'un pilote : on en obtient un en
    /// # // passant un noyau d'élément à `assemble_block`, exactement comme le fait
    /// # // une physique.
    /// # let noyau = |verifier: &(dyn Fn(&CellGeom) -> pyrucast::Result<()> + Sync)| {
    /// #     assemble_block(
    /// #         std::slice::from_ref(&zone), &support, &support,
    /// #         vec!["q".into()], vec!["T".into()], DofOrdering::NodesThenVars, true,
    /// #         None, None,
    /// #         |geoms, _m, _s, _ke| verifier(&geoms[0]),
    /// #     ).map(|_| ())
    /// # };
    /// noyau(&|geom| {
    ///     // Les coordonnées sont rassemblées **paresseusement** : un noyau
    ///     // purement local ne les paie pas.
    ///     assert_eq!(geom.node_coord(1)?, &[2.0, 0.0]);
    ///     Ok(())
    /// })?;
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn node_coord(&self, local: usize) -> Result<&[f64]> {
        self.coords.position(self.node_ids()[local])
    }

    /// Fill the lazy `cell_coords` cache on first use (gather from the held
    /// `Coords`, no store access).
    fn ensure_cell_coords(&self) -> Result<()> {
        let mut cc = self.cell_coords.borrow_mut();
        if cc.is_none() {
            let mut v = Vec::with_capacity(self.n_nodes * self.space_dim);
            for &id in self.node_ids() {
                v.extend_from_slice(self.coords.position(id)?);
            }
            *cc = Some(v);
        }
        Ok(())
    }

    /// `∂N_i/∂x_a` at Gauss point `g`, flat layout `[i * space_dim + a]`.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::DofOrdering;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::models::kernel::{assemble_block, CellGeom};
    /// # let coords = Handle::new(Coords::new(2).unwrap());
    /// # let n: Vec<Node> = [[0.0, 0.0], [2.0, 0.0], [0.0, 2.0]]
    /// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
    /// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
    /// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
    /// # let zone = fes.get(0).unwrap();
    /// # let support = zone.read().submesh().read().to_poi1().unwrap();
    /// # // `CellGeom` n'existe qu'à l'intérieur d'un pilote : on en obtient un en
    /// # // passant un noyau d'élément à `assemble_block`, exactement comme le fait
    /// # // une physique.
    /// # let noyau = |verifier: &(dyn Fn(&CellGeom) -> pyrucast::Result<()> + Sync)| {
    /// #     assemble_block(
    /// #         std::slice::from_ref(&zone), &support, &support,
    /// #         vec!["q".into()], vec!["T".into()], DofOrdering::NodesThenVars, true,
    /// #         None, None,
    /// #         |geoms, _m, _s, _ke| verifier(&geoms[0]),
    /// #     ).map(|_| ())
    /// # };
    /// noyau(&|geom| {
    ///     // La matrice B. Les gradients somment au vecteur nul.
    ///     let b = geom.dn_dx(0)?;
    ///     assert!((b[0] + b[2] + b[4]).abs() < 1e-12);
    ///     Ok(())
    /// })?;
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn dn_dx(&self, g: usize) -> Result<Vec<f64>> {
        self.ensure_cell_coords()?;
        let cc = self.cell_coords.borrow();
        let cc = cc.as_ref().unwrap();
        let dn = &self.rd.dn_ref[g];
        let jac = build_jacobian(cc, dn, self.space_dim, self.rd.ref_dim, self.n_nodes);
        build_dn_dx(&jac, dn, self.space_dim, self.rd.ref_dim, self.n_nodes)
    }

    /// Shape-function values `N_i(ξ_g)` at Gauss point `g`.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::DofOrdering;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::models::kernel::{assemble_block, CellGeom};
    /// # let coords = Handle::new(Coords::new(2).unwrap());
    /// # let n: Vec<Node> = [[0.0, 0.0], [2.0, 0.0], [0.0, 2.0]]
    /// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
    /// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
    /// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
    /// # let zone = fes.get(0).unwrap();
    /// # let support = zone.read().submesh().read().to_poi1().unwrap();
    /// # // `CellGeom` n'existe qu'à l'intérieur d'un pilote : on en obtient un en
    /// # // passant un noyau d'élément à `assemble_block`, exactement comme le fait
    /// # // une physique.
    /// # let noyau = |verifier: &(dyn Fn(&CellGeom) -> pyrucast::Result<()> + Sync)| {
    /// #     assemble_block(
    /// #         std::slice::from_ref(&zone), &support, &support,
    /// #         vec!["q".into()], vec!["T".into()], DofOrdering::NodesThenVars, true,
    /// #         None, None,
    /// #         |geoms, _m, _s, _ke| verifier(&geoms[0]),
    /// #     ).map(|_| ())
    /// # };
    /// noyau(&|geom| {
    ///     assert!((geom.n_at_g(0)?.iter().sum::<f64>() - 1.0).abs() < 1e-12);
    ///     Ok(())
    /// })?;
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn n_at_g(&self, g: usize) -> Result<&[f64]> {
        Ok(&self.rd.n_ref[g])
    }

    /// Reference coordinates `ξ_g` of Gauss point `g`, of length `ref_dim`.
    ///
    /// What a formulation carrying **its own** basis needs: the quadrature is
    /// the subspace's, but the functions to evaluate at it are the element's own
    /// — a discrete-Kirchhoff shell interpolates its rotations quadratically over
    /// a mesh the space declares linear.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::DofOrdering;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::models::kernel::{assemble_block, CellGeom};
    /// # let coords = Handle::new(Coords::new(2).unwrap());
    /// # let n: Vec<Node> = [[0.0, 0.0], [2.0, 0.0], [0.0, 2.0]]
    /// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
    /// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
    /// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
    /// # let zone = fes.get(0).unwrap();
    /// # let support = zone.read().submesh().read().to_poi1().unwrap();
    /// # // `CellGeom` n'existe qu'à l'intérieur d'un pilote : on en obtient un en
    /// # // passant un noyau d'élément à `assemble_block`, exactement comme le fait
    /// # // une physique.
    /// # let noyau = |verifier: &(dyn Fn(&CellGeom) -> pyrucast::Result<()> + Sync)| {
    /// #     assemble_block(
    /// #         std::slice::from_ref(&zone), &support, &support,
    /// #         vec!["q".into()], vec!["T".into()], DofOrdering::NodesThenVars, true,
    /// #         None, None,
    /// #         |geoms, _m, _s, _ke| verifier(&geoms[0]),
    /// #     ).map(|_| ())
    /// # };
    /// noyau(&|geom| {
    ///     assert_eq!(geom.gauss_xi(0)?.len(), 2); // dans l'élément de référence
    ///     Ok(())
    /// })?;
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn gauss_xi(&self, g: usize) -> Result<&[f64]> {
        self.rd
            .gauss_xi
            .get(g)
            .map(|v| v.as_slice())
            .ok_or_else(|| {
                PyrucastError::Message(format!(
                    "CellGeom: Gauss point {g} out of range ({} points)",
                    self.n_gauss
                ))
            })
    }

    /// Number of **field** shape functions — `n_nodes` for a Lagrange space,
    /// twice that under a C¹ interpolation.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::DofOrdering;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::models::kernel::{assemble_block, CellGeom};
    /// # let coords = Handle::new(Coords::new(2).unwrap());
    /// # let n: Vec<Node> = [[0.0, 0.0], [2.0, 0.0], [0.0, 2.0]]
    /// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
    /// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
    /// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
    /// # let zone = fes.get(0).unwrap();
    /// # let support = zone.read().submesh().read().to_poi1().unwrap();
    /// # // `CellGeom` n'existe qu'à l'intérieur d'un pilote : on en obtient un en
    /// # // passant un noyau d'élément à `assemble_block`, exactement comme le fait
    /// # // une physique.
    /// # let noyau = |verifier: &(dyn Fn(&CellGeom) -> pyrucast::Result<()> + Sync)| {
    /// #     assemble_block(
    /// #         std::slice::from_ref(&zone), &support, &support,
    /// #         vec!["q".into()], vec!["T".into()], DofOrdering::NodesThenVars, true,
    /// #         None, None,
    /// #         |geoms, _m, _s, _ke| verifier(&geoms[0]),
    /// #     ).map(|_| ())
    /// # };
    /// noyau(&|geom| {
    ///     // Le nombre de formes du **champ** : il diffère de `n_nodes` dès que
    ///     // l'interpolation est d'Hermite.
    ///     assert_eq!(geom.shape_count(), 3);
    ///     Ok(())
    /// })?;
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn shape_count(&self) -> usize {
        self.rd.shape_count
    }

    /// Field shape values at Gauss point `g`, already scaled to act on
    /// **physical** degrees of freedom.
    ///
    /// Under a C¹ interpolation the odd slots are slope functions, whose
    /// reference degree of freedom is `∂w/∂ξ` while the physical one is
    /// `∂w/∂x`: they carry an extra factor `J`. For a Lagrange space this is
    /// [`n_at_g`](Self::n_at_g) unchanged.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::DofOrdering;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::models::kernel::{assemble_block, CellGeom};
    /// # let coords = Handle::new(Coords::new(2).unwrap());
    /// # let n: Vec<Node> = [[0.0, 0.0], [2.0, 0.0], [0.0, 2.0]]
    /// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
    /// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
    /// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
    /// # let zone = fes.get(0).unwrap();
    /// # let support = zone.read().submesh().read().to_poi1().unwrap();
    /// # // `CellGeom` n'existe qu'à l'intérieur d'un pilote : on en obtient un en
    /// # // passant un noyau d'élément à `assemble_block`, exactement comme le fait
    /// # // une physique.
    /// # let noyau = |verifier: &(dyn Fn(&CellGeom) -> pyrucast::Result<()> + Sync)| {
    /// #     assemble_block(
    /// #         std::slice::from_ref(&zone), &support, &support,
    /// #         vec!["q".into()], vec!["T".into()], DofOrdering::NodesThenVars, true,
    /// #         None, None,
    /// #         |geoms, _m, _s, _ke| verifier(&geoms[0]),
    /// #     ).map(|_| ())
    /// # };
    /// noyau(&|geom| {
    ///     assert_eq!(geom.field_n_at_g(0)?.len(), geom.shape_count());
    ///     Ok(())
    /// })?;
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn field_n_at_g(&self, g: usize) -> Result<Vec<f64>> {
        self.reject_if_model_embedded("shape values")?;
        if self.rd.field_n_ref.is_empty() {
            return Ok(self.rd.n_ref[g].clone());
        }
        let j = self.segment_jacobian()?;
        Ok(scale_slope_slots(&self.rd.field_n_ref[g], j))
    }

    /// Field second derivatives `∂²N_i/∂x²` at Gauss point `g`, acting on
    /// physical degrees of freedom — the **curvature operator** of a C¹ element.
    ///
    /// Defined for a straight 1-D reference element, which is where a C¹ basis
    /// exists today. The chain rule is then simply `∂²/∂x² = J⁻² ∂²/∂ξ²`: the
    /// `∂J/∂ξ` term that would appear on a curved element vanishes, because a
    /// `SEG2` has a constant Jacobian by construction.
    ///
    /// # Errors
    ///
    /// The space is not C¹, or its reference element is not a segment.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::DofOrdering;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::models::kernel::{self, assemble_block};
    /// # let coords = Handle::new(Coords::new(3).unwrap());
    /// # let n: Vec<Node> = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
    /// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
    /// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
    /// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
    /// # let zone = fes.get(0).unwrap();
    /// # let support = zone.read().submesh().read().to_poi1().unwrap();
    /// // Les dérivées secondes **physiques**, ce qu'exige la courbure d'une
    /// // poutre. Réservées aux espaces C¹ sur un segment : ailleurs, une
    /// // erreur nommée plutôt qu'un zéro.
    /// kernel::reduce_cells(&zone, |geom| {
    ///     assert!(geom.field_d2n_dx2(0).is_err()); // TRI3 Lagrange
    ///     Ok(0.0)
    /// })?;
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn field_d2n_dx2(&self, g: usize) -> Result<Vec<f64>> {
        self.reject_if_model_embedded("second derivatives")?;
        if self.rd.field_d2n_ref.is_empty() {
            return Err(PyrucastError::Message(
                "CellGeom: this subspace has no C¹ field basis, so no curvature operator".into(),
            ));
        }
        let j = self.segment_jacobian()?;
        // `J` scales the slope columns to physical DOFs, `J⁻²` maps the
        // reference second derivative to a physical one.
        let scaled = scale_slope_slots(&self.rd.field_d2n_ref[g], j);
        Ok(scaled.iter().map(|v| v / (j * j)).collect())
    }

    /// The guard the field accessors share — see
    /// [`SubFiniteElementSpace`](crate::containers::finite_element_space::SubFiniteElementSpace).
    fn reject_if_model_embedded(&self, what: &str) -> Result<()> {
        if self.rd.model_embedded {
            return Err(PyrucastError::Message(format!(
                "CellGeom: this subspace is MODEL_EMBEDDED — it declares no field basis, so it \
                 has no {what}. Its formulation owns the interpolation, so evaluating a field \
                 inside one of its elements is that formulation's business."
            )));
        }
        Ok(())
    }

    /// `J = ∂x/∂ξ` of a straight segment: **signed** in a 1-D space (where the
    /// slope degree of freedom is taken along the global axis, so a cell whose
    /// nodes run backwards must flip it), and the arc length `L/2` when the
    /// segment is embedded in a plane or in space — there the consumer has
    /// already rotated into a local axis running from node 0 to node 1.
    fn segment_jacobian(&self) -> Result<f64> {
        if self.rd.ref_dim != 1 {
            return Err(PyrucastError::Message(format!(
                "CellGeom: a C¹ basis needs a 1-D reference element, got ref_dim {}",
                self.rd.ref_dim
            )));
        }
        let (a, b) = (self.node_coord(0)?.to_vec(), self.node_coord(1)?.to_vec());
        if self.space_dim == 1 {
            return Ok((b[0] - a[0]) / 2.0);
        }
        let l2: f64 = (0..self.space_dim).map(|i| (b[i] - a[i]).powi(2)).sum();
        Ok(l2.sqrt() / 2.0)
    }

    /// Physical coordinates of Gauss point `g`, `x_a = Σ_i N_i(ξ_g) · x_{i,a}`
    /// (length `space_dim`). Uses the same lazy coordinate gather as
    /// [`dn_dx`](Self::dn_dx), so a kernel that never asks pays nothing.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::DofOrdering;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::models::kernel::{assemble_block, CellGeom};
    /// # let coords = Handle::new(Coords::new(2).unwrap());
    /// # let n: Vec<Node> = [[0.0, 0.0], [2.0, 0.0], [0.0, 2.0]]
    /// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
    /// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
    /// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
    /// # let zone = fes.get(0).unwrap();
    /// # let support = zone.read().submesh().read().to_poi1().unwrap();
    /// # // `CellGeom` n'existe qu'à l'intérieur d'un pilote : on en obtient un en
    /// # // passant un noyau d'élément à `assemble_block`, exactement comme le fait
    /// # // une physique.
    /// # let noyau = |verifier: &(dyn Fn(&CellGeom) -> pyrucast::Result<()> + Sync)| {
    /// #     assemble_block(
    /// #         std::slice::from_ref(&zone), &support, &support,
    /// #         vec!["q".into()], vec!["T".into()], DofOrdering::NodesThenVars, true,
    /// #         None, None,
    /// #         |geoms, _m, _s, _ke| verifier(&geoms[0]),
    /// #     ).map(|_| ())
    /// # };
    /// noyau(&|geom| {
    ///     // Le point de Gauss en coordonnées **physiques** : Σ N_i x_i.
    ///     // La règle du TRI3 place ses points aux **milieux des arêtes** :
    ///     // le premier est donc sur le bord, en (1, 0).
    ///     assert_eq!(geom.x_at_g(0)?, vec![1.0, 0.0]);
    ///     Ok(())
    /// })?;
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn x_at_g(&self, g: usize) -> Result<Vec<f64>> {
        self.ensure_cell_coords()?;
        let cc = self.cell_coords.borrow();
        let cc = cc.as_ref().unwrap();
        let n = &self.rd.n_ref[g];
        let d = self.space_dim;
        let mut x = vec![0.0_f64; d];
        for i in 0..self.n_nodes {
            for (a, xa) in x.iter_mut().enumerate() {
                *xa += n[i] * cc[i * d + a];
            }
        }
        Ok(x)
    }

    /// Radius `r` at Gauss point `g` — the first physical coordinate, on an
    /// **axisymmetric** geometry only (errors otherwise, since `x` is then just
    /// an abscissa and dividing by it would be meaningless).
    ///
    /// Gauss points are interior to the cell, so `r > 0` even for a cell touching
    /// the axis: the `N_i / r` of the hoop strain stays finite — the standard
    /// treatment of the axis in an axisymmetric formulation.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::DofOrdering;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::models::kernel::assemble_block;
    /// // Le rayon n'a de sens que sur un repère de **révolution**.
    /// let coords = Handle::new(Coords::axisymmetric()?);
    /// # let n: Vec<Node> = [[1.0, 0.0], [3.0, 0.0], [1.0, 2.0]]
    /// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
    /// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
    /// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()])?;
    /// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm))?;
    /// # let zone = fes.get(0)?;
    /// # let support = zone.read().submesh().read().to_poi1()?;
    /// assemble_block(
    ///     std::slice::from_ref(&zone), &support, &support,
    ///     vec!["q".into()], vec!["T".into()], DofOrdering::NodesThenVars, true,
    ///     None, None,
    ///     |geoms, _m, _s, _ke| {
    ///         let geom = &geoms[0];
    ///         assert!(geom.axisymmetric);
    ///         // `x = r` : le rayon est l'abscisse du point de Gauss.
    ///         assert_eq!(geom.radius(0)?, geom.x_at_g(0)?[0]);
    ///         Ok(())
    ///     },
    /// )?;
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn radius(&self, g: usize) -> Result<f64> {
        if !self.axisymmetric {
            return Err(PyrucastError::Message(
                "CellGeom::radius: the geometry is not axisymmetric (build the Coords \
                 with Coords::axisymmetric)"
                    .into(),
            ));
        }
        self.ensure_cell_coords()?;
        let cc = self.cell_coords.borrow();
        Ok(Self::radius_from(
            cc.as_ref().unwrap(),
            &self.rd.n_ref[g],
            self.n_nodes,
            self.space_dim,
        ))
    }

    /// `r = Σ_i N_i x_i` from already-borrowed cell coordinates — the scalar
    /// core of [`radius`](Self::radius), split out so the hot
    /// [`det_j_w`](Self::det_j_w) computes it under its **existing** borrow
    /// instead of allocating an `x_at_g` vector per Gauss point.
    fn radius_from(cell_coords: &[f64], n: &[f64], n_nodes: usize, space_dim: usize) -> f64 {
        let mut r = 0.0;
        for i in 0..n_nodes {
            r += n[i] * cell_coords[i * space_dim];
        }
        r
    }

    /// The cell's **tangent vectors** at Gauss point `g` — the columns of the
    /// Jacobian, `a_k = ∂x/∂ξ_k`, one per reference direction (`ref_dim` vectors
    /// of length `space_dim`).
    ///
    /// They are the raw material of surface kinematics: the deformed tangents
    /// are `ā_k = a_k + ∂u/∂ξ_k`, and everything about how a surface stretches
    /// and turns follows from them. A physics working on a **manifold** wants
    /// these rather than the tangential gradient `∇_s u`, because `∇_s u` has no
    /// component along the normal and so cannot be completed into a deformation
    /// gradient: `I + ∇_s u` goes singular under a quarter-turn, while the
    /// tangents never do.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::DofOrdering;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::models::kernel::{assemble_block, CellGeom};
    /// # let coords = Handle::new(Coords::new(2).unwrap());
    /// # let n: Vec<Node> = [[0.0, 0.0], [2.0, 0.0], [0.0, 2.0]]
    /// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
    /// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
    /// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
    /// # let zone = fes.get(0).unwrap();
    /// # let support = zone.read().submesh().read().to_poi1().unwrap();
    /// # // `CellGeom` n'existe qu'à l'intérieur d'un pilote : on en obtient un en
    /// # // passant un noyau d'élément à `assemble_block`, exactement comme le fait
    /// # // une physique.
    /// # let noyau = |verifier: &(dyn Fn(&CellGeom) -> pyrucast::Result<()> + Sync)| {
    /// #     assemble_block(
    /// #         std::slice::from_ref(&zone), &support, &support,
    /// #         vec!["q".into()], vec!["T".into()], DofOrdering::NodesThenVars, true,
    /// #         None, None,
    /// #         |geoms, _m, _s, _ke| verifier(&geoms[0]),
    /// #     ).map(|_| ())
    /// # };
    /// noyau(&|geom| {
    ///     // Une tangente par direction de l'élément de référence : deux pour
    ///     // une surface, une pour une ligne.
    ///     assert_eq!(geom.tangents(0)?.len(), 2);
    ///     Ok(())
    /// })?;
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn tangents(&self, g: usize) -> Result<Vec<Vec<f64>>> {
        self.ensure_cell_coords()?;
        let cc = self.cell_coords.borrow();
        let cc = cc.as_ref().unwrap();
        let (d, r) = (self.space_dim, self.rd.ref_dim);
        let jac = build_jacobian(cc, &self.rd.dn_ref[g], d, r, self.n_nodes);
        // `build_jacobian` lays the tangents out as `jac[a * r + k]`: column `k`
        // is the tangent along reference direction `k`.
        Ok((0..r)
            .map(|k| (0..d).map(|a| jac[a * r + k]).collect())
            .collect())
    }

    /// The (unnormalised) normal of a boundary cell from its tangents — the
    /// cross product in 3-D, the tangent turned by −90° in 2-D. Its **norm** is
    /// the surface measure per unit reference parameter, which is what makes it
    /// usable for an area ratio as well as a direction.
    ///
    /// Shared by [`normal`](Self::normal) and by the surface kinematics of a
    /// follower load, which needs the same construction on the *deformed*
    /// tangents.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::DofOrdering;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::models::kernel::{assemble_block, CellGeom};
    /// # let coords = Handle::new(Coords::new(2).unwrap());
    /// # let n: Vec<Node> = [[0.0, 0.0], [2.0, 0.0], [0.0, 2.0]]
    /// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
    /// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
    /// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
    /// # let zone = fes.get(0).unwrap();
    /// # let support = zone.read().submesh().read().to_poi1().unwrap();
    /// # // `CellGeom` n'existe qu'à l'intérieur d'un pilote : on en obtient un en
    /// # // passant un noyau d'élément à `assemble_block`, exactement comme le fait
    /// # // une physique.
    /// # let noyau = |verifier: &(dyn Fn(&CellGeom) -> pyrucast::Result<()> + Sync)| {
    /// #     assemble_block(
    /// #         std::slice::from_ref(&zone), &support, &support,
    /// #         vec!["q".into()], vec!["T".into()], DofOrdering::NodesThenVars, true,
    /// #         None, None,
    /// #         |geoms, _m, _s, _ke| verifier(&geoms[0]),
    /// #     ).map(|_| ())
    /// # };
    /// // Une fonction libre : la normale déduite des tangentes — le produit
    /// // vectoriel en 3-D, la rotation d'un quart de tour en 2-D. Elle porte
    /// // la **norme** des tangentes, elle n'est pas normalisée ;
    /// // [`normal`](Self::normal) s'en charge.
    /// assert_eq!(CellGeom::normal_from_tangents(&[vec![2.0, 0.0]])?, vec![0.0, -2.0]);
    /// // Une ou deux tangentes, pas davantage.
    /// assert!(CellGeom::normal_from_tangents(&[]).is_err());
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn normal_from_tangents(tangents: &[Vec<f64>]) -> Result<Vec<f64>> {
        match tangents {
            [t] => Ok(vec![t[1], -t[0]]),
            [a1, a2] => Ok(vec![
                a1[1] * a2[2] - a1[2] * a2[1],
                a1[2] * a2[0] - a1[0] * a2[2],
                a1[0] * a2[1] - a1[1] * a2[0],
            ]),
            _ => Err(PyrucastError::Message(format!(
                "normal_from_tangents: a normal needs 1 tangent (2-D) or 2 (3-D), got {}",
                tangents.len()
            ))),
        }
    }

    /// Unit **normal** of a boundary cell at Gauss point `g`, in the reference
    /// configuration.
    ///
    /// Defined only on a **manifold** (`ref_dim == space_dim − 1`): a boundary
    /// edge in 2-D, a boundary face in 3-D. A cell that fills its space has no
    /// normal, and asking for one is a modelling error, so it errors rather than
    /// returning a convention.
    ///
    /// The direction follows the cell's own **winding** — it is the mesh, not
    /// this accessor, that decides which side is « outside ». That is why the
    /// physics needing a normal (a pressure, a signed flux) are the ones that
    /// must care about their boundary mesh's orientation, while those that do
    /// not ([`boundary_transfer`](crate::models::boundary_transfer),
    /// [`radiation`](crate::models::radiation)) never call this: their direction
    /// is already consumed in writing `q·n`, and
    /// [`det_j_w`](Self::det_j_w) returns an orientation-invariant magnitude.
    ///
    /// - 2-D: the edge tangent `t` rotated by −90°, `n = (t_y, −t_x)/|t|`.
    /// - 3-D: `n = (a₁ × a₂)/|a₁ × a₂|`, the two Jacobian columns.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::DofOrdering;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::models::kernel::{self, assemble_block};
    /// # let coords = Handle::new(Coords::new(3).unwrap());
    /// # let n: Vec<Node> = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
    /// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
    /// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
    /// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
    /// # let zone = fes.get(0).unwrap();
    /// # let support = zone.read().submesh().read().to_poi1().unwrap();
    /// // La normale **unitaire** — celle que `normal_from_tangents` rend
    /// // brute, divisée par sa norme. Elle n'a de sens que sur une facette :
    /// // une dimension de référence de moins que l'espace.
    /// kernel::reduce_cells(&zone, |geom| {
    ///     let nu = geom.normal(0)?;
    ///     assert!((nu.iter().map(|x| x * x).sum::<f64>() - 1.0).abs() < 1e-12);
    ///     assert!((nu[2].abs() - 1.0).abs() < 1e-12); // le triangle est dans z = 0
    ///     Ok(0.0)
    /// })?;
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn normal(&self, g: usize) -> Result<Vec<f64>> {
        let (d, r) = (self.space_dim, self.rd.ref_dim);
        if r + 1 != d {
            return Err(PyrucastError::Message(format!(
                "CellGeom::normal: a {r}-D cell in a {d}-D space has no normal — a normal is \
                 defined on a boundary (a {}-D cell here)",
                d - 1
            )));
        }
        let mut n = Self::normal_from_tangents(&self.tangents(g)?)?;
        let norm = n.iter().map(|v| v * v).sum::<f64>().sqrt();
        if norm <= f64::EPSILON {
            return Err(PyrucastError::Message(format!(
                "CellGeom::normal: cell {} is degenerate at Gauss point {g} (null normal)",
                self.cell
            )));
        }
        for v in &mut n {
            *v /= norm;
        }
        Ok(n)
    }

    /// `|J|_g · w_g` — the integration weight of Gauss point `g`.
    ///
    /// On an **axisymmetric** geometry this is `2πr_g · |J|_g · w_g`: the
    /// circumferential measure is applied here, once, so *every* integral built
    /// on `CellGeom` — stiffness, mass, conductivity, distributed flux, volumes,
    /// internal forces, on the body and on its boundary alike — integrates over
    /// the full ring without its kernel knowing.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::DofOrdering;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::models::kernel::{assemble_block, CellGeom};
    /// # let coords = Handle::new(Coords::new(2).unwrap());
    /// # let n: Vec<Node> = [[0.0, 0.0], [2.0, 0.0], [0.0, 2.0]]
    /// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
    /// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
    /// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
    /// # let zone = fes.get(0).unwrap();
    /// # let support = zone.read().submesh().read().to_poi1().unwrap();
    /// # // `CellGeom` n'existe qu'à l'intérieur d'un pilote : on en obtient un en
    /// # // passant un noyau d'élément à `assemble_block`, exactement comme le fait
    /// # // une physique.
    /// # let noyau = |verifier: &(dyn Fn(&CellGeom) -> pyrucast::Result<()> + Sync)| {
    /// #     assemble_block(
    /// #         std::slice::from_ref(&zone), &support, &support,
    /// #         vec!["q".into()], vec!["T".into()], DofOrdering::NodesThenVars, true,
    /// #         None, None,
    /// #         |geoms, _m, _s, _ke| verifier(&geoms[0]),
    /// #     ).map(|_| ())
    /// # };
    /// noyau(&|geom| {
    ///     // **L'unique endroit** où se décide la mesure d'intégration : ici
    ///     // |J|·w, et 2πr·|J|·w en révolution. C'est ce qui permet à toute
    ///     // physique d'intégrer sur l'anneau complet sans rien changer.
    ///     let aire: f64 = (0..geom.n_gauss).map(|g| geom.det_j_w(g).unwrap()).sum();
    ///     assert!((aire - 2.0).abs() < 1e-12); // l'aire du triangle
    ///     Ok(())
    /// })?;
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn det_j_w(&self, g: usize) -> Result<f64> {
        self.ensure_cell_coords()?;
        let cc = self.cell_coords.borrow();
        let cc = cc.as_ref().unwrap();
        let dn = &self.rd.dn_ref[g];
        let jac = build_jacobian(cc, dn, self.space_dim, self.rd.ref_dim, self.n_nodes);
        let w = jacobian_measure(&jac, self.space_dim, self.rd.ref_dim) * self.rd.weights[g];
        // One predictable branch on a per-subspace constant; the Cartesian path
        // returns here having paid nothing else. The revolved path reuses the
        // borrow above, so it allocates nothing either.
        if !self.axisymmetric {
            return Ok(w);
        }
        let r = Self::radius_from(cc, &self.rd.n_ref[g], self.n_nodes, self.space_dim);
        Ok(w * std::f64::consts::TAU * r)
    }
}

/// Integrate a point-local constitutive law over `fespace`, in parallel.
///
/// `point(geom, input, material, g, out)` is a pure sequential kernel: for the
/// cell `geom.cell` at Gauss point `g`, it reads the deformation (+ `VAR0`) from
/// `input` and the material from `material` (both borrowed in place), and writes
/// the `out_components.len()` output values into `out`. `material` is `Some` iff
/// the physics declared a material FE subspace.
///
/// The **element-field-input** driver, mirrored by `nodal_pointwise` (which
/// reads a nodal field instead). Backs the constitutive integration
/// [`crate::ops::element_field::behavior::integrate`] and the thermal strain
/// [`crate::ops::element_field::thermal_strain`](fn@crate::ops::element_field::thermal_strain).
///
/// Returns the material-state field (flux/stress + `VAR1`) on `fespace`.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::element_field::{ElementField, SubElementField};
/// # use pyrucast::containers::field::SubField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::matrix::DofOrdering;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::kernel;
/// # let coords = Handle::new(Coords::new(2).unwrap());
/// # let n: Vec<Node> = [[0.0, 0.0], [2.0, 0.0], [0.0, 2.0]]
/// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
/// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
/// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
/// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let zone = fes.get(0).unwrap();
/// # let support = zone.read().submesh().read().to_poi1().unwrap();
/// # let mut entree = SubElementField::new(zone.clone(), vec!["eps".into()])?;
/// # entree.set_uniform("eps", 2.0)?;
/// # let entree = Handle::new(entree);
/// // Un noyau **au point de Gauss** : la loi de comportement en est un —
/// // lire la déformation et le matériau, écrire la contrainte.
/// let sortie = kernel::element_pointwise(
///     &zone, &entree, None, vec!["sig".into()],
///     |_geom, champ, _mat, g, slot| {
///         slot[0] = 3.0 * champ.get(0, g, 0)?;
///         Ok(())
///     },
/// )?;
/// assert_eq!(sortie.value(0, 0, "sig")?, 6.0);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn element_pointwise(
    fespace: &Handle<SubFiniteElementSpace>,
    input: &Handle<SubElementField>,
    material: Option<&Handle<SubElementField>>,
    out_components: Vec<String>,
    point: impl Fn(&CellGeom, &SubElementField, Option<&SubElementField>, usize, &mut [f64]) -> Result<()>
        + Sync,
) -> Result<SubElementField> {
    let out_stride = out_components.len();
    let mut out = SubElementField::new(fespace.clone(), out_components)?;

    // Guards held for the whole parallel region — slices borrowed, not copied.
    // Reference data snapshotted once (no per-cell store reads inside the loop).
    let fe = fespace.read();
    let submesh = fe.submesh();
    let sm = submesh.read();
    let coords_h = sm.coords();
    let coords = coords_h.read();
    let fin = input.read();
    let mat_guard = material.map(|h| h.read());

    let rd = RefData::snapshot(&fe)?;
    let n_gauss = rd.n_gauss;
    let conn: &[NodeId] = sm.connectivity();
    let rd_ref: &RefData = &rd;
    let coords_ref: &Coords = &coords;
    let in_ref: &SubElementField = &fin;
    let mat_ref: Option<&SubElementField> = mat_guard.as_deref();

    out.values_mut()
        .par_chunks_mut(n_gauss * out_stride)
        .with_min_len((MIN_PARALLEL_LEN / n_gauss.max(1)).max(1))
        .enumerate()
        .try_for_each(|(cell, ochunk)| -> Result<()> {
            let geom = CellGeom::new(rd_ref, coords_ref, conn, cell)?;
            for g in 0..n_gauss {
                let slot = &mut ochunk[g * out_stride..(g + 1) * out_stride];
                point(&geom, in_ref, mat_ref, g, slot)?;
            }
            Ok(())
        })?;
    Ok(out)
}

/// Produce a per-element (Gauss-point) field from a **nodal** field, in parallel.
///
/// The nodal counterpart of [`element_pointwise`]: where that reads an
/// element-field `input`, this reads a nodal-field view `field`. `point(geom,
/// field, g, out)` is a pure sequential kernel: for the cell `geom.cell` at Gauss
/// point `g`, it reads nodal values (`field.value(id, comp)` for
/// `id in geom.node_ids()`) and writes the `out_components.len()` output values
/// into `out`. It uses `geom.n_at_g(g)` to interpolate values and/or
/// `geom.dn_dx(g)` to differentiate them.
///
/// Same guarantees as [`element_pointwise`]: reference data snapshotted once,
/// guards held for the whole region (slices borrowed, not copied), each output
/// slot written exactly once (`par_chunks_mut` over cells) ⇒ **bit-for-bit
/// deterministic**. Backs the geometric field producers
/// [`crate::ops::element_field::gradient`](fn@crate::ops::element_field::gradient),
/// [`crate::ops::element_field::deformation`](fn@crate::ops::element_field::deformation) and
/// [`crate::ops::element_field::interp_to_gauss`](fn@crate::ops::element_field::interp_to_gauss).
pub(crate) fn nodal_pointwise(
    fespace: &Handle<SubFiniteElementSpace>,
    field: &NodeFieldView,
    out_components: Vec<String>,
    point: impl Fn(&CellGeom, &NodeFieldView, usize, &mut [f64]) -> Result<()> + Sync,
) -> Result<SubElementField> {
    let out_stride = out_components.len();
    let mut out = SubElementField::new(fespace.clone(), out_components)?;

    // Guards held for the whole parallel region — slices borrowed, not copied.
    let fe = fespace.read();
    let submesh = fe.submesh();
    let sm = submesh.read();
    let coords_h = sm.coords();
    let coords = coords_h.read();

    let rd = RefData::snapshot(&fe)?;
    let n_gauss = rd.n_gauss;
    let conn: &[NodeId] = sm.connectivity();
    let rd_ref: &RefData = &rd;
    let coords_ref: &Coords = &coords;

    out.values_mut()
        .par_chunks_mut(n_gauss * out_stride)
        .with_min_len((MIN_PARALLEL_LEN / n_gauss.max(1)).max(1))
        .enumerate()
        .try_for_each(|(cell, ochunk)| -> Result<()> {
            let geom = CellGeom::new(rd_ref, coords_ref, conn, cell)?;
            for g in 0..n_gauss {
                let slot = &mut ochunk[g * out_stride..(g + 1) * out_stride];
                point(&geom, field, g, slot)?;
            }
            Ok(())
        })?;
    Ok(out)
}

/// Assemble one stiffness block by a per-cell element-matrix kernel, in
/// parallel.
///
/// `element(geoms, material, ke)` is a pure sequential kernel: it fills `ke` —
/// the cell's local dense matrix, row-major, **node-major / variable-minor**:
///   row `r = li * n_dual + di`, col `c = lj * n_primal + pj`
/// (with `li/lj` local node indices, `di/pj` indices into `dual_vars` /
/// `primal_vars`). `geoms` holds one [`CellGeom`] per FE subspace of `fespaces`
/// (same order); a single-space physics reads `geoms[0]`, a multi-quadrature one
/// reads each. The driver scatters `ke` into the block's COO serially in cell
/// order. `material` is `Some` iff the physics supplied one.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::element_field::{ElementField, SubElementField};
/// # use pyrucast::containers::field::SubField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::matrix::DofOrdering;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::kernel;
/// # let coords = Handle::new(Coords::new(2).unwrap());
/// # let n: Vec<Node> = [[0.0, 0.0], [2.0, 0.0], [0.0, 2.0]]
/// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
/// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
/// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
/// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let zone = fes.get(0).unwrap();
/// # let support = zone.read().submesh().read().to_poi1().unwrap();
/// // Le pilote appelle un noyau **pur et séquentiel** par maille ; la
/// // parallélisation, l'emprunt zéro-copie et le rangement en COO sont à
/// // lui. Ici, une matrice identité locale.
/// let bloc = kernel::assemble_block(
///     std::slice::from_ref(&zone), &support, &support,
///     vec!["q".into()], vec!["T".into()], DofOrdering::NodesThenVars, true,
///     None, None,
///     |geoms, _m, _s, ke| {
///         let npc = geoms[0].n_nodes;
///         for i in 0..npc {
///             ke[i * npc + i] = 1.0;
///         }
///         Ok(())
///     },
/// )?;
/// assert_eq!((bloc.n_rows(), bloc.n_cols()), (3, 3));
/// assert_eq!(bloc.get(n[0].id(), "q", n[0].id(), "T"), 1.0);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
#[allow(clippy::too_many_arguments)]
pub fn assemble_block(
    fespaces: &[Handle<SubFiniteElementSpace>],
    row_support: &Handle<SubMesh>,
    col_support: &Handle<SubMesh>,
    dual_vars: Vec<String>,
    primal_vars: Vec<String>,
    ordering: DofOrdering,
    symmetric: bool,
    material: Option<&Handle<SubElementField>>,
    state: Option<&Handle<SubElementField>>,
    element: impl Fn(
            &[CellGeom],
            Option<&SubElementField>,
            Option<&SubElementField>,
            &mut [f64],
        ) -> Result<()>
        + Sync,
) -> Result<SubMatrix> {
    let (nrows, ncols, trips) = element_block_triplets(
        fespaces,
        row_support,
        col_support,
        dual_vars.len(),
        primal_vars.len(),
        ordering,
        material,
        state,
        element,
    )?;
    let mut rows = Vec::with_capacity(trips.len());
    let mut cols = Vec::with_capacity(trips.len());
    let mut vals = Vec::with_capacity(trips.len());
    for (r, c, v) in trips {
        rows.push(r);
        cols.push(c);
        vals.push(v);
    }
    let coo = CooMatrix::try_from_triplets(nrows, ncols, rows, cols, vals)
        .map_err(|e| PyrucastError::Message(format!("assemble_block: invalid COO: {e}")))?;
    SubMatrix::from_coo(
        row_support.clone(),
        col_support.clone(),
        dual_vars,
        primal_vars,
        ordering,
        symmetric,
        coo,
    )
}

/// `(nrows, ncols, local (row, col, value) triplets)` — the shape returned by
/// [`element_block_triplets`].
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::element_field::{ElementField, SubElementField};
/// # use pyrucast::containers::field::SubField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::matrix::DofOrdering;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::kernel;
/// # let coords = Handle::new(Coords::new(2).unwrap());
/// # let n: Vec<Node> = [[0.0, 0.0], [2.0, 0.0], [0.0, 2.0]]
/// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
/// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
/// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
/// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let zone = fes.get(0).unwrap();
/// # let support = zone.read().submesh().read().to_poi1().unwrap();
/// // Ce que rend `element_block_triplets` : la taille du bloc et ses
/// // triplets, en numérotation **locale** au bloc.
/// let (nr, nc, trips): kernel::BlockTriplets = kernel::element_block_triplets(
///     std::slice::from_ref(&zone), &support, &support, 1, 1,
///     DofOrdering::NodesThenVars, None, None,
///     |geoms, _m, _s, ke| { ke[0] = geoms[0].det_j_w(0)?; Ok(()) },
/// )?;
/// assert_eq!((nr, nc), (3, 3));
/// assert!(!trips.is_empty());
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub type BlockTriplets = (usize, usize, Vec<(usize, usize, f64)>);

/// Compute one stiffness block's entries as **local** `(row, col, value)`
/// triplets (the block's own numbering, `(node_local, var)` via `ordering`),
/// driving the per-cell `element` kernel in parallel. This is the shared core of
/// both [`assemble_block`] (which wraps these in a [`SubMatrix`]) and the global
/// computed-block assembler (which remaps them to global indices).
///
/// Cells are computed in parallel and their triplets concatenated **in cell
/// order** (within a cell: `li, di, lj, pj`), so the stream is identical to a
/// sequential run — the assembled values are reproducible. Returns
/// `(nrows, ncols, triplets)`.
#[allow(clippy::too_many_arguments)]
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::element_field::{ElementField, SubElementField};
/// # use pyrucast::containers::field::SubField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::matrix::DofOrdering;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::kernel;
/// # let coords = Handle::new(Coords::new(2).unwrap());
/// # let n: Vec<Node> = [[0.0, 0.0], [2.0, 0.0], [0.0, 2.0]]
/// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
/// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
/// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
/// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let zone = fes.get(0).unwrap();
/// # let support = zone.read().submesh().read().to_poi1().unwrap();
/// // Ce que rend `element_block_triplets` : la taille du bloc et ses
/// // triplets, en numérotation **locale** au bloc.
/// let (nr, nc, trips): kernel::BlockTriplets = kernel::element_block_triplets(
///     std::slice::from_ref(&zone), &support, &support, 1, 1,
///     DofOrdering::NodesThenVars, None, None,
///     |geoms, _m, _s, ke| { ke[0] = geoms[0].det_j_w(0)?; Ok(()) },
/// )?;
/// assert_eq!((nr, nc), (3, 3));
/// assert!(!trips.is_empty());
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn element_block_triplets(
    fespaces: &[Handle<SubFiniteElementSpace>],
    row_support: &Handle<SubMesh>,
    col_support: &Handle<SubMesh>,
    n_dual: usize,
    n_primal: usize,
    ordering: DofOrdering,
    material: Option<&Handle<SubElementField>>,
    state: Option<&Handle<SubElementField>>,
    element: impl Fn(
            &[CellGeom],
            Option<&SubElementField>,
            Option<&SubElementField>,
            &mut [f64],
        ) -> Result<()>
        + Sync,
) -> Result<BlockTriplets> {
    let (nrows, ncols, per_cell) = element_block_triplets_per_cell(
        fespaces,
        row_support,
        col_support,
        n_dual,
        n_primal,
        ordering,
        material,
        state,
        element,
    )?;
    let total: usize = per_cell.iter().map(|v| v.len()).sum();
    let mut trips = Vec::with_capacity(total);
    for cell_trips in per_cell {
        trips.extend(cell_trips);
    }
    Ok((nrows, ncols, trips))
}

/// `(nrows, ncols, per-cell local (row, col, value) triplets)` — the shape
/// returned by [`element_block_triplets_per_cell`].
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::element_field::{ElementField, SubElementField};
/// # use pyrucast::containers::field::SubField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::matrix::DofOrdering;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::kernel;
/// # let coords = Handle::new(Coords::new(2).unwrap());
/// # let n: Vec<Node> = [[0.0, 0.0], [2.0, 0.0], [0.0, 2.0]]
/// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
/// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
/// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
/// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let zone = fes.get(0).unwrap();
/// # let support = zone.read().submesh().read().to_poi1().unwrap();
/// // La même chose, **groupée par maille** : ce que consomme l'assembleur
/// // global pour verser droit dans le CSR sans matérialiser de valeurs.
/// let (nr, nc, par_maille): kernel::BlockTripletsPerCell =
///     kernel::element_block_triplets_per_cell(
///         std::slice::from_ref(&zone), &support, &support, 1, 1,
///         DofOrdering::NodesThenVars, None, None,
///         |geoms, _m, _s, ke| { ke[0] = geoms[0].det_j_w(0)?; Ok(()) },
///     )?;
/// assert_eq!((nr, nc), (3, 3));
/// assert_eq!(par_maille.len(), 1); // une maille
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub type BlockTripletsPerCell = (usize, usize, Vec<Vec<(usize, usize, f64)>>);

/// Same as [`element_block_triplets`] but keeps each cell's triplets in its own
/// `Vec` **instead of concatenating** them. Cells are still computed in parallel
/// and their triplets ordered `li, di, lj, pj` within a cell, so the per-cell
/// lists are reproducible. The grouping lets a colour-driven scatter process one
/// colour's cells (which touch disjoint DOFs) in parallel without write
/// conflicts.
///
/// `fespaces` is the block's FE subspaces: usually one, but several for a
/// multi-quadrature element. They must **share one submesh** (same connectivity,
/// same coordinates, same nodes-per-cell), differing only by quadrature — the
/// primary (index 0) drives the cell loop and the scatter numbering, the others
/// only add their reference data. The kernel receives one [`CellGeom`] per
/// subspace, in order.
#[allow(clippy::too_many_arguments)]
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::element_field::{ElementField, SubElementField};
/// # use pyrucast::containers::field::SubField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::matrix::DofOrdering;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::kernel;
/// # let coords = Handle::new(Coords::new(2).unwrap());
/// # let n: Vec<Node> = [[0.0, 0.0], [2.0, 0.0], [0.0, 2.0]]
/// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
/// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
/// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
/// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let zone = fes.get(0).unwrap();
/// # let support = zone.read().submesh().read().to_poi1().unwrap();
/// // La même chose, **groupée par maille** : ce que consomme l'assembleur
/// // global pour verser droit dans le CSR sans matérialiser de valeurs.
/// let (nr, nc, par_maille): kernel::BlockTripletsPerCell =
///     kernel::element_block_triplets_per_cell(
///         std::slice::from_ref(&zone), &support, &support, 1, 1,
///         DofOrdering::NodesThenVars, None, None,
///         |geoms, _m, _s, ke| { ke[0] = geoms[0].det_j_w(0)?; Ok(()) },
///     )?;
/// assert_eq!((nr, nc), (3, 3));
/// assert_eq!(par_maille.len(), 1); // une maille
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn element_block_triplets_per_cell(
    fespaces: &[Handle<SubFiniteElementSpace>],
    row_support: &Handle<SubMesh>,
    col_support: &Handle<SubMesh>,
    n_dual: usize,
    n_primal: usize,
    ordering: DofOrdering,
    material: Option<&Handle<SubElementField>>,
    state: Option<&Handle<SubElementField>>,
    element: impl Fn(
            &[CellGeom],
            Option<&SubElementField>,
            Option<&SubElementField>,
            &mut [f64],
        ) -> Result<()>
        + Sync,
) -> Result<BlockTripletsPerCell> {
    let primary = fespaces.first().ok_or_else(|| {
        PyrucastError::Message("element_block_triplets_per_cell: no FE subspace".into())
    })?;
    let fe = primary.read();
    let submesh = fe.submesh();
    let sm = submesh.read();
    let coords_h = sm.coords();
    let coords = coords_h.read();
    let mat_guard = material.map(|h| h.read());
    let state_guard = state.map(|h| h.read());

    // Reference data of every subspace, snapshotted once (they share the submesh
    // ⇒ one connectivity + coords drive every CellGeom; only quadrature differs).
    let mut rds = Vec::with_capacity(fespaces.len());
    rds.push(RefData::snapshot(&fe)?);
    for h in &fespaces[1..] {
        let f = h.read();
        if !f.submesh().same_object(&submesh) {
            return Err(PyrucastError::Message(
                "element_block_triplets_per_cell: all FE subspaces of a block must share one submesh"
                    .into(),
            ));
        }
        rds.push(RefData::snapshot(&f)?);
    }

    let n_cells = fe.cell_count()?;
    let n_nodes = rds[0].n_nodes;
    let conn: &[NodeId] = sm.connectivity();
    let rds_ref: &[RefData] = &rds;
    let coords_ref: &Coords = &coords;
    let mat_ref: Option<&SubElementField> = mat_guard.as_deref();
    let state_ref: Option<&SubElementField> = state_guard.as_deref();

    let n_cols_loc = n_nodes * n_primal;
    let ke_len = (n_nodes * n_dual) * n_cols_loc;

    // Support → local position maps (first occurrence wins), and the block's
    // local row/col dimensions — all known up front, so the whole loop runs in
    // parallel (no shared mutation).
    let row_nodes: Vec<NodeId> = row_support.read().connectivity().to_vec();
    let col_nodes: Vec<NodeId> = col_support.read().connectivity().to_vec();
    let n_row_nodes = row_nodes.len();
    let n_col_nodes = col_nodes.len();
    let nrows = n_row_nodes * n_dual;
    let ncols = n_col_nodes * n_primal;
    let pos_map = |nodes: &[NodeId]| -> HashMap<NodeId, u32> {
        let mut m = HashMap::with_capacity(nodes.len());
        for (i, &n) in nodes.iter().enumerate() {
            m.entry(n).or_insert(i as u32);
        }
        m
    };
    let row_pos = pos_map(&row_nodes);
    let col_pos = pos_map(&col_nodes);

    // Per cell, in parallel: compute the element matrix, then emit its triplets
    // in **local** indices. Order within a cell is li,di,lj,pj; cells are
    // concatenated in order below ⇒ identical triplet stream regardless of
    // thread count (bit-for-bit result).
    let per_cell: Vec<Vec<(usize, usize, f64)>> = (0..n_cells)
        .into_par_iter()
        .with_min_len((MIN_PARALLEL_LEN / n_nodes.max(1)).max(1))
        .map(|cell| -> Result<Vec<(usize, usize, f64)>> {
            let geoms: Vec<CellGeom> = rds_ref
                .iter()
                .map(|rd| CellGeom::new(rd, coords_ref, conn, cell))
                .collect::<Result<_>>()?;
            let mut ke = vec![0.0_f64; ke_len];
            element(&geoms, mat_ref, state_ref, &mut ke)?;

            let ids = &conn[cell * n_nodes..(cell + 1) * n_nodes];
            let mut rpos = Vec::with_capacity(n_nodes);
            let mut cpos = Vec::with_capacity(n_nodes);
            for &nid in ids {
                rpos.push(*row_pos.get(&nid).ok_or_else(|| {
                    PyrucastError::Message(format!(
                        "element_block_triplets: node {nid:?} not in row support"
                    ))
                })? as usize);
                cpos.push(*col_pos.get(&nid).ok_or_else(|| {
                    PyrucastError::Message(format!(
                        "element_block_triplets: node {nid:?} not in col support"
                    ))
                })? as usize);
            }

            let mut trips = Vec::with_capacity(ke_len);
            for li in 0..n_nodes {
                for di in 0..n_dual {
                    let r = li * n_dual + di;
                    let ri = ordering.to_index(rpos[li], di, n_row_nodes, n_dual);
                    for lj in 0..n_nodes {
                        for pj in 0..n_primal {
                            let c = lj * n_primal + pj;
                            let ci = ordering.to_index(cpos[lj], pj, n_col_nodes, n_primal);
                            trips.push((ri, ci, ke[r * n_cols_loc + c]));
                        }
                    }
                }
            }
            Ok(trips)
        })
        .collect::<Result<_>>()?;

    Ok((nrows, ncols, per_cell))
}

/// `(nrows, ncols, per-cell block-local (row, col) index pairs)` — the shape
/// returned by [`element_block_pattern`].
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::element_field::{ElementField, SubElementField};
/// # use pyrucast::containers::field::SubField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::matrix::DofOrdering;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::kernel;
/// # let coords = Handle::new(Coords::new(2).unwrap());
/// # let n: Vec<Node> = [[0.0, 0.0], [2.0, 0.0], [0.0, 2.0]]
/// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
/// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
/// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
/// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let zone = fes.get(0).unwrap();
/// # let support = zone.read().submesh().read().to_poi1().unwrap();
/// // Le **motif** seul, sans valeurs : c'est lui qui est mis en cache et
/// // réutilisé d'un assemblage à l'autre, la matière ne le changeant pas.
/// let (nr, nc, motif): kernel::BlockPattern = kernel::element_block_pattern(
///     &zone, &support, &support, 1, 1, DofOrdering::NodesThenVars,
/// )?;
/// assert_eq!((nr, nc), (3, 3));
/// assert_eq!(motif[0].len(), 3 * 3); // toutes les paires d'une maille
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub type BlockPattern = (usize, usize, Vec<Vec<(usize, usize)>>);

/// The **symbolic** structure of a computed stiffness block: for each cell, the
/// block-**local** `(row, col)` index pairs it writes, in the exact order
/// [`element_block_triplets`] emits their values (`li, di, lj, pj`). Carries no
/// geometry and evaluates no kernel — only connectivity + the DOF `ordering` —
/// so an assembler can build the global CSR sparsity pattern (and, from it,
/// per-cell scatter slots) cheaply and cache it, then run the numeric kernel
/// only when values are needed. Returns `(nrows, ncols, per_cell_pairs)`.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::element_field::{ElementField, SubElementField};
/// # use pyrucast::containers::field::SubField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::matrix::DofOrdering;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::kernel;
/// # let coords = Handle::new(Coords::new(2).unwrap());
/// # let n: Vec<Node> = [[0.0, 0.0], [2.0, 0.0], [0.0, 2.0]]
/// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
/// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
/// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
/// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let zone = fes.get(0).unwrap();
/// # let support = zone.read().submesh().read().to_poi1().unwrap();
/// // Le **motif** seul, sans valeurs : c'est lui qui est mis en cache et
/// // réutilisé d'un assemblage à l'autre, la matière ne le changeant pas.
/// let (nr, nc, motif): kernel::BlockPattern = kernel::element_block_pattern(
///     &zone, &support, &support, 1, 1, DofOrdering::NodesThenVars,
/// )?;
/// assert_eq!((nr, nc), (3, 3));
/// assert_eq!(motif[0].len(), 3 * 3); // toutes les paires d'une maille
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn element_block_pattern(
    fespace: &Handle<SubFiniteElementSpace>,
    row_support: &Handle<SubMesh>,
    col_support: &Handle<SubMesh>,
    n_dual: usize,
    n_primal: usize,
    ordering: DofOrdering,
) -> Result<BlockPattern> {
    let fe = fespace.read();
    let submesh = fe.submesh();
    let sm = submesh.read();
    let conn: &[NodeId] = sm.connectivity();
    let n_cells = fe.cell_count()?;
    let n_nodes = conn.len().checked_div(n_cells).unwrap_or(0);

    let row_nodes: Vec<NodeId> = row_support.read().connectivity().to_vec();
    let col_nodes: Vec<NodeId> = col_support.read().connectivity().to_vec();
    let n_row_nodes = row_nodes.len();
    let n_col_nodes = col_nodes.len();
    let nrows = n_row_nodes * n_dual;
    let ncols = n_col_nodes * n_primal;
    let pos_map = |nodes: &[NodeId]| -> HashMap<NodeId, u32> {
        let mut m = HashMap::with_capacity(nodes.len());
        for (i, &n) in nodes.iter().enumerate() {
            m.entry(n).or_insert(i as u32);
        }
        m
    };
    let row_pos = pos_map(&row_nodes);
    let col_pos = pos_map(&col_nodes);

    let per_cell: Vec<Vec<(usize, usize)>> = (0..n_cells)
        .into_par_iter()
        .with_min_len((MIN_PARALLEL_LEN / n_nodes.max(1)).max(1))
        .map(|cell| -> Result<Vec<(usize, usize)>> {
            let ids = &conn[cell * n_nodes..(cell + 1) * n_nodes];
            let mut rpos = Vec::with_capacity(n_nodes);
            let mut cpos = Vec::with_capacity(n_nodes);
            for &nid in ids {
                rpos.push(*row_pos.get(&nid).ok_or_else(|| {
                    PyrucastError::Message(format!(
                        "element_block_pattern: node {nid:?} not in row support"
                    ))
                })? as usize);
                cpos.push(*col_pos.get(&nid).ok_or_else(|| {
                    PyrucastError::Message(format!(
                        "element_block_pattern: node {nid:?} not in col support"
                    ))
                })? as usize);
            }
            let mut pairs = Vec::with_capacity(n_nodes * n_dual * n_nodes * n_primal);
            for li in 0..n_nodes {
                for di in 0..n_dual {
                    let ri = ordering.to_index(rpos[li], di, n_row_nodes, n_dual);
                    for lj in 0..n_nodes {
                        for pj in 0..n_primal {
                            let ci = ordering.to_index(cpos[lj], pj, n_col_nodes, n_primal);
                            pairs.push((ri, ci));
                        }
                    }
                }
            }
            Ok(pairs)
        })
        .collect::<Result<_>>()?;

    Ok((nrows, ncols, per_cell))
}

// ─── Inter-mesh coupling blocks ─────────────────────────────────────────────

/// Check that two FE subspace groups face each other cell for cell, and return
/// the shared `(cell count, row nodes/cell, col nodes/cell)`.
///
/// Two conforming boundary meshes are what an interface law needs: cell `i` of
/// one against cell `i` of the other. Anything else is a meshing problem, and is
/// reported as one rather than silently paired.
fn check_conforming(
    row_fe: &SubFiniteElementSpace,
    col_fe: &SubFiniteElementSpace,
    row_conn: &[NodeId],
    col_conn: &[NodeId],
) -> Result<(usize, usize, usize)> {
    let (n_row_cells, n_col_cells) = (row_fe.cell_count()?, col_fe.cell_count()?);
    if n_row_cells != n_col_cells {
        return Err(PyrucastError::Message(format!(
            "coupling block: the two sides of an interface must be conforming — \
             {n_row_cells} cell(s) facing {n_col_cells}"
        )));
    }
    let n_row_nodes = row_conn.len().checked_div(n_row_cells).unwrap_or(0);
    let n_col_nodes = col_conn.len().checked_div(n_col_cells).unwrap_or(0);
    Ok((n_row_cells, n_row_nodes, n_col_nodes))
}

/// Per-cell local triplets of an **inter-mesh** block: rows on one mesh, columns
/// on the facing one, paired cell by cell.
///
/// The same shape as [`element_block_triplets_per_cell`], and the same guarantees
/// (element matrices evaluated in parallel, triplets emitted in `li, di, lj, pj`
/// order so the stream is reproducible). What differs is that the cell loop
/// walks **two** connectivities: the row indices come from the row mesh, the
/// column indices from the facing one.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::matrix::DofOrdering;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::kernel::{self, assemble_block};
/// # let coords = Handle::new(Coords::new(3).unwrap());
/// # let n: Vec<Node> = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
/// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
/// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
/// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
/// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let zone = fes.get(0).unwrap();
/// # let support = zone.read().submesh().read().to_poi1().unwrap();
/// // Les valeurs du bloc de couplage, groupées par maille — le noyau y
/// // reçoit **deux** jeux de `CellGeom`, un par côté.
/// let (nr, nc, par_maille) = kernel::coupling_block_triplets_per_cell(
///     std::slice::from_ref(&zone), std::slice::from_ref(&zone),
///     &support, &support, 1, 1, DofOrdering::NodesThenVars, None,
///     |lignes, colonnes, _m, ke| {
///         assert_eq!(lignes.len(), colonnes.len());
///         ke[0] = lignes[0].det_j_w(0)?;
///         Ok(())
///     },
/// )?;
/// assert_eq!((nr, nc), (3, 3));
/// assert_eq!(par_maille.len(), 1);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
#[allow(clippy::too_many_arguments)]
pub fn coupling_block_triplets_per_cell(
    row_fespaces: &[Handle<SubFiniteElementSpace>],
    col_fespaces: &[Handle<SubFiniteElementSpace>],
    row_support: &Handle<SubMesh>,
    col_support: &Handle<SubMesh>,
    n_dual: usize,
    n_primal: usize,
    ordering: DofOrdering,
    material: Option<&Handle<SubElementField>>,
    element: impl Fn(&[CellGeom], &[CellGeom], Option<&SubElementField>, &mut [f64]) -> Result<()>
        + Sync,
) -> Result<BlockTripletsPerCell> {
    let row_primary = row_fespaces.first().ok_or_else(|| {
        PyrucastError::Message("coupling_block_triplets_per_cell: no row FE subspace".into())
    })?;
    let col_primary = col_fespaces.first().ok_or_else(|| {
        PyrucastError::Message("coupling_block_triplets_per_cell: no column FE subspace".into())
    })?;
    let (row_fe, col_fe) = (row_primary.read(), col_primary.read());
    let (row_sm_h, col_sm_h) = (row_fe.submesh(), col_fe.submesh());
    let (row_sm, col_sm) = (row_sm_h.read(), col_sm_h.read());
    let (row_coords_h, col_coords_h) = (row_sm.coords(), col_sm.coords());
    let (row_coords, col_coords) = (row_coords_h.read(), col_coords_h.read());
    let mat_guard = material.map(|h| h.read());

    let row_conn: &[NodeId] = row_sm.connectivity();
    let col_conn: &[NodeId] = col_sm.connectivity();
    let (n_cells, n_row_nodes_cell, n_col_nodes_cell) =
        check_conforming(&row_fe, &col_fe, row_conn, col_conn)?;

    let mut row_rds = vec![RefData::snapshot(&row_fe)?];
    for h in &row_fespaces[1..] {
        let f = h.read();
        row_rds.push(RefData::snapshot(&f)?);
    }
    let mut col_rds = vec![RefData::snapshot(&col_fe)?];
    for h in &col_fespaces[1..] {
        let f = h.read();
        col_rds.push(RefData::snapshot(&f)?);
    }

    let row_nodes: Vec<NodeId> = row_support.read().connectivity().to_vec();
    let col_nodes: Vec<NodeId> = col_support.read().connectivity().to_vec();
    let (n_row_support, n_col_support) = (row_nodes.len(), col_nodes.len());
    let (nrows, ncols) = (n_row_support * n_dual, n_col_support * n_primal);
    let row_pos = position_map(&row_nodes);
    let col_pos = position_map(&col_nodes);

    let n_cols_loc = n_col_nodes_cell * n_primal;
    let ke_len = (n_row_nodes_cell * n_dual) * n_cols_loc;
    let (row_rds_ref, col_rds_ref) = (&row_rds[..], &col_rds[..]);
    let (row_coords_ref, col_coords_ref): (&Coords, &Coords) = (&row_coords, &col_coords);
    let mat_ref: Option<&SubElementField> = mat_guard.as_deref();

    let per_cell: Vec<Vec<(usize, usize, f64)>> = (0..n_cells)
        .into_par_iter()
        .with_min_len((MIN_PARALLEL_LEN / n_row_nodes_cell.max(1)).max(1))
        .map(|cell| -> Result<Vec<(usize, usize, f64)>> {
            let row_geoms: Vec<CellGeom> = row_rds_ref
                .iter()
                .map(|rd| CellGeom::new(rd, row_coords_ref, row_conn, cell))
                .collect::<Result<_>>()?;
            let col_geoms: Vec<CellGeom> = col_rds_ref
                .iter()
                .map(|rd| CellGeom::new(rd, col_coords_ref, col_conn, cell))
                .collect::<Result<_>>()?;
            let mut ke = vec![0.0_f64; ke_len];
            element(&row_geoms, &col_geoms, mat_ref, &mut ke)?;

            let rpos = local_positions(
                &row_conn[cell * n_row_nodes_cell..(cell + 1) * n_row_nodes_cell],
                &row_pos,
                "row",
            )?;
            let cpos = local_positions(
                &col_conn[cell * n_col_nodes_cell..(cell + 1) * n_col_nodes_cell],
                &col_pos,
                "column",
            )?;

            let mut trips = Vec::with_capacity(ke_len);
            for (li, &rl) in rpos.iter().enumerate() {
                for di in 0..n_dual {
                    let r = li * n_dual + di;
                    let ri = ordering.to_index(rl, di, n_row_support, n_dual);
                    for (lj, &cl) in cpos.iter().enumerate() {
                        for pj in 0..n_primal {
                            let c = lj * n_primal + pj;
                            let ci = ordering.to_index(cl, pj, n_col_support, n_primal);
                            trips.push((ri, ci, ke[r * n_cols_loc + c]));
                        }
                    }
                }
            }
            Ok(trips)
        })
        .collect::<Result<_>>()?;

    Ok((nrows, ncols, per_cell))
}

/// The **symbolic** structure of an inter-mesh block — the coupling counterpart
/// of [`element_block_pattern`], carrying no geometry and evaluating no kernel.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::matrix::DofOrdering;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::kernel::{self, assemble_block};
/// # let coords = Handle::new(Coords::new(3).unwrap());
/// # let n: Vec<Node> = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
/// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
/// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
/// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
/// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let zone = fes.get(0).unwrap();
/// # let support = zone.read().submesh().read().to_poi1().unwrap();
/// // Un bloc **hors diagonale** : ses lignes vivent sur un maillage, ses
/// // colonnes sur celui d'en face. C'est ce qu'exige une loi d'interface.
/// // Ici les deux côtés sont le même, ce qui suffit à montrer la forme.
/// let (nr, nc, motif) = kernel::coupling_block_pattern(
///     &zone, &zone, &support, &support, 1, 1, DofOrdering::NodesThenVars)?;
/// assert_eq!((nr, nc), (3, 3));
/// assert_eq!(motif[0].len(), 3 * 3);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn coupling_block_pattern(
    row_fespace: &Handle<SubFiniteElementSpace>,
    col_fespace: &Handle<SubFiniteElementSpace>,
    row_support: &Handle<SubMesh>,
    col_support: &Handle<SubMesh>,
    n_dual: usize,
    n_primal: usize,
    ordering: DofOrdering,
) -> Result<BlockPattern> {
    let (row_fe, col_fe) = (row_fespace.read(), col_fespace.read());
    let (row_sm_h, col_sm_h) = (row_fe.submesh(), col_fe.submesh());
    let (row_sm, col_sm) = (row_sm_h.read(), col_sm_h.read());
    let row_conn: &[NodeId] = row_sm.connectivity();
    let col_conn: &[NodeId] = col_sm.connectivity();
    let (n_cells, n_row_nodes_cell, n_col_nodes_cell) =
        check_conforming(&row_fe, &col_fe, row_conn, col_conn)?;

    let row_nodes: Vec<NodeId> = row_support.read().connectivity().to_vec();
    let col_nodes: Vec<NodeId> = col_support.read().connectivity().to_vec();
    let (n_row_support, n_col_support) = (row_nodes.len(), col_nodes.len());
    let (nrows, ncols) = (n_row_support * n_dual, n_col_support * n_primal);
    let row_pos = position_map(&row_nodes);
    let col_pos = position_map(&col_nodes);

    let per_cell: Vec<Vec<(usize, usize)>> = (0..n_cells)
        .into_par_iter()
        .with_min_len((MIN_PARALLEL_LEN / n_row_nodes_cell.max(1)).max(1))
        .map(|cell| -> Result<Vec<(usize, usize)>> {
            let rpos = local_positions(
                &row_conn[cell * n_row_nodes_cell..(cell + 1) * n_row_nodes_cell],
                &row_pos,
                "row",
            )?;
            let cpos = local_positions(
                &col_conn[cell * n_col_nodes_cell..(cell + 1) * n_col_nodes_cell],
                &col_pos,
                "column",
            )?;
            let mut pairs = Vec::with_capacity(rpos.len() * n_dual * cpos.len() * n_primal);
            for &rl in &rpos {
                for di in 0..n_dual {
                    let ri = ordering.to_index(rl, di, n_row_support, n_dual);
                    for &cl in &cpos {
                        for pj in 0..n_primal {
                            pairs.push((ri, ordering.to_index(cl, pj, n_col_support, n_primal)));
                        }
                    }
                }
            }
            Ok(pairs)
        })
        .collect::<Result<_>>()?;

    Ok((nrows, ncols, per_cell))
}

/// Node → position in a support (first occurrence wins).
fn position_map(nodes: &[NodeId]) -> HashMap<NodeId, u32> {
    let mut m = HashMap::with_capacity(nodes.len());
    for (i, &n) in nodes.iter().enumerate() {
        m.entry(n).or_insert(i as u32);
    }
    m
}

/// Positions of a cell's nodes in a support, erroring by name on a stray node.
fn local_positions(ids: &[NodeId], pos: &HashMap<NodeId, u32>, side: &str) -> Result<Vec<usize>> {
    ids.iter()
        .map(|nid| {
            pos.get(nid).map(|&p| p as usize).ok_or_else(|| {
                PyrucastError::Message(format!(
                    "coupling block: node {nid:?} is not in the {side} support"
                ))
            })
        })
        .collect()
}

/// Integrate a per-cell kernel over `fespaces` and **scatter the result to the
/// nodes** of `support`, in parallel — the shared nodal integrate-and-scatter
/// driver. It backs the `Bᵀ` operators (internal forces `∫ Bᵀ σ`, Cast3m `BSIG`;
/// the weak divergence
/// [`crate::ops::node_field::divergence`](fn@crate::ops::node_field::divergence)) and the
/// distributed flux load `∫ φ N`
/// ([`crate::ops::node_field::flux`](fn@crate::ops::node_field::flux)) alike.
///
/// `element(geoms, fe)` is a pure sequential kernel: for one cell it fills `fe` —
/// the cell's local vector, **node-major / variable-minor**
/// (`fe[li * n_dual + di]`, `di` indexing `dual_vars`) — from the cell geometry
/// (one [`CellGeom`] per FE subspace of `fespaces`, same order). Its integrand
/// (a stress field, a flux density, …) is **captured by the closure**, borrowed
/// in place; the driver itself is agnostic to it. `element` never sees rayon, the
/// store, or a lock.
///
/// Each cell's vector is built and **scattered in the same parallel pass**, colour
/// by colour (the primary FE subspace's cached cell colouring): within a colour
/// the cells touch pairwise-disjoint nodes, so their accumulation into `support`'s
/// node slots never races, and colours run in sequence. Each node therefore sums
/// its cells in a fixed colour order — reproducible for any thread count, though
/// not bit-for-bit with a cell-order sum (see
/// [`crate::parallel::colored_scatter`]). The local vector lives on a per-thread
/// scratch buffer, so the whole element set is never materialised. Returns the
/// [`SubNodeField`] with one component per `dual_vars` on `support`.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::element_field::{ElementField, SubElementField};
/// # use pyrucast::containers::field::SubField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::matrix::DofOrdering;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::kernel;
/// # let coords = Handle::new(Coords::new(2).unwrap());
/// # let n: Vec<Node> = [[0.0, 0.0], [2.0, 0.0], [0.0, 2.0]]
/// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
/// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
/// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
/// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let zone = fes.get(0).unwrap();
/// # let support = zone.read().submesh().read().to_poi1().unwrap();
/// // Le pilote des **producteurs de champ nodal** : forces internes, flux.
/// // L'écriture concurrente passe par un coloriage, d'où un résultat
/// // reproductible bit à bit.
/// let f = kernel::scatter_to_nodes(
///     std::slice::from_ref(&zone), &support, vec!["q".into()],
///     |geoms, fe| {
///         for (i, slot) in fe.iter_mut().enumerate() {
///             *slot = geoms[0].det_j_w(0)? * (i as f64);
///         }
///         Ok(())
///     },
/// )?;
/// assert_eq!(f.node_count(), 3);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn scatter_to_nodes(
    fespaces: &[Handle<SubFiniteElementSpace>],
    support: &Handle<SubMesh>,
    dual_vars: Vec<String>,
    element: impl Fn(&[CellGeom], &mut [f64]) -> Result<()> + Sync,
) -> Result<SubNodeField> {
    let n_dual = dual_vars.len();
    let primary = fespaces
        .first()
        .ok_or_else(|| PyrucastError::Message("scatter_to_nodes: no FE subspace".into()))?;
    let fe = primary.read();
    let submesh = fe.submesh();
    let sm = submesh.read();
    let coords_h = sm.coords();
    let coords = coords_h.read();

    // Reference data of every subspace, snapshotted once (they share the submesh
    // ⇒ one connectivity + coords drive every CellGeom; only quadrature differs).
    let mut rds = Vec::with_capacity(fespaces.len());
    rds.push(RefData::snapshot(&fe)?);
    for h in &fespaces[1..] {
        let f = h.read();
        if !f.submesh().same_object(&submesh) {
            return Err(PyrucastError::Message(
                "scatter_to_nodes: all FE subspaces must share one submesh".into(),
            ));
        }
        rds.push(RefData::snapshot(&f)?);
    }

    let n_cells = fe.cell_count()?;
    let n_nodes = rds[0].n_nodes;
    let conn: &[NodeId] = sm.connectivity();
    let rds_ref: &[RefData] = &rds;
    let coords_ref: &Coords = &coords;
    let fe_len = n_nodes * n_dual;

    // Support slots: the unique nodes of `support` and each node's flat base.
    // `support` is the POI1 of the submesh, so it covers every connectivity node
    // and the map is total.
    let unique: Vec<NodeId> = support.read().connectivity().to_vec();
    let slot_of: HashMap<NodeId, usize> = unique.iter().enumerate().map(|(k, &n)| (n, k)).collect();

    // Cell colouring (cached on the primary FE subspace): two cells sharing a
    // node get different colours, so within a colour the cells scatter to
    // pairwise-disjoint nodes.
    let coloring = fe.coloring(|| coloring::greedy_color(n_cells, n_nodes, conn));

    // Fused compute + scatter, colour by colour: each cell builds its local
    // vector on a per-thread scratch buffer (no per-cell heap alloc, no
    // materialisation of the whole element set) and scatters it straight into the
    // node slots.
    let flat = colored_scatter(
        unique.len() * n_dual,
        coloring,
        (MIN_PARALLEL_LEN / n_nodes.max(1)).max(1),
        || vec![0.0_f64; fe_len],
        |cell, fe_cell, out| {
            let geoms: Vec<CellGeom> = rds_ref
                .iter()
                .map(|rd| CellGeom::new(rd, coords_ref, conn, cell))
                .collect::<Result<_>>()?;
            fe_cell.iter_mut().for_each(|v| *v = 0.0);
            element(&geoms, fe_cell)?;
            let ids = &conn[cell * n_nodes..(cell + 1) * n_nodes];
            for (li, &nid) in ids.iter().enumerate() {
                let node_slot = *slot_of.get(&nid).ok_or_else(|| {
                    PyrucastError::Message(
                        "scatter_to_nodes: support does not cover a cell node".into(),
                    )
                })?;
                let base = node_slot * n_dual;
                for di in 0..n_dual {
                    out.add(base + di, fe_cell[li * n_dual + di]);
                }
            }
            Ok(())
        },
    )?;

    let mut out = SubNodeField::from_poi1(support, dual_vars.clone())?;
    for (k, &nid) in unique.iter().enumerate() {
        for (di, name) in dual_vars.iter().enumerate() {
            out.set_value(nid, name, flat[k * n_dual + di])?;
        }
    }
    Ok(out)
}

/// Parallel **scalar reduction over the cells** of `fespace` — the reduction
/// counterpart of [`scatter_to_nodes`]. `cell(geom)` returns one cell's scalar
/// contribution (e.g. `∫_cell f dΩ` by quadrature); the driver sums them over
/// every cell and returns `Σ_cell cell(geom)`.
///
/// The per-cell geometry is the same lock-free [`CellGeom`] (reference data
/// snapshotted once, coordinates/connectivity borrowed in place), so `cell`
/// never touches rayon or the store. Its integrand (a nodal field, an element
/// field, …) is **captured by the closure**.
///
/// Determinism: the sum is an adaptive parallel reduction, so — like
/// [`crate::containers::field::SubField::dot`] and the other value reductions —
/// floating-point non-associativity makes the total thread-count-dependent to
/// the last ULP (not bit-for-bit reproducible). Cell-local contributions and the
/// geometry are identical to a sequential run; only the summation grouping
/// varies.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::element_field::{ElementField, SubElementField};
/// # use pyrucast::containers::field::SubField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::matrix::DofOrdering;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::kernel;
/// # let coords = Handle::new(Coords::new(2).unwrap());
/// # let n: Vec<Node> = [[0.0, 0.0], [2.0, 0.0], [0.0, 2.0]]
/// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
/// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
/// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
/// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let zone = fes.get(0).unwrap();
/// # let support = zone.read().submesh().read().to_poi1().unwrap();
/// // Une réduction sur les mailles : l'aire du maillage, par exemple, se
/// // lit comme la somme des mesures d'intégration.
/// let aire = kernel::reduce_cells(&zone, |geom| {
///     (0..geom.n_gauss).map(|g| geom.det_j_w(g)).sum()
/// })?;
/// assert!((aire - 2.0).abs() < 1e-12);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn reduce_cells(
    fespace: &Handle<SubFiniteElementSpace>,
    cell: impl Fn(&CellGeom) -> Result<f64> + Sync,
) -> Result<f64> {
    let fe = fespace.read();
    let submesh = fe.submesh();
    let sm = submesh.read();
    let coords_h = sm.coords();
    let coords = coords_h.read();
    let rd = RefData::snapshot(&fe)?;

    let n_cells = fe.cell_count()?;
    let n_nodes = rd.n_nodes;
    let conn: &[NodeId] = sm.connectivity();
    let rd_ref: &RefData = &rd;
    let coords_ref: &Coords = &coords;

    (0..n_cells)
        .into_par_iter()
        .with_min_len((MIN_PARALLEL_LEN / n_nodes.max(1)).max(1))
        .map(|c| -> Result<f64> {
            let geom = CellGeom::new(rd_ref, coords_ref, conn, c)?;
            cell(&geom)
        })
        .try_reduce(|| 0.0, |a, b| Ok(a + b))
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::aggregate::Aggregate;
    use crate::atoms::{ElementType, Node};
    use crate::containers::finite_element_space::FiniteElementSpace;
    use crate::containers::mesh::Mesh;
    use crate::handle::Handle;

    /// One QUA4 spanning `r ∈ [r0, r1]`, `z ∈ [0, 1]`, in the requested frame.
    fn one_quad(r0: f64, r1: f64, axisymmetric: bool) -> Handle<SubFiniteElementSpace> {
        let coords = Handle::new(if axisymmetric {
            Coords::axisymmetric().unwrap()
        } else {
            Coords::new(2).unwrap()
        });
        let a = Node::create_in(coords.clone(), &[r0, 0.0]).unwrap();
        let b = Node::create_in(coords.clone(), &[r1, 0.0]).unwrap();
        let c = Node::create_in(coords.clone(), &[r1, 1.0]).unwrap();
        let d = Node::create_in(coords.clone(), &[r0, 1.0]).unwrap();
        let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::QUA4));
        mesh.add_cell(&[a.id(), b.id(), c.id(), d.id()]).unwrap();
        FiniteElementSpace::lagrange1(&mesh)
            .unwrap()
            .get(0)
            .unwrap()
    }

    /// `Σ_g det_j_w` is the cell measure: the plane area in Cartesian, the
    /// **revolved volume** `π(r1² − r0²)·h` in axisymmetric — the single check
    /// that the 2πr factor lands in the quadrature weight.
    #[test]
    fn det_j_w_carries_the_revolution_measure() {
        let plane = reduce_cells(&one_quad(1.0, 2.0, false), |geom| {
            (0..geom.n_gauss).map(|g| geom.det_j_w(g)).sum()
        })
        .unwrap();
        assert!((plane - 1.0).abs() < 1e-12, "plane area {plane} ≠ 1");

        let revolved = reduce_cells(&one_quad(1.0, 2.0, true), |geom| {
            (0..geom.n_gauss).map(|g| geom.det_j_w(g)).sum()
        })
        .unwrap();
        let expected = std::f64::consts::PI * (2.0_f64.powi(2) - 1.0);
        assert!(
            (revolved - expected).abs() < 1e-10,
            "revolved volume {revolved} ≠ {expected}"
        );
    }

    /// Gauss points sit strictly inside the cell, so a cell **touching the axis**
    /// still has `r > 0` — what keeps the hoop term `N_i / r` finite there.
    #[test]
    fn radius_is_positive_even_on_a_cell_touching_the_axis() {
        reduce_cells(&one_quad(0.0, 1.0, true), |geom| {
            for g in 0..geom.n_gauss {
                let r = geom.radius(g)?;
                assert!(r > 0.0 && r < 1.0, "radius {r} outside (0, 1)");
                // radius is the first physical coordinate, and z stays in [0, 1].
                let x = geom.x_at_g(g)?;
                assert_eq!(x.len(), 2);
                assert!((x[0] - r).abs() < 1e-15);
                assert!(x[1] > 0.0 && x[1] < 1.0);
            }
            Ok(0.0)
        })
        .unwrap();
    }

    /// `radius` is meaningless without the revolution hypothesis, and says so.
    #[test]
    fn radius_rejects_a_cartesian_geometry() {
        let err = reduce_cells(&one_quad(1.0, 2.0, false), |geom| geom.radius(0)).unwrap_err();
        assert!(format!("{err}").contains("not axisymmetric"));
    }
}