Simpatico  v1.10
mcMd/configIos/DdMdConfigIo.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 "DdMdConfigIo.h"
9 #include <mcMd/simulation/System.h>
10 #include <mcMd/simulation/Simulation.h>
11 #include <mcMd/chemistry/Molecule.h>
12 #include <mcMd/chemistry/Atom.h>
13 #include <simp/species/Species.h>
14 #include <util/space/Vector.h>
15 #include <util/space/IntVector.h>
16 #include <util/param/Label.h>
17 #include <util/format/Format.h>
18 #include <util/format/Int.h>
19 #include <util/format/Dbl.h>
20 #include <util/misc/ioUtil.h>
21 
22 #include <vector>
23 
24 namespace McMd
25 {
26 
27  using namespace Util;
28  using namespace Simp;
29 
30  /*
31  * Constructor.
32  */
33  DdMdConfigIo::DdMdConfigIo(System &system, bool hasMolecules)
34  : ConfigIo(system),
35  hasMolecules_(hasMolecules)
36  {}
37 
38  /*
39  * Destructor.
40  */
42  {}
43 
44  void DdMdConfigIo::read(std::istream &in)
45  {
46  // Precondition
47  UTIL_CHECK(system().simulation().hasSpecies());
48 
49  // Calculate atom and bond capacities for entire simulation
50  int atomCapacity = 0;
51  int bondCapacity = 0;
52  int nSpecies = simulation().nSpecies();
53  int speciesCapacity = 0;
54  int iSpec;
55  Species* speciesPtr;
56  for (iSpec = 0; iSpec < nSpecies; ++iSpec) {
57  speciesPtr = &simulation().species(iSpec);
58  speciesCapacity = speciesPtr->capacity();
59  atomCapacity += speciesCapacity*speciesPtr->nAtom();
60  #ifdef SIMP_BOND
61  bondCapacity += speciesCapacity*speciesPtr->nBond();
62  #endif
63  }
64 
65  // Read boundary
66  in >> Label("BOUNDARY");
67  in >> system().boundary();
68 
69  // Read atomic positions
70  // Atom tags must appear in order, numbered from 0
71  // Number of atoms must match atomCapacity.
72  in >> Label("ATOMS");
73  int nAtom;
74  in >> Label("nAtom") >> nAtom;
75  if (nAtom != atomCapacity) {
76  UTIL_THROW("nAtom != atomCapacity");
77  }
78  Molecule* molPtr;
79  Molecule::AtomIterator atomIter;
80  int iMol, iAtom, nMolecule, atomId, atomTypeId;
81  int sId, mId, aId;
82  for (iSpec=0; iSpec < nSpecies; ++iSpec) {
83  speciesPtr = &simulation().species(iSpec);
84  nMolecule = speciesPtr->capacity();
85  for (iMol = 0; iMol < nMolecule; ++iMol) {
86  molPtr = &(simulation().getMolecule(iSpec));
87  system().addMolecule(*molPtr);
88 
89  // Read positions
90  iAtom = 0;
91  for (molPtr->begin(atomIter); atomIter.notEnd(); ++atomIter) {
92 
93  in >> atomId >> atomTypeId;
94  if (atomId != atomIter->id()) {
95  UTIL_THROW("Atom tags not ordered");
96  }
97  if (hasMolecules_) {
98  in >> sId >> mId >> aId;
99  if (sId != iSpec) {
100  UTIL_THROW("Invalid species id");
101  }
102  if (mId != iMol) {
103  UTIL_THROW("Invalid molecule id");
104  }
105  if (aId != iAtom) {
106  UTIL_THROW("Invalid local atom id");
107  }
108  }
109 
110  in >> atomIter->position();
111  in >> atomIter->velocity();
112 
113  ++iAtom;
114  }
115  }
116  }
117 
118  }
119 
120  void DdMdConfigIo::write(std::ostream &out)
121  {
122  // Count total numbers of atoms and bonds in all species.
123  Species *speciesPtr;
124  int iSpec, nMolecule;
125  int nAtom = 0;
126  #ifdef SIMP_BOND
127  int nBond = 0;
128  #endif
129  #ifdef SIMP_ANGLE
130  int nAngle = 0;
131  #endif
132  #ifdef SIMP_DIHEDRAL
133  int nDihedral = 0;
134  #endif
135  for (iSpec = 0; iSpec < simulation().nSpecies(); ++iSpec) {
136  speciesPtr = &simulation().species(iSpec);
137  nMolecule = system().nMolecule(iSpec);
138  nAtom += nMolecule*(speciesPtr->nAtom());
139  #ifdef SIMP_BOND
140  nBond += nMolecule*(speciesPtr->nBond());
141  #endif
142  #ifdef SIMP_ANGLE
143  nAngle += nMolecule*(speciesPtr->nAngle());
144  #endif
145  #ifdef SIMP_DIHEDRAL
146  nDihedral += nMolecule*(speciesPtr->nDihedral());
147  #endif
148  }
149 
150  // Write boundary
151  out << "BOUNDARY" << std::endl;
152  out << system().boundary() << std::endl;
153  out << std::endl;
154 
155  // Write atoms
156  out << "ATOMS" << std::endl;
157  out << "nAtom " << nAtom << std::endl;
158  System::MoleculeIterator molIter;
159  Molecule::AtomIterator atomIter;
160  int i = 0;
161  int iMol, iAtom;
162  for (iSpec=0; iSpec < simulation().nSpecies(); ++iSpec) {
163  iMol = 0;
164  system().begin(iSpec, molIter);
165  for ( ; molIter.notEnd(); ++molIter) {
166  iAtom = 0;
167  molIter->begin(atomIter);
168  for ( ; atomIter.notEnd(); ++atomIter) {
169  out << Int(atomIter->id(), 10);
170  out << Int(atomIter->typeId(), 5);
171  if (hasMolecules_) {
172  out << Int(iSpec, 6);
173  out << Int(iMol, 10);
174  out << Int(iAtom, 6);
175  }
176  out << "\n" << atomIter->position();
177  out << "\n" << atomIter->velocity();
178  out << "\n";
179  ++i;
180  ++iAtom;
181  }
182  ++iMol;
183  }
184  }
185  out << std::endl;
186 
187  #ifdef SIMP_BOND
188  // Write Bonds
189  out << "BONDS" << std::endl;
190  out << "nBond " << nBond << std::endl;
191  Molecule::BondIterator bondIter;
192  i = 0;
193  for (iSpec=0; iSpec < simulation().nSpecies(); ++iSpec) {
194  if (system().simulation().species(iSpec).nBond() > 0) {
195  system().begin(iSpec, molIter);
196  for ( ; molIter.notEnd(); ++molIter) {
197  molIter->begin(bondIter);
198  for ( ; bondIter.notEnd(); ++bondIter) {
199  out << Int(i, 8) << Int(bondIter->typeId(), 5);
200  out << Int(bondIter->atom(0).id(), 10);
201  out << Int(bondIter->atom(1).id(), 10);
202  out << std::endl;
203  ++i;
204  }
205  }
206  }
207  }
208  out << std::endl;
209  #endif
210 
211  #ifdef SIMP_ANGLE
212  // Write Angles
213  out << "ANGLES" << std::endl;
214  out << "nAngle " << nAngle << std::endl;
215  Molecule::AngleIterator angleIter;
216  i = 0;
217  for (iSpec=0; iSpec < simulation().nSpecies(); ++iSpec) {
218  if (system().simulation().species(iSpec).nAngle() > 0) {
219  system().begin(iSpec, molIter);
220  for ( ; molIter.notEnd(); ++molIter) {
221  molIter->begin(angleIter);
222  for ( ; angleIter.notEnd(); ++angleIter) {
223  out << Int(i, 8) << Int(angleIter->typeId(), 5);
224  out << Int(angleIter->atom(0).id(), 10);
225  out << Int(angleIter->atom(1).id(), 10);
226  out << Int(angleIter->atom(2).id(), 10);
227  out << std::endl;
228  ++i;
229  }
230  }
231  }
232  }
233  out << std::endl;
234  #endif
235 
236  #ifdef SIMP_DIHEDRAL
237  // Write Dihedral
238  out << "DIHEDRALS" << std::endl;
239  out << "nDihedral " << nDihedral << std::endl;
240  Molecule::DihedralIterator dihedralIter;
241  i = 0;
242  for (iSpec=0; iSpec < simulation().nSpecies(); ++iSpec) {
243  if (system().simulation().species(iSpec).nDihedral() > 0) {
244  system().begin(iSpec, molIter);
245  for ( ; molIter.notEnd(); ++molIter) {
246  molIter->begin(dihedralIter);
247  for ( ; dihedralIter.notEnd(); ++dihedralIter) {
248  out << Int(i, 8) << Int(dihedralIter->typeId(), 5);
249  out << Int(dihedralIter->atom(0).id(), 10);
250  out << Int(dihedralIter->atom(1).id(), 10);
251  out << Int(dihedralIter->atom(2).id(), 10);
252  out << Int(dihedralIter->atom(3).id(), 10);
253  out << std::endl;
254  ++i;
255  }
256  }
257  }
258  }
259  out << std::endl;
260  #endif
261 
262  // Reset Format defaults to initialization values
264  }
265 
266 }
void begin(int speciesId, MoleculeIterator &iterator)
Initialize an iterator for molecules of one species in this System.
Definition: System.h:1147
int nAtom() const
Get number of atoms per molecule for this Species.
bool notEnd() const
Is the current pointer not at the end of the array?
Definition: ArrayIterator.h:83
virtual ~DdMdConfigIo()
Destructor.
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
Classes used by all simpatico molecular simulations.
Molecule & getMolecule(int speciesId)
Get a new molecule from a reservoir of unused Molecule objects.
int nDihedral() const
Get number of dihedrals per molecule for this Species.
int nMolecule(int speciesId) const
Get the number of molecules of one Species in this System.
Definition: System.h:1128
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
Simulation & simulation() const
Get the parent Simulation by reference.
Definition: System.h:1055
void addMolecule(Molecule &molecule)
Add a Molecule to this System.
Definition: System.cpp:1111
bool notEnd() const
Is the current pointer not at the end of the PArray?
Utility classes for scientific computation.
Definition: accumulators.mod:1
System & system() const
Get a reference to the parent System.
Wrapper for an int, for formatted ostream output.
Definition: Int.h:36
void read(std::istream &in)
Read configuration (particle positions) from file.
Forward iterator for an Array or a C array.
Definition: ArrayIterator.h:39
Forward iterator for a PArray.
Definition: ArraySet.h:19
void write(std::ostream &out)
Write configuration (particle positions) to file.
A label string in a file format.
Definition: Label.h:36
Simulation & simulation() const
Get a reference to the parent Simulation.
Boundary & boundary() const
Get the Boundary by reference.
Definition: System.h:1064
DdMdConfigIo(System &system, bool hasMolecules=false)
Constructor.
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
int nSpecies() const
Get the number of Species in this Simulation.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition: global.h:68
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.
int nBond() const
Get number of bonds per molecule for this Species.
int nAngle() const
Get number of angles per molecule for this Species.
Species & species(int i)
Get a specific Species by reference.
static void initStatic()
Initialize or reset default width and precision values.
Definition: Format.cpp:47
System configuration file reader and writer.