Simpatico  v1.10
CfbLinearEndMove.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 "CfbLinearEndMove.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  CfbLinear(system),
32  nRegrow_(-1)
33  { setClassName("CfbLinearEndMove"); }
34 
35  /*
36  * Read parameters speciesId, nRegrow, and nTrial
37  */
38  void CfbLinearEndMove::readParameters(std::istream& in)
39  {
40  // Read parameters
41  readProbability(in);
43  read<int>(in, "nRegrow", nRegrow_);
44 
45  // Validate
46  if (nRegrow_ <= 0) {
47  UTIL_THROW("nRegrow_ <= 0");
48  }
49  int nAtom = system().simulation().species(speciesId()).nAtom();
50  if (nRegrow_ >= nAtom) {
51  UTIL_THROW("nRegrow_ >= nAtom");
52  }
53 
54  // Allocate
55  oldPos_.allocate(nRegrow_);
56  }
57 
58  /*
59  * Read parameters speciesId, nRegrow, and nTrial
60  */
62  {
63  // Read parameters
66  loadParameter<int>(ar, "nRegrow", nRegrow_);
67 
68  // Validate
69  if (nRegrow_ <= 0) {
70  UTIL_THROW("nRegrow_ <= 0");
71  }
72  int nAtom = system().simulation().species(speciesId()).nAtom();
73  if (nRegrow_ >= nAtom) {
74  UTIL_THROW("nRegrow_ >= nAtom");
75  }
76 
77  // Allocate array to store old positions
78  oldPos_.allocate(nRegrow_);
79  }
80 
81  /*
82  * Save state to archive.
83  */
85  {
86  McMove::save(ar);
87  CfbLinear::save(ar);
88  ar & nRegrow_;
89  }
90 
91  /*
92  * Generate, attempt and accept or reject a Monte Carlo move.
93  */
95  {
96  double rosenbluth, rosen_r, rosen_f;
97  double energy, energy_r, energy_f;
98  Molecule *molPtr; // pointer to randomly chosen molecule
99  Atom* atom0Ptr; // pointer to the current end atom
100  Atom* atom1Ptr; // pointer to "pivot" atom, which is bonded to atom0
101  int nAtom, sign, beginId, endId, atomId, i;
102  bool accept;
103 
105 
106  // Choose a molecule at random
107  molPtr = &(system().randomMolecule(speciesId()));
108  nAtom = molPtr->nAtom();
109 
110  // Require that chain nAtom > nRegrow
111  if (nRegrow_ >= nAtom) {
112  UTIL_THROW("nRegrow_ >= nAtom");
113  }
114 
115  // Choose which chain end to regrow
116  if (random().uniform(0.0, 1.0) > 0.5) {
117  sign = +1;
118  beginId = nAtom - nRegrow_;
119  endId = nAtom - 1;
120  } else {
121  sign = -1;
122  beginId = nRegrow_ - 1;
123  endId = 0;
124  }
125 
126  // Store current atomic positions from segment to be regrown
127  atomId = beginId;
128  for (i = 0; i < nRegrow_; ++i) {
129  oldPos_[i] = molPtr->atom(atomId).position();
130  atomId += sign;
131  }
132 
133  // Delete monomers, starting from chain end
134  rosen_r = 1.0;
135  energy_r = 0.0;
136  atomId = endId;
137  for (i = 0; i < nRegrow_; ++i) {
138  deleteAtom(*molPtr, atomId, sign, rosenbluth, energy);
139  #ifndef SIMP_NOPAIR
140  // Add end atom from cell list
141  system().pairPotential().deleteAtom(molPtr->atom(atomId));
142  #endif
143  rosen_r *= rosenbluth;
144  energy_r += energy;
145  atomId -= sign;
146  }
147  assert(atomId == beginId - sign);
148 
149  // Regrow monomers
150  rosen_f = 1.0;
151  energy_f = 0.0;
152  atomId = beginId;
153  for (i = 0; i < nRegrow_; ++i) {
154  atom0Ptr = &molPtr->atom(atomId);
155  atom1Ptr = atom0Ptr - sign;
156  addAtom(*molPtr, *atom0Ptr, *atom1Ptr, atomId, sign,
157  rosenbluth, energy);
158  rosen_f *= rosenbluth;
159  energy_f += energy;
160  #ifndef SIMP_NOPAIR
161  // Add end atom to cell list
162  system().pairPotential().addAtom(*atom0Ptr);
163  #endif
164  atomId += sign;
165  }
166  assert(atomId == endId + sign);
167 
168  // Accept or reject the move
169  accept = random().metropolis(rosen_f/rosen_r);
170  if (accept) {
171  // If accepted, keep current positions.
172  // Increment counter for accepted moves.
174  } else {
175  // If rejected, restore original atom positions
176  atomId = beginId;
177  for (i = 0; i < nRegrow_; ++i) {
178  atom0Ptr = &(molPtr->atom(atomId));
179  atom0Ptr->position() = oldPos_[i];
180  #ifndef SIMP_NOPAIR
181  system().pairPotential().updateAtomCell(*atom0Ptr);
182  #endif
183  atomId += sign;
184  }
185  }
186 
187  return accept;
188  }
189 }
A System for use in a Markov chain Monte Carlo simulation.
Definition: McSystem.h:52
void addAtom(Molecule &molecule, Atom &atom0, Atom &atom1, int atomId, int sign, double &rosenbluth, double &energy)
Configuration bias algorithm for adding an atom to a Linear molecule.
Definition: CfbLinear.cpp:240
void incrementNAttempt()
Increment the number of attempted moves.
Definition: McMove.h:191
int nAtom() const
Get number of atoms per molecule for this Species.
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.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
Definition: McMove.cpp:48
virtual void save(Serializable::OArchive &ar)
Save speciesId and nTrial.
Definition: CfbLinear.cpp:112
DArray< Vector > oldPos_
Array of old positions of temporarily deleted atoms (temporary).
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.
virtual void readParameters(std::istream &in)
Read and validate speciesId and nTrial.
Definition: CfbLinear.cpp:92
Base class for configuration bias (CFB) end regrowth moves.
Definition: CfbLinear.h:39
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
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
Simulation & simulation() const
Get the parent Simulation by reference.
Definition: System.h:1055
virtual bool move()
Generate and accept or reject configuration bias move.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
A point particle within a Molecule.
Utility classes for scientific computation.
Definition: accumulators.mod:1
virtual void readParameters(std::istream &in)
Read parameters from file.
int speciesId() const
Get speciesId.
Definition: CfbLinear.h:187
Random & random()
Get Random number generator of parent Simulation.
Definition: McMove.h:209
virtual void loadParameters(Serializable::IArchive &ar)
Load and validate speciesId and nTrial.
Definition: CfbLinear.cpp:102
McPairPotential & pairPotential() const
Return the McPairPotential by reference.
Definition: McSystem.h:388
void deleteAtom(Molecule &molecule, int atomId, int sign, double &rosenbluth, double &energy)
CFB algorithm for deleting an end atom from a Linear molecule.
Definition: CfbLinear.cpp:122
CfbLinearEndMove(McSystem &system)
Constructor.
Saving archive for binary istream.
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
A physical molecule (a set of covalently bonded Atoms).
const Vector & position() const
Get the position Vector by const reference.
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.
int nAtom() const
Get the number of Atoms in this Molecule.