-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGenHam.cpp
More file actions
484 lines (397 loc) · 12.7 KB
/
GenHam.cpp
File metadata and controls
484 lines (397 loc) · 12.7 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
#include "GenHam.h"
//#define SPIN_INV
//----------------------------------------------------------
GENHAM::GENHAM(const int Ns, const h_float J_, const h_float J2_, const h_float Q_, const int Sz)
: JJ(J_), J2(J2_), QQ(Q_)
//create bases and determine dim of full Hilbert space
{
int Dim;
Nsite = Ns;
Dim = 2; // S=1/2 models : two states
for (int ch=1; ch<Nsite; ch++) Dim *=2;
Fdim = Dim;
BasPos.resize(Dim,-1); //initialization
Vdim=0;
unsigned long temp; //create basis (16 site cluster)
//for spin iversion symmetry
//-------------------------
SpinInv = 0; //default: comment below to shut off
#ifdef SPIN_INV
if (Sz == 0) {//set Spin Inversion symmetry on
SpinInv = ~ (Dim-1); //This sets this integer to the complement of the 0 bits
//cout<<SpinInv<<endl;
}
if (SpinInv != 0) Dim /= 2; //use only first half of Hilbert space
#endif
//-------------------------
for (unsigned long i1=0; i1<Dim; i1++)
{
temp = 0;
for (int sp =0; sp<Nsite; sp++)
temp += (i1>>sp)&1; //unpack bra
if (temp==(Nsite/2+Sz) ){
Basis.push_back(i1);
BasPos.at(i1)=Basis.size()-1;
Vdim++;
}
}//Dim
cout<<"Vdim "<<Vdim<<" "<<Dim<<endl;
}//constructor
//----------------------------------------------------------
void GENHAM::printg()
{
int i,j;
vector<int> tempP;
vector<h_float> tempV;
for (i=0; i<PosHam.size(); i++){
//cout<<PosHam[i][0]<<" * ";
cout<<i+1<<" * ";
for (j=0; j<=PosHam[i][0]; j++){
cout<<"("<<PosHam[i][j]+1<<","<<ValHam[i][j]<<") ";
}
cout<<endl;
}
}//print
//----------------------------------------------------------
void GENHAM::FullHamJQ(){
int ii, tempI;
vector<long> revBas(Fdim,-1);
for (ii=0; ii<Basis.size(); ii++) { //reverse lookup
tempI = Basis.at(ii);
revBas.at(tempI) = ii;
}
Ham.resize(Vdim,Vdim);
Ham = 0;
unsigned long tempi, tempj, tempod;
double tempD;
int si,sj,sk,sl, revPos;
for (ii=0; ii<Basis.size(); ii++){
tempi = Basis.at(ii);
//Hamiltonian for diagonal
tempD = (*this).HdiagPart(tempi);
Ham(ii,ii) = tempD;
for (int T0=0; T0<Nsite; T0++){ // Now generate off-diagonal part
si = PlaqX(T0,0); //si = Bond(T0,0);
//if (si != T0) cout<<"Square error \n";
// X Bond
tempod = tempi;
sj = PlaqX(T0,1); //sj = Bond(T0,1);
tempod ^= (1<<si); //toggle bit
tempod ^= (1<<sj); //toggle bit
revPos = revBas.at(tempod);
if (revPos != -1){
tempD = (*this).HOFFdBondX(T0,tempi);
Ham(ii,revPos) = tempD;
}
// Y Bond
tempod = tempi;
sj = PlaqX(T0,3); //sj = Bond(T0,2);
tempod ^= (1<<si); //toggle bit
tempod ^= (1<<sj); //toggle bit
revPos = revBas.at(tempod);
if (revPos != -1){
tempD = (*this).HOFFdBondY(T0,tempi);
Ham(ii,revPos) = tempD;
}
//Next-nearest neighbor bonds: J2
//bond 0,2
si = PlaqX(T0,0);
tempod = tempi;
sj = PlaqX(T0,2); //sj = Bond(T0,1);
tempod ^= (1<<si); //toggle bit
tempod ^= (1<<sj); //toggle bit
revPos = revBas.at(tempod);
if (revPos != -1){
tempD = (*this).HOFFdBond_02(T0,tempi);
Ham(ii,revPos) = tempD;
}
//bond 1,3
si = PlaqX(T0,1);
tempod = tempi;
sj = PlaqX(T0,3); //sj = Bond(T0,1);
tempod ^= (1<<si); //toggle bit
tempod ^= (1<<sj); //toggle bit
revPos = revBas.at(tempod);
if (revPos != -1){
tempD = (*this).HOFFdBond_13(T0,tempi);
Ham(ii,revPos) = tempD;
}
// Square Plaquette
tempod = tempi;
si = PlaqX(T0,0);
sj = PlaqX(T0,1);
sk = PlaqX(T0,2);
sl = PlaqX(T0,3);
tempod ^= (1<<si); //toggle bits
tempod ^= (1<<sj);
tempod ^= (1<<sk);
tempod ^= (1<<sl);
revPos = revBas.at(tempod);
if (revPos != -1){
tempD = (*this).HOFFdPlaq(T0,tempi);
Ham(ii,revPos) = tempD;
}
}//si
}//ii
}//FullHamHeis
//----------------------------------------------------------
void GENHAM::SparseHamJQ()
{
int ii, jj;
int Rsize;
vector<long> tempBas;
//vector<long> tempBas2;
vector<h_float> tempH;
unsigned long tempi, tempj, tempod;
int si, sj,sk,sl;
double tempD;
for (ii=0; ii<Basis.size(); ii++){
tempH.clear();
tempBas.clear();
tempi = Basis.at(ii);
tempBas.push_back(0); //first element (Row size)
tempH.push_back(0); //make base 0
//-----1: diagonal
tempBas.push_back(BasPos.at(tempi));
tempD = (*this).HdiagPart(tempi);
tempH.push_back(tempD);
for (int T0=0; T0<Nsite; T0++){ //T0 is your square index
si = PlaqX(T0,0); //si = Bond(T0,0); //the lower left bond spin is not always T0
//if (si != T0) cout<<"Square error 2\n";
//-----2: first bond (Horizontal)
tempod = tempi;
sj = PlaqX(T0,1); //sj = Bond(T0,1);
tempod ^= (1<<si); //toggle bit
tempod ^= (1<<sj); //toggle bit
//spin inversion symmmetry ---
if ( ( SpinInv !=0) && (si == (Nsite-1) || sj == (Nsite-1 ) ) ) {
tempod = ~tempod;
tempod -= SpinInv;
} //--------------------------
if (BasPos.at(tempod) != -1 && BasPos.at(tempod) > ii){ //build only upper half of matrix
tempBas.push_back(BasPos.at(tempod));
tempD = (*this).HOFFdBondX(T0,tempi);
tempH.push_back(tempD);
}
//-----3: second bond (Vertical)
tempod = tempi;
sj = PlaqX(T0,3); //sj = Bond(T0,2);
tempod ^= (1<<si); //toggle bit
tempod ^= (1<<sj); //toggle bit
//spin inversion symmmetry ---
if ( ( SpinInv !=0) && (si == (Nsite-1) || sj == (Nsite-1 ) ) ) {
tempod = ~tempod;
tempod -= SpinInv;
} //--------------------------
if (BasPos.at(tempod) != -1 && BasPos.at(tempod) > ii){
tempBas.push_back(BasPos.at(tempod));
tempD = (*this).HOFFdBondY(T0,tempi);
tempH.push_back(tempD);
}
//Next-nearest neighbor bonds: J2
//bond 0,2
si = PlaqX(T0,0);
tempod = tempi;
sj = PlaqX(T0,2); //sj = Bond(T0,1);
tempod ^= (1<<si); //toggle bit
tempod ^= (1<<sj); //toggle bit
//spin inversion symmmetry ---
if ( ( SpinInv !=0) && (si == (Nsite-1) || sj == (Nsite-1 ) ) ) {
tempod = ~tempod;
tempod -= SpinInv;
} //--------------------------
if (BasPos.at(tempod) != -1 && BasPos.at(tempod) > ii){
tempBas.push_back(BasPos.at(tempod));
tempD = (*this).HOFFdBond_02(T0,tempi);
tempH.push_back(tempD);
}
//bond 1,3
si = PlaqX(T0,1);
tempod = tempi;
sj = PlaqX(T0,3); //sj = Bond(T0,1);
tempod ^= (1<<si); //toggle bit
tempod ^= (1<<sj); //toggle bit
//spin inversion symmmetry ---
if ( ( SpinInv !=0) && (si == (Nsite-1) || sj == (Nsite-1 ) ) ) {
tempod = ~tempod;
tempod -= SpinInv;
} //--------------------------
if (BasPos.at(tempod) != -1 && BasPos.at(tempod) > ii){
tempBas.push_back(BasPos.at(tempod));
tempD = (*this).HOFFdBond_13(T0,tempi);
tempH.push_back(tempD);
}
//-----4: plaquette
tempod = tempi;
si = PlaqX(T0,0); //not always redundant here
sj = PlaqX(T0,1);
sk = PlaqX(T0,2);
sl = PlaqX(T0,3);
tempod ^= (1<<si); //toggle bits
tempod ^= (1<<sj);
tempod ^= (1<<sk);
tempod ^= (1<<sl);
//spin inversion symmetry ----
if ( ( SpinInv !=0) && (si == (Nsite-1) || sj == (Nsite-1 ) ||
sk == (Nsite-1) || sl == (Nsite-1 ) ) ) {
tempod = ~tempod;
tempod -= SpinInv;
} //--------------------------
if (BasPos.at(tempod) != -1 && BasPos.at(tempod) > ii){
tempBas.push_back(BasPos.at(tempod));
tempD = (*this).HOFFdPlaq(T0,tempi);
tempH.push_back(tempD);
}
}//si
tempBas.at(0) = tempBas.size()-1;
//cout<<tempBas.at(0)<<" "<<tempBas.size()<<" "<<tempH.size()<<endl;
//bubble sort (slow)
long stemp;
bool noswap = false;
while (noswap == false){
noswap = true;
for (int i2=1; i2<tempBas.size()-1; i2++){ //ignore 0 element
if (tempBas.at(i2) > tempBas.at(i2+1) ) {
stemp = tempBas.at(i2);
tempBas.at(i2) = tempBas.at(i2+1);
tempBas.at(i2+1) = stemp;
tempD = tempH.at(i2);
tempH.at(i2) = tempH.at(i2+1);
tempH.at(i2+1) = tempD;
noswap = false;
}
}//i2
}//while
PosHam.push_back(tempBas);
ValHam.push_back(tempH);
}//ii
}//Heisenberg
//----------------------------------------------------------
double GENHAM::HdiagPart(const long bra){
int S0b,S1b ; //spins (bra
int T0,T1; //site
int P0, P1, P2, P3; //sites for plaquette (Q)
int s0p, s1p, s2p, s3p;
double valH = 0;
for (int Ti=0; Ti<Nsite; Ti++){
//***HEISENBERG PART
T0 = PlaqX(Ti,0); //T0 = Bond(Ti,0); //lower left spin
S0b = (bra>>T0)&1;
//if (T0 != Ti) cout<<"Square error 3\n";
T1 = PlaqX(Ti,1); //T1 = Bond(Ti,1); //first bond
S1b = (bra>>T1)&1; //unpack bra
valH += JJ*(S0b-0.5)*(S1b-0.5);
T1 = PlaqX(Ti,3); //T1 = Bond(Ti,2); //second bond
S1b = (bra>>T1)&1; //unpack bra
valH += JJ*(S0b-0.5)*(S1b-0.5);
//Next-Nearest Neighbor part
//bond 0,2
T0 = PlaqX(Ti,0);
S0b = (bra>>T0)&1;
T1 = PlaqX(Ti,2);
S1b = (bra>>T1)&1;
valH += J2*(S0b-0.5)*(S1b-0.5);
//bond 1,3
T0 = PlaqX(Ti,1);
S0b = (bra>>T0)&1;
T1 = PlaqX(Ti,3);
S1b = (bra>>T1)&1;
valH += J2*(S0b-0.5)*(S1b-0.5);
//X Plaquettes
P0 = PlaqX(Ti,0); //if (P0 != Ti) cout<<"ERROR \n";
s0p = (bra>>P0)&1;
P1 = PlaqX(Ti,1);
s1p = (bra>>P1)&1;
P2 = PlaqX(Ti,2);
s2p = (bra>>P2)&1;
P3 = PlaqX(Ti,3);
s3p = (bra>>P3)&1;
valH += QQ* ((s0p-0.5)*(s1p-0.5) - 0.25) * ((s2p-0.5)*(s3p-0.5) - 0.25);
//Y Plaquettes -rotate PlaqX
P0 = PlaqX(Ti,1);
s0p = (bra>>P0)&1;
P1 = PlaqX(Ti,2);
s1p = (bra>>P1)&1;
P2 = PlaqX(Ti,3);
s2p = (bra>>P2)&1;
P3 = PlaqX(Ti,0);
s3p = (bra>>P3)&1;
valH += QQ* ((s0p-0.5)*(s1p-0.5) - 0.25) * ((s2p-0.5)*(s3p-0.5) - 0.25);
}//T0
//cout<<bra<<" "<<valH<<endl;
return valH;
}//HdiagPart
//----------------------------------------------------------
double GENHAM::HOFFdBondX(const int si, const long bra){
double valH;
int S0, S1;
int T0, T1;
valH = JJ*0.5; //contribution from the J part of the Hamiltonian
T0 = OtherTwoX(si,0); //first other set (diagonal - Q) spin
T1 = OtherTwoX(si,1);
S0 = (bra>>T0)&1; //spin values base 0
S1 = (bra>>T1)&1;
valH += QQ*0.5*( (S0-0.5)*(S1-0.5) - 0.25);
T0 = OtherTwoX(si,2); //second other set (diagonal - Q) spin
T1 = OtherTwoX(si,3);
S0 = (bra>>T0)&1; //spin values base 0
S1 = (bra>>T1)&1;
valH += QQ*0.5*( (S0-0.5)*(S1-0.5) - 0.25);
return valH;
}//HOFFdPart
//----------------------------------------------------------
double GENHAM::HOFFdBondY(const int si, const long bra){
double valH;
int S0, S1;
int T0, T1;
valH = JJ*0.5; //contribution from the J part of the Hamiltonian
T0 = OtherTwoY(si,0); //first other set (diagonal - Q) spin
T1 = OtherTwoY(si,1);
S0 = (bra>>T0)&1; //spin values base 0
S1 = (bra>>T1)&1;
valH += QQ*0.5*( (S0-0.5)*(S1-0.5) - 0.25);
T0 = OtherTwoY(si,2); //second other set (diagonal - Q) spin
T1 = OtherTwoY(si,3);
S0 = (bra>>T0)&1; //spin values base 0
S1 = (bra>>T1)&1;
valH += QQ*0.5*( (S0-0.5)*(S1-0.5) - 0.25);
return valH;
}//HOFFdPart
//----------------------------------------------------------
double GENHAM::HOFFdBond_02(const int si, const long bra){
double valH;
valH = J2*0.5; //contribution from the J2 part of the Hamiltonian
return valH;
}//HOFFdPart
//----------------------------------------------------------
double GENHAM::HOFFdBond_13(const int si, const long bra){
double valH;
valH = J2*0.5; //contribution from the J2 part of the Hamiltonian
return valH;
}//HOFFdPart
//----------------------------------------------------------
double GENHAM::HOFFdPlaq(const int si, const long bra){
int S0, S1, S2, S3;
int T0, T1, T2, T3;
double valH=0.0;
T0 = PlaqX(si,0); //if (T0 != si) cout<<"ERROR \n";
S0 = (bra>>T0)&1;
T1 = PlaqX(si,1);
S1 = (bra>>T1)&1; //
T2 = PlaqX(si,2); // 3 2
S2 = (bra>>T2)&1; // 0 1
T3 = PlaqX(si,3); //
S3 = (bra>>T3)&1;
//X-contribution to energy
//if ( (S0 != S1) && (S2 != S3) ) valH += QQ*0.25;
if (S0 != S1) {
if (S2 == S3) cout<<"P1 error \n";
valH += QQ*0.25;
}
//Y-contribution to energy
if (S0 != S3) {
if (S2 == S1) cout<<"P2 error \n";
valH += QQ*0.25;
}
return valH;
}//HOFFdPart