KiThe 0.3.6

A numerical suite for chemical kinetics and thermodynamics, combustion, heat and mass transfer,chemical engeneering. Work in progress. Advices and contributions will be appreciated
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
#![allow(dead_code)]
//! Canonical chemical equilibrium solver using log-mole formulation and reaction extent approach.
//!
//! # Purpose
//!
//! This module is the **central orchestrator** of the chemical equilibrium calculation
//! pipeline. It owns all equilibrium data (elemental composition, stoichiometry, Gibbs
//! functions, solver settings), manages the solve lifecycle, and publishes results.
//! The module solves chemical equilibrium problems by **minimizing Gibbs free energy**
//! using reaction extents in log-mole space.
//!
//! # Physical and Mathematical Background
//!
//! ## Log-Mole Formulation
//!
//! The solver uses **logarithmic mole variables** `y_i = ln(n_i)` instead of physical
//! moles `n_i`. This transformation:
//!
//! - Automatically enforces `n_i > 0` (physical constraint).
//! - Handles trace species with very small concentrations without underflow.
//! - Linearizes the mass-action equations near equilibrium.
//!
//! The residual vector `F(y)` has two blocks:
//!
//! 1. **Element conservation** (E equations, one per element):
//!    ```text
//!    F_elem = A^T · n(y) - b_0
//!    ```
//!    where `A` is the `(species × elements)` composition matrix, `n(y) = exp(y)`
//!    are physical moles, and `b_0` are the total element moles from the initial state.
//!
//! 2. **Reaction affinity** (R equations, one per independent reaction):
//!    ```text
//!    F_rxn = ν^T · (μ(y) / (R·T))
//!    ```
//!    where `ν` is the `(species × reactions)` stoichiometric matrix from SVD,
//!    and `μ(y) = μ°(T) + R·T·ln(a_i(y))` is the chemical potential.
//!
//! ## SVD Reaction Basis
//!
//! Independent reactions are computed via **SVD of the element composition matrix**:
//!
//! ```text
//! A = U · Σ · V^T
//! ```
//!
//! The nullspace of `A^T` (columns of `V` corresponding to zero singular values)
//! forms the stoichiometric matrix `ν`. This guarantees that reaction extents
//! automatically conserve elements.
//!
//! ## Scaling
//!
//! Row scaling normalizes residual rows by their characteristic magnitude,
//! improving numerical conditioning for systems with species at very different
//! concentration levels.
//!
//! # Architecture and Dataflow
//!
//! ```text
//!   ┌─────────────────────────────────────────────────────────────────────┐
//!   │                        EquilibriumLogMoles                          │
//!   │                                                                     │
//!   │  ┌──────────────┐  ┌──────────────┐  ┌──────────────────────────┐  │
//!   │  │  Problem Data │  │  Solver Data │  │  Publication State       │  │
//!   │  │  ─ elem_comp  │  │  ─ settings  │  │  ─ solution (log_moles)  │  │
//!   │  │  ─ stoich_mat │  │  ─ params    │  │  ─ moles (physical)     │  │
//!   │  │  ─ gibbs      │  │  ─ policy    │  │  ─ solve_report         │  │
//!   │  │  ─ phases     │  │  ─ budget    │  │  ─ keq_validation       │  │
//!   │  │  ─ n0         │  │  ─ seed      │  │  ─ mole_table           │  │
//!   │  │  ─ T, P, p0   │  │              │  │                          │  │
//!   │  └──────┬───────┘  └──────┬───────┘  └──────────┬───────────────┘  │
//!   └─────────┼─────────────────┼──────────────────────┼──────────────────┘
//!             │                 │                      │
//!             v                 v                      v
//!   ┌─────────────────────────────────────────────────────────────────────┐
//!   │  Solve Pipeline                                                     │
//!   │                                                                     │
//!   │  1. check_task() ──> validate dimensions and settings               │
//!   │  2. stage_equilibrium_system() ──> build SVD reaction basis         │
//!   │  3. build_solve_contract() ──> create residual/Jacobian closures    │
//!   │  4. solver_impl() ──> run backend cascade                           │
//!   │     ├── solve_backend_cascade() ──> try backends in order           │
//!   │     │   ├── Legacy(LM/NR/TR) ──> equilibrium_legacy_backend         │
//!   │     │   └── RustedSciThe ──> equilibrium_rst_backend                │
//!   │     └── validate_equilibrium_candidate() ──> acceptance gate        │
//!   │  5. publish_solve_candidate() ──> store accepted solution           │
//!   │  6. run_keq_cross_validation() ──> independent K_eq check           │
//!   └─────────────────────────────────────────────────────────────────────┘
//! ```
//!
//! ## Temperature Range Pipeline
//!
//! ```text
//!   solve_for_T_range(T_start, T_end, T_step)
//!//!     ├── build_temperature_ranges() ──> split range into segments
//!     ├── build_temperature_gibbs_cache() ──> precompute Gibbs functions
//!     ├── build_temperature_point_index() ──> map T → range segment
//!//!     └── for each temperature point:
//!           ├── continuation_seed_for_point() ──> seed from previous solution
//!           ├── solve_temperature_point_from_seed() ──> solve at this T
//!           └── publish_solve_candidate() ──> store result
//! ```
//!
//! # Key Structures
//!
//! | Structure | Role |
//! |-----------|------|
//! | [`EquilibriumLogMoles`] | Central orchestrator — owns all data and methods |
//! | [`SolverParams`] | Numerical solver tuning parameters (λ, tol, max_iter, etc.) |
//! | [`EquilibriumSolverSettings`] | High-level settings (backend policy, scaling, K_eq validation) |
//! | [`EquilibriumSolveContract`] | Bundles residual, Jacobian, and feasibility closures |
//! | [`EquilibriumSolveCandidate`] | One accepted candidate (solution + reports) |
//! | [`TemperatureWorkerSeed`] | Snapshot of solver state for temperature-range parallelism |
//! | [`TemperatureSolveSnapshot`] | Result of one temperature point solve |
//! | [`TemperatureSolveFailure`] | Error information for a failed temperature point |
//! | [`Phase`] | Phase descriptor (activity model + species indices) |
//! | [`Solvers`] | Enum: LM, NR, TR |
//!
//! # Key Free Functions
//!
//! | Function | Purpose |
//! |----------|---------|
//! | [`compute_species_moles`] | Converts log-moles `y` to physical moles `n = exp(y)` |
//! | [`compute_element_totals`] | Computes `A^T · n` — element totals from composition matrix |
//! | [`reaction_standard_gibbs`] | Computes `ν^T · g(T)` — standard Gibbs for each reaction |
//! | [`equilibrium_scaling`] | Computes row scaling factors for residual/Jacobian |
//! | [`species_to_phase_map`] | Maps each species index to its phase index |
//! | [`reaction_phase_stoichiometry`] | Aggregates stoichiometry by phase |
//! | [`evaluate_equilibrium_logmole_residual`] | Evaluates the full residual vector |
//! | [`evaluate_equilibrium_logmole_jacobian`] | Evaluates the full Jacobian matrix |
//! | [`scaled_residual`] / [`scaled_jacobian`] | Wraps residual/Jacobian with row scaling |
//! | [`validate_logmole_system_dimensions`] | Validates all matrix/vector dimensions |
//! | [`validate_residual_conditions`] | Validates T, P, p0 for residual evaluation |
//!
//! # Examples
//!
//! ```rust, ignore
//! use KiThe::Thermodynamics::ChemEquilibrium::equilibrium_log_moles::*;
//!
//! // Create equilibrium solver for gas-phase system
//! let mut solver = gas_solver(
//!     vec!["CO".to_string(), "CO2".to_string(), "O2".to_string()],
//!     1000.0, 101325.0, Solvers::LM, Some("info"), true
//! );
//! solver.solve().unwrap();
//! let solution = solver.accepted_solution().unwrap();
//! ```
//!
//! # Non-obvious Details
//!
//! - **Log-mole variables**: `y_i = ln(n_i)`. The solver works in log-mole space;
//!   `solver.solution` contains log-moles, not physical moles. Use
//!   [`compute_species_moles`] to convert.
//! - **SVD reaction basis**: The stoichiometric matrix is the nullspace of `A^T`,
//!   computed via SVD. This guarantees element conservation by construction.
//! - **Row scaling**: [`equilibrium_scaling`] computes per-row scale factors based on
//!   element totals and reaction standard Gibbs. Scaling is optional but recommended
//!   for systems with large concentration disparities.
//! - **Temperature continuation**: When solving over a temperature range, the solution
//!   at each point seeds the next point (unless `ContinuationSeedPolicy::IndependentPerPoint`
//!   is set). This dramatically improves convergence.
//! - **Backend cascade**: If one solver backend fails, the cascade tries the next
//!   backend in order, subject to the iteration budget.
//!
//! # Related Modules
//!
//! - [`equilibrium_nonlinear`](super::equilibrium_nonlinear) — numerical solver implementations
//! - [`equilibrium_workflows`](super::equilibrium_workflows) — phase control and convenience workflows
//! - [`equilibrium_problem`](super::equilibrium_problem) — typed input boundary
//! - [`equilibrium_validation`](super::equilibrium_validation) — backend-independent acceptance gate
//! - [`equilibrium_solver_policy`](super::equilibrium_solver_policy) — backend selection and cascade budget
//! - [`equilibrium_rst_backend`](super::equilibrium_rst_backend) — RustedSciThe symbolic backend
//! - [`equilibrium_legacy_backend`](super::equilibrium_legacy_backend) — legacy LM/NR/TR adapter
//! - [`equilibrium_activity`](super::equilibrium_activity) — activity models
//! - [`equilibrium_ids`](super::equilibrium_ids) — typed index wrappers
//! - [`equilibrium_constant_cross_validation`](super::equilibrium_constant_cross_validation) — K_eq cross-validation
//! - [`equilibrium_temperature_postprocessing`](super::equilibrium_temperature_postprocessing) — postprocessing
//!
use crate::Thermodynamics::ChemEquilibrium::equilibrium_activity::{
    PhaseActivityModel, phase_activity_models,
};
use crate::Thermodynamics::ChemEquilibrium::equilibrium_backend_adapter::{
    BackendSolveRequest, EquilibriumNonlinearBackend,
};
use crate::Thermodynamics::ChemEquilibrium::equilibrium_constant_cross_validation::{
    EquilibriumConstantCrossValidationStatus, EquilibriumConstantCrossValidationTolerances,
    classify_equilibrium_constant_cross_validation,
};
use crate::Thermodynamics::ChemEquilibrium::equilibrium_constant_problem::EquilibriumConstantProblem;
use crate::Thermodynamics::ChemEquilibrium::equilibrium_constant_solver::{
    EquilibriumConstantSolver, EquilibriumConstantSolverMode,
};
use crate::Thermodynamics::ChemEquilibrium::equilibrium_constant_validation::{
    EquilibriumConstantValidationMode, EquilibriumConstantValidationTolerances,
};
use crate::Thermodynamics::ChemEquilibrium::equilibrium_nonlinear::{
    ReactionBasis, ReactionExtentError, compute_reaction_basis,
};
use crate::Thermodynamics::ChemEquilibrium::equilibrium_problem::{
    DEFAULT_TRACE_MOLE_FLOOR, EquilibriumConditions, EquilibriumProblem, EquilibriumSolution,
    LogMolesInitialGuess, PreparedEquilibriumProblem, TraceSpeciesSeedPolicy,
};
use crate::Thermodynamics::ChemEquilibrium::equilibrium_rst_backend::{
    RstPreparedProblem, prepare_rst_symbolic_problem,
};
use crate::Thermodynamics::ChemEquilibrium::equilibrium_solver_policy::{
    EquilibriumSolveReport, SolverAttemptFailureKind, SolverAttemptOutcome, SolverAttemptReport,
    SolverBackend, SolverCascadeBudget, SolverPolicy,
};
use crate::Thermodynamics::ChemEquilibrium::equilibrium_temperature_postprocessing::{
    TemperaturePostprocessingPolicy, TemperaturePostprocessingResult,
    postprocess_temperature_series,
};
use crate::Thermodynamics::ChemEquilibrium::equilibrium_validation::{
    EquilibriumAcceptanceCriteria, EquilibriumCandidateReport, EquilibriumCandidateResiduals,
    validate_equilibrium_candidate,
};
use crate::Thermodynamics::ChemEquilibrium::equilibrium_workflows::{
    PhaseControlledSolveReport, PhaseManager, multiphase_equilibrium_residual_generator_sym,
};
use crate::Thermodynamics::User_substances::SubsData;
use crate::Thermodynamics::User_substances_error::SubsDataError;
use RustedSciThe::symbolic::symbolic_engine::Expr;
use RustedSciThe::symbolic::symbolic_functions::Jacobian;
use log::{error, info, warn};
use nalgebra::{DMatrix, DVector};
use prettytable::{Cell, Row, Table};
use std::collections::{HashMap, HashSet};
use std::default::Default;
use std::f64;
use std::rc::Rc;
use std::time::Instant;
/// Universal gas constant in J/(mol·K)
pub const R: f64 = 8.314;

/// Configuration parameters for numerical solvers.
///
/// These parameters control the convergence behavior of the nonlinear solvers
/// (LM, NR, Trust Region). They are validated by
/// [`EquilibriumSolverSettings::validate()`] before any solver is constructed.
#[derive(Clone)]
pub struct SolverParams {
    /// Maximum number of nonlinear iterations before giving up.
    pub max_iter: usize,
    /// Convergence tolerance for the residual L2 norm.
    /// The solver stops when `||F(x)|| < tol`.
    pub tol: f64,
    /// Initial damping parameter for the Levenberg-Marquardt solver.
    /// Controls the blend between Gauss-Newton (λ → 0) and gradient descent (λ → ∞).
    pub lambda: f64,
    /// Minimum step size in line search before declaring failure.
    /// If the Armijo condition cannot be satisfied with α ≥ alpha_min, the solver stops.
    pub alpha_min: f64,
    /// Initial trust-region radius for the Trust Region solver.
    pub delta_init: f64,
    /// Maximum trust-region radius. Must be ≥ delta_init.
    pub delta_max: f64,
    /// Acceptance threshold for the trust-region ratio ρ.
    /// If ρ > eta, the step is accepted; if ρ < eta/4, the radius shrinks.
    /// Must lie in the open interval (0, 1).
    pub eta: f64,
}

/// Backend-specific solver controls separated from the physical equilibrium data.
///
/// The `EquilibriumLogMoles` facade still carries domain data, sweep state, and
/// published results. This structure groups the numerical policy knobs that are
/// specific to backend selection and continuation behavior.
#[derive(Clone)]
pub struct EquilibriumSolverSettings {
    /// Numerical solver parameters (max iterations, tolerances, damping, trust-region radii).
    pub solver_params: SolverParams,
    /// Preferred legacy solver backend (LM, NR, or TR) used when `solver_policy` is `None`.
    pub solver: Solvers,
    /// Whether to enable row scaling of the residual and Jacobian.
    /// When `true`, each residual/Jacobian row is divided by its scale factor.
    pub scaling_flag: bool,
    /// Typed policy for generating implicit log-mole seeds from physical
    /// initial moles when the caller did not provide an explicit seed.
    pub trace_seed_policy: TraceSpeciesSeedPolicy,
    /// Optional explicit backend policy. `None` retains the historical order
    /// beginning with [`Self::solver`] while callers migrate to typed policies.
    pub solver_policy: Option<SolverPolicy>,
    /// Optional resource limits for a solver cascade. `None` derives a budget
    /// from the current policy and legacy per-backend iteration setting.
    pub solver_budget: Option<SolverCascadeBudget>,
    /// Controls whether the accepted solution is additionally checked by the
    /// independent equilibrium-constant validator.
    pub keq_validation_mode: EquilibriumConstantValidationMode,
    /// Numerical tolerances for the independent cross-validation report.
    pub keq_validation_tolerances: EquilibriumConstantCrossValidationTolerances,
    /// Explicit warm-start behavior for sequential and chunked temperature
    /// sweeps. The fully parallel `par2` sweep is always independent.
    pub continuation_seed_policy: ContinuationSeedPolicy,
}

impl Default for EquilibriumSolverSettings {
    fn default() -> Self {
        Self {
            solver_params: SolverParams::default(),
            solver: Solvers::LM,
            scaling_flag: false,
            trace_seed_policy: TraceSpeciesSeedPolicy::Absolute {
                floor: DEFAULT_TRACE_MOLE_FLOOR,
            },
            solver_policy: None,
            solver_budget: None,
            keq_validation_mode: EquilibriumConstantValidationMode::Off,
            keq_validation_tolerances: EquilibriumConstantCrossValidationTolerances::default(),
            continuation_seed_policy: ContinuationSeedPolicy::default(),
        }
    }
}

impl EquilibriumSolverSettings {
    /// Validates numerical controls before any residual, Jacobian, or backend
    /// is constructed.
    ///
    /// Keeping this check with the settings bundle makes the canonical
    /// one-shot API and the temporary mutable facade reject the same malformed
    /// solver request at the same boundary.
    pub fn validate(&self) -> Result<(), ReactionExtentError> {
        let params = &self.solver_params;
        if params.max_iter == 0 {
            return Err(ReactionExtentError::InvalidProblem {
                field: "solver_params.max_iter",
                message: "max_iter must be greater than zero".to_string(),
            });
        }
        for (field, value) in [
            ("solver_params.tol", params.tol),
            ("solver_params.lambda", params.lambda),
            ("solver_params.alpha_min", params.alpha_min),
            ("solver_params.delta_init", params.delta_init),
            ("solver_params.delta_max", params.delta_max),
        ] {
            if !value.is_finite() || value <= 0.0 {
                return Err(ReactionExtentError::InvalidProblem {
                    field,
                    message: "value must be finite and strictly positive".to_string(),
                });
            }
        }
        if params.delta_max < params.delta_init {
            return Err(ReactionExtentError::InvalidProblem {
                field: "solver_params.delta_max",
                message: "delta_max must be greater than or equal to delta_init".to_string(),
            });
        }
        if !params.eta.is_finite() || !(0.0..1.0).contains(&params.eta) {
            return Err(ReactionExtentError::InvalidProblem {
                field: "solver_params.eta",
                message: "eta must be finite and lie in the open interval (0, 1)".to_string(),
            });
        }

        if let Some(policy) = &self.solver_policy {
            if policy.ordered_backends().is_empty() {
                return Err(ReactionExtentError::InvalidProblem {
                    field: "solver_policy",
                    message: "a solver policy must contain at least one backend".to_string(),
                });
            }
        }
        if let Some(budget) = self.solver_budget {
            if budget.max_attempts == 0
                || budget.max_iterations_per_attempt == 0
                || budget.max_total_iterations == 0
            {
                return Err(ReactionExtentError::InvalidProblem {
                    field: "solver_budget",
                    message: "attempt, per-attempt, and total iteration limits must be positive"
                        .to_string(),
                });
            }
        }
        self.keq_validation_tolerances.validate()?;
        Ok(())
    }
}

/// Available numerical solvers for equilibrium calculations.
///
/// These are the legacy hand-written solvers. New code should prefer
/// [`SolverBackend::RustedSciThe`](crate::Thermodynamics::ChemEquilibrium::equilibrium_solver_policy::SolverBackend::RustedSciThe)
/// via the [`SolverPolicy`](crate::Thermodynamics::ChemEquilibrium::equilibrium_solver_policy::SolverPolicy) system.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Solvers {
    /// Levenberg-Marquardt solver with adaptive damping.
    LM,
    /// Newton-Raphson solver with line search and feasibility constraints.
    NR,
    /// Trust Region solver with adaptive radius (dogleg method).
    TR,
}

/// Defines how a temperature sweep obtains the initial log-mole iterate for
/// each requested temperature point.
///
/// `PreviousAccepted` is a continuation method: an accepted point warms the
/// next point. `IndependentPerPoint` always reuses the seed configured before
/// the sweep, which is useful for reproducible point-by-point comparisons.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ContinuationSeedPolicy {
    PreviousAccepted,
    IndependentPerPoint,
}

impl Default for ContinuationSeedPolicy {
    fn default() -> Self {
        Self::PreviousAccepted
    }
}

impl Default for SolverParams {
    fn default() -> Self {
        Self {
            max_iter: 50,
            tol: 1e-6,
            lambda: 1e-3,
            alpha_min: 1e-6,
            delta_init: 1.0,
            delta_max: 100.0,
            eta: 0.1,
        }
    }
}
/// Central orchestrator for chemical equilibrium calculations.
///
/// This struct owns all equilibrium data (elemental composition, stoichiometry,
/// Gibbs functions, solver settings), manages the solve lifecycle, and publishes
/// results. It is the primary entry point for the canonical equilibrium solver.
///
/// # Dataflow
///
/// See the [module-level documentation](self) for a complete dataflow diagram.
///
/// # Warning
///
/// This struct has **22 public fields** (see SourceCraft Diagnostics item A.4
/// in [`TODO_ANALYSIS.md`](https://todo)). Many fields are interdependent and
/// should be modified through methods rather than directly. Prefer using
/// [`EquilibriumProblem`](super::equilibrium_problem::EquilibriumProblem) for
/// constructing new problems and [`from_problem()`](Self::from_problem) for
/// loading them into the solver.
pub struct EquilibriumLogMoles {
    /// Substance database with thermochemical data for all species.
    pub subs_data: SubsData,
    /// Element composition matrix `A` of shape `(species × elements)`.
    /// `A[i, j]` = number of atoms of element `j` in species `i`.
    pub elem_composition: DMatrix<f64>,
    /// SVD-derived reaction basis containing the stoichiometric nullspace.
    pub reaction_basis: ReactionBasis,
    /// Optional explicit initial guess in log-mole space (`y_i = ln(n_i)`).
    /// If `None`, the solver derives a seed from `n0` and the trace policy.
    pub initial_guess: Option<Vec<f64>>,
    /// System pressure in Pascals (Pa).
    pub P: f64,
    /// Temperature in Kelvin (K).
    pub T: f64,
    /// Stoichiometric matrix `ν` of shape `(species × reactions)`.
    /// Columns are independent reaction directions from the SVD nullspace.
    pub stoich_matrix: DMatrix<f64>,
    /// Initial physical moles `n_i^0` for each species.
    pub n0: Vec<f64>,
    /// Standard Gibbs free energy functions `g_i(T)` for each species, in J/mol.
    pub gibbs: Vec<GibbsFn>,
    /// Symbolic expressions for Gibbs free energy (used by RustedSciThe backend).
    pub gibbs_sym: Vec<Expr>,
    /// Phase descriptors: activity model and species indices for each phase.
    pub phases: Vec<Phase>,
    /// Maps each species index to its phase index: `species_phase[i]` = phase of species i.
    pub species_phase: Vec<usize>,
    /// Persistent phase-control mask for the current equilibrium problem.
    /// It survives outer-loop restarts and is reset only when a new problem
    /// or phase inventory is staged.
    pub(crate) phase_active_mask: Vec<bool>,
    /// Legacy temperature-only failure list retained for callers that only
    /// need a quick visual summary. Prefer [`Self::temperature_failures`].
    pub list_of_failed_T: Vec<f64>,
    /// Typed diagnostics for every temperature point whose solve was not
    /// accepted during the most recent sweep.
    pub temperature_failures: Vec<TemperatureSolveFailure>,
    /// Immutable evidence for every accepted temperature point from the most
    /// recent sweep, in deterministic temperature order.
    pub temperature_solutions: Vec<TemperatureSolveSnapshot>,
    /// Accepted solution in log-mole space: `y_i = ln(n_i)`.
    /// Published by [`publish_solve_candidate()`](Self::publish_solve_candidate).
    pub solution: Vec<f64>,
    /// Accepted solution in physical moles: `n_i = exp(y_i)`.
    /// Published simultaneously with [`solution`](Self::solution).
    pub moles: Vec<f64>,
    /// Validation evidence for the currently published solution.
    pub last_validation_report: Option<EquilibriumCandidateReport>,
    /// Backend-selection trace for the currently published solution.
    pub last_solve_report: Option<EquilibriumSolveReport>,
    /// Optional independent K_eq cross-validation status for the last accepted solve.
    pub last_keq_validation_status: Option<EquilibriumConstantCrossValidationStatus>,
    /// Active-set transition evidence for the last accepted phase-controlled solve.
    pub last_phase_control_report: Option<PhaseControlledSolveReport>,
    /// Element totals vector `b_0 = A^T · n0` — total moles of each element.
    /// Used in the element conservation residual block.
    pub elements_vector: Vec<f64>,
    /// Optional log level for diagnostic output (e.g., "info", "debug").
    pub loglevel: Option<String>,
    /// Species-level epsilon for phase detection thresholds.
    pub species_eps: f64,
    /// Substate-level epsilon for phase detection thresholds.
    pub substate_eps: f64,
    /// Map from substance name to vector of moles across temperature points.
    /// Populated by [`map_of_moles_for_each_substance()`](Self::map_of_moles_for_each_substance).
    pub map_of_moles_for_each_substance: HashMap<String, Vec<f64>>,
    /// Raw temperature-range results: `(temperature, moles_at_this_T)` pairs.
    pub moles_for_T_range: Vec<(f64, Vec<f64>)>,
    /// Solver configuration: backend policy, scaling, K_eq validation, etc.
    pub solver_settings: EquilibriumSolverSettings,
    /// Phase manager for multiphase equilibrium control (hysteresis, thresholds).
    pub phase_manager: PhaseManager,
    /// Reference pressure in Pascals (typically 101325 Pa = 1 atm).
    pub p0: f64,
}

/// Immutable bundle of closures and symbolic state for one nonlinear solve.
///
/// The builder centralizes the formulas for single solves and every sweep
/// variant so backend selection cannot drift between entry points.
#[allow(dead_code)]
pub(crate) struct EquilibriumSolveContract {
    /// Unscaled residual closure: `F_raw(y) = [A^T·n(y) - b0; ν^T·μ/(R·T)]`.
    f_raw: Rc<dyn Fn(&[f64]) -> Result<Vec<f64>, ReactionExtentError>>,
    /// Scaled residual closure (if scaling is enabled), or same as `f_raw`.
    f: Box<dyn Fn(&[f64]) -> Result<Vec<f64>, ReactionExtentError>>,
    /// Optional analytical Jacobian closure `J(y) = dF/dy`.
    /// `None` for backends that compute their own Jacobian (e.g., RustedSciThe).
    j: Option<Box<dyn Fn(&[f64]) -> Result<DMatrix<f64>, ReactionExtentError>>>,
    /// Optional precomputed symbolic problem for the RustedSciThe backend.
    rst_problem: Option<RstPreparedProblem>,
}

/// Fully validated result of one nonlinear solve before it is published.
///
/// Phase-control restarts use this bundle transactionally: an intermediate
/// candidate may guide the next active-set decision, but it cannot overwrite
/// the last accepted public state until the outer loop reaches a fixed point.
pub(crate) struct EquilibriumSolveCandidate {
    /// Accepted solution in log-mole space: `y_i = ln(n_i)`.
    pub(crate) log_moles: Vec<f64>,
    /// Reconstructed physical moles: `n_i = exp(y_i)`.
    pub(crate) moles: Vec<f64>,
    /// Map from substance name to moles (for display/export).
    pub(crate) mole_table: HashMap<String, Vec<f64>>,
    /// Backend-independent acceptance evidence.
    pub(crate) validation_report: EquilibriumCandidateReport,
    /// Backend-selection trace for this candidate.
    pub(crate) solve_report: EquilibriumSolveReport,
    /// Optional K_eq cross-validation status.
    pub(crate) keq_validation_status: Option<EquilibriumConstantCrossValidationStatus>,
}

#[allow(dead_code)]
impl EquilibriumSolveContract {
    fn residual(&self, values: &[f64]) -> Result<Vec<f64>, ReactionExtentError> {
        (self.f.as_ref())(values)
    }

    fn raw_residual(&self, values: &[f64]) -> Result<Vec<f64>, ReactionExtentError> {
        (self.f_raw.as_ref())(values)
    }

    fn jacobian(&self) -> Option<&dyn Fn(&[f64]) -> Result<DMatrix<f64>, ReactionExtentError>> {
        self.j.as_deref()
    }
}

/// Snapshot of the state copied into a temperature-sweep worker.
///
/// This keeps the parallel sweep closures free of the main solver object, so
/// rayon never sees the `Rc`-backed fields that make the facade itself
/// non-`Sync`.
/// Snapshot of the state copied into a temperature-sweep worker.
///
/// This keeps the parallel sweep closures free of the main solver object, so
/// rayon never sees the `Rc`-backed fields that make the facade itself
/// non-`Sync`.
#[derive(Clone)]
pub(crate) struct TemperatureWorkerSeed {
    /// Cloned substance database for the worker.
    subs_data: SubsData,
    /// Element composition matrix `A` (species × elements).
    elem_composition: DMatrix<f64>,
    /// SVD-derived reaction basis.
    reaction_basis: ReactionBasis,
    /// Stoichiometric matrix `ν` (species × reactions).
    stoich_matrix: DMatrix<f64>,
    /// Initial physical moles `n_i^0`.
    n0: Vec<f64>,
    /// Phase descriptors.
    phases: Vec<Phase>,
    /// Species-to-phase mapping.
    species_phase: Vec<usize>,
    /// Element totals vector `b_0`.
    elements_vector: Vec<f64>,
    /// Optional log level for diagnostic output.
    loglevel: Option<String>,
    /// Species-level epsilon for phase detection.
    species_eps: f64,
    /// Substate-level epsilon for phase detection.
    substate_eps: f64,
    /// Solver configuration (backend policy, scaling, etc.).
    solver_settings: EquilibriumSolverSettings,
    /// System pressure in Pa.
    P: f64,
    /// Reference pressure in Pa.
    p0: f64,
}

impl TemperatureWorkerSeed {
    /// Copies the frozen seed state into a local solver instance for one temperature point.
    ///
    /// This is the core cloning mechanism for parallel temperature sweeps. It copies
    /// all problem data (element composition, reaction basis, stoichiometry, phases,
    /// solver settings) into the local solver, sets the temperature, and clears any
    /// previously published state. The worker can then call `local_solver.solve()`
    /// independently without affecting other workers or the main solver.
    pub(crate) fn apply(
        &self,
        local_solver: &mut EquilibriumLogMoles,
        temperature: f64,
        gibbs: Vec<GibbsFn>,
        initial_guess: Option<Vec<f64>>,
    ) {
        local_solver.subs_data = self.subs_data.clone();
        local_solver.elem_composition = self.elem_composition.clone();
        local_solver.reaction_basis = self.reaction_basis.clone();
        local_solver.stoich_matrix = self.stoich_matrix.clone();
        local_solver.n0 = self.n0.clone();
        local_solver.gibbs = gibbs;
        local_solver.gibbs_sym = Vec::new();
        local_solver.phases = self.phases.clone();
        local_solver.species_phase = self.species_phase.clone();
        local_solver.list_of_failed_T.clear();
        local_solver.temperature_failures.clear();
        local_solver.temperature_solutions.clear();
        local_solver.solution.clear();
        local_solver.moles.clear();
        local_solver.last_validation_report = None;
        local_solver.last_solve_report = None;
        local_solver.last_keq_validation_status = None;
        local_solver.last_phase_control_report = None;
        local_solver.elements_vector = self.elements_vector.clone();
        local_solver.loglevel = self.loglevel.clone();
        local_solver.species_eps = self.species_eps;
        local_solver.substate_eps = self.substate_eps;
        local_solver.moles_for_T_range.clear();
        local_solver.solver_settings = self.solver_settings.clone();
        local_solver.P = self.P;
        local_solver.T = temperature;
        local_solver.p0 = self.p0;
        local_solver.initial_guess = initial_guess;
    }

    /// Returns the number of species (length of `n0`).
    pub(crate) fn species_count(&self) -> usize {
        self.n0.len()
    }

    /// Returns a reference to the initial moles vector.
    pub(crate) fn n0(&self) -> &[f64] {
        &self.n0
    }

    /// Returns the pressure in Pa.
    pub(crate) fn pressure(&self) -> f64 {
        self.P
    }

    /// Returns the reference pressure in Pa.
    pub(crate) fn reference_pressure(&self) -> f64 {
        self.p0
    }

    /// Returns a reference to the solver settings.
    pub(crate) fn solver_settings(&self) -> &EquilibriumSolverSettings {
        &self.solver_settings
    }

    /// Returns a reference to the element composition matrix.
    pub(crate) fn elem_composition(&self) -> &DMatrix<f64> {
        &self.elem_composition
    }

    /// Returns a reference to the stoichiometry matrix.
    pub(crate) fn stoich_matrix(&self) -> &DMatrix<f64> {
        &self.stoich_matrix
    }

    /// Returns a reference to the phases vector.
    pub(crate) fn phases(&self) -> &[Phase] {
        &self.phases
    }

    /// Returns a reference to the species-to-phase map.
    pub(crate) fn species_phase(&self) -> &[usize] {
        &self.species_phase
    }

    /// Returns a reference to the element totals vector.
    pub(crate) fn elements_vector(&self) -> &[f64] {
        &self.elements_vector
    }

    /// Returns the species epsilon (phase detection threshold).
    pub(crate) fn species_eps(&self) -> f64 {
        self.species_eps
    }

    /// Returns the substate epsilon.
    pub(crate) fn substate_eps(&self) -> f64 {
        self.substate_eps
    }
}

/// One non-accepted temperature point from a continuation or parallel sweep.
///
/// Failure diagnostics are kept separate from successful rows so consumers
/// cannot mistake a stale solution for a result at the failed temperature.
#[derive(Debug, Clone)]
pub struct TemperatureSolveFailure {
    /// Temperature requested for this point, in K.
    pub temperature: f64,
    /// Stable diagnostic text for non-cascade and setup failures.
    pub message: String,
    /// Ordered backend trace when the failure exhausted a solver cascade.
    pub attempts: Vec<SolverAttemptReport>,
}

/// One accepted temperature point together with the evidence that admitted it.
#[derive(Debug, Clone)]
pub struct TemperatureSolveSnapshot {
    /// Temperature requested for this point, in K.
    pub temperature: f64,
    /// Accepted nonlinear coordinates `ln(n_i)` in canonical species order.
    pub log_moles: Vec<f64>,
    /// Reconstructed physical moles in the same species order.
    pub moles: Vec<f64>,
    /// Backend-independent acceptance evidence.
    pub validation: EquilibriumCandidateReport,
    /// Ordered numerical backend trace for this accepted point.
    pub solve_report: EquilibriumSolveReport,
}

/// Constructs a typed failure record from a temperature point that could not be solved.
///
/// Extracts the ordered backend cascade trace from `AllBackendsFailed` errors,
/// or creates a simple diagnostic message for non-cascade failures (e.g., invalid
/// temperature range, dimension mismatch).
pub(crate) fn temperature_failure(temperature: f64, error: &ReactionExtentError) -> TemperatureSolveFailure {
    let attempts = match error {
        ReactionExtentError::AllBackendsFailed { attempts }
        | ReactionExtentError::CascadeAborted { attempts, .. } => attempts.clone(),
        _ => Vec::new(),
    };
    TemperatureSolveFailure {
        temperature,
        message: error.to_string(),
        attempts,
    }
}

/// Selects the seed for one temperature point without mutating solver state.
///
/// Keeping this selection pure makes the continuation contract testable and
/// avoids accidental seed reuse when sweep implementations are rearranged.
pub(crate) fn continuation_seed_for_point(
    policy: ContinuationSeedPolicy,
    configured_seed: &Option<Vec<f64>>,
    previous_accepted_seed: &Option<Vec<f64>>,
) -> Option<Vec<f64>> {
    match policy {
        ContinuationSeedPolicy::PreviousAccepted => previous_accepted_seed.clone(),
        ContinuationSeedPolicy::IndependentPerPoint => configured_seed.clone(),
    }
}

impl EquilibriumLogMoles {
    /// Returns `true` when the current mutable facade has enough prepared
    /// thermochemical data to build the RST symbolic problem directly.
    ///
    /// The canonical typed problem can always carry numeric Gibbs closures, but
    /// the RST adapter still needs prepared searchable thermochemical data for
    /// the symbolic bridge. This helper keeps the default policy honest: RST
    /// Checks whether the solver has the symbolic Gibbs expressions needed by the RST backend.
    ///
    /// The RST backend requires `gibbs_sym` (symbolic `Expr` closures) in addition to
    /// the numeric `GibbsFn` closures. This method returns `true` only when the symbolic
    /// vector is non-empty and its length matches the substance database. Falling back to
    /// the legacy backend when this returns `false` prevents a cryptic RST initialization
    /// failure.
    pub(crate) fn has_rst_symbolic_context(&self) -> bool {
        (self.gibbs_sym.len() == self.subs_data.substances.len() && !self.gibbs_sym.is_empty())
            || !self.subs_data.search_results.is_empty()
            || !self.subs_data.search_states.is_empty()
            || !self.subs_data.therm_map_of_sym.is_empty()
            || !self.subs_data.therm_map_of_fun.is_empty()
    }

    /// Validates a temperature-grid request before it can enter a mutable
    /// coefficient-selection or solve path.
    ///
    /// All three sweep implementations use this gate so an invalid step cannot
    /// Validates temperature range parameters before any sweep setup.
    ///
    /// Checks that:
    /// - All values are finite and strictly positive (T > 0 K).
    /// - `T_end >= T_start` (non-decreasing range).
    /// - `T_step > 0` (strictly positive step).
    /// - The range produces at least one point.
    ///
    /// This prevents division by zero, infinite loops, and nonsensical temperature
    /// inputs before any solver resources are allocated.
    pub(crate) fn validate_temperature_range(
        T_start: f64,
        T_end: f64,
        T_step: f64,
    ) -> Result<(), ReactionExtentError> {
        for (field, value) in [("T_start", T_start), ("T_end", T_end), ("T_step", T_step)] {
            if !value.is_finite() {
                return Err(ReactionExtentError::InvalidProblem {
                    field,
                    message: "temperature-range values must be finite".to_string(),
                });
            }
        }
        if T_step <= 0.0 {
            return Err(ReactionExtentError::InvalidProblem {
                field: "T_step",
                message: "temperature step must be positive".to_string(),
            });
        }
        if T_start >= T_end {
            return Err(ReactionExtentError::InvalidProblem {
                field: "temperature_range",
                message: "T_start must be strictly smaller than T_end".to_string(),
            });
        }
        Ok(())
    }

    /// Builds ordered Gibbs closures from the current `SubsData` coefficients.
    ///
    /// The substance list is the canonical ordering for all solver vectors, so
    /// a missing entry is a typed setup error rather than an `unwrap()` panic.
    pub(crate) fn ordered_gibbs_functions(
        substances: &[String],
        mut functions: HashMap<String, Box<dyn Fn(f64) -> f64 + Send + Sync>>,
    ) -> Result<Vec<Box<dyn Fn(f64) -> f64 + Send + Sync>>, ReactionExtentError> {
        substances
            .iter()
            .map(|substance| {
                functions.remove(substance).ok_or_else(|| {
                    ReactionExtentError::SubsDataError(SubsDataError::MissingData {
                        field: "standard Gibbs closure".to_string(),
                        substance: substance.clone(),
                    })
                })
            })
            .collect()
    }

    /// Builds ordered Gibbs closures from the current `SubsData` coefficients.
    ///
    /// The substance list is the canonical ordering for all solver vectors, so
    /// Builds Gibbs closures for all species in the solver's current substance order.
    ///
    /// Delegates to [`Self::ordered_gibbs_functions`] using the solver's own
    /// `subs_data.substances` list. This is the single-threaded path used by
    /// sequential temperature sweeps and single-point solves.
    pub(crate) fn build_gibbs_functions(&mut self) -> Result<Vec<GibbsFn>, ReactionExtentError> {
        let substances = self.subs_data.substances.clone();
        Self::ordered_gibbs_functions(&substances, self.subs_data.calculate_dG0_fun_one_phase()?)?
            .into_iter()
            .map(|function| Ok(Rc::new(move |temperature: f64| function(temperature)) as GibbsFn))
            .collect()
    }

    /// Builds thread-safe Gibbs closures with the same ordering and missing-data
    /// contract as [`Self::build_gibbs_functions`].
    pub(crate) fn build_parallel_gibbs_functions(
        &mut self,
    ) -> Result<Vec<std::sync::Arc<dyn Fn(f64) -> f64 + Send + Sync>>, ReactionExtentError> {
        let substances = self.subs_data.substances.clone();
        Self::ordered_gibbs_functions(&substances, self.subs_data.calculate_dG0_fun_one_phase()?)?
            .into_iter()
            .map(|function| {
                Ok(
                    std::sync::Arc::new(move |temperature: f64| function(temperature))
                        as std::sync::Arc<dyn Fn(f64) -> f64 + Send + Sync>,
                )
            })
            .collect()
    }

    /// Copies the complete accepted-solve evidence into one sweep snapshot.
    pub(crate) fn snapshot_published_solution(
        &self,
        temperature: f64,
    ) -> Result<TemperatureSolveSnapshot, ReactionExtentError> {
        let validation = self.last_validation_report.clone().ok_or_else(|| {
            ReactionExtentError::InvalidProblem {
                field: "temperature_snapshot",
                message: "accepted temperature solve did not publish validation evidence"
                    .to_string(),
            }
        })?;
        let solve_report =
            self.last_solve_report
                .clone()
                .ok_or_else(|| ReactionExtentError::InvalidProblem {
                    field: "temperature_snapshot",
                    message: "accepted temperature solve did not publish a backend report"
                        .to_string(),
                })?;
        Ok(TemperatureSolveSnapshot {
            temperature,
            log_moles: self.solution.clone(),
            moles: self.moles.clone(),
            validation,
            solve_report,
        })
    }

    /// Builds the legacy mutable engine from one validated canonical problem.
    ///
    /// New callers should construct [`EquilibriumProblem`] first. This adapter
    /// exists while the existing temperature-sweep and phase-control code is
    /// migrated away from its public mutable fields.
    pub fn from_problem(problem: EquilibriumProblem) -> Result<Self, ReactionExtentError> {
        let prepared = PreparedEquilibriumProblem::new(problem)?;
        let (
            species,
            initial_moles,
            initial_log_moles,
            element_composition,
            gibbs,
            phases,
            conditions,
            reaction_basis,
            element_totals,
            species_phase,
        ) = prepared.into_legacy_parts();
        let mut solver = Self::empty();
        solver.subs_data.substances = species;
        solver.n0 = initial_moles;
        solver.initial_guess = Some(initial_log_moles);
        solver.elem_composition = element_composition;
        solver.gibbs = gibbs;
        solver.phases = phases;
        solver.T = conditions.temperature();
        solver.P = conditions.pressure();
        solver.p0 = conditions.reference_pressure();
        solver.stoich_matrix = reaction_basis.reactions.clone();
        solver.reaction_basis = reaction_basis;
        solver.elements_vector = element_totals;
        solver.species_phase = species_phase;
        Ok(solver)
    }

    /// Solves one canonical equilibrium problem with the default solver
    /// configuration.
    ///
    /// This is the explicit construction path the refactor is steering toward:
    /// build a validated problem, optionally adjust typed solver settings, then
    /// perform one solve and return the accepted immutable snapshot.
    pub fn solve_problem(
        problem: EquilibriumProblem,
    ) -> Result<EquilibriumSolution, ReactionExtentError> {
        Self::solve_problem_with(problem, |_| {})
    }

    /// Solves one canonical equilibrium problem after applying a typed solver
    /// configuration hook.
    ///
    /// The hook receives only [`EquilibriumSolverSettings`], so callers can
    /// tune policies and budgets without mutating the already validated
    /// chemistry, phase layout, or thermodynamic conditions.
    pub fn solve_problem_with<F>(
        problem: EquilibriumProblem,
        configure: F,
    ) -> Result<EquilibriumSolution, ReactionExtentError>
    where
        F: FnOnce(&mut EquilibriumSolverSettings),
    {
        let mut solver = Self::from_problem(problem)?;
        configure(&mut solver.solver_settings);
        solver.solve()?;
        solver.accepted_solution()
    }

    /// Returns an immutable snapshot only when the current candidate passed
    /// the backend-independent acceptance gate.
    ///
    /// Legacy mutable fields remain available during migration, but new
    /// callers should use this method rather than combining them manually.
    pub fn accepted_solution(&self) -> Result<EquilibriumSolution, ReactionExtentError> {
        let validation = self.last_validation_report.clone().ok_or_else(|| {
            ReactionExtentError::InvalidCandidate {
                field: "accepted_solution",
                message: "no accepted equilibrium solution is available".to_string(),
            }
        })?;
        let conditions = crate::Thermodynamics::ChemEquilibrium::equilibrium_problem::EquilibriumConditions::new(
            self.T, self.P, self.p0,
        )?;
        EquilibriumSolution::new(
            self.solution.clone(),
            self.moles.clone(),
            conditions,
            validation,
        )
    }

    /// Builds a typed canonical problem snapshot from the current mutable state.
    ///
    /// The snapshot is used by secondary validation paths so they can work on
    /// the same thermodynamic inputs as the main solve without peeking into
    /// solver-only mutable publication fields.
    fn current_problem_snapshot(&self) -> Result<EquilibriumProblem, ReactionExtentError> {
        let initial_log_moles = match &self.initial_guess {
            Some(seed) => LogMolesInitialGuess::new(seed.clone())?,
            None => LogMolesInitialGuess::from_initial_moles(&self.n0)?,
        };
        let conditions = EquilibriumConditions::new(self.T, self.P, self.p0)?;
        EquilibriumProblem::new(
            self.subs_data.substances.clone(),
            self.n0.clone(),
            initial_log_moles,
            self.elem_composition.clone(),
            self.gibbs.clone(),
            self.phases.clone(),
            conditions,
        )
    }

    /// Runs the optional independent K_eq cross-validation boundary.
    ///
    /// This is intentionally separate from the main backend acceptance gate:
    /// the canonical solver still publishes its own acceptance evidence first,
    /// and this secondary pass only decides whether an additional independent
    /// report should be attached or whether a required validation must fail.
    fn run_keq_cross_validation(
        &self,
        candidate: &EquilibriumSolution,
    ) -> Result<Option<EquilibriumConstantCrossValidationStatus>, ReactionExtentError> {
        let mode = self.solver_settings.keq_validation_mode;
        if mode == EquilibriumConstantValidationMode::Off {
            return Ok(None);
        }

        if self.phases.len() != 1 || !matches!(self.phases[0].kind, PhaseActivityModel::IdealGas) {
            let status = EquilibriumConstantCrossValidationStatus::ValidatorNotApplicable {
                message: "independent validation currently supports exactly one ideal-gas phase"
                    .to_string(),
            };
            return match mode {
                EquilibriumConstantValidationMode::Required => {
                    Err(ReactionExtentError::ValidationNotApplicable {
                        path: "equilibrium_constant_cross_validation",
                        message:
                            "independent validation currently supports exactly one ideal-gas phase"
                                .to_string(),
                    })
                }
                EquilibriumConstantValidationMode::Off
                | EquilibriumConstantValidationMode::WhenApplicable => Ok(Some(status)),
            };
        }

        let prepared = PreparedEquilibriumProblem::new(self.current_problem_snapshot()?)?;
        let keq_problem =
            EquilibriumConstantProblem::from_prepared_ideal_gas(&prepared, Default::default())?;
        let solver = EquilibriumConstantSolver {
            mode: match mode {
                EquilibriumConstantValidationMode::Off => EquilibriumConstantSolverMode::Off,
                EquilibriumConstantValidationMode::WhenApplicable => {
                    EquilibriumConstantSolverMode::WhenApplicable
                }
                EquilibriumConstantValidationMode::Required => {
                    EquilibriumConstantSolverMode::Required
                }
            },
            validation_tolerances: EquilibriumConstantValidationTolerances::default(),
            ..Default::default()
        };
        let validator = solver.solve_if_applicable(&keq_problem)?;
        let status = classify_equilibrium_constant_cross_validation(
            &keq_problem,
            Ok(candidate.clone()),
            Ok(validator),
            self.solver_settings.keq_validation_tolerances,
        )?;

        if mode == EquilibriumConstantValidationMode::Required {
            match &status {
                EquilibriumConstantCrossValidationStatus::Compared(report) if report.accepted => {}
                _ => {
                    return Err(ReactionExtentError::InvalidCandidate {
                        field: "equilibrium_constant_cross_validation",
                        message: status.to_string(),
                    });
                }
            }
        }

        Ok(Some(status))
    }

    /// Creates a solver with default settings and an empty substance database.
    ///
    /// The solver starts with `T = 273.15 K`, `P = 101325 Pa`, `p0 = 101325 Pa`,
    /// no species, no elements, and default solver parameters (LM backend, no scaling).
    /// Call [`set_problem()`](Self::set_problem) or populate fields directly before solving.
    pub fn new() -> Self {
        let ReactionBasis0 = ReactionBasis {
            rank: 0,
            num_reactions: 0,
            reactions: DMatrix::zeros(0, 0),
        };
        Self {
            subs_data: SubsData::new(),
            elem_composition: DMatrix::zeros(0, 0),
            reaction_basis: ReactionBasis0,
            initial_guess: None,
            P: 101325.0,
            T: 273.15,
            stoich_matrix: DMatrix::zeros(0, 0),
            n0: Vec::new(),
            gibbs: Vec::new(),
            gibbs_sym: Vec::new(),
            phases: Vec::new(),
            species_phase: Vec::new(),
            phase_active_mask: Vec::new(),
            solution: Vec::new(),
            last_validation_report: None,
            last_solve_report: None,
            last_keq_validation_status: None,
            last_phase_control_report: None,
            list_of_failed_T: Vec::new(),
            temperature_failures: Vec::new(),
            temperature_solutions: Vec::new(),
            map_of_moles_for_each_substance: HashMap::new(),
            moles_for_T_range: Vec::new(),
            moles: Vec::new(),
            elements_vector: Vec::new(),
            loglevel: None,
            species_eps: DEFAULT_TRACE_MOLE_FLOOR,
            substate_eps: DEFAULT_TRACE_MOLE_FLOOR,
            solver_settings: EquilibriumSolverSettings::default(),
            phase_manager: PhaseManager::default(),
            p0: 101325.0,
        }
    }

    /// Creates a solver with default settings and a completely empty database.
    ///
    /// Identical to [`new()`](Self::new) but uses `SubsData::empty()` instead of
    /// `SubsData::new()`. Both constructors produce the same initial state for
    /// practical purposes. Provided for semantic clarity when the caller explicitly
    /// wants an empty database.
    pub fn empty() -> Self {
        let ReactionBasis0 = ReactionBasis {
            rank: 0,
            num_reactions: 0,
            reactions: DMatrix::zeros(0, 0),
        };
        Self {
            subs_data: SubsData::empty(),
            elem_composition: DMatrix::zeros(0, 0),
            reaction_basis: ReactionBasis0,
            initial_guess: None,
            P: 101325.0,
            T: 273.15,
            stoich_matrix: DMatrix::zeros(0, 0),
            n0: Vec::new(),
            gibbs: Vec::new(),
            gibbs_sym: Vec::new(),
            phases: Vec::new(),
            species_phase: Vec::new(),
            phase_active_mask: Vec::new(),
            solution: Vec::new(),
            last_validation_report: None,
            last_solve_report: None,
            last_keq_validation_status: None,
            last_phase_control_report: None,
            list_of_failed_T: Vec::new(),
            temperature_failures: Vec::new(),
            temperature_solutions: Vec::new(),
            map_of_moles_for_each_substance: HashMap::new(),
            moles_for_T_range: Vec::new(),
            moles: Vec::new(),
            elements_vector: Vec::new(),
            loglevel: None,
            species_eps: DEFAULT_TRACE_MOLE_FLOOR,
            substate_eps: DEFAULT_TRACE_MOLE_FLOOR,
            solver_settings: EquilibriumSolverSettings::default(),
            phase_manager: PhaseManager::default(),
            p0: 101325.0,
        }
    }
    /// Computes the stoichiometric matrix `ν` from the SVD reaction basis.
    ///
    /// The stoichiometric matrix has shape `(species × reactions)` where each column
    /// is one independent reaction direction from the SVD nullspace of the element
    /// composition matrix. This must be called after `elem_composition` is populated
    /// and before any solver backend is invoked.
    pub fn create_stoich_matrix(&mut self) -> Result<(), ReactionExtentError> {
        self.solver_settings.validate()?;
        let reaction_basis = compute_reaction_basis(
            &self.elem_composition,
            self.solver_settings.solver_params.tol,
        )?;
        let stoich_matrix = reaction_basis.reactions.clone();
        let elements_vector = compute_element_totals(&self.elem_composition, &self.n0)?;
        let species_phase = species_to_phase_map(&self.phases, self.n0.len())?;

        self.reaction_basis = reaction_basis;
        self.stoich_matrix = stoich_matrix;
        self.elements_vector = elements_vector.data.as_vec().clone();
        self.species_phase = species_phase;
        Ok(())
    }

    /// Stores the caller's preferred log level for this solver request.
    ///
    /// A numerical solver must not initialize a process-global logger: GUI,
    /// library, and test hosts own that responsibility. The setting is kept as
    /// request metadata while legacy workflow constructors migrate to typed
    /// Sets the log level for diagnostic output and returns the solver for chaining.
    ///
    /// Pass `None` to disable logging, or `Some("info")` / `Some("debug")` for
    /// progressively more verbose output. This is a builder-pattern convenience
    /// wrapper around the mutable `loglevel` field.
    pub fn with_loglevel(&mut self, loglevel: Option<&str>) -> &mut Self {
        self.loglevel = loglevel.map(str::to_string);
        self
    }
    /// Sets a legacy log-mole initial iterate.
    ///
    /// The values are `ln(n_i)`, not physical mole numbers. New code should
    /// prefer `LogMolesInitialGuess` through `EquilibriumProblem`.
    /// Replaces the log-mole initial iterate after validating it immediately.
    ///
    /// This legacy mutable entry point follows the same finite-value and
    /// dimension contract as [`LogMolesInitialGuess`], rather than delaying a
    /// Overrides the default initial guess with an explicit log-mole seed.
    ///
    /// The seed is validated (all values must be finite) before it replaces the
    /// current `initial_guess`. This allows callers to provide a warm-start from
    /// a previous solve or an external estimate, avoiding the default trace-floor
    /// initialization.
    pub fn set_initial_guess(&mut self, guess: Vec<f64>) -> Result<(), ReactionExtentError> {
        let guess = LogMolesInitialGuess::new(guess)?;
        let expected = self.subs_data.substances.len();
        if expected > 0 && guess.as_slice().len() != expected {
            return Err(ReactionExtentError::DimensionMismatch(format!(
                "initial log-mole guess has {} entries, expected {expected}",
                guess.as_slice().len(),
            )));
        }
        self.initial_guess = Some(guess.into_inner());
        Ok(())
    }

    /// Replaces the default backend policy with an explicit [`SolverPolicy`].
    ///
    /// When set, this policy determines the backend order (single or cascade)
    /// instead of the legacy `solver` field. Pass a `Cascade` with specific
    /// backends to control fallback behavior.
    pub fn set_solver_policy(&mut self, policy: SolverPolicy) {
        self.solver_settings.solver_policy = Some(policy);
    }

    /// Clears the explicit solver policy, reverting to the legacy default order.
    ///
    /// After calling this, the backend order is determined by the `solver` field
    /// and the historical fallback chain (preferred → LM → NR → TR).
    pub fn clear_solver_policy(&mut self) {
        self.solver_settings.solver_policy = None;
    }

    /// Sets explicit iteration and attempt limits for the solver cascade.
    ///
    /// The budget controls how many backends may start (`max_attempts`), how many
    /// iterations each backend receives (`max_iterations_per_attempt`), and the
    /// total iteration budget across all backends (`max_total_iterations`).
    pub fn set_solver_budget(&mut self, budget: SolverCascadeBudget) {
        self.solver_settings.solver_budget = Some(budget);
    }

    /// Clears the explicit cascade budget, reverting to the policy-derived default.
    ///
    /// After calling this, the budget is computed from `SolverParams.max_iter`
    /// and the number of backends in the current policy.
    pub fn clear_solver_budget(&mut self) {
        self.solver_settings.solver_budget = None;
    }

    /// Selects whether a temperature sweep continues from accepted points or
    /// Sets the warm-start behavior for sequential temperature sweeps.
    ///
    /// - `PreviousAccepted` (default): each temperature point starts from the
    ///   previous point's solution (faster convergence for smooth grids).
    /// - `IndependentPerPoint`: each point starts from the configured trace seed
    ///   (required for fully parallel sweeps).
    pub fn set_continuation_seed_policy(&mut self, policy: ContinuationSeedPolicy) {
        self.solver_settings.continuation_seed_policy = policy;
    }

    /// Clears every published solve artifact after a new mutable problem state
    /// has been validated and applied.
    pub(crate) fn clear_published_state(&mut self) {
        self.solution.clear();
        self.moles.clear();
        self.last_validation_report = None;
        self.last_solve_report = None;
        self.last_keq_validation_status = None;
        self.last_phase_control_report = None;
        self.list_of_failed_T.clear();
        self.temperature_failures.clear();
        self.temperature_solutions.clear();
        self.moles_for_T_range.clear();
        self.map_of_moles_for_each_substance.clear();
    }

    /// Validates the legacy mutable problem shape before the state is staged.
    pub(crate) fn validate_mutable_problem_shape(&self, n0: &[f64]) -> Result<(), ReactionExtentError> {
        if n0.iter().any(|moles| !moles.is_finite() || *moles < 0.0) {
            return Err(ReactionExtentError::InvalidProblem {
                field: "initial_moles",
                message: "initial moles must be finite and non-negative".to_string(),
            });
        }
        if !self.subs_data.substances.is_empty() && n0.len() != self.subs_data.substances.len() {
            return Err(ReactionExtentError::DimensionMismatch(format!(
                "initial moles have {} entries but SubsData contains {} substances",
                n0.len(),
                self.subs_data.substances.len(),
            )));
        }
        if let Some(initial_guess) = self.initial_guess.as_ref() {
            if initial_guess.len() != n0.len() {
                return Err(ReactionExtentError::DimensionMismatch(format!(
                    "initial log-mole guess has {} entries, but the new problem has {} species",
                    initial_guess.len(),
                    n0.len(),
                )));
            }
        }
        Ok(())
    }

    /// Stages the mutable equilibrium-system inputs in local values first.
    pub(crate) fn stage_equilibrium_system(
        &self,
        n0: &[f64],
    ) -> Result<
        (
            SubsData,
            DMatrix<f64>,
            ReactionBasis,
            DMatrix<f64>,
            Vec<f64>,
            Vec<usize>,
        ),
        ReactionExtentError,
    > {
        self.solver_settings.validate()?;
        let mut staged_subs_data = self.subs_data.clone();
        staged_subs_data
            .search_substances()
            .map_err(ReactionExtentError::SubsDataError)?;
        staged_subs_data
            .parse_all_thermal_coeffs()
            .map_err(ReactionExtentError::SubsDataError)?;
        staged_subs_data
            .calculate_elem_composition_and_molar_mass(None)
            .map_err(ReactionExtentError::SubsDataError)?;
        let element_composition = staged_subs_data
            .element_composition_matrix()
            .cloned()
            .ok_or_else(|| {
                ReactionExtentError::SubsDataError(SubsDataError::MissingData {
                    field: "element composition matrix after calculation".to_string(),
                    substance: "all_substances".to_string(),
                })
            })?;

        if n0.len() != element_composition.nrows() {
            return Err(ReactionExtentError::DimensionMismatch(format!(
                "initial moles have {} entries but the staged element matrix has {} species",
                n0.len(),
                element_composition.nrows(),
            )));
        }

        let reaction_basis =
            compute_reaction_basis(&element_composition, self.solver_settings.solver_params.tol)?;
        let elements_vector = compute_element_totals(&element_composition, n0)?;
        let species_phase = species_to_phase_map(&self.phases, n0.len())?;
        let stoich_matrix = reaction_basis.reactions.clone();

        Ok((
            staged_subs_data,
            element_composition,
            reaction_basis,
            stoich_matrix,
            elements_vector.data.as_vec().clone(),
            species_phase,
        ))
    }

    /// Resolves the active log-mole seed for the current mutable problem.
    ///
    /// If the caller did not set an explicit seed, the helper derives one from
    /// the physical initial moles using the canonical trace floor. That keeps
    /// the fallback in one place and prevents the same seed contract from
    /// being re-encoded in `solve`, sweep setup, and tests.
    pub(crate) fn resolved_initial_guess(&self) -> Result<(Vec<f64>, bool), ReactionExtentError> {
        match &self.initial_guess {
            Some(seed) => {
                let guess = LogMolesInitialGuess::new(seed.clone())?;
                let expected = self.subs_data.substances.len();
                if expected > 0 && guess.as_slice().len() != expected {
                    return Err(ReactionExtentError::DimensionMismatch(format!(
                        "initial log-mole guess has {} entries, expected {expected}",
                        guess.as_slice().len(),
                    )));
                }
                Ok((guess.into_inner(), false))
            }
            None => Ok((
                LogMolesInitialGuess::from_moles_with_policy(
                    &self.n0,
                    self.solver_settings.trace_seed_policy,
                )?
                .into_inner(),
                true,
            )),
        }
    }

    /// Builds the nonlinear residual/Jacobian contract used by the solver
    /// cascade.
    ///
    /// Keeping this in one place ensures the scalar solve and all sweep
    /// variants evaluate the same equations in the same order.
    #[allow(dead_code)]
    pub(crate) fn build_solve_contract(&mut self) -> Result<EquilibriumSolveContract, ReactionExtentError> {
        let subs_eps = self.substate_eps;
        let phase_eps = self.species_eps;
        let stoich = Rc::new(self.stoich_matrix.clone());
        let species_phase = Rc::new(self.species_phase.clone());
        let delta_n = Rc::new(reaction_phase_stoichiometry(
            &self.stoich_matrix,
            &self.phases,
        ));
        let elem_composition = self.elem_composition.clone();
        let elements_vector = self.elements_vector.clone();

        let f_raw: Rc<dyn Fn(&[f64]) -> Result<Vec<f64>, ReactionExtentError>> =
            Rc::from(equilibrium_logmole_residual(
                (*stoich).clone(),
                elem_composition.clone(),
                elements_vector.clone(),
                self.gibbs.clone(),
                self.phases.clone(),
                self.T,
                self.P,
                self.p0,
                (*species_phase).clone(),
                subs_eps,
                phase_eps,
            )?);

        let scale = equilibrium_scaling(
            &stoich,
            &self.elem_composition,
            &self.gibbs,
            &elements_vector,
            self.T,
        )?;
        let f: Box<dyn Fn(&[f64]) -> Result<Vec<f64>, ReactionExtentError>> =
            if self.solver_settings.scaling_flag {
                let f_raw = f_raw.clone();
                let residual_scale = scale.clone();
                Box::new(move |values| scale_residual_rows(f_raw(values)?, &residual_scale))
            } else {
                let f_raw = f_raw.clone();
                Box::new(move |values| f_raw(values))
            };

        let ordered_backends = self
            .solver_settings
            .solver_policy
            .clone()
            .unwrap_or_else(|| {
                if self.has_rst_symbolic_context() {
                    SolverPolicy::rusted_scithe_default()
                } else {
                    SolverPolicy::legacy_default(self.solver_settings.solver)
                }
            })
            .ordered_backends();
        let uses_legacy_backend = ordered_backends
            .iter()
            .any(|backend| matches!(backend, SolverBackend::Legacy(_)));
        let uses_rst_backend = ordered_backends
            .iter()
            .any(|backend| matches!(backend, SolverBackend::RustedSciThe(_)));

        let phases_len = self.phases.len();
        let scaling_flag = self.solver_settings.scaling_flag;
        let j = if uses_legacy_backend {
            let stoich = stoich.clone();
            let species_phase = species_phase.clone();
            let delta_n = delta_n.clone();
            let elem_composition = elem_composition.clone();
            let j_raw = Box::new(move |y: &[f64]| {
                equilibrium_logmole_jacobian(
                    y,
                    &stoich,
                    &elem_composition,
                    &species_phase,
                    delta_n.as_ref(),
                    phases_len,
                    subs_eps,
                    phase_eps,
                )
            })
                as Box<dyn Fn(&[f64]) -> Result<DMatrix<f64>, ReactionExtentError>>;
            Some(if scaling_flag {
                scaled_jacobian(j_raw, scale)
            } else {
                j_raw
            })
        } else {
            None
        };

        let rst_problem = if uses_rst_backend {
            Some(prepare_rst_symbolic_problem(self)?)
        } else {
            None
        };

        Ok(EquilibriumSolveContract {
            f_raw,
            f,
            j,
            rst_problem,
        })
    }

    /// Captures the mutable sweep-relevant state without borrowing the main
    /// solver object in a rayon closure.
    pub(crate) fn temperature_worker_seed(&self) -> TemperatureWorkerSeed {
        TemperatureWorkerSeed {
            subs_data: self.subs_data.clone(),
            elem_composition: self.elem_composition.clone(),
            reaction_basis: self.reaction_basis.clone(),
            stoich_matrix: self.stoich_matrix.clone(),
            n0: self.n0.clone(),
            phases: self.phases.clone(),
            species_phase: self.species_phase.clone(),
            elements_vector: self.elements_vector.clone(),
            loglevel: self.loglevel.clone(),
            species_eps: self.species_eps,
            substate_eps: self.substate_eps,
            solver_settings: self.solver_settings.clone(),
            P: self.P,
            p0: self.p0,
        }
    }

    /// Finds contiguous temperature intervals where the coefficient cache can
    /// be reused without recalculating Gibbs polynomials.
    ///
    /// Both sequential and parallel sweeps use the same interval discovery
    /// logic so a coefficient update is handled once, in one place.
    pub(crate) fn build_temperature_ranges(
        &mut self,
        T_start: f64,
        T_end: f64,
        T_step: f64,
    ) -> Result<Vec<(f64, f64)>, ReactionExtentError> {
        let mut temp_ranges = Vec::new();
        let mut T = T_start;

        while T < T_end {
            let user_subs = &mut self.subs_data;
            let vec_of_coeffs = user_subs
                .extract_coeffs_if_current_coeffs_not_valid_for_all_subs(T)
                .map_err(|_| {
                    ReactionExtentError::SubsDataError(SubsDataError::CoefficientExtractionFailed {
                        substance: "all_subs".to_string(),
                        temperature: Some(T),
                    })
                })?;

            if vec_of_coeffs.len() != 0 || self.gibbs.len() == 0 {
                let range_start = T;
                let mut T_next = T + T_step;
                while T_next < T_end {
                    let changed = self
                        .subs_data
                        .extract_coeffs_if_current_coeffs_not_valid_for_all_subs(T_next)
                        .map_err(|_| {
                            ReactionExtentError::SubsDataError(
                                SubsDataError::CoefficientExtractionFailed {
                                    substance: "all_subs".to_string(),
                                    temperature: Some(T_next),
                                },
                            )
                        })?;
                    if !changed.is_empty() {
                        break;
                    }
                    T_next += T_step;
                }
                temp_ranges.push((range_start, T_next));
                T = T_next;
            } else {
                T += T_step;
            }
        }

        Ok(temp_ranges)
    }

    /// Builds the precomputed Gibbs-function cache for each temperature band.
    ///
    /// This is intentionally separate from interval discovery so the code can
    /// reuse the same range plan for sequential and parallel execution.
    pub(crate) fn build_temperature_gibbs_cache(
        &mut self,
        temp_ranges: &[(f64, f64)],
    ) -> Result<
        std::collections::HashMap<
            usize,
            (
                Vec<std::sync::Arc<dyn Fn(f64) -> f64 + Send + Sync>>,
                f64,
                f64,
            ),
        >,
        ReactionExtentError,
    > {
        let mut gibbs_cache = std::collections::HashMap::new();

        for (range_id, (T_start_range, T_end_range)) in temp_ranges.iter().enumerate() {
            self.subs_data
                .extract_coeffs_if_current_coeffs_not_valid_for_all_subs(*T_start_range)
                .map_err(|_| {
                    ReactionExtentError::SubsDataError(SubsDataError::CoefficientExtractionFailed {
                        substance: "all_subs".to_string(),
                        temperature: Some(*T_start_range),
                    })
                })?;

            let g_vec = self.build_parallel_gibbs_functions()?;
            gibbs_cache.insert(range_id, (g_vec, *T_start_range, *T_end_range));
        }

        Ok(gibbs_cache)
    }

    /// Maps each temperature step to the Gibbs-cache band that covers it.
    pub(crate) fn build_temperature_point_index(
        temp_ranges: &[(f64, f64)],
        T_start: f64,
        T_end: f64,
        T_step: f64,
    ) -> Vec<(f64, usize)> {
        let mut temp_range_pairs = Vec::new();
        let mut T = T_start;

        while T < T_end {
            if let Some((range_id, _)) = temp_ranges
                .iter()
                .enumerate()
                .find(|(_, (t_start, t_end))| T >= *t_start && T < *t_end)
            {
                temp_range_pairs.push((T, range_id));
            }
            T += T_step;
        }

        temp_range_pairs
    }

    /// Solves one temperature point inside a sweep using a captured canonical
    /// seed and one precomputed Gibbs-function set.
    ///
    /// Both parallel sweep variants call this helper so the worker setup,
    /// backend contract, and snapshot publication all stay identical.
    pub(crate) fn solve_temperature_point_from_seed(
        worker_seed: &TemperatureWorkerSeed,
        temperature: f64,
        gibbs_vec: Vec<std::sync::Arc<dyn Fn(f64) -> f64 + Send + Sync>>,
        initial_guess: Option<Vec<f64>>,
        point_index: f64,
    ) -> Result<(TemperatureSolveSnapshot, f64), (f64, ReactionExtentError)> {
        let now = Instant::now();
        let mut local_solver = EquilibriumLogMoles::empty();

        let gibbs_as_rc: Vec<GibbsFn> = gibbs_vec
            .iter()
            .map(|arc_fn| {
                let fn_clone = arc_fn.clone();
                Rc::new(move |T: f64| fn_clone(T)) as GibbsFn
            })
            .collect();

        worker_seed.apply(&mut local_solver, temperature, gibbs_as_rc, initial_guess);

        match local_solver.solve() {
            Ok(()) => local_solver
                .snapshot_published_solution(temperature)
                .map(|snapshot| (snapshot, now.elapsed().as_millis() as f64))
                .map_err(|error| (point_index, error)),
            Err(e) => Err((point_index, e)),
        }
    }

    /// Replaces the mutable problem state after validating it as one unit.
    ///
    /// Previously published solve artifacts are cleared only after validation
    /// succeeds, so a rejected update cannot leave a half-reconfigured solver.
    pub fn set_problem(
        &mut self,
        n0: Vec<f64>,
        phases: Vec<Phase>,
        P: f64,
    ) -> Result<(), ReactionExtentError> {
        if !P.is_finite() || P <= 0.0 {
            return Err(ReactionExtentError::InvalidConditions {
                parameter: "pressure",
                value: P,
            });
        }
        self.validate_mutable_problem_shape(&n0)?;
        let species_phase = species_to_phase_map(&phases, n0.len())?;

        self.n0 = n0;
        self.phases = phases;
        self.P = P;
        self.species_phase = species_phase;
        self.phase_active_mask.clear();
        self.clear_published_state();
        Ok(())
    }
    /// Applies an explicit initial-mole map without accepting misspelled names
    /// or publishing a partially updated vector.
    pub fn set_n0_from_non_zero_map(
        &mut self,
        map_of_nonzero_moles: HashMap<String, f64>,
    ) -> Result<(), ReactionExtentError> {
        let subs = self.subs_data.substances.clone();
        for (substance, moles) in &map_of_nonzero_moles {
            if !subs.iter().any(|known| known == substance) {
                return Err(ReactionExtentError::SubsDataError(
                    SubsDataError::SubstanceNotFound(substance.clone()),
                ));
            }
            if !moles.is_finite() || *moles < 0.0 {
                return Err(ReactionExtentError::InvalidProblem {
                    field: "initial_moles",
                    message: format!(
                        "initial amount for '{substance}' must be finite and non-negative"
                    ),
                });
            }
        }
        let mut n0 = vec![1e-8; subs.len()];
        for (i, subs_i) in subs.iter().enumerate() {
            if let Some(moles_i) = map_of_nonzero_moles.get(subs_i) {
                n0[i] = *moles_i;
            }
        }
        self.n0 = n0;
        self.phase_active_mask.clear();
        self.clear_published_state();
        Ok(())
    }
    /// Creates a fully prepared equilibrium system as one transactional update.
    ///
    /// Substance lookup, coefficient parsing, elemental composition, and the
    /// reaction basis are built in local values first. The solver publishes the
    /// new state only when every stage succeeds, avoiding a mixture of old and
    /// new matrices after an incomplete setup attempt.
    pub fn create_equilibrium_system(&mut self) -> Result<(), ReactionExtentError> {
        let (
            staged_subs_data,
            element_composition,
            reaction_basis,
            stoich_matrix,
            elements_vector,
            species_phase,
        ) = self.stage_equilibrium_system(&self.n0)?;
        self.subs_data = staged_subs_data;
        self.elem_composition = element_composition;
        self.reaction_basis = reaction_basis;
        self.stoich_matrix = stoich_matrix;
        self.elements_vector = elements_vector;
        self.species_phase = species_phase;
        self.phase_active_mask.clear();

        Ok(())
    }
    pub fn create_symbolic_system(&mut self) -> Result<(), ReactionExtentError> {
        // creating variables
        let stoich = self.stoich_matrix.clone();
        let vars = Expr::IndexedVars(stoich.nrows(), "y").0;
        let vars_Str: Vec<String> = vars.iter().map(|y| y.to_string().clone()).collect();
        //let vars_str: Vec<&str> = vars_Str.iter().map(|yi| yi.as_str()).collect();
        let mut jac = Jacobian::new();
        // creating vector of symbolic dG0
        let user_subs = &mut self.subs_data;
        user_subs
            .calculate_therm_map_of_sym()
            .map_err(|e| ReactionExtentError::SubsDataError(e))?;
        let map_of_functions = user_subs.calculate_dG0_sym_one_phase()?;
        let mut g_vec: Vec<Expr> = Vec::new();
        for subs in user_subs.substances.clone() {
            let dg0 = map_of_functions.get(&subs).ok_or_else(|| {
                ReactionExtentError::SubsDataError(SubsDataError::MissingData {
                    field: "standard Gibbs symbolic expression".to_string(),
                    substance: subs.clone(),
                })
            })?;
            g_vec.push(dg0.clone());
        }
        // vector of elements
        // let elements_vector = compute_element_totals(&self.elem_composition, &self.n0);
        // self.elements_vector = elements_vector.data.as_vec().clone();
        let elements_vector = self.elements_vector.clone();
        // creating vector of symbolic equations for equilibrium
        let vec_of_functions = multiphase_equilibrium_residual_generator_sym(
            self.stoich_matrix.clone(),
            self.elem_composition.clone(),
            elements_vector.clone(),
            g_vec,
            self.phases.clone(),
            self.P,
            self.p0,
        )?;
        jac.set_vector_of_functions(vec_of_functions);
        jac.vector_of_variables = vars;
        jac.variable_string = vars_Str;
        jac.parameters_string = vec!["T".to_string()];
        jac.calc_jacobian();

        Ok(())
    }
    pub fn solve_for_T_range(
        &mut self,
        T_start: f64,
        T_end: f64,
        T_step: f64,
    ) -> Result<Vec<(f64, Vec<f64>)>, ReactionExtentError> {
        Self::validate_temperature_range(T_start, T_end, T_step)?;
        let now = Instant::now();
        let mut T = T_start;
        let mut moles_for_T_range = Vec::new();
        let mut failures = Vec::new();
        let mut snapshots = Vec::new();
        let configured_seed = self.initial_guess.clone();

        while T < T_end {
            info!("\n solving equilibrium for T = {} \n", T);
            self.initial_guess = continuation_seed_for_point(
                self.solver_settings.continuation_seed_policy,
                &configured_seed,
                &self.initial_guess,
            );
            // Build Gibbs functions while holding a short-lived mutable borrow to subs_data
            let user_subs = &mut self.subs_data;
            let vec_of_coeffs = user_subs
                .extract_coeffs_if_current_coeffs_not_valid_for_all_subs(T)
                .map_err(|_| {
                    ReactionExtentError::SubsDataError(SubsDataError::CoefficientExtractionFailed {
                        substance: "all_subs".to_string(),
                        temperature: Some(T),
                    })
                })?;
            if vec_of_coeffs.len() != 0 || self.gibbs.len() == 0 {
                info!("\n coefficients outdated ... \n renewing pipeline ...");
                self.gibbs = self.build_gibbs_functions()?;
            }
            self.T = T; // Temperature in K
            match self.solve() {
                Ok(()) => {}
                Err(e) => {
                    error!("Equilibrium calculation failed at T = {} K: {:?}", T, e);
                    failures.push(temperature_failure(T, &e));
                    T += T_step;
                    continue;
                }
            }

            let snapshot = self.snapshot_published_solution(T)?;
            if self.solver_settings.continuation_seed_policy
                == ContinuationSeedPolicy::PreviousAccepted
            {
                self.initial_guess = Some(snapshot.log_moles.clone());
            }
            moles_for_T_range.push((T, snapshot.moles.clone()));
            snapshots.push(snapshot);
            T += T_step;
        }
        let mole_table = build_mole_series_table(&self.subs_data.substances, &moles_for_T_range)?;
        self.moles_for_T_range = moles_for_T_range.clone();
        self.list_of_failed_T = failures.iter().map(|failure| failure.temperature).collect();
        self.temperature_failures = failures;
        self.temperature_solutions = snapshots;
        self.map_of_moles_for_each_substance = mole_table;
        info!(
            "total elapsed time non-parallel solver{} ms",
            now.elapsed().as_millis()
        );
        Ok(Vec::new())
    }

    /// parallel solver
    pub fn solve_for_T_range_par(
        &mut self,
        T_start: f64,
        T_end: f64,
        T_step: f64,
    ) -> Result<Vec<(f64, Vec<f64>)>, ReactionExtentError> {
        Self::validate_temperature_range(T_start, T_end, T_step)?;
        use rayon::prelude::*;
        use std::sync::Arc;
        let now = Instant::now();

        let temp_ranges = self.build_temperature_ranges(T_start, T_end, T_step)?;
        let step2 = now.elapsed().as_millis();
        let gibbs_cache = self.build_temperature_gibbs_cache(&temp_ranges)?;
        let step3 = now.elapsed().as_millis();
        let temp_range_pairs =
            Self::build_temperature_point_index(&temp_ranges, T_start, T_end, T_step);
        let step4 = now.elapsed().as_millis();
        let continuation_seed_policy = self.solver_settings.continuation_seed_policy;
        let configured_seed = self.initial_guess.clone();
        let gibbs_cache = Arc::new(gibbs_cache);
        let worker_seed = self.temperature_worker_seed();
        info!("time for closure creation {:?}", now.elapsed());
        info!("closure stages: {step2}, {step3}, {step4}");
        let now = Instant::now();
        // Chunk the temperature tasks to reduce per-task scheduling overhead.
        // Compute chunk size based on Rayon thread count so work is balanced.
        let num_threads = rayon::current_num_threads();
        let chunk_size = std::cmp::max(1, (temp_range_pairs.len() + num_threads - 1) / num_threads);

        let chunks: Vec<Vec<(f64, usize)>> = temp_range_pairs
            .chunks(chunk_size)
            .map(|c| c.to_vec())
            .collect();

        let mut chunk_results: Vec<
            Vec<Result<(TemperatureSolveSnapshot, f64), (f64, ReactionExtentError)>>,
        > = Vec::new();

        for chunk in chunks {
            let initial_guess = continuation_seed_for_point(
                continuation_seed_policy,
                &configured_seed,
                &self.initial_guess,
            );
            info!("starting temperature chunk with seed {:?}", initial_guess);
            let local_results: Vec<
                Result<(TemperatureSolveSnapshot, f64), (f64, ReactionExtentError)>,
            > = chunk
                .par_iter()
                .map(|(T, range_id)| {
                    // Retrieve the Gibbs functions for this range
                    let gibbs_vec = if let Some((g_vec, _, _)) = gibbs_cache.get(range_id) {
                        g_vec.clone()
                    } else {
                        return Err((
                            *T,
                            ReactionExtentError::InvalidProblem {
                                field: "gibbs_cache",
                                message: format!(
                                    "no precomputed Gibbs functions for range {range_id}"
                                ),
                            },
                        ));
                    };
                    Self::solve_temperature_point_from_seed(
                        &worker_seed,
                        *T,
                        gibbs_vec,
                        initial_guess.clone(),
                        *T,
                    )
                })
                .collect();
            let last_solution = local_results.iter().rev().find_map(|result| {
                result
                    .as_ref()
                    .ok()
                    .map(|(snapshot, _)| snapshot.log_moles.clone())
            });
            let time = local_results
                .iter()
                .map(|res| match res {
                    Ok((_, t)) => *t,
                    Err(_) => 0.0,
                })
                .sum::<f64>();
            info!("time for temperature chunk {:?} ms", time);
            if continuation_seed_policy == ContinuationSeedPolicy::PreviousAccepted {
                if let Some(last_solution) = last_solution {
                    // Use only an accepted local result as the next chunk's seed.
                    self.initial_guess = Some(last_solution);
                } else {
                    warn!(
                        "temperature chunk produced no accepted solution; preserving the previous seed"
                    );
                }
            }
            chunk_results.push(local_results);
        }

        // Flatten chunk results into a single results Vec
        let results: Vec<Result<(TemperatureSolveSnapshot, f64), (f64, ReactionExtentError)>> =
            chunk_results.into_iter().flatten().collect();

        // Step 4: Process results
        let mut moles_for_T_range = Vec::new();
        let mut failed_temps = Vec::new();
        let mut failures = Vec::new();
        let mut snapshots = Vec::new();

        for result in results {
            match result {
                Ok((snapshot, _time)) => {
                    moles_for_T_range.push((snapshot.temperature, snapshot.moles.clone()));
                    snapshots.push(snapshot);
                }
                Err((T, e)) => {
                    error!("Equilibrium calculation failed at T = {} K: {:?}", T, e);
                    failed_temps.push(T);
                    failures.push(temperature_failure(T, &e));
                }
            }
        }

        moles_for_T_range.sort_by(|a, b| a.0.total_cmp(&b.0));
        snapshots.sort_by(|a, b| a.temperature.total_cmp(&b.temperature));

        let mole_table = build_mole_series_table(&self.subs_data.substances, &moles_for_T_range)?;
        self.moles_for_T_range = moles_for_T_range.clone();
        self.list_of_failed_T = failed_temps;
        self.temperature_failures = failures;
        self.temperature_solutions = snapshots;
        self.map_of_moles_for_each_substance = mole_table;
        info!("time for parallel solving {:?}", now.elapsed());
        Ok(moles_for_T_range)
    }

    /// Solves every temperature point independently in parallel.
    ///
    /// This mode deliberately does not apply [`ContinuationSeedPolicy`]: a
    /// previous accepted point is unavailable while tasks run concurrently.
    pub fn solve_for_T_range_par2(
        &mut self,
        T_start: f64,
        T_end: f64,
        T_step: f64,
    ) -> Result<Vec<(f64, Vec<f64>)>, ReactionExtentError> {
        Self::validate_temperature_range(T_start, T_end, T_step)?;
        use rayon::prelude::*;
        use std::sync::Arc;
        let now = Instant::now();
        let temp_ranges = self.build_temperature_ranges(T_start, T_end, T_step)?;
        let step2 = now.elapsed().as_millis();
        let gibbs_cache = self.build_temperature_gibbs_cache(&temp_ranges)?;
        let step3 = now.elapsed().as_millis();
        let temp_range_pairs =
            Self::build_temperature_point_index(&temp_ranges, T_start, T_end, T_step);
        let step4 = now.elapsed().as_millis();
        let gibbs_cache = Arc::new(gibbs_cache);
        let worker_seed = self.temperature_worker_seed();
        info!("time for closure creation {:?}", now.elapsed());
        info!("closure stages: {step2}, {step3}, {step4}");
        let results: Vec<Result<TemperatureSolveSnapshot, (f64, ReactionExtentError)>> =
            temp_range_pairs
                .par_iter()
                .map(|(T, range_id)| {
                    let gibbs_vec = if let Some((g_vec, _, _)) = gibbs_cache.get(range_id) {
                        g_vec.clone()
                    } else {
                        return Err((
                            *T,
                            ReactionExtentError::InvalidProblem {
                                field: "gibbs_cache",
                                message: format!(
                                    "no precomputed Gibbs functions for range {range_id}"
                                ),
                            },
                        ));
                    };

                    Self::solve_temperature_point_from_seed(&worker_seed, *T, gibbs_vec, None, *T)
                        .map(|(snapshot, _elapsed_ms)| snapshot)
                })
                .collect();

        // Step 4: Process results
        let mut moles_for_T_range = Vec::new();
        let mut failed_temps = Vec::new();
        let mut failures = Vec::new();
        let mut snapshots = Vec::new();

        for result in results {
            match result {
                Ok(snapshot) => {
                    moles_for_T_range.push((snapshot.temperature, snapshot.moles.clone()));
                    snapshots.push(snapshot);
                }
                Err((T, e)) => {
                    error!("Equilibrium calculation failed at T = {} K: {:?}", T, e);
                    failed_temps.push(T);
                    failures.push(temperature_failure(T, &e));
                }
            }
        }

        moles_for_T_range.sort_by(|a, b| a.0.total_cmp(&b.0));
        snapshots.sort_by(|a, b| a.temperature.total_cmp(&b.temperature));

        let mole_table = build_mole_series_table(&self.subs_data.substances, &moles_for_T_range)?;
        self.moles_for_T_range = moles_for_T_range.clone();
        self.list_of_failed_T = failed_temps;
        self.temperature_failures = failures;
        self.temperature_solutions = snapshots;
        self.map_of_moles_for_each_substance = mole_table;
        info!("time for parallel solving {:?}", now.elapsed());
        Ok(moles_for_T_range)
    }

    /// Publishes a column-oriented sweep table only after every result row has
    /// been checked against the canonical substance ordering.
    pub fn map_of_moles_for_each_substance(&mut self) -> Result<(), ReactionExtentError> {
        let published =
            build_mole_series_table(&self.subs_data.substances, &self.moles_for_T_range)?;
        self.map_of_moles_for_each_substance = published;
        Ok(())
    }

    /// Builds a typed postprocessing view from the latest solved temperature sweep.
    ///
    /// The solver keeps the solved points as the source of truth. This helper
    /// only packages them for plotting, export, or GUI resampling.
    pub fn temperature_postprocessing(
        &self,
        policy: &TemperaturePostprocessingPolicy,
    ) -> Result<TemperaturePostprocessingResult, ReactionExtentError> {
        postprocess_temperature_series(
            self.subs_data.substances.clone(),
            &self.moles_for_T_range,
            policy,
        )
    }

    /// Builds a display table for the currently published sweep results.
    ///
    /// The equilibrium engine deliberately does not print it: callers choose
    /// whether to render the table in a terminal, GUI, file, or test report.
    pub fn moles_table(&self) -> Table {
        let mut table = Table::new();

        // Get the keys (headers) and sort them for consistent order
        let mut keys: Vec<&String> = self.map_of_moles_for_each_substance.keys().collect();
        keys.sort();

        // Create header row
        let header_row: Vec<Cell> = keys.iter().map(|k| Cell::new(k)).collect();
        table.add_row(Row::new(header_row));

        // Determine the number of rows (length of the vectors)
        let num_rows = if let Some(first_vec) = self.map_of_moles_for_each_substance.values().next()
        {
            first_vec.len()
        } else {
            0
        };

        // Add data rows
        for row_idx in 0..num_rows {
            let mut row: Vec<Cell> = Vec::new();
            for key in &keys {
                if let Some(vec) = self.map_of_moles_for_each_substance.get(*key) {
                    if row_idx < vec.len() {
                        row.push(Cell::new(&format!("{:.6}", vec[row_idx])));
                    } else {
                        row.push(Cell::new(""));
                    }
                } else {
                    row.push(Cell::new(""));
                }
            }
            table.add_row(Row::new(row));
        }

        table
    }

    /// Solve the equilibrium and return an error enum on failure
    pub fn solve(&mut self) -> Result<(), ReactionExtentError> {
        self.check_task()?;

        let (initial_guess, generated_initial_guess) = self.resolved_initial_guess()?;
        let generated_seed_to_publish = generated_initial_guess.then(|| initial_guess.clone());
        let candidate = self.solve_candidate_from_seed(initial_guess)?;
        if let Some(generated_seed) = generated_seed_to_publish {
            self.initial_guess = Some(generated_seed);
        }
        self.last_phase_control_report = None;
        self.publish_solve_candidate(candidate);
        Ok(())
    }

    /// Solves and validates one fixed formulation without publishing it.
    ///
    /// The supplied seed is explicit so an outer active-set algorithm cannot
    /// accidentally fall back to the seed from an earlier phase assemblage.
    pub(crate) fn solve_candidate_from_seed(
        &mut self,
        initial_guess: Vec<f64>,
    ) -> Result<EquilibriumSolveCandidate, ReactionExtentError> {
        self.check_task()?;
        if initial_guess.len() != self.n0.len()
            || initial_guess.iter().any(|value| !value.is_finite())
        {
            return Err(ReactionExtentError::InvalidProblem {
                field: "initial_log_moles",
                message: format!(
                    "candidate seed must contain {} finite values, received {}",
                    self.n0.len(),
                    initial_guess.len()
                ),
            });
        }
        let policy = self
            .solver_settings
            .solver_policy
            .clone()
            .unwrap_or_else(|| {
                if self.has_rst_symbolic_context() {
                    SolverPolicy::rusted_scithe_default()
                } else {
                    SolverPolicy::legacy_default(self.solver_settings.solver)
                }
            });
        if policy.ordered_backends().is_empty() {
            return Err(ReactionExtentError::InvalidProblem {
                field: "solver_policy",
                message: "a solver policy must contain at least one backend".to_string(),
            });
        }

        let contract = self.build_solve_contract()?;
        info!("Initial guess: {:?}", &initial_guess);
        info!("n0 (species initial moles): {:?}", &self.n0);
        info!("Stoichiometric matrix:\n{}", &self.stoich_matrix);

        let try_step = contract.raw_residual(&initial_guess)?;
        if try_step.iter().any(|v| v.is_nan() || v.is_infinite()) {
            error!("Initial guess produces invalid residuals: {:?}", &try_step);
            return Err(ReactionExtentError::InvalidInitialResiduals(try_step));
        }

        let feasible = {
            move |xi: &[f64]| {
                compute_species_moles(xi)
                    .map(|moles| moles.iter().all(|&moles_i| moles_i >= -1e-12))
                    .unwrap_or(false)
            }
        };

        let validate_candidate = |candidate: &[f64]| {
            let raw_residual = contract.raw_residual(candidate)?;
            let acceptance_residual = contract.residual(candidate)?;
            validate_equilibrium_candidate(
                EquilibriumCandidateResiduals {
                    log_moles: candidate,
                    raw_residual: &raw_residual,
                    acceptance_residual: &acceptance_residual,
                },
                EquilibriumAcceptanceCriteria::new(
                    self.solver_settings.solver_params.tol,
                    10.0 * self.solver_settings.solver_params.tol.max(1e-8),
                    self.solver_settings.solver_params.tol,
                )?,
                &self.elem_composition,
                &self.elements_vector,
            )
        };

        let (sol, validation_report, solve_report) = self.solver_impl(
            initial_guess,
            contract.f.as_ref(),
            contract.jacobian(),
            &feasible,
            &validate_candidate,
            policy,
            contract.rst_problem.as_ref(),
        )?;

        let (moles, mole_table) = self.reconstructed_mole_state(&sol)?;
        let conditions = EquilibriumConditions::new(self.T, self.P, self.p0)?;
        let candidate_solution = EquilibriumSolution::new(
            sol.clone(),
            moles.clone(),
            conditions,
            validation_report.clone(),
        )?;
        let keq_validation_status = self.run_keq_cross_validation(&candidate_solution)?;
        Ok(EquilibriumSolveCandidate {
            log_moles: sol,
            moles,
            mole_table,
            validation_report,
            solve_report,
            keq_validation_status,
        })
    }

    /// Atomically publishes one candidate that has already passed every
    /// backend-independent validation gate.
    pub(crate) fn publish_solve_candidate(&mut self, candidate: EquilibriumSolveCandidate) {
        self.last_keq_validation_status = candidate.keq_validation_status;
        self.moles = candidate.moles;
        self.map_of_moles_for_each_substance = candidate.mole_table;
        self.solution = candidate.log_moles;
        self.last_validation_report = Some(candidate.validation_report);
        self.last_solve_report = Some(candidate.solve_report);
    }
    /// Runs the explicit backend policy and records every numerical outcome.
    pub(crate) fn solver_impl(
        &self,
        initial_guess: Vec<f64>,
        f: &dyn Fn(&[f64]) -> Result<Vec<f64>, ReactionExtentError>,
        j: Option<&dyn Fn(&[f64]) -> Result<DMatrix<f64>, ReactionExtentError>>,
        feasible: &dyn Fn(&[f64]) -> bool,
        validate_candidate: &dyn Fn(
            &[f64],
        )
            -> Result<EquilibriumCandidateReport, ReactionExtentError>,
        policy: SolverPolicy,
        rst_problem: Option<&RstPreparedProblem>,
    ) -> Result<(Vec<f64>, EquilibriumCandidateReport, EquilibriumSolveReport), ReactionExtentError>
    {
        let backends = policy.ordered_backends();
        if backends.is_empty() {
            return Err(ReactionExtentError::InvalidProblem {
                field: "solver_policy",
                message: "a solver policy must contain at least one backend".to_string(),
            });
        }

        let budget = self.solver_settings.solver_budget.unwrap_or_else(|| {
            SolverCascadeBudget::new(
                backends.len(),
                self.solver_settings.solver_params.max_iter,
                self.solver_settings
                    .solver_params
                    .max_iter
                    .saturating_mul(backends.len()),
            )
        });
        let backend_refs: Vec<&dyn EquilibriumNonlinearBackend> = backends
            .iter()
            .map(|backend| backend as &dyn EquilibriumNonlinearBackend)
            .collect();

        Self::solve_backend_cascade(
            &backend_refs,
            initial_guess,
            f,
            j,
            feasible,
            validate_candidate,
            policy,
            budget,
            &self.solver_settings.solver_params,
            &self.n0,
            &self.stoich_matrix,
            rst_problem,
        )
    }

    /// Runs the explicit backend policy and records every numerical outcome.
    pub(crate) fn solve_backend_cascade(
        backends: &[&dyn EquilibriumNonlinearBackend],
        initial_guess: Vec<f64>,
        f: &dyn Fn(&[f64]) -> Result<Vec<f64>, ReactionExtentError>,
        j: Option<&dyn Fn(&[f64]) -> Result<DMatrix<f64>, ReactionExtentError>>,
        feasible: &dyn Fn(&[f64]) -> bool,
        validate_candidate: &dyn Fn(
            &[f64],
        )
            -> Result<EquilibriumCandidateReport, ReactionExtentError>,
        policy: SolverPolicy,
        budget: SolverCascadeBudget,
        solver_params: &SolverParams,
        initial_moles: &[f64],
        reactions: &DMatrix<f64>,
        rst_problem: Option<&RstPreparedProblem>,
    ) -> Result<(Vec<f64>, EquilibriumCandidateReport, EquilibriumSolveReport), ReactionExtentError>
    {
        if backends.is_empty() {
            return Err(ReactionExtentError::InvalidProblem {
                field: "solver_policy",
                message: "a solver policy must contain at least one backend".to_string(),
            });
        }

        if budget.max_attempts == 0
            || budget.max_iterations_per_attempt == 0
            || budget.max_total_iterations == 0
        {
            return Err(ReactionExtentError::InvalidProblem {
                field: "solver_budget",
                message: "attempt, per-attempt, and total iteration limits must be positive"
                    .to_string(),
            });
        }

        let mut attempts = Vec::with_capacity(backends.len());
        let mut started_attempts = 0usize;
        let mut remaining_iterations = budget.max_total_iterations;
        for &backend in backends {
            let backend_id = backend.backend();
            if started_attempts >= budget.max_attempts || remaining_iterations == 0 {
                let reason = if started_attempts >= budget.max_attempts {
                    format!(
                        "cascade attempt budget exhausted after {} started backend(s)",
                        budget.max_attempts
                    )
                } else {
                    "cascade total iteration budget exhausted".to_string()
                };
                attempts.push(SolverAttemptReport {
                    backend: backend_id,
                    outcome: SolverAttemptOutcome::Skipped { reason },
                    metrics: None,
                });
                continue;
            }
            let max_iterations = budget.max_iterations_per_attempt.min(remaining_iterations);
            started_attempts += 1;
            remaining_iterations -= max_iterations;

            // Every backend receives this same validated seed. A previous
            // rejected candidate is evidence for diagnostics, never an
            // implicit warm start.
            let result = backend.solve(BackendSolveRequest {
                initial_guess: initial_guess.clone(),
                residual: f,
                jacobian: j,
                feasible,
                params: solver_params,
                initial_moles,
                reactions,
                max_iterations,
                rst_problem,
            });

            match result {
                Ok(candidate) => match validate_candidate(&candidate.solution) {
                    Ok(validation) => {
                        attempts.push(SolverAttemptReport {
                            backend: backend_id,
                            outcome: SolverAttemptOutcome::Accepted,
                            metrics: candidate.metrics,
                        });
                        let report = EquilibriumSolveReport {
                            policy,
                            attempts,
                            accepted_backend: backend_id,
                        };
                        if report.accepted_after_fallback() {
                            info!("Solver accepted after fallback: {}", report);
                        }
                        return Ok((candidate.solution, validation, report));
                    }
                    Err(error) => attempts.push(SolverAttemptReport {
                        backend: backend_id,
                        outcome: SolverAttemptOutcome::RejectedCandidate {
                            reason: error.to_string(),
                        },
                        metrics: candidate.metrics,
                    }),
                },
                Err(e) => {
                    let Some(kind) = recoverable_backend_failure_kind(&e) else {
                        return if attempts.is_empty() {
                            Err(e)
                        } else {
                            Err(ReactionExtentError::CascadeAborted {
                                attempts,
                                cause: Box::new(e),
                            })
                        };
                    };
                    attempts.push(SolverAttemptReport {
                        backend: backend_id,
                        outcome: SolverAttemptOutcome::Failed {
                            kind,
                            // Attempt reports are part of the public solve
                            // contract, so keep their wording independent of
                            // a private Debug implementation.
                            reason: e.to_string(),
                        },
                        metrics: None,
                    });
                }
            }
        }

        Err(ReactionExtentError::AllBackendsFailed { attempts })
    }

    /// Converts log-mole solution to actual mole numbers
    ///
    /// Transforms the solver output (ln(n_i)) back to mole numbers (n_i = exp(ln(n_i))).
    pub fn compute_species_moles(&mut self, sol: Vec<f64>) -> Result<(), ReactionExtentError> {
        let (moles, map_of_moles_for_each_substance) = self.reconstructed_mole_state(&sol)?;
        self.moles = moles;
        self.map_of_moles_for_each_substance = map_of_moles_for_each_substance;
        Ok(())
    }

    /// Reconstructs the complete public mole view without mutating solver
    /// state. Both regular solve publication and the compatibility mutator use
    /// this one validation boundary.
    pub(crate) fn reconstructed_mole_state(
        &self,
        sol: &[f64],
    ) -> Result<(Vec<f64>, HashMap<String, Vec<f64>>), ReactionExtentError> {
        let moles = compute_species_moles(&sol)?;
        if moles.len() != self.subs_data.substances.len() {
            return Err(ReactionExtentError::DimensionMismatch(format!(
                "solution has {} mole values for {} substances",
                moles.len(),
                self.subs_data.substances.len(),
            )));
        }
        let map_of_moles_for_each_substance: HashMap<String, Vec<f64>> = self
            .subs_data
            .substances
            .clone()
            .iter()
            .zip(moles.clone())
            .map(|(subs, n)| (subs.clone(), vec![n]))
            .collect();
        if map_of_moles_for_each_substance.len() != moles.len() {
            return Err(ReactionExtentError::InvalidProblem {
                field: "species",
                message: "species names must be unique when publishing mole results".to_string(),
            });
        }
        Ok((moles, map_of_moles_for_each_substance))
    }

    /// Validates problem setup for dimensional consistency
    ///
    /// Ensures that initial moles, substances, and initial guess vectors
    /// all have consistent dimensions.
    pub(crate) fn check_task(&self) -> Result<(), ReactionExtentError> {
        self.solver_settings.validate()?;
        let n_subs = self.subs_data.substances.len();
        let mut seen_species = HashSet::with_capacity(n_subs);
        if self
            .subs_data
            .substances
            .iter()
            .any(|species| !seen_species.insert(species))
        {
            return Err(ReactionExtentError::InvalidProblem {
                field: "species",
                message: "species names must be unique".to_string(),
            });
        }
        if self.n0.len() != n_subs {
            return Err(ReactionExtentError::DimensionMismatch(format!(
                "initial moles number of substances {} doesn't
            match number of substances {}",
                self.n0.len(),
                n_subs
            )));
        }
        if self.gibbs.len() != n_subs {
            return Err(ReactionExtentError::DimensionMismatch(format!(
                "Gibbs vector has {} entries but there are {n_subs} substances",
                self.gibbs.len(),
            )));
        }
        if self
            .n0
            .iter()
            .any(|moles| !moles.is_finite() || *moles < 0.0)
        {
            return Err(ReactionExtentError::InvalidProblem {
                field: "initial_moles",
                message: "initial moles must be finite and non-negative".to_string(),
            });
        }
        if let Some(initial_guess) = self.initial_guess.as_ref() {
            if initial_guess.len() != n_subs {
                return Err(ReactionExtentError::DimensionMismatch(format!(
                    "initial guess for solution {} doesn't match number of substances {n_subs}",
                    initial_guess.len(),
                )));
            }
            if initial_guess.iter().any(|value| !value.is_finite()) {
                return Err(ReactionExtentError::InvalidProblem {
                    field: "initial_log_moles",
                    message: "initial log-mole values must be finite".to_string(),
                });
            }
        }
        Ok(())
    }
}

/// A fallback policy can recover from numerical failures, but it must not
/// hide malformed input or invalid solver configuration behind later
/// backends. Candidate validation is handled separately and is retryable by
/// design because a later method can produce a better iterate.
#[cfg(test)]
fn is_recoverable_backend_failure(error: &ReactionExtentError) -> bool {
    recoverable_backend_failure_kind(error).is_some()
}

/// Classifies only failures for which another numerical backend may still
/// produce an accepted candidate. Invalid domain input deliberately maps to
/// `None` so a fallback can never hide a malformed equilibrium problem.
pub(crate) fn recoverable_backend_failure_kind(
    error: &ReactionExtentError,
) -> Option<SolverAttemptFailureKind> {
    if !error.is_retryable_backend_failure() {
        return None;
    }
    match error {
        ReactionExtentError::SolveError(_) => Some(SolverAttemptFailureKind::Solver),
        ReactionExtentError::BackendFailure { .. } => Some(SolverAttemptFailureKind::Backend),
        ReactionExtentError::ResidualEvaluation(_) => {
            Some(SolverAttemptFailureKind::ResidualEvaluation)
        }
        ReactionExtentError::JacobianEvaluation(_) => {
            Some(SolverAttemptFailureKind::JacobianEvaluation)
        }
        _ => None,
    }
}

#[cfg(test)]
mod retry_policy_tests {
    use super::{is_recoverable_backend_failure, recoverable_backend_failure_kind};
    use crate::Thermodynamics::ChemEquilibrium::equilibrium_nonlinear::{
        BackendFailureKind, ReactionExtentError, SolveError,
    };
    use crate::Thermodynamics::ChemEquilibrium::equilibrium_solver_policy::SolverAttemptFailureKind;

    #[test]
    fn retries_only_numerical_backend_failures() {
        assert!(is_recoverable_backend_failure(
            &ReactionExtentError::SolveError(SolveError::MaxIterations,)
        ));
        assert!(is_recoverable_backend_failure(
            &ReactionExtentError::JacobianEvaluation("singular numerical step".to_string()),
        ));
        assert!(is_recoverable_backend_failure(
            &ReactionExtentError::BackendFailure {
                backend: "rst_levenberg_marquardt".to_string(),
                kind: BackendFailureKind::SingularJacobian,
                message: "ill-conditioned system".to_string(),
            },
        ));
        assert!(!is_recoverable_backend_failure(
            &ReactionExtentError::InvalidProblem {
                field: "rst_solve_options",
                message: "max_iterations must be greater than zero".to_string(),
            },
        ));
        assert!(!is_recoverable_backend_failure(
            &ReactionExtentError::DimensionMismatch("species count".to_string()),
        ));
        assert!(
            ReactionExtentError::InvalidProblem {
                field: "solver_options",
                message: "invalid settings".to_string(),
            }
            .is_non_retryable_input_error()
        );
        assert_eq!(
            recoverable_backend_failure_kind(&ReactionExtentError::ResidualEvaluation(
                "temporary residual failure".to_string(),
            )),
            Some(SolverAttemptFailureKind::ResidualEvaluation),
        );
    }
}

#[cfg(test)]
mod solver_budget_tests {
    use super::{EquilibriumLogMoles, temperature_failure};
    use crate::Thermodynamics::ChemEquilibrium::equilibrium_log_moles::Solvers;
    use crate::Thermodynamics::ChemEquilibrium::equilibrium_nonlinear::ReactionExtentError;
    use crate::Thermodynamics::ChemEquilibrium::equilibrium_rst_backend::RustedSciTheSolver;
    use crate::Thermodynamics::ChemEquilibrium::equilibrium_solver_policy::{
        SolverAttemptFailureKind, SolverAttemptOutcome, SolverAttemptReport, SolverBackend,
        SolverCascadeBudget, SolverPolicy,
    };
    use nalgebra::DMatrix;
    use std::cell::RefCell;

    #[test]
    fn global_iteration_budget_skips_later_backends_in_declared_order() {
        let mut solver = EquilibriumLogMoles::empty();
        solver.set_solver_budget(SolverCascadeBudget::new(2, 3, 1));
        let forced_failure = |_values: &[f64]| {
            Err(ReactionExtentError::ResidualEvaluation(
                "forced numerical failure".to_string(),
            ))
        };
        let jacobian = |_values: &[f64]| Ok(DMatrix::identity(1, 1));
        let feasible = |_values: &[f64]| true;
        let accept = |_values: &[f64]| {
            Err(ReactionExtentError::InvalidCandidate {
                field: "test_acceptance_gate",
                message: "candidate should not be reached".to_string(),
            })
        };

        let error = solver
            .solver_impl(
                vec![0.0],
                &forced_failure,
                Some(&jacobian),
                &feasible,
                &accept,
                SolverPolicy::Cascade(vec![
                    SolverBackend::Legacy(Solvers::LM),
                    SolverBackend::Legacy(Solvers::NR),
                ]),
                None,
            )
            .unwrap_err();
        let attempts = match error {
            ReactionExtentError::AllBackendsFailed { attempts } => attempts,
            other => panic!("expected a cascade report, got {other:?}"),
        };

        assert_eq!(attempts.len(), 2);
        let SolverAttemptOutcome::Failed { kind, reason } = &attempts[0].outcome else {
            panic!(
                "expected the first backend to fail, got {:?}",
                attempts[0].outcome
            );
        };
        assert_eq!(*kind, SolverAttemptFailureKind::Solver);
        assert_eq!(
            reason,
            "equilibrium solver failed: nonlinear solver evaluation failed: residual evaluation failed: equilibrium residual evaluation failed: forced numerical failure"
        );
        assert!(matches!(
            attempts[1].outcome,
            SolverAttemptOutcome::Skipped { .. }
        ));
    }

    #[test]
    fn every_started_backend_receives_the_original_validated_seed() {
        let mut solver = EquilibriumLogMoles::empty();
        solver.set_solver_budget(SolverCascadeBudget::new(2, 1, 2));
        let observed_seeds = RefCell::new(Vec::new());
        let forced_failure = |values: &[f64]| {
            observed_seeds.borrow_mut().push(values.to_vec());
            Err(ReactionExtentError::ResidualEvaluation(
                "forced numerical failure".to_string(),
            ))
        };
        let jacobian = |_values: &[f64]| Ok(DMatrix::identity(1, 1));
        let feasible = |_values: &[f64]| true;
        let accept = |_values: &[f64]| {
            Err(ReactionExtentError::InvalidCandidate {
                field: "test_acceptance_gate",
                message: "candidate should not be reached".to_string(),
            })
        };

        let _ = solver.solver_impl(
            vec![-3.0],
            &forced_failure,
            Some(&jacobian),
            &feasible,
            &accept,
            SolverPolicy::Cascade(vec![
                SolverBackend::Legacy(Solvers::LM),
                SolverBackend::Legacy(Solvers::NR),
            ]),
            None,
        );

        assert_eq!(observed_seeds.into_inner(), vec![vec![-3.0], vec![-3.0]]);
    }

    #[test]
    fn non_retryable_cascade_abort_retains_earlier_attempts_and_root_cause() {
        let solver = EquilibriumLogMoles::empty();
        let recoverable_failure = |_values: &[f64]| {
            Err(ReactionExtentError::ResidualEvaluation(
                "forced numerical failure".to_string(),
            ))
        };
        let jacobian = |_values: &[f64]| Ok(DMatrix::identity(1, 1));
        let feasible = |_values: &[f64]| true;
        let accept = |_values: &[f64]| {
            Err(ReactionExtentError::InvalidCandidate {
                field: "test_acceptance_gate",
                message: "candidate should not be reached".to_string(),
            })
        };

        let error = solver
            .solver_impl(
                vec![0.0],
                &recoverable_failure,
                Some(&jacobian),
                &feasible,
                &accept,
                SolverPolicy::Cascade(vec![
                    SolverBackend::Legacy(Solvers::LM),
                    SolverBackend::RustedSciThe(RustedSciTheSolver::LevenbergMarquardt),
                ]),
                None,
            )
            .unwrap_err();

        let ReactionExtentError::CascadeAborted { attempts, cause } = error else {
            panic!("expected the cascade to retain its abort trace");
        };
        assert_eq!(attempts.len(), 1);
        assert!(matches!(
            attempts[0].outcome,
            SolverAttemptOutcome::Failed { .. }
        ));
        assert!(matches!(
            *cause,
            ReactionExtentError::InvalidProblem {
                field: "rst_symbolic_problem",
                ..
            }
        ));
    }

    #[test]
    fn temperature_failure_retains_the_cascade_trace() {
        let attempts = vec![SolverAttemptReport {
            backend: SolverBackend::Legacy(Solvers::LM),
            outcome: SolverAttemptOutcome::Skipped {
                reason: "iteration budget exhausted".to_string(),
            },
            metrics: None,
        }];
        let failure = temperature_failure(
            1200.0,
            &ReactionExtentError::AllBackendsFailed {
                attempts: attempts.clone(),
            },
        );

        assert_eq!(failure.temperature, 1200.0);
        assert_eq!(failure.attempts, attempts);
        assert_eq!(
            failure.message,
            "all equilibrium solver backends failed after 1 attempt(s)"
        );

        let aborted = temperature_failure(
            1300.0,
            &ReactionExtentError::CascadeAborted {
                attempts: attempts.clone(),
                cause: Box::new(ReactionExtentError::InvalidProblem {
                    field: "rst_symbolic_problem",
                    message: "symbolic preparation failed".to_string(),
                }),
            },
        );
        assert_eq!(aborted.attempts, attempts);
        assert!(aborted.message.contains("cascade aborted"));
    }
}

#[cfg(test)]
mod solver_cascade_story_tests {
    use super::{EquilibriumLogMoles, SolverParams};
    use crate::Thermodynamics::ChemEquilibrium::equilibrium_backend_adapter::{
        BackendSolveRequest, BackendSolveResult, EquilibriumNonlinearBackend,
    };
    use crate::Thermodynamics::ChemEquilibrium::equilibrium_nonlinear::ReactionExtentError;
    use crate::Thermodynamics::ChemEquilibrium::equilibrium_rst_backend::RustedSciTheSolver;
    use crate::Thermodynamics::ChemEquilibrium::equilibrium_solver_policy::{
        EquilibriumSolveReport, SolverAttemptFailureKind, SolverAttemptOutcome, SolverBackend,
        SolverCascadeBudget, SolverPolicy,
    };
    use crate::Thermodynamics::ChemEquilibrium::equilibrium_validation::EquilibriumCandidateReport;
    use nalgebra::DMatrix;

    #[derive(Clone)]
    enum FakeBackendBehavior {
        RecoverableFailure,
        Success { solution: Vec<f64> },
    }

    struct FakeBackend {
        backend: SolverBackend,
        behavior: FakeBackendBehavior,
    }

    impl EquilibriumNonlinearBackend for FakeBackend {
        fn backend(&self) -> SolverBackend {
            self.backend
        }

        fn solve(
            &self,
            request: BackendSolveRequest<'_>,
        ) -> Result<BackendSolveResult, ReactionExtentError> {
            match &self.behavior {
                FakeBackendBehavior::RecoverableFailure => {
                    Err(ReactionExtentError::ResidualEvaluation(
                        "forced recoverable failure".to_string(),
                    ))
                }
                FakeBackendBehavior::Success { solution } => {
                    let _ = request;
                    Ok(BackendSolveResult {
                        solution: solution.clone(),
                        metrics: Some(
                            crate::Thermodynamics::ChemEquilibrium::equilibrium_solver_policy::SolverAttemptMetrics {
                                termination: crate::Thermodynamics::ChemEquilibrium::equilibrium_solver_policy::SolverTermination::Converged,
                                backend_converged: true,
                                iterations: 1,
                                residual_evaluations: 1,
                                jacobian_evaluations: 1,
                                linear_solves: 1,
                                elapsed_millis: 0,
                            },
                        ),
                    })
                }
            }
        }
    }

    fn accepted_candidate_report() -> EquilibriumCandidateReport {
        EquilibriumCandidateReport {
            residual_l2_norm: 0.0,
            residual_rms: 0.0,
            max_abs_residual: 0.0,
            raw_residual_l2_norm: 0.0,
            raw_residual_rms: 0.0,
            raw_max_abs_residual: 0.0,
            max_abs_element_balance_error: 0.0,
            reaction_affinity_l2_norm: 0.0,
            max_abs_reaction_affinity: 0.0,
            min_moles: 1.0,
        }
    }

    fn solve_with_fake_backends(
        backends: Vec<&dyn EquilibriumNonlinearBackend>,
        validate_candidate: impl Fn(&[f64]) -> Result<EquilibriumCandidateReport, ReactionExtentError>,
        budget: SolverCascadeBudget,
    ) -> Result<(Vec<f64>, EquilibriumCandidateReport, EquilibriumSolveReport), ReactionExtentError>
    {
        let residual = |values: &[f64]| Ok(values.to_vec());
        let jacobian = |_values: &[f64]| Ok(DMatrix::identity(1, 1));
        let feasible = |_values: &[f64]| true;
        let policy =
            SolverPolicy::Cascade(backends.iter().map(|backend| backend.backend()).collect());

        EquilibriumLogMoles::solve_backend_cascade(
            &backends,
            vec![0.0],
            &residual,
            Some(&jacobian),
            &feasible,
            &validate_candidate,
            policy,
            budget,
            &SolverParams::default(),
            &[1.0],
            &DMatrix::from_row_slice(1, 1, &[1.0]),
            None,
        )
    }

    #[test]
    fn first_backend_fails_recoverably_second_succeeds_and_is_accepted() {
        let fail = FakeBackend {
            backend: SolverBackend::Legacy(super::Solvers::LM),
            behavior: FakeBackendBehavior::RecoverableFailure,
        };
        let success = FakeBackend {
            backend: SolverBackend::Legacy(super::Solvers::NR),
            behavior: FakeBackendBehavior::Success {
                solution: vec![2.0],
            },
        };

        let result = solve_with_fake_backends(
            vec![&fail, &success],
            |_candidate| Ok(accepted_candidate_report()),
            SolverCascadeBudget::new(2, 3, 6),
        )
        .expect("the second backend should be accepted");

        assert_eq!(result.0, vec![2.0]);
        assert_eq!(result.2.attempts.len(), 2);
        assert!(matches!(
            result.2.attempts[0].outcome,
            SolverAttemptOutcome::Failed {
                kind: SolverAttemptFailureKind::ResidualEvaluation,
                ..
            }
        ));
        assert_eq!(result.2.attempts[1].outcome, SolverAttemptOutcome::Accepted);
        assert_eq!(
            result.2.accepted_backend,
            SolverBackend::Legacy(super::Solvers::NR)
        );
    }

    #[test]
    fn first_backend_returns_a_rejected_candidate_and_the_next_one_succeeds() {
        let first = FakeBackend {
            backend: SolverBackend::Legacy(super::Solvers::LM),
            behavior: FakeBackendBehavior::Success {
                solution: vec![1.0],
            },
        };
        let second = FakeBackend {
            backend: SolverBackend::Legacy(super::Solvers::NR),
            behavior: FakeBackendBehavior::Success {
                solution: vec![2.0],
            },
        };

        let result = solve_with_fake_backends(
            vec![&first, &second],
            |candidate| {
                if candidate[0] == 1.0 {
                    Err(ReactionExtentError::InvalidCandidate {
                        field: "candidate_acceptance",
                        message: "first backend candidate is intentionally rejected".to_string(),
                    })
                } else {
                    Ok(accepted_candidate_report())
                }
            },
            SolverCascadeBudget::new(2, 3, 6),
        )
        .expect("the second backend should be accepted after rejection");

        assert_eq!(result.0, vec![2.0]);
        assert_eq!(result.2.attempts.len(), 2);
        assert!(matches!(
            result.2.attempts[0].outcome,
            SolverAttemptOutcome::RejectedCandidate { .. }
        ));
        assert_eq!(result.2.attempts[1].outcome, SolverAttemptOutcome::Accepted);
    }

    #[test]
    fn every_backend_fails_and_the_report_keeps_the_declared_order() {
        let first = FakeBackend {
            backend: SolverBackend::Legacy(super::Solvers::LM),
            behavior: FakeBackendBehavior::RecoverableFailure,
        };
        let second = FakeBackend {
            backend: SolverBackend::Legacy(super::Solvers::NR),
            behavior: FakeBackendBehavior::RecoverableFailure,
        };

        let error = solve_with_fake_backends(
            vec![&first, &second],
            |_candidate| Ok(accepted_candidate_report()),
            SolverCascadeBudget::new(2, 3, 6),
        )
        .unwrap_err();

        let ReactionExtentError::AllBackendsFailed { attempts } = error else {
            panic!("expected an exhaustive cascade failure");
        };
        assert_eq!(attempts.len(), 2);
        assert_eq!(
            attempts[0].backend,
            SolverBackend::Legacy(super::Solvers::LM)
        );
        assert_eq!(
            attempts[1].backend,
            SolverBackend::Legacy(super::Solvers::NR)
        );
        assert!(matches!(
            attempts[0].outcome,
            SolverAttemptOutcome::Failed { .. }
        ));
        assert!(matches!(
            attempts[1].outcome,
            SolverAttemptOutcome::Failed { .. }
        ));
    }

    #[test]
    fn global_budget_exhaustion_stops_later_backends_deterministically() {
        let first = FakeBackend {
            backend: SolverBackend::Legacy(super::Solvers::LM),
            behavior: FakeBackendBehavior::RecoverableFailure,
        };
        let second = FakeBackend {
            backend: SolverBackend::Legacy(super::Solvers::NR),
            behavior: FakeBackendBehavior::Success {
                solution: vec![2.0],
            },
        };
        let third = FakeBackend {
            backend: SolverBackend::RustedSciThe(RustedSciTheSolver::LevenbergMarquardt),
            behavior: FakeBackendBehavior::Success {
                solution: vec![3.0],
            },
        };

        let error = solve_with_fake_backends(
            vec![&first, &second, &third],
            |_candidate| Ok(accepted_candidate_report()),
            SolverCascadeBudget::new(1, 2, 2),
        )
        .unwrap_err();

        let ReactionExtentError::AllBackendsFailed { attempts } = error else {
            panic!("expected a budget-limited cascade failure");
        };
        assert_eq!(attempts.len(), 3);
        assert!(matches!(
            attempts[0].outcome,
            SolverAttemptOutcome::Failed { .. }
        ));
        assert!(matches!(
            attempts[1].outcome,
            SolverAttemptOutcome::Skipped { .. }
        ));
        assert!(matches!(
            attempts[2].outcome,
            SolverAttemptOutcome::Skipped { .. }
        ));
    }
}

#[cfg(test)]
mod continuation_seed_policy_tests {
    use super::{
        ContinuationSeedPolicy, EquilibriumLogMoles, Solvers, continuation_seed_for_point,
    };
    use crate::Thermodynamics::ChemEquilibrium::equilibrium_problem::{
        DEFAULT_TRACE_MOLE_FLOOR, TraceSpeciesSeedPolicy,
    };
    use crate::Thermodynamics::ChemEquilibrium::equilibrium_workflows::gas_solver;

    #[test]
    fn default_policy_continues_from_the_last_accepted_point() {
        assert_eq!(
            EquilibriumLogMoles::empty()
                .solver_settings
                .continuation_seed_policy,
            ContinuationSeedPolicy::PreviousAccepted
        );
        assert_eq!(
            continuation_seed_for_point(
                ContinuationSeedPolicy::PreviousAccepted,
                &Some(vec![-1.0]),
                &Some(vec![-4.0]),
            ),
            Some(vec![-4.0])
        );
    }

    #[test]
    fn independent_policy_preserves_the_configured_seed() {
        let mut solver = EquilibriumLogMoles::empty();
        solver.set_continuation_seed_policy(ContinuationSeedPolicy::IndependentPerPoint);

        assert_eq!(
            continuation_seed_for_point(
                solver.solver_settings.continuation_seed_policy,
                &Some(vec![-1.0]),
                &Some(vec![-4.0]),
            ),
            Some(vec![-1.0])
        );
    }

    #[test]
    fn solver_settings_use_the_absolute_trace_seed_by_default() {
        let mut solver = EquilibriumLogMoles::empty();
        solver.n0 = vec![1.0, 0.0];

        let (seed, generated) = solver.resolved_initial_guess().unwrap();
        assert!(generated);
        assert_eq!(seed[0], 0.0);
        assert_eq!(seed[1], DEFAULT_TRACE_MOLE_FLOOR.ln());
    }

    #[test]
    fn solver_settings_can_generate_a_relative_trace_seed_explicitly() {
        let mut solver = EquilibriumLogMoles::empty();
        solver.n0 = vec![4.0, 0.0];
        solver.solver_settings.trace_seed_policy =
            TraceSpeciesSeedPolicy::RelativeToLargestInitialMole {
                fraction: 1e-6,
                minimum_floor: 1e-30,
            };
        solver.solver_settings.solver = Solvers::LM;

        let (seed, generated) = solver.resolved_initial_guess().unwrap();
        assert!(generated);
        assert_eq!(seed[0], 4.0_f64.ln());
        assert!((seed[1] - (4.0e-6_f64).ln()).abs() < 1e-12);
    }

    #[test]
    fn gibbs_closure_builders_preserve_species_order_for_both_rc_and_arc_wrappers() {
        let mut solver = gas_solver(
            vec!["O2".to_string(), "O".to_string()],
            1000.0,
            101325.0,
            Solvers::LM,
            None,
            false,
        )
        .unwrap();

        let serial = solver.build_gibbs_functions().unwrap();
        let parallel = solver.build_parallel_gibbs_functions().unwrap();

        assert_eq!(serial.len(), 2);
        assert_eq!(parallel.len(), 2);
        for temperature in [300.0, 1200.0, 2400.0] {
            for index in 0..serial.len() {
                let rc_value = serial[index](temperature);
                let arc_value = parallel[index](temperature);
                assert!(
                    (rc_value - arc_value).abs() <= 1e-12,
                    "builder mismatch for species {index} at T={temperature}"
                );
            }
        }
    }
}

#[cfg(test)]
mod activity_contract_regression_tests {
    #![allow(deprecated)]
    use super::{
        GibbsFn, Phase, PhaseKind, equilibrium_logmole_residual2,
        evaluate_equilibrium_logmole_residual, reaction_phase_stoichiometry,
    };
    use nalgebra::DMatrix;
    use std::rc::Rc;

    #[test]
    fn deprecated_residual_path_matches_the_canonical_activity_contract() {
        let reactions = DMatrix::from_row_slice(2, 1, &[-1.0, 1.0]);
        let elements = DMatrix::from_row_slice(2, 1, &[1.0, 1.0]);
        let element_totals = vec![2.0];
        let gibbs: Vec<GibbsFn> = vec![Rc::new(|_| 0.0), Rc::new(|_| 0.0)];
        let phases = vec![
            Phase {
                kind: PhaseKind::IdealGas,
                species: vec![0],
            },
            Phase {
                kind: PhaseKind::IdealSolution,
                species: vec![1],
            },
        ];
        let species_phase = vec![0, 1];
        let delta_n = reaction_phase_stoichiometry(&reactions, &phases);
        let y = vec![0.0, 0.0];

        let canonical = evaluate_equilibrium_logmole_residual(
            &y,
            &reactions,
            &elements,
            &element_totals,
            &gibbs,
            &phases,
            500.0,
            202_650.0,
            101_325.0,
            &species_phase,
            &delta_n,
        )
        .unwrap();
        let legacy = equilibrium_logmole_residual2(
            reactions,
            elements,
            element_totals,
            gibbs,
            phases,
            500.0,
            202_650.0,
            101_325.0,
            1e-30,
            1e-30,
        )
        .unwrap();
        let legacy = legacy(&y).unwrap();

        assert_eq!(canonical.len(), legacy.len());
        for (lhs, rhs) in canonical.iter().zip(legacy.iter()) {
            assert!((lhs - rhs).abs() <= 1e-12);
        }
    }
}

/// Reconstructs finite, strictly positive physical mole values from log-moles.
///
/// The logarithmic formulation intentionally represents trace species, but it
/// must not silently turn an extreme iterate into an underflowed zero or an
/// overflowed infinity. Callers decide whether this is a candidate, residual,
/// or Jacobian failure; the reconstruction contract itself stays uniform.
pub fn compute_species_moles(log_moles: &[f64]) -> Result<Vec<f64>, ReactionExtentError> {
    let mut moles = Vec::with_capacity(log_moles.len());
    for (index, &log_moles_i) in log_moles.iter().enumerate() {
        if !log_moles_i.is_finite() {
            return Err(ReactionExtentError::InvalidCandidate {
                field: "log_moles",
                message: format!("log-mole coordinate {index} is non-finite"),
            });
        }
        let moles_i = log_moles_i.exp();
        if !moles_i.is_finite() || moles_i <= 0.0 {
            return Err(ReactionExtentError::InvalidCandidate {
                field: "mole_amount",
                message: format!(
                    "log-mole coordinate {index} = {log_moles_i:e} reconstructs to an invalid mole amount"
                ),
            });
        }
        moles.push(moles_i);
    }
    Ok(moles)
}

/// Builds the column-oriented sweep table without changing any solver field.
///
/// Sweep entry points use this as a final validation step before publishing a
/// bundle of rows, failures, snapshots, and the derived table together.
fn build_mole_series_table(
    substances: &[String],
    rows: &[(f64, Vec<f64>)],
) -> Result<HashMap<String, Vec<f64>>, ReactionExtentError> {
    let mut unique_species = HashSet::with_capacity(substances.len());
    if substances
        .iter()
        .any(|substance| !unique_species.insert(substance))
    {
        return Err(ReactionExtentError::InvalidProblem {
            field: "species",
            message: "species names must be unique when publishing sweep results".to_string(),
        });
    }
    for (temperature, moles) in rows {
        if moles.len() != substances.len() {
            return Err(ReactionExtentError::DimensionMismatch(format!(
                "temperature {temperature} produced {} mole values for {} substances",
                moles.len(),
                substances.len(),
            )));
        }
    }

    let mut published = HashMap::with_capacity(substances.len() + 1);
    published.insert(
        "T".to_string(),
        rows.iter().map(|(temperature, _)| *temperature).collect(),
    );
    for (index, substance) in substances.iter().enumerate() {
        published.insert(
            substance.clone(),
            rows.iter().map(|(_, moles)| moles[index]).collect(),
        );
    }
    Ok(published)
}

/// Computes total element amounts from species composition.
///
/// The check is deliberately performed before `nalgebra` multiplication: a
/// mismatched public matrix/vector pair must become a typed domain error, not
/// a linear-algebra assertion failure.
pub fn compute_element_totals(
    a: &DMatrix<f64>,
    n0: &[f64],
) -> Result<DVector<f64>, ReactionExtentError> {
    if a.nrows() != n0.len() {
        return Err(ReactionExtentError::DimensionMismatch(format!(
            "element composition has {} species rows but initial moles have {} entries",
            a.nrows(),
            n0.len(),
        )));
    }
    if a.iter().any(|value| !value.is_finite()) || n0.iter().any(|value| !value.is_finite()) {
        return Err(ReactionExtentError::InvalidProblem {
            field: "element_composition_or_initial_moles",
            message: "element composition and initial moles must be finite".to_string(),
        });
    }
    Ok(&a.transpose() * DVector::from_column_slice(n0))
}

/// Calculates standard Gibbs free energy change for each reaction
///
/// Computes ΔG°_rxn = Σ ν_i * G°_i for each independent reaction.
pub fn reaction_standard_gibbs(stoich: &DMatrix<f64>, gibbs: &[GibbsFn], T: f64) -> Vec<f64> {
    let m = stoich.nrows();
    let r = stoich.ncols();

    let mut dg0 = vec![0.0; r];

    for k in 0..r {
        let mut sum = 0.0;
        for i in 0..m {
            sum += stoich[(i, k)] * gibbs[i](T);
        }
        dg0[k] = sum;
    }
    dg0
}

/// Computes row scaling factors for the canonical log-mole residual.
///
/// The returned vector has one entry per residual row:
/// - the first `r` entries correspond to reaction-equilibrium equations and
///   are measured in the same dimensionless log-space as the residual itself;
/// - the remaining `e` entries correspond to element-balance equations and are
///   measured in mole units.
///
/// Scaling is a numerical conditioning aid only. It does not change the
/// physical variables, and it must be applied to residual rows and Jacobian
/// rows consistently.
pub fn equilibrium_scaling(
    stoich: &DMatrix<f64>,   // m × r
    elements: &DMatrix<f64>, // m × E
    gibbs: &[GibbsFn],       // m
    element_totals: &[f64],  // E
    T: f64,
) -> Result<Vec<f64>, ReactionExtentError> {
    let m = stoich.nrows();
    let r = stoich.ncols();
    let e = elements.ncols();
    if !T.is_finite() || T <= 0.0 {
        return Err(ReactionExtentError::InvalidConditions {
            parameter: "temperature",
            value: T,
        });
    }
    if elements.nrows() != m {
        return Err(ReactionExtentError::DimensionMismatch(format!(
            "stoichiometric matrix has {m} species rows but element matrix has {}",
            elements.nrows(),
        )));
    }
    if gibbs.len() != m {
        return Err(ReactionExtentError::DimensionMismatch(format!(
            "scaling received {} Gibbs functions for {m} species",
            gibbs.len(),
        )));
    }
    if element_totals.len() != e {
        return Err(ReactionExtentError::DimensionMismatch(format!(
            "scaling received {} element totals for {e} elements",
            element_totals.len(),
        )));
    }

    let rt = 8.314462618 * T;

    // --- reaction scaling ---
    let dg0 = reaction_standard_gibbs(stoich, gibbs, T);
    let mut scale = vec![0.0; r + e];

    for k in 0..r {
        let mut nu_norm = 0.0;
        for i in 0..m {
            let nu = stoich[(i, k)];
            nu_norm += nu * nu;
        }
        nu_norm = nu_norm.sqrt();

        scale[k] = dg0[k].abs().max(rt * nu_norm).max(10.0); // dimensionless, log-scale
    }

    // --- element balance scaling ---
    for el in 0..e {
        scale[r + el] = element_totals[el].abs().max(10.0);
    }

    Ok(scale)
}

/// Legacy spelling retained only for fixture literals while callers migrate.
///
/// This is a type alias, not a second model. All solver production paths store
/// and consume [`PhaseActivityModel`] directly.
pub type PhaseKind = PhaseActivityModel;

/// Represents a thermodynamic phase containing multiple species
#[derive(Debug, Clone)]
pub struct Phase {
    /// Canonical activity law (ideal gas or ideal solution).
    pub kind: PhaseActivityModel,
    /// Indices of species belonging to this phase
    pub species: Vec<usize>,
}

/// Function type for Gibbs free energy evaluation
///
/// Takes temperature in K and returns standard Gibbs free energy in J/mol.
pub type GibbsFn = Rc<dyn Fn(f64) -> f64>;

/// Maps species indices to their corresponding phase indices
///
/// Validates that each species belongs to exactly one phase and returns
/// a vector where element i gives the phase index for species i.
pub fn species_to_phase_map(
    phases: &[Phase],
    num_species: usize,
) -> Result<Vec<usize>, ReactionExtentError> {
    let mut map = vec![None; num_species];

    for (j, phase) in phases.iter().enumerate() {
        for &i in &phase.species {
            if i >= num_species {
                return Err(ReactionExtentError::DimensionMismatch(format!(
                    "phase {j} references species index {i}, but the problem has {num_species} species"
                )));
            }
            if map[i].is_some() {
                error!(
                    "Species {} appears in more than one phase. Duplicate species per phase instead (e.g. O2_g, O2_l).",
                    i
                );
                return Err(ReactionExtentError::DuplicateSpecies(i));
            }
            // Assign species i to phase j
            map[i] = Some(j);
        }
    }

    let mut out = Vec::with_capacity(num_species);
    for x in map.into_iter() {
        match x {
            Some(v) => out.push(v),
            None => {
                error!("Species not assigned to any phase");
                return Err(ReactionExtentError::SpeciesNotAssigned { index: out.len() });
            }
        }
    }
    Ok(out)
}

/// Computes change in mole numbers per phase for each reaction
///
/// For each reaction k and phase j, calculates Δn[k][j] = sum of stoichiometric
/// coefficients for all species in phase j participating in reaction k.
pub fn reaction_phase_stoichiometry(
    reactions: &DMatrix<f64>, // m × r
    phases: &[Phase],
) -> Vec<Vec<f64>> {
    let r = reactions.ncols();

    let mut delta_n = vec![vec![0.0; phases.len()]; r];

    for (j, phase) in phases.iter().enumerate() {
        for &i in &phase.species {
            for k in 0..r {
                delta_n[k][j] += reactions[(i, k)];
            }
        }
    }

    delta_n
}

// The threshold-based residual/Jacobian pair below is retained temporarily as
// a deprecated compatibility surface. The canonical solver never selects it;
// new code must use the validated log-mole formulation without activity masks.
#[inline]
fn species_active(n: f64, eps: f64) -> bool {
    n > eps
}

#[inline]
fn phase_active(n: f64, eps: f64) -> bool {
    n > eps
}

/// Validates the square log-mole equilibrium system before numerical closures
/// capture its data. The formulation has one equation per species: independent
/// reaction equations plus elemental-balance equations must equal `m`.
pub(crate) fn validate_logmole_system_dimensions(
    reactions: &DMatrix<f64>,
    elements: &DMatrix<f64>,
    species_phase: &[usize],
    phase_count: usize,
    delta_n: &[Vec<f64>],
) -> Result<(), ReactionExtentError> {
    let species_count = reactions.nrows();
    let reaction_count = reactions.ncols();
    let element_count = elements.ncols();

    if elements.nrows() != species_count {
        return Err(ReactionExtentError::DimensionMismatch(format!(
            "element matrix has {} species rows, expected {species_count}",
            elements.nrows()
        )));
    }
    if reaction_count + element_count != species_count {
        return Err(ReactionExtentError::DimensionMismatch(format!(
            "log-mole formulation has {reaction_count} reaction and {element_count} element equations for {species_count} species"
        )));
    }
    if phase_count == 0 || species_phase.len() != species_count {
        return Err(ReactionExtentError::DimensionMismatch(format!(
            "species-to-phase map has {} entries for {species_count} species and {phase_count} phases",
            species_phase.len()
        )));
    }
    if species_phase.iter().any(|&phase| phase >= phase_count) {
        return Err(ReactionExtentError::DimensionMismatch(
            "species-to-phase map references a missing phase".to_string(),
        ));
    }
    if delta_n.len() != reaction_count
        || delta_n
            .iter()
            .any(|phase_row| phase_row.len() != phase_count)
    {
        return Err(ReactionExtentError::DimensionMismatch(
            "reaction-to-phase stoichiometry dimensions do not match the problem".to_string(),
        ));
    }

    Ok(())
}

/// Validates the three scalar conditions consumed directly by the residual.
///
/// This is deliberately separate from matrix validation so every caller gets
/// the exact offending parameter and value instead of an ambiguous combined
/// "temperature or pressure" diagnostic.
pub(crate) fn validate_residual_conditions(
    temperature: f64,
    pressure: f64,
    reference_pressure: f64,
) -> Result<(), ReactionExtentError> {
    for (parameter, value) in [
        ("temperature", temperature),
        ("pressure", pressure),
        ("reference_pressure", reference_pressure),
    ] {
        if !value.is_finite() || value <= 0.0 {
            return Err(ReactionExtentError::InvalidConditions { parameter, value });
        }
    }
    Ok(())
}

/// Generates a legacy residual closure for the canonical log-mole system.
///
/// The residual has two blocks:
/// - reaction equations, written in dimensionless log-space;
/// - element-balance equations, written in mole units.
///
/// This closure factory exists for compatibility with the temporary legacy
/// backend adapter. New code should prefer the pure
/// [`evaluate_equilibrium_logmole_residual`] entry point on a prepared problem.
///
pub fn equilibrium_logmole_residual(
    reactions: DMatrix<f64>,  // m × r
    elements: DMatrix<f64>,   // m × E
    element_totals: Vec<f64>, // E
    gibbs: Vec<GibbsFn>,      // m
    phases: Vec<Phase>,
    temperature: f64,
    pressure: f64,
    p0: f64,
    species_phase: Vec<usize>,
    _subs_eps: f64,
    __phase_eps: f64,
) -> Result<Box<dyn Fn(&[f64]) -> Result<Vec<f64>, ReactionExtentError>>, ReactionExtentError> {
    let m = reactions.nrows();
    let e = elements.ncols();

    if gibbs.len() != m || element_totals.len() != e {
        return Err(ReactionExtentError::DimensionMismatch(format!(
            "log-mole residual has {m} species, {} Gibbs functions, and {} element totals for {e} elements",
            gibbs.len(),
            element_totals.len(),
        )));
    }

    let delta_n = reaction_phase_stoichiometry(&reactions, &phases);
    validate_logmole_system_dimensions(
        &reactions,
        &elements,
        &species_phase,
        phases.len(),
        &delta_n,
    )?;
    validate_residual_conditions(temperature, pressure, p0)?;
    Ok(Box::new(move |log_moles| {
        evaluate_equilibrium_logmole_residual(
            log_moles,
            &reactions,
            &elements,
            &element_totals,
            &gibbs,
            &phases,
            temperature,
            pressure,
            p0,
            &species_phase,
            &delta_n,
        )
    }))
}

/// Evaluates the canonical log-mole residual for one prepared state.
///
/// Residual layout:
/// - rows `0..r` are reaction-equilibrium equations in dimensionless log
///   space;
/// - rows `r..r+e` are element-balance equations in mole units.
///
/// The row ordering is deterministic and must match the prepared problem's
/// reaction basis and element layout exactly. This function is the canonical
/// implementation; legacy closure adapters delegate here rather than keeping a
/// second formula alive.
#[allow(clippy::too_many_arguments)]
pub fn evaluate_equilibrium_logmole_residual(
    log_moles: &[f64],
    reactions: &DMatrix<f64>,
    elements: &DMatrix<f64>,
    element_totals: &[f64],
    gibbs: &[GibbsFn],
    phases: &[Phase],
    temperature: f64,
    pressure: f64,
    p0: f64,
    species_phase: &[usize],
    phase_stoichiometry: &[Vec<f64>],
) -> Result<Vec<f64>, ReactionExtentError> {
    let m = reactions.nrows();
    let r = reactions.ncols();

    if gibbs.len() != m || element_totals.len() != elements.ncols() {
        return Err(ReactionExtentError::DimensionMismatch(
            "canonical residual input dimensions are inconsistent".to_string(),
        ));
    }
    validate_logmole_system_dimensions(
        reactions,
        elements,
        species_phase,
        phases.len(),
        phase_stoichiometry,
    )?;
    validate_residual_conditions(temperature, pressure, p0)?;
    if log_moles.len() != m {
        return Err(ReactionExtentError::ResidualEvaluation(
            "log-mole vector length mismatch".to_string(),
        ));
    }

    let moles = compute_species_moles(log_moles).map_err(|error| {
        ReactionExtentError::ResidualEvaluation(format!(
            "cannot reconstruct species moles for residual evaluation: {error}"
        ))
    })?;
    let mut phase_moles = vec![0.0; phases.len()];
    for (species, &phase) in species_phase.iter().enumerate() {
        phase_moles[phase] += moles[species];
    }
    for (index, &total) in phase_moles.iter().enumerate() {
        if !total.is_finite() || total <= 0.0 {
            return Err(ReactionExtentError::InvalidNPhase {
                index,
                value: total,
            });
        }
    }

    let phase_offsets = phase_activity_models(phases)
        .into_iter()
        .map(|model| model.log_phase_offset(pressure, p0))
        .collect::<Result<Vec<_>, _>>()?;
    let mut residual = vec![0.0; m];
    let rt = R * temperature;

    for reaction in 0..r {
        let mut sum_log_moles = 0.0;
        let mut sum_log_phase_moles = 0.0;
        let mut sum_phase_offsets = 0.0;
        let mut standard_gibbs = 0.0;
        for species in 0..m {
            let coefficient = reactions[(species, reaction)];
            if coefficient != 0.0 {
                sum_log_moles += coefficient * log_moles[species];
                let species_gibbs = gibbs[species](temperature);
                if !species_gibbs.is_finite() {
                    return Err(ReactionExtentError::InvalidDG0 {
                        species_index: species,
                        dg0: species_gibbs,
                        temperature,
                    });
                }
                standard_gibbs += coefficient * species_gibbs;
            }
        }
        for phase in 0..phases.len() {
            let coefficient = phase_stoichiometry[reaction][phase];
            if coefficient != 0.0 {
                sum_log_phase_moles += coefficient * phase_moles[phase].ln();
                sum_phase_offsets += coefficient * phase_offsets[phase];
            }
        }
        residual[reaction] =
            sum_log_moles - sum_log_phase_moles + sum_phase_offsets - (-standard_gibbs / rt);
        if !residual[reaction].is_finite() {
            return Err(ReactionExtentError::ResidualEvaluation(format!(
                "reaction {reaction} produced a non-finite residual"
            )));
        }
    }

    for element in 0..elements.ncols() {
        let reconstructed_total = (0..m)
            .map(|species| elements[(species, element)] * moles[species])
            .sum::<f64>();
        residual[r + element] = reconstructed_total - element_totals[element];
    }

    Ok(residual)
}

/// Deprecated threshold-based residual retained only for compatibility with
/// experiments outside the canonical equilibrium path.
#[deprecated(note = "use evaluate_equilibrium_logmole_residual instead")]
pub fn equilibrium_logmole_residual2(
    reactions: DMatrix<f64>,  // m × r
    elements: DMatrix<f64>,   // m × E
    element_totals: Vec<f64>, // E
    gibbs: Vec<GibbsFn>,      // m
    phases: Vec<Phase>,
    temperature: f64,
    pressure: f64,
    p0: f64,
    subs_eps: f64,
    phase_eps: f64,
) -> Result<Box<dyn Fn(&[f64]) -> Result<Vec<f64>, ReactionExtentError>>, ReactionExtentError> {
    let m = reactions.nrows();
    let r = reactions.ncols();
    let e = elements.ncols();

    let species_phase = species_to_phase_map(&phases, m)?;
    let delta_n = reaction_phase_stoichiometry(&reactions, &phases);
    let rt = R * temperature;

    Ok(Box::new(move |y: &[f64]| {
        if y.len() != m {
            return Err(ReactionExtentError::ResidualEvaluation(
                "log-mole vector length mismatch".to_string(),
            ));
        }

        // --- species moles ---
        let mut n = vec![0.0; m];
        let mut active = vec![false; m];
        for i in 0..m {
            n[i] = y[i].exp();
            active[i] = species_active(n[i], subs_eps);
        }

        // --- phase totals ---
        let mut n_phase = vec![0.0; phases.len()];
        let mut phase_active_mask = vec![false; phases.len()];
        for i in 0..m {
            if active[i] {
                n_phase[species_phase[i]] += n[i];
            }
        }
        for j in 0..phases.len() {
            phase_active_mask[j] = phase_active(n_phase[j], phase_eps);
        }

        // --- phase offsets ---
        let phi = phase_activity_models(&phases)
            .into_iter()
            .map(|model| model.log_phase_offset(pressure, p0))
            .collect::<Result<Vec<_>, _>>()?;

        let mut f = vec![0.0; m];

        // -------------------------
        // Reaction equilibrium eqs
        // -------------------------
        for k in 0..r {
            let mut sum_ln_n = 0.0;
            let mut sum_ln_n_phase = 0.0;
            let mut sum_phi = 0.0;
            let mut dg0 = 0.0;

            for i in 0..m {
                if !active[i] {
                    continue;
                }
                let nu = reactions[(i, k)];
                if nu != 0.0 {
                    sum_ln_n += nu * y[i];
                    dg0 += nu * gibbs[i](temperature);
                }
            }

            for j in 0..phases.len() {
                if !phase_active_mask[j] {
                    continue;
                }
                let dnk = delta_n[k][j];
                if dnk != 0.0 {
                    sum_ln_n_phase += dnk * n_phase[j].ln();
                    sum_phi += dnk * phi[j];
                }
            }

            let ln_k = -dg0 / rt;
            f[k] = sum_ln_n - sum_ln_n_phase + sum_phi - ln_k;
        }

        // -------------------------
        // Element balance eqs
        // -------------------------
        for el in 0..e {
            let mut sum = 0.0;
            for i in 0..m {
                if active[i] {
                    sum += elements[(i, el)] * n[i];
                }
            }
            f[r + el] = sum - element_totals[el];
        }

        // -------------------------
        // Inactive species constraints
        // -------------------------
        for i in 0..m {
            if !active[i] {
                f[i] = y[i]; // push to -∞
            }
        }

        Ok(f)
    }))
}

/// Creates a scaled residual closure for improved numerical conditioning.
///
/// The scale vector must have one entry per residual row. Scaling is applied
/// after residual evaluation and before backend acceptance, so the physical
/// equations remain unchanged.
pub fn scaled_residual(
    f: Box<dyn Fn(&[f64]) -> Result<Vec<f64>, ReactionExtentError>>,
    scale: Vec<f64>,
) -> Box<dyn Fn(&[f64]) -> Result<Vec<f64>, ReactionExtentError>> {
    Box::new(move |xi: &[f64]| scale_residual_rows(f(xi)?, &scale))
}

/// Applies a validated row scale without rebuilding a residual closure.
///
/// This helper is intentionally pure: it does not inspect solver state or
/// modify any cached problem data.
pub fn scale_residual_rows(
    mut residual: Vec<f64>,
    scale: &[f64],
) -> Result<Vec<f64>, ReactionExtentError> {
    if residual.len() != scale.len() {
        return Err(ReactionExtentError::DimensionMismatch(format!(
            "residual has {} rows but scale has {} entries",
            residual.len(),
            scale.len(),
        )));
    }

    for (index, (value, factor)) in residual.iter_mut().zip(scale).enumerate() {
        if *factor <= 0.0 || !factor.is_finite() {
            return Err(ReactionExtentError::InvalidProblem {
                field: "residual_scale",
                message: format!("scale[{index}] must be finite and positive, got {factor}"),
            });
        }
        *value /= factor;
    }
    Ok(residual)
}

/// Evaluates the canonical analytical Jacobian for one log-moles state.
///
/// This is the pure counterpart to [`evaluate_equilibrium_logmole_residual`].
/// New backend adapters should use it instead of carrying the historical
/// threshold parameters required by [`equilibrium_logmole_jacobian`].
pub fn evaluate_equilibrium_logmole_jacobian(
    log_moles: &[f64],
    reactions: &DMatrix<f64>,
    elements: &DMatrix<f64>,
    species_phase: &[usize],
    phase_stoichiometry: &[Vec<f64>],
    phase_count: usize,
) -> Result<DMatrix<f64>, ReactionExtentError> {
    equilibrium_logmole_jacobian(
        log_moles,
        reactions,
        elements,
        species_phase,
        phase_stoichiometry,
        phase_count,
        0.0,
        0.0,
    )
}

/// Computes the canonical Jacobian for the log-mole equilibrium residual.
///
/// The Jacobian follows the same row ordering as
/// [`evaluate_equilibrium_logmole_residual`]:
/// - reaction rows first,
/// - element-balance rows second.
///
/// Columns correspond to the log-mole coordinates in the prepared problem's
/// species ordering.
pub fn equilibrium_logmole_jacobian(
    y: &[f64],
    reactions: &DMatrix<f64>, // m × r
    elements: &DMatrix<f64>,  // m × E
    species_phase: &[usize],
    delta_n: &[Vec<f64>],
    n_phases: usize,

    _subs_eps: f64,
    _phase_eps: f64,
) -> Result<DMatrix<f64>, ReactionExtentError> {
    let m = reactions.nrows();
    let r = reactions.ncols();
    let e = elements.ncols();

    validate_logmole_system_dimensions(reactions, elements, species_phase, n_phases, delta_n)?;
    if y.len() != m {
        return Err(ReactionExtentError::DimensionMismatch(format!(
            "log-mole vector has {} entries, expected {m}",
            y.len()
        )));
    }

    let n = compute_species_moles(y).map_err(|error| {
        ReactionExtentError::JacobianEvaluation(format!(
            "cannot reconstruct species moles for Jacobian evaluation: {error}"
        ))
    })?;

    let mut n_phase = vec![0.0; n_phases];
    for i in 0..m {
        n_phase[species_phase[i]] += n[i];
    }
    for (index, &total) in n_phase.iter().enumerate() {
        if !total.is_finite() || total <= 0.0 {
            return Err(ReactionExtentError::InvalidNPhase {
                index,
                value: total,
            });
        }
    }

    let mut jmat = DMatrix::<f64>::zeros(m, m);

    // --- equilibrium equations ---
    for k in 0..r {
        for i in 0..m {
            let nu = reactions[(i, k)];
            // A species with zero direct stoichiometric coefficient still
            // differentiates ln(N_phase) when it shares a phase with a
            // reacting species. Omitting this term breaks mixed-phase
            // Jacobians while leaving many all-reacting gas fixtures green.
            let phase = species_phase[i];
            let term = delta_n[k][phase] * n[i] / n_phase[phase];
            jmat[(k, i)] = nu - term;
        }
    }

    // --- element balances ---
    for el in 0..e {
        for i in 0..m {
            jmat[(r + el, i)] = elements[(i, el)] * n[i];
        }
    }

    Ok(jmat)
}

/// Deprecated threshold-based Jacobian paired with
/// [`equilibrium_logmole_residual2`].
#[deprecated(note = "use evaluate_equilibrium_logmole_jacobian instead")]
pub fn equilibrium_logmole_jacobian2(
    y: &[f64],
    reactions: &DMatrix<f64>, // m × r
    elements: &DMatrix<f64>,  // m × E
    species_phase: &[usize],
    delta_n: &[Vec<f64>],
    n_phases: usize,
    subs_eps: f64,
    phase_eps: f64,
) -> Result<DMatrix<f64>, ReactionExtentError> {
    let m = reactions.nrows();
    let r = reactions.ncols();
    let e = elements.ncols();

    let mut n = vec![0.0; m];
    let mut active = vec![false; m];
    for i in 0..m {
        n[i] = y[i].exp();
        active[i] = species_active(n[i], subs_eps);
    }

    let mut n_phase = vec![0.0; n_phases];
    let mut phase_active_mask = vec![false; n_phases];
    for i in 0..m {
        if active[i] {
            n_phase[species_phase[i]] += n[i];
        }
    }
    for j in 0..n_phases {
        phase_active_mask[j] = phase_active(n_phase[j], phase_eps);
    }

    let mut jmat = DMatrix::<f64>::zeros(m, m);

    // --- reaction equilibrium rows ---
    for k in 0..r {
        for i in 0..m {
            if !active[i] {
                continue;
            }
            let nu = reactions[(i, k)];

            let phase = species_phase[i];
            if phase_active_mask[phase] {
                let term = delta_n[k][phase] * n[i] / n_phase[phase];
                jmat[(k, i)] = nu - term;
            } else {
                jmat[(k, i)] = nu;
            }
        }
    }

    // --- element balances ---
    for el in 0..e {
        for i in 0..m {
            if active[i] {
                jmat[(r + el, i)] = elements[(i, el)] * n[i];
            }
        }
    }

    // --- inactive species diagonal ---
    for i in 0..m {
        if !active[i] {
            jmat[(i, i)] = 1.0;
        }
    }

    Ok(jmat)
}

/// Creates a scaled Jacobian closure for improved conditioning.
///
/// The same row scale used for the residual must be reused here so that the
/// backend sees a consistent linearization of the same equations.
pub fn scaled_jacobian(
    j: Box<dyn Fn(&[f64]) -> Result<DMatrix<f64>, ReactionExtentError>>,
    scale: Vec<f64>,
) -> Box<dyn Fn(&[f64]) -> Result<DMatrix<f64>, ReactionExtentError>> {
    Box::new(move |xi: &[f64]| scale_jacobian_rows(j(xi)?, &scale))
}

/// Applies a validated row scale to an analytical Jacobian.
pub fn scale_jacobian_rows(
    mut jacobian: DMatrix<f64>,
    scale: &[f64],
) -> Result<DMatrix<f64>, ReactionExtentError> {
    if jacobian.nrows() != scale.len() {
        return Err(ReactionExtentError::DimensionMismatch(format!(
            "Jacobian has {} rows but scale has {} entries",
            jacobian.nrows(),
            scale.len(),
        )));
    }

    for k in 0..jacobian.nrows() {
        let s = scale[k];
        if s <= 0.0 || !s.is_finite() {
            return Err(ReactionExtentError::InvalidProblem {
                field: "residual_scale",
                message: format!("scale[{k}] must be finite and positive, got {s}"),
            });
        }
        for jcol in 0..jacobian.ncols() {
            jacobian[(k, jcol)] /= s;
        }
    }
    Ok(jacobian)
}