From c17ee12989b95e2fc160032494d19380230879f3 Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Fri, 9 Apr 2021 15:57:17 -0400 Subject: [PATCH] Update dump xyz/gz --- src/COMPRESS/dump_xyz_gz.cpp | 57 +++++++------------ src/COMPRESS/dump_xyz_gz.h | 5 +- src/COMPRESS/dump_xyz_zstd.cpp | 3 +- unittest/formats/test_dump_xyz_compressed.cpp | 4 +- 4 files changed, 27 insertions(+), 42 deletions(-) diff --git a/src/COMPRESS/dump_xyz_gz.cpp b/src/COMPRESS/dump_xyz_gz.cpp index c63d354e80..5c90c48d0f 100644 --- a/src/COMPRESS/dump_xyz_gz.cpp +++ b/src/COMPRESS/dump_xyz_gz.cpp @@ -15,19 +15,13 @@ #include "error.h" #include "update.h" - #include - using namespace LAMMPS_NS; DumpXYZGZ::DumpXYZGZ(LAMMPS *lmp, int narg, char **arg) : DumpXYZ(lmp, narg, arg) { - gzFp = nullptr; - - compression_level = Z_BEST_COMPRESSION; - if (!compressed) error->all(FLERR,"Dump xyz/gz only writes compressed files"); } @@ -37,12 +31,8 @@ DumpXYZGZ::DumpXYZGZ(LAMMPS *lmp, int narg, char **arg) : DumpXYZGZ::~DumpXYZGZ() { - if (gzFp) gzclose(gzFp); - gzFp = nullptr; - fp = nullptr; } - /* ---------------------------------------------------------------------- generic opening of a dump file ASCII or binary or gzipped @@ -92,17 +82,12 @@ void DumpXYZGZ::openfile() // each proc with filewriter = 1 opens a file if (filewriter) { - std::string mode; - if (append_flag) { - mode = fmt::format("ab{}", compression_level); - } else { - mode = fmt::format("wb{}", compression_level); + try { + writer.open(filecurrent, append_flag); + } catch (FileWriterException &e) { + error->one(FLERR, e.what()); } - - gzFp = gzopen(filecurrent, mode.c_str()); - - if (gzFp == nullptr) error->one(FLERR,"Cannot open dump file"); - } else gzFp = nullptr; + } // delete string with timestep replaced @@ -112,8 +97,9 @@ void DumpXYZGZ::openfile() void DumpXYZGZ::write_header(bigint ndump) { if (me == 0) { - gzprintf(gzFp,BIGINT_FORMAT "\n",ndump); - gzprintf(gzFp,"Atoms. Timestep: " BIGINT_FORMAT "\n",update->ntimestep); + std::string header = fmt::format("{}\n", ndump); + header += fmt::format("Atoms. Timestep: {}\n", update->ntimestep); + writer.write(header.c_str(), header.length()); } } @@ -121,7 +107,7 @@ void DumpXYZGZ::write_header(bigint ndump) void DumpXYZGZ::write_data(int n, double *mybuf) { - gzwrite(gzFp,mybuf,sizeof(char)*n); + writer.write(mybuf, n); } /* ---------------------------------------------------------------------- */ @@ -131,11 +117,11 @@ void DumpXYZGZ::write() DumpXYZ::write(); if (filewriter) { if (multifile) { - gzclose(gzFp); - gzFp = nullptr; + writer.close(); } else { - if (flush_flag) - gzflush(gzFp,Z_SYNC_FLUSH); + if (flush_flag && writer.isopen()) { + writer.flush(); + } } } } @@ -146,14 +132,15 @@ int DumpXYZGZ::modify_param(int narg, char **arg) { int consumed = DumpXYZ::modify_param(narg, arg); if (consumed == 0) { - if (strcmp(arg[0],"compression_level") == 0) { - if (narg < 2) error->all(FLERR,"Illegal dump_modify command"); - int min_level = Z_DEFAULT_COMPRESSION; - int max_level = Z_BEST_COMPRESSION; - compression_level = utils::inumeric(FLERR, arg[1], false, lmp); - if (compression_level < min_level || compression_level > max_level) - error->all(FLERR, fmt::format("Illegal dump_modify command: compression level must in the range of [{}, {}]", min_level, max_level)); - return 2; + try { + if (strcmp(arg[0],"compression_level") == 0) { + if (narg < 2) error->all(FLERR,"Illegal dump_modify command"); + int compression_level = utils::inumeric(FLERR, arg[1], false, lmp); + writer.setCompressionLevel(compression_level); + return 2; + } + } catch (FileWriterException &e) { + error->one(FLERR, fmt::format("Illegal dump_modify command: {}", e.what())); } } return consumed; diff --git a/src/COMPRESS/dump_xyz_gz.h b/src/COMPRESS/dump_xyz_gz.h index 834db488a5..2fce0a3f5a 100644 --- a/src/COMPRESS/dump_xyz_gz.h +++ b/src/COMPRESS/dump_xyz_gz.h @@ -21,7 +21,7 @@ DumpStyle(xyz/gz,DumpXYZGZ) #define LMP_DUMP_XYZ_GZ_H #include "dump_xyz.h" -#include +#include "gz_file_writer.h" namespace LAMMPS_NS { @@ -31,8 +31,7 @@ class DumpXYZGZ : public DumpXYZ { virtual ~DumpXYZGZ(); protected: - int compression_level; - gzFile gzFp; // file pointer for the compressed output stream + GzFileWriter writer; virtual void openfile(); virtual void write_header(bigint); diff --git a/src/COMPRESS/dump_xyz_zstd.cpp b/src/COMPRESS/dump_xyz_zstd.cpp index 7c5b73d0ba..03edf561b1 100644 --- a/src/COMPRESS/dump_xyz_zstd.cpp +++ b/src/COMPRESS/dump_xyz_zstd.cpp @@ -19,7 +19,6 @@ #include "dump_xyz_zstd.h" #include "error.h" -#include "file_writer.h" #include "update.h" #include @@ -158,7 +157,7 @@ int DumpXYZZstd::modify_param(int narg, char **arg) return 2; } } catch (FileWriterException &e) { - error->one(FLERR, e.what()); + error->one(FLERR, fmt::format("Illegal dump_modify command: {}", e.what())); } } return consumed; diff --git a/unittest/formats/test_dump_xyz_compressed.cpp b/unittest/formats/test_dump_xyz_compressed.cpp index dad7911b4c..b627cb5a99 100644 --- a/unittest/formats/test_dump_xyz_compressed.cpp +++ b/unittest/formats/test_dump_xyz_compressed.cpp @@ -195,7 +195,7 @@ TEST_F(DumpXYZCompressTest, compressed_modify_bad_param) command(fmt::format("dump id1 all {} 1 {}", compression_style, compressed_dump_filename("modify_bad_param_run0_*.melt.xyz"))); END_HIDE_OUTPUT(); - TEST_FAILURE(".*ERROR: Illegal dump_modify command: compression level must in the range of.*", + TEST_FAILURE(".*ERROR on proc 0: Illegal dump_modify command: Compression level must in the range of.*", command("dump_modify id1 compression_level 12"); ); } @@ -208,7 +208,7 @@ TEST_F(DumpXYZCompressTest, compressed_modify_multi_bad_param) command(fmt::format("dump id1 all {} 1 {}", compression_style, compressed_dump_filename("modify_multi_bad_param_run0_*.melt.xyz"))); END_HIDE_OUTPUT(); - TEST_FAILURE(".*ERROR: Illegal dump_modify command: compression level must in the range of.*", + TEST_FAILURE(".*ERROR on proc 0: Illegal dump_modify command: Compression level must in the range of.*", command("dump_modify id1 pad 3 compression_level 12"); ); }