22#include " elecstate_getters.h"
33#include " module_base/formatter.h"
44#include " module_base/global_variable.h"
5+ #include " module_base/parallel_common.h"
56#include " module_elecstate/potentials/H_Hartree_pw.h"
67#include " module_elecstate/potentials/efield.h"
78#include " module_elecstate/potentials/gatefield.h"
@@ -152,7 +153,9 @@ void print_scf_iterinfo(const std::string& ks_solver,
152153void ElecState::print_eigenvalue (std::ofstream& ofs)
153154{
154155 bool wrong = false ;
155- for (int ik = 0 ; ik < this ->klist ->get_nks (); ++ik)
156+ const int nks = this ->klist ->get_nks ();
157+ const int nkstot = this ->klist ->get_nkstot ();
158+ for (int ik = 0 ; ik < nks; ++ik)
156159 {
157160 for (int ib = 0 ; ib < this ->ekb .nc ; ++ib)
158161 {
@@ -164,76 +167,87 @@ void ElecState::print_eigenvalue(std::ofstream& ofs)
164167 }
165168 }
166169 }
170+ #ifdef __MPI
171+ MPI_Allreduce (MPI_IN_PLACE, &wrong, 1 , MPI_C_BOOL, MPI_LOR, MPI_COMM_WORLD);
172+ #endif
167173 if (wrong)
168174 {
169175 ModuleBase::WARNING_QUIT (" print_eigenvalue" , " Eigenvalues are too large!" );
170176 }
177+ std::stringstream ss;
178+ if (PARAM.inp .out_alllog )
179+ {
180+ ss << PARAM.globalv .global_out_dir << " running_" << PARAM.inp .calculation << " _" << GlobalV::MY_RANK + 1 << " .log" ;
181+ }
182+ else
183+ {
184+ ss << PARAM.globalv .global_out_dir << " running_" << PARAM.inp .calculation << " .log" ;
185+ }
186+ std::string filename = ss.str ();
187+ std::vector<int > ngk_tot = this ->klist ->ngk ;
171188
172- if (GlobalV::MY_RANK != 0 )
189+ #ifdef __MPI
190+ if (!PARAM.inp .out_alllog )
173191 {
174- return ;
192+ Parallel_Common::bcast_string (filename) ;
175193 }
194+ MPI_Allreduce (MPI_IN_PLACE, ngk_tot.data (), nks, MPI_INT, MPI_SUM, POOL_WORLD);
195+ #endif
176196
177197 ModuleBase::TITLE (" ESolver_KS_PW" , " print_eigenvalue" );
178198
179199 ofs << " \n STATE ENERGY(eV) AND OCCUPATIONS " ;
180- for (int ik = 0 ; ik < this ->klist ->get_nks (); ik++)
200+ const int nk_fac = PARAM.inp .nspin == 2 ? 2 : 1 ;
201+ const int nks_np = nks / nk_fac;
202+ const int nkstot_np = nkstot / nk_fac;
203+ ofs << " NSPIN == " << PARAM.inp .nspin << std::endl;
204+ for (int is = 0 ; is < nk_fac; ++is)
181205 {
182- ofs << std::setprecision (5 );
183- ofs << std::setiosflags (std::ios::showpoint);
184- if (ik == 0 )
206+ if (is == 0 && nk_fac == 2 )
185207 {
186- ofs << " NSPIN == " << PARAM.inp .nspin << std::endl;
187- if (PARAM.inp .nspin == 2 )
188- {
189- ofs << " SPIN UP : " << std::endl;
190- }
208+ ofs << " SPIN UP : " << std::endl;
191209 }
192- else if (ik == this -> klist -> get_nks () / 2 )
210+ else if (is == 1 && nk_fac == 2 )
193211 {
194- if (PARAM.inp .nspin == 2 )
195- {
196- ofs << " SPIN DOWN : " << std::endl;
197- }
212+ ofs << " SPIN DOWN : " << std::endl;
198213 }
199214
200- if (PARAM. inp . nspin == 2 )
215+ for ( int ip = 0 ; ip < GlobalV::KPAR; ++ip )
201216 {
202- if (this ->klist ->isk [ik] == 0 )
203- {
204- ofs << " " << ik + 1 << " /" << this ->klist ->get_nks () / 2
205- << " kpoint (Cartesian) = " << this ->klist ->kvec_c [ik].x << " " << this ->klist ->kvec_c [ik].y << " "
206- << this ->klist ->kvec_c [ik].z << " (" << this ->klist ->ngk [ik] << " pws)" << std::endl;
207-
208- ofs << std::setprecision (6 );
209- }
210- if (this ->klist ->isk [ik] == 1 )
217+ #ifdef __MPI
218+ MPI_Barrier (MPI_COMM_WORLD);
219+ #endif
220+ bool ip_flag = PARAM.inp .out_alllog || (GlobalV::RANK_IN_POOL == 0 && GlobalV::MY_STOGROUP == 0 );
221+ if (GlobalV::MY_POOL == ip && ip_flag)
211222 {
212- ofs << " " << ik + 1 - this ->klist ->get_nks () / 2 << " /" << this ->klist ->get_nks () / 2
213- << " kpoint (Cartesian) = " << this ->klist ->kvec_c [ik].x << " " << this ->klist ->kvec_c [ik].y << " "
214- << this ->klist ->kvec_c [ik].z << " (" << this ->klist ->ngk [ik] << " pws)" << std::endl;
223+ const int start_ik = nks_np * is;
224+ const int end_ik = nks_np * (is + 1 );
225+ for (int ik = start_ik; ik < end_ik; ++ik)
226+ {
227+ std::ofstream ofs_eig (filename.c_str (), std::ios::app);
228+ ofs_eig << std::setprecision (5 );
229+ ofs_eig << std::setiosflags (std::ios::showpoint);
230+ ofs_eig << " " << this ->klist ->ik2iktot [ik] + 1 - is * nkstot_np << " /" << nkstot_np
231+ << " kpoint (Cartesian) = " << this ->klist ->kvec_c [ik].x << " " << this ->klist ->kvec_c [ik].y
232+ << " " << this ->klist ->kvec_c [ik].z << " (" << ngk_tot[ik] << " pws)" << std::endl;
215233
216- ofs << std::setprecision (6 );
234+ ofs_eig << std::setprecision (6 );
235+ ofs_eig << std::setiosflags (std::ios::showpoint);
236+ for (int ib = 0 ; ib < this ->ekb .nc ; ib++)
237+ {
238+ ofs_eig << std::setw (8 ) << ib + 1 << std::setw (15 ) << this ->ekb (ik, ib) * ModuleBase::Ry_to_eV
239+ << std::setw (15 ) << this ->wg (ik, ib) << std::endl;
240+ }
241+ ofs_eig << std::endl;
242+ ofs_eig.close ();
243+ }
217244 }
218- } // Pengfei Li added 14-9-9
219- else
220- {
221- ofs << " " << ik + 1 << " /" << this ->klist ->get_nks ()
222- << " kpoint (Cartesian) = " << this ->klist ->kvec_c [ik].x << " " << this ->klist ->kvec_c [ik].y << " "
223- << this ->klist ->kvec_c [ik].z << " (" << this ->klist ->ngk [ik] << " pws)" << std::endl;
224-
225- ofs << std::setprecision (6 );
226245 }
227-
228- ofs << std::setprecision (6 );
229- ofs << std::setiosflags (std::ios::showpoint);
230- for (int ib = 0 ; ib < this ->ekb .nc ; ib++)
231- {
232- ofs << std::setw (8 ) << ib + 1 << std::setw (15 ) << this ->ekb (ik, ib) * ModuleBase::Ry_to_eV << std::setw (15 )
233- << this ->wg (ik, ib) << std::endl;
234- }
235- ofs << std::endl;
236- } // end ik
246+ #ifdef __MPI
247+ MPI_Barrier (MPI_COMM_WORLD);
248+ #endif
249+ ofs.seekp (0 , std::ios::end);
250+ }
237251 return ;
238252}
239253
0 commit comments