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

Add compression_level parameter to dump cfg/gz

parent 5faca3ae
Loading
Loading
Loading
Loading
+26 −2
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@
#include "update.h"

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

using namespace LAMMPS_NS;
#define UNWRAPEXPAND 10.0
@@ -27,6 +28,8 @@ DumpCFGGZ::DumpCFGGZ(LAMMPS *lmp, int narg, char **arg) :
{
  gzFp = NULL;

  compression_level = Z_BEST_COMPRESSION;

  if (!compressed)
    error->all(FLERR,"Dump cfg/gz only writes compressed files");
}
@@ -93,12 +96,15 @@ void DumpCFGGZ::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;

@@ -164,3 +170,21 @@ void DumpCFGGZ::write()
  }
}

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

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;
      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 DumpCFGGZ : public DumpCFG {
  virtual ~DumpCFGGZ();

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

}