ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
restart_exx_csr.hpp
Go to the documentation of this file.
1#pragma once
7#include <RI/global/Tensor.h>
8#include <map>
9
10namespace ModuleIO
11{
12 template<typename Tdata>
13 void read_Hexxs_csr(const std::string& file_name, const UnitCell& ucell,
14 const int nspin, const int nbasis,
15 std::vector<std::map<int, std::map<TAC, RI::Tensor<Tdata>>>>& Hexxs)
16 {
17 ModuleBase::TITLE("ModuleIO", "read_Hexxs_csr");
18 Hexxs.resize(nspin);
19 for (int is = 0;is < nspin;++is)
20 {
21 ModuleIO::csrFileReader<Tdata> csr(file_name + "_" + std::to_string(is) + ".csr");
22 int nR = csr.getNumberOfR();
23 int nbasis = csr.getMatrixDimension();
24 // allocate Hexxs[is]
25 for (int iat1 = 0; iat1 < ucell.nat; ++iat1) {
26 for (int iat2 = 0;iat2 < ucell.nat;++iat2) {
27 for (int iR = 0;iR < nR;++iR)
28 {
29 const std::vector<int>& R = csr.getRCoordinate(iR);
30 TC dR({ R[0], R[1], R[2] });
31 Hexxs[is][iat1][{iat2, dR}] = RI::Tensor<Tdata>(
32 {
33 static_cast<size_t>(ucell.atoms[ucell.iat2it[iat1]].nw),
34 static_cast<size_t>(ucell.atoms[ucell.iat2it[iat2]].nw)
35 }
36 );
37 }
38 }
39 }
40#ifdef _OPENMP
41#pragma omp for schedule(dynamic)
42#endif
43 for (int iR = 0;iR < nR;++iR)
44 {
45 const std::vector<int>& R = csr.getRCoordinate(iR);
46 const SparseMatrix<Tdata>& matrix = csr.getMatrix(iR);
47 for (auto& ijv : matrix.getElements())
48 {
49 const int& npol = ucell.get_npol();
50 const int& i = ijv.first.first * npol;
51 const int& j = ijv.first.second * npol;
52 Hexxs.at(is).at(ucell.iwt2iat[i]).at(
53 {
54 ucell.iwt2iat[j],
55 { R[0], R[1], R[2] }
56 }
57 )(ucell.iwt2iw[i] / npol, ucell.iwt2iw[j] / npol) = ijv.second;
58 }
59 }
60 }
61 }
62
63 template<typename Tdata>
64 void read_Hexxs_cereal(const std::string& file_name,
65 std::vector<std::map<int, std::map<TAC, RI::Tensor<Tdata>>>>& Hexxs)
66 {
67 ModuleBase::TITLE("ModuleIO", "read_Hexxs_cereal");
68 ModuleBase::timer::tick("Exx_LRI", "read_Hexxs_cereal");
69 std::ifstream ifs(file_name, std::ios::binary);
70 if(!ifs.is_open())
71 { ModuleBase::WARNING_QUIT("read_Hexxs_cereal", file_name+" not found."); }
72 cereal::BinaryInputArchive iar(ifs);
73 iar(Hexxs);
74 ModuleBase::timer::tick("Exx_LRI", "read_Hexxs_cereal");
75 }
76
77 template<typename Tdata>
78 std::map<Abfs::Vector3_Order<int>, std::map<size_t, std::map<size_t, Tdata>>>
79 calculate_RI_Tensor_sparse(const double& sparse_threshold,
80 const std::map<int, std::map<TAC, RI::Tensor<Tdata>>>& Hexxs,
81 const UnitCell& ucell)
82 {
83 ModuleBase::TITLE("ModuleIO", "calculate_HContainer_sparse_d");
84 std::map<Abfs::Vector3_Order<int>, std::map<size_t, std::map<size_t, Tdata>>> target;
85 for (auto& a1_a2R_data : Hexxs)
86 {
87 int iat1 = a1_a2R_data.first;
88 for (auto& a2R_data : a1_a2R_data.second)
89 {
90 int iat2 = a2R_data.first.first;
91 int nw1 = ucell.atoms[ucell.iat2it[iat1]].nw;
92 int nw2 = ucell.atoms[ucell.iat2it[iat2]].nw;
93 int start1 = ucell.atoms[ucell.iat2it[iat1]].stapos_wf / ucell.get_npol() + ucell.iat2ia[iat1] * nw1;
94 int start2 = ucell.atoms[ucell.iat2it[iat2]].stapos_wf / ucell.get_npol() + ucell.iat2ia[iat2] * nw2;
95
96 const TC& R = a2R_data.first.second;
97 auto& matrix = a2R_data.second;
98 Abfs::Vector3_Order<int> dR(R[0], R[1], R[2]);
99 for (int i = 0;i < nw1;++i) {
100 for (int j = 0;j < nw2;++j) {
101 target[dR][start1 + i][start2 + j] =
102 ((std::abs(matrix(i, j)) > sparse_threshold) ? matrix(i, j) : static_cast<Tdata>(0));
103 }
104 }
105 }
106 }
107 return target;
108 }
109
110 template<typename Tdata>
111 void write_Hexxs_csr(const std::string& file_name, const UnitCell& ucell,
112 const std::vector<std::map<int, std::map<TAC, RI::Tensor<Tdata>>>>& Hexxs)
113 {
114 ModuleBase::TITLE("ModuleIO", "write_Hexxs_csr");
115 std::set<Abfs::Vector3_Order<int>> all_R_coor;
116 double sparse_threshold = 1e-10;
117 for (int is = 0;is < Hexxs.size();++is)
118 {
119 for (const auto& HexxA : Hexxs[is])
120 {
121 const int iat0 = HexxA.first;
122 for (const auto& HexxB : HexxA.second)
123 {
124 const int iat1 = HexxB.first.first;
125 const Abfs::Vector3_Order<int> R = RI_Util::array3_to_Vector3(HexxB.first.second);
126 all_R_coor.insert(R);
127 }
128 }
130 calculate_RI_Tensor_sparse(sparse_threshold, Hexxs[is], ucell),
131 all_R_coor,
132 sparse_threshold,
133 false, //binary
134 file_name + "_" + std::to_string(is) + ".csr",
136 "Hexxs_" + std::to_string(is),
137 -1,
138 false); //no reduce, one file for each process
139 }
140 }
141}
Definition abfs-vector3_order.h:16
int nw
Definition atom_spec.h:23
int stapos_wf
Definition atom_spec.h:33
static void tick(const std::string &class_name_in, const std::string &name_in)
Use twice at a time: the first time, set start_flag to false; the second time, calculate the time dur...
Definition timer.cpp:57
Sparse matrix class designed mainly for csr format input and output.
Definition sparse_matrix.h:24
const std::map< std::pair< int, int >, T > & getElements() const
Definition sparse_matrix.h:90
Class to read CSR file.
Definition csr_reader.h:35
std::vector< int > getRCoordinate(int index) const
Definition csr_reader.cpp:80
int getMatrixDimension() const
Definition csr_reader.cpp:123
SparseMatrix< T > getMatrix(int Rx, int Ry, int Rz)
Definition csr_reader.cpp:102
int getNumberOfR() const
Definition csr_reader.cpp:116
Definition parallel_orbitals.h:9
Definition unitcell.h:16
int *& iat2it
Definition unitcell.h:47
Atom * atoms
Definition unitcell.h:18
const int & get_npol() const
Definition unitcell.h:76
int *& iwt2iw
Definition unitcell.h:50
int & nat
Definition unitcell.h:46
int *& iat2ia
Definition unitcell.h:48
int *& iwt2iat
Definition unitcell.h:49
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
This class has two functions: restart psi from the previous calculation, and write psi to the disk.
Definition cal_dos.h:9
std::pair< int, TC > TAC
Definition restart_exx_csr.h:11
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 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
std::map< Abfs::Vector3_Order< int >, std::map< size_t, std::map< size_t, Tdata > > > calculate_RI_Tensor_sparse(const double &sparse_threshold, const std::vector< std::map< int, std::map< TAC, RI::Tensor< Tdata > > > > &Hexxs, const UnitCell &ucell)
void save_sparse(const std::map< Abfs::Vector3_Order< int >, std::map< size_t, std::map< size_t, Tdata > > > &smat, const std::set< Abfs::Vector3_Order< int > > &all_R_coor, const double &sparse_thr, const bool &binary, const std::string &filename, const Parallel_Orbitals &pv, const std::string &label, const int &istep=-1, const bool &reduce=true)
Definition write_HS_sparse.cpp:704
std::array< int, 3 > TC
Definition restart_exx_csr.h:10
ModuleBase::Vector3< Tcell > array3_to_Vector3(const std::array< Tcell, 3 > &v)
Definition RI_Util.h:38