ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
Exx_LRI_interface.hpp
Go to the documentation of this file.
1#ifndef EXX_LRI_INTERFACE_HPP
2#define EXX_LRI_INTERFACE_HPP
4
5#include "Exx_LRI_interface.h"
10
14
15#include <sys/time.h>
16#include <stdexcept>
17#include <string>
18
19template<typename T, typename Tdata>
20void Exx_LRI_Interface<T, Tdata>::init(const MPI_Comm &mpi_comm,
21 const UnitCell &ucell,
22 const K_Vectors &kv,
23 const LCAO_Orbitals& orb)
24{
25 ModuleBase::TITLE("Exx_LRI_Interface","init");
26 this->exx_ptr->init(mpi_comm, ucell, kv, orb);
27 this->flag_finish.init = true;
28}
29
30template<typename T, typename Tdata>
31void Exx_LRI_Interface<T, Tdata>::cal_exx_ions(const UnitCell& ucell, const bool write_cv)
32{
33 ModuleBase::TITLE("Exx_LRI_Interface","cal_exx_ions");
34 if(!this->flag_finish.init)
35 { throw std::runtime_error("Exx init unfinished when "+std::string(__FILE__)+" line "+std::to_string(__LINE__)); }
36
37 this->exx_ptr->cal_exx_ions(ucell, write_cv);
38
39 this->flag_finish.ions = true;
40}
41
42template<typename T, typename Tdata>
43void Exx_LRI_Interface<T, Tdata>::cal_exx_elec(const std::vector<std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>>& Ds,
44 const UnitCell& ucell,
45 const Parallel_Orbitals& pv,
47{
48 ModuleBase::TITLE("Exx_LRI_Interface","cal_exx_elec");
49 if(!this->flag_finish.init || !this->flag_finish.ions)
50 {
51 throw std::runtime_error("Exx init unfinished when "
52 +std::string(__FILE__)+" line "+std::to_string(__LINE__));
53 }
54
55 this->exx_ptr->cal_exx_elec(Ds, ucell, pv, p_symrot);
56
57 this->flag_finish.elec = true;
58}
59
60template<typename T, typename Tdata>
62{
63 ModuleBase::TITLE("Exx_LRI_Interface","cal_exx_force");
64 if(!this->flag_finish.init || !this->flag_finish.ions)
65 {
66 throw std::runtime_error("Exx init unfinished when "+std::string(__FILE__)+" line "+std::to_string(__LINE__));
67 }
68 if(!this->flag_finish.elec)
69 {
70 throw std::runtime_error("Exx Hamiltonian unfinished when "+std::string(__FILE__)
71 +" line "+std::to_string(__LINE__));
72 }
73
74 this->exx_ptr->cal_exx_force(nat);
75
76 this->flag_finish.force = true;
77}
78
79template<typename T, typename Tdata>
80void Exx_LRI_Interface<T, Tdata>::cal_exx_stress(const double& omega, const double& lat0)
81{
82 ModuleBase::TITLE("Exx_LRI_Interface","cal_exx_stress");
83 if(!this->flag_finish.init || !this->flag_finish.ions)
84 {
85 throw std::runtime_error("Exx init unfinished when "
86 +std::string(__FILE__)+" line "+std::to_string(__LINE__));
87 }
88 if(!this->flag_finish.elec)
89 {
90 throw std::runtime_error("Exx Hamiltonian unfinished when "
91 +std::string(__FILE__)+" line "+std::to_string(__LINE__));
92 }
93
94 this->exx_ptr->cal_exx_stress(omega, lat0);
95
96 this->flag_finish.stress = true;
97}
98
99template<typename T, typename Tdata>
101 const K_Vectors& kv,
102 const UnitCell& ucell,
103 const Parallel_2D& pv)
104{
105 ModuleBase::TITLE("Exx_LRI_Interface","exx_before_all_runners");
106 // initialize the rotation matrix in AO representation
107 this->exx_spacegroup_symmetry = (PARAM.inp.nspin < 4 && ModuleSymmetry::Symmetry::symm_flag == 1);
108 if (this->exx_spacegroup_symmetry)
109 {
110 const std::array<int, 3>& period = RI_Util::get_Born_vonKarmen_period(kv);
111 this->symrot_.find_irreducible_sector(
112 ucell.symm, ucell.atoms, ucell.st,
113 RI_Util::get_Born_von_Karmen_cells(period), period, ucell.lat);
114 this->symrot_.set_abfs_Lmax(GlobalC::exx_info.info_ri.abfs_Lmax);
115 this->symrot_.cal_Ms(kv, ucell, pv);
116 }
117}
118
119template<typename T, typename Tdata>
121 const K_Vectors& kv,
122 const Charge_Mixing& chgmix,
123 const UnitCell& ucell,
124 const LCAO_Orbitals& orb)
125{
126 ModuleBase::TITLE("Exx_LRI_Interface","exx_beforescf");
127#ifdef __MPI
128 if (GlobalC::exx_info.info_global.cal_exx)
129 {
130 if ((GlobalC::restart.info_load.load_H_finish && !GlobalC::restart.info_load.restart_exx)
131 || (istep > 0)
132 || (PARAM.inp.init_wfc == "file"))
133 {
135 }
136 else
137 {
139 }
140
141 this->cal_exx_ions(ucell,PARAM.inp.out_ri_cv);
142 }
143
144 // set initial parameter for mix_DMk_2D
145 if(GlobalC::exx_info.info_global.cal_exx)
146 {
147 if (this->exx_spacegroup_symmetry)
148 {this->mix_DMk_2D.set_nks(kv.get_nkstot_full() * (PARAM.inp.nspin == 2 ? 2 : 1), PARAM.globalv.gamma_only_local);}
149 else
150 {this->mix_DMk_2D.set_nks(kv.get_nks(), PARAM.globalv.gamma_only_local);}
151
152 if(GlobalC::exx_info.info_global.separate_loop)
153 { this->mix_DMk_2D.set_mixing(nullptr); }
154 else
155 { this->mix_DMk_2D.set_mixing(chgmix.get_mixing()); }
156 // for exx two_level scf
157 this->two_level_step = 0;
158 }
159#endif // __MPI
160}
161
162template<typename T, typename Tdata>
164 const UnitCell& ucell,
166 const K_Vectors& kv,
167 const int& iter)
168{
169 ModuleBase::TITLE("Exx_LRI_Interface","exx_eachiterinit");
170 if (GlobalC::exx_info.info_global.cal_exx)
171 {
172 if (!GlobalC::exx_info.info_global.separate_loop
173 && (this->two_level_step
174 || istep > 0
175 || PARAM.inp.init_wfc == "file") // non separate loop case
177 && PARAM.inp.init_wfc == "file"
178 && this->two_level_step == 0
179 && iter == 1)
180 ) // the first iter in separate loop case
181 {
182 const bool flag_restart = (iter == 1) ? true : false;
183
184 auto cal = [this, &ucell,&kv, &flag_restart](const elecstate::DensityMatrix<T, double>& dm_in)
185 {
186 if (this->exx_spacegroup_symmetry)
187 { this->mix_DMk_2D.mix(symrot_.restore_dm(kv,dm_in.get_DMK_vector(), *dm_in.get_paraV_pointer()), flag_restart); }
188 else
189 { this->mix_DMk_2D.mix(dm_in.get_DMK_vector(), flag_restart); }
190 const std::vector<std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>>
192 ? RI_2D_Comm::split_m2D_ktoR<Tdata>(
193 ucell,
194 *this->exx_ptr->p_kv,
195 this->mix_DMk_2D.get_DMk_gamma_out(),
196 *dm_in.get_paraV_pointer(),
198 : RI_2D_Comm::split_m2D_ktoR<Tdata>(
199 ucell,
200 *this->exx_ptr->p_kv,
201 this->mix_DMk_2D.get_DMk_k_out(),
202 *dm_in.get_paraV_pointer(),
204 this->exx_spacegroup_symmetry);
205
206 if (this->exx_spacegroup_symmetry && GlobalC::exx_info.info_ri.exx_symmetry_realspace)
207 { this->cal_exx_elec(Ds, ucell,*dm_in.get_paraV_pointer(), &this->symrot_); }
208 else
209 { this->cal_exx_elec(Ds, ucell,*dm_in.get_paraV_pointer()); }
210 };
211
212 if(istep > 0 && flag_restart)
213 { cal(*dm_last_step); }
214 else
215 { cal(dm); }
216 }
217 }
218}
219
220template<typename T, typename Tdata>
222{
223 ModuleBase::TITLE("Exx_LRI_Interface","exx_hamilt2density");
224 // Peize Lin add 2020.04.04
226 {
227 // add exx
228 // Peize Lin add 2016-12-03
229 if (GlobalC::restart.info_load.load_H_finish && !GlobalC::restart.info_load.restart_exx
230 && this->two_level_step == 0 && iter == 1)
231 {
232 if (GlobalV::MY_RANK == 0)
233 {
234 try
235 { GlobalC::restart.load_disk("Eexx", 0, 1, &this->exx_ptr->Eexx); }
236 catch (const std::exception& e)
237 { std::cout << "WARNING: Cannot read Eexx from disk, the energy of the 1st loop will be wrong, sbut it does not influence the subsequent loops." << std::endl; }
238 }
239 Parallel_Common::bcast_double(this->exx_ptr->Eexx);
240 this->exx_ptr->Eexx /= GlobalC::exx_info.info_global.hybrid_alpha;
241 }
242 elec.set_exx(this->get_Eexx());
243 }
244 else
245 {
246 elec.f_en.exx = 0.;
247 }
248}
249
250template<typename T, typename Tdata>
252 const UnitCell& ucell,
255 elecstate::DensityMatrix<T,double>* dm, // mohan add 2025-11-04
256 Charge_Mixing& chgmix,
257 const double& scf_ene_thr,
258 int& iter,
259 const int istep,
260 bool& conv_esolver)
261{
262 ModuleBase::TITLE("Exx_LRI_Interface","exx_iter_finish");
263 if (GlobalC::restart.info_save.save_H && (this->two_level_step > 0 || istep > 0)
264 && (!GlobalC::exx_info.info_global.separate_loop || iter == 1)) // to avoid saving the same value repeatedly
265 {
267 /*
268 hamilt::HS_Matrix_K<TK> Hexxk_save(&this->pv, 1);
269 for (int ik = 0; ik < this->kv.get_nks(); ++ik) {
270 Hexxk_save.set_zero_hk();
271
272 hamilt::OperatorEXX<hamilt::OperatorLCAO<TK, TR>> opexx_save(&Hexxk_save,
273 nullptr,
274 this->kv);
275
276 opexx_save.contributeHk(ik);
277
278 GlobalC::restart.save_disk("Hexx",
279 ik,
280 this->pv.get_local_size(),
281 Hexxk_save.get_hk());
282 }*/
284 const std::string& restart_HR_path = GlobalC::restart.folder + "HexxR" + std::to_string(GlobalV::MY_RANK);
285 ModuleIO::write_Hexxs_csr(restart_HR_path, ucell, this->get_Hexxs());
286
287 if (GlobalV::MY_RANK == 0)
288 {
289 GlobalC::restart.save_disk("Eexx", 0, 1, &elec.f_en.exx);
290 }
291 }
292
293 if (GlobalC::exx_info.info_global.cal_exx && conv_esolver)
294 {
295 // Kerker mixing does not work for the density matrix.
296 // In the separate loop case, it can still work in the subsequent inner loops where Hexx(DM) is fixed.
297 // In the non-separate loop case where Hexx(DM) is updated in every iteration of the 2nd loop, it should be
298 // closed.
299 if (!GlobalC::exx_info.info_global.separate_loop)
300 {
301 chgmix.close_kerker_gg0();
302 }
303 // mohan update 2025-11-04
304 this->dm_last_step = dm;
305 conv_esolver = this->exx_after_converge(
306 ucell,
307 hamilt,
308 *dm,
309 kv,
311 iter,
312 istep,
313 elec.f_en.etot,
314 scf_ene_thr);
315 }
316 //else if ( PARAM.inp.rdmft && two_level_step ) { conv_esolver = true; } // for RDMFT in the future to quit after the first iter of the exx-loop
317}
318
319template<typename T, typename Tdata>
321 const UnitCell& ucell,
324 const K_Vectors& kv,
325 const int& nspin,
326 int& iter,
327 const int& istep,
328 const double& etot,
329 const double& scf_ene_thr)
330{ // only called if (GlobalC::exx_info.info_global.cal_exx)
331 ModuleBase::TITLE("Exx_LRI_Interface","exx_after_converge");
332 auto restart_reset = [this]()
333 { // avoid calling restart related procedure in the subsequent ion steps
335 this->exx_ptr->Eexx = 0;
336 };
337
338 // no separate_loop case
339 if (!GlobalC::exx_info.info_global.separate_loop)
340 {
342
343 // in no_separate_loop case, scf loop only did twice
344 // in first scf loop, exx updated once in beginning,
345 // in second scf loop, exx updated every iter
346
347 if (this->two_level_step || istep > 0)
348 {
349 restart_reset();
350 return true;
351 }
352 else
353 {
354 // update exx and redo scf
356 iter = 0;
357 std::cout << " Entering 2nd SCF, where EXX is updated" << std::endl;
358 this->two_level_step++;
359 return false;
360 }
361 }
362 else
363 { // has separate_loop case
364 const double ediff = std::abs(etot - etot_last_outer_loop) * ModuleBase::Ry_to_eV;
365 if (two_level_step)
366 { std::cout << FmtCore::format(" deltaE (eV) from outer loop: %.8e \n", ediff); }
367 // exx converged or get max exx steps
368 if (this->two_level_step == GlobalC::exx_info.info_global.hybrid_step
369 || (iter == 1 && this->two_level_step != 0) // density convergence of outer loop
370 || (ediff < scf_ene_thr && this->two_level_step != 0)) //energy convergence of outer loop
371 {
372 restart_reset();
373 return true;
374 }
375 else
376 {
377 this->etot_last_outer_loop = etot;
378 // update exx and redo scf
379 if (this->two_level_step == 0)
381
382 std::cout << " Updating EXX " << std::flush;
383 timeval t_start; gettimeofday(&t_start, nullptr);
384
385 // if init_wfc == "file", DM is calculated in the 1st iter of the 1st two-level step, so we mix it here
386 const bool flag_restart = (this->two_level_step == 0 && PARAM.inp.init_wfc != "file") ? true : false;
387
388 if (this->exx_spacegroup_symmetry)
389 {this->mix_DMk_2D.mix(symrot_.restore_dm(kv, dm.get_DMK_vector(), *dm.get_paraV_pointer()), flag_restart);}
390 else
391 {this->mix_DMk_2D.mix(dm.get_DMK_vector(), flag_restart);}
392
393 // GlobalC::exx_lcao.cal_exx_elec(p_esolver->LOC, p_esolver->LOWF.wfc_k_grid);
394 const std::vector<std::map<int, std::map<std::pair<int, std::array<int, 3>>, RI::Tensor<Tdata>>>>
395 Ds = std::is_same<T, double>::value //gamma_only_local
396 ? RI_2D_Comm::split_m2D_ktoR<Tdata>(ucell,*this->exx_ptr->p_kv, this->mix_DMk_2D.get_DMk_gamma_out(), *dm.get_paraV_pointer(), nspin)
397 : RI_2D_Comm::split_m2D_ktoR<Tdata>(ucell,*this->exx_ptr->p_kv, this->mix_DMk_2D.get_DMk_k_out(), *dm.get_paraV_pointer(), nspin, this->exx_spacegroup_symmetry);
398
399 if (this->exx_spacegroup_symmetry && GlobalC::exx_info.info_ri.exx_symmetry_realspace)
400 { this->cal_exx_elec(Ds, ucell, *dm.get_paraV_pointer(), &this->symrot_); }
401 else
402 { this->cal_exx_elec(Ds, ucell, *dm.get_paraV_pointer()); } // restore DM but not Hexx
403 iter = 0;
404 this->two_level_step++;
405
406 timeval t_end; gettimeofday(&t_end, nullptr);
407 std::cout << "and rerun SCF\t"
408 << std::setprecision(3) << std::setiosflags(std::ios::scientific)
409 << (double)(t_end.tv_sec-t_start.tv_sec) + (double)(t_end.tv_usec-t_start.tv_usec)/1000000.0
410 << std::defaultfloat << " (s)" << std::endl;
411 return false;
412 }
413 } // if(GlobalC::exx_info.info_global.separate_loop)
414 restart_reset();
415 return true;
416}
417
418#endif
Atom_pseudo ncpp
Definition atom_spec.h:13
Definition charge_mixing.h:9
Base_Mixing::Mixing * get_mixing() const
Definition charge_mixing.h:103
void close_kerker_gg0()
Definition charge_mixing.h:52
void cal_exx_force(const int &nat)
: in cal_exx_force: Exx_LRI::cal_exx_force()
Definition Exx_LRI_interface.hpp:61
void cal_exx_stress(const double &omega, const double &lat0)
: in cal_exx_stress: Exx_LRI::cal_exx_stress()
Definition Exx_LRI_interface.hpp:80
void exx_beforescf(const int istep, const K_Vectors &kv, const Charge_Mixing &chgmix, const UnitCell &ucell, const LCAO_Orbitals &orb)
in beforescf: set xc type, opt_orb, do DM mixing
Definition Exx_LRI_interface.hpp:120
std::pair< TA, TC > TAC
Definition Exx_LRI_interface.h:37
void cal_exx_ions(const UnitCell &ucell, const bool write_cv=false)
: in cal_exx_ions: Exx_LRI::cal_exx_ions()
Definition Exx_LRI_interface.hpp:31
void init(const MPI_Comm &mpi_comm, const UnitCell &ucell, const K_Vectors &kv, const LCAO_Orbitals &orb)
in init: Exx_LRI::init()
Definition Exx_LRI_interface.hpp:20
void exx_hamilt2rho(elecstate::ElecState &elec, const Parallel_Orbitals &pv, const int iter)
in hamilt2rho: calculate Hexx and Eexx
Definition Exx_LRI_interface.hpp:221
bool exx_after_converge(const UnitCell &ucell, hamilt::Hamilt< T > &hamilt, const elecstate::DensityMatrix< T, double > &dm, const K_Vectors &kv, const int &nspin, int &iter, const int &istep, const double &etot, const double &scf_ene_thr)
: in do_after_converge: add exx operators; do DM mixing if seperate loop
Definition Exx_LRI_interface.hpp:320
void exx_before_all_runners(const K_Vectors &kv, const UnitCell &ucell, const Parallel_2D &pv)
in before_all_runners: set symmetry according to irreducible k-points since k-points are not reduced ...
Definition Exx_LRI_interface.hpp:100
void exx_eachiterinit(const int istep, const UnitCell &ucell, const elecstate::DensityMatrix< T, double > &dm, const K_Vectors &kv, const int &iter)
in eachiterinit: do DM mixing and calculate Hexx when entering 2nd SCF
Definition Exx_LRI_interface.hpp:163
int TA
Definition Exx_LRI_interface.h:35
void cal_exx_elec(const std::vector< std::map< TA, std::map< TAC, RI::Tensor< Tdata > > > > &Ds, const UnitCell &ucell, const Parallel_Orbitals &pv, const ModuleSymmetry::Symmetry_rotation *p_symrot=nullptr)
: in cal_exx_elec: Exx_LRI::cal_exx_elec()
Definition Exx_LRI_interface.hpp:43
void exx_iter_finish(const K_Vectors &kv, const UnitCell &ucell, hamilt::Hamilt< T > &hamilt, elecstate::ElecState &elec, elecstate::DensityMatrix< T, double > *dm, Charge_Mixing &chgmix, const double &scf_ene_thr, int &iter, const int istep, bool &conv_esolver)
in iter_finish: write Hexx, do something according to whether SCF is converged
Definition Exx_LRI_interface.hpp:251
static std::string format(const char *fmt, const Ts &... args)
static function to format data
Definition formatter.h:41
Definition klist.h:13
int get_nkstot_full() const
Definition klist.h:78
int get_nks() const
Definition klist.h:68
Definition ORB_read.h:19
Definition symmetry_rotation.h:16
static int symm_flag
Definition symmetry.h:30
This class packs the basic information of 2D-block-cyclic parallel distribution of an arbitrary matri...
Definition parallel_2d.h:12
Definition parallel_orbitals.h:9
const Input_para & inp
Definition parameter.h:26
const System_para & globalv
Definition parameter.h:30
std::string folder
Definition restart.h:31
Info_Load info_load
Definition restart.h:29
bool load_disk(const std::string label, const int index, const int size, T *data, const bool error_quit=true) const
Definition restart.h:43
bool save_disk(const std::string label, const int index, const int size, T *data, const bool error_quit=true) const
Definition restart.h:34
Definition unitcell.h:17
Atom * atoms
Definition unitcell.h:19
Lattice lat
Definition unitcell.h:27
ModuleSymmetry::Symmetry symm
Definition unitcell.h:57
Statistics st
Definition unitcell.h:46
static void set_xc_first_loop(const UnitCell &ucell)
Usually in exx caculation, the first SCF loop should be converged with PBE.
Definition xc_functional.cpp:26
static int get_func_type()
Definition xc_functional.h:67
static void set_xc_type(const std::string xc_func_in)
Definition xc_functional.cpp:59
Definition density_matrix.h:36
const std::vector< std::vector< TK > > & get_DMK_vector() const
get pointer vector of DMK
Definition density_matrix.h:164
const Parallel_Orbitals * get_paraV_pointer() const
get pointer of paraV
Definition density_matrix.h:176
Definition elecstate.h:15
fenergy f_en
energies contribute to the total free energy
Definition elecstate.h:151
void set_exx(const double &Eexx)
calculation if converged
Definition elecstate_exx.cpp:8
Definition hamilt.h:16
std::string xc_func
Definition pseudo.h:25
a new formatter library for formatting data
Exx_Info exx_info
Definition test_xc.cpp:29
Restart restart
Definition for_testing_input_conv.h:255
int MY_RANK
global index of process
Definition global_variable.cpp:21
const double Ry_to_eV
Definition constants.h:81
void TITLE(const std::string &class_name, const std::string &function_name, const bool disable)
Definition tool_title.cpp:18
void write_Hexxs_csr(const std::string &file_name, const UnitCell &ucell, const std::map< int, std::map< TAC, RI::Tensor< Tdata > > > &Hexxs)
write Hexxs in CSR format
void bcast_double(double *object, const int n)
Definition parallel_common.cpp:49
std::array< int, 3 > get_Born_vonKarmen_period(const K_Vectors &kv)
Definition RI_Util.hpp:16
std::vector< std::array< Tcell, Ndim > > get_Born_von_Karmen_cells(const std::array< Tcell, Ndim > &Born_von_Karman_period)
Definition RI_Util.hpp:34
Definition hamilt.h:12
Parameter PARAM
Definition parameter.cpp:3
bool separate_loop
Definition exx_info.h:35
double hybrid_alpha
Definition exx_info.h:31
size_t hybrid_step
Definition exx_info.h:36
Exx_Info_Global info_global
Definition exx_info.h:38
std::string init_wfc
"file","atomic","random"
Definition input_parameter.h:48
int nspin
LDA ; LSDA ; non-linear spin.
Definition input_parameter.h:85
bool out_ri_cv
Whether to output the coefficient tensor C and ABFs-representation Coulomb matrix V.
Definition input_parameter.h:555
bool restart_exx
Definition restart.h:27
bool gamma_only_local
Definition system_parameter.h:38
double etot
the total free energy
Definition fp_energy.h:18
double exx
the exact exchange energy.
Definition fp_energy.h:31