HΦ  3.2.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 
64  if(X->Bind.Def.iInputEigenVec==FALSE){
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 == 1) return(TRUE);//restart mode.
98  else if(iret != 0) return(FALSE);
99 
100  if(X->Bind.Def.iCalcEigenVec==CALCVEC_NOT){
101  fprintf(stdoutMPI, " Lanczos EigenValue = %.10lf \n ",X->Bind.Phys.Target_energy);
102  return(TRUE);
103  }
104 
105  fprintf(stdoutMPI, "%s", cLogLanczos_EigenVecStart);
106 
107  if(X->Bind.Check.idim_maxMPI != 1){
108  StartTimer(4200);
109  Lanczos_EigenVector(&(X->Bind));
110  StopTimer(4200);
111 
112  StartTimer(4300);
113  iret=expec_energy_flct(&(X->Bind));
114  StopTimer(4300);
115  if(iret != 0) return(FALSE);
116 
117  //check for the accuracy of the eigenvector
118  var = fabs(X->Bind.Phys.var-X->Bind.Phys.energy*X->Bind.Phys.energy)/fabs(X->Bind.Phys.var);
119  diff_ene = fabs(X->Bind.Phys.Target_CG_energy-X->Bind.Phys.energy)/fabs(X->Bind.Phys.Target_CG_energy);
120 
121  fprintf(stdoutMPI, "\n");
122  fprintf(stdoutMPI, " Accuracy check !!!\n");
123  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);
124  if(diff_ene < eps_Energy && var< eps_Energy){
125  fprintf(stdoutMPI, " Accuracy of Lanczos vectors is enough.\n");
126  fprintf(stdoutMPI, "\n");
127  }
128  else if(X->Bind.Def.iCalcEigenVec==CALCVEC_LANCZOSCG){
129  fprintf(stdoutMPI, " Accuracy of Lanczos vectors is NOT enough\n\n");
130  X->Bind.Def.St=1;
131  StartTimer(4400);
132  iret=CG_EigenVector(&(X->Bind));
133  StopTimer(4400);
134  if(iret != 0) return(FALSE);
135 
136  StartTimer(4300);
137  iret=expec_energy_flct(&(X->Bind));
138  StopTimer(4300);
139  if(iret != 0) return(FALSE);
140 
141  var = fabs(X->Bind.Phys.var-X->Bind.Phys.energy*X->Bind.Phys.energy)/fabs(X->Bind.Phys.var);
142  diff_ene = fabs(X->Bind.Phys.Target_CG_energy-X->Bind.Phys.energy)/fabs(X->Bind.Phys.Target_CG_energy);
143  fprintf(stdoutMPI, "\n");
144  fprintf(stdoutMPI, " CG Accuracy check !!!\n");
145  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);
146  fprintf(stdoutMPI, "\n");
147  //}
148  }
149  }
150  else{//idim_max=1
151  v0[1]=1;
152  StartTimer(4300);
153  iret=expec_energy_flct(&(X->Bind));
154  StopTimer(4300);
155  if(iret != 0) return(FALSE);
156  }
157  }
158  else{// X->Bind.Def.iInputEigenVec=true :input v1:
159  fprintf(stdoutMPI, "An Eigenvector is inputted.\n");
160  StartTimer(4800);
162  StartTimer(4801);
163  sprintf(sdt, cFileNameInputEigen, X->Bind.Def.CDataFileHead, X->Bind.Def.k_exct-1, myrank);
164  childfopenALL(sdt, "rb", &fp);
165  if(fp==NULL){
166  fprintf(stderr, "Error: A file of Inputvector does not exist.\n");
167  exitMPI(-1);
168  }
169  byte_size = fread(&step_i, sizeof(int), 1, fp);
170  byte_size = fread(&i_max, sizeof(long int), 1, fp);
171  if(i_max != X->Bind.Check.idim_max){
172  fprintf(stderr, "Error: A file of Inputvector is incorrect.\n");
173  exitMPI(-1);
174  }
175  byte_size = fread(v1, sizeof(complex double),X->Bind.Check.idim_max+1, fp);
176  fclose(fp);
177  StopTimer(4801);
178  StopTimer(4800);
180  if (byte_size == 0) printf("byte_size: %d \n", (int)byte_size);
181  }
182 
183  fprintf(stdoutMPI, "%s", cLogLanczos_EigenVecEnd);
184  // v1 is eigen vector
185 
186  StartTimer(4500);
187  if(expec_cisajs(&(X->Bind), v1)!=0){
188  fprintf(stderr, "Error: calc OneBodyG.\n");
189  exitMPI(-1);
190  }
191  StopTimer(4500);
192  StartTimer(4600);
193  if(expec_cisajscktaltdc(&(X->Bind), v1)!=0){
194  fprintf(stderr, "Error: calc TwoBodyG.\n");
195  exitMPI(-1);
196  }
197  StopTimer(4600);
198 
199  if(expec_totalSz(&(X->Bind), v1)!=0){
200  fprintf(stderr, "Error: calc TotalSz.\n");
201  exitMPI(-1);
202  }
203 
204  if(X->Bind.Def.St==0){
205  sprintf(sdt, cFileNameEnergy_Lanczos, X->Bind.Def.CDataFileHead);
206  }else if(X->Bind.Def.St==1){
207  sprintf(sdt, cFileNameEnergy_CG, X->Bind.Def.CDataFileHead);
208  }
209 
210  if(childfopenMPI(sdt, "w", &fp)!=0){
211  exitMPI(-1);
212  }
213 
214  fprintf(fp,"Energy %.16lf \n",X->Bind.Phys.energy);
215  fprintf(fp,"Doublon %.16lf \n",X->Bind.Phys.doublon);
216  fprintf(fp,"Sz %.16lf \n",X->Bind.Phys.Sz);
217  // fprintf(fp,"total S^2 %.10lf \n",X->Bind.Phys.s2);
218  fclose(fp);
219 
220  if(X->Bind.Def.iOutputEigenVec==TRUE){
222  sprintf(sdt, cFileNameOutputEigen, X->Bind.Def.CDataFileHead, X->Bind.Def.k_exct-1, myrank);
223  if(childfopenALL(sdt, "wb", &fp)!=0){
224  exitMPI(-1);
225  }
226  fwrite(&X->Bind.Large.itr, sizeof(X->Bind.Large.itr),1,fp);
227  fwrite(&X->Bind.Check.idim_max, sizeof(X->Bind.Check.idim_max),1,fp);
228  fwrite(v1, sizeof(complex double),X->Bind.Check.idim_max+1, fp);
229  fclose(fp);
231  }
232 
233  return TRUE;
234 }
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:410
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:315
unsigned long int idim_max
The dimension of the Hilbert space of this process.
Definition: struct.h:303
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:389
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 outputting an eigenvector. 0: no output, 1:output.
Definition: struct.h:202
struct LargeList Large
Variables for Matrix-Vector product.
Definition: struct.h:412
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:413
unsigned long int idim_maxMPI
The total dimension across process.
Definition: struct.h:304
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:419
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:156
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:363
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:432
double doublon
Expectation value of the Doublon.
Definition: struct.h:356
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:360
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:163
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:355
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:411
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:420
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:388
FILE * stdoutMPI
File pointer to the standard output defined in InitializeMPI()
Definition: global.h:165
unsigned int k_exct
Read from Calcmod in readdef.h.
Definition: struct.h:47