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
//! Model construction and Hamiltonian manipulation methods
use crate::Model;
use crate::atom_struct::{Atom, AtomType, OrbProj};
use crate::error::{Result, TbError};
use crate::generics::hop_use;
use crate::model_utils::find_R;
use crate::{Dimension, SpinDirection};
use ndarray::prelude::*;
use ndarray::*;
use ndarray_linalg::Norm;
use ndarray_linalg::{Determinant, Inverse};
use num_complex::Complex;
/// Macro to update Hamiltonian matrix elements with spin consideration
///
/// This macro updates the Hamiltonian, checking for spin and the indices ind_i, ind_j.
/// It takes a Hamiltonian and returns a new Hamiltonian.
macro_rules! update_hamiltonian {
// This macro updates the Hamiltonian, checking for spin and the indices ind_i, ind_j.
// It takes a Hamiltonian and returns a new Hamiltonian.
($spin:expr, $pauli:expr, $tmp:expr, $new_ham:expr, $ind_i:expr, $ind_j:expr,$norb:expr) => {{
if $spin {
match $pauli {
crate::SpinDirection::None => {
$new_ham[[$ind_i, $ind_j]] = $tmp;
$new_ham[[$ind_i + $norb, $ind_j + $norb]] = $tmp;
}
crate::SpinDirection::x => {
$new_ham[[$ind_i + $norb, $ind_j]] = $tmp;
$new_ham[[$ind_i, $ind_j + $norb]] = $tmp;
}
crate::SpinDirection::y => {
$new_ham[[$ind_i + $norb, $ind_j]] = $tmp * Complex::<f64>::i();
$new_ham[[$ind_i, $ind_j + $norb]] = -$tmp * Complex::<f64>::i();
}
crate::SpinDirection::z => {
$new_ham[[$ind_i, $ind_j]] = $tmp;
$new_ham[[$ind_i + $norb, $ind_j + $norb]] = -$tmp;
}
}
} else {
$new_ham[[$ind_i, $ind_j]] = $tmp;
}
$new_ham
}};
}
/// Macro to add to Hamiltonian matrix elements with spin consideration
///
/// This macro adds to the Hamiltonian, checking for spin and the indices ind_i, ind_j.
/// It takes a Hamiltonian and returns a new Hamiltonian.
macro_rules! add_hamiltonian {
// This macro updates the Hamiltonian, checking for spin and the indices ind_i, ind_j.
// It takes a Hamiltonian and returns a new Hamiltonian.
($spin:expr, $pauli:expr, $tmp:expr, $new_ham:expr, $ind_i:expr, $ind_j:expr,$norb:expr) => {{
if $spin {
match $pauli {
crate::SpinDirection::None => {
$new_ham[[$ind_i, $ind_j]] += $tmp;
$new_ham[[$ind_i + $norb, $ind_j + $norb]] += $tmp;
}
crate::SpinDirection::x => {
$new_ham[[$ind_i + $norb, $ind_j]] += $tmp;
$new_ham[[$ind_i, $ind_j + $norb]] += $tmp;
}
crate::SpinDirection::y => {
$new_ham[[$ind_i + $norb, $ind_j]] += $tmp * Complex::<f64>::i();
$new_ham[[$ind_i, $ind_j + $norb]] -= $tmp * Complex::<f64>::i();
}
crate::SpinDirection::z => {
$new_ham[[$ind_i, $ind_j]] += $tmp;
$new_ham[[$ind_i + $norb, $ind_j + $norb]] -= $tmp;
}
}
} else {
$new_ham[[$ind_i, $ind_j]] += $tmp;
}
$new_ham
}};
}
impl Model {
/// Create a new tight-binding model with the given crystal structure.
///
/// This constructor initializes a `Model` with the specified lattice and orbital
/// positions. The Hamiltonian and position matrices are initially empty and can
/// be populated using `set_hop`, `set_onsite`, and related methods.
///
/// # Arguments
/// * `dim_r` - Real space dimensionality (1, 2, or 3)
/// * `lat` - Lattice vectors as a $d \times d$ matrix
/// * `orb` - Orbital positions in fractional coordinates
/// * `spin` - Whether to include spin degrees of freedom
/// * `atom` - Optional list of atoms with orbital information
///
/// # Returns
/// `Result<Model>` containing the initialized tight-binding model
///
/// # Examples
/// ```
/// use ndarray::*;
/// use num_complex::Complex;
/// use Rustb::*;
///
/// // Create graphene model
/// let lat = array![[1.0, 0.0], [-0.5, 3_f64.sqrt() / 2.0]];
/// let orb = array![[1.0 / 3.0, 2.0 / 3.0], [2.0 / 3.0, 1.0 / 3.0]];
/// let spin = false;
/// let mut graphene_model = Model::tb_model(2, lat, orb, spin, None).unwrap();
/// ```
pub fn tb_model(
dim_r: usize,
lat: Array2<f64>,
orb: Array2<f64>,
spin: bool,
atom: Option<Vec<Atom>>,
) -> Result<Model> {
//! This function is used to initialize a Model. The variables that need to be input are as follows:
//!
//! - dim_r: the dimension of the model
//!
//! - lat: the lattice constant
//!
//! - orb: the orbital coordinates
//!
//! - spin: whether to consider spin
//!
//! - atom: the atomic coordinates, which can be None
//!
//! - atom_list: the number of orbitals for each atom, which can be None.
//!
//! Note that if any of the atomic variables are None, it is better to make them all None.
let norb: usize = orb.len_of(Axis(0));
let mut nsta: usize = norb;
if spin {
nsta *= 2;
}
let mut new_atom_list: Vec<usize> = vec![1];
let mut new_atom = Array2::<f64>::zeros((0, dim_r));
if lat.len_of(Axis(1)) != dim_r {
return Err(TbError::LatticeDimensionError {
expected: dim_r,
actual: lat.len_of(Axis(1)),
});
}
if lat.len_of(Axis(0)) != lat.len_of(Axis(1)) {
return Err(TbError::LatticeDimensionError {
expected: lat.len_of(Axis(1)),
actual: lat.len_of(Axis(0)),
});
}
let new_atom = match atom {
Some(atom0) => atom0,
None => {
// Determine if orbitals belong to the same atom by checking if they are too close;
// this method only works when wannier90 does not perform maximal localization.
let mut new_atom = Vec::new();
new_atom.push(Atom::new(orb.row(0).to_owned(), 1, AtomType::H));
for i in 1..norb {
if (orb.row(i).to_owned() - new_atom[new_atom.len() - 1].position()).norm_l2()
> 1e-2
{
let use_atom = Atom::new(orb.row(i).to_owned(), 1, AtomType::H);
new_atom.push(use_atom);
} else {
let n = new_atom.len();
new_atom[n - 1].push_orb();
}
}
new_atom
}
};
let natom = new_atom.len();
let ham = Array3::<Complex<f64>>::zeros((1, nsta, nsta));
let hamR = Array2::<isize>::zeros((1, dim_r));
let mut rmatrix = Array4::<Complex<f64>>::zeros((1, dim_r, nsta, nsta));
for i in 0..norb {
for r in 0..dim_r {
rmatrix[[0, r, i, i]] = Complex::<f64>::from(orb[[i, r]]);
if spin {
rmatrix[[0, r, i + norb, i + norb]] = Complex::<f64>::from(orb[[i, r]]);
}
}
}
let orb_projection = vec![OrbProj::s; norb];
let mut model = Model {
dim_r: Dimension::try_from(dim_r)?,
spin,
lat,
orb,
orb_projection,
atoms: new_atom,
ham,
hamR,
rmatrix,
};
Ok(model)
}
pub fn set_projection(&mut self, proj: &Vec<OrbProj>) {
//! This function sets the tight-binding model's orbital projections.
self.orb_projection = proj.clone();
}
#[allow(non_snake_case)]
pub fn set_hop<T: Data<Elem = isize>, U: hop_use>(
&mut self,
tmp: U,
ind_i: usize,
ind_j: usize,
R: &ArrayBase<T, Ix1>,
pauli: impl Into<SpinDirection>,
) {
//! This function is used to add hopping to the model. The "set" indicates that it can be used to override previous hopping.
//!
//! - tmp: the parameters for hopping
//!
//! - ind_i and ind_j: the orbital indices in the Hamiltonian, representing hopping from i to j
//!
//! - R: the position of the target unit cell for hopping
//!
//! - pauli: can take the values of 0, 1, 2, or 3, representing $\sigma_0$, $\sigma_x$, $\sigma_y$, $\sigma_z$.
//!
//! In general, this function is used to set $\bra{i\bm 0}\hat H\ket{j\bm R}=$tmp.
let pauli: SpinDirection = pauli.into();
let tmp: Complex<f64> = tmp.to_complex();
if pauli != SpinDirection::None && self.spin == false {
eprintln!("Wrong, if spin is True and pauli is not zero, the pauli is not use")
}
assert!(
R.len() == self.dim_r(),
"Wrong, the R length should equal to dim_r"
);
assert!(
ind_i < self.norb() && ind_j < self.norb(),
"Wrong, ind_i and ind_j must be less than norb, here norb is {}, but ind_i={} and ind_j={}",
self.norb(),
ind_i,
ind_j
);
let norb = self.norb();
let negative_R = &(-R);
match find_R(&self.hamR, &R) {
Some(index) => {
// Get the index of negative R (must exist, otherwise panic)
let index_inv =
find_R(&self.hamR, &negative_R).expect("Negative R not found in hamR");
if self.ham[[index, ind_i, ind_j]] != Complex::new(0.0, 0.0) {
eprintln!(
"Warning, the data of ham you input is {}, not zero, I hope you know what you are doing. If you want to eliminate this warning, use del_add to remove hopping.",
self.ham[[index, ind_i, ind_j]]
);
}
// Update matrix elements at R position
update_hamiltonian!(
self.spin,
pauli,
tmp,
self.ham.slice_mut(s![index, .., ..]),
ind_i,
ind_j,
norb
);
// Update matrix elements at negative R position (unless onsite and R=0)
if index != 0 || ind_i != ind_j {
update_hamiltonian!(
self.spin,
pauli,
tmp.conj(),
self.ham.slice_mut(s![index_inv, .., ..]),
ind_j,
ind_i,
norb
);
}
// Check if onsite matrix element is real
assert!(
!(ind_i == ind_j && tmp.im != 0.0 && index == 0),
"Wrong, the onsite hopping must be real, but here is {}",
tmp
)
}
None => {
let mut new_ham = Array2::<Complex<f64>>::zeros((self.nsta(), self.nsta()));
let new_ham =
update_hamiltonian!(self.spin, pauli, tmp, new_ham, ind_i, ind_j, norb);
self.ham.push(Axis(0), new_ham.view()).unwrap();
self.hamR.push(Axis(0), R.view()).unwrap();
let mut new_ham = Array2::<Complex<f64>>::zeros((self.nsta(), self.nsta()));
let new_ham =
update_hamiltonian!(self.spin, pauli, tmp.conj(), new_ham, ind_j, ind_i, norb);
self.ham.push(Axis(0), new_ham.view()).unwrap();
self.hamR.push(Axis(0), negative_R.view()).unwrap();
}
}
}
#[allow(non_snake_case)]
pub fn add_hop<T: Data<Elem = isize>, U: hop_use>(
&mut self,
tmp: U,
ind_i: usize,
ind_j: usize,
R: &ArrayBase<T, Ix1>,
pauli: impl Into<SpinDirection>,
) {
//! Parameters are the same as set_hop, but $\bra{i\bm 0}\hat H\ket{j\bm R}$ += tmp
let pauli: SpinDirection = pauli.into();
let tmp: Complex<f64> = tmp.to_complex();
if pauli != SpinDirection::None && self.spin == false {
eprintln!("Wrong, if spin is True and pauli is not zero, the pauli is not use")
}
assert!(
R.len() == self.dim_r(),
"Wrong, the R length should equal to dim_r"
);
assert!(
ind_i < self.norb() && ind_j < self.norb(),
"Wrong, ind_i and ind_j must be less than norb, here norb is {}, but ind_i={} and ind_j={}",
self.norb(),
ind_i,
ind_j
);
let norb = self.norb();
let negative_R = &(-R);
match find_R(&self.hamR, &R) {
Some(index) => {
// Get the index of negative R (must exist, otherwise panic)
let index_inv =
find_R(&self.hamR, &negative_R).expect("Negative R not found in hamR");
// Update matrix elements at R position
add_hamiltonian!(
self.spin,
pauli,
tmp,
self.ham.slice_mut(s![index, .., ..]),
ind_i,
ind_j,
norb
);
// Update matrix elements at negative R position (unless onsite and R=0)
if index != 0 || ind_i != ind_j {
add_hamiltonian!(
self.spin,
pauli,
tmp.conj(),
self.ham.slice_mut(s![index_inv, .., ..]),
ind_j,
ind_i,
norb
);
}
// Check if onsite matrix element is real
assert!(
!(ind_i == ind_j && tmp.im != 0.0 && index == 0),
"Wrong, the onsite hopping must be real, but here is {}",
tmp
)
}
None => {
let mut new_ham = Array2::<Complex<f64>>::zeros((self.nsta(), self.nsta()));
let new_ham =
update_hamiltonian!(self.spin, pauli, tmp, new_ham, ind_i, ind_j, norb);
self.ham.push(Axis(0), new_ham.view()).unwrap();
self.hamR.push(Axis(0), R.view()).unwrap();
let mut new_ham = Array2::<Complex<f64>>::zeros((self.nsta(), self.nsta()));
let new_ham =
update_hamiltonian!(self.spin, pauli, tmp.conj(), new_ham, ind_j, ind_i, norb);
self.ham.push(Axis(0), new_ham.view()).unwrap();
self.hamR.push(Axis(0), negative_R.view()).unwrap();
}
}
}
#[allow(non_snake_case)]
pub fn add_element(
&mut self,
tmp: Complex<f64>,
ind_i: usize,
ind_j: usize,
R: &Array1<isize>,
) -> Result<()> {
//! Parameters are the same as set_hop, but $\bra{i\bm 0}\hat H\ket{j\bm R}$ += tmp, ignoring spin, directly adding parameters
if R.len() != self.dim_r() {
return Err(TbError::RVectorLengthError {
expected: self.dim_r(),
actual: R.len(),
});
}
if ind_i >= self.nsta() || ind_j >= self.nsta() {
return Err(TbError::DimensionMismatch {
context: "orbital indices".to_string(),
expected: self.nsta(),
found: std::cmp::max(ind_i, ind_j),
});
}
if let Some(index) = find_R(&self.hamR, &R) {
let index_inv = find_R(&self.hamR, &(-R)).expect("Negative R not found in hamR");
self.ham[[index, ind_i, ind_j]] = tmp;
if index != 0 && ind_i != ind_j {
self.ham[[index_inv, ind_j, ind_i]] = tmp.conj();
}
if ind_i == ind_j && tmp.im != 0.0 && index == 0 {
return Err(TbError::OnsiteHoppingMustBeReal(tmp));
}
} else {
let mut new_ham = Array2::<Complex<f64>>::zeros((self.nsta(), self.nsta()));
new_ham[[ind_i, ind_j]] = tmp;
self.ham.push(Axis(0), new_ham.view()).unwrap();
self.hamR.push(Axis(0), R.view()).unwrap();
let mut new_ham = Array2::<Complex<f64>>::zeros((self.nsta(), self.nsta()));
new_ham[[ind_j, ind_i]] = tmp.conj();
self.ham.push(Axis(0), new_ham.view()).unwrap();
self.hamR.push(Axis(0), (-R).view()).unwrap();
}
Ok(())
}
#[allow(non_snake_case)]
pub fn set_onsite(&mut self, tmp: &Array1<f64>, pauli: impl Into<SpinDirection>) {
//! Directly set diagonal elements
let pauli = pauli.into();
if tmp.len() != self.norb() {
panic!(
"Wrong, the norb is {}, however, the onsite input's length is {}",
self.norb(),
tmp.len()
)
}
for (i, item) in tmp.iter().enumerate() {
self.set_onsite_one(*item, i, pauli);
}
}
#[allow(non_snake_case)]
pub fn add_onsite(&mut self, tmp: &Array1<f64>, pauli: impl Into<SpinDirection>) {
//! Directly set diagonal elements
let pauli = pauli.into();
if tmp.len() != self.norb() {
panic!(
"Wrong, the norb is {}, however, the onsite input's length is {}",
self.norb(),
tmp.len()
)
}
let R = Array1::zeros(self.dim_r());
for (i, item) in tmp.iter().enumerate() {
//self.set_onsite_one(*item,i,pauli)
self.add_hop(Complex::new(*item, 0.0), i, i, &R, pauli)
}
}
#[allow(non_snake_case)]
pub fn set_onsite_one(&mut self, tmp: f64, ind: usize, pauli: impl Into<SpinDirection>) {
//! Set $\bra{i\bm 0}\hat H\ket{i\bm 0}$
let pauli = pauli.into();
let R = Array1::<isize>::zeros(self.dim_r());
self.set_hop(Complex::new(tmp, 0.0), ind, ind, &R, pauli)
}
pub fn del_hop(
&mut self,
ind_i: usize,
ind_j: usize,
R: &Array1<isize>,
pauli: impl Into<SpinDirection>,
) {
//! Delete $\bra{i\bm 0}\hat H\ket{j\bm R}$
let pauli = pauli.into();
if R.len() != self.dim_r() {
panic!("Wrong, the R length should equal to dim_r")
}
if ind_i >= self.norb() || ind_j >= self.norb() {
panic!(
"Wrong, ind_i and ind_j must less than norb, here norb is {}, but ind_i={} and ind_j={}",
self.norb(),
ind_i,
ind_j
)
}
self.set_hop(Complex::new(0.0, 0.0), ind_i, ind_j, &R, pauli);
}
}
impl Model {
pub fn shift_to_atom(&mut self) {
//!这个是将轨道移动到原子位置上
let mut a = 0;
for (i, atom) in self.atoms.iter().enumerate() {
for j in 0..atom.norb() {
self.orb.row_mut(a).assign(&atom.position());
a += 1;
}
}
}
pub fn move_to_atom(&mut self) {
///This function moves the orbital position to the atomic position
let mut a = 0;
for i in 0..self.natom() {
for j in 0..self.atoms[i].norb() {
self.orb.row_mut(a).assign(&self.atoms[i].position());
a += 1;
}
}
}
pub fn remove_orb(&mut self, orb_list: &Vec<usize>) {
let mut use_orb_list = orb_list.clone();
use_orb_list.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
let has_duplicates = { use_orb_list.windows(2).any(|window| window[0] == window[1]) };
if has_duplicates {
panic!("Wrong, make sure no duplicates in orb_list");
}
let mut index: Vec<_> = (0..=self.norb() - 1)
.filter(|&num| !use_orb_list.contains(&num))
.collect(); //要保留下来的元素
let delete_n = orb_list.len();
self.orb = self.orb.select(Axis(0), &index);
let mut new_orb_proj = Vec::new();
for i in index.iter() {
new_orb_proj.push(self.orb_projection[*i])
}
self.orb_projection = new_orb_proj;
let mut b = 0;
for (i, a) in self.atoms.clone().iter().enumerate() {
b += a.norb();
while b > use_orb_list[0] {
self.atoms[i].remove_orb();
let _ = use_orb_list.remove(0);
}
}
self.atoms.retain(|x| x.norb() != 0);
//开始计算nsta
if self.spin {
let index_add: Vec<_> = index.iter().map(|x| *x + self.norb()).collect();
index.extend(index_add);
}
let mut b = 0;
//开始操作哈密顿量
let new_ham = self.ham.select(Axis(1), &index);
let new_ham = new_ham.select(Axis(2), &index);
self.ham = new_ham;
//开始操作rmatrix
let new_rmatrix = self.rmatrix.select(Axis(2), &index);
let new_rmatrix = new_rmatrix.select(Axis(3), &index);
self.rmatrix = new_rmatrix;
}
pub fn remove_atom(&mut self, atom_list: &Vec<usize>) {
//----------判断是否存在重复, 并给出保留的index
let mut use_atom_list = atom_list.clone();
use_atom_list.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
let has_duplicates = {
use_atom_list
.windows(2)
.any(|window| window[0] == window[1])
};
if has_duplicates {
panic!("Wrong, make sure no duplicates in orb_list");
}
let mut atom_index: Vec<_> = (0..=self.natom() - 1)
.filter(|&num| !use_atom_list.contains(&num))
.collect(); //要保留下来的元素
let new_atoms = {
let mut new_atoms = Vec::new();
for i in atom_index.iter() {
new_atoms.push(self.atoms[*i].clone());
}
new_atoms
}; //选出需要的原子以及需要的轨道
//接下来选择需要的轨道
let mut b = 0;
let mut orb_index = Vec::new(); //要保留下来的轨道
let atom_list = self.atom_list();
let mut int_atom_list = Array1::zeros(self.natom());
int_atom_list[[0]] = 0;
for i in 1..self.natom() {
int_atom_list[[i]] = int_atom_list[[i - 1]] + atom_list[i - 1];
}
for i in atom_index.iter() {
for j in 0..self.atoms[*i].norb() {
orb_index.push(int_atom_list[[*i]] + j);
}
}
let norb = self.norb(); //保留之前的norb
self.orb = self.orb.select(Axis(0), &orb_index);
self.atoms = new_atoms;
let mut new_orb_proj = Vec::new();
for i in orb_index.iter() {
new_orb_proj.push(self.orb_projection[*i])
}
self.orb_projection = new_orb_proj;
if self.spin {
let index_add: Vec<_> = orb_index.iter().map(|x| *x + norb).collect();
orb_index.extend(index_add);
}
//开始操作哈密顿量
let new_ham = self.ham.select(Axis(1), &orb_index);
let new_ham = new_ham.select(Axis(2), &orb_index);
self.ham = new_ham;
//开始操作rmatrix
let new_rmatrix = self.rmatrix.select(Axis(2), &orb_index);
let new_rmatrix = new_rmatrix.select(Axis(3), &orb_index);
self.rmatrix = new_rmatrix;
}
pub fn reorder_atom(&mut self, order: &Vec<usize>) {
///这个函数是用来调整模型的原子顺序的, 主要用来检查某些模型
if order.len() != self.natom() {
panic!(
"Wrong! when you using reorder_atom, the order's length {} must equal to the num of atoms {}.",
order.len(),
self.natom()
);
};
//首先我们根据原子顺序得到轨道顺序
let mut new_orb_order = Vec::new();
//第n个原子的最开始的轨道数
let mut orb_atom_map = Vec::new();
let mut a = 0;
for atom in self.atoms.iter() {
orb_atom_map.push(a);
a += atom.norb();
}
for i in order.iter() {
let mut s = String::new();
for j in 0..self.atoms[*i].norb() {
new_orb_order.push(orb_atom_map[*i] + j);
}
}
//重排轨道顺序
self.orb = self.orb.select(Axis(0), &new_orb_order);
let mut new_atom = Vec::new();
//重排轨道projection顺序
let mut new_orb_proj = Vec::new();
for i in new_orb_order.iter() {
new_orb_proj.push(self.orb_projection[*i]);
}
self.orb_projection = new_orb_proj;
//重排原子顺序
for i in 0..self.natom() {
new_atom.push(self.atoms[order[i]].clone());
}
self.atoms = new_atom;
//开始重排哈密顿量
let new_state_order = if self.spin {
//如果有自旋
let mut new_state_order = new_orb_order.clone();
for i in new_orb_order.iter() {
new_state_order.push(*i + self.norb());
}
new_state_order
} else {
new_orb_order
};
self.ham = self.ham.select(Axis(1), &new_state_order);
self.ham = self.ham.select(Axis(2), &new_state_order);
self.rmatrix = self.rmatrix.select(Axis(2), &new_state_order);
self.rmatrix = self.rmatrix.select(Axis(3), &new_state_order);
}
pub fn make_supercell(&self, U: &Array2<f64>) -> Result<Model> {
//这个函数是用来对模型做变换的, 变换前后模型的基矢 $L'=UL$.
//!This function is used to transform the model, where the new basis after transformation is given by $L' = UL$.
if self.dim_r() != U.len_of(Axis(0)) {
return Err(TbError::TransformationMatrixDimMismatch {
expected: self.dim_r(),
actual: U.len_of(Axis(0)),
});
}
//新的lattice
let new_lat = U.dot(&self.lat);
//体积的扩大倍数
let U_det = U.det().unwrap() as isize;
if U_det < 0 {
return Err(TbError::InvalidSupercellDet { det: U_det as f64 });
} else if U_det == 0 {
return Err(TbError::InvalidSupercellDet { det: 0.0 });
}
let U_inv = U.inv().unwrap();
//开始判断是否存在小数
for i in 0..U.len_of(Axis(0)) {
for j in 0..U.len_of(Axis(1)) {
if U[[i, j]].fract() > 1e-8 {
return Err(TbError::InvalidSupercellMatrix);
}
}
}
//开始构建新的轨道位置和原子位置
//新的轨道
let mut use_orb = self.orb.dot(&U_inv);
//新的原子位置
let use_atom_position = self.atom_position().dot(&U_inv);
//新的atom_list
let mut use_atom_list: Vec<usize> = Vec::new();
let mut orb_list: Vec<usize> = Vec::new();
let mut new_orb = Array2::<f64>::zeros((0, self.dim_r()));
let mut new_orb_proj = Vec::new();
let mut new_atom = Vec::new();
let mut a = 0;
for i in 0..self.natom() {
use_atom_list.push(a);
a += self.atoms[i].norb();
}
match self.dim_r() {
3 => {
for i in -U_det - 1..U_det + 1 {
for j in -U_det - 1..U_det + 1 {
for k in -U_det - 1..U_det + 1 {
for n in 0..self.natom() {
let mut atoms = use_atom_position.row(n).to_owned()
+ (i as f64) * U_inv.row(0).to_owned()
+ (j as f64) * U_inv.row(1).to_owned()
+ (k as f64) * U_inv.row(2).to_owned(); //原子的位置在新的坐标系下的坐标
atoms[[0]] = if atoms[[0]].abs() < 1e-8 {
0.0
} else if (atoms[[0]] - 1.0).abs() < 1e-8 {
1.0
} else {
atoms[[0]]
};
atoms[[1]] = if atoms[[1]].abs() < 1e-8 {
0.0
} else if (atoms[[1]] - 1.0).abs() < 1e-8 {
1.0
} else {
atoms[[1]]
};
atoms[[2]] = if atoms[[2]].abs() < 1e-8 {
0.0
} else if (atoms[[2]] - 1.0).abs() < 1e-8 {
1.0
} else {
atoms[[2]]
};
if atoms.iter().all(|x| *x >= 0.0 && *x < 1.0) {
//判断是否在原胞内
new_atom.push(Atom::new(
atoms,
self.atoms[n].norb(),
self.atoms[n].atom_type(),
));
for n0 in
use_atom_list[n]..use_atom_list[n] + self.atoms[n].norb()
{
//开始根据原子位置开始生成轨道
let mut orbs = use_orb.row(n0).to_owned()
+ (i as f64) * U_inv.row(0).to_owned()
+ (j as f64) * U_inv.row(1).to_owned()
+ (k as f64) * U_inv.row(2).to_owned(); //新的轨道的坐标
new_orb.push_row(orbs.view());
new_orb_proj.push(self.orb_projection[n0]);
orb_list.push(n0);
}
}
}
}
}
}
}
2 => {
for i in -U_det - 1..U_det + 1 {
for j in -U_det - 1..U_det + 1 {
for n in 0..self.natom() {
let mut atoms = use_atom_position.row(n).to_owned()
+ (i as f64) * U_inv.row(0).to_owned()
+ (j as f64) * U_inv.row(1).to_owned(); //原子的位置在新的坐标系下的坐标
atoms[[0]] = if atoms[[0]].abs() < 1e-8 {
0.0
} else if (atoms[[0]] - 1.0).abs() < 1e-8 {
1.0
} else {
atoms[[0]]
};
atoms[[1]] = if atoms[[1]].abs() < 1e-8 {
0.0
} else if (atoms[[1]] - 1.0).abs() < 1e-8 {
1.0
} else {
atoms[[1]]
};
if atoms.iter().all(|x| *x >= 0.0 && *x < 1.0) {
//判断是否在原胞内
new_atom.push(Atom::new(
atoms,
self.atoms[n].norb(),
self.atoms[n].atom_type(),
));
for n0 in use_atom_list[n]..use_atom_list[n] + self.atoms[n].norb()
{
//开始根据原子位置开始生成轨道
let mut orbs = use_orb.row(n0).to_owned()
+ (i as f64) * U_inv.row(0).to_owned()
+ (j as f64) * U_inv.row(1).to_owned(); //新的轨道的坐标
new_orb.push_row(orbs.view());
new_orb_proj.push(self.orb_projection[n0]);
orb_list.push(n0);
//orb_list_R.push_row(&arr1(&[i,j]));
}
}
}
}
}
}
1 => {
for i in -U_det - 1..U_det + 1 {
for n in 0..self.natom() {
let mut atoms = use_atom_position.row(n).to_owned()
+ (i as f64) * U_inv.row(0).to_owned(); //原子的位置在新的坐标系下的坐标
atoms[[0]] = if atoms[[0]].abs() < 1e-8 {
0.0
} else if (atoms[[0]] - 1.0).abs() < 1e-8 {
1.0
} else {
atoms[[0]]
};
if atoms.iter().all(|x| *x >= 0.0 && *x < 1.0) {
//判断是否在原胞内
new_atom.push(Atom::new(
atoms,
self.atoms[n].norb(),
self.atoms[n].atom_type(),
));
for n0 in use_atom_list[n]..use_atom_list[n] + self.atoms[n].norb() {
//开始根据原子位置开始生成轨道
let mut orbs = use_orb.row(n0).to_owned()
+ (i as f64) * U_inv.row(0).to_owned(); //新的轨道的坐标
new_orb.push_row(orbs.view());
new_orb_proj.push(self.orb_projection[n0]);
orb_list.push(n0);
//orb_list_R.push_row(&arr1(&[i]));
}
}
}
}
}
_ => todo!(),
}
//轨道位置和原子位置构建完成, 接下来我们开始构建哈密顿量
let norb = new_orb.len_of(Axis(0));
let nsta = if self.spin { 2 * norb } else { norb };
let natom = new_atom.len();
let n_R = self.hamR.len_of(Axis(0));
let mut new_hamR = Array2::<isize>::zeros((1, self.dim_r())); //超胞准备用的hamR
let mut use_hamR = Array2::<isize>::zeros((1, self.dim_r())); //超胞的hamR的可能, 如果这个hamR没有对应的hopping就会被删除
let mut new_ham = Array3::<Complex<f64>>::zeros((1, nsta, nsta)); //超胞准备用的ham
//超胞准备用的rmatrix
let mut new_rmatrix = Array4::<Complex<f64>>::zeros((1, self.dim_r(), nsta, nsta));
let max_use_hamR = self.hamR.mapv(|x| x as f64);
let max_use_hamR = max_use_hamR.dot(&U.inv().unwrap());
let mut max_hamR =
max_use_hamR
.outer_iter()
.fold(Array1::zeros(self.dim_r()), |mut acc, x| {
for i in 0..self.dim_r() {
acc[[i]] = if acc[[i]] > x[[i]].abs() {
acc[[i]]
} else {
x[[i]].abs()
};
}
acc
});
let max_R = max_hamR.mapv(|x| (x.ceil() as isize) + 1);
//let mut max_R=Array1::<isize>::zeros(self.dim_r());
//let max_R:isize=U_det.abs()*(self.dim_r() as isize);
//let max_R=Array1::<isize>::ones(self.dim_r())*max_R;
//用来产生可能的hamR
match self.dim_r() {
1 => {
for i in -max_R[[0]]..max_R[[0]] + 1 {
if i != 0 {
use_hamR.push_row(array![i].view());
}
}
}
2 => {
for j in -max_R[[1]]..max_R[[1]] + 1 {
for i in -max_R[[0]]..max_R[[0]] + 1 {
if i != 0 || j != 0 {
use_hamR.push_row(array![i, j].view());
}
}
}
}
3 => {
for k in -max_R[[2]]..max_R[[2]] + 1 {
for i in -max_R[[0]]..max_R[[0]] + 1 {
for j in -max_R[[1]]..max_R[[1]] + 1 {
if i != 0 || j != 0 || k != 0 {
use_hamR.push_row(array![i, j, k].view());
}
}
}
}
}
_ => todo!(),
}
let use_n_R = use_hamR.len_of(Axis(0));
let mut gen_rmatrix: bool = false;
if self.rmatrix.len_of(Axis(0)) == 1 {
for i in 0..self.dim_r() {
for s in 0..norb {
new_rmatrix[[0, i, s, s]] = Complex::new(new_orb[[s, i]], 0.0);
}
}
if self.spin {
for i in 0..self.dim_r() {
for s in 0..norb {
new_rmatrix[[0, i, s + norb, s + norb]] =
Complex::new(new_orb[[s, i]], 0.0);
}
}
}
} else {
gen_rmatrix = true;
}
if self.spin && gen_rmatrix {
for (R, use_R) in use_hamR.outer_iter().enumerate() {
let mut add_R: bool = false;
let mut useham = Array2::<Complex<f64>>::zeros((nsta, nsta));
let mut use_rmatrix = Array3::<Complex<f64>>::zeros((self.dim_r(), nsta, nsta));
for (int_i, use_i) in orb_list.iter().enumerate() {
for (int_j, use_j) in orb_list.iter().enumerate() {
//接下来计算超胞中的R在原胞中对应的hamR
let R0: Array1<f64> = new_orb.row(int_j).to_owned()
- new_orb.row(int_i).to_owned()
+ use_R.mapv(|x| x as f64); //超胞的 R 在原始原胞的 R
let R0: Array1<isize> =
(R0.dot(U) - self.orb.row(*use_j) + self.orb.row(*use_i)).mapv(|x| {
if x.fract().abs() < 1e-8 || x.fract().abs() > 1.0 - 1e-8 {
x.round() as isize
} else {
x.floor() as isize
}
});
if let Some(index) = find_R(&self.hamR, &R0) {
add_R = true;
useham[[int_i, int_j]] = self.ham[[index, *use_i, *use_j]];
useham[[int_i + norb, int_j]] =
self.ham[[index, *use_i + self.norb(), *use_j]];
useham[[int_i, int_j + norb]] =
self.ham[[index, *use_i, *use_j + self.norb()]];
useham[[int_i + norb, int_j + norb]] =
self.ham[[index, *use_i + self.norb(), *use_j + self.norb()]];
for r in 0..self.dim_r() {
use_rmatrix[[r, int_i, int_j]] =
self.rmatrix[[index, r, *use_i, *use_j]];
use_rmatrix[[r, int_i + norb, int_j]] =
self.rmatrix[[index, r, *use_i + self.norb(), *use_j]];
use_rmatrix[[r, int_i, int_j + norb]] =
self.rmatrix[[index, r, *use_i, *use_j + self.norb()]];
use_rmatrix[[r, int_i + norb, int_j + norb]] = self.rmatrix
[[index, r, *use_i + self.norb(), *use_j + self.norb()]];
}
} else {
continue;
}
}
}
if add_R && R != 0 {
new_ham.push(Axis(0), useham.view());
new_hamR.push_row(use_R.view());
new_rmatrix.push(Axis(0), use_rmatrix.view());
} else if R == 0 {
new_ham.slice_mut(s![0, .., ..]).assign(&useham);
new_rmatrix
.slice_mut(s![0, .., .., ..])
.assign(&use_rmatrix);
}
}
} else if gen_rmatrix && !self.spin {
for (R, use_R) in use_hamR.outer_iter().enumerate() {
let mut add_R: bool = false;
let mut useham = Array2::<Complex<f64>>::zeros((norb, norb));
let mut use_rmatrix = Array3::<Complex<f64>>::zeros((self.dim_r(), norb, norb));
for (int_i, use_i) in orb_list.iter().enumerate() {
for (int_j, use_j) in orb_list.iter().enumerate() {
//接下来计算超胞中的R在原胞中对应的hamR
let R0: Array1<f64> = new_orb.row(int_j).to_owned()
- new_orb.row(int_i).to_owned()
+ use_R.mapv(|x| x as f64); //超胞的 R 在原始原胞的 R
let R0: Array1<isize> =
(R0.dot(U) - self.orb.row(*use_j) + self.orb.row(*use_i)).mapv(|x| {
if x.fract().abs() < 1e-8 || x.fract().abs() > 1.0 - 1e-8 {
x.round() as isize
} else {
x.floor() as isize
}
});
if let Some(index) = find_R(&self.hamR, &R0) {
add_R = true;
useham[[int_i, int_j]] = self.ham[[index, *use_i, *use_j]];
for r in 0..self.dim_r() {
use_rmatrix[[r, int_i, int_j]] =
self.rmatrix[[index, r, *use_i, *use_j]]
}
} else {
continue;
}
}
}
if add_R && R != 0 {
new_ham.push(Axis(0), useham.view());
new_rmatrix.push(Axis(0), use_rmatrix.view());
new_hamR.push_row(use_R);
} else if R == 0 {
new_ham.slice_mut(s![0, .., ..]).assign(&useham);
new_rmatrix
.slice_mut(s![0, .., .., ..])
.assign(&use_rmatrix);
}
}
} else if self.spin {
for (R, use_R) in use_hamR.outer_iter().enumerate() {
let mut add_R: bool = false;
let mut useham = Array2::<Complex<f64>>::zeros((nsta, nsta));
for (int_i, use_i) in orb_list.iter().enumerate() {
for (int_j, use_j) in orb_list.iter().enumerate() {
//接下来计算超胞中的R在原胞中对应的hamR
let R0: Array1<f64> =
&new_orb.row(int_j) - &new_orb.row(int_i) + &use_R.map(|x| *x as f64); //超胞的 R 在原始原胞的 R
let R0: Array1<isize> =
(R0.dot(U) - self.orb.row(*use_j) + self.orb.row(*use_i)).mapv(|x| {
if x.fract().abs() < 1e-8 || x.fract().abs() > 1.0 - 1e-8 {
x.round() as isize
} else {
x.floor() as isize
}
});
if let Some(index) = find_R(&self.hamR, &R0) {
add_R = true;
useham[[int_i, int_j]] = self.ham[[index, *use_i, *use_j]];
useham[[int_i + norb, int_j]] =
self.ham[[index, *use_i + self.norb(), *use_j]];
useham[[int_i, int_j + norb]] =
self.ham[[index, *use_i, *use_j + self.norb()]];
useham[[int_i + norb, int_j + norb]] =
self.ham[[index, *use_i + self.norb(), *use_j + self.norb()]];
} else {
continue;
}
}
}
if add_R && R != 0 {
new_ham.push(Axis(0), useham.view());
new_hamR.push_row(use_R.view());
} else if R == 0 {
new_ham.slice_mut(s![0, .., ..]).assign(&useham);
}
}
} else {
for (R, use_R) in use_hamR.outer_iter().enumerate() {
let mut add_R: bool = false;
let mut useham = Array2::<Complex<f64>>::zeros((nsta, nsta));
for (int_i, use_i) in orb_list.iter().enumerate() {
for (int_j, use_j) in orb_list.iter().enumerate() {
//接下来计算超胞中的R在原胞中对应的hamR
let R0: Array1<f64> = new_orb.row(int_j).to_owned()
- new_orb.row(int_i).to_owned()
+ use_R.mapv(|x| x as f64); //超胞的 R 在原始原胞的 R
let R0: Array1<isize> =
(R0.dot(U) - self.orb.row(*use_j) + self.orb.row(*use_i)).mapv(|x| {
if x.fract().abs() < 1e-8 || x.fract().abs() > 1.0 - 1e-8 {
x.round() as isize
} else {
x.floor() as isize
}
});
if let Some(index) = find_R(&self.hamR, &R0) {
add_R = true;
useham[[int_i, int_j]] = self.ham[[index, *use_i, *use_j]];
} else {
continue;
}
}
}
if add_R && R != 0 {
new_ham.push(Axis(0), useham.view());
new_hamR.push_row(use_R);
} else if R == 0 {
new_ham.slice_mut(s![0, .., ..]).assign(&useham);
}
}
}
// Keep new_rmatrix in sync with new_ham for magnetic field compatibility
let n_r = new_ham.len_of(Axis(0));
if new_rmatrix.len_of(Axis(0)) < n_r {
let extra = n_r - new_rmatrix.len_of(Axis(0));
let zero_rm = Array3::<Complex<f64>>::zeros((self.dim_r(), nsta, nsta));
for _ in 0..extra {
new_rmatrix.push(Axis(0), zero_rm.view());
}
}
let mut model = Model {
dim_r: Dimension::try_from(self.dim_r())?,
spin: self.spin,
lat: new_lat,
orb: new_orb,
orb_projection: new_orb_proj,
atoms: new_atom,
ham: new_ham,
hamR: new_hamR,
rmatrix: new_rmatrix,
};
Ok(model)
}
}