HΦ  3.2.0
bitcalc.c
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.h"
17 #include "wrapperMPI.h"
18 
46  const int Nsite,
47  long unsigned int *irght,
48  long unsigned int *ilft,
49  long unsigned int *ihfbit
50 ){
51  if(Nsite<1){
52  fprintf(stderr, "%s", cErrSiteNumber);
53  return -1;
54  }
55  *ihfbit=1;
56  *ihfbit=(*ihfbit<<(unsigned long int)((Nsite+1)/2));
57  *irght = *ihfbit-1;
58  *ilft=1;
59  *ilft = (*ilft<<(unsigned long int)Nsite)-1;
60  *ilft= *ilft ^ *irght;
61  return 0;
62 }
63 
79  const int Nsite,
80  const int iCalcModel,
81  long unsigned int *irght,
82  long unsigned int *ilft,
83  long unsigned 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, cErrNoModel, 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 unsigned 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", cErrSiteNumber);
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 unsigned int ibit,
168  const long unsigned int irght,
169  const long unsigned int ilft,
170  const long unsigned int ihfbit,
171  long unsigned int *isplited_Bit_right,
172  long unsigned 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 unsigned int *_list_2_1,
197  long unsigned int *_list_2_2,
198  long unsigned int _ibit,
199  const long unsigned int _irght,
200  const long unsigned int _ilft,
201  const long unsigned int _ihfbit,
202  long unsigned int *_ioffComp
203 )
204 {
205  long unsigned 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 unsigned int org_ibit,
245  const int org_isite,
246  const int org_ispin,
247  const int off_ispin,
248  long unsigned int *_ioffComp,
249  const long int *SiteToBit,
250  const long unsigned 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 unsigned int org_ibit,
287  const long unsigned int ihlfbit,
288  long unsigned int *_ilist1Comp
289 )
290 {
291  long unsigned int ia, ib;
292  long unsigned int tmp_list;
293  ia=org_ibit%ihlfbit;
294  ib=org_ibit/ihlfbit;
295  if(list_2_1[ia]*list_2_2[ib]==0){
296  *_ilist1Comp=0;
297  return FALSE;
298  }
299  *_ilist1Comp = list_2_1[ia] + list_2_2[ib] - 2;
300  return TRUE;
301 }
302 
314  const long unsigned int org_bit,
315  int *sgn
316 )
317 {
318  long unsigned int bit;
319 
320  bit = org_bit^(org_bit>>1);
321  bit = (bit^(bit>>2) ) & 0x11111111;
322  bit = bit*0x11111111;
323  *sgn = 1-2*((bit>>28) & 1); // sgn = pm 1
324 }
325 
326 
327 // for 64 bit
339 void SgnBit(
340  const long unsigned int org_bit,
341  int *sgn
342 )
343 {
344  long unsigned int bit;
345 
346  bit = org_bit^(org_bit>>1);
347  bit = bit^(bit>>2);
348  bit = bit^(bit>>4);
349  bit = bit^(bit>>8);
350  bit = bit^(bit>>16);
351  bit = bit^(bit>>32);
352  *sgn = 1-2*(bit & 1); // sgn = pm 1
353 }
354 
367 int BitCheck(
368  const long unsigned int org_bit,
369  const long unsigned int target_bit
370 )
371 {
372  return (org_bit >> target_bit) &1;
373  // (org_bit & (2^target_bit))/2^target_bit
374 }
375 
376 
377 
394  const long unsigned int org_bit,
395  const unsigned int org_isite,
396  const unsigned int target_ispin,
397  const long int *SiteToBit,
398  const long unsigned int *Tpow
399 )
400 {
401 
402  if(GetBitGeneral(org_isite, org_bit, SiteToBit, Tpow) !=target_ispin){
403  return FALSE;
404  }
405  return TRUE;
406 }
407 
408 
423  const unsigned int isite,
424  const long unsigned int org_bit,
425  const long int *SiteToBit,
426  const long unsigned int *Tpow
427 )
428 {
429  long unsigned int tmp_bit=(org_bit/Tpow[isite-1])%SiteToBit[isite-1] ;
430  return (tmp_bit);
431 }
432 
433 
448 int GetLocal2Sz
449 (
450  const unsigned int isite,
451  const long unsigned int org_bit,
452  const long int *SiteToBit,
453  const long unsigned int *Tpow
454  )
455 {
456  int TwiceSz=0;
457  int bitAtSite=0;
458  //get bit
459  bitAtSite=GetBitGeneral(isite, org_bit, SiteToBit, Tpow);
460  TwiceSz=-(SiteToBit[isite-1]-1)+2*bitAtSite; //-2S^{total}_i+2Sz_i
461  return TwiceSz;
462 }
474 unsigned long int snoob(unsigned long int x){
475  unsigned long int smallest, ripple, ones;
476  smallest = x &(-x);
477  ripple = x+ smallest;
478  ones = x ^ ripple;
479  ones = (ones>>2)/smallest;
480  return ripple|ones;
481 }
492 int pop(unsigned int x){
493  x = x - ((x>>1) & 0x55555555);
494  x = (x & 0x33333333)+ ((x>>2)& 0x33333333);
495  x = (x+(x>>4)) & 0x0F0F0F0F;
496  x = x+ (x>>8);
497  x = x+ (x>>16);
498  return x & 0x0000003F;
499 }
int BitCheck(const long unsigned int org_bit, const long unsigned int target_bit)
bit check function
Definition: bitcalc.c:367
int GetSplitBitForGeneralSpin(const int Nsite, long unsigned int *ihfbit, const long int *SiteToBit)
function of getting right, left and half bits corresponding to a original space.
Definition: bitcalc.c:124
int ConvertToList1GeneralSpin(const long unsigned int org_ibit, const long unsigned int ihlfbit, long unsigned int *_ilist1Comp)
function of converting component to list_1
Definition: bitcalc.c:285
void SplitBit(const long unsigned int ibit, const long unsigned int irght, const long unsigned int ilft, const long unsigned int ihfbit, long unsigned int *isplited_Bit_right, long unsigned int *isplited_Bit_left)
function of splitting a original bit to right and left spaces
Definition: bitcalc.c:166
#define TRUE
Definition: global.h:26
int GetLocal2Sz(const unsigned int isite, const long unsigned int org_bit, const long int *SiteToBit, const long unsigned int *Tpow)
get 2sz at a site for general spin
Definition: bitcalc.c:449
int pop(unsigned int x)
calculating number of 1-bits in x (32 bit) This method is introduced in S.H. Warren, Hacker$B!G(Bs Delight, second ed., Addison-Wesley, ISBN: 0321842685, 2012.
Definition: bitcalc.c:492
int GetSplitBitByModel(const int Nsite, const int iCalcModel, long unsigned int *irght, long unsigned int *ilft, long unsigned int *ihfbit)
function of splitting original bit into right and left spaces.
Definition: bitcalc.c:78
long unsigned int * list_2_1
Definition: global.h:49
int GetOffComp(long unsigned int *_list_2_1, long unsigned int *_list_2_2, long unsigned int _ibit, const long unsigned int _irght, const long unsigned int _ilft, const long unsigned int _ihfbit, long unsigned int *_ioffComp)
function of getting off-diagonal component
Definition: bitcalc.c:195
int GetBitGeneral(const unsigned int isite, const long unsigned int org_bit, const long int *SiteToBit, const long unsigned int *Tpow)
get bit at a site for general spin
Definition: bitcalc.c:422
int GetSplitBit(const int Nsite, long unsigned int *irght, long unsigned int *ilft, long unsigned int *ihfbit)
function of getting right, left and half bits corresponding to a original Hilbert space...
Definition: bitcalc.c:45
#define FALSE
Definition: global.h:25
void SgnBit_old(const long unsigned int org_bit, int *sgn)
function of getting fermion signs (for 32bit)
Definition: bitcalc.c:313
char * cErrNoModel
Error Message in diagonal calc.c.
Definition: ErrorMessage.c:99
long unsigned int * list_2_2
Definition: global.h:50
int BitCheckGeneral(const long unsigned int org_bit, const unsigned int org_isite, const unsigned int target_ispin, const long int *SiteToBit, const long unsigned int *Tpow)
bit check function for general spin
Definition: bitcalc.c:393
int GetOffCompGeneralSpin(const long unsigned int org_ibit, const int org_isite, const int org_ispin, const int off_ispin, long unsigned int *_ioffComp, const long int *SiteToBit, const long unsigned int *Tpow)
function of getting off-diagonal component for general spin
Definition: bitcalc.c:243
char * cErrSiteNumber
Error Message in bitcalc.c.
Definition: ErrorMessage.c:103
void SgnBit(const long unsigned int org_bit, int *sgn)
function of getting fermion sign (64 bit)
Definition: bitcalc.c:339
unsigned long int snoob(unsigned long int x)
"finding the next higher number after a given number that has the same number of 1-bits" This method ...
Definition: bitcalc.c:474