-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhamsparse2D.cpp
256 lines (203 loc) · 5.97 KB
/
hamsparse2D.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
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
/* 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 "hamsparse2D.h"
/**
* Constructor of the SparseHamiltonian2D class
* @param L The Length of the 2D grid
* @param D The depth of the 2D grid
* @param Nu Number of Up Electrons
* @param Nd Number of Down Electrons
* @param J The hopping strengh
* @param U The onsite interaction strength
*/
SparseHamiltonian2D::SparseHamiltonian2D(int L,int D, int Nu, int Nd, double J, double U)
: SparseHamiltonian2D_CSR(L,D,Nu,Nd,J,U)
{
Up_data = 0;
Down_data = 0;
Up_ind = 0;
Down_ind = 0;
size_Up = 0;
size_Down = 0;
}
/**
* Destructor of the SparseHamiltonian2D class
*/
SparseHamiltonian2D::~SparseHamiltonian2D()
{
if(Up_data)
delete [] Up_data;
if(Down_data)
delete [] Down_data;
if(Up_ind)
delete [] Up_ind;
if(Down_ind)
delete [] Down_ind;
}
/**
* Builds and fills the sparse hamiltonian
*/
void SparseHamiltonian2D::BuildSparseHam()
{
SparseHamiltonian2D_CSR::BuildSparseHam();
unsigned int NumUp = baseUp.size();
unsigned int NumDown = baseDown.size();
unsigned int max = 0;
for(unsigned int i=0;i<NumUp;i++)
if( (Up_row[i+1]-Up_row[i]) > max )
max = Up_row[i+1]-Up_row[i];
size_Up = max;
max = 0;
for(unsigned int i=0;i<NumDown;i++)
if( (Down_row[i+1]-Down_row[i]) > max )
max = Down_row[i+1]-Down_row[i];
size_Down = max;
Up_data = new double [NumUp*size_Up];
Up_ind = new unsigned int [NumUp*size_Up];
Down_data = new double [NumDown*size_Down];
Down_ind = new unsigned int [NumDown*size_Down];
std::cout << "Sizes: " << size_Up << "\t" << size_Down << std::endl;
for(unsigned int a=0;a<NumUp;a++)
{
int count = 0;
for(unsigned int b=Up_row[a];b<Up_row[a+1];b++)
{
Up_data[a+count*NumUp] = Up_data_CSR[b];
Up_ind[a+count*NumUp] = Up_col[b];
count++;
}
if(count < size_Up)
for(int i=count;i<size_Up;i++)
{
Up_data[a+i*NumUp] = 0;
Up_ind[a+i*NumUp] = 0;
}
else if(count > size_Up)
std::cerr << "Something went terribly wrong!" << std::endl;
}
for(unsigned int a=0;a<NumDown;a++)
{
int count = 0;
for(unsigned int b=Down_row[a];b<Down_row[a+1];b++)
{
Down_data[a+count*NumDown] = Down_data_CSR[b];
Down_ind[a+count*NumDown] = Down_col[b];
count++;
}
if(count < size_Down)
for(int i=count;i<size_Down;i++)
{
Down_data[a+i*NumDown] = 0;
Down_ind[a+i*NumDown] = 0;
}
else if(count > size_Down)
std::cerr << "Something went terribly wrong!" << std::endl;
}
}
/**
* Prints the Sparse Hamiltonian
*/
void SparseHamiltonian2D::PrintSparse() const
{
unsigned int NumUp = baseUp.size();
unsigned int NumDown = baseDown.size();
std::cout << "Up:" << std::endl;
for(unsigned int i=0;i<NumUp;i++)
{
int count = 0;
for(unsigned int j=0;j<NumUp;j++)
if(count < size_Up && Up_ind[i+count*NumUp] == j)
std::cout << Up_data[i + NumUp*count++] << "\t";
else
std::cout << "0\t";
std::cout << std::endl;
}
std::cout << "Down:" << std::endl;
for(unsigned int i=0;i<NumDown;i++)
{
int count = 0;
for(unsigned int j=0;j<NumDown;j++)
if(count < size_Down && Down_ind[i + count*NumDown] == j)
std::cout << Down_data[i + NumDown*count++] << "\t";
else
std::cout << "0\t";
std::cout << std::endl;
}
}
void SparseHamiltonian2D::PrintRawELL() const
{
unsigned int NumUp = baseUp.size();
unsigned int NumDown = baseDown.size();
std::cout << "Up:" << std::endl;
std::cout << "Data:" << std::endl;
for(unsigned int i=0;i<NumUp;i++)
{
for(int j=0;j<size_Up;j++)
std::cout << Up_data[i+j*NumUp] << "\t";
std::cout << std::endl;
}
std::cout << "Indices:" << std::endl;
for(unsigned int i=0;i<NumUp;i++)
{
for(int j=0;j<size_Up;j++)
std::cout << Up_ind[i+j*NumUp] << "\t";
std::cout << std::endl;
}
std::cout << "Down:" << std::endl;
std::cout << "Data:" << std::endl;
for(unsigned int i=0;i<NumDown;i++)
{
for(int j=0;j<size_Down;j++)
std::cout << Down_data[i+j*NumDown] << "\t";
std::cout << std::endl;
}
std::cout << "Indices:" << std::endl;
for(unsigned int i=0;i<NumDown;i++)
{
for(int j=0;j<size_Down;j++)
std::cout << Down_ind[i+j*NumDown] << "\t";
std::cout << std::endl;
}
}
/**
* Matrix vector product with (sparse) hamiltonian: y = ham*x + alpha*y
* @param x the x vector
* @param y the y vector
* @param alpha the scaling value
*/
void SparseHamiltonian2D::mvprod(double *x, double *y, double alpha) const
{
int NumUp = baseUp.size();
int NumDown = baseDown.size();
int incx = 1;
for(int i=0;i<NumUp;i++)
{
#pragma omp parallel for
for(int k=0;k<NumDown;k++)
{
// the interaction part
y[i*NumDown+k] = alpha*y[i*NumDown+k] + U * CountBits(baseUp[i] & baseDown[k]) * x[i*NumDown+k];
// the Hop_down part
for(int l=0;l<size_Down;l++)
y[i*NumDown+k] += Down_data[k+l*NumDown] * x[i*NumDown + Down_ind[k+l*NumDown]];
}
// the Hop_Up part
for(int l=0;l<size_Up;l++)
daxpy_(&NumDown,&Up_data[i+l*NumUp],&x[Up_ind[i+l*NumUp]*NumDown],&incx,&y[i*NumDown],&incx);
}
}
/* vim: set ts=8 sw=4 tw=0 expandtab :*/