Simpatico  v1.10
NveVvIntegrator.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 "NveVvIntegrator.h"
9 #include <mcMd/mdSimulation/MdSystem.h>
10 #include <mcMd/simulation/Simulation.h>
11 #include <mcMd/potentials/pair/MdPairPotential.h>
12 #include <mcMd/chemistry/Molecule.h>
13 #include <mcMd/chemistry/Atom.h>
14 #include <simp/boundary/Boundary.h>
15 #include <util/space/Vector.h>
16 #include <util/archives/Serializable_includes.h>
17 
18 //#define USE_ITERATOR 1
19 namespace McMd
20 {
21 
22  using namespace Util;
23  using namespace Simp;
24 
25  /*
26  * Constructor.
27  */
29  : MdIntegrator(system)
30  { setClassName("NveVvIntegrator"); }
31 
32  /*
33  * Destructor.
34  */
36  {}
37 
38  /*
39  * Read parameter and configuration files, initialize system.
40  */
41  void NveVvIntegrator::readParameters(std::istream &in)
42  {
43  read<double>(in, "dt", dt_);
44  int nAtomType = simulation().nAtomType();
45  prefactors_.allocate(nAtomType);
46  }
47 
48  /*
49  * Load the internal state to an archive.
50  */
52  {
53  loadParameter<double>(ar, "dt", dt_);
54  int nAtomType = simulation().nAtomType();
55  prefactors_.allocate(nAtomType);
56  ar & prefactors_;
57  }
58 
59  /*
60  * Save the internal state to an archive.
61  */
63  {
64  ar & dt_;
65  ar & prefactors_;
66  }
67 
68  /*
69  * Initialize constants.
70  */
72  {
73  double mass;
74  int nAtomType = prefactors_.capacity();
75  for (int i = 0; i < nAtomType; ++i) {
76  mass = simulation().atomType(i).mass();
77  prefactors_[i] = 0.5*dt_/mass;
78  }
81  }
82 
83  /*
84  * Verlet MD NVE integrator step
85  *
86  * This method implements the algorithm:
87  *
88  * vm(n) = v(n) + 0.5*a(n)*dt
89  * x(n+1) = x(n) + vm(n)*dt
90  *
91  * calculate acceleration a(n+1)
92  *
93  * v(n+1) = vm(n) + 0.5*a(n+1)*dt
94  *
95  * where x is position, v is velocity, and a is acceleration.
96  */
98  {
99  Vector dv;
100  Vector dr;
101  System::MoleculeIterator molIter;
102  double prefactor;
103  int iSpecies, nSpecies;
104 
105  nSpecies = simulation().nSpecies();
106 
107  // 1st half velocity Verlet, loop over atoms
108  #if USE_ITERATOR
109  Molecule::AtomIterator atomIter;
110  for (iSpecies=0; iSpecies < nSpecies; ++iSpecies) {
111  system().begin(iSpecies, molIter);
112  for ( ; molIter.notEnd(); ++molIter) {
113  for (molIter->begin(atomIter); atomIter.notEnd(); ++atomIter) {
114 
115  prefactor = prefactors_[atomIter->typeId()];
116 
117  dv.multiply(atomIter->force(), prefactor);
118  atomIter->velocity() += dv;
119 
120  dr.multiply(atomIter->velocity(), dt_);
121  atomIter->position() += dr;
122 
123  }
124  }
125  }
126  #else
127  Atom* atomPtr;
128  int ia;
129  for (iSpecies=0; iSpecies < nSpecies; ++iSpecies) {
130  system().begin(iSpecies, molIter);
131  for ( ; molIter.notEnd(); ++molIter) {
132  for (ia=0; ia < molIter->nAtom(); ++ia) {
133  atomPtr = &molIter->atom(ia);
134  prefactor = prefactors_[atomPtr->typeId()];
135 
136  dv.multiply(atomPtr->force(), prefactor);
137  atomPtr->velocity() += dv;
138  dr.multiply(atomPtr->velocity(), dt_);
139 
140  atomPtr->position() += dr;
141 
142  }
143  }
144  }
145  #endif
148 
149  #ifndef SIMP_NOPAIR
150  if (!system().pairPotential().isPairListCurrent()) {
152  }
153  #endif
154 
156 
157  // 2nd half velocity Verlet, loop over atoms
158  #if USE_ITERATOR
159  for (iSpecies=0; iSpecies < nSpecies; ++iSpecies) {
160  system().begin(iSpecies, molIter);
161  for ( ; molIter.notEnd(); ++molIter) {
162  for (molIter->begin(atomIter); atomIter.notEnd(); ++atomIter) {
163  prefactor = prefactors_[atomIter->typeId()];
164  dv.multiply(atomIter->force(), prefactor);
165  atomIter->velocity() += dv;
166  }
167  }
168  }
169  #else
170  for (iSpecies=0; iSpecies < nSpecies; ++iSpecies) {
171  system().begin(iSpecies, molIter);
172  for ( ; molIter.notEnd(); ++molIter)
173  {
174  for (ia=0; ia < molIter->nAtom(); ++ia) {
175  atomPtr = &molIter->atom(ia);
176  prefactor = prefactors_[atomPtr->typeId()];
177  dv.multiply(atomPtr->force(), prefactor);
178  atomPtr->velocity() += dv;
179  }
180  }
181  }
182  #endif
184 
185  }
186 
187 }
188 #undef USE_ITERATOR
A Vector is a Cartesian vector.
Definition: Vector.h:75
void calculateForces()
Compute all forces in this System.
Definition: MdSystem.cpp:857
void begin(int speciesId, MoleculeIterator &iterator)
Initialize an iterator for molecules of one species in this System.
Definition: System.h:1147
Vector & velocity()
Get atomic velocity Vector by reference.
bool notEnd() const
Is the current pointer not at the end of the array?
Definition: ArrayIterator.h:83
Signal & positionSignal()
Signal to indicate change in atomic positions.
Definition: MdSystem.h:669
Vector & force()
Get atomic force Vector by reference.
Vector & multiply(const Vector &v, double s)
Multiply a vector v by a scalar s.
Definition: Vector.h:686
Classes used by all simpatico molecular simulations.
Simulation & simulation()
Get parent Simulation by reference.
Definition: MdIntegrator.h:111
Abstract base for molecular dynamics integrators.
Definition: MdIntegrator.h:30
Saving / output archive for binary ostream.
MdPairPotential & pairPotential() const
Return MdPairPotential by reference.
Definition: MdSystem.h:520
const AtomType & atomType(int i) const
Get a single AtomType object by const reference.
int typeId() const
Get type index for this Atom.
MdSystem & system()
Get parent MdSystem by reference.
Definition: MdIntegrator.h:105
NveVvIntegrator(MdSystem &system)
Constructor.
void notify(const T &t)
Notify all observers.
Definition: Signal.cpp:22
A point particle within a Molecule.
virtual void save(Serializable::OArchive &ar)
Save the internal state to an archive.
bool notEnd() const
Is the current pointer not at the end of the PArray?
Utility classes for scientific computation.
Definition: accumulators.mod:1
virtual void step()
Take a complete NVE MD integration step.
Forward iterator for an Array or a C array.
Definition: ArrayIterator.h:39
Forward iterator for a PArray.
Definition: ArraySet.h:19
virtual void readParameters(std::istream &in)
Read parameters from file and initialize this MdSystem.
double dt_
Integrator time step.
Definition: MdIntegrator.h:79
Signal & velocitySignal()
Signal to indicate change in atomic velocities.
Definition: MdSystem.h:675
virtual ~NveVvIntegrator()
Destructor.
virtual void setup()
Setup private variables before main loop.
Saving archive for binary istream.
double mass() const
Get the mass.
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
int nSpecies() const
Get the number of Species in this Simulation.
void buildPairList()
Build the internal PairList.
void setClassName(const char *className)
Set class name string.
A System for Molecular Dynamics simulation.
Definition: MdSystem.h:68
int capacity() const
Return allocated size.
Definition: Array.h:153
void allocate(int capacity)
Allocate the underlying C array.
Definition: DArray.h:191
const Vector & position() const
Get the position Vector by const reference.
int nAtomType() const
Get the number of atom types.
virtual void loadParameters(Serializable::IArchive &ar)
Load the internal state to an archive.