pyrucast 0.10.0

Finite-element library in Rust, inspired by cast3m, exposed to Python
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
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
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
//! Sparse matrix indexed by **named DOFs** `(NodeId, field_name)`.
//!
//! Hierarchy:
//!
//! - [`SubMatrix`] — one COO block.  Structure is fully declared at
//!   construction time via two POI1 [`SubMesh`] handles (row/col node
//!   sets), two variable-name lists (dual / primal), and a
//!   [`DofOrdering`] that maps `(node_local_idx, var_idx)` to a flat
//!   matrix-row or -column index.  The node sets are **read from the
//!   supports**, never copied into the block: the supports are sealed, so
//!   the numbering cannot drift (see `book/src/developper/parallelisme.md`
//!   § Zéro-copie).  The actual non-zeros are stored in a
//!   [`nalgebra_sparse::CooMatrix`]; [`SubMatrix::add_entry`] appends
//!   triplets that accumulate on `get` / densification.
//! - [`Matrix`] — aggregate of [`SubMatrix`] blocks (one
//!   `Vec<Handle<SubMatrix>>`), produced by
//!   [`crate::containers::model::Model`] assembly (one or several blocks
//!   per sub-model).  Read-only: every accessor unions the blocks on the
//!   fly.
//!
//! Each [`SubMatrix`] declares what share of the matrix's symmetry it carries
//! ([`Symmetry`]), and the aggregate [`Matrix`] adds those declarations up
//! ([`Matrix::symmetric`]). Storage is never de-duplicated — a symmetric matrix
//! still holds both triangles — but the declaration *is* consulted: it decides
//! whether the assembled CSR may be handed to the factorization as its own CSC.
//!
//! # DOF layout
//!
//! Given a [`SubMatrix`] with `n_rn` row-support nodes, `n_dv` dual
//! variables, and [`DofOrdering::NodesThenVars`]:
//!
//! ```text
//! row index i = node_local * n_dv + var_idx
//! ```
//!
//! With [`DofOrdering::VarsThenNodes`]:
//!
//! ```text
//! row index i = var_idx * n_rn + node_local
//! ```
//!
//! The same formula applies symmetrically to columns with `n_cn` (col
//! nodes) and `n_pv` (primal variables).
//!
//! # Example — single block
//!
//! ```
//! use pyrucast::coords::Coords;
//! use pyrucast::atoms::ElementType;
//! use pyrucast::containers::mesh::SubMesh;
//! use pyrucast::atoms::Node;
//! use pyrucast::containers::matrix::{DofOrdering, SubMatrix, Symmetry};
//! use pyrucast::handle::Handle;
//!
//! let coords = Handle::new(Coords::new(1).unwrap());
//! let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
//! let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
//! let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
//! sm.add_cell(&[a.id()]).unwrap();
//! sm.add_cell(&[b.id()]).unwrap();
//! let support = Handle::new(sm);
//!
//! let mut k = SubMatrix::new(
//!     support.clone(), support.clone(),
//!     vec!["q".into()], vec!["T".into()],
//!     DofOrdering::NodesThenVars, Symmetry::Full,
//! );
//! k.add_entry(a.id(), "q", a.id(), "T",  2.0).unwrap();
//! k.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
//! k.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
//! k.add_entry(b.id(), "q", b.id(), "T",  2.0).unwrap();
//!
//! assert_eq!(k.n_rows(), 2);
//! assert_eq!(k.n_cols(), 2);
//! assert!(k.is_symmetric());
//! assert_eq!(k.get(a.id(), "q", a.id(), "T"), 2.0);
//! ```

use crate::aggregate::Aggregate;
use crate::atoms::NodeId;
use crate::containers::element_field::SubElementField;
use crate::containers::finite_element_space::SubFiniteElementSpace;
use crate::containers::mesh::Mesh;
use crate::containers::mesh::SubMesh;
use crate::containers::model::SubModel;
use crate::containers::node_field::{NodeField, SubNodeField};
use crate::coords::Coords;
use crate::error::{PyrucastError, Result};
use crate::handle::{Handle, ReadGuard};
use crate::models::{MatrixKind, Physics};
use crate::parallel::*;
use nalgebra::{DMatrix, DVector};
use nalgebra_sparse::{CooMatrix, CscMatrix, CsrMatrix};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;

/// A single COO entry with DOFs materialised as `(NodeId, var_name)` pairs:
/// `(row_node, row_var, col_node, col_var, value)`.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::matrix::{DofOrdering, Matrix, SubMatrix, Symmetry};
/// # use pyrucast::containers::mesh::SubMesh;
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # let coords = Handle::new(Coords::new(1).unwrap());
/// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
/// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
/// # let support = {
/// #     let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
/// #     sm.add_cell(&[a.id()]).unwrap();
/// #     sm.add_cell(&[b.id()]).unwrap();
/// #     Handle::new(sm)
/// # };
/// # let bloc = || {
/// #     let mut z = SubMatrix::new(
/// #         support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
/// #         DofOrdering::NodesThenVars, Symmetry::Full);
/// #     z.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
/// #     z.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
/// #     z.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
/// #     z.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
/// #     z
/// # };
/// # use pyrucast::containers::matrix::MatrixEntry;
/// // An entry whose DOFs are **materialized**: no more indices, but
/// // `(node, variable)` already resolved.
/// let entrees: Vec<MatrixEntry> = bloc().iter_entries();
/// assert_eq!(entrees.len(), 4);
/// assert_eq!((entrees[0].1.as_str(), entrees[0].3.as_str()), ("q", "T"));
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub type MatrixEntry = (NodeId, String, NodeId, String, f64);

// ─── DofOrdering ───────────────────────────────────────────────────────────

/// How `(node_local_idx, var_idx)` maps to a flat matrix-row or -column
/// index inside a [`SubMatrix`].
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::matrix::{DofOrdering, Matrix, SubMatrix, Symmetry};
/// # use pyrucast::containers::mesh::SubMesh;
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # let coords = Handle::new(Coords::new(1).unwrap());
/// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
/// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
/// # let support = {
/// #     let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
/// #     sm.add_cell(&[a.id()]).unwrap();
/// #     sm.add_cell(&[b.id()]).unwrap();
/// #     Handle::new(sm)
/// # };
/// # let bloc = || {
/// #     let mut z = SubMatrix::new(
/// #         support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
/// #         DofOrdering::NodesThenVars, Symmetry::Full);
/// #     z.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
/// #     z.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
/// #     z.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
/// #     z.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
/// #     z
/// # };
/// // Two ways to flatten `(node, variable)` into a row index. On two nodes
/// // and two variables, they coincide only at the ends.
/// assert_eq!(DofOrdering::NodesThenVars.to_index(1, 0, 2, 2), 2);
/// assert_eq!(DofOrdering::VarsThenNodes.to_index(1, 0, 2, 2), 1);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum DofOrdering {
    /// `index = var_idx * n_nodes + node_local`
    ///
    /// All nodes for variable 0, then all nodes for variable 1, …
    VarsThenNodes,
    /// `index = node_local * n_vars + var_idx`
    ///
    /// All variables for node 0, then all variables for node 1, …
    NodesThenVars,
}

impl DofOrdering {
    /// `(node_local, var_idx)` → flat matrix index.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, Matrix, SubMatrix, Symmetry};
    /// # use pyrucast::containers::mesh::SubMesh;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = {
    /// #     let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
    /// #     sm.add_cell(&[a.id()]).unwrap();
    /// #     sm.add_cell(&[b.id()]).unwrap();
    /// #     Handle::new(sm)
    /// # };
    /// # let bloc = || {
    /// #     let mut z = SubMatrix::new(
    /// #         support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #         DofOrdering::NodesThenVars, Symmetry::Full);
    /// #     z.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// #     z.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// #     z
    /// # };
    /// // `NodesThenVars`: every variable of node 0, then those of node 1.
    /// assert_eq!(DofOrdering::NodesThenVars.to_index(1, 1, 2, 2), 3);
    /// // `VarsThenNodes`: every node of variable 0, then those of variable 1.
    /// assert_eq!(DofOrdering::VarsThenNodes.to_index(1, 1, 2, 2), 3);
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn to_index(
        self,
        node_local: usize,
        var_idx: usize,
        n_nodes: usize,
        n_vars: usize,
    ) -> usize {
        match self {
            DofOrdering::VarsThenNodes => var_idx * n_nodes + node_local,
            DofOrdering::NodesThenVars => node_local * n_vars + var_idx,
        }
    }

    /// flat matrix index → `(node_local, var_idx)`.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, Matrix, SubMatrix, Symmetry};
    /// # use pyrucast::containers::mesh::SubMesh;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = {
    /// #     let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
    /// #     sm.add_cell(&[a.id()]).unwrap();
    /// #     sm.add_cell(&[b.id()]).unwrap();
    /// #     Handle::new(sm)
    /// # };
    /// # let bloc = || {
    /// #     let mut z = SubMatrix::new(
    /// #         support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #         DofOrdering::NodesThenVars, Symmetry::Full);
    /// #     z.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// #     z.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// #     z
    /// # };
    /// // The exact inverse of `to_index`, for both layouts.
    /// for o in [DofOrdering::NodesThenVars, DofOrdering::VarsThenNodes] {
    ///     for i in 0..4 {
    ///         let (nl, vi) = o.from_index(i, 2, 2);
    ///         assert_eq!(o.to_index(nl, vi, 2, 2), i);
    ///     }
    /// }
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn from_index(self, idx: usize, n_nodes: usize, n_vars: usize) -> (usize, usize) {
        match self {
            DofOrdering::VarsThenNodes => (idx % n_nodes, idx / n_nodes),
            DofOrdering::NodesThenVars => (idx / n_vars, idx % n_vars),
        }
    }
}

impl crate::dump::Dump for DofOrdering {
    fn render(&self, _opts: &crate::dump::DumpOptions) -> String {
        format!("{self:?}")
    }
}

// ─── SubMatrix ─────────────────────────────────────────────────────────────

/// What a computed block's kernel reads **besides** its material, named per
/// [`MatrixKind`] rather than encoded in an `Option`.
///
/// A stiffness and a mass need nothing; a geometric stiffness needs the current
/// stress; a consistent tangent needs what the constitutive law needs — because
/// it is evaluated at the Gauss point rather than read back from a field that
/// nobody else would consume.
///
/// ```
/// # use pyrucast::containers::matrix::KernelInputs;
/// // A stiffness reads only its material — and that is the default, because
/// // this is the case for three matrix kinds out of four.
/// assert!(matches!(KernelInputs::default(), KernelInputs::MaterialOnly));
/// ```
#[derive(Clone, Default, Serialize, Deserialize)]
pub enum KernelInputs {
    /// Stiffness, mass: the material is enough.
    #[default]
    MaterialOnly,
    /// Geometric stiffness: the current stress.
    State(Handle<SubElementField>),
    /// Consistent tangent: exactly what `behavior::integrate` is given. `prev`
    /// is **not** an `Option` — the rest state is materialised at the operator
    /// boundary, so below that line there is always a real field.
    Behavior {
        deformation: Handle<SubElementField>,
        prev: Handle<SubElementField>,
        dt: f64,
    },
}

/// One sparse COO block whose DOF layout is fully described by two POI1
/// sub-meshes and variable-name lists.
///
/// The **row DOF** at matrix index `i` is
/// `(row_support.connectivity()[node_local], dual_vars[var_idx])` where
/// `(node_local, var_idx) = ordering.from_index(i, n_row_nodes, n_dual_vars)`.
/// Columns are symmetric with `col_support` and `primal_vars`. The block keeps
/// **no node list of its own**: it reads its supports in place, which is sound
/// because both are sealed at construction. Their nodes must be **distinct** —
/// the shape `to_poi1` produces — since `node_local` is looked up through
/// [`SubMesh::node_index`], a rank that only matches the position without
/// repeats.
/// The recipe a **computed** [`SubMatrix`] carries *instead of* stored values:
/// how to evaluate its contribution on the fly. The global assembler drives the
/// sub-model's [`element_matrix`](crate::models::Domain::element_matrix) kernel
/// over `fespace`'s cells and scatters the result straight into the global
/// matrix — a computed block never materialises a COO (its own `coo` stays an
/// empty, correctly-sized placeholder, so structural queries still work).
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::element_field::ElementField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::matrix::{ComputedRecipe, DofOrdering, KernelInputs, SubMatrix, Symmetry};
/// # use pyrucast::models::MatrixKind;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::containers::model::Model;
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::ops::element_field;
/// # use pyrucast::ops::model;
/// # let coords = Handle::new(Coords::new(2).unwrap());
/// # let n: Vec<Node> = [[0.0, 0.0], [1.0, 0.0], [0.0, 1.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 modele = model::heat_conduction(&fes).unwrap();
/// # let mat = element_field::material_field(&modele, &[("k", 1.0)]).unwrap();
/// // A **computed** block materializes no value: it carries the recipe
/// // that the global assembler unwinds cell by cell, straight into the CSR.
/// let recette = ComputedRecipe {
///     submodel: modele.get(0)?,
///     fespaces: vec![zone.clone()],
///     material: Some(mat.get(0)?),
///     kind: MatrixKind::Stiffness,
///     inputs: KernelInputs::MaterialOnly,
///     col_fespaces: Vec::new(),
/// };
/// let z = SubMatrix::computed(
///     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
///     DofOrdering::NodesThenVars, Symmetry::Full, recette);
/// assert!(z.is_computed());
/// // Its structure is complete — hence the structural queries that
/// // work — but it counts **no** stored entry.
/// assert_eq!((z.n_rows(), z.n_cols()), (3, 3));
/// assert_eq!(z.entry_count(), 0);
/// // And nothing can be added to it by hand.
/// # let mut z = z;
/// assert!(z.add_entry(n[0].id(), "q", n[0].id(), "T", 1.0).is_err());
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
#[derive(Clone, Serialize, Deserialize)]
pub struct ComputedRecipe {
    /// Sub-model whose element kernel produces the contribution.
    pub submodel: Handle<SubModel>,
    /// FE subspaces the kernel integrates over. Usually one; several (sharing one
    /// submesh, differing by quadrature) for a multi-quadrature element. The
    /// primary (index 0) drives the cell loop and the scatter numbering.
    pub fespaces: Vec<Handle<SubFiniteElementSpace>>,
    /// Material field for the kernel; `Some` iff the physics declares one.
    pub material: Option<Handle<SubElementField>>,
    /// Which element matrix this recipe produces — the discriminant the scatter
    /// dispatches on to pick the sub-model's kernel
    /// ([`SubModelKind::matrix_element`](crate::models::SubModelKind::matrix_element)).
    /// Defaults to [`MatrixKind::Stiffness`] for backward-compatible deserialization.
    #[serde(default)]
    pub kind: MatrixKind,
    /// What this kind's kernel reads **besides** the material — see
    /// [`KernelInputs`].
    #[serde(default)]
    pub inputs: KernelInputs,
    /// FE subspaces carrying the **columns** when this block couples two meshes
    /// (an interface exchange law). **Empty** — the overwhelming case — means
    /// rows and columns live on the same mesh and `fespaces` drives both; the
    /// scatter routes on exactly that emptiness.
    #[serde(default)]
    pub col_fespaces: Vec<Handle<SubFiniteElementSpace>>,
}

/// Default value of [`SubMatrix::factor`] for pre-existing serialized data
/// that predates the field.
fn default_factor() -> f64 {
    1.0
}

/// Identity of a symmetric **pair** of blocks — see [`Symmetry::Half`].
///
/// Its high 63 bits are a hash of what defines the pair; bit 0 tells the two
/// members apart. Only the producer that builds both halves mints one, so a
/// `Half` never arrives alone by accident.
///
/// ```
/// # use pyrucast::containers::matrix::{PairId, Symmetry};
/// // Two halves of one pair name the same identity and differ in its low bit.
/// let Symmetry::Half(id) = Symmetry::Half(42 as PairId) else { unreachable!() };
/// assert_eq!(id & !1, 42);
/// ```
pub type PairId = u64;

/// FNV-1a over `bytes`, folded into the running hash `h`.
///
/// Hand-rolled because the pair identity must be **deterministic across runs** —
/// it is serialized, and two saves of the same objects must give the same bytes
/// (`tests/archive.rs`). `DefaultHasher` guarantees no such stability, and a
/// random id would need a dependency this crate does not have.
fn fnv(h: u64, bytes: &[u8]) -> u64 {
    let mut h = h;
    for &b in bytes {
        h ^= b as u64;
        h = h.wrapping_mul(0x0000_0100_0000_01b3);
    }
    h
}

/// The seed every pair hash starts from (the FNV-1a offset basis).
pub(crate) const FNV_SEED: u64 = 0xcbf2_9ce4_8422_2325;

/// Fold a node list into a running pair hash.
pub(crate) fn hash_nodes(h: u64, nodes: &[NodeId]) -> u64 {
    nodes.iter().fold(h, |h, n| fnv(h, &n.0.to_le_bytes()))
}

/// Fold a name into a running pair hash.
pub(crate) fn hash_name(h: u64, name: &str) -> u64 {
    fnv(h, name.as_bytes())
}

/// Fold an `f64` into a running pair hash, by its bits.
pub(crate) fn hash_f64(h: u64, v: f64) -> u64 {
    fnv(h, &v.to_bits().to_le_bytes())
}

/// The two identities of one symmetric pair, from a hash of its content.
///
/// Handed back **together** because they only mean anything together: no
/// producer can obtain one half without the other, which is what keeps a lone
/// `Half` from ever being minted by hand.
///
/// `content` should fold in everything that distinguishes this pair from any
/// other — both supports' nodes, the variable names, any coefficient — through
/// [`hash_nodes`], [`hash_name`] and [`hash_f64`] starting from [`FNV_SEED`].
/// Two genuinely distinct pairs that hashed alike would be *rejected* by
/// [`Matrix::symmetric`], never wrongly accepted: the error is to forget a
/// symmetry, never to invent one.
pub(crate) fn mint_pair(content: u64) -> (PairId, PairId) {
    // Bit 0 is not a property of either block — it is a label saying "these two
    // are not the same one". Which member gets which is irrelevant; that they
    // differ is what the count below relies on.
    let base = content & !1;
    (base, base | 1)
}

/// What share of the matrix's symmetry a [`SubMatrix`] carries.
///
/// The property at stake is the assembled array's: `A[i][j] == A[j][i]` on the
/// CSR. A block **declares** its share and is believed — a model knows what it
/// writes, and nothing here verifies it.
///
/// Declaring correctly is therefore the producer's whole job, and the aggregate
/// adds up declarations without correcting them. In particular an **empty block
/// is symmetric** (it contributes nothing that could break `A = Aᵀ`), so it
/// declares [`Full`](Self::Full) — the rule has no exception for it.
///
/// ```
/// # use pyrucast::containers::matrix::Symmetry;
/// # use pyrucast::named::Named;
/// // Only the two nameable shares can be parsed: a half means nothing alone.
/// assert_eq!(Symmetry::parse("full")?, Symmetry::Full);
/// assert!(Symmetry::parse("half").is_err());
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Symmetry {
    /// This block is symmetric on its own — the usual case for a block whose
    /// row and column supports coincide (a stiffness, a mass, a Gram matrix).
    Full,
    /// This block carries **half** of a symmetry: its transpose is the other
    /// block sharing this [`PairId`]. Neither is symmetric alone — a Dirichlet's
    /// `C` and `Cᵀ` are rectangular — and only the aggregate can see whether
    /// both are present.
    Half(PairId),
    /// This block carries none of it.
    None,
}

impl crate::named::Named for Symmetry {
    const LABEL: &'static str = "symmetry";
    // `Half` is deliberately absent: a half cannot be named into existence, it
    // only means anything paired. Refusing "half" with `expected full|none` is
    // the right message for someone declaring a block by hand.
    const VALUES: &'static [Self] = &[Symmetry::Full, Symmetry::None];

    fn name(self) -> &'static str {
        match self {
            Symmetry::Full => "full",
            Symmetry::Half(_) => "half",
            Symmetry::None => "none",
        }
    }
}

///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::matrix::{DofOrdering, Matrix, SubMatrix, Symmetry};
/// # use pyrucast::containers::mesh::SubMesh;
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # let coords = Handle::new(Coords::new(1).unwrap());
/// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
/// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
/// # let support = {
/// #     let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
/// #     sm.add_cell(&[a.id()]).unwrap();
/// #     sm.add_cell(&[b.id()]).unwrap();
/// #     Handle::new(sm)
/// # };
/// # let mut bloc = SubMatrix::new(
/// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
/// #     DofOrdering::NodesThenVars, Symmetry::Full);
/// # bloc.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
/// # bloc.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
/// // A **block**: its row and column supports, its variables
/// // dual and primal, and its entries. Nothing lives in the aggregate.
/// assert_eq!((bloc.n_rows(), bloc.n_cols()), (2, 2));
/// assert_eq!(bloc.dual_vars(), &["q".to_string()]);
/// assert_eq!(bloc.get(a.id(), "q", a.id(), "T"), 2.0);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
#[derive(Clone, Serialize, Deserialize)]
pub struct SubMatrix {
    /// POI1 mesh: cell `k` holds the k-th row-support node.
    row_support: Handle<SubMesh>,
    /// POI1 mesh: cell `k` holds the k-th col-support node.
    col_support: Handle<SubMesh>,
    /// Row variable names (dual variables).
    dual_vars: Vec<String>,
    /// Column variable names (primal variables).
    primal_vars: Vec<String>,
    /// `(node_local, var_idx)` ↔ matrix index mapping.
    ordering: DofOrdering,
    /// COO data, sized `(n_row_nodes × n_dual_vars) × (n_col_nodes × n_primal_vars)`.
    #[serde(with = "coo_serde")]
    coo: CooMatrix<f64>,
    /// What share of the matrix's symmetry this block carries — **declared**
    /// by its producer, believed without verification. See [`Symmetry`].
    symmetry: Symmetry,
    /// `Some` ⇒ this is a **computed** block: `coo` is an empty placeholder and
    /// the contribution is produced on the fly by the global assembler from this
    /// recipe. `None` ⇒ **literal** block, `coo` holds the values (the historical
    /// behaviour, unchanged).
    #[serde(default)]
    recipe: Option<ComputedRecipe>,
    /// The set of [`Physics`] natures of the sub-model that produced this block,
    /// set by the assembler ([`crate::ops::matrix`]) for **both** the computed
    /// and the literal path (so a Dirichlet C/Cᵀ pair is tagged too). **Empty**
    /// for a block built directly, outside assembly (the « rien » case), or
    /// carrying several natures for a coupled physics. Consumed by
    /// [`Matrix::filter`](Matrix::filter).
    #[serde(default)]
    physics: Vec<Physics>,
    /// Lazy scalar scale applied to every value this block emits — at direct
    /// accessors (`get`, `dense`, …) and at global assembly (`build_global_triplets`,
    /// [`crate::ops::scatter`]) alike. Defaults to `1.0`; set via
    /// `Mul<f64>`/`Div<f64>` ([`std::ops::Mul`], [`std::ops::Div`]) rather than
    /// eagerly rewriting `coo`, so it works for a **computed** block too (its
    /// values don't exist until assembly evaluates the recipe). Never touches
    /// `local_coo_arrays`/`local_triplets`, which stay raw — every consumer of
    /// those applies the factor itself.
    #[serde(default = "default_factor")]
    factor: f64,
}

impl SubMatrix {
    /// Build a new block.
    ///
    /// `row_support` / `col_support` must be POI1 sub-meshes whose cells
    /// define the row/col node sequence, and whose nodes are **distinct** —
    /// what `to_poi1` produces, and what every physics hands over. A repeated
    /// node is not rejected but addresses the wrong row: the block looks
    /// `node_local` up through [`SubMesh::node_index`], a deduplicated rank
    /// that parts from the flat position as soon as a node appears twice.
    /// `dual_vars` / `primal_vars` are the row/column variable names; they must
    /// be non-empty.
    ///
    /// Both supports are sealed here: the block reads their connectivity in
    /// place ever after, it keeps no copy.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, SubMatrix, Symmetry};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::mesh;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = mesh::poi1_from_nodes(&[a.clone(), b.clone()]).unwrap().get(0).unwrap();
    /// # let mut bloc = SubMatrix::new(
    /// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #     DofOrdering::NodesThenVars, Symmetry::Full);
    /// # bloc.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// # bloc.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// // The block knows its two POI1 supports and its two sets of variables:
    /// // its dimensions follow, nodes × variables on each side.
    /// assert_eq!((bloc.n_rows(), bloc.n_cols()), (2, 2));
    /// ```
    pub fn new(
        row_support: Handle<SubMesh>,
        col_support: Handle<SubMesh>,
        dual_vars: Vec<String>,
        primal_vars: Vec<String>,
        ordering: DofOrdering,
        symmetry: Symmetry,
    ) -> Self {
        // The block's row/col numbering *is* these supports' connectivity, read
        // in place on every access rather than copied; freeze them so it holds.
        crate::containers::mesh::seal(&row_support);
        crate::containers::mesh::seal(&col_support);
        let nrows = row_support.read().connectivity().len() * dual_vars.len();
        let ncols = col_support.read().connectivity().len() * primal_vars.len();
        Self {
            row_support,
            col_support,
            dual_vars,
            primal_vars,
            ordering,
            coo: CooMatrix::new(nrows, ncols),
            symmetry,
            recipe: None,
            physics: Vec::new(),
            factor: 1.0,
        }
    }

    /// Build a **computed** block: a sized-but-empty placeholder that carries a
    /// [`ComputedRecipe`] instead of values. Its structure (supports, vars,
    /// ordering, dimensions) is fully defined; the values are produced by the
    /// global assembler, which drives `submodel`'s element kernel over
    /// `recipe.fespace` and scatters straight into the global matrix.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::element_field::ElementField;
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::{ComputedRecipe, DofOrdering, KernelInputs, SubMatrix, Symmetry};
    /// # use pyrucast::models::MatrixKind;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::containers::model::Model;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::element_field;
    /// # use pyrucast::ops::model;
    /// # let coords = Handle::new(Coords::new(2).unwrap());
    /// # let n: Vec<Node> = [[0.0, 0.0], [1.0, 0.0], [0.0, 1.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 modele = model::heat_conduction(&fes).unwrap();
    /// # let mat = element_field::material_field(&modele, &[("k", 1.0)]).unwrap();
    /// // The same seen from the constructor: a sized, empty template,
    /// // whose values will come from the sub-model's kernel.
    /// let z = SubMatrix::computed(
    ///     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    ///     DofOrdering::NodesThenVars, Symmetry::Full,
    ///     ComputedRecipe {
    ///         submodel: modele.get(0)?,
    ///         fespaces: vec![zone.clone()],
    ///         material: Some(mat.get(0)?),
    ///         kind: MatrixKind::Stiffness,
    ///         inputs: KernelInputs::MaterialOnly,
    ///         col_fespaces: Vec::new(),
    ///     });
    /// assert!(z.recipe().is_some());
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    #[allow(clippy::too_many_arguments)]
    pub fn computed(
        row_support: Handle<SubMesh>,
        col_support: Handle<SubMesh>,
        dual_vars: Vec<String>,
        primal_vars: Vec<String>,
        ordering: DofOrdering,
        symmetry: Symmetry,
        recipe: ComputedRecipe,
    ) -> Self {
        // The block's row/col numbering *is* these supports' connectivity, read
        // in place on every access rather than copied; freeze them so it holds.
        crate::containers::mesh::seal(&row_support);
        crate::containers::mesh::seal(&col_support);
        let nrows = row_support.read().connectivity().len() * dual_vars.len();
        let ncols = col_support.read().connectivity().len() * primal_vars.len();
        Self {
            row_support,
            col_support,
            dual_vars,
            primal_vars,
            ordering,
            coo: CooMatrix::new(nrows, ncols),
            symmetry,
            recipe: Some(recipe),
            physics: Vec::new(),
            factor: 1.0,
        }
    }

    /// Build a block from an already-assembled COO whose indices are this
    /// block's **local** numbering (`(node_local, var_idx)` via `ordering`,
    /// nodes positioned as in `row_support` / `col_support`). Lets an assembler
    /// produce all entries in parallel and hand them over in one shot, bypassing
    /// the per-entry [`add_entry`](Self::add_entry).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, Matrix, SubMatrix, Symmetry};
    /// # use pyrucast::containers::mesh::SubMesh;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = {
    /// #     let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
    /// #     sm.add_cell(&[a.id()]).unwrap();
    /// #     sm.add_cell(&[b.id()]).unwrap();
    /// #     Handle::new(sm)
    /// # };
    /// # let bloc = || {
    /// #     let mut z = SubMatrix::new(
    /// #         support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #         DofOrdering::NodesThenVars, Symmetry::Full);
    /// #     z.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// #     z.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// #     z
    /// # };
    /// # use nalgebra_sparse::CooMatrix;
    /// // The assembler's path: produce every entry in parallel,
    /// // then hand them back in one go, without going through `add_entry`.
    /// let mut coo = CooMatrix::new(2, 2);
    /// coo.push(0, 0, 2.0);
    /// coo.push(1, 1, 2.0);
    /// let z = SubMatrix::from_coo(
    ///     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    ///     DofOrdering::NodesThenVars, Symmetry::Full, coo)?;
    /// assert_eq!(z.get(a.id(), "q", a.id(), "T"), 2.0);
    /// // Indices are **local** to the block, and its size must match.
    /// assert!(SubMatrix::from_coo(
    ///     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    ///     DofOrdering::NodesThenVars, Symmetry::Full, CooMatrix::new(3, 3)).is_err());
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    #[allow(clippy::too_many_arguments)]
    pub fn from_coo(
        row_support: Handle<SubMesh>,
        col_support: Handle<SubMesh>,
        dual_vars: Vec<String>,
        primal_vars: Vec<String>,
        ordering: DofOrdering,
        symmetry: Symmetry,
        coo: CooMatrix<f64>,
    ) -> Result<Self> {
        // The block's row/col numbering *is* these supports' connectivity, read
        // in place on every access rather than copied; freeze them so it holds.
        crate::containers::mesh::seal(&row_support);
        crate::containers::mesh::seal(&col_support);
        let nrows = row_support.read().connectivity().len() * dual_vars.len();
        let ncols = col_support.read().connectivity().len() * primal_vars.len();
        if coo.nrows() != nrows || coo.ncols() != ncols {
            return Err(PyrucastError::Message(format!(
                "from_coo: COO is {}×{} but the support/vars imply {}×{}",
                coo.nrows(),
                coo.ncols(),
                nrows,
                ncols
            )));
        }
        Ok(Self {
            row_support,
            col_support,
            dual_vars,
            primal_vars,
            ordering,
            coo,
            symmetry,
            recipe: None,
            physics: Vec::new(),
            factor: 1.0,
        })
    }

    /// Whether this is a **computed** block (carries a [`ComputedRecipe`], no
    /// stored values) rather than a literal one.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, SubMatrix, Symmetry};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::mesh;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = mesh::poi1_from_nodes(&[a.clone(), b.clone()]).unwrap().get(0).unwrap();
    /// # let mut bloc = SubMatrix::new(
    /// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #     DofOrdering::NodesThenVars, Symmetry::Full);
    /// # bloc.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// # bloc.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// // A hand-built block carries its values; a *computed* block carries
    /// // a recipe the assembler unwinds.
    /// assert!(!bloc.is_computed());
    /// ```
    pub fn is_computed(&self) -> bool {
        self.recipe.is_some()
    }

    /// The block's [`ComputedRecipe`], or `None` for a literal block.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, SubMatrix, Symmetry};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::mesh;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = mesh::poi1_from_nodes(&[a.clone(), b.clone()]).unwrap().get(0).unwrap();
    /// # let mut bloc = SubMatrix::new(
    /// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #     DofOrdering::NodesThenVars, Symmetry::Full);
    /// # bloc.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// # bloc.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// assert!(bloc.recipe().is_none()); // not a recipe: values
    /// ```
    pub fn recipe(&self) -> Option<&ComputedRecipe> {
        self.recipe.as_ref()
    }

    /// The set of [`Physics`] natures of the sub-model that produced this block —
    /// **empty** for a block built outside assembly (the « rien » case), one entry
    /// for a plain physics, several for a coupled one. Set by the assembler on
    /// every block it emits (see [`crate::ops::matrix`]).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, SubMatrix, Symmetry};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::mesh;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = mesh::poi1_from_nodes(&[a.clone(), b.clone()]).unwrap().get(0).unwrap();
    /// # let mut bloc = SubMatrix::new(
    /// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #     DofOrdering::NodesThenVars, Symmetry::Full);
    /// # bloc.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// # bloc.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// # use pyrucast::models::Physics;
    /// // A hand-built block has no kind: `Matrix::filter` will
    /// // select it by none. Tagging it `Other` makes it reachable.
    /// assert!(bloc.physics().is_empty());
    /// bloc.set_physics(vec![Physics::Other]);
    /// assert_eq!(bloc.physics(), &[Physics::Other]);
    /// ```
    pub fn physics(&self) -> &[Physics] {
        &self.physics
    }

    /// Tag this block with the [`Physics`] nature set of its producing sub-model —
    /// the assembler calls this on each emitted block so [`Matrix::filter`] can
    /// select by nature (matched by containment).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, Matrix, SubMatrix, Symmetry};
    /// # use pyrucast::containers::mesh::SubMesh;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = {
    /// #     let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
    /// #     sm.add_cell(&[a.id()]).unwrap();
    /// #     sm.add_cell(&[b.id()]).unwrap();
    /// #     Handle::new(sm)
    /// # };
    /// # let bloc = || {
    /// #     let mut z = SubMatrix::new(
    /// #         support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #         DofOrdering::NodesThenVars, Symmetry::Full);
    /// #     z.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// #     z.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// #     z
    /// # };
    /// # use pyrucast::models::Physics;
    /// // The kind travels with the block up to the assembled matrix:
    /// // this is what `Matrix::filter` reads.
    /// let mut z = bloc();
    /// z.set_physics(vec![Physics::Thermal]);
    /// assert_eq!(z.physics(), &[Physics::Thermal]);
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn set_physics(&mut self, physics: Vec<Physics>) {
        self.physics = physics;
    }

    /// The scalar factor applied to every value this block emits (`1.0` unless
    /// scaled via `Mul<f64>`/`Div<f64>`) — see the struct-level field doc for
    /// exactly where it is and isn't applied.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, SubMatrix, Symmetry};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::mesh;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = mesh::poi1_from_nodes(&[a.clone(), b.clone()]).unwrap().get(0).unwrap();
    /// # let mut bloc = SubMatrix::new(
    /// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #     DofOrdering::NodesThenVars, Symmetry::Full);
    /// # bloc.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// # bloc.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// // The block's scale factor, applied at assembly — what
    /// // `&matrix / dt` sets without rewriting a single value.
    /// assert_eq!(bloc.factor(), 1.0);
    /// ```
    pub fn factor(&self) -> f64 {
        self.factor
    }

    /// Whether the assembler declared this block numerically symmetric.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, SubMatrix, Symmetry};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::mesh;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = mesh::poi1_from_nodes(&[a.clone(), b.clone()]).unwrap().get(0).unwrap();
    /// # let mut bloc = SubMatrix::new(
    /// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #     DofOrdering::NodesThenVars, Symmetry::Full);
    /// # bloc.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// # bloc.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// assert!(bloc.is_symmetric()); // declared at construction
    /// ```
    pub fn is_symmetric(&self) -> bool {
        matches!(self.symmetry, Symmetry::Full)
    }

    /// What share of the matrix's symmetry this block declares.
    ///
    /// [`is_symmetric`](Self::is_symmetric) answers the yes/no question about
    /// this block alone; this one also tells a half from nothing, which only
    /// [`Matrix::symmetric`] can resolve.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, SubMatrix, Symmetry};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::mesh;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let support = mesh::poi1_from_nodes(&[a.clone()]).unwrap().get(0).unwrap();
    /// # let bloc = SubMatrix::new(
    /// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #     DofOrdering::NodesThenVars, Symmetry::Full);
    /// assert_eq!(bloc.symmetry(), Symmetry::Full);
    /// ```
    pub fn symmetry(&self) -> Symmetry {
        self.symmetry
    }

    /// Number of row DOFs = `n_row_nodes × n_dual_vars`.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, SubMatrix, Symmetry};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::mesh;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = mesh::poi1_from_nodes(&[a.clone(), b.clone()]).unwrap().get(0).unwrap();
    /// # let mut bloc = SubMatrix::new(
    /// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #     DofOrdering::NodesThenVars, Symmetry::Full);
    /// # bloc.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// # bloc.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// // nodes of the row support × dual variables.
    /// assert_eq!(bloc.n_rows(), 2);
    /// ```
    pub fn n_rows(&self) -> usize {
        self.coo.nrows()
    }

    /// Number of column DOFs = `n_col_nodes × n_primal_vars`.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, SubMatrix, Symmetry};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::mesh;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = mesh::poi1_from_nodes(&[a.clone(), b.clone()]).unwrap().get(0).unwrap();
    /// # let mut bloc = SubMatrix::new(
    /// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #     DofOrdering::NodesThenVars, Symmetry::Full);
    /// # bloc.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// # bloc.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// assert_eq!(bloc.n_cols(), 2);
    /// ```
    pub fn n_cols(&self) -> usize {
        self.coo.ncols()
    }

    /// Number of COO triplets stored (counting duplicates at the same
    /// `(row, col)`).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, SubMatrix, Symmetry};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::mesh;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = mesh::poi1_from_nodes(&[a.clone(), b.clone()]).unwrap().get(0).unwrap();
    /// # let mut bloc = SubMatrix::new(
    /// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #     DofOrdering::NodesThenVars, Symmetry::Full);
    /// # bloc.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// # bloc.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// assert_eq!(bloc.entry_count(), 4); // the four `add_entry` of the setup
    /// ```
    pub fn entry_count(&self) -> usize {
        self.coo.nnz()
    }

    /// Heap bytes this block holds, estimated: its COO entries, each a row
    /// index, a column index and a value.
    ///
    /// What it leaves out: the supports and the variable names, which are
    /// shared with the objects the block was built from — counting them here
    /// would count them once per block. A *computed* block stores no entry at
    /// all, so it answers `0`: its values are born at assembly time and live in
    /// the global CSR ([`Matrix::memory_bytes`]).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::Node;
    /// # use pyrucast::containers::matrix::{DofOrdering, SubMatrix, Symmetry};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::mesh;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = mesh::poi1_from_nodes(&[a.clone(), b.clone()]).unwrap().get(0).unwrap();
    /// # let mut bloc = SubMatrix::new(
    /// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #     DofOrdering::NodesThenVars, Symmetry::Full);
    /// let vide = bloc.memory_bytes();
    /// bloc.add_entry(a.id(), "q", b.id(), "T", -1.0)?;
    /// // One more entry: an index, an index and a value.
    /// assert_eq!(bloc.memory_bytes(), vide + 24);
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn memory_bytes(&self) -> usize {
        self.coo.nnz() * (2 * std::mem::size_of::<usize>() + std::mem::size_of::<f64>())
    }

    /// Row variable names (dual variables).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, SubMatrix, Symmetry};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::mesh;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = mesh::poi1_from_nodes(&[a.clone(), b.clone()]).unwrap().get(0).unwrap();
    /// # let mut bloc = SubMatrix::new(
    /// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #     DofOrdering::NodesThenVars, Symmetry::Full);
    /// # bloc.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// # bloc.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// assert_eq!(bloc.dual_vars(), &["q".to_string()]); // côté lignes
    /// ```
    pub fn dual_vars(&self) -> &[String] {
        &self.dual_vars
    }

    /// Column variable names (primal variables).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, SubMatrix, Symmetry};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::mesh;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = mesh::poi1_from_nodes(&[a.clone(), b.clone()]).unwrap().get(0).unwrap();
    /// # let mut bloc = SubMatrix::new(
    /// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #     DofOrdering::NodesThenVars, Symmetry::Full);
    /// # bloc.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// # bloc.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// assert_eq!(bloc.primal_vars(), &["T".to_string()]); // côté colonnes
    /// ```
    pub fn primal_vars(&self) -> &[String] {
        &self.primal_vars
    }

    /// DOF ordering strategy.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, SubMatrix, Symmetry};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::mesh;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = mesh::poi1_from_nodes(&[a.clone(), b.clone()]).unwrap().get(0).unwrap();
    /// # let mut bloc = SubMatrix::new(
    /// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #     DofOrdering::NodesThenVars, Symmetry::Full);
    /// # bloc.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// # bloc.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// // The DOF ordering inside the block: nodes first, or variables first.
    /// assert_eq!(bloc.ordering(), DofOrdering::NodesThenVars);
    /// ```
    pub fn ordering(&self) -> DofOrdering {
        self.ordering
    }

    /// Handle to the row-support POI1 sub-mesh.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, Matrix, SubMatrix, Symmetry};
    /// # use pyrucast::containers::mesh::SubMesh;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = {
    /// #     let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
    /// #     sm.add_cell(&[a.id()]).unwrap();
    /// #     sm.add_cell(&[b.id()]).unwrap();
    /// #     Handle::new(sm)
    /// # };
    /// # let bloc = || {
    /// #     let mut z = SubMatrix::new(
    /// #         support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #         DofOrdering::NodesThenVars, Symmetry::Full);
    /// #     z.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// #     z.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// #     z
    /// # };
    /// # use pyrucast::handle::Handle as H;
    /// // The **row** support — the dual variables live there.
    /// assert!(H::same_object(bloc().row_support(), &support));
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn row_support(&self) -> &Handle<SubMesh> {
        &self.row_support
    }

    /// Handle to the col-support POI1 sub-mesh.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, Matrix, SubMatrix, Symmetry};
    /// # use pyrucast::containers::mesh::SubMesh;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = {
    /// #     let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
    /// #     sm.add_cell(&[a.id()]).unwrap();
    /// #     sm.add_cell(&[b.id()]).unwrap();
    /// #     Handle::new(sm)
    /// # };
    /// # let bloc = || {
    /// #     let mut z = SubMatrix::new(
    /// #         support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #         DofOrdering::NodesThenVars, Symmetry::Full);
    /// #     z.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// #     z.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// #     z
    /// # };
    /// # use pyrucast::handle::Handle as H;
    /// // The **column** one. It differs from the former as soon as the block
    /// // couples two meshes — a Lagrange block, an interface law.
    /// assert!(H::same_object(bloc().col_support(), &support));
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn col_support(&self) -> &Handle<SubMesh> {
        &self.col_support
    }

    /// Union of `dual_vars` and `primal_vars`, in that order, without
    /// duplicates.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, SubMatrix, Symmetry};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::mesh;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = mesh::poi1_from_nodes(&[a.clone(), b.clone()]).unwrap().get(0).unwrap();
    /// # let mut bloc = SubMatrix::new(
    /// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #     DofOrdering::NodesThenVars, Symmetry::Full);
    /// # bloc.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// # bloc.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// // The union of both sets, deduplicated.
    /// assert_eq!(bloc.field_names(), vec!["q".to_string(), "T".to_string()]);
    /// ```
    pub fn field_names(&self) -> Vec<String> {
        let mut out = self.dual_vars.clone();
        for pv in &self.primal_vars {
            if !out.contains(pv) {
                out.push(pv.clone());
            }
        }
        out
    }

    /// All row DOFs in matrix-row order: `(NodeId, var_name)` for each
    /// row index `0..n_rows`.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, SubMatrix, Symmetry};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::mesh;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = mesh::poi1_from_nodes(&[a.clone(), b.clone()]).unwrap().get(0).unwrap();
    /// # let mut bloc = SubMatrix::new(
    /// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #     DofOrdering::NodesThenVars, Symmetry::Full);
    /// # bloc.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// # bloc.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// let lignes = bloc.row_dofs();
    /// assert_eq!(lignes.len(), bloc.n_rows());
    /// assert_eq!(lignes[0], (a.id(), "q".to_string()));
    /// ```
    pub fn row_dofs(&self) -> Vec<(NodeId, String)> {
        self.row_dofs_with(&self.row_support.read())
    }

    /// [`row_dofs`](Self::row_dofs) reading a support guard the caller already
    /// holds — the zero-copy form (see `book/src/developper/parallelisme.md`).
    pub(crate) fn row_dofs_with(&self, row_support: &SubMesh) -> Vec<(NodeId, String)> {
        let nodes = row_support.connectivity();
        let n_nodes = nodes.len();
        let n_vars = self.dual_vars.len();
        (0..self.coo.nrows())
            .map(|i| {
                let (nl, vi) = self.ordering.from_index(i, n_nodes, n_vars);
                (nodes[nl], self.dual_vars[vi].clone())
            })
            .collect()
    }

    /// All column DOFs in matrix-column order: `(NodeId, var_name)` for
    /// each column index `0..n_cols`.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, SubMatrix, Symmetry};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::mesh;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = mesh::poi1_from_nodes(&[a.clone(), b.clone()]).unwrap().get(0).unwrap();
    /// # let mut bloc = SubMatrix::new(
    /// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #     DofOrdering::NodesThenVars, Symmetry::Full);
    /// # bloc.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// # bloc.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// assert_eq!(bloc.col_dofs()[0], (a.id(), "T".to_string()));
    /// ```
    pub fn col_dofs(&self) -> Vec<(NodeId, String)> {
        self.col_dofs_with(&self.col_support.read())
    }

    /// [`col_dofs`](Self::col_dofs) reading a support guard the caller already
    /// holds.
    pub(crate) fn col_dofs_with(&self, col_support: &SubMesh) -> Vec<(NodeId, String)> {
        let nodes = col_support.connectivity();
        let n_nodes = nodes.len();
        let n_vars = self.primal_vars.len();
        (0..self.coo.ncols())
            .map(|i| {
                let (nl, vi) = self.ordering.from_index(i, n_nodes, n_vars);
                (nodes[nl], self.primal_vars[vi].clone())
            })
            .collect()
    }

    /// Row DOFs as packed [`DofKey`]s, in the same order as [`row_dofs`] —
    /// the allocation-free twin the assembler numbers with.
    ///
    /// `slot_of` names each variable by its index in the aggregate's table
    /// ([`Matrix::dof_vars`]); it is resolved **once per block**, so no DOF ever
    /// touches a string.
    pub(crate) fn row_dof_keys(&self, slot_of: &HashMap<String, u32>) -> Vec<DofKey> {
        self.row_dof_keys_with(&self.row_support.read(), slot_of)
    }

    /// [`row_dof_keys`](Self::row_dof_keys) reading a support guard the caller
    /// already holds — the zero-copy form (see `book/src/developper/parallelisme.md`).
    pub(crate) fn row_dof_keys_with(
        &self,
        row_support: &SubMesh,
        slot_of: &HashMap<String, u32>,
    ) -> Vec<DofKey> {
        Self::keys(
            row_support.connectivity(),
            &self.var_slots(&self.dual_vars, slot_of),
            self.ordering,
            self.coo.nrows(),
        )
    }

    /// Column DOFs as packed [`DofKey`]s — the column twin of
    /// [`row_dof_keys`](Self::row_dof_keys).
    pub(crate) fn col_dof_keys(&self, slot_of: &HashMap<String, u32>) -> Vec<DofKey> {
        self.col_dof_keys_with(&self.col_support.read(), slot_of)
    }

    /// [`col_dof_keys`](Self::col_dof_keys) reading a support guard the caller
    /// already holds.
    pub(crate) fn col_dof_keys_with(
        &self,
        col_support: &SubMesh,
        slot_of: &HashMap<String, u32>,
    ) -> Vec<DofKey> {
        Self::keys(
            col_support.connectivity(),
            &self.var_slots(&self.primal_vars, slot_of),
            self.ordering,
            self.coo.ncols(),
        )
    }

    /// This block's variable names as slots in the aggregate's table.
    ///
    /// Infallible by construction: `slot_of` is always built from the
    /// aggregate's own [`field_names`](Matrix::field_names), which is the union
    /// of every block's dual and primal variables — so a block looks its own
    /// names up in a table that was made to contain them. A miss would mean the
    /// caller built the table from something else, which is a bug here and not
    /// a condition a user can produce.
    fn var_slots(&self, vars: &[String], slot_of: &HashMap<String, u32>) -> Vec<u32> {
        vars.iter()
            .map(|v| {
                *slot_of.get(v).unwrap_or_else(|| {
                    panic!("variable '{v}' is absent from the matrix name table")
                })
            })
            .collect()
    }

    /// `n` DOF keys in matrix order, from a node list and its variables' slots.
    fn keys(nodes: &[NodeId], slots: &[u32], ordering: DofOrdering, n: usize) -> Vec<DofKey> {
        let (n_nodes, n_vars) = (nodes.len(), slots.len());
        (0..n)
            .map(|i| {
                let (nl, vi) = ordering.from_index(i, n_nodes, n_vars);
                dof_key(nodes[nl], slots[vi])
            })
            .collect()
    }

    /// Append an entry at `(row_node, row_var) × (col_node, col_var)`.
    ///
    /// Returns an error if either `row_node` / `col_node` is not in its
    /// respective support, or if the variable name is not in the
    /// `dual_vars` / `primal_vars` list.  Repeated calls at the same
    /// `(row, col)` accumulate.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, Matrix, SubMatrix, Symmetry};
    /// # use pyrucast::containers::mesh::SubMesh;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = {
    /// #     let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
    /// #     sm.add_cell(&[a.id()]).unwrap();
    /// #     sm.add_cell(&[b.id()]).unwrap();
    /// #     Handle::new(sm)
    /// # };
    /// # let bloc = || {
    /// #     let mut z = SubMatrix::new(
    /// #         support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #         DofOrdering::NodesThenVars, Symmetry::Full);
    /// #     z.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// #     z.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// #     z
    /// # };
    /// let mut z = SubMatrix::new(
    ///     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    ///     DofOrdering::NodesThenVars, Symmetry::Full);
    /// // Two calls at the same place **accumulate** — that is what lets the
    /// // assembly pour cell by cell without reading anything back.
    /// z.add_entry(a.id(), "q", a.id(), "T", 1.0)?;
    /// z.add_entry(a.id(), "q", a.id(), "T", 1.0)?;
    /// assert_eq!(z.get(a.id(), "q", a.id(), "T"), 2.0);
    /// // A node outside the support, or an undeclared variable, is an error.
    /// assert!(z.add_entry(a.id(), "f_x", a.id(), "T", 1.0).is_err());
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn add_entry(
        &mut self,
        row_node: NodeId,
        row_var: &str,
        col_node: NodeId,
        col_var: &str,
        value: f64,
    ) -> Result<()> {
        // The supports' own `NodeId → position` tables, borrowed in place. Rows
        // and columns usually share one handle, so ask that lock only once.
        // The computed-block refusal lives in `add_entry_with`, tested once here
        // rather than twice.
        let (row_g, col_g) = self.support_guards();
        let row = &row_g;
        let col = col_g.as_deref().unwrap_or(&row_g);
        self.add_entry_with(row, col, row_node, row_var, col_node, col_var, value)
    }

    /// [`add_entry`](Self::add_entry) reading support guards the caller already
    /// holds — the zero-copy form (see `book/src/developper/parallelisme.md`).
    /// Pass the same reference twice for a square block.
    ///
    /// A caller filling a block in a loop should hold the guards once and use
    /// this, rather than let every entry re-lock the supports.
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn add_entry_with(
        &mut self,
        row_support: &SubMesh,
        col_support: &SubMesh,
        row_node: NodeId,
        row_var: &str,
        col_node: NodeId,
        col_var: &str,
        value: f64,
    ) -> Result<()> {
        if self.is_computed() {
            return Err(PyrucastError::Message(
                "add_entry: this is a computed block (its values come from its \
                 recipe at assembly time); literal entries cannot be added"
                    .into(),
            ));
        }
        let n_rn = row_support.connectivity().len();
        let n_dv = self.dual_vars.len();
        let n_cn = col_support.connectivity().len();
        let n_pv = self.primal_vars.len();

        let rnl = *row_support.node_index().get(&row_node).ok_or_else(|| {
            PyrucastError::Message(format!(
                "add_entry: row node {row_node:?} not in row_support"
            ))
        })?;
        let rvi = self
            .dual_vars
            .iter()
            .position(|v| v == row_var)
            .ok_or_else(|| {
                PyrucastError::Message(format!("add_entry: row var '{row_var}' not in dual_vars"))
            })?;
        let cnl = *col_support.node_index().get(&col_node).ok_or_else(|| {
            PyrucastError::Message(format!(
                "add_entry: col node {col_node:?} not in col_support"
            ))
        })?;
        let cvi = self
            .primal_vars
            .iter()
            .position(|v| v == col_var)
            .ok_or_else(|| {
                PyrucastError::Message(format!("add_entry: col var '{col_var}' not in primal_vars"))
            })?;

        let ri = self.ordering.to_index(rnl, rvi, n_rn, n_dv);
        let ci = self.ordering.to_index(cnl, cvi, n_cn, n_pv);
        self.coo.push(ri, ci, value);
        Ok(())
    }

    /// COO entries in **local** index form `(row, col, value)` — the block's own
    /// numbering. Used by the aggregate to scatter into the global matrix via a
    /// per-block translation table. **Not** scaled by [`SubMatrix::factor`]: the
    /// caller applies it (every consumer inside `containers`/`ops::matrix` does).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, Matrix, SubMatrix, Symmetry};
    /// # use pyrucast::containers::mesh::SubMesh;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = {
    /// #     let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
    /// #     sm.add_cell(&[a.id()]).unwrap();
    /// #     sm.add_cell(&[b.id()]).unwrap();
    /// #     Handle::new(sm)
    /// # };
    /// # let bloc = || {
    /// #     let mut z = SubMatrix::new(
    /// #         support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #         DofOrdering::NodesThenVars, Symmetry::Full);
    /// #     z.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// #     z.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// #     z
    /// # };
    /// // The entries in **local** numbering, as the aggregate
    /// // pours into the global matrix through its translation table.
    /// let z = bloc();
    /// let t: Vec<_> = z.local_triplets().collect();
    /// assert_eq!(t.len(), 4);
    /// assert_eq!(t[0], (0, 0, 2.0));
    /// // They do **not** carry the factor: the caller applies it.
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn local_triplets(&self) -> impl Iterator<Item = (usize, usize, f64)> + '_ {
        self.coo.triplet_iter().map(|(r, c, &v)| (r, c, v))
    }

    /// The block's COO as raw parallel slices `(rows, cols, values)`, in
    /// **local** index form. Same data as [`local_triplets`](Self::local_triplets)
    /// but indexable, so the aggregate can remap the entries in parallel. **Not**
    /// scaled by [`SubMatrix::factor`] — see [`local_triplets`](Self::local_triplets).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, SubMatrix, Symmetry};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::mesh;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = mesh::poi1_from_nodes(&[a.clone(), b.clone()]).unwrap().get(0).unwrap();
    /// # let mut bloc = SubMatrix::new(
    /// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #     DofOrdering::NodesThenVars, Symmetry::Full);
    /// # bloc.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// # bloc.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// // The three COO arrays **local** to the block, as the assembler
    /// // scatters into the global pattern.
    /// let (lignes, colonnes, valeurs) = bloc.local_coo_arrays();
    /// assert_eq!((lignes.len(), colonnes.len(), valeurs.len()), (4, 4, 4));
    /// ```
    pub fn local_coo_arrays(&self) -> (&[usize], &[usize], &[f64]) {
        (
            self.coo.row_indices(),
            self.coo.col_indices(),
            self.coo.values(),
        )
    }

    /// Handle to the `Coords` backing this block's row support (the col support
    /// shares it in any assembled system).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, SubMatrix, Symmetry};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::mesh;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = mesh::poi1_from_nodes(&[a.clone(), b.clone()]).unwrap().get(0).unwrap();
    /// # let mut bloc = SubMatrix::new(
    /// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #     DofOrdering::NodesThenVars, Symmetry::Full);
    /// # bloc.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// # bloc.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// # use pyrucast::handle::Handle as H;
    /// assert!(H::same_object(&bloc.coords(), &coords));
    /// ```
    pub fn coords(&self) -> Handle<Coords> {
        self.row_support.read().coords()
    }

    /// Sum of all entries at `(row_node, row_var) × (col_node, col_var)`.
    /// Returns `0.0` if the DOF pair is unknown or has no entry.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, SubMatrix, Symmetry};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::mesh;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = mesh::poi1_from_nodes(&[a.clone(), b.clone()]).unwrap().get(0).unwrap();
    /// # let mut bloc = SubMatrix::new(
    /// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #     DofOrdering::NodesThenVars, Symmetry::Full);
    /// # bloc.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// # bloc.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// assert_eq!(bloc.get(a.id(), "q", b.id(), "T"), -1.0);
    /// // An absent coordinate returns **zero**, not an error: reading
    /// // a sparse block does not tell "zero" from "outside the pattern".
    /// assert_eq!(bloc.get(a.id(), "q", a.id(), "absente"), 0.0);
    /// ```
    pub fn get(&self, row_node: NodeId, row_var: &str, col_node: NodeId, col_var: &str) -> f64 {
        let (row_g, col_g) = self.support_guards();
        self.get_with(
            &row_g,
            col_g.as_deref().unwrap_or(&row_g),
            row_node,
            row_var,
            col_node,
            col_var,
        )
    }

    /// Read guards on both supports, taking a **single** one when rows and
    /// columns share the handle — the square case every physics produces.
    /// Asking the same `RwLock` twice in one thread is what this avoids.
    fn support_guards(&self) -> (ReadGuard<SubMesh>, Option<ReadGuard<SubMesh>>) {
        let row = self.row_support.read();
        let col = if self.col_support.same_object(&self.row_support) {
            None
        } else {
            Some(self.col_support.read())
        };
        (row, col)
    }

    /// [`get`](Self::get) reading support guards the caller already holds — the
    /// zero-copy form (see `book/src/developper/parallelisme.md`). Pass the same
    /// reference twice when the block is square on one support.
    pub(crate) fn get_with(
        &self,
        row_support: &SubMesh,
        col_support: &SubMesh,
        row_node: NodeId,
        row_var: &str,
        col_node: NodeId,
        col_var: &str,
    ) -> f64 {
        let n_rn = row_support.connectivity().len();
        let n_dv = self.dual_vars.len();
        let n_cn = col_support.connectivity().len();
        let n_pv = self.primal_vars.len();

        // `node_index` is the support's own `NodeId → position` table, built
        // once and shared by every consumer — first occurrence wins, as the
        // linear `position` scan this replaces did.
        let rnl = match row_support.node_index().get(&row_node) {
            Some(&i) => i,
            None => return 0.0,
        };
        let rvi = match self.dual_vars.iter().position(|v| v == row_var) {
            Some(i) => i,
            None => return 0.0,
        };
        let cnl = match col_support.node_index().get(&col_node) {
            Some(&i) => i,
            None => return 0.0,
        };
        let cvi = match self.primal_vars.iter().position(|v| v == col_var) {
            Some(i) => i,
            None => return 0.0,
        };

        let ri = self.ordering.to_index(rnl, rvi, n_rn, n_dv);
        let ci = self.ordering.to_index(cnl, cvi, n_cn, n_pv);

        let raw: f64 = self
            .coo
            .row_indices()
            .iter()
            .zip(self.coo.col_indices())
            .zip(self.coo.values())
            .filter(|&((&r, &c), _)| r == ri && c == ci)
            .map(|(_, &v)| v)
            .sum();
        raw * self.factor
    }

    /// All COO triplets, in insertion order, with DOFs materialised as
    /// `(NodeId, var_name)` pairs.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, SubMatrix, Symmetry};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::mesh;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = mesh::poi1_from_nodes(&[a.clone(), b.clone()]).unwrap().get(0).unwrap();
    /// # let mut bloc = SubMatrix::new(
    /// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #     DofOrdering::NodesThenVars, Symmetry::Full);
    /// # bloc.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// # bloc.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// // Insertion order preserved; variable names already resolved.
    /// assert_eq!(bloc.iter_entries().len(), 4);
    /// assert_eq!(bloc.iter_entries()[0].4, 2.0);
    /// ```
    pub fn iter_entries(&self) -> Vec<MatrixEntry> {
        let (row_g, col_g) = self.support_guards();
        self.iter_entries_with(&row_g, col_g.as_deref().unwrap_or(&row_g))
    }

    /// [`iter_entries`](Self::iter_entries) reading support guards the caller
    /// already holds. Pass the same reference twice for a square block.
    pub(crate) fn iter_entries_with(
        &self,
        row_support: &SubMesh,
        col_support: &SubMesh,
    ) -> Vec<MatrixEntry> {
        let row_nodes = row_support.connectivity();
        let col_nodes = col_support.connectivity();
        let n_rn = row_nodes.len();
        let n_dv = self.dual_vars.len();
        let n_cn = col_nodes.len();
        let n_pv = self.primal_vars.len();

        self.coo
            .row_indices()
            .iter()
            .zip(self.coo.col_indices())
            .zip(self.coo.values())
            .map(|((&ri, &ci), &v)| {
                let (rnl, rvi) = self.ordering.from_index(ri, n_rn, n_dv);
                let (cnl, cvi) = self.ordering.from_index(ci, n_cn, n_pv);
                (
                    row_nodes[rnl],
                    self.dual_vars[rvi].clone(),
                    col_nodes[cnl],
                    self.primal_vars[cvi].clone(),
                    v * self.factor,
                )
            })
            .collect()
    }

    /// Materialise as a row-major dense buffer of length `n_rows × n_cols`.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, SubMatrix, Symmetry};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::mesh;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = mesh::poi1_from_nodes(&[a.clone(), b.clone()]).unwrap().get(0).unwrap();
    /// # let mut bloc = SubMatrix::new(
    /// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #     DofOrdering::NodesThenVars, Symmetry::Full);
    /// # bloc.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// # bloc.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// assert_eq!(bloc.dense(), vec![2.0, -1.0, -1.0, 2.0]); // ligne-major
    /// ```
    pub fn dense(&self) -> Vec<f64> {
        let m = self.to_dmatrix();
        let mut out = Vec::with_capacity(m.nrows() * m.ncols());
        for i in 0..m.nrows() {
            for j in 0..m.ncols() {
                out.push(m[(i, j)]);
            }
        }
        out
    }

    /// Materialise as a [`nalgebra::DMatrix<f64>`]. Entries at the same
    /// `(row, col)` are summed.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, SubMatrix, Symmetry};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::mesh;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = mesh::poi1_from_nodes(&[a.clone(), b.clone()]).unwrap().get(0).unwrap();
    /// # let mut bloc = SubMatrix::new(
    /// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #     DofOrdering::NodesThenVars, Symmetry::Full);
    /// # bloc.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// # bloc.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// assert_eq!(bloc.to_dmatrix()[(0, 1)], -1.0);
    /// ```
    pub fn to_dmatrix(&self) -> DMatrix<f64> {
        let nr = self.coo.nrows();
        let nc = self.coo.ncols();
        let mut out = DMatrix::<f64>::zeros(nr, nc);
        for ((&r, &c), &v) in self
            .coo
            .row_indices()
            .iter()
            .zip(self.coo.col_indices())
            .zip(self.coo.values())
        {
            out[(r, c)] += v * self.factor;
        }
        out
    }

    /// The internal COO matrix, with [`SubMatrix::factor`] baked in (cloned as-is
    /// when the factor is `1.0`, rebuilt with scaled values otherwise).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, SubMatrix, Symmetry};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::mesh;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = mesh::poi1_from_nodes(&[a.clone(), b.clone()]).unwrap().get(0).unwrap();
    /// # let mut bloc = SubMatrix::new(
    /// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #     DofOrdering::NodesThenVars, Symmetry::Full);
    /// # bloc.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// # bloc.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// assert_eq!(bloc.to_coo().nnz(), 4);
    /// ```
    pub fn to_coo(&self) -> CooMatrix<f64> {
        if self.factor == 1.0 {
            return self.coo.clone();
        }
        let mut out = CooMatrix::new(self.coo.nrows(), self.coo.ncols());
        for (r, c, &v) in self.coo.triplet_iter() {
            out.push(r, c, v * self.factor);
        }
        out
    }

    /// Convert this block to a [`nalgebra_sparse::CsrMatrix`] (factor applied).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, SubMatrix, Symmetry};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::mesh;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = mesh::poi1_from_nodes(&[a.clone(), b.clone()]).unwrap().get(0).unwrap();
    /// # let mut bloc = SubMatrix::new(
    /// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #     DofOrdering::NodesThenVars, Symmetry::Full);
    /// # bloc.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// # bloc.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// assert_eq!(bloc.to_csr().nnz(), 4);
    /// ```
    pub fn to_csr(&self) -> CsrMatrix<f64> {
        CsrMatrix::from(&self.to_coo())
    }

    /// Convert this block to a [`nalgebra_sparse::CscMatrix`] (factor applied).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, SubMatrix, Symmetry};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::mesh;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = mesh::poi1_from_nodes(&[a.clone(), b.clone()]).unwrap().get(0).unwrap();
    /// # let mut bloc = SubMatrix::new(
    /// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #     DofOrdering::NodesThenVars, Symmetry::Full);
    /// # bloc.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// # bloc.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// assert_eq!(bloc.to_csc().nrows(), 2);
    /// ```
    pub fn to_csc(&self) -> CscMatrix<f64> {
        CscMatrix::from(&self.to_coo())
    }

    /// `y = A · x` (dense). Returns an error if `x.len() != n_cols`.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, SubMatrix, Symmetry};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::mesh;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = mesh::poi1_from_nodes(&[a.clone(), b.clone()]).unwrap().get(0).unwrap();
    /// # let mut bloc = SubMatrix::new(
    /// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #     DofOrdering::NodesThenVars, Symmetry::Full);
    /// # bloc.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// # bloc.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// # bloc.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// // [[2, -1], [-1, 2]] · [1, 1] = [1, 1].
    /// assert_eq!(bloc.mul_dense(&[1.0, 1.0]).unwrap(), vec![1.0, 1.0]);
    /// ```
    pub fn mul_dense(&self, x: &[f64]) -> Result<Vec<f64>> {
        if x.len() != self.n_cols() {
            return Err(PyrucastError::Message(format!(
                "mul_dense: x has length {} but sub-matrix has {} columns",
                x.len(),
                self.n_cols()
            )));
        }
        let csr = self.to_csr();
        let x_vec = DVector::<f64>::from_column_slice(x);
        let y_vec: DVector<f64> = &csr * &x_vec;
        Ok(y_vec.iter().copied().collect())
    }
}

/// Reject a divisor that no matrix can survive.
///
/// A scalar operator yields the value type — as it does for every number in
/// `std` — so `Div<f64>` has no fallible form to return. A divisor that would
/// make every value non-finite therefore stops here, rather than seeding an
/// `inf` that surfaces as a `NaN` inside the solver several calls later. The
/// Python surface, where a zero can actually arrive from user data, raises
/// `ZeroDivisionError` instead (`crate::py::matrix`).
#[track_caller]
fn check_divisor(rhs: f64) -> f64 {
    assert!(
        rhs != 0.0 && rhs.is_finite(),
        "matrix division by {rhs}: every value of the result would be non-finite"
    );
    rhs
}

// ─── SubMatrix scalar operators ─────────────────────────────────────────────
//
// `blk * s` / `blk / s` / `-blk` only touch `factor` — never the stored `coo` —
// so they are zero-copy and work identically for a literal block (values live
// in `coo`) and a computed one (values don't exist until assembly evaluates the
// recipe). No `Add`/`Sub<f64>`: shifting a matrix by a constant has no physical
// meaning. Block **plus** block, on the other hand, is a sum of contributions
// and yields a two-block `Matrix` — see the aggregate operators.

impl std::ops::Mul<f64> for SubMatrix {
    type Output = SubMatrix;
    fn mul(mut self, rhs: f64) -> SubMatrix {
        self.factor *= rhs;
        self
    }
}

impl std::ops::Mul<f64> for &SubMatrix {
    type Output = SubMatrix;
    fn mul(self, rhs: f64) -> SubMatrix {
        self.clone() * rhs
    }
}

// Scalar on the left — `2.0 * blk` reads as the mathematics does. `f64` is a
// foreign type, so these live here rather than as a blanket impl.
impl std::ops::Mul<SubMatrix> for f64 {
    type Output = SubMatrix;
    fn mul(self, rhs: SubMatrix) -> SubMatrix {
        rhs * self
    }
}

impl std::ops::Mul<&SubMatrix> for f64 {
    type Output = SubMatrix;
    fn mul(self, rhs: &SubMatrix) -> SubMatrix {
        rhs * self
    }
}

impl std::ops::Div<f64> for SubMatrix {
    type Output = SubMatrix;
    #[track_caller]
    fn div(mut self, rhs: f64) -> SubMatrix {
        self.factor /= check_divisor(rhs);
        self
    }
}

impl std::ops::Div<f64> for &SubMatrix {
    type Output = SubMatrix;
    #[track_caller]
    fn div(self, rhs: f64) -> SubMatrix {
        self.clone() / rhs
    }
}

impl std::ops::Neg for SubMatrix {
    type Output = SubMatrix;
    fn neg(self) -> SubMatrix {
        self * -1.0
    }
}

impl std::ops::Neg for &SubMatrix {
    type Output = SubMatrix;
    fn neg(self) -> SubMatrix {
        self * -1.0
    }
}

/// `b` bytes, in the largest unit that keeps the number short — the estimates
/// of [`SubMatrix::memory_bytes`] and [`Matrix::memory_bytes`] are shown this
/// way, a raw byte count of a big matrix being unreadable.
fn human_bytes(b: usize) -> String {
    const UNITS: [&str; 5] = ["B", "kB", "MB", "GB", "TB"];
    let mut v = b as f64;
    let mut u = 0;
    while v >= 1024.0 && u + 1 < UNITS.len() {
        v /= 1024.0;
        u += 1;
    }
    if u == 0 {
        format!("{b} B")
    } else {
        format!("{v:.1} {}", UNITS[u])
    }
}

impl fmt::Debug for SubMatrix {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SubMatrix")
            .field("n_rows", &self.coo.nrows())
            .field("n_cols", &self.coo.ncols())
            .field("entries", &self.coo.nnz())
            .field(
                "memory",
                &crate::aggregate::Unquoted(&human_bytes(self.memory_bytes())),
            )
            .field("symmetry", &self.symmetry)
            .field("dual_vars", &self.dual_vars)
            .field("primal_vars", &self.primal_vars)
            .field("ordering", &self.ordering)
            .field("factor", &self.factor)
            .finish()
    }
}

impl fmt::Display for SubMatrix {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // Literal blocks render exactly as before; computed blocks have no
        // stored entries, so report their recipe instead of an entry count.
        let entries: std::borrow::Cow<str> = if self.is_computed() {
            "computed (values from recipe)".into()
        } else {
            format!("{} entries", self.coo.nnz()).into()
        };
        let physics: std::borrow::Cow<str> = if self.physics.is_empty() {
            "".into()
        } else {
            let tags: Vec<&str> = self.physics.iter().map(|p| p.name()).collect();
            format!(", {}", tags.join("+")).into()
        };
        let factor: std::borrow::Cow<str> = if self.factor == 1.0 {
            "".into()
        } else {
            format!(", ×{}", self.factor).into()
        };
        write!(
            f,
            "SubMatrix: {} row(s) × {} col(s), {}{}{}{}",
            self.coo.nrows(),
            self.coo.ncols(),
            entries,
            if self.is_symmetric() {
                ", symmetric"
            } else {
                ""
            },
            physics,
            factor,
        )
    }
}

/// Format a DOF `(node, var)` pair as the grid label `(node,var)`.
fn dof_label((n, v): &NamedDof) -> String {
    format!("({n},{v})")
}

impl crate::dump::Dump for SubMatrix {
    fn render(&self, opts: &crate::dump::DumpOptions) -> String {
        // A computed block holds no values — show its structure, not a grid of
        // zeros. The recipe's handles identify its sub-model / FE subspace.
        if self.is_computed() {
            let recipe = self.recipe.as_ref().expect("is_computed ⇒ recipe");
            return format!(
                "{self}\n  recipe: submodel {:?}, fespaces {:?}{}\n  dual_vars: [{}]\n  primal_vars: [{}]",
                recipe.submodel,
                recipe.fespaces,
                recipe
                    .material
                    .as_ref()
                    .map(|m| format!(", material {m:?}"))
                    .unwrap_or_default(),
                self.dual_vars.join(", "),
                self.primal_vars.join(", "),
            );
        }
        let row_labels: Vec<String> = self.row_dofs().iter().map(dof_label).collect();
        let col_labels: Vec<String> = self.col_dofs().iter().map(dof_label).collect();
        let data = self.dense();
        // What `Display` does not say — the DOF ordering decides how to read the
        // grid that follows, and the factor decides its values. Without this
        // line, the "content" level would teach less than the structure does.
        format!(
            "{self}\n  symmetry: {}, ordering: {:?}, factor: {:?}\n  dual_vars: [{}], primal_vars: [{}]\n{}",
            crate::named::Named::name(self.symmetry),
            self.ordering,
            self.factor,
            self.dual_vars.join(", "),
            self.primal_vars.join(", "),
            crate::dump::labeled_grid(&row_labels, &col_labels, &data, opts)
        )
    }
}

// ─── CooMatrix serde ───────────────────────────────────────────────────────

mod coo_serde {
    use nalgebra_sparse::CooMatrix;
    use serde::{Deserialize, Deserializer, Serialize, Serializer};

    #[derive(Serialize, Deserialize)]
    struct CooData {
        nrows: usize,
        ncols: usize,
        row_indices: Vec<usize>,
        col_indices: Vec<usize>,
        values: Vec<f64>,
    }

    pub fn serialize<S: Serializer>(
        coo: &CooMatrix<f64>,
        s: S,
    ) -> std::result::Result<S::Ok, S::Error> {
        CooData {
            nrows: coo.nrows(),
            ncols: coo.ncols(),
            row_indices: coo.row_indices().to_vec(),
            col_indices: coo.col_indices().to_vec(),
            values: coo.values().to_vec(),
        }
        .serialize(s)
    }

    pub fn deserialize<'de, D: Deserializer<'de>>(
        d: D,
    ) -> std::result::Result<CooMatrix<f64>, D::Error> {
        let data = CooData::deserialize(d)?;
        let mut coo = CooMatrix::new(data.nrows, data.ncols);
        for ((r, c), v) in data
            .row_indices
            .into_iter()
            .zip(data.col_indices)
            .zip(data.values)
        {
            coo.push(r, c, v);
        }
        Ok(coo)
    }
}

// ─── Matrix (aggregate) ────────────────────────────────────────────────────

/// The assembled CSR, its **index arrays shared** with whatever produced them.
///
/// A `CsrMatrix` owns its three arrays, so storing one here meant copying the
/// sparsity out of the assembler's (already shared, already memoised) pattern at
/// every assembly — twenty gigabytes of `memcpy` on a solid mesh, to reproduce
/// something that had not changed. Only the values are new from one assembly to
/// the next; the offsets and the column indices are the pattern's, held by
/// `Arc`. A `CsrMatrix` is built on demand, where one is actually wanted.
pub(crate) struct AssembledCsr {
    pub(crate) row_offsets: std::sync::Arc<Vec<usize>>,
    pub(crate) col_indices: std::sync::Arc<Vec<usize>>,
    pub(crate) values: Vec<f64>,
    pub(crate) ncols: usize,
}

impl AssembledCsr {
    fn nrows(&self) -> usize {
        self.row_offsets.len().saturating_sub(1)
    }

    /// Slot of `(r, c)` in `values`, or `None` when the pattern has no such
    /// entry. The columns of a row are sorted, so this is a binary search.
    fn slot(&self, r: usize, c: usize) -> Option<usize> {
        let (lo, hi) = (*self.row_offsets.get(r)?, *self.row_offsets.get(r + 1)?);
        self.col_indices[lo..hi]
            .binary_search(&c)
            .ok()
            .map(|k| lo + k)
    }

    /// Every stored entry as `(row, col, value)`, in CSR order.
    fn triplets(&self) -> impl Iterator<Item = (usize, usize, f64)> + '_ {
        (0..self.nrows()).flat_map(move |r| {
            let (lo, hi) = (self.row_offsets[r], self.row_offsets[r + 1]);
            (lo..hi).map(move |k| (r, self.col_indices[k], self.values[k]))
        })
    }

    /// Materialise a `CsrMatrix` — the one place the index arrays are copied,
    /// and only for a caller that asked for that type.
    fn to_csr(&self) -> Result<CsrMatrix<f64>> {
        CsrMatrix::try_from_csr_data(
            self.nrows(),
            self.ncols,
            (*self.row_offsets).clone(),
            (*self.col_indices).clone(),
            self.values.clone(),
        )
        .map_err(|e| PyrucastError::Message(format!("to_csr: invalid CSR: {e}")))
    }
}

/// Snapshot produced by [`Matrix::finalize`]: DOF numbering + assembled CSR.
///
/// The numbering is held as packed [`DofKey`]s over a shared name table, not as
/// a `Vec<(NodeId, String)>`: on a solid mesh the materialised form is thirty
/// million `String`s, rebuilt at every assembly, for a handful of distinct
/// names.
struct AssembledData {
    vars: std::sync::Arc<Vec<String>>,
    /// Shared with the pattern that produced them, like the sparsity beside
    /// them: the numbering is the pattern's, and an assembled matrix borrows it
    /// rather than copying eight bytes per degree of freedom out of it at every
    /// assembly — twice, rows and columns.
    row_keys: std::sync::Arc<Vec<DofKey>>,
    col_keys: std::sync::Arc<Vec<DofKey>>,
    csr: AssembledCsr,
}

/// Aggregate of [`SubMatrix`] blocks.
///
/// Call [`Matrix::finalize`] before passing to a solver. Solver-facing methods
/// (`to_csr`, `to_dmatrix`, `mul_dense`, `dense`, `to_coo`, `to_csc`) return
/// an error if the matrix has not been finalized. `add_sub` invalidates the
/// assembled state.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::matrix::{DofOrdering, Matrix, SubMatrix, Symmetry};
/// # use pyrucast::containers::mesh::SubMesh;
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # let coords = Handle::new(Coords::new(1).unwrap());
/// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
/// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
/// # let support = {
/// #     let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
/// #     sm.add_cell(&[a.id()]).unwrap();
/// #     sm.add_cell(&[b.id()]).unwrap();
/// #     Handle::new(sm)
/// # };
/// # let mut bloc = SubMatrix::new(
/// #     support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
/// #     DofOrdering::NodesThenVars, Symmetry::Full);
/// # bloc.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
/// # bloc.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
/// // The aggregate: blocks, a global numbering, an assembled state.
/// let mut k = Matrix::empty();
/// k.add_sub(Handle::new(bloc))?;
/// k.finalize()?;
/// assert_eq!((k.n_rows()?, k.n_cols()?), (2, 2));
/// assert_eq!(k.to_csr()?.nnz(), 2);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
#[derive(Serialize, Deserialize, Default)]
pub struct Matrix {
    subs: Vec<Handle<SubMatrix>>,
    #[serde(skip)]
    assembled: Option<AssembledData>,
    /// Transparently cached factorization (e.g. the solver's sparse LU), reused
    /// across solves on the same matrix. Derived state: never serialized,
    /// type-erased so `containers` stays decoupled from the solver, and cleared
    /// whenever the matrix changes (`add_sub` → `post_push`). Interior mutability
    /// so `solve(&Matrix)` can fill it under a shared store read lock.
    #[serde(skip)]
    factorization: parking_lot::Mutex<Option<std::sync::Arc<dyn std::any::Any + Send + Sync>>>,
}

crate::impl_aggregate!(Matrix, SubMatrix, sub_matrix, "sub-matrix(es)", {
    fn post_push(&mut self) {
        self.assembled = None;
        // The matrix content changed ⇒ any cached factorization is stale.
        *self.factorization.get_mut() = None;
    }
    fn display_extra(&self) -> Option<String> {
        let n_rows = self.n_rows().unwrap_or(0);
        let n_cols = self.n_cols().unwrap_or(0);
        let sym = self.symmetric();
        Some(format!(
            ", {} row(s) × {} col(s){}, ~{}",
            n_rows,
            n_cols,
            if sym { ", symmetric" } else { "" },
            human_bytes(self.memory_bytes())
        ))
    }
});

/// One row or column DOF of an aggregate [`Matrix`], in materialised form.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::matrix::{DofOrdering, Matrix, SubMatrix, Symmetry};
/// # use pyrucast::containers::mesh::SubMesh;
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # let coords = Handle::new(Coords::new(1).unwrap());
/// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
/// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
/// # let support = {
/// #     let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
/// #     sm.add_cell(&[a.id()]).unwrap();
/// #     sm.add_cell(&[b.id()]).unwrap();
/// #     Handle::new(sm)
/// # };
/// # let bloc = || {
/// #     let mut z = SubMatrix::new(
/// #         support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
/// #         DofOrdering::NodesThenVars, Symmetry::Full);
/// #     z.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
/// #     z.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
/// #     z.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
/// #     z.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
/// #     z
/// # };
/// # use pyrucast::containers::matrix::NamedDof;
/// let mut k = Matrix::empty();
/// k.add_sub(Handle::new(bloc()))?;
/// k.finalize()?;
/// // A row or column DOF of the aggregate, in materialized form.
/// let dofs: Vec<NamedDof> = k.row_dofs()?;
/// assert_eq!(dofs[0], (a.id(), "q".to_string()));
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub type NamedDof = (NodeId, String);

/// A DOF in **packed** form: the node id in the high 32 bits, the index of its
/// variable in the matrix's name table in the low 32.
///
/// The materialised [`NamedDof`] carries a `String` per DOF. That is one heap
/// allocation, one hash of a string and one clone for every degree of freedom
/// of the problem — thirty million of each on a solid mesh, paid again at every
/// assembly, to re-express what a handful of variable names already say. The
/// key says the same thing in eight bytes, and the names live once in the
/// table the key indexes ([`Matrix::dof_vars`]).
///
/// ```
/// # use pyrucast::atoms::NodeId;
/// # use pyrucast::containers::matrix::{dof_key, dof_node, dof_var, DofKey};
/// // Node on top, variable below: two integers in one `u64`.
/// let k: DofKey = dof_key(NodeId(7), 2);
/// assert_eq!(dof_node(k), NodeId(7));
/// assert_eq!(dof_var(k), 2);
/// // Key order is nodes first, then variables — a sort by
/// // key therefore lays the DOFs out node by node.
/// assert!(dof_key(NodeId(7), 2) < dof_key(NodeId(8), 0));
/// ```
pub type DofKey = u64;

/// One side's global DOF numbering, shared rather than copied.
///
/// The row and column orders are handed to several consumers at once — the
/// assembly pattern, the solver, the elimination — and each holds on to its
/// own for as long as it needs it, so they are reference-counted.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::matrix::DofOrder;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::containers::model::Model;
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::ops::{element_field, matrix};
/// # use pyrucast::ops::model;
/// # let coords = Handle::new(Coords::new(1).unwrap());
/// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
/// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
/// # let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
/// # mesh.add_cell(&[a.id(), b.id()]).unwrap();
/// # let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
/// # let model = model::heat_conduction(&fes).unwrap();
/// # let materials = element_field::material_field(&model, &[("k", 1.0)]).unwrap();
/// # let k = matrix::stiffness(&model, &materials).unwrap();
/// // It numbers the rows: one entry per row of the assembled CSR.
/// let lignes: DofOrder = k.row_dof_keys()?;
/// assert_eq!(lignes.len(), k.n_rows()?);
/// // And it is **shared**, not rebuilt: two callers asking an assembled matrix
/// // for its numbering get the very same allocation, not a copy of it.
/// assert!(std::sync::Arc::ptr_eq(&lignes, &k.row_dof_keys()?));
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub type DofOrder = std::sync::Arc<Vec<DofKey>>;

/// Pack `(node, var_slot)` into a [`DofKey`].
///
/// ```
/// # use pyrucast::atoms::NodeId;
/// # use pyrucast::containers::matrix::{dof_key, dof_node, dof_var};
/// // Node on top, variable below: both fit in a single `u64`.
/// let k = dof_key(NodeId(7), 2);
/// assert_eq!((dof_node(k), dof_var(k)), (NodeId(7), 2));
/// ```
#[inline]
pub fn dof_key(node: NodeId, var_slot: u32) -> DofKey {
    ((node.0 as u64) << 32) | var_slot as u64
}

/// The node of a [`DofKey`].
///
/// ```
/// # use pyrucast::atoms::NodeId;
/// # use pyrucast::containers::matrix::{dof_key, dof_node};
/// assert_eq!(dof_node(dof_key(NodeId(7), 2)), NodeId(7));
/// ```
#[inline]
pub fn dof_node(key: DofKey) -> NodeId {
    NodeId((key >> 32) as u32)
}

/// The variable slot of a [`DofKey`] — an index into the matrix's name table
/// ([`Matrix::dof_vars`]).
///
/// ```
/// # use pyrucast::atoms::NodeId;
/// # use pyrucast::containers::matrix::{dof_key, dof_var};
/// assert_eq!(dof_var(dof_key(NodeId(7), 2)), 2);
/// ```
#[inline]
pub fn dof_var(key: DofKey) -> u32 {
    (key & 0xffff_ffff) as u32
}

/// A **seen-set** over DOF keys, direct-addressed when the node ids are dense
/// enough to make that cheaper than hashing.
///
/// Node ids index `Coords` directly, so `node × n_vars + var` is a perfect hash
/// whenever the id space is not riddled with holes. When it is — a long-lived
/// `Coords` that has seen many deletions — the flat table would dwarf the DOF
/// set it describes, and a `HashSet` of the (already compact) keys is the
/// honest fallback.
enum DofSeen {
    /// One flag per `(node, var)` slot: `node × n_vars + var`.
    Dense {
        flags: Vec<bool>,
        n_vars: usize,
    },
    Sparse(std::collections::HashSet<DofKey>),
}

impl DofSeen {
    /// Sized for `n_vars` variables over node ids up to `max_node`, holding
    /// `n_dofs` DOFs. Goes dense unless the flat table would cost more than
    /// eight flags per DOF actually stored.
    fn new(max_node: u32, n_vars: usize, n_dofs: usize) -> Self {
        let slots = (max_node as usize + 1).saturating_mul(n_vars.max(1));
        if slots <= n_dofs.saturating_mul(8).max(1024) {
            DofSeen::Dense {
                flags: vec![false; slots],
                n_vars: n_vars.max(1),
            }
        } else {
            DofSeen::Sparse(std::collections::HashSet::with_capacity(n_dofs))
        }
    }

    /// Record `key`; `true` the first time it is seen.
    #[inline]
    fn insert(&mut self, key: DofKey) -> bool {
        match self {
            DofSeen::Dense { flags, n_vars } => {
                let i = dof_node(key).0 as usize * *n_vars + dof_var(key) as usize;
                let fresh = !flags[i];
                flags[i] = true;
                fresh
            }
            DofSeen::Sparse(set) => set.insert(key),
        }
    }
}

/// Spell a packed key for an error message — `"node 9 · imposed_T"`.
fn dof_text(key: DofKey, vars: &[String]) -> String {
    format!("node {} · {}", dof_node(key).0, vars[dof_var(key) as usize])
}

/// Record one conjugate pair in the two global orders.
///
/// The two sides advance **together** or not at all. A DOF that is new on one
/// side and already placed on the other means the same dual DOF has been
/// declared conjugate to two different primal ones: a contradiction between
/// blocks, reported rather than papered over — the reader counts, it does not
/// correct.
fn push_conjugate(
    seen_row: &mut DofSeen,
    seen_col: &mut DofSeen,
    out_row: &mut Vec<DofKey>,
    out_col: &mut Vec<DofKey>,
    vars: &[String],
    r: DofKey,
    c: DofKey,
) -> Result<()> {
    let fresh_row = seen_row.insert(r);
    let fresh_col = seen_col.insert(c);
    if fresh_row != fresh_col {
        return Err(PyrucastError::Message(format!(
            "Matrix: {} and {} are declared conjugate, but one of them is already \
             paired with another DOF — two blocks disagree on what faces what",
            dof_text(r, vars),
            dof_text(c, vars)
        )));
    }
    if fresh_row {
        out_row.push(r);
        out_col.push(c);
    }
    Ok(())
}

/// Both sides of a conjugate run must hold the same number of DOFs.
fn conjugate_lengths(rows: usize, cols: usize, what: &str) -> Result<()> {
    if rows != cols {
        return Err(PyrucastError::Message(format!(
            "Matrix: {what} faces {rows} row DOF(s) with {cols} column DOF(s); a block \
             declaring symmetry must be square, and the two members of a pair must be \
             each other's transpose"
        )));
    }
    Ok(())
}

/// Global CSR sparsity pattern plus the DOF numbering it indexes.
///
/// A pure function of a model's **block structure** — not of the material
/// values — so it can be built once and reused across assemblies of the same
/// model (materials change, sparsity does not). The assembler
/// ([`crate::ops::matrix`]) builds it from a [`Matrix`]'s blocks and caches it
/// on the model (see `Model::matrix_pattern`); the numeric scatter then only
/// fills the values at each entry's fixed slot.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::matrix::{DofOrdering, Matrix, SubMatrix, Symmetry};
/// # use pyrucast::containers::mesh::SubMesh;
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # let coords = Handle::new(Coords::new(1).unwrap());
/// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
/// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
/// # let support = {
/// #     let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
/// #     sm.add_cell(&[a.id()]).unwrap();
/// #     sm.add_cell(&[b.id()]).unwrap();
/// #     Handle::new(sm)
/// # };
/// # let bloc = || {
/// #     let mut z = SubMatrix::new(
/// #         support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
/// #         DofOrdering::NodesThenVars, Symmetry::Full);
/// #     z.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
/// #     z.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
/// #     z.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
/// #     z.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
/// #     z
/// # };
/// # use pyrucast::ops::scatter;
/// let mut k = Matrix::empty();
/// k.add_sub(Handle::new(bloc()))?;
/// k.finalize()?;
/// // A pure function of the model's **structure**, not of its materials:
/// // built once, reused from one assembly to the next.
/// let motif = scatter::build_pattern(&k)?;
/// assert_eq!(motif.nnz(), 4);
/// assert_eq!(motif.row_offsets.len(), motif.row_keys.len() + 1);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
#[derive(Clone)]
pub struct AssemblyPattern {
    /// The variable name table the DOF keys index — interned once for the whole
    /// pattern, rows and columns together.
    pub vars: std::sync::Arc<Vec<String>>,
    /// Global row DOFs as packed [`DofKey`]s, in CSR row order. Held by `Arc`
    /// for the same reason as the sparsity below: every matrix assembled from
    /// this pattern **shares** the numbering instead of copying it.
    pub row_keys: std::sync::Arc<Vec<DofKey>>,
    /// Global column DOFs as packed [`DofKey`]s, in CSR column order. Shared
    /// like [`row_keys`](Self::row_keys).
    pub col_keys: std::sync::Arc<Vec<DofKey>>,
    /// CSR row offsets, length `row_keys.len() + 1`. Held by `Arc` because the
    /// assembled matrices built from this pattern **share** it rather than each
    /// copying the sparsity out.
    pub row_offsets: std::sync::Arc<Vec<usize>>,
    /// CSR column indices, sorted within each row, length `row_offsets[nrows]`.
    /// Shared like [`row_offsets`](Self::row_offsets).
    pub col_indices: std::sync::Arc<Vec<usize>>,
    /// Precomputed value-array slot of every entry each block contributes, one
    /// entry per block in the matrix's `subs` order. Since the pattern is
    /// material-independent and cached, the `binary_search` that maps a
    /// block-local `(r, c)` to its CSR slot is paid **once here**, not on every
    /// assembly's numeric scatter. The scatter reads these back directly (see
    /// [`crate::ops::scatter`]).
    pub block_slots: Vec<BlockSlots>,
}

/// Precomputed CSR value-array slots for one block, aligned with the order that
/// block emits its entries at scatter time. A computed block's cells are
/// evaluated cell-by-cell, so its slots are grouped per cell, matching
/// `element_block_triplets_per_cell`'s `per_cell` (and, entry-for-entry, the
/// `(li, di, lj, pj)` emission order of `element_block_pattern`). A literal
/// block's slots follow its `local_coo_arrays` order.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::matrix::{DofOrdering, Matrix, SubMatrix, Symmetry};
/// # use pyrucast::containers::mesh::SubMesh;
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # let coords = Handle::new(Coords::new(1).unwrap());
/// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
/// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
/// # let support = {
/// #     let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
/// #     sm.add_cell(&[a.id()]).unwrap();
/// #     sm.add_cell(&[b.id()]).unwrap();
/// #     Handle::new(sm)
/// # };
/// # let bloc = || {
/// #     let mut z = SubMatrix::new(
/// #         support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
/// #         DofOrdering::NodesThenVars, Symmetry::Full);
/// #     z.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
/// #     z.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
/// #     z.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
/// #     z.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
/// #     z
/// # };
/// # use pyrucast::containers::matrix::BlockSlots;
/// # use pyrucast::ops::scatter;
/// let mut k = Matrix::empty();
/// k.add_sub(Handle::new(bloc()))?;
/// k.finalize()?;
/// // A **literal** block lays its slots out in its COO order;
/// // a computed block groups them per cell, in the order the kernel
/// // produit.
/// let motif = scatter::build_pattern(&k)?;
/// match &motif.block_slots[0] {
///     BlockSlots::Literal(slots) => assert_eq!(slots.len(), 4),
///     _ => unreachable!("this block carries its values"),
/// }
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
#[derive(Clone)]
pub enum BlockSlots {
    /// A computed block, one **flat** run of `stride` slots per cell, in the
    /// kernel's `(li, di, lj, pj)` emission order.
    ///
    /// A `Vec` per cell meant ten million allocations on a solid mesh, each
    /// holding a few hundred indices. One buffer holds them all instead, and
    /// `u32` is twice as narrow as the `usize` it replaces.
    Computed { slots: Vec<u32>, stride: usize },
    /// A computed block whose trailing primal variable is **factored out**:
    /// `stride` base slots per cell in `(li, di, lj)` order, the entry for
    /// primal variable `pj` sitting at `base + pj`.
    ///
    /// The columns of one node are consecutive integers under
    /// [`DofOrdering::NodesThenVars`], and a CSR row keeps its columns sorted,
    /// so they land in consecutive slots. Storing one base instead of
    /// `n_primal` slots divides this block's index memory by as much — three,
    /// for a 3-D elasticity. The assembler only takes this form after
    /// **checking** the consecutiveness on the block's own numbering.
    ComputedBlocked {
        bases: Vec<u32>,
        stride: usize,
        n_primal: usize,
    },
    /// One slot per COO entry (literal block).
    Literal(Vec<usize>),
}

impl BlockSlots {
    /// Hand `add(slot, value)` every entry of `cell`, pairing that cell's `ke`
    /// with the CSR slot each value lands in.
    ///
    /// The two computed forms differ only in how the slot is found, and both
    /// walk `ke` in the kernel's `(li, di, lj, pj)` emission order — so the
    /// scatter is written once, and the compression stays an implementation
    /// detail of the pattern. A literal block has no cells and contributes
    /// nothing here.
    pub(crate) fn each_entry(&self, cell: usize, ke: &[f64], mut add: impl FnMut(usize, f64)) {
        match self {
            BlockSlots::Computed { slots, stride } => {
                let base = cell * stride;
                for (k, &v) in ke.iter().enumerate() {
                    add(slots[base + k] as usize, v);
                }
            }
            BlockSlots::ComputedBlocked {
                bases,
                stride,
                n_primal,
            } => {
                // `k = (…, lj) · n_primal + pj`, and the pattern checked that a
                // node's primal columns sit in consecutive slots — so one base
                // per `(li, di, lj)` names all `n_primal` of them.
                let row = &bases[cell * stride..(cell + 1) * stride];
                let mut k = 0;
                for &base in row {
                    for pj in 0..*n_primal {
                        add(base as usize + pj, ke[k]);
                        k += 1;
                    }
                }
            }
            BlockSlots::Literal(_) => {}
        }
    }
}

impl AssemblyPattern {
    /// Value-array slot of global entry `(r, c)`. `c` must be present in row
    /// `r`'s column set — it is, for any entry a block contributes (the pattern
    /// was built from exactly those entries).
    ///
    /// Only the pattern build calls this; the numeric scatter reads the
    /// precomputed [`AssemblyPattern::block_slots`] instead.
    #[inline]
    pub(crate) fn slot(&self, r: usize, c: usize) -> usize {
        let base = self.row_offsets[r];
        let seg = &self.col_indices[base..self.row_offsets[r + 1]];
        base + seg
            .binary_search(&c)
            .expect("scatter: entry (r, c) absent from the CSR pattern")
    }

    /// Number of stored entries (CSR `nnz`).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, Matrix, SubMatrix, Symmetry};
    /// # use pyrucast::containers::mesh::SubMesh;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = {
    /// #     let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
    /// #     sm.add_cell(&[a.id()]).unwrap();
    /// #     sm.add_cell(&[b.id()]).unwrap();
    /// #     Handle::new(sm)
    /// # };
    /// # let bloc = || {
    /// #     let mut z = SubMatrix::new(
    /// #         support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #         DofOrdering::NodesThenVars, Symmetry::Full);
    /// #     z.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// #     z.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// #     z
    /// # };
    /// # use pyrucast::ops::scatter;
    /// let mut k = Matrix::empty();
    /// k.add_sub(Handle::new(bloc()))?;
    /// k.finalize()?;
    /// let motif = scatter::build_pattern(&k)?;
    /// // The number of **stored** entries — the CSR's, not the blocks'.
    /// assert_eq!(motif.nnz(), motif.col_indices.len());
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn nnz(&self) -> usize {
        self.col_indices.len()
    }

    /// The row DOFs in materialised `(node, variable name)` form.
    ///
    /// The pattern numbers with packed keys; this is the translation for the
    /// callers that speak names, and it allocates a `String` per DOF — so it
    /// belongs at an API edge, never inside an assembly.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::containers::model::Model;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::{element_field, matrix, scatter};
    /// # use pyrucast::ops::model;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    /// # mesh.add_cell(&[a.id(), b.id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    /// # let model = model::heat_conduction(&fes).unwrap();
    /// # let materials = element_field::material_field(&model, &[("k", 1.0)]).unwrap();
    /// # let k = matrix::stiffness(&model, &materials).unwrap();
    /// # let motif = scatter::build_pattern(&k)?;
    /// // The pattern numbers in keys; names, it gives back on demand.
    /// assert_eq!(motif.row_dofs()[0], (a.id(), "q".to_string()));
    /// assert_eq!(motif.row_dofs().len(), motif.row_keys.len());
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn row_dofs(&self) -> Vec<NamedDof> {
        self.named(&self.row_keys)
    }

    /// The column DOFs in materialised form — see
    /// [`row_dofs`](Self::row_dofs).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::containers::model::Model;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::{element_field, matrix, scatter};
    /// # use pyrucast::ops::model;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    /// # mesh.add_cell(&[a.id(), b.id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    /// # let model = model::heat_conduction(&fes).unwrap();
    /// # let materials = element_field::material_field(&model, &[("k", 1.0)]).unwrap();
    /// # let k = matrix::stiffness(&model, &materials).unwrap();
    /// # let motif = scatter::build_pattern(&k)?;
    /// // Côté colonne, la variable primale.
    /// assert_eq!(motif.col_dofs()[0], (a.id(), "T".to_string()));
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn col_dofs(&self) -> Vec<NamedDof> {
        self.named(&self.col_keys)
    }

    /// Wrap `values` — the numeric phase's output, in this pattern's slot order
    /// — into an assembled CSR that **shares** this pattern's sparsity.
    pub(crate) fn assembled_csr(&self, values: Vec<f64>) -> AssembledCsr {
        AssembledCsr {
            row_offsets: self.row_offsets.clone(),
            col_indices: self.col_indices.clone(),
            values,
            ncols: self.col_keys.len(),
        }
    }

    fn named(&self, keys: &[DofKey]) -> Vec<NamedDof> {
        keys.iter()
            .map(|&k| (dof_node(k), self.vars[dof_var(k) as usize].clone()))
            .collect()
    }
}

/// Sort each row segment `pairs[bounds[i]..bounds[i+1]]` by column, in place and
/// in parallel. `bounds` are absolute offsets into the original buffer (so
/// `bounds[0]` is this slice's base); recursion splits the **row range** and the
/// buffer together via `split_at_mut`, giving each task a disjoint slice. The
/// sort is stable, preserving the stream order of equal columns.
fn sort_rows_in_place(pairs: &mut [(usize, f64)], bounds: &[usize]) {
    let nrows = bounds.len() - 1;
    let base = bounds[0];
    // Below this many entries, the task-spawn overhead outweighs the work: sort
    // the remaining rows serially.
    const SERIAL_BELOW: usize = 4096;
    if nrows <= 1 || pairs.len() < SERIAL_BELOW {
        for i in 0..nrows {
            pairs[bounds[i] - base..bounds[i + 1] - base].sort_by_key(|&(c, _)| c);
        }
        return;
    }
    let mid = nrows / 2;
    let (left, right) = pairs.split_at_mut(bounds[mid] - base);
    rayon::join(
        || sort_rows_in_place(left, &bounds[..=mid]),
        || sort_rows_in_place(right, &bounds[mid..]),
    );
}

/// Build a CSR matrix from unsorted global `(row, col, value)` triplets,
/// **summing duplicates**, in parallel. Equivalent to
/// `CsrMatrix::from(&CooMatrix::try_from_triplets(…))`.
///
/// Uses a counting sort by row (cache-friendly bucket scatter) so the only
/// comparison sort is *within* each row — tiny segments sorted across rows in
/// parallel ([`sort_rows_in_place`]). The histogram, scatter and final
/// dedup-and-sum scan are O(nnz) serial passes. The per-row sort is stable, so
/// equal `(row, col)` entries are summed in stream order — bit-for-bit identical
/// to the serial path.
#[allow(clippy::type_complexity)]
fn csr_from_triplets_parallel(
    nrows: usize,
    _ncols: usize,
    triplets: Vec<(usize, usize, f64)>,
) -> (Vec<usize>, Vec<usize>, Vec<f64>) {
    let nnz = triplets.len();
    // 1. Entries per row → exclusive prefix sum → per-row bucket bounds.
    let mut bounds = vec![0usize; nrows + 1];
    for &(r, _, _) in &triplets {
        bounds[r + 1] += 1;
    }
    for r in 0..nrows {
        bounds[r + 1] += bounds[r];
    }
    // 2. Scatter (col, val) into each row's bucket, preserving stream order.
    let mut cursor: Vec<usize> = bounds[..nrows].to_vec();
    let mut pairs = vec![(0usize, 0.0f64); nnz];
    for (r, c, v) in triplets {
        pairs[cursor[r]] = (c, v);
        cursor[r] += 1;
    }
    // 3. Sort each row's columns, in place and across rows in parallel.
    sort_rows_in_place(&mut pairs, &bounds);
    // 4. Serial scan: emit one CSR entry per distinct (row, col), summing dups.
    let mut row_offsets = vec![0usize; nrows + 1];
    let mut col_indices: Vec<usize> = Vec::with_capacity(nnz);
    let mut values: Vec<f64> = Vec::with_capacity(nnz);
    for r in 0..nrows {
        let mut last_col: Option<usize> = None;
        for &(c, v) in &pairs[bounds[r]..bounds[r + 1]] {
            if last_col == Some(c) {
                *values.last_mut().unwrap() += v;
            } else {
                col_indices.push(c);
                values.push(v);
                row_offsets[r + 1] += 1;
                last_col = Some(c);
            }
        }
    }
    for r in 0..nrows {
        row_offsets[r + 1] += row_offsets[r];
    }
    (row_offsets, col_indices, values)
}

impl Matrix {
    /// Build the global DOF union + CSR. Must be called before any
    /// solver-facing method (`to_csr`, `to_dmatrix`, `mul_dense`, `dense`,
    /// `to_coo`, `to_csc`). Idempotent: a second call is a no-op if no
    /// `add_sub` has occurred since the last `finalize`.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, Matrix, SubMatrix, Symmetry};
    /// # use pyrucast::containers::mesh::SubMesh;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = {
    /// #     let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
    /// #     sm.add_cell(&[a.id()]).unwrap();
    /// #     sm.add_cell(&[b.id()]).unwrap();
    /// #     Handle::new(sm)
    /// # };
    /// # let bloc = || {
    /// #     let mut z = SubMatrix::new(
    /// #         support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #         DofOrdering::NodesThenVars, Symmetry::Full);
    /// #     z.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// #     z.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// #     z
    /// # };
    /// let mut k = Matrix::empty();
    /// k.add_sub(Handle::new(bloc()))?;
    /// // The size reads off the blocks, without assembly…
    /// assert_eq!((k.n_rows()?, k.n_cols()?), (2, 2));
    /// // …but the sparse views exist only once the matrix is finalized.
    /// assert!(k.to_csr().is_err());
    /// k.finalize()?;
    /// assert_eq!(k.to_csr()?.nnz(), 4);
    /// // Idempotent: a second call reassembles nothing.
    /// k.finalize()?;
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn finalize(&mut self) -> Result<()> {
        if self.assembled.is_some() {
            return Ok(());
        }
        // A *computed* block carries no values: producing them means driving a
        // model kernel that lives in `crate::models` (outside `containers`).
        // Assembling it here would force a matrix↔kernel cycle, so we don't —
        // the global assembler in `ops::matrix::stiffness` handles computed
        // blocks and injects the finished CSR via `set_assembled`.
        for h in &*self {
            if h.read().is_computed() {
                return Err(PyrucastError::Message(
                    "Matrix::finalize: this matrix carries a computed block; \
                     assemble it with m.assemble() (or \
                     ops::matrix::stiffness), which scatters the kernel into \
                     the global CSR — finalize() cannot (it must not reach into \
                     the model/kernel)"
                        .into(),
                ));
            }
        }
        let vars = std::sync::Arc::new(self.field_names());
        let (row_keys, col_keys) = self.dof_orders(&vars)?;
        let triplets = self.build_global_triplets(&vars, &row_keys, &col_keys)?;
        let (row_offsets, col_indices, values) =
            csr_from_triplets_parallel(row_keys.len(), col_keys.len(), triplets);
        let csr = AssembledCsr {
            row_offsets: std::sync::Arc::new(row_offsets),
            col_indices: std::sync::Arc::new(col_indices),
            values,
            ncols: col_keys.len(),
        };
        self.assembled = Some(AssembledData {
            vars,
            row_keys: std::sync::Arc::new(row_keys),
            col_keys: std::sync::Arc::new(col_keys),
            csr,
        });
        Ok(())
    }

    /// Inject a globally-assembled CSR built by an external assembler
    /// ([`crate::ops::matrix`]), bypassing [`finalize`](Self::finalize).
    /// `row_dofs` / `col_dofs` must be this matrix' global DOF union (as
    /// returned by [`row_dofs`](Self::row_dofs) / [`col_dofs`](Self::col_dofs))
    /// and index `csr`. This is the path for matrices carrying *computed*
    /// blocks, which `finalize` cannot assemble on its own (it would have to
    /// reach into the model/kernel — the cycle Option B avoids).
    pub(crate) fn set_assembled(
        &mut self,
        vars: std::sync::Arc<Vec<String>>,
        row_keys: std::sync::Arc<Vec<DofKey>>,
        col_keys: std::sync::Arc<Vec<DofKey>>,
        csr: AssembledCsr,
    ) {
        self.assembled = Some(AssembledData {
            vars,
            row_keys,
            col_keys,
            csr,
        });
    }

    fn assembled_or_err(&self) -> Result<&AssembledData> {
        self.assembled.as_ref().ok_or_else(|| {
            PyrucastError::Message(
                "Matrix has not been finalized; call finalize() before solving".into(),
            )
        })
    }

    /// The cached factorization downcast to `T`, if one is present and of that
    /// type. Lets the solver reuse a previous factorization transparently.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, Matrix, SubMatrix, Symmetry};
    /// # use pyrucast::containers::mesh::SubMesh;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = {
    /// #     let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
    /// #     sm.add_cell(&[a.id()]).unwrap();
    /// #     sm.add_cell(&[b.id()]).unwrap();
    /// #     Handle::new(sm)
    /// # };
    /// # let bloc = || {
    /// #     let mut z = SubMatrix::new(
    /// #         support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #         DofOrdering::NodesThenVars, Symmetry::Full);
    /// #     z.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// #     z.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// #     z
    /// # };
    /// # use std::sync::Arc;
    /// let mut k = Matrix::empty();
    /// k.add_sub(Handle::new(bloc()))?;
    /// k.finalize()?;
    /// assert!(k.cached_factorization::<usize>().is_none()); // rien encore
    /// k.store_factorization(Arc::new(42usize));
    /// // The downcast is checked: another type yields nothing.
    /// assert!(k.cached_factorization::<String>().is_none());
    /// assert_eq!(k.cached_factorization::<usize>().as_deref(), Some(&42));
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn cached_factorization<T: std::any::Any + Send + Sync>(
        &self,
    ) -> Option<std::sync::Arc<T>> {
        let arc = self.factorization.lock().as_ref().cloned()?;
        arc.downcast::<T>().ok()
    }

    /// Store a freshly computed factorization for transparent reuse. Cleared
    /// automatically whenever the matrix changes (`add_sub`).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, Matrix, SubMatrix, Symmetry};
    /// # use pyrucast::containers::mesh::SubMesh;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = {
    /// #     let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
    /// #     sm.add_cell(&[a.id()]).unwrap();
    /// #     sm.add_cell(&[b.id()]).unwrap();
    /// #     Handle::new(sm)
    /// # };
    /// # let bloc = || {
    /// #     let mut z = SubMatrix::new(
    /// #         support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #         DofOrdering::NodesThenVars, Symmetry::Full);
    /// #     z.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// #     z.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// #     z
    /// # };
    /// # use std::sync::Arc;
    /// let mut k = Matrix::empty();
    /// k.add_sub(Handle::new(bloc()))?;
    /// k.finalize()?;
    /// // The solver drops its factorization here to reuse it as is at the next
    /// // step; any change to the matrix wipes it.
    /// k.store_factorization(Arc::new(42usize));
    /// assert_eq!(k.cached_factorization::<usize>().as_deref(), Some(&42));
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn store_factorization(
        &self,
        factorization: std::sync::Arc<dyn std::any::Any + Send + Sync>,
    ) {
        *self.factorization.lock() = Some(factorization);
    }

    // ── Block-traversal helpers (no finalize required) ──────────────────

    /// Deduplicated concatenation of the blocks' row (or col) DOFs, as packed
    /// [`DofKey`]s — the global DOF numbering.
    ///
    /// O(total block DOFs), and **allocation-free per DOF**: the variable names
    /// are resolved to slots once per block, the dedup runs on a direct-addressed
    /// seen-set over the packed keys ([`DofSeen`]), and nothing here builds a
    /// `String`.
    ///
    /// Order: **solver order** when the backing `Coords` carries a
    /// [`permutation`](crate::coords::Coords::permutation) (stable
    /// sort by the node's permutation index, so the per-node variable order is
    /// preserved); otherwise **first-seen** (identical to the historical
    /// behaviour, hence bit-for-bit stable when no permutation is set).
    fn collect_dof_keys(&self, row: bool, vars: &[String]) -> Result<Vec<DofKey>> {
        let slot_of: HashMap<String, u32> = vars
            .iter()
            .cloned()
            .enumerate()
            .map(|(i, v)| (v, i as u32))
            .collect();

        // Size the seen-set before filling it: the DOF total bounds the output,
        // the largest node id bounds the direct-addressed table.
        let (mut total, mut max_node) = (0usize, 0u32);
        for h in self {
            let sub = h.read();
            let support = if row {
                sub.row_support().read()
            } else {
                sub.col_support().read()
            };
            total += if row { sub.n_rows() } else { sub.n_cols() };
            for n in support.connectivity() {
                max_node = max_node.max(n.0);
            }
        }

        let mut seen = DofSeen::new(max_node, vars.len(), total);
        let mut out: Vec<DofKey> = Vec::with_capacity(total);
        for h in self {
            let sub = h.read();
            let keys = if row {
                sub.row_dof_keys_with(&sub.row_support().read(), &slot_of)
            } else {
                sub.col_dof_keys_with(&sub.col_support().read(), &slot_of)
            };
            for k in keys {
                if seen.insert(k) {
                    out.push(k);
                }
            }
        }

        if let Some(first) = self.iter().next() {
            let coords_h = first.read().coords();
            if let Some(perm) = coords_h.read().permutation() {
                // Stable, so a node's variables keep their relative order.
                out.par_sort_by_key(|&k| perm[dof_node(k).0 as usize]);
            }
        }
        Ok(out)
    }

    /// The two global DOF orders, row and column.
    ///
    /// On a matrix that [declares itself symmetric](Self::symmetric) they are
    /// built **together**, so that rank `i` holds conjugate DOFs on both sides —
    /// which is what makes the assembled array symmetric and not merely the
    /// operator it represents. Otherwise the two orders are collected
    /// independently, as they always were: a rectangular matrix has no conjugate
    /// to pair with, and demanding the alignment would refuse it.
    fn dof_orders(&self, vars: &[String]) -> Result<(Vec<DofKey>, Vec<DofKey>)> {
        if self.symmetric() {
            self.collect_conjugate_dof_keys(vars)
        } else {
            Ok((
                self.collect_dof_keys(true, vars)?,
                self.collect_dof_keys(false, vars)?,
            ))
        }
    }

    /// Both DOF orders in **one** walk, conjugate rank for rank.
    ///
    /// Nothing here learns that `q` is the dual of `T`. It reads only which
    /// *positions* correspond, which the blocks already declare:
    ///
    /// - a `Full` block is square over one support (a physics block hands the
    ///   **same handle** to both sides) with its dual and primal variables
    ///   matched by position, so its row `k` faces its own column `k`;
    /// - a `Half` pair crosses: [`pairs_are_mutual_transposes`] already holds
    ///   that one block's row support **is** the other's column support, by
    ///   handle identity. So `a`'s rows face `b`'s columns, and `a`'s columns
    ///   face `b`'s rows.
    ///
    /// [`pairs_are_mutual_transposes`]: Self::pairs_are_mutual_transposes
    fn collect_conjugate_dof_keys(&self, vars: &[String]) -> Result<(Vec<DofKey>, Vec<DofKey>)> {
        let slot_of: HashMap<String, u32> = vars
            .iter()
            .cloned()
            .enumerate()
            .map(|(i, v)| (v, i as u32))
            .collect();

        // Both sides bound the seen-sets here, the walk filling them in step.
        let (mut total, mut max_node) = (0usize, 0u32);
        for h in self {
            let sub = h.read();
            total += sub.n_rows();
            for support in [sub.row_support(), sub.col_support()] {
                for n in support.read().connectivity() {
                    max_node = max_node.max(n.0);
                }
            }
        }

        let mut seen_row = DofSeen::new(max_node, vars.len(), total);
        let mut seen_col = DofSeen::new(max_node, vars.len(), total);
        let mut out_row: Vec<DofKey> = Vec::with_capacity(total);
        let mut out_col: Vec<DofKey> = Vec::with_capacity(total);
        let mut consumed = vec![false; self.len()];

        for (i, h) in self.iter().enumerate() {
            if consumed[i] {
                continue;
            }
            let a = h.read();
            // Each support guard is taken and dropped on its own: the two sides
            // of a pair share one `SubMesh`, and holding both at once would mean
            // two read locks on the same lock.
            let a_rows = {
                let g = a.row_support().read();
                a.row_dof_keys_with(&g, &slot_of)
            };
            let a_cols = {
                let g = a.col_support().read();
                a.col_dof_keys_with(&g, &slot_of)
            };
            match a.symmetry() {
                Symmetry::Full => {
                    conjugate_lengths(a_rows.len(), a_cols.len(), "a Full block")?;
                    for (&r, &c) in a_rows.iter().zip(a_cols.iter()) {
                        push_conjugate(
                            &mut seen_row,
                            &mut seen_col,
                            &mut out_row,
                            &mut out_col,
                            vars,
                            r,
                            c,
                        )?;
                    }
                }
                Symmetry::Half(id) => {
                    // The other member: same group, **other** low bit. Two blocks
                    // sharing the bit are the same member and do not cross.
                    let partner = self.iter().enumerate().position(|(j, g)| {
                        j != i
                            && !consumed[j]
                            && matches!(g.read().symmetry(),
                                Symmetry::Half(o) if o & !1 == id & !1 && o & 1 != id & 1)
                    });
                    let Some(j) = partner else {
                        return Err(PyrucastError::Message(
                            "Matrix: a Half block has no partner in this matrix, so the two \
                             DOF orders cannot be paired — the symmetry declaration and the \
                             blocks disagree"
                                .into(),
                        ));
                    };
                    let p = self.iter().nth(j).expect("index from position").read();
                    let p_rows = {
                        let g = p.row_support().read();
                        p.row_dof_keys_with(&g, &slot_of)
                    };
                    let p_cols = {
                        let g = p.col_support().read();
                        p.col_dof_keys_with(&g, &slot_of)
                    };
                    conjugate_lengths(a_rows.len(), p_cols.len(), "a Half pair")?;
                    conjugate_lengths(p_rows.len(), a_cols.len(), "a Half pair")?;
                    for (&r, &c) in a_rows.iter().zip(p_cols.iter()) {
                        push_conjugate(
                            &mut seen_row,
                            &mut seen_col,
                            &mut out_row,
                            &mut out_col,
                            vars,
                            r,
                            c,
                        )?;
                    }
                    for (&r, &c) in p_rows.iter().zip(a_cols.iter()) {
                        push_conjugate(
                            &mut seen_row,
                            &mut seen_col,
                            &mut out_row,
                            &mut out_col,
                            vars,
                            r,
                            c,
                        )?;
                    }
                    consumed[j] = true;
                }
                // `symmetric()` returned true, so no block declares `None`.
                Symmetry::None => unreachable!("a None block cannot reach the conjugate walk"),
            }
        }

        if let Some(first) = self.iter().next() {
            let coords_h = first.read().coords();
            if let Some(perm) = coords_h.read().permutation() {
                // Sorted as **pairs**, on the row's node — the conjugate shares
                // it — so the two sides cannot drift apart. Stable, so a node's
                // variables keep their relative order.
                let mut pairs: Vec<(DofKey, DofKey)> = out_row.into_iter().zip(out_col).collect();
                pairs.par_sort_by_key(|&(r, _)| perm[dof_node(r).0 as usize]);
                (out_row, out_col) = pairs.into_iter().unzip();
            }
        }
        Ok((out_row, out_col))
    }

    /// Materialise packed keys into `(node, variable name)` pairs.
    fn name_dofs(keys: &[DofKey], vars: &[String]) -> Vec<NamedDof> {
        keys.iter()
            .map(|&k| (dof_node(k), vars[dof_var(k) as usize].clone()))
            .collect()
    }

    /// Map every block's **local** triplets to global `(row, col, value)`
    /// arrays via a per-block translation table (built once from the global DOF
    /// maps). The remap is index-preserving, so the concatenated stream — blocks
    /// in order, entries in COO order — matches the old serial scatter. The
    /// per-block remap runs in parallel. O(total block DOFs + nnz), no per-entry
    /// search.
    fn build_global_triplets(
        &self,
        vars: &[String],
        row_keys: &[DofKey],
        col_keys: &[DofKey],
    ) -> Result<Vec<(usize, usize, f64)>> {
        let slot_of: HashMap<String, u32> = vars
            .iter()
            .cloned()
            .enumerate()
            .map(|(i, v)| (v, i as u32))
            .collect();
        let row_map: HashMap<DofKey, usize> =
            row_keys.iter().enumerate().map(|(i, &k)| (k, i)).collect();
        let col_map: HashMap<DofKey, usize> =
            col_keys.iter().enumerate().map(|(i, &k)| (k, i)).collect();
        let mut out: Vec<(usize, usize, f64)> = Vec::new();
        for h in self {
            let sub = h.read();
            // local DOF index → global index (the "simple remap").
            let trow: Vec<usize> = sub
                .row_dof_keys(&slot_of)
                .iter()
                .map(|k| row_map[k])
                .collect();
            let tcol: Vec<usize> = sub
                .col_dof_keys(&slot_of)
                .iter()
                .map(|k| col_map[k])
                .collect();
            let (lr, lc, lv) = sub.local_coo_arrays();
            let factor = sub.factor();
            let block: Vec<(usize, usize, f64)> = (0..lv.len())
                .into_par_iter()
                .with_min_len(MIN_PARALLEL_LEN)
                .map(|k| (trow[lr[k]], tcol[lc[k]], lv[k] * factor))
                .collect();
            if out.is_empty() {
                out = block;
            } else {
                out.extend(block);
            }
        }
        Ok(out)
    }

    // ── Inspection (always available) ───────────────────────────────────

    /// Whether the assembled matrix satisfies `A[i][j] == A[j][i]`, by adding up
    /// what its blocks [declare](Symmetry). Vacuously true for an empty aggregate.
    ///
    /// A block declaring [`Full`](Symmetry::Full) carries its own symmetry. A
    /// [`Half`](Symmetry::Half) carries only one side of one, so the aggregate
    /// must find the other: among the blocks sharing a [`PairId`], the two
    /// members — told apart by its low bit — must be present **in equal,
    /// non-zero numbers**.
    ///
    /// Counting the members rather than the blocks is what lets a constraint
    /// declared twice (four blocks, two of each) stand, while a pair broken by
    /// [`Aggregate::subset`] (two blocks, both the same member) falls. A bare
    /// "exactly two" would lose the first; a parity check would accept the second.
    ///
    /// Blocks are a handful, so the tally lives in one small `Vec` — and this runs
    /// once when a matrix is about to be solved, never inside a loop.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::Matrix;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::containers::model::Model;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::{element_field, matrix};
    /// # use pyrucast::ops::model;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    /// # mesh.add_cell(&[a.id(), b.id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    /// # let model = model::heat_conduction(&fes).unwrap();
    /// # let materials = element_field::material_field(&model, &[("k", 1.0)]).unwrap();
    /// # let k = matrix::stiffness(&model, &materials).unwrap();
    /// // A Galerkin stiffness is symmetric, and its Dirichlet's two rectangular
    /// // blocks are a pair — so the aggregate concludes it is.
    /// assert!(k.symmetric());
    /// ```
    pub fn symmetric(&self) -> bool {
        // One entry per pair met: its identity with the low bit cleared, and how
        // many blocks of each member it has been handed.
        let mut pairs: Vec<(PairId, [usize; 2])> = Vec::new();
        for h in self {
            match h.read().symmetry() {
                Symmetry::Full => {}
                Symmetry::None => return false,
                Symmetry::Half(id) => {
                    let (base, member) = (id & !1, (id & 1) as usize);
                    match pairs.iter_mut().find(|(b, _)| *b == base) {
                        Some((_, seen)) => seen[member] += 1,
                        None => {
                            let mut seen = [0usize; 2];
                            seen[member] = 1;
                            pairs.push((base, seen));
                        }
                    }
                }
            }
        }
        if pairs.iter().any(|(_, seen)| seen[0] != seen[1]) {
            return false;
        }
        debug_assert!(
            self.pairs_are_mutual_transposes(),
            "a Half group holds blocks that are not each other's transpose — \
             two distinct pairs hashed alike, or a pair was minted wrongly"
        );
        true
    }

    /// Every `Half` group holds blocks whose supports and variables cross — the
    /// structural half of what `Half` claims.
    ///
    /// Only a `debug_assert!` calls this. It cannot check the low bit, which
    /// encodes a producer's choice rather than a fact, and it is not meant to
    /// stand between a hash collision and a wrong answer in release: it catches a
    /// pair minted wrongly while that mistake is still cheap to fix.
    fn pairs_are_mutual_transposes(&self) -> bool {
        let halves: Vec<_> = self
            .iter()
            .filter_map(|h| match h.read().symmetry() {
                Symmetry::Half(id) => Some((id & !1, h)),
                _ => None,
            })
            .collect();
        halves.iter().all(|(base, h)| {
            let a = h.read();
            halves.iter().any(|(other_base, g)| {
                if other_base != base || Handle::same_object(h, g) {
                    return false;
                }
                let b = g.read();
                // Only the **supports** cross. The variable names do not: the
                // transpose swaps the dual and primal *roles*, and a constraint
                // gives those roles different names on each side (`imposed_T`
                // against `lambda_T` at the multiplier). There is nothing to
                // compare there.
                Handle::same_object(a.row_support(), b.col_support())
                    && Handle::same_object(a.col_support(), b.row_support())
            })
        })
    }

    /// Union of all row DOFs across blocks, in first-seen order.
    /// If finalized, returns the cached order (consistent with the CSR).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::Matrix;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::containers::model::Model;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::{element_field, matrix};
    /// # use pyrucast::ops::model;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    /// # mesh.add_cell(&[a.id(), b.id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    /// # let model = model::heat_conduction(&fes).unwrap();
    /// # let materials = element_field::material_field(&model, &[("k", 1.0)]).unwrap();
    /// # let k = matrix::stiffness(&model, &materials).unwrap();
    /// // Un DDL de ligne : (nœud, variable **duale**).
    /// let lignes = k.row_dofs().unwrap();
    /// assert_eq!(lignes.len(), k.n_rows().unwrap());
    /// assert_eq!(lignes[0].1, "q");
    /// ```
    pub fn row_dofs(&self) -> Result<Vec<NamedDof>> {
        let vars = self.dof_vars();
        Ok(Self::name_dofs(&self.row_dof_keys()?, &vars))
    }

    /// Union of all column DOFs across blocks, in first-seen order.
    /// If finalized, returns the cached order (consistent with the CSR).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::Matrix;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::containers::model::Model;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::{element_field, matrix};
    /// # use pyrucast::ops::model;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    /// # mesh.add_cell(&[a.id(), b.id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    /// # let model = model::heat_conduction(&fes).unwrap();
    /// # let materials = element_field::material_field(&model, &[("k", 1.0)]).unwrap();
    /// # let k = matrix::stiffness(&model, &materials).unwrap();
    /// // Un DDL de colonne : (nœud, variable **primale**).
    /// assert_eq!(k.col_dofs().unwrap()[0].1, "T");
    /// ```
    pub fn col_dofs(&self) -> Result<Vec<NamedDof>> {
        let vars = self.dof_vars();
        Ok(Self::name_dofs(&self.col_dof_keys()?, &vars))
    }

    /// The matrix's **variable name table** — the names a [`DofKey`]'s slot
    /// indexes. Interned once, rows and columns together.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::{dof_node, dof_var, Matrix};
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::containers::model::Model;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::{element_field, matrix};
    /// # use pyrucast::ops::model;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    /// # mesh.add_cell(&[a.id(), b.id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    /// # let model = model::heat_conduction(&fes).unwrap();
    /// # let materials = element_field::material_field(&model, &[("k", 1.0)]).unwrap();
    /// # let k = matrix::stiffness(&model, &materials).unwrap();
    /// // The compact form and the named form say the same thing: names are
    /// // live in the table, the key carries only their index.
    /// let noms = k.dof_vars();
    /// let cles = k.row_dof_keys()?;
    /// let nommes = k.row_dofs()?;
    /// assert_eq!(dof_node(cles[0]), nommes[0].0);
    /// assert_eq!(noms[dof_var(cles[0]) as usize], nommes[0].1);
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn dof_vars(&self) -> std::sync::Arc<Vec<String>> {
        if let Some(a) = &self.assembled {
            return a.vars.clone();
        }
        std::sync::Arc::new(self.field_names())
    }

    /// Row DOFs as packed [`DofKey`]s, in CSR row order — the numbering the
    /// assembler and the solver index with.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::{dof_node, dof_var, Matrix};
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::containers::model::Model;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::{element_field, matrix};
    /// # use pyrucast::ops::model;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    /// # mesh.add_cell(&[a.id(), b.id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    /// # let model = model::heat_conduction(&fes).unwrap();
    /// # let materials = element_field::material_field(&model, &[("k", 1.0)]).unwrap();
    /// # let k = matrix::stiffness(&model, &materials).unwrap();
    /// // The compact form of a row DOF: the node, and the index of the
    /// // dual name in the table — not the name itself.
    /// let cles = k.row_dof_keys()?;
    /// assert_eq!(dof_node(cles[0]), a.id());
    /// assert_eq!(k.dof_vars()[dof_var(cles[0]) as usize], "q");
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn row_dof_keys(&self) -> Result<DofOrder> {
        if let Some(a) = &self.assembled {
            return Ok(a.row_keys.clone());
        }
        Ok(self.dof_key_orders()?.0)
    }

    /// Both DOF orders at once — what a caller needing the two should ask for.
    ///
    /// On a symmetric matrix the two are built in a single conjugate walk, so
    /// asking for them separately would run that walk twice for one half of its
    /// result each time.
    pub(crate) fn dof_key_orders(&self) -> Result<(DofOrder, DofOrder)> {
        if let Some(a) = &self.assembled {
            return Ok((a.row_keys.clone(), a.col_keys.clone()));
        }
        let vars = self.field_names();
        let (row, col) = self.dof_orders(&vars)?;
        Ok((std::sync::Arc::new(row), std::sync::Arc::new(col)))
    }

    /// Column DOFs as packed [`DofKey`]s, in CSR column order — the column twin
    /// of [`row_dof_keys`](Self::row_dof_keys).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::{dof_node, dof_var, Matrix};
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::containers::model::Model;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::{element_field, matrix};
    /// # use pyrucast::ops::model;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    /// # mesh.add_cell(&[a.id(), b.id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    /// # let model = model::heat_conduction(&fes).unwrap();
    /// # let materials = element_field::material_field(&model, &[("k", 1.0)]).unwrap();
    /// # let k = matrix::stiffness(&model, &materials).unwrap();
    /// // Same shape on the column side, over the **primal** variables.
    /// let cles = k.col_dof_keys()?;
    /// assert_eq!(dof_node(cles[0]), a.id());
    /// assert_eq!(k.dof_vars()[dof_var(cles[0]) as usize], "T");
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn col_dof_keys(&self) -> Result<DofOrder> {
        if let Some(a) = &self.assembled {
            return Ok(a.col_keys.clone());
        }
        Ok(self.dof_key_orders()?.1)
    }

    /// Union of all field names (dual + primal) across blocks.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::Matrix;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::containers::model::Model;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::{element_field, matrix};
    /// # use pyrucast::ops::model;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    /// # mesh.add_cell(&[a.id(), b.id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    /// # let model = model::heat_conduction(&fes).unwrap();
    /// # let materials = element_field::material_field(&model, &[("k", 1.0)]).unwrap();
    /// # let k = matrix::stiffness(&model, &materials).unwrap();
    /// // interned once only, rows and columns together.
    /// let noms = k.field_names();
    /// assert!(noms.contains(&"T".to_string()) && noms.contains(&"q".to_string()));
    /// ```
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::Matrix;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::containers::model::Model;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::{element_field, matrix};
    /// # use pyrucast::ops::model;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    /// # mesh.add_cell(&[a.id(), b.id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    /// # let model = model::heat_conduction(&fes).unwrap();
    /// # let materials = element_field::material_field(&model, &[("k", 1.0)]).unwrap();
    /// # let k = matrix::stiffness(&model, &materials).unwrap();
    /// // interned once only, rows and columns together.
    /// let noms = k.field_names();
    /// assert!(noms.contains(&"T".to_string()) && noms.contains(&"q".to_string()));
    /// ```
    pub fn field_names(&self) -> Vec<String> {
        let mut out: Vec<String> = Vec::new();
        for h in self {
            for name in h.read().field_names() {
                if !out.contains(&name) {
                    out.push(name);
                }
            }
        }
        out
    }

    /// Number of distinct row DOFs.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::Matrix;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::containers::model::Model;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::{element_field, matrix};
    /// # use pyrucast::ops::model;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    /// # mesh.add_cell(&[a.id(), b.id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    /// # let model = model::heat_conduction(&fes).unwrap();
    /// # let materials = element_field::material_field(&model, &[("k", 1.0)]).unwrap();
    /// # let k = matrix::stiffness(&model, &materials).unwrap();
    /// assert_eq!(k.n_rows().unwrap(), 2); // two nodes, one dual variable
    /// ```
    pub fn n_rows(&self) -> Result<usize> {
        Ok(self.row_dof_keys()?.len())
    }

    /// Number of distinct column DOFs.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::Matrix;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::containers::model::Model;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::{element_field, matrix};
    /// # use pyrucast::ops::model;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    /// # mesh.add_cell(&[a.id(), b.id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    /// # let model = model::heat_conduction(&fes).unwrap();
    /// # let materials = element_field::material_field(&model, &[("k", 1.0)]).unwrap();
    /// # let k = matrix::stiffness(&model, &materials).unwrap();
    /// assert_eq!(k.n_cols().unwrap(), 2);
    /// ```
    pub fn n_cols(&self) -> Result<usize> {
        Ok(self.col_dof_keys()?.len())
    }

    /// Total COO entries stored across all blocks (counting duplicates).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::Matrix;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::containers::model::Model;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::{element_field, matrix};
    /// # use pyrucast::ops::model;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    /// # mesh.add_cell(&[a.id(), b.id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    /// # let model = model::heat_conduction(&fes).unwrap();
    /// # let materials = element_field::material_field(&model, &[("k", 1.0)]).unwrap();
    /// # let k = matrix::stiffness(&model, &materials).unwrap();
    /// // Counts the entries **stored** in the blocks. A *computed* block — what
    /// // `stiffness` produces — stores none: its values are born at assembly
    /// // time and live in the global CSR.
    /// assert_eq!(k.entry_count(), 0);
    /// assert_eq!(k.to_csr().unwrap().nnz(), 4); // the CSR does carry them
    /// ```
    pub fn entry_count(&self) -> usize {
        let mut total = 0usize;
        for h in self {
            total += h.read().entry_count();
        }
        total
    }

    /// Heap bytes this matrix holds, estimated — what a big model actually
    /// spends before it reaches the solver.
    ///
    /// The assembled CSR dominates: per non-zero, a column index and a value,
    /// plus one offset per row and the packed DOF key of each row and column.
    /// The blocks' own entries ([`SubMatrix::memory_bytes`]) are added to it;
    /// a matrix straight out of [`stiffness`](crate::ops::matrix::stiffness)
    /// carries none, its blocks being computed.
    ///
    /// **What it cannot count: the factorization.** It is the largest of all —
    /// twenty to sixty-five times the matrix — but faer keeps the size of its
    /// factors private, so it is out of reach here.
    ///
    /// ```
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::{element_field, matrix, model};
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    /// # mesh.add_cell(&[a.id(), b.id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    /// # let model = model::heat_conduction(&fes).unwrap();
    /// # let materials = element_field::material_field(&model, &[("k", 1.0)]).unwrap();
    /// let k = matrix::stiffness(&model, &materials)?;
    /// // At least an index and a value per non-zero, counted independently on
    /// // the CSR itself.
    /// assert!(k.memory_bytes() >= k.to_csr()?.nnz() * 16);
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn memory_bytes(&self) -> usize {
        let mut total = 0usize;
        for h in self {
            total += h.read().memory_bytes();
        }
        if let Some(a) = &self.assembled {
            let word = std::mem::size_of::<usize>();
            total += a.csr.row_offsets.len() * word
                + a.csr.col_indices.len() * word
                + a.csr.values.len() * std::mem::size_of::<f64>()
                + (a.row_keys.len() + a.col_keys.len()) * std::mem::size_of::<DofKey>();
        }
        total
    }

    /// Sum of contributions at `(row, col)` across every block.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::Matrix;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::containers::model::Model;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::{element_field, matrix};
    /// # use pyrucast::ops::model;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    /// # mesh.add_cell(&[a.id(), b.id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    /// # let model = model::heat_conduction(&fes).unwrap();
    /// # let materials = element_field::material_field(&model, &[("k", 1.0)]).unwrap();
    /// # let k = matrix::stiffness(&model, &materials).unwrap();
    /// // Un SEG2 de longueur 1, k = 1 : K = [[1, -1], [-1, 1]].
    /// assert_eq!(k.get(a.id(), "q", a.id(), "T"), 1.0);
    /// assert_eq!(k.get(a.id(), "q", b.id(), "T"), -1.0);
    /// // A coordinate absent from the pattern is zero: reading does not raise.
    /// assert_eq!(k.get(a.id(), "q", a.id(), "absente"), 0.0);
    /// ```
    pub fn get(&self, row_node: NodeId, row_field: &str, col_node: NodeId, col_field: &str) -> f64 {
        // When assembled, the CSR is the source of truth — and the only place a
        // *computed* block's values live (its COO is empty). Fall back to the
        // per-block COO sum only for an unassembled (literal) matrix.
        if let Some(a) = &self.assembled {
            let slot = |name: &str| a.vars.iter().position(|v| v == name).map(|i| i as u32);
            let find = |keys: &[DofKey], node: NodeId, name: &str| {
                slot(name).and_then(|s| keys.iter().position(|&k| k == dof_key(node, s)))
            };
            let r = find(&a.row_keys, row_node, row_field);
            let c = find(&a.col_keys, col_node, col_field);
            return match (r, c) {
                (Some(r), Some(c)) => a.csr.slot(r, c).map_or(0.0, |k| a.csr.values[k]),
                _ => 0.0,
            };
        }
        let mut total = 0.0;
        for h in self {
            total += h.read().get(row_node, row_field, col_node, col_field);
        }
        total
    }

    /// All COO entries across every block, in block-insertion order.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::Matrix;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::containers::model::Model;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::{element_field, matrix};
    /// # use pyrucast::ops::model;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    /// # mesh.add_cell(&[a.id(), b.id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    /// # let model = model::heat_conduction(&fes).unwrap();
    /// # let materials = element_field::material_field(&model, &[("k", 1.0)]).unwrap();
    /// # let k = matrix::stiffness(&model, &materials).unwrap();
    /// // One 5-tuple per entry: row node, dual var, column node,
    /// // primal var, value — names are already resolved there. Like
    /// // `entry_count` walks the entries **stored**: a
    /// // computed one has none, its values living in the global CSR.
    /// assert!(k.iter_entries().is_empty());
    /// ```
    pub fn iter_entries(&self) -> Vec<MatrixEntry> {
        let mut out = Vec::new();
        for h in self {
            out.extend(h.read().iter_entries());
        }
        out
    }

    // ── Solver-facing (require finalize) ────────────────────────────────

    /// Assembled CSR, **materialised**. Requires [`finalize`](Self::finalize).
    ///
    /// The assembled state holds the sparsity by `Arc`, shared with the
    /// assembler's memoised pattern, and only the values as its own. Building a
    /// `CsrMatrix` — which owns all three arrays — therefore copies the
    /// sparsity, so ask for one only where that type is genuinely needed; to
    /// read the assembled matrix, [`csr_arrays`](Self::csr_arrays) borrows it.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::Matrix;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::containers::model::Model;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::{element_field, matrix};
    /// # use pyrucast::ops::model;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    /// # mesh.add_cell(&[a.id(), b.id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    /// # let model = model::heat_conduction(&fes).unwrap();
    /// # let materials = element_field::material_field(&model, &[("k", 1.0)]).unwrap();
    /// # let k = matrix::stiffness(&model, &materials).unwrap();
    /// // The CSR **is** the assembled state: borrowed, not rebuilt.
    /// assert_eq!(k.to_csr().unwrap().nnz(), 4);
    /// ```
    pub fn to_csr(&self) -> Result<CsrMatrix<f64>> {
        self.assembled_or_err()?.csr.to_csr()
    }

    /// The assembled CSR **borrowed**: `(row_offsets, col_indices, values)`.
    /// Requires [`finalize`](Self::finalize).
    ///
    /// The reading form of [`to_csr`](Self::to_csr), copying nothing.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::Matrix;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::containers::model::Model;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::{element_field, matrix};
    /// # use pyrucast::ops::model;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    /// # mesh.add_cell(&[a.id(), b.id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    /// # let model = model::heat_conduction(&fes).unwrap();
    /// # let materials = element_field::material_field(&model, &[("k", 1.0)]).unwrap();
    /// # let k = matrix::stiffness(&model, &materials).unwrap();
    /// // The CSR's three arrays, borrowed — nothing is copied.
    /// let (offsets, cols, values) = k.csr_arrays()?;
    /// assert_eq!(offsets.len(), k.n_rows()? + 1);
    /// assert_eq!(cols.len(), values.len());
    /// assert_eq!(values.len(), k.to_csr()?.nnz());
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn csr_arrays(&self) -> Result<(&[usize], &[usize], &[f64])> {
        let a = self.assembled_or_err()?;
        Ok((&a.csr.row_offsets, &a.csr.col_indices, &a.csr.values))
    }

    /// Assembled dense matrix. Requires [`finalize`](Self::finalize).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::Matrix;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::containers::model::Model;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::{element_field, matrix};
    /// # use pyrucast::ops::model;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    /// # mesh.add_cell(&[a.id(), b.id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    /// # let model = model::heat_conduction(&fes).unwrap();
    /// # let materials = element_field::material_field(&model, &[("k", 1.0)]).unwrap();
    /// # let k = matrix::stiffness(&model, &materials).unwrap();
    /// // A nalgebra view (column-major), ready for LU or Cholesky.
    /// let m = k.to_dmatrix().unwrap();
    /// assert_eq!((m.nrows(), m.ncols()), (2, 2));
    /// ```
    pub fn to_dmatrix(&self) -> Result<DMatrix<f64>> {
        let a = self.assembled_or_err()?;
        let nr = a.row_keys.len();
        let nc = a.col_keys.len();
        let mut out = DMatrix::<f64>::zeros(nr, nc);
        for (r, c, v) in a.csr.triplets() {
            out[(r, c)] += v;
        }
        Ok(out)
    }

    /// Row-major dense buffer. Requires [`finalize`](Self::finalize).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::Matrix;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::containers::model::Model;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::{element_field, matrix};
    /// # use pyrucast::ops::model;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    /// # mesh.add_cell(&[a.id(), b.id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    /// # let model = model::heat_conduction(&fes).unwrap();
    /// # let materials = element_field::material_field(&model, &[("k", 1.0)]).unwrap();
    /// # let k = matrix::stiffness(&model, &materials).unwrap();
    /// // Vue dense ligne-major, longueur n_rows × n_cols.
    /// let d = k.dense().unwrap();
    /// assert_eq!(d.len(), k.n_rows().unwrap() * k.n_cols().unwrap());
    /// assert_eq!(d, vec![1.0, -1.0, -1.0, 1.0]);
    /// ```
    pub fn dense(&self) -> Result<Vec<f64>> {
        let m = self.to_dmatrix()?;
        let mut out = Vec::with_capacity(m.nrows() * m.ncols());
        for i in 0..m.nrows() {
            for j in 0..m.ncols() {
                out.push(m[(i, j)]);
            }
        }
        Ok(out)
    }

    /// Assembled COO (rebuilt from the CSR). Requires [`finalize`](Self::finalize).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::Matrix;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::containers::model::Model;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::{element_field, matrix};
    /// # use pyrucast::ops::model;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    /// # mesh.add_cell(&[a.id(), b.id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    /// # let model = model::heat_conduction(&fes).unwrap();
    /// # let materials = element_field::material_field(&model, &[("k", 1.0)]).unwrap();
    /// # let k = matrix::stiffness(&model, &materials).unwrap();
    /// assert_eq!(k.to_coo().unwrap().nnz(), 4);
    /// ```
    pub fn to_coo(&self) -> Result<CooMatrix<f64>> {
        let a = self.assembled_or_err()?;
        let mut coo = CooMatrix::<f64>::new(a.row_keys.len(), a.col_keys.len());
        for (r, c, v) in a.csr.triplets() {
            coo.push(r, c, v);
        }
        Ok(coo)
    }

    /// Assembled CSC. Requires [`finalize`](Self::finalize).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::Matrix;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::containers::model::Model;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::{element_field, matrix};
    /// # use pyrucast::ops::model;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    /// # mesh.add_cell(&[a.id(), b.id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    /// # let model = model::heat_conduction(&fes).unwrap();
    /// # let materials = element_field::material_field(&model, &[("k", 1.0)]).unwrap();
    /// # let k = matrix::stiffness(&model, &materials).unwrap();
    /// assert_eq!(k.to_csc().unwrap().ncols(), 2);
    /// ```
    pub fn to_csc(&self) -> Result<CscMatrix<f64>> {
        Ok(CscMatrix::from(&self.assembled_or_err()?.csr.to_csr()?))
    }

    /// `y = A · x` (dense). Requires [`finalize`](Self::finalize).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::Matrix;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::containers::model::Model;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::{element_field, matrix};
    /// # use pyrucast::ops::model;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    /// # mesh.add_cell(&[a.id(), b.id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    /// # let model = model::heat_conduction(&fes).unwrap();
    /// # let materials = element_field::material_field(&model, &[("k", 1.0)]).unwrap();
    /// # let k = matrix::stiffness(&model, &materials).unwrap();
    /// // K · [1, 1] = 0: the rigid mode of a conduction.
    /// assert_eq!(k.mul_dense(&[1.0, 1.0]).unwrap(), vec![0.0, 0.0]);
    /// ```
    pub fn mul_dense(&self, x: &[f64]) -> Result<Vec<f64>> {
        let a = self.assembled_or_err()?;
        let nc = a.col_keys.len();
        if x.len() != nc {
            return Err(PyrucastError::Message(format!(
                "mul_dense: x has length {} but matrix has {} columns",
                x.len(),
                nc
            )));
        }
        // Row-wise SpMV straight on the CSR arrays: each row's dot product is
        // written once, so the result is independent of the thread count.
        let (offsets, cols, vals) = (&a.csr.row_offsets, &a.csr.col_indices, &a.csr.values);
        let mut y = vec![0.0f64; a.csr.nrows()];
        y.par_iter_mut()
            .with_min_len(MIN_PARALLEL_LEN)
            .enumerate()
            .for_each(|(r, out)| {
                let (lo, hi) = (offsets[r], offsets[r + 1]);
                *out = (lo..hi).map(|k| vals[k] * x[cols[k]]).sum();
            });
        Ok(y)
    }

    /// [`Mesh`] over the blocks' distinct **row** supports (the dual side:
    /// where a right-hand side is read and where `A · x` lands) — one POI1
    /// submesh per distinct support, **sharing the blocks' own handles**
    /// (nothing copied, first-seen order).
    ///
    /// The projection target for building a field that combines with this
    /// matrix's row-side fields: `restrict(&f_ext, &k.row_mesh()?)` lands the
    /// external forces on the very supports [`mul_field`](Self::mul_field)
    /// (internal forces `K·u`) lives on, so `&f_ext_r - &f_int` aligns zone by
    /// zone instead of passing through. Available before
    /// [`finalize`](Self::finalize) (the supports are structural).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::Matrix;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::containers::model::Model;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::{element_field, matrix};
    /// # use pyrucast::ops::model;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    /// # mesh.add_cell(&[a.id(), b.id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    /// # let model = model::heat_conduction(&fes).unwrap();
    /// # let materials = element_field::material_field(&model, &[("k", 1.0)]).unwrap();
    /// # let k = matrix::stiffness(&model, &materials).unwrap();
    /// // Le support **ligne** : la cible de projection d'un second membre.
    /// assert_eq!(k.row_mesh().unwrap().cell_count(), 2);
    /// ```
    pub fn row_mesh(&self) -> Result<Mesh> {
        self.support_mesh(true)
    }

    /// [`Mesh`] over the blocks' distinct **column** supports (the primal
    /// side: where a `solve` solution lives). Column twin of
    /// [`row_mesh`](Self::row_mesh) — e.g. to project an initial or imposed
    /// field onto the exact supports of the solution before combining.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::Matrix;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::containers::model::Model;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::{element_field, matrix};
    /// # use pyrucast::ops::model;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    /// # mesh.add_cell(&[a.id(), b.id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    /// # let model = model::heat_conduction(&fes).unwrap();
    /// # let materials = element_field::material_field(&model, &[("k", 1.0)]).unwrap();
    /// # let k = matrix::stiffness(&model, &materials).unwrap();
    /// // The **column** support: that of a field being multiplied.
    /// assert_eq!(k.col_mesh().unwrap().cell_count(), 2);
    /// ```
    pub fn col_mesh(&self) -> Result<Mesh> {
        self.support_mesh(false)
    }

    /// A fresh [`Matrix`] holding only the blocks **whose nature set contains** the
    /// given [`Physics`] (`k.filter(Physics::Mechanical)` → every block that is at
    /// least mechanical). The matrix-side twin of
    /// [`Model::filter`](crate::containers::model::Model::filter).
    ///
    /// Block order is preserved and handles are **shared** (refcount bump) via
    /// [`Aggregate::subset`]. Blocks with an **empty** nature set (built outside
    /// assembly — the « rien » case) are never selected by a concrete nature; tag
    /// them [`Physics::Other`] to reach them with `filter(Physics::Other)`. The
    /// result is **not assembled** — like any matrix with freshly added blocks,
    /// call [`Matrix::assemble`] before handing it to a solver.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::Matrix;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::containers::model::Model;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::{element_field, matrix};
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    /// # mesh.add_cell(&[a.id(), b.id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    /// # let model = model::heat_conduction(&fes).unwrap();
    /// # let materials = element_field::material_field(&model, &[("k", 1.0)]).unwrap();
    /// # let k = matrix::stiffness(&model, &materials).unwrap();
    /// # use pyrucast::models::Physics;
    /// # use pyrucast::ops::model;
    /// // The result is **not** assembled: call `assemble` before solving.
    /// let thermique = k.filter(Physics::Thermal);
    /// assert_eq!(thermique.len(), 1);
    /// assert!(k.filter(Physics::Mechanical).is_empty());
    /// ```
    pub fn filter(&self, physics: Physics) -> Matrix {
        // Built block by block rather than through `subset`: that one is
        // fallible on an out-of-range index, which selecting by predicate
        // cannot produce — and `Matrix` is the one aggregate with no
        // `check_push`, so appending never fails either.
        let mut out = Matrix::empty();
        for h in self {
            if h.read().physics().contains(&physics) {
                out.push(h.clone());
            }
        }
        out.post_push();
        out
    }

    /// The set of [`Physics`] natures present across this matrix's blocks —
    /// first-seen order, deduplicated. Empty if no block is tagged (« rien »).
    /// A matrix aggregating several physics reports **several** tags here (e.g.
    /// a heat model with a Dirichlet → `[Thermal, Constraint]`).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::matrix::Matrix;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::containers::model::Model;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::ops::{element_field, matrix};
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    /// # mesh.add_cell(&[a.id(), b.id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    /// # let model = model::heat_conduction(&fes).unwrap();
    /// # let materials = element_field::material_field(&model, &[("k", 1.0)]).unwrap();
    /// # let k = matrix::stiffness(&model, &materials).unwrap();
    /// # use pyrucast::models::Physics;
    /// # use pyrucast::ops::model;
    /// // The kinds **present**, deduplicated.
    /// assert!(k.physics().contains(&Physics::Thermal));
    /// ```
    pub fn physics(&self) -> Vec<Physics> {
        let mut out: Vec<Physics> = Vec::new();
        for h in self {
            for &p in h.read().physics() {
                if !out.contains(&p) {
                    out.push(p);
                }
            }
        }
        out
    }

    /// Shared body of `row_mesh` / `col_mesh`.
    fn support_mesh(&self, rows: bool) -> Result<Mesh> {
        let mut out = Mesh::empty();
        for h in self {
            let s = h.read();
            let sup = if rows {
                s.row_support.clone()
            } else {
                s.col_support.clone()
            };
            if !out.iter().any(|m| m.same_object(&sup)) {
                out.add_sub(sup)?;
            }
        }
        Ok(out)
    }

    /// Wrap a flat **column-ordered** vector (`assembled.col_dofs` order — the
    /// layout a solver's solution comes in) into a [`NodeField`] whose zones
    /// **share the blocks' `col_support` handles**: one zone per distinct
    /// column support, carrying the union of the primal variables the blocks
    /// declare on it. No support `SubMesh` is materialised — the zones sit on
    /// the very POI1 supports the sub-models built once at construction, so
    /// the output satisfies [`same_support`](crate::containers::field::SubField::same_support)
    /// with any other field on those supports, across solves and re-assemblies.
    ///
    /// A `(node, variable)` pair a support carries but the DOF table does not
    /// reads as `0.0` (the [`NodeField::gather`] convention). Interface nodes
    /// shared by several supports are stored once per zone, with equal values
    /// by construction (they come from the same vector).
    ///
    /// Requires [`finalize`](Self::finalize).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, Matrix, SubMatrix, Symmetry};
    /// # use pyrucast::containers::mesh::SubMesh;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = {
    /// #     let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
    /// #     sm.add_cell(&[a.id()]).unwrap();
    /// #     sm.add_cell(&[b.id()]).unwrap();
    /// #     Handle::new(sm)
    /// # };
    /// # let bloc = || {
    /// #     let mut z = SubMatrix::new(
    /// #         support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #         DofOrdering::NodesThenVars, Symmetry::Full);
    /// #     z.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// #     z.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// #     z
    /// # };
    /// # use pyrucast::containers::field::SubField;
    /// let mut k = Matrix::empty();
    /// k.add_sub(Handle::new(bloc()))?;
    /// k.finalize()?;
    /// // A flat vector, in column DOF order, becomes a field again —
    /// // laid on the **existing** POI1 supports, materializing no other.
    /// let f = k.field_from_col_values(&[10.0, 20.0])?;
    /// assert_eq!(f.get(0)?.read().value(b.id(), "T")?, 20.0);
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn field_from_col_values(&self, x: &[f64]) -> Result<NodeField> {
        self.field_from_flat_values(x, false)
    }

    /// Row-side twin of [`field_from_col_values`](Self::field_from_col_values):
    /// wrap a flat **row-ordered** vector (`assembled.row_dofs` order — the
    /// layout `A · x` comes in) into a [`NodeField`] whose zones share the
    /// blocks' `row_support` handles and carry their dual variables.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, Matrix, SubMatrix, Symmetry};
    /// # use pyrucast::containers::mesh::SubMesh;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = {
    /// #     let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
    /// #     sm.add_cell(&[a.id()]).unwrap();
    /// #     sm.add_cell(&[b.id()]).unwrap();
    /// #     Handle::new(sm)
    /// # };
    /// # let bloc = || {
    /// #     let mut z = SubMatrix::new(
    /// #         support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #         DofOrdering::NodesThenVars, Symmetry::Full);
    /// #     z.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// #     z.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// #     z
    /// # };
    /// # use pyrucast::containers::field::SubField;
    /// let mut k = Matrix::empty();
    /// k.add_sub(Handle::new(bloc()))?;
    /// k.finalize()?;
    /// // Le jumeau côté lignes : la disposition dans laquelle arrive `A · x`,
    /// // and whose components are the **dual** variables.
    /// let f = k.field_from_row_values(&[1.0, 2.0])?;
    /// assert_eq!(f.get(0)?.read().value(b.id(), "q")?, 2.0);
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn field_from_row_values(&self, y: &[f64]) -> Result<NodeField> {
        self.field_from_flat_values(y, true)
    }

    /// Shared body of `field_from_{col,row}_values` (`rows` picks the side).
    fn field_from_flat_values(&self, values: &[f64], rows: bool) -> Result<NodeField> {
        let a = self.assembled_or_err()?;
        let keys = if rows { &a.row_keys } else { &a.col_keys };
        if values.len() != keys.len() {
            return Err(PyrucastError::Message(format!(
                "field_from_{}_values: {} value(s) for {} DOF(s)",
                if rows { "row" } else { "col" },
                values.len(),
                keys.len()
            )));
        }
        // Global DOF key → flat index, one hash pass over eight-byte keys.
        let index: HashMap<DofKey, usize> = keys.iter().enumerate().map(|(i, &k)| (k, i)).collect();
        let slot_of: HashMap<&str, u32> = a
            .vars
            .iter()
            .enumerate()
            .map(|(i, v)| (v.as_str(), i as u32))
            .collect();

        // Group the blocks by support slot; union their variables per group.
        // Same slot ⇒ same sealed POI1 ⇒ same node list, read from the support.
        struct Group {
            support: Handle<SubMesh>,
            vars: Vec<String>,
        }
        let mut groups: Vec<Group> = Vec::new();
        for h in self {
            let s = h.read();
            let (support, vars) = if rows {
                (s.row_support().clone(), s.dual_vars())
            } else {
                (s.col_support().clone(), s.primal_vars())
            };
            match groups.iter_mut().find(|g| g.support.same_object(&support)) {
                Some(g) => {
                    for v in vars {
                        if !g.vars.contains(v) {
                            g.vars.push(v.clone());
                        }
                    }
                }
                None => groups.push(Group {
                    support,
                    vars: vars.to_vec(),
                }),
            }
        }

        // One zone per group, on the block's own support handle. The field's
        // row order is the support's cell order, which is the very connectivity
        // read below, so values are written positionally, no per-node lookup.
        let mut out = NodeField::default();
        for g in &groups {
            use crate::containers::field::SubField;
            // Before the guard: `from_poi1` seals, which reads this same handle.
            let mut sub = SubNodeField::from_poi1(&g.support, g.vars.clone())?;
            let ncomp = g.vars.len();
            let vals = sub.values_mut();
            // The variables are resolved to slots once per zone, never per node.
            let slots: Vec<Option<u32>> = g
                .vars
                .iter()
                .map(|v| slot_of.get(v.as_str()).copied())
                .collect();
            let support = g.support.read();
            for (ni, nid) in support.connectivity().iter().enumerate() {
                for (ci, slot) in slots.iter().enumerate() {
                    let Some(slot) = slot else { continue };
                    if let Some(&gi) = index.get(&dof_key(*nid, *slot)) {
                        vals[ni * ncomp + ci] = values[gi];
                    }
                }
            }
            drop(support);
            out.add_sub(Handle::new(sub))?;
        }
        Ok(out)
    }

    /// `y = A · x` against a [`NodeField`]. The column vector `x` is read from
    /// `x_field` at the matrix's **column** DOFs (aggregate resolution, first
    /// zone wins; a DOF no zone defines reads as `0.0`); the result `y` is a
    /// `NodeField` whose zones **share the blocks' row supports**
    /// ([`field_from_row_values`](Self::field_from_row_values)) and carry
    /// their dual variables.
    ///
    /// Columns carry the **primal** variables and rows the **dual** ones
    /// (`K · u = f`), so this maps a *primal* field (e.g. `"T"`, `"u"`) to a
    /// *dual* one (e.g. `"q"`). That is the exact mirror of
    /// [`crate::ops::solver::lu::solve`], which reads a *dual* right-hand side at the
    /// rows and produces a *primal* solution at the columns. Both use
    /// [`NodeField::gather`] / [`NodeField::from_dof_values`] to bridge the
    /// abstract field and the flat DOF vector. Requires
    /// [`finalize`](Self::finalize); the `*` operator (`&matrix * &field`) is
    /// sugar for this method.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::matrix::{DofOrdering, Matrix, SubMatrix, Symmetry};
    /// # use pyrucast::containers::mesh::SubMesh;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
    /// # let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
    /// # let support = {
    /// #     let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
    /// #     sm.add_cell(&[a.id()]).unwrap();
    /// #     sm.add_cell(&[b.id()]).unwrap();
    /// #     Handle::new(sm)
    /// # };
    /// # let bloc = || {
    /// #     let mut z = SubMatrix::new(
    /// #         support.clone(), support.clone(), vec!["q".into()], vec!["T".into()],
    /// #         DofOrdering::NodesThenVars, Symmetry::Full);
    /// #     z.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
    /// #     z.add_entry(a.id(), "q", b.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", a.id(), "T", -1.0).unwrap();
    /// #     z.add_entry(b.id(), "q", b.id(), "T", 2.0).unwrap();
    /// #     z
    /// # };
    /// # use pyrucast::containers::field::SubField;
    /// # use pyrucast::containers::node_field::NodeField;
    /// let mut k = Matrix::empty();
    /// k.add_sub(Handle::new(bloc()))?;
    /// k.finalize()?;
    /// // `x` is read at the **column** DOFs (primal), the result lives on the
    /// // **Row** DOFs (dual): K · u = f. The `*` operator is its sugar.
    /// let x = NodeField::from_submesh(&k.col_mesh()?.get(0)?, vec!["T".into()])?;
    /// x.get(0)?.write().add_to_component("T", 1.0)?;
    /// let y = k.mul_field(&x)?;
    /// // This block's rows sum to 1, hence K · 1 = 1.
    /// assert_eq!(y.get(0)?.read().value(a.id(), "q")?, 1.0);
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn mul_field(&self, x_field: &NodeField) -> Result<NodeField> {
        let x = x_field.gather(&self.col_dofs()?)?;
        let y = self.mul_dense(&x)?;
        self.field_from_row_values(&y)
    }

    /// A fresh [`Matrix`] with every block replaced by `f(block.clone())`, each
    /// re-inserted under a **new store slot**. Never mutates `self` or any of its
    /// blocks in place: `add_sub`/`union`/`filter` share `Handle<SubMatrix>`s
    /// (same store slot, refcount bump — see [`Aggregate::subset`]), so mutating
    /// one would silently reach every other `Matrix` aliasing it. Like
    /// [`filter`](Self::filter), the result is **not assembled**.
    fn map_blocks(&self, f: impl Fn(SubMatrix) -> SubMatrix) -> Matrix {
        let mut out = Matrix::empty();
        for h in self {
            let mapped = f((*h.read()).clone());
            out.push(Handle::new(mapped));
        }
        out.post_push();
        out
    }

    /// A fresh [`Matrix`] whose values are `self`'s put through `f` — the engine
    /// behind `Mul<f64>`, `Div<f64>` and `Neg`, which pass `|v| v * s`,
    /// `|v| v / s` and `|v| -v`.
    ///
    /// `f` lands on each block's [`factor`](SubMatrix::factor), never on the
    /// stored `coo`, so a computed block scales exactly like a literal one.
    ///
    /// **The assembled CSR comes along, scaled.** Scaling every block by the
    /// same amount scales every entry by it, leaving the sparsity untouched, so
    /// the assembly stays valid: only `values` is walked. Dropping it instead —
    /// as this did until the operators were reworked — meant the mandatory
    /// `assemble()` re-ran every element kernel over the whole mesh to apply one
    /// scalar. The index arrays and the name table are `Arc`s and are shared
    /// rather than copied ([`AssembledCsr`]). Note that `(Σ v) · s` is not, bit
    /// for bit, the `Σ (v · s)` a re-assembly would compute: both are the same
    /// quantity to within rounding, each reproducible.
    ///
    /// The cached factorization is *not* carried over: the fresh `Matrix` starts
    /// without one.
    fn scaled(&self, f: impl Fn(f64) -> f64) -> Matrix {
        let mut out = self.map_blocks(|mut b| {
            b.factor = f(b.factor);
            b
        });
        // After `map_blocks`, whose `post_push` has just cleared this.
        out.assembled = self.assembled.as_ref().map(|a| AssembledData {
            vars: a.vars.clone(),
            row_keys: a.row_keys.clone(),
            col_keys: a.col_keys.clone(),
            csr: AssembledCsr {
                row_offsets: a.csr.row_offsets.clone(),
                col_indices: a.csr.col_indices.clone(),
                values: a.csr.values.iter().map(|&v| f(v)).collect(),
                ncols: a.csr.ncols,
            },
        });
        out
    }

    /// [`scaled`](Self::scaled) for an owner: same result, but a block **no one
    /// else holds** is rescaled where it lies instead of being copied.
    ///
    /// A block is rebuilt only to protect the other matrices sharing it. When
    /// [`Handle::is_sole_owner`] says there are none — the common case for a
    /// matrix just back from [`crate::ops::matrix::stiffness`] — there is nobody
    /// to protect, and a literal block's COO need not be copied at all.
    fn scale_in_place(&mut self, f: impl Fn(f64) -> f64) {
        for h in self.items_mut() {
            if h.is_sole_owner() {
                let mut blk = h.write();
                blk.factor = f(blk.factor);
            } else {
                let mut blk = (*h.read()).clone();
                blk.factor = f(blk.factor);
                *h = Handle::new(blk);
            }
        }
        if let Some(a) = &mut self.assembled {
            for v in &mut a.csr.values {
                *v = f(*v);
            }
        }
        // Scaled values ⇒ any factorization of the old ones is stale. Done by
        // hand: nothing was pushed, so `post_push` never ran.
        *self.factorization.get_mut() = None;
    }

    /// A fresh [`Matrix`] holding `self`'s blocks then `other`'s, **shared** and
    /// deliberately **not** deduplicated — the engine behind `Add`/`Sub`.
    ///
    /// This is what parts `+` from `|`: [`Aggregate::union`] drops a block whose
    /// slot it already holds, so `k | k` is `k`, whereas a sum must count a
    /// contribution as many times as it is handed over — `k + k` is `2k`. The
    /// assembler sums whatever lands on the same global `(row, col)` and walks
    /// blocks by position, so a block present twice simply contributes twice.
    ///
    /// Like [`filter`](Self::filter), the result is **not assembled**.
    fn concat(&self, other: &Matrix) -> Matrix {
        let mut out = Matrix::empty();
        for h in self.iter().chain(other.iter()) {
            out.push(h.clone());
        }
        out.post_push();
        out
    }
}

impl std::ops::Mul<&NodeField> for &Matrix {
    type Output = Result<NodeField>;
    /// `&matrix * &field` — sugar for [`Matrix::mul_field`]. Fallible (the
    /// matrix must be finalized), so the result is a `Result`: `(&k * &x)?`.
    fn mul(self, rhs: &NodeField) -> Self::Output {
        self.mul_field(rhs)
    }
}

impl std::ops::Mul<&NodeField> for Matrix {
    type Output = Result<NodeField>;
    fn mul(self, rhs: &NodeField) -> Self::Output {
        self.mul_field(rhs)
    }
}

// ─── Matrix scalar operators ────────────────────────────────────────────────
//
// `&matrix * s`, `/ s` and `-matrix` — a fresh `Matrix` whose blocks are scaled
// clones of `self`'s, carrying the assembled CSR along, scaled (see `scaled`).
// The owning forms rescale in place whatever block no one else holds
// (`scale_in_place`). **Infallible**: cloning a block and appending it cannot
// fail, `Matrix` being the one aggregate that declares no `check_push` — except
// a division, which refuses a divisor no matrix can survive (`check_divisor`).

impl std::ops::Mul<f64> for &Matrix {
    type Output = Matrix;
    fn mul(self, rhs: f64) -> Self::Output {
        self.scaled(|v| v * rhs)
    }
}

impl std::ops::Mul<f64> for Matrix {
    type Output = Matrix;
    fn mul(mut self, rhs: f64) -> Self::Output {
        self.scale_in_place(|v| v * rhs);
        self
    }
}

// Scalar on the left — `2.0 * k` reads as the mathematics does.
impl std::ops::Mul<Matrix> for f64 {
    type Output = Matrix;
    fn mul(self, rhs: Matrix) -> Matrix {
        rhs * self
    }
}

impl std::ops::Mul<&Matrix> for f64 {
    type Output = Matrix;
    fn mul(self, rhs: &Matrix) -> Matrix {
        rhs * self
    }
}

impl std::ops::Div<f64> for &Matrix {
    type Output = Matrix;
    #[track_caller]
    fn div(self, rhs: f64) -> Self::Output {
        let d = check_divisor(rhs);
        self.scaled(|v| v / d)
    }
}

impl std::ops::Div<f64> for Matrix {
    type Output = Matrix;
    #[track_caller]
    fn div(mut self, rhs: f64) -> Self::Output {
        let d = check_divisor(rhs);
        self.scale_in_place(|v| v / d);
        self
    }
}

impl std::ops::Neg for &Matrix {
    type Output = Matrix;
    fn neg(self) -> Matrix {
        self.scaled(|v| -v)
    }
}

impl std::ops::Neg for Matrix {
    type Output = Matrix;
    fn neg(mut self) -> Matrix {
        self.scale_in_place(|v| -v);
        self
    }
}

// ─── Matrix algebra: `a + b`, `a - b` ───────────────────────────────────────
//
// A sum is **structural**: the result holds the blocks of both operands, shared
// and not deduplicated, and the assembler does the adding — it already sums
// whatever lands on the same global `(row, col)`. So `M/dt + K` costs a handful
// of refcount bumps, no value is touched, and a computed block stays computed.
// Like `filter`, the result is not assembled: `assemble()` before solving.
//
// This is where `+` parts from `|`. `union` drops a block it already holds, so
// `k | k` is `k` — right for composing an operator out of distinct pieces,
// wrong for a sum, which must count a contribution once per handing over.
// `k + k` is `2k`. Reach for `|` to assemble one operator from its parts, for
// `+` to add two operators.
//
// `a - b` negates `b`'s blocks, which copies them (the factor lives in the
// block); `a + b` copies nothing.

impl SubMatrix {
    /// This block alone in a fresh one-block [`Matrix`], copied into a new store
    /// slot — the operand the mixed block/matrix sums are built from.
    fn as_matrix(&self) -> Matrix {
        let mut out = Matrix::empty();
        out.push(Handle::new(self.clone()));
        out.post_push();
        out
    }

    /// This block negated, alone in a fresh [`Matrix`] — the right-hand side of
    /// a subtraction.
    fn as_negated_matrix(&self) -> Matrix {
        (-self).as_matrix()
    }
}

/// Write the three owned/borrowed variants of a binary operator whose reference
/// form (`&lhs op &rhs`) is already spelled out just above.
macro_rules! forward_ref_binop {
    ($trait:ident, $method:ident, $lhs:ty, $rhs:ty) => {
        impl std::ops::$trait<$rhs> for &$lhs {
            type Output = Matrix;
            fn $method(self, rhs: $rhs) -> Matrix {
                std::ops::$trait::$method(self, &rhs)
            }
        }
        impl std::ops::$trait<&$rhs> for $lhs {
            type Output = Matrix;
            fn $method(self, rhs: &$rhs) -> Matrix {
                std::ops::$trait::$method(&self, rhs)
            }
        }
        impl std::ops::$trait<$rhs> for $lhs {
            type Output = Matrix;
            fn $method(self, rhs: $rhs) -> Matrix {
                std::ops::$trait::$method(&self, &rhs)
            }
        }
    };
}

impl std::ops::Add<&Matrix> for &Matrix {
    type Output = Matrix;
    fn add(self, rhs: &Matrix) -> Matrix {
        self.concat(rhs)
    }
}

impl std::ops::Sub<&Matrix> for &Matrix {
    type Output = Matrix;
    fn sub(self, rhs: &Matrix) -> Matrix {
        self.concat(&rhs.map_blocks(|b| -b))
    }
}

impl std::ops::Add<&SubMatrix> for &Matrix {
    type Output = Matrix;
    fn add(self, rhs: &SubMatrix) -> Matrix {
        self.concat(&rhs.as_matrix())
    }
}

impl std::ops::Sub<&SubMatrix> for &Matrix {
    type Output = Matrix;
    fn sub(self, rhs: &SubMatrix) -> Matrix {
        self.concat(&rhs.as_negated_matrix())
    }
}

impl std::ops::Add<&Matrix> for &SubMatrix {
    type Output = Matrix;
    fn add(self, rhs: &Matrix) -> Matrix {
        self.as_matrix().concat(rhs)
    }
}

impl std::ops::Sub<&Matrix> for &SubMatrix {
    type Output = Matrix;
    fn sub(self, rhs: &Matrix) -> Matrix {
        self.as_matrix().concat(&rhs.map_blocks(|b| -b))
    }
}

impl std::ops::Add<&SubMatrix> for &SubMatrix {
    type Output = Matrix;
    fn add(self, rhs: &SubMatrix) -> Matrix {
        self.as_matrix().concat(&rhs.as_matrix())
    }
}

impl std::ops::Sub<&SubMatrix> for &SubMatrix {
    type Output = Matrix;
    fn sub(self, rhs: &SubMatrix) -> Matrix {
        self.as_matrix().concat(&rhs.as_negated_matrix())
    }
}

forward_ref_binop!(Add, add, Matrix, Matrix);
forward_ref_binop!(Sub, sub, Matrix, Matrix);
forward_ref_binop!(Add, add, Matrix, SubMatrix);
forward_ref_binop!(Sub, sub, Matrix, SubMatrix);
forward_ref_binop!(Add, add, SubMatrix, Matrix);
forward_ref_binop!(Sub, sub, SubMatrix, Matrix);
forward_ref_binop!(Add, add, SubMatrix, SubMatrix);
forward_ref_binop!(Sub, sub, SubMatrix, SubMatrix);

impl crate::dump::Dump for Matrix {
    fn render(&self, opts: &crate::dump::DumpOptions) -> String {
        // Build the global labelled grid on the fly — `collect_*_dofs` and
        // `build_global_triplets` take `&self`, so no `finalize()` (which needs
        // `&mut`) is required: a matrix dumps the same content whether assembled
        // or not.
        let grid = (|| -> Result<String> {
            // When assembled, dump the cached CSR (the single source of truth —
            // and the only correct view for a matrix with *computed* blocks,
            // whose values the literal triplet path does not carry). Otherwise
            // build the labelled grid on the fly from the literal blocks.
            let (row_dofs, col_dofs, data) = if let Some(a) = &self.assembled {
                let nc = a.col_keys.len();
                let mut data = vec![0.0f64; a.row_keys.len() * nc];
                for (r, c, v) in a.csr.triplets() {
                    data[r * nc + c] = v;
                }
                (
                    Self::name_dofs(&a.row_keys, &a.vars),
                    Self::name_dofs(&a.col_keys, &a.vars),
                    data,
                )
            } else {
                let vars = self.field_names();
                let (row_keys, col_keys) = self.dof_orders(&vars)?;
                let triplets = self.build_global_triplets(&vars, &row_keys, &col_keys)?;
                let row_dofs = Self::name_dofs(&row_keys, &vars);
                let col_dofs = Self::name_dofs(&col_keys, &vars);
                let nc = col_dofs.len();
                let mut data = vec![0.0f64; row_dofs.len() * nc];
                for (r, c, v) in triplets {
                    data[r * nc + c] += v;
                }
                (row_dofs, col_dofs, data)
            };
            let row_labels: Vec<String> = row_dofs.iter().map(dof_label).collect();
            let col_labels: Vec<String> = col_dofs.iter().map(dof_label).collect();
            Ok(crate::dump::labeled_grid(
                &row_labels,
                &col_labels,
                &data,
                opts,
            ))
        })();
        match grid {
            Ok(g) => format!("{self}\n{g}"),
            Err(e) => format!("{self}\n<{e}>"),
        }
    }
}

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

// ─── Archive ────────────────────────────────────────────────────────────────

impl crate::archive::Archivable for SubMatrix {
    const TAG: &'static str = "SubMatrix";
    // No `on_load`: the block keeps no node list of its own — it reads its
    // supports, which the archive restores with it.
}

impl crate::archive::Archivable for Matrix {
    const TAG: &'static str = "Matrix";
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::aggregate::Aggregate;
    use crate::atoms::ElementType;
    use crate::atoms::Node;
    use crate::coords::Coords;
    use crate::handle::Handle;

    /// Build a POI1 SubMesh with `n` fresh nodes in a new 1-D Coords.
    /// Returns `(coords, nodes, support_handle)`.
    fn make_poi1(n: usize) -> (Handle<Coords>, Vec<Node>, Handle<SubMesh>) {
        let coords = Handle::new(Coords::new(1).unwrap());
        let nodes: Vec<Node> = (0..n)
            .map(|i| Node::create_in(coords.clone(), &[i as f64]).unwrap())
            .collect();
        let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
        for node in &nodes {
            sm.add_cell(&[node.id()]).unwrap();
        }
        (coords, nodes, Handle::new(sm))
    }

    /// Build a POI1 SubMesh whose connectivity **repeats** its first node:
    /// cells are `[a, b, a, c, …]`. Returns `(coords, nodes, support_handle)`,
    /// where `nodes` holds the distinct nodes in first-appearance order.
    fn make_poi1_repeated(n: usize) -> (Handle<Coords>, Vec<Node>, Handle<SubMesh>) {
        let coords = Handle::new(Coords::new(1).unwrap());
        let nodes: Vec<Node> = (0..n)
            .map(|i| Node::create_in(coords.clone(), &[i as f64]).unwrap())
            .collect();
        let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
        sm.add_cell(&[nodes[0].id()]).unwrap();
        for node in &nodes[1..] {
            sm.add_cell(&[node.id()]).unwrap();
            sm.add_cell(&[nodes[0].id()]).unwrap();
        }
        (coords, nodes, Handle::new(sm))
    }

    /// Why a block's support must have **distinct** nodes.
    ///
    /// The block reads its numbering straight from the support: row `i` is
    /// `(connectivity()[node_local], dual_vars[var])`, and `add_entry` / `get`
    /// find `node_local` through [`SubMesh::node_index`]. The two only agree
    /// when no node repeats — `node_index` stores a **dedup-compacted rank**,
    /// the connectivity a **flat position**.
    ///
    /// Every support in practice comes from `to_poi1`, which deduplicates, so
    /// the two coincide. This test pins both halves so the requirement cannot
    /// be forgotten: it is documented on the constructors, never validated.
    #[test]
    fn node_index_is_the_flat_position_only_on_a_deduplicated_support() {
        // Distinct nodes: rank and position coincide, for every node.
        let (_c, nodes, sup) = make_poi1(4);
        let guard = sup.read();
        for (position, nid) in guard.connectivity().iter().enumerate() {
            assert_eq!(
                guard.node_index()[nid],
                position,
                "deduplicated support: rank and position must agree on {nid:?}"
            );
        }
        assert_eq!(guard.node_index().len(), nodes.len());
        drop(guard);

        // Repeated node: they part company, and the gap is not benign — the
        // compacted rank names a row the flat position does not own.
        let (_c2, nodes2, sup2) = make_poi1_repeated(3);
        let guard2 = sup2.read();
        // Connectivity is [a, b, a, c, a]: `b` agrees at 1, `c` does not —
        // position 3 in the connectivity, rank 2 once deduplicated.
        assert_eq!(guard2.connectivity().len(), 5);
        assert_eq!(guard2.node_index()[&nodes2[1].id()], 1);
        assert_eq!(guard2.connectivity()[3], nodes2[2].id());
        assert_eq!(guard2.node_index()[&nodes2[2].id()], 2);
    }

    // ── SubMatrix tests ─────────────────────────────────────────────────────

    #[test]
    fn empty_sub_matrix() {
        let (_cfg, _nodes, sup) = make_poi1(2);
        let m = SubMatrix::new(
            sup.clone(),
            sup,
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::None,
        );
        assert_eq!(m.n_rows(), 2);
        assert_eq!(m.n_cols(), 2);
        assert_eq!(m.entry_count(), 0);
        assert!(!m.is_symmetric());
    }

    /// The declared share survives a save and a reload — deserialization being
    /// the fourth way a block enters the world, alongside the three
    /// constructors. A `Half` that lost its identity on the way back would
    /// silently stop pairing, and nothing would say so.
    ///
    /// The value alone is round-tripped, not a whole block: a `SubMatrix` holds
    /// `Handle`s, which refuse to serialize outside an archive.
    #[test]
    fn symmetry_round_trips_through_serde() {
        let (left, right) = mint_pair(0x1234_5678_9abc_def0);
        for declared in [Symmetry::Full, Symmetry::None, Symmetry::Half(left)] {
            let bytes =
                bincode::serde::encode_to_vec(declared, bincode::config::standard()).unwrap();
            let (back, _): (Symmetry, usize) =
                bincode::serde::decode_from_slice(&bytes, bincode::config::standard()).unwrap();
            assert_eq!(back, declared, "{declared:?} did not survive");
        }
        assert_ne!(left, right);
    }

    /// The two identities of a pair differ, and only in the bit that says so.
    /// Two contents that differ give two pairs that differ.
    #[test]
    fn mint_pair_splits_one_content_in_two() {
        let (a, b) = mint_pair(0xdead_beef_0000_1111);
        assert_ne!(a, b);
        assert_eq!(a & !1, b & !1, "both halves name the same pair");
        assert_eq!(a & 1, 0);
        assert_eq!(b & 1, 1);
        // Minting is a function of the content alone — the same content reminted
        // gives the same pair, which is what lets an archive be reproducible.
        assert_eq!(mint_pair(0xdead_beef_0000_1111), (a, b));

        let mut h = FNV_SEED;
        h = hash_name(h, "T");
        let mut other = FNV_SEED;
        other = hash_name(other, "P");
        assert_ne!(mint_pair(h).0 & !1, mint_pair(other).0 & !1);
    }

    /// A pair counts as one symmetry when both halves are there, and as none
    /// when one has been sliced away. Neither half is symmetric on its own.
    #[test]
    fn a_half_counts_only_with_its_other_half() {
        // One configuration, so the two node ids cannot collide.
        let (coords, nodes, _) = make_poi1(2);
        let (a, m) = (nodes[0].id(), nodes[1].id());
        let one = |n: NodeId| {
            let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
            sm.add_cell(&[n]).unwrap();
            Handle::new(sm)
        };
        let (sup_a, sup_m) = (one(a), one(m));
        let (id_c, id_ct) = mint_pair(0x5555_5555_5555_5555);
        // C: rows on the multiplier, columns on the constrained node — and Cᵀ
        // the other way round, with the variables crossed.
        let c = SubMatrix::new(
            sup_m.clone(),
            sup_a.clone(),
            vec!["imposed_T".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::Half(id_c),
        );
        let ct = SubMatrix::new(
            sup_a,
            sup_m,
            vec!["T".into()],
            vec!["imposed_T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::Half(id_ct),
        );
        assert!(!c.is_symmetric(), "a half is not symmetric on its own");

        let mut both = Matrix::empty();
        both.add_sub(Handle::new(c)).unwrap();
        both.add_sub(Handle::new(ct)).unwrap();
        assert!(both.symmetric(), "the pair is complete");

        let halved = both.subset([0]).unwrap();
        assert!(!halved.symmetric(), "one half alone carries no symmetry");
    }

    #[test]
    fn add_entry_and_field_names() {
        let (_cfg, nodes, sup) = make_poi1(2);
        let (a, b) = (nodes[0].id(), nodes[1].id());
        let mut m = SubMatrix::new(
            sup.clone(),
            sup,
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::None,
        );
        m.add_entry(a, "q", a, "T", 2.0).unwrap();
        m.add_entry(a, "q", b, "T", -1.0).unwrap();
        m.add_entry(b, "q", a, "T", -1.0).unwrap();
        m.add_entry(b, "q", b, "T", 2.0).unwrap();
        // dual_vars + primal_vars = 2 distinct names
        assert_eq!(m.field_names().len(), 2);
        assert_eq!(m.n_rows(), 2);
        assert_eq!(m.n_cols(), 2);
        assert_eq!(m.entry_count(), 4);
    }

    #[test]
    fn get_unknown_returns_zero() {
        let (_cfg, nodes, sup) = make_poi1(1);
        let m = SubMatrix::new(
            sup.clone(),
            sup,
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::None,
        );
        // unknown field → 0.0
        assert_eq!(m.get(nodes[0].id(), "x", nodes[0].id(), "y"), 0.0);
    }

    #[test]
    fn get_sums_duplicates() {
        let (_cfg, nodes, sup) = make_poi1(1);
        let a = nodes[0].id();
        let mut m = SubMatrix::new(
            sup.clone(),
            sup,
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::None,
        );
        m.add_entry(a, "q", a, "T", 2.0).unwrap();
        m.add_entry(a, "q", a, "T", 1.5).unwrap();
        m.add_entry(a, "q", a, "T", -0.5).unwrap();
        assert_eq!(m.get(a, "q", a, "T"), 3.0);
    }

    #[test]
    fn dense_matches_get() {
        let (_cfg, nodes, sup) = make_poi1(2);
        let (a, b) = (nodes[0].id(), nodes[1].id());
        let mut m = SubMatrix::new(
            sup.clone(),
            sup,
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::None,
        );
        m.add_entry(a, "q", a, "T", 2.0).unwrap();
        m.add_entry(a, "q", b, "T", -1.0).unwrap();
        m.add_entry(b, "q", a, "T", -1.0).unwrap();
        m.add_entry(b, "q", b, "T", 2.0).unwrap();
        assert_eq!(m.dense(), vec![2.0, -1.0, -1.0, 2.0]);
    }

    #[test]
    fn dump_labels_grid_with_dofs() {
        use crate::dump::{Dump, DumpOptions};
        let (_cfg, nodes, sup) = make_poi1(2);
        let (a, b) = (nodes[0].id(), nodes[1].id());
        let mut m = SubMatrix::new(
            sup.clone(),
            sup,
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::None,
        );
        m.add_entry(a, "q", a, "T", 2.0).unwrap();
        m.add_entry(a, "q", b, "T", -1.0).unwrap();
        m.add_entry(b, "q", a, "T", -1.0).unwrap();
        m.add_entry(b, "q", b, "T", 2.0).unwrap();

        let s = m.render(&DumpOptions::default());
        let mut lines = s.lines();
        assert_eq!(
            lines.next().unwrap(),
            "SubMatrix: 2 row(s) × 2 col(s), 4 entries"
        );
        // In-line DOF labels on both axes + values at default precision.
        assert!(s.contains(&format!("({a},q)")), "row label:\n{s}");
        assert!(s.contains(&format!("({a},T)")), "col label:\n{s}");
        assert!(s.contains("2.000") && s.contains("-1.000"), "values:\n{s}");
        assert_eq!(
            s.lines().count(),
            6,
            "summary + 2 metadata lines + header + 2 rows:\n{s}"
        );
    }

    #[test]
    fn mul_dense_against_known_matrix() {
        let (_cfg, nodes, sup) = make_poi1(2);
        let (a, b) = (nodes[0].id(), nodes[1].id());
        let mut m = SubMatrix::new(
            sup.clone(),
            sup,
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::Full,
        );
        m.add_entry(a, "q", a, "T", 2.0).unwrap();
        m.add_entry(a, "q", b, "T", -1.0).unwrap();
        m.add_entry(b, "q", a, "T", -1.0).unwrap();
        m.add_entry(b, "q", b, "T", 2.0).unwrap();
        assert_eq!(m.mul_dense(&[1.0, 1.0]).unwrap(), vec![1.0, 1.0]);
        assert_eq!(m.mul_dense(&[1.0, 2.0]).unwrap(), vec![0.0, 3.0]);
    }

    #[test]
    fn mul_dense_rejects_wrong_size() {
        let (_cfg, nodes, sup) = make_poi1(1);
        let a = nodes[0].id();
        let mut m = SubMatrix::new(
            sup.clone(),
            sup,
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::None,
        );
        m.add_entry(a, "q", a, "T", 1.0).unwrap();
        // 1 col, but x has 2 elements
        assert!(m.mul_dense(&[1.0, 2.0]).is_err());
    }

    #[test]
    fn rectangular_sub_matrix_distinct_row_and_col_supports() {
        let (_cfg_r, row_nodes, row_sup) = make_poi1(2);
        let (_cfg_c, col_nodes, col_sup) = make_poi1(2);
        let mut c = SubMatrix::new(
            row_sup,
            col_sup,
            vec!["T".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::None,
        );
        c.add_entry(row_nodes[0].id(), "T", col_nodes[0].id(), "T", 1.0)
            .unwrap();
        c.add_entry(row_nodes[1].id(), "T", col_nodes[1].id(), "T", 1.0)
            .unwrap();
        assert_eq!(c.n_rows(), 2);
        assert_eq!(c.n_cols(), 2);
        // "T" appears in both dual and primal — field_names deduplicates
        assert_eq!(c.field_names().len(), 1);
    }

    #[test]
    fn iter_entries_preserves_insertion_order() {
        let (_cfg, nodes, sup) = make_poi1(2);
        let (a, b) = (nodes[0].id(), nodes[1].id());
        let mut m = SubMatrix::new(
            sup.clone(),
            sup,
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::None,
        );
        m.add_entry(a, "q", a, "T", 1.0).unwrap();
        m.add_entry(b, "q", b, "T", 2.0).unwrap();
        m.add_entry(a, "q", a, "T", 3.0).unwrap();
        let entries = m.iter_entries();
        assert_eq!(entries.len(), 3);
        assert_eq!(entries[0].4, 1.0);
        assert_eq!(entries[1].4, 2.0);
        assert_eq!(entries[2].4, 3.0);
    }

    #[test]
    fn sub_matrix_mul_and_div_scale_only_the_factor() {
        let (_cfg, nodes, sup) = make_poi1(1);
        let a = nodes[0].id();
        let mut m = SubMatrix::new(
            sup.clone(),
            sup,
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::Full,
        );
        m.add_entry(a, "q", a, "T", 2.0).unwrap();
        assert_eq!(m.factor(), 1.0);

        // Reference version clones, leaving `m` untouched.
        let scaled = &m * 3.0;
        assert_eq!(scaled.factor(), 3.0);
        assert_eq!(scaled.get(a, "q", a, "T"), 6.0);
        assert_eq!(scaled.dense(), vec![6.0]);
        assert_eq!(scaled.iter_entries()[0].4, 6.0);
        assert_eq!(
            m.get(a, "q", a, "T"),
            2.0,
            "reference Mul must not mutate m"
        );

        // Consuming version chains: ×3 then ÷2 ⇒ factor 1.5.
        let halved = scaled / 2.0;
        assert_eq!(halved.factor(), 1.5);
        assert_eq!(halved.get(a, "q", a, "T"), 3.0);
        assert_eq!(halved.to_coo().values(), &[3.0]);
        assert_eq!(halved.to_csr().values(), &[3.0]);
        assert_eq!(halved.to_csc().values(), &[3.0]);
        assert_eq!(halved.to_dmatrix()[(0, 0)], 3.0);
        assert_eq!(halved.mul_dense(&[1.0]).unwrap(), vec![3.0]);
        // The raw local form is untouched by the factor.
        assert_eq!(halved.local_coo_arrays().2, &[2.0]);
    }

    #[test]
    fn physics_tag_set_empty_multiple_and_other() {
        let (_cfg, nodes, sup) = make_poi1(2);
        let (a, _b) = (nodes[0].id(), nodes[1].id());
        let make = || {
            SubMatrix::new(
                sup.clone(),
                sup.clone(),
                vec!["q".into()],
                vec!["T".into()],
                DofOrdering::NodesThenVars,
                Symmetry::None,
            )
        };

        // A fresh block is untagged — the "rien" case.
        let bare = make();
        assert!(bare.physics().is_empty());

        // A coupled block carries several natures; filter matches by containment.
        let mut coupled = make();
        coupled.set_physics(vec![Physics::Mechanical, Physics::Thermal]);
        assert_eq!(coupled.physics(), &[Physics::Mechanical, Physics::Thermal]);

        // An explicit "other" nature is filterable, unlike the empty set.
        let mut other = make();
        other.set_physics(vec![Physics::Other]);

        let mut k = Matrix::empty();
        k.add_sub(Handle::new(bare)).unwrap();
        k.add_sub(Handle::new(coupled)).unwrap();
        k.add_sub(Handle::new(other)).unwrap();
        let _ = a; // silence unused in some build configs

        // Containment: the coupled block appears under both its natures.
        assert_eq!(k.filter(Physics::Mechanical).len(), 1);
        assert_eq!(k.filter(Physics::Thermal).len(), 1);
        // Only the explicitly-tagged block is reached by Other; the bare one never.
        assert_eq!(k.filter(Physics::Other).len(), 1);
        // The aggregate reports every distinct nature present (bare contributes none).
        let present = k.physics();
        assert!(present.contains(&Physics::Mechanical));
        assert!(present.contains(&Physics::Thermal));
        assert!(present.contains(&Physics::Other));
        assert_eq!(present.len(), 3);
    }

    #[test]
    fn sub_debug_and_display() {
        let (_cfg, nodes, sup) = make_poi1(1);
        let a = nodes[0].id();
        let mut m = SubMatrix::new(
            sup.clone(),
            sup,
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::Full,
        );
        m.add_entry(a, "q", a, "T", 2.0).unwrap();
        let d = format!("{:?}", m);
        assert!(d.contains("SubMatrix"));
        assert!(d.contains("n_rows"));
        assert!(d.contains("symmetry"));
        let s = format!("{}", m);
        assert!(s.contains("SubMatrix"));
        assert!(s.contains("1 row"));
        assert!(s.contains("symmetric"));
    }

    #[test]
    fn dof_ordering_vars_then_nodes() {
        let (_cfg, nodes, sup) = make_poi1(2);
        let (a, b) = (nodes[0].id(), nodes[1].id());
        // 2 vars × 2 nodes = 4×4 block
        let (_cfg2, nodes2, sup2) = make_poi1(2);
        let (c, d) = (nodes2[0].id(), nodes2[1].id());
        let mut m = SubMatrix::new(
            sup,
            sup2,
            vec!["p".into(), "q".into()],
            vec!["u".into(), "v".into()],
            DofOrdering::VarsThenNodes,
            Symmetry::None,
        );
        // With VarsThenNodes: row 0 = (p, node_a), row 1 = (p, node_b),
        //                     row 2 = (q, node_a), row 3 = (q, node_b)
        m.add_entry(a, "p", c, "u", 1.0).unwrap();
        m.add_entry(b, "p", d, "v", 2.0).unwrap();
        m.add_entry(a, "q", c, "v", 3.0).unwrap();
        assert_eq!(m.get(a, "p", c, "u"), 1.0);
        assert_eq!(m.get(b, "p", d, "v"), 2.0);
        assert_eq!(m.get(a, "q", c, "v"), 3.0);
        assert_eq!(m.get(b, "q", d, "u"), 0.0);

        // row_dofs in VarsThenNodes order: (p,a),(p,b),(q,a),(q,b)
        let rdofs = m.row_dofs();
        assert_eq!(rdofs[0], (a, "p".to_string()));
        assert_eq!(rdofs[1], (b, "p".to_string()));
        assert_eq!(rdofs[2], (a, "q".to_string()));
        assert_eq!(rdofs[3], (b, "q".to_string()));
    }

    #[test]
    fn dof_ordering_nodes_then_vars() {
        let (_cfg, nodes, sup) = make_poi1(2);
        let (a, b) = (nodes[0].id(), nodes[1].id());
        let (_cfg2, nodes2, sup2) = make_poi1(2);
        let (c, _d) = (nodes2[0].id(), nodes2[1].id());
        let mut m = SubMatrix::new(
            sup,
            sup2,
            vec!["p".into(), "q".into()],
            vec!["u".into(), "v".into()],
            DofOrdering::NodesThenVars,
            Symmetry::None,
        );
        // With NodesThenVars: row 0 = (node_a, p), row 1 = (node_a, q),
        //                     row 2 = (node_b, p), row 3 = (node_b, q)
        m.add_entry(a, "p", c, "u", 7.0).unwrap();
        assert_eq!(m.get(a, "p", c, "u"), 7.0);

        let rdofs = m.row_dofs();
        assert_eq!(rdofs[0], (a, "p".to_string()));
        assert_eq!(rdofs[1], (a, "q".to_string()));
        assert_eq!(rdofs[2], (b, "p".to_string()));
        assert_eq!(rdofs[3], (b, "q".to_string()));
    }

    // ── Matrix (aggregate) tests ────────────────────────────────────────────

    #[test]
    fn empty_aggregate_is_vacuous_symmetric() {
        let m = Matrix::empty();
        assert_eq!(m.n_rows().unwrap(), 0);
        assert_eq!(m.n_cols().unwrap(), 0);
        assert!(m.symmetric());
        assert_eq!(m.entry_count(), 0);
    }

    #[test]
    fn aggregate_unions_dofs_and_sums_at_coincidence() {
        // Single configuration: 5 distinct nodes to avoid NodeId collisions.
        let (coords, nodes, _) = make_poi1(5);
        let (na, ca0, ca1, m0, m1) = (
            nodes[0].id(),
            nodes[1].id(),
            nodes[2].id(),
            nodes[3].id(),
            nodes[4].id(),
        );

        // Block a: 1 row (na) × 2 cols (ca0, ca1) — rectangular, hence `None`:
        // a block that is not square cannot carry any symmetry.
        let mut row_a = SubMesh::new(coords.clone(), ElementType::POI1);
        row_a.add_cell(&[na]).unwrap();
        let mut col_a = SubMesh::new(coords.clone(), ElementType::POI1);
        col_a.add_cell(&[ca0]).unwrap();
        col_a.add_cell(&[ca1]).unwrap();
        let mut a = SubMatrix::new(
            Handle::new(row_a),
            Handle::new(col_a),
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::None,
        );
        a.add_entry(na, "q", ca0, "T", 2.0).unwrap();
        a.add_entry(na, "q", ca1, "T", -1.0).unwrap();

        // Block b: 2 rows (m0, m1) × 2 cols (m0, m1)
        let mut row_b = SubMesh::new(coords.clone(), ElementType::POI1);
        row_b.add_cell(&[m0]).unwrap();
        row_b.add_cell(&[m1]).unwrap();
        let mut col_b = SubMesh::new(coords.clone(), ElementType::POI1);
        col_b.add_cell(&[m0]).unwrap();
        col_b.add_cell(&[m1]).unwrap();
        let mut b = SubMatrix::new(
            Handle::new(row_b),
            Handle::new(col_b),
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::Full,
        );
        b.add_entry(m0, "q", m0, "T", 0.5).unwrap();
        b.add_entry(m1, "q", m0, "T", -1.0).unwrap();
        b.add_entry(m1, "q", m1, "T", 2.0).unwrap();

        let mut k = Matrix::empty();
        k.add_sub(Handle::new(a)).unwrap();
        k.add_sub(Handle::new(b)).unwrap();

        // Union of row DOFs: (na,"q"), (m0,"q"), (m1,"q") — 3
        // Union of col DOFs: (ca0,"T"), (ca1,"T"), (m0,"T"), (m1,"T") — 4
        assert_eq!(k.n_rows().unwrap(), 3);
        assert_eq!(k.n_cols().unwrap(), 4);
    }

    #[test]
    fn aggregate_symmetric_is_and_of_subs() {
        let (_cfg, _nodes, sup) = make_poi1(1);
        let a = SubMatrix::new(
            sup.clone(),
            sup.clone(),
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::Full,
        );
        let b = SubMatrix::new(
            sup.clone(),
            sup,
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::None,
        );
        let mut k = Matrix::empty();
        k.add_sub(Handle::new(a)).unwrap();
        k.add_sub(Handle::new(b)).unwrap();
        assert!(!k.symmetric());
    }

    #[test]
    fn aggregate_scale_is_isolated_from_the_source_matrix() {
        let (_cfg, nodes, sup) = make_poi1(2);
        let (a, b) = (nodes[0].id(), nodes[1].id());
        let mut blk = SubMatrix::new(
            sup.clone(),
            sup,
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::Full,
        );
        blk.add_entry(a, "q", a, "T", 2.0).unwrap();
        blk.add_entry(b, "q", b, "T", 3.0).unwrap();
        let mut orig = Matrix::empty();
        orig.add_sub(Handle::new(blk)).unwrap();

        let scaled = &orig * 10.0;

        // A new store slot per block — no aliasing with the source's blocks.
        let orig_h = orig.iter().next().unwrap();
        let scaled_h = scaled.iter().next().unwrap();
        assert!(!orig_h.same_object(scaled_h));

        // Values diverge accordingly: the source is untouched.
        assert_eq!(orig.get(a, "q", a, "T"), 2.0);
        assert_eq!(orig.get(b, "q", b, "T"), 3.0);
        assert_eq!(scaled.get(a, "q", a, "T"), 20.0);
        assert_eq!(scaled.get(b, "q", b, "T"), 30.0);

        // `/` divides the factor, chaining from the already-scaled matrix.
        let halved = &scaled / 2.0;
        assert_eq!(halved.get(a, "q", a, "T"), 10.0);
        assert_eq!(
            scaled.get(a, "q", a, "T"),
            20.0,
            "/ must not mutate its source either"
        );
    }

    /// A one-block literal matrix on `sup`, carrying `value` on the diagonal of
    /// each node of `nodes`.
    fn diag(sup: &Handle<SubMesh>, nodes: &[NodeId], value: f64) -> Matrix {
        let mut blk = SubMatrix::new(
            sup.clone(),
            sup.clone(),
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::Full,
        );
        for &n in nodes {
            blk.add_entry(n, "q", n, "T", value).unwrap();
        }
        let mut m = Matrix::empty();
        m.add_sub(Handle::new(blk)).unwrap();
        m
    }

    /// Scaling an **assembled** matrix carries the CSR along, scaled, instead of
    /// dropping it. What that buys: the result is usable without a second
    /// assembly — which, on a matrix with computed blocks, would re-run every
    /// element kernel to apply one scalar.
    #[test]
    fn scaling_carries_the_assembled_csr() {
        let (_cfg, nodes, sup) = make_poi1(2);
        let (a, b) = (nodes[0].id(), nodes[1].id());
        let mut k = diag(&sup, &[a, b], 2.0);
        k.finalize().unwrap();

        let scaled = &k * 3.0;

        // Assembled on arrival: no `finalize()` / `assemble()` in between.
        assert_eq!(scaled.to_csr().unwrap().values(), &[6.0, 6.0]);
        assert_eq!(scaled.get(a, "q", a, "T"), 6.0);
        // The sparsity is untouched, so the index arrays are *shared*, not copied.
        let (ka, sa) = (
            k.assembled.as_ref().unwrap(),
            scaled.assembled.as_ref().unwrap(),
        );
        assert!(std::sync::Arc::ptr_eq(
            &ka.csr.row_offsets,
            &sa.csr.row_offsets
        ));
        assert!(std::sync::Arc::ptr_eq(
            &ka.csr.col_indices,
            &sa.csr.col_indices
        ));
        // And the source keeps its own values.
        assert_eq!(k.to_csr().unwrap().values(), &[2.0, 2.0]);

        // An unassembled source still yields an unassembled result.
        let raw = diag(&sup, &[a], 1.0);
        assert!((&raw * 2.0).to_csr().is_err());
    }

    /// The owning `*` rescales a block **no one else holds** where it lies; a
    /// block another matrix shares is rebuilt, so that other matrix is spared.
    #[test]
    fn owned_scaling_reuses_a_block_no_one_else_holds() {
        let (_cfg, nodes, sup) = make_poi1(1);
        let a = nodes[0].id();

        let sole = diag(&sup, &[a], 2.0);
        let slot = sole.iter().next().unwrap().id();
        let scaled = sole * 3.0;
        assert_eq!(
            scaled.iter().next().unwrap().id(),
            slot,
            "sole owner: in place"
        );
        assert_eq!(scaled.get(a, "q", a, "T"), 6.0);

        // Now with a second holder of the same block.
        let shared = diag(&sup, &[a], 2.0);
        let alias = shared.subset([0]).unwrap();
        let slot = shared.iter().next().unwrap().id();
        let scaled = shared * 3.0;
        assert_ne!(scaled.iter().next().unwrap().id(), slot, "shared: rebuilt");
        assert_eq!(scaled.get(a, "q", a, "T"), 6.0);
        assert_eq!(
            alias.get(a, "q", a, "T"),
            2.0,
            "the aliasing matrix is spared"
        );
    }

    /// What parts `+` from `|`: a sum counts a contribution once per handing
    /// over, a union drops a block whose slot it already holds.
    #[test]
    fn sum_counts_a_shared_block_twice_where_union_drops_it() {
        let (_cfg, nodes, sup) = make_poi1(1);
        let a = nodes[0].id();
        let k = diag(&sup, &[a], 2.0);

        let mut sum = &k + &k;
        sum.finalize().unwrap();
        assert_eq!(sum.len(), 2);
        assert_eq!(sum.get(a, "q", a, "T"), 4.0, "k + k is 2k");

        let mut union = k.union(&k).unwrap();
        union.finalize().unwrap();
        assert_eq!(union.len(), 1);
        assert_eq!(union.get(a, "q", a, "T"), 2.0, "k | k is k");

        // The sum shares its operands' blocks — nothing is copied.
        assert!(sum
            .iter()
            .next()
            .unwrap()
            .same_object(k.iter().next().unwrap()));
        // …and leaves the source alone.
        assert_eq!(k.get(a, "q", a, "T"), 2.0);
    }

    /// `a - b` negates the right-hand side's blocks, which copies them; `a + b`
    /// copies nothing. Both leave their operands alone.
    #[test]
    fn difference_negates_only_the_right_hand_side() {
        let (_cfg, nodes, sup) = make_poi1(1);
        let a = nodes[0].id();
        let x = diag(&sup, &[a], 5.0);
        let y = diag(&sup, &[a], 2.0);

        let mut d = &x - &y;
        d.finalize().unwrap();
        assert_eq!(d.get(a, "q", a, "T"), 3.0);
        assert_eq!(y.get(a, "q", a, "T"), 2.0, "the subtrahend is untouched");
        assert!(!d
            .iter()
            .nth(1)
            .unwrap()
            .same_object(y.iter().next().unwrap()));

        // A matrix minus itself is zero — the structural sum, then the assembler.
        let mut z = &x - &x;
        z.finalize().unwrap();
        assert_eq!(z.get(a, "q", a, "T"), 0.0);
    }

    /// The scalar reads on either side, and the unary minus agrees with `× -1`.
    #[test]
    fn scalar_on_the_left_and_unary_minus() {
        let (_cfg, nodes, sup) = make_poi1(1);
        let a = nodes[0].id();
        let k = diag(&sup, &[a], 2.0);

        assert_eq!((2.0 * &k).get(a, "q", a, "T"), 4.0);
        assert_eq!((-&k).get(a, "q", a, "T"), -2.0);
        assert_eq!((-&k).get(a, "q", a, "T"), (&k * -1.0).get(a, "q", a, "T"));

        // On a block too.
        let blk = (*k.iter().next().unwrap().read()).clone();
        assert_eq!((3.0 * &blk).factor(), 3.0);
        assert_eq!((-&blk).factor(), -1.0);
    }

    /// A block and an aggregate mix in a sum, either way round.
    #[test]
    fn a_block_and_a_matrix_mix_in_a_sum() {
        let (_cfg, nodes, sup) = make_poi1(1);
        let a = nodes[0].id();
        let k = diag(&sup, &[a], 2.0);
        let blk = (*k.iter().next().unwrap().read()).clone();

        for mut s in [&k + &blk, &blk + &k] {
            s.finalize().unwrap();
            assert_eq!(s.len(), 2);
            assert_eq!(s.get(a, "q", a, "T"), 4.0);
        }
        let mut two_blocks = &blk + &blk;
        two_blocks.finalize().unwrap();
        assert_eq!(two_blocks.get(a, "q", a, "T"), 4.0);

        let mut d = &k - &blk;
        d.finalize().unwrap();
        assert_eq!(d.get(a, "q", a, "T"), 0.0);
    }

    /// A divisor no matrix survives is refused at the operator, not left to
    /// surface as a `NaN` inside the solver.
    #[test]
    #[should_panic(expected = "matrix division by 0")]
    fn division_by_zero_is_refused() {
        let (_cfg, nodes, sup) = make_poi1(1);
        let k = diag(&sup, &[nodes[0].id()], 2.0);
        let _ = &k / 0.0;
    }

    #[test]
    #[should_panic(expected = "non-finite")]
    fn division_by_infinity_is_refused() {
        let (_cfg, nodes, sup) = make_poi1(1);
        let k = diag(&sup, &[nodes[0].id()], 2.0);
        let _ = &k / f64::INFINITY;
    }

    /// `M/dt + K` through the **union**, which stays the way to compose one
    /// operator out of distinct parts (`+` is the algebraic sum — see
    /// `sum_counts_a_shared_block_twice_where_union_drops_it`). `K` carries a
    /// DOF (`c`) that `M` doesn't (mirroring a Dirichlet multiplier row/column,
    /// which only ever enters the stiffness matrix) — the union must still
    /// assemble correctly, leaving that entry untouched by `M`'s contribution.
    #[test]
    fn union_and_reassemble_combines_scaled_mass_with_stiffness() {
        let (coords, nodes, _) = make_poi1(3);
        let (a, b, c) = (nodes[0].id(), nodes[1].id(), nodes[2].id());

        let mut sup_k = SubMesh::new(coords.clone(), ElementType::POI1);
        sup_k.add_cell(&[a]).unwrap();
        sup_k.add_cell(&[b]).unwrap();
        sup_k.add_cell(&[c]).unwrap();
        let sup_k = Handle::new(sup_k);
        let mut k_blk = SubMatrix::new(
            sup_k.clone(),
            sup_k,
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::Full,
        );
        k_blk.add_entry(a, "q", a, "T", 2.0).unwrap();
        k_blk.add_entry(a, "q", b, "T", -1.0).unwrap();
        k_blk.add_entry(b, "q", a, "T", -1.0).unwrap();
        k_blk.add_entry(b, "q", b, "T", 2.0).unwrap();
        k_blk.add_entry(c, "q", c, "T", 5.0).unwrap(); // Lagrange-only DOF
        let mut k = Matrix::empty();
        k.add_sub(Handle::new(k_blk)).unwrap();

        let mut sup_m = SubMesh::new(coords.clone(), ElementType::POI1);
        sup_m.add_cell(&[a]).unwrap();
        sup_m.add_cell(&[b]).unwrap();
        let sup_m = Handle::new(sup_m);
        let mut m_blk = SubMatrix::new(
            sup_m.clone(),
            sup_m,
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::Full,
        );
        m_blk.add_entry(a, "q", a, "T", 4.0).unwrap();
        m_blk.add_entry(b, "q", b, "T", 4.0).unwrap();
        let mut m = Matrix::empty();
        m.add_sub(Handle::new(m_blk)).unwrap();

        let dt = 0.5;
        let m_dt = &m / dt; // factor = 1/0.5 = 2 ⇒ diag(8, 8)

        let mut sys = m_dt.union(&k).unwrap();
        sys.assemble().unwrap();

        assert_eq!(sys.n_rows().unwrap(), 3);
        assert_eq!(sys.n_cols().unwrap(), 3);
        assert_eq!(sys.get(a, "q", a, "T"), 2.0 + 8.0);
        assert_eq!(sys.get(a, "q", b, "T"), -1.0);
        assert_eq!(sys.get(b, "q", a, "T"), -1.0);
        assert_eq!(sys.get(b, "q", b, "T"), 2.0 + 8.0);
        // Untouched by M (which doesn't carry the c DOF at all).
        assert_eq!(sys.get(c, "q", c, "T"), 5.0);
    }

    #[test]
    fn aggregate_to_dmatrix_layout_is_union_first_seen() {
        // Two distinct nodes in the SAME configuration to avoid NodeId collisions.
        let (coords, nodes, _) = make_poi1(2);
        let na = nodes[0].id();
        let nb = nodes[1].id();

        let mut sm_a = SubMesh::new(coords.clone(), ElementType::POI1);
        sm_a.add_cell(&[na]).unwrap();
        let sup_a = Handle::new(sm_a);

        let mut sm_b = SubMesh::new(coords.clone(), ElementType::POI1);
        sm_b.add_cell(&[nb]).unwrap();
        let sup_b = Handle::new(sm_b);

        let mut a = SubMatrix::new(
            sup_a.clone(),
            sup_a,
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::None,
        );
        a.add_entry(na, "q", na, "T", 2.0).unwrap();

        let mut b = SubMatrix::new(
            sup_b.clone(),
            sup_b,
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::None,
        );
        b.add_entry(nb, "q", nb, "T", 3.0).unwrap();

        let mut k = Matrix::empty();
        k.add_sub(Handle::new(a)).unwrap();
        k.add_sub(Handle::new(b)).unwrap();
        k.finalize().unwrap();

        let d = k.to_dmatrix().unwrap();
        assert_eq!(d.nrows(), 2);
        assert_eq!(d.ncols(), 2);
        assert_eq!(d[(0, 0)], 2.0);
        assert_eq!(d[(0, 1)], 0.0);
        assert_eq!(d[(1, 0)], 0.0);
        assert_eq!(d[(1, 1)], 3.0);
    }

    #[test]
    fn aggregate_dof_order_follows_permutation() {
        // Same data as the first-seen test: na→2.0, nb→3.0, but a permutation
        // orders nb's DOF before na's (solver order).
        let (coords, nodes, _) = make_poi1(2);
        let na = nodes[0].id();
        let nb = nodes[1].id();

        let mk = |nid| {
            let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
            sm.add_cell(&[nid]).unwrap();
            Handle::new(sm)
        };
        let sup_a = mk(na);
        let sup_b = mk(nb);
        let mut a = SubMatrix::new(
            sup_a.clone(),
            sup_a,
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::None,
        );
        a.add_entry(na, "q", na, "T", 2.0).unwrap();
        let mut b = SubMatrix::new(
            sup_b.clone(),
            sup_b,
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::None,
        );
        b.add_entry(nb, "q", nb, "T", 3.0).unwrap();

        let mut k = Matrix::empty();
        k.add_sub(Handle::new(a)).unwrap();
        k.add_sub(Handle::new(b)).unwrap();

        // Transposition of the identity ⇒ nb sorts before na.
        let cap = coords.read().capacity();
        let mut perm: Vec<u32> = (0..cap as u32).collect();
        perm.swap(na.0 as usize, nb.0 as usize);
        coords.write().set_permutation(perm).unwrap();

        k.finalize().unwrap();

        let rows = k.row_dofs().unwrap();
        assert_eq!(rows[0], (nb, "q".to_string()));
        assert_eq!(rows[1], (na, "q".to_string()));
        let d = k.to_dmatrix().unwrap();
        assert_eq!(d[(0, 0)], 3.0); // nb first
        assert_eq!(d[(1, 1)], 2.0); // na second
    }

    #[test]
    fn not_finalized_yields_error() {
        let (_cfg, nodes, sup) = make_poi1(1);
        let a = nodes[0].id();
        let mut m = SubMatrix::new(
            sup.clone(),
            sup,
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::None,
        );
        m.add_entry(a, "q", a, "T", 1.0).unwrap();
        let mut k = Matrix::empty();
        k.add_sub(Handle::new(m)).unwrap();
        // must not succeed before finalize
        assert!(k.to_csr().is_err());
        assert!(k.to_dmatrix().is_err());
        assert!(k.mul_dense(&[1.0]).is_err());
        k.finalize().unwrap();
        assert!(k.to_csr().is_ok());
    }

    #[test]
    fn aggregate_mul_dense_matches_dense() {
        let (_cfg_a, nodes_a, sup_a) = make_poi1(2);
        let (na0, na1) = (nodes_a[0].id(), nodes_a[1].id());
        // row block a: only node na0 as row
        let mut row_a = SubMesh::new(sup_a.read().coords(), ElementType::POI1);
        row_a.add_cell(&[na0]).unwrap();
        let row_a_h = Handle::new(row_a);

        // One row against a two-node column support: rectangular, so `None`.
        let mut a = SubMatrix::new(
            row_a_h,
            sup_a.clone(),
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::None,
        );
        a.add_entry(na0, "q", na0, "T", 2.0).unwrap();
        a.add_entry(na0, "q", na1, "T", -1.0).unwrap();

        let mut row_b = SubMesh::new(sup_a.read().coords(), ElementType::POI1);
        row_b.add_cell(&[na1]).unwrap();
        let row_b_h = Handle::new(row_b);

        let mut b = SubMatrix::new(
            row_b_h,
            sup_a,
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::None,
        );
        b.add_entry(na1, "q", na0, "T", -1.0).unwrap();
        b.add_entry(na1, "q", na1, "T", 2.0).unwrap();

        let mut k = Matrix::empty();
        k.add_sub(Handle::new(a)).unwrap();
        k.add_sub(Handle::new(b)).unwrap();
        k.finalize().unwrap();

        assert_eq!(k.mul_dense(&[1.0, 1.0]).unwrap(), vec![1.0, 1.0]);
        assert_eq!(k.mul_dense(&[1.0, 2.0]).unwrap(), vec![0.0, 3.0]);
    }

    #[test]
    fn aggregate_mul_field_matches_mul_dense_and_operator() {
        use crate::containers::node_field::{NodeField, SubNodeField};

        // K = [[2,-1],[-1,2]] with dual rows "q" and primal columns "T".
        let (_cfg, nodes, sup) = make_poi1(2);
        let (a, b) = (nodes[0].id(), nodes[1].id());
        let mut sm = SubMatrix::new(
            sup.clone(),
            sup.clone(),
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::Full,
        );
        sm.add_entry(a, "q", a, "T", 2.0).unwrap();
        sm.add_entry(a, "q", b, "T", -1.0).unwrap();
        sm.add_entry(b, "q", a, "T", -1.0).unwrap();
        sm.add_entry(b, "q", b, "T", 2.0).unwrap();
        let mut k = Matrix::empty();
        k.add_sub(Handle::new(sm)).unwrap();
        k.finalize().unwrap();

        // x = T:[1, 2] over both column nodes ⇒ y = K·x = [0, 3] at the "q" rows.
        let mut x_sub = SubNodeField::from_poi1(&sup, vec!["T".into()]).unwrap();
        x_sub.set_value(a, "T", 1.0).unwrap();
        x_sub.set_value(b, "T", 2.0).unwrap();
        let x = NodeField::from_sub(x_sub);

        let y = k.mul_field(&x).unwrap();
        assert_eq!(y.value(a, "q").unwrap(), 0.0);
        assert_eq!(y.value(b, "q").unwrap(), 3.0);
        // The result lives on the row DOFs — component is "q", not "T".
        assert!(y.value_opt(a, "T").is_none());

        // The `*` operator is sugar for `mul_field`.
        let y_op = (&k * &x).unwrap();
        assert_eq!(y_op.value(a, "q").unwrap(), 0.0);
        assert_eq!(y_op.value(b, "q").unwrap(), 3.0);

        // A column DOF the field does not define contributes 0: with only
        // T(a)=3 set, y = K·[3, 0] = [6, -3].
        let mut x2_sub = SubNodeField::from_poi1(&sup, vec!["T".into()]).unwrap();
        x2_sub.set_value(a, "T", 3.0).unwrap();
        let x2 = NodeField::from_sub(x2_sub);
        let y2 = k.mul_field(&x2).unwrap();
        assert_eq!(y2.value(a, "q").unwrap(), 6.0);
        assert_eq!(y2.value(b, "q").unwrap(), -3.0);
    }

    #[test]
    fn aggregate_entries_concatenates_blocks() {
        let (_cfg_a, nodes_a, sup_a) = make_poi1(1);
        let (_cfg_b, nodes_b, sup_b) = make_poi1(1);
        let na = nodes_a[0].id();
        let nb = nodes_b[0].id();

        let mut a = SubMatrix::new(
            sup_a.clone(),
            sup_a,
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::None,
        );
        a.add_entry(na, "q", na, "T", 1.0).unwrap();

        let mut b = SubMatrix::new(
            sup_b.clone(),
            sup_b,
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::None,
        );
        b.add_entry(nb, "q", nb, "T", 2.0).unwrap();

        let mut k = Matrix::empty();
        k.add_sub(Handle::new(a)).unwrap();
        k.add_sub(Handle::new(b)).unwrap();

        let entries = k.iter_entries();
        assert_eq!(entries.len(), 2);
        assert_eq!(entries[0].4, 1.0);
        assert_eq!(entries[1].4, 2.0);
    }

    #[test]
    fn aggregate_debug_and_display() {
        let (_cfg, nodes, sup) = make_poi1(1);
        let a_id = nodes[0].id();
        let mut a = SubMatrix::new(
            sup.clone(),
            sup,
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::Full,
        );
        a.add_entry(a_id, "q", a_id, "T", 2.0).unwrap();
        let mut k = Matrix::empty();
        k.add_sub(Handle::new(a)).unwrap();
        let d = format!("{:?}", k);
        assert!(d.contains("Matrix"));
        let s = format!("{}", k);
        assert!(s.contains("Matrix"));
        assert!(s.contains("1 row"));
        assert!(s.contains("symmetric"));
    }

    // ── field_from_{col,row}_values ─────────────────────────────────────────

    /// Saddle-point-shaped aggregate (K, C, Cᵀ): the output has one zone per
    /// distinct column support, **sharing the blocks' own handles**, and every
    /// column DOF reads back its slot in the flat vector.
    #[test]
    fn field_from_col_values_shares_block_supports_and_orders_values() {
        // Two supports on one Coords: phys (2 nodes) and mult (1 node).
        let coords = Handle::new(Coords::new(1).unwrap());
        let phys_nodes: Vec<Node> = (0..2)
            .map(|i| Node::create_in(coords.clone(), &[i as f64]).unwrap())
            .collect();
        let mult_node = Node::create_in(coords.clone(), &[10.0]).unwrap();
        let phys = {
            let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
            for n in &phys_nodes {
                sm.add_cell(&[n.id()]).unwrap();
            }
            Handle::new(sm)
        };
        let mult = {
            let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
            sm.add_cell(&[mult_node.id()]).unwrap();
            Handle::new(sm)
        };

        // K (phys × phys), C (mult × phys), Cᵀ (phys × mult).
        let mut k = SubMatrix::new(
            phys.clone(),
            phys.clone(),
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::Full,
        );
        k.add_entry(phys_nodes[0].id(), "q", phys_nodes[0].id(), "T", 1.0)
            .unwrap();
        let mut c = SubMatrix::new(
            mult.clone(),
            phys.clone(),
            vec!["imposed_T".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::None,
        );
        c.add_entry(mult_node.id(), "imposed_T", phys_nodes[0].id(), "T", 1.0)
            .unwrap();
        let mut ct = SubMatrix::new(
            phys.clone(),
            mult.clone(),
            vec!["q".into()],
            vec!["lambda_T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::None,
        );
        ct.add_entry(phys_nodes[0].id(), "q", mult_node.id(), "lambda_T", 1.0)
            .unwrap();

        let mut m = Matrix::empty();
        m.add_sub(Handle::new(k)).unwrap();
        m.add_sub(Handle::new(c)).unwrap();
        m.add_sub(Handle::new(ct)).unwrap();

        // Not finalized yet ⇒ error.
        assert!(m.field_from_col_values(&[0.0]).is_err());
        m.finalize().unwrap();

        // x holds its own flat index at every column DOF.
        let col_dofs = m.col_dofs().unwrap();
        let x: Vec<f64> = (0..col_dofs.len()).map(|i| i as f64).collect();
        let f = m.field_from_col_values(&x).unwrap();

        // One zone per distinct column support (phys ← K+C, mult ← Cᵀ),
        // each sharing the block's own handle — nothing rebuilt.
        assert_eq!(f.len(), 2);
        assert!(f.get(0).unwrap().read().support().same_object(&phys));
        assert!(f.get(1).unwrap().read().support().same_object(&mult));

        // Every column DOF reads back its slot; the aggregate is coherent.
        for (i, (nid, var)) in col_dofs.iter().enumerate() {
            assert_eq!(f.value(*nid, var).unwrap(), i as f64);
        }
        f.check().unwrap();

        // Wrong vector length is rejected.
        assert!(m.field_from_col_values(&x[..1]).is_err());
    }

    /// Two blocks on the **same** column support with different primal vars:
    /// one output zone carrying the union of the variables.
    #[test]
    fn field_from_col_values_unions_vars_on_a_shared_support() {
        use crate::containers::field::SubField;
        let (_cfg, nodes, sup) = make_poi1(2);
        let mut a = SubMatrix::new(
            sup.clone(),
            sup.clone(),
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::None,
        );
        a.add_entry(nodes[0].id(), "q", nodes[0].id(), "T", 1.0)
            .unwrap();
        let mut b = SubMatrix::new(
            sup.clone(),
            sup.clone(),
            vec!["r".into()],
            vec!["P".into()],
            DofOrdering::NodesThenVars,
            Symmetry::None,
        );
        b.add_entry(nodes[1].id(), "r", nodes[1].id(), "P", 1.0)
            .unwrap();

        let mut m = Matrix::empty();
        m.add_sub(Handle::new(a)).unwrap();
        m.add_sub(Handle::new(b)).unwrap();
        m.finalize().unwrap();

        let col_dofs = m.col_dofs().unwrap();
        let x: Vec<f64> = (0..col_dofs.len()).map(|i| 10.0 + i as f64).collect();
        let f = m.field_from_col_values(&x).unwrap();

        assert_eq!(f.len(), 1, "same support ⇒ one zone");
        {
            let z = f.get(0).unwrap().read();
            assert!(z.support().same_object(&sup));
            assert_eq!(SubField::components(&*z), &["T", "P"]);
        }
        for (i, (nid, var)) in col_dofs.iter().enumerate() {
            assert_eq!(f.value(*nid, var).unwrap(), 10.0 + i as f64);
        }
    }

    /// `row_mesh` / `col_mesh` expose the blocks' supports (shared handles,
    /// deduplicated); a field `restrict`ed onto them lands on those very
    /// supports, so it combines zone by zone with `mul_field`'s output —
    /// the external-forces-minus-internal-forces pattern.
    #[test]
    fn row_mesh_enables_zone_aligned_residual() {
        use crate::containers::node_field::SubNodeField;
        // Saddle-point shape: phys (2 nodes) and mult (1 node) supports.
        let coords = Handle::new(Coords::new(1).unwrap());
        let n0 = Node::create_in(coords.clone(), &[0.0]).unwrap();
        let n1 = Node::create_in(coords.clone(), &[1.0]).unwrap();
        let nm = Node::create_in(coords.clone(), &[10.0]).unwrap();
        let phys = {
            let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
            sm.add_cell(&[n0.id()]).unwrap();
            sm.add_cell(&[n1.id()]).unwrap();
            Handle::new(sm)
        };
        let mult = {
            let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
            sm.add_cell(&[nm.id()]).unwrap();
            Handle::new(sm)
        };
        // K (phys × phys) and C (mult × phys): row supports {phys, mult},
        // col supports {phys} — K and C share the phys column support.
        let mut k = SubMatrix::new(
            phys.clone(),
            phys.clone(),
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::Full,
        );
        k.add_entry(n0.id(), "q", n0.id(), "T", 2.0).unwrap();
        k.add_entry(n1.id(), "q", n1.id(), "T", 2.0).unwrap();
        let mut c = SubMatrix::new(
            mult.clone(),
            phys.clone(),
            vec!["imposed_T".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::None,
        );
        c.add_entry(nm.id(), "imposed_T", n0.id(), "T", 1.0)
            .unwrap();
        let mut m = Matrix::empty();
        m.add_sub(Handle::new(k)).unwrap();
        m.add_sub(Handle::new(c)).unwrap();

        // Meshes: available pre-finalize, deduplicated, sharing the handles.
        let rm = m.row_mesh().unwrap();
        assert_eq!(rm.len(), 2);
        assert!(rm.get(0).unwrap().same_object(&phys));
        assert!(rm.get(1).unwrap().same_object(&mult));
        let cm = m.col_mesh().unwrap();
        assert_eq!(cm.len(), 1, "K and C share the phys column support");
        assert!(cm.get(0).unwrap().same_object(&phys));

        m.finalize().unwrap();

        // f_int = A · x with x: T = [1, 1] on phys.
        let x = NodeField::from_sub(
            SubNodeField::from_poi1(&phys, vec!["T".into()])
                .map(|mut s| {
                    s.set_value(n0.id(), "T", 1.0).unwrap();
                    s.set_value(n1.id(), "T", 1.0).unwrap();
                    s
                })
                .unwrap(),
        );
        let f_int = m.mul_field(&x).unwrap();

        // External forces on their own support (as `flux` would build them),
        // projected onto the matrix's row mesh: lands on the blocks' handles.
        let f_ext = {
            let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
            sm.add_cell(&[n0.id()]).unwrap();
            sm.add_cell(&[n1.id()]).unwrap();
            let mut s = SubNodeField::from_poi1(&Handle::new(sm), vec!["q".into()]).unwrap();
            s.set_value(n0.id(), "q", 5.0).unwrap();
            s.set_value(n1.id(), "q", 5.0).unwrap();
            NodeField::from_sub(s)
        };
        let f_ext_r = crate::ops::node_field::restrict(&f_ext, &rm).unwrap();
        for (za, zb) in f_ext_r.iter().zip(f_int.iter()) {
            let sa = za.read().support();
            let sb = zb.read().support();
            assert!(
                sa.same_object(&sb),
                "restrict must land on the block supports"
            );
        }

        // Zone-aligned residual: q combines (not passthrough) on phys.
        // K·x: q = 2 at n0 and n1; C·x contributes imposed_T = 1 at nm.
        let r = (&f_ext_r - &f_int).unwrap();
        assert_eq!(r.value(n0.id(), "q").unwrap(), 3.0); // 5 − 2 ⇒ aligned
        assert_eq!(r.value(n1.id(), "q").unwrap(), 3.0);
        // `imposed_T` exists on the f_int side only (restrict carries the
        // source field's components) ⇒ union semantics pass it through RAW
        // (+1, not −1) — the documented `merge_components` behaviour.
        assert_eq!(r.value(nm.id(), "imposed_T").unwrap(), 1.0);

        // Strict residual (every component subtracted, missing read as 0):
        // reproject onto f_int's exact supports AND components.
        let f_ext_like = crate::ops::node_field::restrict_like(&f_ext, &f_int).unwrap();
        let r2 = (&f_ext_like - &f_int).unwrap();
        assert_eq!(r2.value(n0.id(), "q").unwrap(), 3.0);
        assert_eq!(r2.value(nm.id(), "imposed_T").unwrap(), -1.0); // 0 − 1
    }

    /// Row-side twin: zones on the blocks' row supports, dual variables.
    #[test]
    fn field_from_row_values_uses_row_supports_and_dual_vars() {
        let coords = Handle::new(Coords::new(1).unwrap());
        let r0 = Node::create_in(coords.clone(), &[0.0]).unwrap();
        let r1 = Node::create_in(coords.clone(), &[1.0]).unwrap();
        let c0 = Node::create_in(coords.clone(), &[2.0]).unwrap();
        let sup_r = {
            let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
            sm.add_cell(&[r0.id()]).unwrap();
            sm.add_cell(&[r1.id()]).unwrap();
            Handle::new(sm)
        };
        let sup_c = {
            let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
            sm.add_cell(&[c0.id()]).unwrap();
            Handle::new(sm)
        };
        let mut blk = SubMatrix::new(
            sup_r.clone(),
            sup_c,
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            Symmetry::None,
        );
        blk.add_entry(r0.id(), "q", c0.id(), "T", 1.0).unwrap();

        let mut m = Matrix::empty();
        m.add_sub(Handle::new(blk)).unwrap();
        m.finalize().unwrap();

        let f = m.field_from_row_values(&[3.0, 7.0]).unwrap();
        assert_eq!(f.len(), 1);
        assert!(f.get(0).unwrap().read().support().same_object(&sup_r));
        assert_eq!(f.value(r0.id(), "q").unwrap(), 3.0);
        assert_eq!(f.value(r1.id(), "q").unwrap(), 7.0);
    }
}