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() 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 // unregister callbacks to this fix from Atom class
@ -517,30 +520,18 @@ void FixTMD::readfile(char *file)
test if gzipped 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; compressed = 1;
fp = platform::zip_read(file);
#ifdef LAMMPS_GZIP if (!fp) error->one(FLERR,"Cannot open compressed file for reading");
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
} else { } else {
compressed = 0; compressed = 0;
fp = fopen(file,"r"); fp = fopen(file.c_str(),"r");
} }
if (fp == nullptr) if (!fp) error->one(FLERR,"Cannot open file {}: {}", file, utils::getsyserror());
error->one(FLERR,"Cannot open file {}: {}",file, utils::getsyserror());
} }
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */

View File

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

View File

@ -55,9 +55,12 @@ DynamicalMatrix::DynamicalMatrix(LAMMPS *lmp) : Command(lmp), fp(nullptr)
DynamicalMatrix::~DynamicalMatrix() DynamicalMatrix::~DynamicalMatrix()
{ {
if (fp && me == 0) fclose(fp); if (fp && me == 0) {
if (compressed) platform::pclose(fp);
else fclose(fp);
memory->destroy(groupmap); memory->destroy(groupmap);
fp = nullptr; fp = nullptr;
}
} }
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
@ -212,32 +215,24 @@ void DynamicalMatrix::options(int narg, char **arg)
some derived classes override this function some derived classes override this function
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
void DynamicalMatrix::openfile(const char* filename) void DynamicalMatrix::openfile(const char *filename)
{ {
// if file already opened, return // if file already opened, return
//if (me!=0) return;
if (file_opened) return; if (file_opened) return;
fp = nullptr;
if (compressed) {
#ifdef LAMMPS_GZIP if (me == 0) {
char gzip[128]; if (compressed) {
sprintf(gzip,"gzip -6 > %s",filename); fp = platform::zip_write(std::string(filename)+".gz");
#ifdef _WIN32 if (!fp) error->one(FLERR,"Cannot open gzipped file");
fp = _popen(gzip,"wb"); } else if (binaryflag) {
#else
fp = popen(gzip,"w");
#endif
#else
error->one(FLERR,"Cannot open gzipped file");
#endif
} else if (binaryflag) {
fp = fopen(filename,"wb"); fp = fopen(filename,"wb");
} else { } else {
fp = fopen(filename,"w"); 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; file_opened = 1;
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -94,7 +94,10 @@ NEB::~NEB()
MPI_Comm_free(&roots); MPI_Comm_free(&roots);
memory->destroy(all); memory->destroy(all);
delete[] rdist; 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 universe proc 0 opens NEB data file
test if gzipped test if compressed
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
void NEB::open(char *file) void NEB::open(char *file)
{ {
compressed = 0; compressed = 0;
char *suffix = file + strlen(file) - 3; if (platform::has_zip_extension(file)) {
if (suffix > file && strcmp(suffix,".gz") == 0) compressed = 1; compressed = 1;
if (!compressed) fp = fopen(file,"r"); fp = platform::zip_read(file);
else { if (!fp) error->one(FLERR,"Cannot open compressed file");
#ifdef LAMMPS_GZIP } else fp = fopen(file,"r");
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 (fp == nullptr) if (fp == nullptr)
error->one(FLERR,"Cannot open file {}: {}",file,utils::getsyserror()); error->one(FLERR,"Cannot open file {}: {}",file,utils::getsyserror());

View File

@ -79,7 +79,10 @@ NEBSpin::~NEBSpin()
MPI_Comm_free(&roots); MPI_Comm_free(&roots);
memory->destroy(all); memory->destroy(all);
delete[] rdist; 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) void NEBSpin::open(char *file)
{ {
compressed = 0; compressed = 0;
char *suffix = file + strlen(file) - 3; if (platform::has_zip_extension(file)) {
if (suffix > file && strcmp(suffix,".gz") == 0) compressed = 1; fp = platform::zip_read(file);
if (!compressed) fp = fopen(file,"r"); if (!fp) error->one(FLERR,"Cannot open compressed file");
else { } else fp = fopen(file,"r");
#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 (fp == nullptr) if (fp == nullptr)
error->one(FLERR,"Cannot open file {}: {}",file,utils::getsyserror()); 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 proc and replace % with proc-ID
// if contains '*', write one file per timestep and replace * with timestep // if contains '*', write one file per timestep and replace * with timestep
// check file suffixes // check file suffixes
// if ends in .bin = binary file // if ends in .bin -> binary file
// else if ends in .gz = gzipped text file // else if ends in .gz or other known extensions -> compressed text file
// else if ends in .zst = Zstd compressed text file
// else ASCII text file // else ASCII text file
fp = nullptr; fp = nullptr;
@ -144,8 +143,7 @@ Dump::Dump(LAMMPS *lmp, int /*narg*/, char **arg) : Pointers(lmp)
if (strchr(filename,'*')) multifile = 1; if (strchr(filename,'*')) multifile = 1;
if (utils::strmatch(filename, "\\.bin$")) binary = 1; if (utils::strmatch(filename, "\\.bin$")) binary = 1;
if (utils::strmatch(filename, "\\.gz$") if (platform::has_zip_extension(filename)) compressed = 1;
|| utils::strmatch(filename, "\\.zst$")) compressed = 1;
} }
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */
@ -580,16 +578,7 @@ void Dump::openfile()
if (filewriter) { if (filewriter) {
if (compressed) { if (compressed) {
#ifdef LAMMPS_GZIP fp = platform::zip_write(filecurrent);
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
} else if (binary) { } else if (binary) {
fp = fopen(filecurrent,"wb"); fp = fopen(filecurrent,"wb");
} else if (append_flag) { } else if (append_flag) {

View File

@ -741,7 +741,7 @@ void ReadData::command(int narg, char **arg)
// close file // close file
if (me == 0) { if (me == 0) {
if (compressed) pclose(fp); if (compressed) platform::pclose(fp);
else fclose(fp); else fclose(fp);
fp = nullptr; fp = nullptr;
} }
@ -1954,31 +1954,17 @@ int ReadData::reallocate(int **pcount, int cmax, int amax)
test if gzipped 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; compressed = 1;
fp = platform::zip_read(file);
#ifdef LAMMPS_GZIP if (!fp) error->one(FLERR,"Cannot open compressed file {}", file);
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
} else { } else {
compressed = 0; 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 // methods
void open(char *); void open(const std::string &);
void scan(int &, int &, int &, int &); void scan(int &, int &, int &, int &);
int reallocate(int **, int, int); int reallocate(int **, int, int);
void header(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 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 (fp != nullptr) close_file();
if (utils::strmatch(file,"\\.gz$")) { if (platform::has_zip_extension(file)) {
compressed = 1; compressed = 1;
fp = platform::zip_read(file);
#ifdef LAMMPS_GZIP if (!fp) error->one(FLERR,"Cannot open compressed file for reading");
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
} else { } else {
compressed = 0; compressed = 0;
fp = fopen(file,"r"); fp = fopen(file.c_str(),"r");
} }
if (fp == nullptr) if (!fp) error->one(FLERR,"Cannot open file {}: {}", file, utils::getsyserror());
error->one(FLERR,"Cannot open file {}: {}",
file, utils::getsyserror());
} }
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
@ -69,7 +56,7 @@ void Reader::open_file(const char *file)
void Reader::close_file() void Reader::close_file()
{ {
if (fp == nullptr) return; if (fp == nullptr) return;
if (compressed) pclose(fp); if (compressed) platform::pclose(fp);
else fclose(fp); else fclose(fp);
fp = nullptr; fp = nullptr;
} }

View File

@ -33,7 +33,7 @@ class Reader : protected Pointers {
int &, int &, int &) = 0; int &, int &, int &) = 0;
virtual void read_atoms(int, int, double **) = 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(); virtual void close_file();
protected: protected: