Simpatico  v1.10
RigidDisplaceMove.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 "RigidDisplaceMove.h"
9 #include <mcMd/simulation/Simulation.h>
10 #include <mcMd/mcSimulation/McSystem.h>
12 #include <mcMd/chemistry/Molecule.h>
13 #include <mcMd/chemistry/Atom.h>
14 
15 #include <simp/species/Species.h>
16 #include <simp/boundary/Boundary.h>
17 
18 #include <util/space/Dimension.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  : SystemMove(system),
32  delta_(0.0),
33  speciesId_(-1)
34  { setClassName("RigidDisplaceMove"); }
35 
36  /*
37  * Read speciesId and delta.
38  */
39  void RigidDisplaceMove::readParameters(std::istream& in)
40  {
41  readProbability(in);
42  read<int>(in, "speciesId", speciesId_);
43  read<double>(in, "delta", delta_);
44  readBlank(in);
45 
46  nAtom_ = simulation().species(speciesId_).nAtom();
47  oldPositions_.allocate(nAtom_);
48  }
49 
50  /*
51  * Load internal state from an archive.
52  */
54  {
56  loadParameter<int>(ar, "speciesId", speciesId_);
57  loadParameter<double>(ar, "delta", delta_);
58  ar & nAtom_;
59  if (nAtom_ != simulation().species(speciesId_).nAtom()) {
60  UTIL_THROW("Inconsistent values for nAtom.");
61  }
62  oldPositions_.allocate(nAtom_);
63  }
64 
65  /*
66  * Save internal state to an archive.
67  */
69  {
70  McMove::save(ar);
71  ar & speciesId_;
72  ar & delta_;
73  ar & nAtom_;
74  }
75 
76  /*
77  * Generate, attempt and accept or reject a rigid molecule translation.
78  */
80  {
81  Vector oldPos, newPos, dr;
82  double newEnergy, oldEnergy;
83  Molecule* molPtr;
84  Atom* atomPtr;
85  int iAtom, j;
86 
88 
89  // Choose a molecule at random
90  molPtr = &(system().randomMolecule(speciesId_));
91 
92  // Calculate current energy and store old positions.
93  // Ignore covalent bonding energy, because it won't change.
94  oldEnergy = 0.0;
95  for (iAtom = 0; iAtom < nAtom_; ++iAtom) {
96  atomPtr = &molPtr->atom(iAtom);
97  oldPositions_[iAtom] = atomPtr->position();
98  #ifndef SIMP_NOPAIR
99  oldEnergy += system().pairPotential().atomEnergy(*atomPtr);
100  #endif
101  #ifdef SIMP_EXTERNAL
102  if (system().hasExternalPotential()) {
103  oldEnergy += system().externalPotential().atomEnergy(*atomPtr);
104  }
105  #endif
106  #ifdef SIMP_TETHER
107  oldEnergy += system().atomTetherEnergy(*atomPtr);
108  #endif
109  }
110 
111  // Generate trial displacement Vector dr
112  for (j = 0; j < Dimension; ++j) {
113  dr[j] = random().uniform(-delta_, delta_);
114  }
115 
116  // Move every atom by dr and calculate new trial energy.
117  newEnergy = 0.0;
118  for (iAtom = 0; iAtom < nAtom_; ++iAtom) {
119  atomPtr = &molPtr->atom(iAtom);
120  atomPtr->position() += dr;
121  boundary().shift(atomPtr->position());
122  #ifndef SIMP_NOPAIR
123  newEnergy += system().pairPotential().atomEnergy(*atomPtr);
124  #endif
125  #ifdef SIMP_EXTERNAL
126  if (system().hasExternalPotential()) {
127  newEnergy += system().externalPotential().atomEnergy(*atomPtr);
128  }
129  #endif
130  #ifdef SIMP_TETHER
131  newEnergy += system().atomTetherEnergy(*atomPtr);
132  #endif
133  }
134 
135  // Decide whether to accept the move
136  bool accept = random().metropolis(boltzmann(newEnergy - oldEnergy));
137 
138  if (accept) {
139 
140  #ifndef SIMP_NOPAIR
141  // Update cell
142  for (iAtom = 0; iAtom < nAtom_; ++iAtom) {
143  system().pairPotential().updateAtomCell(molPtr->atom(iAtom));
144  }
145  #endif
146 
148 
149  } else {
150 
151  // Return atoms to original positions
152  for (iAtom = 0; iAtom < nAtom_; ++iAtom) {
153  molPtr->atom(iAtom).position() = oldPositions_[iAtom];
154  }
155 
156  }
157  return accept;
158  }
159 
160 }
const int Dimension
Dimensionality of space.
Definition: Dimension.h:19
A System for use in a Markov chain Monte Carlo simulation.
Definition: McSystem.h:52
A Vector is a Cartesian vector.
Definition: Vector.h:75
virtual double atomEnergy(const Atom &atom) const =0
Calculate the external energy for one Atom.
void incrementNAttempt()
Increment the number of attempted moves.
Definition: McMove.h:191
int nAtom() const
Get number of atoms per molecule for this Species.
Include this file to include all MC potential energy classes at once.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
Definition: McMove.cpp:58
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.
double uniform()
Return a random floating point number x, uniformly distributed in the range 0 <= x < 1...
Definition: Random.h:203
Blank & readBlank(std::istream &in)
Add and read a new Blank object, representing a blank line.
virtual bool move()
Generate, attempt and accept or reject a move.
Saving / output archive for binary ostream.
double boltzmann(double energy)
Boltzmann weight associated with an energy difference.
Definition: SystemMove.h:100
#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
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
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
ExternalPotential & externalPotential() const
Return ExternalPotential by reference.
Definition: McSystem.h:473
A point particle within a Molecule.
Utility classes for scientific computation.
Definition: accumulators.mod:1
An McMove that acts on one McSystem.
Definition: SystemMove.h:28
void shift(Vector &r) const
Shift Cartesian Vector r to its primary image.
Random & random()
Get Random number generator of parent Simulation.
Definition: McMove.h:209
RigidDisplaceMove(McSystem &system)
Constructor.
McPairPotential & pairPotential() const
Return the McPairPotential by reference.
Definition: McSystem.h:388
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
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
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.
Species & species(int i)
Get a specific Species by reference.
void updateAtomCell(Atom &atom)
Update the cell list to reflect a new position.
virtual double atomEnergy(const Atom &atom) const =0
Calculate the nonbonded pair energy for one Atom.
Boundary & boundary()
Get Boundary object of parent McSystem.
Definition: SystemMove.h:88
virtual void readParameters(std::istream &in)
Read species to which displacement is applied.