Use GzFileWriter in dump cfg/gz

This commit is contained in:
Richard Berger 2021-04-09 11:41:45 -04:00
parent ded22bf8bc
commit 2682663df6
No known key found for this signature in database
GPG Key ID: A9E83994E0BA0CAB
4 changed files with 41 additions and 59 deletions

View File

@ -11,41 +11,30 @@
See the README file in the top-level LAMMPS directory. See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
#include "dump_cfg_gz.h"
#include "atom.h" #include "atom.h"
#include "domain.h" #include "domain.h"
#include "dump_cfg_gz.h"
#include "error.h" #include "error.h"
#include "update.h" #include "update.h"
#include <cstring> #include <cstring>
using namespace LAMMPS_NS; using namespace LAMMPS_NS;
#define UNWRAPEXPAND 10.0 #define UNWRAPEXPAND 10.0
DumpCFGGZ::DumpCFGGZ(LAMMPS *lmp, int narg, char **arg) : DumpCFGGZ::DumpCFGGZ(LAMMPS *lmp, int narg, char **arg) :
DumpCFG(lmp, narg, arg) DumpCFG(lmp, narg, arg)
{ {
gzFp = nullptr;
compression_level = Z_BEST_COMPRESSION;
if (!compressed) if (!compressed)
error->all(FLERR,"Dump cfg/gz only writes compressed files"); error->all(FLERR,"Dump cfg/gz only writes compressed files");
} }
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */
DumpCFGGZ::~DumpCFGGZ() DumpCFGGZ::~DumpCFGGZ()
{ {
if (gzFp) gzclose(gzFp);
gzFp = nullptr;
fp = nullptr;
} }
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
generic opening of a dump file generic opening of a dump file
ASCII or binary or gzipped ASCII or binary or gzipped
@ -95,17 +84,12 @@ void DumpCFGGZ::openfile()
// each proc with filewriter = 1 opens a file // each proc with filewriter = 1 opens a file
if (filewriter) { if (filewriter) {
std::string mode; try {
if (append_flag) { writer.open(filecurrent, append_flag);
mode = fmt::format("ab{}", compression_level); } catch (FileWriterException &e) {
} else { error->one(FLERR, e.what());
mode = fmt::format("wb{}", compression_level);
} }
}
gzFp = gzopen(filecurrent, mode.c_str());
if (gzFp == nullptr) error->one(FLERR,"Cannot open dump file");
} else gzFp = nullptr;
// delete string with timestep replaced // delete string with timestep replaced
@ -127,30 +111,30 @@ void DumpCFGGZ::write_header(bigint n)
if (atom->peri_flag) scale = atom->pdscale; if (atom->peri_flag) scale = atom->pdscale;
else if (unwrapflag == 1) scale = UNWRAPEXPAND; else if (unwrapflag == 1) scale = UNWRAPEXPAND;
char str[64]; std::string header = fmt::format("Number of particles = {}\n", n);
sprintf(str,"Number of particles = %s\n",BIGINT_FORMAT); header += fmt::format("A = {0:g} Angstrom (basic length-scale)\n", scale);
gzprintf(gzFp,str,n); header += fmt::format("H0(1,1) = {0:g} A\n",domain->xprd);
gzprintf(gzFp,"A = %g Angstrom (basic length-scale)\n",scale); header += fmt::format("H0(1,2) = 0 A \n");
gzprintf(gzFp,"H0(1,1) = %g A\n",domain->xprd); header += fmt::format("H0(1,3) = 0 A \n");
gzprintf(gzFp,"H0(1,2) = 0 A \n"); header += fmt::format("H0(2,1) = {0:g} A \n",domain->xy);
gzprintf(gzFp,"H0(1,3) = 0 A \n"); header += fmt::format("H0(2,2) = {0:g} A\n",domain->yprd);
gzprintf(gzFp,"H0(2,1) = %g A \n",domain->xy); header += fmt::format("H0(2,3) = 0 A \n");
gzprintf(gzFp,"H0(2,2) = %g A\n",domain->yprd); header += fmt::format("H0(3,1) = {0:g} A \n",domain->xz);
gzprintf(gzFp,"H0(2,3) = 0 A \n"); header += fmt::format("H0(3,2) = {0:g} A \n",domain->yz);
gzprintf(gzFp,"H0(3,1) = %g A \n",domain->xz); header += fmt::format("H0(3,3) = {0:g} A\n",domain->zprd);
gzprintf(gzFp,"H0(3,2) = %g A \n",domain->yz); header += fmt::format(".NO_VELOCITY.\n");
gzprintf(gzFp,"H0(3,3) = %g A\n",domain->zprd); header += fmt::format("entry_count = {}\n",nfield-2);
gzprintf(gzFp,".NO_VELOCITY.\n");
gzprintf(gzFp,"entry_count = %d\n",nfield-2);
for (int i = 0; i < nfield-5; i++) 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) 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(); DumpCFG::write();
if (filewriter) { if (filewriter) {
if (multifile) { if (multifile) {
gzclose(gzFp); writer.close();
gzFp = nullptr;
} else { } else {
if (flush_flag) if (flush_flag && writer.isopen()) {
gzflush(gzFp,Z_SYNC_FLUSH); writer.flush();
}
} }
} }
} }
@ -175,16 +159,16 @@ int DumpCFGGZ::modify_param(int narg, char **arg)
{ {
int consumed = DumpCFG::modify_param(narg, arg); int consumed = DumpCFG::modify_param(narg, arg);
if (consumed == 0) { if (consumed == 0) {
if (strcmp(arg[0],"compression_level") == 0) { try {
if (narg < 2) error->all(FLERR,"Illegal dump_modify command"); if (strcmp(arg[0],"compression_level") == 0) {
int min_level = Z_DEFAULT_COMPRESSION; if (narg < 2) error->all(FLERR,"Illegal dump_modify command");
int max_level = Z_BEST_COMPRESSION; int compression_level = utils::inumeric(FLERR, arg[1], false, lmp);
compression_level = utils::inumeric(FLERR, arg[1], false, lmp); writer.setCompressionLevel(compression_level);
if (compression_level < min_level || compression_level > max_level) return 2;
error->all(FLERR, fmt::format("Illegal dump_modify command: compression level must in the range of [{}, {}]", min_level, max_level)); }
return 2; } catch (FileWriterException &e) {
error->one(FLERR, fmt::format("Illegal dump_modify command: {}", e.what()));
} }
} }
return consumed; return consumed;
} }

View File

@ -21,7 +21,7 @@ DumpStyle(cfg/gz,DumpCFGGZ)
#define LMP_DUMP_CFG_GZ_H #define LMP_DUMP_CFG_GZ_H
#include "dump_cfg.h" #include "dump_cfg.h"
#include <zlib.h> #include "gz_file_writer.h"
namespace LAMMPS_NS { namespace LAMMPS_NS {
@ -31,8 +31,7 @@ class DumpCFGGZ : public DumpCFG {
virtual ~DumpCFGGZ(); virtual ~DumpCFGGZ();
protected: protected:
int compression_level; GzFileWriter writer;
gzFile gzFp; // file pointer for the compressed output stream
virtual void openfile(); virtual void openfile();
virtual void write_header(bigint); virtual void write_header(bigint);

View File

@ -21,7 +21,6 @@
#include "domain.h" #include "domain.h"
#include "dump_cfg_zstd.h" #include "dump_cfg_zstd.h"
#include "error.h" #include "error.h"
#include "file_writer.h"
#include "update.h" #include "update.h"
#include <cstring> #include <cstring>
@ -46,7 +45,7 @@ DumpCFGZstd::~DumpCFGZstd()
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
generic opening of a dump file generic opening of a dump file
ASCII or binary or zstdipped ASCII or binary or compressed
some derived classes override this function some derived classes override this function
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */

View File

@ -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)); command(fmt::format("dump id1 all {} 1 {} {}", compression_style, compressed_dump_filename("modify_bad_param_run0_*.melt.cfg"), fields));
END_HIDE_OUTPUT(); 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"); 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)); command(fmt::format("dump id1 all {} 1 {} {}", compression_style, compressed_dump_filename("modify_multi_bad_param_run0_*.melt.cfg"), fields));
END_HIDE_OUTPUT(); 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"); command("dump_modify id1 pad 3 compression_level 12");
); );
} }