-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathExcitonTB.cpp
More file actions
1441 lines (1215 loc) · 54 KB
/
Copy pathExcitonTB.cpp
File metadata and controls
1441 lines (1215 loc) · 54 KB
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
#include <math.h>
#include <iomanip>
#include "xatu/ExcitonTB.hpp"
#include "xatu/utils.hpp"
#include "xatu/davidson.hpp"
using namespace arma;
using namespace std::chrono;
namespace xatu {
/**
* Method to set the values of the attributes of an exciton object.
* @param ncell Number of unit cells per axis.
* @param bands Vector with the indices of the bands that form the exciton.
* @param parameters Dielectric constants and screening length.
* @param Q Center-of-mass momentum of the exciton.
* @return void
*/
void ExcitonTB::initializeExcitonAttributes(int ncell, const arma::ivec& bands,
const arma::rowvec& parameters, const arma::rowvec& Q){
this->ncell_ = ncell;
this->totalCells_ = pow(ncell, system_->ndim);
this->Q_ = Q;
this->bands_ = bands;
this->eps_m_ = parameters(0);
this->eps_s_ = parameters(1);
this->r0_ = parameters(2);
// Set ry
if (parameters.n_elem >= 4) {
this->ry_ = parameters(3);
} else {
this->ry_ = r0_;
}
// Set rz
if (parameters.n_elem >= 5) {
this->rz_ = parameters(4);
} else {
this->rz_ = 0.5 * (this->r0_ + this->ry_);
}
this->cutoff_ = ncell/2.5; // Default value, seems to preserve crystal point group
if(r0 == 0){
throw std::invalid_argument("Error: r0 must be non-zero");
}
}
/**
* Method to set the attributes of an exciton object from a ExcitonConfiguration object.
* @details Overload of the method to use a configuration object. Based on the parametric method.
* @param cfg ExcitonConfiguration object from parsed file.
* @return void
*/
void ExcitonTB::initializeExcitonAttributes(const ExcitonConfiguration& cfg){
int ncell = cfg.excitonInfo.ncell;
int nbands = cfg.excitonInfo.nbands;
arma::ivec bands = cfg.excitonInfo.bands;
arma::rowvec parameters = arma::conv_to<arma::rowvec>::from(cfg.excitonInfo.eps);
arma::rowvec Q = cfg.excitonInfo.Q;
if (bands.empty()){
bands = arma::regspace<arma::ivec>(- nbands + 1, nbands);
}
initializeExcitonAttributes(ncell, bands, parameters, Q);
std::vector<arma::s64> valence, conduction;
for(int i = 0; i < bands.n_elem; i++){
if (bands(i) <= 0){
valence.push_back(bands(i) + system->fermiLevel);
}
else{
conduction.push_back(bands(i) + system->fermiLevel);
}
}
this->valenceBands_ = arma::ivec(valence);
this->conductionBands_ = arma::ivec(conduction);
this->bandList_ = arma::conv_to<arma::uvec>::from(arma::join_cols(valenceBands, conductionBands));
// Set flags
this->exchange = cfg.excitonInfo.exchange;
this->scissor_ = cfg.excitonInfo.scissor;
this->mode_ = cfg.excitonInfo.mode;
this->nReciprocalVectors_ = cfg.excitonInfo.nReciprocalVectors;
this->regularization_ = cfg.excitonInfo.regularization;
if (regularization_ == 0){
regularization_ = system->a;
}
this->potential_ = cfg.excitonInfo.potential;
this->exchangePotential_ = cfg.excitonInfo.exchangePotential;
}
/**
* Exciton constructor from a SystemConfiguration object and a vector with the bands that form
* the exciton, as well as the other parameters.
* @param config SystemConfiguration object from config file.
* @param ncell Number of unit cells along each axis.
* @param bands Vector with the indices of the bands that form the exciton.
* @param parameters Vector with dielectric constants and screening length.
* @param Q Center-of-mass momentum.
*/
ExcitonTB::ExcitonTB(const SystemConfiguration& config, int ncell, const arma::ivec& bands,
const arma::rowvec& parameters, const arma::rowvec& Q) {
system_.reset(new SystemTB(config));
initializeExcitonAttributes(ncell, bands, parameters, Q);
if (bands.n_elem > excitonbasisdim){
cout << "Error: Number of bands cannot be higher than actual material bands" << endl;
exit(1);
}
// arma::ivec is implemented with typedef s64
std::vector<arma::s64> valence, conduction;
for(int i = 0; i < bands.n_elem; i++){
if (bands(i) <= 0){
valence.push_back(bands(i) + system->fermiLevel);
}
else{
conduction.push_back(bands(i) + system->fermiLevel);
}
}
this->valenceBands_ = arma::ivec(valence);
this->conductionBands_ = arma::ivec(conduction);
this->bandList_ = arma::conv_to<arma::uvec>::from(arma::join_cols(valenceBands, conductionBands));
};
/**
* Exciton constructor from a SystemConfiguration object. One specifies the number of valence and conduction
* bands, as well as the other parameters.
* @param config SystemConfiguration object from config file.
* @param ncell Number of unit cells along each axis.
* @param nbands Number of bands (same number for both valence and conduction) that form the exciton.
* @param nrmbands Number of bands to be removed with respect to the Fermi level.
* @param parameters Vector with dielectric constants and screening length.
* @param Q Center-of-mass momentum.
*/
ExcitonTB::ExcitonTB(const SystemConfiguration& config, int ncell, int nbands, int nrmbands,
const arma::rowvec& parameters, const arma::rowvec& Q) :
ExcitonTB(config, ncell, {}, parameters, Q){
if (2*nbands > system->basisdim){
cout << "Error: Number of bands cannot be higher than actual material bands" << endl;
exit(1);
}
int fermiLevel = system->fermiLevel;
this->valenceBands_ = arma::regspace<arma::ivec>(fermiLevel - nbands - nrmbands + 1,
fermiLevel - nrmbands);
this->conductionBands_ = arma::regspace<arma::ivec>(fermiLevel + 1 + nrmbands,
fermiLevel + nbands + nrmbands);
this->bands_ = arma::join_cols(valenceBands, conductionBands) - fermiLevel;
this->bandList_ = arma::conv_to<arma::uvec>::from(arma::join_cols(valenceBands, conductionBands));
};
/**
* Exciton constructor from SystemConfiguration and ExcitonConfiguration.
* @param config SystemConfiguration object.
* @param excitonConfig ExcitonConfiguration object.
*/
ExcitonTB::ExcitonTB(const SystemConfiguration& config, const ExcitonConfiguration& excitonConfig){
system_.reset(new SystemTB(config));
initializeExcitonAttributes(excitonConfig);
}
ExcitonTB::ExcitonTB(std::shared_ptr<SystemTB> sys, int ncell, const arma::ivec& bands,
const arma::rowvec& parameters, const arma::rowvec& Q){
system_ = sys;
initializeExcitonAttributes(ncell, bands, parameters, Q);
if (bands.n_elem > system->basisdim){
cout << "Error: Number of bands cannot be higher than actual material bands" << endl;
exit(1);
}
// arma::ivec is implemented with typedef s64
std::vector<arma::s64> valence, conduction;
for(int i = 0; i < bands.n_elem; i++){
if (bands(i) <= 0){
valence.push_back(bands(i) + system->fermiLevel);
}
else{
conduction.push_back(bands(i) + system->fermiLevel);
}
}
this->valenceBands_ = arma::ivec(valence);
this->conductionBands_ = arma::ivec(conduction);
this->bandList_ = arma::conv_to<arma::uvec>::from(arma::join_cols(valenceBands, conductionBands));
};
/**
* Exciton constructor from an already initialized System object, and all the exciton parameters.
* @param system System object where excitons are computed.
* @param ncell Number of unit cells along each axis.
* @param nbands Number of bands (same for valence and conduction) that form the exciton.
* @param nrmbands Number of bands to be removed with respect to the Fermi level.
* @param parameters Dielectric constant and screening length.
* @param Q Center-of-mass momentum of the exciton.
*/
ExcitonTB::ExcitonTB(std::shared_ptr<SystemTB> sys, int ncell, int nbands, int nrmbands,
const arma::rowvec& parameters, const arma::rowvec& Q) :
ExcitonTB(sys, ncell, {}, parameters, Q) {
if (2*nbands > system->basisdim){
cout << "Error: Number of bands cannot be higher than actual material bands" << endl;
exit(1);
}
int fermiLevel = system->fermiLevel;
this->valenceBands_ = arma::regspace<arma::ivec>(fermiLevel - nbands - nrmbands + 1,
fermiLevel - nrmbands);
this->conductionBands_ = arma::regspace<arma::ivec>(fermiLevel + 1 + nrmbands,
fermiLevel + nbands + nrmbands);
this->bands_ = arma::join_cols(valenceBands, conductionBands) - fermiLevel;
this->bandList_ = arma::conv_to<arma::uvec>::from(arma::join_cols(valenceBands, conductionBands));
};
/**
* Exciton destructor.
* @details Used mainly for debugging; the message should be removed at some point.
*/
// ExcitonTB::~ExcitonTB(){};
/* ------------------------------ Setters ------------------------------ */
/**
* Method to set the parameters of the Keldysh potential, namely the environmental
* dielectric constants and the effective screening lengths.
* @param parameters Vector with 3 to 5 parameters: '{eps_m, eps_s, r0[, ry[, rz]]}'.
* @return void
*/
void ExcitonTB::setParameters(const arma::rowvec& parameters) {
if (parameters.n_elem < 3) {
std::cout << "parameters array must be at least 3D (eps_m, eps_s, r0)" << std::endl;
return;
}
eps_m_ = parameters(0);
eps_s_ = parameters(1);
r0_ = parameters(2);
// Set ry
if (parameters.n_elem >= 4) {
ry_ = parameters(3);
} else {
ry_ = r0_;
}
// Set rz
if (parameters.n_elem >= 5) {
rz_ = parameters(4);
} else {
rz_ = 0.5 * (r0_ + ry_);
}
}
/**
* Sets the parameters of the Keldysh potential.
* @param eps_m Dielectric constant of embedding medium.
* @param eps_s Dielectric constant of substrate.
* @param r0 Effective screeening length.
* @return void
*/
void ExcitonTB::setParameters(double eps_m, double eps_s, double r0, double ry, double rz){
eps_m_ = eps_m;
eps_s_ = eps_s;
r0_ = r0;
// Set ry
if (ry != 0) {
ry_ = ry;
} else {
ry_ = r0;
}
// Set rz
if (rz != 0) {
rz_ = rz;
} else {
rz_ = 0.5 * (r0 + ry);
}
}
/**
* Sets the gauge used for the Bloch basis, either 'lattice' or 'atomic'.
* @param gauge Gause to be used, default to 'lattice'.
* @return void
*/
void ExcitonTB::setGauge(std::string gauge){
if(gauge != "lattice" && gauge != "atomic"){
throw std::invalid_argument("setGauge(): gauge must be either lattice or atomic");
}
this->gauge_ = gauge;
}
/**
* Sets the type of calculation used to obtain the exciton spectrum. It can be 'realspace' (default)
* or 'reciprocalspace'.
* @param mode Calculation model.
* @return void
*/
void ExcitonTB::setMode(std::string mode){
if(mode != "realspace" && mode != "reciprocalspace"){
throw std::invalid_argument("setMode(): mode must be either realspace or reciprocalspace");
}
this->mode_ = mode;
}
/**
* Sets the number of reciprocal vectors to use if the exciton calculation is set to 'reciprocalspace'.
* @param nReciprocalVector Number of reciprocal vectors to sum over.
* @return void
*/
void ExcitonTB::setReciprocalVectors(int nReciprocalVectors){
if(nReciprocalVectors < 0){
throw std::invalid_argument("setReciprocalVectors(): given number must be positive");
}
this->nReciprocalVectors_ = nReciprocalVectors;
}
/**
* Sets the regularization distance for the Coulomb potential divergence at r=0.
* @param regularization Distance in Angstroms.
* @return void
*/
void ExcitonTB::setRegularization(double regularization){
this->regularization_ = regularization;
}
/*---------------------------------------- Potentials ----------------------------------------*/
/**
* Purpose: Compute Struve function H0(x).
* Source: http://jean-pierre.moreau.pagesperso-orange.fr/Cplus/mstvh0_cpp.txt
* @param X x --- Argument of H0(x) ( x ò 0 )
* @param SH0 SH0 --- H0(x). The return value is written to the direction of the pointer.
*/
void ExcitonTB::STVH0(double X, double *SH0) {
double A0,BY0,P0,Q0,R,S,T,T2,TA0;
int K, KM;
S=1.0;
R=1.0;
if (X <= 20.0) {
A0=2.0*X/PI;
for (K=1; K<61; K++) {
R=-R*X/(2.0*K+1.0)*X/(2.0*K+1.0);
S=S+R;
if (fabs(R) < fabs(S)*1.0e-12) goto e15;
}
e15: *SH0=A0*S;
}
else {
KM=int(0.5*(X+1.0));
if (X >= 50.0) KM=25;
for (K=1; K<=KM; K++) {
R=-R*pow((2.0*K-1.0)/X,2);
S=S+R;
if (fabs(R) < fabs(S)*1.0e-12) goto e25;
}
e25: T=4.0/X;
T2=T*T;
P0=((((-.37043e-5*T2+.173565e-4)*T2-.487613e-4)*T2+.17343e-3)*T2-0.1753062e-2)*T2+.3989422793;
Q0=T*(((((.32312e-5*T2-0.142078e-4)*T2+0.342468e-4)*T2-0.869791e-4)*T2+0.4564324e-3)*T2-0.0124669441);
TA0=X-0.25*PI;
BY0=2.0/sqrt(X)*(P0*sin(TA0)+Q0*cos(TA0));
*SH0=2.0/(PI*X)*S+BY0;
}
}
/**
* Calculate value of interaction potential (Keldysh). Units are eV.
* @details If the distance is zero, then the interaction is renormalized to be V(a) since
* V(0) is infinite, where a is the lattice parameter. Also, for r > cutoff the interaction is taken to be zero.
* @param r Distance at which we evaluate the potential.
* @return Value of Keldysh potential, V(r).
*/
double ExcitonTB::keldysh(arma::rowvec r){
double eps_bar = (eps_m + eps_s)/2;
double SH0;
double cutoff = arma::norm(system->bravaisLattice.row(0)) * cutoff_ + 1E-5;
arma::rowvec R0 = {r0,ry,rz};
double R = arma::norm(r/R0);
double r_norm = arma::norm(r);
double r0avg = (r0 + ry + rz)/3;
double potential_value;
if(r_norm < 1E-10){
STVH0(regularization/r0, &SH0);
potential_value = ec/(8E-10*eps0*eps_bar*r0avg)*(SH0 - y0(regularization/r0avg));
}
else if (r_norm > cutoff){
potential_value = 0.0;
}
else{
STVH0(R, &SH0);
potential_value = ec/(8E-10*eps0*eps_bar*r0avg)*(SH0 - y0(R));
};
return potential_value;
};
/**
* Coulomb potential in real space.
* @param r Distance at which we evaluate the potential.
* @param regularization Regularization distance to remove divergence at r=0.
* @return Value of Coulomb potential, V(r).
*/
double ExcitonTB::coulomb(arma::rowvec r){
double cutoff = arma::norm(system->bravaisLattice.row(0)) * cutoff_ + 1E-5;
double R = abs(arma::norm(r));
if (R > cutoff){
return 0.0;
}
return (R != 0) ? ec/(4E-10*PI*eps0*R) : ec*1E10/(4*PI*eps0*regularization);
}
/**
* Method to select the potential to be used in the of the exciton calculation.
* @param potential Potential to be used in the direct term.
* @return Pointer to function representing the potential.
*/
potptr ExcitonTB::selectPotential(std::string potential){
if(potential == "keldysh"){
return &ExcitonTB::keldysh;
}
else if(potential == "coulomb"){
return &ExcitonTB::coulomb;
}
else{
throw std::invalid_argument("selectPotential(): potential must be either 'keldysh' or 'coulomb'");
}
}
/*---------------------------------------- Fourier transforms ----------------------------------------*/
/**
* Evaluates the Fourier transform of the Keldysh potential, which is an analytical expression.
* @param q kpoint where we evaluate the FT.
* @return Fourier transform of the potential at q, FT[V](q).
*/
double ExcitonTB::keldyshFT(arma::rowvec q){
double radius = cutoff*arma::norm(system->reciprocalLattice.row(0));
double potential = 0;
double eps_bar = (eps_m + eps_s)/2;
double eps = arma::norm(system->reciprocalLattice.row(0))/totalCells;
double qnorm = arma::norm(q);
if (qnorm < eps){
potential = 0;
}
else{
potential = 1/(qnorm*(1 + r0*qnorm));
}
potential = potential*ec*1E10/(2*eps0*eps_bar*system->unitCellArea*totalCells);
return potential;
}
/**
* Routine to compute the lattice Fourier transform with the potential displaced by some
* vectors of the motif.
* @param fAtomIndex Index of first atom of the motif.
* @param sAtomIndex Index of second atom of the motif.
* @param k kpoint where we evaluate the FT.
* @param cells Matrix with the unit cells over which we sum to compute the lattice FT.
* @param potential Pointer to potential function.
* @return Motif lattice Fourier transform of the Keldysh potential at k.
*/
std::complex<double> ExcitonTB::motifFourierTransform(int fAtomIndex, int sAtomIndex, const arma::rowvec& k,
const arma::mat& cells, potptr potential){
std::complex<double> imag(0,1);
std::complex<double> Vk = 0.0;
arma::rowvec firstAtom = system->motif.row(fAtomIndex).subvec(0, 2);
arma::rowvec secondAtom = system->motif.row(sAtomIndex).subvec(0, 2);
for(int n = 0; n < cells.n_rows; n++){
arma::rowvec cell = cells.row(n);
// double module = arma::norm(cell + firstAtom - secondAtom);
arma::rowvec dist = cell + firstAtom - secondAtom;
Vk += (this->*potential)(dist)*std::exp(imag*arma::dot(k, cell));
}
Vk /= pow(totalCells, 1);
return Vk;
}
/**
* Method to compute the motif FT matrix at a given k vector.
* @param k k vector where we compute the motif FT.
* @param cells Matrix of unit cells over which the motif FT is computed.
* @param potential Pointer to potential function.
* @return void
*/
arma::cx_mat ExcitonTB::motifFTMatrix(const arma::rowvec& k, const arma::mat& cells, potptr potential){
// Uses hermiticity of V
int natoms = system->natoms;
arma::cx_mat motifFT = arma::zeros<arma::cx_mat>(natoms, natoms);
for(int fAtomIndex = 0; fAtomIndex < natoms; fAtomIndex++){
for(int sAtomIndex = fAtomIndex; sAtomIndex < natoms; sAtomIndex++){
motifFT(fAtomIndex, sAtomIndex) = motifFourierTransform(fAtomIndex, sAtomIndex, k, cells, potential);
motifFT(sAtomIndex, fAtomIndex) = conj(motifFT(fAtomIndex, sAtomIndex));
}
}
return motifFT;
}
/**
* Method to extend the motif Fourier transform matrix to match the dimension of the
* one-particle basis.
* @param motifFT Matrix storing the motif Fourier transform to be extended.
* @return Extended matrix.
*/
arma::cx_mat ExcitonTB::extendMotifFT(const arma::cx_mat& motifFT){
arma::cx_mat extendedMFT = arma::zeros<arma::cx_mat>(system->basisdim, system->basisdim);
int rowIterator = 0;
int colIterator = 0;
for(unsigned int atom_index_r = 0; atom_index_r < system->motif.n_rows; atom_index_r++){
int species_r = system->motif.row(atom_index_r)(3);
int norbitals_r = system->orbitals(species_r);
colIterator = 0;
for(unsigned int atom_index_c = 0; atom_index_c < system->motif.n_rows; atom_index_c++){
int species_c = system->motif.row(atom_index_c)(3);
int norbitals_c = system->orbitals(species_c);
extendedMFT.submat(rowIterator, colIterator,
rowIterator + norbitals_r - 1, colIterator + norbitals_c - 1) =
motifFT(atom_index_r, atom_index_c) * arma::ones(norbitals_r, norbitals_c);
colIterator += norbitals_c;
}
rowIterator += norbitals_r;
}
return extendedMFT;
}
/*------------------------------------ Interaction matrix elements ------------------------------------*/
/**
* Real space implementation of interaction term, valid for both direct and exchange.
* To compute the direct term, the expected order is (ck,v'k',c'k',vk).
* For the exchange term, the order is (ck,v'k',vk,c'k').
* @param coefsK1 First eigenstate vector.
* @param coefsK2 Second eigenstate vector.
* @param coefsK3 Third eigenstate vector.
* @param coefsK4 Fourth eigenstate vector.
* @param motifFT Motif Fourier transform.
* @return Interaction term.
*/
std::complex<double> ExcitonTB::realSpaceInteractionTerm(const arma::cx_vec& coefsK1,
const arma::cx_vec& coefsK2,
const arma::cx_vec& coefsK3,
const arma::cx_vec& coefsK4,
const arma::cx_mat& motifFT){
arma::cx_vec firstCoefArray = arma::conj(coefsK1) % coefsK3;
arma::cx_vec secondCoefArray = arma::conj(coefsK2) % coefsK4;
/* Old implementation; one below should be faster */
// std::complex<double> term = arma::dot(firstCoefArray, extendMotifFT(motifFT) * secondCoefArray);
/* Instead of extending the motifFT matrix, reduce the coefs vectors */
arma::cx_vec reducedFirstCoefArray = arma::zeros<arma::cx_vec>(system->natoms);
arma::cx_vec reducedSecondCoefArray = arma::zeros<arma::cx_vec>(system->natoms);
int iterator = 0;
for(unsigned int atom_index = 0; atom_index < system->motif.n_rows; atom_index++){
int norbitals = system->orbitals(system->motif.row(atom_index)(3));
reducedFirstCoefArray(atom_index) = arma::sum(firstCoefArray.subvec(iterator, iterator + norbitals - 1));
reducedSecondCoefArray(atom_index) = arma::sum(secondCoefArray.subvec(iterator, iterator + norbitals - 1));
iterator += norbitals;
}
std::complex<double> term = arma::dot(reducedFirstCoefArray, motifFT * reducedSecondCoefArray);
return term;
};
/**
* Reciprocal space implementation of interaction term, valid for both direct and exchange.
* @param coefsK Vector of eigenstate |v,k>.
* @param coefsK2 Vector of eigenstate |v',k'>.
* @param coefsKQ Vector of eigenstate |c,k+Q>.
* @param coefsK2Q Vector of eigenstate |c',k'+Q>.
* @param k kpoint corresponding to k.
* @param k2 kpoint corresponding to k'.
* @param kQ kpoint corresponding to k + Q.
* @param k2Q kpoint corresponding to k' + Q.
* @return Interaction term.
*/
std::complex<double> ExcitonTB::reciprocalInteractionTerm(const arma::cx_vec& coefsK,
const arma::cx_vec& coefsK2,
const arma::cx_vec& coefsKQ,
const arma::cx_vec& coefsK2Q,
const arma::rowvec& k,
const arma::rowvec& k2,
const arma::rowvec& kQ,
const arma::rowvec& k2Q,
int nrcells){
std::complex<double> Ic, Iv;
std::complex<double> term = 0;
double radius = cutoff * arma::norm(system->reciprocalLattice.row(0));
arma::mat reciprocalVectors = system_->truncateReciprocalSupercell(nrcells, radius);
for(int i = 0; i < reciprocalVectors.n_rows; i++){
auto G = reciprocalVectors.row(i);
Ic = blochCoherenceFactor(coefsKQ, coefsK2Q, kQ, k2Q, G);
Iv = blochCoherenceFactor(coefsK, coefsK2, k, k2, G);
// must change this to include Q2DRK...
term += Ic*conj(Iv)*keldyshFT(k - k2 + G);
}
return term;
};
/**
* Calculation of Bloch coherence factors, required to compute the interaction terms in reciprocal space.
* @param coefs1 Vector of eigenstate |n,k>.
* @param coefs2 Vector of eigenstate |n',k'>.
* @param k1 kpoint k.
* @param k2 kpoint k'.
* @param G Reciprocal lattice vector used to compute the coherence factor.
* @return Bloch coherence factor I evaluated at G for states |nk>, |n'k'>.
*/
std::complex<double> ExcitonTB::blochCoherenceFactor(const arma::cx_vec& coefs1, const arma::cx_vec& coefs2,
const arma::rowvec& k1, const arma::rowvec& k2,
const arma::rowvec& G){
std::complex<double> imag(0, 1);
arma::cx_vec coefs = arma::conj(coefs1) % coefs2;
arma::cx_vec phases = arma::ones<arma::cx_vec>(system->basisdim);
int index_min = 0;
int index_max = -1;
for(int i = 0; i < system->natoms; i++){
int species = system->motif.row(i)(3);
arma::rowvec atomPosition = system->motif.row(i).subvec(0, 2);
index_max += system->orbitals(species);
phases.subvec(index_min, index_max) *= exp(imag*arma::dot(k1 - k2 + G, atomPosition));
index_min = index_max + 1;
}
std::complex<double> factor = arma::dot(coefs, phases);
return factor;
}
/*------------------------------------ Electron-hole pair basis ------------------------------------*/
/**
* Method to generate a basis which is a subset of the basis considered for the
* exciton. Its main purpose is to allow computation of Fermi golden rule between
* two specified subbasis.
* @param bands Band subset of the originally specified for the exciton.
* @return Matrix with the states corresponding to the specified subset.
*/
arma::imat ExcitonTB::specifyBasisSubset(const arma::ivec& bands){
// Check if given bands vector corresponds to subset of bands
try{
for (const auto& band : bands){
for (const auto& reference_band : bandList){
if ((band + system->fermiLevel - reference_band) == 0) {
continue;
}
}
throw "Error: Given band list must be a subset of the exciton one";
};
}
catch (std::string e){
std::cerr << e;
};
int reducedBasisDim = system->nk*bands.n_elem;
std::vector<arma::s64> valence, conduction;
for(int i = 0; i < bands.n_elem; i++){
if (bands(i) <= 0){
valence.push_back(bands(i) + system->fermiLevel);
}
else{
conduction.push_back(bands(i) + system->fermiLevel);
}
}
arma::ivec valenceBands = arma::ivec(valence);
arma::ivec conductionBands = arma::ivec(conduction);
arma::imat states = createBasis(conductionBands, valenceBands);
return states;
}
/**
* Criterium to fix the phase of the single-particle eigenstates after diagonalization.
* @details The prescription we take here is to impose that the sum of all the coefficients is real.
* @return Fixed coefficients.
*/
arma::cx_mat ExcitonTB::fixGlobalPhase(arma::cx_mat& coefs){
arma::cx_rowvec sums = arma::sum(coefs);
std::complex<double> imag(0, 1);
for(int j = 0; j < sums.n_elem; j++){
double phase = arg(sums(j));
coefs.col(j) *= exp(-imag*phase);
}
return coefs;
}
/*------------------------------------ Initializers ------------------------------------*/
/**
* Method to initialize the motif Fourier transform for all possible motif combination
* at a given kpoint.
* @param i Index of kpoint.
* @param cells Matrix of unit cells over which the motif FT is computed.
* @param potential Pointer to potential function.
* @return void
*/
void ExcitonTB::initializeMotifFT(int i, const arma::mat& cells, potptr potential){
ftMotifStack.slice(i) = motifFTMatrix(system->meshBZ.row(i), cells, potential);
}
/**
* Main method to compute all the relevant single-particle quantities (bands, eigenstates and fourier transforms),
* to compute the Bethe-Salpeter equation.
* @details It precomputes and saves the relevant data in the heap for later computations.
* @param triangular Boolean to specify whether the Hamiltonian matrices are triangular (default = false).
* @return void
*/
void ExcitonTB::initializeResultsH0(){
int nTotalBands = bandList.n_elem;
double radius = arma::norm(system->bravaisLattice.row(0)) * cutoff_;
arma::mat cells = system_->truncateSupercell(ncell, radius);
int nk = system->nk;
int natoms = system->natoms;
int basisdim = system->basisdim;
this->eigvecKStack_ = arma::cx_cube(basisdim, nTotalBands, nk);
this->eigvecKQStack_ = arma::cx_cube(basisdim, nTotalBands, nk);
this->eigvalKStack_ = arma::mat(nTotalBands, nk);
this->eigvalKQStack_ = arma::mat(nTotalBands, nk);
this->ftMotifStack = arma::cx_cube(natoms, natoms, system->meshBZ.n_rows);
this->ftMotifQ = arma::cx_mat(natoms, natoms);
vec auxEigVal(basisdim);
arma::cx_mat auxEigvec(basisdim, basisdim);
arma::cx_mat h;
// Progress bar variables
int step = 1;
int displayNext = step;
int percent = 0;
system_->calculateInverseReciprocalMatrix();
std::complex<double> imag(0, 1);
std::cout << "Diagonalizing H0 for all k points... " << std::flush;
for (int i = 0; i < nk; i++){
arma::rowvec k = system->kpoints.row(i);
system->solveBands(k, auxEigVal, auxEigvec);
auxEigvec = fixGlobalPhase(auxEigvec);
eigvalKStack_.col(i) = auxEigVal(bandList);
eigvecKStack_.slice(i) = auxEigvec.cols(bandList);
if(arma::norm(Q) != 0){
arma::rowvec kQ = system->kpoints.row(i) + Q;
system->solveBands(kQ, auxEigVal, auxEigvec);
auxEigvec = fixGlobalPhase(auxEigvec);
eigvalKQStack_.col(i) = auxEigVal(bandList);
eigvecKQStack_.slice(i) = auxEigvec.cols(bandList);
}
else{
eigvecKQStack_.slice(i) = eigvecKStack.slice(i);
eigvalKQStack_.col(i) = eigvalKStack.col(i);
};
};
std::cout << "Done" << std::endl;
if(this->mode == "realspace"){
std::cout << "Computing lattice Fourier transform... " << std::flush;
potptr directPotential = selectPotential(this->potential_);
#pragma omp parallel for
for (unsigned int i = 0; i < system->meshBZ.n_rows; i++){
// BIGGEST BOTTLENECK OF THE CODE
initializeMotifFT(i, cells, directPotential);
/* AJU 24-11-23: Progress bar does not work properly with parallel for
Fix it or remove it direcly? */
// percent = (100 * (i + 1)) / meshBZ.n_rows ;
// if (percent >= displayNext){
// cout << "\r" << "[" << std::string(percent / 5, '|') << std::string(100 / 5 - percent / 5, ' ') << "]";
// cout << percent << "%";
// std::cout.flush();
// displayNext += step;
// }
}
std::cout << "Done\n" << std::endl;
}
if(this->exchange){
potptr exchangePotential = selectPotential(this->exchangePotential_);
this->ftMotifQ = motifFTMatrix(this->Q, cells, exchangePotential);
}
};
/**
* Routine to initialize the required variables to construct the Bethe-Salpeter Hamiltonian.
* @param triangular Boolean to specify whether the single-particle Hamiltonian matrices are triangular.
* @return void.
*/
void ExcitonTB::initializeHamiltonian(){
if(bands.empty()){
throw std::invalid_argument("Error: Exciton object must have some bands");
}
if(system->nk == 0){
throw std::invalid_argument("Error: BZ mesh must be initialized first");
}
if (this->regularization_ < 1E-10){
regularization_ = system->a;
}
this->excitonbasisdim_ = system->nk*valenceBands.n_elem*conductionBands.n_elem;
this->totalCells_ = pow(ncell*system->factor, system->ndim);
std::cout << "Initializing basis for BSE... " << std::flush;
initializeBasis();
generateBandDictionary();
initializeResultsH0();
}
/**
* Method to initialize the BSE.
* @details Calls the more general routine which allows
* to specify a subset of the complete basis.
*/
void ExcitonTB::BShamiltonian(){
arma::imat basis = {};
BShamiltonian(basis);
}
/**
* Initialize BSE hamiltonian matrix and kinetic matrix.
* @details Instead of calculating the energies and coeficients dinamically, which
* is too expensive, instead we first calculate those for each k, save them
* in the heap, and then call them consecutively as we build the matrix.
* Analogously, we calculate the Fourier transform of the potential beforehand,
* saving it in the stack so that it can be later called in the matrix element
* calculation.
* Also note that this routine involves a omp parallelization when building the matrix.
* @param basis Subset of the exciton basis to build the BSE. If none, defaults to
* the complete or original basis.
* @return void
*/
void ExcitonTB::BShamiltonian(const arma::imat& basis){
arma::imat basisStates = this->basisStates;
if (!basis.is_empty()){
basisStates = basis;
};
uint64_t basisDimBSE = basisStates.n_rows;
std::cout << "BSE dimension: " << basisDimBSE << std::endl;
std::cout << "Initializing Bethe-Salpeter matrix... " << std::flush;
HBS_ = arma::zeros<cx_mat>(basisDimBSE, basisDimBSE);
// To be able to parallelize over the triangular matrix, we build
uint64_t loopLength = basisDimBSE*(basisDimBSE + 1)/2.;
// https://stackoverflow.com/questions/242711/algorithm-for-index-numbers-of-triangular-matrix-coefficients
#pragma omp parallel for
for(uint64_t n = 0; n < loopLength; n++){
arma::cx_vec coefsK, coefsK2, coefsKQ, coefsK2Q;
uint64_t ii = loopLength - 1 - n;
uint64_t m = floor((sqrt(8*ii + 1) - 1)/2);
uint64_t i = basisDimBSE - 1 - m;
uint64_t j = basisDimBSE - 1 - ii + m*(m+1)/2;
uint32_t k_index = basisStates(i, 2);
int v = bandToIndex[basisStates(i, 0)];
int c = bandToIndex[basisStates(i, 1)];
uint32_t kQ_index = k_index;
uint32_t k2_index = basisStates(j, 2);
int v2 = bandToIndex[basisStates(j, 0)];
int c2 = bandToIndex[basisStates(j, 1)];
uint32_t k2Q_index = k2_index;
// Using the atomic gauge
if(gauge == "atomic"){
coefsK = system_->latticeToAtomicGauge(eigvecKStack.slice(k_index).col(v), system->kpoints.row(k_index));
coefsKQ = system_->latticeToAtomicGauge(eigvecKQStack.slice(kQ_index).col(c), system->kpoints.row(kQ_index));
coefsK2 = system_->latticeToAtomicGauge(eigvecKStack.slice(k2_index).col(v2), system->kpoints.row(k2_index));
coefsK2Q = system_->latticeToAtomicGauge(eigvecKQStack.slice(k2Q_index).col(c2), system->kpoints.row(k2Q_index));
}
else{
coefsK = eigvecKStack.slice(k_index).col(v);
coefsKQ = eigvecKQStack.slice(kQ_index).col(c);
coefsK2 = eigvecKStack.slice(k2_index).col(v2);
coefsK2Q = eigvecKQStack.slice(k2Q_index).col(c2);
}
std::complex<double> D, X = 0.0;
if (mode == "realspace"){
uint32_t effective_k_index = system_->findEquivalentPointBZ(system->kpoints.row(k2_index) - system->kpoints.row(k_index), ncell);
arma::cx_mat motifFT = ftMotifStack.slice(effective_k_index);
D = realSpaceInteractionTerm(coefsKQ, coefsK2, coefsK2Q, coefsK, motifFT);
if(this->exchange){
X = realSpaceInteractionTerm(coefsKQ, coefsK2, coefsK, coefsK2Q, this->ftMotifQ);
}
}
else if (mode == "reciprocalspace"){
arma::rowvec k = system->kpoints.row(k_index);
arma::rowvec k2 = system->kpoints.row(k2_index);
D = reciprocalInteractionTerm(coefsK, coefsK2, coefsKQ, coefsK2Q, k, k2, k, k2, this->nReciprocalVectors);
if(this->exchange){
X = reciprocalInteractionTerm(coefsK2Q, coefsK2, coefsKQ, coefsK, k2 + Q, k2, k + Q, k, this->nReciprocalVectors);
}
}
if (i == j){
HBS_(i, j) = (this->scissor +
eigvalKQStack.col(kQ_index)(c) - eigvalKStack.col(k_index)(v))/2.
- (D - X)/2.;
}
else{
HBS_(i, j) = - (D - X);
};
}
HBS_ = HBS + HBS.t();
std::cout << "Done" << std::endl;
};
/**
* Routine to diagonalize the BSE and return a Result object.
* @param method Method to diagonalize the BSE, either 'diag' (standard diagonalization)
* 'davidson' (iterative diagonalization) or 'sparse' (Lanczos).
* @param nstates Number of states to be stored from the diagonalization.
* @return Result object storing the exciton energies and states.
*/
ResultTB* ExcitonTB::diagonalizeRaw(std::string method, int nstates){
if (HBS.empty() || HBS.is_zero()){
throw std::invalid_argument("diagonalizeRaw(): BSE Hamiltonian is not initialized.");
}
std::cout << "Solving BSE with ";
arma::vec eigval;
arma::cx_mat eigvec;
if (method == "diag"){
std::cout << "exact diagonalization... " << std::flush;
arma::eig_sym(eigval, eigvec, HBS);
}
else if (method == "davidson"){
std::cout << "Davidson method... " << std::flush;
davidson_method(eigval, eigvec, HBS, nstates);
}
else if (method == "sparse"){
std::cout << "Lanczos method..." << std::flush;
arma::cx_vec cx_eigval;
arma::eigs_gen(cx_eigval, eigvec, arma::sp_cx_mat(HBS), nstates, "sr");