Skip to content

Commit bf89de3

Browse files
committed
Add support for reading old version HexxR* files.
1 parent 67076fd commit bf89de3

File tree

3 files changed

+74
-9
lines changed

3 files changed

+74
-9
lines changed

source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/op_exx_lcao.hpp

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,40 @@ OperatorEXX<OperatorLCAO<TK, TR>>::OperatorEXX(HS_Matrix_K<TK>* hsk_in,
102102
if (PARAM.inp.calculation == "nscf" && GlobalC::exx_info.info_global.cal_exx)
103103
{ // if nscf, read HexxR first and reallocate hR according to the read-in HexxR
104104
const std::string file_name_exx = PARAM.globalv.global_readin_dir + "HexxR" + std::to_string(GlobalV::MY_RANK);
105-
if (GlobalC::exx_info.info_ri.real_number)
105+
bool all_exist = true;
106+
for (int is=0;is<PARAM.inp.nspin;++is)
107+
{
108+
std::ifstream ifs(file_name_exx + "_" + std::to_string(is) + ".csr");
109+
if (!ifs) { all_exist = false; break; }
110+
}
111+
if (all_exist)
106112
{
107-
ModuleIO::read_Hexxs_csr(file_name_exx, GlobalC::ucell, PARAM.inp.nspin, PARAM.globalv.nlocal, *Hexxd);
108-
if (this->add_hexx_type == Add_Hexx_Type::R) { reallocate_hcontainer(*Hexxd, this->hR); }
113+
// Read HexxR in CSR format
114+
if (GlobalC::exx_info.info_ri.real_number)
115+
{
116+
ModuleIO::read_Hexxs_csr(file_name_exx, GlobalC::ucell, PARAM.inp.nspin, PARAM.globalv.nlocal, *Hexxd);
117+
if (this->add_hexx_type == Add_Hexx_Type::R) { reallocate_hcontainer(*Hexxd, this->hR); }
118+
}
119+
else
120+
{
121+
ModuleIO::read_Hexxs_csr(file_name_exx, GlobalC::ucell, PARAM.inp.nspin, PARAM.globalv.nlocal, *Hexxc);
122+
if (this->add_hexx_type == Add_Hexx_Type::R) { reallocate_hcontainer(*Hexxc, this->hR); }
123+
}
109124
}
110125
else
111126
{
112-
ModuleIO::read_Hexxs_csr(file_name_exx, GlobalC::ucell, PARAM.inp.nspin, PARAM.globalv.nlocal, *Hexxc);
113-
if (this->add_hexx_type == Add_Hexx_Type::R) { reallocate_hcontainer(*Hexxc, this->hR); }
127+
// Read HexxR in binary format (old version)
128+
const std::string file_name_exx_cereal = PARAM.globalv.global_readin_dir + "HexxR_" + std::to_string(GlobalV::MY_RANK);
129+
if (GlobalC::exx_info.info_ri.real_number)
130+
{
131+
ModuleIO::read_Hexxs_cereal(file_name_exx_cereal, *Hexxd);
132+
if (this->add_hexx_type == Add_Hexx_Type::R) { reallocate_hcontainer(*Hexxd, this->hR); }
133+
}
134+
else
135+
{
136+
ModuleIO::read_Hexxs_cereal(file_name_exx_cereal, *Hexxc);
137+
if (this->add_hexx_type == Add_Hexx_Type::R) { reallocate_hcontainer(*Hexxc, this->hR); }
138+
}
114139
}
115140
this->use_cell_nearest = false;
116141
}
@@ -179,11 +204,32 @@ OperatorEXX<OperatorLCAO<TK, TR>>::OperatorEXX(HS_Matrix_K<TK>* hsk_in,
179204
{
180205
// read in Hexx(R)
181206
const std::string restart_HR_path = GlobalC::restart.folder + "HexxR" + std::to_string(GlobalV::MY_RANK);
182-
if (GlobalC::exx_info.info_ri.real_number) {
183-
ModuleIO::read_Hexxs_csr(restart_HR_path, GlobalC::ucell, PARAM.inp.nspin, PARAM.globalv.nlocal, *Hexxd);
207+
bool all_exist = true;
208+
for (int is = 0; is < PARAM.inp.nspin; ++is)
209+
{
210+
std::ifstream ifs(restart_HR_path + "_" + std::to_string(is) + ".csr");
211+
if (!ifs) { all_exist = false; break; }
212+
}
213+
if (all_exist)
214+
{
215+
// Read HexxR in CSR format
216+
if (GlobalC::exx_info.info_ri.real_number) {
217+
ModuleIO::read_Hexxs_csr(restart_HR_path, GlobalC::ucell, PARAM.inp.nspin, PARAM.globalv.nlocal, *Hexxd);
218+
}
219+
else {
220+
ModuleIO::read_Hexxs_csr(restart_HR_path, GlobalC::ucell, PARAM.inp.nspin, PARAM.globalv.nlocal, *Hexxc);
221+
}
184222
}
185-
else {
186-
ModuleIO::read_Hexxs_csr(restart_HR_path, GlobalC::ucell, PARAM.inp.nspin, PARAM.globalv.nlocal, *Hexxc);
223+
else
224+
{
225+
// Read HexxR in binary format (old version)
226+
const std::string restart_HR_path_cereal = GlobalC::restart.folder + "HexxR_" + std::to_string(GlobalV::MY_RANK);
227+
if (GlobalC::exx_info.info_ri.real_number) {
228+
ModuleIO::read_Hexxs_cereal(restart_HR_path_cereal, *Hexxd);
229+
}
230+
else {
231+
ModuleIO::read_Hexxs_cereal(restart_HR_path_cereal, *Hexxc);
232+
}
187233
}
188234
}
189235

source/module_io/restart_exx_csr.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "module_base/abfs-vector3_order.h"
33
#include "module_cell/unitcell.h"
44
#include <RI/global/Tensor.h>
5+
#include "module_ri/serialization_cereal.h"
56
#include <map>
67

78
namespace ModuleIO
@@ -15,6 +16,11 @@ namespace ModuleIO
1516
const int nspin, const int nbasis,
1617
std::vector<std::map<int, std::map<TAC, RI::Tensor<Tdata>>>>& Hexxs);
1718

19+
/// read Hexxs in cereal format
20+
template<typename Tdata>
21+
void read_Hexxs_cereal(const std::string& file_name,
22+
std::vector<std::map<int, std::map<TAC, RI::Tensor<Tdata>>>>& Hexxs);
23+
1824
/// write Hexxs in CSR format
1925
template<typename Tdata>
2026
void write_Hexxs_csr(const std::string& file_name, const UnitCell& ucell,

source/module_io/restart_exx_csr.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "module_cell/unitcell.h"
44
#include "module_io/csr_reader.h"
55
#include "module_io/write_HS_sparse.h"
6+
#include "module_ri/serialization_cereal.h"
67
#include <RI/global/Tensor.h>
78
#include <map>
89

@@ -49,6 +50,18 @@ namespace ModuleIO
4950
}
5051
}
5152

53+
template<typename Tdata>
54+
void read_Hexxs_cereal(const std::string& file_name,
55+
std::vector<std::map<int, std::map<TAC, RI::Tensor<Tdata>>>>& Hexxs)
56+
{
57+
ModuleBase::TITLE("Exx_LRI", "read_Hexxs_cereal");
58+
ModuleBase::timer::tick("Exx_LRI", "read_Hexxs_cereal");
59+
std::ifstream ifs(file_name, std::ios::binary);
60+
cereal::BinaryInputArchive iar(ifs);
61+
iar(Hexxs);
62+
ModuleBase::timer::tick("Exx_LRI", "read_Hexxs_cereal");
63+
}
64+
5265
template<typename Tdata>
5366
std::map<Abfs::Vector3_Order<int>, std::map<size_t, std::map<size_t, Tdata>>>
5467
calculate_RI_Tensor_sparse(const double& sparse_threshold,

0 commit comments

Comments
 (0)