ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
hs_sparse_io_detail.h
Go to the documentation of this file.
1#ifndef HS_SPARSE_IO_DETAIL_H
2#define HS_SPARSE_IO_DETAIL_H
3
4#include "hs_sparse_io.h"
10
11#include <cmath>
12#include <fstream>
13#include <iomanip>
14#include <limits>
15#include <string>
16#include <vector>
17
25namespace ModuleIO
26{
27namespace detail
28{
37template <typename Tdata>
38std::vector<long long> count_nonzeros_by_R(
39 const SparseRMatrix<Tdata>& smat,
40 const std::set<RCoordinate>& all_R_coor,
41 const double threshold,
42 const bool reduce)
43{
44 std::vector<long long> nonzero_num(all_R_coor.size(), 0);
45 int count = 0;
46 for (const Abfs::Vector3_Order<int>& R_coor: all_R_coor)
47 {
48 const typename SparseRMatrix<Tdata>::const_iterator iter = smat.find(R_coor);
49 if (iter != smat.end())
50 {
51 for (const typename SparseRBlock<Tdata>::value_type& row_loop: iter->second)
52 {
53 for (const std::pair<const std::size_t, Tdata>& col_value: row_loop.second)
54 {
55 if (std::abs(col_value.second) > threshold)
56 {
57 ++nonzero_num[count];
58 }
59 }
60 }
61 }
62 ++count;
63 }
64
65 if (reduce)
66 {
67 Parallel_Reduce::reduce_all(nonzero_num.data(), static_cast<int>(nonzero_num.size()));
68 }
69 return nonzero_num;
70}
71
78inline void check_output_file_open(const std::ofstream& ofs,
79 const std::string& filename,
80 const std::string& context)
81{
82 if (!ofs.is_open())
83 {
84 ModuleBase::WARNING_QUIT(context, "Cannot open sparse matrix file: " + filename);
85 }
86}
87
97inline bool append_ionic_step(const int out_app_flag, const int istep)
98{
99 return out_app_flag == 1 && istep > 0;
100}
101
116inline std::string make_sparse_R_filename(const std::string& prefix,
117 const std::string& spin_suffix,
118 const bool numbered,
119 const int istep,
120 const std::string& extension)
121{
122 if (prefix.empty())
123 {
124 ModuleBase::WARNING_QUIT("ModuleIO::detail::make_sparse_R_filename",
125 "File name prefix must not be empty.");
126 }
127 if (extension.empty() || extension[0] != '.')
128 {
129 ModuleBase::WARNING_QUIT("ModuleIO::detail::make_sparse_R_filename",
130 "File extension must start with a dot.");
131 }
132
133 std::string name = prefix + spin_suffix;
134 if (numbered && istep >= 0)
135 {
136 name += "g" + std::to_string(istep + 1);
137 }
138 name += "_nao" + extension;
139 return name;
140}
141
161
167inline void write_csr_text_header(std::ofstream& ofs, const CsrTextHeader& header)
168{
169 if (header.ucell == nullptr)
170 {
171 ModuleBase::WARNING_QUIT("ModuleIO::detail::write_csr_text_header",
172 "UnitCell pointer must not be null.");
173 }
174 if (header.nspin <= 0)
175 {
176 ModuleBase::WARNING_QUIT("ModuleIO::detail::write_csr_text_header",
177 "Number of spin directions must be positive.");
178 }
179 if (header.ispin < 1 || header.ispin > header.nspin)
180 {
181 ModuleBase::WARNING_QUIT("ModuleIO::detail::write_csr_text_header",
182 "Spin index must be within the spin range.");
183 }
184 if (header.nbasis <= 0)
185 {
186 ModuleBase::WARNING_QUIT("ModuleIO::detail::write_csr_text_header",
187 "Number of localized basis functions must be positive.");
188 }
189 if (header.number_of_R < 0)
190 {
191 ModuleBase::WARNING_QUIT("ModuleIO::detail::write_csr_text_header",
192 "Number of Bravais lattice vectors must not be negative.");
193 }
194
195 const std::string time_stamp = ModuleIO::local_timestamp();
196 ofs << "Ionic_Step " << header.step << ModuleIO::prov_inline(time_stamp) << std::endl;
197 ofs << header.description << std::endl;
198 ofs << " " << header.nspin << " # number of spin directions" << std::endl;
199 ofs << " " << header.ispin << " # spin index";
200 if (header.has_efermi)
201 {
202 ofs << ", E_Fermi = " << std::setprecision(6) << header.efermi_eV << " eV";
203 }
204 ofs << std::endl;
205 ofs << " " << header.nbasis << " # number of localized basis" << std::endl;
206 ofs << " " << header.number_of_R << " # number of Bravais lattice vector R" << std::endl;
207 ofs << std::endl;
208
210 if (!header.representation_note.empty())
211 {
212 ofs << "# representation: " << header.representation_note << std::endl;
213 }
214 ofs << std::endl;
215
216 if (header.write_banner)
217 {
219 }
220}
221
229inline void write_csr_binary_header(std::ofstream& ofs,
230 const int step,
231 const int nbasis,
232 const int number_of_R)
233{
234 ofs.write(reinterpret_cast<const char*>(&step), sizeof(int));
235 ofs.write(reinterpret_cast<const char*>(&nbasis), sizeof(int));
236 ofs.write(reinterpret_cast<const char*>(&number_of_R), sizeof(int));
237}
238
249inline void write_R_record(std::ofstream& ofs,
250 const int rx,
251 const int ry,
252 const int rz,
253 const long long nnz,
254 const bool binary,
255 const bool leading_space)
256{
257 if (binary)
258 {
259 if (nnz > static_cast<long long>(std::numeric_limits<int>::max()))
260 {
261 ModuleBase::WARNING_QUIT("ModuleIO::detail::write_R_record",
262 "Too many non-zero elements for the binary R record.");
263 }
264 const int count = static_cast<int>(nnz);
265 ofs.write(reinterpret_cast<const char*>(&rx), sizeof(int));
266 ofs.write(reinterpret_cast<const char*>(&ry), sizeof(int));
267 ofs.write(reinterpret_cast<const char*>(&rz), sizeof(int));
268 ofs.write(reinterpret_cast<const char*>(&count), sizeof(int));
269 }
270 else
271 {
272 if (leading_space)
273 {
274 ofs << " ";
275 }
276 ofs << rx << " " << ry << " " << rz << " " << nnz << std::endl;
277 }
278}
279} // namespace detail
280} // namespace ModuleIO
281
282#endif // HS_SPARSE_IO_DETAIL_H
Definition abfs_vector3_order.h:16
static void write_ucell(std::ofstream &ofs, const UnitCell *ucell)
Writes the unit cell information to a file.
Definition ucell_io.cpp:8
Provide the basic information about unitcell.
Definition unitcell.h:16
void WARNING_QUIT(const std::string &, const std::string &)
Combine the functions of WARNING and QUIT.
Definition test_delley.cpp:15
Definition hs_sparse_io_detail.h:28
void write_csr_text_header(std::ofstream &ofs, const CsrTextHeader &header)
Write the common self-describing header of a CSR text file.
Definition hs_sparse_io_detail.h:167
void write_R_record(std::ofstream &ofs, const int rx, const int ry, const int rz, const long long nnz, const bool binary, const bool leading_space)
Write the "Rx Ry Rz nnz" record that starts one R block.
Definition hs_sparse_io_detail.h:249
bool append_ionic_step(const int out_app_flag, const int istep)
Decide whether the current ionic step appends to an existing file.
Definition hs_sparse_io_detail.h:97
std::vector< long long > count_nonzeros_by_R(const SparseRMatrix< Tdata > &smat, const std::set< RCoordinate > &all_R_coor, const double threshold, const bool reduce)
Count non-zero elements per R-block of a sparse R-matrix.
Definition hs_sparse_io_detail.h:38
void write_csr_binary_header(std::ofstream &ofs, const int step, const int nbasis, const int number_of_R)
Write the three-integer binary CSR header.
Definition hs_sparse_io_detail.h:229
std::string make_sparse_R_filename(const std::string &prefix, const std::string &spin_suffix, const bool numbered, const int istep, const std::string &extension)
Build the "<prefix><spin suffix>[g<step>]_nao<ext>" name shared by every R-space sparse output.
Definition hs_sparse_io_detail.h:116
void check_output_file_open(const std::ofstream &ofs, const std::string &filename, const std::string &context)
Quit if a sparse-matrix output stream failed to open.
Definition hs_sparse_io_detail.h:78
Internal helpers shared by the R-space sparse text writers. Declared here (instead of duplicated in e...
Definition csr_reader.cpp:8
std::map< RCoordinate, SparseRBlock< T > > SparseRMatrix
Definition hs_sparse_io.h:22
std::string local_timestamp()
Definition provenance_header.h:16
std::map< size_t, std::map< size_t, T > > SparseRBlock
Definition hs_sparse_io.h:19
std::string prov_inline(const std::string &time_stamp)
Definition provenance_header.h:48
void write_csr_banner(std::ostream &ofs)
Write the CSR-format description banner used by every text CSR output (H(R)/S(R), DM(R),...
Definition sparse_matrix.cpp:84
void reduce_all(T &object)
reduce in all process
Definition depend_mock.cpp:14
#define threshold
Definition sph_bessel_rec_test.cpp:4
Metadata shared by every self-describing CSR text writer.
Definition hs_sparse_io_detail.h:148
const UnitCell * ucell
Unit cell; must not be null.
Definition hs_sparse_io_detail.h:155
int nbasis
Number of localized basis functions.
Definition hs_sparse_io_detail.h:153
int ispin
One-based spin index.
Definition hs_sparse_io_detail.h:152
int step
One-based ionic step.
Definition hs_sparse_io_detail.h:149
std::string description
Full " # print ..." matrix description line.
Definition hs_sparse_io_detail.h:150
bool write_banner
Write the CSR layout banner.
Definition hs_sparse_io_detail.h:159
bool has_efermi
Append the Fermi energy to the spin line.
Definition hs_sparse_io_detail.h:157
std::string representation_note
Optional note written after the cell block.
Definition hs_sparse_io_detail.h:156
int number_of_R
Number of R blocks actually written.
Definition hs_sparse_io_detail.h:154
int nspin
Number of spin directions.
Definition hs_sparse_io_detail.h:151
double efermi_eV
Fermi energy in eV.
Definition hs_sparse_io_detail.h:158