HΦ  3.1.0
CalcByLanczos.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 "expec_cisajs.h"
17 #include "expec_cisajscktaltdc.h"
18 #include "expec_totalspin.h"
19 #include "CG_EigenVector.h"
20 #include "expec_energy_flct.h"
21 #include "Lanczos_EigenValue.h"
22 #include "Lanczos_EigenVector.h"
23 #include "CalcByLanczos.h"
24 #include "FileIO.h"
25 #include "wrapperMPI.h"
26 #include "CalcTime.h"
27 
55  struct EDMainCalStruct *X
56  )
57 {
58  char sdt[D_FileNameMax];
59  double diff_ene,var;
60  long int i_max=0;
61  FILE *fp;
62  size_t byte_size;
63 
65  // this part will be modified
66  switch(X->Bind.Def.iCalcModel){
67  case HubbardGC:
68  case SpinGC:
69  case KondoGC:
70  case SpinlessFermionGC:
71  initial_mode = 1; // 1 -> random initial vector
72  break;
73  case Hubbard:
74  case Kondo:
75  case Spin:
76  case SpinlessFermion:
77 
78  if(X->Bind.Def.iFlgGeneralSpin ==TRUE){
79  initial_mode=1;
80  }
81  else{
82  if(X->Bind.Def.initial_iv>0){
83  initial_mode = 0; // 0 -> only v[iv] = 1
84  }else{
85  initial_mode = 1; // 1 -> random initial vector
86  }
87  }
88  break;
89  default:
90  exitMPI(-1);
91  }
92 
93  StartTimer(4100);
94  int iret=0;
95  iret=Lanczos_EigenValue(&(X->Bind));
96  StopTimer(4100);
97  if(iret != 0) return(FALSE);
98 
99  if(X->Bind.Def.iCalcEigenVec==CALCVEC_NOT){
100  fprintf(stdoutMPI, " Lanczos EigenValue = %.10lf \n ",X->Bind.Phys.Target_energy);
101  return(TRUE);
102  }
103 
104  fprintf(stdoutMPI, "%s", cLogLanczos_EigenVecStart);
105 
106  if(X->Bind.Check.idim_maxMPI != 1){
107  StartTimer(4200);
109  StopTimer(4200);
110 
111  StartTimer(4300);
112  iret=expec_energy_flct(&(X->Bind));
113  StopTimer(4300);
114  if(iret != 0) return(FALSE);
115 
116  //check for the accuracy of the eigenvector
117  var = fabs(X->Bind.Phys.var-X->Bind.Phys.energy*X->Bind.Phys.energy)/fabs(X->Bind.Phys.var);
118  diff_ene = fabs(X->Bind.Phys.Target_CG_energy-X->Bind.Phys.energy)/fabs(X->Bind.Phys.Target_CG_energy);
119 
120  fprintf(stdoutMPI, "\n");
121  fprintf(stdoutMPI, " Accuracy check !!!\n");
122  fprintf(stdoutMPI, " LanczosEnergy = %.14e \n EnergyByVec = %.14e \n diff_ene = %.14e \n var = %.14e \n",X->Bind.Phys.Target_CG_energy,X->Bind.Phys.energy,diff_ene,var);
123  if(diff_ene < eps_Energy && var< eps_Energy){
124  fprintf(stdoutMPI, " Accuracy of Lanczos vectors is enough.\n");
125  fprintf(stdoutMPI, "\n");
126  }
127  else if(X->Bind.Def.iCalcEigenVec==CALCVEC_LANCZOSCG){
128  fprintf(stdoutMPI, " Accuracy of Lanczos vectors is NOT enough\n\n");
129  X->Bind.Def.St=1;
130  StartTimer(4400);
131  iret=CG_EigenVector(&(X->Bind));
132  StopTimer(4400);
133  if(iret != 0) return(FALSE);
134 
135  StartTimer(4300);
136  iret=expec_energy_flct(&(X->Bind));
137  StopTimer(4300);
138  if(iret != 0) return(FALSE);
139 
140  var = fabs(X->Bind.Phys.var-X->Bind.Phys.energy*X->Bind.Phys.energy)/fabs(X->Bind.Phys.var);
141  diff_ene = fabs(X->Bind.Phys.Target_CG_energy-X->Bind.Phys.energy)/fabs(X->Bind.Phys.Target_CG_energy);
142  fprintf(stdoutMPI, "\n");
143  fprintf(stdoutMPI, " CG Accuracy check !!!\n");
144  fprintf(stdoutMPI, " LanczosEnergy = %.14e\n EnergyByVec = %.14e\n diff_ene = %.14e\n var = %.14e \n ",X->Bind.Phys.Target_CG_energy,X->Bind.Phys.energy,diff_ene,var);
145  fprintf(stdoutMPI, "\n");
146  //}
147  }
148  }
149  else{//idim_max=1
150  v0[1]=1;
151  StartTimer(4300);
152  iret=expec_energy_flct(&(X->Bind));
153  StopTimer(4300);
154  if(iret != 0) return(FALSE);
155  }
156  }
157  else{// X->Bind.Def.iInputEigenVec=true :input v1:
158  fprintf(stdoutMPI, "An Eigenvector is inputted.\n");
159  StartTimer(4800);
161  StartTimer(4801);
163  childfopenALL(sdt, "rb", &fp);
164  if(fp==NULL){
165  fprintf(stderr, "Error: A file of Inputvector does not exist.\n");
166  exitMPI(-1);
167  }
168  byte_size = fread(&step_i, sizeof(int), 1, fp);
169  byte_size = fread(&i_max, sizeof(long int), 1, fp);
170  if(i_max != X->Bind.Check.idim_max){
171  fprintf(stderr, "Error: A file of Inputvector is incorrect.\n");
172  exitMPI(-1);
173  }
174  byte_size = fread(v1, sizeof(complex double),X->Bind.Check.idim_max+1, fp);
175  fclose(fp);
176  StopTimer(4801);
177  StopTimer(4800);
179  if (byte_size == 0) printf("byte_size: %d \n", (int)byte_size);
180  }
181 
182  fprintf(stdoutMPI, "%s", cLogLanczos_EigenVecEnd);
183  // v1 is eigen vector
184 
185  StartTimer(4500);
186  if(expec_cisajs(&(X->Bind), v1)!=0){
187  fprintf(stderr, "Error: calc OneBodyG.\n");
188  exitMPI(-1);
189  }
190  StopTimer(4500);
191  StartTimer(4600);
192  if(expec_cisajscktaltdc(&(X->Bind), v1)!=0){
193  fprintf(stderr, "Error: calc TwoBodyG.\n");
194  exitMPI(-1);
195  }
196  StopTimer(4600);
197 
198  if(expec_totalSz(&(X->Bind), v1)!=0){
199  fprintf(stderr, "Error: calc TotalSz.\n");
200  exitMPI(-1);
201  }
202 
203  if(X->Bind.Def.St==0){
205  }else if(X->Bind.Def.St==1){
206  sprintf(sdt, cFileNameEnergy_CG, X->Bind.Def.CDataFileHead);
207  }
208 
209  if(childfopenMPI(sdt, "w", &fp)!=0){
210  exitMPI(-1);
211  }
212 
213  fprintf(fp,"Energy %.16lf \n",X->Bind.Phys.energy);
214  fprintf(fp,"Doublon %.16lf \n",X->Bind.Phys.doublon);
215  fprintf(fp,"Sz %.16lf \n",X->Bind.Phys.Sz);
216  // fprintf(fp,"total S^2 %.10lf \n",X->Bind.Phys.s2);
217  fclose(fp);
218 
219  if(X->Bind.Def.iOutputEigenVec==TRUE){
222  if(childfopenALL(sdt, "wb", &fp)!=0){
223  exitMPI(-1);
224  }
225  fwrite(&X->Bind.Large.itr, sizeof(X->Bind.Large.itr),1,fp);
226  fwrite(&X->Bind.Check.idim_max, sizeof(X->Bind.Check.idim_max),1,fp);
227  fwrite(v1, sizeof(complex double),X->Bind.Check.idim_max+1, fp);
228  fclose(fp);
230  }
231 
232  return TRUE;
233 }
const char * cFileNameEnergy_CG
Definition: global.c:42
int childfopenALL(const char *_cPathChild, const char *_cmode, FILE **_fp)
All processes open file in output/ directory.
Definition: FileIO.c:50
void exitMPI(int errorcode)
MPI Abortation wrapper.
Definition: wrapperMPI.c:86
struct DefineList Def
Definision of system (Hamiltonian) etc.
Definition: struct.h:409
int expec_cisajscktaltdc(struct BindStruct *X, double complex *vec)
Parent function to calculate two-body green&#39;s functions.
int St
0 or 1, but it affects nothing.
Definition: struct.h:80
void StartTimer(int n)
function for initializing elapse time [start]
Definition: time.c:71
const char * cOutputEigenVecStart
Definition: LogMessage.c:45
int itr
Iteration number.
Definition: struct.h:314
unsigned long int idim_max
The dimension of the Hilbert space of this process.
Definition: struct.h:302
int initial_mode
Definition: global.h:60
const char * cFileNameInputEigen
Definition: global.c:50
double Target_CG_energy
Taget energy of CG-inversed iteration (NOT LOBCG) method.
Definition: struct.h:388
const char * cLogLanczos_EigenVecEnd
void StopTimer(int n)
function for calculating elapse time [elapse time=StartTimer-StopTimer]
Definition: time.c:83
double complex * v1
Definition: global.h:35
#define D_FileNameMax
Definition: global.h:23
int iOutputEigenVec
ASwitch for outputing an eigenvector. 0: no output, 1:output.
Definition: struct.h:202
struct LargeList Large
Variables for Matrix-Vector product.
Definition: struct.h:411
int childfopenMPI(const char *_cPathChild, const char *_cmode, FILE **_fp)
Only the root process open file in output/ directory.
Definition: FileIO.c:27
const char * cFileNameOutputEigen
Definition: global.c:49
struct PhysList Phys
Physical quantities.
Definition: struct.h:412
unsigned long int idim_maxMPI
The total dimension across process.
Definition: struct.h:303
int CalcByLanczos(struct EDMainCalStruct *X)
A main function to calculate eigenvalues and eigenvectors by Lanczos method.
Definition: CalcByLanczos.c:54
#define TRUE
Definition: global.h:26
int expec_totalSz(struct BindStruct *X, double complex *vec)
Bind.
Definition: struct.h:418
int CG_EigenVector(struct BindStruct *X)
inversed power method with CG
const char * cOutputEigenVecFinish
Definition: LogMessage.c:46
const char * cLogLanczos_EigenVecStart
int expec_cisajs(struct BindStruct *X, double complex *vec)
function of calculation for one body green&#39;s function
Definition: expec_cisajs.c:69
double eps_Energy
Definition: global.h:155
const char * cReadEigenVecFinish
Definition: LogMessage.c:44
void Lanczos_EigenVector(struct BindStruct *X)
Calculate eigenvectors by the Lanczos method. The calculated tridiagonal matrix components are stor...
double var
Expectation value of the Energy variance.
Definition: struct.h:362
int iFlgGeneralSpin
Flag for the general (Sz/=1/2) spin.
Definition: struct.h:86
#define FALSE
Definition: global.h:25
int iCalcEigenVec
Switch for method to calculate eigenvectors. 0:Lanczos+CG, 1: Lanczos. default value is set as 0 in r...
Definition: struct.h:193
const char * cFileNameTimeKeep
Definition: global.c:23
const char * cReadEigenVecStart
Definition: LogMessage.c:43
int Lanczos_EigenValue(struct BindStruct *X)
Main function for calculating eigen values by Lanczos method. The energy convergence is judged by the...
struct EDMainCalStruct X
Definition: struct.h:431
double doublon
Expectation value of the Doublon.
Definition: struct.h:355
int iCalcModel
Switch for model. 0:Hubbard, 1:Spin, 2:Kondo, 3:HubbardGC, 4:SpinGC, 5:KondoGC, 6:HubbardNConserved.
Definition: struct.h:198
double Sz
Expectation value of the Total Sz.
Definition: struct.h:359
long int initial_iv
Seed of random number for initial guesss of wavefunctions.
Definition: struct.h:76
int myrank
Process ID, defined in InitializeMPI()
Definition: global.h:162
int expec_energy_flct(struct BindStruct *X)
Parent function to calculate expected values of energy and physical quantities.
double energy
Expectation value of the total energy.
Definition: struct.h:354
double complex * v0
Definition: global.h:34
int iInputEigenVec
Switch for reading an eigenvector. 0: no input, 1:input.
Definition: struct.h:203
const char * cFileNameEnergy_Lanczos
Definition: global.c:40
struct CheckList Check
Size of the Hilbert space.
Definition: struct.h:410
int step_i
Definition: global.h:66
char * CDataFileHead
Read from Calcmod in readdef.h. Header of output file such as Green&#39;s function.
Definition: struct.h:42
struct BindStruct Bind
Binded struct.
Definition: struct.h:419
int TimeKeeper(struct BindStruct *X, const char *cFileName, const char *cTimeKeeper_Message, const char *cWriteType)
Functions for writing a time log.
Definition: log.c:42
double Target_energy
Is it really used ???
Definition: struct.h:387
FILE * stdoutMPI
File pointer to the standard output defined in InitializeMPI()
Definition: global.h:164
unsigned int k_exct
Read from Calcmod in readdef.h.
Definition: struct.h:47