Simpatico  v1.10
McPairPerturbation.h
1 #ifdef MCMD_PERTURB
2 #ifndef SIMP_NOPAIR
3 #ifndef MCMD_MC_PAIR_PERTURBATION_H
4 #define MCMD_MC_PAIR_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/neighbor/CellList.h> // member
16 
17 #include <mcMd/mcSimulation/McSystem.h>
18 #include <mcMd/potentials/pair/McPairPotentialImpl.h>
19 #include <mcMd/chemistry/Atom.h>
20 #include <simp/ensembles/EnergyEnsemble.h>
21 
22 namespace McMd
23 {
24 
25  using namespace Util;
26  using namespace Simp;
27 
28  class McSystem;
29 
40  template < class Interaction >
41  class McPairPerturbation : public LinearPerturbation<McSystem>
42  {
43 
44  public:
45 
53  McPairPerturbation(McSystem& system, int size, int rank);
54 
58  virtual ~McPairPerturbation();
59 
65  virtual void readParameters(std::istream& in);
66 
72  virtual void loadParameters(Serializable::IArchive& ar);
73 
77  virtual void setParameter();
78 
85  virtual double parameter(int i) const;
86 
93  virtual double derivative(int i) const;
94 
98  Interaction& interaction() const;
99 
100  private:
101 
103  mutable CellList::NeighborArray neighbors_;
104 
106  mutable Interaction* interactionPtr_;
107 
108  /*
109  * Number of perturbation parameters associated with a System.
110  * nParameters = 1 for McPairPerturbation.
111  */
112  int nParameters_;
113 
114  };
115 
117 
118  /*
119  * Constructor.
120  */
121  template < class Interaction >
123  int size, int rank)
124  : LinearPerturbation<McSystem>(system, size, rank),
125  interactionPtr_(0)
126  { setClassName("McPairPerturbation"); }
127 
128  /*
129  * Destructor.
130  */
131  template < class Interaction >
132  McPairPerturbation<Interaction>::~McPairPerturbation<Interaction>()
133  {}
134 
135  /*
136  * Read epsilon(0,1) from file
137  */
138  template < class Interaction >
140  {
142  nParameters_ = Perturbation::getNParameters();
143  }
144 
148  template < class Interaction >
150  {
152  nParameters_ = Perturbation::getNParameters();
153  }
154 
155  /*
156  * Return the pair interaction by reference.
157  */
158  template < class Interaction >
160  {
161  if (interactionPtr_ == 0) {
162  McPairPotential* pairPtr = &(system().pairPotential());
164  implPtr = dynamic_cast< McPairPotentialImpl< Interaction >* >(pairPtr);
165  if (implPtr == 0) {
166  UTIL_THROW("Failed dynamic cast of McPairPotential");
167  }
168  interactionPtr_ = &implPtr->interaction();
169  }
170  return *interactionPtr_;
171  }
172 
173  /*
174  * Set the parameter epsilon(0,1) for this McSystem.
175  */
176  template < class Interaction >
178  { interaction().setEpsilon(0, 1, parameter_[0]); }
179 
180  /*
181  * Get the tempering variable from the parent System.
182  */
183  template < class Interaction >
185  {
186  if (i > nParameters_) {
187  UTIL_THROW("perturbation parameter index is out of bounds");
188  }
189  return interaction().epsilon(0, 1);
190  }
191 
192  /*
193  * Return pair energy for unlike pairs / (kT*epsilon(0,1))
194  */
195  template < class Interaction >
197  {
198  // Preconditions
199  if (i > nParameters_) {
200  UTIL_THROW("perturbation parameter index is out of bounds");
201  }
202  if (fabs(parameter_[i] - parameter(i)) > 1.0E-8) {
203  UTIL_THROW("Perturbation parameter is not set correctly");
204  }
205  if (!system().energyEnsemble().isIsothermal()) {
206  UTIL_THROW("Non isothermal ensemble for McPairPerturbation.");
207  }
208 
209  double energy, rsq;
210  Atom *jAtomPtr, *kAtomPtr;
211  int nNeighbor, nInCell;
212  int ic, nc, j, k, jId, kId, jType, kType;
213 
214  // Loop over cells
215  energy = 0.0;
216  nc = system().pairPotential().cellList().totCells();
217  for (ic = 0; ic < nc; ++ic) {
218 
219  // Get array of neighbors_
220  system().pairPotential().cellList().getCellNeighbors(ic, neighbors_, nInCell);
221  nNeighbor = neighbors_.size();
222 
223  // Loop over primary atoms in this cell
224  for (j = 0; j < nInCell; ++j) {
225  jAtomPtr = neighbors_[j];
226  jId = jAtomPtr->id();
227  jType = jAtomPtr->typeId();
228 
229  // Loop over secondary atoms in this and neighboring cells
230  for (k = 0; k < nNeighbor; ++k) {
231  kAtomPtr = neighbors_[k];
232  kId = kAtomPtr->id();
233  kType = kAtomPtr->typeId();
234 
235  // Count each pair only once
236  if (kId > jId && jType != kType) {
237 
238  // Exclude masked pairs
239  if (!jAtomPtr->mask().isMasked(*kAtomPtr)) {
240 
241  rsq = system().boundary().
242  distanceSq(jAtomPtr->position(), kAtomPtr->position());
243 
244  energy += interaction().
245  energy(rsq, jAtomPtr->typeId(), kAtomPtr->typeId());
246 
247  }
248 
249  }
250 
251  } // secondary atoms
252 
253  } // primary atoms
254 
255  } // cells
256 
257  // Multiply by the temperature factor.
258  if (system().energyEnsemble().isIsothermal()) {
259  energy *= system().energyEnsemble().beta();
260  } else {
261  UTIL_THROW("Non isothermal ensemble for McPairPerturbation.");
262  }
263 
264  return energy/parameter_[i];
265  }
266 
267 }
268 
269 #endif
270 #endif // #ifndef SIMP_NOPAIR
271 #endif // ifdef MCMD_PERTURB
DArray< double > parameter_
Value of the perturbation parameter for the associated System.
Definition: Perturbation.h:172
virtual double parameter(int i) const
Return the pair parameter epsilon(0,1) for this System.
A System for use in a Markov chain Monte Carlo simulation.
Definition: McSystem.h:52
Interaction & interaction() const
Return the pair potential interaction.
const CellList & cellList() const
Get the cellList by const reference.
virtual void setParameter()
Set pair interaction parameter epsilon(0,1) for this System.
A Perturbation that is a linear function of a parameter.
A PairPotential for MC simulations (abstract).
EnergyEnsemble & energyEnsemble() const
Get the EnergyEnsemble by reference.
Definition: System.h:1095
Mask & mask()
Get the associated Mask by reference.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
Classes used by all simpatico molecular simulations.
virtual void readParameters(std::istream &in)
Read parameter epsilon(0, 1) from file.
bool isMasked(const Atom &atom) const
True if the atom is in the masked set for the target Atom.
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
Implementation template for an McPairPotential.
int typeId() const
Get type index for this Atom.
void getCellNeighbors(int ic, NeighborArray &neighbors, int &nInCell) const
Fill an array with pointers to atoms in a cell and neighboring cells.
bool isIsothermal() const
Is this an Isothermal ensemble?
A point particle within a Molecule.
Utility classes for scientific computation.
Definition: accumulators.mod:1
int getNParameters() const
Gets the number of parameters per system.
int id() const
Get global index for this Atom within the Simulation.
Interaction & interaction()
Return reference to underlying pair interaction.
int totCells() const
Get total number of cells in this CellList.
A Perturbation in the pair interaction epsilon(0,1) for any pair potential supporting setEpsilon()...
McSystem & system() const
Get the associated System by reference.
McPairPotential & pairPotential() const
Return the McPairPotential by reference.
Definition: McSystem.h:388
virtual double derivative(int i) const
Return (0-1 pair energy) / ( kT *epsilon(0,1) )
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).
void setClassName(const char *className)
Set class name string.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
const Vector & position() const
Get the position Vector by const reference.
McPairPerturbation(McSystem &system, int size, int rank)
Constructor.
int size() const
Return logical size of this array (i.e., number of elements).
Definition: FSArray.h:207
void readParameters(std::istream &in)
Read perturbation parameter(s) from file.