11#include " source_base/module_out/binstream.h"
2- #include " source_base/global_function.h"
32#include " source_base/vector3.h"
43#include " source_base/module_parallel/para_mpi_func.h"
54#include " rhog_io.h"
5+ #include < algorithm>
66#include < numeric>
77#include < unistd.h>
88
@@ -20,13 +20,34 @@ inline void warn(std::ostream* os,
2020}
2121} // namespace
2222
23- bool ModuleIO ::read_rhog (const std::string& filename,
23+ bool elecstate ::read_rhog (const std::string& filename,
2424 const ModulePW::PW_Basis* pw_rhod,
2525 const int nspin,
2626 std::complex <double >** rhog,
2727 const Parallel::ParaWorld& pw_world,
2828 std::ostream* os_warning)
2929{
30+ if (pw_rhod == nullptr )
31+ {
32+ warn (os_warning, pw_world, " elecstate::read_rhog" , " pw_rhod is null" );
33+ return false ;
34+ }
35+ if (rhog == nullptr )
36+ {
37+ warn (os_warning, pw_world, " elecstate::read_rhog" , " rhog is null" );
38+ return false ;
39+ }
40+ if (nspin <= 0 || nspin > 4 )
41+ {
42+ warn (os_warning, pw_world, " elecstate::read_rhog" , " nspin must be 1, 2, or 4" );
43+ return false ;
44+ }
45+ if (pw_rhod->nx <= 0 || pw_rhod->ny <= 0 || pw_rhod->nz <= 0 )
46+ {
47+ warn (os_warning, pw_world, " elecstate::read_rhog" , " PW_Basis grid dimensions must be positive" );
48+ return false ;
49+ }
50+
3051 const int nx = pw_rhod->nx ;
3152 const int ny = pw_rhod->ny ;
3253 const int nz = pw_rhod->nz ;
@@ -52,7 +73,7 @@ bool ModuleIO::read_rhog(const std::string& filename,
5273
5374 if (error)
5475 {
55- warn (os_warning, pw_world, " ModuleIO ::read_rhog" , " Can't open file " + filename);
76+ warn (os_warning, pw_world, " elecstate ::read_rhog" , " Can't open file " + filename);
5677 return false ;
5778 }
5879
@@ -69,23 +90,23 @@ bool ModuleIO::read_rhog(const std::string& filename,
6990 }
7091 if (npwtot_in > pw_rhod->npwtot )
7192 {
72- warn (os_warning, pw_world, " ModuleIO ::read_rhog" , " some planewaves in file are not used" );
93+ warn (os_warning, pw_world, " elecstate ::read_rhog" , " some planewaves in file are not used" );
7394 }
7495 else if (npwtot_in < pw_rhod->npwtot )
7596 {
76- warn (os_warning, pw_world, " ModuleIO ::read_rhog" , " some planewaves in file are missing" );
97+ warn (os_warning, pw_world, " elecstate ::read_rhog" , " some planewaves in file are missing" );
7798 }
7899 if (nspin_in < nspin)
79100 {
80- warn (os_warning, pw_world, " ModuleIO ::read_rhog" , " some spin channels in file are missing" );
101+ warn (os_warning, pw_world, " elecstate ::read_rhog" , " some spin channels in file are missing" );
81102 }
82103 }
83104
84105 Parallel::bcast_bool (error, pw_world);
85106
86107 if (error)
87108 {
88- warn (os_warning, pw_world, " ModuleIO ::read_rhog" , " gamma_only read from file is inconsistent with INPUT" );
109+ warn (os_warning, pw_world, " elecstate ::read_rhog" , " gamma_only read from file is inconsistent with INPUT" );
89110 return false ;
90111 }
91112
@@ -115,7 +136,7 @@ bool ModuleIO::read_rhog(const std::string& filename,
115136 // set to zero
116137 for (int is = 0 ; is < nspin; ++is)
117138 {
118- ModuleBase::GlobalFunc::ZEROS (rhog[is], pw_rhod->npw );
139+ std::fill (rhog[is], rhog[is] + pw_rhod->npw , std:: complex < double >( 0.0 , 0.0 ) );
119140 }
120141 // maps ixyz tp ig
121142 std::vector<int > fftixyz2ig (pw_rhod->nxyz , -1 ); // map isz to ig.
@@ -176,8 +197,8 @@ bool ModuleIO::read_rhog(const std::string& filename,
176197 {
177198 rhog[3 ][ig] = rhog[1 ][ig];
178199 }
179- ModuleBase::GlobalFunc::ZEROS (rhog[1 ], pw_rhod->npw );
180- ModuleBase::GlobalFunc::ZEROS (rhog[2 ], pw_rhod->npw );
200+ std::fill (rhog[1 ], rhog[ 1 ] + pw_rhod->npw , std:: complex < double >( 0.0 , 0.0 ) );
201+ std::fill (rhog[2 ], rhog[ 2 ] + pw_rhod->npw , std:: complex < double >( 0.0 , 0.0 ) );
181202 }
182203 }
183204
@@ -188,7 +209,7 @@ bool ModuleIO::read_rhog(const std::string& filename,
188209 return true ;
189210}
190211
191- bool ModuleIO ::write_rhog (const std::string& fchg,
212+ bool elecstate ::write_rhog (const std::string& fchg,
192213 const bool gamma_only,
193214 const ModulePW::PW_Basis* pw_rho,
194215 const int nspin,
@@ -197,6 +218,22 @@ bool ModuleIO::write_rhog(const std::string& fchg,
197218 const Parallel::ParaWorld& pw_world,
198219 std::ostream* os_warning)
199220{
221+ if (pw_rho == nullptr )
222+ {
223+ warn (os_warning, pw_world, " elecstate::write_rhog" , " pw_rho is null" );
224+ return false ;
225+ }
226+ if (rhog == nullptr )
227+ {
228+ warn (os_warning, pw_world, " elecstate::write_rhog" , " rhog is null" );
229+ return false ;
230+ }
231+ if (nspin <= 0 || nspin > 4 )
232+ {
233+ warn (os_warning, pw_world, " elecstate::write_rhog" , " nspin must be 1, 2, or 4" );
234+ return false ;
235+ }
236+
200237 // only rank 0 in the domain writes the header; all ranks cooperate
201238 // on sequential writes synchronized by barriers.
202239 const int irank = pw_world.rank ();
@@ -216,7 +253,7 @@ bool ModuleIO::write_rhog(const std::string& fchg,
216253 ofs.open (fchg, std::ios::binary);
217254 if (!ofs)
218255 {
219- warn (os_warning, pw_world, " ModuleIO ::write_rhog" , " File I/O failure: cannot open file " + fchg);
256+ warn (os_warning, pw_world, " elecstate ::write_rhog" , " File I/O failure: cannot open file " + fchg);
220257 return false ;
221258 }
222259 ofs.write (reinterpret_cast <char *>(&size), sizeof (size));
0 commit comments