Simpatico  v1.10
CfbRebridgeMove.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 "CfbRebridgeMove.h"
9 #include <mcMd/mcSimulation/McSystem.h>
10 #include <mcMd/simulation/Simulation.h>
11 #ifndef SIMP_NOPAIR
12 #include <mcMd/potentials/pair/McPairPotential.h>
13 #endif
14 #include <mcMd/chemistry/Molecule.h>
15 #include <mcMd/chemistry/Bond.h>
16 #include <mcMd/chemistry/Atom.h>
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  CfbRebridgeBase(system),
32  speciesId_(-1),
33  nRegrow_(-1)
34  { setClassName("CfbRebridgeMove"); }
35 
36  /*
37  * Read parameters speciesId, nRegrow, and nTrial
38  */
39  void CfbRebridgeMove::readParameters(std::istream& in)
40  {
41  // Read parameters
42  readProbability(in);
43  read<int>(in, "speciesId", speciesId_);
44  read<int>(in, "nRegrow", nRegrow_);
45 
46  // Read parameters hold by RebridgeBase
48 
49  // Validate
50  Linear* chainPtr;
51  chainPtr = dynamic_cast<Linear*>(&(simulation().species(speciesId_)));
52  if (!chainPtr) {
53  UTIL_THROW("Not a Linear species");
54  }
55 
56  // Initialize tables for spring constants and normalizations.
57  setup();
58 
59  // Allocate array to store old positions
60  oldPos_.allocate(nRegrow_);
61  }
62 
63  /*
64  * Read parameters speciesId, nRegrow, and nTrial
65  */
67  {
68  // Read parameters
70  loadParameter<int>(ar, "speciesId", speciesId_);
71  loadParameter<int>(ar, "nRegrow", nRegrow_);
73 
74  // Validate
75  Linear* chainPtr;
76  chainPtr = dynamic_cast<Linear*>(&(simulation().species(speciesId_)));
77  if (!chainPtr) {
78  UTIL_THROW("Species is not a subclass of Linear");
79  }
80 
81  // Initialize tables for spring constants and normalizations.
82  setup();
83  oldPos_.allocate(nRegrow_);
84  }
85 
86  /*
87  * Save state to archive.
88  */
90  {
91  McMove::save(ar);
92  ar & speciesId_;
93  ar & nRegrow_;
95  }
96 
97  /*
98  * Generate, attempt and accept or reject a Monte Carlo move.
99  *
100  * Convention for index
101  * head of molecule tail of molecule
102  * | |
103  * 0 1 2 3 ... length-1
104  * o---o---o---o---o---o---o---o---o---o
105  * |_______________|___________|
106  * valid beginId nRegrow
107  *
108  */
110  {
111  Molecule *molPtr; // pointer to randomly chosen molecule
112  Atom *thisPtr; // pointer to the current end atom
113  int length, sign, beginId, endId, i;
114  int *bonds;
115  double rosen_r, rosen_f;
116  double energy_r, energy_f;
117  bool accept;
118 
120 
121  // Choose a molecule at random
122  molPtr = &(system().randomMolecule(speciesId_));
123  length = molPtr->nAtom();
124 
125  // Require that chain length - 2 >= nRegrow_
126  if (nRegrow_ > length - 2) {
127  UTIL_THROW("nRegrow_ >= chain length");
128  }
129 
130  // Allocate bonds array
131  bonds = new int[nRegrow_ + 1];
132 
133  // Choose the beginId of the growing interior bridge
134  beginId = random().uniformInt(1, length - nRegrow_);
135 
136  // Choose direction: sign = +1 if beginId < endId
137  if (random().uniform(0.0, 1.0) > 0.5) {
138  sign = +1;
139  endId = beginId + (nRegrow_ - 1);
140  } else {
141  sign = -1;
142  endId = beginId;
143  beginId = endId + (nRegrow_ - 1);
144  }
145 
146  // Store current atomic positions from segment to be regrown
147  thisPtr = &(molPtr->atom(beginId));
148  for (i = 0; i < nRegrow_; ++i) {
149  oldPos_[i] = thisPtr->position();
150  thisPtr += sign;
151  }
152 
153  // Get bond types.
154  if (sign == +1) {
155  for (i = 0; i < nRegrow_ + 1; ++i)
156  bonds[i] = molPtr->bond(endId - i).typeId();
157  } else {
158  for (i = 0; i < nRegrow_ + 1; ++i)
159  bonds[i] = molPtr->bond(endId - 1 + i).typeId();
160  }
161 
162  // Delete atoms: endId -> beginId
163  thisPtr = &(molPtr->atom(endId));
164  deleteSequence(nRegrow_, sign, thisPtr, bonds, rosen_r, energy_r);
165 
166  // Regrow atoms: endId -> beginId
167  thisPtr = &(molPtr->atom(beginId));
168  addSequence(nRegrow_, sign, thisPtr, bonds, rosen_f, energy_f);
169 
170  // Release bonds array
171  delete [] bonds;
172 
173  // Decide whether to accept or reject
174  accept = random().metropolis(rosen_f/rosen_r);
175  if (accept) {
176 
177  // Increment counter for accepted moves of this class.
179 
180  // If the move is accepted, keep current positions.
181 
182  } else {
183 
184  // If the move is rejected, restore old positions
185  thisPtr = &(molPtr->atom(beginId));
186  for (i = 0; i < nRegrow_; ++i) {
187  //system().moveAtom(*thisPtr, oldPos_[i]);
188  thisPtr->position() = oldPos_[i];
189  #ifndef SIMP_NOPAIR
190  system().pairPotential().updateAtomCell(*thisPtr);
191  #endif
192  thisPtr += sign;
193  }
194 
195  }
196 
197  return accept;
198  }
199 }
virtual void setup()
Initialize the arrays for preferential angle, effective spring constant and normalization factor...
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 loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
void incrementNAttempt()
Increment the number of attempted moves.
Definition: McMove.h:191
CfbRebridgeMove(McSystem &system)
Constructor.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
Definition: McMove.cpp:58
virtual void readParameters(std::istream &in)
Read species type, nTrial, and parameters needed to evaluate the orientation bias.
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
File containing preprocessor macros for error handling.
Classes used by all simpatico molecular simulations.
Base class configuration bias moves internal segment regrowth moves.
virtual void readParameters(std::istream &in)
Read species to which displacement is applied.
Saving / output archive for binary ostream.
void addSequence(int nActive, int sign, Atom *beginPtr, int *bonds, double &rosenbluth, double &energy)
Configuration bias algorithm for adding a consequtive sequence of atoms.
#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
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
int speciesId_
Integer index for molecular species.
A point particle within a Molecule.
Utility classes for scientific computation.
Definition: accumulators.mod:1
long uniformInt(long range1, long range2)
Return random long int x uniformly distributed in range1 <= x < range2.
Definition: Random.h:224
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
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
void deleteSequence(int nActive, int sign, Atom *endPtr, int *bonds, double &rosenbluth, double &energy)
Configuration bias algorithm for deleting a consequtive sequence of atoms.
Saving archive for binary istream.
int nRegrow_
Number of particles at end to attempt to regrow.
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
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.
bool metropolis(double ratio)
Metropolis algorithm for whether to accept a MC move.
Definition: Random.h:260
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
Simulation & simulation()
Get parent Simulation object.
Definition: McMove.h:203
A physical molecule (a set of covalently bonded Atoms).
DArray< Vector > oldPos_
Array of old positions of temporarily deleted atoms.
virtual bool move()
Generate and accept or reject configuration bias move.
Species & species(int i)
Get a specific Species by reference.
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.