Simpatico  v1.10
mcMd/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 <mcMd/simulation/System.h>
10 #include <mcMd/simulation/Simulation.h>
11 #include <simp/species/Species.h>
12 #include <mcMd/chemistry/Molecule.h>
13 #include <mcMd/chemistry/Atom.h>
14 #include <util/space/Vector.h>
15 #include <util/space/IntVector.h>
16 #include <util/misc/ioUtil.h>
17 
18 #include <sstream>
19 
20 namespace McMd
21 {
22 
23  using namespace Util;
24 
25  /*
26  * Constructor.
27  */
29  : TrajectoryReader(system)
30  {}
31 
32  /*
33  * Destructor.
34  */
36  {}
37 
38  /*
39  * Open file and setup memory.
40  */
41  void LammpsDumpReader::open(std::string filename)
42  {
43  // Open trajectory file
44  simulation().fileMaster().openInputFile(filename, file_);
45 
46  // Set nAtomTotal_ and add all molecules to system
47  addMolecules();
48 
49  // Allocate private array of atomic positions_
50  if (!positions_.isAllocated()) {
51  positions_.allocate(nAtomTotal_);
52  } else {
53  if (nAtomTotal_ != positions_.capacity()) {
54  UTIL_THROW("Inconsistent values of atom capacity");
55  }
56  }
57  }
58 
59  /*
60  * Read frame, return false if end-of-file
61  */
63  {
64  // Preconditions
65  if (!positions_.isAllocated()) {
66  UTIL_THROW("positions_ array is not allocated");
67  }
68 
69  bool notEnd;
70  std::stringstream line;
71 
72  // Attempt to read first line
73  notEnd = getNextLine(file_, line);
74  if (!notEnd) {
75  return false;
76  }
77 
78  // Process ITEM: TIMESTEP
79  checkString(line, "ITEM:");
80  checkString(line, "TIMESTEP");
81  int step;
82  file_ >> step;
83 
84  // Read ITEM: NUMBER OF ATOMS
85  notEnd = getNextLine(file_, line);
86  if (!notEnd) {
87  UTIL_THROW("EOF reading ITEM: NUMBER OF ATOMS");
88  }
89  checkString(line, "ITEM:");
90  checkString(line, "NUMBER");
91  checkString(line, "OF");
92  checkString(line, "ATOMS");
93  int nAtom;
94  file_ >> nAtom;
95  if (nAtom != nAtomTotal_) {
96  UTIL_THROW("Inconsistent values: nAtom != nAtomTotal_");
97  }
98 
99  // Read ITEM: BOX
100  notEnd = getNextLine(file_, line);
101  if (!notEnd) {
102  UTIL_THROW("EOF reading ITEM: BOX");
103  }
104  checkString(line, "ITEM:");
105  checkString(line, "BOX");
106  // Ignore rest of ITEM: BOX line
107  Vector min, max, lengths;
108  for (int i = 0; i < Dimension; ++i) {
109  file_ >> min[i] >> max[i];
110  lengths[i] = max[i] - min[i];
111  }
112  boundary().setOrthorhombic(lengths);
113 
114  // Read ITEM: ATOMS
115  notEnd = getNextLine(file_, line);
116  checkString(line, "ITEM:");
117  checkString(line, "ATOMS");
118  // Ignore the rest of ITEM: ATOMS line, for now
119 
120  // Load positions into positions_ vector, indexed by id.
121  IntVector shift;
122  int id, typeId, molId, i, j;
123  for (i = 0; i < nAtom; ++i) {
124 
125  file_ >> id;
126  id = id - 1; // Convert convention, Lammps -> Simpatico
127  file_ >> typeId;
128  file_ >> molId;
129 
130  // Read position
131  for (j = 0; j < Util::Dimension; ++j) {
132  file_ >> positions_[id][j];
133  }
134  file_ >> shift;
135 
136  // shift into simulation cell
137  boundary().shift(positions_[id]);
138 
139  }
140 
141  // Assign atom positions, assuming ordered atom ids
142  int iSpecies, iMol;
143  Species *speciesPtr;
144  Molecule::AtomIterator atomIter;
145  Molecule *molPtr;
146  id = 0;
147  for (iSpecies = 0; iSpecies < simulation().nSpecies(); ++iSpecies) {
148  speciesPtr = &simulation().species(iSpecies);
149  for (iMol = 0; iMol < speciesPtr->capacity(); ++iMol) {
150  molPtr = &system().molecule(iSpecies, iMol);
151  for (molPtr->begin(atomIter); atomIter.notEnd(); ++atomIter) {
152  atomIter->position() = positions_[id];
153  id++;
154  }
155  }
156  }
157 
158  return true;
159  }
160 
161  /*
162  * Close trajectory file.
163  */
165  { file_.close();}
166 
167 }
System & system() const
Get a reference to the parent System.
const int Dimension
Dimensionality of space.
Definition: Dimension.h:19
A Vector is a Cartesian vector.
Definition: Vector.h:75
bool readFrame()
Read a single frame.
bool notEnd() const
Is the current pointer not at the end of the array?
Definition: ArrayIterator.h:83
void setOrthorhombic(const Vector &lengths)
Set unit cell dimensions for orthorhombic boundary.
virtual void addMolecules()
Add all molecules to system.
void begin(AtomIterator &iterator)
Set an Molecule::AtomIterator to first Atom in this Molecule.
A set of interacting Molecules enclosed by a Boundary.
Definition: System.h:115
void openInputFile(const std::string &filename, std::ifstream &in, std::ios_base::openmode mode=std::ios_base::in) const
Open an input file.
Definition: FileMaster.cpp:273
void open(std::string filename)
Open file.
Simulation & simulation() const
Get a reference to the parent Simulation.
bool getNextLine(std::istream &in, std::string &line)
Read the next non-empty line into a string, strip trailing whitespace.
Definition: ioUtil.cpp:79
Molecule & molecule(int speciesId, int moleculeId)
Get a specific Molecule in this System, by integer index.
Definition: System.h:1137
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
Utility classes for scientific computation.
Definition: accumulators.mod:1
void shift(Vector &r) const
Shift Cartesian Vector r to its primary image.
Trajectory file reader (base class).
void checkString(std::istream &in, const std::string &expected)
Extract string from stream, and compare to expected value.
Definition: ioUtil.cpp:37
Forward iterator for an Array or a C array.
Definition: ArrayIterator.h:39
int nAtomTotal_
Total number of atoms (all species)
Boundary & boundary() const
Get the Boundary.
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
int nSpecies() const
Get the number of Species in this Simulation.
LammpsDumpReader(System &system)
Constructor.
An IntVector is an integer Cartesian vector.
Definition: IntVector.h:73
int capacity() const
Maximum allowed number of molecules for this Species.
A physical molecule (a set of covalently bonded Atoms).
A Species represents a set of chemically similar molecules.
Species & species(int i)
Get a specific Species by reference.
FileMaster & fileMaster()
Get the FileMaster object.