Simpatico  v1.10
SliplinkerAll.cpp
1 #ifndef SLIPLINKER_ALL_CPP
2 #define SLIPLINKER_ALL_CPP
3 
4 /*
5 * MolMcD - Monte Carlo and Molecular Dynamics Simulator for Molecular Liquids
6 *
7 * Copyright 2010 - 2014, The Regents of the University of Minnesota
8 * Distributed under the terms of the GNU General Public License.
9 */
10 
11 #include "SliplinkerAll.h"
12 #include <mcMd/simulation/Simulation.h>
13 #include <mcMd/mcSimulation/McSystem.h>
14 #include <mcMd/links/LinkMaster.h>
15 #include <mcMd/potentials/pair/McPairPotential.h>
16 #include <mcMd/potentials/bond/BondPotential.h>
17 #include <mcMd/chemistry/Molecule.h>
18 #include <mcMd/chemistry/Atom.h>
19 #include <simp/boundary/Boundary.h>
20 #include <util/misc/FileMaster.h>
21 #include <util/space/Vector.h>
22 #include <util/space/Dimension.h>
23 #include <util/global.h>
24 
25 
26 namespace McMd
27 {
28 
29  using namespace Util;
30  using namespace Simp;
31 
32  // Constructor
34  SystemMove(system),
35  cutoff_(0),
36  mu_(0),
37  speciesId_(0)
38  { setClassName("SliplinkerAll"); }
39 
40  void SliplinkerAll::readParameters(std::istream& in)
41  {
42  readProbability(in);
43  read<double>(in, "cutoff", cutoff_);
44  read<double>(in, "mu", mu_);
45  read<int>(in, "speciesId", speciesId_);
46 
47 
48  }
49 
50 
51  // Create or destroy the slip-springs.
53  {
55  Molecule::AtomIterator atomIter;
56  Atom *atom0Ptr, *atom1Ptr;
57  Atom* atomPtr;
58  // Molecule *mol0Ptr, *mol1Ptr;
59  Molecule* molIPtr;
60  double prob, dRSq, mindRSq=cutoff_*cutoff_, rnd, norm;
61  int i, ntrials, j, nNeighbor, idLink, iAtom, id1, id0;
62  // int iAtom0, iAtom1, iMolecule0, iMolecule1;
63  int n0;
64  Link* linkPtr;
65  static const int maxNeighbor = CellList::MaxNeighbor;
66  double cdf[maxNeighbor], energy, sum;
67  int idneighbors[maxNeighbor];
68 
69 
70  ntrials = 2 * system().simulation().atomCapacity();
71  for (i=0; i < ntrials; ++i){
72 
73  //Choose to create or destroy a link with prob 0.5
74  if (random().uniform(0.0, 1.0) > 0.5){
75  // Try to create a link.
77  // Choose a molecule and atom at random
78  molIPtr = &(system().randomMolecule(speciesId_));
79  // iMolecule0 = system().moleculeId(*molIPtr);
80  iAtom = random().uniformInt(0, molIPtr->nAtom());
81  atomPtr = &molIPtr->atom(iAtom);
82  id0 = atomPtr->id();
83 
84  // Get array of neighbors
85  system().pairPotential().cellList().getNeighbors(atomPtr->position(), neighbors_);
86  nNeighbor = neighbors_.size();
87 
88  n0 = 0;
89  sum = 0;
90  // Loop over neighboring atoms
91  for (j = 0; j < nNeighbor; ++j) {
92  atom1Ptr = neighbors_[j];
93  // mol1Ptr = &atom1Ptr->molecule();
94  // iMolecule1 = system().moleculeId(*mol1Ptr);
95  id1 = atom1Ptr->id();
96 
97  // Check if atoms are the same
98  if (id0 != id1){
99  // Exclude masked pairs
100  if (!atomPtr->mask().isMasked(*atom1Ptr)) {
101  // Identify possible partners and calculate the cumulative distribution function
102  dRSq = system().boundary().distanceSq(atomPtr->position(), atom1Ptr->position());
103  if (dRSq <= mindRSq) {
104  energy = system().linkPotential().energy(dRSq, 0);
105  sum = sum + boltzmann(energy);
106  cdf[n0] = sum;
107  idneighbors[n0] = j;
108  n0++;
109  }
110  }
111  }
112  }
113 
114  // If at least 1 candidate has been found.
115  if (n0 > 0) {
116  // Choose a partner with probability cdf[j]/cdf[n0-1]
117  j = 0;
118  rnd = random().uniform(0.0, 1.0);
119  norm = 1.0/cdf[n0-1];
120  while (rnd > cdf[j]*norm ){
121  j = j + 1;
122  }
123  atom1Ptr = neighbors_[idneighbors[j]];
124  // Create a slip-link between the selected atoms with probability = prob
125  prob = 2.0 * (system().linkMaster().nLink() + 1.0);
126  prob = system().simulation().atomCapacity() * boltzmann(-mu_) * cdf[n0-1]/ prob ;
127  //prob = 2.0 * system().nMolecule(speciesId_) * boltzmann(-mu_) * cdf[n0-1]/ prob ;
128  if (system().simulation().random().uniform(0.0, 1.0) < prob) {
129  system().linkMaster().addLink(*atomPtr, *atom1Ptr, 0);
130  incrementNAccept();
131  }
132  }
133  }
134  else {
135  // Try to destroy a link
137  // Choose a link at random
138  if (system().linkMaster().nLink() > 0){
139  idLink = random().uniformInt(0, system().linkMaster().nLink());
140 
141  // Indentify the atoms for this link.
142  linkPtr = &(system().linkMaster().link(idLink));
143  atom0Ptr = &(linkPtr->atom0());
144  // mol0Ptr = &atom0Ptr->molecule();
145  // iMolecule0 = system().moleculeId(*mol0Ptr);
146  // iAtom0 = atom0Ptr->indexInMolecule();
147  atom1Ptr = &(linkPtr->atom1());
148  // mol1Ptr = &atom1Ptr->molecule();
149  // iMolecule1 = system().moleculeId(*mol1Ptr);
150  // iAtom1 = atom1Ptr->indexInMolecule();
151 
152  // Try to delete the slip-spring
153  dRSq = system().boundary().distanceSq(atom0Ptr->position(), atom1Ptr->position());
154  // try to delete the link if the bond is smaller than the cutoff
155  if (dRSq <= mindRSq) {
156  // Get array of neighbors
157  system().pairPotential().cellList().getNeighbors(atom0Ptr->position(), neighbors_);
158  nNeighbor = neighbors_.size();
159  id0 = atom0Ptr->id();
160  n0 = 0;
161  sum = 0;
162  // Loop over neighboring atoms
163  for (j = 0; j < nNeighbor; ++j) {
164  atom1Ptr = neighbors_[j];
165  // mol1Ptr = &atom1Ptr->molecule();
166  // iMolecule1 = system().moleculeId(*mol1Ptr);
167  id1 = atom1Ptr->id();
168 
169  // Check if atoms are the same
170  if (id0 != id1){
171  // Exclude masked pairs
172  if (!atom0Ptr->mask().isMasked(*atom1Ptr)) {
173  // Identify possible partners and calculate the cumulative distribution function
174  dRSq = system().boundary().distanceSq(atom0Ptr->position(), atom1Ptr->position());
175  if (dRSq <= mindRSq) {
176  energy = system().linkPotential().energy(dRSq, 0);
177  //energy = 0.5*dRSq;
178  sum = sum + boltzmann(energy);
179  cdf[n0] = sum;
180  idneighbors[n0] = j;
181  n0++;
182  }
183  }
184  }
185  }
186  // Destroy the slip-link between the selected atoms with probability = prob
187  //prob = 2.0 * system().nMolecule(speciesId_) * boltzmann(-mu_) * cdf[n0-1];
188  prob = system().simulation().atomCapacity() * boltzmann(-mu_) * cdf[n0-1];
189  prob = 2.0 * system().linkMaster().nLink() / prob;
190  if (system().simulation().random().uniform(0.0, 1.0) < prob) {
191  system().linkMaster().removeLink(idLink);
192  incrementNAccept();
193  }
194  }
195  }
196  }
197  }
198  return true;
199  }
200 
201 }
202 #endif
int nLink() const
Get the total number of active Links.
Definition: LinkMaster.h:261
A System for use in a Markov chain Monte Carlo simulation.
Definition: McSystem.h:52
void removeLink(int id)
Remove a Link.
Definition: LinkMaster.cpp:77
void incrementNAttempt()
Increment the number of attempted moves.
Definition: McMove.h:191
static const int MaxNeighbor
Maximum possible number of neighboring atoms.
void getNeighbors(const Vector &pos, NeighborArray &neighbors) const
Fill a NeighborArray with pointers to atoms near a specified position.
double distanceSq(const Vector &r1, const Vector &r2) const
Return square distance between positions r1 and r2.
const CellList & cellList() const
Get the cellList by const reference.
int atomCapacity() const
Get the total number of Atoms allocated.
Mask & mask()
Get the associated Mask by reference.
File containing preprocessor macros for error handling.
Classes used by all simpatico molecular simulations.
double uniform()
Return a random floating point number x, uniformly distributed in the range 0 <= x < 1...
Definition: Random.h:203
double boltzmann(double energy)
Boltzmann weight associated with an energy difference.
Definition: SystemMove.h:100
bool isMasked(const Atom &atom) const
True if the atom is in the masked set for the target Atom.
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
Simulation & simulation() const
Get the parent Simulation by reference.
Definition: System.h:1055
virtual bool move()
Create or destroy slip-springs.
A point particle within a Molecule.
Utility classes for scientific computation.
Definition: accumulators.mod:1
SliplinkerAll(McSystem &system)
Constructor.
An McMove that acts on one McSystem.
Definition: SystemMove.h:28
int id() const
Get global index for this Atom within the Simulation.
long uniformInt(long range1, long range2)
Return random long int x uniformly distributed in range1 <= x < range2.
Definition: Random.h:224
Forward iterator for an Array or a C array.
Definition: ArrayIterator.h:39
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
virtual double energy(double rSq, int type) const =0
Returns potential energy for one bond.
Boundary & boundary() const
Get the Boundary by reference.
Definition: System.h:1064
BondPotential & linkPotential() const
Return the McLinkPotential by reference.
Definition: McSystem.h:493
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.
const Atom & atom(int localId) const
Get a specific Atom in this Molecule.
Link & link(int id) const
Return an active link by an internal set index.
Definition: LinkMaster.h:255
Simulation & simulation()
Get parent Simulation object.
Definition: McMove.h:203
A physical molecule (a set of covalently bonded Atoms).
const Vector & position() const
Get the position Vector by const reference.
void addLink(Atom &atom0, Atom &atom1, int typeId)
Add a link betwen two specific Atoms.
Definition: LinkMaster.cpp:39
int size() const
Return logical size of this array (i.e., number of elements).
Definition: FSArray.h:207
int nAtom() const
Get the number of Atoms in this Molecule.
LinkMaster & linkMaster() const
Get the LinkMaster by reference.
Definition: System.h:1074
virtual void readParameters(std::istream &in)
Read cutoff and probability.