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"
8#include "source_io/filename.h" // use filename_output function
9
10
11template <typename T>
13 const std::string &global_out_dir,
14 const int nspin,
15 const int nks,
16 const int nkstot,
17 const std::vector<int> &ik2iktot,
18 const std::vector<int> &isk,
19 hamilt::Hamilt<T>* p_hamilt,
20 const Parallel_Orbitals &pv,
21 const bool gamma_only,
22 const bool out_app_flag,
23 const int istep,
24 std::ofstream &ofs_running)
25{
26
27 ofs_running << " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
28 ">>>>>>>>>>>>>>>>>>>>>>>>>" << std::endl;
29 ofs_running << " | "
30 " |" << std::endl;
31 ofs_running << " | Write Hamiltonian matrix H(k) or overlap matrix S(k) in numerical |" << std::endl;
32 ofs_running << " | atomic orbitals at each k-point. |" << std::endl;
33 ofs_running << " | "
34 " |" << std::endl;
35 ofs_running << " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
36 ">>>>>>>>>>>>>>>>>>>>>>>>>" << std::endl;
37 ofs_running << "\n WRITE H(k) OR S(k)" << std::endl;
38
39 for (int ik = 0; ik < nks; ++ik)
40 {
41 p_hamilt->updateHk(ik);
42 bool bit = false; // LiuXh, 2017-03-21
43 // if set bit = true, there would be error in soc-multi-core
44 // calculation, noted by zhengdy-soc
45
48
49 p_hamilt->matrix(h_mat, s_mat);
50
51 const int out_label=1; // 1: .txt, 2: .dat
52
53 std::string h_fn = ModuleIO::filename_output(global_out_dir,
54 "hk","nao",ik,ik2iktot,nspin,nkstot,
55 out_label,out_app_flag,gamma_only,istep);
56
58 h_mat.p,
60 bit,
62 1,
63 out_app_flag,
64 h_fn,
65 pv,
67
68 // mohan note 2025-06-02
69 // for overlap matrix, the two spin channels yield the same matrix
70 // so we only need to print matrix from one spin channel.
71 const int current_spin = isk[ik];
72 if(current_spin == 1)
73 {
74 continue;
75 }
76
77 std::string s_fn = ModuleIO::filename_output(global_out_dir,
78 "sk","nao",ik,ik2iktot,nspin,nkstot,
79 out_label,out_app_flag,gamma_only,istep);
80
81 ofs_running << " The output filename is " << s_fn << std::endl;
82
84 s_mat.p,
86 bit,
88 1,
89 out_app_flag,
90 s_fn,
91 pv,
93 } // end ik
94}
95
96
97// output a square matrix
98template <typename T>
99void ModuleIO::save_mat(const int istep,
100 const T* mat,
101 const int dim,
102 const bool bit,
103 const int precision,
104 const bool tri,
105 const bool app,
106 const std::string& filename,
107 const Parallel_2D& pv,
108 const int drank,
109 const bool reduce)
110{
111 ModuleBase::TITLE("ModuleIO", "save_mat");
112 ModuleBase::timer::tick("ModuleIO", "save_mat");
113
114 // print out .dat file
115 if (bit)
116 {
117#ifdef __MPI
118 FILE* g = nullptr;
119
120 if (drank == 0)
121 {
122 g = fopen(filename.c_str(), "wb");
123 fwrite(&dim, sizeof(int), 1, g);
124 }
125
126 int ir=0;
127 int ic=0;
128 for (int i = 0; i < dim; ++i)
129 {
130 T* line = new T[tri ? dim - i : dim];
131 ModuleBase::GlobalFunc::ZEROS(line, tri ? dim - i : dim);
132
133 ir = pv.global2local_row(i);
134 if (ir >= 0)
135 {
136 // data collection
137 for (int j = (tri ? i : 0); j < dim; ++j)
138 {
139 ic = pv.global2local_col(j);
140 if (ic >= 0)
141 {
142 int iic;
143 if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver))
144 {
145 iic = ir + ic * pv.nrow;
146 }
147 else
148 {
149 iic = ir * pv.ncol + ic;
150 }
151 line[tri ? j - i : j] = mat[iic];
152 }
153 }
154 }
155
156 if (reduce)
157 {
158 Parallel_Reduce::reduce_all(line, tri ? dim - i : dim);
159 }
160
161 if (drank == 0)
162 {
163 for (int j = (tri ? i : 0); j < dim; ++j)
164 {
165 fwrite(&line[tri ? j - i : j], sizeof(T), 1, g);
166 }
167 }
168 delete[] line;
169
170 MPI_Barrier(DIAG_WORLD);
171 }
172
173 if (drank == 0)
174 {
175 fclose(g);
176 }
177#else
178 FILE* g = fopen(filename.c_str(), "wb");
179
180 fwrite(&dim, sizeof(int), 1, g);
181
182 for (int i = 0; i < dim; i++)
183 {
184 for (int j = (tri ? i : 0); j < dim; j++)
185 {
186 fwrite(&mat[i * dim + j], sizeof(T), 1, g);
187 }
188 }
189 fclose(g);
190#endif
191 } // end .dat file
192 else // .txt file
193 {
194 std::ofstream g;
195 g << std::setprecision(precision);
196#ifdef __MPI
197 if (drank == 0)
198 {
199 if (app && istep > 0)
200 {
201 g.open(filename.c_str(), std::ofstream::app);
202 }
203 else
204 {
205 g.open(filename.c_str());
206 }
207 g << dim;
208 }
209
210 int ir=0;
211 int ic=0;
212 for (int i = 0; i < dim; i++)
213 {
214 T* line = new T[tri ? dim - i : dim];
215 ModuleBase::GlobalFunc::ZEROS(line, tri ? dim - i : dim);
216
217 ir = pv.global2local_row(i);
218 if (ir >= 0)
219 {
220 // data collection
221 for (int j = (tri ? i : 0); j < dim; ++j)
222 {
223 ic = pv.global2local_col(j);
224 if (ic >= 0)
225 {
226 int iic;
227 if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver))
228 {
229 iic = ir + ic * pv.nrow;
230 }
231 else
232 {
233 iic = ir * pv.ncol + ic;
234 }
235 line[tri ? j - i : j] = mat[iic];
236 }
237 }
238 }
239
240 if (reduce)
241 {
242 Parallel_Reduce::reduce_all(line, tri ? dim - i : dim);
243 }
244
245 if (drank == 0)
246 {
247 for (int j = (tri ? i : 0); j < dim; j++)
248 {
249 g << " " << line[tri ? j - i : j];
250 }
251 g << std::endl;
252 }
253 delete[] line;
254 }
255
256 if (drank == 0)
257 { // Peize Lin delete ; at 2020.01.31
258 g.close();
259 }
260#else
261 if (app)
262 {
263 std::ofstream g(filename.c_str(), std::ofstream::app);
264 }
265 else
266 {
267 std::ofstream g(filename.c_str());
268 }
269
270 g << dim;
271 g << std::setprecision(precision);
272 for (int i = 0; i < dim; i++)
273 {
274 for (int j = (tri ? i : 0); j < dim; j++)
275 {
276 g << " " << mat[i * dim + j];
277 }
278 g << std::endl;
279 }
280 g.close();
281#endif
282 }
283 ModuleBase::timer::tick("ModuleIO", "save_mat");
284 return;
285}
static void tick(const std::string &class_name_in, const std::string &name_in)
Use twice at a time: the first time, set start_flag to false; the second time, calculate the time dur...
Definition timer.cpp:57
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:16
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:46
virtual void updateHk(const int ik)
for target K point, update consequence of hPsi() and matrix()
Definition hamilt.h:21
#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:99
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)
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:12
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:73
std::vector< int > out_mat_hs
output H matrix and S matrix in local basis.
Definition input_parameter.h:379
int nlocal
total number of local basis.
Definition system_parameter.h:23
Definition matrixblock.h:9
T * p
Definition matrixblock.h:12