From ec1a55b35bf3c623f3a490d75b9c1e244a61c593 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Wed, 6 Oct 2021 15:04:48 -0400 Subject: [PATCH] use platform code for reading/writing of compressed text file via a pipe --- src/EXTRA-FIX/fix_tmd.cpp | 29 +++++++++--------------- src/EXTRA-FIX/fix_tmd.h | 2 +- src/PHONON/dynamical_matrix.cpp | 37 +++++++++++++------------------ src/PHONON/third_order.cpp | 35 +++++++++++++---------------- src/REAXFF/fix_reaxff_bonds.cpp | 25 +++++++-------------- src/REAXFF/fix_reaxff_bonds.h | 2 +- src/REAXFF/fix_reaxff_species.cpp | 34 ++++++++++++---------------- src/REAXFF/fix_reaxff_species.h | 2 +- src/REPLICA/neb.cpp | 28 +++++++++-------------- src/SPIN/neb_spin.cpp | 25 +++++++-------------- src/dump.cpp | 19 ++++------------ src/read_data.cpp | 28 ++++++----------------- src/read_data.h | 2 +- src/reader.cpp | 27 ++++++---------------- src/reader.h | 2 +- 15 files changed, 104 insertions(+), 193 deletions(-) diff --git a/src/EXTRA-FIX/fix_tmd.cpp b/src/EXTRA-FIX/fix_tmd.cpp index a46eb89713..93d7ae6a56 100644 --- a/src/EXTRA-FIX/fix_tmd.cpp +++ b/src/EXTRA-FIX/fix_tmd.cpp @@ -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()); } /* ---------------------------------------------------------------------- */ diff --git a/src/EXTRA-FIX/fix_tmd.h b/src/EXTRA-FIX/fix_tmd.h index fd818eb583..b85869930a 100644 --- a/src/EXTRA-FIX/fix_tmd.h +++ b/src/EXTRA-FIX/fix_tmd.h @@ -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 diff --git a/src/PHONON/dynamical_matrix.cpp b/src/PHONON/dynamical_matrix.cpp index f7cb3f7f44..b2f473f9cc 100644 --- a/src/PHONON/dynamical_matrix.cpp +++ b/src/PHONON/dynamical_matrix.cpp @@ -55,9 +55,12 @@ 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; + } } /* ---------------------------------------------------------------------- @@ -212,32 +215,24 @@ void DynamicalMatrix::options(int narg, char **arg) some derived classes override this function ------------------------------------------------------------------------- */ -void DynamicalMatrix::openfile(const char* filename) +void DynamicalMatrix::openfile(const char *filename) { // if file already opened, return - //if (me!=0) return; if (file_opened) return; - - 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 - } else if (binaryflag) { + fp = nullptr; + + if (me == 0) { + if (compressed) { + 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 { + } else { fp = fopen(filename,"w"); + } + if (!fp) error->one(FLERR,"Cannot open dynmat file: {}", utils::getsyserror()); } - - if (fp == nullptr) error->one(FLERR,"Cannot open dump file"); - + file_opened = 1; } diff --git a/src/PHONON/third_order.cpp b/src/PHONON/third_order.cpp index 565e8b50e5..ca91a424af 100644 --- a/src/PHONON/third_order.cpp +++ b/src/PHONON/third_order.cpp @@ -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 (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 - } else if (binaryflag) { - fp = fopen(filename,"wb"); - } else { - fp = fopen(filename,"w"); + if (me == 0) { + if (compressed) { + 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) error->one(FLERR,"Cannot open third_order file: {}", utils::getsyserror()); } - - if (fp == nullptr) error->one(FLERR,"Cannot open dump file"); - file_opened = 1; } diff --git a/src/REAXFF/fix_reaxff_bonds.cpp b/src/REAXFF/fix_reaxff_bonds.cpp index f13056c5b6..07d0c73f3d 100644 --- a/src/REAXFF/fix_reaxff_bonds.cpp +++ b/src/REAXFF/fix_reaxff_bonds.cpp @@ -45,30 +45,21 @@ 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 {}: " - "{}",arg[4],utils::getsyserror())); + if (!fp) error->one(FLERR,fmt::format("Cannot open fix reaxff/bonds file {}: " + "{}",arg[4],utils::getsyserror())); } if (atom->tag_consecutive() == 0) diff --git a/src/REAXFF/fix_reaxff_bonds.h b/src/REAXFF/fix_reaxff_bonds.h index 4c2fd535ea..4c40017e43 100644 --- a/src/REAXFF/fix_reaxff_bonds.h +++ b/src/REAXFF/fix_reaxff_bonds.h @@ -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; diff --git a/src/REAXFF/fix_reaxff_species.cpp b/src/REAXFF/fix_reaxff_species.cpp index 4fc08975a8..856020227d 100644 --- a/src/REAXFF/fix_reaxff_species.cpp +++ b/src/REAXFF/fix_reaxff_species.cpp @@ -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,23 +107,14 @@ 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 {}: " - "{}",arg[6],utils::getsyserror())); + if (!fp) error->one(FLERR,fmt::format("Cannot open fix reaxff/species file {}: " + "{}",arg[6],utils::getsyserror())); } x0 = nullptr; @@ -256,9 +248,12 @@ 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); diff --git a/src/REAXFF/fix_reaxff_species.h b/src/REAXFF/fix_reaxff_species.h index bcfcf2b6dd..91326273b6 100644 --- a/src/REAXFF/fix_reaxff_species.h +++ b/src/REAXFF/fix_reaxff_species.h @@ -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; diff --git a/src/REPLICA/neb.cpp b/src/REPLICA/neb.cpp index 9e6e32772d..281d0e830f 100644 --- a/src/REPLICA/neb.cpp +++ b/src/REPLICA/neb.cpp @@ -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()); diff --git a/src/SPIN/neb_spin.cpp b/src/SPIN/neb_spin.cpp index fe24235a98..c2cefdcc97 100644 --- a/src/SPIN/neb_spin.cpp +++ b/src/SPIN/neb_spin.cpp @@ -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()); diff --git a/src/dump.cpp b/src/dump.cpp index 647fd3d38d..637ded53d0 100644 --- a/src/dump.cpp +++ b/src/dump.cpp @@ -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) { diff --git a/src/read_data.cpp b/src/read_data.cpp index bb4fbe0315..bbc3e20d44 100644 --- a/src/read_data.cpp +++ b/src/read_data.cpp @@ -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()); } /* ---------------------------------------------------------------------- diff --git a/src/read_data.h b/src/read_data.h index 482c45e6e6..3374f48be0 100644 --- a/src/read_data.h +++ b/src/read_data.h @@ -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); diff --git a/src/reader.cpp b/src/reader.cpp index 71036e7444..3a07417912 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -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; } diff --git a/src/reader.h b/src/reader.h index 2d71750f98..18977790cd 100644 --- a/src/reader.h +++ b/src/reader.h @@ -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: