Simpatico  v1.10
mcMd/trajectory/DdMdTrajectoryReader.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 "DdMdTrajectoryReader.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/archives/BinaryFileIArchive.h>
15 #include <util/space/Vector.h>
16 #include <util/space/IntVector.h>
17 #include <util/misc/ioUtil.h>
18 
19 #include <climits>
20 
21 namespace McMd
22 {
23 
24  using namespace Util;
25 
26  /*
27  * Constructor.
28  */
30  : TrajectoryReader(system)
31  {}
32 
33  /*
34  * Destructor.
35  */
37  {}
38 
39  /*
40  * Open trajectory file and setup to read.
41  */
42  void DdMdTrajectoryReader::open(std::string filename)
43  {
44  // Open trajectory file
45  simulation().fileMaster().openInputFile(filename, file_);
46 
47  BinaryFileIArchive ar(file_);
48  int nAtom;
49  ar >> nAtom;
50  //std::cout << nAtom << std::endl;
51 
52  // Add all molecules to system and check consistency of nAtom.
53  addMolecules();
54  if (nAtom != nAtomTotal_) {
55  UTIL_THROW("Inconsistent values: nAtom != nAtomTotal_");
56  }
57 
58  // Allocate private array of atomic positions_
59  if (!positions_.isAllocated()) {
60  positions_.allocate(nAtomTotal_);
61  } else {
62  if (nAtomTotal_ != positions_.capacity()) {
63  UTIL_THROW("Inconsistent values of atom capacity");
64  }
65  }
66  }
67 
68  /*
69  * Read frame, return false if end-of-file
70  */
72  {
73  // Preconditions
74  if (!positions_.isAllocated()) {
75  UTIL_THROW("positions_ array is not allocated");
76  }
77 
78  BinaryFileIArchive ar(file_);
79 
80  // Attempt to read iStep
81  long iStep = -1;
82  ar >> iStep;
83 
84  // Return false if read failed, indicating end of file.
85  if (file_.eof()) {
86  return false;
87  }
88 
89  // Read boundary dimensions
90  ar >> boundary();
91 
92  // Loop over atoms, read atomic positions
93  Vector r;
94  double h = 1.0/(double(UINT_MAX) + 1.0);
95  int id, i, j;
96  unsigned int ir;
97  for (i = 0; i < nAtomTotal_; ++i) {
98  ar >> id;
99  for (j = 0; j < Dimension; ++j) {
100  ar >> ir;
101  r[j] = ir*h;
102  }
103  boundary().transformGenToCart(r, positions_[id]);
104  }
105 
106  // Assign atom positions, assuming ordered atom ids
107  int iSpecies, iMol;
108  Species *speciesPtr;
109  Molecule::AtomIterator atomIter;
110  Molecule *molPtr;
111  id = 0;
112  for (iSpecies = 0; iSpecies < simulation().nSpecies(); ++iSpecies) {
113  speciesPtr = &simulation().species(iSpecies);
114  for (iMol = 0; iMol < speciesPtr->capacity(); ++iMol) {
115  molPtr = &system().molecule(iSpecies, iMol);
116  for (molPtr->begin(atomIter); atomIter.notEnd(); ++atomIter) {
117  atomIter->position() = positions_[id];
118  id++;
119  }
120  }
121  }
122 
123  return true;
124  }
125 
126  /*
127  * Close trajectory file.
128  */
130  { file_.close(); }
131 
132 }
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
void open(std::string filename)
Open trajectory file, read header, and allocate memory.
bool notEnd() const
Is the current pointer not at the end of the array?
Definition: ArrayIterator.h:83
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
Simulation & simulation() const
Get a reference to the parent Simulation.
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
Trajectory file reader (base class).
Forward iterator for an Array or a C array.
Definition: ArrayIterator.h:39
int nAtomTotal_
Total number of atoms (all species)
void transformGenToCart(const Vector &Rg, Vector &Rc) const
Transform Vector of generalized coordinates to Cartesian Vector.
Boundary & boundary() const
Get the Boundary.
Saving archive for binary istream.
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
int nSpecies() const
Get the number of Species in this Simulation.
int capacity() const
Maximum allowed number of molecules for this Species.
A physical molecule (a set of covalently bonded Atoms).
DdMdTrajectoryReader(System &system)
Constructor.
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.