Rustb 0.7.1

A package for calculating band, angle state, linear and nonlinear conductivities based on tight-binding models
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
#![allow(non_snake_case)]

//! # Rustb -- Tight-Binding Model Library
//!
//! A Rust library for tight-binding model calculations in condensed matter physics.
//! It supports model construction from both explicit hopping parameters and
//! Slater-Koster integrals, band structure solving, topological analysis, and
//! linear/nonlinear transport property calculations.
//!
//! ## Module overview
//!
//! ### Core data structures
//!
//! | Module | Purpose |
//! |--------|---------|
//! | [`model`] | Central [`Model`]<SPIN, DIM, R> struct with lattice, orbital, and hopping data, plus enums
//! |   [`Gauge`], [`Dimension`], [`SpinDirection`] |
//! | [`atom_struct`] | [`Atom`] and [`OrbProj`] types for describing atomic sites and orbital
//! |   projections |
//!
//! ### Model construction and manipulation
//!
//! | Module | Purpose |
//! |--------|---------|
//! | [`model_build`] | Constructors (`Model::tb_model`), hopping setup (`add_hop`, `set_hop`),
//! |   on-site energies (`set_onsite`), and supercell building (`make_supercell`) |
//! | [`hubbard`] | Non-collinear unrestricted Hartree-Fock for on-site Hubbard
//! |   interactions, with fixed chemical potential or fixed initial filling |
//! | [`cut`] | [`CutModel`] trait for extracting finite slabs from supercells |
//! | [`geometry`] | Supercell geometry, dot structures, and related spatial operations |
//! | [`model_utils`] | Internal utility functions for model manipulation |
//!
//! ### Hamiltonian solving
//!
//! | Module | Purpose |
//! |--------|---------|
//! | [`solve_ham`] | Parallel diagonalization of H(k) over k-point meshes
//! |   (`solve_all_parallel`, `solve_band_all_parallel`) |
//! | [`ndarray_lapack`] | LAPACK bindings for ndarray matrices |
//!
//! ### k-space sampling
//!
//! | Module | Purpose |
//! |--------|---------|
//! | [`kpath`] | k-path generation along high-symmetry lines (`k_path`) |
//! | [`kpoints`] | Uniform k-mesh generation (`gen_kmesh`, `gen_krange`) |
//!
//! ### Transport properties
//!
//! | Module | Purpose |
//! |--------|---------|
//! | [`response`] | Linear, nonlinear, and optical conductivity tensors via the Kubo
//! |   formalism and simplex quadrature: anomalous Hall, spin Hall, nonlinear
//! |   responses, and frequency-dependent optical conductivity |
//!
//! ### Operators and observables
//!
//! | Module | Purpose |
//! |--------|---------|
//! | [`velocity`] | Velocity operator v_a(k) at each k-point |
//! | [`orbital_angular`] | Orbital angular momentum operator |
//! | [`math`] | Mathematical utilities (commutators, matrix operations) |
//!
//! ### Topological analysis
//!
//! | Module | Purpose |
//! |--------|---------|
//! | [`model_physics`] | Density of states, Berry curvature, Chern numbers, Wilson loops,
//! |   and Wannier centers |
//!
//! ### Surface and defect calculations
//!
//! | Module | Purpose |
//! |--------|---------|
//! | [`surfgreen`] | Surface Green's function G^s(omega, k_parallel) for
//! |   semi-infinite systems; local density of states at surfaces and edges |
//!
//! ### Interfaces
//!
//! | Module | Purpose |
//! |--------|---------|
//! | [`wannier90`] | Read Wannier90 `_hr.dat` and `_r.dat` files |
//! | [`unfold`] | Band unfolding for supercell calculations (the [`Unfold`] trait) |
//!
//! ### Magnetic field
//!
//! | Module | Purpose |
//! |--------|---------|
//! | [`magnetic_field`] | Uniform magnetic field via Peierls substitution |
//!
//! ### Output and I/O
//!
//! | Module | Purpose |
//! |--------|---------|
//! | [`output`] | Band structure and surface state plotting via gnuplot (`show_band`, etc.) |
//! | [`io`] | Text file I/O for 1D and 2D arrays (`write_txt`, `write_txt_1`) |
//!
//! ### Supporting modules
//!
//! | Module | Purpose |
//! |--------|---------|
//! | [`error`] | Centralized error handling ([`TbError`], [`Result`]) |
//! | [`phy_const`] | Physical constants (hbar, e, k_B, etc.) |
//! | [`generics`] | Numeric type abstractions |
//!
//! ## Mathematical foundation
//!
//! The tight-binding Hamiltonian in second-quantized form:
//!
//! $$
//! H = \sum_{i,j} t_{ij} c_i^\dagger c_j + \sum_i \epsilon_i c_i^\dagger c_i
//! $$
//!
//! where t_{ij} are hopping parameters and epsilon_i are on-site energies.
//!
//! The Bloch Hamiltonian at a given k-point is:
//!
//! $$
//! H_{mn}(\mathbf{k}) = \sum_{\mathbf{R}} H_{mn}(\mathbf{R})\, e^{i \mathbf{k} \cdot \mathbf{R}}
//! $$
//!
//! where R runs over lattice vectors and H_{mn}(R) is the
//! hopping matrix element from orbital n to orbital m.
//!
//! For transport, the Berry curvature is computed as:
//!
//! $$
//! \Omega_n(\mathbf{k}) = -2\,\operatorname{Im}\sum_{m\neq n}
//! \frac{\bra{n}\partial_{k_x} H\ket{m}\bra{m}\partial_{k_y} H\ket{n}}
//!      {(E_n - E_m)^2}
//! $$
//!
//! and the anomalous Hall conductivity follows from the Brillouin-zone integral:
//!
//! $$
//! \sigma_{xy} = \frac{e^2}{\hbar} \int \frac{d^d k}{(2\pi)^d}\,
//! \sum_n f_n(\mathbf{k})\, \Omega_n(\mathbf{k})
//! $$
//!
//! ## Quick start
//!
//! The example below builds a nearest-neighbor graphene model, computes the band
//! structure and density of states, constructs a zigzag nanoribbon, and plots the
//! edge states.
//!
//! ```no_run
//! # use Rustb::error::Result;
//! # fn main() -> Result<()> {
//! use ndarray::prelude::*;
//! use num_complex::Complex;
//! use Rustb::*;
//!
//! // --- Build the graphene tight-binding model ---
//! let t1 = Complex::new(1.0, 0.0);     // nearest-neighbor hopping
//! let t2 = Complex::new(0.1, 0.0);     // next-nearest-neighbor hopping
//! let delta = 0.5;                     // staggered on-site potential
//!
//! // Honeycomb lattice vectors
//! let lat = arr2(&[[3.0_f64.sqrt(), -1.0], [3.0_f64.sqrt(), 1.0]]);
//! // Two sublattice sites in fractional coordinates
//! let orb = arr2(&[[0.0, 0.0], [1.0 / 3.0, 1.0 / 3.0]]);
//!
//! let mut model = Model::<false, 2>::tb_model(lat, orb, None)?;
//! model.set_onsite(&arr1(&[delta, -delta]), None);
//!
//! // Nearest-neighbor hoppings (A <-> B)
//! model.add_hop(t1, 0, 1, &array![0, 0], None);
//! model.add_hop(t1, 0, 1, &array![-1, 0], None);
//! model.add_hop(t1, 0, 1, &array![0, -1], None);
//!
//! // Next-nearest-neighbor hoppings (A-A and B-B)
//! for &(i, j) in &[(0, 0), (1, 1)] {
//!     for r in &[array![1, 0], array![0, 1], array![1, -1]] {
//!         model.add_hop(t2, i, j, r, None);
//!     }
//! }
//!
//! // --- Band structure along high-symmetry path G -> K -> M -> G ---
//! let nk = 1001;
//! let path = arr2(&[[0.0, 0.0], [2.0 / 3.0, 1.0 / 3.0], [0.5, 0.5], [0.0, 0.0]]);
//! let label = vec!["G", "K", "M", "G"];
//! model.show_band(&path, &label, nk, "graphene")?;
//!
//! // --- Zigzag nanoribbon and edge states ---
//! let U = arr2(&[[1.0, 1.0], [-1.0, 1.0]]);
//! let super_model = model.make_supercell(&U)?;
//! let zig_model = super_model.cut_piece(100, 0)?;
//! let path_edge = arr2(&[[0.0, 0.0], [0.0, 0.5], [0.0, 1.0]]);
//! let label_edge = vec!["G", "M", "G"];
//! zig_model.show_band(&path_edge, &label_edge, 501, "graphene_zig")?;
//!
//! // --- Density of states ---
//! let kmesh = arr1(&[101, 101]);
//! let (energies, dos) = model.dos(&kmesh, -3.0, 3.0, 1000, 1e-2)?;
//! // Write DOS data to a text file
//! let dos_data = ndarray::stack![Axis(0), energies, dos];
//! write_txt(&dos_data, "dos.dat")?;
//! # Ok(())
//! # }
//! ```

pub mod atom_struct;
#[cfg(feature = "cryspglib")]
pub mod crystal_symmetry;
pub mod cut;
pub mod error;
pub mod fermi_surface;
pub mod floquet;
pub mod generics;
pub mod geometry;
#[cfg(feature = "cryspglib")]
pub mod hamiltonian_symmetry;
#[path = "Hubbard.rs"]
pub mod hubbard;
pub mod io;
pub mod kpath;
pub mod kplane;
pub mod kpoints;
pub mod magnetic_field;
pub mod math;
pub mod model;
pub mod model_build;
pub mod model_physics;
pub mod model_utils;
pub mod ndarray_lapack;
pub mod orbital_angular;
pub mod output;
pub mod phy_const;
pub mod quantum_geometry;
pub mod response;
pub mod solve_ham;
pub mod surfgreen;
pub mod thermodynamics;
pub mod unfold;
pub mod velocity;
pub mod wannier90;
pub use crate::atom_struct::{Atom, AtomId, AtomType, OrbProj, OrbitalId};
#[cfg(feature = "cryspglib")]
pub use crate::crystal_symmetry::{
    CrystalSymmetry, CrystalSymmetryDataset, CrystalSymmetryOperation, ExternalFields,
    HighSymmetryKPoint, IrreducibleKMesh, MagneticCrystalSymmetry, MagneticGroupType,
    MagneticTableColumns, SymmetryParameters,
};
pub use crate::cut::*;
pub use crate::error::{Result, TbError};
pub use crate::fermi_surface::*;
pub use crate::floquet::*;
use crate::generics::UseFloat;
pub use crate::geometry::*;
#[cfg(feature = "cryspglib")]
pub use crate::hamiltonian_symmetry::{
    BasisActionContext, BasisRepresentationError, BasisSymmetryRepresentation, CellShiftAction,
    FinalMagneticGroup, HamiltonianCompatibility, HamiltonianResidual, HamiltonianResidualWitness,
    HamiltonianSymmetrizationParameters, HamiltonianSymmetryCandidates,
    HamiltonianSymmetryCompleteness, HamiltonianSymmetryReport, HamiltonianSymmetryRequest,
    HamiltonianSymmetryTolerances, IdentifiedMagneticSubgroup, LocalizedBasisAction,
    OperationHamiltonianCheck, OperationHamiltonianStatus, ScalarSiteBasis,
};
pub use crate::hubbard::*;
pub use crate::io::*;
pub use crate::kpath::*;
pub use crate::kplane::*;
pub use crate::kpoints::*;
pub use crate::magnetic_field::*;
pub use crate::math::*;
pub use crate::model::*;
pub use crate::output::*;
pub use crate::quantum_geometry::*;
pub use crate::response::*;
pub use crate::solve_ham::Solve;
pub use crate::surfgreen::*;
pub use crate::thermodynamics::*;
pub use crate::unfold::*;
pub use crate::velocity::*;
pub use crate::wannier90::*;

// --- BLAS/LAPACK 后端(互斥,只能开一个) ---
//
// `ndarray-linalg` 默认不链接任何后端,而且多个后端同时启用时会在
// `intel-mkl-src`/`openblas-src`/`netlib-src` 或本 crate 的
// `ndarray_lapack` 中产生重复符号/重复 extern crate。这里在 const
// 求值阶段强制“有且仅有一个后端”。
const _: () = {
    let backends = cfg!(feature = "intel-mkl-static") as usize
        + cfg!(feature = "intel-mkl-system") as usize
        + cfg!(feature = "openblas-static") as usize
        + cfg!(feature = "openblas-system") as usize
        + cfg!(feature = "netlib-static") as usize
        + cfg!(feature = "netlib-system") as usize;
    assert!(
        backends <= 1,
        "Rustb: enable exactly one BLAS/LAPACK backend feature (intel-mkl-static, intel-mkl-system, openblas-static, openblas-system, netlib-static, netlib-system); they are mutually exclusive"
    );
    assert!(
        backends >= 1,
        "Rustb: no BLAS/LAPACK backend selected. Enable one backend feature (e.g. the default openblas-system, or use --features intel-mkl-system)"
    );
};

// --- 可选高性能分配器(互斥,只能开一个 feature) ---
#[cfg(all(feature = "mimalloc", feature = "jemalloc"))]
compile_error!("mimalloc and jemalloc are mutually exclusive — enable only one.");

#[cfg(feature = "mimalloc")]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

#[cfg(all(feature = "jemalloc", not(feature = "mimalloc")))]
#[global_allocator]
static GLOBAL: jemallocator::Jemalloc = jemallocator::Jemalloc;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::response::config::direction_matrix;
    use crate::solve_ham::Solve;
    use gnuplot::{AxesCommon, Color, Figure, Fix, Font, Major, PointSymbol, Rotate, TextOffset};

    use ndarray::prelude::*;
    use ndarray_linalg::*;
    use ndarray_linalg::{Eigh, UPLO};
    use num_complex::Complex;
    use rayon::prelude::*;
    use std::f64::consts::PI;
    use std::fs::File;
    use std::fs::create_dir_all;
    use std::io::Write;
    use std::time::Instant;

    fn fixed_direction<const DIM: usize>(direction: &Array1<f64>) -> [f64; DIM] {
        direction
            .as_slice()
            .expect("direction must be contiguous")
            .try_into()
            .expect("direction must match the model dimension")
    }

    fn fixed_k_mesh<const DIM: usize>(k_mesh: &Array1<usize>) -> [usize; DIM] {
        k_mesh
            .as_slice()
            .expect("k-mesh must be contiguous")
            .try_into()
            .expect("k-mesh must match the model dimension")
    }

    fn parameters_at_direction<const DIM: usize>(
        k_mesh: [usize; DIM],
        direction: Array2<f64>,
        chemical_potentials: &Array1<f64>,
        temperature_kelvin: f64,
    ) -> Parameters<DIM> {
        let mut params = Parameters::new(k_mesh, direction, chemical_potentials.clone());
        params.T = array![temperature_kelvin];
        params
    }

    fn hall_values<const SPIN: bool, const DIM: usize, R: RMatrixData>(
        model: &Model<SPIN, DIM, R>,
        k_mesh: &Array1<usize>,
        direction_a: &Array1<f64>,
        direction_b: &Array1<f64>,
        chemical_potentials: &Array1<f64>,
        temperature_kelvin: f64,
        spin: Option<SpinDirection>,
        broadening: f64,
        integration: Integration,
    ) -> Result<Array1<f64>> {
        let mut params = parameters_at_direction(
            fixed_k_mesh(k_mesh),
            direction_matrix::<2, DIM>(&[
                fixed_direction(direction_a),
                fixed_direction(direction_b),
            ]),
            chemical_potentials,
            temperature_kelvin,
        );
        params.spin = spin;
        params.eta = broadening;
        params.integration = integration;
        Ok(model.hall_conductivity(&params)?.conductivity)
    }

    fn hall_value<const SPIN: bool, const DIM: usize, R: RMatrixData>(
        model: &Model<SPIN, DIM, R>,
        k_mesh: &Array1<usize>,
        direction_a: &Array1<f64>,
        direction_b: &Array1<f64>,
        chemical_potential: f64,
        temperature_kelvin: f64,
        spin: Option<SpinDirection>,
        broadening: f64,
    ) -> Result<f64> {
        Ok(hall_values(
            model,
            k_mesh,
            direction_a,
            direction_b,
            &array![chemical_potential],
            temperature_kelvin,
            spin,
            broadening,
            Integration::Direct,
        )?[0])
    }

    fn band_berry_curvature<const SPIN: bool, const DIM: usize, R: RMatrixData>(
        model: &Model<SPIN, DIM, R>,
        k: &Array1<f64>,
        direction_a: &Array1<f64>,
        direction_b: &Array1<f64>,
        spin: Option<SpinDirection>,
        broadening: f64,
    ) -> BandBerryCurvature {
        let mut params = Parameters::rank2(
            [1; DIM],
            fixed_direction(direction_a),
            fixed_direction(direction_b),
            array![0.0],
        );
        params.spin = spin;
        params.eta = broadening;
        model.berry_curvature_at(k, &params).unwrap()
    }

    #[allow(clippy::too_many_arguments)]
    fn occupied_berry_curvature<const SPIN: bool, const DIM: usize, R: RMatrixData>(
        model: &Model<SPIN, DIM, R>,
        k_points: &Array2<f64>,
        direction_a: &Array1<f64>,
        direction_b: &Array1<f64>,
        chemical_potential: f64,
        temperature_kelvin: f64,
        spin: Option<SpinDirection>,
        broadening: f64,
    ) -> Array1<f64> {
        let mut params = Parameters::rank2(
            [1; DIM],
            fixed_direction(direction_a),
            fixed_direction(direction_b),
            array![chemical_potential],
        );
        params.T = array![temperature_kelvin];
        params.spin = spin;
        params.eta = broadening;
        model
            .occupied_berry_curvature_on(k_points, &params)
            .unwrap()
    }

    /// Temperature for direct NLH tests with nominal zero temperature:
    /// the old Fermi-smearing width `1/nk^(1/dim)` converted to the
    /// equivalent Fermi-Dirac temperature `width / k_B`.
    fn nonlinear_temperature(
        temperature_kelvin: f64,
        k_mesh: &Array1<usize>,
        integration: Integration,
    ) -> f64 {
        if temperature_kelvin > 0.0 {
            return temperature_kelvin;
        }
        if integration == Integration::EnergyCut {
            return 0.0;
        }
        let points_per_dimension = k_mesh.iter().product::<usize>() as f64;
        let points_per_dimension = points_per_dimension.powf(1.0 / k_mesh.len() as f64);
        let width = (1.0 / points_per_dimension).max(BOLTZMANN_CONSTANT_EV_PER_K);
        width / BOLTZMANN_CONSTANT_EV_PER_K
    }

    fn intrinsic_nonlinear_values<const SPIN: bool, const DIM: usize, R: RMatrixData>(
        model: &Model<SPIN, DIM, R>,
        k_mesh: &Array1<usize>,
        current: &Array1<f64>,
        field_1: &Array1<f64>,
        field_2: &Array1<f64>,
        chemical_potentials: &Array1<f64>,
        temperature_kelvin: f64,
        integration: Integration,
    ) -> Result<Array1<f64>> {
        let mut params = Parameters::rank3(
            fixed_k_mesh(k_mesh),
            fixed_direction(current),
            fixed_direction(field_1),
            fixed_direction(field_2),
            chemical_potentials.clone(),
        );
        params.T = array![nonlinear_temperature(
            temperature_kelvin,
            k_mesh,
            integration
        )];
        params.integration = integration;
        Ok(model.intrinsic_nonlinear_hall(&params)?.conductivity)
    }

    #[allow(clippy::too_many_arguments)]
    fn extrinsic_nonlinear_values<const SPIN: bool, const DIM: usize, R: RMatrixData>(
        model: &Model<SPIN, DIM, R>,
        k_mesh: &Array1<usize>,
        current: &Array1<f64>,
        field_1: &Array1<f64>,
        field_2: &Array1<f64>,
        chemical_potentials: &Array1<f64>,
        temperature_kelvin: f64,
        frequency: f64,
        spin: Option<SpinDirection>,
        broadening: f64,
        integration: Integration,
        field_symmetry: FieldSymmetry,
    ) -> Result<Array1<f64>> {
        let mut params = Parameters::rank3(
            fixed_k_mesh(k_mesh),
            fixed_direction(current),
            fixed_direction(field_1),
            fixed_direction(field_2),
            chemical_potentials.clone(),
        );
        params.T = array![nonlinear_temperature(
            temperature_kelvin,
            k_mesh,
            integration
        )];
        params.omega = array![frequency];
        params.spin = spin;
        params.eta = broadening;
        params.integration = integration;
        params.field_symmetry = field_symmetry;
        Ok(model.extrinsic_nonlinear_hall(&params)?.conductivity)
    }

    fn write_txt(data: Array2<f64>, output: &str) -> std::io::Result<()> {
        let mut file = File::create(output).expect("Unable to BAND.dat");
        let n = data.len_of(Axis(0));
        let s = data.len_of(Axis(1));
        let mut s0 = String::new();
        for i in 0..n {
            for j in 0..s {
                if data[[i, j]] >= 0.0 {
                    s0.push_str("     ");
                } else {
                    s0.push_str("    ");
                }
                let aa = format!("{:.6}", data[[i, j]]);
                s0.push_str(&aa);
            }
            s0.push_str("\n");
        }
        writeln!(file, "{}", s0)?;
        Ok(())
    }

    fn write_txt_1(data: Array1<f64>, output: &str) -> std::io::Result<()> {
        use std::fs::File;
        use std::io::Write;
        let mut file = File::create(output).expect("Unable to BAND.dat");
        let n = data.len_of(Axis(0));
        let mut s0 = String::new();
        for i in 0..n {
            if data[[i]] >= 0.0 {
                s0.push_str(" ");
            }
            let aa = format!("{:.6}\n", data[[i]]);
            s0.push_str(&aa);
        }
        writeln!(file, "{}", s0)?;
        Ok(())
    }
    #[test]
    fn test_rmatrix_grows_with_new_hoppings() {
        // Regression: set_hop/add_hop/add_element must keep rmatrix in sync with
        // hamR, otherwise validate() (and make_supercell/cut_*) reject
        // HasRMatrix models built with these methods.
        let lat = array![[1.0, 0.0], [0.0, 1.0]];
        let orb = array![[0.0, 0.0]];
        let mut model = Model::<false, 2, HasRMatrix>::tb_model(lat, orb, None).unwrap();

        // set_hop adds R=(1,0) and -R -> hamR grows to 3 rows
        model.set_hop(-1.0, 0, 0, &arr1(&[1isize, 0]), None);
        // add_hop adds R=(0,1) and -R -> hamR grows to 5 rows
        model.add_hop(-1.0, 0, 0, &arr1(&[0isize, 1]), None);
        // add_element adds R=(1,1) and -R -> hamR grows to 7 rows
        model
            .add_element(Complex::new(-0.2, 0.0), 0, 0, &arr1(&[1isize, 1]))
            .unwrap();

        // The rmatrix shape must match hamR.nrows() x DIM x nsta x nsta
        let expected = (model.hamR.nrows(), 2, model.nsta(), model.nsta());
        assert_eq!(
            model.rmatrix.as_array4().dim(),
            expected,
            "rmatrix must grow in sync with hamR"
        );
        // validate() must pass (previously returned InvalidModelInvariant)
        model.validate().unwrap();
        // Newly added hopping vectors must carry zero position-matrix blocks
        for i_r in 1..model.hamR.nrows() {
            let block = model.rmatrix.as_array4().index_axis(Axis(0), i_r);
            assert!(
                block.iter().all(|x| x.norm_sqr() == 0.0),
                "position matrix block for R={:?} must be zero",
                model.hamR.row(i_r)
            );
        }
        // make_supercell (documented HasRMatrix workflow) must work
        let supercell = model
            .make_supercell(&array![[2.0, 0.0], [0.0, 1.0]])
            .unwrap();
        supercell.validate().unwrap();
    }

    #[test]
    fn add_element_updates_existing_hopping_hermitically() {
        // Regression: updating an existing R with i == j, or an R = 0
        // off-diagonal element, wrote only one side and broke Hermiticity.
        let mut model = Model::<false, 2>::tb_model(
            array![[1.0, 0.0], [0.0, 1.0]],
            array![[0.0, 0.0], [0.5, 0.0]],
            None,
        )
        .unwrap();

        // Case 1: update an existing R != 0 hopping with i == j.
        model
            .add_element(Complex::new(1.0, 0.0), 0, 0, &array![1, 0])
            .unwrap();
        model
            .add_element(Complex::new(2.0, 0.0), 0, 0, &array![1, 0])
            .unwrap();
        let i_plus = find_R(&model.hamR, &array![1, 0]).unwrap();
        let i_minus = find_R(&model.hamR, &array![-1, 0]).unwrap();
        assert_eq!(model.ham[[i_plus, 0, 0]], Complex::new(2.0, 0.0));
        assert_eq!(
            model.ham[[i_minus, 0, 0]],
            Complex::new(2.0, 0.0),
            "H(-R) must follow H(R) when updating an existing hopping"
        );

        // Case 2: update an R = 0 off-diagonal element.
        model
            .add_element(Complex::new(0.3, 0.1), 0, 1, &array![0, 0])
            .unwrap();
        model
            .add_element(Complex::new(0.4, 0.2), 0, 1, &array![0, 0])
            .unwrap();
        let i_0 = find_R(&model.hamR, &array![0, 0]).unwrap();
        assert_eq!(model.ham[[i_0, 0, 1]], Complex::new(0.4, 0.2));
        assert_eq!(
            model.ham[[i_0, 1, 0]],
            Complex::new(0.4, -0.2),
            "R = 0 off-diagonal update must write the Hermitian conjugate"
        );
    }

    #[test]
    fn add_element_complex_onsite_errors_before_writing() {
        // Regression: the OnsiteHoppingMustBeReal error fired after the
        // Hamiltonian block was already written, corrupting the caller's
        // model. The error must leave the model untouched.
        let mut model =
            Model::<false, 2>::tb_model(array![[1.0, 0.0], [0.0, 1.0]], array![[0.0, 0.0]], None)
                .unwrap();
        let before = model.ham.clone();
        let result = model.add_element(Complex::new(1.0, 2.0), 0, 0, &array![0, 0]);
        assert!(matches!(result, Err(TbError::OnsiteHoppingMustBeReal(_))));
        for (a, b) in model.ham.iter().zip(before.iter()) {
            assert_eq!(a, b, "failed add_element must not modify the Hamiltonian");
        }
    }

    #[test]
    fn test_gen_v() {
        //判断两个Array1<f64> 是否足够接近
        fn are_arrays_close(a: &Array1<f64>, b: &Array1<f64>, tolerance: f64) -> bool {
            a.iter()
                .zip(b.iter())
                .all(|(&x, &y)| (x - y).abs() < tolerance)
        }

        //判断两个Array2<Compelx<f64>> 是否足够接近
        fn are_complex_arrays_close(
            a: &Array2<Complex<f64>>,
            b: &Array2<Complex<f64>>,
            tolerance: f64,
        ) -> bool {
            a.iter()
                .zip(b.iter())
                .all(|(&x, &y)| (x.re - y.re).abs() < tolerance && (x.im - y.im).abs() < tolerance)
        }
        let li: Complex<f64> = 1.0 * Complex::i();
        let t = 1.0;
        let delta = 0.0;
        let lat = arr2(&[[1.0, 0.0], [0.5, 3.0_f64.sqrt() / 2.0]]);
        let orb = arr2(&[[1.0 / 3.0, 1.0 / 3.0], [2.0 / 3.0, 2.0 / 3.0]]);
        let mut model = Model::<false, 2>::tb_model(lat, orb, None).unwrap();
        model.set_onsite(&arr1(&[-delta, delta]), None);
        let R0: Array2<isize> = arr2(&[[0, 0], [-1, 0], [0, -1]]);
        for (_i, R) in R0.axis_iter(Axis(0)).enumerate() {
            let R = R.to_owned();
            model.set_hop(t, 0, 1, &R, None);
        }
        assert_eq!(model.solve_band_onek(&array![0.0, 0.0]), array![-3.0, 3.0]);
        let result = model.solve_band_onek(&array![1.0 / 3.0, 2.0 / 3.0]);
        assert!(
            are_arrays_close(&result, &array![0.0, 0.0], 1e-5),
            "wrong!, the solve_band_onek get wrong result! please check it!"
        );
        let (result, _) = model.gen_v(&array![1.0 / 3.0, 1.0 / 3.0], Gauge::Atom);
        let resulty = array![
            [0.0 * li, -0.4698463103929542 - 0.17101007166283436 * li],
            [-0.4698463103929542 + 0.17101007166283436 * li, 0.0 * li]
        ];
        let resultx = array![
            [0.0 * li, -0.8137976813493737 - 0.2961981327260237 * li],
            [-0.8137976813493737 + 0.2961981327260237 * li, 0.0 * li]
        ];
        println!("result={}", result);
        assert!(
            are_complex_arrays_close(&result.slice(s![0, .., ..]).to_owned(), &resultx, 1e-8),
            "Wrong! the gen_v is get wrong results! please check it!"
        );
        assert!(
            are_complex_arrays_close(&result.slice(s![1, .., ..]).to_owned(), &resulty, 1e-8),
            "Wrong! the gen_v is get wrong results! please check it!"
        );

        let (result, _) = model.gen_v(&array![1.0 / 3.0, 1.0 / 3.0], Gauge::Lattice);
        let resultx = array![
            [
                0.0 * li,
                -3.0 * 3.0_f64.sqrt() / 4.0 * t + 3.0 / 4.0 * t * li
            ],
            [
                -3.0 * 3.0_f64.sqrt() / 4.0 * t - 3.0 / 4.0 * t * li,
                0.0 * li
            ]
        ];
        println!("result={}", &result - &resultx);
        assert!(
            are_complex_arrays_close(&result.slice(s![0, .., ..]).to_owned(), &resultx, 1e-8),
            "Wrong! the gen_v is get wrong results! please check it!"
        );

        let kvec = array![1.0 / 3.0, 1.0 / 3.0];
        let (band, evec) = model.solve_onek(&kvec);
        let ham = model.gen_ham(&kvec, Gauge::Atom);
        let evec_conj = evec.map(|x| x.conj());
        let evec = evec.t();
        let ham = ham.dot(&evec);
        let ham = evec_conj.dot(&ham);
        let new_band = ham.diag().map(|x| x.re);
        assert!(
            are_arrays_close(&new_band, &band, 1e-5),
            "wrong!, the solve_onek get wrong result! please check it!"
        );
    }
    #[test]
    fn conductivity_test() {
        //这个是用 Haldan 模型来测试
        let li: Complex<f64> = 1.0 * Complex::i();
        let t = -1.0 + 0.0 * li;
        let t2 = -1.0 + 0.0 * li;
        let delta = 0.7;
        let lat = arr2(&[[1.0, 0.0], [0.5, 3.0_f64.sqrt() / 2.0]]);
        let orb = arr2(&[[1.0 / 3.0, 1.0 / 3.0], [2.0 / 3.0, 2.0 / 3.0]]);
        let mut model = Model::<false, 2>::tb_model(lat, orb, None).unwrap();
        model.set_onsite(&arr1(&[-delta, delta]), None);
        let R0: Array2<isize> = arr2(&[[0, 0], [-1, 0], [0, -1]]);
        for (_i, R) in R0.axis_iter(Axis(0)).enumerate() {
            let R = R.to_owned();
            model.add_hop(t, 0, 1, &R, None);
        }
        let R0: Array2<isize> = arr2(&[[1, 0], [-1, 1], [0, -1]]);
        for (_i, R) in R0.axis_iter(Axis(0)).enumerate() {
            let R = R.to_owned();
            model.add_hop(t2 * li, 0, 0, &R, None);
        }
        let R0: Array2<isize> = arr2(&[[-1, 0], [1, -1], [0, 1]]);
        for (_i, R) in R0.axis_iter(Axis(0)).enumerate() {
            let R = R.to_owned();
            model.add_hop(t2 * li, 1, 1, &R, None);
        }
        let k_vec = array![1.0 / 3.0, 2.0 / 3.0];
        let dir_1 = array![1.0, 0.0];
        let dir_2 = array![0.0, 1.0];
        let mu = 0.0;
        let T = 0.0;
        let _og = 0.0;
        let spin = None;
        let eta = 1e-3;
        let band_berry = band_berry_curvature(&model, &k_vec, &dir_1, &dir_2, spin, eta);
        let result1 = band_berry
            .berry_curvature
            .iter()
            .zip(&band_berry.energies)
            .filter(|&(_, &energy)| energy <= mu)
            .map(|(&berry, _)| berry)
            .sum::<f64>()
            * (2.0 * PI);

        let mut k_list = Array2::zeros((9, 2));
        let dk = 0.0001;
        k_list.row_mut(0).assign(&(&k_vec + dk * &dir_1));
        k_list
            .row_mut(1)
            .assign(&(&k_vec + dk * &dir_1 + dk * &dir_2));
        k_list.row_mut(2).assign(&(&k_vec + dk * &dir_2));
        k_list
            .row_mut(3)
            .assign(&(&k_vec - dk * &dir_1 + dk * &dir_2));
        k_list.row_mut(4).assign(&(&k_vec - dk * &dir_1));
        k_list
            .row_mut(5)
            .assign(&(&k_vec - dk * &dir_1 - dk * &dir_2));
        k_list.row_mut(6).assign(&(&k_vec - dk * &dir_2));
        k_list
            .row_mut(7)
            .assign(&(&k_vec + dk * &dir_1 - dk * &dir_2));
        k_list.row_mut(8).assign(&(&k_vec + dk * &dir_1));
        let result2 = model.berry_loop(&k_list, &vec![0]);
        let result2 = result2[[0]] / (dk.powi(2)) / 4.0 / (2.0 * PI) * 3_f64.sqrt() / 2.0;
        println!("result2={},result1={}", result2, result1);
        assert!(
            (result2 - result1).abs() < 1e-4,
            "Wrong!, the berry_curvature or berry_flux mut be false"
        );
        // 单化学势构造器与一般化学势网格应给出相同结果。
        let kmesh = array![100, 100];
        let mu = -1.0;
        let a1 = hall_value(&model, &kmesh, &dir_2, &dir_1, mu, T, spin, eta).unwrap();
        let a2 = hall_values(
            &model,
            &kmesh,
            &dir_2,
            &dir_1,
            &array![mu],
            T,
            spin,
            eta,
            Integration::Direct,
        )
        .unwrap()[0];
        assert!(
            (a2 - a1).abs() < 1e-5,
            "single- and multi-chemical-potential Hall results differ"
        )
    }
    #[test]
    fn gen_v_speed_test() {
        println!("开始测试各个函数的运行速度, 用次近邻的石墨烯模型");
        let li: Complex<f64> = 1.0 * Complex::i();
        let t = 2.0 + 0.0 * li;
        let t2 = -1.0 + 0.0 * li;
        let delta = 0.7;
        let lat = arr2(&[[1.0, 0.0], [0.5, 3.0_f64.sqrt() / 2.0]]);
        let orb = arr2(&[[1.0 / 3.0, 1.0 / 3.0], [2.0 / 3.0, 2.0 / 3.0]]);
        let mut model = Model::<false, 2>::tb_model(lat, orb, None).unwrap();
        model.set_onsite(&arr1(&[-delta, delta]), None);
        let R0: Array2<isize> = arr2(&[[0, 0], [-1, 0], [0, -1]]);
        for (_i, R) in R0.axis_iter(Axis(0)).enumerate() {
            let R = R.to_owned();
            model.add_hop(t, 0, 1, &R, None);
        }
        let R0: Array2<isize> = arr2(&[[1, 0], [-1, 1], [0, -1]]);
        for (_i, R) in R0.axis_iter(Axis(0)).enumerate() {
            let R = R.to_owned();
            model.add_hop(t2 * li, 0, 0, &R, None);
        }
        let R0: Array2<isize> = arr2(&[[-1, 0], [1, -1], [0, 1]]);
        for (_i, R) in R0.axis_iter(Axis(0)).enumerate() {
            let R = R.to_owned();
            model.add_hop(t2 * li, 1, 1, &R, None);
        }
        println!("{:?}", model.atom_list());
        let U = array![[3.0, 0.0], [0.0, 3.0]];
        let model = model.make_supercell(&U).unwrap();

        let nk = 101;
        let k_mesh = array![nk, nk];
        let kvec = gen_kmesh(&k_mesh).unwrap();

        {
            println!("开始计算 gen_v 的耗时速度, 为了平均, 我们单线程求解gen_v");
            let start = Instant::now(); // 开始计时
            let _A: Vec<_> = kvec
                .outer_iter()
                .into_par_iter()
                .map(|x| {
                    let (a, _) = model.gen_v(&x.to_owned(), Gauge::Atom);
                    a
                })
                .collect();
            let end = Instant::now(); // 结束计时
            let duration = end.duration_since(start); // 计算执行时间
            println!(
                "run gen_v {} times took {} seconds",
                kvec.nrows(),
                duration.as_secs_f64()
            ); // 输出执行时间
        }
    }
    #[test]
    fn Haldan_model() {
        let li: Complex<f64> = 1.0 * Complex::i();
        let t = -1.0 + 0.0 * li;
        let t2 = -1.0 + 0.0 * li;
        let delta = 0.7;
        let lat = arr2(&[[1.0, 0.0], [0.5, 3.0_f64.sqrt() / 2.0]]);
        let orb = arr2(&[[1.0 / 3.0, 1.0 / 3.0], [2.0 / 3.0, 2.0 / 3.0]]);
        let mut model = Model::<false, 2>::tb_model(lat, orb, None).unwrap();
        model.set_onsite(&arr1(&[-delta, delta]), None);
        let R0: Array2<isize> = arr2(&[[0, 0], [-1, 0], [0, -1]]);
        for (_i, R) in R0.axis_iter(Axis(0)).enumerate() {
            let R = R.to_owned();
            model.add_hop(t, 0, 1, &R, None);
        }
        let R0: Array2<isize> = arr2(&[[1, 0], [-1, 1], [0, -1]]);
        for (_i, R) in R0.axis_iter(Axis(0)).enumerate() {
            let R = R.to_owned();
            model.add_hop(t2 * li, 0, 0, &R, None);
        }
        let R0: Array2<isize> = arr2(&[[-1, 0], [1, -1], [0, 1]]);
        for (_i, R) in R0.axis_iter(Axis(0)).enumerate() {
            let R = R.to_owned();
            model.add_hop(t2 * li, 1, 1, &R, None);
        }
        let nk: usize = 101;
        let path = [
            [0.0, 0.0],
            [2.0 / 3.0, 1.0 / 3.0],
            [0.5, 0.5],
            [1.0 / 3.0, 2.0 / 3.0],
            [0.0, 0.0],
        ];
        let path = arr2(&path);
        let (k_vec, _k_dist, _k_node) = model.k_path(&path, nk).unwrap();
        let (_eval, _evec) = model.solve_all_parallel(&k_vec);
        let label = vec!["G", "K", "M", "K'", "G"];
        model.show_band(&path, &label, nk, "tests/Haldan").unwrap();
        // --- Compute Hall conductivity ---
        let nk: usize = 31;
        let T: f64 = 0.0;
        let eta: f64 = 0.001;
        let og: f64 = 0.0;
        let mu: f64 = 0.0;
        let dir_1 = arr1(&[1.0, 0.0]);
        let dir_2 = arr1(&[0.0, 1.0]);
        let dir_3 = arr1(&[0.0, 1.0]);
        let spin = None;
        let kmesh = arr1(&[nk, nk]);

        let start = Instant::now(); // 开始计时
        let conductivity = hall_value(&model, &kmesh, &dir_1, &dir_2, mu, T, spin, eta).unwrap();
        let end = Instant::now(); // 结束计时
        let duration = end.duration_since(start); // 计算执行时间
        println!("quantom_Hall_effect={}", conductivity * (2.0 * PI));
        assert!(
            (conductivity * (2.0 * PI) - 1.0).abs() < 1e-3,
            "Wrong!, the Hall conductivity is wrong!"
        );
        println!("function_a took {} seconds", duration.as_secs_f64()); // 输出执行时间

        let mu = Array1::linspace(-2.0, 2.0, 101);
        let start = Instant::now(); // 开始计时
        let conductivity_mu = hall_values(
            &model,
            &kmesh,
            &dir_1,
            &dir_2,
            &mu,
            T,
            spin,
            eta,
            Integration::Direct,
        )
        .unwrap();
        let end = Instant::now(); // 结束计时
        let duration = end.duration_since(start); // 计算执行时间
        println!("quantom_Hall_effect={}", conductivity_mu[[50]] * (2.0 * PI));
        assert!(
            (conductivity_mu[[50]] - conductivity).abs() < 1e-3,
            "Wrong!, the Hall conductivity is wrong!, Hall_mu's result is {}, but Hall conductivity is {}",
            conductivity_mu[[50]],
            conductivity
        );
        println!("function_a took {} seconds", duration.as_secs_f64()); // 输出执行时间
        let conductivity = hall_value(&model, &kmesh, &dir_1, &dir_2, -2.0, T, spin, eta).unwrap();
        assert!(
            (conductivity_mu[[0]] - conductivity).abs() < 1e-3,
            "Wrong!, the Hall conductivity is wrong!, Hall_mu's result is {}, but Hall conductivity is {}",
            conductivity_mu[[0]],
            conductivity
        );
        let conductivity = hall_value(&model, &kmesh, &dir_1, &dir_2, 2.0, T, spin, eta).unwrap();
        assert!(
            (conductivity_mu[[100]] - conductivity).abs() < 1e-3,
            "Wrong!, the Hall conductivity is wrong!, Hall_mu's result is {}, but Hall conductivity is {}",
            conductivity_mu[[100]],
            conductivity
        );
        //开始绘图
        let mut fg = Figure::new();
        let x: Vec<f64> = mu.to_vec();
        let axes = fg.axes2d();
        let y: Vec<f64> = (conductivity_mu * 2.0 * PI).to_vec();
        axes.lines(&x, &y, &[Color("black")]);
        let _show_ticks = Vec::<String>::new();
        let mut pdf_name = String::new();
        pdf_name.push_str("tests/Haldan");
        pdf_name.push_str("/hall_mu.pdf");
        fg.set_terminal("pdfcairo", &pdf_name);
        fg.show().expect("failed to draw gnuplot figure");

        let mu = 0.0;
        let nk: usize = 31;
        let kmesh = arr1(&[nk, nk]);
        let start = Instant::now(); // 开始计时
        let conductivity = hall_value(&model, &kmesh, &dir_1, &dir_2, mu, T, spin, eta).unwrap();
        let end = Instant::now(); // 结束计时
        let duration = end.duration_since(start); // 计算执行时间
        println!("霍尔电导率{}", conductivity * (2.0 * PI));
        assert!(
            (conductivity * (2.0 * PI) - 1.0).abs() < 1e-3,
            "Wrong!, the Hall conductivity is wrong!"
        );
        println!("function_a took {} seconds", duration.as_secs_f64()); // 输出执行时间
        //画一下3000k的时候的费米导数分布
        let T = 100.0;
        let nk: usize = 101;
        let kmesh = arr1(&[nk, nk]);
        println!("{}", kmesh);
        let E_min = -3.0;
        let E_max = 3.0;
        let E_n = 1000;
        let mu = Array1::linspace(E_min, E_max, E_n);
        let occupation = Occupation::FermiDirac {
            temperature_kelvin: T,
        };
        let par_f = mu.mapv(|energy| occupation.minus_derivative(energy, 0.0).unwrap());
        let mut fg = Figure::new();
        let x: Vec<f64> = mu.to_vec();
        let axes = fg.axes2d();
        let y: Vec<f64> = par_f.to_vec();
        axes.lines(&x, &y, &[Color("black")]);
        let _show_ticks = Vec::<String>::new();
        let mut pdf_name = String::new();
        pdf_name.push_str("tests/Haldan");
        pdf_name.push_str("/par_f.pdf");
        fg.set_terminal("pdfcairo", &pdf_name);
        fg.show().expect("failed to draw gnuplot figure");

        //画一下omega_n 随能量的分布
        let kvec: Array2<f64> = gen_kmesh(&kmesh).unwrap();
        let _nk: usize = kvec.len_of(Axis(0));
        let (omega, band) =
            model.berry_curvature_dipole_n(&kvec, &dir_1, &dir_2, &dir_3, og, spin, eta);
        let omega = omega.into_raw_vec_and_offset().0;
        let omega = Array1::from(omega);
        let band = band.into_raw_vec_and_offset().0;
        let band = Array1::from(band);
        let mut fg = Figure::new();
        let x: Vec<f64> = band.to_vec();
        let axes = fg.axes2d();
        let y: Vec<f64> = omega.to_vec();
        axes.points(
            x.iter(),
            y.iter(),
            &[Color("black"), PointSymbol((".").chars().next().unwrap())],
        );
        let _show_ticks = Vec::<String>::new();
        let mut pdf_name = String::new();
        pdf_name.push_str("tests/Haldan");
        pdf_name.push_str("/omega_energy.pdf");
        fg.set_terminal("pdfcairo", &pdf_name);
        fg.show().expect("failed to draw gnuplot figure");

        //画一下表面态
        let nk = 101;
        let green = SurfGreen::from_Model(&model, 0, 1e-3, None).unwrap();
        let E_min = -3.0;
        let E_max = 3.0;
        let E_n = 101;
        let path = [[0.0], [0.5], [1.0]];
        let path = arr2(&path);
        let label = vec!["G", "M", "G"];
        green.show_surf_state("tests/Haldan/surf", &path, &label, nk, E_min, E_max, E_n, 0);

        //-----算一下wilson loop 的结果-----------------------
        let dir_1 = arr1(&[1.0, 0.0]);
        let dir_2 = arr1(&[0.0, 1.0]);
        let occ = vec![0];
        let wcc = model.wannier_centre(&occ, &array![0.0, 0.0], &dir_1, &dir_2, 101, 101);
        let nocc = occ.len();

        let mut fg = Figure::new();
        let x: Vec<f64> = Array1::<f64>::linspace(0.0, 1.0, 101).to_vec();
        let axes = fg.axes2d();
        for j in -1..2 {
            for i in 0..nocc {
                let a = wcc.row(i).to_owned() + (j as f64) * 2.0 * PI;
                let y: Vec<f64> = a.to_vec();
                axes.points(
                    &x,
                    &y,
                    &[
                        Color("black"),
                        gnuplot::PointSymbol('O'),
                        gnuplot::PointSize(0.2),
                    ],
                );
            }
        }
        let axes = axes.set_x_range(Fix(0.0), Fix(1.0));
        let axes = axes.set_y_range(Fix(0.0), Fix(2.0 * PI));
        let show_ticks = vec![
            Major(0.0, Fix("0")),
            Major(0.5, Fix("π")),
            Major(1.0, Fix("")),
        ];
        axes.set_x_ticks_custom(
            show_ticks.into_iter(),
            &[],
            &[Font("Times New Roman", 32.0)],
        );
        let show_ticks = vec![
            Major(0.0, Fix("0")),
            Major(PI, Fix("π")),
            Major(2.0 * PI, Fix("")),
        ];
        axes.set_y_ticks_custom(
            show_ticks.into_iter(),
            &[],
            &[Font("Times New Roman", 32.0)],
        );
        axes.set_x_label(
            "k_x",
            &[Font("Times New Roman", 32.0), TextOffset(0.0, -0.5)],
        );
        axes.set_y_label(
            "WCC",
            &[
                Font("Times New Roman", 32.0),
                Rotate(90.0),
                TextOffset(-1.0, 0.0),
            ],
        );
        let mut pdf_name = String::new();
        pdf_name.push_str("tests/Haldan/wcc.pdf");
        fg.set_terminal("pdfcairo", &pdf_name);
        fg.show().expect("failed to draw gnuplot figure");
        //-----------用 berry_flux 算一下
        let C = model
            .berry_flux(
                &occ,
                &array![0.0, 0.0],
                &array![1.0, 0.0],
                &array![0.0, 1.0],
                101,
                101,
            )
            .sum()
            / PI
            / 2.0;
        println!("The Chern number of Haldan model is {}", C);
    }

    /// Sanity check: at one k-point, the reusable Berry-curvature trait and
    /// the gauge-invariant velocity kernel give the same result.
    #[test]
    fn tetra_primitives_sanity() {
        let li = Complex::new(0.0, 1.0);
        let t = Complex::new(-1.0, 0.0);
        let t2 = Complex::new(-1.0, 0.0);
        let delta = 0.7;
        let lat = arr2(&[[1.0, 0.0], [0.5, 3.0_f64.sqrt() / 2.0]]);
        let orb = arr2(&[[1.0 / 3.0, 1.0 / 3.0], [2.0 / 3.0, 2.0 / 3.0]]);
        let mut model = Model::<false, 2>::tb_model(lat, orb, None).unwrap();
        model.set_onsite(&arr1(&[-delta, delta]), None);
        for &(i, j) in &[(0, 0), (-1, 0), (0, -1)] {
            model.add_hop(t, 0, 1, &arr1(&[i, j]), None);
        }
        for &(i, j) in &[(1, 0), (-1, 1), (0, -1)] {
            model.add_hop(t2 * li, 0, 0, &arr1(&[i, j]), None);
        }
        for &(i, j) in &[(-1, 0), (1, -1), (0, 1)] {
            model.add_hop(t2 * li, 1, 1, &arr1(&[i, j]), None);
        }

        let k = arr1(&[0.3, 0.4]);
        let dx = arr1(&[1.0, 0.0]);
        let dy = arr1(&[0.0, 1.0]);
        let eta = 0.01;

        // Reference: band-resolved Berry-curvature trait.
        let omega_ref = band_berry_curvature(&model, &k, &dx, &dy, None, eta).berry_curvature;
        // Tetra primitives
        let dv = Array1::zeros(2);
        let pt = model.compute_velocity_kernel(&k, &dx, &dy, Some(&dv), Gauge::Atom, None);

        // Compute Omega from tetra primitives for each band n
        let nsta = model.nsta();
        for n in 0..nsta {
            let mut omega_n = 0.0;
            for m in 0..nsta {
                if m == n {
                    continue;
                }
                let d = pt.band[[n]] - pt.band[[m]];
                omega_n -= 2.0 * pt.k_ab[[n, m]].im / (d.powi(2) + eta.powi(2));
            }
            println!(
                "band {n}: ref={:.6}, tetra={:.6}, diff={:.2e}",
                omega_ref[[n]],
                omega_n,
                (omega_ref[[n]] - omega_n).abs()
            );
            assert!(
                (omega_ref[[n]] - omega_n).abs() < 1e-6,
                "band {n}: ref={}, tetra={}",
                omega_ref[[n]],
                omega_n
            );
        }
        println!("PASSED");
    }

    /// Verify eigenbasis convention: `U^T · H · U^*` gives diag = band.
    /// If this test ever fails, the convention is broken everywhere.
    #[test]
    fn evec_transform_sanity() {
        let li = Complex::new(0.0, 1.0);
        let t = Complex::new(-1.0, 0.0);
        let t2 = Complex::new(-1.0, 0.0);
        let delta = 0.7;
        let lat = arr2(&[[1.0, 0.0], [0.5, 3.0_f64.sqrt() / 2.0]]);
        let orb = arr2(&[[1.0 / 3.0, 1.0 / 3.0], [2.0 / 3.0, 2.0 / 3.0]]);
        let mut model = Model::<false, 2>::tb_model(lat, orb, None).unwrap();
        model.set_onsite(&arr1(&[-delta, delta]), None);
        for &(i, j) in &[(0, 0), (-1, 0), (0, -1)] {
            model.add_hop(t, 0, 1, &arr1(&[i, j]), None);
        }
        for &(i, j) in &[(1, 0), (-1, 1), (0, -1)] {
            model.add_hop(t2 * li, 0, 0, &arr1(&[i, j]), None);
        }
        for &(i, j) in &[(-1, 0), (1, -1), (0, 1)] {
            model.add_hop(t2 * li, 1, 1, &arr1(&[i, j]), None);
        }

        let k = arr1(&[0.3, 0.4]);
        let ham = model.gen_ham(&k, Gauge::Atom);
        let (band, evec) = ham.eigh(UPLO::Lower).unwrap();
        let ut = evec.t();
        let uc = evec.map(|x| x.conj());

        // U^T · H · U^* should be diagonal with eigenvalues
        let diag = ut.dot(&ham.dot(&uc));
        for i in 0..model.nsta() {
            for j in 0..model.nsta() {
                if i == j {
                    assert!(
                        (diag[[i, j]].re - band[[i]]).abs() < 1e-10,
                        "diag[{i},{i}]={} != band[{i}]={}",
                        diag[[i, i]].re,
                        band[[i]]
                    );
                } else {
                    assert!(
                        diag[[i, j]].norm() < 1e-10,
                        "off-diag[{i},{j}]={} != 0",
                        diag[[i, j]]
                    );
                }
            }
        }
        println!("evec_transform_sanity: U^T H U^* = diag(band) ✓");
    }

    #[test]
    fn graphene() {
        let li: Complex<f64> = 1.0 * Complex::i();
        let t1 = 1.0 + 0.0 * li;
        let t2 = 0.0 + 0.0 * li;
        let t3 = 0.0 + 0.0 * li;
        let delta = 0.0;
        let lat = arr2(&[[3.0_f64.sqrt(), -1.0], [3.0_f64.sqrt(), 1.0]]);
        let orb = arr2(&[[0.0, 0.0], [1.0 / 3.0, 1.0 / 3.0]]);
        let mut model = Model::<false, 2>::tb_model(lat, orb, None).unwrap();
        model.set_onsite(&arr1(&[delta, -delta]), None);
        model.add_hop(t1, 0, 1, &array![0, 0], None);
        model.add_hop(t1, 0, 1, &array![-1, 0], None);
        model.add_hop(t1, 0, 1, &array![0, -1], None);
        model.add_hop(t2, 0, 0, &array![1, 0], None);
        model.add_hop(t2, 1, 1, &array![1, 0], None);
        model.add_hop(t2, 0, 0, &array![0, 1], None);
        model.add_hop(t2, 1, 1, &array![0, 1], None);
        model.add_hop(t2, 0, 0, &array![1, -1], None);
        model.add_hop(t2, 1, 1, &array![1, -1], None);
        model.add_hop(t3, 0, 1, &array![1, -1], None);
        model.add_hop(t3, 0, 1, &array![-1, 1], None);
        model.add_hop(t3, 0, 1, &array![-1, -1], None);
        let nk: usize = 101;
        let path = [[0.0, 0.0], [2.0 / 3.0, 1.0 / 3.0], [0.5, 0.5], [0.0, 0.0]];
        let path = arr2(&path);
        let (k_vec, _k_dist, _k_node) = model.k_path(&path, nk).unwrap();
        let (_eval, _evec) = model.solve_all_parallel(&k_vec);
        let label = vec!["G", "K", "M", "G"];
        model
            .show_band(&path, &label, nk, "tests/graphene")
            .unwrap();

        // 开始计算两个本征态
        let k1 = array![1.0 / 3.0 - 0.002, 2.0 / 3.0];
        let k2 = array![1.0 / 3.0 + 0.001, 2.0 / 3.0];
        let (eval1, evec1) = model.solve_onek(&k1);
        let (eval2, evec2) = model.solve_onek(&k2);
        let evec1 = evec1.reversed_axes();
        let evec2 = evec2.mapv(|x| x.conj());
        println!("{},{}", eval1, eval2);
        println!("{}", evec2.dot(&evec1).mapv(|x| x.norm().round()));

        // --- Compute Hall conductivity ---
        let nk: usize = 11;
        let T: f64 = 0.0;
        let eta: f64 = 0.001;
        let _og: f64 = 0.0;
        let mu: f64 = 0.0;
        let dir_1 = arr1(&[1.0, 0.0]);
        let dir_2 = arr1(&[0.0, 1.0]);
        let spin = None;
        let kmesh = arr1(&[nk, nk]);
        let (_eval, _evec) = model.solve_onek(&arr1(&[0.3, 0.5]));
        let _conductivity = hall_value(&model, &kmesh, &dir_1, &dir_2, mu, T, spin, eta);
        //println!("{}",conductivity/(2.0*PI));
        //开始计算边缘态, 首先是zigsag态
        let nk: usize = 501;
        let U = arr2(&[[1.0, 1.0], [-1.0, 1.0]]);
        let super_model = model.make_supercell(&U).unwrap();
        let zig_model = super_model.cut_piece(100, 0).unwrap();
        let path = [[0.0, 0.0], [0.0, 0.5], [0.0, 1.0]];
        //let path=[[0.0,0.0],[0.5,0.0],[1.0,0.0]];
        //let path=[[0.0,0.0],[0.5,0.0],[0.5,0.5],[0.0,0.5],[0.0,0.0]];
        let path = arr2(&path);
        let (k_vec, _k_dist, _k_node) = super_model.k_path(&path, nk).unwrap();
        let (_eval, _evec) = super_model.solve_all_parallel(&k_vec);
        //let label=vec!["G","X","M","Y","G"];
        let label = vec!["G", "M", "G"];
        zig_model
            .show_band(&path, &label, nk, "tests/graphene_zig")
            .unwrap();

        //开始计算石墨烯的态密度
        let nk: usize = 51;
        let kmesh = arr1(&[nk, nk]);
        let E_min = -3.0;
        let E_max = 3.0;
        let E_n = 1000;
        let (E0, dos) = model.dos(&kmesh, E_min, E_max, E_n, 1e-2).unwrap();
        //开始绘制dos
        let mut fg = Figure::new();
        let x: Vec<f64> = E0.to_vec();
        let axes = fg.axes2d();
        let y: Vec<f64> = dos.to_vec();
        axes.lines(&x, &y, &[Color("black")]);
        let _show_ticks = Vec::<String>::new();
        let mut pdf_name = String::new();
        pdf_name.push_str("tests/graphene");
        pdf_name.push_str("/dos.pdf");
        fg.set_terminal("pdfcairo", &pdf_name);
        fg.show().expect("failed to draw gnuplot figure");

        //开始计算非线性霍尔电导
        let dir_1 = arr1(&[1.0, 0.0]);
        let dir_2 = arr1(&[0.0, 1.0]);
        let dir_3 = arr1(&[1.0, 0.0]);
        let og = 0.0;
        let mu = Array1::linspace(E_min, E_max, E_n);
        let T = 300.0;
        let sigma = extrinsic_nonlinear_values(
            &model,
            &kmesh,
            &dir_1,
            &dir_2,
            &dir_3,
            &mu,
            T,
            og,
            None,
            1e-5,
            Integration::Direct,
            FieldSymmetry::Ordered,
        )
        .unwrap();

        //开始绘制非线性电导
        let mut fg = Figure::new();
        let x: Vec<f64> = mu.to_vec();
        let axes = fg.axes2d();
        let y: Vec<f64> = sigma.to_vec();
        axes.lines(&x, &y, &[Color("black")]);
        let _show_ticks = Vec::<String>::new();
        let mut pdf_name = String::new();
        pdf_name.push_str("tests/graphene");
        pdf_name.push_str("/nonlinear_ex.pdf");
        fg.set_terminal("pdfcairo", &pdf_name);
        fg.show().expect("failed to draw gnuplot figure");
    }

    #[test]
    fn kane_mele() {
        let li: Complex<f64> = 1.0 * Complex::i();
        let t = -1.0;
        let delta = 0.0;
        let _alter = 0.0 + 0.0 * li;
        let soc = 0.06 * t;
        let rashba = 0.0 * t;
        let lat = arr2(&[[1.0, 0.0], [0.5, 3.0_f64.sqrt() / 2.0]]);
        let orb = arr2(&[[1.0 / 3.0, 1.0 / 3.0], [2.0 / 3.0, 2.0 / 3.0]]);
        // Honeycomb A/B sublattice sites, one orbital each
        let atoms = vec![
            Atom::with_orbitals(
                arr1(&[1.0 / 3.0, 1.0 / 3.0]),
                AtomType::C,
                [OrbitalId::new(0)],
            ),
            Atom::with_orbitals(
                arr1(&[2.0 / 3.0, 2.0 / 3.0]),
                AtomType::C,
                [OrbitalId::new(1)],
            ),
        ];
        let mut model = Model::<true, 2>::tb_model(lat, orb, Some(atoms)).unwrap();
        model.set_onsite(&arr1(&[delta, -delta]), None);
        let R0: Array2<isize> = arr2(&[[0, 0], [-1, 0], [0, -1]]);
        for (_i, R) in R0.axis_iter(Axis(0)).enumerate() {
            let R = R.to_owned();
            model.set_hop(t, 0, 1, &R, None);
        }
        let R0: Array2<isize> = arr2(&[[1, 0], [-1, 1], [0, -1]]);
        for (_i, R) in R0.axis_iter(Axis(0)).enumerate() {
            let R = R.to_owned();
            model.set_hop(soc * li, 0, 0, &R, SpinDirection::Z);
        }
        let R0: Array2<isize> = arr2(&[[-1, 0], [1, -1], [0, 1]]);
        for (_i, R) in R0.axis_iter(Axis(0)).enumerate() {
            let R = R.to_owned();
            model.set_hop(soc * li, 1, 1, &R, SpinDirection::Z);
        }
        //加入rashba项
        let R0: Array2<isize> = arr2(&[[1, 0], [-1, 1], [0, -1]]);
        for (_i, R) in R0.axis_iter(Axis(0)).enumerate() {
            let R = R.to_owned();
            let r0 = R.map(|x| *x as f64).dot(&model.lat);
            model.add_hop(rashba * li * r0[[1]], 0, 0, &R, SpinDirection::X);
            model.add_hop(rashba * li * r0[[0]], 0, 0, &R, SpinDirection::Y);
        }

        let R0: Array2<isize> = arr2(&[[-1, 0], [1, -1], [0, 1]]);
        for (_i, R) in R0.axis_iter(Axis(0)).enumerate() {
            let R = R.to_owned();
            let r0 = R.map(|x| *x as f64).dot(&model.lat);
            model.add_hop(-rashba * li * r0[[1]], 1, 1, &R, SpinDirection::X);
            model.add_hop(-rashba * li * r0[[0]], 1, 1, &R, SpinDirection::Y);
        }
        let nk: usize = 101;
        let path = [
            [0.0, 0.0],
            [2.0 / 3.0, 1.0 / 3.0],
            [0.5, 0.5],
            [1.0 / 3.0, 2.0 / 3.0],
            [0.0, 0.0],
        ];
        let path = arr2(&path);
        let (k_vec, _k_dist, _k_node) = model.k_path(&path, nk).unwrap();
        let (_eval, _evec) = model.solve_all_parallel(&k_vec);
        let label = vec!["G", "K", "M", "K'", "G"];
        model.show_band(&path, &label, nk, "tests/kane").unwrap();
        //开始计算超胞

        let super_model = model.cut_piece(50, 0).unwrap();
        let path = [[0.0, 0.0], [0.0, 0.5], [0.0, 1.0]];
        let path = arr2(&path);
        let label = vec!["G", "M", "G"];
        super_model
            .show_band(&path, &label, nk, "tests/kane_super")
            .unwrap();
        //开始计算表面态
        let nk = 101;
        let green = SurfGreen::from_Model(&model, 0, 1e-3, None).unwrap();
        let E_min = -1.0;
        let E_max = 1.0;
        let E_n = 101;
        let path = [[0.0], [0.5], [1.0]];
        let path = arr2(&path);
        let label = vec!["G", "M", "G"];
        green.show_surf_state("tests/kane", &path, &label, nk, E_min, E_max, E_n, 0);

        //-----算一下wilson loop 结果-----------------------
        let n = 51;
        let dir_1 = arr1(&[1.0, 0.0]);
        let dir_2 = arr1(&[0.0, 1.0]);
        let occ = vec![0, 1];
        let wcc = model.wannier_centre(&occ, &array![0.0, 0.0], &dir_1, &dir_2, n, n);
        let nocc = occ.len();
        let mut fg = Figure::new();
        let x: Vec<f64> = Array1::<f64>::linspace(0.0, 1.0, n).to_vec();
        let axes = fg.axes2d();
        for j in -1..2 {
            for i in 0..nocc {
                let a = wcc.row(i).to_owned() + (j as f64) * 2.0 * PI;
                let y: Vec<f64> = a.to_vec();
                axes.points(&x, &y, &[Color("black"), gnuplot::PointSymbol('O')]);
            }
        }
        let axes = axes.set_x_range(Fix(0.0), Fix(1.0));
        let axes = axes.set_y_range(Fix(0.0), Fix(2.0 * PI));
        let show_ticks = vec![
            Major(0.0, Fix("0")),
            Major(0.5, Fix("π")),
            Major(1.0, Fix("")),
        ];
        axes.set_x_ticks_custom(
            show_ticks.into_iter(),
            &[],
            &[Font("Times New Roman", 32.0)],
        );
        let show_ticks = vec![
            Major(0.0, Fix("0")),
            Major(PI, Fix("π")),
            Major(2.0 * PI, Fix("")),
        ];
        axes.set_y_ticks_custom(
            show_ticks.into_iter(),
            &[],
            &[Font("Times New Roman", 32.0)],
        );
        axes.set_x_label(
            "k_x",
            &[Font("Times New Roman", 32.0), TextOffset(0.0, -0.5)],
        );
        axes.set_y_label(
            "WCC",
            &[
                Font("Times New Roman", 32.0),
                Rotate(90.0),
                TextOffset(-1.0, 0.0),
            ],
        );
        let mut pdf_name = String::new();
        pdf_name.push_str("tests/kane/wcc.pdf");
        fg.set_terminal("pdfcairo", &pdf_name);
        fg.show().expect("failed to draw gnuplot figure");

        // --- Compute Hall conductivity ---
        let nk: usize = 31;
        let T: f64 = 0.0;
        let eta: f64 = 0.001;
        let _og: f64 = 0.0;
        let mu: f64 = 0.0;
        //let dir_1=arr1(&[3.0_f64.sqrt()/2.0,-0.5]);
        let dir_1 = arr1(&[1.0, 0.0]);
        let dir_2 = arr1(&[0.0, 1.0]);
        let spin = Some(SpinDirection::Z);
        let kmesh = arr1(&[nk, nk]);
        let start = Instant::now(); // 开始计时
        let conductivity = hall_value(&model, &kmesh, &dir_1, &dir_2, mu, T, spin, eta).unwrap();
        let end = Instant::now(); // 结束计时
        let duration = end.duration_since(start); // 计算执行时间
        println!("{}", conductivity * (2.0 * PI));
        println!("function_a took {} seconds", duration.as_secs_f64()); // 输出执行时间
        let nk: usize = 21;
        let kmesh = arr1(&[nk, nk]);
        let start = Instant::now(); // 开始计时
        let conductivity = hall_value(&model, &kmesh, &dir_1, &dir_2, mu, T, spin, eta).unwrap();
        let end = Instant::now(); // 结束计时
        let duration = end.duration_since(start); // 计算执行时间
        println!("{}", conductivity * (2.0 * PI));
        println!("function_a took {} seconds", duration.as_secs_f64()); // 输出执行时间

        let (E0, dos) = model.dos(&kmesh, E_min, E_max, E_n, 1e-2).unwrap();
        //开始绘制dos
        let mut fg = Figure::new();
        let x: Vec<f64> = E0.to_vec();
        let axes = fg.axes2d();
        let y: Vec<f64> = dos.to_vec();
        axes.lines(&x, &y, &[Color("black")]);
        let _show_ticks = Vec::<String>::new();
        let mut pdf_name = String::new();
        pdf_name.push_str("tests/kane");
        pdf_name.push_str("/dos.pdf");
        fg.set_terminal("pdfcairo", &pdf_name);
        fg.show().expect("failed to draw gnuplot figure");
        //绘制非线性霍尔电导的平面图

        //画一下贝利曲率的分布
        let nk: usize = 31;
        let kmesh = arr1(&[nk, nk]);
        let kvec = gen_kmesh(&kmesh).unwrap();
        //let kvec=kvec-0.5;
        let kvec = kvec * 2.0;
        let kvec = model.lat.dot(&(kvec.reversed_axes()));
        let kvec = kvec.reversed_axes();
        let berry_curv = occupied_berry_curvature(
            &model,
            &kvec,
            &dir_1,
            &dir_2,
            0.0,
            T,
            Some(SpinDirection::X),
            1e-3,
        );
        let data = berry_curv.to_shape((nk, nk)).unwrap();
        draw_heatmap(
            &(-data).map(|x| (x + 1.0).log(10.0)),
            "./tests/kane/berry_curvature_distribution.pdf",
        );

        //开始考虑磁场, 加入磁性
        let B = 0.1 + 0.0 * li;
        let tha = 0.0 / 180.0 * PI;

        model.add_hop(B * tha.cos(), 0, 0, &array![0, 0], SpinDirection::X);
        model.add_hop(B * tha.cos(), 1, 1, &array![0, 0], SpinDirection::X);
        model.add_hop(B * tha.sin(), 0, 0, &array![0, 0], SpinDirection::Y);
        model.add_hop(B * tha.sin(), 1, 1, &array![0, 0], SpinDirection::Y);
        //考虑添加onsite 项破坏空间反演和mirror

        let green = SurfGreen::from_Model(&model, 0, 1e-3, None).unwrap();
        let E_min = -1.0;
        let E_max = 1.0;
        let E_n = nk;
        let path = [[0.0], [0.5], [1.0]];
        let path = arr2(&path);
        let label = vec!["G", "M", "G"];
        green.show_surf_state(
            "tests/kane/magnetic",
            &path,
            &label,
            nk,
            E_min,
            E_max,
            E_n,
            0,
        );

        //-----算一下wilson loop 结果-----------------------
        let n = 51;
        let dir_1 = arr1(&[1.0, 0.0]);
        let dir_2 = arr1(&[0.0, 1.0]);
        let occ = vec![0, 1];
        let wcc = model.wannier_centre(&occ, &array![0.0, 0.0], &dir_1, &dir_2, n, n);
        let nocc = occ.len();
        let mut fg = Figure::new();
        let x: Vec<f64> = Array1::<f64>::linspace(0.0, 1.0, n).to_vec();
        let axes = fg.axes2d();
        for j in -1..2 {
            for i in 0..nocc {
                let a = wcc.row(i).to_owned() + (j as f64) * 2.0 * PI;
                let y: Vec<f64> = a.to_vec();
                axes.points(&x, &y, &[Color("black"), gnuplot::PointSymbol('O')]);
            }
        }
        let axes = axes.set_x_range(Fix(0.0), Fix(1.0));
        let axes = axes.set_y_range(Fix(0.0), Fix(2.0 * PI));
        let show_ticks = vec![
            Major(0.0, Fix("0")),
            Major(0.5, Fix("π")),
            Major(1.0, Fix("")),
        ];
        axes.set_x_ticks_custom(
            show_ticks.into_iter(),
            &[],
            &[Font("Times New Roman", 32.0)],
        );
        let show_ticks = vec![
            Major(0.0, Fix("0")),
            Major(PI, Fix("π")),
            Major(2.0 * PI, Fix("")),
        ];
        axes.set_y_ticks_custom(
            show_ticks.into_iter(),
            &[],
            &[Font("Times New Roman", 32.0)],
        );
        axes.set_x_label(
            "k_x",
            &[Font("Times New Roman", 32.0), TextOffset(0.0, -0.5)],
        );
        axes.set_y_label(
            "WCC",
            &[
                Font("Times New Roman", 32.0),
                Rotate(90.0),
                TextOffset(-1.0, 0.0),
            ],
        );
        let mut pdf_name = String::new();
        pdf_name.push_str("tests/kane/magnetic/wcc.pdf");
        fg.set_terminal("pdfcairo", &pdf_name);
        fg.show().expect("failed to draw gnuplot figure");

        //开始计算角态
        let model = model
            .make_supercell(&array![[0.0, -1.0], [1.0, 0.0]])
            .unwrap();
        let num = 19;
        /*
        let model_1=model.cut_piece(num,0).unwrap();
        let new_model=model_1.cut_piece(num,1);
        */
        let new_model = model.cut_dot(num, 6, None).unwrap();
        let _s = 0;
        let start = Instant::now();
        let (band, evec) = new_model.solve_range_onek(&arr1(&[0.0, 0.0]), (-0.3, 0.3), 1e-5);
        let end = Instant::now(); // 结束计时
        let duration = end.duration_since(start); // 计算执行时间
        println!("solve_band_all took {} seconds", duration.as_secs_f64()); // 输出执行时间
        let nresults = band.len();
        let show_evec = evec.to_owned().map(|x| x.norm_sqr());
        let mut size = Array2::<f64>::zeros((new_model.nsta(), new_model.natom()));
        let _norb = new_model.norb();
        for i in 0..nresults {
            let mut s = 0;
            for j in 0..new_model.natom() {
                for _k in 0..new_model.atoms[j].norb() {
                    size[[i, j]] += show_evec[[i, s]] + show_evec[[i, s + new_model.norb()]];
                    s += 1;
                }
            }
        }

        let show_str = new_model.atom_position().dot(&model.lat);
        let show_str = show_str.slice(s![.., 0..2]).to_owned();
        let _show_size = size.row(new_model.norb()).to_owned();
        create_dir_all("tests/kane/magnetic").expect("can't creat the file");
        write_txt_1(band, "tests/kane/magnetic/band.txt").expect("write_txt failed");
        write_txt(size, "tests/kane/magnetic/evec.txt").expect("write_txt failed");
        write_txt(show_str, "tests/kane/magnetic/structure.txt").expect("write_txt failed");
        //开始绘制角态
    }

    #[test]
    fn Enonlinear() {
        //! arxiv:1706.07702
        //! Test for the extrinsic nonlinear Hall conductivity.
        let li: Complex<f64> = 1.0 * Complex::i();
        let delta = 0.;
        let t1 = 1.0 + 0.0 * li;
        let t2 = 0.2 * t1;
        let t3 = 0.2 * t1;
        let lat = arr2(&[
            [1.0, 0.0, 0.0],
            [0.5, 3.0_f64.sqrt() / 2.0, 0.0],
            [0.0, 0.0, 1.0],
        ]);
        let orb = arr2(&[[1.0 / 3.0, 1.0 / 3.0, 0.0], [2.0 / 3.0, 2.0 / 3.0, 0.0]]);
        let mut model = Model::<false>::tb_model(lat, orb, None).unwrap();
        model.set_onsite(&arr1(&[delta, -delta]), None);
        let R0: Array2<isize> = arr2(&[[0, 0, 0], [-1, 0, 0], [0, -1, 0]]);
        for (_i, R) in R0.axis_iter(Axis(0)).enumerate() {
            let R = R.to_owned();
            model.set_hop(t1, 0, 1, &R, None);
        }
        let R0: Array2<isize> = arr2(&[[1, 0, 1], [-1, 1, 1], [0, -1, 1]]);
        for (_i, R) in R0.axis_iter(Axis(0)).enumerate() {
            let R = R.to_owned();
            model.set_hop(t2, 0, 0, &R, None);
        }
        let R0: Array2<isize> = arr2(&[[1, 0, -1], [-1, 1, -1], [0, -1, -1]]);
        for (_i, R) in R0.axis_iter(Axis(0)).enumerate() {
            let R = R.to_owned();
            model.set_hop(t2, 1, 1, &R, None);
        }
        let R = arr1(&[0, 0, 1]);
        model.set_hop(t3, 0, 0, &R, None);
        model.set_hop(t3, 1, 1, &R, None);
        let path = array![
            [0.0, 0.0, 0.0],
            [1.0 / 3.0, 2.0 / 3.0, 0.0],
            [0.5, 0.5, 0.0],
            [0.0, 0.0, 0.0],
            [1.0 / 3.0, 2.0 / 3.0, 0.0],
            [1.0 / 3.0, 2.0 / 3.0, 0.5],
            [0.0, 0.0, 0.0],
            [0.0, 0.0, 0.5],
            [1.0 / 3.0, 2.0 / 3.0, 0.5],
            [0.5, 0.5, 0.5],
            [0.0, 0.0, 0.5]
        ];
        let label = vec!["G", "K", "M", "G", "K", "H", "G", "A", "H", "L", "A"];
        let nk = 101;
        model
            .show_band(&path, &label, nk, "tests/Enonlinear")
            .unwrap();

        //开始计算非线性霍尔电导
        let dir_1 = arr1(&[1.0, 0.0, 0.0]);
        let dir_2 = arr1(&[0.0, 1.0, 0.0]);
        let dir_3 = arr1(&[0.0, 0.0, 1.0]);
        let nk: usize = 21;
        let kmesh = arr1(&[nk, nk, nk]);
        let E_min = -3.0;
        let E_max = 3.0;
        let E_n = 1000;
        let og = 0.0;
        let mu = Array1::linspace(E_min, E_max, E_n);
        let T = 30.0;
        let sigma = extrinsic_nonlinear_values(
            &model,
            &kmesh,
            &dir_1,
            &dir_2,
            &dir_3,
            &mu,
            T,
            og,
            None,
            1e-5,
            Integration::Direct,
            FieldSymmetry::Ordered,
        )
        .unwrap();

        //开始绘制非线性电导
        let mut fg = Figure::new();
        let x: Vec<f64> = mu.to_vec();
        let axes = fg.axes2d();
        let y: Vec<f64> = sigma.to_vec();
        axes.lines(&x, &y, &[Color("black")]);
        axes.set_y_range(Fix(-10.0), Fix(10.0));
        axes.set_x_range(Fix(E_min), Fix(E_max));
        let _show_ticks = Vec::<String>::new();
        let mut pdf_name = String::new();
        pdf_name.push_str("tests/Enonlinear");
        pdf_name.push_str("/nonlinear_ex.pdf");
        fg.set_terminal("pdfcairo", &pdf_name);
        fg.show().expect("failed to draw gnuplot figure");

        let sigma = intrinsic_nonlinear_values(
            &model,
            &kmesh,
            &dir_1,
            &dir_2,
            &dir_3,
            &mu,
            T,
            Integration::Direct,
        )
        .unwrap();
        //开始绘制非线性电导
        let mut fg = Figure::new();
        let x: Vec<f64> = mu.to_vec();
        let axes = fg.axes2d();
        let y: Vec<f64> = sigma.to_vec();
        axes.lines(&x, &y, &[Color("black")]);
        axes.set_y_range(Fix(-10.0), Fix(10.0));
        axes.set_x_range(Fix(E_min), Fix(E_max));
        let _show_ticks = Vec::<String>::new();
        let mut pdf_name = String::new();
        pdf_name.push_str("tests/Enonlinear");
        pdf_name.push_str("/nonlinear_in.pdf");
        fg.set_terminal("pdfcairo", &pdf_name);
        fg.show().expect("failed to draw gnuplot figure");

        let (E0, dos) = model.dos(&kmesh, E_min, E_max, E_n, 1e-2).unwrap();
        //开始绘制dos
        let mut fg = Figure::new();
        let x: Vec<f64> = E0.to_vec();
        let axes = fg.axes2d();
        let y: Vec<f64> = dos.to_vec();
        axes.lines(&x, &y, &[Color("black")]);
        let _show_ticks = Vec::<String>::new();
        let mut pdf_name = String::new();
        pdf_name.push_str("tests/Enonlinear");
        pdf_name.push_str("/dos.pdf");
        fg.set_terminal("pdfcairo", &pdf_name);
        fg.show().expect("failed to draw gnuplot figure");
    }
    #[test]
    fn kagome() {
        let li: Complex<f64> = 1.0 * Complex::i();
        let t1 = 1.0 + 0.0 * li;
        let _t2 = 0.1 + 0.0 * li;
        let lat = arr2(&[[3.0_f64.sqrt(), -1.0], [3.0_f64.sqrt(), 1.0]]);
        let orb = arr2(&[[0.0, 0.0], [1.0 / 3.0, 0.0], [0.0, 1.0 / 3.0]]);
        let mut model = Model::<false, 2>::tb_model(lat, orb, None).unwrap();
        //最近邻hopping
        model.add_hop(t1, 0, 1, &array![0, 0], None);
        model.add_hop(t1, 2, 0, &array![0, 0], None);
        model.add_hop(t1, 1, 2, &array![0, 0], None);
        model.add_hop(t1, 0, 2, &array![0, -1], None);
        model.add_hop(t1, 0, 1, &array![-1, 0], None);
        model.add_hop(t1, 2, 1, &array![-1, 1], None);
        let nk: usize = 101;
        let path = [[0.0, 0.0], [2.0 / 3.0, 1.0 / 3.0], [0.5, 0.], [0.0, 0.0]];
        let path = arr2(&path);
        let label = vec!["G", "K", "M", "G"];
        model.show_band(&path, &label, nk, "tests/kagome/").unwrap();
        //start to draw the band structure
        //Starting to calculate the edge state, first is the zigzag state
        let nk: usize = 101;
        let U = arr2(&[[1.0, 1.0], [-1.0, 1.0]]);
        let super_model = model.make_supercell(&U).unwrap();
        let zig_model = super_model.cut_piece(30, 0).unwrap();
        let path = [[0.0, 0.0], [0.0, 0.5], [0.0, 1.0]];
        let path = arr2(&path);
        let (k_vec, _k_dist, _k_node) = super_model.k_path(&path, nk).unwrap();
        let (_eval, _evec) = super_model.solve_all_parallel(&k_vec);
        let label = vec!["G", "M", "G"];
        zig_model
            .show_band(&path, &label, nk, "tests/kagome_zig/")
            .unwrap();

        let green = SurfGreen::from_Model(&super_model, 0, 1e-3, None).unwrap();
        let E_min = -2.0;
        let E_max = 4.0;
        let E_n = nk;
        let path = [[0.0], [0.5], [1.0]];
        let path = arr2(&path);
        let label = vec!["G", "M", "G"];
        green.show_surf_state("tests/kagome_zig", &path, &label, nk, E_min, E_max, E_n, 0);

        //Starting to calculate the DOS of kagome
        let nk: usize = 51;
        let kmesh = arr1(&[nk, nk]);
        let E_min = -3.0;
        let E_max = 3.0;
        let E_n = 1000;
        let (E0, dos) = model.dos(&kmesh, E_min, E_max, E_n, 1e-2).unwrap();
        //start to show DOS
        let mut fg = Figure::new();
        let x: Vec<f64> = E0.to_vec();
        let axes = fg.axes2d();
        let y: Vec<f64> = dos.to_vec();
        axes.lines(&x, &y, &[Color("black")]);
        let _show_ticks = Vec::<String>::new();
        let mut pdf_name = String::new();
        pdf_name.push_str("tests/kagome/");
        pdf_name.push_str("dos.pdf");
        fg.set_terminal("pdfcairo", &pdf_name);
        fg.show().expect("failed to draw gnuplot figure");
    }

    #[test]
    fn SSH() {
        let li: Complex<f64> = 1.0 * Complex::i();
        let t1 = 1.0 + 0.0 * li;
        let t2 = 0.5 + 0.0 * li;
        let Delta = 0.0;
        let lat = arr2(&[[1.0]]);
        let orb = arr2(&[[0.3], [0.5]]);
        let mut model = Model::<false, 1>::tb_model(lat, orb, None).unwrap();
        model.add_hop(t1, 0, 1, &array![0], None);
        model.add_hop(t2, 0, 1, &array![-1], None);
        model.add_onsite(&array![Delta, -Delta], None);

        let nk: usize = 101;
        let path = [[0.0], [0.5], [1.0]];
        let path = arr2(&path);
        let label = vec!["G", "M", "G"];
        model.show_band(&path, &label, nk, "tests/SSH/").unwrap();
        let super_model = model.cut_piece(5, 0).unwrap();

        let (band, _evec) = super_model.solve_onek(&array![0.0]);
        println!("{}", band);
    }
    #[test]
    fn BBH_model() {
        let li: Complex<f64> = 1.0 * Complex::i();
        let t1 = 0.1 + 0.0 * li;
        let t2 = 1.0 + 0.0 * li;
        let i0 = -1.0;
        let lat = arr2(&[[1.0, 0.0], [0.0, 1.0]]);
        let orb = arr2(&[[0.0, 0.0], [0.5, 0.0], [0.5, 0.5], [0.0, 0.5]]);
        // Four lattice sites, one orbital each
        let atoms = vec![
            Atom::with_orbitals(arr1(&[0.0, 0.0]), AtomType::C, [OrbitalId::new(0)]),
            Atom::with_orbitals(arr1(&[0.5, 0.0]), AtomType::C, [OrbitalId::new(1)]),
            Atom::with_orbitals(arr1(&[0.5, 0.5]), AtomType::C, [OrbitalId::new(2)]),
            Atom::with_orbitals(arr1(&[0.0, 0.5]), AtomType::C, [OrbitalId::new(3)]),
        ];
        let mut model = Model::<false, 2>::tb_model(lat, orb, Some(atoms)).unwrap();
        model.add_hop(t1, 0, 1, &array![0, 0], None);
        model.add_hop(t1, 1, 2, &array![0, 0], None);
        model.add_hop(t1, 2, 3, &array![0, 0], None);
        model.add_hop(i0 * t1, 3, 0, &array![0, 0], None);
        model.add_hop(t2, 0, 1, &array![-1, 0], None);
        model.add_hop(i0 * t2, 0, 3, &array![0, -1], None);
        model.add_hop(t2, 2, 3, &array![1, 0], None);
        model.add_hop(t2, 2, 1, &array![0, 1], None);
        let nk: usize = 101;
        let path = [[0.0, 0.0], [0.5, 0.0], [0.5, 0.5], [0.0, 0.0]];
        let path = arr2(&path);
        let label = vec!["G", "X", "M", "G"];
        model.show_band(&path, &label, nk, "tests/BBH/").unwrap();
        model.output_hr("tests/BBH/", "wannier90").unwrap();

        //算一下wilson loop
        let n = 51;
        let dir_1 = arr1(&[1.0, 0.0]);
        let dir_2 = arr1(&[0.0, 1.0]);
        let occ = vec![0, 1];
        let wcc = model.wannier_centre(&occ, &array![0.0, 0.0], &dir_1, &dir_2, n, n);
        let nocc = occ.len();
        let mut fg = Figure::new();
        let x: Vec<f64> = Array1::<f64>::linspace(0.0, 1.0, n).to_vec();
        let axes = fg.axes2d();
        for j in -1..2 {
            for i in 0..nocc {
                let a = wcc.row(i).to_owned() + (j as f64) * 2.0 * PI;
                let y: Vec<f64> = a.to_vec();
                axes.points(&x, &y, &[Color("black"), gnuplot::PointSymbol('O')]);
            }
        }
        let axes = axes.set_x_range(Fix(0.0), Fix(1.0));
        let axes = axes.set_y_range(Fix(0.0), Fix(2.0 * PI));
        let show_ticks = vec![
            Major(0.0, Fix("0")),
            Major(0.5, Fix("π")),
            Major(1.0, Fix("")),
        ];
        axes.set_x_ticks_custom(show_ticks.into_iter(), &[], &[]);
        let show_ticks = vec![
            Major(0.0, Fix("0")),
            Major(PI, Fix("π")),
            Major(2.0 * PI, Fix("")),
        ];
        axes.set_y_ticks_custom(show_ticks.into_iter(), &[], &[]);
        let mut pdf_name = String::new();
        pdf_name.push_str("tests/BBH/wcc.pdf");
        fg.set_terminal("pdfcairo", &pdf_name);
        fg.show().expect("failed to draw gnuplot figure");
        //算一下边界态
        let green = SurfGreen::from_Model(&model, 0, 1e-3, None).unwrap();
        let E_min = -2.0;
        let E_max = 2.0;
        let E_n = nk;
        let path = [[0.0], [0.5], [1.0]];
        let path = arr2(&path);
        let label = vec!["G", "X", "G"];
        green.show_surf_state("tests/BBH", &path, &label, nk, E_min, E_max, E_n, 0);

        //算一下corner state
        let num = 10;
        let model_1 = model.cut_piece(num, 0).unwrap();
        let new_model = model_1.cut_piece(2 * num, 1).unwrap();
        let _s = 0;
        let start = Instant::now();
        let (band, evec) = new_model.solve_onek(&arr1(&[0.0, 0.0]));
        println!(
            "band shape is {:?}, evec shape is {:?}",
            band.shape(),
            evec.shape()
        );
        let end = Instant::now(); // 结束计时
        let duration = end.duration_since(start); // 计算执行时间
        println!("solve_band_all took {} seconds", duration.as_secs_f64()); // 输出执行时间
        let _nresults = band.len();
        let show_evec = evec.to_owned().map(|x| x.norm_sqr());
        let _norb = new_model.norb();
        let size = show_evec;
        let show_str = new_model.atom_position().dot(&model.lat);
        create_dir_all("tests/BBH/corner").expect("can't creat the file");
        write_txt_1(band, "tests/BBH/corner/band.txt").expect("write_txt failed");
        write_txt(size, "tests/BBH/corner/evec.txt").expect("write_txt failed");
        write_txt(show_str, "tests/BBH/corner/structure.txt").expect("write_txt failed");
    }

    #[test]
    fn graphene_magnetic_field() {
        use crate::{MagneticField, Model};
        use ndarray::{Axis, arr1, arr2};
        use num_complex::Complex;
        // 如果你在其他地方定义了画图函数,请确保 use 进来,例如:
        // use crate::draw_heatmap;

        // 1. 设置模型基本参数
        let t = Complex::new(-1.0, 0.0);
        let delta = 0.0;

        // 石墨烯晶格:a1 = (1, 0), a2 = (1/2, √3/2)
        let lat = arr2(&[[1.0, 0.0], [0.5, 3.0_f64.sqrt() / 2.0]]);
        // 轨道的相对分数坐标 (Fractional Coordinates)
        let orb = arr2(&[[1.0 / 3.0, 1.0 / 3.0], [2.0 / 3.0, 2.0 / 3.0]]);

        let mut model = Model::<false, 2>::tb_model(lat, orb, None).unwrap();
        model.set_onsite(&arr1(&[-delta, delta]), None);

        // 添加最近邻跃迁
        let r0: ndarray::Array2<isize> = arr2(&[[0, 0], [-1, 0], [0, -1]]);
        for r in r0.axis_iter(Axis(0)) {
            model.add_hop(t, 0, 1, &r.to_owned(), None);
        }

        // 2. 施加磁场
        // 重要:二维中,面外磁场垂直于 xy 平面,对应的索引必须为 z 轴 (即 mag_dir = 2)
        // 扩胞 [3, 3] 表示 a1 和 a2 两个方向各扩胞 3 倍
        // 磁通 8 表示整体超胞含有 8 个磁通量子 (每个原胞 8/9 个磁通)
        let magnetic_model = model.add_magnetic_field(2, [9, 9], 40).unwrap();

        // 3. 高对称路径 (注意:六角晶格的 K 点在分数倒格矢坐标下是 1/3, 1/3)
        let path = arr2(&[[0.0, 0.0], [0.5, 0.0], [1.0 / 3.0, 1.0 / 3.0], [0.0, 0.0]]);
        let label = vec!["Γ", "M", "K", "Γ"];
        let nk = 1001;

        // 4. 绘制折叠态下的超胞能带 (Hofstadter 蝴蝶状能带切片)
        magnetic_model
            .show_band(&path, &label, nk, "tests/graphene_magnetic")
            .unwrap();

        // 5. 展开能带 (Unfold) 回到原胞 Brillouin 区
        let u_matrix = arr2(&[[9.0, 0.0], [0.0, 9.0]]);

        // 生成对应的谱函数 (Spectral Weight)
        let a_spectral = magnetic_model
            .unfold(&u_matrix, &path, nk, -3.0, 3.0, nk, 1e-3, 1e-5)
            .unwrap();

        // 将谱函数输出为热力图
        // (假定 draw_heatmap 接收二维热力矩阵以及保存路径)
        draw_heatmap(
            &a_spectral.reversed_axes(),
            "./tests/graphene_magnetic/unfold_band.pdf",
        );
    }

    #[test]
    fn test_hofstadter_butterfly_gnuplot() {
        use gnuplot::AutoOption::Fix;
        use gnuplot::{AxesCommon, Color, Figure, PointSize, PointSymbol};

        // 1. 初始化一个标准的 2D 正方晶格
        let t = Complex::new(-1.0, 0.0);
        // 正方晶格基矢 a1=(1,0), a2=(0,1)
        let lat = arr2(&[[1.0, 0.0], [0.0, 1.0]]);
        // 单轨道坐标位于原点
        let orb = arr2(&[[0.0, 0.0]]);

        let mut model = Model::<false, 2>::tb_model(lat, orb, None).unwrap();
        model.set_onsite(&arr1(&[0.0]), None);

        // 加上最近邻跃迁 (上下左右4个方向)
        let r0: ndarray::Array2<isize> = arr2(&[[1, 0], [-1, 0], [0, 1], [0, -1]]);
        for r in r0.axis_iter(Axis(0)) {
            model.add_hop(t, 0, 0, &r.to_owned(), None);
        }

        // 2. 准备扫描磁通并记录坐标
        let q = 81; // 分母 q 设定为 49 形成的蝴蝶分形已经足够好看

        // 我们用两个动态数组分别记录散点图的 X 轴 (磁通) 和 Y 轴 (能量)
        let mut x_data = Vec::new();
        let mut y_data = Vec::new();

        println!("开始计算 Hofstadter 蝴蝶能谱,进度: ");

        for p in 0..=q {
            // 在 y 轴方向扩胞 q 倍,即 [1, q],形成一个一维超胞
            let mag_model = model.add_magnetic_field(2, [1, q], p as isize).unwrap();
            let norb = mag_model.norb();

            // 提取 Gamma 点 k = (0,0) 的哈密顿量矩阵
            let mut h_k0 = Array2::<Complex<f64>>::zeros((norb, norb));
            for iR in 0..mag_model.hamR.nrows() {
                for i in 0..norb {
                    for j in 0..norb {
                        h_k0[[i, j]] += mag_model.ham[[iR, i, j]];
                    }
                }
            }

            // --- 求解本征值 ---
            let evals = h_k0.eigvalsh(UPLO::Upper).expect("矩阵对角化失败");

            // 收集坐标点
            let flux_ratio = (p as f64) / (q as f64);
            for &e in evals.iter() {
                x_data.push(flux_ratio);
                y_data.push(e);
            }

            if p % 10 == 0 {
                println!("已完成 {}/{}", p, q);
            }
        }

        println!(
            "计算完成,共有 {} 个能级点,正在使用 gnuplot 绘图...",
            x_data.len()
        );

        // 3. 使用 Gnuplot 直接输出图像
        // 确保目录存在
        create_dir_all("tests").expect("无法创建 tests 文件夹");

        let mut fg = Figure::new();
        let axes = fg.axes2d();

        axes.set_title("Hofstadter's Butterfly", &[]);
        axes.set_x_label("Magnetic Flux (\\Phi / \\Phi_0)", &[]);
        axes.set_y_label("Energy (E/t)", &[]);

        // 固定 x 和 y 的坐标范围
        let axes = axes.set_x_range(Fix(0.0), Fix(1.0));
        let axes = axes.set_y_range(Fix(-10.0), Fix(10.0));

        // 使用 .points 绘制散点图
        // PointSymbol('.') 表示画极小的像素点,最适合用来展示密集的分形结构
        // Color("navy") 使用深蓝色
        axes.points(
            &x_data,
            &y_data,
            &[Color("navy"), PointSymbol('.'), PointSize(0.6)],
        );

        // 将图像渲染为 PDF
        fg.set_terminal("pdfcairo", "tests/hofstadter_butterfly.pdf");
        fg.show().expect("Gnuplot 画图失败");

        println!("完美!图像已保存至 tests/hofstadter_butterfly.pdf");
    }

    #[test]
    fn fermi_surface_graphene() {
        let li: Complex<f64> = 1.0 * Complex::i();
        let t1 = 1.0 + 0.0 * li;
        let lat = arr2(&[[3.0_f64.sqrt(), -1.0], [3.0_f64.sqrt(), 1.0]]);
        let orb = arr2(&[[0.0, 0.0], [1.0 / 3.0, 1.0 / 3.0]]);
        let mut model = Model::<false, 2>::tb_model(lat, orb, None).unwrap();
        model.add_hop(t1, 0, 1, &array![0, 0], None);
        model.add_hop(t1, 0, 1, &array![-1, 0], None);
        model.add_hop(t1, 0, 1, &array![0, -1], None);

        // E_F = 0.1 slightly above Dirac point → small Fermi pockets around K, K'
        let k_mesh = arr1(&[100, 100]);
        model
            .show_fermi_surface(&k_mesh, 0.1, "tests/graphene")
            .expect("Fermi surface plot failed");
        println!("Graphene Fermi surface saved to tests/graphene/fermi_surface.pdf");
    }

    #[test]
    fn fermi_surface_kagome() {
        let li: Complex<f64> = 1.0 * Complex::i();
        let t1 = 1.0 + 0.0 * li;
        let lat = arr2(&[[3.0_f64.sqrt(), -1.0], [3.0_f64.sqrt(), 1.0]]);
        let orb = arr2(&[[0.0, 0.0], [1.0 / 3.0, 0.0], [0.0, 1.0 / 3.0]]);
        let mut model = Model::<false, 2>::tb_model(lat, orb, None).unwrap();
        model.add_hop(t1, 0, 1, &array![0, 0], None);
        model.add_hop(t1, 2, 0, &array![0, 0], None);
        model.add_hop(t1, 1, 2, &array![0, 0], None);
        model.add_hop(t1, 0, 2, &array![0, -1], None);
        model.add_hop(t1, 0, 1, &array![-1, 0], None);
        model.add_hop(t1, 2, 1, &array![-1, 1], None);

        // E_F = -1.0 (flat band) — should show a Fermi surface contour
        let k_mesh = arr1(&[80, 80]);
        model
            .show_fermi_surface(&k_mesh, -1.0, "tests/kagome")
            .expect("Fermi surface plot failed");
        println!("Kagome Fermi surface saved to tests/kagome/fermi_surface.pdf");
    }

    fn build_h_wave_am_model() -> Model<false, 3> {
        let lat = array![[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];
        let orb = array![[0.0, 0.0, 0.0], [0.5, 0.5, 0.5]];
        let mut model = Model::<false, 3>::tb_model(lat, orb, None).unwrap();
        let t = 1.0;
        let j = 1.0;
        model.add_hop(t, 0, 1, &array![0, 0, 0], None);
        model.add_hop(t, 0, 1, &array![-1, 0, 0], None);
        model.add_hop(t, 0, 1, &array![0, -1, 0], None);
        model.add_hop(t, 0, 1, &array![-1, -1, 0], None);
        model.add_hop(t, 0, 1, &array![0, 0, -1], None);
        model.add_hop(t, 0, 1, &array![-1, 0, -1], None);
        model.add_hop(t, 0, 1, &array![0, -1, -1], None);
        model.add_hop(t, 0, 1, &array![-1, -1, -1], None);

        let t0 = Complex::new(0.0, 0.5);
        model.add_hop(t0, 0, 0, &array![2, 1, 1], None);
        model.add_hop(-t0, 0, 0, &array![2, 1, -1], None);
        model.add_hop(-t0, 0, 0, &array![2, -1, 1], None);
        model.add_hop(t0, 0, 0, &array![2, -1, -1], None);
        model.add_hop(-t0, 0, 0, &array![1, 2, 1], None);
        model.add_hop(t0, 0, 0, &array![1, 2, -1], None);
        model.add_hop(t0, 0, 0, &array![-1, 2, 1], None);
        model.add_hop(-t0, 0, 0, &array![-1, 2, -1], None);

        let t0 = -t0;
        model.add_hop(t0, 1, 1, &array![2, 1, 1], None);
        model.add_hop(-t0, 1, 1, &array![2, 1, -1], None);
        model.add_hop(-t0, 1, 1, &array![2, -1, 1], None);
        model.add_hop(t0, 1, 1, &array![2, -1, -1], None);
        model.add_hop(-t0, 1, 1, &array![1, 2, 1], None);
        model.add_hop(t0, 1, 1, &array![1, 2, -1], None);
        model.add_hop(t0, 1, 1, &array![-1, 2, 1], None);
        model.add_hop(-t0, 1, 1, &array![-1, 2, -1], None);

        model.add_onsite(&array![j, -j], None);
        model
    }

    fn max_abs_1d(x: &Array1<f64>) -> f64 {
        x.iter().fold(0.0f64, |a, &v| a.max(v.abs()))
    }

    fn max_abs_diff_1d(a: &Array1<f64>, b: &Array1<f64>) -> f64 {
        a.iter()
            .zip(b.iter())
            .fold(0.0f64, |acc, (&x, &y)| acc.max((x - y).abs()))
    }

    #[test]
    fn nlh_current_first_api_matches_kernel_definitions() {
        let model = build_h_wave_am_model();
        let current = array![1.0, 0.0, 0.0];
        let field_1 = array![0.0, 1.0, 0.0];
        let field_2 = array![0.0, 0.0, 1.0];
        let k_mesh = array![12, 12, 12];
        let chemical_potentials = Array1::linspace(-1.0, 1.0, 21);
        let mut params = Parameters::rank3(
            [12, 12, 12],
            fixed_direction(&current),
            fixed_direction(&field_1),
            fixed_direction(&field_2),
            chemical_potentials.clone(),
        );
        params.T = array![100.0];
        let public = model
            .intrinsic_nonlinear_hall(&params)
            .unwrap()
            .conductivity;

        let k_points = gen_kmesh(&k_mesh).unwrap();
        let (kernel, energies, _) =
            model.berry_connection_dipole(&k_points, &field_1, &field_2, &current, None);
        let occupation = Occupation::FermiDirac {
            temperature_kelvin: 100.0,
        };
        let expected = chemical_potentials.mapv(|mu| {
            kernel
                .iter()
                .zip(energies.iter())
                .map(|(&value, &energy)| value * occupation.minus_derivative(energy, mu).unwrap())
                .sum::<f64>()
                / k_points.nrows() as f64
                / model.lat.det().unwrap()
        });
        assert!(max_abs_diff_1d(&public, &expected) < 1e-12);
        assert!(max_abs_1d(&public) > 1e-8, "test signal is too small");
    }
    // ── Tetra smoke tests ─────────────────────────────────────────────────

    fn build_haldane_2d(t2_imag: f64) -> Model<false, 2> {
        let li = Complex::new(0.0, 1.0);
        let t = Complex::new(-1.0, 0.0);
        let t2 = Complex::new(t2_imag, 0.0); // real → multiplied by i at call site
        let delta = 0.7;
        let lat = arr2(&[[1.0, 0.0], [0.5, 3.0_f64.sqrt() / 2.0]]);
        let orb = arr2(&[[1.0 / 3.0, 1.0 / 3.0], [2.0 / 3.0, 2.0 / 3.0]]);
        let mut m = Model::<false, 2>::tb_model(lat, orb, None).unwrap();
        m.set_onsite(&arr1(&[-delta, delta]), None);
        for &(i, j) in &[(0, 0), (-1, 0), (0, -1)] {
            m.add_hop(t, 0, 1, &arr1(&[i, j]), None);
        }
        for &(i, j) in &[(1, 0), (-1, 1), (0, -1)] {
            m.add_hop(t2 * li, 0, 0, &arr1(&[i, j]), None);
        }
        for &(i, j) in &[(-1, 0), (1, -1), (0, 1)] {
            m.add_hop(t2 * li, 1, 1, &arr1(&[i, j]), None);
        }
        m
    }

    /// 1. API conventions: T>0 guard on single-mu
    #[test]
    fn nlh_api_conventions_and_guards() {
        let model = build_h_wave_am_model();
        let dx = array![1.0, 0.0, 0.0];
        let dy = array![0.0, 1.0, 0.0];
        let dz = array![0.0, 0.0, 1.0];
        let kmesh = array![4, 4, 4];
        let mu1 = arr1(&[0.0]);
        // Reference must accept T>0
        assert!(
            intrinsic_nonlinear_values(
                &model,
                &kmesh,
                &dx,
                &dy,
                &dz,
                &mu1,
                300.0,
                Integration::Direct,
            )
            .is_ok()
        );

        let zero_temperature = Parameters::rank3(
            [4, 4, 4],
            fixed_direction(&dx),
            fixed_direction(&dy),
            fixed_direction(&dz),
            mu1,
        );
        assert!(model.intrinsic_nonlinear_hall(&zero_temperature).is_err());
    }

    /// 2. Intrinsic NLH: H-wave up/dn T‑odd via direct sum.
    ///
    /// σ(up) = −σ(dn) must hold for the reference path.
    #[test]
    fn nlh_intrinsic_hwave_up_dn_odd() {
        let _li = Complex::new(0.0, 1.0);
        let lat = array![[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];
        let orb = array![[0.0, 0.0, 0.0], [0.5, 0.5, 0.5]];
        let t = 1.0;
        let j0 = 1.0;
        let build = |j_sign: f64| {
            let mut m = Model::<false, 3>::tb_model(lat.clone(), orb.clone(), None).unwrap();
            for &(i, j, k) in &[
                (0, 0, 0),
                (-1, 0, 0),
                (0, -1, 0),
                (-1, -1, 0),
                (0, 0, -1),
                (-1, 0, -1),
                (0, -1, -1),
                (-1, -1, -1),
            ] {
                m.add_hop(t, 0, 1, &array![i, j, k], None);
            }
            let t0 = Complex::new(0.0, 0.2);
            let nnn: [(f64, (isize, isize, isize)); 8] = [
                (1.0, (2, 1, 1)),
                (-1.0, (2, 1, -1)),
                (-1.0, (2, -1, 1)),
                (1.0, (2, -1, -1)),
                (-1.0, (1, 2, 1)),
                (1.0, (1, 2, -1)),
                (1.0, (-1, 2, 1)),
                (-1.0, (-1, 2, -1)),
            ];
            for &(s, (i, j, k)) in &nnn {
                m.add_hop(t0.scale(s), 0, 0, &array![i, j, k], None);
                m.add_hop(t0.scale(-s), 1, 1, &array![i, j, k], None);
            }
            m.add_onsite(&array![j0 * j_sign, -j0 * j_sign], None);
            m
        };
        let model_up = build(1.0);
        let model_dn = build(-1.0);
        let dx = array![1.0, 0.0, 0.0];
        let dy = array![0.0, 1.0, 0.0];
        let dz = array![0.0, 0.0, 1.0];
        let T: f64 = 100.0;
        let mu = Array1::linspace(-1.0, 1.0, 21);
        let km = array![12, 12, 12];
        let ref_up =
            intrinsic_nonlinear_values(&model_up, &km, &dx, &dy, &dz, &mu, T, Integration::Direct)
                .unwrap();
        let ref_dn =
            intrinsic_nonlinear_values(&model_dn, &km, &dx, &dy, &dz, &mu, T, Integration::Direct)
                .unwrap();
        let sum = max_abs_1d(&(&ref_up + &ref_dn));
        assert!(
            sum < 1e-10,
            "ref up+dn must vanish (T‑odd), got {:.2e}",
            sum
        );
        assert!(max_abs_1d(&ref_up) > 1e-6, "signal too small");
    }

    /// 3. Intrinsic NLH 2D convergence via direct sum.
    #[test]
    fn intrinsic_haldane_2d_convergence() {
        let model = build_haldane_2d(-0.3);
        let dx = arr1(&[1.0, 0.0]);
        let dy = arr1(&[0.0, 1.0]);
        let mu = Array1::linspace(-4.0, 4.0, 21);
        let mut prev_pk = 0.0;
        for &nk in &[21usize, 31, 41, 51] {
            let km = arr1(&[nk, nk]);
            let ref_val = intrinsic_nonlinear_values(
                &model,
                &km,
                &dx,
                &dy,
                &dy,
                &mu,
                0.0,
                Integration::Direct,
            )
            .unwrap();
            let pk = max_abs_1d(&ref_val);
            assert!(pk > 1e-6, "signal too small at nk={nk}");
            // Result should stabilise with mesh size
            if prev_pk > 0.0 {
                let rel = (pk - prev_pk).abs() / prev_pk;
                assert!(
                    rel < 0.5,
                    "large drift at nk={nk}: pk={pk:.3e} prev={prev_pk:.3e}"
                );
            }
            prev_pk = pk;
        }
    }
    // ── Simplex vs direct-sum comparison tests ──────────────────────────

    /// 8. Berry-curvature trait vs gauge-invariant velocity kernel.
    ///
    /// Compares the **per‑k‑point integrand** Ω^{xy}_n(k) produced by the
    /// public band-resolved result against the gauge‑invariant kernel
    /// `K^{xy}_nm = v^x_nm v^y_mn` evaluated at each k‑point.  The relation
    /// `Ω_n = −2 Im Σ_m K_nm / (d²+η²)` must hold identically.
    #[test]
    fn berry_curvature_kernel_consistency() {
        let model = build_haldane_2d(-0.3);
        let dx = arr1(&[1.0, 0.0]);
        let dy = arr1(&[0.0, 1.0]);
        let eta = 0.05;
        let nk = 21;
        let kmesh = arr1(&[nk, nk]);
        let kvec = crate::kpoints::gen_kmesh(&kmesh).unwrap();
        let nkt = kvec.nrows();

        let mut max_err = 0.0f64;
        for ik in 0..nkt {
            let kv = kvec.row(ik).to_owned();
            // Band-resolved Berry curvature from the public trait.
            let omega_n_old =
                band_berry_curvature(&model, &kv, &dx, &dy, None, eta).berry_curvature;
            // Gauge-invariant K_nm must produce the same Ω_n.
            let tk = model.compute_velocity_kernel(&kv, &dx, &dy, None, Gauge::Atom, None);
            let nsta = model.nsta();
            let mut omega_n_new = Array1::<f64>::zeros(nsta);
            let eta2 = eta * eta;
            for n in 0..nsta {
                let mut g_sum = Complex::new(0.0, 0.0);
                for m in 0..nsta {
                    if m == n {
                        continue;
                    }
                    let de = tk.band[[n]] - tk.band[[m]];
                    let denom = de * de + eta2;
                    g_sum += tk.k_ab[[n, m]] / denom;
                }
                omega_n_new[[n]] = -2.0 * g_sum.im;
            }
            let err = max_abs_diff_1d(&omega_n_old, &omega_n_new);
            max_err = max_err.max(err);
        }
        println!("max per‑k‑point Ω_n discrepancy: {:.3e} (nk={nk})", max_err);
        assert!(max_err < 1e-12, "old vs new Ω_n mismatch: {:.2e}", max_err);
    }

    /// 8b. Berry curvature integral: simplex vs direct sum.
    ///
    /// The Haldane model has total Ω = 0 (Chern numbers sum to zero).
    /// This tests whether the simplex quadrature introduces spurious
    /// non‑zero total Berry curvature.
    #[test]
    fn berry_total_simplex_vs_direct() {
        let model = build_haldane_2d(-0.3);
        let dx = arr1(&[1.0, 0.0]);
        let dy = arr1(&[0.0, 1.0]);
        let eta = 0.05;

        println!("\n--- Berry total Ω^{{xy}} simplex vs direct ---");
        println!(
            "{:>4}  {:>14}  {:>14}  {:>12}",
            "nk", "direct_sum", "simplex", "diff"
        );
        for &nk in &[21usize, 31, 51, 101] {
            let kmesh = arr1(&[nk, nk]);
            let kvec = crate::kpoints::gen_kmesh(&kmesh).unwrap();
            let nkt = kvec.nrows();

            // old: direct sum
            let mut direct = 0.0;
            for ik in 0..nkt {
                let kv = kvec.row(ik).to_owned();
                let omega_n =
                    band_berry_curvature(&model, &kv, &dx, &dy, None, eta).berry_curvature;
                direct += omega_n.iter().sum::<f64>();
            }
            direct /= nkt as f64;

            // new: simplex integral
            let all_pts: Vec<crate::response::VertexKernel> = (0..nkt)
                .map(|ik| {
                    let kv = kvec.row(ik).to_owned();
                    let tk = model.compute_velocity_kernel(&kv, &dx, &dy, None, Gauge::Atom, None);
                    tk
                })
                .collect();
            let (_g, simplex, _unsafe) = crate::response::linear::integrate_occupied_geometry(
                &all_pts,
                &kmesh,
                eta,
                &array![1e100],
                Occupation::ZeroTemperature,
            );
            let simplex = simplex[0];

            let diff = (direct - simplex).abs();
            println!("{nk:>4}  {direct:>14.6e}  {simplex:>14.6e}  {diff:>10.3e}");
            assert!(diff < 5e-4, "total Ω mismatch {diff:.2e} at nk={nk}");
        }
    }

    /// 9. Berry curvature dipole: old direct Fermi‑derivative sum vs new
    /// 8b. AHC energy-cut: Chern quantization in gap.
    #[test]
    fn hall_conductivity_ec_vs_reference() {
        let model = build_haldane_2d(-0.3);
        let dx = arr1(&[1.0, 0.0]);
        let dy = arr1(&[0.0, 1.0]);
        let eta = 0.05;
        let mu = Array1::linspace(-3.0, 3.0, 201);
        let i_mid = mu.len() / 2;

        for &nk in &[31, 51, 71, 101] {
            let kmesh = arr1(&[nk, nk]);
            let direct = hall_values(
                &model,
                &kmesh,
                &dx,
                &dy,
                &mu,
                0.0,
                None,
                eta,
                Integration::Direct,
            )
            .unwrap();
            let ec = hall_values(
                &model,
                &kmesh,
                &dx,
                &dy,
                &mu,
                0.0,
                None,
                eta,
                Integration::EnergyCut,
            )
            .unwrap();
            let max_abs = max_abs_diff_1d(&direct, &ec);
            let c_dir = direct[[i_mid]];
            let c_ec = ec[[i_mid]];
            println!("nk={nk}  max_abs={max_abs:.3e}  C_dir={c_dir:.6}  C_ec={c_ec:.6}");
            assert!(max_abs < 3e-2);
        }
    }

    /// 8c. Time-reversal check: σ(TR Haldane) = −σ(Haldane) for both direct and EC.
    #[test]
    fn hall_conductivity_ec_tr() {
        let model = build_haldane_2d(-0.3);
        let model_tr = build_haldane_2d(0.3); // t2 → −t2
        let dx = arr1(&[1.0, 0.0]);
        let dy = arr1(&[0.0, 1.0]);
        let eta = 0.05;
        let mu = Array1::linspace(-3.0, 3.0, 101);
        let kmesh = arr1(&[51, 51]);

        let d_dir = hall_values(
            &model,
            &kmesh,
            &dx,
            &dy,
            &mu,
            0.0,
            None,
            eta,
            Integration::Direct,
        )
        .unwrap();
        let d_ec = hall_values(
            &model,
            &kmesh,
            &dx,
            &dy,
            &mu,
            0.0,
            None,
            eta,
            Integration::EnergyCut,
        )
        .unwrap();
        let tr_dir = hall_values(
            &model_tr,
            &kmesh,
            &dx,
            &dy,
            &mu,
            0.0,
            None,
            eta,
            Integration::Direct,
        )
        .unwrap();
        let tr_ec = hall_values(
            &model_tr,
            &kmesh,
            &dx,
            &dy,
            &mu,
            0.0,
            None,
            eta,
            Integration::EnergyCut,
        )
        .unwrap();

        // Check σ(Haldane) + σ(TR) ≈ 0 element-wise
        let diff_dir: Vec<f64> = d_dir
            .iter()
            .zip(tr_dir.iter())
            .map(|(&a, &b)| (a + b).abs())
            .collect();
        let diff_ec: Vec<f64> = d_ec
            .iter()
            .zip(tr_ec.iter())
            .map(|(&a, &b)| (a + b).abs())
            .collect();
        let max_dir = diff_dir.iter().fold(0.0f64, |a: f64, &b| a.max(b));
        let max_ec = diff_ec.iter().fold(0.0f64, |a: f64, &b| a.max(b));
        println!("TR check: max|σ+σ_TR|  direct={max_dir:.3e}  EC={max_ec:.3e}");
        assert!(max_dir < 1e-10, "direct sum fails TR: {max_dir:.3e}");
        assert!(max_ec < 1e-10, "EC fails TR: {max_ec:.3e}");
    }

    /// 8c2. Dipole energy-cut: TR check for Haldane (±t2).
    #[test]
    fn dipole_energy_cut_tr() {
        let model = build_haldane_2d(-0.3);
        let model_tr = build_haldane_2d(0.3);
        let dx = arr1(&[1.0, 0.0]);
        let dy = arr1(&[0.0, 1.0]);
        let dc = arr1(&[1.0, 0.0]); // dipole direction = x
        let eta = 0.05;
        let mu = Array1::linspace(-2.0, 2.0, 51);
        let kmesh = arr1(&[31, 31]);

        let d_dir = extrinsic_nonlinear_values(
            &model,
            &kmesh,
            &dx,
            &dy,
            &dc,
            &mu,
            0.0,
            0.0,
            None,
            eta,
            Integration::EnergyCut,
            FieldSymmetry::Ordered,
        )
        .unwrap();
        let d_tr = extrinsic_nonlinear_values(
            &model_tr,
            &kmesh,
            &dx,
            &dy,
            &dc,
            &mu,
            0.0,
            0.0,
            None,
            eta,
            Integration::EnergyCut,
            FieldSymmetry::Ordered,
        )
        .unwrap();
        // BCD is TR‑even: v_TR^c Ω_TR^{ab} = (−v^c)(−Ω^{ab}) = v^c Ω^{ab}  →  D_TR = D
        let max_diff = d_dir
            .iter()
            .zip(d_tr.iter())
            .fold(0.0f64, |a: f64, (&x, &y)| a.max((x - y).abs()));
        let max_d = d_dir.iter().fold(0.0f64, |a: f64, &x| a.max(x.abs()));
        println!("Dipole TR: max|D|={max_d:.3e}  max|D−D_TR|={max_diff:.3e}");
        assert!(max_diff < 1e-10, "Dipole TR-even broken: {max_diff:.3e}");
    }

    /// 8c3. Intrinsic NLH EC vs direct sum (NLH model, σ^{yy;x}).
    /// Direct: current=x, field1=y, field2=y. EC: dir_a=y, dir_b=y, dir_c=x.
    #[test]
    fn intrinsic_ec_vs_direct() {
        let model = build_nlh_2d(2.6, 1.0);
        let dx = arr1(&[1.0, 0.0]);
        let dy = arr1(&[0.0, 1.0]);
        let _eta = 0.03;
        let mu = Array1::linspace(-4.0, 4.0, 41);
        let kmesh = arr1(&[21, 21]);

        let dir = intrinsic_nonlinear_values(
            &model,
            &kmesh,
            &dx,
            &dy,
            &dy,
            &mu,
            0.0,
            Integration::Direct,
        )
        .unwrap();
        let ec = intrinsic_nonlinear_values(
            &model,
            &kmesh,
            &dx,
            &dy,
            &dy,
            &mu,
            0.0,
            Integration::EnergyCut,
        )
        .unwrap();
        let max_abs = max_abs_diff_1d(&dir, &ec);
        let max_dir = dir.iter().fold(0.0f64, |a: f64, &x| a.max(x.abs()));
        println!("Intrinsic EC vs direct: max_abs={max_abs:.3e}  max|σ|={max_dir:.3e}");
        // The direct reference uses an explicit mesh-scale Fermi smearing,
        // while EC uses exact δ‑function line‑cut.  Differences O(1e-3)
        // for this model are expected.
        assert!(
            max_abs < max_dir * 1.5,
            "Intrinsic EC mismatch: {max_abs:.3e}"
        );
    }

    /// Build 3D low-symmetry two-band model for intrinsic NLH testing.
    ///
    /// H(k) = d₀·σ₀ + d_x·σ_x + d_y·σ_y + d_z·σ_z with λ controlling
    /// inversion breaking.  λ=0 restores P-symmetry → intrinsic NLH = 0.
    /// Both orbitals at (0,0,0).  Recommended: m=2.6, λ=1.
    fn build_nlh_3d(m: f64, lambda: f64) -> Model<false, 3> {
        let lat = array![[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];
        let orb = array![[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]];
        let mut model = Model::<false, 3>::tb_model(lat, orb, None).unwrap();
        model.add_onsite(&array![m, -m], None);

        // R = (1,0,0): 0.1 σ₀ − i/2 σ_x + 0.5 σ_z
        let i2 = Complex::new(0.0, -0.5);
        model.add_hop(0.1, 0, 0, &array![1, 0, 0], None);
        model.add_hop(0.1, 1, 1, &array![1, 0, 0], None);
        model.add_hop(i2, 0, 1, &array![1, 0, 0], None);
        model.add_hop(i2, 1, 0, &array![1, 0, 0], None);
        model.add_hop(0.5, 0, 0, &array![1, 0, 0], None);
        model.add_hop(-0.5, 1, 1, &array![1, 0, 0], None);

        // R = (0,1,0): 0.04 σ₀ − 0.15i σ_x − 0.5i σ_y + 0.325 σ_z
        model.add_hop(0.04, 0, 0, &array![0, 1, 0], None);
        model.add_hop(0.04, 1, 1, &array![0, 1, 0], None);
        model.add_hop(Complex::new(0.0, -0.15), 0, 1, &array![0, 1, 0], None);
        model.add_hop(Complex::new(0.0, -0.15), 1, 0, &array![0, 1, 0], None);
        // −0.5i σ_y → H[0,1]=−0.5, H[1,0]=0.5
        model.add_hop(-0.5, 0, 1, &array![0, 1, 0], None);
        model.add_hop(0.5, 1, 0, &array![0, 1, 0], None);
        model.add_hop(0.325, 0, 0, &array![0, 1, 0], None);
        model.add_hop(-0.325, 1, 1, &array![0, 1, 0], None);

        // R = (0,0,1): 0.03 σ₀ + (0.05λ − 0.10i)σ_x − 0.125i σ_y + 0.225 σ_z
        model.add_hop(0.03, 0, 0, &array![0, 0, 1], None);
        model.add_hop(0.03, 1, 1, &array![0, 0, 1], None);
        model.add_hop(
            Complex::new(0.05 * lambda, -0.10),
            0,
            1,
            &array![0, 0, 1],
            None,
        );
        model.add_hop(
            Complex::new(0.05 * lambda, -0.10),
            1,
            0,
            &array![0, 0, 1],
            None,
        );
        // σ_y: −0.125i [[0,−i],[i,0]] → H[0,1]=−0.125, H[1,0]=0.125
        model.add_hop(-0.125, 0, 1, &array![0, 0, 1], None);
        model.add_hop(0.125, 1, 0, &array![0, 0, 1], None);
        model.add_hop(0.225, 0, 0, &array![0, 0, 1], None);
        model.add_hop(-0.225, 1, 1, &array![0, 0, 1], None);

        // R = (1,1,0): 0.125λ σ_x
        model.add_hop(0.125 * lambda, 0, 1, &array![1, 1, 0], None);
        model.add_hop(0.125 * lambda, 1, 0, &array![1, 1, 0], None);

        // R = (1,0,−1): (0.10λ − 0.075i) σ_y
        let _hop10m1 = Complex::new(0.10 * lambda, -0.075);
        // σ_y gives: H[0,1] = −i·hop, H[1,0] = i·hop
        // −i·hop = −i(0.10λ−0.075i) = −0.10iλ + 0.075i² = −0.075−0.10iλ
        // i·hop = i(0.10λ−0.075i) = 0.10iλ − 0.075i² = 0.075+0.10iλ
        model.add_hop(
            Complex::new(-0.075, -0.10 * lambda),
            0,
            1,
            &array![1, 0, -1],
            None,
        );
        model.add_hop(
            Complex::new(0.075, 0.10 * lambda),
            1,
            0,
            &array![1, 0, -1],
            None,
        );

        // R = (1,1,1): −0.125iλ σ_z
        let hz = Complex::new(0.0, -0.125 * lambda);
        model.add_hop(hz, 0, 0, &array![1, 1, 1], None);
        model.add_hop(-hz, 1, 1, &array![1, 1, 1], None);

        model
    }

    /// 2D slice (kz=0) of the NLH test model for intrinsic EC (2D only).
    fn build_nlh_2d(m: f64, lambda: f64) -> Model<false, 2> {
        let lat = array![[1.0, 0.0], [0.0, 1.0]];
        let orb = array![[0.0, 0.0], [0.0, 0.0]];
        let mut model = Model::<false, 2>::tb_model(lat, orb, None).unwrap();
        model.add_onsite(&array![m, -m], None);

        // R = (1,0): 0.1 σ₀ − i/2 σ_x + 0.5 σ_z
        let i2 = Complex::new(0.0, -0.5);
        model.add_hop(0.1, 0, 0, &array![1, 0], None);
        model.add_hop(0.1, 1, 1, &array![1, 0], None);
        model.add_hop(i2, 0, 1, &array![1, 0], None);
        model.add_hop(i2, 1, 0, &array![1, 0], None);
        model.add_hop(0.5, 0, 0, &array![1, 0], None);
        model.add_hop(-0.5, 1, 1, &array![1, 0], None);

        // R = (0,1): 0.04 σ₀ − 0.15i σ_x − 0.5i σ_y + 0.325 σ_z
        model.add_hop(0.04, 0, 0, &array![0, 1], None);
        model.add_hop(0.04, 1, 1, &array![0, 1], None);
        model.add_hop(Complex::new(0.0, -0.15), 0, 1, &array![0, 1], None);
        model.add_hop(Complex::new(0.0, -0.15), 1, 0, &array![0, 1], None);
        model.add_hop(-0.5, 0, 1, &array![0, 1], None);
        model.add_hop(0.5, 1, 0, &array![0, 1], None);
        model.add_hop(0.325, 0, 0, &array![0, 1], None);
        model.add_hop(-0.325, 1, 1, &array![0, 1], None);

        // R = (1,1): 0.125λ σ_x
        model.add_hop(0.125 * lambda, 0, 1, &array![1, 1], None);
        model.add_hop(0.125 * lambda, 1, 0, &array![1, 1], None);

        model
    }

    /// 8c5. Intrinsic NLH: inversion-symmetry benchmark (λ=0 → zero).
    #[test]
    fn intrinsic_ec_inversion() {
        let model_p = build_nlh_2d(2.6, 0.0); // λ=0 restores P-symmetry
        let dx = arr1(&[1.0, 0.0]);
        let dy = arr1(&[0.0, 1.0]);
        let _eta = 0.03;
        let mu = Array1::linspace(-4.0, 4.0, 61);
        let kmesh = arr1(&[21, 21]);
        let ec = intrinsic_nonlinear_values(
            &model_p,
            &kmesh,
            &dy,
            &dx,
            &dy,
            &mu,
            0.0,
            Integration::EnergyCut,
        )
        .unwrap();
        let max_val = ec.iter().fold(0.0f64, |a, &x| a.max(x.abs()));
        println!("Inversion-symmetric (λ=0): max|σ| = {max_val:.3e}");
        assert!(max_val < 1e-10, "P-symmetry broken: {max_val:.3e}");
    }

    /// 8c6. Intrinsic NLH: λ → −λ sign flip (P-odd).
    #[test]
    fn intrinsic_ec_sign_flip() {
        let model_p = build_nlh_2d(2.6, 1.0);
        let model_m = build_nlh_2d(2.6, -1.0);
        let dx = arr1(&[1.0, 0.0]);
        let dy = arr1(&[0.0, 1.0]);
        let _eta = 0.03;
        let mu = Array1::linspace(-4.0, 4.0, 61);
        let kmesh = arr1(&[21, 21]);
        let ec_p = intrinsic_nonlinear_values(
            &model_p,
            &kmesh,
            &dy,
            &dx,
            &dy,
            &mu,
            0.0,
            Integration::EnergyCut,
        )
        .unwrap();
        let ec_m = intrinsic_nonlinear_values(
            &model_m,
            &kmesh,
            &dy,
            &dx,
            &dy,
            &mu,
            0.0,
            Integration::EnergyCut,
        )
        .unwrap();
        let max_sum = ec_p
            .iter()
            .zip(ec_m.iter())
            .fold(0.0f64, |a: f64, (&x, &y)| a.max((x + y).abs()));
        let max_p = ec_p.iter().fold(0.0f64, |a, &x| a.max(x.abs()));
        println!("Sign flip λ→−λ: max|σ|={max_p:.3e}  max|σ(λ)+σ(−λ)|={max_sum:.3e}");
        assert!(max_sum < 1e-10, "P-odd sign flip broken: {max_sum:.3e}");
    }

    /// 8c7. Intrinsic NLH EC convergence: peak |σ| vs nk, T with NLH model.
    #[test]
    fn intrinsic_ec_convergence() {
        let model = build_nlh_2d(2.6, 1.0);
        let dx = arr1(&[1.0, 0.0]);
        let dy = arr1(&[0.0, 1.0]);
        let _eta = 0.03;
        let mu = Array1::linspace(-4.0, 4.0, 81);
        let nks = [15, 21, 31, 41, 51, 61];
        let ts = [0.0, 100.0, 300.0];

        let mut peaks = vec![vec![0.0; nks.len()]; ts.len()];
        for (j, &nk) in nks.iter().enumerate() {
            let kmesh = arr1(&[nk, nk]);
            for (ti, &t) in ts.iter().enumerate() {
                let ec = intrinsic_nonlinear_values(
                    &model,
                    &kmesh,
                    &dy,
                    &dx,
                    &dy,
                    &mu,
                    t,
                    Integration::EnergyCut,
                )
                .unwrap();
                peaks[ti][j] = ec.iter().fold(0.0f64, |a, &x| a.max(x.abs()));
            }
        }

        let ref0 = peaks[0].last().unwrap();
        let ref1 = peaks[1].last().unwrap();
        let ref2 = peaks[2].last().unwrap();
        println!("nk   T=0K peak     Δ/ref     T=100K peak   Δ/ref     T=300K peak   Δ/ref");
        for j in 0..nks.len() {
            let d0 = (*ref0 - peaks[0][j]).abs() / ref0.abs();
            let d1 = (*ref1 - peaks[1][j]).abs() / ref1.abs();
            let d2 = (*ref2 - peaks[2][j]).abs() / ref2.abs();
            println!(
                "{:>3}   {:.4e}   {:.3e}   {:.4e}   {:.3e}   {:.4e}   {:.3e}",
                nks[j], peaks[0][j], d0, peaks[1][j], d1, peaks[2][j], d2,
            );
        }
        // Signal should be O(1e-3) or larger with this model
        assert!(*ref0 > 1e-6, "Intrinsic signal too small: {ref0:.3e}");
    }

    /// 8c8. 3D intrinsic NLH sign-flip (λ→−λ) with NLH model.
    #[test]
    fn intrinsic_ec_3d_sign_flip() {
        let model_p = build_nlh_3d(2.6, 1.0);
        let model_m = build_nlh_3d(2.6, -1.0);
        let dx = arr1(&[1.0, 0.0, 0.0]);
        let dy = arr1(&[0.0, 1.0, 0.0]);
        let dz = arr1(&[0.0, 0.0, 1.0]);
        let _eta = 0.03;
        let mu = Array1::linspace(-3.0, 3.0, 31);
        let kmesh = arr1(&[8, 8, 8]);

        let ec_p = intrinsic_nonlinear_values(
            &model_p,
            &kmesh,
            &dz,
            &dx,
            &dy,
            &mu,
            0.0,
            Integration::EnergyCut,
        )
        .unwrap();
        let ec_m = intrinsic_nonlinear_values(
            &model_m,
            &kmesh,
            &dz,
            &dx,
            &dy,
            &mu,
            0.0,
            Integration::EnergyCut,
        )
        .unwrap();
        let max_sum = ec_p
            .iter()
            .zip(ec_m.iter())
            .fold(0.0f64, |a: f64, (&x, &y)| a.max((x + y).abs()));
        let max_p = ec_p.iter().fold(0.0f64, |a: f64, &x| a.max(x.abs()));
        println!("3D Intrinsic sign flip: max|σ|={max_p:.3e}  max|σ(λ)+σ(−λ)|={max_sum:.3e}");
        // Diagonal-averaged tetrahedralization restores k→−k cancellation
        // to machine precision, matching 2D diagavg result.
        assert!(max_sum < 1e-10, "3D P-odd broken: {max_sum:.3e}");
    }

    /// 8c9. 3D intrinsic EC convergence: peak |σ| vs nk at T=100K.
    #[test]
    fn intrinsic_ec_3d_convergence() {
        let model = build_nlh_3d(2.6, 1.0);
        let dx = arr1(&[1.0, 0.0, 0.0]);
        let dy = arr1(&[0.0, 1.0, 0.0]);
        let dz = arr1(&[0.0, 0.0, 1.0]);
        let _eta = 0.03;
        let mu = Array1::linspace(-3.0, 3.0, 41);
        let nks = [6, 8, 10, 12, 14];

        println!("nk    peak|σ|       Δ/ref");
        let mut peaks = Vec::new();
        for &nk in &nks {
            let kmesh = arr1(&[nk, nk, nk]);
            let ec = intrinsic_nonlinear_values(
                &model,
                &kmesh,
                &dz,
                &dx,
                &dy,
                &mu,
                100.0,
                Integration::EnergyCut,
            )
            .unwrap();
            let peak = ec.iter().fold(0.0f64, |a, &x| a.max(x.abs()));
            peaks.push(peak);
        }
        let ref_val = peaks.last().unwrap();
        for (i, &nk) in nks.iter().enumerate() {
            let d = (ref_val - peaks[i]).abs() / ref_val.abs();
            println!("{:>3}    {:.4e}   {:.3e}", nk, peaks[i], d);
        }
        // 3D surface K‑quadrature converges ∝1/nk² (area), slower than 2D line ∝1/nk.
        // nk=10 is within ~16% of nk=14; nk=14→18 extrapolated ~5%.
        assert!(peaks[2] > 1e-4, "3D intrinsic signal too small");
    }

    /// 8d. AHC energy-cut 3D smoke (altermagnet, Berry ≈ 0, sanity check).
    #[test]
    fn hall_conductivity_ec_3d_smoke() {
        let model = build_h_wave_am_model();
        let dx = arr1(&[1.0, 0.0, 0.0]);
        let dy = arr1(&[0.0, 1.0, 0.0]);
        let eta = 0.1;
        let mu = Array1::linspace(-2.0, 2.0, 41);

        for &nk in &[8, 10] {
            let kmesh = arr1(&[nk, nk, nk]);
            let direct = hall_values(
                &model,
                &kmesh,
                &dx,
                &dy,
                &mu,
                0.0,
                None,
                eta,
                Integration::Direct,
            )
            .unwrap();
            let ec = hall_values(
                &model,
                &kmesh,
                &dx,
                &dy,
                &mu,
                0.0,
                None,
                eta,
                Integration::EnergyCut,
            )
            .unwrap();
            let max_abs = max_abs_diff_1d(&direct, &ec);
            println!("3D smoke nk={nk}  max_abs={max_abs:.3e}");
            assert!(max_abs < 5e-2, "3D mismatch too large: {max_abs:.3e}");
        }
    }

    /// Build stacked 2D QWZ: H = sin kx σx + sin ky σy + (m+cos kx+cos ky) σz, kz-independent.
    fn build_qwz_stacked_3d(m: f64) -> Model<false, 3> {
        let lat = array![[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];
        let orb = array![[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]];
        let mut model = Model::<false, 3>::tb_model(lat, orb, None).unwrap();
        model.add_onsite(&array![m, -m], None);
        // sin(kx) σx: H_01 = (e^{ikx} - e^{-ikx}) / (2i)
        model.add_hop(Complex::new(0.0, -0.5), 0, 1, &array![1, 0, 0], None);
        model.add_hop(Complex::new(0.0, 0.5), 0, 1, &array![-1, 0, 0], None);
        // sin(ky) σy: H_01 = -i sin(ky) = -(e^{iky} - e^{-iky})/2
        model.add_hop(-0.5, 0, 1, &array![0, 1, 0], None);
        model.add_hop(0.5, 0, 1, &array![0, -1, 0], None);
        // cos(kx) σz: (e^{ikx} + e^{-ikx})/2 σz
        model.add_hop(0.5, 0, 0, &array![1, 0, 0], None);
        model.add_hop(0.5, 0, 0, &array![-1, 0, 0], None);
        model.add_hop(-0.5, 1, 1, &array![1, 0, 0], None);
        model.add_hop(-0.5, 1, 1, &array![-1, 0, 0], None);
        // cos(ky) σz
        model.add_hop(0.5, 0, 0, &array![0, 1, 0], None);
        model.add_hop(0.5, 0, 0, &array![0, -1, 0], None);
        model.add_hop(-0.5, 1, 1, &array![0, 1, 0], None);
        model.add_hop(-0.5, 1, 1, &array![0, -1, 0], None);
        model
    }

    /// 8d. 3D stacked QWZ: Ω kz-independent, plateau = 1/(2π).
    #[test]
    fn hall_conductivity_ec_3d_qwz() {
        let model = build_qwz_stacked_3d(-1.0);
        let dx = arr1(&[1.0, 0.0, 0.0]);
        let dy = arr1(&[0.0, 1.0, 0.0]);
        let eta = 0.1;
        let mu = Array1::linspace(-3.0, 3.0, 61);
        let i_mid = mu.len() / 2;
        let c_ref = 1.0 / (2.0 * std::f64::consts::PI);

        for &nk in &[10, 14] {
            let kmesh = arr1(&[nk, nk, 4]); // fewer kz points (Ω is kz-independent)
            let direct = hall_values(
                &model,
                &kmesh,
                &dx,
                &dy,
                &mu,
                0.0,
                None,
                eta,
                Integration::Direct,
            )
            .unwrap();
            let ec = hall_values(
                &model,
                &kmesh,
                &dx,
                &dy,
                &mu,
                0.0,
                None,
                eta,
                Integration::EnergyCut,
            )
            .unwrap();
            let max_abs = max_abs_diff_1d(&direct, &ec);
            let c_dir = direct[[i_mid]];
            let c_ec = ec[[i_mid]];
            let cnt = crate::response::read_reset_fermi_cut_counts();
            println!(
                "3D QWZ nk={nk}  max_abs={max_abs:.3e}  C_dir={c_dir:.6}  C_ec={c_ec:.6}  C_ref={c_ref:.6}  empty/full/partial={}/{}/{}",
                cnt.empty, cnt.full, cnt.partial
            );
            assert!(max_abs < 5e-2, "QWZ EC vs direct mismatch: {max_abs:.3e}");
            assert!((c_ec - c_ref).abs() < 0.003, "QWZ plateau off: {c_ec:.6}");
        }
    }

    // ── G‑wave 3D model (2‑orbital, triangular bilayer) ───────────────────

    fn build_gwave_3d(j: f64) -> Model<false, 3> {
        let lat = array![
            [1.0, 0.0, 0.0],
            [-0.5, 3_f64.sqrt() / 2.0, 0.0],
            [0.0, 0.0, 1.0]
        ];
        let orb = array![[1.0 / 3.0, 2.0 / 3.0, 0.0], [2.0 / 3.0, 1.0 / 3.0, 0.5]];
        let mut model = Model::<false, 3>::tb_model(lat, orb, None).unwrap();

        let t = 1.0;
        model.add_hop(t, 0, 1, &array![0, 0, 0], None);
        model.add_hop(t, 0, 1, &array![-1, 0, 0], None);
        model.add_hop(t, 0, 1, &array![0, 1, 0], None);
        model.add_hop(t, 0, 1, &array![0, 0, -1], None);
        model.add_hop(t, 0, 1, &array![-1, 0, -1], None);
        model.add_hop(t, 0, 1, &array![0, 1, -1], None);

        let t2 = Complex::new(0.0, 0.5);
        let r_pairs: [(isize, isize, isize, Complex<f64>); 6] = [
            (1, -2, 1, -t2),
            (2, -1, 1, t2),
            (3, 1, 1, -t2),
            (3, 2, 1, t2),
            (2, 3, 1, -t2),
            (1, 3, 1, t2),
        ];
        for (a, b, c, val) in &r_pairs {
            model.set_hop(*val, 0, 0, &array![*a, *b, *c], None);
            model.set_hop(-val, 0, 0, &array![*a, *b, -1], None);
        }
        for (a, b, c, val) in &r_pairs {
            model.set_hop(-val, 1, 1, &array![*a, *b, *c], None);
            model.set_hop(*val, 1, 1, &array![*a, *b, -1], None);
        }

        model.add_onsite(&array![j, -j], None);
        model
    }

    /// 9a. G‑wave 3D: EC vs direct intrinsic at diagnostic k‑meshes.
    /// 3D surface K‑quadrature converges ∝1/nk²; nk≲16 is coarse.
    #[test]
    fn gwave_intrinsic_ec_vs_direct() {
        let model = build_gwave_3d(1.0);
        let dx = arr1(&[1.0, 0.0, 0.0]);
        let dy = arr1(&[0.0, 1.0, 0.0]);
        let dz = arr1(&[0.0, 0.0, 1.0]);
        let _eta = 1e-3;
        let mu = Array1::linspace(-4.0, 4.0, 21);
        let nks = [12, 16, 20];
        println!(
            "{:<8}{:<14}{:<14}{:<14}",
            "nk", "peak|σ_ec|", "max|Δ|", "rel_err"
        );
        for &nk in &nks {
            let kmesh = arr1(&[nk, nk, nk]);
            let ec = intrinsic_nonlinear_values(
                &model,
                &kmesh,
                &dz,
                &dx,
                &dy,
                &mu,
                0.0,
                Integration::EnergyCut,
            )
            .unwrap();
            let direct = intrinsic_nonlinear_values(
                &model,
                &kmesh,
                &dz,
                &dx,
                &dy,
                &mu,
                0.0,
                Integration::Direct,
            )
            .unwrap();
            let max_abs = max_abs_diff_1d(&ec, &direct);
            let peak_ec = ec.iter().fold(0.0f64, |a, &x| a.max(x.abs()));
            let peak_dir = direct.iter().fold(0.0f64, |a, &x| a.max(x.abs()));
            println!(
                "{:<8}{:<14.4e}{:<14.4e}{:<14.4e}",
                nk,
                peak_ec.max(peak_dir),
                max_abs,
                max_abs / peak_ec.max(peak_dir).max(1e-30)
            );
        }
        // Smoke: both methods produce O(1e-3) signal at nk=20
        let kmesh20 = arr1(&[20, 20, 20]);
        let ec20 = intrinsic_nonlinear_values(
            &model,
            &kmesh20,
            &dz,
            &dx,
            &dy,
            &mu,
            0.0,
            Integration::EnergyCut,
        )
        .unwrap();
        let peak = ec20.iter().fold(0.0f64, |a, &x| a.max(x.abs()));
        assert!(peak > 1e-6, "G‑wave signal too small: {peak:.3e}");
    }

    /// 9b. G‑wave 3D: J → −J sign check.
    /// Under orbital exchange the g‑wave intra‑orbital hoppings flip sign,
    /// so H(J) and H(−J) are related by U H U^†.  Intrinsic σ should flip sign.
    #[test]
    fn gwave_intrinsic_sign_flip() {
        let model_up = build_gwave_3d(1.0);
        let model_dn = build_gwave_3d(-1.0);
        let dx = arr1(&[1.0, 0.0, 0.0]);
        let dy = arr1(&[0.0, 1.0, 0.0]);
        let dz = arr1(&[0.0, 0.0, 1.0]);
        let _eta = 1e-3;
        let mu = Array1::linspace(-4.0, 4.0, 21);
        // Use only EC — direct sum has degeneracy‑handling differences.
        let nks = [12, 16, 20];
        println!(
            "{:<8}{:<14}{:<14}{:<14}",
            "nk", "peak|σ|", "max|sum|", "sum/peak"
        );
        for &nk in &nks {
            let kmesh = arr1(&[nk, nk, nk]);
            let up = intrinsic_nonlinear_values(
                &model_up,
                &kmesh,
                &dz,
                &dx,
                &dy,
                &mu,
                0.0,
                Integration::EnergyCut,
            )
            .unwrap();
            let dn = intrinsic_nonlinear_values(
                &model_dn,
                &kmesh,
                &dz,
                &dx,
                &dy,
                &mu,
                0.0,
                Integration::EnergyCut,
            )
            .unwrap();
            let max_sum = up
                .iter()
                .zip(dn.iter())
                .fold(0.0f64, |a, (&x, &y)| a.max((x + y).abs()));
            let peak = up.iter().fold(0.0f64, |a, &x| a.max(x.abs()));
            println!(
                "{:<8}{:<14.4e}{:<14.4e}{:<14.4e}",
                nk,
                peak,
                max_sum,
                max_sum / peak.max(1e-30)
            );
        }
        // At nk=20 signal should be nonzero and sum should converge
        let kmesh20 = arr1(&[20, 20, 20]);
        let up20 = intrinsic_nonlinear_values(
            &model_up,
            &kmesh20,
            &dz,
            &dx,
            &dy,
            &mu,
            0.0,
            Integration::EnergyCut,
        )
        .unwrap();
        let peak20 = up20.iter().fold(0.0f64, |a, &x| a.max(x.abs()));
        assert!(peak20 > 1e-6, "G‑wave signal vanished at nk=20");
    }
}