16using TAC = std::pair<int, std::array<int, 3>>;
19template <
typename Tdata,
typename TR>
20void reallocate_hcontainer(
const std::vector<std::map<
int, std::map<
TAC, RI::Tensor<Tdata>>>>& Hexxs,
22 const RI::Cell_Nearest<int, int, 3, double, 3>*
const cell_nearest)
24 auto* pv = hR->get_paraV();
25 bool need_allocate =
false;
26 for (
auto& Htmp1: Hexxs[0])
28 const int& iat0 = Htmp1.first;
29 for (
auto& Htmp2: Htmp1.second)
31 const int& iat1 = Htmp2.first.first;
32 if (pv->get_row_size(iat0) > 0 && pv->get_col_size(iat1) > 0)
35 (cell_nearest ? cell_nearest->get_cell_nearest_discrete(iat0, iat1, Htmp2.first.second)
36 : Htmp2.first.second));
37 BaseMatrix<TR>* HlocR = hR->find_matrix(iat0, iat1, R.
x, R.
y, R.
z);
41 AtomPair<TR> tmp(iat0, iat1, R.
x, R.
y, R.
z, pv);
49 hR->allocate(
nullptr,
true);
55void reallocate_hcontainer(
const int nat,
57 const std::array<int, 3>& Rs_period,
58 const RI::Cell_Nearest<int, int, 3, double, 3>*
const cell_nearest)
60 auto* pv = hR->get_paraV();
62 bool need_allocate =
false;
63 for (
int iat0 = 0; iat0 < nat; ++iat0)
65 for (
int iat1 = 0; iat1 < nat; ++iat1)
69 if (pv->get_row_size(iat0) > 0 && pv->get_col_size(iat1) > 0)
74 (cell_nearest ? cell_nearest->get_cell_nearest_discrete(iat0, iat1, cell) : cell));
75 BaseMatrix<TR>* HlocR = hR->find_matrix(iat0, iat1, R.
x, R.
y, R.
z);
80 AtomPair<TR> tmp(iat0, iat1, R.
x, R.
y, R.
z, pv);
89 hR->allocate(
nullptr,
true);
93template <
typename TK,
typename TR>
94OperatorEXX<OperatorLCAO<TK, TR>>::OperatorEXX(
95 HS_Matrix_K<TK>* hsk_in,
96 HContainer<TR>* hR_in,
99 std::vector<std::map<
int, std::map<
TAC, RI::Tensor<double>>>>* Hexxd_in,
100 std::vector<std::map<
int, std::map<
TAC, RI::Tensor<std::complex<double>>>>>* Hexxc_in,
101 Add_Hexx_Type add_hexx_type_in,
103 int* two_level_step_in,
104 const bool restart_in)
105 : OperatorLCAO<TK, TR>(hsk_in, kv_in.kvec_d, hR_in), ucell(ucell_in), kv(kv_in), Hexxd(Hexxd_in), Hexxc(Hexxc_in),
106 add_hexx_type(add_hexx_type_in),
istep(
istep), two_level_step(two_level_step_in),
restart(restart_in)
109 this->cal_type = calculation_type::lcao_exx;
114 auto file_name_list_csr = []() -> std::vector<std::string> {
115 std::vector<std::string> file_name_list;
121 + std::to_string(is) +
".csr");
124 return file_name_list;
126 auto file_name_list_cereal = []() -> std::vector<std::string> {
127 std::vector<std::string> file_name_list;
130 file_name_list.push_back(
"HexxR_" + std::to_string(irank));
132 return file_name_list;
134 auto check_exist = [](
const std::vector<std::string>& file_name_list) ->
bool {
135 for (
const std::string& file_name: file_name_list)
137 std::ifstream ifs(file_name);
146 std::cout <<
" Attention: The number of MPI processes must be strictly identical between SCF and NSCF when computing exact-exchange." << std::endl;
147 if (check_exist(file_name_list_csr()))
149 const std::string file_name_exx_csr
155 if (this->add_hexx_type == Add_Hexx_Type::R)
157 reallocate_hcontainer(*Hexxd, this->hR);
163 if (this->add_hexx_type == Add_Hexx_Type::R)
165 reallocate_hcontainer(*Hexxc, this->hR);
169 else if (check_exist(file_name_list_cereal()))
172 const std::string file_name_exx_cereal
174 std::ifstream ifs(file_name_exx_cereal, std::ios::binary);
182 if (this->add_hexx_type == Add_Hexx_Type::R)
184 reallocate_hcontainer(*Hexxd, this->hR);
190 if (this->add_hexx_type == Add_Hexx_Type::R)
192 reallocate_hcontainer(*Hexxc, this->hR);
200 this->use_cell_nearest =
false;
204 if (this->add_hexx_type == Add_Hexx_Type::R)
208 std::fmod(this->kv.get_koffset(1), 1.0),
209 std::fmod(this->kv.get_koffset(2), 1.0))
213 const std::array<int, 3> Rs_period = {this->kv.nmp[0], this->kv.nmp[1], this->kv.nmp[2]};
214 if (this->use_cell_nearest)
216 this->cell_nearest = init_cell_nearest(ucell, Rs_period);
217 reallocate_hcontainer(ucell.nat, this->hR, Rs_period, &this->cell_nearest);
221 reallocate_hcontainer(ucell.nat, this->hR, Rs_period);
229 assert(this->two_level_step !=
nullptr);
231 if (this->add_hexx_type == Add_Hexx_Type::k)
234 if (std::is_same<TK, double>::value)
236 this->Hexxd_k_load.resize(this->kv.get_nks());
237 for (
int ik = 0; ik < this->kv.get_nks(); ik++)
243 this->Hexxd_k_load[ik].data(),
253 this->Hexxc_k_load.resize(this->kv.get_nks());
254 for (
int ik = 0; ik < this->kv.get_nks(); ik++)
260 this->Hexxc_k_load[ik].data(),
269 else if (this->add_hexx_type == Add_Hexx_Type::R)
272 const std::string restart_HR_path
277 std::ifstream ifs(restart_HR_path +
"_" + std::to_string(is) +
".csr");
303 const std::string restart_HR_path_cereal
305 std::ifstream ifs(restart_HR_path_cereal, std::ios::binary);
306 int all_exist_cereal = ifs ? 1 : 0;
310 if (!all_exist_cereal)
331 std::cout <<
"WARNING: Hexx not found, restart from the non-exx loop." << std::endl
332 <<
"If the loaded charge density is EXX-solved, this may lead to poor convergence."
340template <
typename TK,
typename TR>
341void OperatorEXX<OperatorLCAO<TK, TR>>::contributeHR()
352 else if (this->istep == 0)
355 bool in_gga_pre_loop = (this->two_level_step !=
nullptr && *this->two_level_step == 0);
361 if (in_gga_pre_loop && lacks_good_guess)
368 if (this->add_hexx_type == Add_Hexx_Type::k)
381 *this->hR->get_paraV(),
384 this->use_cell_nearest ? &this->cell_nearest : nullptr);
391 *this->hR->get_paraV(),
394 this->use_cell_nearest ? &this->cell_nearest : nullptr);
399 this->current_spin = 1 - this->current_spin;
403template <
typename TK,
typename TR>
404void OperatorEXX<OperatorLCAO<TK, TR>>::contributeHk(
int ik)
418 else if (this->istep == 0)
421 if (this->two_level_step !=
nullptr && *this->two_level_step > 0)
423 this->initial_gga_done =
true;
427 bool in_gga_pre_loop = (this->two_level_step !=
nullptr && *this->two_level_step == 0);
430 bool lacks_good_guess = (!this->
restart);
435 if (in_gga_pre_loop && lacks_good_guess && !this->initial_gga_done)
442 if (this->add_hexx_type == Add_Hexx_Type::R)
444 throw std::invalid_argument(
"Set Add_Hexx_Type::k to call OperatorEXX::contributeHk().");
449 if (this->restart && this->two_level_step !=
nullptr)
451 if (*this->two_level_step == 0)
453 this->add_loaded_Hexx(ik);
458 if (this->Hexxd_k_load.size() > 0)
460 this->Hexxd_k_load.clear();
461 this->Hexxd_k_load.shrink_to_fit();
463 else if (this->Hexxc_k_load.size() > 0)
465 this->Hexxc_k_load.clear();
466 this->Hexxc_k_load.shrink_to_fit();
478 *this->hR->get_paraV(),
480 this->hsk->get_hk());
491 *this->hR->get_paraV(),
492 this->hsk->get_hk());
501 *this->hR->get_paraV(),
502 this->hsk->get_hk());
Definition abfs-vector3_order.h:16
3 elements vector
Definition vector3.h:24
T x
Definition vector3.h:26
T norm(void) const
Get the norm of a Vector3.
Definition vector3.h:216
T y
Definition vector3.h:27
T z
Definition vector3.h:28
int64_t get_local_size() const
number of local matrix elements
Definition parallel_2d.h:39
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
static TD_info * td_vel_op
pointer to the only TD_info object itself
Definition mock_tdinfo.cpp:13
static ModuleBase::Vector3< double > cart_At
Store the vector potential for tddft calculation.
Definition mock_tdinfo.cpp:12
static int get_func_type()
Definition xc_functional.h:65
Exx_Info exx_info
Definition exx_info.cpp:8
Restart restart
Definition restart.cpp:47
int istep
Definition ions_move_basic.cpp:12
void WARNING_QUIT(const std::string &, const std::string &)
Combine the functions of WARNING and QUIT.
Definition test_delley.cpp:14
void TITLE(const std::string &class_name, const std::string &function_name, const bool disable)
Definition tool_title.cpp:18
void read_Hexxs_cereal(const std::string &file_name, std::vector< std::map< int, std::map< TAC, RI::Tensor< Tdata > > > > &Hexxs)
read Hexxs in cereal format
Definition restart_exx_csr.hpp:64
void read_Hexxs_csr(const std::string &file_name, const UnitCell &ucell, const int nspin, const int nbasis, std::vector< std::map< int, std::map< TAC, RI::Tensor< Tdata > > > > &Hexxs)
read Hexxs in CSR format
Definition restart_exx_csr.hpp:13
void add_Hexx_td(const UnitCell &ucell, const K_Vectors &kv, const int ik, const double alpha, const std::vector< std::map< TA, std::map< TAC, RI::Tensor< Tdata > > > > &Hs, const Parallel_Orbitals &pv, const ModuleBase::Vector3< double > &At, TK *hk)
Definition RI_2D_Comm.hpp:316
void add_HexxR(const int current_spin, const double alpha, const std::vector< std::map< TA, std::map< TAC, RI::Tensor< Tdata > > > > &Hs, const Parallel_Orbitals &pv, const int npol, hamilt::HContainer< TR > &HlocR, const RI::Cell_Nearest< int, int, 3, double, 3 > *const cell_nearest=nullptr)
Definition RI_2D_Comm.hpp:445
void add_Hexx(const UnitCell &ucell, const K_Vectors &kv, const int ik, const double alpha, const std::vector< std::map< TA, std::map< TAC, RI::Tensor< Tdata > > > > &Hs, const Parallel_Orbitals &pv, TK *hk)
Definition RI_2D_Comm.hpp:267
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
ModuleBase::Vector3< Tcell > array3_to_Vector3(const std::array< Tcell, 3 > &v)
Definition RI_Util.h:38
Parameter PARAM
Definition parameter.cpp:3
std::pair< int, TC > TAC
Definition ri_cv_io_test.cpp:10
bool cal_exx
Definition exx_info.h:14
Exx_Info_Global info_global
Definition exx_info.h:37
bool load_H_finish
Definition restart.h:25
std::string global_readin_dir
global readin directory
Definition system_parameter.h:43
int npol
number of polarization
Definition system_parameter.h:52
int nlocal
total number of local basis.
Definition system_parameter.h:23
int myrank
Definition system_parameter.h:11
int nproc
Definition system_parameter.h:12