Skip to content

Commit

Permalink
Refactor ModuleIO::read/write_cube()
Browse files Browse the repository at this point in the history
  • Loading branch information
PeizeLin committed Sep 15, 2024
1 parent 7fa03ca commit 862dbe2
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 102 deletions.
4 changes: 2 additions & 2 deletions source/module_hamilt_pw/hamilt_pwdft/parallel_grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ void Parallel_Grid::z_distribution(void)


#ifdef __MPI
void Parallel_Grid::zpiece_to_all(double *zpiece, const int &iz, double *rho)
void Parallel_Grid::zpiece_to_all(double *zpiece, const int &iz, double *rho) const
{
if(PARAM.inp.esolver_type == "sdft")
{
Expand Down Expand Up @@ -256,7 +256,7 @@ void Parallel_Grid::zpiece_to_all(double *zpiece, const int &iz, double *rho)
#endif

#ifdef __MPI
void Parallel_Grid::zpiece_to_stogroup(double *zpiece, const int &iz, double *rho)
void Parallel_Grid::zpiece_to_stogroup(double *zpiece, const int &iz, double *rho) const
{
assert(allocate);
//TITLE("Parallel_Grid","zpiece_to_all");
Expand Down
4 changes: 2 additions & 2 deletions source/module_hamilt_pw/hamilt_pwdft/parallel_grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class Parallel_Grid
const int &nczp, const int &nrxx, const int &nbz, const int &bz); //LiuXh add 20180606

#ifdef __MPI
void zpiece_to_all(double *zpiece, const int &iz, double *rho);
void zpiece_to_stogroup(double *zpiece, const int &iz, double *rho); //qainrui add for sto-dft 2021-7-21
void zpiece_to_all(double *zpiece, const int &iz, double *rho) const;
void zpiece_to_stogroup(double *zpiece, const int &iz, double *rho) const; //qainrui add for sto-dft 2021-7-21

void reduce_to_fullrho(double *rhotot, double *rhoin);
#endif
Expand Down
89 changes: 59 additions & 30 deletions source/module_io/cube_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,74 @@

namespace ModuleIO
{
bool read_cube(
extern bool read_cube(
#ifdef __MPI
Parallel_Grid* Pgrid,
const Parallel_Grid*const Pgrid,
#endif
int my_rank,
std::string esolver_type,
int rank_in_stogroup,
const int& is,
const int my_rank,
const std::string esolver_type,
const int rank_in_stogroup,
const int is,
std::ofstream& ofs_running,
const int& nspin,
const int nspin,
const std::string& fn,
double* data,
const int& nx,
const int& ny,
const int& nz,
double*const data,
const int nx,
const int ny,
const int nz,
double& ef,
const UnitCell* ucell,
const UnitCell*const ucell,
int& prenspin,
const bool& warning_flag = true);
const bool warning_flag = true);

void write_cube(
extern void read_cube_core(
std::ifstream &ifs,
#ifdef __MPI
const int& bz,
const int& nbz,
const int& nplane,
const int& startz_current,
const Parallel_Grid*const Pgrid,
#endif
const double* data,
const int& is,
const int& nspin,
const int& iter,
const int my_rank,
const std::string esolver_type,
const int rank_in_stogroup,
double*const data,
const int nx,
const int ny,
const int nz,
const int nx_read,
const int ny_read,
const int nz_read);

extern void write_cube(
#ifdef __MPI
const int bz,
const int nbz,
const int nplane,
const int startz_current,
#endif
const double*const data,
const int is,
const int nspin,
const int iter,
const std::string& fn,
const int& nx,
const int& ny,
const int& nz,
const double& ef,
const UnitCell* ucell,
const int& precision = 11,
const int& out_fermi = 1); // mohan add 2007-10-17
const int nx,
const int ny,
const int nz,
const double ef,
const UnitCell*const ucell,
const int precision = 11,
const int out_fermi = 1); // mohan add 2007-10-17

extern void write_cube_core(
std::ofstream &ofs_cube,
#ifdef __MPI
const int bz,
const int nbz,
const int nplane,
const int startz_current,
#endif
const double*const data,
const int nx,
const int ny,
const int nz);

/**
* @brief The trilinear interpolation method
Expand Down Expand Up @@ -78,7 +107,7 @@ void write_cube(
* @param nz the dimension of grids along z
* @param data the interpolated results
*/
void trilinear_interpolate(std::ifstream& ifs,
extern void trilinear_interpolate(std::ifstream& ifs,
const int& nx_read,
const int& ny_read,
const int& nz_read,
Expand Down
105 changes: 64 additions & 41 deletions source/module_io/read_cube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,44 @@

bool ModuleIO::read_cube(
#ifdef __MPI
Parallel_Grid* Pgrid,
const Parallel_Grid*const Pgrid,
#endif
int my_rank,
std::string esolver_type,
int rank_in_stogroup,
const int& is,
const int my_rank,
const std::string esolver_type,
const int rank_in_stogroup,
const int is,
std::ofstream& ofs_running,
const int& nspin,
const int nspin,
const std::string& fn,
double* data,
const int& nx,
const int& ny,
const int& nz,
double*const data,
const int nx,
const int ny,
const int nz,
double& ef,
const UnitCell* ucell,
const UnitCell*const ucell,
int& prenspin,
const bool& warning_flag)
const bool warning_flag)
{
ModuleBase::TITLE("ModuleIO","read_cube");
std::ifstream ifs(fn.c_str());
if (!ifs)
{
std::string tmp_warning_info = "!!! Couldn't find the charge file of ";
tmp_warning_info += fn;
ofs_running << tmp_warning_info << std::endl;
return false;
}
else
{
ofs_running << " Find the file, try to read charge from file." << std::endl;
}
if (!ifs)
{
std::string tmp_warning_info = "!!! Couldn't find the charge file of ";
tmp_warning_info += fn;
ofs_running << tmp_warning_info << std::endl;
return false;
}
else
{
ofs_running << " Find the file, try to read charge from file." << std::endl;
}

bool quit=false;
bool quit=false;

ifs.ignore(300, '\n'); // skip the header
ifs.ignore(300, '\n'); // skip the header

if(nspin != 4)
{
if(nspin != 4)
{
int v_in;
ifs >> v_in;
if (v_in != nspin)
Expand All @@ -49,16 +49,16 @@ bool ModuleIO::read_cube(
return false;
}
}
else
{
ifs >> prenspin;
}
ifs.ignore(150, ')');
else
{
ifs >> prenspin;
}
ifs.ignore(150, ')');

ifs >> ef;
ofs_running << " read in fermi energy = " << ef << std::endl;
ifs >> ef;
ofs_running << " read in fermi energy = " << ef << std::endl;

ifs.ignore(150, '\n');
ifs.ignore(150, '\n');

ModuleBase::CHECK_INT(ifs, ucell->nat);
ifs.ignore(150, '\n');
Expand Down Expand Up @@ -93,8 +93,6 @@ bool ModuleIO::read_cube(
ifs >> temp >> temp >> temp;
}

const bool same = (nx == nx_read && ny == ny_read && nz == nz_read) ? true : false;

for (int it = 0; it < ucell->ntype; it++)
{
for (int ia = 0; ia < ucell->atoms[it].na; ia++)
Expand All @@ -114,6 +112,35 @@ bool ModuleIO::read_cube(
}
}

#ifdef __MPI
ModuleIO::read_cube_core(ifs, Pgrid, my_rank, esolver_type, rank_in_stogroup, data, nx, ny, nz, nx_read, ny_read, nz_read);
#elif
ModuleIO::read_cube_core(ifs, my_rank, esolver_type, rank_in_stogroup, data, nx, ny, nz, nx_read, ny_read, nz_read);
#endif

if (my_rank == 0 || (esolver_type == "sdft" && rank_in_stogroup == 0))
ifs.close();
return true;
}

void ModuleIO::read_cube_core(
std::ifstream &ifs,
#ifdef __MPI
const Parallel_Grid*const Pgrid,
#endif
const int my_rank,
const std::string esolver_type,
const int rank_in_stogroup,
double*const data,
const int nx,
const int ny,
const int nz,
const int nx_read,
const int ny_read,
const int nz_read)
{
const bool same = (nx == nx_read && ny == ny_read && nz == nz_read) ? true : false;

#ifdef __MPI
const int nxy = nx * ny;
double* zpiece = nullptr;
Expand Down Expand Up @@ -190,10 +217,6 @@ bool ModuleIO::read_cube(
ModuleIO::trilinear_interpolate(ifs, nx_read, ny_read, nz_read, nx, ny, nz, data);
}
#endif

if (my_rank == 0 || (esolver_type == "sdft" && rank_in_stogroup == 0))
ifs.close();
return true;
}

void ModuleIO::trilinear_interpolate(std::ifstream& ifs,
Expand Down
79 changes: 52 additions & 27 deletions source/module_io/write_cube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@

void ModuleIO::write_cube(
#ifdef __MPI
const int& bz,
const int& nbz,
const int& nplane,
const int& startz_current,
const int bz,
const int nbz,
const int nplane,
const int startz_current,
#endif
const double* data,
const int& is,
const int& nspin,
const int& iter,
const double*const data,
const int is,
const int nspin,
const int iter,
const std::string& fn,
const int& nx,
const int& ny,
const int& nz,
const double& ef,
const UnitCell* ucell,
const int& precision,
const int& out_fermi)
const int nx,
const int ny,
const int nz,
const double ef,
const UnitCell*const ucell,
const int precision,
const int out_fermi)
{
ModuleBase::TITLE("ModuleIO", "write_cube");

Expand Down Expand Up @@ -126,8 +126,44 @@ void ModuleIO::write_cube(
ofs_cube << std::scientific;
}

#ifdef __MPI
ModuleIO::write_cube_core(ofs_cube, bz, nbz, nplane, startz_current, data, nx, ny, nz);
#elif
ModuleIO::write_cube_core(ofs_cube, data, nx, ny, nz);
#endif

if (my_rank == 0)
{
end = time(NULL);
ModuleBase::GlobalFunc::OUT_TIME("write_cube", start, end);

/// for cube file
ofs_cube.close();
}

return;
}


void ModuleIO::write_cube_core(
std::ofstream &ofs_cube,
#ifdef __MPI
const int bz,
const int nbz,
const int nplane,
const int startz_current,
#endif
const double*const data,
const int nx,
const int ny,
const int nz)
{
ModuleBase::TITLE("ModuleIO", "write_cube_core");

#ifdef __MPI


const int my_rank = GlobalV::MY_RANK;
const int my_pool = GlobalV::MY_POOL;
const int rank_in_pool = GlobalV::RANK_IN_POOL;
const int nproc_in_pool = GlobalV::NPROC_IN_POOL;
Expand Down Expand Up @@ -268,15 +304,4 @@ void ModuleIO::write_cube(
}
}
#endif

if (my_rank == 0)
{
end = time(NULL);
ModuleBase::GlobalFunc::OUT_TIME("write_cube", start, end);

/// for cube file
ofs_cube.close();
}

return;
}
}

0 comments on commit 862dbe2

Please sign in to comment.