11#include " single_R_io.h"
22#include " source_base/parallel_reduce.h"
3- #include " source_io/module_parameter/parameter.h"
43#include " source_base/global_function.h"
54#include " source_base/global_variable.h"
65
6+ #include < complex>
7+ #include < cstdio>
8+ #include < iomanip>
9+ #include < sstream>
10+ #include < vector>
11+
712inline void write_data (std::ofstream& ofs, const double & data)
813{
914 ofs << " " << std::fixed << std::scientific << std::setprecision (16 ) << data;
@@ -15,25 +20,30 @@ inline void write_data(std::ofstream& ofs, const std::complex<double>& data)
1520
1621template <typename T>
1722void ModuleIO::output_single_R (std::ofstream& ofs,
18- const std::map<size_t , std::map<size_t , T>>& XR ,
19- const double & sparse_threshold,
20- const bool & binary,
23+ const SparseRBlock<T>& XR ,
2124 const Parallel_Orbitals& pv,
22- const bool & reduce )
25+ const SparseWriteOptions& options )
2326{
24- T* line = nullptr ;
27+ const int nlocal = pv.get_global_row_size ();
28+ if (nlocal <= 0 )
29+ {
30+ ModuleBase::WARNING_QUIT (" ModuleIO::output_single_R" ,
31+ " Parallel_Orbitals global row size must be positive." );
32+ }
33+
2534 std::vector<long long > indptr;
26- indptr.reserve (PARAM . globalv . nlocal + 1 );
35+ indptr.reserve (nlocal + 1 );
2736 indptr.push_back (0 );
2837
2938 std::stringstream tem1;
30- tem1 << PARAM .globalv .global_out_dir << std::to_string (GlobalV::DRANK ) + " temp_sparse_indices.dat" ;
39+ tem1 << options.temp_dir << std::to_string (GlobalV::DRANK )
40+ << " temp_sparse_indices.dat" ;
3141 std::ofstream ofs_tem1;
3242 std::ifstream ifs_tem1;
3343
34- if (!reduce || GlobalV::DRANK == 0 )
44+ if (!options. reduce || GlobalV::DRANK == 0 )
3545 {
36- if (binary)
46+ if (options. binary )
3747 {
3848 ofs_tem1.open (tem1.str ().c_str (), std::ios::binary);
3949 }
@@ -43,12 +53,12 @@ void ModuleIO::output_single_R(std::ofstream& ofs,
4353 }
4454 }
4555
46- line = new T[ PARAM . globalv . nlocal ] ;
47- for (int row = 0 ; row < PARAM . globalv . nlocal ; ++row)
56+ std::vector<T> line ( nlocal) ;
57+ for (int row = 0 ; row < nlocal; ++row)
4858 {
49- ModuleBase::GlobalFunc::ZEROS (line, PARAM . globalv . nlocal );
59+ ModuleBase::GlobalFunc::ZEROS (line. data (), nlocal);
5060
51- if (!reduce || pv.global2local_row (row) >= 0 )
61+ if (!options. reduce || pv.global2local_row (row) >= 0 )
5262 {
5363 auto iter = XR .find (row);
5464 if (iter != XR .end ())
@@ -60,19 +70,19 @@ void ModuleIO::output_single_R(std::ofstream& ofs,
6070 }
6171 }
6272
63- if (reduce)
64- {
65- Parallel_Reduce::reduce_all (line, PARAM . globalv . nlocal );
66- }
73+ if (options. reduce )
74+ {
75+ Parallel_Reduce::reduce_all (line. data (), nlocal);
76+ }
6777
68- if (!reduce || GlobalV::DRANK == 0 )
78+ if (!options. reduce || GlobalV::DRANK == 0 )
6979 {
7080 long long nonzeros_count = 0 ;
71- for (int col = 0 ; col < PARAM . globalv . nlocal ; ++col)
81+ for (int col = 0 ; col < nlocal; ++col)
7282 {
73- if (std::abs (line[col]) > sparse_threshold )
83+ if (std::abs (line[col]) > options. threshold )
7484 {
75- if (binary)
85+ if (options. binary )
7686 {
7787 ofs.write (reinterpret_cast <char *>(&line[col]), sizeof (T));
7888 ofs_tem1.write (reinterpret_cast <char *>(&col), sizeof (int ));
@@ -93,11 +103,9 @@ void ModuleIO::output_single_R(std::ofstream& ofs,
93103 }
94104 }
95105
96- delete[] line;
97-
98- if (!reduce || GlobalV::DRANK == 0 )
106+ if (!options.reduce || GlobalV::DRANK == 0 )
99107 {
100- if (binary)
108+ if (options. binary )
101109 {
102110 ofs_tem1.close ();
103111 ifs_tem1.open (tem1.str ().c_str (), std::ios::binary);
@@ -128,15 +136,11 @@ void ModuleIO::output_single_R(std::ofstream& ofs,
128136}
129137
130138template void ModuleIO::output_single_R<double >(std::ofstream& ofs,
131- const std::map<size_t , std::map<size_t , double >>& XR ,
132- const double & sparse_threshold,
133- const bool & binary,
139+ const SparseRBlock<double >& XR ,
134140 const Parallel_Orbitals& pv,
135- const bool & reduce );
141+ const SparseWriteOptions& options );
136142
137143template void ModuleIO::output_single_R<std::complex <double >>(std::ofstream& ofs,
138- const std::map<size_t , std::map<size_t , std::complex <double >>>& XR ,
139- const double & sparse_threshold,
140- const bool & binary,
144+ const SparseRBlock<std::complex <double >>& XR ,
141145 const Parallel_Orbitals& pv,
142- const bool & reduce );
146+ const SparseWriteOptions& options );
0 commit comments