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