Simpatico  v1.10
CfbEndMove.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 "CfbEndMove.h"
9 #include <mcMd/mcSimulation/McSystem.h>
10 #include <mcMd/simulation/Simulation.h>
11 #include <mcMd/chemistry/Molecule.h>
12 #include <mcMd/chemistry/Bond.h>
13 #include <mcMd/chemistry/Atom.h>
14 #ifndef SIMP_NOPAIR
15 #include <mcMd/potentials/pair/McPairPotential.h>
16 #endif
17 #include <simp/species/Linear.h>
18 #include <simp/boundary/Boundary.h>
19 #include <util/global.h>
20 
21 namespace McMd
22 {
23 
24  using namespace Util;
25  using namespace Simp;
26 
27  /*
28  * Constructor
29  */
31  CfbEndBase(system),
32  speciesId_(-1),
33  nRegrow_(-1)
34  { setClassName("CfbEndMove"); }
35 
36  /*
37  * Read parameters speciesId, nRegrow, and nTrial
38  */
39  void CfbEndMove::readParameters(std::istream& in)
40  {
41  // Read parameters
42  readProbability(in);
43  read<int>(in, "speciesId", speciesId_);
44  read<int>(in, "nRegrow", nRegrow_);
45  read<int>(in, "nTrial", nTrial_);
46 
47  // Validate
48  if (nTrial_ <=0 || nTrial_ > MaxTrial_) {
49  UTIL_THROW("Invalid value input for nTrial");
50  }
51  Linear* chainPtr;
52  chainPtr = dynamic_cast<Linear*>(&(simulation().species(speciesId_)));
53  if (!chainPtr) {
54  UTIL_THROW("Species is not a subclass of Linear");
55  }
56 
57  // Allocate
58  oldPos_.allocate(nRegrow_);
59  }
60 
61  /*
62  * Read parameters speciesId, nRegrow, and nTrial
63  */
65  {
66  // Read parameters
68  loadParameter<int>(ar, "speciesId", speciesId_);
69  loadParameter<int>(ar, "nRegrow", nRegrow_);
70  loadParameter<int>(ar, "nTrial", nTrial_);
71 
72  // Validate
73  if (nTrial_ <=0 || nTrial_ > MaxTrial_) {
74  UTIL_THROW("Invalid value input for nTrial");
75  }
76  Linear* chainPtr;
77  chainPtr = dynamic_cast<Linear*>(&(simulation().species(speciesId_)));
78  if (!chainPtr) {
79  UTIL_THROW("Species is not a subclass of Linear");
80  }
81 
82  // Allocate array to store old positions
83  oldPos_.allocate(nRegrow_);
84  }
85 
86  /*
87  * Save state to archive.
88  */
90  {
91  McMove::save(ar);
92  ar & speciesId_;
93  ar & nRegrow_;
94  ar & nTrial_;
95  }
96 
97  /*
98  * Generate, attempt and accept or reject a Monte Carlo move.
99  */
101  {
102  double rosenbluth, rosen_r, rosen_f;
103  double energy, energy_r, energy_f;
104  Atom *endPtr; // pointer to the current end atom
105  Atom *pvtPtr; // pointer to "pivot" atom, which is bonded to the end atom
106  Molecule *molPtr; // pointer to randomly chosen molecule
107  int length, sign, beginId, endId, bondType, i;
108  bool accept;
109 
111 
112  // Choose a molecule at random
113  molPtr = &(system().randomMolecule(speciesId_));
114  length = molPtr->nAtom();
115 
116  // Require that chain length > nRegrow
117  if (nRegrow_ >= length) {
118  UTIL_THROW("nRegrow_ >= chain length");
119  }
120 
121  // Choose which chain end to regrow
122  if (random().uniform(0.0, 1.0) > 0.5) {
123  sign = +1;
124  beginId = length - nRegrow_;
125  endId = length - 1;
126  } else {
127  sign = -1;
128  beginId = nRegrow_ - 1;
129  endId = 0;
130  }
131 
132  // Store current atomic positions from segment to be regrown
133  endPtr = &(molPtr->atom(beginId));
134  for (i = 0; i < nRegrow_; ++i) {
135  oldPos_[i] = endPtr->position();
136  endPtr += sign;
137  }
138 
139  // Delete monomers, starting from chain end
140  rosen_r = 1.0;
141  energy_r = 0.0;
142  endPtr = &(molPtr->atom(endId));
143  for (i = 0; i < nRegrow_; ++i) {
144  pvtPtr = endPtr - sign;
145  if (sign == 1) {
146  bondType = molPtr->bond(endId - 1 - i).typeId();
147  } else {
148  bondType = molPtr->bond(i).typeId();
149  }
150  deleteEndAtom(endPtr, pvtPtr, bondType, rosenbluth, energy);
151  #ifndef SIMP_NOPAIR
152  system().pairPotential().deleteAtom(*endPtr);
153  #endif
154  rosen_r *= rosenbluth;
155  energy_r += energy;
156  endPtr -= sign;
157  }
158 
159  // Regrow monomers
160  rosen_f = 1.0;
161  energy_f = 0.0;
162  endPtr = &(molPtr->atom(beginId));
163  for (i = 0; i < nRegrow_; ++i) {
164  pvtPtr = endPtr - sign;
165  if (sign == 1) {
166  bondType = molPtr->bond(endId - 1 - i).typeId();
167  } else {
168  bondType = molPtr->bond(i).typeId();
169  }
170  addEndAtom(endPtr, pvtPtr, bondType, rosenbluth, energy);
171  rosen_f *= rosenbluth;
172  energy_f += energy;
173 
174  #ifndef SIMP_NOPAIR
175  // Add end atom to McSystem cell list
176  system().pairPotential().addAtom(*endPtr);
177  #endif
178 
179  endPtr += sign;
180  }
181 
182  // Decide whether to accept or reject
183  accept = random().metropolis(rosen_f/rosen_r);
184  if (accept) {
185 
186  // Increment counter for accepted moves of this class.
188 
189  // If the move is accepted, keep current positions.
190 
191  } else {
192 
193  // If the move is rejected, restore old positions
194  endPtr = &(molPtr->atom(beginId));
195  for (i = 0; i < nRegrow_; ++i) {
196  // system().moveAtom(*endPtr, oldPos_[i]);
197  endPtr->position() = oldPos_[i];
198  #ifndef SIMP_NOPAIR
199  system().pairPotential().updateAtomCell(*endPtr);
200  #endif
201  endPtr += sign;
202  }
203 
204  }
205 
206  return accept;
207 
208  }
209 }
static const int MaxTrial_
Maximum allowed number of trial positions for a regrown atom.
Definition: CfbEndBase.h:111
Bond & bond(int localId)
Get a specific Bond in this Molecule by non-const reference.
A System for use in a Markov chain Monte Carlo simulation.
Definition: McSystem.h:52
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
Definition: CfbEndMove.cpp:89
void addEndAtom(Atom *endPtr, Atom *pvtPtr, int bondType, double &rosenbluth, double &energy)
Configuration bias algorithm for adding an atom to a chain end.
Definition: CfbEndBase.cpp:189
virtual void readParameters(std::istream &in)
Read species to which displacement is applied.
Definition: CfbEndMove.cpp:39
void incrementNAttempt()
Increment the number of attempted moves.
Definition: McMove.h:191
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
Definition: McMove.cpp:58
void addAtom(Atom &atom)
Add an Atom to the CellList.
int nRegrow_
Number of particles at end to attempt to regrow.
Definition: CfbEndMove.h:75
int typeId() const
Get the typeId for this covalent group.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
Definition: McMove.cpp:48
CfbEndMove(McSystem &system)
Constructor.
Definition: CfbEndMove.cpp:30
File containing preprocessor macros for error handling.
virtual bool move()
Generate and accept or reject configuration bias move.
Definition: CfbEndMove.cpp:100
Classes used by all simpatico molecular simulations.
DArray< Vector > oldPos_
Array of old positions of temporarily deleted atoms (temporary).
Definition: CfbEndMove.h:69
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
McSystem & system()
Get parent McSystem.
Definition: SystemMove.h:82
int speciesId_
Integer index for molecular species.
Definition: CfbEndMove.h:72
Molecule & randomMolecule(int speciesId)
Get a random Molecule of a specified species in this System.
Definition: System.cpp:1200
void incrementNAccept()
Increment the number of accepted moves.
Definition: McMove.h:197
A point particle within a Molecule.
Utility classes for scientific computation.
Definition: accumulators.mod:1
Random & random()
Get Random number generator of parent Simulation.
Definition: McMove.h:209
McPairPotential & pairPotential() const
Return the McPairPotential by reference.
Definition: McSystem.h:388
void deleteEndAtom(Atom *endPtr, Atom *pvtPtr, int bondType, double &rosenbluth, double &energy)
CFB algorithm for deleting an end atom from a flexible chain.
Definition: CfbEndBase.cpp:62
Saving archive for binary istream.
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
Definition: CfbEndMove.cpp:64
void readProbability(std::istream &in)
Read the probability from file.
Definition: McMove.cpp:42
void setClassName(const char *className)
Set class name string.
const Atom & atom(int localId) const
Get a specific Atom in this Molecule.
int nTrial_
Actual number of trial positions for each regrown atom.
Definition: CfbEndBase.h:114
bool metropolis(double ratio)
Metropolis algorithm for whether to accept a MC move.
Definition: Random.h:260
Simulation & simulation()
Get parent Simulation object.
Definition: McMove.h:203
A physical molecule (a set of covalently bonded Atoms).
const Vector & position() const
Get the position Vector by const reference.
Base class for configuration bias (CFB) end regrowth moves.
Definition: CfbEndBase.h:31
Species & species(int i)
Get a specific Species by reference.
void deleteAtom(Atom &atom)
Remove an Atom from the CellList.
void updateAtomCell(Atom &atom)
Update the cell list to reflect a new position.
A Species of linear polymers (abstract).
Definition: Linear.h:36
int nAtom() const
Get the number of Atoms in this Molecule.