hartree-cli 0.1.2

Command-line driver for hartree: an XYZ file plus a basis and method, to an SCF energy.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
use std::process::ExitCode;

use hartree::core::Molecule;
use hartree::core::units::{ANGSTROM_TO_BOHR, AU_DIPOLE_TO_DEBYE};
use hartree::dft::FunctionalSpec;
use hartree::disp::Dispersion;
use hartree::opt::OptResult;
use hartree::opt::ts::{TsResult, TsStatus};
use hartree::props::frequencies::FrequencyResult;
use hartree::props::population::PopulationAnalysis;
use hartree::props::thermo::ThermoResult;
use hartree::scf::{Reference, ScfResult, Smearing};
use hartree::{DftDiagnostics, Job, JobOptions, Method, PostHfResult};
use serde::Serialize;

mod periodic;
mod ts_flags;

fn main() -> ExitCode {
    match run() {
        Ok(true) => ExitCode::SUCCESS,
        Ok(false) => ExitCode::FAILURE,
        Err(message) => {
            eprintln!("error: {message}");
            ExitCode::FAILURE
        }
    }
}

fn run() -> Result<bool, String> {
    let args: Vec<String> = std::env::args().skip(1).collect();
    if args.is_empty() || args.iter().any(|a| a == "-h" || a == "--help") {
        print_usage();
        return Ok(true);
    }

    if periodic::is_periodic(&args) {
        return periodic::run_periodic_cli(&args);
    }

    let mut xyz_path: Option<String> = None;
    let mut basis = String::from("sto-3g");
    let mut basis_explicit = false;
    let mut method_str = String::from("rhf");
    let mut charge: i32 = 0;
    let mut multiplicity: u32 = 1;
    let mut do_opt = false;
    let mut do_ts = false;
    let mut ts_options = hartree::opt::ts::TsOptions::default();
    let mut ts_flag_seen = false;
    let mut irc_subflag_seen = false;
    let mut ts_product_path: Option<String> = None;
    let mut ts_use_neb = false;
    let mut ts_neb_images: Option<usize> = None;
    let mut ts_scan_points: Option<usize> = None;
    let mut ts_scan_coord: Option<hartree::CoordScanSpec> = None;
    let mut ts_output_path: Option<String> = None;
    let mut all_electron = false;
    let mut direct = false;
    let mut ri = false;
    let mut ri_mp2 = false;
    let mut cosx = false;
    let mut x2c = false;
    let mut do_properties = false;
    let mut do_freq = false;
    let mut do_sph = false;
    let mut conformers: Option<Option<String>> = None;
    let mut conformers_out: Option<String> = None;
    let mut rerank = false;
    let mut symmetry_number: u32 = 1;
    let mut qrrho_w0: f64 = hartree::props::thermo::QRRHO_W0_DEFAULT_CM1;
    let mut grid: Option<usize> = None;
    let mut solvent: Option<String> = None;
    let mut eps: Option<f64> = None;
    let mut smd: Option<String> = None;
    let mut alpb: Option<String> = None;
    let mut gbsa: Option<String> = None;
    let mut cosmo_file: Option<String> = None;
    let mut smear: Option<f64> = None;
    let mut method_explicit = false;
    let mut fod = false;
    let mut fod_cube: Option<String> = None;
    let mut cp_na: Option<usize> = None;
    let mut cp_charges: Option<(i32, i32)> = None;
    let mut cp_mults: Option<(u32, u32)> = None;
    let mut gcp_keyword: Option<String> = None;
    let mut no_method_warnings = false;
    let mut recommend_task: Option<String> = None;
    let mut protocol: Option<String> = None;

    let mut i = 0;
    while i < args.len() {
        match args[i].as_str() {
            "--basis" => {
                basis = take(&args, &mut i, "--basis")?;
                basis_explicit = true;
            }
            "--method" => {
                method_str = take(&args, &mut i, "--method")?.to_ascii_lowercase();
                method_explicit = true;
            }
            "--charge" => {
                charge = take(&args, &mut i, "--charge")?
                    .parse()
                    .map_err(|_| "--charge must be an integer".to_string())?
            }
            flag @ ("--multiplicity" | "--mult" | "--spin") => {
                multiplicity = take(&args, &mut i, flag)?
                    .parse()
                    .map_err(|_| "--multiplicity must be a positive integer (2S+1)".to_string())?
            }
            "--opt" => do_opt = true,
            "--ts" => do_ts = true,
            "--ts-product" => {
                ts_product_path = Some(take(&args, &mut i, "--ts-product")?);
                ts_flag_seen = true;
            }
            "--ts-neb" => {
                ts_use_neb = true;
                ts_flag_seen = true;
            }
            "--ts-neb-images" => {
                let n: usize = take(&args, &mut i, "--ts-neb-images")?
                    .parse()
                    .map_err(|_| "--ts-neb-images must be a positive integer".to_string())?;
                if n == 0 {
                    return Err("--ts-neb-images must be a positive integer (>= 1)".into());
                }
                ts_neb_images = Some(n);
                ts_flag_seen = true;
            }
            "--ts-scan" => {
                let n: usize = take(&args, &mut i, "--ts-scan")?
                    .parse()
                    .map_err(|_| "--ts-scan must be an integer point count".to_string())?;
                if n < 3 {
                    return Err("--ts-scan needs at least 3 path points".into());
                }
                ts_scan_points = Some(n);
                ts_flag_seen = true;
            }
            "--ts-scan-coord" => {
                let spec = take(&args, &mut i, "--ts-scan-coord")?;
                ts_scan_coord = Some(ts_flags::parse_scan_coord(&spec)?);
                ts_flag_seen = true;
            }
            "--ts-output" => {
                let path = take(&args, &mut i, "--ts-output")?;
                if path.trim().is_empty() {
                    return Err("--ts-output needs a non-empty file path".into());
                }
                ts_output_path = Some(path);
                ts_flag_seen = true;
            }
            "--all-electron" => all_electron = true,
            "--direct" => direct = true,
            "--ri" => ri = true,
            "--ri-mp2" => ri_mp2 = true,
            "--cosx" => cosx = true,
            "--x2c" => x2c = true,
            "--properties" | "--props" => do_properties = true,
            "--freq" => do_freq = true,
            "--sph" => do_sph = true,
            "--conformers" => {
                if args.get(i + 1).map(|s| s.as_str()) == Some("crest") {
                    i += 1;
                    conformers = Some(Some("crest".to_string()));
                } else {
                    conformers = Some(None);
                }
            }
            "--conformers-out" => conformers_out = Some(take(&args, &mut i, "--conformers-out")?),
            "--rerank" => rerank = true,
            "--qrrho-w0" => {
                qrrho_w0 = take(&args, &mut i, "--qrrho-w0")?
                    .parse()
                    .map_err(|_| "--qrrho-w0 must be a frequency in cm^-1".to_string())?;
                if !qrrho_w0.is_finite() || qrrho_w0 <= 0.0 {
                    return Err("--qrrho-w0 must be a positive frequency in cm^-1".into());
                }
            }
            "--symmetry-number" => {
                symmetry_number = take(&args, &mut i, "--symmetry-number")?
                    .parse()
                    .map_err(|_| "--symmetry-number must be a positive integer".to_string())?
            }
            "--grid" => {
                grid = Some(
                    take(&args, &mut i, "--grid")?
                        .parse()
                        .map_err(|_| "--grid must be an integer 0..=4".to_string())?,
                )
            }
            "--solvent" => solvent = Some(take(&args, &mut i, "--solvent")?.to_ascii_lowercase()),
            "--smd" => smd = Some(take(&args, &mut i, "--smd")?.to_ascii_lowercase()),
            "--alpb" => alpb = Some(take(&args, &mut i, "--alpb")?.to_ascii_lowercase()),
            "--gbsa" => gbsa = Some(take(&args, &mut i, "--gbsa")?.to_ascii_lowercase()),
            "--cosmo-file" => cosmo_file = Some(take(&args, &mut i, "--cosmo-file")?),
            "--smear" => {
                let t: f64 = take(&args, &mut i, "--smear")?
                    .parse()
                    .map_err(|_| "--smear must be a temperature in kelvin".to_string())?;
                if !t.is_finite() || t <= 0.0 {
                    return Err("--smear temperature must be positive".into());
                }
                smear = Some(t);
            }
            "--cp" => {
                let n: usize = take(&args, &mut i, "--cp")?.parse().map_err(|_| {
                    "--cp must be the number of atoms in fragment A (the first n atoms \
                     of the XYZ, in input order)"
                        .to_string()
                })?;
                if n == 0 {
                    return Err("--cp needs at least one atom in fragment A".into());
                }
                cp_na = Some(n);
            }
            "--cp-charges" => {
                let v = take(&args, &mut i, "--cp-charges")?;
                cp_charges = Some(parse_pair(&v, "--cp-charges")?);
            }
            "--cp-mults" => {
                let v = take(&args, &mut i, "--cp-mults")?;
                let (a, b): (u32, u32) = parse_pair(&v, "--cp-mults")?;
                if a == 0 || b == 0 {
                    return Err("--cp-mults multiplicities must be >= 1".into());
                }
                cp_mults = Some((a, b));
            }
            "--gcp" => gcp_keyword = Some(take(&args, &mut i, "--gcp")?.to_ascii_lowercase()),
            "--protocol" => {
                protocol = Some(take(&args, &mut i, "--protocol")?.to_ascii_lowercase())
            }
            "--no-method-warnings" => no_method_warnings = true,
            "--recommend" => {
                recommend_task = Some(take(&args, &mut i, "--recommend")?.to_ascii_lowercase())
            }
            "--fod" => fod = true,
            "--fod-cube" => fod_cube = Some(take(&args, &mut i, "--fod-cube")?),
            "--eps" => {
                eps = Some(
                    take(&args, &mut i, "--eps")?
                        .parse()
                        .map_err(|_| "--eps must be a number (dielectric constant)".to_string())?,
                )
            }
            "--ts-irc" => {
                ts_options.confirm_irc = true;
                ts_flag_seen = true;
            }
            f if ts_flags::TS_VALUE_FLAGS.contains(&f) => {
                let flag = args[i].clone();
                let value = take(&args, &mut i, &flag)?;
                ts_flags::apply_ts_option(&mut ts_options, &flag, &value)?;
                ts_flag_seen = true;
                // The IRC sub-flags only take effect once `--ts-irc` enables the trace.
                irc_subflag_seen |= flag.starts_with("--ts-irc-");
            }
            other if other.starts_with("--") => return Err(format!("unknown option {other}")),
            path => xyz_path = Some(path.to_string()),
        }
        i += 1;
    }

    if ts_product_path.is_some() && !do_ts {
        return Err(
            "--ts-product gives the product endpoint for a two-endpoint transition-state \
             search; add --ts to request the search"
                .into(),
        );
    }
    if ts_use_neb && ts_product_path.is_none() {
        return Err(
            "--ts-neb relaxes a band between two endpoints; provide the product geometry \
             with --ts-product <file.xyz>"
                .into(),
        );
    }
    if ts_neb_images.is_some() && !ts_use_neb {
        eprintln!("warning: --ts-neb-images is ignored without --ts-neb");
    }
    if ts_scan_points.is_some() && ts_product_path.is_none() {
        return Err(
            "--ts-scan places the guess at the energy peak between two endpoints; provide \
             the product with --ts-product <file.xyz>"
                .into(),
        );
    }
    if ts_scan_points.is_some() && ts_use_neb {
        eprintln!("warning: --ts-scan is ignored with --ts-neb (the band already finds the peak)");
    }
    if ts_scan_coord.is_some() && ts_product_path.is_some() {
        return Err(
            "--ts-scan-coord is a single-ended distinguished-coordinate scan; it is mutually \
             exclusive with the two-endpoint --ts-product route"
                .into(),
        );
    }
    if ts_flag_seen && !do_ts {
        eprintln!("warning: --ts-* flags are ignored without --ts");
    } else if irc_subflag_seen && !ts_options.confirm_irc {
        eprintln!("warning: --ts-irc-* flags are ignored without --ts-irc");
    }

    if let Some(task) = &recommend_task {
        return report_recommendation(task);
    }

    let xyz_path = xyz_path.ok_or("missing XYZ file argument (try --help)")?;
    let xyz = std::fs::read_to_string(&xyz_path).map_err(|e| format!("reading {xyz_path}: {e}"))?;
    let molecule = Molecule::from_xyz(&xyz)
        .map_err(|e| e.to_string())?
        .with_charge(charge)
        .with_multiplicity(multiplicity);
    molecule.validate().map_err(|e| e.to_string())?;

    // Two-endpoint transition-state input: the main XYZ is the reactant, --ts-product
    // is the product. A reaction conserves charge and multiplicity, so the product
    // takes the reactant's. The guess shares the reactant's composition.
    let ts_guess = if let Some(path) = &ts_product_path {
        let pxyz = std::fs::read_to_string(path).map_err(|e| format!("reading {path}: {e}"))?;
        let product = Molecule::from_xyz(&pxyz)
            .map_err(|e| e.to_string())?
            .with_charge(charge)
            .with_multiplicity(multiplicity);
        product.validate().map_err(|e| e.to_string())?;
        let mut input = hartree::TsGuessInput::new(product);
        input.use_neb = ts_use_neb;
        // Permute the product onto the reactant's atom order before building the band, the
        // same correspondence the IDPP route applies, so a product whose atoms are listed
        // in a different order than the reactant is handled identically on both routes.
        input.neb_options.map_atoms = true;
        if let Some(n) = ts_neb_images {
            input.neb_options.n_images = n;
        }
        // The energy-peaked scan applies only to the IDPP route; the band finds its own
        // peak, so it is left unset under --ts-neb.
        if !ts_use_neb {
            input.scan_points = ts_scan_points;
        }
        Some(input)
    } else {
        None
    };

    if do_sph && !do_freq {
        return Err("--sph modifies --freq; pass --freq as well".into());
    }

    let solvent_eps = match (&solvent, eps) {
        (Some(_), Some(_)) => {
            return Err(
                "--solvent and --eps are mutually exclusive (a named solvent already fixes ε)"
                    .into(),
            );
        }
        (Some(name), None) => Some(hartree::solv::solvent_epsilon(name).ok_or_else(|| {
            let names: Vec<&str> = hartree::solv::SOLVENTS.iter().map(|(n, _)| *n).collect();
            format!(
                "unknown solvent {name:?} (available: {}; or give --eps <value> directly)",
                names.join(", ")
            )
        })?),
        (None, Some(e)) => Some(e),
        (None, None) => None,
    };

    if smd.is_some() && (solvent.is_some() || eps.is_some()) {
        return Err(
            "--smd and --solvent/--eps are mutually exclusive (SMD fixes both the dielectric constant and the non-electrostatic parameterization)"
                .into(),
        );
    }
    if let Some(name) = &smd
        && hartree::solv::smd_solvent(name).is_none()
    {
        let names: Vec<&str> = hartree::solv::SMD_SOLVENTS.iter().map(|s| s.name).collect();
        return Err(format!(
            "unknown SMD solvent {name:?} (available: {})",
            names.join(", ")
        ));
    }

    let n_solv = [
        solvent.is_some() || eps.is_some(),
        smd.is_some(),
        alpb.is_some(),
        gbsa.is_some(),
        cosmo_file.is_some(),
    ]
    .iter()
    .filter(|&&x| x)
    .count();
    if n_solv > 1 {
        return Err(
            "the solvation options are mutually exclusive: choose at most one of \
             --solvent/--eps (C-PCM), --smd, --alpb, --gbsa, or --cosmo-file"
                .into(),
        );
    }
    if let Some(name) = &alpb
        && hartree::solv::alpb_solvent(name).is_none()
    {
        return Err(format!(
            "unknown ALPB solvent {name:?} (available: {})",
            hartree::solv::alpb_solvent_names().join(", ")
        ));
    }
    if let Some(name) = &gbsa
        && hartree::solv::gbsa_solvent(name).is_none()
    {
        return Err(format!(
            "unknown GBSA solvent {name:?} (available: {})",
            hartree::solv::gbsa_solvent_names().join(", ")
        ));
    }

    if let Some(name) = &protocol {
        if !matches!(name.as_str(), "w1" | "hartree-w1") {
            return Err(format!(
                "unknown protocol {name:?} (available: w1 — the hartree-W1 composite \
                 thermochemistry protocol)"
            ));
        }
        if method_explicit || basis_explicit {
            return Err(
                "--protocol w1 defines its own methods and bases (B3LYP/cc-pVTZ geometry \
                 + frequencies; CCSD(T)/cc-pVTZ; CCSD/cc-pVQZ; CBS extrapolation); drop \
                 --method/--basis"
                    .into(),
            );
        }
        if do_opt || do_freq {
            return Err(
                "--protocol w1 already optimizes the geometry and computes frequencies as \
                 protocol stages; drop --opt/--freq"
                    .into(),
            );
        }
        for (on, what) in [
            (direct, "--direct"),
            (ri, "--ri"),
            (ri_mp2, "--ri-mp2"),
            (cosx, "--cosx"),
            (x2c, "--x2c"),
            (fod, "--fod"),
            (smear.is_some(), "--smear"),
            (do_properties, "--properties"),
            (do_sph, "--sph"),
            (conformers.is_some(), "--conformers"),
            (rerank, "--rerank"),
            (cosmo_file.is_some(), "--cosmo-file"),
            (gcp_keyword.is_some(), "--gcp"),
            (cp_na.is_some(), "--cp"),
            (grid.is_some(), "--grid"),
            (solvent_eps.is_some(), "--solvent/--eps"),
            (smd.is_some(), "--smd"),
            (alpb.is_some(), "--alpb"),
            (gbsa.is_some(), "--gbsa"),
        ] {
            if on {
                return Err(format!(
                    "{what} is not supported with --protocol w1 (the protocol is a fixed \
                     gas-phase staged workflow); run the stages as separate jobs instead"
                ));
            }
        }
        let w1_opts = hartree::w1::W1Options {
            symmetry_number,
            qrrho_w0_cm1: qrrho_w0,
            all_electron,
            ..hartree::w1::W1Options::default()
        };
        let res = hartree::w1::run_w1(&molecule, &w1_opts)?;
        report_w1(&molecule, &w1_opts, &res);
        return Ok(true);
    }

    let ml_spec = hartree::multilevel::parse_spec(&method_str, multiplicity)?;
    if rerank && ml_spec.is_none() {
        return Err(
            "--rerank requires --conformers and a multi-level '//' --method spec \
             (e.g. --method \"r2scan-3c // hf/sto-3g\")"
                .into(),
        );
    }
    if let Some(spec) = ml_spec {
        if basis_explicit {
            return Err(
                "--basis conflicts with a multi-level '//' spec: each level carries its \
                 basis inside the spec itself (method/basis, or a composite keyword)"
                    .into(),
            );
        }
        if do_opt {
            return Err(
                "--opt is redundant with a multi-level '//' spec: the OPT level (right of \
                 '//') already optimizes the geometry; drop --opt"
                    .into(),
            );
        }
        for (on, what) in [
            (direct, "--direct"),
            (ri, "--ri"),
            (cosx, "--cosx"),
            (x2c, "--x2c"),
            (fod, "--fod"),
            (smear.is_some(), "--smear"),
            (do_properties, "--properties"),
            (do_sph, "--sph"),
            (cosmo_file.is_some(), "--cosmo-file"),
            (gcp_keyword.is_some(), "--gcp"),
            (cp_na.is_some(), "--cp"),
        ] {
            if on {
                return Err(format!(
                    "{what} is not supported with a multi-level '//' spec; \
                     run the stages as separate single-level jobs instead"
                ));
            }
        }
        if let Some(g) = grid
            && g > 4
        {
            return Err("--grid must be an integer 0..=4".into());
        }
        let ml_opts = hartree::multilevel::MultiLevelOptions {
            compute_frequencies: do_freq,
            symmetry_number,
            qrrho_w0_cm1: qrrho_w0,
            grid_override: grid,
            all_electron,
            ri_mp2,
            solvent_eps,
            smd: smd.clone(),
            alpb: alpb.clone(),
            gbsa: gbsa.clone(),
        };
        if let Some(engine) = &conformers {
            if !rerank {
                return Err(
                    "--conformers with a multi-level '//' spec needs --rerank (the CENSO-lite \
                     re-ranking hook); without it the spec is ambiguous for the screening step"
                        .into(),
                );
            }
            return run_multilevel_conformers(
                &molecule,
                &spec,
                &ml_opts,
                engine.as_deref(),
                alpb.clone(),
                conformers_out.as_deref(),
            );
        }
        let res = hartree::multilevel::run_multilevel(&molecule, &spec, &ml_opts)?;
        report_multilevel(&molecule, &spec, &res);
        if !no_method_warnings {
            let mut warnings = res.sp.method_warnings.clone();
            for w in &res.opt.method_warnings {
                if !warnings.contains(w) {
                    warnings.push(w.clone());
                }
            }
            print_method_warnings(&warnings);
        }
        return Ok(true);
    }

    if let Some(engine) = &conformers {
        return run_conformers(
            &molecule,
            &method_str,
            &basis,
            engine.as_deref(),
            alpb.clone(),
            conformers_out.as_deref(),
        );
    }

    if let Some(xtb_method) = hartree::ext::xtb::XtbMethod::from_keyword(&method_str) {
        return run_xtb(&molecule, &method_str, xtb_method, do_opt, alpb.clone());
    }

    if fod_cube.is_some() && !fod {
        return Err("--fod-cube requires --fod".into());
    }
    if fod {
        if method_explicit {
            if method_str != "tpss" {
                eprintln!(
                    "warning: FOD analysis is calibrated at TPSS/def2-TZVP (T_el = 5000 K); \
                     using --method {method_str} — interpret N_FOD thresholds with care"
                );
            }
        } else {
            method_str = String::from("tpss");
        }
        if basis_explicit {
            if !basis.eq_ignore_ascii_case("def2-tzvp") {
                eprintln!(
                    "warning: FOD analysis is calibrated at TPSS/def2-TZVP; using --basis {basis}"
                );
            }
        } else {
            basis = String::from("def2-tzvp");
        }
    }

    {
        let gcp_is_dftc = gcp_keyword
            .as_deref()
            .is_some_and(|k| k.replace('-', "") == "dftc");
        if method_str.replace('-', "").contains("dftc") || gcp_is_dftc {
            return Err(
                "DFT-C is not implemented: its parameter tables (the 1296 pairwise \
                 c_AB/alpha_AB/beta_AB values for H\u{2013}Kr plus the fitted atomic and \
                 gCP/def2-SVPD parameters) are published only in the AIP supplementary \
                 material of J. Chem. Phys. 146, 234105 (2017), which hartree cannot \
                 vendor. Obtain the SI zip in a browser (https://doi.org/10.1063/1.4986962) \
                 or email the corresponding author. The B97M-V/def2-SVPD NCI protocol \
                 runs without DFT-C: --method b97m-v --basis def2-svpd."
                    .into(),
            );
        }
    }

    let (base_method_str, want_disp) = if let Some(base) = method_str.strip_suffix("-d3") {
        (base.to_string(), Some(false))
    } else if let Some(base) = method_str.strip_suffix("-d4") {
        (base.to_string(), Some(true))
    } else {
        (method_str.clone(), None)
    };

    let composite = hartree::composite::composite(&base_method_str);
    if let Some(c) = composite {
        if want_disp.is_some() {
            return Err(format!(
                "{} defines its own {} and short-range corrections; a -d3/-d4 suffix is not                  allowed",
                c.keyword,
                c.dispersion.label()
            ));
        }
        if basis_explicit && !basis.eq_ignore_ascii_case(c.basis) {
            return Err(format!(
                "{} implies the {} basis; --basis {basis} conflicts \
                 (omit --basis or pass {})",
                c.keyword, c.basis_label, c.basis
            ));
        }
        basis = String::from(c.basis);
    }

    let method = if let Some(c) = composite {
        Method::Dft(FunctionalSpec::parse(c.functional).map_err(|e| e.to_string())?)
    } else {
        match base_method_str.as_str() {
            "rhf" => Method::Rhf,
            "uhf" => Method::Uhf,
            "hf" => {
                if multiplicity > 1 {
                    Method::Uhf
                } else {
                    Method::Rhf
                }
            }
            "rohf" => Method::Rohf,
            "mp2" => Method::Mp2,
            "ccsd" => Method::Ccsd,
            "ccsd(t)" | "ccsdt" => Method::CcsdT,
            other => match FunctionalSpec::parse(other) {
                Ok(spec) => Method::Dft(spec),
                Err(hartree::dft::DftError::UnknownFunctional(_)) => {
                    return Err(format!(
                        "unknown method {other:?} (expected hf, rhf, uhf, rohf, mp2, ccsd, ccsd(t), \
                     or a functional like svwn/pbe/blyp/b3lyp/pbe0/tpss/r2scan, \
                     optionally with a -d3 or -d4 suffix)"
                    ));
                }
                Err(err) => return Err(err.to_string()),
            },
        }
    };

    let dispersion = if let Some(c) = composite {
        Some(c.dispersion)
    } else if let Some(d4) = want_disp {
        let (suffix, model) = if d4 { ("-d4", "D4") } else { ("-d3", "D3(BJ)") };
        if matches!(method, Method::Mp2 | Method::Ccsd | Method::CcsdT) {
            return Err(format!(
                "{suffix} is not supported for post-HF methods ({base_method_str}); \
                 the {model} correction applies to HF and DFT functionals"
            ));
        }
        let param_key = match &method {
            Method::Rhf | Method::Uhf | Method::Rohf => "hf".to_string(),
            Method::Dft(spec) => spec
                .d4_param_set()
                .map(str::to_string)
                .unwrap_or_else(|| spec.name().to_string()),
            _ => unreachable!(),
        };
        match Dispersion::for_method(d4, &param_key) {
            Some(d) => Some(d),
            None => {
                return Err(format!(
                    "no {model} parametrization exists for {param_key} \
                     (supported: pbe, blyp, b3lyp, b3lyp5, pbe0, tpss, r2scan, hf; \n                     D4 additionally: b2plyp, revdsd-pbep86, pwpb95)"
                ));
            }
        }
    } else {
        None
    };

    let standalone_gcp = match &gcp_keyword {
        Some(k) => {
            if composite.is_some() {
                return Err(format!(
                    "{} defines its own gCP treatment; --gcp is not allowed with a composite \
                     method",
                    base_method_str
                ));
            }
            Some(hartree::disp::GcpParams::by_keyword(k).ok_or_else(|| {
                format!(
                    "unknown gCP parameter set {k:?} (available: r2scan-3c / def2-mtzvpp, \
                     dft/sv(p) / b3lyp-3c, pbeh-3c / def2-msvp; only the sets vendored from \
                     the reference implementation mctc-gcp are bundled)"
                )
            })?)
        }
        None => None,
    };

    if grid.is_some() && !matches!(method, Method::Dft(_)) {
        return Err("--grid requires a functional method (e.g. --method pbe)".into());
    }
    let metadata_grid = match &method {
        Method::Dft(spec) if grid.is_none() && spec.grid_sensitive() => {
            let level = spec.recommended_grid_level();
            eprintln!(
                "note: {} is grid-sensitive; defaulting to grid level {level} \
                 (override with --grid)",
                spec.name()
            );
            Some(level)
        }
        _ => None,
    };
    let grid_level = grid
        .or(metadata_grid)
        .unwrap_or(composite.map_or(3, |c| c.grid_level));
    if grid_level > 4 {
        return Err("--grid must be an integer 0..=4".into());
    }

    if do_opt && (solvent_eps.is_some() || smd.is_some()) {
        eprintln!("note: solvated geometry optimization uses finite-difference gradients (slower)");
    }

    if do_opt && do_freq {
        eprintln!(
            "note: --freq is not computed in the same run as --opt for single-level jobs; \
             re-run --freq at the printed optimized geometry (multi-level '//' specs and \
             --protocol w1 combine the two)"
        );
    }

    if !method_explicit && !fod && !no_method_warnings {
        eprintln!(
            "note: no --method given; defaulting to RHF (the historic default). For \
             production work r2scan-3c is recommended — see `hartree --recommend general` \
             (suppress this note with --no-method-warnings)"
        );
    }

    let job = Job {
        molecule: molecule.clone(),
        basis: basis.clone(),
        method,
        options: JobOptions {
            all_electron,
            direct,
            ri,
            compute_properties: do_properties,
            compute_frequencies: do_freq,
            single_point_hessian: do_sph,
            optimize_geometry: do_opt,
            transition_state: do_ts,
            symmetry_number,
            qrrho_w0_cm1: qrrho_w0,
            grid_level,
            dispersion,
            solvent_eps,
            smd: smd.clone(),
            alpb: alpb.clone(),
            gbsa: gbsa.clone(),
            cosmo_file: cosmo_file.as_ref().map(Into::into),
            gcp: composite.and_then(|c| c.gcp).or(standalone_gcp),
            srb: composite.and_then(|c| c.srb),
            smearing: smear.map(|temperature_k| Smearing::Fermi { temperature_k }),
            fod,
            fod_cube: fod_cube.as_ref().map(Into::into),
            ri_mp2,
            cosx,
            x2c,
            ts_options,
            ts_guess,
            ts_coord_scan: ts_scan_coord.clone(),
            // The CLI runs one job per process, so it leaves rayon on its global
            // pool and sets no in-process memory budget; both knobs exist for
            // library/embedding callers that drive several jobs in one process.
            n_threads: None,
            mem_budget_bytes: None,
        },
    };

    if let Some(na) = cp_na {
        if na >= molecule.len() {
            return Err(format!(
                "--cp {na}: fragment A must leave at least one atom for fragment B \
                 (the complex has {} atoms)",
                molecule.len()
            ));
        }
        let (qa, qb) = cp_charges.unwrap_or((0, 0));
        let (ma, mb) = cp_mults.unwrap_or((1, 1));
        let frags = hartree::CpFragments {
            fragment_a: (0..na).collect(),
            charge_a: qa,
            multiplicity_a: ma,
            charge_b: qb,
            multiplicity_b: mb,
        };
        let cp = hartree::counterpoise(&job, &frags)?;
        println!(
            "hartree -- counterpoise (Boys-Bernardi) -- {} / {}",
            method_str.to_ascii_uppercase(),
            basis
        );
        println!(
            "complex: {} atoms (charge {}, mult {});  fragment A: atoms 1-{na} \
             (charge {qa}, mult {ma});  fragment B: atoms {}-{} (charge {qb}, mult {mb})",
            molecule.len(),
            molecule.charge,
            molecule.multiplicity,
            na + 1,
            molecule.len()
        );
        println!();
        println!("  E_AB^(AB) (complex)      {:>20.12} Eh", cp.e_complex);
        println!(
            "  E_A^(AB)  (A + ghost-B)  {:>20.12} Eh",
            cp.e_a_in_dimer_basis
        );
        println!(
            "  E_B^(AB)  (ghost-A + B)  {:>20.12} Eh",
            cp.e_b_in_dimer_basis
        );
        println!("  E_A^(A)   (A alone)      {:>20.12} Eh", cp.e_a);
        println!("  E_B^(B)   (B alone)      {:>20.12} Eh", cp.e_b);
        println!();
        let to_kcal = 627.509474063;
        println!(
            "  dE_int (uncorrected)     {:>20.12} Eh  ({:>10.4} kcal/mol)",
            cp.interaction_uncorrected(),
            cp.interaction_uncorrected() * to_kcal
        );
        println!(
            "  dE_int^CP (corrected)    {:>20.12} Eh  ({:>10.4} kcal/mol)",
            cp.interaction_cp(),
            cp.interaction_cp() * to_kcal
        );
        println!(
            "  delta_BSSE               {:>20.12} Eh  ({:>10.4} kcal/mol)",
            cp.bsse(),
            cp.bsse() * to_kcal
        );
        return Ok(true);
    }
    if cp_charges.is_some() || cp_mults.is_some() {
        return Err("--cp-charges/--cp-mults require --cp <nA>".into());
    }

    let result = job.run()?;

    let ecp_atoms = hartree::BasisSet::load(&basis)
        .map(|set| hartree::ecp_summary(&molecule, &set))
        .unwrap_or_default();

    if let Some(opt) = &result.optimized_geometry {
        report_optimization(&molecule, &basis, &method_str, &ecp_atoms, opt);
        if !no_method_warnings {
            print_method_warnings(&result.method_warnings);
        }
        return Ok(opt.converged);
    }

    if let Some(ts) = &result.transition_state {
        if ts_product_path.is_some() {
            let route = if ts_use_neb {
                "climbing-image NEB band"
            } else if ts_scan_points.is_some() {
                "an energy-peaked IDPP path scan"
            } else {
                "IDPP interpolation"
            };
            println!(
                "note: two-endpoint search -- guess built by {route} between the reactant \
                 and --ts-product, then refined\n"
            );
            print_mapping_confidence(result.mapping_confidence.as_ref());
        } else if ts_scan_coord.is_some() {
            println!(
                "note: single-ended search -- guess built by a distinguished-coordinate scan \
                 (one internal coordinate driven, the rest relaxed), then refined\n"
            );
        }
        report_transition_state(&molecule, &basis, &method_str, &ecp_atoms, ts);
        if let Some(path) = &ts_output_path {
            write_ts_json(
                path,
                &molecule,
                &basis,
                &method_str,
                ts,
                result.mapping_confidence.as_ref(),
            );
        }
        if !no_method_warnings {
            print_method_warnings(&result.method_warnings);
        }
        return Ok(ts.converged());
    }

    report(
        &molecule,
        &basis,
        &method_str,
        &ecp_atoms,
        &result.scf,
        result.ri.as_ref(),
        result.cosx.as_ref(),
    );
    if x2c {
        println!();
        println!("X2C-1e scalar-relativistic Hamiltonian: active");
        println!(
            "  spin-free one-electron X2C (c = 137.035999084 a.u., CODATA 2018); \
             two-electron integrals nonrelativistic"
        );
        if do_properties {
            println!(
                "  note: property operators are NOT picture-change corrected (X2C-1e caveat); \
                 dipole moments and populations use the nonrelativistic operators"
            );
        }
    }
    if let Some(dft) = &result.dft {
        report_dft(dft, &result.scf);
    }
    if let (Some(e_nl), None) = (result.vv10_energy, &result.double_hybrid) {
        println!(
            "  E_nl (VV10, non-SC) {:>20.12} Eh   (post-SCF; included in total below)",
            e_nl
        );
        println!(
            "  total energy + E_nl {:>20.12} Eh",
            result.scf.energy + e_nl
        );
    }
    if let Some((occ_a, occ_b)) = &result.scf.occupations {
        let frac = |occ: &[f64]| occ.iter().filter(|&&f| f > 1e-6 && f < 1.0 - 1e-6).count();
        let ts = result.scf.electronic_entropy.unwrap_or(0.0);
        let free = result.scf.free_energy.unwrap_or(result.scf.energy);
        let t_el = smear
            .or(result.fod.as_ref().map(|f| f.temperature_k))
            .expect("occupations imply a smearing temperature");
        println!();
        println!("Fermi smearing (T = {:.1} K):", t_el);
        println!(
            "  fractionally occupied   {:>10} α  /  {:>5} β  (of {} orbitals each)",
            frac(occ_a),
            frac(occ_b),
            occ_a.len()
        );
        println!("  T*S_el              {:>20.12} Eh", ts);
        println!("  free energy F=E-T*S {:>20.12} Eh", free);
    }
    if let Some(f) = &result.fod {
        println!();
        println!("FOD analysis (Grimme):");
        println!(
            "  T_el                {:>20.1} K   (T = 5000 K + 20000 K * a_x)",
            f.temperature_k
        );
        println!(
            "  N_FOD               {:>20.6}     ({:.6} alpha, {:.6} beta)",
            f.n_fod, f.n_fod_alpha, f.n_fod_beta
        );
        if let Some(path) = &fod_cube {
            println!("  rho_FOD cube written to {path}");
        }
        if f.n_fod >= 1.0 {
            println!(
                "  WARNING: N_FOD = {:.3} >= 1.0 — strong static correlation \
                 (multireference character); single-reference results (DFT, MP2, CCSD(T)) \
                 may be unreliable for this system",
                f.n_fod
            );
        } else if f.n_fod >= 0.5 {
            println!(
                "  note: N_FOD = {:.3} is in the borderline 0.5–1.0 range — mild static \
                 correlation; inspect rho_FOD and frontier occupations",
                f.n_fod
            );
        } else {
            println!(
                "  N_FOD < 0.5: no significant static correlation indicated \
                 (single-reference methods are appropriate)"
            );
        }
    }
    if let Some(smd) = &result.smd {
        const KCAL: f64 = 627.509_451;
        println!();
        println!("SMD solvation (Marenich, Cramer, Truhlar 2009):");
        println!("  solvent             {:>20}", smd.solvent);
        println!("  dielectric ε        {:>20.4}", smd.epsilon);
        println!("  E (gas, this geom)  {:>20.12} Eh", smd.e_gas);
        println!(
            "  E (solution)        {:>20.12} Eh   (SCF total in solvent)",
            smd.e_solution
        );
        println!(
            "  ΔG_EP               {:>20.12} Eh   ({:>9.3} kcal/mol)",
            smd.g_ep,
            smd.g_ep * KCAL
        );
        println!(
            "  G_CDS               {:>20.12} Eh   ({:>9.3} kcal/mol)",
            smd.g_cds,
            smd.g_cds * KCAL
        );
        println!(
            "  ΔG_solv             {:>20.12} Eh   ({:>9.3} kcal/mol)",
            smd.dg_solv,
            smd.dg_solv * KCAL
        );
        println!(
            "  standard state: 298 K, fixed 1 mol/L in gas and solution (no concentration term)"
        );
    } else if let Some(g) = &result.gbsa {
        const KCAL: f64 = 627.509_451;
        println!();
        println!(
            "{} solvation (xtb GFN2 parameters; post-SCF on Mulliken charges):",
            g.model
        );
        println!("  solvent             {:>20}", g.solvent);
        println!("  dielectric ε        {:>20.4}", g.epsilon);
        println!(
            "  G_born              {:>20.12} Eh   ({:>9.3} kcal/mol)",
            g.g_born,
            g.g_born * KCAL
        );
        if g.g_hb != 0.0 {
            println!(
                "  G_hb                {:>20.12} Eh   ({:>9.3} kcal/mol)",
                g.g_hb,
                g.g_hb * KCAL
            );
        }
        println!(
            "  G_sasa              {:>20.12} Eh   ({:>9.3} kcal/mol)",
            g.g_sasa,
            g.g_sasa * KCAL
        );
        println!(
            "  G_shift             {:>20.12} Eh   ({:>9.3} kcal/mol)",
            g.g_shift,
            g.g_shift * KCAL
        );
        println!(
            "  ΔG_solv             {:>20.12} Eh   ({:>9.3} kcal/mol)",
            g.g_solv,
            g.g_solv * KCAL
        );
        println!(
            "  caveat: GFN2-fit parameters applied to ab-initio Mulliken charges (provenance \
             documented)"
        );
    } else if let Some(e_solv) = result.scf.solvation_energy {
        println!();
        if let Some(path) = &cosmo_file {
            println!("C-PCM (ideal conductor, ε = ∞) for COSMO-RS export:");
            println!(
                "  E_diel              {:>20.12} Eh   (included in total energy)",
                e_solv
            );
            println!("  .cosmo file written to {path}");
        } else {
            println!("C-PCM solvation (electrostatics only):");
            if let Some(name) = &solvent {
                println!("  solvent             {:>20}", name);
            }
            println!("  dielectric ε        {:>20.4}", solvent_eps.unwrap());
            println!(
                "  E_solv              {:>20.12} Eh   (included in total energy)",
                e_solv
            );
        }
    }
    if let Some(c) = composite {
        let e_disp = result
            .dispersion_energy
            .expect("every composite carries a dispersion correction");
        println!();
        println!("{} composite:", c.keyword);
        println!(
            "  {:<20}{:>20.12} Eh",
            format!("E_SCF ({})", c.functional),
            result.scf.energy
        );
        println!(
            "  {:<20}{:>20.12} Eh",
            format!("E_{}", c.disp_label),
            e_disp
        );
        if let (Some(e_gcp), Some(gcp)) = (result.gcp_energy, c.gcp) {
            println!(
                "  {:<20}{:>20.12} Eh",
                format!("E_gCP ({})", gcp.label),
                e_gcp
            );
        }
        if let Some(e_srb) = result.srb_energy {
            println!("  E_SRB               {:>20.12} Eh", e_srb);
        }
        println!(
            "  composite total     {:>20.12} Eh",
            result.scf.energy
                + e_disp
                + result.gcp_energy.unwrap_or(0.0)
                + result.srb_energy.unwrap_or(0.0)
        );
    } else if let (Some(e_disp), None) = (result.dispersion_energy, &result.double_hybrid) {
        let label = dispersion
            .expect("dispersion energy implies a model")
            .label();
        println!();
        println!("dispersion {label:<9}{:>20.12} Eh", e_disp);
        println!(
            "total energy + disp {:>20.12} Eh",
            result.scf.energy + e_disp
        );
    }
    if composite.is_none()
        && let (Some(e_gcp), Some(p)) = (result.gcp_energy, standalone_gcp)
    {
        println!();
        println!("gCP ({})  {:>20.12} Eh", p.label, e_gcp);
        println!(
            "total energy incl. corrections {:>20.12} Eh",
            result.best_energy()
        );
    }
    if let Some(dh) = &result.double_hybrid {
        println!();
        println!(
            "double hybrid {} (PT2 on {} orbitals):",
            dh.functional_name, dh.scf_functional_name
        );
        println!("  E_SCF (DH, no PT2)  {:>20.12} Eh", dh.e_scf);
        println!(
            "  E_PT2 os            {:>20.12} Eh   (c_os = {:.5} x {:.12})",
            dh.c_os * dh.e_os,
            dh.c_os,
            dh.e_os
        );
        println!(
            "  E_PT2 ss            {:>20.12} Eh   (c_ss = {:.5} x {:.12})",
            dh.c_ss * dh.e_ss,
            dh.c_ss,
            dh.e_ss
        );
        match &dh.pt2_aux_basis {
            Some(aux) => println!("  PT2 backend         {:>20}", format!("RI-MP2 ({aux})")),
            None => println!("  PT2 backend         {:>20}", "conventional MP2"),
        }
        println!("  frozen core         {:>20} orbitals", dh.n_frozen);
        if let Some(e_nl) = result.vv10_energy {
            println!("  E_nl (VV10 x {:.5}){:>20.12} Eh", dh.vv10_scale, e_nl);
        }
        if let Some(e_disp) = result.dispersion_energy {
            println!("  E_disp (D4)         {:>20.12} Eh", e_disp);
        }
        println!("  total energy        {:>20.12} Eh", result.best_energy());
    }
    if let Some(post) = &result.post_hf {
        match post {
            PostHfResult::Mp2 {
                result: r,
                n_frozen,
            } => report_mp2(*n_frozen, r, result.scf.reference),
            PostHfResult::RiMp2 {
                result: r,
                n_frozen,
                aux_basis,
            } => report_ri_mp2(*n_frozen, r, aux_basis, result.scf.reference),
            PostHfResult::Ccsd {
                result: r,
                n_frozen,
            } => report_ccsd(*n_frozen, r),
            PostHfResult::CcsdT {
                result: r,
                n_frozen,
            } => report_ccsdt(*n_frozen, r),
        }
    }
    if let Some(props) = &result.properties {
        report_properties(&molecule, props.dipole_au, &props.population);
    }
    if let Some(freq) = &result.frequencies {
        if freq.is_sph {
            println!();
            println!(
                "NOTE: single-point Hessian (SPH) — geometry taken as-is, gradient direction \
                 projected out (Spicher & Grimme 2021). SPH frequencies are APPROXIMATE."
            );
        }
        report_frequencies(&freq.frequencies);
        report_thermo(&freq.thermochemistry);
    }
    if !no_method_warnings {
        print_method_warnings(&result.method_warnings);
    }

    Ok(result.converged())
}

fn print_method_warnings(warnings: &[String]) {
    if warnings.is_empty() {
        return;
    }
    println!();
    println!("method-quality assessment (informational; suppress with --no-method-warnings):");
    for w in warnings {
        println!("  {w}");
    }
}

fn report_recommendation(task: &str) -> Result<bool, String> {
    let rec = hartree::guardrails::recommend(task).ok_or_else(|| {
        format!(
            "unknown --recommend task {task:?} (available: {})",
            hartree::guardrails::recommendation_tasks().join(", ")
        )
    })?;
    println!("hartree -- recommended level of theory: {}", rec.task);
    println!();
    println!("  level:     {}", rec.level);
    println!("  rationale: {}", rec.rationale);
    println!();
    println!("  run:");
    for inv in rec.invocation {
        println!("    {inv}");
    }
    if !rec.notes.is_empty() {
        println!();
        for note in rec.notes {
            println!("  note: {note}");
        }
    }
    Ok(true)
}

fn parse_pair<T: std::str::FromStr>(value: &str, flag: &str) -> Result<(T, T), String> {
    let mut parts = value.split(',');
    let err = || format!("{flag} expects two comma-separated values, e.g. {flag} 0,1");
    let a = parts.next().ok_or_else(err)?.trim();
    let b = parts.next().ok_or_else(err)?.trim();
    if parts.next().is_some() {
        return Err(err());
    }
    Ok((a.parse().map_err(|_| err())?, b.parse().map_err(|_| err())?))
}

pub(crate) fn take(args: &[String], i: &mut usize, flag: &str) -> Result<String, String> {
    *i += 1;
    args.get(*i)
        .cloned()
        .ok_or_else(|| format!("{flag} needs a value"))
}

const GAP_WARN_HF: f64 = 0.1;
const GAP_WARN_KS: f64 = 0.05;

const T1_WARN: f64 = 0.02;

fn report(
    molecule: &Molecule,
    basis: &str,
    method: &str,
    ecp_atoms: &[(String, u32, u32)],
    result: &ScfResult,
    ri: Option<&hartree::RiDiagnostics>,
    cosx: Option<&hartree::CosxDiagnostics>,
) {
    println!("hartree -- {} / {}", method.to_ascii_uppercase(), basis);
    println!(
        "atoms: {}   charge: {}   multiplicity: {}   electrons: {} ({} alpha, {} beta)   basis fns: {}",
        molecule.len(),
        molecule.charge,
        molecule.multiplicity,
        result.n_alpha + result.n_beta,
        result.n_alpha,
        result.n_beta,
        result.n_basis,
    );
    print_ecp_line(ecp_atoms);
    if let Some(ri) = ri {
        println!(
            "RI-JK density fitting: aux basis {} ({} aux fns)",
            ri.aux_basis, ri.naux
        );
    }
    if let Some(cosx) = cosx {
        let rs_note = match cosx.rs_omega {
            Some(omega) => format!(", RS Coulomb + erf(omega={omega}) kernels"),
            None => String::new(),
        };
        println!(
            "COSX semi-numerical exchange: grid {} ({} points{}{})",
            cosx.grid,
            cosx.n_points,
            if cosx.overlap_fitted {
                ", overlap-fitted"
            } else {
                ""
            },
            rs_note
        );
    }
    println!("nuclear repulsion: {:.10} Eh", result.nuclear_repulsion);
    println!();

    println!("{:>5}  {:>22}  {:>11}", "iter", "energy / Eh", "|error|");
    for step in &result.history {
        println!(
            "{:>5}  {:>22.12}  {:>11.2e}",
            step.iteration, step.energy, step.error_norm
        );
    }
    println!();

    if result.converged {
        println!("converged in {} iterations", result.iterations);
    } else {
        println!("NOT CONVERGED after {} iterations", result.iterations);
    }
    println!();
    println!("total energy        {:>20.12} Eh", result.energy);
    println!("electronic          {:>20.12} Eh", result.electronic_energy);
    println!("nuclear repulsion   {:>20.12} Eh", result.nuclear_repulsion);
    if result.reference != Reference::Rhf {
        println!("<S^2>               {:>20.6}", result.spin_squared);
    }

    let (gap_alpha, gap_beta) = result.homo_lumo_gap();
    let is_ks = result.xc_energy.is_some();
    let threshold = if is_ks { GAP_WARN_KS } else { GAP_WARN_HF };
    if result.reference == Reference::Uhf {
        if let Some(g) = gap_alpha {
            println!("HOMO-LUMO gap (α)   {:>20.6} Eh", g);
        }
        if let Some(g) = gap_beta {
            println!("HOMO-LUMO gap (β)   {:>20.6} Eh", g);
        }
    } else if let Some(g) = gap_alpha {
        println!("HOMO-LUMO gap       {:>20.6} Eh", g);
    }
    for (label, gap) in [("α", gap_alpha), ("β", gap_beta)] {
        if let Some(g) = gap {
            if g < threshold {
                println!(
                    "warning: small HOMO-LUMO gap ({g:.4} Eh < {threshold} Eh{}) — possible \
                     multi-reference character or SCF instability",
                    if result.reference == Reference::Uhf {
                        format!(", {label} channel")
                    } else {
                        String::new()
                    }
                );
                if result.reference != Reference::Uhf {
                    break; // α and β are the same channel; warn once
                }
            }
        }
    }
}

fn report_dft(dft: &DftDiagnostics, scf: &ScfResult) {
    let ks = if scf.reference == Reference::Uhf {
        "UKS"
    } else {
        "RKS"
    };
    println!();
    println!("Kohn-Sham DFT: {} ({})", dft.functional_name, ks);
    println!(
        "  grid level {}   ({} points)",
        dft.grid_level, dft.n_grid_points
    );
    if let Some(exc) = scf.xc_energy {
        println!("  E_xc                {:>20.12} Eh", exc);
    }
    if let Some(ne) = scf.n_elec_grid {
        println!(
            "  grid electrons ∫ρ   {:>20.10}   (N = {})",
            ne,
            scf.n_alpha + scf.n_beta
        );
    }
    if dft.exx_fraction > 0.0 {
        println!("  exact exchange c_x  {:>20.4}", dft.exx_fraction);
    }
}

fn report_mp2(n_frozen: usize, mp2: &hartree::cc::Mp2Result, reference: hartree::scf::Reference) {
    let kind = if reference == hartree::scf::Reference::Uhf {
        "UHF"
    } else {
        "RHF"
    };
    println!();
    println!(
        "{kind}-MP2  (frozen core: {} orbital{})",
        n_frozen,
        if n_frozen == 1 { "" } else { "s" }
    );
    println!("  opposite-spin       {:>20.12} Eh", mp2.opposite_spin);
    println!("  same-spin           {:>20.12} Eh", mp2.same_spin);
    println!("  correlation energy  {:>20.12} Eh", mp2.correlation_energy);
    println!("  total energy        {:>20.12} Eh", mp2.total_energy);
}

fn report_ri_mp2(
    n_frozen: usize,
    mp2: &hartree::cc::RiMp2Result,
    aux_basis: &str,
    reference: hartree::scf::Reference,
) {
    let kind = if reference == hartree::scf::Reference::Uhf {
        "UHF"
    } else {
        "RHF"
    };
    println!();
    println!(
        "{kind}-RI-MP2  (frozen core: {} orbital{})",
        n_frozen,
        if n_frozen == 1 { "" } else { "s" }
    );
    println!(
        "  aux basis (MP2-fit) {:>20}   ({} aux fns)",
        aux_basis, mp2.naux
    );
    println!("  opposite-spin       {:>20.12} Eh", mp2.opposite_spin);
    println!("  same-spin           {:>20.12} Eh", mp2.same_spin);
    println!("  correlation energy  {:>20.12} Eh", mp2.correlation_energy);
    println!("  total energy        {:>20.12} Eh", mp2.total_energy);
}

fn report_ccsd(n_frozen: usize, cc: &hartree::cc::CcsdResult) {
    println!();
    println!(
        "RHF-CCSD (spin-adapted; frozen core: {} orbital{})",
        n_frozen,
        if n_frozen == 1 { "" } else { "s" }
    );
    if cc.converged {
        println!("  converged in {} iterations", cc.iterations);
    } else {
        println!("  NOT CONVERGED after {} iterations", cc.iterations);
    }
    println!("  MP2 (iter-0) check  {:>20.12} Eh", cc.mp2_correlation);
    println!("  correlation energy  {:>20.12} Eh", cc.correlation_energy);
    println!("  total energy        {:>20.12} Eh", cc.total_energy);
    println!("  T1 diagnostic       {:>20.6}", cc.t1_diagnostic);
    if cc.t1_diagnostic > T1_WARN {
        println!(
            "warning: T1 diagnostic {:.4} > {T1_WARN} — significant singles amplitudes; \
             a single-reference CC treatment may be unreliable",
            cc.t1_diagnostic
        );
    }
}

fn report_ccsdt(n_frozen: usize, r: &hartree::cc::CcsdTResult) {
    report_ccsd(n_frozen, &r.ccsd);
    println!();
    println!(
        "RHF-CCSD(T)  (frozen core: {} orbital{})",
        n_frozen,
        if n_frozen == 1 { "" } else { "s" }
    );
    println!("  triples correction  {:>20.12} Eh", r.triples_energy);
    println!("  total energy        {:>20.12} Eh", r.total_energy);
}

fn print_ecp_line(ecp_atoms: &[(String, u32, u32)]) {
    if ecp_atoms.is_empty() {
        return;
    }
    let list = ecp_atoms
        .iter()
        .map(|(sym, z, n_core)| format!("{sym} (Z={z}, {n_core} core electrons replaced)"))
        .collect::<Vec<_>>()
        .join(", ");
    println!("effective core potentials (def2-ECP): {list}");
}

fn report_optimization(
    molecule: &Molecule,
    basis: &str,
    method: &str,
    ecp_atoms: &[(String, u32, u32)],
    result: &OptResult,
) {
    println!(
        "hartree -- {} / {}   [geometry optimization]",
        method.to_ascii_uppercase(),
        basis
    );
    println!(
        "atoms: {}   charge: {}   multiplicity: {}",
        molecule.len(),
        molecule.charge,
        molecule.multiplicity,
    );
    print_ecp_line(ecp_atoms);
    println!();

    println!(
        "{:>5}  {:>22}  {:>11}  {:>11}  {:>11}",
        "step", "energy / Eh", "max force", "rms force", "max disp"
    );
    for step in &result.history {
        println!(
            "{:>5}  {:>22.12}  {:>11.2e}  {:>11.2e}  {:>11.2e}",
            step.iteration, step.energy, step.max_force, step.rms_force, step.max_disp
        );
    }
    println!();

    if result.converged {
        println!("optimization converged in {} steps", result.iterations);
    } else {
        println!(
            "optimization NOT CONVERGED after {} steps",
            result.iterations
        );
    }
    println!();

    println!("optimized geometry (angstrom):");
    for (atom, pos) in molecule.atoms.iter().zip(&result.positions) {
        println!(
            "{:<2}  {:>14.8}  {:>14.8}  {:>14.8}",
            atom.element.symbol(),
            pos[0] / ANGSTROM_TO_BOHR,
            pos[1] / ANGSTROM_TO_BOHR,
            pos[2] / ANGSTROM_TO_BOHR,
        );
    }
    println!();
    println!("total energy        {:>20.12} Eh", result.energy);
}

/// Print a one-line warning when a two-endpoint atom mapping was ambiguous (symmetric or
/// equivalent atoms it could not uniquely resolve), so the user knows the reaction
/// coordinate and guess may rest on an arbitrary choice among interchangeable atoms.
/// Threshold mirrors a "less than fully unique" mapping; a confident or absent mapping
/// prints nothing.
fn print_mapping_confidence(confidence: Option<&hartree::opt::ts::guess::MappingConfidence>) {
    if let Some(c) = confidence {
        if c.confidence < 1.0 {
            println!(
                "note: atom mapping confidence low ({:.2}); {} atom(s) ambiguous \
                 (symmetric/equivalent atoms) -- inspect the mapping if the guess looks wrong\n",
                c.confidence,
                c.ambiguous.len()
            );
        }
    }
}

fn report_transition_state(
    molecule: &Molecule,
    basis: &str,
    method: &str,
    ecp_atoms: &[(String, u32, u32)],
    result: &TsResult,
) {
    println!(
        "hartree -- {} / {}   [transition-state search]",
        method.to_ascii_uppercase(),
        basis
    );
    println!(
        "atoms: {}   charge: {}   multiplicity: {}",
        molecule.len(),
        molecule.charge,
        molecule.multiplicity,
    );
    print_ecp_line(ecp_atoms);
    println!();

    println!(
        "{:>5}  {:>22}  {:>11}  {:>11}  {:>11}",
        "step", "energy / Eh", "max force", "rms force", "max disp"
    );
    for step in &result.history {
        println!(
            "{:>5}  {:>22.12}  {:>11.2e}  {:>11.2e}  {:>11.2e}",
            step.iteration, step.energy, step.max_force, step.rms_force, step.max_disp
        );
    }
    println!();

    match result.status {
        TsStatus::Converged => {
            println!("transition state converged in {} steps", result.iterations)
        }
        TsStatus::NotConverged => println!(
            "transition-state search NOT CONVERGED after {} steps",
            result.iterations
        ),
        TsStatus::WrongImaginaryModeCount => println!(
            "geometry converged in {} steps, but it is NOT a first-order saddle \
             (wrong number of imaginary modes)",
            result.iterations
        ),
        TsStatus::StoppedEarly => {
            println!("transition-state search stopped early by an observer")
        }
        _ => println!("transition-state search finished with an unrecognized status"),
    }

    if !result.converged() {
        if let Some(reason) = &result.diagnostic {
            println!("reason: {reason}");
        }
    }

    if let Some(v) = &result.verification {
        let n_neg = v.negative_eigenvalues.len();
        println!(
            "saddle check: {} imaginary mode{}",
            n_neg,
            if n_neg == 1 { "" } else { "s" }
        );
        if let Some(freq) = v.imaginary_frequency_cm1 {
            println!("  imaginary frequency {:>13.2} cm^-1", freq);
        }
    }
    println!();

    println!("transition-state geometry (angstrom):");
    for (atom, pos) in molecule.atoms.iter().zip(&result.positions) {
        println!(
            "{:<2}  {:>14.8}  {:>14.8}  {:>14.8}",
            atom.element.symbol(),
            pos[0] / ANGSTROM_TO_BOHR,
            pos[1] / ANGSTROM_TO_BOHR,
            pos[2] / ANGSTROM_TO_BOHR,
        );
    }
    println!();
    println!("total energy        {:>20.12} Eh", result.energy);

    if let Some(irc) = &result.irc {
        println!();
        println!(
            "IRC endpoint confirmation (mass-weighted downhill trace along the reaction mode):"
        );
        let df = irc.forward_energy - result.energy;
        let dr = irc.reverse_energy - result.energy;
        let tag = |converged: bool, steps: usize| {
            if converged {
                format!("minimum, {steps} steps")
            } else {
                format!("step cap, {steps} steps")
            }
        };
        println!(
            "  forward endpoint energy  {:>20.12} Eh   ΔE = {:>9.2e} Eh   ({})",
            irc.forward_energy,
            df,
            tag(irc.forward_converged, irc.forward_steps)
        );
        println!(
            "  reverse endpoint energy  {:>20.12} Eh   ΔE = {:>9.2e} Eh   ({})",
            irc.reverse_energy,
            dr,
            tag(irc.reverse_converged, irc.reverse_steps)
        );
        if df <= 0.0 && dr <= 0.0 {
            println!("  both endpoints relaxed below the saddle (ΔE <= 0).");
        } else {
            println!(
                "  note: an endpoint did not relax below the saddle (ΔE > 0); \
                 inspect the geometry."
            );
        }
    }
}

/// One atom in the serialized transition-state geometry: its element symbol and
/// Cartesian coordinates in both angstrom and bohr (atomic units), so a consumer
/// need not know hartree's internal unit convention.
#[derive(Serialize)]
struct TsJsonAtom {
    element: String,
    xyz_angstrom: [f64; 3],
    xyz_bohr: [f64; 3],
}

/// The harmonic-verification summary of a serialized transition state: the
/// imaginary frequency (cm^-1, negative by the usual convention) and the number of
/// negative (imaginary) Hessian modes. Present only when the post-convergence
/// verification ran.
#[derive(Serialize)]
struct TsJsonVerification {
    imaginary_frequency_cm1: Option<f64>,
    n_imaginary_modes: usize,
}

/// The two-endpoint IRC summary of a serialized transition state: the relaxed
/// forward/reverse endpoint energies (Eh), whether each reached a minimum, and the
/// step counts. Present only when an IRC trace ran.
#[derive(Serialize)]
struct TsJsonIrc {
    forward_energy_eh: f64,
    forward_converged: bool,
    forward_steps: usize,
    reverse_energy_eh: f64,
    reverse_converged: bool,
    reverse_steps: usize,
}

/// Machine-readable record of a transition-state search, written by `--ts-output`.
/// A thin wrapper over [`TsResult`] that fixes units explicitly and folds in the
/// CLI-side context (level of theory, atom-mapping confidence) the solver does not
/// carry.
#[derive(Serialize)]
struct TsJson {
    method: String,
    basis: String,
    status: String,
    converged: bool,
    energy_eh: f64,
    n_steps: usize,
    #[serde(skip_serializing_if = "Option::is_none")]
    reason: Option<String>,
    geometry: Vec<TsJsonAtom>,
    #[serde(skip_serializing_if = "Option::is_none")]
    verification: Option<TsJsonVerification>,
    #[serde(skip_serializing_if = "Option::is_none")]
    irc: Option<TsJsonIrc>,
    #[serde(skip_serializing_if = "Option::is_none")]
    mapping_confidence: Option<f64>,
}

/// A short, stable string for a [`TsStatus`], for the JSON `status` field.
fn ts_status_str(status: TsStatus) -> &'static str {
    match status {
        TsStatus::Converged => "converged",
        TsStatus::NotConverged => "not_converged",
        TsStatus::WrongImaginaryModeCount => "wrong_imaginary_mode_count",
        TsStatus::StoppedEarly => "stopped_early",
        _ => "unknown",
    }
}

/// Serialize a completed transition-state search to `path` as JSON. A write failure
/// is reported but does not fail an otherwise-successful run.
fn write_ts_json(
    path: &str,
    molecule: &Molecule,
    basis: &str,
    method: &str,
    result: &TsResult,
    mapping_confidence: Option<&hartree::opt::ts::guess::MappingConfidence>,
) {
    let geometry: Vec<TsJsonAtom> = molecule
        .atoms
        .iter()
        .zip(&result.positions)
        .map(|(atom, pos)| TsJsonAtom {
            element: atom.element.symbol().to_string(),
            xyz_angstrom: [
                pos[0] / ANGSTROM_TO_BOHR,
                pos[1] / ANGSTROM_TO_BOHR,
                pos[2] / ANGSTROM_TO_BOHR,
            ],
            xyz_bohr: *pos,
        })
        .collect();

    let verification = result.verification.as_ref().map(|v| TsJsonVerification {
        imaginary_frequency_cm1: v.imaginary_frequency_cm1,
        n_imaginary_modes: v.negative_eigenvalues.len(),
    });

    let irc = result.irc.as_ref().map(|i| TsJsonIrc {
        forward_energy_eh: i.forward_energy,
        forward_converged: i.forward_converged,
        forward_steps: i.forward_steps,
        reverse_energy_eh: i.reverse_energy,
        reverse_converged: i.reverse_converged,
        reverse_steps: i.reverse_steps,
    });

    let record = TsJson {
        method: method.to_string(),
        basis: basis.to_string(),
        status: ts_status_str(result.status).to_string(),
        converged: result.converged(),
        energy_eh: result.energy,
        n_steps: result.iterations,
        reason: result.diagnostic.clone(),
        geometry,
        verification,
        irc,
        mapping_confidence: mapping_confidence.map(|c| c.confidence),
    };

    match serde_json::to_string_pretty(&record) {
        Ok(mut json) => {
            json.push('\n');
            if let Err(e) = std::fs::write(path, json) {
                eprintln!("warning: could not write --ts-output {path}: {e}");
            } else {
                println!("wrote machine-readable transition-state result to {path}");
            }
        }
        Err(e) => eprintln!("error: could not serialize transition-state result: {e}"),
    }
}

fn report_properties(molecule: &Molecule, mu_au: [f64; 3], pop: &PopulationAnalysis) {
    println!();
    println!("one-electron properties:");
    let mu_debye: [f64; 3] = mu_au.map(|x| x * AU_DIPOLE_TO_DEBYE);
    let mu_mag = mu_debye.iter().map(|x| x * x).sum::<f64>().sqrt();
    println!(
        "  dipole moment:  x = {:>10.6} D   y = {:>10.6} D   z = {:>10.6} D",
        mu_debye[0], mu_debye[1], mu_debye[2]
    );
    println!("  |μ|            {:>10.6} Debye", mu_mag);
    println!();
    println!("  atom  element      Mulliken      Löwdin");
    for (i, atom) in molecule.atoms.iter().enumerate() {
        println!(
            "  {:>4}  {:>7}    {:>10.6}    {:>10.6}",
            i + 1,
            atom.element.symbol(),
            pop.mulliken_charges[i],
            pop.lowdin_charges[i],
        );
    }
    println!();
    println!("  Mayer bond orders:");
    let n = molecule.len();
    for i in 0..n {
        for j in i + 1..n {
            let b = pop.mayer_bond_orders[i][j];
            if b > 0.1 {
                println!(
                    "    {:>2}({}) – {:>2}({})   {:>8.4}",
                    i + 1,
                    molecule.atoms[i].element.symbol(),
                    j + 1,
                    molecule.atoms[j].element.symbol(),
                    b
                );
            }
        }
    }
}

fn run_xtb(
    molecule: &Molecule,
    method_str: &str,
    method: hartree::ext::xtb::XtbMethod,
    do_opt: bool,
    alpb: Option<String>,
) -> Result<bool, String> {
    use hartree::ext::xtb::{XtbInput, XtbRun, run};
    let run_kind = if do_opt { XtbRun::Opt } else { XtbRun::Energy };
    let input = XtbInput::from_molecule(method, molecule, alpb);
    let workdir = std::env::temp_dir().join(format!("hartree_xtb_{}", std::process::id()));
    let result = run(molecule, &input, run_kind, &workdir).map_err(|e| e.to_string())?;
    println!(
        "hartree -- {} (external xtb)",
        method_str.to_ascii_uppercase()
    );
    println!(
        "atoms: {}   charge: {}   multiplicity: {}",
        molecule.len(),
        molecule.charge,
        molecule.multiplicity
    );
    println!();
    println!("total energy        {:>20.12} Eh", result.energy);
    if let Some(opt) = &result.optimized {
        println!();
        println!("optimized geometry (angstrom):");
        for atom in &opt.atoms {
            println!(
                "{:<2}  {:>14.8}  {:>14.8}  {:>14.8}",
                atom.element.symbol(),
                atom.position[0] / ANGSTROM_TO_BOHR,
                atom.position[1] / ANGSTROM_TO_BOHR,
                atom.position[2] / ANGSTROM_TO_BOHR,
            );
        }
    }
    Ok(true)
}

fn run_conformers(
    molecule: &Molecule,
    method_str: &str,
    basis: &str,
    engine: Option<&str>,
    alpb: Option<String>,
    out_path: Option<&str>,
) -> Result<bool, String> {
    use hartree::ext::ensemble::Ensemble;

    let ensemble: Ensemble = if engine == Some("crest") {
        use hartree::ext::crest::{CrestInput, run};
        let method = hartree::ext::xtb::XtbMethod::from_keyword(method_str)
            .unwrap_or(hartree::ext::xtb::XtbMethod::Gfn2Xtb);
        let input = CrestInput::from_molecule(method, molecule, alpb);
        let workdir = std::env::temp_dir().join(format!("hartree_crest_{}", std::process::id()));
        println!(
            "hartree -- conformer ensemble (external CREST, {})",
            method.keyword()
        );
        run(molecule, &input, &workdir).map_err(|e| e.to_string())?
    } else {
        use hartree::ext::confgen::{ConfGenOptions, generate_conformers};
        let method = simple_method(method_str)?;
        let opts = ConfGenOptions::default();
        println!(
            "hartree -- conformer ensemble (fallback torsion-driving generator, {} / {})",
            method_str.to_ascii_uppercase(),
            basis
        );
        let res = generate_conformers(molecule, &opts, |m| {
            let job = Job {
                molecule: m.clone(),
                basis: basis.to_string(),
                method: method.clone(),
                options: JobOptions::default(),
            };
            job.run().ok().and_then(|r| {
                if r.converged() {
                    Some(r.best_energy())
                } else {
                    None
                }
            })
        })
        .map_err(|e| e.to_string())?;
        println!(
            "rotatable bonds: {}   driven: {}   candidates: {}   screened: {}",
            res.rotatable_bonds.len(),
            res.driven_bonds.len(),
            res.n_candidates,
            res.n_screened
        );
        res.ensemble
    };

    if ensemble.is_empty() {
        return Err("conformer search produced no valid conformers".into());
    }
    let rel_kcal = ensemble.relative_energies_kcal();
    let weights = ensemble.boltzmann_weights(298.15);
    println!();
    println!(
        "ensemble ({} conformers, Boltzmann weights at 298.15 K):",
        ensemble.len()
    );
    println!(
        "  {:>4}  {:>20}  {:>12}  {:>10}",
        "rank", "energy / Eh", "ΔE / kcal", "weight"
    );
    for (i, c) in ensemble.conformers.iter().enumerate() {
        println!(
            "  {:>4}  {:>20.12}  {:>12.4}  {:>10.4}",
            i + 1,
            c.energy,
            rel_kcal[i],
            weights[i]
        );
    }

    if let Some(path) = out_path {
        let mut text = String::new();
        for c in &ensemble.conformers {
            text.push_str(
                &hartree::ext::xyz::write_xyz(&c.molecule, &format!("{:.12}", c.energy))
                    .map_err(|e| e.to_string())?,
            );
        }
        std::fs::write(path, text).map_err(|e| format!("writing {path}: {e}"))?;
        println!();
        println!("ensemble written to {path}");
    }
    Ok(true)
}

fn report_w1(molecule: &Molecule, opts: &hartree::w1::W1Options, res: &hartree::w1::W1Result) {
    println!("hartree -- hartree-W1 composite thermochemistry protocol");
    println!("(a W1-STYLE protocol from hartree's native methods -- NOT literal W1/W1-F12)");
    println!(
        "atoms: {}   charge: {}   multiplicity: {}",
        molecule.len(),
        molecule.charge,
        molecule.multiplicity
    );
    println!();
    println!("protocol stages:");
    println!("  1. geometry + frequencies   {}", res.opt_label);
    println!(
        "  2. HF/CBS                   {} / {}  (Karton-Martin 2-pt, E(L) = E_CBS + A(L+1)e^(-9*sqrt(L)))",
        res.basis_small, res.basis_large
    );
    println!(
        "  3. CCSD corr/CBS            {} / {}  (Halkier 2-pt n^-3), frozen core: {}",
        res.basis_small, res.basis_large, res.n_frozen
    );
    println!(
        "  4. (T) additivity           {} (unextrapolated)",
        res.basis_small
    );
    println!("  omitted (documented): core-valence correlation, scalar relativity, spin-orbit");
    println!();
    println!("optimized geometry (angstrom):");
    for atom in &res.geometry.atoms {
        println!(
            "{:<2}  {:>14.8}  {:>14.8}  {:>14.8}",
            atom.element.symbol(),
            atom.position[0] / ANGSTROM_TO_BOHR,
            atom.position[1] / ANGSTROM_TO_BOHR,
            atom.position[2] / ANGSTROM_TO_BOHR,
        );
    }
    println!();
    println!("hartree-W1 energy breakdown:");
    println!(
        "  E_opt   ({:<})        {:>20.12} Eh   (at its optimized geometry)",
        res.opt_label, res.e_opt
    );
    println!(
        "  E_HF    ({:<})            {:>20.12} Eh",
        res.basis_small, res.e_hf_small
    );
    println!(
        "  E_HF    ({:<})            {:>20.12} Eh",
        res.basis_large, res.e_hf_large
    );
    println!(
        "  E_HF/CBS                       {:>20.12} Eh",
        res.e_hf_cbs
    );
    println!(
        "  E_corr(CCSD, {:<})        {:>20.12} Eh",
        res.basis_small, res.e_ccsd_corr_small
    );
    println!(
        "  E_corr(CCSD, {:<})        {:>20.12} Eh",
        res.basis_large, res.e_ccsd_corr_large
    );
    println!(
        "  E_corr(CCSD)/CBS               {:>20.12} Eh",
        res.e_ccsd_corr_cbs
    );
    println!(
        "  E_(T)   ({:<})            {:>20.12} Eh",
        res.basis_small, res.e_t_small
    );
    println!(
        "  E(hartree-W1)                    {:>20.12} Eh   = E_HF/CBS + E_corr/CBS + E_(T)",
        res.electronic_energy()
    );

    if let Some(t) = &res.thermo {
        report_frequencies(&t.freq.frequencies);
        report_thermo(&t.freq.thermochemistry);
        println!();
        println!(
            "hartree-W1 thermochemistry (thermal corrections at {}, sigma = {}):",
            res.opt_label, opts.symmetry_number
        );
        println!("  H_corr(low) = H-E     {:>20.12} Eh", t.h_corr);
        println!("  G_corr(low) = G-E     {:>20.12} Eh   (RRHO)", t.g_corr);
        println!("  G_corr(low, mRRHO)    {:>20.12} Eh", t.g_corr_qrrho);
        println!(
            "  H(hartree-W1)           {:>20.12} Eh   = E + H_corr(low)",
            t.enthalpy
        );
        println!(
            "  G(hartree-W1, RRHO)     {:>20.12} Eh   = E + G_corr(low)",
            t.gibbs
        );
        println!(
            "  G(hartree-W1, mRRHO)    {:>20.12} Eh   (recommended)",
            t.gibbs_qrrho
        );
    }
}

fn report_multilevel(
    molecule: &Molecule,
    spec: &hartree::multilevel::MultiLevelSpec,
    res: &hartree::multilevel::MultiLevelResult,
) {
    println!("hartree -- multi-level: {}", spec.label());
    println!(
        "atoms: {}   charge: {}   multiplicity: {}",
        molecule.len(),
        molecule.charge,
        molecule.multiplicity
    );
    println!("  SP  level (energy)    {}", spec.sp.label);
    println!("  OPT level (geometry)  {}", spec.opt.label);
    println!();

    let opt = res
        .opt
        .optimized_geometry
        .as_ref()
        .expect("multi-level result carries the optimization");
    println!(
        "geometry: optimized at {} ({} steps, converged)",
        spec.opt.label, opt.iterations
    );
    println!("optimized geometry (angstrom):");
    for atom in &res.geometry.atoms {
        println!(
            "{:<2}  {:>14.8}  {:>14.8}  {:>14.8}",
            atom.element.symbol(),
            atom.position[0] / ANGSTROM_TO_BOHR,
            atom.position[1] / ANGSTROM_TO_BOHR,
            atom.position[2] / ANGSTROM_TO_BOHR,
        );
    }
    println!();

    println!("multi-level energy breakdown:");
    println!(
        "  E_low   ({:<})  {:>20.12} Eh   (at its optimized geometry)",
        spec.opt.label, res.e_low
    );
    println!(
        "  E(SCF)  ({:<})  {:>20.12} Eh",
        spec.sp.label, res.sp.scf.energy
    );
    for (label, value) in [
        ("E_disp", res.sp.dispersion_energy),
        ("E_gCP", res.sp.gcp_energy),
        ("E_SRB", res.sp.srb_energy),
        ("E_nl (VV10)", res.sp.vv10_energy),
    ] {
        if let Some(v) = value {
            println!("  {label:<21} {v:>20.12} Eh   (SP level)");
        }
    }
    if let Some(post) = &res.sp.post_hf {
        println!(
            "  E_corr (post-HF)      {:>20.12} Eh   (SP level)",
            post.total_energy() - res.sp.scf.energy
        );
    }
    if let Some(dh) = &res.sp.double_hybrid {
        println!(
            "  E_PT2 ({:<})  {:>20.12} Eh   (SP level)",
            dh.functional_name,
            dh.pt2_energy()
        );
    }
    println!(
        "  E_high//low           {:>20.12} Eh   (final electronic energy)",
        res.e_high
    );

    if let Some(t) = &res.thermo {
        report_frequencies(&t.freq.frequencies);
        report_thermo(&t.freq.thermochemistry);
        println!();
        println!(
            "composite thermochemistry (thermal corrections at {}, electronic energy at {}):",
            spec.opt.label, spec.sp.label
        );
        println!("  E_high//low           {:>20.12} Eh", res.e_high);
        println!("  H_corr(low) = H-E     {:>20.12} Eh", t.h_corr);
        println!("  G_corr(low) = G-E     {:>20.12} Eh   (RRHO)", t.g_corr);
        println!("  G_corr(low, mRRHO)    {:>20.12} Eh", t.g_corr_qrrho);
        println!(
            "  H(composite)          {:>20.12} Eh   = E_high + H_corr(low)",
            t.enthalpy
        );
        println!(
            "  G(composite, RRHO)    {:>20.12} Eh   = E_high + G_corr(low)",
            t.gibbs
        );
        println!(
            "  G(composite, mRRHO)   {:>20.12} Eh   (recommended)",
            t.gibbs_qrrho
        );
    }
}

fn run_multilevel_conformers(
    molecule: &Molecule,
    spec: &hartree::multilevel::MultiLevelSpec,
    ml_opts: &hartree::multilevel::MultiLevelOptions,
    engine: Option<&str>,
    alpb: Option<String>,
    out_path: Option<&str>,
) -> Result<bool, String> {
    use hartree::ext::ensemble::Ensemble;
    use hartree::multilevel::ENSEMBLE_RERANK_CAP;

    let ensemble: Ensemble = if engine == Some("crest") {
        use hartree::ext::crest::{CrestInput, run};
        let method = hartree::ext::xtb::XtbMethod::Gfn2Xtb;
        let input = CrestInput::from_molecule(method, molecule, alpb);
        let workdir = std::env::temp_dir().join(format!("hartree_crest_{}", std::process::id()));
        println!(
            "hartree -- multi-level conformer re-ranking (external CREST, {})",
            method.keyword()
        );
        run(molecule, &input, &workdir).map_err(|e| e.to_string())?
    } else {
        use hartree::ext::confgen::{ConfGenOptions, generate_conformers};
        println!(
            "hartree -- multi-level conformer re-ranking (fallback generator, \
             screened at RHF/STO-3G)"
        );
        let res = generate_conformers(molecule, &ConfGenOptions::default(), |m| {
            let job = Job {
                molecule: m.clone(),
                basis: "sto-3g".into(),
                method: Method::Rhf,
                options: JobOptions::default(),
            };
            job.run().ok().and_then(|r| {
                if r.converged() {
                    Some(r.best_energy())
                } else {
                    None
                }
            })
        })
        .map_err(|e| e.to_string())?;
        res.ensemble
    };
    if ensemble.is_empty() {
        return Err("conformer search produced no valid conformers".into());
    }
    if ensemble.len() > ENSEMBLE_RERANK_CAP {
        println!(
            "note: re-ranking the {ENSEMBLE_RERANK_CAP} lowest of {} conformers \
             (the documented cap keeps the hook cheap)",
            ensemble.len()
        );
    }

    let ranked =
        hartree::multilevel::rerank_ensemble(&ensemble, spec, ml_opts, ENSEMBLE_RERANK_CAP)?;
    println!();
    println!(
        "re-ranked ensemble ({} conformers, {}; Boltzmann weights at 298.15 K \
         from the composite energies):",
        ranked.len(),
        spec.label()
    );
    println!(
        "  {:>4}  {:>20}  {:>20}  {:>12}  {:>10}",
        "rank", "E_low / Eh", "E_high//low / Eh", "ΔE / kcal", "weight"
    );
    const KCAL: f64 = 627.509_474_063;
    let e0 = ranked.first().map(|r| r.e_high).unwrap_or(0.0);
    for (i, r) in ranked.iter().enumerate() {
        println!(
            "  {:>4}  {:>20.12}  {:>20.12}  {:>12.4}  {:>10.4}",
            i + 1,
            r.e_low,
            r.e_high,
            (r.e_high - e0) * KCAL,
            r.weight
        );
    }

    if let Some(path) = out_path {
        let mut text = String::new();
        for r in &ranked {
            text.push_str(
                &hartree::ext::xyz::write_xyz(&r.molecule, &format!("{:.12}", r.e_high))
                    .map_err(|e| e.to_string())?,
            );
        }
        std::fs::write(path, text).map_err(|e| format!("writing {path}: {e}"))?;
        println!();
        println!("re-ranked ensemble written to {path}");
    }
    Ok(true)
}

fn simple_method(method_str: &str) -> Result<Method, String> {
    match method_str {
        "rhf" | "hf" => Ok(Method::Rhf),
        "uhf" => Ok(Method::Uhf),
        "rohf" => Ok(Method::Rohf),
        other => FunctionalSpec::parse(other).map(Method::Dft).map_err(|_| {
            format!("unsupported conformer-screen method {other:?} (use hf or a functional)")
        }),
    }
}

fn report_frequencies(freq: &FrequencyResult) {
    println!();
    println!("harmonic vibrational frequencies (numerical Hessian of the gradient):");
    for (i, &f) in freq.frequencies_cm1.iter().enumerate() {
        let tag = if f < -1.0 {
            "i"
        } else if f.abs() < 10.0 {
            "(trans/rot)"
        } else {
            ""
        };
        let f_display = if f < 0.0 {
            format!("{:.2}i", (-f))
        } else {
            format!("{:.2}", f)
        };
        println!("  {:>3}:  {:>12} cm⁻¹  {}", i + 1, f_display, tag);
    }
    println!();
    if freq.n_imaginary > 0 {
        println!(
            "  WARNING: {} imaginary mode(s) — not at a minimum",
            freq.n_imaginary
        );
    } else {
        let n_vib = freq.frequencies_cm1.iter().filter(|&&f| f >= 10.0).count();
        println!("  {n_vib} real vibrational modes (0 imaginary)");
    }
}

fn report_thermo(thermo: &ThermoResult) {
    println!();
    println!(
        "RRHO thermochemistry at {:.2} K (σ = {}, linear = {}):",
        thermo.temperature, thermo.symmetry_number, thermo.is_linear
    );
    println!(
        "  zero-point energy (ZPE)          {:>20.12} Eh",
        thermo.zpe
    );
    println!(
        "  thermal correction to E          {:>20.12} Eh",
        thermo.thermal_energy_corr
    );
    println!(
        "  thermal correction to H          {:>20.12} Eh",
        thermo.enthalpy_corr
    );
    println!(
        "  total enthalpy H({:.0} K)        {:>20.12} Eh",
        thermo.temperature, thermo.enthalpy
    );
    println!(
        "  entropy S                        {:>20.12} Eh/K",
        thermo.entropy
    );
    println!(
        "  T·S                              {:>20.12} Eh",
        thermo.temperature * thermo.entropy
    );
    println!(
        "  Gibbs free energy G({:.0} K)     {:>20.12} Eh",
        thermo.temperature, thermo.gibbs
    );
    println!();
    println!("quasi-RRHO (mRRHO) thermochemistry, Grimme Chem. Eur. J. 18, 9955 (2012),");
    println!(
        "w0 = {:.1} cm⁻¹ (entropy-only convention: H is the harmonic RRHO enthalpy):",
        thermo.qrrho_w0_cm1
    );
    println!(
        "  entropy S(mRRHO)                 {:>20.12} Eh/K",
        thermo.entropy_qrrho
    );
    println!(
        "  T·S(mRRHO)                       {:>20.12} Eh",
        thermo.temperature * thermo.entropy_qrrho
    );
    println!(
        "  Gibbs free energy G(mRRHO)       {:>20.12} Eh   (recommended)",
        thermo.gibbs_qrrho
    );
}

fn print_usage() {
    println!(
        "hartree -- pure-Rust quantum chemistry\n\n\
         USAGE:\n    hartree <molecule.xyz> [options]\n\n\
         OPTIONS:\n\
         \x20   --basis <name>          basis set [default: sto-3g]. Bundled: sto-3g, 6-31g;\n\
         \x20                           6-311g, 6-311g(d,p), 6-311+g(d,p), 6-311++g(d,p)\n\
         \x20                           (omits He); cc-pvdz, cc-pvtz, cc-pvqz; aug-cc-pvtz\n\
         \x20                           (all H–Ar); the Karlsruhe family, all-electron H–Kr:\n\
         \x20                           def2-svp, def2-svpd, def2-tzvp, def2-tzvpp, def2-tzvpd,\n\
         \x20                           def2-tzvppd, def2-qzvp, def2-qzvpp, def2-mtzvpp,\n\
         \x20                           def2-mtzvp (mTZVP), def2-msvp,\n\
         \x20                           ma-def2-svp, ma-def2-tzvp (minimally augmented);\n\
         \x20                           def2-svp and def2-tzvp also cover Ag, Sn, I, Au via\n\
         \x20                           the def2-ECP effective core potentials (auto-attached)\n\
         \x20   --method <name>         hf | rhf | uhf | rohf | mp2 | ccsd | ccsd(t) | <functional>\n\
         \x20                           (hf auto-selects rhf/uhf by multiplicity)\n\
         \x20                           [default: rhf]   (mp2 auto-selects an RHF or UHF\n\
         \x20                           reference by multiplicity; ccsd/ccsd(t) are\n\
         \x20                           closed-shell RHF single points; a functional name — svwn, pbe, blyp,\n\
         \x20                           b3lyp, pbe0, tpss, r2scan, m06-2x, pw6b95, b97m-v,\n\
         \x20                           wb97x-v, wb97m-v, … — runs Kohn–Sham DFT, auto RKS/UKS.\n\
         \x20                           Double hybrids b2plyp, revdsd-pbep86, pwpb95,
\n         \x20                           wb97m(2)/wb97m2: closed-shell in-core single points
\n         \x20                           (E = E_SCF + c_os*E_os + c_ss*E_ss, frozen-core PT2
\n         \x20                           on the converged KS orbitals; wb97m(2) is evaluated
\n         \x20                           non-SC on wb97m-v orbitals, VV10 scaled by 0.65904).
\n         \x20                           Range-separated (wb97x-v/wb97m-v) and -V (VV10)\n\
         \x20                           functionals: conventional in-core single points, or\n\
         \x20                           --ri with --cosx for the RS hybrids (RI-J Coulomb +\n\
         \x20                           semi-numerical K; --ri alone and --direct are\n\
         \x20                           rejected for RS; no --opt/--freq); E_nl is evaluated\n\
         \x20                           non-self-consistently after the SCF; grid-sensitive\n\
         \x20                           functionals default to grid level 4)\n\
         \x20                           A -d3 suffix (pbe-d3, b3lyp-d3, hf-d3, …) adds the\n\
         \x20                           D3(BJ) dispersion correction; a -d4 suffix (pbe-d4,\n\
         \x20                           r2scan-d4, …) adds DFT-D4 (EEQ charges + ATM term)\n\
         \x20                           instead (neither for svwn or post-HF, not both at once)\n\
         \x20                           Composite (\"3c\") methods bundle functional + basis +\n\
         \x20                           corrections under one keyword; the basis is implied\n\
         \x20                           and -d3/-d4 suffixes are rejected (each defines its\n\
         \x20                           own corrections):\n\
         \x20                           r2scan-3c: r2scan/def2-mTZVPP + D4 (method-specific\n\
         \x20                           parameters) + gCP, grid defaults to 4\n\
         \x20                           b3lyp-3c: b3lyp5/def2-mSVP + D3(BJ)-ATM + gCP\n\
         \x20                           (DFT/SV(P) parameters)\n\
         \x20                           b97-3c: refitted B97 GGA/mTZVP + D3(BJ)-ATM + SRB\n\
         \x20                           (short-range bond-length correction; no gCP)\n\
         \x20                           pbeh-3c: modified PBE hybrid (42% EXX)/def2-mSVP\n\
         \x20                           + D3(BJ)-ATM + damped gCP\n\
         \x20                           Multi-level (\"//\") specs: --method \"SP // OPT\" runs\n\
         \x20                           the multi-level workflow — optimize at the OPT (low)\n\
         \x20                           level, final single point at the SP (high) level on\n\
         \x20                           that geometry (ORCA notation), e.g.\n\
         \x20                           --method \"wb97m-v/def2-tzvpp // r2scan-3c\". Each\n\
         \x20                           side is method[/basis]: composites carry their own\n\
         \x20                           basis (an explicit one is rejected); plain methods\n\
         \x20                           require one; -d3/-d4 suffixes apply per level. With\n\
         \x20                           --freq the frequencies/thermal corrections run at the\n\
         \x20                           OPT level and G = E_high + (G_low - E_low) is\n\
         \x20                           reported (composite free energy). Solvation flags\n\
         \x20                           apply to every stage by that stage's own rules;\n\
         \x20                           --opt/--basis and the single-point-only backends\n\
         \x20                           (--direct/--ri/--cosx/--x2c/--fod/--smear/--cp/\n\
         \x20                           --props/--sph/--gcp) are rejected with the spec\n\
         \x20   --protocol <name>       composite thermochemistry protocol. w1 (hartree-W1):\n\
         \x20                           a W1-STYLE staged workflow (NOT literal W1/W1-F12):\n\
         \x20                           B3LYP/cc-pVTZ geometry +\n\
         \x20                           frequencies/RRHO; HF/CBS from cc-pVTZ/cc-pVQZ\n\
         \x20                           (2-pt Karton-Martin); frozen-core CCSD-corr/CBS from\n\
         \x20                           the same pair (2-pt Halkier n^-3); (T) at cc-pVTZ\n\
         \x20                           (additivity). Closed-shell molecules only. Prints the\n\
         \x20                           stage breakdown and E/H/G. Core-valence correlation,\n\
         \x20                           scalar relativity, and spin-orbit are omitted\n\
         \x20                           (documented). Honours --symmetry-number, --qrrho-w0,\n\
         \x20                           --all-electron; rejects --method/--basis/--opt/--freq\n\
         \x20                           and the backend/solvation flags\n\
         \x20   --rerank                with --conformers and a multi-level \"//\" spec:\n\
         \x20                           CENSO-lite ensemble re-ranking — optimize each\n\
         \x20                           conformer at the OPT level, single-point at the SP\n\
         \x20                           level, Boltzmann weights from the composite energies\n\
         \x20                           (at most 6 conformers, the documented cap; the\n\
         \x20                           generation step is screened at RHF/STO-3G)\n\
         \x20   --grid <0..4>           DFT integration grid level [default: 3]\n\
         \x20                           (only valid with a functional method)\n\
         \x20   --charge <int>          net charge [default: 0]\n\
         \x20   --multiplicity <int>    spin multiplicity 2S+1 [default: 1]\n\
         \x20                           (aliases: --mult, --spin)\n\
         \x20   --all-electron          correlate all orbitals for mp2/ccsd/ccsd(t) (default:\n\
         \x20                           noble-gas frozen core)\n\
         \x20   --opt                   optimize the geometry (analytic gradient for rhf/uhf,\n\
         \x20                           finite differences for rohf and DFT functionals)\n\
         \x20   --ts                    locate a transition state (first-order saddle) by P-RFO\n\
         \x20                           eigenvector-following from the input geometry, which must\n\
         \x20                           be a guess near the saddle; verifies one imaginary mode\n\
         \x20                           and reports its frequency (rhf/uhf/rohf/DFT; mutually\n\
         \x20                           exclusive with --opt; no post-HF/RI/direct/COSX/X2C/\n\
         \x20                           smearing/implicit-solvent)\n\
         \x20   --ts-product <file.xyz>  two-endpoint TS search: the main XYZ is the reactant\n\
         \x20                           and this is the product. Builds a near-saddle guess\n\
         \x20                           between them (a single IDPP guess by default) and seeds\n\
         \x20                           the reaction coordinate before refining (requires --ts;\n\
         \x20                           product takes the reactant's charge/multiplicity)\n\
         \x20   --ts-neb                with --ts-product, relax a climbing-image NEB band onto\n\
         \x20                           the minimum-energy path and refine its climbing image\n\
         \x20                           instead of a single IDPP guess (more robust, costlier)\n\
         \x20   --ts-neb-images <int>   interior images for the --ts-neb band [default: 8] (>= 1)\n\
         \x20   --ts-scan <int>         with --ts-product (IDPP route), place the guess at the\n\
         \x20                           energy maximum of the path: evaluate the surface at <int>\n\
         \x20                           images and parabola-fit the peak (>= 3; better guess at\n\
         \x20                           <int> extra single-point energies)\n\
         \x20   --ts-scan-coord <spec>  single-ended distinguished-coordinate scan: drive one\n\
         \x20                           internal coordinate of the input geometry across a range,\n\
         \x20                           relaxing the rest at each point, and refine the saddle from\n\
         \x20                           the energy peak. spec is \"i,j[,k[,l]]:start:end:steps\":\n\
         \x20                           2 indices = bond i-j (range in angstrom), 3 = angle with\n\
         \x20                           centre k in the middle, 4 = dihedral about j-k (range in\n\
         \x20                           degrees); steps >= 3. Mutually exclusive with --ts-product\n\
         \x20   --ts-output <file.json>  after the search, write a machine-readable JSON\n\
         \x20                           record of the result (status, energy, final geometry\n\
         \x20                           in angstrom and bohr, imaginary frequency, IRC summary,\n\
         \x20                           step count; requires --ts)\n\
         \x20   --ts-irc                after a converged TS, trace the intrinsic reaction\n\
         \x20                           coordinate downhill in both senses of the reaction mode\n\
         \x20                           into the two basins and report the endpoint energies\n\
         \x20                           (confirms the minima the saddle joins)\n\
         \x20   --ts-irc-method <dvv|gs2|eulerpc>  IRC integrator [default: dvv]; dvv is\n\
         \x20                           Hessian-free, gs2 (Gonzalez–Schlegel) is constrained and\n\
         \x20                           most accurate, eulerpc reuses one cached Hessian\n\
         \x20   --ts-irc-step <step>    IRC arc-length step, mass-weighted (√amu·bohr)\n\
         \x20                           [default: 0.1]\n\
         \x20   --ts-irc-max-steps <int>  max IRC steps per endpoint [default: 150] (>= 1)\n\
         \x20   --ts-irc-gtol <a.u.>    IRC convergence threshold on the projected RMS force\n\
         \x20                           [default: 1e-3]\n\
         \x20   --ts-algo <prfo|dimer>  saddle-point algorithm [default: prfo]; both prfo\n\
         \x20                           (Hessian eigenvector-following) and dimer (Hessian-free,\n\
         \x20                           midpoint-gradient curvature estimate) are available\n\
         \x20   --ts-dimer-delta <bohr>  dimer half-separation for the curvature estimate\n\
         \x20                           [default: 1e-2] (--ts-algo dimer only)\n\
         \x20   --ts-max-iter <int>     max saddle-search iterations [default: 300] (>= 1)\n\
         \x20   --ts-trust <bohr>       initial trust radius for the climbing step [default: 0.2]\n\
         \x20   --ts-follow <int>       P-RFO mode to follow uphill, 0 = softest [default: 0]\n\
         \x20   --ts-recalc-hessian <int>  recompute the FD Hessian every N accepted steps;\n\
         \x20                           0 = compute once, then Bofill update [default: 0]\n\
         \x20   --ts-stall-refresh <int>  refresh the FD Hessian after N consecutive\n\
         \x20                           non-improving steps (P-RFO); a soft-surface aid for\n\
         \x20                           floppy systems, 0 = off [default: 0], try 5 if a search\n\
         \x20                           plateaus far from convergence\n\
         \x20   --ts-verify-hessian <strict|maintained|auto>  Hessian the post-convergence\n\
         \x20                           verification uses (P-RFO) [default: strict]; strict\n\
         \x20                           finite-differences a fresh one, maintained reuses the\n\
         \x20                           Bofill Hessian, auto reuses it unless a mode is near the\n\
         \x20                           threshold\n\
         \x20   --ts-coordinates <mass-weighted|internal>  coordinate frame the P-RFO climb\n\
         \x20                           steps in [default: mass-weighted]; internal uses redundant\n\
         \x20                           internal coordinates (bonds+angles) for better conditioning\n\
         \x20                           of soft reaction coordinates, falling back to mass-weighted\n\
         \x20                           when the internal set is incomplete\n\
         \x20   --ts-fd-step <bohr>     finite-difference step for gradients/Hessian [default:\n\
         \x20                           5e-3]\n\
         \x20   --ts-neg-tol <a.u.>     eigenvalue cutoff for a negative (reaction) mode\n\
         \x20                           [default: 1e-4]\n\
         \x20                           (TS runs raise the SCF cap to 400 and add a 0.3 level\n\
         \x20                           shift for small-gap stability)\n\
         \x20                           (the --ts-* knobs take effect only with --ts)\n\
         \x20   --direct                integral-direct SCF: recompute ERIs each iteration\n\
         \x20                           instead of storing the nao^4 tensor (rhf/uhf/rohf\n\
         \x20                           single points; reaches larger systems, slower)\n\
         \x20   --ri                    density-fitted (RI-JK) SCF: J/K from 3-center integrals\n\
         \x20                           over the def2-universal-jkfit auxiliary set (hf and all\n\
         \x20                           DFT functionals, single points; small fitting error\n\
         \x20                           ~1e-4 Eh; incompatible with --direct, --opt, post-HF,\n\
         \x20                           --properties, --freq)\n\
         \x20   --ri-mp2                RI-MP2: density-fitted MP2 over the matching def2 /C\n\
         \x20                           (MP2-fit) auxiliary set (<basis>/c; bundled: def2-svp/c,\n\
         \x20                           def2-tzvp/c — errors if no /C partner exists, never\n\
         \x20                           falls back to jkfit). --method mp2 only (RHF or UHF\n\
         \x20                           reference by multiplicity); the SCF step\n\
         \x20                           keeps its backend (default in-core, or --ri). Same\n\
         \x20                           frozen-core convention and E_OS/E_SS split as\n\
         \x20                           conventional MP2 (fitting error ~1e-5 Eh at def2-svp)\n\
         \x20   --cosx                  COSX semi-numerical exchange: K built on a coarse\n\
         \x20                           overlap-fitted grid (Neese et al. 2009 / Izsak & Neese\n\
         \x20                           2011); J keeps the configured path (in-core or --ri).\n\
         \x20                           HF, global-hybrid, and range-separated DFT single\n\
         \x20                           points (RS hybrids serve K_LR(omega) semi-numerically\n\
         \x20                           too, on either backend); rejected for post-HF,\n\
         \x20                           --direct, --opt, --freq, and --fod\n\
         \x20   --x2c                   scalar-relativistic X2C-1e Hamiltonian: T+V replaced\n\
         \x20                           by the picture-changed spin-free exact-two-component\n\
         \x20                           one-electron Hamiltonian (2e integrals stay\n\
         \x20                           nonrelativistic). HF and DFT on any backend; rejected\n\
         \x20                           with ECP atoms (double-counts relativity), --opt,\n\
         \x20                           --freq, and post-HF; properties carry a picture-change\n\
         \x20                           caveat\n\
         \x20   --solvent <name>        C-PCM implicit solvation (electrostatics only) in a\n\
         \x20                           named solvent: water, acetonitrile, methanol, dmso,\n\
         \x20                           chloroform, toluene (SCF-level methods, incl. --ri and\n\
         \x20                           -d3; E_solv included in the total; --opt uses FD\n\
         \x20                           gradients; --freq in solvent is rejected)\n\
         \x20   --eps <float>           C-PCM with an explicit dielectric constant instead of\n\
         \x20                           a named solvent (mutually exclusive with --solvent)\n\
             --smd <name>            SMD universal solvation model (Marenich/Cramer/
\n                                     Truhlar 2009): C-PCM electrostatics with SMD intrinsic
\n                                     Coulomb radii + the CDS surface-tension term; reports
\n                                     dG_EP, G_CDS, dG_solv (1 M -> 1 M standard state) vs a
\n                                     gas-phase reference SCF. 20 bundled solvents (water,
\n                                     methanol, ethanol, acetonitrile, dmso, acetone, thf,
\n                                     dmf, toluene, benzene, chloroform, ...). Same scope as
\n                                     C-PCM (SCF-level methods; --opt via FD; no --freq);
\n                                     mutually exclusive with --solvent/--eps
\n             --alpb <name>           ALPB implicit solvation (Ehlert/Stahn/Spicher/
\n                                     Grimme 2021), xtb GFN2 parameters. Post-SCF on
\n                                     SCF Mulliken charges; reports G_born/G_hb/G_sasa/
\n                                     G_shift and DeltaG_solv. Single points only.
\n                                     GFN2-fit params on ab-initio charges (caveat).
\n             --gbsa <name>           GBSA implicit solvation (generalized Born + SASA),
\n                                     xtb GFN2 parameters. Like --alpb but the Still
\n                                     kernel and no Poisson-Boltzmann shape term.
\n             --cosmo-file <path>     run C-PCM at the ideal-conductor limit (eps=inf)
\n                                     and write a .cosmo file (TURBOMOLE/COSMOtherm
\n                                     format) for COSMO-RS. Mutually exclusive with the
\n                                     other solvation options
\n         \x20   --smear <K>             Fermi-Dirac fractional-occupation smearing at the\n\
         \x20                           given electronic temperature (kelvin). Energy-only:\n\
         \x20                           rhf/uhf and DFT single points (no --opt, --freq, or\n\
         \x20                           post-HF). Reports occupations, T*S_el, and the free\n\
         \x20                           energy F = E - T*S_el\n\
         \x20   --fod                   Grimme FOD multireference diagnostic (Angew. Chem. Int.\n\
         \x20                           Ed. 54, 12308 (2015)): Fermi-smeared SCF at\n\
         \x20                           T_el = 5000 K + 20000 K*a_x, reporting N_FOD (the\n\
         \x20                           integral of the fractional-occupation weighted density;\n\
         \x20                           N_FOD >= 1 signals strong static correlation). Implies\n\
         \x20                           --method tpss --basis def2-tzvp unless given explicitly\n\
         \x20                           (warned); --smear overrides the temperature. In-core\n\
         \x20                           single points only (no --direct/--ri/--opt/--freq or\n\
         \x20                           post-HF)\n\
         \x20   --fod-cube <path>       with --fod, export rho_FOD (alpha+beta) as a Gaussian\n\
         \x20                           cube file (bounding box + 4 bohr margin, 0.2 bohr\n\
         \x20                           spacing)\n\
         \x20   --properties, --props   compute one-electron properties: dipole (Debye),\n\
         \x20                           Mulliken/Löwdin charges, Mayer bond orders\n\
         \x20   --freq                  compute harmonic frequencies (numerical Hessian of the\n\
         \x20                           gradient) and RRHO + quasi-RRHO (mRRHO, Grimme 2012)\n\
         \x20                           thermochemistry at 298.15 K. Works for rhf/uhf and DFT\n\
         \x20                           functionals incl. the 3c composites and ECP atoms;\n\
         \x20                           run at the optimized geometry for all-real modes\n\
         \x20   --qrrho-w0 <cm-1>       quasi-RRHO interpolation frequency w0 for the mRRHO\n\
         \x20                           entropy [default: 100]\n\
         \x20   --sph                   single-point Hessian (Spicher & Grimme, JCTC 2021):\n\
         \x20                           with --freq, take the geometry as-is (skip the\n\
         \x20                           stationarity assumption) and project the gradient\n\
         \x20                           direction out of the Hessian. Reduces to ordinary\n\
         \x20                           frequencies at a true minimum; SPH frequencies are\n\
         \x20                           APPROXIMATE (gradient-projection variant, not xtb\n\
         \x20                           --bhess; see crate docs)\n\
         \x20   --method gfn2-xtb       run the external `xtb` binary (GFN2-xTB); --method\n\
         \x20   --method gfn-ff         gfn-ff runs GFN-FF. Detected on PATH or via\n\
         \x20                           HARTREE_XTB_PATH; honours --charge/--multiplicity/--alpb/--opt.\n\
         \x20                           A clear 'xtb binary not found' error with install\n\
         \x20                           guidance is raised when absent\n\
         \x20   --conformers [crest]    conformer ensemble search. Bare --conformers uses the\n\
         \x20                           built-in RDKit-free torsion-driving fallback (screened\n\
         \x20                           with --method/--basis single points; default RHF/\n\
         \x20                           STO-3G); `--conformers crest` runs the external CREST\n\
         \x20                           (HARTREE_CREST_PATH or PATH). Prints relative energies\n\
         \x20                           and Boltzmann weights at 298.15 K\n\
         \x20   --conformers-out <path> write the conformer ensemble as a multi-frame XYZ\n\
         \x20   --symmetry-number <int> rotational symmetry number σ for RRHO entropy\n\
         \x20                           [default: 1; use 2 for H₂O, HF, CO; 12 for CH₄]\n\
         \x20   --cp <nA>               Boys-Bernardi counterpoise correction for a\n\
         \x20                           two-fragment complex: the first nA atoms of the XYZ\n\
         \x20                           (input order) are fragment A, the rest fragment B.\n\
         \x20                           Runs the 5 required single points (complex; each\n\
         \x20                           fragment with the partner's ghost basis; each\n\
         \x20                           fragment alone) and reports dE_int^CP, the\n\
         \x20                           uncorrected dE_int, and the BSSE estimate. Works\n\
         \x20                           with any single-point-capable method (no --opt/\n\
         \x20                           --freq/--props/--fod/solvent)\n\
         \x20   --cp-charges <qA,qB>    per-fragment net charges for --cp [default: 0,0];\n\
         \x20                           must sum to --charge\n\
         \x20   --cp-mults <mA,mB>      per-fragment spin multiplicities for --cp\n\
         \x20                           [default: 1,1]\n\
         \x20   --gcp <set>             standalone geometric counterpoise (gCP) correction\n\
         \x20                           added to the total energy (outside the 3c\n\
         \x20                           composites). Parameter sets (vendored from the\n\
         \x20                           reference implementation mctc-gcp): r2scan-3c\n\
         \x20                           (alias def2-mtzvpp), dft/sv(p) (alias b3lyp-3c),\n\
         \x20                           pbeh-3c (alias def2-msvp). Ghost atoms are excluded\n\
         \x20   --recommend <task>      print the recommended level of theory for a task and\n\
         \x20                           exit (no XYZ needed): general (r2scan-3c opt+freq),\n\
         \x20                           barriers / nci (wb97m-v/def2-tzvpp single point on a\n\
         \x20                           r2scan-3c geometry via the multi-level '//' workflow),\n\
         \x20                           thermochemistry (the multi-level composite free\n\
         \x20                           energy with --freq)\n\
         \x20   --no-method-warnings    suppress the method-quality assessment section\n\
         \x20                           (informational warnings: Pople/cc bases with DFT,\n\
         \x20                           missing dispersion where the functional metadata\n\
         \x20                           recommends one, minimal/unpolarized bases, pure-GGA\n\
         \x20                           barrier caveats, HF-without-correlation, coarse grids\n\
         \x20                           under grid-sensitive functionals) and the no-method\n\
         \x20                           recommendation pointer. The warnings stay attached to\n\
         \x20                           the library JobResult either way\n\
         \n\
         \x20 PERIODIC (solid-state GPW DFT; selected by --cell / --kpoints / --cutoff):\n\
         \x20   --cell <spec>           unit cell: 'cubic <a>', or 1/3/6/9 numbers (Å, deg):\n\
         \x20                           1=cubic, 3=a b c (orthorhombic), 6=a b c α β γ,\n\
         \x20                           9=three row vectors. Omit the spec (or '--cell file')\n\
         \x20                           to read a Lattice=\"…\" line from the extended-XYZ file\n\
         \x20   --kpoints <n1 n2 n3>    Monkhorst–Pack mesh (or 'gamma') [default: gamma]\n\
         \x20   --cutoff <E>            plane-wave density cutoff in hartree [default: 280]\n\
         \x20   --basis <name>          GTH basis [default: DZVP-GTH-PADE; also SZV-GTH]\n\
         \x20   --xc <name>             pade (GTH-PADE LDA) | lda (Slater+PW92) [default: pade]\n\
         \x20   --pseudo <file>         user GTH potential file (CP2K format) [default: bundled]\n\
         \x20   --basis-file <file>     user GTH basis file (.gbs) [default: bundled]\n\
         \x20   --forces                also compute analytic forces\n\
         \x20   --stress                also compute the analytic stress tensor\n\
         \x20   --max-iter <n> --mixing <a>   SCF controls\n\
         \x20                           (v1: GTH PPs, spin-restricted, insulators/semiconductors;\n\
         \x20                           bundled elements: H, C, O, Si)\n\
         \x20   -h, --help              show this help\n\n\
         XYZ coordinates are in angstrom. Ghost atoms (counterpoise centers: basis\n\
         functions only, no nuclear charge, no electrons) are written Gh(O) or with\n\
         the @O shorthand in the element column; they are excluded from D3/D4/gCP/SRB\n\
         and from geometry jobs (--opt/--freq), and rejected with --props/solvent."
    );
}