SSAGES  0.1
A MetaDynamics Package
Public Member Functions | Static Public Member Functions | Private Attributes | List of all members
SSAGES::RouseModeCV Class Reference

Collective variable is a Rouse mode for a polymer chain comprised of N particle groups. More...

#include <RouseModeCV.h>

Inheritance diagram for SSAGES::RouseModeCV:
Inheritance graph
[legend]

Public Member Functions

 RouseModeCV (const std::vector< Label > &groups, int p)
 Basic Constructor for Rouse Mode CV. More...
 
void setMasses (const std::vector< Label > &groups, const Snapshot &snapshot)
 Helper function to determine masses of each group. More...
 
void Initialize (const Snapshot &snapshot) override
 Initialize necessary variables. More...
 
void Evaluate (const Snapshot &snapshot) override
 Evaluate the CV. More...
 
- Public Member Functions inherited from SSAGES::CollectiveVariable
 CollectiveVariable ()
 Constructor.
 
virtual ~CollectiveVariable ()
 Destructor.
 
virtual void Initialize (const class Snapshot &)
 Initialize CV. More...
 
virtual void Evaluate (const class Snapshot &)=0
 Evaluate CV. More...
 
double GetValue () const
 Get current value of the CV. More...
 
virtual double GetMinimumImage (double) const
 Returns the minimum image of a CV based on the input location. More...
 
virtual double GetPeriodicValue (double location) const
 Apply periodic boundaries to a given value. More...
 
const std::vector< Vector3 > & GetGradient () const
 Get current gradient of the CV. More...
 
const Matrix3GetBoxGradient () const
 Get gradient contribution to box.
 
const std::array< double, 2 > & GetBoundaries ()
 Get CV boundaries. More...
 
virtual double GetDifference (double location) const
 

Static Public Member Functions

static RouseModeCVBuild (const Json::Value &json, const std::string &path)
 
- Static Public Member Functions inherited from SSAGES::CollectiveVariable
static CollectiveVariableBuildCV (const Json::Value &json, const std::string &path)
 Set up collective variable. More...
 

Private Attributes

std::vector< Labelgroups_
 
std::vector< double > massg_
 
int N_
 
int p_
 
Vector3 xp_
 
std::vector< Vector3r_
 

Additional Inherited Members

- Protected Attributes inherited from SSAGES::CollectiveVariable
std::vector< Vector3grad_
 Gradient vector dCv/dxi.
 
Matrix3 boxgrad_
 Gradient w.r.t box vectors dCv/dHij.
 
double val_
 Current value of CV.
 
std::array< double, 2 > bounds_
 Bounds on CV.
 

Detailed Description

Collective variable is a Rouse mode for a polymer chain comprised of N particle groups.

This CV returns the value for the p'th Rouse mode, computed as Xp(t) ~ (1/N) sum_{i=1}^{N} ri(t)*cos[pi(n-0.5)p/N], where N is the number of particle groups, p is the mode index, ri is the center-of-mass position of a collection of atoms comprising the i'th bead in the N-bead polymer chain

Definition at line 39 of file RouseModeCV.h.

Constructor & Destructor Documentation

SSAGES::RouseModeCV::RouseModeCV ( const std::vector< Label > &  groups,
int  p 
)
inline

Basic Constructor for Rouse Mode CV.

Parameters
groups- vector of vector of atom IDs, each group comprising a bead in the Rouse chain
p- index for relevant Rouse mode

Definition at line 55 of file RouseModeCV.h.

Referenced by Evaluate().

55  :
56  groups_(groups), p_(p), N_( groups_.size())
57  { massg_.resize( groups_.size(),0.0 ); }

Here is the caller graph for this function:

Member Function Documentation

void SSAGES::RouseModeCV::Evaluate ( const Snapshot snapshot)
inlineoverride

Evaluate the CV.

Parameters
snapshotCurrent simulation snapshot.

Definition at line 120 of file RouseModeCV.h.

References SSAGES::Snapshot::ApplyMinimumImage(), SSAGES::Snapshot::CenterOfMass(), Json::Requirement::GetErrors(), SSAGES::Snapshot::GetLocalIndices(), SSAGES::Snapshot::GetMasses(), SSAGES::Snapshot::GetNumAtoms(), SSAGES::CollectiveVariable::grad_, Json::Requirement::HasErrors(), Json::ObjectRequirement::Parse(), RouseModeCV(), SSAGES::CollectiveVariable::val_, and Json::ObjectRequirement::Validate().

121  {
122 
123  // Get data from snapshot.
124  auto ntot = snapshot.GetNumAtoms(); // total number of atoms
125  const auto& masses = snapshot.GetMasses(); // mass of each atom
126 
127  // Initialize working variables
128  double ppi_n = p_ * M_PI / N_; // constant
129  xp_.fill(0.0); // vectorial Rouse mode
130  r_.resize(N_); // position vector for beads in Rouse chain (unwrapped)
131  grad_.resize(ntot, Vector3{0,0,0}); // gradient set to 0.0 for all atoms
132  std::fill(grad_.begin(), grad_.end(), Vector3{0,0,0});
133 
134  // Iterate over all N_ atom groups and compute the center of mass for each
135  std::vector<Vector3> rcom; // vector of COM positions
136  for (size_t i = 0; i < N_; ++i) {
137  Label idi; // list of indices
138  snapshot.GetLocalIndices(groups_[i], &idi);
139  rcom.push_back(snapshot.CenterOfMass(idi,massg_[i])); // center of mass for group
140  }
141 
142  // Now compute differences vectors between the neighboring beads
143  // accumulate displacements to reconstruct unwrapped polymer chain
144  // for simplicity, we consider the first bead to be the reference position
145  // in all snapshots
146  r_[0] = rcom[0];
147  for (size_t i = 1; i< N_; ++i) {
148  Vector3 dri = snapshot.ApplyMinimumImage(rcom[i] - rcom[i-1]);
149  r_[i] = r_[i-1] + dri; // r_i = r_{i-1} + (r_i - r_{i-1})
150  }
151 
152  // Determine the value of the Rouse coordinate
153  // Xp(t) = 1/sqrt(N) * sum_{i=1}^{N} ri, p = 0
154  // Xp(t) = sqrt(2/N) * sum_{i=1}^{N} ri * cos[p*pi/N*(i-0.5)], p = 1,...,N-1
155  // Note: this solution is valid for homogeneous friction
156  xp_.fill(0.0);
157  for (size_t i = 0; i<N_; ++i) {
158  xp_ += r_[i]*cos ( ppi_n * (i+0.5) );
159  }
160  xp_ /= sqrt(N_);
161  if ( p_ != 0 ) xp_ *= sqrt(2.0) ;
162 
163  // Compute Rouse vector norm as the CV
164  // CV = sqrt(Xp*Xp), Xp = (Xp1,Xp2,Xp3)
165  val_ = xp_.norm();
166 
167  // Now perform gradient operation
168  // dCV/dxjd = (Xpd/CV)*(c/N)**0.5*sum_i=1^N cos[p*pi(i-0.5)/N] mj/Mi *delta_j({i})
169  // delta_j({i}) = 1, if j in {i}, 0 otherwise
170  Vector3 gradpcon = xp_ / sqrt(N_) / val_;
171  if ( p_ != 0) gradpcon *= sqrt(2.0); // (Xpd/CV)*(c/N)**0.5
172  for (size_t i = 0; i < N_; ++i) {
173  Label idi; // list of indices
174  snapshot.GetLocalIndices(groups_[i], &idi);
175  // go over each atom in the group and add to its gradient
176  // Note: performance tradeoff here. All gradient elements have a common factor of
177  // Xpd/CV*sqrt(c/N) = prefactor, with c = 2 if p != 0
178  // this could be factored out, but if ntot >> number of atoms in groups
179  // then it won't be worth it to post multiply all gradient terms...
180  // Could also go over loop again after to do the multiplication, but
181  // that is troublesome if atom ids appear in multiple groups for some reason
182  double cosval = cos(ppi_n*(i+0.5)) / massg_[i]; // cos[p*pi(i-0.5)/N] / Mi
183  for (auto& id : idi) {
184  grad_[id] += gradpcon* cosval * masses[id];
185  }
186  }
187 
188  }
std::vector< int > Label
List of integers.
Definition: types.h:48
std::vector< Vector3 > grad_
Gradient vector dCv/dxi.
Eigen::Vector3d Vector3
Three-dimensional vector.
Definition: types.h:33
double val_
Current value of CV.

Here is the call graph for this function:

void SSAGES::RouseModeCV::Initialize ( const Snapshot snapshot)
inlineoverride

Initialize necessary variables.

Parameters
snapshotCurrent simulation snapshot.

Definition at line 79 of file RouseModeCV.h.

References SSAGES::Snapshot::GetCommunicator(), SSAGES::Snapshot::GetLocalIndex(), and setMasses().

80  {
81  using std::to_string;
82 
83  // Check for valid p_
84  if (p_ > groups_.size())
85  throw std::invalid_argument(
86  "RouseModeCV: Expected to find p to be less than " +
87  to_string(groups_.size()) +" but found p = " +
88  to_string(p_)
89  );
90  // Check for valid groups
91  for (size_t j = 0; j < groups_.size(); ++j) {
92  auto nj = groups_[j].size();
93  // Make sure atom IDs in the group are somewhere
94  std::vector<int> found(nj,0);
95  for (size_t i = 0; i <nj; ++i) {
96  if(snapshot.GetLocalIndex(groups_[j][i]) != -1)
97  found[i] = 1;
98  }
99 
100  MPI_Allreduce(MPI_IN_PLACE, found.data(), nj, MPI_INT, MPI_SUM, snapshot.GetCommunicator());
101  unsigned njtot = std::accumulate(found.begin(), found.end(), 0, std::plus<int>());
102 
103  if(njtot != nj)
104  throw std::invalid_argument(
105  "RouseModeCV: Expected to find " +
106  to_string(nj) +
107  " atoms in group " + to_string(j) +", but only found " +
108  to_string(njtot) + "."
109  );
110 
111  }
112  // Set the masses of each particle group in massg_
113  this->setMasses( groups_, snapshot);
114  }
void setMasses(const std::vector< Label > &groups, const Snapshot &snapshot)
Helper function to determine masses of each group.
Definition: RouseModeCV.h:64

Here is the call graph for this function:

void SSAGES::RouseModeCV::setMasses ( const std::vector< Label > &  groups,
const Snapshot snapshot 
)
inline

Helper function to determine masses of each group.

Note: this here assumes that the masses of each group are not changing during the simulation, which is likely typical...

Parameters
groups- vector of vector of atom IDs, each group comprising a bead in the Rouse chain

Definition at line 64 of file RouseModeCV.h.

References SSAGES::Snapshot::GetLocalIndices(), and SSAGES::Snapshot::TotalMass().

Referenced by Initialize().

64  {
65  // Compute mass of each group and store to massg_
66  double massi; Label listi;
67  for (size_t i = 0; i < N_; ++i) {
68  listi = groups[i]; // MW: can this be used directly in Total Mass
69  Label idi;
70  snapshot.GetLocalIndices(listi, &idi);
71  massg_[i] = snapshot.TotalMass(idi);
72  }
73  }
std::vector< int > Label
List of integers.
Definition: types.h:48

Here is the call graph for this function:

Here is the caller graph for this function:


The documentation for this class was generated from the following file: