diff --git a/src/Make.sh b/src/Make.sh index ca280a02c4..d6d51ebdc9 100644 --- a/src/Make.sh +++ b/src/Make.sh @@ -60,6 +60,7 @@ if (test $1 = "style") then style KSPACE_CLASS "" kspace force style MINIMIZE_CLASS min_ minimize update style PAIR_CLASS pair_ pair force pair_hybrid + style READER_CLASS reader_ reader read_dump style REGION_CLASS region_ region domain # edit Makefile.lib diff --git a/src/read_dump.cpp b/src/read_dump.cpp index a24d64cd14..2327f0ba13 100644 --- a/src/read_dump.cpp +++ b/src/read_dump.cpp @@ -20,7 +20,8 @@ #include "string.h" #include "stdlib.h" #include "read_dump.h" -#include "read_dump_native.h" +#include "reader.h" +#include "style_reader.h" #include "atom.h" #include "atom_vec.h" #include "update.h" @@ -39,7 +40,6 @@ using namespace LAMMPS_NS; enum{ID,TYPE,X,Y,Z,VX,VY,VZ,IX,IY,IZ}; enum{UNSET,UNSCALED,SCALED}; -enum{NATIVE}; /* ---------------------------------------------------------------------- */ @@ -59,6 +59,10 @@ ReadDump::ReadDump(LAMMPS *lmp) : Pointers(lmp) fieldlabel = NULL; fields = NULL; + int n = strlen("native") + 1; + readerstyle = new char[n]; + strcpy(readerstyle,"native"); + reader = NULL; fp = NULL; } @@ -72,6 +76,7 @@ ReadDump::~ReadDump() for (int i = 0; i < nfield; i++) delete [] fieldlabel[i]; delete [] fieldlabel; delete [] fieldtype; + delete [] readerstyle; memory->destroy(fields); delete reader; @@ -169,14 +174,24 @@ void ReadDump::store_files(int nstr, char **str) void ReadDump::setup_reader() { - // create reader class - // could make this a parent class and customize with other readers - - if (format == NATIVE) reader = new ReadDumpNative(lmp); - // allocate snapshot field buffer memory->create(fields,CHUNK,nfield,"read_dump:fields"); + + // create reader class + // match readerstyle to options in style_reader.h + + if (0) return; // dummy line to enable else-if macro expansion + +#define READER_CLASS +#define ReaderStyle(key,Class) \ + else if (strcmp(readerstyle,#key) == 0) reader = new Class(lmp); +#include "style_reader.h" +#undef READER_CLASS + + // unrecognized style + + else error->all(FLERR,"Invalid dump reader style"); } /* ---------------------------------------------------------------------- @@ -558,7 +573,6 @@ void ReadDump::fields_and_keywords(int narg, char **arg) addflag = 0; for (int i = 0; i < nfield; i++) fieldlabel[i] = NULL; scaledflag = UNSCALED; - format = NATIVE; while (iarg < narg) { if (strcmp(arg[iarg],"box") == 0) { @@ -609,8 +623,10 @@ void ReadDump::fields_and_keywords(int narg, char **arg) iarg += 2; } else if (strcmp(arg[iarg],"format") == 0) { if (iarg+2 > narg) error->all(FLERR,"Illegal read_dump command"); - if (strcmp(arg[iarg+1],"native") == 0) format = NATIVE; - else error->all(FLERR,"Illegal read_dump command"); + delete [] readerstyle; + int n = strlen(arg[iarg+1]) + 1; + readerstyle = new char[n]; + strcpy(readerstyle,arg[iarg+1]); iarg += 2; } else error->all(FLERR,"Illegal read_dump command"); } diff --git a/src/read_dump.h b/src/read_dump.h index 05ea5a0dc6..5cdfc0f932 100644 --- a/src/read_dump.h +++ b/src/read_dump.h @@ -57,8 +57,8 @@ private: int trimflag,purgeflag; int scaledflag; // user setting for coordinate scaling int scaled; // actual setting for coordinate scaling - int format; // style of dump file int compressed; // flag for dump file compression + char *readerstyle; // style of dump files to read int nfield; // # of fields to extract from dump file int *fieldtype; // type of each field = X,VY,IZ,etc @@ -78,7 +78,7 @@ private: int *uflag; // set to 1 if snapshot atom matches owned atom int *ucflag,*ucflag_all; // set to 1 if snapshot chunk atom was processed - class ReadDumpNative *reader; // class that reads native dump file + class Reader *reader; // class that reads dump file void process_atoms(int); void delete_atoms(); diff --git a/src/reader.cpp b/src/reader.cpp new file mode 100644 index 0000000000..b8947716a5 --- /dev/null +++ b/src/reader.cpp @@ -0,0 +1,31 @@ +/* ---------------------------------------------------------------------- + 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 "stdio.h" +#include "reader.h" + +using namespace LAMMPS_NS; + +/* ---------------------------------------------------------------------- */ + +Reader::Reader(LAMMPS *lmp) : Pointers(lmp) {} + +/* ---------------------------------------------------------------------- + set file ptr + caller opens/closes dump files +------------------------------------------------------------------------- */ + +void Reader::file(FILE *fpcaller) +{ + fp = fpcaller; +} diff --git a/src/reader.h b/src/reader.h new file mode 100644 index 0000000000..28c6c35709 --- /dev/null +++ b/src/reader.h @@ -0,0 +1,43 @@ +/* ---------------------------------------------------------------------- + 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. + + Contributed by Timothy Sirk +------------------------------------------------------------------------- */ + +#ifndef LMP_READER_H +#define LMP_READER_H + +#include "stdio.h" +#include "pointers.h" + +namespace LAMMPS_NS { + +class Reader : protected Pointers { + public: + Reader(class LAMMPS *); + virtual ~Reader() {} + + virtual int read_time(bigint &) = 0; + virtual void skip() = 0; + virtual bigint read_header(double [3][3], int &, int, int, int *, char **, + int, int &, int &, int &, int &) = 0; + virtual void read_atoms(int, int, double **) = 0; + + void file(FILE *); + + protected: + FILE *fp; // pointer to file opened by caller +}; + +} + +#endif diff --git a/src/read_dump_native.cpp b/src/reader_native.cpp similarity index 93% rename from src/read_dump_native.cpp rename to src/reader_native.cpp index 695a680d3e..7c17588fea 100644 --- a/src/read_dump_native.cpp +++ b/src/reader_native.cpp @@ -13,7 +13,7 @@ #include "string.h" #include "stdlib.h" -#include "read_dump_native.h" +#include "reader_native.h" #include "atom.h" #include "memory.h" #include "error.h" @@ -27,7 +27,7 @@ enum{UNSET,UNSCALED,SCALED}; /* ---------------------------------------------------------------------- */ -ReadDumpNative::ReadDumpNative(LAMMPS *lmp) : Pointers(lmp) +ReaderNative::ReaderNative(LAMMPS *lmp) : Reader(lmp) { line = new char[MAXLINE]; words = NULL; @@ -36,30 +36,20 @@ ReadDumpNative::ReadDumpNative(LAMMPS *lmp) : Pointers(lmp) /* ---------------------------------------------------------------------- */ -ReadDumpNative::~ReadDumpNative() +ReaderNative::~ReaderNative() { delete [] line; delete [] words; memory->destroy(fieldindex); } -/* ---------------------------------------------------------------------- - set file ptr - caller opens/closes dump files -------------------------------------------------------------------------- */ - -void ReadDumpNative::file(FILE *fpcaller) -{ - fp = fpcaller; -} - /* ---------------------------------------------------------------------- read and return time stamp from dump file if first read reaches end-of-file, return 1 so caller can open next file only called by proc 0 ------------------------------------------------------------------------- */ -int ReadDumpNative::read_time(bigint &ntimestep) +int ReaderNative::read_time(bigint &ntimestep) { char *eof = fgets(line,MAXLINE,fp); if (eof == NULL) return 1; @@ -77,7 +67,7 @@ int ReadDumpNative::read_time(bigint &ntimestep) only called by proc 0 ------------------------------------------------------------------------- */ -void ReadDumpNative::skip() +void ReaderNative::skip() { read_lines(2); bigint natoms; @@ -110,7 +100,7 @@ void ReadDumpNative::skip() only called by proc 0 ------------------------------------------------------------------------- */ -bigint ReadDumpNative::read_header(double box[3][3], int &triclinic, +bigint ReaderNative::read_header(double box[3][3], int &triclinic, int fieldinfo, int nfield, int *fieldtype, char **fieldlabel, int scaledflag, int &fieldflag, @@ -284,7 +274,7 @@ bigint ReadDumpNative::read_header(double box[3][3], int &triclinic, only called by proc 0 ------------------------------------------------------------------------- */ -void ReadDumpNative::read_atoms(int n, int nfield, double **fields) +void ReaderNative::read_atoms(int n, int nfield, double **fields) { int i,m; char *eof; @@ -311,7 +301,7 @@ void ReadDumpNative::read_atoms(int n, int nfield, double **fields) return index of match or -1 if no match ------------------------------------------------------------------------- */ -int ReadDumpNative::find_label(const char *label, int n, char **labels) +int ReaderNative::find_label(const char *label, int n, char **labels) { for (int i = 0; i < n; i++) if (strcmp(label,labels[i]) == 0) return i; @@ -325,7 +315,7 @@ int ReadDumpNative::find_label(const char *label, int n, char **labels) only called by proc 0 ------------------------------------------------------------------------- */ -void ReadDumpNative::read_lines(int n) +void ReaderNative::read_lines(int n) { char *eof; for (int i = 0; i < n; i++) eof = fgets(line,MAXLINE,fp); diff --git a/src/read_dump_native.h b/src/reader_native.h similarity index 80% rename from src/read_dump_native.h rename to src/reader_native.h index e7295c2535..68e09428bb 100644 --- a/src/read_dump_native.h +++ b/src/reader_native.h @@ -13,20 +13,24 @@ Contributed by Timothy Sirk ------------------------------------------------------------------------- */ -#ifndef LMP_READ_DUMP_NATIVE_H -#define LMP_READ_DUMP_NATIVE_H +#ifdef READER_CLASS -#include "stdio.h" -#include "pointers.h" +ReaderStyle(native,ReaderNative) + +#else + +#ifndef LMP_READER_NATIVE_H +#define LMP_READER_NATIVE_H + +#include "reader.h" namespace LAMMPS_NS { -class ReadDumpNative : protected Pointers { +class ReaderNative : public Reader { public: - ReadDumpNative(class LAMMPS *); - ~ReadDumpNative(); + ReaderNative(class LAMMPS *); + ~ReaderNative(); - void file(FILE *); int read_time(bigint &); void skip(); bigint read_header(double [3][3], int &, int, int, int *, char **, @@ -34,7 +38,6 @@ class ReadDumpNative : protected Pointers { void read_atoms(int, int, double **); private: - FILE *fp; // pointer to file opened by caller char *line; // line read from dump file int nwords; // # of per-atom columns in dump file @@ -48,3 +51,4 @@ private: } #endif +#endif