JDFTx  1.2.0
LatticeUtils.h
1 /*-------------------------------------------------------------------
2 Copyright 2012 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_LATTICEUTILS_H
21 #define JDFTX_CORE_LATTICEUTILS_H
22 
24 
25 #include <core/GridInfo.h>
26 #include <vector>
27 
29 static const double symmThreshold = 1e-4;
30 static const double symmThresholdSq = symmThreshold * symmThreshold;
31 
36 matrix3<> reduceLatticeVectors(const matrix3<>& R,
37  matrix3<int>* transmission=0, matrix3<int>* invTransmission=0);
38 
44 std::vector<matrix3<int>> getSymmetries(const matrix3<>& R,
45  vector3<bool> isTruncated=vector3<bool>(false,false,false),
46  matrix3<>* Rreduced=0, matrix3<int>* transmission=0, matrix3<int>* invTransmission=0);
47 
49 struct Supercell
50 {
51  const GridInfo& gInfo;
52  std::vector<vector3<>> kmesh;
55 
59  { unsigned iReduced;
60  unsigned iSym;
61  int invert;
63  };
64  std::vector<KmeshTransform> kmeshTransform;
65 
68  Supercell(const GridInfo& gInfo,
69  const std::vector<vector3<>>& kmeshReduced,
70  const std::vector<matrix3<int>>& sym, const std::vector<int>& invertList);
71 };
72 
76 std::map<vector3<int>, double> getCellMap(const matrix3<>& R, const matrix3<>& Rsup, string fname=string());
77 
78 //Helper function for PeriodicLookup< vector3<> > used in Supercell::Supercell
79 inline vector3<> getCoord(const vector3<>& pos) { return pos; }
80 
83 template<typename T> class PeriodicLookup
84 { const std::vector<T>& points;
85  vector3<int> S; //lookup mesh sample count
86  std::vector< std::vector<size_t> > indices; //list of indices into points, for each lookup mesh cell
87 
88  inline size_t meshIndex(vector3<int> iv) const
89  { for(int k=0; k<3; k++) //wrap to [0,S)
90  { if(iv[k] < 0) iv[k] += S[k];
91  if(iv[k] >= S[k]) iv[k] -= S[k];
92  }
93  return iv[0]+S[0]*size_t(iv[1]+S[1]*iv[2]);
94  }
95 
96 public:
99  PeriodicLookup(const std::vector<T>& points, matrix3<> metric, size_t nPointsTarget=0) : points(points)
100  { //Set S such that prod(S) ~ nPoints and the sample counts are proportional to dimension length
101  vector3<> Stmp; for(int k=0; k<3; k++) Stmp[k] = sqrt(metric(k,k));
102  Stmp *= pow(std::max(points.size(), nPointsTarget)/(Stmp[0]*Stmp[1]*Stmp[2]), 1./3); //normalize so that product is nPoints
103  for(int k=0; k<3; k++)
104  { S[k] = std::max(1, int(round(Stmp[k])));
105  assert(symmThreshold*S[k] < 0.5);
106  }
107  //Initialize indices:
108  indices.resize(S[0]*S[1]*S[2]);
109  for(size_t iPoint=0; iPoint<points.size(); iPoint++)
110  addPoint(iPoint, points[iPoint]);
111  }
112 
115  void addPoint(size_t iPoint, const T& point)
116  { vector3<> v = getCoord(point);
117  vector3<int> iv;
118  for(int k=0; k<3; k++)
119  { v[k] -= floor(v[k]); //in [0,1)
120  iv[k] = int(floor(v[k]*S[k] + 0.5)); //in [0, S]
121  }
122  indices[meshIndex(iv)].push_back(iPoint);
123  }
124 
127  template<typename Tag = double, typename TagEquality = std::equal_to<Tag> >
128  size_t find(vector3<> v, Tag tag = Tag(), const std::vector<Tag>* tagArr=0, TagEquality tagEquality = std::equal_to<Tag>()) const
129  { vector3<int> ivMin, ivMax;
130  for(int k=0; k<3; k++)
131  { v[k] -= floor(v[k]); //in [0,1)
132  ivMin[k] = int(floor((v[k]-symmThreshold)*S[k] + 0.5)); //in [-1, S]
133  ivMax[k] = int(floor((v[k]+symmThreshold)*S[k] + 0.5)); //in [0, S]
134  }
135  vector3<int> iv;
136  for(iv[0]=ivMin[0]; iv[0]<=ivMax[0]; iv[0]++)
137  for(iv[1]=ivMin[1]; iv[1]<=ivMax[1]; iv[1]++)
138  for(iv[2]=ivMin[2]; iv[2]<=ivMax[2]; iv[2]++)
139  for(size_t index: indices[meshIndex(iv)])
140  if(circDistanceSquared(v, getCoord(points[index])) < symmThresholdSq
141  && (!tagArr || tagEquality(tag, tagArr->at(index))) )
142  return index;
143  return string::npos;
144  }
145 };
146 
147 #endif // JDFTX_CORE_LATTICEUTILS_H
ScalarField pow(const ScalarField &, double alpha)
Elementwise power (preserve input)
ScalarField sqrt(const ScalarField &)
Elementwise square root (preserve input)
Simulation grid descriptor.
Definition: GridInfo.h:45
Definition: LatticeUtils.h:58
unsigned iReduced
corresponding reduced index
Definition: LatticeUtils.h:59
PeriodicLookup(const std::vector< T > &points, matrix3<> metric, size_t nPointsTarget=0)
Definition: LatticeUtils.h:99
const GridInfo & gInfo
unit cell and corresponding grids
Definition: LatticeUtils.h:51
matrix3< int > super
linear combinations to get Rsuper (Rsuper = R * super)
Definition: LatticeUtils.h:54
std::vector< vector3<> > kmesh
closure of kmeshReduced under symmetry group sym
Definition: LatticeUtils.h:52
vector3< int > offset
additional translation to get to kmesh from reduced one
Definition: LatticeUtils.h:62
void addPoint(size_t iPoint, const T &point)
Definition: LatticeUtils.h:115
matrix3 Rsuper
super-cell lattice vectors
Definition: LatticeUtils.h:53
Geometry of the simulation grid.
__hostanddev__ vector3< int > round(const vector3<> &v, double *err=0)
Round vector3<> to vector3<int> (and optionally retrieve error)
Definition: vector3.h:128
__hostanddev__ double circDistanceSquared(const vector3<> &a, const vector3<> &b)
Definition: vector3.h:138
unsigned iSym
symmetry matrix for transforming reduced to current value
Definition: LatticeUtils.h:60
size_t find(vector3<> v, Tag tag=Tag(), const std::vector< Tag > *tagArr=0, TagEquality tagEquality=std::equal_to< Tag >()) const
Definition: LatticeUtils.h:128
Supercell corresponding to a given k-point mesh.
Definition: LatticeUtils.h:49
Supercell(const GridInfo &gInfo, const std::vector< vector3<>> &kmeshReduced, const std::vector< matrix3< int >> &sym, const std::vector< int > &invertList)
int invert
sign of transformation to include inversion symmetry in k-space
Definition: LatticeUtils.h:61
Definition: LatticeUtils.h:83
#define assert(expr)
A custom assertion with stack trace (NOTE: enabled in release modes as well)
Definition: Util.h:100