Simpatico  v1.10
HybridMdMove.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 "HybridMdMove.h"
9 #include <mcMd/mcSimulation/McSystem.h>
10 #include <mcMd/mdIntegrators/MdIntegrator.h>
11 #include <mcMd/mdSimulation/MdSystem.h>
12 #include <mcMd/simulation/Simulation.h>
13 #include <mcMd/chemistry/Atom.h>
14 #ifndef SIMP_NOPAIR
15 #include <mcMd/potentials/pair/MdPairPotential.h>
16 #include <mcMd/potentials/pair/McPairPotential.h>
17 #endif
18 
19 namespace McMd
20 {
21 
22  using namespace Util;
23 
24  /*
25  * Constructor
26  */
28  SystemMove(system),
29  mdSystemPtr_(0),
30  nStep_(0)
31  {
32  setClassName("HybridMdMove");
33  mdSystemPtr_ = new MdSystem(system);
34  oldPositions_.allocate(simulation().atomCapacity());
35  }
36 
37  /*
38  * Destructor.
39  */
41  {
42  if (mdSystemPtr_) {
43  delete mdSystemPtr_;
44  }
45  }
46 
47  /*
48  * Read parameter maxDisp
49  */
50  void HybridMdMove::readParameters(std::istream& in)
51  {
52  readProbability(in);
53  read<int>(in, "nStep", nStep_);
54  readParamComposite(in, *mdSystemPtr_);
55  }
56 
57  /*
58  * Load internal state from an archive.
59  */
61  {
63  loadParameter<int>(ar, "nStep", nStep_);
64  loadParamComposite(ar, *mdSystemPtr_);
65  }
66 
67  /*
68  * Save internal state to an archive.
69  */
71  {
72  McMove::save(ar);
73  ar << nStep_;
74  mdSystemPtr_->saveParameters(ar);
75  }
76 
77  /*
78  * Generate, attempt and accept or reject a Hybrid MD/MC move.
79  */
81  {
83  Molecule::AtomIterator atomIter;
84  double oldEnergy, newEnergy;
85  int iSpec;
86  int nSpec = simulation().nSpecies();
87  bool accept;
88 
90 
91  // Store old atom positions in oldPositions_ array.
92  for (iSpec = 0; iSpec < nSpec; ++iSpec) {
93  mdSystemPtr_->begin(iSpec, molIter);
94  for ( ; molIter.notEnd(); ++molIter) {
95  for (molIter->begin(atomIter); atomIter.notEnd(); ++atomIter) {
96  oldPositions_[atomIter->id()] = atomIter->position();
97  }
98  }
99  }
100 
101  // Initialize MdSystem
102  #ifndef SIMP_NOPAIR
103  mdSystemPtr_->pairPotential().buildPairList();
104  #endif
105  mdSystemPtr_->calculateForces();
106  mdSystemPtr_->setBoltzmannVelocities(energyEnsemble().temperature());
107  mdSystemPtr_->mdIntegrator().setup();
108 
109  // Store old energy
110  oldEnergy = mdSystemPtr_->potentialEnergy();
111  oldEnergy += mdSystemPtr_->kineticEnergy();
112 
113  // Run a short MD simulation
114  for (int iStep = 0; iStep < nStep_; ++iStep) {
115  mdSystemPtr_->mdIntegrator().step();
116  }
117 
118  // Calculate new energy
119  newEnergy = mdSystemPtr_->potentialEnergy();
120  newEnergy += mdSystemPtr_->kineticEnergy();
121 
122  // Decide whether to accept or reject
123  accept = random().metropolis( boltzmann(newEnergy-oldEnergy) );
124 
125  // Accept move
126  if (accept) {
127 
128  #ifndef SIMP_NOPAIR
129  // Rebuild the McSystem cellList using the new positions.
131  #endif
132 
133  // Increment counter for the number of accepted moves.
135 
136  } else {
137 
138  // Restore old atom positions
139  for (iSpec = 0; iSpec < nSpec; ++iSpec) {
140  mdSystemPtr_->begin(iSpec, molIter);
141  for ( ; molIter.notEnd(); ++molIter) {
142  molIter->begin(atomIter);
143  for ( ; atomIter.notEnd(); ++atomIter) {
144  atomIter->position() = oldPositions_[atomIter->id()];
145  }
146  }
147  }
148 
149  }
150 
151  return accept;
152 
153  }
154 
155 }
A System for use in a Markov chain Monte Carlo simulation.
Definition: McSystem.h:52
void buildCellList()
Build the CellList with current configuration.
void calculateForces()
Compute all forces in this System.
Definition: MdSystem.cpp:857
void incrementNAttempt()
Increment the number of attempted moves.
Definition: McMove.h:191
void begin(int speciesId, MoleculeIterator &iterator)
Initialize an iterator for molecules of one species in this System.
Definition: System.h:1147
bool notEnd() const
Is the current pointer not at the end of the array?
Definition: ArrayIterator.h:83
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
Definition: McMove.cpp:58
void loadParamComposite(Serializable::IArchive &ar, ParamComposite &child, bool next=true)
Add and load a required child ParamComposite.
MdIntegrator & mdIntegrator()
Return the MdIntegrator by reference.
Definition: MdSystem.h:660
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
Definition: McMove.cpp:48
virtual void readParameters(std::istream &in)
Read nStep, dt, skin, maxNPair from file.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
double potentialEnergy()
Compute and return total potential energy.
Definition: MdSystem.cpp:910
virtual void setup()
Initialize internal state, if any.
Definition: MdIntegrator.h:53
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
MdPairPotential & pairPotential() const
Return MdPairPotential by reference.
Definition: MdSystem.h:520
McSystem & system()
Get parent McSystem.
Definition: SystemMove.h:82
void incrementNAccept()
Increment the number of accepted moves.
Definition: McMove.h:197
bool notEnd() const
Is the current pointer not at the end of the PArray?
Utility classes for scientific computation.
Definition: accumulators.mod:1
void setBoltzmannVelocities(double temperature)
Set all velocities to Boltzmann distributed random values.
Definition: MdSystem.cpp:797
~HybridMdMove()
Destructor.
An McMove that acts on one McSystem.
Definition: SystemMove.h:28
Forward iterator for an Array or a C array.
Definition: ArrayIterator.h:39
double kineticEnergy() const
Compute and return total kinetic energy.
Definition: MdSystem.cpp:1003
Forward iterator for a PArray.
Definition: ArraySet.h:19
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
HybridMdMove(McSystem &system)
Constructor.
Saving archive for binary istream.
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
int nSpecies() const
Get the number of Species in this Simulation.
void buildPairList()
Build the internal PairList.
void readProbability(std::istream &in)
Read the probability from file.
Definition: McMove.cpp:42
void setClassName(const char *className)
Set class name string.
void readParamComposite(std::istream &in, ParamComposite &child, bool next=true)
Add and read a required child ParamComposite.
bool metropolis(double ratio)
Metropolis algorithm for whether to accept a MC move.
Definition: Random.h:260
A System for Molecular Dynamics simulation.
Definition: MdSystem.h:68
Simulation & simulation()
Get parent Simulation object.
Definition: McMove.h:203
virtual void step()=0
Take a complete MD integration step.
EnergyEnsemble & energyEnsemble()
Get EnergyEnsemble object of parent McSystem.
Definition: SystemMove.h:94
virtual void saveParameters(Serializable::OArchive &ar)
Save parameters to an archive, without configuration.
Definition: MdSystem.cpp:582
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.