Simpatico  v1.10
DdMdConfigReader.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 "DdMdConfigReader.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  DdMdConfigReader::DdMdConfigReader(Configuration& configuration, bool hasMolecules)
29  : ConfigReader(configuration),
30  hasMolecules_(hasMolecules)
31  { setClassName("DdMdConfigReader"); }
32 
33  /*
34  * Private method to read Group<N> objects.
35  */
36  template <int N>
37  int DdMdConfigReader::readGroups(std::ifstream& file,
38  const char* sectionLabel,
39  const char* nGroupLabel,
40  GroupStorage<N>& groups)
41  {
42  int nGroup; // Total number of groups in file
43  file >> Label(sectionLabel);
44  file >> Label(nGroupLabel) >> nGroup;
45  Group<N>* groupPtr;
46  for (int i = 0; i < nGroup; ++i) {
47  groupPtr = groups.newPtr();
48  file >> *groupPtr;
49  }
50  return nGroup;
51  }
52 
53  /*
54  * Read a configuration file.
55  */
56  void DdMdConfigReader::readConfig(std::ifstream& file)
57  {
58  // Precondition
59  if (!file.is_open()) {
60  UTIL_THROW("Error: File is not open");
61  }
62 
63  // Read and broadcast boundary
64  file >> Label("BOUNDARY");
65  file >> configuration().boundary();
66 
67  // Read and distribute atoms
68 
69  // Read atoms
70  Atom* atomPtr;
71  int atomCapacity = configuration().atoms().capacity(); // Maximum allowed id + 1
72  int nAtom;
73  file >> Label("ATOMS");
74  file >> Label("nAtom") >> nAtom;
75  for (int i = 0; i < nAtom; ++i) {
76 
77  // Get pointer to new atom
78  atomPtr = configuration().atoms().newPtr();
79 
80  file >> atomPtr->id;
81  if (atomPtr->id < 0) {
82  std::cout << "atom id =" << atomPtr->id << std::endl;
83  UTIL_THROW("Negative atom id");
84  }
85  if (atomPtr->id >= atomCapacity) {
86  std::cout << "atom id =" << atomPtr->id << std::endl;
87  std::cout << "atomCapacity =" << atomCapacity << std::endl;
88  UTIL_THROW("Invalid atom id");
89  }
90  file >> atomPtr->typeId;
91  if (hasMolecules_) {
92  file >> atomPtr->speciesId;
93  if (atomPtr->speciesId < 0) {
94  std::cout << "species Id =" << atomPtr->speciesId << std::endl;
95  UTIL_THROW("Negative species id");
96  }
97  file >> atomPtr->moleculeId;
98  if (atomPtr->moleculeId < 0) {
99  std::cout << "molecule Id =" << atomPtr->moleculeId << std::endl;
100  UTIL_THROW("Negative molecule id");
101  }
102  file >> atomPtr->atomId;
103  if (atomPtr->atomId < 0) {
104  std::cout << "atom id =" << atomPtr->atomId << std::endl;
105  UTIL_THROW("Negative atom id in molecule");
106  }
107  }
108  file >> atomPtr->position;
109  file >> atomPtr->velocity;
110 
111  // Finalize addition of new atom
112  configuration().atoms().add();
113  }
114 
115  // Read Covalent Groups
116  #ifdef SIMP_BOND
117  if (configuration().bonds().capacity()) {
118  readGroups(file, "BONDS", "nBond", configuration().bonds());
119  //if (maskPolicy == MaskBonded) {
120  // setAtomMasks();
121  //}
122  }
123  #endif
124 
125  #ifdef SIMP_ANGLE
126  if (configuration().angles().capacity()) {
127  readGroups(file, "ANGLES", "nAngle", configuration().angles());
128  }
129  #endif
130 
131  #ifdef SIMP_DIHEDRAL
132  if (configuration().dihedrals().capacity()) {
133  readGroups(file, "DIHEDRALS", "nDihedral", configuration().dihedrals());
134  }
135  #endif
136 
137  // Optionally add atoms to species
138  if (configuration().nSpecies() > 0) {
139  if (hasMolecules_) {
141  } else {
142  bool success;
143  success = setAtomContexts();
144  if (success) {
146  }
147  }
148  }
149 
150  }
151 
152 }
Vector velocity
Atom velocity.
A container for Group<N> objects.
int speciesId
Index for species of parent molecule.
int id
Unique global index (tag)
Abstract reader/writer for configuration files.
Definition: ConfigReader.h:27
Group< N > * newPtr()
Append a new element to container and return its address.
int capacity() const
Get atom capacity (maximum id + 1).
AtomStorage & atoms()
Get the AtomStorage.
An instantaneous molecular dynamics configuration.
Definition: Configuration.h:40
bool setAtomContexts()
Set atom context data for all atoms, assuming consecutive ids.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
int typeId
Atom type index.
Utility classes for scientific computation.
Definition: accumulators.mod:1
void add()
Finalize addition of atom (allows lookup by id).
Single-processor classes for pre- and post-processing MD trajectories.
int atomId
Index of atom within its molecule.
virtual void readConfig(std::ifstream &file)
Read configuration file in DdMd default format.
A point particle in an MD simulation.
A label string in a file format.
Definition: Label.h:36
A group of covalently interacting atoms.
Atom * newPtr()
Return pointer to location for new atom.
Boundary & boundary()
Get the Boundary by non-const reference.
void addAtomsToSpecies()
Add all atoms to Species containers and associated molecules.
void setClassName(const char *className)
Set class name string.
Vector position
Atom position.
DdMdConfigReader(bool hasMolecules=false)
Default constructor.
Configuration & configuration()
Get parent Configuration by reference.
Definition: ConfigReader.h:89
int moleculeId
Index of molecule with its species.