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"
2#include <map>
3#include <cstddef>
4#define IZ(x) int x = 0;
5#define SZ(x) std::size_t x = 0;
6
7namespace LRI_CV_Tools
8{
9 using TC = std::array<int, 3>;
10 using TAC = std::pair<int, TC>;
11 template <typename T>
12 using TLRI = std::map<int, std::map<TAC, RI::Tensor<T>>>;;
13
14 template<typename T>
15 inline double absmax(const RI::Tensor<T>& t)
16 {
17 double res = 0;
18 for (int i = 0;i < t.get_shape_all();++i)
19 {
20 double v = std::abs(std::norm(t.ptr()[i]));
21 if (v > res) { res = v; }
22 }
23 return std::sqrt(res);
24 }
25
26 template<typename T>
27 TLRI<T> read_Cs_ao(const std::string& file_path, const double& threshold)
28 {
29 IZ(natom) IZ(ncell) IZ(ia1) IZ(ia2) IZ(ic_1) IZ(ic_2) IZ(ic_3)
30 SZ(nw1) SZ(nw2) SZ(nabf)
31 std::ifstream infile;
32 infile.open(file_path);
33 infile >> natom >> ncell; // no use of ncell
34
35 TLRI<T> Cs;
36 while (infile.peek() != EOF)
37 {
38 infile >> ia1 >> ia2 >> ic_1 >> ic_2 >> ic_3 >> nw1 >> nw2 >> nabf;
39 const TC& box = { ic_1, ic_2, ic_3 };
40 RI::Tensor<T> tensor_cs({ nabf, nw1, nw2 });
41 for (std::size_t i = 0; i != nw1; i++) { for (std::size_t j = 0; j != nw2; j++) { for (std::size_t mu = 0; mu != nabf; mu++) { infile >> tensor_cs(mu, i, j); } } }
42 // no screening for data-structure consistency
43 if (absmax(tensor_cs) >= threshold) { Cs[ia1 - 1][{ia2 - 1, box}] = tensor_cs; }
44 }
45 infile.close();
46 return Cs;
47 }
48
49 template<typename T>
50 void write_Cs_ao(const TLRI<T>& Cs, const std::string& file_path)
51 {
52 std::ofstream outfile;
53 outfile.open(file_path);
54 outfile << Cs.size() << " " << Cs.at(0).size() / Cs.size() << std::endl; //natom, ncell
55 for (auto& it1 : Cs)
56 {
57 const int& ia1 = it1.first;
58 for (auto& it2 : it1.second)
59 {
60 const int& ia2 = it2.first.first;
61 const auto& box = it2.first.second;
62 const auto& tensor_cs = it2.second;
63 outfile << ia1 + 1 << " " << ia2 + 1 << " " << box[0] << " " << box[1] << " " << box[2] << std::endl;
64 const int& nw1 = tensor_cs.shape[1], nw2 = tensor_cs.shape[2], nabf = tensor_cs.shape[0];
65 outfile << nw1 << " " << nw2 << " " << nabf << std::endl;
66 for (int i = 0; i != nw1; i++)
67 {
68 for (int j = 0; j != nw2; j++)
69 {
70 for (int mu = 0; mu != nabf; mu++) { outfile << tensor_cs(mu, i, j) << " "; }
71 outfile << std::endl;
72 }
73 }
74 }
75 }
76 outfile.close();
77 }
78
79 template<typename T>
80 TLRI<T> read_Vs_abf(const std::string& file_path, const double& threshold)
81 {
82 IZ(natom) IZ(ncell) IZ(ia1) IZ(ia2) IZ(ic_1) IZ(ic_2) IZ(ic_3)
83 SZ(nabf1) SZ(nabf2)
84 std::ifstream infile;
85 infile.open(file_path);
86 infile >> natom >> ncell; // no use of ncell
87
88 TLRI<T> Vs;
89 while (infile.peek() != EOF)
90 {
91 infile >> ia1 >> ia2 >> ic_1 >> ic_2 >> ic_3 >> nabf1 >> nabf2;
92 const TC& box = { ic_1, ic_2, ic_3 };
93 RI::Tensor<T> tensor_vs({ nabf1, nabf2 });
94 for (std::size_t i = 0; i != nabf1; i++) {
95 for (std::size_t j = 0; j != nabf2; j++){ infile >> tensor_vs(i, j);}
96}
97 if (absmax(tensor_vs) >= threshold) { Vs[ia1 - 1][{ia2 - 1, box}] = tensor_vs; }
98 // else ++cs_discard;
99 }
100 infile.close();
101 return Vs;
102 }
103
104 template <typename T>
105 void write_Vs_abf(const TLRI<T>& Vs, const std::string& file_path)
106 {
107 std::ofstream outfile;
108 outfile.open(file_path);
109 outfile << Vs.size() << " " << Vs.at(0).size() / Vs.size() << std::endl; //natom, ncell
110 for (const auto& it1 : Vs)
111 {
112 const int& ia1 = it1.first;
113 for (const auto& it2 : it1.second)
114 {
115 const int& ia2 = it2.first.first;
116 const auto& box = it2.first.second;
117 const auto& tensor_v = it2.second;
118 outfile << ia1 + 1 << " " << ia2 + 1 << " " << box[0] << " " << box[1] << " " << box[2] << std::endl;
119 outfile << tensor_v.shape[0] << " " << tensor_v.shape[1] << std::endl;
120 for (int i = 0; i != tensor_v.shape[0]; i++)
121 {
122 for (int j = 0; j != tensor_v.shape[1]; j++) {outfile << tensor_v(i, j) << " ";}
123 outfile << std::endl;
124 }
125 }
126 }
127 outfile.close();
128 }
129}
Definition LRI_CV_Tools.h:20
TLRI< T > read_Cs_ao(const std::string &file_path, const double &threshold=1e-10)
Definition write_ri_cv.hpp:27
std::map< int, std::map< TAC, RI::Tensor< T > > > TLRI
Definition LRI_CV_Tools.h:135
std::array< int, 3 > TC
Definition LRI_CV_Tools.h:132
void write_Cs_ao(const TLRI< T > &Vs, const std::string &file_path)
Definition write_ri_cv.hpp:50
TLRI< T > read_Vs_abf(const std::string &file_path, const double &threshold=1e-10)
Definition write_ri_cv.hpp:80
double absmax(const RI::Tensor< T > &t)
Definition write_ri_cv.hpp:15
void write_Vs_abf(const TLRI< T > &Vs, const std::string &file_path)
Definition write_ri_cv.hpp:105
std::array< int, 3 > TC
Definition ri_cv_io_test.cpp:9
std::pair< int, TC > TAC
Definition ri_cv_io_test.cpp:10
std::map< int, std::map< TAC, RI::Tensor< T > > > TLRI
Definition ri_cv_io_test.cpp:12
#define threshold
Definition sph_bessel_recursive_test.cpp:4
#define SZ(x)
Definition write_ri_cv.hpp:5
#define IZ(x)
Definition write_ri_cv.hpp:4