ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
pw_basis_big.h
Go to the documentation of this file.
1#ifndef PW_BASIS_BIG_H
2#define PW_BASIS_BIG_H
3#include "pw_basis_sup.h"
4// temporary class, because previous ABACUS consider big grid for fft grids
5// which are used for grid integration in LCAO.
6// In fact, it is unnecessary. It will be moved after grid integration is refactored.
7namespace ModulePW
8{
9
11{
12public:
13 // combine [bx,by,bz] FFT grids into a big one
14 // typical values are bx=2, by=2, bz=2
15 // nbx=nx/bx, nby=ny/by, nbz=nz/bz,
17 {
18 }
19 PW_Basis_Big(std::string device_, std::string precision_) : PW_Basis_Sup(device_, precision_)
20 {
21 }
22
24 void setbxyz(const int bx_in, const int by_in, const int bz_in)
25 {
26 bx = bx_in;
27 by = by_in;
28 bz = bz_in;
29 bxyz = bx * by * bz;
30 }
31 int bx = 1, by = 1, bz = 1, bxyz = 1;
32 int nbx=0;
33 int nby=0;
34 int nbz=0;
35 int nbzp=0;
36 int nbxx=0;
38
39 void autoset_big_cell_size(int& b_size, const int& nc_size, const int nproc = 0)
40 {
41 //original default setting is 4
42 b_size = 4;
43 //only for bz
44 if(nproc > 0)
45 {
46 int candidate_lists[4] = {4, 3, 5, 2};
47 int max_bz[4];
48 for(int i=0;i<4;i++)
49 {
50 int tmp = candidate_lists[i];
51 max_bz[i] = nc_size / tmp;
52 if(nc_size % tmp!=0)
53 {//ignore candidates which can't be factored by nc_size
54 max_bz[i]=0;
55 continue;
56 }
57 if(max_bz[i] % nproc == 0)
58 {
59 b_size = tmp;
60 return;
61 }
62 }
63
64 //choose maximum residual
65 double res = 0.0;
66 double res_temp = 0.0;
67 for(int i=0;i<4;i++)
68 {
69 if(max_bz[i]==0) continue;
70 res_temp = double(max_bz[i] % nproc) / nproc;
71 if(res < res_temp)
72 {
73 res = res_temp;
74 b_size = candidate_lists[i];
75 }
76 }
77 return;
78 }
79 //for bx and by, choose maximum residual of (5,4,3)
80 else
81 {
82 int res = 0;
83 int res_temp = 0;
84 for(int i=5;i>2;i--)
85 {
86 res_temp = nc_size % i;
87 if(res_temp == 0)
88 {
89 b_size = i;
90 return;
91 }
92 else if(res < res_temp)
93 {
94 res = res_temp;
95 b_size = i;
96 }
97 }
98 return;
99 }
100 }
101
102
103 virtual void initgrids(const double lat0_in,const ModuleBase::Matrix3 latvec_in,
104 const double gridecut){
105 //init lattice
106 this->lat0 = lat0_in;
107 this->latvec = latvec_in;
108 this->omega = std::abs(latvec.Det()) * lat0 * lat0 * lat0;
109 this->GT = latvec.Inverse();
110 this->G = GT.Transpose();
111 this->GGT = G * GT;
112
113 //------------------------------------------------------------
114 //-------------------------init grids-------------------------
115 //------------------------------------------------------------
116 this->tpiba = ModuleBase::TWO_PI / this->lat0;
117 this->tpiba2 = this->tpiba * this->tpiba;
118 this->gridecut_lat = gridecut / tpiba2;
120 int *ibox = new int[3];
121
122 lat.x = latvec.e11;
123 lat.y = latvec.e12;
124 lat.z = latvec.e13;
125 ibox[0] = 2 * int(sqrt(gridecut_lat) * sqrt(lat * lat)) + 1;
126
127 lat.x = latvec.e21;
128 lat.y = latvec.e22;
129 lat.z = latvec.e23;
130 ibox[1] = 2 * int(sqrt(gridecut_lat) * sqrt(lat * lat)) + 1;
131
132 lat.x = latvec.e31;
133 lat.y = latvec.e32;
134 lat.z = latvec.e33;
135 ibox[2] = 2 * int(sqrt(gridecut_lat) * sqrt(lat * lat)) + 1;
136
137 // We should check if ibox is the minimum number to cover the planewave ball.
138 // Find the minimum number of ibox by traveling all possible ibox
139 int n1,n2,n3;
140 n1 = n2 = n3 = 0;
141 for(int igz = -ibox[2]+this->poolrank; igz <= ibox[2]; igz += this->poolnproc)
142 {
143 for(int igy = -ibox[1]; igy <= ibox[1]; ++igy)
144 {
145 for(int igx = -ibox[0]; igx <= ibox[0]; ++igx)
146 {
148 f.x = igx;
149 f.y = igy;
150 f.z = igz;
151 double modulus = f * (this->GGT * f);
152 if(modulus <= this->gridecut_lat)
153 {
154 if(n1 < std::abs(igx)) n1 = std::abs(igx);
155 if(n2 < std::abs(igy)) n2 = std::abs(igy);
156 if(n3 < std::abs(igz)) n3 = std::abs(igz);
157 }
158 }
159 }
160 }
161 ibox[0] = 2*n1+1;
162 ibox[1] = 2*n2+1;
163 ibox[2] = 2*n3+1;
164#ifdef __MPI
165 MPI_Allreduce(MPI_IN_PLACE, ibox, 3, MPI_INT, MPI_MAX , this->pool_world);
166#endif
167
168 // Find the minimal FFT box size the factors into the primes (2,3,5,7).
169 for (int i = 0; i < 3; i++)
170 {
171 int b = 0;
172 int n2 = 0;
173 int n3 = 0;
174 int n5 = 0;
175 //int n7 = 0;
176 bool done_factoring = false;
177
178 // increase ibox[i] by 1 until it is totally factorizable by (2,3,5,7)
179 do
180 {
181 b = ibox[i];
182
183 //n2 = n3 = n5 = n7 = 0;
184 n2 = n3 = n5 = 0;
185 done_factoring = false;
186 if ((this->full_pw && this->full_pw_dim == 2) && b % 2 != 0) done_factoring = true; // full_pw_dim = 2 means FFT dimensions should be even.
187 while (!done_factoring)
188 {
189 if (b % 2 == 0 && (!this->full_pw || this->full_pw_dim != 1)) // full_pw_dim = 1 means FFT dimension should be odd.
190 {
191 n2++;
192 b /= 2;
193 continue;
194 }
195 if (b % 3 == 0)
196 {
197 n3++;
198 b /= 3;
199 continue;
200 }
201 if (b % 5 == 0)
202 {
203 n5++;
204 b /= 5;
205 continue;
206 }
207 //if (b%7==0) { n7++; b /= 7; continue; }
208 done_factoring = true;
209 }
210 ibox[i] += 1;
211 }
212 while (b != 1);
213 ibox[i] -= 1;
214 // b==1 means fftbox[i] is (2,3,5,7) factorizable
215 }
216 //autoset bx/by/bz if not set in INPUT
217 if(!this->bz)
218 {
219 this->autoset_big_cell_size(this->bz, ibox[2], this->poolnproc);
220 }
221 if(!this->bx)
222 {
223 //if cz == cx, autoset bx==bz for keeping same symmetry
224 if(ibox[0] == ibox[2])
225 {
226 this->bx = this->bz;
227 }
228 else
229 {
230 this->autoset_big_cell_size(this->bx, ibox[0]);
231 }
232 }
233 if(!this->by)
234 {
235 //if cz == cy, autoset by==bz for keeping same symmetry
236 if(ibox[1] == ibox[2])
237 {
238 this->by = this->bz;
239 }
240 else
241 {
242 this->autoset_big_cell_size(this->by, ibox[1]);
243 }
244 }
245 this->bxyz = this->bx * this->by * this->bz;
246 if(ibox[0]%this->bx != 0) ibox[0] += (this->bx - ibox[0] % this->bx);
247 if(ibox[1]%this->by != 0) ibox[1] += (this->by - ibox[1] % this->by);
248 if(ibox[2]%this->bz != 0) ibox[2] += (this->bz - ibox[2] % this->bz);
249
250 this->nx = ibox[0];
251 this->ny = ibox[1];
252 this->nz = ibox[2];
253 this->nxy =this->nx * this->ny;
254 this->nxyz = this->nxy * this->nz;
255 this->nbx = this->nx / bx;
256 this->nby = this->ny / by;
257 this->nbz = this->nz / bz;
258
259 delete[] ibox;
260 return;
261
262 }
263
264 virtual void initgrids(
265 const double lat0_in,
266 const ModuleBase::Matrix3 latvec_in, // Unitcell lattice vectors
267 const int nx_in, int ny_in, int nz_in
268 )
269 {
270 this->lat0 = lat0_in;
271 this->tpiba = ModuleBase::TWO_PI / this->lat0;
272 this->tpiba2 = this->tpiba*this->tpiba;
273 this->latvec = latvec_in;
274 this->omega = std::abs(latvec.Det()) * lat0 * lat0 * lat0;
275 this->GT = latvec.Inverse();
276 this->G = GT.Transpose();
277 this->GGT = G * GT;
278 this->nx = nx_in;
279 this->ny = ny_in;
280 this->nz = nz_in;
281 // autoset bx/by/bz if not set in INPUT
282 if (!this->bz)
283 {
284 this->autoset_big_cell_size(this->bz, nz, this->poolnproc);
285 }
286 if (!this->bx)
287 {
288 // if cz == cx, autoset bx==bz for keeping same symmetry
289 if (nx == nz)
290 {
291 this->bx = this->bz;
292 }
293 else
294 {
295 this->autoset_big_cell_size(this->bx, nx);
296 }
297 }
298 if (!this->by)
299 {
300 // if cz == cy, autoset by==bz for keeping same symmetry
301 if (ny == nz)
302 {
303 this->by = this->bz;
304 }
305 else
306 {
307 this->autoset_big_cell_size(this->by, ny);
308 }
309 }
310 this->bxyz = this->bx * this->by * this->bz;
311 if(this->nx%this->bx != 0) this->nx += (this->bx - this->nx % this->bx);
312 if(this->ny%this->by != 0) this->ny += (this->by - this->ny % this->by);
313 if(this->nz%this->bz != 0) this->nz += (this->bz - this->nz % this->bz);
314 this->nbx = this->nx / bx;
315 this->nby = this->ny / by;
316 this->nbz = this->nz / bz;
317 this->nxy = this->nx * this->ny;
318 this->nxyz = this->nxy * this->nz;
319
320 int *ibox = new int[3];
321 ibox[0] = int((this->nx-1)/2)+1;
322 ibox[1] = int((this->ny-1)/2)+1;
323 ibox[2] = int((this->nz-1)/2)+1;
324 this->gridecut_lat = 1e20;
325 int count = 0;
326 for(int igz = -ibox[2]; igz <= ibox[2]; ++igz)
327 {
328 for(int igy = -ibox[1]; igy <= ibox[1]; ++igy)
329 {
330 for(int igx = -ibox[0]; igx <= ibox[0]; ++igx)
331 {
332 ++count;
333 if(count%this->poolnproc != this->poolrank) continue;
334 if(std::abs(igx)<=ibox[0]-1 && std::abs(igy)<=ibox[1]-1 && std::abs(igz)<=ibox[2]-1 ) continue;
336 f.x = igx;
337 f.y = igy;
338 f.z = igz;
339 double modulus = f * (this->GGT * f);
340 if(modulus < this->gridecut_lat)
341 {
342 this->gridecut_lat = modulus;
343 }
344 }
345 }
346 }
347#ifdef __MPI
348 MPI_Allreduce(MPI_IN_PLACE, &this->gridecut_lat, 1, MPI_DOUBLE, MPI_MIN , this->pool_world);
349#endif
350 this->gridecut_lat -= 1e-6;
351
352 delete[] ibox;
353
354 return;
355 }
356
357 virtual void distribute_r()
358 {
359 delete[] this->numz; this->numz = new int[this->poolnproc];
360 delete[] this->startz; this->startz = new int[this->poolnproc];
363
364 int npbz = this->nbz / this->poolnproc;
365 int modbz = this->nbz % this->poolnproc;
366 this->startz[0] = 0;
367 for(int ip = 0 ; ip < this->poolnproc ; ++ip)
368 {
369 this->numz[ip] = npbz*this->bz;
370 if(ip < modbz) this->numz[ip]+=this->bz;
371 if(ip < this->poolnproc - 1) this->startz[ip+1] = this->startz[ip] + numz[ip];
372 if(ip == this->poolrank)
373 {
374 this->nplane = numz[ip];
375 this->startz_current = startz[ip];
376 }
377 }
378 this->nbzp = this->nplane / this->bz;
379 this->nrxx = this->numz[this->poolrank] * this->nxy;
380 this->nbxx = this->nbzp * this->nbx * this->nby;
381 this->nbzp_start = this->startz[this->poolrank] / this->bz;
382 return;
383 }
384
385};
386}
387#endif
3x3 matrix and related mathamatical operations
Definition matrix3.h:15
3 elements vector
Definition vector3.h:21
T x
Definition vector3.h:23
T y
Definition vector3.h:24
T z
Definition vector3.h:25
void autoset_big_cell_size(int &b_size, const int &nc_size, const int nproc=0)
Definition pw_basis_big.h:39
int nbz
Definition pw_basis_big.h:34
int bx
Definition pw_basis_big.h:31
virtual void initgrids(const double lat0_in, const ModuleBase::Matrix3 latvec_in, const double gridecut)
Definition pw_basis_big.h:103
int nbx
Definition pw_basis_big.h:32
~PW_Basis_Big()
Definition pw_basis_big.h:23
virtual void distribute_r()
distribute real-space grids to different processors
Definition pw_basis_big.h:357
PW_Basis_Big()
Definition pw_basis_big.h:16
int nbzp_start
Definition pw_basis_big.h:37
PW_Basis_Big(std::string device_, std::string precision_)
Definition pw_basis_big.h:19
int by
Definition pw_basis_big.h:31
int nbxx
Definition pw_basis_big.h:36
int nby
Definition pw_basis_big.h:33
void setbxyz(const int bx_in, const int by_in, const int bz_in)
Definition pw_basis_big.h:24
int bz
Definition pw_basis_big.h:31
virtual void initgrids(const double lat0_in, const ModuleBase::Matrix3 latvec_in, const int nx_in, int ny_in, int nz_in)
Definition pw_basis_big.h:264
int bxyz
Definition pw_basis_big.h:31
int nbzp
Definition pw_basis_big.h:35
PW_Basis_Sup()
Definition pw_basis_sup.h:25
int nrxx
Definition pw_basis.h:127
double omega
volume of the cell
Definition pw_basis.h:185
int nx
Definition pw_basis.h:246
int * numz
Definition pw_basis.h:129
ModuleBase::Matrix3 GT
traspose of G
Definition pw_basis.h:183
int nxy
Definition pw_basis.h:246
double tpiba
2pi/lat0
Definition pw_basis.h:179
int poolnproc
Definition pw_basis.h:189
int ny
Definition pw_basis.h:246
int startz_current
Definition pw_basis.h:134
double lat0
unit length for lattice, unit in bohr
Definition pw_basis.h:178
int poolrank
Definition pw_basis.h:190
int nplane
Definition pw_basis.h:135
int full_pw_dim
Definition pw_basis.h:187
ModuleBase::Matrix3 latvec
Unitcell lattice vectors, unit in lat0.
Definition pw_basis.h:181
MPI_Comm pool_world
Definition pw_basis.h:110
ModuleBase::Matrix3 G
reciprocal lattice vector, unit in 1/lat0
Definition pw_basis.h:182
int nxyz
Definition pw_basis.h:246
bool full_pw
Definition pw_basis.h:174
double tpiba2
4pi^2/lat0^2
Definition pw_basis.h:180
int * startz
Definition pw_basis.h:128
ModuleBase::Matrix3 GGT
GGT = G*GT.
Definition pw_basis.h:184
double gridecut_lat
Energy cut off for all fft grids = ecutrho(Ry)*lat0^2/4pi^2, unit in 1/lat0^2.
Definition pw_basis.h:177
int nz
Definition pw_basis.h:246
void ZEROS(std::complex< T > *u, const TI n)
Definition global_function.h:109
const double TWO_PI
Definition constants.h:21
Definition pw_op.cpp:3