Simpatico  v1.10
McMdConfigIo.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 "McMdConfigIo.h"
9 #include <mcMd/simulation/Simulation.h>
10 #include <mcMd/simulation/System.h>
11 #include <mcMd/species/SpeciesMutator.h>
12 #include <mcMd/chemistry/Molecule.h>
13 #include <mcMd/chemistry/Atom.h>
14 #ifdef SIMP_TETHER
15 #include <mcMd/tethers/TetherMaster.h>
16 #endif
17 #ifdef MCMD_LINK
18 #include <mcMd/links/LinkMaster.h>
19 #endif
20 #include <simp/species/Species.h>
21 #include <util/param/Label.h>
22 #include <util/param/OptionalLabel.h>
23 #include <util/format/Int.h>
24 
25 namespace McMd
26 {
27 
28  using namespace Util;
29  using namespace Simp;
30 
31  /*
32  * Constructor.
33  */
35  : ConfigIo(system)
36  {}
37 
38  /*
39  * Destructor.
40  */
42  {}
43 
44  /*
45  * Read the configuration file.
46  */
47  void McMdConfigIo::read(std::istream &in)
48  {
49  // Precondition
50  UTIL_CHECK(system().simulation().hasSpecies());
51 
52  // Read and set dimensions of simulation Boundary.
53  in >> Label("BOUNDARY");
54  in >> boundary() ;
55 
56  // Get molecules and read atomic positions
57  Label speciesLabel("species");
58  Label nMoleculeLabel("nMolecule");
59  Label moleculeLabel("molecule");
60  Species* speciesPtr;
61  Molecule* molPtr;
62  Molecule::AtomIterator atomIter;
63  int iSpecies, iSpeciesIn, nMolecule, iMol, iMolIn;
64 
65  in >> Label("MOLECULES");
66  for (iSpecies = 0; iSpecies < simulation().nSpecies(); ++iSpecies) {
67  speciesPtr = &simulation().species(iSpecies);
68  in >> Label("species") >> iSpeciesIn;
69  if (iSpeciesIn != iSpecies) {
70  UTIL_THROW("Error: iSpeciesIn != iSpecies");
71  }
72  in >> Label("nMolecule") >> nMolecule;
73  for (iMol = 0; iMol < nMolecule; ++iMol) {
74  in >> moleculeLabel >> iMolIn;
75  molPtr = &(simulation().getMolecule(iSpecies));
76  system().addMolecule(*molPtr);
77 
78  if (iMolIn != iMol) {
79  UTIL_THROW("Error: iMolIn != iMol");
80  }
81  if (molPtr != &system().molecule(iSpecies, iMol)) {
82  UTIL_THROW("Molecule index error");
83  }
84 
85  // If mutable, read molecule state id
86  if (speciesPtr->isMutable()) {
87  speciesPtr->mutator().readMoleculeState(in, *molPtr);
88  }
89 
90  // Read positions, shift into primary cell
91  for (molPtr->begin(atomIter); atomIter.notEnd(); ++atomIter) {
92  readAtom(in, *atomIter);
93  }
94 
95  }
96  }
97 
98  #ifdef SIMP_TETHER
99  if (OptionalLabel("TETHERS").match(in))
100  {
101 
102  Vector anchor;
103  Atom* atomPtr;
104  int iTether, nTether, iAtom;
105 
106  // Read Tethers
107  in >> Label("nTether") >> nTether;
108  for (iTether = 0; iTether < nTether; ++iTether) {
109  in >> iSpecies >> iMol >> iAtom >> anchor;
110  if (iSpecies >= simulation().nSpecies()) {
111  UTIL_THROW("Invalid species index for tether");
112  }
113  if (iMol >= system().nMolecule(iSpecies)) {
114  UTIL_THROW("Invalid molecule index for tether");
115  }
116  molPtr = &system().molecule(iSpecies, iMol);
117  if (iAtom >= molPtr->nAtom()) {
118  Log::file() << iAtom << " " << molPtr->nAtom() << std::endl;
119  UTIL_THROW("Invalid atom index for tether");
120  }
121  atomPtr = &molPtr->atom(iAtom);
122  system().tetherMaster().addTether(*atomPtr, anchor);
123  }
124 
125  }
126  #endif
127 
128  #ifdef MCMD_LINK
129  if (OptionalLabel("LINKS").match(in)) {
130 
131  // Read Links
132  Atom* atom0Ptr;
133  Atom* atom1Ptr;
134  int iLink, nLink, iAtom, linkType;
135 
136  in >> Label("nLink") >> nLink;
137  for (iLink = 0; iLink < nLink; ++iLink) {
138 
139  // Read atom 0
140  in >> iSpecies >> iMol >> iAtom;
141  if (iSpecies < 0 || iSpecies >= simulation().nSpecies()) {
142  Log::file() << "iLink = " << iLink << std::endl;
143  UTIL_THROW("Invalid iSpecies0 index in link");
144  }
145  if (iMol < 0 || iMol >= system().nMolecule(iSpecies)) {
146  Log::file() << "iLink = " << iLink << std::endl;
147  UTIL_THROW("Invalid iMol0 index in link");
148  }
149  molPtr = &system().molecule(iSpecies, iMol);
150  if (iAtom < 0 || iAtom >= molPtr->nAtom()) {
151  Log::file() << "iLink = " << iLink << std::endl;
152  UTIL_THROW("Invalid iAtom0 index for link");
153  }
154  atom0Ptr = &molPtr->atom(iAtom);
155 
156  // Read atom 1
157  in >> iSpecies >> iMol >> iAtom;
158  if (iSpecies < 0 || iSpecies >= simulation().nSpecies()) {
159  Log::file() << "iLink = " << iLink << std::endl;
160  UTIL_THROW("Invalid iSpecies1 index in link");
161  }
162  if (iMol < 0 || iMol >= system().nMolecule(iSpecies)) {
163  Log::file() << "iLink = " << iLink << std::endl;
164  UTIL_THROW("Invalid iMol1 index in link");
165  }
166  molPtr = &system().molecule(iSpecies, iMol);
167  if (iAtom < 0 || iAtom >= molPtr->nAtom()) {
168  Log::file() << "iLink = " << iLink << std::endl;
169  UTIL_THROW("Invalid iAtom1 index for link");
170  }
171  atom1Ptr = &molPtr->atom(iAtom);
172 
173  in >> linkType;
174  system().linkMaster().addLink(*atom0Ptr, *atom1Ptr, linkType);
175 
176  }
177 
178  }
179  #endif
180 
181  }
182 
183  /*
184  * Write the configuration file.
185  */
186  void McMdConfigIo::write(std::ostream &out)
187  {
188  using std::endl;
189 
190  // Write Boundary dimensions
191  out << "BOUNDARY" << endl << endl;
192  out << boundary() << endl;
193 
194  // Write atomic positions
197  Species* speciesPtr;
198  int iSpecies, iMolecule;
199  out << endl << "MOLECULES" << endl;
200  for (iSpecies = 0; iSpecies < simulation().nSpecies(); ++iSpecies) {
201  out << endl;
202  out << "species " << iSpecies << endl;
203  out << "nMolecule " << system().nMolecule(iSpecies) << endl;
204  speciesPtr = &simulation().species(iSpecies);
205  iMolecule = 0;
206  system().begin(iSpecies, molIter);
207  for ( ; molIter.notEnd(); ++molIter) {
208  out << endl;
209  out << "molecule " << iMolecule << endl;
210  if (speciesPtr->isMutable()) {
211  speciesPtr->mutator().writeMoleculeState(out, *molIter);
212  }
213  for (molIter->begin(atomIter); atomIter.notEnd(); ++atomIter) {
214  //out << atomIter->position() << endl;
215  writeAtom(out, *atomIter);
216  }
217  ++iMolecule;
218  }
219  }
220 
221  #ifdef SIMP_TETHER
222  { // Scope for local variables
223 
224  // Write Tethers
225  Tether* tetherPtr;
226  Atom* atomPtr;
227  Molecule* molPtr;
228  int iTether, nTether, iAtom;
229  out << std::endl;
230  out << "TETHERS" << endl << endl;
231  nTether = system().tetherMaster().nTether();
232  out << Label("nTether") << nTether << std::endl;
233  for (iTether = 0; iTether < nTether; ++iTether) {
234  tetherPtr = &(system().tetherMaster().tether(iTether));
235  atomPtr = &tetherPtr->atom();
236  molPtr = &atomPtr->molecule();
237  iAtom = atomPtr->indexInMolecule();
238  iMolecule = system().moleculeId(*molPtr);
239  iSpecies = molPtr->species().id();
240  out << Int(iSpecies,5) << Int(iMolecule,9) << Int(iAtom,6)
241  << tetherPtr->anchor() << std::endl;
242  }
243 
244  }
245  #endif
246 
247  #ifdef MCMD_LINK
248  { // Scope for local variables
249 
250  // Write Links
251  Link* linkPtr;
252  Atom* atomPtr;
253  Molecule* molPtr;
254  int iLink, nLink, iAtom;
255  out << std::endl;
256  out << "LINKS" << endl << endl;
257  nLink = system().linkMaster().nLink();
258  out << Label("nLink") << nLink << std::endl;
259  for (iLink = 0; iLink < nLink; ++iLink) {
260  linkPtr = &(system().linkMaster().link(iLink));
261 
262  // Output species, molecule, atom ids for atom 0
263  atomPtr = &(linkPtr->atom0());
264  molPtr = &atomPtr->molecule();
265  iAtom = atomPtr->indexInMolecule();
266  iMolecule = system().moleculeId(*molPtr);
267  iSpecies = molPtr->species().id();
268  out << Int(iSpecies,8) << Int(iMolecule,8) << Int(iAtom,8);
269  out << " ";
270 
271  // Output species, molecule, atom ids for atom 1
272  atomPtr = &(linkPtr->atom1());
273  molPtr = &atomPtr->molecule();
274  iAtom = atomPtr->indexInMolecule();
275  iMolecule = system().moleculeId(*molPtr);
276  iSpecies = molPtr->species().id();
277  out << Int(iSpecies,8) << Int(iMolecule,8) << Int(iAtom,8);
278  out << " ";
279 
280  out << Int(linkPtr->typeId(),8) << std::endl;
281  }
282 
283  }
284  #endif
285 
286  }
287 
288 }
int nLink() const
Get the total number of active Links.
Definition: LinkMaster.h:261
Boundary & boundary() const
Get the Boundary.
An optional Label string in a file format.
Definition: OptionalLabel.h:23
McMdConfigIo(System &system)
Constructor.
Molecule & molecule() const
Get the parent Molecule by reference.
A Vector is a Cartesian vector.
Definition: Vector.h:75
void begin(int speciesId, MoleculeIterator &iterator)
Initialize an iterator for molecules of one species in this System.
Definition: System.h:1147
bool notEnd() const
Is the current pointer not at the end of the array?
Definition: ArrayIterator.h:83
Species & species() const
Get the molecular Species by reference.
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
Forward iterator for a PArray.
virtual void read(std::istream &in)
Read configuration (particle positions) from file.
virtual void write(std::ostream &out)
Write configuration (particle positions) to file.
Classes used by all simpatico molecular simulations.
virtual void readAtom(std::istream &out, Atom &atom)=0
Read data for one atom.
Molecule & getMolecule(int speciesId)
Get a new molecule from a reservoir of unused Molecule objects.
Forward const iterator for an Array or a C array.
virtual ~McMdConfigIo()
Destructor.
Molecule & molecule(int speciesId, int moleculeId)
Get a specific Molecule in this System, by integer index.
Definition: System.h:1137
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
void addMolecule(Molecule &molecule)
Add a Molecule to this System.
Definition: System.cpp:1111
A point particle within a Molecule.
Utility classes for scientific computation.
Definition: accumulators.mod:1
System & system() const
Get a reference to the parent System.
virtual void readMoleculeState(std::istream &in, Molecule &molecule)
Read the state id for one molecule from a configuration file stream.
Wrapper for an int, for formatted ostream output.
Definition: Int.h:36
Forward iterator for an Array or a C array.
Definition: ArrayIterator.h:39
virtual void writeMoleculeState(std::ostream &out, const Molecule &molecule) const
Write the state id for one molecule to a configuration file stream.
A label string in a file format.
Definition: Label.h:36
Simulation & simulation() const
Get a reference to the parent Simulation.
static std::ostream & file()
Get log ostream by reference.
Definition: Log.cpp:57
bool notEnd() const
Is the current pointer not at the end of the array?
int id() const
Get integer id of this Species.
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
int nSpecies() const
Get the number of Species in this Simulation.
bool isMutable() const
Is this a mutable Species?
virtual void writeAtom(std::ostream &out, const Atom &atom)=0
Write data for one atom.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition: global.h:68
const Atom & atom(int localId) const
Get a specific Atom in this Molecule.
int indexInMolecule() const
Get local index for this Atom within the parent molecule;.
Link & link(int id) const
Return an active link by an internal set index.
Definition: LinkMaster.h:255
A physical molecule (a set of covalently bonded Atoms).
bool notEnd() const
Is this not the end of the array?
McMd::SpeciesMutator & mutator()
Return the species mutator object by reference.
A Species represents a set of chemically similar molecules.
void addLink(Atom &atom0, Atom &atom1, int typeId)
Add a link betwen two specific Atoms.
Definition: LinkMaster.cpp:39
Species & species(int i)
Get a specific Species by reference.
int nAtom() const
Get the number of Atoms in this Molecule.
int moleculeId(const Molecule &molecule) const
Get the index of a Molecule within its Species in this System.
Definition: System.cpp:1189
System configuration file reader and writer.
LinkMaster & linkMaster() const
Get the LinkMaster by reference.
Definition: System.h:1074