HubbardGPU
Hubbard diagonalisation on the GPU (and CPU)
 All Classes Files Functions Variables Typedefs Friends Macros
helpers.h
Go to the documentation of this file.
1 /* Copyright (C) 2014 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 #ifndef MATRIX_H
20 #define MATRIX_H
21 
22 #include <iostream>
23 #include <memory>
24 #include <vector>
25 #include <cstring>
26 #include <assert.h>
27 #include "bare-ham.h"
28 #include "SparseMatrix_CCS.h"
29 
34 class matrix
35 {
36  public:
37  matrix();
38 
39  matrix(int n_, int m_);
40 
41  matrix(const matrix &orig);
42 
43  matrix(matrix &&orig);
44 
45  virtual ~matrix() { }
46 
47  matrix& operator=(const matrix &orig);
48 
49  matrix& operator=(double val);
50 
51  int getn() const;
52 
53  int getm() const;
54 
55  double operator()(int x,int y) const;
56 
57  double& operator()(int x,int y);
58 
59  double& operator[](int x);
60 
61  double operator[](int x) const;
62 
63  double* getpointer() const;
64 
65  matrix& prod(matrix const &A, matrix const &B);
66 
67  std::unique_ptr<double []> svd();
68 
69  void Print() const;
70 
71  private:
73  std::unique_ptr<double []> mat;
75  int n;
77  int m;
78 };
79 
85 class MomBasis
86 {
87  public:
88  MomBasis(int L, int Nu, int Nd);
89 
90  virtual ~MomBasis() { };
91 
92  myint getUp(int K, int index) const;
93 
94  myint getDown(int K, int index) const;
95 
96  int findUp(int K, myint ket) const;
97 
98  int findDown(int K, myint ket) const;
99 
100  int getdim() const;
101 
102  int getdimK(int K) const;
103 
104  void Print() const;
105 
106  void getvec(int K, int i, myint &upket, myint &downket) const;
107 
108  int getL() const;
109 
110  int getNu() const;
111 
112  int getNd() const;
113 
114  const std::pair<myint, myint>& operator()(int K, int index) const;
115 
116  std::shared_ptr<class KBlock> getBlock(int K) const;
117 
118  private:
119  void BuildBase();
120 
122  int L;
124  int Nu;
126  int Nd;
127 
129  int dim;
130 
131  std::vector< std::shared_ptr<class KBlock> > basisblocks;
132 };
133 
140 class KBlock
141 {
142  friend class MomBasis;
143 
144  public:
145  KBlock(const KBlock &);
146  KBlock(KBlock &&);
147  virtual ~KBlock() { };
148 
149  myint getUp(int index) const;
150 
151  myint getDown(int index) const;
152 
153  int getdim() const;
154 
155  int getK() const;
156 
157  int getL() const;
158 
159  const std::pair<myint,myint>& operator[](int index) const;
160 
161  void Print() const;
162 
163  private:
164  KBlock(int K, int L, int Nu, int Nd);
165 
166  int L;
167  int K;
168  int Nu;
169  int Nd;
170 
171  std::vector< std::pair<myint, myint> > basis;
172 };
173 
175 {
176  public:
177  MyDeleter(int K=-1, int Sz=-1)
178  {
179  this->K = K;
180  this->Sz = Sz;
181  }
182 
183  virtual ~MyDeleter() { }
184 
186  {
187  std::cout << "deleting matrix with K=" << K << " Sz=" << Sz;
188  std::cout << " Freeing: " << p->getn()*p->getm()*sizeof(double)*1.0/1024/1024 << " MB" << std::endl;
189  delete p;
190  }
191 
192  int K;
193  int Sz;
194 };
195 
200 class SubBasis
201 {
202  friend class SpinBasis;
203 
204  public:
205  SubBasis() { };
206  SubBasis(int K, MomBasis &orig, int dim);
207  SubBasis(const SubBasis &orig);
208  SubBasis(SubBasis &&orig);
209  virtual ~SubBasis() { };
210 
211  SubBasis& operator=(const SubBasis &orig);
212 
213  SubBasis& operator=(SubBasis &&orig);
214 
215  int getdim() const;
216 
217  int getspacedim() const;
218 
219  void Print() const;
220 
221  void Slad_min(SubBasis &orig);
222 
223  void Get(int index, myint &upket, myint &downket) const;
224 
225  std::pair<myint,myint> Get(int index) const;
226 
227  double GetCoeff(int i, int j) const;
228 
229  double& GetCoeff(int i, int j);
230 
231  void SetCoeff(int i, int j, double value);
232 
233  int getindex(myint upket, myint downket) const;
234 
235  void Normalize();
236 
237  void ToSparseMatrix();
238 
239  const SparseMatrix_CCS& getSparse() const;
240 
241  void SetCoeff(matrix &&coeffs);
242 
243  private:
245  int L;
247  int Nu;
249  int Nd;
250 
252  std::unique_ptr<class matrix, class MyDeleter> coeffs;
253 
255  std::unique_ptr<class SparseMatrix_CCS> s_coeffs;
256 
258  std::shared_ptr<class KBlock> basis;
259 };
260 
266 {
267  public:
268  BasisList(int L, int Nu, int Nd);
269 
270  virtual ~BasisList() { };
271 
272  bool Exists(int K, int S, int Sz) const;
273 
274  SubBasis& Get(int K, int S, int Sz);
275 
276  void Create(int K, int S, int Sz, MomBasis &orig, int dim);
277 
278  void Print() const;
279 
280  void DoProjection(int K, int S, int Sz, MomBasis const &orig);
281 
282  void MakeEmpty();
283 
284  void Clean(int Sz);
285 
287  static const int EMPTY;
288 
289  private:
291  int L;
293  int Nu;
295  int Nd;
296 
297  int totS;
298 
300  int Smax;
301 
303  std::vector<int> ind_list;
304 
305  std::vector<SubBasis> list;
306 };
307 
312 {
313  public:
314  SpinBasis(int L,int Nu,int Nd,BasisList &);
315  SpinBasis(const char *filename);
316  virtual ~SpinBasis() { };
317 
318  int getnumblocks() const;
319 
320  void SaveBasis(const char *filename) const;
321 
322  void ReadBasis(const char *filename);
323 
324  std::pair<int,int> getKS(int index) const;
325 
326  const SubBasis& getBlock(int index) const;
327 
328  private:
330  int L;
332  int Nu;
334  int Nd;
335 
337  std::vector<class SubBasis> basis;
338 
340  std::vector< std::pair<int,int> > ind;
341 };
342 
343 #endif /* MATRIX_H */
344 
345 /* vim: set ts=8 sw=4 tw=0 expandtab :*/