Simpatico  v1.10
HoomdConfigWriter.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 "HoomdConfigWriter.h"
9 
10 #include <tools/chemistry/Atom.h>
11 #include <tools/chemistry/Group.h>
12 #include <tools/chemistry/Species.h>
13 //#include <tools/chemistry/MaskPolicy.h>
14 #include <tools/storage/GroupStorage.h>
15 #include <tools/storage/Configuration.h>
16 
17 #include <util/space/Vector.h>
18 #include <util/format/Int.h>
19 #include <util/format/Dbl.h>
20 #include <util/misc/ioUtil.h>
21 
22 namespace Tools
23 {
24 
25  using namespace Util;
26 
27  /*
28  * Constructor.
29  */
31  : ConfigWriter(configuration, true),
32  hasTypeMaps_(false)
33  { setClassName("HoomdConfigWriter"); }
34 
35  /*
36  * Read auxiliary type map file.
37  */
38  void HoomdConfigWriter::readAuxiliaryFile(std::ifstream& file)
39  {
40  bool notEnd;
41  std::stringstream line;
42 
43  notEnd = getNextLine(file, line);
44  if (notEnd) {
45  checkString(line, "ATOM");
46  checkString(line, "TYPES:");
47  atomTypeMap_.read(file);
48  }
49 
50  notEnd = getNextLine(file, line);
51  if (notEnd) {
52  checkString(line, "BOND");
53  checkString(line, "TYPES:");
54  bondTypeMap_.read(file);
55  }
56 
57  notEnd = getNextLine(file, line);
58  if (notEnd) {
59  checkString(line, "ANGLE");
60  checkString(line, "TYPES:");
61  angleTypeMap_.read(file);
62  }
63 
64  hasTypeMaps_ = true;
65  }
66 
67  /*
68  * Private function template for writing groups.
69  */
70  template <int N> void
71  HoomdConfigWriter::writeGroups(std::ofstream& file,
72  const std::string& label,
73  const GroupStorage<N>& storage,
74  const TypeMap& map)
75  {
76  int nGroup = storage.size();
77  file << "<" << label
78  << " num=\"" << nGroup << "\">\n";
79  std::string typeName;
80  int j;
82  for (storage.begin(iter); iter.notEnd(); ++iter) {
83  typeName = map.name(iter->typeId);
84  file << typeName << " ";
85  for (j = 0; j < N; ++j) {
86  file << iter->atomIds[j] << " ";
87  }
88  file << "\n";
89  }
90  file << "</" << label << ">\n";
91  }
92 
93  /*
94  * Write the configuration file.
95  */
96  void HoomdConfigWriter::writeConfig(std::ofstream& file)
97  {
98  // Precondition
99  if (!file.is_open()) {
100  UTIL_THROW("Error: File is not open");
101  }
102  if (!hasTypeMaps_) {
103  UTIL_THROW("Error: Must read type map auxiliary file before config");
104  }
105  int nAtom = configuration().atoms().size();
106 
107  file << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
108  file << "<hoomd_xml version=\"1.5\">\n";
109  file << "<configuration time_step=\"0\" dimensions=\"3\" natoms=\""
110  << nAtom << "\" >\n";
111 
112  // Set ostream floating point precision, restore default format
113  file.precision(12);
114  file.unsetf(std::ios_base::floatfield);
115 
116  // Write box
117  Boundary& boundary = configuration().boundary();
118  Vector lengths = boundary.lengths();
119  file << "<box"
120  << " lx=\"" << lengths[0] << "\""
121  << " ly=\"" << lengths[1] << "\""
122  << " lz=\"" << lengths[2] << "\""
123  << " xy=\"0\""
124  << " xz=\"0\""
125  << " yz=\"0\""
126  << "/>\n";
127 
128  // Write atom position node
129  file << "<position num=\"" << nAtom << "\">\n";
130  Vector rg, rc;
132  int j;
133  configuration().atoms().begin(iter);
134  for (; iter.notEnd(); ++iter) {
135  boundary.shift(iter->position);
136  boundary.transformCartToGen(iter->position, rg);
137  for (j = 0; j < Dimension; ++j) {
138  if (rg[j] >= 0.5) {
139  rg[j] -= 1.0;
140  }
141  }
142  boundary.transformGenToCart(rg, rc);
143  for (j = 0; j < Dimension; ++j) {
144  file.precision(12);
145  file << rc[j] << " ";
146  }
147  file << "\n";
148  }
149  file << "</position>\n";
150 
151  #if 0
152  // Write velocity
153  file << "<velocity num=\"" << nAtom << "\">\n";
154  configuration().atoms().begin(iter);
155  for (; iter.notEnd(); ++iter) {
156  file << iter->velocity << "\n";
157  }
158  file << "</velocity>\n";
159  #endif
160 
161  // Write type
162  file << "<type num=\"" << nAtom << "\">\n";
163  configuration().atoms().begin(iter);
164  for (; iter.notEnd(); ++iter) {
165  file << atomTypeMap_.name(iter->typeId) << "\n";
166  }
167  file << "</type>\n";
168 
169  // Write covalent groups, as needed
170  #ifdef SIMP_BOND
171  if (configuration().bonds().size()) {
172  writeGroups(file, "bond", configuration().bonds(),
173  bondTypeMap_);
174  }
175  #endif
176  #ifdef SIMP_ANGLE
177  if (configuration().angles().size()) {
178  writeGroups(file, "angle", configuration().angles(),
179  angleTypeMap_);
180  }
181  #endif
182  #ifdef SIMP_DIHEDRAL
183  if (configuration().dihedrals().size()) {
184  writeGroups(file, "dihedral", configuration().dihedrals(),
185  dihedralTypeMap_);
186  }
187  #endif
188 
189  file << "</configuration>\n";
190  file << "</hoomd_xml>\n";
191  }
192 
193 }
int size() const
Return logical size of this array (i.e., number of elements).
A container for Group<N> objects.
const int Dimension
Dimensionality of space.
Definition: Dimension.h:19
Map between type names and type ids.
Definition: TypeMap.h:26
A Vector is a Cartesian vector.
Definition: Vector.h:75
virtual void writeConfig(std::ofstream &file)
Write configuration file in DdMd default format.
bool notEnd() const
Is the current pointer not at the end of the array?
Definition: ArrayIterator.h:83
const std::string & name(int id) const
Get type name associated with an integer id.
Definition: TypeMap.h:92
HoomdConfigWriter()
Default constructor.
const Vector & lengths() const
Get Vector of unit cell lengths by const reference.
An orthorhombic periodic unit cell.
void read(std::istream &in)
Read pairs from file.
Definition: TypeMap.cpp:42
int size() const
Get number of atoms.
virtual void readAuxiliaryFile(std::ifstream &file)
Read auxiliary file with type map information.
AtomStorage & atoms()
Get the AtomStorage.
An instantaneous molecular dynamics configuration.
Definition: Configuration.h:40
Abstract reader/writer for configuration files.
Forward const iterator for an Array or a C array.
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
Utility classes for scientific computation.
Definition: accumulators.mod:1
void shift(Vector &r) const
Shift Cartesian Vector r to its primary image.
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
Single-processor classes for pre- and post-processing MD trajectories.
void transformGenToCart(const Vector &Rg, Vector &Rc) const
Transform Vector of generalized coordinates to Cartesian Vector.
void begin(Iterator &iter)
Initialize an iterator for atoms.
Boundary & boundary()
Get the Boundary by non-const reference.
void begin(ArrayIterator< Group< N > > &iterator)
Set an ArrayIterator to the beginning of this Array.
void setClassName(const char *className)
Set class name string.
bool notEnd() const
Is this not the end of the array?
void transformCartToGen(const Vector &Rc, Vector &Rg) const
Transform Cartesian Vector to scaled / generalized coordinates.