From 2682663df6ac7ee5092b2df090a95c5349074471 Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Fri, 9 Apr 2021 11:41:45 -0400 Subject: [PATCH] Use GzFileWriter in dump cfg/gz --- src/COMPRESS/dump_cfg_gz.cpp | 88 ++++++++----------- src/COMPRESS/dump_cfg_gz.h | 5 +- src/COMPRESS/dump_cfg_zstd.cpp | 3 +- unittest/formats/test_dump_cfg_compressed.cpp | 4 +- 4 files changed, 41 insertions(+), 59 deletions(-) diff --git a/src/COMPRESS/dump_cfg_gz.cpp b/src/COMPRESS/dump_cfg_gz.cpp index 378baf502f..6bbf118789 100644 --- a/src/COMPRESS/dump_cfg_gz.cpp +++ b/src/COMPRESS/dump_cfg_gz.cpp @@ -11,41 +11,30 @@ See the README file in the top-level LAMMPS directory. ------------------------------------------------------------------------- */ -#include "dump_cfg_gz.h" #include "atom.h" #include "domain.h" +#include "dump_cfg_gz.h" #include "error.h" #include "update.h" - #include - using namespace LAMMPS_NS; #define UNWRAPEXPAND 10.0 DumpCFGGZ::DumpCFGGZ(LAMMPS *lmp, int narg, char **arg) : DumpCFG(lmp, narg, arg) { - gzFp = nullptr; - - compression_level = Z_BEST_COMPRESSION; - if (!compressed) error->all(FLERR,"Dump cfg/gz only writes compressed files"); } - /* ---------------------------------------------------------------------- */ DumpCFGGZ::~DumpCFGGZ() { - if (gzFp) gzclose(gzFp); - gzFp = nullptr; - fp = nullptr; } - /* ---------------------------------------------------------------------- generic opening of a dump file ASCII or binary or gzipped @@ -95,17 +84,12 @@ void DumpCFGGZ::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 @@ -127,30 +111,30 @@ void DumpCFGGZ::write_header(bigint n) if (atom->peri_flag) scale = atom->pdscale; else if (unwrapflag == 1) scale = UNWRAPEXPAND; - char str[64]; - sprintf(str,"Number of particles = %s\n",BIGINT_FORMAT); - gzprintf(gzFp,str,n); - gzprintf(gzFp,"A = %g Angstrom (basic length-scale)\n",scale); - gzprintf(gzFp,"H0(1,1) = %g A\n",domain->xprd); - gzprintf(gzFp,"H0(1,2) = 0 A \n"); - gzprintf(gzFp,"H0(1,3) = 0 A \n"); - gzprintf(gzFp,"H0(2,1) = %g A \n",domain->xy); - gzprintf(gzFp,"H0(2,2) = %g A\n",domain->yprd); - gzprintf(gzFp,"H0(2,3) = 0 A \n"); - gzprintf(gzFp,"H0(3,1) = %g A \n",domain->xz); - gzprintf(gzFp,"H0(3,2) = %g A \n",domain->yz); - gzprintf(gzFp,"H0(3,3) = %g A\n",domain->zprd); - gzprintf(gzFp,".NO_VELOCITY.\n"); - gzprintf(gzFp,"entry_count = %d\n",nfield-2); + std::string header = fmt::format("Number of particles = {}\n", n); + header += fmt::format("A = {0:g} Angstrom (basic length-scale)\n", scale); + header += fmt::format("H0(1,1) = {0:g} A\n",domain->xprd); + header += fmt::format("H0(1,2) = 0 A \n"); + header += fmt::format("H0(1,3) = 0 A \n"); + header += fmt::format("H0(2,1) = {0:g} A \n",domain->xy); + header += fmt::format("H0(2,2) = {0:g} A\n",domain->yprd); + header += fmt::format("H0(2,3) = 0 A \n"); + header += fmt::format("H0(3,1) = {0:g} A \n",domain->xz); + header += fmt::format("H0(3,2) = {0:g} A \n",domain->yz); + header += fmt::format("H0(3,3) = {0:g} A\n",domain->zprd); + header += fmt::format(".NO_VELOCITY.\n"); + header += fmt::format("entry_count = {}\n",nfield-2); for (int i = 0; i < nfield-5; i++) - gzprintf(gzFp,"auxiliary[%d] = %s\n",i,auxname[i]); + header += fmt::format("auxiliary[{}] = {}\n",i,auxname[i]); + + writer.write(header.c_str(), header.length()); } /* ---------------------------------------------------------------------- */ void DumpCFGGZ::write_data(int n, double *mybuf) { - gzwrite(gzFp,mybuf,sizeof(char)*n); + writer.write(mybuf, n); } /* ---------------------------------------------------------------------- */ @@ -160,11 +144,11 @@ void DumpCFGGZ::write() DumpCFG::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(); + } } } } @@ -175,16 +159,16 @@ int DumpCFGGZ::modify_param(int narg, char **arg) { int consumed = DumpCFG::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_cfg_gz.h b/src/COMPRESS/dump_cfg_gz.h index 0c6ed24f06..2844902a38 100644 --- a/src/COMPRESS/dump_cfg_gz.h +++ b/src/COMPRESS/dump_cfg_gz.h @@ -21,7 +21,7 @@ DumpStyle(cfg/gz,DumpCFGGZ) #define LMP_DUMP_CFG_GZ_H #include "dump_cfg.h" -#include +#include "gz_file_writer.h" namespace LAMMPS_NS { @@ -31,8 +31,7 @@ class DumpCFGGZ : public DumpCFG { virtual ~DumpCFGGZ(); 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_cfg_zstd.cpp b/src/COMPRESS/dump_cfg_zstd.cpp index 459649c70a..978e695d1a 100644 --- a/src/COMPRESS/dump_cfg_zstd.cpp +++ b/src/COMPRESS/dump_cfg_zstd.cpp @@ -21,7 +21,6 @@ #include "domain.h" #include "dump_cfg_zstd.h" #include "error.h" -#include "file_writer.h" #include "update.h" #include @@ -46,7 +45,7 @@ DumpCFGZstd::~DumpCFGZstd() /* ---------------------------------------------------------------------- generic opening of a dump file - ASCII or binary or zstdipped + ASCII or binary or compressed some derived classes override this function ------------------------------------------------------------------------- */ diff --git a/unittest/formats/test_dump_cfg_compressed.cpp b/unittest/formats/test_dump_cfg_compressed.cpp index 20f902091b..6d5e8bcf04 100644 --- a/unittest/formats/test_dump_cfg_compressed.cpp +++ b/unittest/formats/test_dump_cfg_compressed.cpp @@ -234,7 +234,7 @@ TEST_F(DumpCfgCompressTest, compressed_modify_bad_param) command(fmt::format("dump id1 all {} 1 {} {}", compression_style, compressed_dump_filename("modify_bad_param_run0_*.melt.cfg"), fields)); 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"); ); } @@ -248,7 +248,7 @@ TEST_F(DumpCfgCompressTest, compressed_modify_multi_bad_param) command(fmt::format("dump id1 all {} 1 {} {}", compression_style, compressed_dump_filename("modify_multi_bad_param_run0_*.melt.cfg"), fields)); 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"); ); }