Simpatico  v1.10
Multiblock.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 "Multiblock.h"
9 #include <util/global.h>
10 
11 namespace Simp
12 {
13 
14  using namespace Util;
15 
16  /*
17  * Default constructor.
18  */
20  : Linear(),
21  nBlock_(2)
22  #ifdef SIMP_ANGLE
23  , angleType_(NullIndex)
24  #endif
25  #ifdef SIMP_DIHEDRAL
26  , dihedralType_(NullIndex)
27  #endif
28  { setClassName("Multiblock");
29 
30  }
31 
32  /*
33  * Read blockLengths_ and atomTypes_ for each block
34  */
35  void Multiblock::readSpeciesParam(std::istream &in)
36  {
37  read<int>(in,"nBlock", nBlock_);
39  readDArray<int>(in,"blockLengths", blockLengths_, nBlock_);
40  for (int i=0; i < nBlock_; ++i) {
41  if (blockLengths_[i] < 1) {
42  UTIL_THROW("Invalid blockLength for Multiblock.");
43  }
44  }
45  atomTypes_.allocate(nBlock_);
46  readDArray<int>(in,"atomTypes", atomTypes_, nBlock_);
47  for (int i=0; i < nBlock_; ++i) {
48  if (atomTypes_[i] < 0) {
49  UTIL_THROW("Invalid atomType for Multiblock.");
50  }
51  }
52  blockBegin_.allocate(nBlock_);
53  for (int i=0; i < nBlock_; ++i) {
54  if (i == 0) {
55  blockBegin_[i] = 0;
56  } else {
57  blockBegin_[i] = blockBegin_[i-1] + blockLengths_[i-1];
58  }
59  }
60  nAtom_ = 0;
61  for (int i=0; i < nBlock_; ++i) {
62  nAtom_ = nAtom_ + blockLengths_[i];
63  }
64 
65  nBond_ = nAtom_ - 1;
66  read<int>(in, "bondType", bondType_);
67 
68  #ifdef SIMP_ANGLE
69  hasAngles_ = 0; // default value
70  readOptional<int>(in, "hasAngles", hasAngles_);
71  if (hasAngles_) {
72  if (nAtom_ < 3) {
73  UTIL_THROW("Error: Cannot have angles with nAtom < 3");
74  }
75  nAngle_ = nAtom_ - 2;
76  read<int>(in, "angleType", angleType_);
77  } else {
78  nAngle_ = 0;
79  }
80  #endif
81 
82  #ifdef SIMP_DIHEDRAL
83  hasDihedrals_ = 0; // default value
84  readOptional<int>(in, "hasDihedrals", hasDihedrals_);
85  if (hasDihedrals_) {
86  if (nAtom_ < 4) {
87  UTIL_THROW("Error: Cannot have dihedrals with nAtom < 4");
88  }
89  nDihedral_ = nAtom_ - 3;
90  read<int>(in, "dihedralType", dihedralType_);
91  } else {
92  nDihedral_ = 0;
93  }
94  #endif
95 
96  buildLinear();
97  }
98 
99  /*
100  * Load from Serializable::IArchive.
101  */
103  {
104  loadParameter<int>(ar,"nBlock", nBlock_);
106  loadDArray<int>(ar, "blockLengths", blockLengths_, nBlock_);
107  for (int i=0; i < nBlock_; ++i) {
108  if (blockLengths_[i] < 1) {
109  UTIL_THROW("Invalid blockLength for Multiblock.");
110  }
111  }
112  atomTypes_.allocate(nBlock_);
113  loadDArray<int>(ar, "atomTypes", atomTypes_, nBlock_);
114  for (int i=0; i < nBlock_; ++i) {
115  if (atomTypes_[i] < 0) {
116  UTIL_THROW("Invalid atomType for Multiblock.");
117  }
118  }
119  blockBegin_.allocate(nBlock_);
120  for (int i = 0; i < nBlock_; ++i) {
121  if (i == 0) {
122  blockBegin_[i] = 0;
123  } else {
124  blockBegin_[i] = blockBegin_[i-1] + blockLengths_[i-1];
125  }
126  }
127  nAtom_ = 0;
128  for (int i=0; i < nBlock_; ++i) {
129  nAtom_ = nAtom_ + blockLengths_[i];
130  }
131 
132  nBond_ = nAtom_ - 1;
133  loadParameter<int>(ar, "bondType", bondType_);
134 
135  #ifdef SIMP_ANGLE
136  hasAngles_ = 0; // default value
137  loadParameter<int>(ar, "hasAngles", hasAngles_, false); // optional
138  if (hasAngles_) {
139  if (nAtom_ < 3) {
140  UTIL_THROW("Error: Cannot have angles with nAtom < 3");
141  }
142  nAngle_ = nAtom_ - 2;
143  loadParameter<int>(ar, "angleType", angleType_);
144  } else {
145  nAngle_ = 0;
146  }
147  #endif
148 
149  #ifdef SIMP_DIHEDRAL
150  hasDihedrals_ = 0; // default value
151  loadParameter<int>(ar, "hasDihedrals", hasDihedrals_, false); // optional
152  if (hasDihedrals_) {
153  if (nAtom_ < 4) {
154  UTIL_THROW("Error: Cannot have dihedrals with nAtom < 4");
155  }
156  nDihedral_ = nAtom_ - 3;
157  loadParameter<int>(ar, "dihedralType", dihedralType_);
158  } else {
159  nDihedral_ = 0;
160  }
161  #endif
162 
163  buildLinear();
164  }
165 
166  /*
167  * Save internal state to an archive.
168  */
170  {
171  ar << moleculeCapacity_;
172  ar & blockLengths_;
173  ar & atomTypes_;
174  ar & blockBegin_;
175  ar << bondType_;
176  #ifdef SIMP_ANGLE
178  if (hasAngles_ && nAngle_ > 0) {
179  ar << angleType_;
180  }
181  #endif
182  #ifdef SIMP_DIHEDRAL
184  if (hasDihedrals_ && nDihedral_ > 0) {
185  ar << dihedralType_;
186  }
187  #endif
188  }
189 
190  /*
191  * Return atomTypes_ for every atom.
192  */
193  int Multiblock::calculateAtomTypeId(int index) const
194  {
195  int type = -1;
196  for (int i = 0; i < nBlock_; ++i) {
197  if (index >= blockBegin_[i]) {
198  type = atomTypes_[i];
199  } else {
200  break;
201  }
202  }
203  assert(type >= 0);
204  return type;
205  }
206 
207  /*
208  * Return bondType_ for every bond.
209  */
210  int Multiblock::calculateBondTypeId(int molBondId) const
211  { return bondType_; }
212 
213  #ifdef SIMP_ANGLE
214  /*
215  * Return angleType_ for every angle.
216  */
217  int Multiblock::calculateAngleTypeId(int index) const
218  { return angleType_; }
219  #endif
220 
221  #ifdef SIMP_DIHEDRAL
222  /*
223  * Return dihedralType_ for every dihedral.
224  */
226  { return dihedralType_; }
227  #endif
228 
229 }
int moleculeCapacity_
Number of molecules associated with the species.
virtual void loadSpeciesParam(Serializable::IArchive &ar)
Load species structure from an Archive.
Definition: Multiblock.cpp:102
virtual int calculateDihedralTypeId(int index) const
Return same dihedral type for any dihedral in any chain.
Definition: Multiblock.cpp:225
Multiblock()
Default constructor.
Definition: Multiblock.cpp:19
File containing preprocessor macros for error handling.
Classes used by all simpatico molecular simulations.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
Definition: Multiblock.cpp:169
int hasAngles_
Does this chain have angle potentials (0 = false, 1 = true).
Definition: Linear.h:57
int nAtom_
Number of atoms per molecule.
Saving / output archive for binary ostream.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
DArray< int > atomTypes_
Particle type ids for each block.
Definition: Multiblock.h:54
virtual void readSpeciesParam(std::istream &in)
Read block lengths length and and types.
Definition: Multiblock.cpp:35
Utility classes for scientific computation.
Definition: accumulators.mod:1
int nBlock_
Number of particles in each block.
Definition: Multiblock.h:48
DArray< int > blockLengths_
Number of particles in each block.
Definition: Multiblock.h:51
int nDihedral_
Number of dihedrals per molecule.
virtual int calculateAngleTypeId(int index) const
Return same angle type for any angle in any chain.
Definition: Multiblock.cpp:217
int nAngle_
Number of angles per molecule.
virtual int calculateBondTypeId(int index) const
Return same bond type for any bond in any chain.
Definition: Multiblock.cpp:210
Saving archive for binary istream.
int hasDihedrals_
Does this chain have dihedral potentials (0 = false, 1 = true).
Definition: Linear.h:64
void buildLinear()
Build the chemical structure for a linear molecule.
Definition: Linear.cpp:39
void setClassName(const char *className)
Set class name string.
void allocate(int capacity)
Allocate the underlying C array.
Definition: DArray.h:191
static void saveOptional(Serializable::OArchive &ar, Type &value, bool isActive)
Save an optional parameter value to an output archive.
Definition: Parameter.h:224
int nBond_
Number of bonds per molecule.
A Species of linear polymers (abstract).
Definition: Linear.h:36
DArray< int > blockBegin_
Particle index for the beginning of each block.
Definition: Multiblock.h:57
virtual int calculateAtomTypeId(int index) const
Return the atom type for a specific atom.
Definition: Multiblock.cpp:193