Simpatico  v1.10
tools/trajectory/LammpsDumpReader.cpp
1 /*
2 * Simpatico - Simulation Package for Polymeric and Molecular Liquids
3 *
4 * Copyright 2010 - 2017, The Regents of the University of Minnesota
5 * Distributed under the terms of the GNU General Public License.
6 */
7 
8 #include "LammpsDumpReader.h"
9 #include <tools/storage/Configuration.h>
10 #include <util/space/Vector.h>
11 #include <util/misc/ioUtil.h>
12 
13 namespace Tools
14 {
15 
16  using namespace Util;
17 
18  /*
19  * Constructor.
20  */
22  : TrajectoryReader(configuration, false)
23  { setClassName("LammpsDumpReader"); }
24 
25  /*
26  * Destructor.
27  */
29  {}
30 
31  /*
32  * Read a frame.
33  */
34  bool LammpsDumpReader::readFrame(std::ifstream& file)
35  {
36  bool notEnd;
37  std::stringstream line;
38 
39  // Read ITEM: TIMESTEP
40  notEnd = getNextLine(file, line);
41  if (!notEnd) {
42  return false;
43  }
44  checkString(line, "ITEM:");
45  checkString(line, "TIMESTEP");
46  int step;
47  file >> step;
48 
49  // Read ITEM: NUMBER OF ATOMS
50  notEnd = getNextLine(file, line);
51  if (!notEnd) {
52  UTIL_THROW("EOF reading ITEM: NUMBER OF ATOMS");
53  }
54  checkString(line, "ITEM:");
55  checkString(line, "NUMBER");
56  checkString(line, "OF");
57  checkString(line, "ATOMS");
58  int nAtom;
59  file >> nAtom;
60 
61  // Read ITEM: BOX
62  notEnd = getNextLine(file, line);
63  if (!notEnd) {
64  UTIL_THROW("EOF reading ITEM: BOX");
65  }
66  checkString(line, "ITEM:");
67  checkString(line, "BOX");
68  // Ignore rest of ITEM: BOX line
69  Vector min, max, lengths;
70  for (int i = 0; i < Dimension; ++i) {
71  file >> min[i] >> max[i];
72  lengths[i] = max[i] - min[i];
73  }
74 
75  // Read ITEM: ATOMS
76  notEnd = getNextLine(file, line);
77  checkString(line, "ITEM:");
78  checkString(line, "ATOMS");
79  // Ignore the rest of ITEM: ATOMS line, for now
80 
81  // Loop over atoms, read positions
82  AtomStorage* storagePtr = &configuration().atoms();
83  Atom* atomPtr;
84  Vector r;
85  IntVector shift;
86  int i, j, id, typeId, molId;
87  for (i = 0; i < nAtom; ++i) {
88 
89  // Find atom address in AtomStorage by id
90  file >> id;
91  id = id - 1; // Convert from Lammps -> Simpatico integer convention
92  atomPtr = storagePtr->ptr(id);
93  if (atomPtr == 0) {
94  UTIL_THROW("Unknown atom");
95  }
96  file >> typeId;
97  file >> molId;
98 
99  // Read position
100  for (j = 0; j < Util::Dimension; ++j) {
101  file >> atomPtr->position[j];
102  }
103  file >> shift;
104 
105  }
106  return true;
107  }
108 
109 }
const int Dimension
Dimensionality of space.
Definition: Dimension.h:19
A Vector is a Cartesian vector.
Definition: Vector.h:75
LammpsDumpReader()
Default constructor.
Container for a set of atoms.
Abstract trajectory file reader.
Atom * ptr(int id)
Get a pointer to an atom by global id.
AtomStorage & atoms()
Get the AtomStorage.
An instantaneous molecular dynamics configuration.
Definition: Configuration.h:40
bool getNextLine(std::istream &in, std::string &line)
Read the next non-empty line into a string, strip trailing whitespace.
Definition: ioUtil.cpp:79
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
virtual bool readFrame(std::ifstream &file)
Read a frame.
Utility classes for scientific computation.
Definition: accumulators.mod:1
void checkString(std::istream &in, const std::string &expected)
Extract string from stream, and compare to expected value.
Definition: ioUtil.cpp:37
Single-processor classes for pre- and post-processing MD trajectories.
A point particle in an MD simulation.
void setClassName(const char *className)
Set class name string.
Vector position
Atom position.
An IntVector is an integer Cartesian vector.
Definition: IntVector.h:73
Configuration & configuration()
Return parent Configuraiton by reference.