JDFTx  1.1.0
ElecInfo.h
1 /*-------------------------------------------------------------------
2 Copyright 2011 Ravishankar Sundararaman, Kendra Letchworth Weaver
3 Copyright 1996-2003 Sohrab Ismail-Beigi
4 
5 This file is part of JDFTx.
6 
7 JDFTx is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 JDFTx is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with JDFTx. If not, see <http://www.gnu.org/licenses/>.
19 -------------------------------------------------------------------*/
20 
21 #ifndef JDFTX_ELECTRONIC_ELECINFO_H
22 #define JDFTX_ELECTRONIC_ELECINFO_H
23 
24 #include <electronic/common.h>
25 #include <core/vector3.h>
26 
28 enum SpinType {
29  SpinNone,
30  SpinZ,
31  SpinVector,
32  SpinOrbit
33 };
34 
36 {
37 public:
39  int spin;
40  double weight;
41 
42  QuantumNumber() : spin(0) {}
43  int index() const { return spin<0 ? 1 : 0; }
44 };
45 
47 inline vector3<> getCoord(const QuantumNumber& qnum) { return qnum.k; }
48 
49 class ElecInfo
50 {
51 public:
52  int nBands, nStates;
53  int nDensities, spinWeight, qWeightSum;
54  int qStart, qStop;
55  bool isMine(int q) const { return qDivision.isMine(q); }
56  int whose(int q) const { return qDivision.whose(q); }
57  int qStartOther(int iProc) const { return qDivision.start(iProc); }
58  int qStopOther(int iProc) const { return qDivision.stop(iProc); }
59 
60  SpinType spinType;
61  double nElectrons;
62  std::vector<QuantumNumber> qnums;
63 
64  bool isNoncollinear() const { return spinType==SpinVector || spinType==SpinOrbit; }
65  int spinorLength() const { return isNoncollinear() ? 2 : 1; }
66 
71  MaximumOverlapMethod
72  }
73  fillingsUpdate;
74 
75  double kT;
76  double mu;
77 
80 
81  bool hasU;
82 
83  std::vector<std::tuple<int, int, double>> customFillings;
85 
86  ElecInfo();
87  void setup(const Everything &e, std::vector<diagMatrix>& F, Energies& ener);
88  void printFillings(FILE* fp) const;
89  void printFermi(const char* suffix, const double* muOverride=0) const; //Fermi fillings report (compute mu from eigenvalues in eVars if muOverride not provided)
90  void mixFillings(std::vector<diagMatrix>& F, Energies& ener);
91  void updateFillingsEnergies(const std::vector<diagMatrix>& F, Energies&) const;
92 
93  //Fermi function utilities:
94  inline double muEff(double mu, double Bz, int q) const { return mu + Bz*qnums[q].spin; }
95  double fermi(double mu, double eps) const { return 0.5*(1.-tanh(betaBy2*(eps-mu))); }
96  double fermiPrime(double mu, double eps) const { return -0.5*betaBy2/pow(cosh(betaBy2*(eps-mu)), 2); }
97  diagMatrix fermi(double mu, const diagMatrix& eps) const;
98  diagMatrix fermiPrime(double mu, const diagMatrix& eps) const;
99 
101  matrix fermiGrad(double mu, const diagMatrix& eps, const matrix& gradF) const;
102 
105  double nElectronsFermi(double mu, const std::vector<diagMatrix>& eps, double& Bz) const;
106 
109  double findMu(const std::vector<diagMatrix>& eps, double nElectrons, double& Bz) const;
110 
111  void kpointsPrint(FILE* fp, bool printSpin=false) const;
112  void kpointPrint(FILE* fp, int q, bool printSpin=false) const;
113 
114  int findHOMO(int q) const;
115 
116  //Parallel I/O utilities for diagMatrix/matrix array (one-per-kpoint, with nBands rows and columns unless overridden):
117  void read(std::vector<diagMatrix>&, const char *fname, int nRowsOverride=0) const;
118  void read(std::vector<matrix>&, const char *fname, int nRowsOverride=0, int nColsOverride=0) const;
119  void write(const std::vector<diagMatrix>&, const char *fname, int nRowsOverride=0) const;
120  void write(const std::vector<matrix>&, const char *fname, int nRowsOverride=0, int nColsOverride=0) const;
121  void appendWrite(const std::vector<diagMatrix>&, const char *fname, int nRowsOverride=0) const;
122 
123 private:
124  const Everything* e;
125  double betaBy2;
126  TaskDivision qDivision;
127 
128  //Initial fillings:
129  int nBandsOld;
130  double Qinitial, Minitial;
131  bool Mconstrain;
132  friend struct CommandElecInitialFillings;
133  friend struct CommandElecInitialCharge;
134  friend struct CommandElecInitialMagnetization;
135  friend struct CommandInitialState;
136  friend class ElecVars;
137  friend struct LCAOminimizer;
138 
140  double magnetizationFermi(double mu, double Bz, const std::vector<diagMatrix>& eps, double& nElectrons) const;
141 
142  //Fillings mix / mu-controller properties:
143  double fillingMixFraction;
144  double dnPrev, muMeasuredPrev;
145  double Cmeasured, Cweight;
146  double dnMixFraction;
147  friend struct CommandElecFermiFillings;
148  friend struct CommandTargetMu;
149 
150  //k-points:
151  vector3<int> kfold;
152  friend struct CommandKpointFolding;
153  friend class Everything;
154  friend class Phonon;
155  void kpointsFold();
156  void kpointsReduce();
157 };
158 
159 #endif // JDFTX_ELECTRONIC_ELECINFO_H
std::vector< QuantumNumber > qnums
k-points, spins and weights for each state
Definition: ElecInfo.h:62
ScalarField pow(const ScalarField &, double alpha)
Elementwise power (preserve input)
int index() const
return the appropriate index into electron (spin) density/potential arrays
Definition: ElecInfo.h:43
vector3 k
k-point wave vector
Definition: ElecInfo.h:38
string initialFillingsFilename
filename for initial fillings (zero-length if none)
Definition: ElecInfo.h:84
General complex matrix.
Definition: matrix.h:57
int mixInterval
we recalc. fillings every so many iterations
Definition: ElecInfo.h:78
int qStop
Range of states handled by current process (= 0 and nStates for non-MPI jobs)
Definition: ElecInfo.h:54
Definition: Energies.h:26
int whose(int q) const
find out which process this state index belongs to
Definition: ElecInfo.h:56
double weight
state weight (= 1x or 2x k-point weight depending on spintype)
Definition: ElecInfo.h:40
Real diagonal matrix.
Definition: matrix.h:30
Definition: ElecInfo.h:35
double fermiPrime(double mu, double eps) const
derivative of fermi function
Definition: ElecInfo.h:96
constant fillings (T=0)
Definition: ElecInfo.h:68
Calculate phonon dispersion, free energies and electron-phonon matrix elements.
Definition: Phonon.h:47
SpinType spinType
tells us what sort of spins we are using if any
Definition: ElecInfo.h:60
int qStartOther(int iProc) const
find out qStart for another process
Definition: ElecInfo.h:57
int spin
possible spin orientation. up=1, down=-1, none=0
Definition: ElecInfo.h:39
FillingsUpdate
Definition: ElecInfo.h:67
double fermi(double mu, double eps) const
fermi function
Definition: ElecInfo.h:95
bool isMine(int q) const
check if state index is local
Definition: ElecInfo.h:55
double mu
If NaN, fix nElectrons, otherwise fix/target chemical potential to this.
Definition: ElecInfo.h:76
double nElectrons
the number of electrons = Sum w Tr[F]
Definition: ElecInfo.h:61
fillings are a fermi function of the (auxilliary) subspace hamiltonian (recommended) ...
Definition: ElecInfo.h:70
Definition: Everything.h:41
Definition: ElecVars.h:30
Definition: ElecInfo.h:49
int nStates
Number of bands and total number of states.
Definition: ElecInfo.h:52
double kT
Temperature for Fermi distribution of fillings.
Definition: ElecInfo.h:75
bool subspaceRotation
whether subspace variables are required (either rotation or aux hamiltonian)
Definition: ElecInfo.h:79
int qStopOther(int iProc) const
find out qStop for another process
Definition: ElecInfo.h:58
Definition: MPIUtil.h:87
std::vector< std::tuple< int, int, double > > customFillings
Flag to check whether the calculation has a DFT+U self-interaction correction.
Definition: ElecInfo.h:83
int qWeightSum
number of density components, spin weight factor (= max occupation per state) and sum of k-point weig...
Definition: ElecInfo.h:53
mix fermi functions every mixInterval iterations
Definition: ElecInfo.h:69