File: FT_NLP.c

package info (click to toggle)
openmx 3.7.6-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 325,856 kB
  • ctags: 3,575
  • sloc: ansic: 152,655; f90: 2,080; python: 876; makefile: 675; sh: 25; perl: 18
file content (278 lines) | stat: -rw-r--r-- 6,831 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/**********************************************************************
  FT_NLP.c:

     FT_NLP.c is a subroutine to Fourier transform projectors
     of nonlocal potentials.

  Log of FT_NLP.c:

     15/Sep/2002  Released by T.Ozaki

***********************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
/*  stat section */
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
/*  end stat section */
#include "openmx_common.h"
#include "mpi.h"
#include <omp.h>



void FT_NLP()
{
  int numprocs,myid,ID,tag=999;
  int count,NumSpe;
  int i,kj,num_k,so;
  int Lspe,spe,L,GL,MaxGL;
  int RestartRead_Succeed;
  double dk,norm_k;
  double rmin,rmax,r,r2,h,sum[2];
  double **SphB;
  double *tmp_SphB,*tmp_SphBp;
  double TStime, TEtime;
  /* for MPI */
  MPI_Status stat;
  MPI_Request request;
  /* for OpenMP */
  int OMPID,Nthrds,Nprocs;

  char fileFT[YOUSO10];
  char operate[300];
  FILE *fp;
  size_t size; 

  dtime(&TStime);

  /* MPI */
  MPI_Comm_size(mpi_comm_level1,&numprocs);
  MPI_Comm_rank(mpi_comm_level1,&myid);
 
  if (myid==Host_ID && 0<level_stdout) printf("<FT_NLP>          Fourier transform of non-local projectors\n");

  RestartRead_Succeed = 0;

  /***********************************************************
     In case of Scf_RestartFromFile==1, read Spe_NLRF_Bessel
  ***********************************************************/

  if (Scf_RestartFromFile){

    /****************************************************
          generate radial grids in the k-space
    ****************************************************/

    dk = PAO_Nkmax/(double)Ngrid_NormK;
    for (i=0; i<Ngrid_NormK; i++){
      NormK[i] = (double)i*dk;
    }

    /***********************************************************
                      read Spe_NLRF_Bessel
    ***********************************************************/

    sprintf(fileFT,"%s%s_rst/%s.ftnlp",filepath,filename,filename);

    if ((fp = fopen(fileFT,"rb")) != NULL){

      RestartRead_Succeed = 1;

      for (spe=0; spe<SpeciesNum; spe++){
	for (so=0; so<=VPS_j_dependency[spe]; so++){
	  for (L=1; L<=Spe_Num_RVPS[spe]; L++){

	    size = fread(&Spe_NLRF_Bessel[so][spe][L][0],sizeof(double),List_YOUSO[15],fp);
	    if (size!=List_YOUSO[15]) RestartRead_Succeed = 0;
	  }
	}
      }

      fclose(fp);
    }
    else{
      printf("Could not open a file %s in FT_NLP\n",fileFT);
    }
  }

  /***********************************************************
     if (RestartRead_Succeed==0), calculate Spe_NLRF_Bessel
  ***********************************************************/

  if (RestartRead_Succeed==0){

    for (Lspe=0; Lspe<MSpeciesNum; Lspe++){

      spe = Species_Top[myid] + Lspe;

      num_k = Ngrid_NormK;
      dk = PAO_Nkmax/(double)num_k;
      rmin = Spe_VPS_RV[spe][0];
      rmax = Spe_Atom_Cut1[spe] + 0.5;
      h = (rmax - rmin)/(double)OneD_Grid;

      /* kj loop */

#pragma omp parallel shared(Spe_VPS_List,spe,Spe_Num_RVPS,num_k,dk,OneD_Grid,rmin,h,VPS_j_dependency,Spe_NLRF_Bessel)  private(MaxGL,L,GL,SphB,tmp_SphB,tmp_SphBp,OMPID,Nthrds,Nprocs,norm_k,i,r,r2,sum,so,kj)
      {

	/* allocate SphB */

	MaxGL = -1;
	for (L=1; L<=Spe_Num_RVPS[spe]; L++){
	  GL = Spe_VPS_List[spe][L];
	  if (MaxGL<GL) MaxGL = GL;
	}      

	SphB = (double**)malloc(sizeof(double*)*(MaxGL+3));
	for(GL=0; GL<(MaxGL+3); GL++){ 
	  SphB[GL] = (double*)malloc(sizeof(double)*(OneD_Grid+1));
	}

	tmp_SphB  = (double*)malloc(sizeof(double)*(MaxGL+3));
	tmp_SphBp = (double*)malloc(sizeof(double)*(MaxGL+3));

	/* get info. on OpenMP */ 

	OMPID = omp_get_thread_num();
	Nthrds = omp_get_num_threads();
	Nprocs = omp_get_num_procs();

	for ( kj=OMPID; kj<num_k; kj+=Nthrds ){

	  norm_k = (double)kj*dk;

	  /* calculate SphB */

	  for (i=0; i<=OneD_Grid; i++){

	    r = rmin + (double)i*h;

	    Spherical_Bessel(norm_k*r,MaxGL,tmp_SphB,tmp_SphBp);

	    r2 = r*r;
	    for(GL=0; GL<=MaxGL; GL++){ 
	      SphB[GL][i] = tmp_SphB[GL]*r2; 
	    }
	  }

	  for(GL=0; GL<=MaxGL; GL++){ 
	    SphB[GL][0] *= 0.5;
	    SphB[GL][OneD_Grid] *= 0.5;
	  }

	  /* loof for L */

	  for (L=1; L<=Spe_Num_RVPS[spe]; L++){

	    GL = Spe_VPS_List[spe][L];

	    /****************************************************
                      \int jL(k*r)*RL*r^2 dr 
	    ****************************************************/

	    sum[0] = 0.0;
	    sum[1] = 0.0;

	    for (i=0; i<=OneD_Grid; i++){
	      r = rmin + (double)i*h;
	      for (so=0; so<=VPS_j_dependency[spe]; so++){
		sum[so] += Nonlocal_RadialF(spe,L-1,so,r)*SphB[GL][i];
	      }
	    }

	    for (so=0; so<=VPS_j_dependency[spe]; so++){
	      Spe_NLRF_Bessel[so][spe][L][kj] = sum[so]*h;
	    }

	  } /* L */
	} /* kj */

	/* free arrays */

	for(GL=0; GL<(MaxGL+3); GL++){ 
	  free(SphB[GL]);
	}
	free(SphB);

	free(tmp_SphB);
	free(tmp_SphBp);

#pragma omp flush(Spe_NLRF_Bessel)

      } /* #pragma omp parallel */

    } /* Lspe */

    /****************************************************
     Remedy for MSpeciesNum==0
     generate radial grids in the k-space
    ****************************************************/

    dk = PAO_Nkmax/(double)Ngrid_NormK;
    for (i=0; i<Ngrid_NormK; i++){
      NormK[i] = (double)i*dk;
    }

    /***********************************************************
        sending and receiving of Spe_RF_Bessel by MPI
    ***********************************************************/

    for (ID=0; ID<Num_Procs2; ID++){
      NumSpe = Species_End[ID] - Species_Top[ID] + 1;
      for (Lspe=0; Lspe<NumSpe; Lspe++){
	spe = Species_Top[ID] + Lspe;
	for (so=0; so<=VPS_j_dependency[spe]; so++){
	  for (L=1; L<=Spe_Num_RVPS[spe]; L++){
	    MPI_Bcast(&Spe_NLRF_Bessel[so][spe][L][0],
		      List_YOUSO[15],MPI_DOUBLE,ID,mpi_comm_level1);
	  }
	}
      }
    }

    /***********************************************************
                      save Spe_NLRF_Bessel
    ***********************************************************/

    if (myid==Host_ID){

      sprintf(fileFT,"%s%s_rst/%s.ftnlp",filepath,filename,filename);

      if ((fp = fopen(fileFT,"wb")) != NULL){

	for (spe=0; spe<SpeciesNum; spe++){
  	  for (so=0; so<=VPS_j_dependency[spe]; so++){
	    for (L=1; L<=Spe_Num_RVPS[spe]; L++){
	      fwrite(&Spe_NLRF_Bessel[so][spe][L][0],sizeof(double),List_YOUSO[15],fp);
	    }
	  }
	}  

	fclose(fp);
      }
      else{
	printf("Could not open a file %s in FT_NLP\n",fileFT);
      }
    }

  } /* if (RestartRead_Succeed==0) */

  /***********************************************************
                         elapsed time
  ***********************************************************/

  dtime(&TEtime);

  /*
  printf("myid=%2d Elapsed Time (s) = %15.12f\n",myid,TEtime-TStime);
  MPI_Finalize();
  exit(0);
  */

}