ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
lr_util_print.h
Go to the documentation of this file.
1#pragma once
2#include <ATen/tensor.h>
3#include "source_psi/psi.h"
4#ifdef __EXX
5#include <RI/global/Tensor.h>
6#endif
7namespace LR_Util
8{
9 template<typename T>
10 constexpr T filter(const T& v, const double& threshold = 1e-10)
11 {
12 return (std::abs(v) > threshold ? v : 0);
13 }
14
15 template<typename T>
16 int read_value(std::ifstream& ifs, T* ptr, const int& size) { for (int i = 0;i < size;++i) { ifs >> ptr[i]; } return size; }
17 template<typename T, typename... Args>
18 int read_value(std::ifstream& ifs, T* ptr, const int& size, Args&&... args)
19 {
20 int size_now = 0;
21 for (int i = 0;i < size;++i) { size_now += read_value(ifs, ptr + size_now, args...); }
22 return size_now;
23 }
24 template<typename T, typename... Args>
25 int read_value(const std::string& file, T* ptr, const int& size, Args&&... args)
26 {
27 std::ifstream ifs(file);
28 const int res = read_value(ifs, ptr, size, args...);
29 ifs.close();
30 return res;
31 }
32
33 template<typename T>
34 int write_value(std::ofstream& ofs, const T* ptr, const int& size)
35 {
36 for (int i = 0;i < size;++i) { ofs << filter(ptr[i]) << " "; }
37 ofs << std::endl;
38 return size;
39 }
40 template<typename T, typename... Args>
41 int write_value(std::ofstream& ofs, const T* ptr, const int& size, Args&&... args)
42 {
43 int size_now = 0;
44 for (int i = 0;i < size;++i) { size_now += write_value(ofs, ptr + size_now, args...); }
45 ofs << std::endl;
46 return size_now;
47 }
48 template<typename T, typename... Args>
49 int write_value(const std::string& file, const int& prec, const T* ptr, const int& size, Args&&... args)
50 {
51 std::ofstream ofs(file);
52 ofs << std::setprecision(prec) << std::scientific;
53 const int res = write_value(ofs, ptr, size, args...);
54 ofs.close();
55 return res;
56 }
57 template<typename T>
58 int print_value(const T* ptr, const int& size)
59 {
60 for (int i = 0;i < size;++i) { std::cout << filter(ptr[i]) << " "; }
61 std::cout << std::endl;
62 return size;
63 }
64 template<typename T, typename... Args>
65 int print_value(const T* ptr, const int& size, Args&&... args)
66 {
67 int size_now = 0;
68 for (int i = 0;i < size;++i) { size_now += print_value(ptr + size_now, args...); }
69 std::cout << std::endl;
70 return size_now;
71 }
72
73 template<typename T>
74 void print_psi_bandfirst(const psi::Psi<T>& psi, const std::string& label, const int& ib, const double& threshold = 1e-10)
75 {
76 assert(psi.get_k_first() == 0);
77 std::cout << label << ": band " << ib << "\n";
78 for (int ik = 0;ik < psi.get_nk();++ik)
79 {
80 std::cout << "iks=" << ik << "\n";
81 for (int i = 0;i < psi.get_nbasis();++i)
82 {
83 std::cout << filter(psi(ib, ik, i)) << " ";
84 }
85 std::cout << "\n";
86 }
87 }
88 template<typename T>
89 void write_psi_bandfirst(const psi::Psi<T>& psi, const std::string& filename, const int& rank, const double& threshold = 1e-10, const int& precision = 8)
90 {
91 assert(psi.get_k_first() == 0);
92 std::ofstream ofs(filename + "_" + std::to_string(rank) + ".dat");
93 ofs << std::setprecision(precision) << std::scientific;
94 ofs << psi.get_nbands() << " " << psi.get_nk() << " " << psi.get_nbasis() << "\n";
95 assert(psi.size() == write_value(ofs, &psi(0, 0, 0), psi.get_nbands(), psi.get_nk(), psi.get_nbasis()));
96 ofs.close();
97 }
98 template<typename T>
99 void write_psi_bandfirst(const T* psi, const int& nband, const int& nk, const int& nbasis,
100 const std::string& filename, const int& rank, const double& threshold = 1e-10, const int& precision = 8)
101 {
102 std::ofstream ofs(filename + "_" + std::to_string(rank) + ".dat");
103 ofs << std::setprecision(precision) << std::scientific;
104 ofs << nband << " " << nk << " " << nbasis << "\n";
105 assert(nband * nk * nbasis == write_value(ofs, psi, nband, nk, nbasis));
106 ofs.close();
107 }
108 template<typename T>
109 psi::Psi<T> read_psi_bandfirst(const std::string& filename, const int& rank)
110 {
111 std::ifstream ifs(filename + "_" + std::to_string(rank) + ".dat");
112 int nbands, nks, nbasis;
113 ifs >> nbands >> nks >> nbasis;
114 psi::Psi<T> psi(nks, nbands, nbasis, nullptr, false);
115 assert(psi.size() == read_value(ifs, psi.get_pointer(), nbands, nks, nbasis));
116 ifs.close();
117 return psi;
118 }
119 template<typename T >
120 void print_psi_kfirst(const psi::Psi<T>& psi, const std::string& label, const double& threshold = 1e-10)
121 {
122 assert(psi.get_k_first() == 1);
123 for (int ik = 0;ik < psi.get_nk();++ik)
124 {
125 std::cout << label << ": k " << ik << "\n";
126 for (int ib = 0;ib < psi.get_nbands();++ib)
127 {
128 std::cout << "ib=" << ib << ": ";
129 for (int i = 0;i < psi.get_nbasis();++i)
130 {
131 std::cout << filter(psi(ik, ib, i)) << " ";
132 }
133 std::cout << "\n";
134 }
135 }
136 }
137 template<typename T>
138 void print_tensor(const container::Tensor& t, const std::string& label, const Parallel_2D* pmat, const double& threshold = 1e-10)
139 {
140 std::cout << label << "\n";
141 for (int j = 0; j < pmat->get_col_size();++j)
142 {
143 for (int i = 0;i < pmat->get_row_size();++i)
144 {
145 std::cout << filter(t.data<T>()[j * pmat->get_row_size() + i]) << " ";
146 }
147 std::cout << std::endl;
148 }
149 std::cout << "\n";
150 }
151 template<typename T>
152 void print_grid_nonzero(T* rho, const int& nrxx, const int& nnz, const std::string& label, const double& threshold = 1e-5)
153 {
154 std::cout << "first " << nnz << " non-zero elements of " << label << "\n";
155 int inz = 0;int i = 0;
156 while (inz < nnz && i < nrxx) { if (rho[++i] - 0.0 > threshold) { std::cout << rho[i] << " ";++inz; } };
157 }
158
159
160 using TC = std::array<int, 3>;
161 using TAC = std::pair<int, TC>;
162 template<typename T>
163 using TLRIX = std::map<int, std::map<TAC, std::vector<T>>>;
164
165 template<typename T>
166 void print_CsX(const TLRIX<T>& CsX, const int nvirt, const std::string& label, const double& threshold = 1e-10)
167 {
168 std::cout << label << "\n";
169 for (const auto& tmp1 : CsX)
170 {
171 const int& iat1 = tmp1.first;
172 for (const auto& tmp2 : tmp1.second)
173 {
174 const int& iat2 = tmp2.first.first;
175 const auto& R = tmp2.first.second;
176 auto& t = tmp2.second;
177 const int nocc = t.size() / nvirt;
178 std::cout << "iat1=" << iat1 << " iat2=" << iat2 << " R=(" << R[0] << " " << R[1] << " " << R[2] << ")\n";
179 for (int io = 0;io < nocc;++io)
180 {
181 for (int iv = 0;iv < nvirt;++iv) { std::cout << filter(t[io * nvirt + iv]) << " "; }
182 std::cout << "\n";
183 }
184 std::cout << "\n";
185 }
186 }
187 }
188
189#ifdef __EXX
190 template <typename T>
191 using TLRI = std::map<int, std::map<TAC, RI::Tensor<T>>>;
192 template<typename T>
193 void print_CV(const TLRI<T>& Cs, const std::string& label, const double& threshold = 1e-10)
194 {
195 std::cout << label << "\n";
196 for (const auto& tmp1 : Cs)
197 {
198 const int& iat1 = tmp1.first;
199 for (const auto& tmp2 : tmp1.second)
200 {
201 const int& iat2 = tmp2.first.first;
202 const auto& R = tmp2.first.second;
203 auto& t = tmp2.second;
204 if (R != TC({ 0, 0, 0 })) {continue;} // for test
205 std::cout << "iat1=" << iat1 << " iat2=" << iat2 << " R=(" << R[0] << " " << R[1] << " " << R[2] << ")\n";
206 if (t.shape.size() == 2)
207 {
208 for (int iabf1 = 0;iabf1 < t.shape[0];++iabf1)
209 {
210 for (int iabf2 = 0;iabf2 < t.shape[1];++iabf2)
211 {
212 std::cout << filter(t(iabf1, iabf2)) << " ";
213 }
214 std::cout << "\n";
215 }
216 }
217 else if (t.shape.size() == 3)
218 {
219 const int nabf = t.shape[0];
220 const int nw1 = t.shape[1];
221 const int nw2 = t.shape[2];
222 std::cout << "size: " << nabf << " " << nw1 << " " << nw2 << "\n";
223 for (int iabf = 0;iabf < nabf;++iabf)
224 {
225 for (int iw1 = 0;iw1 < nw1;++iw1)
226 {
227 for (int iw2 = 0;iw2 < nw2;++iw2)
228 {
229 std::cout << filter(t(iabf, iw1, iw2)) << " ";
230 }
231 std::cout << "\n";
232 }
233 std::cout << "\n";
234 }
235 }
236 }
237 }
238 }
239#endif
240}
This class packs the basic information of 2D-block-cyclic parallel distribution of an arbitrary matri...
Definition parallel_2d.h:12
int get_row_size() const
number of local rows
Definition parallel_2d.h:21
int get_col_size() const
number of local columns
Definition parallel_2d.h:27
A multi-dimensional array of elements of a single data type.
Definition tensor.h:32
void * data() const
Get a pointer to the data buffer of the tensor.
Definition tensor.cpp:73
Definition psi.h:37
#define T
Definition exp.cpp:237
Definition lr_util.cpp:6
void print_psi_bandfirst(const psi::Psi< T > &psi, const std::string &label, const int &ib, const double &threshold=1e-10)
Definition lr_util_print.h:74
psi::Psi< T > read_psi_bandfirst(const std::string &filename, const int &rank)
Definition lr_util_print.h:109
void print_grid_nonzero(T *rho, const int &nrxx, const int &nnz, const std::string &label, const double &threshold=1e-5)
Definition lr_util_print.h:152
std::array< int, 3 > TC
Definition lr_util_print.h:160
void print_psi_kfirst(const psi::Psi< T > &psi, const std::string &label, const double &threshold=1e-10)
Definition lr_util_print.h:120
int read_value(std::ifstream &ifs, T *ptr, const int &size)
Definition lr_util_print.h:16
std::pair< int, TC > TAC
Definition lr_util_print.h:161
constexpr T filter(const T &v, const double &threshold=1e-10)
Definition lr_util_print.h:10
int write_value(std::ofstream &ofs, const T *ptr, const int &size)
Definition lr_util_print.h:34
std::map< int, std::map< TAC, std::vector< T > > > TLRIX
Definition lr_util_print.h:163
void write_psi_bandfirst(const psi::Psi< T > &psi, const std::string &filename, const int &rank, const double &threshold=1e-10, const int &precision=8)
Definition lr_util_print.h:89
void print_CsX(const TLRIX< T > &CsX, const int nvirt, const std::string &label, const double &threshold=1e-10)
Definition lr_util_print.h:166
int print_value(const T *ptr, const int &size)
Definition lr_util_print.h:58
void print_tensor(const container::Tensor &t, const std::string &label, const Parallel_2D *pmat, const double &threshold=1e-10)
Definition lr_util_print.h:138
Definition exx_lip.h:23
std::map< int, std::map< TAC, RI::Tensor< T > > > TLRI
Definition ri_cv_io_test.cpp:12
file(GLOB ATen_CORE_SRCS "*.cpp") set(ATen_CPU_SRCS $
Definition CMakeLists.txt:1
#define threshold
Definition sph_bessel_recursive_test.cpp:4