Commit f7cbdcf9 authored by Steve Plimpton's avatar Steve Plimpton Committed by GitHub
Browse files

Merge pull request #673 from pastewka/17_dump_nc2

Multi file support for NetCDF dump style
parents 4cfa88b7 352a20fc
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -15,9 +15,11 @@ dump_modify dump-ID keyword values ... :pre
dump-ID = ID of dump to modify :ulb,l
one or more keyword/value pairs may be appended :l
these keywords apply to various dump styles :l
keyword = {append} or {buffer} or {element} or {every} or {fileper} or {first} or {flush} or {format} or {image} or {label} or {nfile} or {pad} or {precision} or {region} or {scale} or {sort} or {thresh} or {unwrap} :l
  {append} arg = {yes} or {no} or {at} N
keyword = {append} or {at} or {buffer} or {element} or {every} or {fileper} or {first} or {flush} or {format} or {image} or {label} or {nfile} or {pad} or {precision} or {region} or {scale} or {sort} or {thresh} or {unwrap} :l
  {append} arg = {yes} or {no}
  {at} arg = N
    N = index of frame written upon first dump
    only available after "append yes"
  {buffer} arg = {yes} or {no}
  {element} args = E1 E2 ... EN, where N = # of atom types
    E1,...,EN = element name, e.g. C or Fe or Ga
+2 −2
Original line number Diff line number Diff line
@@ -25,7 +25,8 @@ args = list of atom attributes, same as for "dump_style custom"_dump.html :l,ule

dump 1 all netcdf 100 traj.nc type x y z vx vy vz
dump_modify 1 append yes at -1 thermo yes
dump 1 all netcdf/mpiio 1000 traj.nc id type x y z :pre
dump 1 all netcdf/mpiio 1000 traj.nc id type x y z
dump 1 all netcdf 1000 traj.*.nc id type x y z :pre

[Description:]

@@ -73,4 +74,3 @@ section for more info.
[Related commands:]

"dump"_dump.html, "dump_modify"_dump_modify.html, "undump"_undump.html
+54 −77
Original line number Diff line number Diff line
@@ -88,8 +88,8 @@ DumpNetCDF::DumpNetCDF(LAMMPS *lmp, int narg, char **arg) :

  if (multiproc)
    error->all(FLERR,"Multi-processor writes are not supported.");
  if (multifile)
    error->all(FLERR,"Multiple files are not supported.");
  if (append_flag && multifile)
    error->all(FLERR,"Cannot append when writing to multiple files.");

  perat = new nc_perat_t[nfield];

@@ -224,6 +224,24 @@ DumpNetCDF::~DumpNetCDF()

void DumpNetCDF::openfile()
{
  char *filecurrent = filename;
  if (multifile && !singlefile_opened) {
    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 (thermo && !singlefile_opened) {
    if (thermovar)  delete [] thermovar;
    thermovar = new int[output->thermo->nfield];
@@ -268,14 +286,14 @@ void DumpNetCDF::openfile()
  ntotalgr = group->count(igroup);

  if (filewriter) {
    if (append_flag && access(filename, F_OK) != -1) {
    if (append_flag && !multifile && access(filecurrent, F_OK) != -1) {
      // Fixme! Perform checks if dimensions and variables conform with
      // data structure standard.

      if (singlefile_opened) return;
      singlefile_opened = 1;

      NCERRX( nc_open(filename, NC_WRITE, &ncid), filename );
      NCERRX( nc_open(filecurrent, NC_WRITE, &ncid), filecurrent );

      // dimensions
      NCERRX( nc_inq_dimid(ncid, NC_FRAME_STR, &frame_dim), NC_FRAME_STR );
@@ -348,8 +366,8 @@ void DumpNetCDF::openfile()
      if (singlefile_opened) return;
      singlefile_opened = 1;

      NCERRX( nc_create(filename, NC_64BIT_DATA, &ncid),
          filename );
      NCERRX( nc_create(filecurrent, NC_64BIT_DATA, &ncid),
              filecurrent );

      // dimensions
      NCERRX( nc_def_dim(ncid, NC_FRAME_STR, NC_UNLIMITED, &frame_dim),
@@ -598,15 +616,39 @@ void DumpNetCDF::closefile()
  if (filewriter && singlefile_opened) {
    NCERR( nc_close(ncid) );
    singlefile_opened = 0;
    // write to next frame upon next open
    if (multifile)
      framei = 1;
    else {
      // append next time DumpNetCDF::openfile is called
      append_flag = 1;
    // write to next frame upon next open
      framei++;
    }
  }
}

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

template <typename T>
int nc_put_var1_bigint(int ncid, int varid, const size_t index[], const T* tp)
{
  return nc_put_var1_int(ncid, varid, index, tp);
}

template <>
int nc_put_var1_bigint<long>(int ncid, int varid, const size_t index[],
                        const long* tp)
{
  return nc_put_var1_long(ncid, varid, index, tp);
}

template <>
int nc_put_var1_bigint<long long>(int ncid, int varid, const size_t index[],
                             const long long* tp)
{
  return nc_put_var1_longlong(ncid, varid, index, tp);
}

void DumpNetCDF::write()
{
  // open file
@@ -638,13 +680,8 @@ void DumpNetCDF::write()
                  th->keyword[i] );
        }
        else if (th->vtype[i] == BIGINT) {
#if defined(LAMMPS_SMALLBIG) || defined(LAMMPS_BIGBIG)
          NCERRX( nc_put_var1_long(ncid, thermovar[i], start, &th->bivalue),
                  th->keyword[i] );
#else
          NCERRX( nc_put_var1_int(ncid, thermovar[i], start, &th->bivalue),
          NCERRX( nc_put_var1_bigint(ncid, thermovar[i], start, &th->bivalue),
                  th->keyword[i] );
#endif
        }
      }
    }
@@ -887,6 +924,8 @@ int DumpNetCDF::modify_param(int narg, char **arg)
    return 2;
  }
  else if (strcmp(arg[iarg],"at") == 0) {
    if (!append_flag)
      error->all(FLERR,"expected 'append yes' before 'at' keyword");
    iarg++;
    framei = force->inumeric(FLERR,arg[iarg]);
    if (framei < 0)  framei--;
@@ -911,68 +950,6 @@ int DumpNetCDF::modify_param(int narg, char **arg)

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

void DumpNetCDF::write_prmtop()
{
  char fn[1024];
  char tmp[81];
  FILE *f;

  strcpy(fn, filename);
  strcat(fn, ".prmtop");

  f = fopen(fn, "w");
  fprintf(f, "%%VERSION  LAMMPS\n");
  fprintf(f, "%%FLAG TITLE\n");
  fprintf(f, "%%FORMAT(20a4)\n");
  memset(tmp, ' ', 76);
  tmp[76] = '\0';
  fprintf(f, "NASN%s\n", tmp);

  fprintf(f, "%%FLAG POINTERS\n");
  fprintf(f, "%%FORMAT(10I8)\n");
#if defined(LAMMPS_SMALLBIG) || defined(LAMMPS_BIGBIG)
  fprintf(f, "%8li", ntotalgr);
#else
  fprintf(f, "%8i", ntotalgr);
#endif
  for (int i = 0; i < 11; i++)
    fprintf(f, "%8i", 0);
  fprintf(f, "\n");
  for (int i = 0; i < 12; i++)
    fprintf(f, "%8i", 0);
  fprintf(f, "\n");
  for (int i = 0; i < 6; i++)
    fprintf(f, "%8i", 0);
  fprintf(f, "\n");

  fprintf(f, "%%FLAG ATOM_NAME\n");
  fprintf(f, "%%FORMAT(20a4)\n");
  for (int i = 0; i < ntotalgr; i++) {
    fprintf(f, "%4s", "He");
    if ((i+1) % 20 == 0)
      fprintf(f, "\n");
  }

  fprintf(f, "%%FLAG CHARGE\n");
  fprintf(f, "%%FORMAT(5E16.5)\n");
  for (int i = 0; i < ntotalgr; i++) {
    fprintf(f, "%16.5e", 0.0);
    if ((i+1) % 5 == 0)
      fprintf(f, "\n");
  }

  fprintf(f, "%%FLAG MASS\n");
  fprintf(f, "%%FORMAT(5E16.5)\n");
  for (int i = 0; i < ntotalgr; i++) {
    fprintf(f, "%16.5e", 1.0);
    if ((i+1) % 5 == 0)
        fprintf(f, "\n");
  }
  fclose(f);
}

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

void DumpNetCDF::ncerr(int err, const char *descr, int line)
{
  if (err != NC_NOERR) {
+0 −1
Original line number Diff line number Diff line
@@ -92,7 +92,6 @@ class DumpNetCDF : public DumpCustom {
  void closefile();
  virtual void write_header(bigint);
  virtual void write_data(int, double *);
  void write_prmtop();

  virtual int modify_param(int, char **);

+56 −16
Original line number Diff line number Diff line
@@ -88,8 +88,8 @@ DumpNetCDFMPIIO::DumpNetCDFMPIIO(LAMMPS *lmp, int narg, char **arg) :

  if (multiproc)
    error->all(FLERR,"Multi-processor writes are not supported.");
  if (multifile)
    error->all(FLERR,"Multiple files are not supported.");
  if (append_flag && multifile)
    error->all(FLERR,"Cannot append when writing to multiple files.");

  perat = new nc_perat_t[nfield];

@@ -217,6 +217,24 @@ DumpNetCDFMPIIO::~DumpNetCDFMPIIO()

void DumpNetCDFMPIIO::openfile()
{
  char *filecurrent = filename;
  if (multifile && !singlefile_opened) {
    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 (thermo && !singlefile_opened) {
    if (thermovar)  delete [] thermovar;
    thermovar = new int[output->thermo->nfield];
@@ -260,7 +278,7 @@ void DumpNetCDFMPIIO::openfile()
  // get total number of atoms
  ntotalgr = group->count(igroup);

  if (append_flag && access(filename, F_OK) != -1) {
  if (append_flag && !multifile && access(filecurrent, F_OK) != -1) {
    // Fixme! Perform checks if dimensions and variables conform with
    // data structure standard.

@@ -270,8 +288,8 @@ void DumpNetCDFMPIIO::openfile()
    if (singlefile_opened) return;
    singlefile_opened = 1;

    NCERRX( ncmpi_open(MPI_COMM_WORLD, filename, NC_WRITE, MPI_INFO_NULL,
                       &ncid), filename );
    NCERRX( ncmpi_open(MPI_COMM_WORLD, filecurrent, NC_WRITE, MPI_INFO_NULL,
                       &ncid), filecurrent );

    // dimensions
    NCERRX( ncmpi_inq_dimid(ncid, NC_FRAME_STR, &frame_dim), NC_FRAME_STR );
@@ -344,8 +362,8 @@ void DumpNetCDFMPIIO::openfile()
    if (singlefile_opened) return;
    singlefile_opened = 1;

    NCERRX( ncmpi_create(MPI_COMM_WORLD, filename, NC_64BIT_DATA,
                         MPI_INFO_NULL, &ncid), filename );
    NCERRX( ncmpi_create(MPI_COMM_WORLD, filecurrent, NC_64BIT_DATA,
                         MPI_INFO_NULL, &ncid), filecurrent );

    // dimensions
    NCERRX( ncmpi_def_dim(ncid, NC_FRAME_STR, NC_UNLIMITED, &frame_dim),
@@ -574,15 +592,40 @@ void DumpNetCDFMPIIO::closefile()
  if (singlefile_opened) {
    NCERR( ncmpi_close(ncid) );
    singlefile_opened = 0;
    // write to next frame upon next open
    if (multifile)
      framei = 1;
    else {
      // append next time DumpNetCDFMPIIO::openfile is called
      append_flag = 1;
    // write to next frame upon next open
      framei++;
    }
  }
}

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

template <typename T>
int ncmpi_put_var1_bigint(int ncid, int varid, const MPI_Offset index[],
                     const T* tp)
{
  return ncmpi_put_var1_int(ncid, varid, index, tp);
}

template <>
int ncmpi_put_var1_bigint<long>(int ncid, int varid, const MPI_Offset index[],
                           const long* tp)
{
  return ncmpi_put_var1_long(ncid, varid, index, tp);
}

template <>
int ncmpi_put_var1_bigint<long long>(int ncid, int varid, const MPI_Offset index[],
                                const long long* tp)
{
  return ncmpi_put_var1_longlong(ncid, varid, index, tp);
}

void DumpNetCDFMPIIO::write()
{
  // open file
@@ -616,13 +659,8 @@ void DumpNetCDFMPIIO::write()
                  th->keyword[i] );
        }
        else if (th->vtype[i] == BIGINT) {
#if defined(LAMMPS_SMALLBIG) || defined(LAMMPS_BIGBIG)
          NCERRX( ncmpi_put_var1_long(ncid, thermovar[i], start, &th->bivalue),
          NCERRX( ncmpi_put_var1_bigint(ncid, thermovar[i], start, &th->bivalue),
                  th->keyword[i] );
#else
          NCERRX( ncmpi_put_var1_int(ncid, thermovar[i], start, &th->bivalue),
                  th->keyword[i] );
#endif
        }
      }
    }
@@ -882,6 +920,8 @@ int DumpNetCDFMPIIO::modify_param(int narg, char **arg)
    return 2;
  }
  else if (strcmp(arg[iarg],"at") == 0) {
    if (!append_flag)
      error->all(FLERR,"expected 'append yes' before 'at' keyword");
    iarg++;
    framei = force->inumeric(FLERR,arg[iarg]);
    if (framei < 0)  framei--;