ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
dftu.h
Go to the documentation of this file.
1#ifndef DFTU_H
2#define DFTU_H
3
4#include "source_cell/klist.h"
7#ifdef __LCAO
12#include "source_lcao/force_stress_arrays.h" // mohan add 2024-06-15
13#endif
14
15#include <string>
16#include <vector>
17
18
19class Plus_U
20{
21
22 public:
23 Plus_U();
24 ~Plus_U();
25
26 public:
27 // allocate relevant data strcutures
28 void init(UnitCell& cell, // unitcell class
29 const Parallel_Orbitals* pv,
30 const int nks
31#ifdef __LCAO
32 , const LCAO_Orbitals* orb = nullptr
33#endif
34 );
35
36 // calculate the energy correction
37 void cal_energy_correction(const UnitCell& ucell, const int istep);
38
39 // mohan change the function to static, 20251106
40 static double get_energy()
41 {
42 return Plus_U::energy_u;
43 }
44
45 static void set_energy(const double &e)
46 {
48 }
49
50 static void set_double_energy() // mohan add 20251107
51 {
52 Plus_U::energy_u *= 2.0;
53 }
54
55 void uramping_update(); // update U by uramping
56 bool u_converged(); // check if U is converged
57
58 // mohan change these parameters to static, 2025-11-07
59 static std::vector<double> U; // U (Hubbard parameter U)
60 static std::vector<double> U0; // U0 (target Hubbard parameter U0)
61 static std::vector<int> orbital_corr; //
62 static double uramping; // increase U by uramping, default is -1.0
63 static int omc; // occupation matrix control
64 static int mixing_dftu; //whether to mix locale
65
66 private:
67
68 // mohan change the variable to static, 20251106
69 static double energy_u; //+U energy, mohan update 2025-11-06, change this to private
70
71 const Parallel_Orbitals* paraV = nullptr;
72 int cal_type = 3; // 1:dftu_tpye=1, dc=1; 2:dftu_type=1, dc=2; 3:dftu_tpye=2, dc=1; 4:dftu_tpye=2, dc=2;
73
74 // FIXME: the following variable does not have static lifetime;
75 // while the present class is used via a global variable. This has
76 // potential to cause dangling pointer issues.
77#ifdef __LCAO
78 const LCAO_Orbitals* ptr_orb_ = nullptr;
79 std::vector<double> orb_cutoff_;
80#endif
81
82 // transform between iwt index and it, ia, L, N and m index
83 std::vector<std::vector<std::vector<std::vector<std::vector<int>>>>>
84 iatlnmipol2iwt; // iatlnm2iwt[iat][l][n][m][ipol]
85
86#ifdef __LCAO
87 //=============================================================
88 // In dftu_hamilt.cpp
89 // For calculating contribution to Hamiltonian matrices
90 //=============================================================
91 public:
92 void cal_eff_pot_mat_complex(const int ik,
93 std::complex<double>* eff_pot,
94 const std::vector<int>& isk,
95 const std::complex<double>* sk);
96
97 void cal_eff_pot_mat_real(const int ik,
98 double* eff_pot,
99 const std::vector<int>& isk,
100 const double* sk);
101
102 void cal_eff_pot_mat_R_double(const int ispin, double* SR, double* HR);
103
104 void cal_eff_pot_mat_R_complex_double(const int ispin,
105 std::complex<double>* SR,
106 std::complex<double>* HR);
107#endif
108
109 //=============================================================
110 // In dftu_occup.cpp
111 // For calculating occupation matrix and saving to locale
112 // and other operations of locale: copy,zero out,mix
113 //=============================================================
114 public:
117 void cal_occ_pw(const int iter,
118 const void* psi_in,
119 const ModuleBase::matrix& wg_in,
120 const UnitCell& cell,
121 const double& mixing_beta);
122
124 void cal_VU_pot_pw(const int spin);
125
127 const std::complex<double>* get_eff_pot_pw(const int iat) const
128 {
129 return &(eff_pot_pw[this->eff_pot_pw_index[iat]]);
130 }
131
133 {
134 return eff_pot_pw.size();
135 }
136
137#ifdef __LCAO
138 // calculate the local occupation number matrix
139 void cal_occup_m_k(const int iter,
140 const UnitCell& ucell,
141 const std::vector<std::vector<std::complex<double>>>& dm_k,
142 const K_Vectors& kv,
143 const double& mixing_beta,
144 hamilt::Hamilt<std::complex<double>>* p_ham);
145
146 void cal_occup_m_gamma(const int iter,
147 const UnitCell& ucell,
148 const std::vector<std::vector<double>>& dm_gamma,
149 const double& mixing_beta,
151#endif
152
153 // dftu can be calculated only after locale has been initialed
154 bool initialed_locale = false;
155
156 private:
157
158 void copy_locale(const UnitCell& ucell);
159 void zero_locale(const UnitCell& ucell);
160 void mix_locale(const UnitCell& ucell,const double& mixing_beta);
161
162 std::vector<std::complex<double>> eff_pot_pw;
163 std::vector<int> eff_pot_pw_index;
164
165 public:
166 // local occupancy matrix of the correlated subspace
167 // locale: the out put local occupation number matrix of correlated electrons in the current electronic step
168 // locale_save: the input local occupation number matrix of correlated electrons in the current electronic step
169 std::vector<std::vector<std::vector<std::vector<ModuleBase::matrix>>>> locale; // locale[iat][l][n][spin](m1,m2)
170 std::vector<std::vector<std::vector<std::vector<ModuleBase::matrix>>>> locale_save; // locale_save[iat][l][n][spin](m1,m2)
171
172#ifdef __LCAO
173private:
174 //=============================================================
175 // In dftu_tools.cpp
176 // For calculating onsite potential, which is used
177 // for both Hamiltonian and force/stress
178 //=============================================================
179
180 void cal_VU_pot_mat_complex(const int spin, const bool newlocale, std::complex<double>* VU);
181 void cal_VU_pot_mat_real(const int spin, const bool newlocale, double* VU);
182
183 double get_onebody_eff_pot(const int T,
184 const int iat,
185 const int L,
186 const int N,
187 const int spin,
188 const int m0,
189 const int m1,
190 const bool newlocale);
191
192 //=============================================================
193 // In dftu_folding.cpp
194 // Subroutines for folding S and dS matrix
195 //=============================================================
196
197 void fold_dSR_gamma(const UnitCell& ucell,
198 const Parallel_Orbitals& pv,
199 const Grid_Driver* gd,
200 double* dsloc_x,
201 double* dsloc_y,
202 double* dsloc_z,
203 double* dh_r,
204 const int dim1,
205 const int dim2,
206 double* dSR_gamma);
207
208 // dim = 0 : S, for Hamiltonian
209 // dim = 1-3 : dS, for force
210 // dim = 4-6 : dS * dR, for stress
211
212 void folding_matrix_k(const UnitCell& ucell,
213 const Grid_Driver& gd,
215 const Parallel_Orbitals& pv,
216 const int ik,
217 const int dim1,
218 const int dim2,
219 std::complex<double>* mat_k,
220 const ModuleBase::Vector3<double>& kvec_d);
221
227 void folding_matrix_k_new(const int ik,
228 hamilt::Hamilt<std::complex<double>>* p_ham);
229
230 //=============================================================
231 // In dftu_force.cpp
232 // For calculating force and stress fomr DFT+U
233 //=============================================================
234 public:
235 void force_stress(const UnitCell& ucell,
236 const Grid_Driver& gd,
237 std::vector<std::vector<double>>* dmk_d, // mohan modify 2025-11-02
238 std::vector<std::vector<std::complex<double>>>* dmk_c, // dmat.get_dm()->get_DMK_vector();
239 const Parallel_Orbitals& pv,
241 ModuleBase::matrix& force_dftu,
242 ModuleBase::matrix& stress_dftu,
243 const K_Vectors& kv);
244
245 private:
246 void cal_force_k(const UnitCell& ucell,
247 const Grid_Driver& gd,
249 const Parallel_Orbitals& pv,
250 const int ik,
251 const std::complex<double>* rho_VU,
252 ModuleBase::matrix& force_dftu,
253 const ModuleBase::Vector3<double>& kvec_d);
254
255 void cal_stress_k(const UnitCell& ucell,
256 const Grid_Driver& gd,
258 const Parallel_Orbitals& pv,
259 const int ik,
260 const std::complex<double>* rho_VU,
261 ModuleBase::matrix& stress_dftu,
262 const ModuleBase::Vector3<double>& kvec_d);
263
264 void cal_force_gamma(const UnitCell& ucell,
265 const double* rho_VU,
266 const Parallel_Orbitals& pv,
267 double* dsloc_x,
268 double* dsloc_y,
269 double* dsloc_z,
270 ModuleBase::matrix& force_dftu);
271
272 void cal_stress_gamma(const UnitCell& ucell,
273 const Parallel_Orbitals& pv,
274 const Grid_Driver* gd,
275 double* dsloc_x,
276 double* dsloc_y,
277 double* dsloc_z,
278 double* dh_r,
279 const double* rho_VU,
280 ModuleBase::matrix& stress_dftu);
281#endif
282
283 //=============================================================
284 // In dftu_io.cpp
285 // For reading/writing/broadcasting/copying relevant data structures
286 //=============================================================
287 public:
288 void output(const UnitCell& ucell);
289
290 private:
291 void write_occup_m(const UnitCell& ucell,
292 std::ofstream& ofs,
293 bool diag=false);
294 void read_occup_m(const UnitCell& ucell,
295 const std::string& fn);
296 void local_occup_bcast(const UnitCell& ucell);
297
298 //=============================================================
299 // In dftu_yukawa.cpp
300 // Relevant for calculating U using Yukawa potential
301 //=============================================================
302
303 public:
304 static bool Yukawa; // 1:use Yukawa potential; 0: do not use Yukawa potential
305 void cal_slater_UJ(const UnitCell& ucell, double** rho, const int& nrxx);
306
307 private:
308 double lambda; // the parameter in Yukawa potential
309 std::vector<std::vector<std::vector<std::vector<double>>>> Fk; // slater integral:Fk[T][L][N][k]
310 std::vector<std::vector<std::vector<double>>> U_Yukawa; // U_Yukawa[T][L][N]
311 std::vector<std::vector<std::vector<double>>> J_Yukawa; // J_Yukawa[T][L][N]
312
313 void cal_slater_Fk(const UnitCell& ucell,const int L, const int T); // L:angular momnet, T:atom type
314 void cal_yukawa_lambda(double** rho, const int& nrxx);
315
316 double spherical_Bessel(const int k, const double r, const double lambda);
317 double spherical_Hankel(const int k, const double r, const double lambda);
318
319#ifdef __LCAO
320 public:
326 const hamilt::HContainer<double>* get_dmr(int ispin) const;
331 void set_dmr(const elecstate::DensityMatrix<double, double>* dm_in_dftu_d);
332 void set_dmr(const elecstate::DensityMatrix<std::complex<double>, double>* dm_in_dftu_cd);
333
334 private:
335 const UnitCell* ucell = nullptr;
336 const elecstate::DensityMatrix<double, double>* dm_in_dftu_d = nullptr;
337 const elecstate::DensityMatrix<std::complex<double>, double>* dm_in_dftu_cd = nullptr;
338#endif
339};
340
341
342#ifdef __LCAO
343template <typename T>
344void dftu_cal_occup_m(const int iter,
345 const UnitCell& ucell,
346 const std::vector<std::vector<T>>& dm,
347 const K_Vectors& kv,
348 const double& mixing_beta,
349 hamilt::Hamilt<T>* p_ham,
350 Plus_U &dftu);
351#endif
352
353
354#endif
Definition force_stress_arrays.h:5
Definition sltk_grid_driver.h:43
Definition klist.h:13
Definition ORB_read.h:19
3 elements vector
Definition vector3.h:22
Definition matrix.h:19
Definition parallel_orbitals.h:9
Definition dftu.h:20
void cal_slater_UJ(const UnitCell &ucell, double **rho, const int &nrxx)
const Parallel_Orbitals * paraV
Definition dftu.h:71
void cal_VU_pot_pw(const int spin)
calculate the local DFT+U effective potential matrix for PW base.
Definition dftu_pw.cpp:215
static std::vector< double > U
Definition dftu.h:26
void init(UnitCell &cell, const Parallel_Orbitals *pv, const int nks)
Definition dftu.cpp:46
const std::complex< double > * get_eff_pot_pw(const int iat) const
get effective potential matrix for PW base
Definition dftu.h:127
double spherical_Hankel(const int k, const double r, const double lambda)
static double energy_u
Definition dftu.h:69
void cal_occ_pw(const int iter, const void *psi_in, const ModuleBase::matrix &wg_in, const UnitCell &cell, const double &mixing_beta)
calculate occupation matrix for DFT+U
Definition dftu_pw.cpp:9
void write_occup_m(const UnitCell &ucell, std::ofstream &ofs, bool diag=false)
Definition dftu_io.cpp:76
void copy_locale(const UnitCell &ucell)
Definition dftu_occup.cpp:10
void cal_energy_correction(const UnitCell &ucell, const int istep)
void cal_yukawa_lambda(double **rho, const int &nrxx)
static void set_energy(const double &e)
Definition dftu.h:45
std::vector< std::vector< std::vector< std::vector< ModuleBase::matrix > > > > locale_save
Definition dftu.h:170
std::vector< std::vector< std::vector< std::vector< std::vector< int > > > > > iatlnmipol2iwt
Definition dftu.h:84
bool initialed_locale
Definition dftu.h:154
void local_occup_bcast(const UnitCell &ucell)
Definition dftu_io.cpp:407
std::vector< std::vector< std::vector< double > > > J_Yukawa
Definition dftu.h:311
~Plus_U()
Definition dftu.cpp:43
void cal_slater_Fk(const UnitCell &ucell, const int L, const int T)
void read_occup_m(const UnitCell &ucell, const std::string &fn)
Definition dftu_io.cpp:234
static bool Yukawa
Definition dftu.h:304
std::vector< std::vector< std::vector< std::vector< ModuleBase::matrix > > > > locale
Definition dftu.h:169
static int mixing_dftu
Definition dftu.h:64
bool u_converged()
Definition dftu.cpp:415
static void set_double_energy()
Definition dftu.h:50
std::vector< std::complex< double > > eff_pot_pw
Definition dftu.h:162
static double uramping
Definition dftu.h:62
static std::vector< int > orbital_corr
Definition dftu.h:30
static std::vector< double > U0
Definition dftu.h:28
static int omc
Definition dftu.h:63
static double get_energy()
Definition dftu.h:40
int get_size_eff_pot_pw() const
Definition dftu.h:132
double lambda
Definition dftu.h:308
std::vector< int > eff_pot_pw_index
Definition dftu.h:163
double spherical_Bessel(const int k, const double r, const double lambda)
int cal_type
Definition dftu.h:72
std::vector< std::vector< std::vector< double > > > U_Yukawa
Definition dftu.h:310
Plus_U()
Definition dftu.cpp:40
std::vector< std::vector< std::vector< std::vector< double > > > > Fk
Definition dftu.h:309
void uramping_update()
Definition dftu.cpp:395
void mix_locale(const UnitCell &ucell, const double &mixing_beta)
Definition dftu_occup.cpp:86
void zero_locale(const UnitCell &ucell)
Definition dftu_occup.cpp:48
Definition unitcell.h:17
Definition density_matrix.h:36
Definition hcontainer.h:144
Definition hamilt.h:16
Definition output.h:13
#define N
Definition exp.cpp:24
#define T
Definition exp.cpp:237
base device SOURCES math_hegvd_test cpp endif() if(ENABLE_GOOGLEBENCH) AddTest(TARGET PERF_MODULE_HSOLVER_KERNELS LIBS parameter $
Definition CMakeLists.txt:10
Plus_U dftu
Definition test_dftu.cpp:14