diff --git a/src/COMPRESS/dump_local_gz.cpp b/src/COMPRESS/dump_local_gz.cpp new file mode 100644 index 0000000000..801547543a --- /dev/null +++ b/src/COMPRESS/dump_local_gz.cpp @@ -0,0 +1,171 @@ +/* ---------------------------------------------------------------------- + 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 "dump_local_gz.h" +#include "domain.h" +#include "error.h" +#include "update.h" + +#include + +using namespace LAMMPS_NS; + +DumpLocalGZ::DumpLocalGZ(LAMMPS *lmp, int narg, char **arg) : + DumpLocal(lmp, narg, arg) +{ + gzFp = NULL; + + if (!compressed) + error->all(FLERR,"Dump local/gz only writes compressed files"); +} + + +/* ---------------------------------------------------------------------- */ + +DumpLocalGZ::~DumpLocalGZ() +{ + if (gzFp) gzclose(gzFp); + gzFp = NULL; + fp = NULL; +} + + +/* ---------------------------------------------------------------------- + generic opening of a dump file + ASCII or binary or gzipped + some derived classes override this function +------------------------------------------------------------------------- */ + +void DumpLocalGZ::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) { + gzFp = gzopen(filecurrent,"ab9"); + } else { + gzFp = gzopen(filecurrent,"wb9"); + } + + if (gzFp == NULL) error->one(FLERR,"Cannot open dump file"); + } else gzFp = NULL; + + // delete string with timestep replaced + + if (multifile) delete [] filecurrent; +} + +void DumpLocalGZ::write_header(bigint ndump) +{ + if ((multiproc) || (!multiproc && me == 0)) { + if (unit_flag && !unit_count) { + ++unit_count; + gzprintf(gzFp,"ITEM: UNITS\n%s\n",update->unit_style); + } + gzprintf(gzFp,"ITEM: TIMESTEP\n"); + gzprintf(gzFp,BIGINT_FORMAT "\n",update->ntimestep); + gzprintf(gzFp,"ITEM: NUMBER OF ATOMS\n"); + 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); + } 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,"ITEM: %s %s\n",label,columns); + } +} + +/* ---------------------------------------------------------------------- */ + +void DumpLocalGZ::write_data(int n, double *mybuf) +{ + if (buffer_flag == 1) { + gzwrite(gzFp,mybuf,sizeof(char)*n); + + } else { + int i,j; + int m = 0; + for (i = 0; i < n; i++) { + for (j = 0; j < size_one; j++) { + if (vtype[j] == INT) + gzprintf(gzFp,vformat[j],static_cast (mybuf[m])); + else gzprintf(gzFp,vformat[j],mybuf[m]); + m++; + } + gzprintf(gzFp,"\n"); + } + } +} + +/* ---------------------------------------------------------------------- */ + +void DumpLocalGZ::write() +{ + DumpLocal::write(); + if (filewriter) { + if (multifile) { + gzclose(gzFp); + gzFp = NULL; + } else { + if (flush_flag) + gzflush(gzFp,Z_SYNC_FLUSH); + } + } +} + diff --git a/src/COMPRESS/dump_local_gz.h b/src/COMPRESS/dump_local_gz.h new file mode 100644 index 0000000000..cc788863de --- /dev/null +++ b/src/COMPRESS/dump_local_gz.h @@ -0,0 +1,57 @@ +/* -*- 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. +------------------------------------------------------------------------- */ + +#ifdef DUMP_CLASS + +DumpStyle(local/gz,DumpLocalGZ) + +#else + +#ifndef LMP_DUMP_LOCAL_GZ_H +#define LMP_DUMP_LOCAL_GZ_H + +#include "dump_local.h" +#include + +namespace LAMMPS_NS { + +class DumpLocalGZ : public DumpLocal { + public: + DumpLocalGZ(class LAMMPS *, int, char **); + virtual ~DumpLocalGZ(); + + protected: + 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(); +}; + +} + +#endif +#endif + +/* ERROR/WARNING messages: + +E: Dump local/gz only writes compressed files + +The dump local/gz output file name must have a .gz suffix. + +E: Cannot open dump file + +Self-explanatory. + +*/ diff --git a/src/dump_local.h b/src/dump_local.h index 91381ba1ec..9b15082995 100644 --- a/src/dump_local.h +++ b/src/dump_local.h @@ -29,7 +29,7 @@ class DumpLocal : public Dump { DumpLocal(LAMMPS *, int, char **); virtual ~DumpLocal(); - private: + protected: int nevery; // dump frequency to check Fix against char *label; // string for dump file header @@ -55,11 +55,11 @@ class DumpLocal : public Dump { void init_style(); int modify_param(int, char **); - void write_header(bigint); + virtual void write_header(bigint); int count(); void pack(tagint *); int convert_string(int, double *); - void write_data(int, double *); + virtual void write_data(int, double *); void parse_fields(int, char **); int add_compute(char *);