HΦ  3.1.0
Lanczos_EigenValue.c File Reference

Calculate eigenvalues by the Lanczos method. More...

#include "Common.h"
#include "mfmemory.h"
#include "mltply.h"
#include "vec12.h"
#include "bisec.h"
#include "FileIO.h"
#include "matrixlapack.h"
#include "Lanczos_EigenValue.h"
#include "wrapperMPI.h"
#include "CalcTime.h"

Go to the source code of this file.

Functions

int Lanczos_EigenValue (struct BindStruct *X)
 Main function for calculating eigen values by Lanczos method.
The energy convergence is judged by the level of target energy determined by \( \verb|k_exct| \).
. More...
 
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. More...
 
int ReadInitialVector (struct BindStruct *X, double complex *_v0, double complex *_v1, unsigned long int *liLanczosStp_vec)
 Read initial vectors for the restart calculation. More...
 
int OutputLanczosVector (struct BindStruct *X, double complex *tmp_v0, double complex *tmp_v1, unsigned long int liLanczosStp_vec)
 Output initial vectors for the restart calculation. More...
 
void SetInitialVector (struct BindStruct *X, double complex *tmp_v0, double complex *tmp_v1)
 Set initial vector to start the calculation for Lanczos method.
. More...
 
int ReadTMComponents (struct BindStruct *X, double *_dnorm, unsigned long int *_i_max, int iFlg)
 Read tridiagonal matrix components obtained by the Lanczos method.
. More...
 
int OutputTMComponents (struct BindStruct *X, double *_alpha, double *_beta, double _dnorm, int liLanczosStp)
 Output tridiagonal matrix components obtained by the Lanczos method. More...
 

Detailed Description

Calculate eigenvalues by the Lanczos method.

Version
0.1
Author
Takahiro Misawa (The University of Tokyo)
Kazuyoshi Yoshimi (The University of Tokyo)

Definition in file Lanczos_EigenValue.c.

Function Documentation

◆ Lanczos_EigenValue()

int Lanczos_EigenValue ( struct BindStruct X)

Main function for calculating eigen values by Lanczos method.
The energy convergence is judged by the level of target energy determined by \( \verb|k_exct| \).
.

Parameters
X[in] Struct to give the information for calculating the eigen values.
Return values
-2Fail to read the initial vectors or triangular matrix components.
-1Fail to obtain the eigen values with in the \( \verb| Lanczos_max |\) step
0Succeed to calculate the eigen values.
Version
0.1
Author
Takahiro Misawa (The University of Tokyo)
Kazuyoshi Yoshimi (The University of Tokyo)

Definition at line 50 of file Lanczos_EigenValue.c.

References alpha, beta, cFileNameLanczosStep, cFileNameTimeKeep, childfopenMPI(), cLanczos_EigenValueFinish, cLanczos_EigenValueStart, cLanczos_EigenValueStep, cLogLanczos_EigenValueEnd, cLogLanczos_EigenValueNotConverged, cLogLanczos_EigenValueStart, D_FileNameMax, DSEVvalue(), eps_Lanczos, mfint, mltply(), OutputLanczosVector(), OutputTMComponents(), ReadInitialVector(), ReadTMComponents(), SetInitialVector(), StartTimer(), stdoutMPI, StopTimer(), SumMPI_dc(), SumMPI_li(), TimeKeeper(), TimeKeeperWithStep(), TRUE, v0, v1, vec12(), and X.

Referenced by CalcByLanczos().

50  {
51 
52  fprintf(stdoutMPI, "%s", cLogLanczos_EigenValueStart);
53  FILE *fp;
54  char sdt[D_FileNameMax], sdt_2[D_FileNameMax];
55  int stp;
56  long int i, i_max;
57  unsigned long int i_max_tmp;
58  int k_exct, Target;
59  int iconv = -1;
60  double beta1, alpha1; //beta,alpha1 should be real
61  double complex temp1, temp2;
62  double complex cbeta1;
63  double E[5], ebefor, E_target;
64 
65 // for GC
66  double dnorm=0.0;
67 
68  double **tmp_mat;
69  double *tmp_E;
70  int int_i, int_j, mfint[7];
71  int iret=0;
72  sprintf(sdt_2, cFileNameLanczosStep, X->Def.CDataFileHead);
73 
74  i_max = X->Check.idim_max;
75  k_exct = X->Def.k_exct;
76  unsigned long int liLanczosStp;
77  liLanczosStp = X->Def.Lanczos_max;
78  unsigned long int liLanczosStp_vec=0;
79 
80  if (X->Def.iReStart == RESTART_INOUT || X->Def.iReStart == RESTART_IN){
81  if(ReadInitialVector( X, v0, v1, &liLanczosStp_vec)!=0){
82  fprintf(stdoutMPI, " Error: Fail to read initial vectors\n");
83  return -2;
84  }
85  iret=ReadTMComponents(X, &dnorm, &liLanczosStp, 0);
86  if(iret !=TRUE){
87  fprintf(stdoutMPI, " Error: Fail to read TMcomponents\n");
88  return -2;
89  }
90  if(liLanczosStp_vec !=liLanczosStp){
91  fprintf(stdoutMPI, " Error: Input files for vector and TMcomponents are incoorect.\n");
92  fprintf(stdoutMPI, " Error: Input vector %ld th stps, TMcomponents %ld th stps.\n", liLanczosStp_vec, liLanczosStp);
93  return -2;
94  }
95  X->Def.Lanczos_restart=liLanczosStp;
96  //Calculate EigenValue
97 
98  liLanczosStp = liLanczosStp+X->Def.Lanczos_max;
99  alpha1=alpha[X->Def.Lanczos_restart];
100  beta1=beta[X->Def.Lanczos_restart];
101  }/*X->Def.iReStart == RESTART_INOUT || X->Def.iReStart == RESTART_IN*/
102  else {
103  SetInitialVector(X, v0, v1);
104  //Eigenvalues by Lanczos method
106  StartTimer(4101);
107  mltply(X, v0, v1);
108  StopTimer(4101);
109  stp = 1;
111 
112  alpha1 = creal(X->Large.prdct);// alpha = v^{\dag}*H*v
113 
114  alpha[1] = alpha1;
115  cbeta1 = 0.0;
116 
117 #pragma omp parallel for reduction(+:cbeta1) default(none) private(i) shared(v0, v1) firstprivate(i_max, alpha1)
118  for (i = 1; i <= i_max; i++) {
119  cbeta1 += conj(v0[i] - alpha1 * v1[i]) * (v0[i] - alpha1 * v1[i]);
120  }
121  cbeta1 = SumMPI_dc(cbeta1);
122  beta1 = creal(cbeta1);
123  beta1 = sqrt(beta1);
124  beta[1] = beta1;
125  ebefor = 0;
126  liLanczosStp = X->Def.Lanczos_max;
127  X->Def.Lanczos_restart =1;
128  }/*else restart*/
129 
130  /*
131  * Set Maximum number of loop to the dimention of the Wavefunction
132  */
133  i_max_tmp = SumMPI_li(i_max);
134  if (i_max_tmp < liLanczosStp) {
135  liLanczosStp = i_max_tmp;
136  }
137  if (i_max_tmp < X->Def.LanczosTarget) {
138  liLanczosStp = i_max_tmp;
139  }
140  if (i_max_tmp == 1) {
141  E[1] = alpha[1];
142  StartTimer(4102);
143  vec12(alpha, beta, stp, E, X);
144  StopTimer(4102);
145  X->Large.itr = stp;
146  X->Phys.Target_energy = E[k_exct];
147  iconv = 0;
148  fprintf(stdoutMPI, " LanczosStep E[1] \n");
149  fprintf(stdoutMPI, " stp=%d %.10lf \n", stp, E[1]);
150  }
151 
152  fprintf(stdoutMPI, " LanczosStep E[1] E[2] E[3] E[4] Target:E[%d] E_Max/Nsite\n", X->Def.LanczosTarget + 1);
153  for (stp = X->Def.Lanczos_restart+1; stp <= liLanczosStp; stp++) {
154 #pragma omp parallel for default(none) private(i,temp1, temp2) shared(v0, v1) firstprivate(i_max, alpha1, beta1)
155  for (i = 1; i <= i_max; i++) {
156  temp1 = v1[i];
157  temp2 = (v0[i] - alpha1 * v1[i]) / beta1;
158  v0[i] = -beta1 * temp1;
159  v1[i] = temp2;
160  }
161 
162  StartTimer(4101);
163  mltply(X, v0, v1);
164  StopTimer(4101);
166  alpha1 = creal(X->Large.prdct);
167  alpha[stp] = alpha1;
168  cbeta1 = 0.0;
169 
170 #pragma omp parallel for reduction(+:cbeta1) default(none) private(i) shared(v0, v1) firstprivate(i_max, alpha1)
171  for (i = 1; i <= i_max; i++) {
172  cbeta1 += conj(v0[i] - alpha1 * v1[i]) * (v0[i] - alpha1 * v1[i]);
173  }
174  cbeta1 = SumMPI_dc(cbeta1);
175  beta1 = creal(cbeta1);
176  beta1 = sqrt(beta1);
177  beta[stp] = beta1;
178 
179  Target = X->Def.LanczosTarget;
180 
181  if (stp == 2) {
182  d_malloc2(tmp_mat, stp, stp);
183  d_malloc1(tmp_E, stp + 1);
184 
185  for (int_i = 0; int_i < stp; int_i++) {
186  for (int_j = 0; int_j < stp; int_j++) {
187  tmp_mat[int_i][int_j] = 0.0;
188  }
189  }
190  tmp_mat[0][0] = alpha[1];
191  tmp_mat[0][1] = beta[1];
192  tmp_mat[1][0] = beta[1];
193  tmp_mat[1][1] = alpha[2];
194  DSEVvalue(stp, tmp_mat, tmp_E);
195  E[1] = tmp_E[0];
196  E[2] = tmp_E[1];
197  if (Target < 2) {
198  E_target = tmp_E[Target];
199  ebefor = E_target;
200  }
201  d_free1(tmp_E, stp + 1);
202  d_free2(tmp_mat, stp, stp);
203 
204  childfopenMPI(sdt_2, "w", &fp);
205 
206  fprintf(fp, "LanczosStep E[1] E[2] E[3] E[4] Target:E[%d] E_Max/Nsite\n", Target + 1);
207  if (Target < 2) {
208  fprintf(stdoutMPI, " stp = %d %.10lf %.10lf xxxxxxxxxx xxxxxxxxx %.10lf xxxxxxxxx \n", stp, E[1], E[2],
209  E_target);
210  fprintf(fp, "stp = %d %.10lf %.10lf xxxxxxxxxx xxxxxxxxx %.10lf xxxxxxxxx \n", stp, E[1], E[2], E_target);
211  } else {
212  fprintf(stdoutMPI, " stp = %d %.10lf %.10lf xxxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx \n", stp, E[1], E[2]);
213  fprintf(fp, "stp = %d %.10lf %.10lf xxxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx \n", stp, E[1], E[2]);
214  }
215 
216  fclose(fp);
217  }
218 
219  //if (stp > 2 && stp % 2 == 0) {
220  if (stp > 2) {
221  childfopenMPI(sdt_2, "a", &fp);
222 
223  d_malloc2(tmp_mat, stp, stp);
224  d_malloc1(tmp_E, stp + 1);
225 
226  for (int_i = 0; int_i < stp; int_i++) {
227  for (int_j = 0; int_j < stp; int_j++) {
228  tmp_mat[int_i][int_j] = 0.0;
229  }
230  }
231  tmp_mat[0][0] = alpha[1];
232  tmp_mat[0][1] = beta[1];
233  for (int_i = 1; int_i < stp - 1; int_i++) {
234  tmp_mat[int_i][int_i] = alpha[int_i + 1];
235  tmp_mat[int_i][int_i + 1] = beta[int_i + 1];
236  tmp_mat[int_i][int_i - 1] = beta[int_i];
237  }
238  tmp_mat[int_i][int_i] = alpha[int_i + 1];
239  tmp_mat[int_i][int_i - 1] = beta[int_i];
240  StartTimer(4103);
241  DSEVvalue(stp, tmp_mat, tmp_E);
242  StopTimer(4103);
243  E[1] = tmp_E[0];
244  E[2] = tmp_E[1];
245  E[3] = tmp_E[2];
246  E[4] = tmp_E[3];
247  E[0] = tmp_E[stp - 1];
248  if (stp > Target) {
249  E_target = tmp_E[Target];
250  }
251  d_free1(tmp_E, stp + 1);
252  d_free2(tmp_mat, stp, stp);
253  if (stp > Target) {
254  fprintf(stdoutMPI, " stp = %d %.10lf %.10lf %.10lf %.10lf %.10lf %.10lf\n", stp, E[1], E[2], E[3], E[4],
255  E_target, E[0] / (double) X->Def.NsiteMPI);
256  fprintf(fp, "stp=%d %.10lf %.10lf %.10lf %.10lf %.10lf %.10lf\n", stp, E[1], E[2], E[3], E[4], E_target,
257  E[0] / (double) X->Def.NsiteMPI);
258  } else {
259  fprintf(stdoutMPI, " stp = %d %.10lf %.10lf %.10lf %.10lf xxxxxxxxx %.10lf\n", stp, E[1], E[2], E[3], E[4],
260  E[0] / (double) X->Def.NsiteMPI);
261  fprintf(fp, "stp=%d %.10lf %.10lf %.10lf %.10lf xxxxxxxxx %.10lf\n", stp, E[1], E[2], E[3], E[4],
262  E[0] / (double) X->Def.NsiteMPI);
263  }
264  fclose(fp);
265  if (stp > Target) {
266  if (fabs((E_target - ebefor) / E_target) < eps_Lanczos || fabs(beta[stp]) < pow(10.0, -14)) {
267  /*
268  if(X->Def.iReStart == RESTART_INOUT ||X->Def.iReStart == RESTART_OUT){
269  break;
270  }
271  */
272  d_malloc1(tmp_E, stp + 1);
273  StartTimer(4102);
274  vec12(alpha, beta, stp, tmp_E, X);
275  StopTimer(4102);
276  X->Large.itr = stp;
277  X->Phys.Target_energy = E_target;
278  X->Phys.Target_CG_energy = tmp_E[k_exct]; //for CG
279  iconv = 0;
280  d_free1(tmp_E, stp + 1);
281  break;
282  }
283  ebefor = E_target;
284  }
285 
286  }
287  }
288  if (X->Def.iReStart == RESTART_INOUT ||X->Def.iReStart == RESTART_OUT ){
289  if(stp != X->Def.Lanczos_restart+2) { // 2 steps are needed to get the value: E[stp+2]-E[stp+1]
290  OutputTMComponents(X, alpha, beta, dnorm, stp - 1);
291  OutputLanczosVector(X, v0, v1, stp - 1);
292  }
293  //return 0;
294  }
295 
296  sprintf(sdt, cFileNameTimeKeep, X->Def.CDataFileHead);
297  if (iconv != 0) {
298  sprintf(sdt, "%s", cLogLanczos_EigenValueNotConverged);
299  fprintf(stdoutMPI, " Lanczos Eigenvalue is not converged in this process.\n");
300  return -1;
301  }
302 
304  fprintf(stdoutMPI, "%s", cLogLanczos_EigenValueEnd);
305 
306  return 0;
307 }
int mltply(struct BindStruct *X, double complex *tmp_v0, double complex *tmp_v1)
Parent function of multiplying the wavefunction by the Hamiltonian. . First, the calculation of diago...
Definition: mltply.c:56
void StartTimer(int n)
function for initializing elapse time [start]
Definition: time.c:71
double complex SumMPI_dc(double complex norm)
MPI wrapper function to obtain sum of Double complex across processes.
Definition: wrapperMPI.c:205
const char * cLogLanczos_EigenValueNotConverged
Definition: LogMessage.c:89
void SetInitialVector(struct BindStruct *X, double complex *tmp_v0, double complex *tmp_v1)
Set initial vector to start the calculation for Lanczos method. .
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 childfopenMPI(const char *_cPathChild, const char *_cmode, FILE **_fp)
Only the root process open file in output/ directory.
Definition: FileIO.c:27
const char * cLanczos_EigenValueFinish
Definition: LogMessage.c:93
int DSEVvalue(int xNsize, double **A, double *r)
obtain eigenvalues of real symmetric A
Definition: matrixlapack.c:94
const char * cFileNameLanczosStep
Definition: global.c:39
#define TRUE
Definition: global.h:26
int OutputLanczosVector(struct BindStruct *X, double complex *tmp_v0, double complex *tmp_v1, unsigned long int liLanczosStp_vec)
Output initial vectors for the restart calculation.
const char * cLogLanczos_EigenValueStart
const char * cLogLanczos_EigenValueEnd
const char * cLanczos_EigenValueStart
Definition: LogMessage.c:91
static unsigned long int mfint[7]
Definition: xsetmem.c:34
int ReadInitialVector(struct BindStruct *X, double complex *_v0, double complex *_v1, unsigned long int *liLanczosStp_vec)
Read initial vectors for the restart calculation.
double * alpha
Definition: global.h:44
const char * cFileNameTimeKeep
Definition: global.c:23
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.
const char * cLanczos_EigenValueStep
Definition: LogMessage.c:92
struct EDMainCalStruct X
Definition: struct.h:431
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
unsigned long int SumMPI_li(unsigned long int idim)
MPI wrapper function to obtain sum of unsigned long integer across processes.
Definition: wrapperMPI.c:239
int TimeKeeperWithStep(struct BindStruct *X, const char *cFileName, const char *cTimeKeeper_Message, const char *cWriteType, const int istep)
Functions for writing a time log.
Definition: log.c:78
double eps_Lanczos
Definition: global.h:154
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
FILE * stdoutMPI
File pointer to the standard output defined in InitializeMPI()
Definition: global.h:164

◆ Lanczos_GetTridiagonalMatrixComponents()

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.

Parameters
X[in] Struct to give the information to calculate triangular matrix components.
_alpha[in,out] Triangular matrix components.
_beta[in,out] Triangular matrix components.
tmp_v1[in, out] A temporary vector to calculate triangular matrix components.
liLanczos_step[in] The max iteration step.
Version
1.2
Returns
TRUE
Author
Kazuyoshi Yoshimi (The University of Tokyo)

Definition at line 321 of file Lanczos_EigenValue.c.

References alpha, beta, c_Lanczos_SpectrumStep, cFileNameLanczosStep, cFileNameTimeKeep, D_FileNameMax, mltply(), stdoutMPI, SumMPI_dc(), SumMPI_li(), TimeKeeperWithStep(), TRUE, v0, v1, and X.

Referenced by CalcSpectrumByTPQ().

327  {
328 
329  char sdt[D_FileNameMax];
330  int stp;
331  long int i, i_max;
332  i_max = X->Check.idim_max;
333 
334  unsigned long int i_max_tmp;
335  double beta1, alpha1; //beta,alpha1 should be real
336  double complex temp1, temp2;
337  double complex cbeta1;
338 
339  sprintf(sdt, cFileNameLanczosStep, X->Def.CDataFileHead);
340 
341  /*
342  Set Maximum number of loop to the dimension of the Wavefunction
343  */
344  i_max_tmp = SumMPI_li(i_max);
345  if (i_max_tmp < *liLanczos_step || i_max_tmp < X->Def.LanczosTarget) {
346  *liLanczos_step = i_max_tmp;
347  }
348 
349  if (X->Def.Lanczos_restart == 0) { // initial procedure (not restart)
350 #pragma omp parallel for default(none) private(i) shared(v0, v1, tmp_v1) firstprivate(i_max)
351  for (i = 1; i <= i_max; i++) {
352  v0[i] = 0.0;
353  v1[i] = tmp_v1[i];
354  }
355  stp = 0;
356  mltply(X, v0, tmp_v1);
358  alpha1 = creal(X->Large.prdct);// alpha = v^{\dag}*H*v
359  _alpha[1] = alpha1;
360  cbeta1 = 0.0;
361  fprintf(stdoutMPI, " Step / Step_max alpha beta \n");
362 
363 #pragma omp parallel for reduction(+:cbeta1) default(none) private(i) shared(v0, v1) firstprivate(i_max, alpha1)
364  for (i = 1; i <= i_max; i++) {
365  cbeta1 += conj(v0[i] - alpha1 * v1[i]) * (v0[i] - alpha1 * v1[i]);
366  }
367  cbeta1 = SumMPI_dc(cbeta1);
368  beta1 = creal(cbeta1);
369  beta1 = sqrt(beta1);
370  _beta[1] = beta1;
371  X->Def.Lanczos_restart = 1;
372  } else { // restart case
373  alpha1 = alpha[X->Def.Lanczos_restart];
374  beta1 = beta[X->Def.Lanczos_restart];
375  }
376 
377  for (stp = X->Def.Lanczos_restart + 1; stp <= *liLanczos_step; stp++) {
378 
379  if (fabs(_beta[stp - 1]) < pow(10.0, -14)) {
380  *liLanczos_step = stp - 1;
381  break;
382  }
383 
384 #pragma omp parallel for default(none) private(i, temp1, temp2) shared(v0, v1) firstprivate(i_max, alpha1, beta1)
385  for (i = 1; i <= i_max; i++) {
386  temp1 = v1[i];
387  temp2 = (v0[i] - alpha1 * v1[i]) / beta1;
388  v0[i] = -beta1 * temp1;
389  v1[i] = temp2;
390  }
391 
392  mltply(X, v0, v1);
394  alpha1 = creal(X->Large.prdct);
395  _alpha[stp] = alpha1;
396  cbeta1 = 0.0;
397 
398 #pragma omp parallel for reduction(+:cbeta1) default(none) private(i) shared(v0, v1) firstprivate(i_max, alpha1)
399  for (i = 1; i <= i_max; i++) {
400  cbeta1 += conj(v0[i] - alpha1 * v1[i]) * (v0[i] - alpha1 * v1[i]);
401  }
402  cbeta1 = SumMPI_dc(cbeta1);
403  beta1 = creal(cbeta1);
404  beta1 = sqrt(beta1);
405  _beta[stp] = beta1;
406  if(stp %10 == 0) {
407  fprintf(stdoutMPI, " stp = %d / %lu %.10lf %.10lf \n", stp, *liLanczos_step, alpha1, beta1);
408  }
409  }
410 
411  return TRUE;
412 }
int mltply(struct BindStruct *X, double complex *tmp_v0, double complex *tmp_v1)
Parent function of multiplying the wavefunction by the Hamiltonian. . First, the calculation of diago...
Definition: mltply.c:56
double complex SumMPI_dc(double complex norm)
MPI wrapper function to obtain sum of Double complex across processes.
Definition: wrapperMPI.c:205
double complex * v1
Definition: global.h:35
#define D_FileNameMax
Definition: global.h:23
const char * c_Lanczos_SpectrumStep
Definition: LogMessage.c:76
const char * cFileNameLanczosStep
Definition: global.c:39
#define TRUE
Definition: global.h:26
double * alpha
Definition: global.h:44
const char * cFileNameTimeKeep
Definition: global.c:23
double * beta
Definition: global.h:44
struct EDMainCalStruct X
Definition: struct.h:431
double complex * v0
Definition: global.h:34
unsigned long int SumMPI_li(unsigned long int idim)
MPI wrapper function to obtain sum of unsigned long integer across processes.
Definition: wrapperMPI.c:239
int TimeKeeperWithStep(struct BindStruct *X, const char *cFileName, const char *cTimeKeeper_Message, const char *cWriteType, const int istep)
Functions for writing a time log.
Definition: log.c:78
FILE * stdoutMPI
File pointer to the standard output defined in InitializeMPI()
Definition: global.h:164

◆ OutputLanczosVector()

int OutputLanczosVector ( struct BindStruct X,
double complex *  tmp_v0,
double complex *  tmp_v1,
unsigned long int  liLanczosStp_vec 
)

Output initial vectors for the restart calculation.

Parameters
X[in] Give the dimension for the vector _v0 and _v1.
tmp_v0[in] The outputted vector for recalculation \( v_0 \).
tmp_v1[in] The outputted vector for recalculation \( v_1 \).
liLanczosStp_vec[in] The step for finishing the iteration.
Return values
-1Fail to output the vector.
0Succeed to output the vector.
Version
2.0
Author
Kazuyoshi Yoshimi (The University of Tokyo)

Definition at line 463 of file Lanczos_EigenValue.c.

References c_OutputSpectrumRecalcvecEnd, c_OutputSpectrumRecalcvecStart, cFileNameOutputRestartVec, cFileNameTimeKeep, childfopenALL(), D_FileNameMax, myrank, stdoutMPI, TimeKeeper(), and X.

Referenced by Lanczos_EigenValue().

466  {
467  char sdt[D_FileNameMax];
468  FILE *fp;
469 
470  fprintf(stdoutMPI, " Start: Output vectors for recalculation.\n");
472 
473  sprintf(sdt, cFileNameOutputRestartVec, X->Def.CDataFileHead, myrank);
474  if(childfopenALL(sdt, "wb", &fp)!=0){
475  return -1;
476  }
477  fwrite(&liLanczosStp_vec, sizeof(liLanczosStp_vec),1,fp);
478  fwrite(&X->Check.idim_max, sizeof(X->Check.idim_max),1,fp);
479  fwrite(tmp_v0, sizeof(complex double),X->Check.idim_max+1, fp);
480  fwrite(tmp_v1, sizeof(complex double),X->Check.idim_max+1, fp);
481  fclose(fp);
482 
483  fprintf(stdoutMPI, " End: Output vectors for recalculation.\n");
485  return 0;
486 }
int childfopenALL(const char *_cPathChild, const char *_cmode, FILE **_fp)
All processes open file in output/ directory.
Definition: FileIO.c:50
#define D_FileNameMax
Definition: global.h:23
const char * cFileNameOutputRestartVec
Definition: global.c:81
const char * c_OutputSpectrumRecalcvecEnd
Definition: LogMessage.c:71
const char * cFileNameTimeKeep
Definition: global.c:23
struct EDMainCalStruct X
Definition: struct.h:431
int myrank
Process ID, defined in InitializeMPI()
Definition: global.h:162
const char * c_OutputSpectrumRecalcvecStart
Definition: LogMessage.c:70
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
FILE * stdoutMPI
File pointer to the standard output defined in InitializeMPI()
Definition: global.h:164

◆ OutputTMComponents()

int OutputTMComponents ( struct BindStruct X,
double *  _alpha,
double *  _beta,
double  _dnorm,
int  liLanczosStp 
)

Output tridiagonal matrix components obtained by the Lanczos method.

Parameters
X[in] Give the input file name.
_alpha[in] The array of tridiagonal matrix components.
_beta[in] The array of tridiagonal matrix components.
_dnorm[in] The norm.
liLanczosStp[in] The iteration step.
Return values
FALSEFail to open the file for the output.
TRUESucceed to open the file for the output.
Version
1.2
Author
Kazuyoshi Yoshimi (The University of Tokyo)

Definition at line 691 of file Lanczos_EigenValue.c.

References cFileNameTridiagonalMatrixComponents, childfopenMPI(), D_FileNameMax, FALSE, TRUE, and X.

Referenced by CalcSpectrumByTPQ(), and Lanczos_EigenValue().

698 {
699  char sdt[D_FileNameMax];
700  unsigned long int i;
701  FILE *fp;
702 
703  sprintf(sdt, cFileNameTridiagonalMatrixComponents, X->Def.CDataFileHead);
704  if(childfopenMPI(sdt,"w", &fp)!=0){
705  return FALSE;
706  }
707  fprintf(fp, "%d \n",liLanczosStp);
708  fprintf(fp, "%.10lf \n",_dnorm);
709  for( i = 1 ; i <= liLanczosStp; i++){
710  fprintf(fp,"%.10lf %.10lf \n", _alpha[i], _beta[i]);
711  }
712  fclose(fp);
713  return TRUE;
714 }
#define D_FileNameMax
Definition: global.h:23
int childfopenMPI(const char *_cPathChild, const char *_cmode, FILE **_fp)
Only the root process open file in output/ directory.
Definition: FileIO.c:27
#define TRUE
Definition: global.h:26
const char * cFileNameTridiagonalMatrixComponents
Definition: global.c:52
#define FALSE
Definition: global.h:25
struct EDMainCalStruct X
Definition: struct.h:431

◆ ReadInitialVector()

int ReadInitialVector ( struct BindStruct X,
double complex *  _v0,
double complex *  _v1,
unsigned long int *  liLanczosStp_vec 
)

Read initial vectors for the restart calculation.

Parameters
X[in] Give the dimension for the vector _v0 and _v1.
_v0[out] The inputted vector for recalculation \( v_0 \).
_v1[out] The inputted vector for recalculation \( v_1 \).
liLanczosStp_vec[in] The max iteration step.
Return values
-1Fail to read the initial vector.
0Succeed to read the initial vector.
Version
1.2
Author
Kazuyoshi Yoshimi (The University of Tokyo)

Definition at line 425 of file Lanczos_EigenValue.c.

References c_InputSpectrumRecalcvecEnd, c_InputSpectrumRecalcvecStart, cFileNameOutputRestartVec, cFileNameTimeKeep, childfopenALL(), D_FileNameMax, myrank, stdoutMPI, TimeKeeper(), and X.

Referenced by Lanczos_EigenValue().

426 {
427  size_t byte_size;
428  char sdt[D_FileNameMax];
429  FILE *fp;
430  unsigned long int i_max;
431 
432  fprintf(stdoutMPI, " Start: Input vectors for recalculation.\n");
434  sprintf(sdt, cFileNameOutputRestartVec, X->Def.CDataFileHead, myrank);
435  if (childfopenALL(sdt, "rb", &fp) != 0) {
436  return -1;
437  }
438  byte_size = fread(liLanczosStp_vec, sizeof(*liLanczosStp_vec),1,fp);
439  byte_size = fread(&i_max, sizeof(long int), 1, fp);
440  if(i_max != X->Check.idim_max){
441  fprintf(stderr, "Error: A size of Inputvector is incorrect.\n");
442  return -1;
443  }
444  byte_size = fread(_v0, sizeof(complex double), X->Check.idim_max + 1, fp);
445  byte_size = fread(_v1, sizeof(complex double), X->Check.idim_max + 1, fp);
446  fclose(fp);
447  fprintf(stdoutMPI, " End: Input vectors for recalculation.\n");
449  if (byte_size == 0) printf("byte_size: %d \n", (int)byte_size);
450  return 0;
451 }
int childfopenALL(const char *_cPathChild, const char *_cmode, FILE **_fp)
All processes open file in output/ directory.
Definition: FileIO.c:50
#define D_FileNameMax
Definition: global.h:23
const char * cFileNameOutputRestartVec
Definition: global.c:81
const char * c_InputSpectrumRecalcvecStart
Definition: LogMessage.c:72
const char * cFileNameTimeKeep
Definition: global.c:23
const char * c_InputSpectrumRecalcvecEnd
Definition: LogMessage.c:73
struct EDMainCalStruct X
Definition: struct.h:431
int myrank
Process ID, defined in InitializeMPI()
Definition: global.h:162
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
FILE * stdoutMPI
File pointer to the standard output defined in InitializeMPI()
Definition: global.h:164

◆ ReadTMComponents()

int ReadTMComponents ( struct BindStruct X,
double *  _dnorm,
unsigned long int *  _i_max,
int  iFlg 
)

Read tridiagonal matrix components obtained by the Lanczos method.
.

Note
The arrays of tridiaonal components alpha and beta are global arrays.
Parameters
X[in] Give the iteration number for the recalculation and the input file name.
_dnorm[out] Get the norm.
_i_max[in] The iteration step for the input data.
iFlg[in] Flag for the recalculation.
Return values
FALSEFail to read the file.
TRUESucceed to read the file.
Version
1.2
Author
Kazuyoshi Yoshimi (The University of Tokyo)

Definition at line 621 of file Lanczos_EigenValue.c.

References alpha, beta, cFileNameTridiagonalMatrixComponents, childfopenMPI(), D_FileNameMax, FALSE, fgetsMPI(), TRUE, vec, and X.

Referenced by CalcSpectrumByTPQ(), and Lanczos_EigenValue().

626  {
627  char sdt[D_FileNameMax];
628  char ctmp[256];
629 
630  unsigned long int idx, i, ivec;
631  unsigned long int i_max;
632  double dnorm;
633  FILE *fp;
634  idx=1;
635  sprintf(sdt, cFileNameTridiagonalMatrixComponents, X->Def.CDataFileHead);
636  if(childfopenMPI(sdt,"r", &fp)!=0){
637  return FALSE;
638  }
639 
640  fgetsMPI(ctmp, sizeof(ctmp)/sizeof(char), fp);
641  sscanf(ctmp,"%ld \n", &i_max);
642  if (X->Def.LanczosTarget > X->Def.nvec) {
643  ivec = X->Def.LanczosTarget + 1;
644  }
645  else {
646  ivec =X->Def.nvec + 1;
647  }
648 
649  if(iFlg==0) {
650  alpha = (double *) realloc(alpha, sizeof(double) * (i_max + X->Def.Lanczos_max + 1));
651  beta = (double *) realloc(beta, sizeof(double) * (i_max + X->Def.Lanczos_max + 1));
652  for (i = 0; i < ivec; i++) {
653  vec[i] = (complex double *) realloc(vec[i], (i_max + X->Def.Lanczos_max + 1) * sizeof(complex double));
654  }
655  }
656  else if(iFlg==1){
657  alpha=(double*)realloc(alpha, sizeof(double)*(i_max + 1));
658  beta=(double*)realloc(beta, sizeof(double)*(i_max + 1));
659  for (i = 0; i < ivec; i++) {
660  vec[i] = (complex double *) realloc(vec[i], (i_max + X->Def.Lanczos_max + 1) * sizeof(complex double));
661  }
662  }
663  else{
664  fclose(fp);
665  return FALSE;
666  }
667  fgetsMPI(ctmp, sizeof(ctmp)/sizeof(char), fp);
668  sscanf(ctmp,"%lf \n", &dnorm);
669  while(fgetsMPI(ctmp, sizeof(ctmp)/sizeof(char), fp) != NULL){
670  sscanf(ctmp,"%lf %lf \n", &alpha[idx], &beta[idx]);
671  idx++;
672  }
673  fclose(fp);
674  *_dnorm=dnorm;
675  *_i_max=i_max;
676  return TRUE;
677 }
#define D_FileNameMax
Definition: global.h:23
double complex ** vec
Definition: global.h:45
int childfopenMPI(const char *_cPathChild, const char *_cmode, FILE **_fp)
Only the root process open file in output/ directory.
Definition: FileIO.c:27
#define TRUE
Definition: global.h:26
const char * cFileNameTridiagonalMatrixComponents
Definition: global.c:52
double * alpha
Definition: global.h:44
#define FALSE
Definition: global.h:25
double * beta
Definition: global.h:44
struct EDMainCalStruct X
Definition: struct.h:431
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

◆ SetInitialVector()

void SetInitialVector ( struct BindStruct X,
double complex *  tmp_v0,
double complex *  tmp_v1 
)

Set initial vector to start the calculation for Lanczos method.
.

Parameters
X[in, out] Get the information of reading initisl vectors.
Input: idim_max, iFlgMPI, k_exct, iInitialVecType.
Output: Large.iv.
tmp_v0[out] The initial vector whose components are zero.
tmp_v1[out] The initial vector whose components are randomly given when initial_mode=1, otherwise, iv-th component is only given.

Definition at line 496 of file Lanczos_EigenValue.c.

References BcastMPI_li(), initial_mode, myrank, nproc, nthreads, stdoutMPI, SumMPI_dc(), SumMPI_li(), and X.

Referenced by Lanczos_EigenValue().

496  {
497  int iproc;
498  long int i, iv, i_max;
499  unsigned long int i_max_tmp, sum_i_max;
500  int mythread;
501 
502 // for GC
503  double dnorm;
504  double complex cdnorm;
505  long unsigned int u_long_i;
506  dsfmt_t dsfmt;
507 
508  i_max = X->Check.idim_max;
509  if (initial_mode == 0) {
510  if(X->Def.iFlgMPI==0) {
511  sum_i_max = SumMPI_li(X->Check.idim_max);
512  }
513  else{
514  sum_i_max =X->Check.idim_max;
515  }
516  X->Large.iv = (sum_i_max / 2 + X->Def.initial_iv) % sum_i_max + 1;
517  iv = X->Large.iv;
518  fprintf(stdoutMPI, " initial_mode=%d normal: iv = %ld i_max=%ld k_exct =%d \n\n", initial_mode, iv, i_max,
519  X->Def.k_exct);
520 #pragma omp parallel for default(none) private(i) shared(tmp_v0, tmp_v1) firstprivate(i_max)
521  for (i = 1; i <= i_max; i++) {
522  tmp_v0[i] = 0.0;
523  tmp_v1[i] = 0.0;
524  }
525 
526  sum_i_max = 0;
527  if(X->Def.iFlgMPI==0) {
528  for (iproc = 0; iproc < nproc; iproc++) {
529  i_max_tmp = BcastMPI_li(iproc, i_max);
530  if (sum_i_max <= iv && iv < sum_i_max + i_max_tmp) {
531  if (myrank == iproc) {
532  tmp_v1[iv - sum_i_max + 1] = 1.0;
533  if (X->Def.iInitialVecType == 0) {
534  tmp_v1[iv - sum_i_max + 1] += 1.0 * I;
535  tmp_v1[iv - sum_i_max + 1] /= sqrt(2.0);
536  }
537  }/*if (myrank == iproc)*/
538  }/*if (sum_i_max <= iv && iv < sum_i_max + i_max_tmp)*/
539 
540  sum_i_max += i_max_tmp;
541 
542  }/*for (iproc = 0; iproc < nproc; iproc++)*/
543  }
544  else {
545  tmp_v1[iv + 1] = 1.0;
546  if (X->Def.iInitialVecType == 0) {
547  tmp_v1[iv + 1] += 1.0 * I;
548  tmp_v1[iv + 1] /= sqrt(2.0);
549  }
550  }
551  }/*if(initial_mode == 0)*/
552  else if (initial_mode == 1) {
553  iv = X->Def.initial_iv;
554  fprintf(stdoutMPI, " initial_mode=%d (random): iv = %ld i_max=%ld k_exct =%d \n\n", initial_mode, iv, i_max,
555  X->Def.k_exct);
556 #pragma omp parallel default(none) private(i, u_long_i, mythread, dsfmt) \
557  shared(tmp_v0, tmp_v1, iv, X, nthreads, myrank) firstprivate(i_max)
558  {
559 
560 #pragma omp for
561  for (i = 1; i <= i_max; i++) {
562  tmp_v0[i] = 0.0;
563  }
564  /*
565  Initialise MT
566  */
567 #ifdef _OPENMP
568  mythread = omp_get_thread_num();
569 #else
570  mythread = 0;
571 #endif
572  if(X->Def.iFlgMPI==0) {
573  u_long_i = 123432 + labs(iv) + mythread + nthreads * myrank;
574  }
575  else{
576  u_long_i = 123432 + labs(iv)+ mythread ;
577  }
578  dsfmt_init_gen_rand(&dsfmt, u_long_i);
579 
580  if (X->Def.iInitialVecType == 0) {
581 #pragma omp for
582  for (i = 1; i <= i_max; i++)
583  tmp_v1[i] = 2.0 * (dsfmt_genrand_close_open(&dsfmt) - 0.5) +
584  2.0 * (dsfmt_genrand_close_open(&dsfmt) - 0.5) * I;
585  } else {
586 #pragma omp for
587  for (i = 1; i <= i_max; i++)
588  tmp_v1[i] = 2.0 * (dsfmt_genrand_close_open(&dsfmt) - 0.5);
589  }
590 
591  }/*#pragma omp parallel*/
592 
593  cdnorm = 0.0;
594 #pragma omp parallel for default(none) private(i) shared(tmp_v1, i_max) reduction(+: cdnorm)
595  for (i = 1; i <= i_max; i++) {
596  cdnorm += conj(tmp_v1[i]) * tmp_v1[i];
597  }
598  if(X->Def.iFlgMPI==0) {
599  cdnorm = SumMPI_dc(cdnorm);
600  }
601  dnorm = creal(cdnorm);
602  dnorm = sqrt(dnorm);
603 #pragma omp parallel for default(none) private(i) shared(tmp_v1) firstprivate(i_max, dnorm)
604  for (i = 1; i <= i_max; i++) {
605  tmp_v1[i] = tmp_v1[i] / dnorm;
606  }
607  }/*else if(initial_mode==1)*/
608 }
double complex SumMPI_dc(double complex norm)
MPI wrapper function to obtain sum of Double complex across processes.
Definition: wrapperMPI.c:205
int initial_mode
Definition: global.h:60
unsigned long int BcastMPI_li(int root, unsigned long int idim)
MPI wrapper function to broadcast unsigned long integer across processes.
Definition: wrapperMPI.c:273
int nthreads
Number of Threads, defined in InitializeMPI()
Definition: global.h:163
int nproc
Number of processors, defined in InitializeMPI()
Definition: global.h:161
struct EDMainCalStruct X
Definition: struct.h:431
int myrank
Process ID, defined in InitializeMPI()
Definition: global.h:162
unsigned long int SumMPI_li(unsigned long int idim)
MPI wrapper function to obtain sum of unsigned long integer across processes.
Definition: wrapperMPI.c:239
FILE * stdoutMPI
File pointer to the standard output defined in InitializeMPI()
Definition: global.h:164