JDFTx  1.2.0
GridInfo.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------
2 Copyright 2011 Ravishankar Sundararaman
3 
4 This file is part of JDFTx.
5 
6 JDFTx is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 JDFTx is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with JDFTx. If not, see <http://www.gnu.org/licenses/>.
18 -------------------------------------------------------------------*/
19 
20 #ifndef JDFTX_CORE_GRIDINFO_H
21 #define JDFTX_CORE_GRIDINFO_H
22 
27 #include <core/matrix3.h>
28 #include <core/ScalarField.h>
29 #include <core/GpuUtil.h>
30 #include <map>
31 #include <fftw3.h>
32 #include <stdint.h>
33 #include <cstdio>
34 #include <mutex>
35 
45 class GridInfo
46 {
47 public:
48 
49  GridInfo();
50  ~GridInfo();
51 
52  void update();
53  void printLattice();
54  void printReciprocalLattice();
55 
57  { Manual,
58  //7 lattice systems specified using sides a,b,c and alpha,beta,gamma
59  Triclinic,
60  Monoclinic,
61  Orthorhombic,
62  Tetragonal,
63  Rhombohedral,
64  Hexagonal,
65  Cubic
66  }
67  latticeType;
68 
69  enum LatticeModification
70  { Simple,
71  BodyCentered,
72  BaseCentered,
73  FaceCentered
74  }
75  latticeModification;
76 
77  double a, b, c, alpha, beta, gamma;
78  void setLatticeVectors();
79 
81 
83  double Gmax;
84  double GmaxRho;
86 
91  void initialize(bool skipHeader=false, const std::vector< matrix3<int> > sym = std::vector< matrix3<int> >(1, matrix3<int>(1,1,1)));
92 
93  double detR;
94  matrix3<> RT, RTR, invR, invRT, invRTR;
95  matrix3<> G, GT, GGT, invGGT;
96 
97  double dV;
98  vector3<> h[3];
99  int nr;
100  int nG;
101 
102  double dGradial;
103  double GmaxSphere;
104  double GmaxGrid;
105  static const double maxAllowedStrain;
106 
107  //Recommended division amongst processes
108  //NOTE: Most operators are independent on each process, this is only a suggested division
109  //for functions that are expensive to evaluate at each grid point; the corresponding client
110  //code is responsible for making sure that the final results are broadcast to all processes
111  int irStart, irStop; //division for real space anf full-G space loops
112  int iGstart, iGstop; //division for half-G space loops
113 
114  //FFT plans:
115  enum PlanType
122  };
123  fftw_plan getPlan(PlanType planType, int nThreads) const; //get an FFTW plan of specified type with specified thread count
124  #ifdef GPU_ENABLED
125  cufftHandle planZ2Z;
126  cufftHandle planD2Z;
127  cufftHandle planZ2D;
128  cufftHandle planZ2Dcompat;
129  #endif
130 
131  //Indexing utilities (inlined for efficiency)
132  inline vector3<int> wrapGcoords(const vector3<int> iG) const
133  { vector3<int> iGwrapped = iG;
134  for(int k=0; k<3; k++)
135  if(iGwrapped[k]<0)
136  iGwrapped[k] += S[k];
137  return iGwrapped;
138  }
139  inline int fullRindex(const vector3<int> iR) const
140  { return iR[2] + S[2]*(iR[1] + S[1]*iR[0]);
141  }
142  inline int fullGindex(const vector3<int> iG) const
143  { return fullRindex(wrapGcoords(iG));
144  }
145  inline int halfGindex(const vector3<int> iG) const
146  { vector3<int> iGwrapped = wrapGcoords(iG);
147  return iGwrapped[2] + (S[2]/2+1)*(iGwrapped[1] + S[1]*iGwrapped[0]);
148  }
149 
150 private:
151  bool initialized;
152  void updateSdependent();
153 
154  //FFTW plans by thread count and type:
155  std::map<std::pair<PlanType,int>,fftw_plan> planCache;
156  static std::mutex planLock; //Global lock since planner routines are not thread safe
157 };
158 
159 #endif //JDFTX_CORE_GRIDINFO_H
void printLattice()
Update the grid information on changing the lattice vectors.
cufftHandle planZ2Z
CUFFT plan for all the complex transforms.
Definition: GridInfo.h:125
int halfGindex(const vector3< int > iG) const
< Index into the half-reduced reciprocal-space box:
Definition: GridInfo.h:145
double gamma
lattice specified by type, modification and standard side lengths, angles in degrees ...
Definition: GridInfo.h:77
Simulation grid descriptor.
Definition: GridInfo.h:45
Real to complex transform.
Definition: GridInfo.h:120
Complex to real transform.
Definition: GridInfo.h:121
cufftHandle planD2Z
CUFFT plan for R -> G.
Definition: GridInfo.h:126
double dV
volume per grid point
Definition: GridInfo.h:97
void setLatticeVectors()
Set lattice vectors based on lattice type, modification and parameters above.
int nr
position space grid count = S[0]*S[1]*S[2]
Definition: GridInfo.h:99
vector3 h[3]
real space sample vectors
Definition: GridInfo.h:98
PlanType
Definition: GridInfo.h:115
double GmaxGrid
recommended maximum G-vector for radial functions for the density grid
Definition: GridInfo.h:104
cufftHandle planZ2D
CUFFT plan for G -> R.
Definition: GridInfo.h:127
double GmaxRho
if non-zero, override the FFT box inscribable sphere radius
Definition: GridInfo.h:84
matrix3 R
directly specified lattice vectors
Definition: GridInfo.h:82
Real and complex scalar fields in real and reciprocal space.
int nG
reciprocal lattice count = S[0]*S[1]*(S[2]/2+1) [on account of using r2c and c2r ffts] ...
Definition: GridInfo.h:100
int fullRindex(const vector3< int > iR) const
< Index into the full real-space box:
Definition: GridInfo.h:139
int fullGindex(const vector3< int > iG) const
< Index into the full reciprocal-space box:
Definition: GridInfo.h:142
vector3< int > S
sample points in each dimension (if 0, will be determined automatically based on Gmax) ...
Definition: GridInfo.h:85
cufftHandle planZ2Dcompat
CUFFT plan for G -> R in FFTW compatibility mode (required when nyquist component is assymetric) ...
Definition: GridInfo.h:128
matrix3 invGGT
various combinations of reciporcal lattice-vectors
Definition: GridInfo.h:95
void initialize(bool skipHeader=false, const std::vector< matrix3< int > > sym=std::vector< matrix3< int > >(1, matrix3< int >(1, 1, 1)))
double GmaxSphere
recommended maximum G-vector for radial functions for the wavefunction sphere
Definition: GridInfo.h:103
Forward complex transform.
Definition: GridInfo.h:116
double detR
cell volume
Definition: GridInfo.h:93
Directly specify R.
Definition: GridInfo.h:57
double dGradial
recommended spacing of radial G functions
Definition: GridInfo.h:102
Forward in-place complex transform.
Definition: GridInfo.h:118
matrix3 invRTR
various combinations of lattice-vectors
Definition: GridInfo.h:94
Inverse in-place complex transform.
Definition: GridInfo.h:119
LatticeType
Print the reciprocal lattice vectors.
Definition: GridInfo.h:56
vector3< int > wrapGcoords(const vector3< int > iG) const
< wrap negative G-indices to the positive side
Definition: GridInfo.h:132
void printReciprocalLattice()
Print the lattice vectors.
vector3 lattScale
Remember lattice scale specified at input (Note R always includes scale, once latt-scale has processe...
Definition: GridInfo.h:80
double Gmax
radius of wavefunction G-sphere, whole density sphere (double the radius) must be inscribable within ...
Definition: GridInfo.h:83
static const double maxAllowedStrain
maximum strain allowed when updating lattice vectors and using update()
Definition: GridInfo.h:105
Inverse complex transform.
Definition: GridInfo.h:117