Simpatico  v1.10
HomopolymerSemiGrandMove.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 "HomopolymerSemiGrandMove.h"
9 #include <mcMd/simulation/Simulation.h>
10 #include <mcMd/mcSimulation/McSystem.h>
11 #ifndef SIMP_NOPAIR
12 #include <mcMd/potentials/pair/McPairPotential.h>
13 #endif
14 #include <mcMd/species/HomopolymerSG.h>
15 #include <util/global.h>
16 
17 namespace McMd
18 {
19 
20  using namespace Util;
21 
22  /*
23  * Constructor
24  */
26  SystemMove(system),
27  speciesId_(-1)
28  { setClassName("HomopolymerSemiGrandMove"); }
29 
30  /*
31  * Read parameter speciesId.
32  */
34  {
35  // Read parameters
36  readProbability(in);
37  read<int>(in, "speciesId", speciesId_);
38 
39  // Cast the Species to HomopolymerSG
40  speciesPtr_ = dynamic_cast<HomopolymerSG*>(&(simulation().species(speciesId_)));
41  if (!speciesPtr_) {
42  UTIL_THROW("Error: Species must be HomopolymerSG");
43  }
44 
45  }
46 
47  /*
48  * Load state from an archive.
49  */
51  {
53  loadParameter<int>(ar, "speciesId", speciesId_);
54 
55  // Cast the Species to HomopolymerSG
56  speciesPtr_ = dynamic_cast<HomopolymerSG*>(&(simulation().species(speciesId_)));
57  if (!speciesPtr_) {
58  UTIL_THROW("Species is not a HomopolymerSG");
59  }
60  }
61 
62  /*
63  * Save state to an archive.
64  */
66  {
67  McMove::save(ar);
68  ar & speciesId_;
69  }
70 
71  /*
72  * Generate, attempt and accept or reject a Monte Carlo move.
73  */
75  {
78 
79  #ifndef SIMP_NOPAIR
80  // Calculate pair energy for the chosen molecule
81  double oldEnergy = system().pairPotential().moleculeEnergy(molecule);
82  #endif
83 
84  // Toggle state of the molecule
85  int oldStateId = speciesPtr_->mutator().moleculeStateId(molecule);
86  int newStateId = (oldStateId == 0) ? 1 : 0;
87  speciesPtr_->mutator().setMoleculeState(molecule, newStateId);
88 
89  #ifdef SIMP_NOPAIR
90 
91  bool accept = true;
92 
93  #else // ifndef SIMP_NOPAIR
94 
95  // Recalculate pair energy for the molecule
96  double newEnergy = system().pairPotential().moleculeEnergy(molecule);
97 
98  // Decide whether to accept or reject
99  double oldWeight = speciesPtr_->mutator().stateWeight(oldStateId);
100  double newWeight = speciesPtr_->mutator().stateWeight(newStateId);
101  double ratio = boltzmann(newEnergy - oldEnergy)*newWeight/oldWeight;
102  bool accept = random().metropolis(ratio);
103 
104  #endif
105 
106  if (accept) {
107 
109 
110  } else {
111 
112  // Revert chosen molecule to original state
113  speciesPtr_->mutator().setMoleculeState(molecule, oldStateId);
114 
115  }
116 
117  return accept;
118  }
119 
120 }
A System for use in a Markov chain Monte Carlo simulation.
Definition: McSystem.h:52
void incrementNAttempt()
Increment the number of attempted moves.
Definition: McMove.h:191
virtual void save(Serializable::OArchive &ar)
Save state to an archive.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
Definition: McMove.cpp:58
double stateWeight(int stateId) const
Get the statistical weight for a specfic molecular state.
A Homopolymer with a mutable type, for semigrand ensemble.
Definition: HomopolymerSG.h:30
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
Definition: McMove.cpp:48
File containing preprocessor macros for error handling.
int moleculeStateId(const Molecule &molecule) const
Get the state id for a specific molecule.
virtual bool move()
Generate and accept or reject configuration bias move.
Saving / output archive for binary ostream.
double boltzmann(double energy)
Boltzmann weight associated with an energy difference.
Definition: SystemMove.h:100
virtual void readParameters(std::istream &in)
Read species to which displacement is applied.
#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
virtual double moleculeEnergy(const Molecule &molecule) const =0
Calculate the nonbonded pair energy for an entire Molecule.
Utility classes for scientific computation.
Definition: accumulators.mod:1
An McMove that acts on one McSystem.
Definition: SystemMove.h:28
virtual void setMoleculeState(Molecule &molecule, int stateId)=0
Change the state of a specific molecule.
HomopolymerSemiGrandMove(McSystem &system)
Constructor.
HomopolymerSG * speciesPtr_
Pointer to instance of HomopolymerSG.
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 loadParameters(Serializable::IArchive &ar)
Load state from an archive.
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.
bool metropolis(double ratio)
Metropolis algorithm for whether to accept a MC move.
Definition: Random.h:260
Simulation & simulation()
Get parent Simulation object.
Definition: McMove.h:203
A physical molecule (a set of covalently bonded Atoms).
McMd::SpeciesMutator & mutator()
Return the species mutator object by reference.
Species & species(int i)
Get a specific Species by reference.
int speciesId_
Integer index for molecular species.