diff --git a/src/COMPRESS/dump_cfg_zstd.cpp b/src/COMPRESS/dump_cfg_zstd.cpp index ac746df431..2b683b4861 100644 --- a/src/COMPRESS/dump_cfg_zstd.cpp +++ b/src/COMPRESS/dump_cfg_zstd.cpp @@ -11,6 +11,10 @@ See the README file in the top-level LAMMPS directory. ------------------------------------------------------------------------- */ +/* ---------------------------------------------------------------------- + Contributing author: Richard Berger (Temple U) +------------------------------------------------------------------------- */ + #include "dump_cfg_zstd.h" #include "atom.h" #include "domain.h" diff --git a/src/COMPRESS/dump_cfg_zstd.h b/src/COMPRESS/dump_cfg_zstd.h index 71e06b0529..653ff6a776 100644 --- a/src/COMPRESS/dump_cfg_zstd.h +++ b/src/COMPRESS/dump_cfg_zstd.h @@ -11,6 +11,10 @@ See the README file in the top-level LAMMPS directory. ------------------------------------------------------------------------- */ +/* ---------------------------------------------------------------------- + Contributing author: Richard Berger (Temple U) +------------------------------------------------------------------------- */ + #ifdef DUMP_CLASS DumpStyle(cfg/zstd,DumpCFGZstd) diff --git a/src/COMPRESS/dump_local_gz.cpp b/src/COMPRESS/dump_local_gz.cpp index 280b66bb2a..eb7e31ad36 100644 --- a/src/COMPRESS/dump_local_gz.cpp +++ b/src/COMPRESS/dump_local_gz.cpp @@ -122,18 +122,18 @@ void DumpLocalGZ::write_header(bigint ndump) gzprintf(gzFp,"ITEM: TIMESTEP\n"); gzprintf(gzFp,BIGINT_FORMAT "\n",update->ntimestep); - gzprintf(gzFp,"ITEM: NUMBER OF ENTRIES\n"); + gzprintf(gzFp,"ITEM: NUMBER OF %s\n",label); gzprintf(gzFp,BIGINT_FORMAT "\n",ndump); if (domain->triclinic) { gzprintf(gzFp,"ITEM: BOX BOUNDS xy xz yz %s\n",boundstr); - gzprintf(gzFp,"%-1.16g %-1.16g %-1.16g\n",boxxlo,boxxhi,boxxy); - gzprintf(gzFp,"%-1.16g %-1.16g %-1.16g\n",boxylo,boxyhi,boxxz); - gzprintf(gzFp,"%-1.16g %-1.16g %-1.16g\n",boxzlo,boxzhi,boxyz); + gzprintf(gzFp,"%-1.16e %-1.16e %-1.16e\n",boxxlo,boxxhi,boxxy); + gzprintf(gzFp,"%-1.16e %-1.16e %-1.16e\n",boxylo,boxyhi,boxxz); + gzprintf(gzFp,"%-1.16e %-1.16e %-1.16e\n",boxzlo,boxzhi,boxyz); } else { gzprintf(gzFp,"ITEM: BOX BOUNDS %s\n",boundstr); - gzprintf(gzFp,"%-1.16g %-1.16g\n",boxxlo,boxxhi); - gzprintf(gzFp,"%-1.16g %-1.16g\n",boxylo,boxyhi); - gzprintf(gzFp,"%-1.16g %-1.16g\n",boxzlo,boxzhi); + gzprintf(gzFp,"%-1.16e %-1.16e\n",boxxlo,boxxhi); + gzprintf(gzFp,"%-1.16e %-1.16e\n",boxylo,boxyhi); + gzprintf(gzFp,"%-1.16e %-1.16e\n",boxzlo,boxzhi); } gzprintf(gzFp,"ITEM: %s %s\n",label,columns); } diff --git a/src/COMPRESS/dump_local_zstd.cpp b/src/COMPRESS/dump_local_zstd.cpp new file mode 100644 index 0000000000..643e29f2cc --- /dev/null +++ b/src/COMPRESS/dump_local_zstd.cpp @@ -0,0 +1,191 @@ +/* ---------------------------------------------------------------------- + LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator + http://lammps.sandia.gov, Sandia National Laboratories + Steve Plimpton, sjplimp@sandia.gov + + Copyright (2003) Sandia Corporation. Under the terms of Contract + DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains + certain rights in this software. This software is distributed under + the GNU General Public License. + + See the README file in the top-level LAMMPS directory. +------------------------------------------------------------------------- */ + +/* ---------------------------------------------------------------------- + Contributing author: Richard Berger (Temple U) +------------------------------------------------------------------------- */ + +#include "dump_local_zstd.h" +#include "domain.h" +#include "error.h" +#include "update.h" +#include "force.h" + +#include +#include + +using namespace LAMMPS_NS; + +DumpLocalZstd::DumpLocalZstd(LAMMPS *lmp, int narg, char **arg) : + DumpLocal(lmp, narg, arg) +{ + if (!compressed) + error->all(FLERR,"Dump local/zstd only writes compressed files"); +} + + +/* ---------------------------------------------------------------------- */ + +DumpLocalZstd::~DumpLocalZstd() +{ +} + + +/* ---------------------------------------------------------------------- + generic opening of a dump file + ASCII or binary or gzipped + some derived classes override this function +------------------------------------------------------------------------- */ + +void DumpLocalZstd::openfile() +{ + // single file, already opened, so just return + + if (singlefile_opened) return; + if (multifile == 0) singlefile_opened = 1; + + // if one file per timestep, replace '*' with current timestep + + char *filecurrent = filename; + if (multiproc) filecurrent = multiname; + + if (multifile) { + char *filestar = filecurrent; + filecurrent = new char[strlen(filestar) + 16]; + char *ptr = strchr(filestar,'*'); + *ptr = '\0'; + if (padflag == 0) + sprintf(filecurrent,"%s" BIGINT_FORMAT "%s", + filestar,update->ntimestep,ptr+1); + else { + char bif[8],pad[16]; + strcpy(bif,BIGINT_FORMAT); + sprintf(pad,"%%s%%0%d%s%%s",padflag,&bif[1]); + sprintf(filecurrent,pad,filestar,update->ntimestep,ptr+1); + } + *ptr = '*'; + if (maxfiles > 0) { + if (numfiles < maxfiles) { + nameslist[numfiles] = new char[strlen(filecurrent)+1]; + strcpy(nameslist[numfiles],filecurrent); + ++numfiles; + } else { + remove(nameslist[fileidx]); + delete[] nameslist[fileidx]; + nameslist[fileidx] = new char[strlen(filecurrent)+1]; + strcpy(nameslist[fileidx],filecurrent); + fileidx = (fileidx + 1) % maxfiles; + } + } + } + + // each proc with filewriter = 1 opens a file + + if (filewriter) { + if (append_flag) { + error->one(FLERR, "dump cfg/zstd currently doesn't support append"); + } + + try { + writer.open(filecurrent); + } catch (FileWriterException & e) { + error->one(FLERR, e.what()); + } + } + + // delete string with timestep replaced + + if (multifile) delete [] filecurrent; +} + +void DumpLocalZstd::write_header(bigint ndump) +{ + std::string header; + + if ((multiproc) || (!multiproc && me == 0)) { + if (unit_flag && !unit_count) { + ++unit_count; + header = fmt::format("ITEM: UNITS\n{}\n",update->unit_style); + } + + if (time_flag) { + header += fmt::format("ITEM: TIME\n{0:.16g}\n", compute_time()); + } + + header += fmt::format("ITEM: TIMESTEP\n{}\n", update->ntimestep); + header += fmt::format("ITEM: NUMBER OF {}\n{}\n", label, ndump); + if (domain->triclinic == 0) { + header += fmt::format("ITEM: BOX BOUNDS {}\n", boundstr); + header += fmt::format("{0:-1.16e} {1:-1.16e}\n", boxxlo, boxxhi); + header += fmt::format("{0:-1.16e} {1:-1.16e}\n", boxylo, boxyhi); + header += fmt::format("{0:-1.16e} {1:-1.16e}\n", boxzlo, boxzhi); + } else { + header += fmt::format("ITEM: BOX BOUNDS xy xz yz {}\n", boundstr); + header += fmt::format("{0:-1.16e} {1:-1.16e} {2:-1.16e}\n", boxxlo, boxxhi, boxxy); + header += fmt::format("{0:-1.16e} {1:-1.16e} {2:-1.16e}\n", boxylo, boxyhi, boxxz); + header += fmt::format("{0:-1.16e} {1:-1.16e} {2:-1.16e}\n", boxzlo, boxzhi, boxyz); + } + header += fmt::format("ITEM: {} {}\n", label, columns); + + writer.write(header.c_str(), header.length()); + } +} + +/* ---------------------------------------------------------------------- */ + +void DumpLocalZstd::write_data(int n, double *mybuf) +{ + writer.write(mybuf, sizeof(char)*n); +} + +/* ---------------------------------------------------------------------- */ + +void DumpLocalZstd::write() +{ + DumpLocal::write(); + if (filewriter) { + if (multifile) { + writer.close(); + } else { + if (flush_flag && writer.isopen()) { + writer.flush(); + } + } + } +} + +/* ---------------------------------------------------------------------- */ + +int DumpLocalZstd::modify_param(int narg, char **arg) +{ + int consumed = DumpLocal::modify_param(narg, arg); + if(consumed == 0) { + try { + if (strcmp(arg[0],"checksum") == 0) { + if (narg < 2) error->all(FLERR,"Illegal dump_modify command"); + if (strcmp(arg[1],"yes") == 0) writer.setChecksum(true); + else if (strcmp(arg[1],"no") == 0) writer.setChecksum(false); + else error->all(FLERR,"Illegal dump_modify command"); + return 2; + } else if (strcmp(arg[0],"compression_level") == 0) { + if (narg < 2) error->all(FLERR,"Illegal dump_modify command"); + int compression_level = force->inumeric(FLERR,arg[1]); + writer.setCompressionLevel(compression_level); + return 2; + } + } catch (FileWriterException & e) { + error->one(FLERR, e.what()); + } + } + return consumed; +} diff --git a/src/COMPRESS/dump_local_zstd.h b/src/COMPRESS/dump_local_zstd.h new file mode 100644 index 0000000000..5f3ddb84f2 --- /dev/null +++ b/src/COMPRESS/dump_local_zstd.h @@ -0,0 +1,63 @@ +/* -*- c++ -*- ---------------------------------------------------------- + LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator + http://lammps.sandia.gov, Sandia National Laboratories + Steve Plimpton, sjplimp@sandia.gov + + Copyright (2003) Sandia Corporation. Under the terms of Contract + DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains + certain rights in this software. This software is distributed under + the GNU General Public License. + + See the README file in the top-level LAMMPS directory. +------------------------------------------------------------------------- */ + +/* ---------------------------------------------------------------------- + Contributing author: Richard Berger (Temple U) +------------------------------------------------------------------------- */ + +#ifdef DUMP_CLASS + +DumpStyle(local/zstd,DumpLocalZstd) + +#else + +#ifndef LMP_DUMP_LOCAL_ZSTD_H +#define LMP_DUMP_LOCAL_ZSTD_H + +#include "dump_local.h" +#include "zstd_file_writer.h" + +namespace LAMMPS_NS { + +class DumpLocalZstd : public DumpLocal { + public: + DumpLocalZstd(class LAMMPS *, int, char **); + virtual ~DumpLocalZstd(); + + protected: + ZstdFileWriter writer; + + virtual void openfile(); + virtual void write_header(bigint); + virtual void write_data(int, double *); + virtual void write(); + + virtual int modify_param(int, char **); +}; + +} + +#endif +#endif + +/* ERROR/WARNING messages: + +E: Dump local/zstd only writes compressed files + +The dump local/zstd output file name must have a .zst suffix. + +E: Cannot open dump file + +Self-explanatory. + +*/ diff --git a/src/COMPRESS/dump_xyz_zstd.cpp b/src/COMPRESS/dump_xyz_zstd.cpp new file mode 100644 index 0000000000..e825051474 --- /dev/null +++ b/src/COMPRESS/dump_xyz_zstd.cpp @@ -0,0 +1,166 @@ +/* ---------------------------------------------------------------------- + LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator + http://lammps.sandia.gov, Sandia National Laboratories + Steve Plimpton, sjplimp@sandia.gov + + Copyright (2003) Sandia Corporation. Under the terms of Contract + DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains + certain rights in this software. This software is distributed under + the GNU General Public License. + + See the README file in the top-level LAMMPS directory. +------------------------------------------------------------------------- */ + +/* ---------------------------------------------------------------------- + Contributing author: Richard Berger (Temple U) +------------------------------------------------------------------------- */ + +#include "dump_xyz_zstd.h" +#include "error.h" +#include "update.h" +#include "force.h" + +#include +#include + +using namespace LAMMPS_NS; + +DumpXYZZstd::DumpXYZZstd(LAMMPS *lmp, int narg, char **arg) : + DumpXYZ(lmp, narg, arg) +{ + if (!compressed) + error->all(FLERR,"Dump xyz/zstd only writes compressed files"); +} + + +/* ---------------------------------------------------------------------- */ + +DumpXYZZstd::~DumpXYZZstd() +{ +} + + +/* ---------------------------------------------------------------------- + generic opening of a dump file + ASCII or binary or gzipped + some derived classes override this function +------------------------------------------------------------------------- */ + +void DumpXYZZstd::openfile() +{ + // single file, already opened, so just return + + if (singlefile_opened) return; + if (multifile == 0) singlefile_opened = 1; + + // if one file per timestep, replace '*' with current timestep + + char *filecurrent = filename; + if (multiproc) filecurrent = multiname; + + if (multifile) { + char *filestar = filecurrent; + filecurrent = new char[strlen(filestar) + 16]; + char *ptr = strchr(filestar,'*'); + *ptr = '\0'; + if (padflag == 0) + sprintf(filecurrent,"%s" BIGINT_FORMAT "%s", + filestar,update->ntimestep,ptr+1); + else { + char bif[8],pad[16]; + strcpy(bif,BIGINT_FORMAT); + sprintf(pad,"%%s%%0%d%s%%s",padflag,&bif[1]); + sprintf(filecurrent,pad,filestar,update->ntimestep,ptr+1); + } + *ptr = '*'; + if (maxfiles > 0) { + if (numfiles < maxfiles) { + nameslist[numfiles] = new char[strlen(filecurrent)+1]; + strcpy(nameslist[numfiles],filecurrent); + ++numfiles; + } else { + remove(nameslist[fileidx]); + delete[] nameslist[fileidx]; + nameslist[fileidx] = new char[strlen(filecurrent)+1]; + strcpy(nameslist[fileidx],filecurrent); + fileidx = (fileidx + 1) % maxfiles; + } + } + } + + // each proc with filewriter = 1 opens a file + + if (filewriter) { + if (append_flag) { + error->one(FLERR, "dump cfg/zstd currently doesn't support append"); + } + + try { + writer.open(filecurrent); + } catch (FileWriterException & e) { + error->one(FLERR, e.what()); + } + } + + // delete string with timestep replaced + + if (multifile) delete [] filecurrent; +} + +void DumpXYZZstd::write_header(bigint ndump) +{ + if (me == 0) { + std::string header = fmt::format("{}\n", ndump); + header += fmt::format("Atoms. Timestep: {}\n", update->ntimestep); + writer.write(header.c_str(), header.length()); + } +} + +/* ---------------------------------------------------------------------- */ + +void DumpXYZZstd::write_data(int n, double *mybuf) +{ + writer.write(mybuf, n); +} + +/* ---------------------------------------------------------------------- */ + +void DumpXYZZstd::write() +{ + DumpXYZ::write(); + if (filewriter) { + if (multifile) { + writer.close(); + } else { + if (flush_flag && writer.isopen()) { + writer.flush(); + } + } + } +} + +/* ---------------------------------------------------------------------- */ + +int DumpXYZZstd::modify_param(int narg, char **arg) +{ + int consumed = DumpXYZ::modify_param(narg, arg); + if(consumed == 0) { + try { + if (strcmp(arg[0],"checksum") == 0) { + if (narg < 2) error->all(FLERR,"Illegal dump_modify command"); + if (strcmp(arg[1],"yes") == 0) writer.setChecksum(true); + else if (strcmp(arg[1],"no") == 0) writer.setChecksum(false); + else error->all(FLERR,"Illegal dump_modify command"); + return 2; + } else if (strcmp(arg[0],"compression_level") == 0) { + if (narg < 2) error->all(FLERR,"Illegal dump_modify command"); + int compression_level = force->inumeric(FLERR,arg[1]); + writer.setCompressionLevel(compression_level); + return 2; + } + } catch (FileWriterException & e) { + error->one(FLERR, e.what()); + } + } + return consumed; +} diff --git a/src/COMPRESS/dump_xyz_zstd.h b/src/COMPRESS/dump_xyz_zstd.h new file mode 100644 index 0000000000..e1991202d6 --- /dev/null +++ b/src/COMPRESS/dump_xyz_zstd.h @@ -0,0 +1,63 @@ +/* -*- c++ -*- ---------------------------------------------------------- + LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator + http://lammps.sandia.gov, Sandia National Laboratories + Steve Plimpton, sjplimp@sandia.gov + + Copyright (2003) Sandia Corporation. Under the terms of Contract + DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains + certain rights in this software. This software is distributed under + the GNU General Public License. + + See the README file in the top-level LAMMPS directory. +------------------------------------------------------------------------- */ + +/* ---------------------------------------------------------------------- + Contributing author: Richard Berger (Temple U) +------------------------------------------------------------------------- */ + +#ifdef DUMP_CLASS + +DumpStyle(xyz/zstd,DumpXYZZstd) + +#else + +#ifndef LMP_DUMP_XYZ_ZSTD_H +#define LMP_DUMP_XYZ_ZSTD_H + +#include "dump_xyz.h" +#include "zstd_file_writer.h" + +namespace LAMMPS_NS { + +class DumpXYZZstd : public DumpXYZ { + public: + DumpXYZZstd(class LAMMPS *, int, char **); + virtual ~DumpXYZZstd(); + + protected: + ZstdFileWriter writer; + + virtual void openfile(); + virtual void write_header(bigint); + virtual void write_data(int, double *); + virtual void write(); + + virtual int modify_param(int, char **); +}; + +} + +#endif +#endif + +/* ERROR/WARNING messages: + +E: Dump xyz/zstd only writes compressed files + +The dump xyz/zstd output file name must have a .zst suffix. + +E: Cannot open dump file + +Self-explanatory. + +*/ diff --git a/src/dump_local.cpp b/src/dump_local.cpp index 103836b8a2..0709eed952 100644 --- a/src/dump_local.cpp +++ b/src/dump_local.cpp @@ -268,14 +268,14 @@ void DumpLocal::write_header(bigint ndump) fprintf(fp,BIGINT_FORMAT "\n",ndump); if (domain->triclinic) { fprintf(fp,"ITEM: BOX BOUNDS xy xz yz %s\n",boundstr); - fprintf(fp,"%-1.16g %-1.16g %-1.16g\n",boxxlo,boxxhi,boxxy); - fprintf(fp,"%-1.16g %-1.16g %-1.16g\n",boxylo,boxyhi,boxxz); - fprintf(fp,"%-1.16g %-1.16g %-1.16g\n",boxzlo,boxzhi,boxyz); + fprintf(fp,"%-1.16e %-1.16e %-1.16e\n",boxxlo,boxxhi,boxxy); + fprintf(fp,"%-1.16e %-1.16e %-1.16e\n",boxylo,boxyhi,boxxz); + fprintf(fp,"%-1.16e %-1.16e %-1.16e\n",boxzlo,boxzhi,boxyz); } else { fprintf(fp,"ITEM: BOX BOUNDS %s\n",boundstr); - fprintf(fp,"%-1.16g %-1.16g\n",boxxlo,boxxhi); - fprintf(fp,"%-1.16g %-1.16g\n",boxylo,boxyhi); - fprintf(fp,"%-1.16g %-1.16g\n",boxzlo,boxzhi); + fprintf(fp,"%-1.16e %-1.16e\n",boxxlo,boxxhi); + fprintf(fp,"%-1.16e %-1.16e\n",boxylo,boxyhi); + fprintf(fp,"%-1.16e %-1.16e\n",boxzlo,boxzhi); } fprintf(fp,"ITEM: %s %s\n",label,columns); } diff --git a/unittest/formats/CMakeLists.txt b/unittest/formats/CMakeLists.txt index 80c09d691a..0a3e7bce56 100644 --- a/unittest/formats/CMakeLists.txt +++ b/unittest/formats/CMakeLists.txt @@ -82,6 +82,18 @@ if (PKG_COMPRESS) add_test(NAME DumpCfgZstd COMMAND test_dump_cfg_zstd WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) set_tests_properties(DumpCfgZstd PROPERTIES ENVIRONMENT "LAMMPS_POTENTIALS=${LAMMPS_POTENTIALS_DIR}") set_tests_properties(DumpCfgZstd PROPERTIES ENVIRONMENT "ZSTD_BINARY=${ZSTD_BINARY}") + + add_executable(test_dump_xyz_zstd test_dump_xyz_zstd.cpp) + target_link_libraries(test_dump_xyz_zstd PRIVATE lammps GTest::GMock GTest::GTest) + add_test(NAME DumpXYZZstd COMMAND test_dump_xyz_zstd WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) + set_tests_properties(DumpXYZZstd PROPERTIES ENVIRONMENT "LAMMPS_POTENTIALS=${LAMMPS_POTENTIALS_DIR}") + set_tests_properties(DumpXYZZstd PROPERTIES ENVIRONMENT "ZSTD_BINARY=${ZSTD_BINARY}") + + add_executable(test_dump_local_zstd test_dump_local_zstd.cpp) + target_link_libraries(test_dump_local_zstd PRIVATE lammps GTest::GMock GTest::GTest) + add_test(NAME DumpLocalZstd COMMAND test_dump_local_zstd WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) + set_tests_properties(DumpLocalZstd PROPERTIES ENVIRONMENT "LAMMPS_POTENTIALS=${LAMMPS_POTENTIALS_DIR}") + set_tests_properties(DumpLocalZstd PROPERTIES ENVIRONMENT "ZSTD_BINARY=${ZSTD_BINARY}") endif() endif() diff --git a/unittest/formats/test_dump_local_zstd.cpp b/unittest/formats/test_dump_local_zstd.cpp new file mode 100644 index 0000000000..cf29f4ee04 --- /dev/null +++ b/unittest/formats/test_dump_local_zstd.cpp @@ -0,0 +1,106 @@ +/* ---------------------------------------------------------------------- + LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator + http://lammps.sandia.gov, Sandia National Laboratories + Steve Plimpton, sjplimp@sandia.gov + + Copyright (2003) Sandia Corporation. Under the terms of Contract + DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains + certain rights in this software. This software is distributed under + the GNU General Public License. + + See the README file in the top-level LAMMPS directory. +------------------------------------------------------------------------- */ + +#include "gtest/gtest.h" +#include "gmock/gmock.h" +#include "fmt/format.h" +#include "utils.h" +#include "../testing/core.h" +#include "../testing/systems/melt.h" +#include "../testing/utils.h" + +char * ZSTD_BINARY = nullptr; + +using ::testing::Eq; + +class DumpLocalGZTest : public MeltTest { + std::string dump_style = "local"; +public: + void generate_text_and_compressed_dump(std::string text_file, std::string compressed_file, std::string compression_style, + std::string fields, std::string dump_modify_options, int ntimesteps) { + if (!verbose) ::testing::internal::CaptureStdout(); + command(fmt::format("dump id0 all {} 1 {} {}", dump_style, text_file, fields)); + command(fmt::format("dump id1 all {} 1 {} {}", compression_style, compressed_file, fields)); + + if (!dump_modify_options.empty()) { + command(fmt::format("dump_modify id0 {}", dump_modify_options)); + command(fmt::format("dump_modify id1 {}", dump_modify_options)); + } + + command(fmt::format("run {}", ntimesteps)); + if (!verbose) ::testing::internal::GetCapturedStdout(); + } + + std::string convert_compressed_to_text(std::string compressed_file) { + if (!verbose) ::testing::internal::CaptureStdout(); + std::string converted_file = compressed_file.substr(0, compressed_file.find_last_of('.')); + std::string cmdline = fmt::format("{} -d -c {} > {}", ZSTD_BINARY, compressed_file, converted_file); + system(cmdline.c_str()); + if (!verbose) ::testing::internal::GetCapturedStdout(); + return converted_file; + } +}; + +TEST_F(DumpLocalGZTest, compressed_run0) +{ + if(!ZSTD_BINARY) GTEST_SKIP(); + + if (!verbose) ::testing::internal::CaptureStdout(); + command("compute comp all pair/local dist eng"); + if (!verbose) ::testing::internal::GetCapturedStdout(); + + auto text_files = "dump_local_zstd_text_run*.melt.local"; + auto compressed_files = "dump_local_zstd_compressed_run*.melt.local.zst"; + auto text_file = "dump_local_zstd_text_run0.melt.local"; + auto compressed_file = "dump_local_zstd_compressed_run0.melt.local.zst"; + auto fields = "index c_comp[1]"; + + generate_text_and_compressed_dump(text_files, compressed_files, "local/zstd", fields, "", 0); + + TearDown(); + + ASSERT_FILE_EXISTS(text_file); + ASSERT_FILE_EXISTS(compressed_file); + + auto converted_file = convert_compressed_to_text(compressed_file); + + ASSERT_FILE_EXISTS(converted_file); + ASSERT_FILE_EQUAL(text_file, converted_file); + delete_file(text_file); + delete_file(compressed_file); + delete_file(converted_file); +} + +int main(int argc, char **argv) +{ + MPI_Init(&argc, &argv); + ::testing::InitGoogleMock(&argc, argv); + + // handle arguments passed via environment variable + if (const char *var = getenv("TEST_ARGS")) { + std::vector env = utils::split_words(var); + for (auto arg : env) { + if (arg == "-v") { + verbose = true; + } + } + } + + ZSTD_BINARY = getenv("ZSTD_BINARY"); + + if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = true; + + int rv = RUN_ALL_TESTS(); + MPI_Finalize(); + return rv; +} diff --git a/unittest/formats/test_dump_xyz_zstd.cpp b/unittest/formats/test_dump_xyz_zstd.cpp new file mode 100644 index 0000000000..70684b594f --- /dev/null +++ b/unittest/formats/test_dump_xyz_zstd.cpp @@ -0,0 +1,101 @@ +/* ---------------------------------------------------------------------- + LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator + http://lammps.sandia.gov, Sandia National Laboratories + Steve Plimpton, sjplimp@sandia.gov + + Copyright (2003) Sandia Corporation. Under the terms of Contract + DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains + certain rights in this software. This software is distributed under + the GNU General Public License. + + See the README file in the top-level LAMMPS directory. +------------------------------------------------------------------------- */ + +#include "gtest/gtest.h" +#include "gmock/gmock.h" +#include "fmt/format.h" +#include "utils.h" +#include "../testing/core.h" +#include "../testing/systems/melt.h" +#include "../testing/utils.h" + +char * ZSTD_BINARY = nullptr; + +using ::testing::Eq; + +class DumpXYZGZTest : public MeltTest { + std::string dump_style = "xyz"; +public: + void generate_text_and_compressed_dump(std::string text_file, std::string compressed_file, std::string compression_style, + std::string dump_modify_options, int ntimesteps) { + if (!verbose) ::testing::internal::CaptureStdout(); + command(fmt::format("dump id0 all {} 1 {}", dump_style, text_file)); + command(fmt::format("dump id1 all {} 1 {}", compression_style, compressed_file)); + + if (!dump_modify_options.empty()) { + command(fmt::format("dump_modify id0 {}", dump_modify_options)); + command(fmt::format("dump_modify id1 {}", dump_modify_options)); + } + + command(fmt::format("run {}", ntimesteps)); + if (!verbose) ::testing::internal::GetCapturedStdout(); + } + + std::string convert_compressed_to_text(std::string compressed_file) { + if (!verbose) ::testing::internal::CaptureStdout(); + std::string converted_file = compressed_file.substr(0, compressed_file.find_last_of('.')); + std::string cmdline = fmt::format("{} -d -c {} > {}", ZSTD_BINARY, compressed_file, converted_file); + system(cmdline.c_str()); + if (!verbose) ::testing::internal::GetCapturedStdout(); + return converted_file; + } +}; + +TEST_F(DumpXYZGZTest, compressed_run0) +{ + if(!ZSTD_BINARY) GTEST_SKIP(); + + auto text_files = "dump_xyz_zstd_text_run*.melt.xyz"; + auto compressed_files = "dump_xyz_zstd_compressed_run*.melt.xyz.zst"; + auto text_file = "dump_xyz_zstd_text_run0.melt.xyz"; + auto compressed_file = "dump_xyz_zstd_compressed_run0.melt.xyz.zst"; + + generate_text_and_compressed_dump(text_files, compressed_files, "xyz/zstd", "", 0); + + TearDown(); + + ASSERT_FILE_EXISTS(text_file); + ASSERT_FILE_EXISTS(compressed_file); + + auto converted_file = convert_compressed_to_text(compressed_file); + + ASSERT_FILE_EXISTS(converted_file); + ASSERT_FILE_EQUAL(text_file, converted_file); + delete_file(text_file); + delete_file(compressed_file); + delete_file(converted_file); +} + +int main(int argc, char **argv) +{ + MPI_Init(&argc, &argv); + ::testing::InitGoogleMock(&argc, argv); + + // handle arguments passed via environment variable + if (const char *var = getenv("TEST_ARGS")) { + std::vector env = utils::split_words(var); + for (auto arg : env) { + if (arg == "-v") { + verbose = true; + } + } + } + + ZSTD_BINARY = getenv("ZSTD_BINARY"); + + if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = true; + + int rv = RUN_ALL_TESTS(); + MPI_Finalize(); + return rv; +}