Simpatico  v1.10
DdMdConfigWriter.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 "DdMdConfigWriter.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/Configuration.h>
15 
16 #include <util/space/Vector.h>
17 #include <util/format/Int.h>
18 #include <util/format/Dbl.h>
19 
20 namespace Tools
21 {
22 
23  using namespace Util;
24 
25  /*
26  * Constructor.
27  */
28  DdMdConfigWriter::DdMdConfigWriter(Configuration& configuration, bool hasMolecules)
29  : ConfigWriter(configuration),
30  hasMolecules_(hasMolecules)
31  { setClassName("DdMdConfigWriter"); }
32 
33  /*
34  * Private method to write Group<N> objects.
35  */
36  template <int N>
37  int DdMdConfigWriter::writeGroups(std::ofstream& file, const char* sectionLabel,
38  const char* nGroupLabel, GroupStorage<N>& groups)
39  {
41  int nGroup = configuration().bonds().size();
42 
43  file << std::endl;
44  file << sectionLabel << std::endl;
45  file << nGroupLabel << Int(nGroup, 10) << std::endl;
46  for (groups.begin(iter); iter.notEnd(); ++iter) {
47  file << *iter << std::endl;
48  }
49  return nGroup;
50  }
51 
52  /*
53  * Write the configuration file.
54  */
55  void DdMdConfigWriter::writeConfig(std::ofstream& file)
56  {
57  // Precondition
58  if (!file.is_open()) {
59  UTIL_THROW("Error: File is not open");
60  }
61 
62  // Write boundary box dimensions
63  file << "BOUNDARY" << std::endl << std::endl;
64  file << configuration().boundary() << std::endl;
65  file << std::endl;
66 
67  // Write atoms
68  file << "ATOMS" << std::endl;
69  int nAtom = configuration().atoms().size();
70  file << "nAtom" << Int(nAtom, 10) << std::endl;
71  Vector r;
73  configuration().atoms().begin(iter);
74  for (; iter.notEnd(); ++iter) {
75  file << Int(iter->id, 10)
76  << Int(iter->typeId, 6);
77  r = iter->position;
78  if (hasMolecules_) {
79  file << Int(iter->speciesId, 6)
80  << Int(iter->moleculeId, 10)
81  << Int(iter->atomId, 6);
82  }
83  file << "\n" << r
84  << "\n" << iter->velocity << "\n";
85  }
86 
87  // Write the groups
88  #ifdef SIMP_BOND
89  if (configuration().bonds().capacity()) {
90  writeGroups(file, "BONDS", "nBond", configuration().bonds());
91  }
92  #endif
93 
94  #ifdef SIMP_ANGLE
95  if (configuration().angles().capacity()) {
96  writeGroups(file, "ANGLES", "nAngle", configuration().angles());
97  }
98  #endif
99 
100  #ifdef SIMP_DIHEDRAL
101  if (configuration().dihedrals().capacity()) {
102  writeGroups(file, "DIHEDRALS", "nDihedral", configuration().dihedrals());
103  }
104  #endif
105 
106  }
107 
108 }
int size() const
Return logical size of this array (i.e., number of elements).
Definition: DSArray.h:388
A container for Group<N> objects.
A Vector is a Cartesian vector.
Definition: Vector.h:75
bool notEnd() const
Is the current pointer not at the end of the array?
Definition: ArrayIterator.h:83
int size() const
Get number of atoms.
AtomStorage & atoms()
Get the AtomStorage.
An instantaneous molecular dynamics configuration.
Definition: Configuration.h:40
Abstract reader/writer for configuration files.
#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
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
Single-processor classes for pre- and post-processing MD trajectories.
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.
DdMdConfigWriter(bool hasMolecules=false)
Default constructor.
virtual void writeConfig(std::ofstream &file)
Write configuration file in DdMd default format.
GroupStorage< 2 > & bonds()
Get the Bond storage.