HPhi++  3.1.0
bitcalc.cpp
Go to the documentation of this file.
1 /* HPhi - Quantum Lattice Model Simulator */
2 /* Copyright (C) 2015 The University of Tokyo */
3 
4 /* This program is free software: you can redistribute it and/or modify */
5 /* it under the terms of the GNU General Public License as published by */
6 /* the Free Software Foundation, either version 3 of the License, or */
7 /* (at your option) any later version. */
8 
9 /* This program is distributed in the hope that it will be useful, */
10 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
11 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
12 /* GNU General Public License for more details. */
13 
14 /* You should have received a copy of the GNU General Public License */
15 /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
16 #include "bitcalc.hpp"
17 #include "wrapperMPI.hpp"
18 
46  const int Nsite,
47  long int *irght,
48  long int *ilft,
49  long int *ihfbit
50 ){
51  if(Nsite<1){
52  fprintf(stderr, "%s", "Error: Total Site Number is incorrect.\n");
53  return -1;
54  }
55  *ihfbit=1;
56  *ihfbit=(*ihfbit<<(long int)((Nsite+1)/2));
57  *irght = *ihfbit-1;
58  *ilft=1;
59  *ilft = (*ilft<<(long int)Nsite)-1;
60  *ilft= *ilft ^ *irght;
61  return 0;
62 }
63 
79  const int Nsite,
80  const int iCalcModel,
81  long int *irght,
82  long int *ilft,
83  long int *ihfbit
84  )
85 {
86  int tmpNsite=Nsite;
87  switch(iCalcModel){
88  case HubbardGC:
89  case KondoGC:
90  case HubbardNConserved:
91  case Hubbard:
92  case Kondo:
93  tmpNsite *= 2;
94  break;
95  case Spin:
96  case SpinGC:
97  break;
98  default:
99  fprintf(stderr, "Error: CalcModel %d is incorrect.\n", iCalcModel);
100  return -1;
101  }
102 
103  if(GetSplitBit(tmpNsite, irght, ilft, ihfbit)!=0){
104  return -1;
105  }
106 
107  return 0;
108 }
109 
110 
125  const int Nsite,
126  long int *ihfbit,
127  const long int *SiteToBit
128 ){
129  int isite=0;
130  long int isqrtMaxDim=1;
131  long int tmpbit=1;
132 
133  if(Nsite<1){
134  fprintf(stderr, "%s", "Error: Total Site Number is incorrect.\n");
135  return -1;
136  }
137 
138  for(isite=1; isite<=Nsite ; isite++){
139  isqrtMaxDim *= SiteToBit[isite-1];
140  }
141  isqrtMaxDim =(long int)sqrt(isqrtMaxDim);
142 
143  for(isite=1; isite<=Nsite ; isite++){
144  tmpbit *= SiteToBit[isite-1];
145  if(tmpbit >= isqrtMaxDim) break;
146  }
147  *ihfbit=tmpbit;
148  return 0;
149 }
150 
151 
166 void SplitBit(
167  const long int ibit,
168  const long int irght,
169  const long int ilft,
170  const long int ihfbit,
171  long int *isplited_Bit_right,
172  long int *isplited_Bit_left
173 )
174 {
175  *isplited_Bit_right=ibit & irght;
176  *isplited_Bit_left=ibit & ilft;
177  *isplited_Bit_left=*isplited_Bit_left/ihfbit;
178 }
179 
196  long int *_list_2_1,
197  long int *_list_2_2,
198  long int _ibit,
199  const long int _irght,
200  const long int _ilft,
201  const long int _ihfbit,
202  long int *_ioffComp
203 )
204 {
205  long int ia, ib;
206  SplitBit(_ibit, _irght, _ilft, _ihfbit, &ia, &ib);
207 /*
208  *_ioffComp =_list_2_1[ia];
209  *_ioffComp+=_list_2_2[ib];
210 */
211 
212  //if(myrank==1)
213  //printf( "DEGBUG:_ibit=%ld, _list_2_1=%ld, _list_2_2=%ld\n", _ibit, _list_2_1[ia], _list_2_2[ib]);
214 
215  if(_list_2_1[ia]*_list_2_2[ib]==0){
216  *_ioffComp=0;
217  return FALSE;
218  }
219  *_ioffComp =_list_2_1[ia]-1;
220  *_ioffComp+=_list_2_2[ib]-1;
221 
222  return TRUE;
223 }
224 
225 
244  const long int org_ibit,
245  const int org_isite,
246  const int org_ispin,
247  const int off_ispin,
248  long int *_ioffComp,
249  const long int *SiteToBit,
250  const long int *Tpow
251 )
252 {
253  if(off_ispin>SiteToBit[org_isite-1]-1 ||
254  off_ispin<0 ||
255  org_ispin>SiteToBit[org_isite-1]-1 ||
256  org_ispin <0){
257  *_ioffComp=0;
258  return FALSE;
259  }
260  if(BitCheckGeneral(org_ibit, org_isite, org_ispin, SiteToBit, Tpow) == FALSE){
261  *_ioffComp=0;
262  return FALSE;
263  }
264 
265  //delete org_ispin and create off_ispin
266  long int tmp_off=0;
267  tmp_off=(long int)(off_ispin-org_ispin);
268  tmp_off *=Tpow[org_isite-1];
269  tmp_off +=org_ibit;
270  *_ioffComp =tmp_off;
271  return TRUE;
272 }
273 
286  const long int org_ibit,
287  const long int ihlfbit,
288  long int *_ilist1Comp
289 )
290 {
291  long int ia, ib;
292  ia=org_ibit%ihlfbit;
293  ib=org_ibit/ihlfbit;
294  if(list_2_1[ia]*list_2_2[ib]==0){
295  *_ilist1Comp=0;
296  return FALSE;
297  }
298  *_ilist1Comp = list_2_1[ia] + list_2_2[ib] - 2;
299  return TRUE;
300 }
301 
313  const long int org_bit,
314  int *sgn
315 )
316 {
317  long int bit;
318 
319  bit = org_bit^(org_bit>>1);
320  bit = (bit^(bit>>2) ) & 0x11111111;
321  bit = bit*0x11111111;
322  *sgn = 1-2*((bit>>28) & 1); // sgn = pm 1
323 }
324 
325 
326 // for 64 bit
338 void SgnBit(
339  const long int org_bit,
340  int *sgn
341 )
342 {
343  long int bit;
344 
345  bit = org_bit^(org_bit>>1);
346  bit = bit^(bit>>2);
347  bit = bit^(bit>>4);
348  bit = bit^(bit>>8);
349  bit = bit^(bit>>16);
350  bit = bit^(bit>>32);
351  *sgn = 1-2*(bit & 1); // sgn = pm 1
352 }
353 
366 int BitCheck(
367  const long int org_bit,
368  const long int target_bit
369 )
370 {
371  return (org_bit >> target_bit) &1;
372  // (org_bit & (2^target_bit))/2^target_bit
373 }
374 
375 
376 
393  const long int org_bit,
394  const int org_isite,
395  const int target_ispin,
396  const long int *SiteToBit,
397  const long int *Tpow
398 )
399 {
400 
401  if(GetBitGeneral(org_isite, org_bit, SiteToBit, Tpow) !=target_ispin){
402  return FALSE;
403  }
404  return TRUE;
405 }
406 
407 
422  const int isite,
423  const long int org_bit,
424  const long int *SiteToBit,
425  const long int *Tpow
426 )
427 {
428  long int tmp_bit=(org_bit/Tpow[isite-1])%SiteToBit[isite-1] ;
429  return (tmp_bit);
430 }
431 
432 
447 int GetLocal2Sz
448 (
449  const int isite,
450  const long int org_bit,
451  const long int *SiteToBit,
452  const long int *Tpow
453  )
454 {
455  int TwiceSz=0;
456  int bitAtSite=0;
457  //get bit
458  bitAtSite=GetBitGeneral(isite, org_bit, SiteToBit, Tpow);
459  TwiceSz=-(SiteToBit[isite-1]-1)+2*bitAtSite; //-2S^{total}_i+2Sz_i
460  return TwiceSz;
461 }
473 long int snoob(long int x){
474  long int smallest, ripple, ones;
475  smallest = x &(-x);
476  ripple = x+ smallest;
477  ones = x ^ ripple;
478  ones = (ones>>2)/smallest;
479  return ripple|ones;
480 }
491 int pop(int x){
492  x = x - ((x>>1) & 0x55555555);
493  x = (x & 0x33333333)+ ((x>>2)& 0x33333333);
494  x = (x+(x>>4)) & 0x0F0F0F0F;
495  x = x+ (x>>8);
496  x = x+ (x>>16);
497  return x & 0x0000003F;
498 }
int pop(int x)
calculating number of 1-bits in x (32 bit) This method is introduced in S.H. Warren, Hacker’s Delight, second ed., Addison-Wesley, ISBN: 0321842685, 2012.
Definition: bitcalc.cpp:491
int GetSplitBitByModel(const int Nsite, const int iCalcModel, long int *irght, long int *ilft, long int *ihfbit)
function of splitting original bit into right and left spaces.
Definition: bitcalc.cpp:78
int BitCheckGeneral(const long int org_bit, const int org_isite, const int target_ispin, const long int *SiteToBit, const long int *Tpow)
bit check function for general spin
Definition: bitcalc.cpp:392
long int * list_2_1
Definition: global.cpp:27
long int * list_2_2
Definition: global.cpp:28
int GetSplitBit(const int Nsite, long int *irght, long int *ilft, long int *ihfbit)
function of getting right, left and half bits corresponding to a original Hilbert space...
Definition: bitcalc.cpp:45
int ConvertToList1GeneralSpin(const long int org_ibit, const long int ihlfbit, long int *_ilist1Comp)
function of converting component to list_1
Definition: bitcalc.cpp:285
int GetOffCompGeneralSpin(const long int org_ibit, const int org_isite, const int org_ispin, const int off_ispin, long int *_ioffComp, const long int *SiteToBit, const long int *Tpow)
function of getting off-diagonal component for general spin
Definition: bitcalc.cpp:243
int GetSplitBitForGeneralSpin(const int Nsite, long int *ihfbit, const long int *SiteToBit)
function of getting right, left and half bits corresponding to a original space.
Definition: bitcalc.cpp:124
int GetOffComp(long int *_list_2_1, long int *_list_2_2, long int _ibit, const long int _irght, const long int _ilft, const long int _ihfbit, long int *_ioffComp)
function of getting off-diagonal component
Definition: bitcalc.cpp:195
void SplitBit(const long int ibit, const long int irght, const long int ilft, const long int ihfbit, long int *isplited_Bit_right, long int *isplited_Bit_left)
function of splitting a original bit to right and left spaces
Definition: bitcalc.cpp:166
void SgnBit_old(const long int org_bit, int *sgn)
function of getting fermion signs (for 32bit)
Definition: bitcalc.cpp:312
int GetLocal2Sz(const int isite, const long int org_bit, const long int *SiteToBit, const long int *Tpow)
get 2sz at a site for general spin
Definition: bitcalc.cpp:448
long int snoob(long int x)
"finding the next higher number after a given number that has the same number of 1-bits" This method ...
Definition: bitcalc.cpp:473
int BitCheck(const long int org_bit, const long int target_bit)
bit check function
Definition: bitcalc.cpp:366
void SgnBit(const long int org_bit, int *sgn)
function of getting fermion sign (64 bit)
Definition: bitcalc.cpp:338
int GetBitGeneral(const int isite, const long int org_bit, const long int *SiteToBit, const long int *Tpow)
get bit at a site for general spin
Definition: bitcalc.cpp:421