JDFTx  1.2.0
WannierMinimizer.h
1 /*-------------------------------------------------------------------
2 Copyright 2014 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_WANNIER_WANNIERMINIMIZER_H
21 #define JDFTX_WANNIER_WANNIERMINIMIZER_H
22 
23 #include <core/Util.h>
24 #include <core/Minimize.h>
25 #include <core/LatticeUtils.h>
26 #include <electronic/Everything.h>
27 #include <electronic/ColumnBundle.h>
28 #include <electronic/matrix.h>
29 #include <electronic/ColumnBundleTransform.h>
30 #include <wannier/Wannier.h>
31 
32 struct WannierGradient : public std::vector<matrix>
33 { const class WannierMinimizer* wmin;
34  void init(const class WannierMinimizer* wmin);
35  size_t ikStart() const;
36  size_t ikStop() const;
37 };
38 
39 //---- linear algebra functions required by Minimizable<WannierGradient> -----
41 double dot(const WannierGradient& x, const WannierGradient& y);
42 WannierGradient& operator*=(WannierGradient& x, double alpha);
43 void axpy(double alpha, const WannierGradient& x, WannierGradient& y);
44 matrix randomMatrix(int nRows, int nCols);
45 void randomize(WannierGradient& x);
46 
48 class WannierMinimizer : public Minimizable<WannierGradient>
49 {
50 public:
51  WannierMinimizer(const Everything& e, const Wannier& wannier, bool needSuperOverride=false);
52  virtual ~WannierMinimizer() {}
53  void initTransformDependent(); //second half of common initialization that must happen after sub-class is fully initialized
54 
55  void saveMLWF(); //save wannier functions for all spins
56  void saveMLWF(int iSpin); //save for specified spin
57 
58  //Interface for minimize:
59  void step(const WannierGradient& grad, double alpha);
60  double compute(WannierGradient* grad, WannierGradient* Kgrad); //identity preconditioner, but impose hermiticity constraint
61  void constrain(WannierGradient& grad); //enforce hermiticity
62  bool report(int iter);
63  double sync(double x) const;
64 
67  { bool operator<(const Kpoint& other) const;
68  bool operator==(const Kpoint& other) const;
69  };
70 
72  struct KmeshEntry
74  //State of system for Wannier minimize:
75  int nIn; //number of bands that contribute to the Wannier subspace
76  int nFixed; //number of bands that contribute fully to the Wannier subspace (cannot be partially mixed out)
78  matrix U1; //Initial rotation into Wannier subspace (nBands x nIn)
79  matrix U2; //Rotation within Wannier subspace (nCenters x nCenters)
80  };
81 
82  //-------- Interface for subclasses that provide the objective function for Wannier minimization
83 
85  virtual void initialize(int iSpin)=0;
86 
92  virtual double getOmega(bool grad=false)=0;
93 
94  //Like getOmega, but for the subspace-invariant OmegaI instead.
95  virtual double getOmegaI(bool grad=false)=0;
96 
97 protected:
98  friend struct WannierGradient;
99  const Everything& e;
100  const Wannier& wannier;
101  const std::vector< matrix3<int> >& sym;
102  int nCenters, nFrozen, nBands;
103  int nSpins, qCount;
104  int nSpinor;
105  std::vector<double> rSqExpect;
106  std::vector< vector3<> > rExpect;
107  std::vector<bool> pinned;
108  std::vector< vector3<> > rPinned;
109 
110  //Supercell grid and basis:
111  bool needSuper;
112  GridInfo gInfoSuper;
113  Basis basisSuper;
114  QuantumNumber qnumSuper;
115  std::map<vector3<int>,double> iCellMap, phononCellMap; //unit-cell indices in supercell (and weights to account for surface multiplicity)
116  int nPhononModes; //number of phonon modes
117 
118  std::vector<KmeshEntry> kMesh;
119  std::set<Kpoint> kpoints;
120  std::shared_ptr<ColumnBundleTransform::BasisWrapper> basisWrapper, basisSuperWrapper;
121  std::map<Kpoint, std::shared_ptr<ColumnBundleTransform> > transformMap, transformMapSuper;
123 
124  //k-mesh MPI division:
125  TaskDivision kDivision;
126  size_t ikStart, ikStop;
127  inline bool isMine(size_t ik) const { return kDivision.isMine(ik); }
128  inline int whose(size_t ik) const { return kDivision.whose(ik); }
129 
130  //state MPI division (wrappers to ElecInfo)
131  bool isMine_q(int ik, int iSpin) const { return e.eInfo.isMine(kMesh[ik].point.iReduced + iSpin*qCount); }
132  int whose_q(int ik, int iSpin) const { return e.eInfo.whose(kMesh[ik].point.iReduced + iSpin*qCount); }
133 
135  ColumnBundle getWfns(const Kpoint& kpoint, int iSpin) const;
136  std::vector<ColumnBundle> Cother; //wavefunctions from another process
137 
139  void axpyWfns(double alpha, const matrix& A, const Kpoint& kpoint, int iSpin, ColumnBundle& result) const;
140 
142  void axpyWfns_grad(double alpha, matrix& Omega_A, const Kpoint& kpoint, int iSpin, const ColumnBundle& Omega_result) const;
143 
145  ColumnBundle trialWfns(const Kpoint& kpoint) const;
146  std::map< Kpoint, std::shared_ptr<ColumnBundle> > numericalOrbitals;
147 
150  matrix overlap(const ColumnBundle& C1, const ColumnBundle& C2) const;
151 
152  //Dump a named matrix variable to file, optionally zeroing out the real parts
153  void dumpMatrix(const matrix& H, string varName, bool realPartOnly, int iSpin) const;
154 
155  static matrix fixUnitary(const matrix& U); //return an exactly unitary version of U (orthogonalize columns)
156 
157  //Preconditioner for Wannier optimization: identity by default, override in derived class to change
158  virtual WannierGradient precondition(const WannierGradient& grad);
159 };
160 
161 #endif // JDFTX_WANNIER_WANNIERMINIMIZER_H
Entry in the k-point mesh, including state of minimizer (subspace rotations)
Definition: WannierMinimizer.h:72
Base class for different wannier minimizers:
Definition: WannierMinimizer.h:48
void randomize(TptrCollection &x)
Initialize to normal random numbers:
Definition: ScalarFieldArray.h:154
Simulation grid descriptor.
Definition: GridInfo.h:45
int whose(size_t task) const
Which process number should handle this task number.
complex dot(const Tptr &X, const Tptr &Y)
Definition: Operators.h:196
std::set< Kpoint > kpoints
list of all k-points that will be in use (including those in FD formulae)
Definition: WannierMinimizer.h:119
std::vector< KmeshEntry > kMesh
k-point mesh with FD formula
Definition: WannierMinimizer.h:118
Definition: LatticeUtils.h:58
Basis basis
common basis (with indexing into full G-space)
Definition: WannierMinimizer.h:122
General complex matrix.
Definition: matrix.h:58
Definition: WannierMinimizer.h:32
int whose(int q) const
find out which process this state index belongs to
Definition: ElecInfo.h:56
matrix Omega_U
net rotation (nBands x nCenters) and intermediate gradient w.r.t it
Definition: WannierMinimizer.h:77
Entries in the k-point mesh.
Definition: WannierMinimizer.h:66
Definition: ElecInfo.h:35
std::vector< vector3<> > rPinned
Whether centers are pinned or free.
Definition: WannierMinimizer.h:108
bool needSuper
Where centers are pinned to (if they are)
Definition: WannierMinimizer.h:111
Tptr clone(const Tptr &X)
Clone (NOTE: operator= is by reference for the ScalarField classes)
Definition: Operators.h:111
std::shared_ptr< ColumnBundleTransform::BasisWrapper > basisSuperWrapper
look-up tables for initializing transforms
Definition: WannierMinimizer.h:120
Definition: Basis.h:29
bool isMine(int q) const
check if state index is local
Definition: ElecInfo.h:55
std::map< Kpoint, std::shared_ptr< ColumnBundleTransform > > transformMapSuper
wave-function transforms for each k-point to the common bases
Definition: WannierMinimizer.h:121
Miscellaneous utilities.
std::vector< vector3<> > rExpect
Expectation values for r per center in current group.
Definition: WannierMinimizer.h:106
Definition: Everything.h:41
Tptr & operator*=(Tptr &in, double scaleFac)
Scale.
Definition: Operators.h:115
Nonlinear minimization templates.
std::vector< double > rSqExpect
Expectation values for r^2 per center in current group.
Definition: WannierMinimizer.h:105
int nSpinor
number of spinor components
Definition: WannierMinimizer.h:104
int qCount
number of spins, and number of states per spin
Definition: WannierMinimizer.h:103
int nBands
number of Wannier centers (total and frozen) and source bands
Definition: WannierMinimizer.h:102
Definition: Minimize.h:46
void axpy(double alpha, const Tptr &X, Tptr &Y)
Generic axpy for complex data types (Note: null pointers are treated as zero)
Definition: Operators.h:158
Compute Maximally-Localized Wannier Functions.
Definition: Wannier.h:29
Kpoint point
point in the mesh
Definition: WannierMinimizer.h:73
bool isMine(size_t task) const
Whether current process handle this task number.
Definition: MPIUtil.h:96
Definition: MPIUtil.h:87
Definition: ColumnBundle.h:30