ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
write_HS.hpp
Go to the documentation of this file.
1#include "write_HS.h"
2
5#include "source_base/timer.h"
7#include "source_io/module_output/filename.h" // use filename_output function
8
9
10template <typename T>
12 const std::string &global_out_dir,
13 const int nspin,
14 const int nks,
15 const int nkstot,
16 const std::vector<int> &ik2iktot,
17 const std::vector<int> &isk,
18 hamilt::Hamilt<T>* p_hamilt,
19 const Parallel_Orbitals &pv,
20 const bool gamma_only,
21 const bool out_app_flag,
22 const int istep,
23 std::ofstream &ofs_running)
24{
25
26 ofs_running << " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
27 ">>>>>>>>>>>>>>>>>>>>>>>>>" << std::endl;
28 ofs_running << " | "
29 " |" << std::endl;
30 ofs_running << " | Write Hamiltonian matrix H(k) or overlap matrix S(k) in numerical |" << std::endl;
31 ofs_running << " | atomic orbitals at each k-point. |" << std::endl;
32 ofs_running << " | "
33 " |" << std::endl;
34 ofs_running << " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
35 ">>>>>>>>>>>>>>>>>>>>>>>>>" << std::endl;
36 ofs_running << "\n WRITE H(k) OR S(k)" << std::endl;
37
38 for (int ik = 0; ik < nks; ++ik)
39 {
40 p_hamilt->updateHk(ik);
41 bool bit = false; // LiuXh, 2017-03-21
42 // if set bit = true, there would be error in soc-multi-core
43 // calculation, noted by zhengdy-soc
44
47
48 p_hamilt->matrix(h_mat, s_mat);
49
50 const int out_label=1; // 1: .txt, 2: .dat
51
52 std::string h_fn = ModuleIO::filename_output(global_out_dir,
53 "hk","nao",ik,ik2iktot,nspin,nkstot,
54 out_label,out_app_flag,gamma_only,istep);
55
57 h_mat.p,
59 bit,
61 1,
62 out_app_flag,
63 h_fn,
64 pv,
66
67 // mohan note 2025-06-02
68 // for overlap matrix, the two spin channels yield the same matrix
69 // so we only need to print matrix from one spin channel.
70 const int current_spin = isk[ik];
71 if(current_spin == 1)
72 {
73 continue;
74 }
75
76 std::string s_fn = ModuleIO::filename_output(global_out_dir,
77 "sk","nao",ik,ik2iktot,nspin,nkstot,
78 out_label,out_app_flag,gamma_only,istep);
79
80 ofs_running << " The output filename is " << s_fn << std::endl;
81
83 s_mat.p,
85 bit,
87 1,
88 out_app_flag,
89 s_fn,
90 pv,
92 } // end ik
93}
94
95
96// output a square matrix
97template <typename T>
98void ModuleIO::save_mat(const int istep,
99 const T* mat,
100 const int dim,
101 const bool bit,
102 const int precision,
103 const bool tri,
104 const bool app,
105 const std::string& filename,
106 const Parallel_2D& pv,
107 const int drank,
108 const bool reduce)
109{
110 ModuleBase::TITLE("ModuleIO", "save_mat");
111 ModuleBase::timer::start("ModuleIO", "save_mat");
112
113 const bool gamma_only = std::is_same<T, double>::value;
114
115 // write .dat file
116 if (bit)
117 {
118// write .dat file with MPI
119#ifdef __MPI
120 FILE* out_matrix = nullptr;
121
122 if (drank == 0)
123 {
124 out_matrix = fopen(filename.c_str(), "wb");
125 fwrite(&dim, sizeof(int), 1, out_matrix);
126 }
127
128 int ir=0;
129 int ic=0;
130 for (int i = 0; i < dim; ++i)
131 {
132 T* line = new T[tri ? dim - i : dim];
133 ModuleBase::GlobalFunc::ZEROS(line, tri ? dim - i : dim);
134
135 ir = pv.global2local_row(i);
136 if (ir >= 0)
137 {
138 // data collection
139 for (int j = (tri ? i : 0); j < dim; ++j)
140 {
141 ic = pv.global2local_col(j);
142 if (ic >= 0)
143 {
144 int iic;
145 if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver))
146 {
147 iic = ir + ic * pv.nrow;
148 }
149 else
150 {
151 iic = ir * pv.ncol + ic;
152 }
153 line[tri ? j - i : j] = mat[iic];
154 }
155 }
156 }
157
158 if (reduce)
159 {
160 Parallel_Reduce::reduce_all(line, tri ? dim - i : dim);
161 }
162
163 if (drank == 0)
164 {
165 for (int j = (tri ? i : 0); j < dim; ++j)
166 {
167 fwrite(&line[tri ? j - i : j], sizeof(T), 1, out_matrix);
168 }
169 }
170 delete[] line;
171
172 MPI_Barrier(DIAG_WORLD);
173 }
174
175 if (drank == 0)
176 {
177 fclose(out_matrix);
178 }
179// write .dat file without MPI
180#else
181 FILE* out_matrix = fopen(filename.c_str(), "wb");
182
183 fwrite(&dim, sizeof(int), 1, out_matrix);
184
185 for (int i = 0; i < dim; i++)
186 {
187 for (int j = (tri ? i : 0); j < dim; j++)
188 {
189 fwrite(&mat[i * dim + j], sizeof(T), 1, out_matrix);
190 }
191 }
192 fclose(out_matrix);
193#endif
194 } // end writing .dat file
195 else // write .txt file
196 {
197 std::ofstream out_matrix;
198 out_matrix << std::scientific << std::setprecision(precision);
199#ifdef __MPI
200 if (drank == 0)
201 {
202 if (app && istep > 0)
203 {
204 out_matrix.open(filename.c_str(), std::ofstream::app);
205 }
206 else
207 {
208 out_matrix.open(filename.c_str());
209 }
210 out_matrix << "#------------------------------------------------------------------------" << std::endl;
211 out_matrix << "# ionic step " << istep+1 << std::endl; // istep starts from 0
212 out_matrix << "# filename " << filename << std::endl;
213 out_matrix << "# gamma only " << gamma_only << std::endl;
214 out_matrix << "# rows " << dim << std::endl;
215 out_matrix << "# columns " << dim << std::endl;
216 out_matrix << "#------------------------------------------------------------------------" << std::endl;
217
218 }
219
220 int ir=0;
221 int ic=0;
222 for (int i = 0; i < dim; i++)
223 {
224 T* line = new T[tri ? dim - i : dim];
225 ModuleBase::GlobalFunc::ZEROS(line, tri ? dim - i : dim);
226
227 ir = pv.global2local_row(i);
228 if (ir >= 0)
229 {
230 // data collection
231 for (int j = (tri ? i : 0); j < dim; ++j)
232 {
233 ic = pv.global2local_col(j);
234 if (ic >= 0)
235 {
236 int iic=0;
237 if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver))
238 {
239 iic = ir + ic * pv.nrow;
240 }
241 else
242 {
243 iic = ir * pv.ncol + ic;
244 }
245 line[tri ? j - i : j] = mat[iic];
246 }
247 }
248 }
249
250 if (reduce)
251 {
252 Parallel_Reduce::reduce_all(line, tri ? dim - i : dim);
253 }
254
255 if (drank == 0)
256 {
257 out_matrix << "Row " << i+1 << std::endl;
258 size_t count = 0;
259 for (int j = (tri ? i : 0); j < dim; j++)
260 {
261 out_matrix << " " << line[tri ? j - i : j];
262 ++count;
263 if(count%8==0)
264 {
265 if(j!=dim-1)
266 {
267 out_matrix << std::endl;
268 }
269 }
270 }
271 out_matrix << std::endl;
272 }
273 delete[] line;
274 }
275
276 if (drank == 0)
277 {
278 out_matrix.close();
279 }
280#else
281 if (app)
282 {
283 std::ofstream out_matrix(filename.c_str(), std::ofstream::app);
284 }
285 else
286 {
287 std::ofstream out_matrix(filename.c_str());
288 }
289
290 out_matrix << dim;
291 out_matrix << std::setprecision(precision);
292 for (int i = 0; i < dim; i++)
293 {
294 for (int j = (tri ? i : 0); j < dim; j++)
295 {
296 out_matrix << " " << mat[i * dim + j];
297 }
298 out_matrix << std::endl;
299 }
300 out_matrix.close();
301#endif
302 }
303 ModuleBase::timer::end("ModuleIO", "save_mat");
304 return;
305}
const std::complex< double > i
Definition cal_pLpR.cpp:46
static void start(void)
Start total time calculation.
Definition timer.cpp:44
static void end(const std::string &class_name_in, const std::string &name_in)
Definition timer.cpp:109
This class packs the basic information of 2D-block-cyclic parallel distribution of an arbitrary matri...
Definition parallel_2d.h:12
int ncol
Definition parallel_2d.h:116
int nrow
local size (nloc = nrow * ncol)
Definition parallel_2d.h:115
int global2local_col(const int igc) const
get the local index of a global index (col)
Definition parallel_2d.h:51
int global2local_row(const int igr) const
get the local index of a global index (row)
Definition parallel_2d.h:45
Definition parallel_orbitals.h:9
const Input_para & inp
Definition parameter.h:26
const System_para & globalv
Definition parameter.h:30
Definition hamilt.h:17
virtual void matrix(MatrixBlock< std::complex< double > > &hk_in, MatrixBlock< std::complex< double > > &sk_in)
core function: return H(k) and S(k) matrixs for direct solving eigenvalues.
Definition hamilt.h:53
void updateHk(const int ik) override
for target K point, update consequence of hPsi() and matrix()
Definition hamilt.h:22
#define T
Definition exp.cpp:237
int DRANK
Definition global_variable.cpp:28
void ZEROS(std::complex< T > *u, const TI n)
Definition global_function.h:109
void TITLE(const std::string &class_name, const std::string &function_name, const bool disable)
Definition tool_title.cpp:18
void save_mat(const int istep, const T *mat, const int dim, const bool bit, const int precision, const bool tri, const bool app, const std::string &file_name, const Parallel_2D &pv, const int drank, const bool reduce=true)
save a square matrix, such as H(k) and S(k)
Definition write_HS.hpp:98
std::string filename_output(const std::string &directory, const std::string &property, const std::string &basis, const int ik_local, const std::vector< int > &ik2iktot, const int nspin, const int nkstot, const int out_type, const bool out_app_flag, const bool gamma_only, const int istep, const int iter)
Definition filename.cpp:8
void write_hsk(const std::string &global_out_dir, const int nspin, const int nks, const int nkstot, const std::vector< int > &ik2iktot, const std::vector< int > &isk, hamilt::Hamilt< T > *p_hamilt, const Parallel_Orbitals &pv, const bool gamma_only, const bool out_app_flag, const int istep, std::ofstream &ofs_running)
Definition write_HS.hpp:11
void reduce_all(T &object)
reduce in all process
Definition depend_mock.cpp:14
MPI_Comm DIAG_WORLD
Definition parallel_comm.cpp:11
Parameter PARAM
Definition parameter.cpp:3
std::string ks_solver
xiaohui add 2013-09-01
Definition input_parameter.h:77
std::vector< int > out_mat_hs
output H matrix and S matrix in local basis.
Definition input_parameter.h:393
int nlocal
total number of local basis.
Definition system_parameter.h:23
Definition matrixblock.h:9
T * p
Definition matrixblock.h:12