ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
exx_lri_interface.h
Go to the documentation of this file.
1#ifndef EXX_LRI_INTERFACE_H
2#define EXX_LRI_INTERFACE_H
3
4#include "exx_lri.h"
9#include "source_estate/module_dm/density_matrix.h" // mohan add 2025-11-04
12#include <memory>
13
14class LCAO_Matrix;
15class Charge_Mixing;
16namespace elecstate
17{
18 class ElecState;
19 class TDFieldManager;
20}
21namespace module_dm
22{
23 template <typename TK, typename TR>
24 class DensityMatrix;
25}
26namespace elecstate
27{
29 std::vector<std::vector<std::complex<double>>> restore_dm(const K_Vectors& kv,
30 const std::vector<std::vector<std::complex<double>>>& dm_k_ibz,
32 const Parallel_Blkcyclic& pv);
34 std::vector<std::vector<double>> restore_dm(const K_Vectors& kv,
35 const std::vector<std::vector<double>>& dm_k_ibz,
37 const Parallel_Blkcyclic& pv);
38
39}
40
41template<typename T, typename Tdata>
43{
44public:
45 using TA = int;
46 using TC = std::array<int, 3>;
47 using TAC = std::pair<TA, TC>;
48
51 {
52 this->exx_ptr = std::make_shared<Exx_LRI<Tdata>>(info_ri);
53 this->info_global = info_global;
54 this->hybrid_step_ = info_global.hybrid_step;
55 }
57
58 void set_gpu(const bool use_gpu)
59 {
60#ifdef __GPU_RI
61 RI::LRI_Cal_Mode& mode = this->exx_ptr->exx_lri.lri.cal_mode;
62 mode = use_gpu ? RI::LRI_Cal_Mode::GPU : RI::LRI_Cal_Mode::CPU;
63#else
64 (void)use_gpu;
65#endif
66 }
67
69 //void write_Hexxs_cereal(const std::string& file_name) const;
70 //void read_Hexxs_cereal(const std::string& file_name);
71
72 std::vector<std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>>& get_Hexxs() const { return this->exx_ptr->Hexxs; }
73 double &get_Eexx() const { return this->exx_ptr->Eexx; }
74 ModuleBase::matrix &get_force() const { return this->exx_ptr->force_exx; }
75 ModuleBase::matrix &get_stress() const { return this->exx_ptr->stress_exx; }
77 {
78 return this->two_level_step;
79 }
80
81 // Processes in ESolver_KS_LCAO
83 void init(
84 const MPI_Comm &mpi_comm,
85 const UnitCell &ucell,
86 const K_Vectors &kv,
87 const LCAO_Orbitals &orb,
88 const bool use_gpu);
89
91 void cal_exx_ions(const UnitCell& ucell, const bool write_cv = false);
92
94 void cal_exx_elec(const std::vector<std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>>& Ds,
95 const UnitCell& ucell,
96 const Parallel_Orbitals& pv,
97 const ModuleSymmetry::Symmetry_rotation* p_symrot = nullptr);
98
100 void cal_exx_force(const int& nat);
101
103 void cal_exx_stress(const double& omega, const double& lat0);
104
105 // Processes in ESolver_KS_LCAO
107 void exx_before_all_runners(const K_Vectors& kv, const UnitCell& ucell, const Parallel_Blkcyclic& pv);
108
110 void refresh_symmetry(const K_Vectors& kv, const UnitCell& ucell, const Parallel_Blkcyclic& pv);
111
113 void exx_beforescf(
114 const int istep,
115 const K_Vectors &kv,
116 const Charge_Mixing &chgmix,
117 const UnitCell &ucell,
118 const LCAO_Orbitals &orb,
119 const Parallel_Blkcyclic &pv,
120 const bool update_symmetry,
121 const bool use_gpu);
122
124 void exx_eachiterinit(const int istep,
125 const UnitCell& ucell,
127 const K_Vectors& kv,
128 const Parallel_Orbitals& pv,
129 const int& iter,
130 const elecstate::ElecState& elec);
131
133 void exx_hamilt2rho(elecstate::ElecState& elec, const Parallel_Orbitals& pv, const int iter);
134
136 void exx_iter_finish(const K_Vectors& kv,
137 const UnitCell& ucell,
140 module_dm::DensityMatrix<T,double>* dm, // mohan add 2025-11-04
141 const Parallel_Orbitals& pv,
142 Charge_Mixing& chgmix,
143 const double& scf_ene_thr,
144 int& iter,
145 const int istep,
146 bool& conv_esolver);
148 bool exx_after_converge(const UnitCell& ucell,
151 const K_Vectors& kv,
152 const Parallel_Orbitals& pv,
153 const int& nspin,
154 int& iter,
155 const int& istep,
156 const double& etot,
157 const double& scf_ene_thr,
158 const elecstate::TDFieldManager* field);
159
167
168 size_t hybrid_step() const { return hybrid_step_; }
169 void set_hybrid_step(size_t s) { hybrid_step_ = s; }
170
171 std::shared_ptr<Exx_LRI<Tdata>> exx_ptr;
172
173private:
174
178
179 std::vector<std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>> prepare_exx_density(
180 const std::vector<std::vector<T>>& dmk, const UnitCell& ucell,
181 const K_Vectors& kv, const Parallel_Orbitals& pv, const int nspin,
182 const bool restart, const ModuleBase::Vector3<double>* source_A);
184 const UnitCell& ucell, const ModuleBase::Vector3<double>& source_A, const bool restart);
185 void mix_common_density(std::vector<std::map<TA, std::map<TAC, RI::Tensor<double>>>>& density,
186 const UnitCell& ucell, const ModuleBase::Vector3<double>& source_A, const bool restart);
187
189 size_t hybrid_step_ = 1;
190 // non-owning ptr to Charge_Mixing captured in exx_beforescf, used to refresh the
191 // borrowed mixing pointer in exx_eachiterinit (mixing_restart reallocates it via init_mixing)
192 const Charge_Mixing* p_chgmix_ = nullptr;
193 // identity of the last borrowed mixing engine; a change means init_mixing() reallocated it
194 // (mixing_restart fired), so the DM mixer must also restart to keep engine+history consistent
195 const void* last_borrowed_mixing_ = nullptr;
196
199
201 {
202 bool init = false;
203 bool ions = false;
204 bool elec = false;
205 bool force = false;
206 bool stress = false;
207 };
209};
210
211
212#endif
Definition chg_mix.h:11
void cal_exx_force(const int &nat)
: in cal_exx_force: Exx_LRI::cal_exx_force()
Definition exx_lri_interface.cpp:66
void exx_eachiterinit(const int istep, const UnitCell &ucell, const module_dm::DensityMatrix< T, double > &dm, const K_Vectors &kv, const Parallel_Orbitals &pv, const int &iter, const elecstate::ElecState &elec)
in eachiterinit: do DM mixing and calculate Hexx when entering 2nd SCF
Definition exx_lri_interface.cpp:293
Exx_LRI_Interface(const Exx_Info_RI &info_ri, const Exx_Info_Global &info_global)
Constructor for Exx_LRI_Interface.
Definition exx_lri_interface.h:50
ModuleBase::matrix & get_stress() const
Definition exx_lri_interface.h:75
Exx_LRI_Interface()=delete
void cal_exx_stress(const double &omega, const double &lat0)
: in cal_exx_stress: Exx_LRI::cal_exx_stress()
Definition exx_lri_interface.cpp:79
void init(const MPI_Comm &mpi_comm, const UnitCell &ucell, const K_Vectors &kv, const LCAO_Orbitals &orb, const bool use_gpu)
in init: Exx_LRI::init()
Definition exx_lri_interface.cpp:22
module_dm::DensityMatrix< T, double > * dm_last_step
Definition exx_lri_interface.h:166
void set_hybrid_step(size_t s)
Definition exx_lri_interface.h:169
RI_2D_Comm::HybridDensityLayout common_density_layout_
Definition exx_lri_interface.h:177
std::pair< TA, TC > TAC
Definition exx_lri_interface.h:47
std::vector< std::map< TA, std::map< TAC, RI::Tensor< Tdata > > > > & get_Hexxs() const
Definition exx_lri_interface.h:72
double etot_last_outer_loop
Definition exx_lri_interface.h:165
int get_two_level_step() const
Definition exx_lri_interface.h:76
void mix_common_density(RI_2D_Comm::HybridGaugeComplexDensity &density, const UnitCell &ucell, const ModuleBase::Vector3< double > &source_A, const bool restart)
Definition exx_lri_interface.cpp:92
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.cpp:42
size_t hybrid_step() const
Definition exx_lri_interface.h:168
ModuleSymmetry::Symmetry_rotation symrot_
Definition exx_lri_interface.h:198
Mix_DMk_2D< T > mix_DMk_2D
Definition exx_lri_interface.h:175
const Charge_Mixing * p_chgmix_
Definition exx_lri_interface.h:192
std::vector< std::map< TA, std::map< TAC, RI::Tensor< Tdata > > > > prepare_exx_density(const std::vector< std::vector< T > > &dmk, const UnitCell &ucell, const K_Vectors &kv, const Parallel_Orbitals &pv, const int nspin, const bool restart, const ModuleBase::Vector3< double > *source_A)
Definition exx_lri_interface.cpp:134
const void * last_borrowed_mixing_
Definition exx_lri_interface.h:195
Mix_DMk_2D< std::complex< double > > mix_common_density_
Definition exx_lri_interface.h:176
bool exx_spacegroup_symmetry
Definition exx_lri_interface.h:197
Exx_Info_Global info_global
Definition exx_lri_interface.h:188
int two_level_step
the step of the outer loop. nullptr: no dependence on the number of two_level_step,...
Definition exx_lri_interface.h:164
ModuleBase::matrix & get_force() const
Definition exx_lri_interface.h:74
void refresh_symmetry(const K_Vectors &kv, const UnitCell &ucell, const Parallel_Blkcyclic &pv)
Discard old atom mappings, lattice returns and rotation caches before rebuilding.
Definition exx_lri_interface.cpp:193
void exx_hamilt2rho(elecstate::ElecState &elec, const Parallel_Orbitals &pv, const int iter)
in hamilt2rho: calculate Hexx and Eexx
Definition exx_lri_interface.cpp:352
void exx_beforescf(const int istep, const K_Vectors &kv, const Charge_Mixing &chgmix, const UnitCell &ucell, const LCAO_Orbitals &orb, const Parallel_Blkcyclic &pv, const bool update_symmetry, const bool use_gpu)
in beforescf: set xc type, opt_orb, do DM mixing
Definition exx_lri_interface.cpp:214
Flag_Finish flag_finish
Definition exx_lri_interface.h:208
void exx_iter_finish(const K_Vectors &kv, const UnitCell &ucell, hamilt::Hamilt< T > &hamilt, elecstate::ElecState &elec, module_dm::DensityMatrix< T, double > *dm, const Parallel_Orbitals &pv, 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.cpp:384
std::array< int, 3 > TC
Definition exx_lri_interface.h:46
bool exx_after_converge(const UnitCell &ucell, hamilt::Hamilt< T > &hamilt, const module_dm::DensityMatrix< T, double > &dm, const K_Vectors &kv, const Parallel_Orbitals &pv, const int &nspin, int &iter, const int &istep, const double &etot, const double &scf_ene_thr, const elecstate::TDFieldManager *field)
: in do_after_converge: add exx operators; do DM mixing if seperate loop
Definition exx_lri_interface.cpp:470
size_t hybrid_step_
Definition exx_lri_interface.h:189
int TA
Definition exx_lri_interface.h:45
double & get_Eexx() const
Definition exx_lri_interface.h:73
void set_gpu(const bool use_gpu)
Definition exx_lri_interface.h:58
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.cpp:52
std::shared_ptr< Exx_LRI< Tdata > > exx_ptr
Definition exx_lri_interface.h:171
void exx_before_all_runners(const K_Vectors &kv, const UnitCell &ucell, const Parallel_Blkcyclic &pv)
Initialize EXX symmetry before the first ionic step.
Definition exx_lri_interface.cpp:183
Class for k-points management.
Definition klist.h:20
Definition orb_read.h:18
Definition mix_dmk_2d.h:16
3 elements vector
Definition vector3.h:21
Definition matrix.h:15
Definition symm_rotation.h:20
Parallel block-cyclic matrix distribution.
Definition parallel_blkcyc.h:25
Definition parallel_orbitals.h:16
Definition rpa_lri_coeff_v1.h:11
Provide the basic information about unitcell.
Definition unitcell.h:16
Definition elecstate.h:20
Own and advance all time-dependent electric fields in RT-TDDFT.
Definition td_field_manager.h:36
Definition hamilt.h:17
DensityMatrix Class <TK,TR> = <double,double> for Gamma-only calculation <TK,TR> = <std::complex<doub...
Definition density_matrix.h:172
std::vector< HybridSpinDensity > HybridGaugeComplexDensity
Definition hybrid_gauge_phase.h:40
std::vector< std::vector< HybridDensityBlock > > HybridDensityLayout
Definition hybrid_gauge_phase.h:43
Definition read_atoms_hlp_test.cpp:13
std::vector< std::vector< std::complex< double > > > restore_dm(const K_Vectors &kv, const std::vector< std::vector< std::complex< double > > > &dm_k_ibz, const ModuleSymmetry::Symmetry_rotation &symrot, const Parallel_Blkcyclic &pv)
for symmetry, multi-k, nspin<4: restore DM(k) form DM(k_ibz)
Definition wf_op.cpp:6
Definition density_matrix.cpp:13
Definition exx_info_global.h:9
Definition exx_info_ri.h:11
Definition exx_lri_interface.h:201
bool ions
Definition exx_lri_interface.h:203
bool force
Definition exx_lri_interface.h:205
bool init
Definition exx_lri_interface.h:202
bool stress
Definition exx_lri_interface.h:206
bool elec
Definition exx_lri_interface.h:204
std::pair< int, TC > TAC
Definition test_write_ri_cv.cpp:10