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#ifndef ABACUS_SOURCE_LCAO_MODULE_LR_UTILS_LR_UTIL_PRINT_H
2#define ABACUS_SOURCE_LCAO_MODULE_LR_UTILS_LR_UTIL_PRINT_H
3
4#include <ATen/tensor.h>
6#include <iomanip>
7#include <ostream>
8#include "source_psi/psi.h"
9#ifdef __EXX
10#include <RI/global/Tensor.h>
11#endif
12namespace LR_Util
13{
16 inline void print_excitation_energies(std::ostream& log, const double* energies,
17 const int nstates, const std::string& channel)
18 {
19 const std::ios_base::fmtflags flags = log.flags();
20 const std::streamsize precision = log.precision();
21 log << " LR excitation energies: " << channel << '\n';
22 log << " State Excitation Energy (Ry, eV)\n";
23 log << std::defaultfloat << std::setprecision(6);
24 for (int state = 0; state < nstates; ++state)
25 {
26 log << state << ' ' << energies[state] << ' '
27 << energies[state] * ModuleBase::Ry_to_eV << '\n';
28 }
29 log << " End LR excitation energies\n";
30 log.flags(flags);
31 log.precision(precision);
32 }
33
34 template<typename T>
35 constexpr T filter(const T& v, const double& threshold = 1e-10)
36 {
37 return (std::abs(v) > threshold ? v : 0);
38 }
39
40 template<typename T>
41 int read_value(std::ifstream& ifs, T* ptr, const int& size) { for (int i = 0;i < size;++i) { ifs >> ptr[i]; } return size; }
42 template<typename T, typename... Args>
43 int read_value(std::ifstream& ifs, T* ptr, const int& size, Args&&... args)
44 {
45 int size_now = 0;
46 for (int i = 0;i < size;++i) { size_now += read_value(ifs, ptr + size_now, args...); }
47 return size_now;
48 }
49 template<typename T, typename... Args>
50 int read_value(const std::string& file, T* ptr, const int& size, Args&&... args)
51 {
52 std::ifstream ifs(file);
53 if (!ifs.is_open()){
54 std::cerr << "Error: Cannot open file " << file << std::endl;
55 return -1;
56 }
57 const int res = read_value(ifs, ptr, size, args...);
58 ifs.close();
59 return res;
60 }
61
62 template<typename T>
63 int write_value(std::ofstream& ofs, const T* ptr, const int& size)
64 {
65 for (int i = 0;i < size;++i) { ofs << filter(ptr[i]) << " "; }
66 ofs << std::endl;
67 return size;
68 }
69 template<typename T, typename... Args>
70 int write_value(std::ofstream& ofs, const T* ptr, const int& size, Args&&... args)
71 {
72 int size_now = 0;
73 for (int i = 0;i < size;++i) { size_now += write_value(ofs, ptr + size_now, args...); }
74 ofs << std::endl;
75 return size_now;
76 }
77 template<typename T, typename... Args>
78 int write_value(const std::string& file, const int& prec, const T* ptr, const int& size, Args&&... args)
79 {
80 std::ofstream ofs(file);
81 if (!ofs.is_open()){
82 std::cerr << "Error: Cannot open file " << file << std::endl;
83 return -1;
84 }
85 ofs << std::setprecision(prec) << std::scientific;
86 const int res = write_value(ofs, ptr, size, args...);
87 ofs.close();
88 return res;
89 }
90 template<typename T>
91 int print_value(const T* ptr, const int& size)
92 {
93 for (int i = 0;i < size;++i) { std::cout << filter(ptr[i]) << " "; }
94 std::cout << std::endl;
95 return size;
96 }
97 template<typename T, typename... Args>
98 int print_value(const T* ptr, const int& size, Args&&... args)
99 {
100 int size_now = 0;
101 for (int i = 0;i < size;++i) { size_now += print_value(ptr + size_now, args...); }
102 std::cout << std::endl;
103 return size_now;
104 }
105
106 template<typename T>
107 void print_psi_bandfirst(const psi::Psi<T>& psi, const std::string& label, const int& ib, const double& threshold = 1e-10)
108 {
109 assert(psi.get_k_first() == 0);
110 std::cout << label << ": band " << ib << "\n";
111 for (int ik = 0;ik < psi.get_nk();++ik)
112 {
113 std::cout << "iks=" << ik << "\n";
114 for (int i = 0;i < psi.get_nbasis();++i)
115 {
116 std::cout << filter(psi(ib, ik, i)) << " ";
117 }
118 std::cout << "\n";
119 }
120 }
121 template<typename T>
122 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)
123 {
124 assert(psi.get_k_first() == 0);
125 std::ofstream ofs(filename + "_" + std::to_string(rank) + ".dat");
126 ofs << std::setprecision(precision) << std::scientific;
127 ofs << psi.get_nbands() << " " << psi.get_nk() << " " << psi.get_nbasis() << "\n";
128 assert(psi.size() == write_value(ofs, &psi(0, 0, 0), psi.get_nbands(), psi.get_nk(), psi.get_nbasis()));
129 ofs.close();
130 }
131 template<typename T>
132 void write_psi_bandfirst(const T* psi, const int& nband, const int& nk, const int& nbasis,
133 const std::string& filename, const int& rank, const double& threshold = 1e-10, const int& precision = 8)
134 {
135 std::ofstream ofs(filename + "_" + std::to_string(rank) + ".dat");
136 ofs << std::setprecision(precision) << std::scientific;
137 ofs << nband << " " << nk << " " << nbasis << "\n";
138 assert(nband * nk * nbasis == write_value(ofs, psi, nband, nk, nbasis));
139 ofs.close();
140 }
141 template<typename T>
142 psi::Psi<T> read_psi_bandfirst(const std::string& filename, const int& rank)
143 {
144 std::ifstream ifs(filename + "_" + std::to_string(rank) + ".dat");
145 int nbands, nks, nbasis;
146 ifs >> nbands >> nks >> nbasis;
147 psi::Psi<T> psi(nks, nbands, nbasis, nullptr, false);
148 assert(psi.size() == read_value(ifs, psi.get_pointer(), nbands, nks, nbasis));
149 ifs.close();
150 return psi;
151 }
152 template<typename T >
153 void print_psi_kfirst(const psi::Psi<T>& psi, const std::string& label, const double& threshold = 1e-10)
154 {
155 assert(psi.get_k_first() == 1);
156 for (int ik = 0;ik < psi.get_nk();++ik)
157 {
158 std::cout << label << ": k " << ik << "\n";
159 for (int ib = 0;ib < psi.get_nbands();++ib)
160 {
161 std::cout << "ib=" << ib << ": ";
162 for (int i = 0;i < psi.get_nbasis();++i)
163 {
164 std::cout << filter(psi(ik, ib, i)) << " ";
165 }
166 std::cout << "\n";
167 }
168 }
169 }
170 template<typename T>
171 void print_tensor(const container::Tensor& t, const std::string& label, const Parallel_Blkcyclic* pmat, const double& threshold = 1e-10)
172 {
173 std::cout << label << "\n";
174 for (int j = 0; j < pmat->ncloc(); ++j)
175 {
176 for (int i = 0; i < pmat->nrloc(); ++i)
177 {
178 std::cout << filter(t.data<T>()[j * pmat->nrloc() + i]) << " ";
179 }
180 std::cout << std::endl;
181 }
182 std::cout << "\n";
183 }
184 template<typename T>
185 void print_grid_nonzero(T* rho, const int& nrxx, const int& nnz, const std::string& label, const double& threshold = 1e-5)
186 {
187 std::cout << "first " << nnz << " non-zero elements of " << label << "\n";
188 int inz = 0;int i = 0;
189 while (inz < nnz && i < nrxx) { if (rho[++i] - 0.0 > threshold) { std::cout << rho[i] << " ";++inz; } };
190 }
191
192
193 using TC = std::array<int, 3>;
194 using TAC = std::pair<int, TC>;
195 template<typename T>
196 using TLRIX = std::map<int, std::map<TAC, std::vector<T>>>;
197
198 template<typename T>
199 void print_CsX(const TLRIX<T>& CsX, const int nvirt, const std::string& label, const double& threshold = 1e-10)
200 {
201 std::cout << label << "\n";
202 for (const auto& tmp1 : CsX)
203 {
204 const int& iat1 = tmp1.first;
205 for (const auto& tmp2 : tmp1.second)
206 {
207 const int& iat2 = tmp2.first.first;
208 const auto& R = tmp2.first.second;
209 auto& t = tmp2.second;
210 const int nocc = t.size() / nvirt;
211 std::cout << "iat1=" << iat1 << " iat2=" << iat2 << " R=(" << R[0] << " " << R[1] << " " << R[2] << ")\n";
212 for (int io = 0;io < nocc;++io)
213 {
214 for (int iv = 0;iv < nvirt;++iv) { std::cout << filter(t[io * nvirt + iv]) << " "; }
215 std::cout << "\n";
216 }
217 std::cout << "\n";
218 }
219 }
220 }
221
222#ifdef __EXX
223 template <typename T>
224 using TLRI = std::map<int, std::map<TAC, RI::Tensor<T>>>;
225 template<typename T>
226 void print_CV(const TLRI<T>& Cs, const std::string& label, const double& threshold = 1e-10)
227 {
228 std::cout << label << "\n";
229 for (const auto& tmp1 : Cs)
230 {
231 const int& iat1 = tmp1.first;
232 for (const auto& tmp2 : tmp1.second)
233 {
234 const int& iat2 = tmp2.first.first;
235 const auto& R = tmp2.first.second;
236 auto& t = tmp2.second;
237 if (R != TC({ 0, 0, 0 })) {continue;} // for test
238 std::cout << "iat1=" << iat1 << " iat2=" << iat2 << " R=(" << R[0] << " " << R[1] << " " << R[2] << ")\n";
239 if (t.shape.size() == 2)
240 {
241 for (int iabf1 = 0;iabf1 < t.shape[0];++iabf1)
242 {
243 for (int iabf2 = 0;iabf2 < t.shape[1];++iabf2)
244 {
245 std::cout << filter(t(iabf1, iabf2)) << " ";
246 }
247 std::cout << "\n";
248 }
249 }
250 else if (t.shape.size() == 3)
251 {
252 const int nabf = t.shape[0];
253 const int nw1 = t.shape[1];
254 const int nw2 = t.shape[2];
255 std::cout << "size: " << nabf << " " << nw1 << " " << nw2 << "\n";
256 for (int iabf = 0;iabf < nabf;++iabf)
257 {
258 for (int iw1 = 0;iw1 < nw1;++iw1)
259 {
260 for (int iw2 = 0;iw2 < nw2;++iw2)
261 {
262 std::cout << filter(t(iabf, iw1, iw2)) << " ";
263 }
264 std::cout << "\n";
265 }
266 std::cout << "\n";
267 }
268 }
269 }
270 }
271 }
272#endif
273}
274
275#endif // ABACUS_SOURCE_LCAO_MODULE_LR_UTILS_LR_UTIL_PRINT_H
Parallel block-cyclic matrix distribution.
Definition parallel_blkcyc.h:25
int ncloc() const
Definition parallel_blkcyc.h:40
int nrloc() const
Definition parallel_blkcyc.h:35
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 exciton_plotter.cpp:368
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:107
psi::Psi< T > read_psi_bandfirst(const std::string &filename, const int &rank)
Definition lr_util_print.h:142
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:185
void print_tensor(const container::Tensor &t, const std::string &label, const Parallel_Blkcyclic *pmat, const double &threshold=1e-10)
Definition lr_util_print.h:171
void print_excitation_energies(std::ostream &log, const double *energies, const int nstates, const std::string &channel)
Definition lr_util_print.h:16
std::pair< int, TC > TAC
Definition lr_util_print.h:194
void print_psi_kfirst(const psi::Psi< T > &psi, const std::string &label, const double &threshold=1e-10)
Definition lr_util_print.h:153
int read_value(std::ifstream &ifs, T *ptr, const int &size)
Definition lr_util_print.h:41
std::map< int, std::map< TAC, std::vector< T > > > TLRIX
Definition lr_util_print.h:196
constexpr T filter(const T &v, const double &threshold=1e-10)
Definition lr_util_print.h:35
int write_value(std::ofstream &ofs, const T *ptr, const int &size)
Definition lr_util_print.h:63
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:122
void print_CsX(const TLRIX< T > &CsX, const int nvirt, const std::string &label, const double &threshold=1e-10)
Definition lr_util_print.h:199
std::array< int, 3 > TC
Definition lr_util_print.h:193
int print_value(const T *ptr, const int &size)
Definition lr_util_print.h:91
const double Ry_to_eV
Definition constants.h:81
Definition diag_lcao.h:14
file(GLOB ATen_CORE_SRCS "*.cpp") set(ATen_CPU_SRCS $
Definition CMakeLists.txt:1
#define threshold
Definition sph_bessel_rec_test.cpp:4
std::map< int, std::map< TAC, RI::Tensor< T > > > TLRI
Definition test_write_ri_cv.cpp:12