-
Notifications
You must be signed in to change notification settings - Fork 7
/
hamsparse2D_CSR.cpp
234 lines (190 loc) · 5.84 KB
/
hamsparse2D_CSR.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
/* 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_CSR.h"
/**
* Constructor of the SparseHamiltonian2D_CSR 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_CSR::SparseHamiltonian2D_CSR(int L, int D, int Nu, int Nd, double J, double U)
: HubHam2D(L,D,Nu,Nd,J,U)
{
}
/**
* Destructor of the SparseHamiltonian2D_CSR class
*/
SparseHamiltonian2D_CSR::~SparseHamiltonian2D_CSR()
{
}
/**
* Builds and fills the sparse hamiltonian
*/
void SparseHamiltonian2D_CSR::BuildSparseHam()
{
if( !baseUp.size() || !baseDown.size() )
{
std::cerr << "Build base before building Hamiltonian" << std::endl;
return;
}
unsigned int NumUp = baseUp.size();
unsigned int NumDown = baseDown.size();
// by convention the last element of the row array is nnz
Up_row.reserve(NumUp+1);
Down_row.reserve(NumDown+1);
Up_row.push_back(0);
Down_row.push_back(0);
int count = 0;
for(unsigned int a=0;a<NumUp;a++)
{
for(unsigned int b=0;b<NumUp;b++)
{
int result = hopping(baseUp[a], baseUp[b]);
if(result != 0)
{
Up_data_CSR.push_back(J*result);
Up_col.push_back(b);
count++;
}
}
Up_row.push_back(count);
}
count = 0;
for(unsigned int a=0;a<NumDown;a++)
{
for(unsigned int b=0;b<NumDown;b++)
{
int result = hopping(baseDown[a], baseDown[b]);
if(result != 0)
{
Down_data_CSR.push_back(J*result);
Down_col.push_back(b);
count++;
}
}
Down_row.push_back(count);
}
}
/**
* Prints the Sparse Hamiltonian
*/
void SparseHamiltonian2D_CSR::PrintSparse() const
{
unsigned int NumUp = baseUp.size();
unsigned int NumDown = baseDown.size();
std::cout << "Up:" << std::endl;
unsigned int count = 0;
for(unsigned int i=0;i<NumUp;i++)
{
for(unsigned int j=0;j<NumUp;j++)
if( count < Up_data_CSR.size() && Up_col[count] == j )
std::cout << Up_data_CSR[count++] << "\t";
else
std::cout << "0\t";
std::cout << std::endl;
}
std::cout << "Down:" << std::endl;
count = 0;
for(unsigned int i=0;i<NumDown;i++)
{
for(unsigned int j=0;j<NumDown;j++)
if( count < Down_data_CSR.size() && Down_col[count] == j )
std::cout << Down_data_CSR[count++] << "\t";
else
std::cout << "0\t";
std::cout << std::endl;
}
}
/**
* Print the raw CSR array's
*/
void SparseHamiltonian2D_CSR::PrintRawCSR() const
{
std::cout << "Up:" << std::endl;
std::cout << "Data(" << Up_data_CSR.size() << "):" << std::endl;
for(unsigned int i=0;i<Up_data_CSR.size();i++)
std::cout << Up_data_CSR[i] << " ";
std::cout << std::endl;
std::cout << "Col indices:" << std::endl;
for(unsigned int i=0;i<Up_col.size();i++)
std::cout << Up_col[i] << " ";
std::cout << std::endl;
std::cout << "Row indices:" << std::endl;
for(unsigned int i=0;i<Up_row.size();i++)
std::cout << Up_row[i] << " ";
std::cout << std::endl;
std::cout << "Down:" << std::endl;
std::cout << "Data(" << Down_data_CSR.size() << "):" << std::endl;
for(unsigned int i=0;i<Down_data_CSR.size();i++)
std::cout << Down_data_CSR[i] << " ";
std::cout << std::endl;
std::cout << "Col indices:" << std::endl;
for(unsigned int i=0;i<Down_col.size();i++)
std::cout << Down_col[i] << " ";
std::cout << std::endl;
std::cout << "Row indices:" << std::endl;
for(unsigned int i=0;i<Down_row.size();i++)
std::cout << Down_row[i] << " ";
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_CSR::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(unsigned int l=Down_row[k];l<Down_row[k+1];l++)
y[i*NumDown+k] += Down_data_CSR[l] * x[i*NumDown + Down_col[l]];
}
// the Hop_Up part
for(unsigned int l=Up_row[i];l<Up_row[i+1];l++)
daxpy_(&NumDown,&Up_data_CSR[l],&x[Up_col[l]*NumDown],&incx,&y[i*NumDown],&incx);
}
}
/**
* Builds the interaction diagonal
* @return pointer to interaction vector. You have to free this yourself
*/
double* SparseHamiltonian2D_CSR::Umatrix() const
{
double *Umat = new double[getDim()];
int NumDown = baseDown.size();
for(int i=0;i<getDim();i++)
{
int a = i / NumDown;
int b = i % NumDown;
Umat[i] = U * CountBits(baseUp[a] & baseDown[b]);
}
return Umat;
}
/* vim: set ts=8 sw=4 tw=0 expandtab :*/