-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathham.cpp
197 lines (157 loc) · 4.1 KB
/
ham.cpp
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
/* Copyright (C) 2012 Ward Poelmans
This file is part of Hubbard-GPU.
Hubbard-GPU is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Hubbard-GPU is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Hubbard-GPU. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <cstdlib>
#include <cmath>
#include "ham.h"
/**
* Constructor of the Hamiltonian class
* @param L Number of lattice sites
* @param Nu Number of Up Electrons
* @param Nd Number of Down Electrons
* @param J The hopping strengh
* @param U The onsite interaction strength
*/
Hamiltonian::Hamiltonian(int L, int Nu, int Nd, double J, double U) : BareHamiltonian(L,Nu,Nd,J,U)
{
}
/**
* Destructor of the Hamiltonian class
*/
Hamiltonian::~Hamiltonian()
{
}
/**
* Builds the full Hamiltonian matrix
*/
void Hamiltonian::BuildFullHam()
{
if( !baseUp.size() || !baseDown.size() )
{
std::cerr << "Build base before building Hamiltonian" << std::endl;
return;
}
ham = new double[dim*dim];
int NumDown = CalcDim(L,Nd);
int upjumpsign, downjumpsign;
if( Nu % 2 == 0)
upjumpsign = -1;
else
upjumpsign = 1;
if( Nd % 2 == 0)
downjumpsign = -1;
else
downjumpsign = 1;
for(unsigned int a=0;a<baseUp.size();a++)
for(unsigned int b=0;b<baseDown.size();b++)
{
int i = a * NumDown + b;
for(unsigned int c=a;c<baseUp.size();c++)
for(unsigned int d=0;d<baseDown.size();d++)
{
int j = c * NumDown + d;
ham[j+dim*i] = 0;
if(b == d)
ham[j+dim*i] += J * hopping(baseUp[a], baseUp[c],upjumpsign);
if(a == c)
ham[j+dim*i] += J * hopping(baseDown[b], baseDown[d],downjumpsign);
ham[i+dim*j] = ham[j+dim*i];
}
// count number of double occupied states
ham[i+dim*i] = U * CountBits(baseUp[a] & baseDown[b]);
}
}
/**
* private method used to see if a hopping between state a and b is
* possible and with which sign. Only for 1D Hubbard.
* @param a the bra to use
* @param b the ket to use
* @param jumpsign the sign to use when a jump over the periodic boundary occurs
* @returns matrix element of the hopping term between the ket and the bra. You still
* have to multiply this with the hopping strength J
*/
int Hamiltonian::hopping(myint a, myint b,int jumpsign) const
{
int result = 0;
int sign;
myint cur = a;
// move all electrons one site to the right
cur <<= 1;
// periodic boundary condition
if( cur & Hb )
cur ^= Hb + 0x1; // flip highest bit and lowest bit
// find places where a electron can jump into
cur &= ~a;
while(cur)
{
// isolate the rightmost 1 bit
myint hop = cur & (~cur + 1);
cur ^= hop;
sign = 1;
if(hop & 0x1)
{
hop += Hb>>1;
sign = jumpsign;
}
else
hop += hop>>1;
if( (a ^ hop) == b )
{
result -= sign;
break;
}
}
cur = a;
// move all electrons one site to the left
cur >>= 1;
// periodic boundary condition
if( a & 0x1 )
cur ^= Hb>>1; // flip highest bit
// find places where a electron can jump into
cur &= ~a;
while(cur)
{
// isolate the rightmost 1 bit
myint hop = cur & (~cur + 1);
cur ^= hop;
sign = 1;
if(hop & Hb>>1)
{
hop += 0x1;
sign = jumpsign;
}
else
hop += hop<<1;
if( (a ^ hop) == b )
{
result -= sign;
break;
}
}
return result;
}
/**
* Matrix vector product with hamiltonian: y = ham*x + alpha*y
* @param x the input vector
* @param y the output vector
* @param alpha the scaling value
*/
void Hamiltonian::mvprod(double *x, double *y, double alpha) const
{
double beta = 1;
int incx = 1;
char uplo = 'U';
dsymv_(&uplo,&dim,&beta,ham,&dim,x,&incx,&alpha,y,&incx);
}
/* vim: set ts=8 sw=4 tw=0 expandtab :*/