Simpatico  v1.10
HybridNphMdMove.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 "HybridNphMdMove.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 #include <simp/ensembles/BoundaryEnsemble.h>
19 #include <simp/boundary/OrthorhombicBoundary.h>
20 
21 namespace McMd
22 {
23 
24  using namespace Util;
25  using namespace Simp;
26 
27  /*
28  * Constructor
29  */
31  SystemMove(system),
32  mdSystemPtr_(0),
33  nStep_(0),
34  nphIntegratorPtr_(0),
35  barostatMass_(0.0),
36  mode_()
37  {
38  setClassName("HybridNphMdMove");
39  mdSystemPtr_ = new MdSystem(system);
40  oldPositions_.allocate(simulation().atomCapacity());
41  }
42 
43  /*
44  * Destructor.
45  */
47  {
48  if (mdSystemPtr_) {
49  delete mdSystemPtr_;
50  }
51  }
52 
53  /*
54  * Read parameter maxDisp
55  */
56  void HybridNphMdMove::readParameters(std::istream& in)
57  {
58  readProbability(in);
59  read<int>(in, "nStep", nStep_);
60  readParamComposite(in, *mdSystemPtr_);
61 
62  nphIntegratorPtr_ = dynamic_cast<NphIntegrator*>(&mdSystemPtr_->mdIntegrator());
63 
64  barostatMass_ = nphIntegratorPtr_->barostatMass();
65  mode_ = nphIntegratorPtr_->mode();
66  }
67 
68  /*
69  * Load internal state from an archive.
70  */
72  {
74  loadParameter<int>(ar, "nStep", nStep_);
75  loadParamComposite(ar, *mdSystemPtr_);
76  nphIntegratorPtr_ = dynamic_cast<NphIntegrator*>(&mdSystemPtr_->mdIntegrator());
77 
78  barostatMass_ = nphIntegratorPtr_->barostatMass();
79  mode_ = nphIntegratorPtr_->mode();
80  }
81 
82  /*
83  * Save internal state to an archive.
84  */
86  {
87  McMove::save(ar);
88  ar << nStep_;
89  mdSystemPtr_->saveParameters(ar);
90  }
91 
92 
93  /*
94  * Generate, attempt and accept or reject a Hybrid MD/MC move.
95  */
97  {
99  Molecule::AtomIterator atomIter;
100  double oldEnergy, newEnergy;
101  int iSpec;
102  int nSpec = simulation().nSpecies();
103 
104  bool accept;
105 
106  if (nphIntegratorPtr_ == NULL) {
107  UTIL_THROW("null integrator pointer");
108  }
109  // Increment counter for attempted moves
111 
112  // Store old boundary lengths.
113  Vector oldLengths = system().boundary().lengths();
114 
115  // Store old atom positions in oldPositions_ array.
116  for (iSpec = 0; iSpec < nSpec; ++iSpec) {
117  mdSystemPtr_->begin(iSpec, molIter);
118  for ( ; molIter.notEnd(); ++molIter) {
119  for (molIter->begin(atomIter); atomIter.notEnd(); ++atomIter) {
120  oldPositions_[atomIter->id()] = atomIter->position();
121  }
122  }
123  }
124 
125  // Initialize MdSystem
126  #ifndef SIMP_NOPAIR
127  mdSystemPtr_->pairPotential().buildPairList();
128  #endif
129  mdSystemPtr_->calculateForces();
130  mdSystemPtr_->setBoltzmannVelocities(energyEnsemble().temperature());
131  nphIntegratorPtr_->setup();
132 
133  // generate integrator variables from a Gaussian distribution
135 
136  double temp = system().energyEnsemble().temperature();
137 
138  double volume = system().boundary().volume();
139 
140  if (mode_ == Cubic) {
141  // one degree of freedom
142  // barostat_energy = 1/2 (1/W) eta_x^2
143  double sigma = sqrt(temp/barostatMass_);
144  nphIntegratorPtr_->setEta(0, sigma*random.gaussian());
145  } else if (mode_ == Tetragonal) {
146  // two degrees of freedom
147  // barostat_energy = 1/2 (1/W) eta_x^2 + 1/2 (1/(2W)) eta_y^2
148  double sigma1 = sqrt(temp/barostatMass_);
149  nphIntegratorPtr_->setEta(0, sigma1*random.gaussian());
150  double sigma2 = sqrt(temp/barostatMass_/2.0);
151  nphIntegratorPtr_->setEta(1, sigma2*random.gaussian());
152  } else if (mode_ == Orthorhombic) {
153  // three degrees of freedom
154  // barostat_energy = 1/2 (1/W) (eta_x^2 + eta_y^2 + eta_z^2)
155  double sigma = sqrt(temp/barostatMass_);
156  nphIntegratorPtr_->setEta(0, sigma*random.gaussian());
157  nphIntegratorPtr_->setEta(1, sigma*random.gaussian());
158  nphIntegratorPtr_->setEta(2, sigma*random.gaussian());
159  }
160 
161  // Store old energy
162  oldEnergy = mdSystemPtr_->potentialEnergy();
163  oldEnergy += mdSystemPtr_->kineticEnergy();
164  oldEnergy += system().boundaryEnsemble().pressure()*volume;
165  oldEnergy += nphIntegratorPtr_->barostatEnergy();
166 
167  // Run a short MD simulation
168  for (int iStep = 0; iStep < nStep_; ++iStep) {
169  nphIntegratorPtr_->step();
170  }
171 
172  volume = system().boundary().volume();
173 
174  // Calculate new energy
175  newEnergy = mdSystemPtr_->potentialEnergy();
176  newEnergy += mdSystemPtr_->kineticEnergy();
177  newEnergy += system().boundaryEnsemble().pressure()*volume;
178  newEnergy += nphIntegratorPtr_->barostatEnergy();
179 
180  // Decide whether to accept or reject
181  accept = random.metropolis( boltzmann(newEnergy-oldEnergy) );
182 
183  // Accept move
184  if (accept) {
185 
186  #ifndef SIMP_NOPAIR
187  // Rebuild the McSystem cellList using the new positions.
189  #endif
190 
191  // Increment counter for the number of accepted moves.
193 
194  } else {
195 
196  // Restore old boundary lengths
197  system().boundary().setOrthorhombic(oldLengths);
198 
199  // Restore old atom positions
200  for (iSpec = 0; iSpec < nSpec; ++iSpec) {
201  mdSystemPtr_->begin(iSpec, molIter);
202  for ( ; molIter.notEnd(); ++molIter) {
203  molIter->begin(atomIter);
204  for ( ; atomIter.notEnd(); ++atomIter) {
205  atomIter->position() = oldPositions_[atomIter->id()];
206  }
207  }
208  }
209 
210  }
211 
212  return accept;
213 
214  }
215 
216 }
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
virtual void readParameters(std::istream &in)
Read nStep and MdSystem parameters from file.
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 void setup()
Setup private variables before main loop.
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
double pressure() const
Get the target pressure.
~HybridNphMdMove()
Destructor.
bool notEnd() const
Is the current pointer not at the end of the array?
Definition: ArrayIterator.h:83
double volume() const
Return unit cell volume.
void setOrthorhombic(const Vector &lengths)
Set unit cell dimensions for orthorhombic boundary.
const Vector & lengths() const
Get Vector of unit cell lengths by const reference.
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.
EnergyEnsemble & energyEnsemble() const
Get the EnergyEnsemble by reference.
Definition: System.h:1095
virtual double barostatEnergy()
Get the barostat energy.
MdIntegrator & mdIntegrator()
Return the MdIntegrator by reference.
Definition: MdSystem.h:660
virtual void step()
Take a complete NVE MD integration step.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
Definition: McMove.cpp:48
BoundaryEnsemble & boundaryEnsemble() const
Get the BoundaryEnsemble by reference.
Definition: System.h:1104
double potentialEnergy()
Compute and return total potential energy.
Definition: MdSystem.cpp:910
Classes used by all simpatico molecular simulations.
bool move()
Generate, attempt and accept or reject a move.
virtual LatticeSystem mode() const
Get the integrator mode.
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
#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
void incrementNAccept()
Increment the number of accepted moves.
Definition: McMove.h:197
virtual double barostatMass() const
Get the barostat mass.
HybridNphMdMove(McSystem &system)
Constructor.
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
An McMove that acts on one McSystem.
Definition: SystemMove.h:28
double gaussian(void)
Return a Gaussian random number with zero average and unit variance.
Definition: Random.cpp:92
virtual void setEta(unsigned int index, double eta)
Get the barostat momentum.
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
An explictly reversible/measure-preserving Parrinello-Rahman type NPH integrator. ...
Boundary & boundary() const
Get the Boundary by reference.
Definition: System.h:1064
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
Random number generator.
Definition: Random.h:46
Simulation & simulation()
Get parent Simulation object.
Definition: McMove.h:203
double temperature() const
Return the temperature.
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
Random & random()
Get the random number generator by reference.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.