ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
write_ri_cv.hpp
Go to the documentation of this file.
1// #include "source_lcao/module_ri/lri_cv_tools.h"
4#include <map>
5#include <cstddef>
6#include <dirent.h>
7#define IZ(x) int x = 0;
8#define SZ(x) std::size_t x = 0;
9
10namespace LRI_CV_Tools
11{
12 using TC = std::array<int, 3>;
13 using TAC = std::pair<int, TC>;
14 template <typename T>
15 using TLRI = std::map<int, std::map<TAC, RI::Tensor<T>>>;;
16
17 template<typename T>
18 inline double absmax(const RI::Tensor<T>& t)
19 {
20 double res = 0;
21 for (int i = 0;i < t.get_shape_all();++i)
22 {
23 double v = std::abs(std::norm(t.ptr()[i]));
24 if (v > res) { res = v; }
25 }
26 return std::sqrt(res);
27 }
28
29 template<typename T>
30 TLRI<T> read_Cs_ao(const std::string& file_path, const double& threshold)
31 {
32 IZ(natom) IZ(ncell) IZ(ia1) IZ(ia2) IZ(ic_1) IZ(ic_2) IZ(ic_3)
33 SZ(nw1) SZ(nw2) SZ(nabf)
34 std::ifstream infile;
35 infile.open(file_path);
36 infile >> natom >> ncell; // no use of ncell
37
38 TLRI<T> Cs;
39 while (infile.peek() != EOF)
40 {
41 infile >> ia1 >> ia2 >> ic_1 >> ic_2 >> ic_3 >> nw1 >> nw2 >> nabf;
42 const TC& box = { ic_1, ic_2, ic_3 };
43 RI::Tensor<T> tensor_cs({ nabf, nw1, nw2 });
44 for (std::size_t i = 0; i != nw1; i++) {
45 for (std::size_t j = 0; j != nw2; j++) {
46 for (std::size_t mu = 0; mu != nabf; mu++) {
47 infile >> tensor_cs(mu, i, j);
48 }
49 }
50 }
51 // no screening for data-structure consistency
52 if (absmax(tensor_cs) >= threshold) { Cs[ia1 - 1][{ia2 - 1, box}] = tensor_cs; }
53 }
54 infile.close();
55 return Cs;
56 }
57
58 template<typename T>
59 void write_Cs_ao(const TLRI<T>& Cs, const std::string& file_path)
60 {
61 std::ofstream outfile;
62 outfile.open(file_path);
63 if (Cs.size() == 0)
64 {
65 outfile << " Empty Cs " << std::endl;
66 outfile.close();
67 return;
68 }
69 outfile << Cs.size() << " " << Cs.begin()->second.size() / Cs.size() << std::endl; //natom, (ncell, not real if Cs is incomplete)
70 for (auto& it1 : Cs)
71 {
72 const int& ia1 = it1.first;
73 for (auto& it2 : it1.second)
74 {
75 const int& ia2 = it2.first.first;
76 const auto& box = it2.first.second;
77 const auto& tensor_cs = it2.second;
78 outfile << ia1 + 1 << " " << ia2 + 1 << " " << box[0] << " " << box[1] << " " << box[2] << std::endl;
79 const int& nw1 = tensor_cs.shape[1], nw2 = tensor_cs.shape[2], nabf = tensor_cs.shape[0];
80 outfile << nw1 << " " << nw2 << " " << nabf << std::endl;
81 for (int i = 0; i != nw1; i++)
82 {
83 for (int j = 0; j != nw2; j++)
84 {
85 for (int mu = 0; mu != nabf; mu++) { outfile << tensor_cs(mu, i, j) << " "; }
86 outfile << std::endl;
87 }
88 }
89 }
90 }
91 outfile.close();
92 }
93
94 template<typename T>
95 TLRI<T> read_Cs_ao_all(const std::string& path, const double& threshold)
96 {
97 IZ(natom) IZ(ncell) IZ(ia1) IZ(ia2) IZ(ic_1) IZ(ic_2) IZ(ic_3)
98 SZ(nw1) SZ(nw2) SZ(nabf)
99 TLRI<T> Cs;
100
101 struct dirent *ptr;
102 DIR *dir;
103 dir = opendir(path.c_str());
104 while ((ptr = readdir(dir)) != NULL){// read all the files in the directory
105 std::string fm(ptr->d_name);
106 if (fm.find("Cs_data_") == 0)// find file Cs_data_xxx
107 {
108 ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "found Cs file: " + fm + ", start reading...");
109 std::ifstream infile(path + fm);
110 infile >> natom >> ncell; // no use of ncell
111
112 while (infile.peek() != EOF)
113 {
114 if(!(infile >> ia1 >> ia2 >> ic_1 >> ic_2 >> ic_3 >> nw1 >> nw2 >> nabf)){break;}
115 const TC& box = { ic_1, ic_2, ic_3 };
116 RI::Tensor<T> tensor_cs({ nabf, nw1, nw2 });
117 for (std::size_t i = 0; i != nw1; i++) {
118 for (std::size_t j = 0; j != nw2; j++) {
119 for (std::size_t mu = 0; mu != nabf; mu++) {
120 infile >> tensor_cs(mu, i, j);
121 }
122 }
123 }
124 // no screening for data-structure consistency
125 if (absmax(tensor_cs) >= threshold) { Cs[ia1 - 1][{ia2 - 1, box}] = tensor_cs; }
126 }
127 infile.close();
128 }
129 }
130 closedir(dir);
131 ModuleBase::TITLE("LRI_CV_Tools", "read_Cs_ao_all done.");
132 return Cs;
133 }
134
135 template<typename T>
136 TLRI<T> read_Vs_abf(const std::string& file_path, const double& threshold)
137 {
138 IZ(natom) IZ(ncell) IZ(ia1) IZ(ia2) IZ(ic_1) IZ(ic_2) IZ(ic_3)
139 SZ(nabf1) SZ(nabf2)
140 std::ifstream infile;
141 infile.open(file_path);
142 infile >> natom >> ncell; // no use of ncell
143
144 TLRI<T> Vs;
145 while (infile.peek() != EOF)
146 {
147 infile >> ia1 >> ia2 >> ic_1 >> ic_2 >> ic_3 >> nabf1 >> nabf2;
148 const TC& box = { ic_1, ic_2, ic_3 };
149 RI::Tensor<T> tensor_vs({ nabf1, nabf2 });
150 for (std::size_t i = 0; i != nabf1; i++) {
151 for (std::size_t j = 0; j != nabf2; j++){ infile >> tensor_vs(i, j);}
152}
153 if (absmax(tensor_vs) >= threshold) { Vs[ia1 - 1][{ia2 - 1, box}] = tensor_vs; }
154 // else ++cs_discard;
155 }
156 infile.close();
157 return Vs;
158 }
159
160 template <typename T>
161 void write_Vs_abf(const TLRI<T>& Vs, const std::string& file_path)
162 {
163 std::ofstream outfile;
164 outfile.open(file_path);
165 if (Vs.size() == 0)
166 {
167 outfile << " Empty Vs " << std::endl;
168 outfile.close();
169 return;
170 }
171 outfile << Vs.size() << " " << Vs.begin()->second.size() / Vs.size() << std::endl; //natom, (ncell, not real if Vs is incomplete)
172 for (const auto& it1 : Vs)
173 {
174 const int& ia1 = it1.first;
175 for (const auto& it2 : it1.second)
176 {
177 const int& ia2 = it2.first.first;
178 const auto& box = it2.first.second;
179 const auto& tensor_v = it2.second;
180 outfile << ia1 + 1 << " " << ia2 + 1 << " " << box[0] << " " << box[1] << " " << box[2] << std::endl;
181 outfile << tensor_v.shape[0] << " " << tensor_v.shape[1] << std::endl;
182 for (int i = 0; i != tensor_v.shape[0]; i++)
183 {
184 for (int j = 0; j != tensor_v.shape[1]; j++) {outfile << tensor_v(i, j) << " ";}
185 outfile << std::endl;
186 }
187 }
188 }
189 outfile.close();
190 }
191}
Definition rpa_lri_coeff_v1.h:11
std::ofstream ofs_running
Definition global_variable.cpp:38
Definition lri_cv_tools.h:23
TLRI< T > read_Cs_ao(const std::string &file_path, const double &threshold=1e-10)
Definition write_ri_cv.hpp:30
std::array< int, 3 > TC
Definition lri_cv_tools.h:151
void write_Cs_ao(const TLRI< T > &Vs, const std::string &file_path)
Definition write_ri_cv.hpp:59
TLRI< T > read_Vs_abf(const std::string &file_path, const double &threshold=1e-10)
Definition write_ri_cv.hpp:136
std::map< int, std::map< TAC, RI::Tensor< T > > > TLRI
Definition lri_cv_tools.h:154
TLRI< T > read_Cs_ao_all(const std::string &path, const double &threshold=1e-10)
Definition write_ri_cv.hpp:95
double absmax(const RI::Tensor< T > &t)
Definition write_ri_cv.hpp:18
void write_Vs_abf(const TLRI< T > &Vs, const std::string &file_path)
Definition write_ri_cv.hpp:161
void DONE(std::ofstream &ofs, const std::string &description, const bool only_rank0)
Definition global_function.cpp:78
void TITLE(const std::string &class_name, const std::string &function_name, const bool disable)
Definition tool_title.cpp:18
#define threshold
Definition sph_bessel_rec_test.cpp:4
std::array< int, 3 > TC
Definition test_write_ri_cv.cpp:9
std::pair< int, TC > TAC
Definition test_write_ri_cv.cpp:10
std::map< int, std::map< TAC, RI::Tensor< T > > > TLRI
Definition test_write_ri_cv.cpp:12
#define SZ(x)
Definition write_ri_cv.hpp:8
#define IZ(x)
Definition write_ri_cv.hpp:7