HΦ  3.2.0
CalcSpectrumByTPQ.c
Go to the documentation of this file.
1 /* HPhi - Quantum Lattice Model Simulator */
2 /* Copyright (C) 2015 Takahiro Misawa, Kazuyoshi Yoshimi, Mitsuaki Kawamura, Youhei Yamaji, Synge Todo, Naoki Kawashima */
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 "CalcSpectrumByTPQ.h"
17 #include "Lanczos_EigenValue.h"
18 #include "FileIO.h"
19 #include "wrapperMPI.h"
20 #include "vec12.h"
21 #include "common/setmemory.h"
33 int ReadTPQData(
41  struct EDMainCalStruct *X,
42  double* ene,
43  double* temp,
44  double* specificHeat
45 ){
46  FILE *fp;
47  char sdt[D_FileNameMax];
48  char ctmp2[256];
49  double dinv_temp;
50  double dene, dHvar, dn, ddoublon;
51  int istp;
52  sprintf(sdt, cFileNameSSRand, X->Bind.Def.irand);
53  childfopenMPI(sdt, "r", &fp);
54  if(fp==NULL){
55  fprintf(stderr, " Error: SS_rand%d.dat does not exist.\n", X->Bind.Def.irand);
56  fclose(fp);
57  return FALSE;
58  }
59  fgetsMPI(ctmp2, 256, fp);
60  while(fgetsMPI(ctmp2, 256, fp) != NULL) {
61  sscanf(ctmp2, "%lf %lf %lf %lf %lf %d\n",
62  &dinv_temp,
63  &dene,
64  &dHvar,
65  &dn,
66  &ddoublon,
67  &istp
68  );
69  if(istp==X->Bind.Large.itr) break;
70  }
71  fclose(fp);
72 
73  *ene = dene;
74  *temp = 1.0/dinv_temp;
75  *specificHeat = (dHvar-dene*dene)*(dinv_temp*dinv_temp);
76 
77  return TRUE;
78 }
79 
92 int GetCalcSpectrumTPQ(double complex dcomega, double dtemp, double dspecificheat,
93  double ene, double *tmp_E, int Nsite, int idim_max, double complex * dc_tmpSpec)
94 {
95  int l;
96  double tmp_dcSpec;
97  double factor, pre_factor;
98  pre_factor=2.0*dtemp*dtemp*dspecificheat;
99  factor=M_PI*pre_factor;
100  factor=1.0/sqrt(factor);
101  tmp_dcSpec=0;
102 
103  if(cimag(dcomega)>0) {
104  for (l = 1; l <= idim_max; l++) {
105  //TODO: Check omega is real ?
106  //fprintf(stdoutMPI, "Debug: %lf, %lf\n", creal(dcomega) - tmp_E[l] + ene, pre_factor);
107  tmp_dcSpec += (double)(vec[l][1] * conj(vec[l][1])) * exp(-pow((creal(dcomega) - tmp_E[l] + ene),2)/(pre_factor));
108  }
109  }
110  else{
111  fprintf(stderr, " an imarginary part of omega must be positive.\n");
112  return FALSE;
113  }
114  tmp_dcSpec *=factor;
115  *dc_tmpSpec = tmp_dcSpec;
116  return TRUE;
117 }
118 
131  struct EDMainCalStruct *X,
132  double complex *tmp_v1,
133  double dnorm,
134  int Nomega,
135  double complex *dcSpectrum,
136  double complex *dcomega
137 )
138 {
139  char sdt[D_FileNameMax];
140  unsigned long int i, i_max;
141  FILE *fp;
142  int iret;
143  unsigned long int liLanczosStp = X->Bind.Def.Lanczos_max;
144  unsigned long int liLanczosStp_vec=0;
145  double dene, dtemp, dspecificHeat;
146  double *tmp_E;
147  double complex dctmp_Spectrum;
148  int stp;
149  size_t byte_size;
150 
151  //Read Ene, temp, C
152  if(ReadTPQData(X, &dene, &dtemp, &dspecificHeat)!=TRUE){
153  return FALSE;
154  }
155 
156  if(X->Bind.Def.iFlgCalcSpec == RECALC_FROM_TMComponents_VEC ||
157  X->Bind.Def.iFlgCalcSpec == RECALC_INOUT_TMComponents_VEC) {
158  fprintf(stdoutMPI, " Start: Input vectors for recalculation.\n");
160 
162  if (childfopenALL(sdt, "rb", &fp) != 0) {
163  exitMPI(-1);
164  }
165  byte_size = fread(&liLanczosStp_vec, sizeof(liLanczosStp_vec),1,fp);
166  byte_size = fread(&i_max, sizeof(long int), 1, fp);
167  if(i_max != X->Bind.Check.idim_max){
168  fprintf(stderr, "Error: A size of Inputvector is incorrect.\n");
169  exitMPI(-1);
170  }
171  byte_size = fread(v0, sizeof(complex double), X->Bind.Check.idim_max + 1, fp);
172  byte_size = fread(v1, sizeof(complex double), X->Bind.Check.idim_max + 1, fp);
173  fclose(fp);
174  fprintf(stdoutMPI, " End: Input vectors for recalculation.\n");
176  if (byte_size == 0) printf("byte_size: %d \n", (int)byte_size);
177  }
178 
179  //Read diagonal components
180  if(X->Bind.Def.iFlgCalcSpec == RECALC_FROM_TMComponents ||
181  X->Bind.Def.iFlgCalcSpec ==RECALC_FROM_TMComponents_VEC||
182  X->Bind.Def.iFlgCalcSpec == RECALC_INOUT_TMComponents_VEC)
183  {
184  int iFlgTMComp=0;
185  if(X->Bind.Def.iFlgCalcSpec == RECALC_INOUT_TMComponents_VEC ||
186  X->Bind.Def.iFlgCalcSpec == RECALC_FROM_TMComponents_VEC)
187  {
188  iFlgTMComp=0;
189  }
190  else{
191  iFlgTMComp=1;
192  }
193  iret=ReadTMComponents(&(X->Bind), &dnorm, &liLanczosStp, iFlgTMComp);
194  if(iret !=TRUE){
195  fprintf(stdoutMPI, " Error: Fail to read TMcomponents\n");
196  return FALSE;
197  }
198 
199  if(X->Bind.Def.iFlgCalcSpec == RECALC_FROM_TMComponents){
200  X->Bind.Def.Lanczos_restart=0;
201  }
202  else if(X->Bind.Def.iFlgCalcSpec == RECALC_INOUT_TMComponents_VEC||
203  X->Bind.Def.iFlgCalcSpec == RECALC_FROM_TMComponents_VEC){
204  if(liLanczosStp_vec !=liLanczosStp){
205  fprintf(stdoutMPI, " Error: Input files for vector and TMcomponents are incoorect.\n");
206  fprintf(stdoutMPI, " Error: Input vector %ld th stps, TMcomponents %ld th stps.\n", liLanczosStp_vec, liLanczosStp);
207  return FALSE;
208  }
209  X->Bind.Def.Lanczos_restart=liLanczosStp;
210  liLanczosStp = liLanczosStp+X->Bind.Def.Lanczos_max;
211  }
212  }
213 
214  // calculate ai, bi
215  if (X->Bind.Def.iFlgCalcSpec == RECALC_NOT ||
216  X->Bind.Def.iFlgCalcSpec == RECALC_OUTPUT_TMComponents_VEC ||
217  X->Bind.Def.iFlgCalcSpec == RECALC_FROM_TMComponents_VEC ||
218  X->Bind.Def.iFlgCalcSpec == RECALC_INOUT_TMComponents_VEC
219  )
220  {
221  fprintf(stdoutMPI, " Start: Calculate tridiagonal matrix components.\n");
223  // Functions in Lanczos_EigenValue
224  iret = Lanczos_GetTridiagonalMatrixComponents(&(X->Bind), alpha, beta, tmp_v1, &(liLanczosStp));
225  if (iret != TRUE) {
226  //Error Message will be added.
227  return FALSE;
228  }
229  fprintf(stdoutMPI, " End: Calculate tridiagonal matrix components.\n\n");
231  OutputTMComponents(&(X->Bind), alpha,beta, dnorm, liLanczosStp);
232  }//X->Bind.Def.iFlgCalcSpec == RECALC_NOT || RECALC_FROM_TMComponents_VEC
233 
234  stp=liLanczosStp;
235  tmp_E = d_1d_allocate(stp + 1);
236  X->Bind.Def.nvec= stp;
237  vec12(alpha,beta,stp,tmp_E, &(X->Bind));
238  fprintf(stdoutMPI, " Start: Caclulate spectrum from tridiagonal matrix components.\n");
240  for( i = 0 ; i < Nomega; i++) {
241  dctmp_Spectrum=0;
242  iret = GetCalcSpectrumTPQ(dcomega[i], dtemp, dspecificHeat, dene, tmp_E, X->Bind.Def.Nsite, stp, &dctmp_Spectrum);
243  if (iret != TRUE) {
244  //ReAlloc alpha, beta and Set alpha_start and beta_start in Lanczos_EigenValue
245  return FALSE;
246  }
247  dcSpectrum[i] = dnorm * dctmp_Spectrum;
248  }
249  fprintf(stdoutMPI, " End: Caclulate spectrum from tridiagonal matrix components.\n\n");
251 
252  free_d_1d_allocate(tmp_E);
253  //output vectors for recalculation
254  if(X->Bind.Def.iFlgCalcSpec==RECALC_OUTPUT_TMComponents_VEC ||
255  X->Bind.Def.iFlgCalcSpec==RECALC_INOUT_TMComponents_VEC){
256  fprintf(stdoutMPI, " Start: Output vectors for recalculation.\n");
258 
260  if(childfopenALL(sdt, "wb", &fp)!=0){
261  exitMPI(-1);
262  }
263  fwrite(&liLanczosStp, sizeof(liLanczosStp),1,fp);
264  fwrite(&X->Bind.Check.idim_max, sizeof(X->Bind.Check.idim_max),1,fp);
265  fwrite(v0, sizeof(complex double),X->Bind.Check.idim_max+1, fp);
266  fwrite(v1, sizeof(complex double),X->Bind.Check.idim_max+1, fp);
267  fclose(fp);
268 
269  fprintf(stdoutMPI, " End: Output vectors for recalculation.\n");
271  }
272 
273  return TRUE;
274 }
int childfopenALL(const char *_cPathChild, const char *_cmode, FILE **_fp)
All processes open file in output/ directory.
Definition: FileIO.c:50
int irand
Input keyword TargetTPQRand ???
Definition: struct.h:79
void exitMPI(int errorcode)
MPI Abortation wrapper.
Definition: wrapperMPI.c:86
struct DefineList Def
Definision of system (Hamiltonian) etc.
Definition: struct.h:410
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
double complex * v1
Definition: global.h:35
int ReadTPQData(struct EDMainCalStruct *X, double *ene, double *temp, double *specificHeat)
Read TPQ data at "X->Bind.Large.itr" step in SS_rand file.
#define D_FileNameMax
Definition: global.h:23
double complex ** vec
Definition: global.h:45
struct LargeList Large
Variables for Matrix-Vector product.
Definition: struct.h:412
int Lanczos_GetTridiagonalMatrixComponents(struct BindStruct *X, double *_alpha, double *_beta, double complex *tmp_v1, unsigned long int *liLanczos_step)
Calculate tridiagonal matrix components by Lanczos method.
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 * cFileNameOutputRestartVec
Definition: global.c:81
unsigned int nvec
Read from Calcmod in readdef.h.
Definition: struct.h:46
#define TRUE
Definition: global.h:26
Bind.
Definition: struct.h:419
const char * c_OutputSpectrumRecalcvecEnd
Definition: LogMessage.c:71
unsigned int Nsite
Number of sites in the INTRA process region.
Definition: struct.h:56
int CalcSpectrumByTPQ(struct EDMainCalStruct *X, double complex *tmp_v1, double dnorm, int Nomega, double complex *dcSpectrum, double complex *dcomega)
A main function to calculate spectrum by TPQ (Note: This method is trial)
int Lanczos_restart
Number of iterations performed in the restart computation.
Definition: struct.h:75
const char * c_InputSpectrumRecalcvecStart
Definition: LogMessage.c:72
const char * c_GetTridiagonalStart
Definition: LogMessage.c:66
const char * c_CalcSpectrumFromTridiagonalEnd
Definition: LogMessage.c:69
double * alpha
Definition: global.h:44
#define FALSE
Definition: global.h:25
int GetCalcSpectrumTPQ(double complex dcomega, double dtemp, double dspecificheat, double ene, double *tmp_E, int Nsite, int idim_max, double complex *dc_tmpSpec)
Calculate spectrum function from the TPQ state.
const char * cFileNameTimeKeep
Definition: global.c:23
const char * c_InputSpectrumRecalcvecEnd
Definition: LogMessage.c:73
double * beta
Definition: global.h:44
void vec12(double alpha[], double beta[], unsigned int ndim, double tmp_E[], struct BindStruct *X)
Diagonalize a tri-diagonal matrix and store eigenvectors into vec.
Definition: vec12.c:31
int OutputTMComponents(struct BindStruct *X, double *_alpha, double *_beta, double _dnorm, int liLanczosStp)
Output tridiagonal matrix components obtained by the Lanczos method.
struct EDMainCalStruct X
Definition: struct.h:432
int myrank
Process ID, defined in InitializeMPI()
Definition: global.h:163
int iFlgCalcSpec
Input parameter CalcSpec in teh CalcMod file.
Definition: struct.h:216
const char * cFileNameSSRand
Definition: global.c:56
const char * c_CalcSpectrumFromTridiagonalStart
Definition: LogMessage.c:68
const char * c_GetTridiagonalEnd
Definition: LogMessage.c:67
char * fgetsMPI(char *InputString, int maxcount, FILE *fp)
MPI file I/O (get a line, fgets) wrapper. Only the root node (myrank = 0) reads and broadcast string...
Definition: wrapperMPI.c:122
int ReadTMComponents(struct BindStruct *X, double *_dnorm, unsigned long int *_i_max, int iFlg)
Read tridiagonal matrix components obtained by the Lanczos method. .
double complex * v0
Definition: global.h:34
const char * c_OutputSpectrumRecalcvecStart
Definition: LogMessage.c:70
struct CheckList Check
Size of the Hilbert space.
Definition: struct.h:411
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
unsigned int Lanczos_max
Maximum number of iterations.
Definition: struct.h:74
FILE * stdoutMPI
File pointer to the standard output defined in InitializeMPI()
Definition: global.h:165