Update dump xyz/gz

This commit is contained in:
Richard Berger
2021-04-09 15:57:17 -04:00
parent eb3cddb028
commit c17ee12989
4 changed files with 27 additions and 42 deletions

View File

@ -15,19 +15,13 @@
#include "error.h" #include "error.h"
#include "update.h" #include "update.h"
#include <cstring> #include <cstring>
using namespace LAMMPS_NS; using namespace LAMMPS_NS;
DumpXYZGZ::DumpXYZGZ(LAMMPS *lmp, int narg, char **arg) : DumpXYZGZ::DumpXYZGZ(LAMMPS *lmp, int narg, char **arg) :
DumpXYZ(lmp, narg, arg) DumpXYZ(lmp, narg, arg)
{ {
gzFp = nullptr;
compression_level = Z_BEST_COMPRESSION;
if (!compressed) if (!compressed)
error->all(FLERR,"Dump xyz/gz only writes compressed files"); error->all(FLERR,"Dump xyz/gz only writes compressed files");
} }
@ -37,12 +31,8 @@ DumpXYZGZ::DumpXYZGZ(LAMMPS *lmp, int narg, char **arg) :
DumpXYZGZ::~DumpXYZGZ() DumpXYZGZ::~DumpXYZGZ()
{ {
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
@ -92,17 +82,12 @@ void DumpXYZGZ::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
@ -112,8 +97,9 @@ void DumpXYZGZ::openfile()
void DumpXYZGZ::write_header(bigint ndump) void DumpXYZGZ::write_header(bigint ndump)
{ {
if (me == 0) { if (me == 0) {
gzprintf(gzFp,BIGINT_FORMAT "\n",ndump); std::string header = fmt::format("{}\n", ndump);
gzprintf(gzFp,"Atoms. Timestep: " BIGINT_FORMAT "\n",update->ntimestep); 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) 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(); DumpXYZ::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();
}
} }
} }
} }
@ -146,14 +132,15 @@ int DumpXYZGZ::modify_param(int narg, char **arg)
{ {
int consumed = DumpXYZ::modify_param(narg, arg); int consumed = DumpXYZ::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(xyz/gz,DumpXYZGZ)
#define LMP_DUMP_XYZ_GZ_H #define LMP_DUMP_XYZ_GZ_H
#include "dump_xyz.h" #include "dump_xyz.h"
#include <zlib.h> #include "gz_file_writer.h"
namespace LAMMPS_NS { namespace LAMMPS_NS {
@ -31,8 +31,7 @@ class DumpXYZGZ : public DumpXYZ {
virtual ~DumpXYZGZ(); virtual ~DumpXYZGZ();
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

@ -19,7 +19,6 @@
#include "dump_xyz_zstd.h" #include "dump_xyz_zstd.h"
#include "error.h" #include "error.h"
#include "file_writer.h"
#include "update.h" #include "update.h"
#include <cstring> #include <cstring>
@ -158,7 +157,7 @@ int DumpXYZZstd::modify_param(int narg, char **arg)
return 2; return 2;
} }
} catch (FileWriterException &e) { } catch (FileWriterException &e) {
error->one(FLERR, e.what()); error->one(FLERR, fmt::format("Illegal dump_modify command: {}", e.what()));
} }
} }
return consumed; return consumed;

View File

@ -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"))); command(fmt::format("dump id1 all {} 1 {}", compression_style, compressed_dump_filename("modify_bad_param_run0_*.melt.xyz")));
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");
); );
} }
@ -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"))); command(fmt::format("dump id1 all {} 1 {}", compression_style, compressed_dump_filename("modify_multi_bad_param_run0_*.melt.xyz")));
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");
); );
} }