JDFTx  1.1.1
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);
61  WannierGradient precondition(const WannierGradient& grad); //identity preconditioner, but impose hermiticity constraint
62  void constrain(WannierGradient& grad); //enforce hermiticity
63  bool report(int iter);
64  double sync(double x) const;
65 
68  { bool operator<(const Kpoint& other) const;
69  bool operator==(const Kpoint& other) const;
70  };
71 
73  struct KmeshEntry
75  //State of system for Wannier minimize:
76  int nIn; //number of bands that contribute to the Wannier subspace
77  int nFixed; //number of bands that contribute fully to the Wannier subspace (cannot be partially mixed out)
80  //Stage 1: Select linear cominations of bands that enter Wannier subspace
81  matrix calc_V1();
82  matrix U1, V1, B1evecs; //U1 = initial rotation (nBands x nIn), V1 = subsequent rotation from B (nIn x nCenters)
83  diagMatrix B1eigs;
84  //Stage 2: Rotations within Wannier subspace (all nCenters x nCenters)
85  matrix U2, V2, B2evecs;
86  diagMatrix B2eigs;
87  };
88 
89  //-------- Interface for subclasses that provide the objective function for Wannier minimization
90 
92  virtual void initialize(int iSpin)=0;
93 
99  virtual double getOmega(bool grad=false)=0;
100 
101  //Like getOmega, but for the subspace-invariant OmegaI instead.
102  virtual double getOmegaI(bool grad=false)=0;
103 
104 protected:
105  friend struct WannierGradient;
106  const Everything& e;
107  const Wannier& wannier;
108  const std::vector< matrix3<int> >& sym;
109  int nCenters, nFrozen, nBands;
110  int nSpins, qCount;
111  int nSpinor;
112  std::vector<double> rSqExpect;
113  std::vector< vector3<> > rExpect;
114  std::vector<bool> pinned;
115  std::vector< vector3<> > rPinned;
116 
117  //Supercell grid and basis:
118  bool needSuper;
119  GridInfo gInfoSuper;
120  Basis basisSuper;
121  QuantumNumber qnumSuper;
122  std::map<vector3<int>,double> iCellMap, phononCellMap; //unit-cell indices in supercell (and weights to account for surface multiplicity)
123  int nPhononModes; //number of phonon modes
124 
125  std::vector<KmeshEntry> kMesh;
126  std::set<Kpoint> kpoints;
127  std::shared_ptr<ColumnBundleTransform::BasisWrapper> basisWrapper, basisSuperWrapper;
128  std::map<Kpoint, std::shared_ptr<ColumnBundleTransform> > transformMap, transformMapSuper;
130 
131  //k-mesh MPI division:
132  TaskDivision kDivision;
133  size_t ikStart, ikStop;
134  inline bool isMine(size_t ik) const { return kDivision.isMine(ik); }
135  inline int whose(size_t ik) const { return kDivision.whose(ik); }
136 
137  //state MPI division (wrappers to ElecInfo)
138  bool isMine_q(int ik, int iSpin) const { return e.eInfo.isMine(kMesh[ik].point.iReduced + iSpin*qCount); }
139  int whose_q(int ik, int iSpin) const { return e.eInfo.whose(kMesh[ik].point.iReduced + iSpin*qCount); }
140 
142  ColumnBundle getWfns(const Kpoint& kpoint, int iSpin) const;
143  std::vector<ColumnBundle> Cother; //wavefunctions from another process
144 
146  void axpyWfns(double alpha, const matrix& A, const Kpoint& kpoint, int iSpin, ColumnBundle& result) const;
147 
149  void axpyWfns_grad(double alpha, matrix& Omega_A, const Kpoint& kpoint, int iSpin, const ColumnBundle& Omega_result) const;
150 
152  ColumnBundle trialWfns(const Kpoint& kpoint) const;
153  std::map< Kpoint, std::shared_ptr<ColumnBundle> > numericalOrbitals;
154 
157  matrix overlap(const ColumnBundle& C1, const ColumnBundle& C2) const;
158 
159  //Dump a named matrix variable to file, optionally zeroing out the real parts
160  void dumpMatrix(const matrix& H, string varName, bool realPartOnly, int iSpin) const;
161 
162  static matrix fixUnitary(const matrix& U); //return an exactly unitary version of U (orthogonalize columns)
163 };
164 
165 #endif // JDFTX_WANNIER_WANNIERMINIMIZER_H
Entry in the k-point mesh, including state of minimizer (subspace rotations)
Definition: WannierMinimizer.h:73
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:126
std::vector< KmeshEntry > kMesh
k-point mesh with FD formula
Definition: WannierMinimizer.h:125
Definition: LatticeUtils.h:58
Basis basis
common basis (with indexing into full G-space)
Definition: WannierMinimizer.h:129
General complex matrix.
Definition: matrix.h:57
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:79
Entries in the k-point mesh.
Definition: WannierMinimizer.h:67
Real diagonal matrix.
Definition: matrix.h:30
Definition: ElecInfo.h:35
std::vector< vector3<> > rPinned
Whether centers are pinned or free.
Definition: WannierMinimizer.h:115
bool needSuper
Where centers are pinned to (if they are)
Definition: WannierMinimizer.h:118
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:127
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:128
matrix B
Independent variable for minimization (nCenters x nIn)
Definition: WannierMinimizer.h:78
Miscellaneous utilities.
std::vector< vector3<> > rExpect
Expectation values for r per center in current group.
Definition: WannierMinimizer.h:113
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:112
int nSpinor
number of spinor components
Definition: WannierMinimizer.h:111
int qCount
number of spins, and number of states per spin
Definition: WannierMinimizer.h:110
int nBands
number of Wannier centers (total and frozen) and source bands
Definition: WannierMinimizer.h:109
Definition: Minimize.h:45
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:74
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