HubbardGPU
Hubbard diagonalisation on the GPU (and CPU)
 All Classes Files Functions Variables Typedefs Friends Macros
hamsparse2D_CSR.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2012 Ward Poelmans
2 
3 This file is part of Hubbard-GPU.
4 
5 Hubbard-GPU is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9 
10 Hubbard-GPU is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with Hubbard-GPU. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include <iostream>
20 #include <cstdlib>
21 #include <cmath>
22 #include "hamsparse2D_CSR.h"
23 
33 SparseHamiltonian2D_CSR::SparseHamiltonian2D_CSR(int L, int D, int Nu, int Nd, double J, double U)
34  : HubHam2D(L,D,Nu,Nd,J,U)
35 {
36 }
37 
42 {
43 }
44 
49 {
50  if( !baseUp.size() || !baseDown.size() )
51  {
52  std::cerr << "Build base before building Hamiltonian" << std::endl;
53  return;
54  }
55 
56  unsigned int NumUp = baseUp.size();
57  unsigned int NumDown = baseDown.size();
58 
59  // by convention the last element of the row array is nnz
60  Up_row.reserve(NumUp+1);
61  Down_row.reserve(NumDown+1);
62  Up_row.push_back(0);
63  Down_row.push_back(0);
64  int count = 0;
65 
66  for(unsigned int a=0;a<NumUp;a++)
67  {
68  for(unsigned int b=0;b<NumUp;b++)
69  {
70  int result = hopping(baseUp[a], baseUp[b]);
71 
72  if(result != 0)
73  {
74  Up_data_CSR.push_back(J*result);
75  Up_col.push_back(b);
76  count++;
77  }
78  }
79  Up_row.push_back(count);
80  }
81 
82 
83  count = 0;
84 
85  for(unsigned int a=0;a<NumDown;a++)
86  {
87  for(unsigned int b=0;b<NumDown;b++)
88  {
89  int result = hopping(baseDown[a], baseDown[b]);
90 
91  if(result != 0)
92  {
93  Down_data_CSR.push_back(J*result);
94  Down_col.push_back(b);
95  count++;
96  }
97  }
98  Down_row.push_back(count);
99  }
100 
101 }
102 
107 {
108  unsigned int NumUp = baseUp.size();
109  unsigned int NumDown = baseDown.size();
110 
111  std::cout << "Up:" << std::endl;
112 
113  unsigned int count = 0;
114 
115  for(unsigned int i=0;i<NumUp;i++)
116  {
117  for(unsigned int j=0;j<NumUp;j++)
118  if( count < Up_data_CSR.size() && Up_col[count] == j )
119  std::cout << Up_data_CSR[count++] << "\t";
120  else
121  std::cout << "0\t";
122 
123  std::cout << std::endl;
124  }
125 
126 
127  std::cout << "Down:" << std::endl;
128  count = 0;
129 
130  for(unsigned int i=0;i<NumDown;i++)
131  {
132  for(unsigned int j=0;j<NumDown;j++)
133  if( count < Down_data_CSR.size() && Down_col[count] == j )
134  std::cout << Down_data_CSR[count++] << "\t";
135  else
136  std::cout << "0\t";
137 
138  std::cout << std::endl;
139  }
140 }
141 
146 {
147  std::cout << "Up:" << std::endl;
148 
149  std::cout << "Data(" << Up_data_CSR.size() << "):" << std::endl;
150  for(unsigned int i=0;i<Up_data_CSR.size();i++)
151  std::cout << Up_data_CSR[i] << " ";
152  std::cout << std::endl;
153 
154  std::cout << "Col indices:" << std::endl;
155  for(unsigned int i=0;i<Up_col.size();i++)
156  std::cout << Up_col[i] << " ";
157  std::cout << std::endl;
158 
159  std::cout << "Row indices:" << std::endl;
160  for(unsigned int i=0;i<Up_row.size();i++)
161  std::cout << Up_row[i] << " ";
162  std::cout << std::endl;
163 
164  std::cout << "Down:" << std::endl;
165 
166  std::cout << "Data(" << Down_data_CSR.size() << "):" << std::endl;
167  for(unsigned int i=0;i<Down_data_CSR.size();i++)
168  std::cout << Down_data_CSR[i] << " ";
169  std::cout << std::endl;
170 
171  std::cout << "Col indices:" << std::endl;
172  for(unsigned int i=0;i<Down_col.size();i++)
173  std::cout << Down_col[i] << " ";
174  std::cout << std::endl;
175 
176  std::cout << "Row indices:" << std::endl;
177  for(unsigned int i=0;i<Down_row.size();i++)
178  std::cout << Down_row[i] << " ";
179  std::cout << std::endl;
180 }
181 
188 void SparseHamiltonian2D_CSR::mvprod(double *x, double *y, double alpha) const
189 {
190  int NumUp = baseUp.size();
191  int NumDown = baseDown.size();
192 
193  int incx = 1;
194 
195  for(int i=0;i<NumUp;i++)
196  {
197 #pragma omp parallel for
198  for(int k=0;k<NumDown;k++)
199  {
200  // the interaction part
201  y[i*NumDown+k] = alpha*y[i*NumDown+k] + U * CountBits(baseUp[i] & baseDown[k]) * x[i*NumDown+k];
202 
203  // the Hop_down part
204  for(unsigned int l=Down_row[k];l<Down_row[k+1];l++)
205  y[i*NumDown+k] += Down_data_CSR[l] * x[i*NumDown + Down_col[l]];
206  }
207 
208  // the Hop_Up part
209  for(unsigned int l=Up_row[i];l<Up_row[i+1];l++)
210  daxpy_(&NumDown,&Up_data_CSR[l],&x[Up_col[l]*NumDown],&incx,&y[i*NumDown],&incx);
211  }
212 }
213 
219 {
220  double *Umat = new double[getDim()];
221 
222  int NumDown = baseDown.size();
223 
224  for(int i=0;i<getDim();i++)
225  {
226  int a = i / NumDown;
227  int b = i % NumDown;
228  Umat[i] = U * CountBits(baseUp[a] & baseDown[b]);
229  }
230 
231  return Umat;
232 }
233 
234 /* vim: set ts=8 sw=4 tw=0 expandtab :*/