Simpatico  v1.10
McExternalPerturbation.cpp
1 #ifdef MCMD_PERTURB
2 #ifdef SIMP_EXTERNAL
3 
4 /*
5 * Simpatico - Simulation Package for Polymeric and Molecular Liquids
6 *
7 * Copyright 2010 - 2017, The Regents of the University of Minnesota
8 * Distributed under the terms of the GNU General Public License.
9 */
10 
11 #include "McExternalPerturbation.h"
12 #include <mcMd/mcSimulation/McSystem.h>
13 #include <mcMd/simulation/Simulation.h>
14 #include <mcMd/chemistry/Atom.h>
15 #include <simp/ensembles/EnergyEnsemble.h>
16 
17 namespace McMd
18 {
19 
20  using namespace Util;
21  using namespace Simp;
22 
23  /*
24  * Constructor.
25  */
26  template < class Interaction >
28  int size, int rank)
29  : LinearPerturbation<McSystem>(system, int size, int rank),
30  interactionPtr_(0)
31  { }
32 
33  /*
34  * Destructor.
35  */
36  template < class Interaction >
37  McExternalPerturbation<Interaction>::~McExternalPerturbation<Interaction>()
38  { }
39 
40  /*
41  * Read external parameter from file
42  */
43  template < class Interaction >
45  {
47  nParameters_ = Perturbation::getNParameters();
48  }
49 
50  /*
51  */
52  template < class Interaction >
54  {
55  if (interactionPtr_ == 0) {
56  ExternalPotential* externalPtr = &(system().externalPotential());
58  implPtr = dynamic_cast< ExternalPotentialImpl< Interaction >* >(externalPtr);
59  if (implPtr == 0) {
60  UTIL_THROW("Failed dynamic cast of ExternalPotential");
61  }
62  interactionPtr_ = &implPtr->interaction();
63  }
64  return *interactionPtr_;
65  }
66 
67  /*
68  * Set the external parameter for this McSystem.
69  */
70  template < class Interaction >
72  {
73  interaction().setExternalParameter(parameter_[0]);
74  }
75 
76  /*
77  * Get the tempering variable from the parent System.
78  */
79  template < class Interaction >
81  {
82  if (i > nParameters_) {
83  UTIL_THROW("perturbation parameter index is out of bounds");
84  }
85  return interaction.externalParameter();
86  }
87 
88  /*
89  * Return external energy / (kT*epsilon(0,1))
90  */
91  template < class Interaction >
93  {
94  // Preconditions
95  if (i > nParameters_) {
96  UTIL_THROW("perturbation parameter index is out of bounds");
97  }
98 
99  if (fabs(parameter_[i] - parameter(i)) > 1.0E-8) {
100  UTIL_THROW("Perturbation parameter is not set correctly");
101  }
102 
103  double energy;
104  System::MoleculeIterator molIter;
105  Molecule::AtomIterator atomIter;
106 
107  energy = 0.0;
108 
109  for (int iSpec=0; iSpec < system().simulation().nSpecies(); ++iSpec){
110  for (system().begin(iSpec, molIter); molIter.notEnd(); ++molIter){
111  for (molIter->begin(atomIter); atomIter.notEnd(); ++atomIter) {
112  energy += interaction().energy(atomIter->position(), atomIter->typeId());
113 
114  }
115  }
116  }
117 
118  // Multiply by the temperature factor.
119  if (system().energyEnsemble().isIsothermal()) {
120  energy *= system().energyEnsemble().beta();
121  } else {
122  UTIL_THROW("Non isothermal ensemble.");
123  }
124 
125  return energy/parameter_[i];
126  }
127 
128 }
129 #endif // #ifndef SIMP_EXTERNAL
130 #endif // #ifdef MCMD_PERTURB
DArray< double > parameter_
Value of the perturbation parameter for the associated System.
Definition: Perturbation.h:172
A System for use in a Markov chain Monte Carlo simulation.
Definition: McSystem.h:52
Interaction & interaction() const
Return the external potential interaction.
void begin(int speciesId, MoleculeIterator &iterator)
Initialize an iterator for molecules of one species in this System.
Definition: System.h:1147
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.
EnergyEnsemble & energyEnsemble() const
Get the EnergyEnsemble by reference.
Definition: System.h:1095
Classes used by all simpatico molecular simulations.
double beta() const
Return the inverse temperature.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
Simulation & simulation() const
Get the parent Simulation by reference.
Definition: System.h:1055
virtual void readParameters(std::istream &in)
Read external parameter from file.
Abstract External Potential class.
ExternalPotential & externalPotential() const
Return ExternalPotential by reference.
Definition: McSystem.h:473
bool isIsothermal() const
Is this an Isothermal ensemble?
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
McSystem & system() const
Get the associated System by reference.
Forward iterator for a PArray.
Definition: ArraySet.h:19
Template implementation of ExternalPotential.
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
int nSpecies() const
Get the number of Species in this Simulation.
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.