Unverified Commit e9fd8b3e authored by Richard Berger's avatar Richard Berger
Browse files

Migrate changes to dump custom/gz and add tests

parent 78a1b925
Loading
Loading
Loading
Loading
+32 −8
Original line number Diff line number Diff line
@@ -15,8 +15,10 @@
#include "domain.h"
#include "error.h"
#include "update.h"
#include "force.h"

#include <cstring>
#include <fmt/format.h>

using namespace LAMMPS_NS;

@@ -25,6 +27,8 @@ DumpCustomGZ::DumpCustomGZ(LAMMPS *lmp, int narg, char **arg) :
{
  gzFp = NULL;

  compression_level = Z_BEST_COMPRESSION;

  if (!compressed)
    error->all(FLERR,"Dump custom/gz only writes compressed files");
}
@@ -91,12 +95,15 @@ void DumpCustomGZ::openfile()
  // each proc with filewriter = 1 opens a file

  if (filewriter) {
    std::string mode;
    if (append_flag) {
      gzFp = gzopen(filecurrent,"ab9");
      mode = fmt::format("ab{}", compression_level);
    } else {
      gzFp = gzopen(filecurrent,"wb9");
      mode = fmt::format("wb{}", compression_level);
    }

    gzFp = gzopen(filecurrent, mode.c_str());

    if (gzFp == NULL) error->one(FLERR,"Cannot open dump file");
  } else gzFp = NULL;

@@ -120,14 +127,14 @@ void DumpCustomGZ::write_header(bigint ndump)
    gzprintf(gzFp,BIGINT_FORMAT "\n",ndump);
    if (domain->triclinic == 0) {
      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);
    } else {
      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);
    }
    gzprintf(gzFp,"ITEM: ATOMS %s\n",columns);
  }
@@ -156,3 +163,20 @@ void DumpCustomGZ::write()
  }
}

/* ---------------------------------------------------------------------- */

int DumpCustomGZ::modify_param(int narg, char **arg)
{
  int consumed = DumpCustom::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;
      if (compression_level < 0 || 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;
    }
  }
  return consumed;
}
+3 −0
Original line number Diff line number Diff line
@@ -31,12 +31,15 @@ class DumpCustomGZ : public DumpCustom {
  virtual ~DumpCustomGZ();

 protected:
  int compression_level;
  gzFile gzFp;  // file pointer for the compressed output stream

  virtual void openfile();
  virtual void write_header(bigint);
  virtual void write_data(int, double *);
  virtual void write();

  virtual int modify_param(int, char **);
};

}
+7 −0
Original line number Diff line number Diff line
@@ -31,12 +31,19 @@ set_tests_properties(DumpAtom PROPERTIES ENVIRONMENT "LAMMPS_POTENTIALS=${LAMMPS

if (PKG_COMPRESS)
    find_program(GZIP_BINARY NAMES gzip REQUIRED)

    add_executable(test_dump_atom_gz test_dump_atom_gz.cpp)
    target_link_libraries(test_dump_atom_gz PRIVATE lammps GTest::GMock GTest::GTest)
    add_test(NAME DumpAtomGZ COMMAND test_dump_atom_gz WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
    set_tests_properties(DumpAtomGZ PROPERTIES ENVIRONMENT "LAMMPS_POTENTIALS=${LAMMPS_POTENTIALS_DIR}")
    set_tests_properties(DumpAtomGZ PROPERTIES ENVIRONMENT "GZIP_BINARY=${GZIP_BINARY}")

    add_executable(test_dump_custom_gz test_dump_custom_gz.cpp)
    target_link_libraries(test_dump_custom_gz PRIVATE lammps GTest::GMock GTest::GTest)
    add_test(NAME DumpCustomGZ COMMAND test_dump_custom_gz WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
    set_tests_properties(DumpCustomGZ PROPERTIES ENVIRONMENT "LAMMPS_POTENTIALS=${LAMMPS_POTENTIALS_DIR}")
    set_tests_properties(DumpCustomGZ PROPERTIES ENVIRONMENT "GZIP_BINARY=${GZIP_BINARY}")

    find_program(ZSTD_BINARY NAMES zstd)

    if (ZSTD_BINARY)
+140 −0
Original line number Diff line number Diff line
/* ----------------------------------------------------------------------
   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"

using ::testing::Eq;

char * GZIP_BINARY = nullptr;

class DumpCustomGZTest : public MeltTest {
    std::string dump_style = "custom";
public:
    void enable_triclinic() {
        if (!verbose) ::testing::internal::CaptureStdout();
        command("change_box all triclinic");
        if (!verbose) ::testing::internal::GetCapturedStdout();
    }

    void generate_dump(std::string dump_file, std::string fields, std::string dump_modify_options, int ntimesteps) {
        if (!verbose) ::testing::internal::CaptureStdout();
        command(fmt::format("dump id all {} 1 {} {}", dump_style, dump_file, fields));

        if (!dump_modify_options.empty()) {
            command(fmt::format("dump_modify id {}", dump_modify_options));
        }

        command(fmt::format("run {}", ntimesteps));
        if (!verbose) ::testing::internal::GetCapturedStdout();
    }

    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 {} > {}", GZIP_BINARY, compressed_file, converted_file);
        system(cmdline.c_str());
        if (!verbose) ::testing::internal::GetCapturedStdout();
        return converted_file;
    }
};

TEST_F(DumpCustomGZTest, compressed_run1)
{
    if(!GZIP_BINARY) GTEST_SKIP();

    auto text_file = "dump_custom_text_run1.melt";
    auto compressed_file = "dump_custom_compressed_run1.melt.gz";
    auto fields = "id type proc x y z ix iy iz xs ys zs xu yu zu xsu ysu zsu vx vy vz fx fy fz";

    generate_text_and_compressed_dump(text_file, compressed_file, "custom/gz", fields, "units yes", 1);

    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);
}

TEST_F(DumpCustomGZTest, compressed_triclinic_run1)
{
    if(!GZIP_BINARY) GTEST_SKIP();

    auto text_file = "dump_custom_tri_text_run1.melt";
    auto compressed_file = "dump_custom_tri_compressed_run1.melt.gz";
    auto fields = "id type proc x y z xs ys zs xsu ysu zsu vx vy vz fx fy fz";
    
    enable_triclinic();

    generate_text_and_compressed_dump(text_file, compressed_file, "custom/gz", fields, "units yes", 1);

    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<std::string> env = utils::split_words(var);
        for (auto arg : env) {
            if (arg == "-v") {
                verbose = true;
            }
        }
    }

    GZIP_BINARY = getenv("GZIP_BINARY");

    if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = true;

    int rv = RUN_ALL_TESTS();
    MPI_Finalize();
    return rv;
}