use platform code for reading/writing of compressed text file via a pipe

This commit is contained in:
Axel Kohlmeyer
2021-10-06 15:04:48 -04:00
parent 891d4c278f
commit ec1a55b35b
15 changed files with 104 additions and 193 deletions

View File

@ -123,7 +123,10 @@ nfileevery(0), fp(nullptr), xf(nullptr), xold(nullptr)
FixTMD::~FixTMD()
{
if (nfileevery && me == 0) fclose(fp);
if (nfileevery && me == 0) {
if (compressed) platform::pclose(fp);
else fclose(fp);
}
// unregister callbacks to this fix from Atom class
@ -517,30 +520,18 @@ void FixTMD::readfile(char *file)
test if gzipped
------------------------------------------------------------------------- */
void FixTMD::open(char *file)
void FixTMD::open(const std::string &file)
{
if (utils::strmatch(file,"\\.gz$")) {
if (platform::has_zip_extension(file)) {
compressed = 1;
#ifdef LAMMPS_GZIP
auto gunzip = fmt::format("gzip -c -d {}",file);
#ifdef _WIN32
fp = _popen(gunzip.c_str(),"rb");
#else
fp = popen(gunzip.c_str(),"r");
#endif
#else
error->one(FLERR,"Cannot open gzipped file without gzip support");
#endif
fp = platform::zip_read(file);
if (!fp) error->one(FLERR,"Cannot open compressed file for reading");
} else {
compressed = 0;
fp = fopen(file,"r");
fp = fopen(file.c_str(),"r");
}
if (fp == nullptr)
error->one(FLERR,"Cannot open file {}: {}",file, utils::getsyserror());
if (!fp) error->one(FLERR,"Cannot open file {}: {}", file, utils::getsyserror());
}
/* ---------------------------------------------------------------------- */

View File

@ -52,7 +52,7 @@ class FixTMD : public Fix {
double **xf, **xold;
void readfile(char *);
void open(char *);
void open(const std::string &);
};
} // namespace LAMMPS_NS

View File

@ -55,10 +55,13 @@ DynamicalMatrix::DynamicalMatrix(LAMMPS *lmp) : Command(lmp), fp(nullptr)
DynamicalMatrix::~DynamicalMatrix()
{
if (fp && me == 0) fclose(fp);
if (fp && me == 0) {
if (compressed) platform::pclose(fp);
else fclose(fp);
memory->destroy(groupmap);
fp = nullptr;
}
}
/* ----------------------------------------------------------------------
setup without output or one-time post-init setup
@ -215,28 +218,20 @@ void DynamicalMatrix::options(int narg, char **arg)
void DynamicalMatrix::openfile(const char *filename)
{
// if file already opened, return
//if (me!=0) return;
if (file_opened) return;
fp = nullptr;
if (me == 0) {
if (compressed) {
#ifdef LAMMPS_GZIP
char gzip[128];
sprintf(gzip,"gzip -6 > %s",filename);
#ifdef _WIN32
fp = _popen(gzip,"wb");
#else
fp = popen(gzip,"w");
#endif
#else
error->one(FLERR,"Cannot open gzipped file");
#endif
fp = platform::zip_write(std::string(filename)+".gz");
if (!fp) error->one(FLERR,"Cannot open gzipped file");
} else if (binaryflag) {
fp = fopen(filename,"wb");
} else {
fp = fopen(filename,"w");
}
if (fp == nullptr) error->one(FLERR,"Cannot open dump file");
if (!fp) error->one(FLERR,"Cannot open dynmat file: {}", utils::getsyserror());
}
file_opened = 1;
}

View File

@ -55,7 +55,10 @@ ThirdOrder::ThirdOrder(LAMMPS *lmp) : Command(lmp), fp(nullptr)
ThirdOrder::~ThirdOrder()
{
if (fp && me == 0) fclose(fp);
if (fp && me == 0) {
if (compressed) platform::pclose(fp);
else fclose(fp);
}
fp = nullptr;
memory->destroy(groupmap);
}
@ -215,27 +218,19 @@ void ThirdOrder::openfile(const char* filename)
{
// if file already opened, return
if (file_opened) return;
fp = nullptr;
if (me == 0) {
if (compressed) {
#ifdef LAMMPS_GZIP
char gzip[128];
sprintf(gzip,"gzip -6 > %s",filename);
#ifdef _WIN32
fp = _popen(gzip,"wb");
#else
fp = popen(gzip,"w");
#endif
#else
error->one(FLERR,"Cannot open gzipped file");
#endif
fp = platform::zip_write(std::string(filename)+".gz");
if (!fp) error->one(FLERR,"Cannot open gzipped file");
} else if (binaryflag) {
fp = fopen(filename,"wb");
} else {
fp = fopen(filename,"w");
}
if (fp == nullptr) error->one(FLERR,"Cannot open dump file");
if (!fp) error->one(FLERR,"Cannot open third_order file: {}", utils::getsyserror());
}
file_opened = 1;
}

View File

@ -45,29 +45,20 @@ FixReaxFFBonds::FixReaxFFBonds(LAMMPS *lmp, int narg, char **arg) :
MPI_Comm_size(world,&nprocs);
ntypes = atom->ntypes;
nmax = atom->nmax;
compressed = 0;
nevery = utils::inumeric(FLERR,arg[3],false,lmp);
if (nevery <= 0)
error->all(FLERR,"Illegal fix reaxff/bonds command");
if (nevery <= 0) error->all(FLERR,"Illegal fix reaxff/bonds command");
if (me == 0) {
char *suffix = strrchr(arg[4],'.');
if (suffix && strcmp(suffix,".gz") == 0) {
#ifdef LAMMPS_GZIP
auto gzip = fmt::format("gzip -6 > {}",arg[4]);
#ifdef _WIN32
fp = _popen(gzip.c_str(),"wb");
#else
fp = popen(gzip.c_str(),"w");
#endif
#else
error->one(FLERR,"Cannot open gzipped file");
#endif
if (platform::has_zip_extension(arg[4])) {
compressed = 1;
fp = platform::zip_write(arg[4]);
if (!fp) error->one(FLERR,"Cannot open compressed file");
} else fp = fopen(arg[4],"w");
if (!fp)
error->one(FLERR,fmt::format("Cannot open fix reaxff/bonds file {}: "
if (!fp) error->one(FLERR,fmt::format("Cannot open fix reaxff/bonds file {}: "
"{}",arg[4],utils::getsyserror()));
}

View File

@ -36,7 +36,7 @@ class FixReaxFFBonds : public Fix {
void end_of_step();
protected:
int me, nprocs, nmax, ntypes, maxsize;
int me, nprocs, nmax, ntypes, maxsize, compressed;
int *numneigh;
tagint **neighid;
double **abo;

View File

@ -56,6 +56,7 @@ FixReaxFFSpecies::FixReaxFFSpecies(LAMMPS *lmp, int narg, char **arg) :
size_peratom_cols = 0;
peratom_freq = 1;
compressed = 0;
nvalid = -1;
MPI_Comm_rank(world,&me);
@ -106,22 +107,13 @@ FixReaxFFSpecies::FixReaxFFSpecies(LAMMPS *lmp, int narg, char **arg) :
strcpy(tmparg[2],arg[5]);
if (me == 0) {
char *suffix = strrchr(arg[6],'.');
if (suffix && strcmp(suffix,".gz") == 0) {
#ifdef LAMMPS_GZIP
auto gzip = fmt::format("gzip -6 > {}",arg[6]);
#ifdef _WIN32
fp = _popen(gzip.c_str(),"wb");
#else
fp = popen(gzip.c_str(),"w");
#endif
#else
error->one(FLERR,"Cannot open gzipped file");
#endif
if (platform::has_zip_extension(arg[6])) {
fp = platform::zip_write(arg[6]);
compressed = 1;
if (!fp) error->one(FLERR,"Cannot open compressed file");
} else fp = fopen(arg[6],"w");
if (!fp)
error->one(FLERR,fmt::format("Cannot open fix reaxff/species file {}: "
if (!fp) error->one(FLERR,fmt::format("Cannot open fix reaxff/species file {}: "
"{}",arg[6],utils::getsyserror()));
}
@ -256,8 +248,11 @@ FixReaxFFSpecies::~FixReaxFFSpecies()
if (filepos)
delete [] filepos;
if (me == 0) fclose(fp);
if (me == 0 && posflag && multipos_opened) fclose(pos);
if (me == 0) {
if (compressed) platform::pclose(fp);
else fclose(fp);
if (posflag && multipos_opened) fclose(pos);
}
modify->delete_compute("SPECATOM");
modify->delete_fix("SPECBOND");
@ -683,8 +678,7 @@ void FixReaxFFSpecies::OpenPos()
char *ptr = strchr(filepos,'*');
*ptr = '\0';
if (padflag == 0)
sprintf(filecurrent,"%s" BIGINT_FORMAT "%s",
filepos,ntimestep,ptr+1);
sprintf(filecurrent,"%s" BIGINT_FORMAT "%s",filepos,ntimestep,ptr+1);
else {
char bif[8],pad[16];
strcpy(bif,BIGINT_FORMAT);

View File

@ -45,7 +45,7 @@ class FixReaxFFSpecies : public Fix {
protected:
int me, nprocs, nmax, nlocal, ntypes, ntotal;
int nrepeat, nfreq, posfreq;
int nrepeat, nfreq, posfreq, compressed;
int Nmoltype, vector_nmole, vector_nspec;
int *Name, *MolName, *NMol, *nd, *MolType, *molmap;
double *clusterID;

View File

@ -94,7 +94,10 @@ NEB::~NEB()
MPI_Comm_free(&roots);
memory->destroy(all);
delete[] rdist;
if (fp) fclose(fp);
if (fp) {
if (compressed) platform::pclose(fp);
else fclose(fp);
}
}
/* ----------------------------------------------------------------------
@ -539,28 +542,17 @@ void NEB::readfile(char *file, int flag)
/* ----------------------------------------------------------------------
universe proc 0 opens NEB data file
test if gzipped
test if compressed
------------------------------------------------------------------------- */
void NEB::open(char *file)
{
compressed = 0;
char *suffix = file + strlen(file) - 3;
if (suffix > file && strcmp(suffix,".gz") == 0) compressed = 1;
if (!compressed) fp = fopen(file,"r");
else {
#ifdef LAMMPS_GZIP
auto gunzip = std::string("gzip -c -d ") + file;
#ifdef _WIN32
fp = _popen(gunzip.c_str(),"rb");
#else
fp = popen(gunzip.c_str(),"r");
#endif
#else
error->one(FLERR,"Cannot open gzipped file");
#endif
}
if (platform::has_zip_extension(file)) {
compressed = 1;
fp = platform::zip_read(file);
if (!fp) error->one(FLERR,"Cannot open compressed file");
} else fp = fopen(file,"r");
if (fp == nullptr)
error->one(FLERR,"Cannot open file {}: {}",file,utils::getsyserror());

View File

@ -79,7 +79,10 @@ NEBSpin::~NEBSpin()
MPI_Comm_free(&roots);
memory->destroy(all);
delete[] rdist;
if (fp) fclose(fp);
if (fp) {
if (compressed) platform::pclose(fp);
else fclose(fp);
}
}
/* ----------------------------------------------------------------------
@ -690,22 +693,10 @@ int NEBSpin::initial_rotation(double *spi, double *sploc, double fraction)
void NEBSpin::open(char *file)
{
compressed = 0;
char *suffix = file + strlen(file) - 3;
if (suffix > file && strcmp(suffix,".gz") == 0) compressed = 1;
if (!compressed) fp = fopen(file,"r");
else {
#ifdef LAMMPS_GZIP
auto gunzip = std::string("gzip -c -d ") + file;
#ifdef _WIN32
fp = _popen(gunzip.c_str(),"rb");
#else
fp = popen(gunzip.c_str(),"r");
#endif
#else
error->one(FLERR,"Cannot open gzipped file");
#endif
}
if (platform::has_zip_extension(file)) {
fp = platform::zip_read(file);
if (!fp) error->one(FLERR,"Cannot open compressed file");
} else fp = fopen(file,"r");
if (fp == nullptr)
error->one(FLERR,"Cannot open file {}: {}",file,utils::getsyserror());

View File

@ -108,9 +108,8 @@ Dump::Dump(LAMMPS *lmp, int /*narg*/, char **arg) : Pointers(lmp)
// if contains '%', write one file per proc and replace % with proc-ID
// if contains '*', write one file per timestep and replace * with timestep
// check file suffixes
// if ends in .bin = binary file
// else if ends in .gz = gzipped text file
// else if ends in .zst = Zstd compressed text file
// if ends in .bin -> binary file
// else if ends in .gz or other known extensions -> compressed text file
// else ASCII text file
fp = nullptr;
@ -144,8 +143,7 @@ Dump::Dump(LAMMPS *lmp, int /*narg*/, char **arg) : Pointers(lmp)
if (strchr(filename,'*')) multifile = 1;
if (utils::strmatch(filename, "\\.bin$")) binary = 1;
if (utils::strmatch(filename, "\\.gz$")
|| utils::strmatch(filename, "\\.zst$")) compressed = 1;
if (platform::has_zip_extension(filename)) compressed = 1;
}
/* ---------------------------------------------------------------------- */
@ -580,16 +578,7 @@ void Dump::openfile()
if (filewriter) {
if (compressed) {
#ifdef LAMMPS_GZIP
auto gzip = fmt::format("gzip -6 > {}",filecurrent);
#ifdef _WIN32
fp = _popen(gzip.c_str(),"wb");
#else
fp = popen(gzip.c_str(),"w");
#endif
#else
error->one(FLERR,"Cannot open gzipped file");
#endif
fp = platform::zip_write(filecurrent);
} else if (binary) {
fp = fopen(filecurrent,"wb");
} else if (append_flag) {

View File

@ -741,7 +741,7 @@ void ReadData::command(int narg, char **arg)
// close file
if (me == 0) {
if (compressed) pclose(fp);
if (compressed) platform::pclose(fp);
else fclose(fp);
fp = nullptr;
}
@ -1954,31 +1954,17 @@ int ReadData::reallocate(int **pcount, int cmax, int amax)
test if gzipped
------------------------------------------------------------------------- */
void ReadData::open(char *file)
void ReadData::open(const std::string &file)
{
if (utils::strmatch(file,"\\.gz$")) {
if (platform::has_zip_extension(file)) {
compressed = 1;
#ifdef LAMMPS_GZIP
auto gunzip = fmt::format("gzip -c -d {}",file);
#ifdef _WIN32
fp = _popen(gunzip.c_str(),"rb");
#else
fp = popen(gunzip.c_str(),"r");
#endif
#else
error->one(FLERR,"Cannot open gzipped file without gzip support");
#endif
fp = platform::zip_read(file);
if (!fp) error->one(FLERR,"Cannot open compressed file {}", file);
} else {
compressed = 0;
fp = fopen(file,"r");
fp = fopen(file.c_str(),"r");
if (!fp) error->one(FLERR,"Cannot open file {}: {}", file, utils::getsyserror());
}
if (fp == nullptr)
error->one(FLERR,"Cannot open file {}: {}",
file, utils::getsyserror());
}
/* ----------------------------------------------------------------------

View File

@ -78,7 +78,7 @@ class ReadData : public Command {
// methods
void open(char *);
void open(const std::string &);
void scan(int &, int &, int &, int &);
int reallocate(int **, int, int);
void header(int);

View File

@ -32,33 +32,20 @@ Reader::Reader(LAMMPS *lmp) : Pointers(lmp)
generic version for ASCII files that may be compressed
------------------------------------------------------------------------- */
void Reader::open_file(const char *file)
void Reader::open_file(const std::string &file)
{
if (fp != nullptr) close_file();
if (utils::strmatch(file,"\\.gz$")) {
if (platform::has_zip_extension(file)) {
compressed = 1;
#ifdef LAMMPS_GZIP
auto gunzip = fmt::format("gzip -c -d {}",file);
#ifdef _WIN32
fp = _popen(gunzip.c_str(),"rb");
#else
fp = popen(gunzip.c_str(),"r");
#endif
#else
error->one(FLERR,"Cannot open gzipped file without gzip support");
#endif
fp = platform::zip_read(file);
if (!fp) error->one(FLERR,"Cannot open compressed file for reading");
} else {
compressed = 0;
fp = fopen(file,"r");
fp = fopen(file.c_str(),"r");
}
if (fp == nullptr)
error->one(FLERR,"Cannot open file {}: {}",
file, utils::getsyserror());
if (!fp) error->one(FLERR,"Cannot open file {}: {}", file, utils::getsyserror());
}
/* ----------------------------------------------------------------------
@ -69,7 +56,7 @@ void Reader::open_file(const char *file)
void Reader::close_file()
{
if (fp == nullptr) return;
if (compressed) pclose(fp);
if (compressed) platform::pclose(fp);
else fclose(fp);
fp = nullptr;
}

View File

@ -33,7 +33,7 @@ class Reader : protected Pointers {
int &, int &, int &) = 0;
virtual void read_atoms(int, int, double **) = 0;
virtual void open_file(const char *);
virtual void open_file(const std::string &);
virtual void close_file();
protected: