ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
pw_basis.h
Go to the documentation of this file.
1#ifndef PWBASIS_H
2#define PWBASIS_H
3
10#include "source_base/vector3.h"
11#include <complex>
13#include <cstring>
14#ifdef __MPI
15#include "mpi.h"
16#endif
17
18class PWBasisTEST; // unit-test fixture, see module_pw/test_serial/
19
20namespace ModulePW
21{
22
60{
63 friend class ::PWBasisTEST;
64
65public:
66 std::string classname;
67 PW_Basis();
68 PW_Basis(std::string device_, std::string precision_);
69 virtual ~PW_Basis();
70 //Init mpi parameters
71#ifdef __MPI
72 void initmpi(
73 const int poolnproc_in, // Number of processors in this pool
74 const int poolrank_in, // Rank in this pool
75 MPI_Comm pool_world_in //Comm world for pw_basis
76 );
77#endif
78
79 //Init the grids for FFT
80 virtual void initgrids(
81 const double lat0_in, //unit length (unit in bohr)
82 const ModuleBase::Matrix3 latvec_in, // Unitcell lattice vectors (unit in lat0)
83 const double gridecut //unit in Ry, ecut to set up grids
84 );
85 //Init the grids for FFT
86 virtual void initgrids(
87 const double lat0_in,
88 const ModuleBase::Matrix3 latvec_in, // Unitcell lattice vectors
89 const int nx_in, int ny_in, int nz_in
90 );
91
92 //Init some parameters
93 void initparameters(
94 const bool gamma_only_in,
95 const double pwecut_in, //unit in Ry, ecut to decides plane waves
96 const int distribution_type_in = 1,
97 const bool xprime_in = true
98 );
99
100 //Set parameters about full planewave, only used in OFDFT for now.
101 void setfullpw(
102 const bool inpt_full_pw = false,
103 const int inpt_full_pw_dim = 0
104 );
105//===============================================
106// distribution maps
107//===============================================
108public:
109#ifdef __MPI
110 MPI_Comm pool_world=MPI_COMM_NULL;
111#endif
112
113 int *ig2isz=nullptr; // map ig to (is, iz).
114 int *ig2ixyz_gpu = nullptr;
115 int *istot2ixy=nullptr; // istot2ixy[is]: iy + ix * ny of is^th stick among all sticks.
116 int *is2fftixy=nullptr, * d_is2fftixy = nullptr; // is2fftixy[is]: iy + ix * ny of is^th stick among sticks on current proc.
117 int *fftixy2ip=nullptr; // fftixy2ip[iy + ix * fftny]: ip of proc which contains stick on (ix, iy). if no stick: -1
118 int nst=0; //num. of sticks in current proc.
119 int *nst_per=nullptr;// nst on each core
120 int nstnz=0; // nst * nz
121 int nstot=0; //num. of sticks in total.
122 int npw=0; //num. of plane waves in current proc.
123 int *npw_per=nullptr; //npw on each core
124 int npwtot=0; // total num. of plane waves in all proc. in this pool
125
126 //real space
127 int nrxx=0; //num. of real space grids
128 int *startz=nullptr; //startz[ip]: starting z plane in the ip-th proc. in current POOL_WORLD
129 int *numz=nullptr; //numz[ip]: num. of z planes in the ip-th proc. in current POOL_WORLD
130 int *numg=nullptr; //numg[ip] : nst_per[poolrank] * numz[ip]
131 int *numr=nullptr; //numr[ip] : numz[poolrank] * nst_per[ip]
132 int *startg=nullptr; // startg[ip] = numg[ip-1] + startg[ip-1]
133 int *startr=nullptr; // startr[ip] = numr[ip-1] + startr[ip-1]
135 int nplane=0; //num. of planes in current proc.
136
137 ModuleBase::Vector3<double> *gdirect=nullptr; //(= *G1d) ; // ig = new Vector igc[npw]
138 ModuleBase::Vector3<double> *gcar=nullptr; //G vectors in cartesian corrdinate
139 double *gg=nullptr; // modulus (G^2) of G vectors [npw]
140 //gg[ng]=ig[ng]*GGT*ig[ng]/(lat0*lat0)=g[ng]*g[ng] (/lat0*lat0)
141 // gg_global dimension: [cutgg_num_now] (save memory skill is used)
142 int ig_gge0=-1; //ig when gg == 0
143
144 //distribute plane waves and grids and set up fft
145 void setuptransform();
146
147protected:
148 int *startnsz_per=nullptr;//useless intermediate variable// startnsz_per[ip]: starting is * nz stick in the ip^th proc.
149
150 //distribute plane waves to different processors
151 void distribute_g();
152
153 //distribute real-space grids to different processors
154 virtual void distribute_r();
155
156 //prepare for MPI_Alltoall
157 void getstartgr();
158
159
160public:
161 //collect gdirect, gcar, gg
162 void collect_local_pw();
163
164public:
165 int ngg=0; //number of different modulus (G^2) of G vectors
166 int *ig2igg=nullptr;//[npw] map ig to igg(<ngg: the index of G^2)
167 double *gg_uniq=nullptr; //[ngg] modulus (G^2) of G vectors of igg, each gg of igg is unique.
168 //collect gg_uniq
169 void collect_uniqgg();
170
171
172public:
173 bool gamma_only = false;
174 bool full_pw = false;
176 double ggecut = 0.0;
177 double gridecut_lat = 0.0;
178 double lat0 = 1;
179 double tpiba = 1;
180 double tpiba2 = 1;
185 double omega = 1.0;
187 int full_pw_dim = 0;
189 int poolnproc = 1;
190 int poolrank = 0;
191
192protected:
193 //distribute plane waves to different processors
194 //method 1: first consider number of plane waves
196 // Distribute sticks to cores in method 1.
197 void divide_sticks_1(
198 int* st_i, // x or x + fftnx (if x < 0) of stick.
199 int* st_j, // y or y + fftny (if y < 0) of stick.
200 int* st_length // the stick on (ix, iy) consists of st_length[ix*fftny+iy] planewaves.
201 );
202
203 //method 2: first consider number of sticks
205 // Distribute sticks to cores in method 2.
206 void divide_sticks_2();
207
208 //Count the total number of planewaves (tot_npw) and sticks (this->nstot) (in distributeg method1 and method2)
209 void count_pw_st(
210 int* st_length2D, // the number of planewaves that belong to the stick located on (x, y).
211 int* st_bottom2D // the z-coordinate of the bottom of stick on (x, y).
212 );
213
214 //get ig2isz and is2fftixy
216 int* st_bottom, // minimum z of stick, stored in 1d array with tot_nst elements.
217 int* st_length // the stick on (x, y) consists of st_length[x*fftny+y] planewaves.
218 );
219
220 //Collect the x, y indexs, length of the sticks (in distributeg method1)
221 void collect_st(
222 int* st_length2D, // the number of planewaves that belong to the stick located on (x, y), stored in 2d x-y plane.
223 int* st_bottom2D, // the z-coordinate of the bottom of stick on (x, y), stored in 2d x-y plane.
224 int* st_i, // x or x + fftnx (if x < 0) of stick.
225 int* st_j, // y or y + fftny (if y < 0) of stick.
226 int* st_length // number of planewaves in stick, stored in 1d array with tot_nst elements.
227 );
228
229 //get istot2ixy
230 void get_istot2ixy(
231 int* st_i, // x or x + fftnx (if x < 0) of stick.
232 int* st_j // y or y + fftny (if y < 0) of stick.
233 );
234
235 //Create the maps from ixy to (in method 2)
236 void create_maps(
237 int* st_length2D // the number of planewaves that belong to the stick located on (x, y), stored in 2d x-y plane.
238 );
239
240//===============================================
241// FFT
242//===============================================
243public:
244 // FFT dimensions for wave functions.
245 int fftnx=0, fftny=0, fftnz=0, fftnxyz=0, fftnxy=0;
246 int nx=0, ny=0, nz=0, nxyz=0, nxy=0; // Gamma_only: fftny = int(ny/2)-1 , others: fftny = ny
247 int liy=0, riy=0;// liy: the left edge of the pw ball; riy: the right edge of the pw ball in the y direction
248 int lix=0, rix=0;// lix: the left edge of the pw ball; rix: the right edge of the pw ball in the x direction
249 bool xprime = true; // true: when do recip2real, x-fft will be done last and when doing real2recip, x-fft will be
250 // done first; false: y-fft For gamma_only, true: we use half x; false: we use half y
251 int ng_xeq0 = 0; //only used when xprime = true, number of g whose gx = 0
252 int nmaxgr = 0; // Gamma_only: max between npw and (nrxx+1)/2, others: max between npw and nrxx
253 // Thus std::complex<double>[nmaxgr] is able to contain either reciprocal or real data
254 // FFT ft;
256 //The position of pointer in and out can be equal(in-place transform) or different(out-of-place transform).
257
258 template <typename FPTYPE>
259 void real2recip(const FPTYPE* in,
260 std::complex<FPTYPE>* out,
261 const bool add = false,
262 const FPTYPE factor = 1.0) const; // in:(nplane,nx*ny) ; out(nz, ns)
263 template <typename FPTYPE>
264 void real2recip(const std::complex<FPTYPE>* in,
265 std::complex<FPTYPE>* out,
266 const bool add = false,
267 const FPTYPE factor = 1.0) const; // in:(nplane,nx*ny) ; out(nz, ns)
268 template <typename FPTYPE>
269 void recip2real(const std::complex<FPTYPE>* in,
270 FPTYPE* out,
271 const bool add = false,
272 const FPTYPE factor = 1.0) const; // in:(nz, ns) ; out(nplane,nx*ny)
273 template <typename FPTYPE>
274 void recip2real(const std::complex<FPTYPE>* in,
275 std::complex<FPTYPE>* out,
276 const bool add = false,
277 const FPTYPE factor = 1.0) const; // in:(nz, ns) ; out(nplane,nx*ny)
278
279 template <typename FPTYPE>
280 void real2recip_gpu(const FPTYPE* in,
281 std::complex<FPTYPE>* out,
282 const bool add = false,
283 const FPTYPE factor = 1.0) const; // in:(nplane,nx*ny) ; out(nz, ns)
284 template <typename FPTYPE>
285 void real2recip_gpu(const std::complex<FPTYPE>* in,
286 std::complex<FPTYPE>* out,
287 const bool add = false,
288 const FPTYPE factor = 1.0) const; // in:(nplane,nx*ny) ; out(nz, ns)
289 template <typename FPTYPE>
290 void recip2real_gpu(const std::complex<FPTYPE>* in,
291 FPTYPE* out,
292 const bool add = false,
293 const FPTYPE factor = 1.0) const; // in:(nz, ns) ; out(nplane,nx*ny)
294 template <typename FPTYPE>
295 void recip2real_gpu(const std::complex<FPTYPE>* in,
296 std::complex<FPTYPE>* out,
297 const bool add = false,
298 const FPTYPE factor = 1.0) const; // in:(nz, ns) ; out(nplane,nx*ny)
299
320 template <typename TK,
321 typename TR,
322 typename Device,
323 typename std::enable_if<!std::is_same<TK, typename GetTypeReal<TK>::type>::value
324 && (std::is_same<TR, typename GetTypeReal<TK>::type>::value
325 || std::is_same<TR, TK>::value)
326 && std::is_same<Device, base_device::DEVICE_CPU>::value,
327 int>::type
328 = 0>
329 void recip_to_real(TK* in, TR* out, const bool add = false, const typename GetTypeReal<TK>::type factor = 1.0) const
330 {
331 this->recip2real(in, out, add, factor);
332 };
333
351 template <typename TK,
352 typename TR,
353 typename Device,
354 typename std::enable_if<!std::is_same<TK, typename GetTypeReal<TK>::type>::value
355 && (std::is_same<TR, typename GetTypeReal<TK>::type>::value
356 || std::is_same<TR, TK>::value)
357 && std::is_same<Device, base_device::DEVICE_GPU>::value,
358 int>::type
359 = 0>
360 void recip_to_real(TK* in,
361 TR* out,
362 const bool add = false,
363 const typename GetTypeReal<TK>::type factor = 1.0) const
364 {
365 this->recip2real_gpu(in,out,add,factor);
366 };
367
368 // template <typename FPTYPE,
369 // typename Device,
370 // typename std::enable_if<!std::is_same<FPTYPE, typename GetTypeReal<FPTYPE>::type>::value, int>::type = 0>
371 // void recip_to_real(FPTYPE* in,
372 // FPTYPE* out,
373 // const bool add = false,
374 // const typename GetTypeReal<FPTYPE>::type factor = 1.0) const;
397 template <typename TR,typename TK,typename Device,
398 typename std::enable_if<!std::is_same<TK, typename GetTypeReal<TK>::type>::value
399 && (std::is_same<TR, typename GetTypeReal<TK>::type>::value || std::is_same<TR, TK>::value)
400 && std::is_same<Device, base_device::DEVICE_CPU>::value ,int>::type = 0>
401 void real_to_recip(TR* in,
402 TK* out,
403 const bool add = false,
404 const typename GetTypeReal<TK>::type factor = 1.0) const
405 {
406 this->real2recip(in, out, add, factor);
407 }
408
409 template <typename TR, typename TK, typename Device,
410 typename std::enable_if<!std::is_same<TK, typename GetTypeReal<TK>::type>::value
411 && (std::is_same<TR, typename GetTypeReal<TK>::type>::value || std::is_same<TR, TK>::value)
412 && std::is_same<Device, base_device::DEVICE_GPU>::value ,int>::type = 0>
413 void real_to_recip(TR* in,
414 TK* out,
415 const bool add = false,
416 const typename GetTypeReal<TK>::type factor = 1.0) const
417 {
418 this->real2recip_gpu(in,out,add,factor);
419 };
420
421 protected:
422 //gather planes and scatter sticks of all processors
423 template <typename T>
424 void gatherp_scatters(std::complex<T>* in, std::complex<T>* out) const;
425
426 // gather sticks of and scatter planes of all processors
427 template <typename T>
428 void gathers_scatterp(std::complex<T>* in, std::complex<T>* out) const;
429
430 public:
431 //get fftixy2is;
432 void getfftixy2is(int * fftixy2is) const;
433
437 // using default_device_cpu = base_device::DEVICE_CPU;
438
439 void set_device(std::string device_);
440 void set_precision(std::string precision_);
441
442 std::string get_device() const { return device; }
443 std::string get_precision() const { return precision; }
444
445protected:
446
447 std::string device = "cpu";
448 std::string precision = "double";
449 bool double_data_ = true;
450 bool float_data_ = false;
451};
452}
453#endif // PWBASIS_H
Definition fft_bundle.h:11
3x3 matrix and related mathamatical operations
Definition matrix3.h:15
3 elements vector
Definition vector3.h:21
void recip2real_gpu(const std::complex< FPTYPE > *in, std::complex< FPTYPE > *out, const bool add=false, const FPTYPE factor=1.0) const
int nrxx
Definition pw_basis.h:127
void getfftixy2is(int *fftixy2is) const
Definition pw_basis.cpp:274
int nst
Definition pw_basis.h:118
int nmaxgr
Definition pw_basis.h:252
void setfullpw(const bool inpt_full_pw=false, const int inpt_full_pw_dim=0)
Definition pw_init.cpp:246
int nstot
Definition pw_basis.h:121
double omega
volume of the cell
Definition pw_basis.h:185
void real2recip(const FPTYPE *in, std::complex< FPTYPE > *out, const bool add=false, const FPTYPE factor=1.0) const
Transform real-valued real-space data to reciprocal-space (gamma-only or non-gamma).
Definition pw_transform.cpp:150
void real_to_recip(TR *in, TK *out, const bool add=false, const typename GetTypeReal< TK >::type factor=1.0) const
Converts data from real space to reciprocal space (Fourier space).
Definition pw_basis.h:401
int riy
Definition pw_basis.h:247
int lix
Definition pw_basis.h:248
bool xprime
Definition pw_basis.h:249
int nx
Definition pw_basis.h:246
int * numz
Definition pw_basis.h:129
std::string precision
single, double, mixing
Definition pw_basis.h:448
void set_device(std::string device_)
Definition pw_basis.cpp:296
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 * ig2ixyz_gpu
Definition pw_basis.h:114
int liy
Definition pw_basis.h:247
int poolnproc
Definition pw_basis.h:189
void gatherp_scatters(std::complex< T > *in, std::complex< T > *out) const
gather planes and scatter sticks
Definition pw_gatherscatter.h:55
int * startr
Definition pw_basis.h:133
void distribution_method1()
Definition pw_distg_method1.cpp:25
int rix
Definition pw_basis.h:248
int * startg
Definition pw_basis.h:132
bool double_data_
if has double data
Definition pw_basis.h:449
void count_pw_st(int *st_length2D, int *st_bottom2D)
(1) We count the total number of planewaves (tot_npw) and sticks (this->nstot) here.
Definition pw_distributeg.cpp:43
int npw
Definition pw_basis.h:122
bool gamma_only
only half g are used.
Definition pw_basis.h:173
std::string device
cpu or gpu
Definition pw_basis.h:447
double ggecut
Energy cut off for g^2/2 = ecutwfc(Ry)*lat0^2/4pi^2, unit in 1/lat0^2.
Definition pw_basis.h:176
int * d_is2fftixy
Definition pw_basis.h:116
double * gg_uniq
Definition pw_basis.h:167
PW_Basis()
Definition pw_basis.cpp:11
ModuleBase::Vector3< double > * gdirect
Definition pw_basis.h:137
int * startnsz_per
Definition pw_basis.h:148
void initmpi(const int poolnproc_in, const int poolrank_in, MPI_Comm pool_world_in)
Definition pw_init.cpp:7
int * numr
Definition pw_basis.h:131
void divide_sticks_1(int *st_i, int *st_j, int *st_length)
(3-1) Distribute sticks to cores according to the number of plane waves.
Definition pw_distg_method1.cpp:261
int * istot2ixy
Definition pw_basis.h:115
void create_maps(int *st_length2D)
(3) Create the maps from ixy to ip, istot, and from istot to ixy, and construt npw_per simultaneously...
Definition pw_distg_method2.cpp:142
int fftnxy
Definition pw_basis.h:245
int ny
Definition pw_basis.h:246
int fftny
Definition pw_basis.h:245
void divide_sticks_2()
(2) Devide the sticks to each core according to the number of sticks Sticks are in the order of ixy i...
Definition pw_distg_method2.cpp:120
int fftnz
Definition pw_basis.h:245
void distribution_method2()
Definition pw_distg_method2.cpp:25
void set_precision(std::string precision_)
Definition pw_basis.cpp:300
void get_ig2isz_is2fftixy(int *st_bottom, int *st_length)
(5) Construct ig2isz, and is2fftixy.
Definition pw_distributeg.cpp:171
int startz_current
Definition pw_basis.h:134
virtual void distribute_r()
distribute real-space grids to different processors
Definition pw_distributer.cpp:11
virtual void initgrids(const double lat0_in, const ModuleBase::Matrix3 latvec_in, const double gridecut)
Definition pw_init.cpp:23
void distribute_g()
distribute plane waves to different cores
Definition pw_distributeg.cpp:12
double lat0
unit length for lattice, unit in bohr
Definition pw_basis.h:178
int poolrank
Definition pw_basis.h:190
std::string classname
Definition pw_basis.h:66
int ngg
Definition pw_basis.h:165
std::string get_precision() const
Definition pw_basis.h:443
void initparameters(const bool gamma_only_in, const double pwecut_in, const int distribution_type_in=1, const bool xprime_in=true)
Definition pw_init.cpp:213
int * nst_per
Definition pw_basis.h:119
base_device::memory::resize_memory_op< int, base_device::DEVICE_GPU > resmem_int_op
Definition pw_basis.h:434
int ng_xeq0
Definition pw_basis.h:251
base_device::memory::delete_memory_op< int, base_device::DEVICE_GPU > delmem_int_op
Definition pw_basis.h:435
int fftnx
Definition pw_basis.h:245
int * npw_per
Definition pw_basis.h:123
int nplane
Definition pw_basis.h:135
std::string get_device() const
Definition pw_basis.h:442
void collect_uniqgg()
Definition pw_basis.cpp:193
int full_pw_dim
Definition pw_basis.h:187
void real2recip_gpu(const std::complex< FPTYPE > *in, std::complex< FPTYPE > *out, const bool add=false, const FPTYPE factor=1.0) const
void real2recip_gpu(const FPTYPE *in, std::complex< FPTYPE > *out, const bool add=false, const FPTYPE factor=1.0) const
ModuleBase::FFT_Bundle fft_bundle
Definition pw_basis.h:255
int fftnxyz
Definition pw_basis.h:245
ModuleBase::Matrix3 latvec
Unitcell lattice vectors, unit in lat0.
Definition pw_basis.h:181
void recip2real_gpu(const std::complex< FPTYPE > *in, FPTYPE *out, const bool add=false, const FPTYPE factor=1.0) const
MPI_Comm pool_world
Definition pw_basis.h:110
ModuleBase::Matrix3 G
reciprocal lattice vector, unit in 1/lat0
Definition pw_basis.h:182
void recip2real(const std::complex< FPTYPE > *in, FPTYPE *out, const bool add=false, const FPTYPE factor=1.0) const
Transform reciprocal-space to real-valued real-space (gamma-only or non-gamma).
Definition pw_transform.cpp:381
void gathers_scatterp(std::complex< T > *in, std::complex< T > *out) const
gather sticks and scatter planes
Definition pw_gatherscatter.h:149
int nxyz
Definition pw_basis.h:246
int distribution_type
distribution method
Definition pw_basis.h:186
bool full_pw
Definition pw_basis.h:174
int * numg
Definition pw_basis.h:130
double tpiba2
4pi^2/lat0^2
Definition pw_basis.h:180
virtual ~PW_Basis()
Definition pw_basis.cpp:23
void get_istot2ixy(int *st_i, int *st_j)
(3-2) Rearrange sticks in the order of the ip of core increasing, in each core, sticks are sorted in ...
Definition pw_distg_method1.cpp:317
int * ig2igg
Definition pw_basis.h:166
void collect_local_pw()
Definition pw_basis.cpp:135
double * gg
Definition pw_basis.h:139
int * is2fftixy
Definition pw_basis.h:116
int ig_gge0
Definition pw_basis.h:142
int nstnz
Definition pw_basis.h:120
int * startz
Definition pw_basis.h:128
ModuleBase::Matrix3 GGT
GGT = G*GT.
Definition pw_basis.h:184
bool float_data_
if has float data
Definition pw_basis.h:450
int * fftixy2ip
Definition pw_basis.h:117
base_device::memory::synchronize_memory_op< int, base_device::DEVICE_GPU, base_device::DEVICE_CPU > syncmem_int_h2d_op
Definition pw_basis.h:436
void collect_st(int *st_length2D, int *st_bottom2D, int *st_i, int *st_j, int *st_length)
(2) Collect the x, y indexs, length of the sticks.
Definition pw_distg_method1.cpp:131
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 npwtot
Definition pw_basis.h:124
int * ig2isz
Definition pw_basis.h:113
int nz
Definition pw_basis.h:246
void setuptransform()
Definition pw_basis.cpp:56
void getstartgr()
Definition pw_basis.cpp:76
ModuleBase::Vector3< double > * gcar
Definition pw_basis.h:138
void recip_to_real(TK *in, TR *out, const bool add=false, const typename GetTypeReal< TK >::type factor=1.0) const
Converts data from reciprocal space to real space on Cpu.
Definition pw_basis.h:329
Definition pw_basis_test.cpp:42
Definition pw_op.cpp:3
T type
Definition macros.h:8
Definition memory_op.h:115
Definition memory_op.h:17