Page MenuHomec4science

read_dump_xyz.cpp
No OneTemporary

File Metadata

Created
Sun, Sep 15, 03:17

read_dump_xyz.cpp

/* ----------------------------------------------------------------------
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 "string.h"
#include "stdlib.h"
#include "read_dump_xyz.h"
#include "atom.h"
#include "memory.h"
#include "error.h"
using namespace LAMMPS_NS;
#define MAXLINE 1024 // max line length in dump file
enum{ID,TYPE,X,Y,Z};
enum{UNSET,UNSCALED};
/* ---------------------------------------------------------------------- */
ReadDumpXYZ::ReadDumpXYZ(LAMMPS *lmp) : Pointers(lmp)
{
line = new char[MAXLINE];
fieldindex = NULL;
nstep = 0;
}
/* ---------------------------------------------------------------------- */
ReadDumpXYZ::~ReadDumpXYZ()
{
delete [] line;
memory->destroy(fieldindex);
}
/* ----------------------------------------------------------------------
set file ptr
caller opens/closes dump files
------------------------------------------------------------------------- */
void ReadDumpXYZ::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 ReadDumpXYZ::read_time(bigint &ntimestep)
{
char *eof = fgets(line,MAXLINE,fp);
if (eof == NULL) return 1;
// first line has to have the number of atoms
natoms = ATOBIGINT(line);
if (natoms < 1)
error->one(FLERR,"Dump file is incorrectly formatted");
// count step and ...
ntimestep = ++nstep;
// ... skip over comment/title line
read_lines(1);
return 0;
}
/* ----------------------------------------------------------------------
skip snapshot from timestamp onward
only called by proc 0
------------------------------------------------------------------------- */
void ReadDumpXYZ::skip()
{
// invoke read_lines() in chunks no larger than MAXSMALLINT
int nchunk;
bigint nremain = natoms;
while (nremain) {
nchunk = MIN(nremain,MAXSMALLINT);
read_lines(nchunk);
nremain -= nchunk;
}
}
/* ----------------------------------------------------------------------
read remaining header info:
return natoms
box bounds, triclinic (inferred), fieldflag (1 if any fields not found),
xyz flag = UNSET (not a requested field), SCALED, UNSCALED
if fieldflag set:
match Nfield fields to per-atom column labels
allocate and set fieldindex = which column each field maps to
fieldtype = X,VX,IZ etc
fieldlabel = user-specified label or NULL if use fieldtype default
xyz flag = scaledflag if has fieldlabel name, else set by x,xs,xu,xsu
only called by proc 0
------------------------------------------------------------------------- */
bigint ReadDumpXYZ::read_header(double box[3][3], int &triclinic,
int fieldinfo, int nfield,
int *fieldtype, char **fieldlabel,
int scaledflag, int &fieldflag,
int &xflag, int &yflag, int &zflag)
{
nid = 0;
// if no field info requested, just return
if (!fieldinfo) return natoms;
memory->create(fieldindex,nfield,"read_dump:fieldindex");
// for .xyz we know nothing about the style of coordinates,
// so the caller has to set the proper flag.
xflag = scaledflag;
yflag = scaledflag;
zflag = scaledflag;
// copy fieldtype list for supported fields
fieldflag = 0;
for (int i = 0; i < nfield; i++) {
if ( (fieldtype[i] == X) ||
(fieldtype[i] == Y) ||
(fieldtype[i] == Z) ||
(fieldtype[i] == ID) ||
(fieldtype[i] == TYPE) ) {
fieldindex[i] = fieldtype[i];
} else {
fieldflag = 1;
}
}
return natoms;
}
/* ----------------------------------------------------------------------
read N atom lines from dump file
stores appropriate values in fields array
return 0 if success, 1 if error
only called by proc 0
------------------------------------------------------------------------- */
void ReadDumpXYZ::read_atoms(int n, int nfield, double **fields)
{
int i,m;
char *eof;
int mytype;
double myx, myy, myz;
for (int i = 0; i < n; i++) {
eof = fgets(line,MAXLINE,fp);
if (eof == NULL) error->one(FLERR,"Unexpected end of dump file");
++nid;
sscanf(line,"%*s%lg%lg%lg", &myx, &myy, &myz);
// FIXME: we need to find a way to signal parser errors here. XXX
mytype = atoi(line);
if (mytype < 1) mytype = 1;
for (m = 0; m < nfield; m++) {
switch (fieldindex[m]) {
case X:
fields[i][m] = myx;
break;
case Y:
fields[i][m] = myy;
break;
case Z:
fields[i][m] = myz;
break;
case ID:
fields[i][m] = nid;
break;
case TYPE:
fields[i][m] = mytype;
break;
}
}
}
}
/* ----------------------------------------------------------------------
read N lines from dump file
only last one is saved in line
return NULL if end-of-file error, else non-NULL
only called by proc 0
------------------------------------------------------------------------- */
void ReadDumpXYZ::read_lines(int n)
{
char *eof;
for (int i = 0; i < n; i++) eof = fgets(line,MAXLINE,fp);
if (eof == NULL) error->all(FLERR,"Unexpected end of dump file");
}

Event Timeline