Simpatico  v1.10
McExternalPerturbation.h
1 #ifdef MCMD_PERTURB
2 #ifdef SIMP_EXTERNAL
3 #ifndef MCMD_MC_EXTERNAL_PERTURBATION_H
4 #define MCMD_MC_EXTERNAL_PERTURBATION_H
5 
6 
7 /*
8 * Simpatico - Simulation Package for Polymeric and Molecular Liquids
9 *
10 * Copyright 2010 - 2017, The Regents of the University of Minnesota
11 * Distributed under the terms of the GNU General Public License.
12 */
13 
14 #include <mcMd/perturb/LinearPerturbation.h> // base class
15 #include <mcMd/potentials/external/ExternalPotentialImpl.h>
16 #include <mcMd/mcSimulation/McSystem.h>
17 #include <mcMd/simulation/Simulation.h>
18 #include <mcMd/chemistry/Atom.h>
19 #include <simp/ensembles/EnergyEnsemble.h>
20 
21 namespace McMd
22 {
23 
24  using namespace Util;
25  using namespace Simp;
26 
27  class McSystem;
28 
37  template < class Interaction >
38  class McExternalPerturbation : public LinearPerturbation<McSystem>
39  {
40 
41  public:
42 
50  McExternalPerturbation(McSystem& system, int size, int rank);
51 
55  virtual ~McExternalPerturbation();
56 
62  virtual void readParameters(std::istream& in);
63 
67  virtual void setParameter();
68 
75  virtual double parameter(int i) const;
76 
83  virtual double derivative(int i) const;
84 
88  Interaction& interaction() const;
89 
90  private:
91 
93  mutable Interaction* interactionPtr_;
94 
95  // Number of perturbation parameters associated with a System.
96  // nParameters = 1 for McExternalPerturbation.
97  int nParameters_;
98  };
99 
100  /*
101  * Constructor.
102  */
103  template < class Interaction >
105  int size, int rank)
106  : LinearPerturbation<McSystem>(system, size, rank),
107  interactionPtr_(0)
108  { setClassName("McExternalPerturbation"); }
109 
110  /*
111  * Destructor.
112  */
113  template < class Interaction >
114  McExternalPerturbation<Interaction>::~McExternalPerturbation<Interaction>()
115  {}
116 
117  /*
118  * Read external parameter from file
119  */
120  template < class Interaction >
122  {
124  nParameters_ = Perturbation::getNParameters();
125  }
126 
127  /*
128  * Return underlying interaction object.
129  */
130  template < class Interaction >
132  {
133  if (interactionPtr_ == 0) {
134  ExternalPotential* externalPtr = &(system().externalPotential());
136  implPtr = dynamic_cast< ExternalPotentialImpl< Interaction >* >(externalPtr);
137  if (implPtr == 0) {
138  UTIL_THROW("Failed dynamic cast of ExternalPotential");
139  }
140  interactionPtr_ = &implPtr->interaction();
141  }
142  return *interactionPtr_;
143  }
144 
145  /*
146  * Set the external parameter for this McSystem.
147  */
148  template < class Interaction >
150  {
151  interaction().setExternalParameter(parameter_[0]);
152  }
153 
154  /*
155  * Get the tempering variable from the parent System.
156  */
157  template < class Interaction >
159  {
160  if (i > nParameters_) {
161  UTIL_THROW("perturbation parameter index is out of bounds");
162  }
163  return interaction().externalParameter();
164  }
165 
166  /*
167  * Return external energy / (kT*epsilon(0,1))
168  */
169  template < class Interaction >
171  {
172  // Preconditions
173  if (i > nParameters_) {
174  UTIL_THROW("perturbation parameter index is out of bounds");
175  }
176 
177  if (fabs(parameter_[i] - parameter(i)) > 1.0E-8) {
178  UTIL_THROW("Perturbation parameter is not set correctly");
179  }
180 
181  double energy;
182  System::MoleculeIterator molIter;
183  Molecule::AtomIterator atomIter;
184 
185  energy = 0.0;
186 
187  for (int iSpec=0; iSpec < system().simulation().nSpecies(); ++iSpec){
188  for (system().begin(iSpec, molIter); molIter.notEnd(); ++molIter){
189  for (molIter->begin(atomIter); atomIter.notEnd(); ++atomIter) {
190  energy += interaction().energy(atomIter->position(), atomIter->typeId());
191 
192  }
193  }
194  }
195 
196  // Multiply by the temperature factor.
197  if (system().energyEnsemble().isIsothermal()) {
198  energy *= system().energyEnsemble().beta();
199  } else {
200  UTIL_THROW("Non isothermal ensemble.");
201  }
202 
203  return energy/parameter_[i];
204  }
205 
206 }
207 #endif
208 #endif // #ifdef SIMP_EXTERNAL
209 #endif // #ifdef MCMD_PERTURB
A System for use in a Markov chain Monte Carlo simulation.
Definition: McSystem.h:52
Interaction & interaction() const
Return the external potential interaction.
virtual double derivative(int i) const
Return external potential energy / ( kT *external parameter )
bool notEnd() const
Is the current pointer not at the end of the array?
Definition: ArrayIterator.h:83
A Perturbation in the external potential external parameter.
A Perturbation that is a linear function of a parameter.
Classes used by all simpatico molecular simulations.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
virtual void readParameters(std::istream &in)
Read external parameter from file.
Abstract External Potential class.
Interaction & interaction()
Return external interaction by reference.
bool notEnd() const
Is the current pointer not at the end of the PArray?
Utility classes for scientific computation.
Definition: accumulators.mod:1
int getNParameters() const
Gets the number of parameters per system.
Forward iterator for an Array or a C array.
Definition: ArrayIterator.h:39
Forward iterator for a PArray.
Definition: ArraySet.h:19
Template implementation of ExternalPotential.
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
virtual double parameter(int i) const
Return the external parameter for this System.
McExternalPerturbation(McSystem &system, int size, int rank)
Constructor.
virtual void setParameter()
Set external parameter for this System.
void readParameters(std::istream &in)
Read perturbation parameter(s) from file.