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

Collective variable on components of the gyration tensor. More...

#include <GyrationTensorCV.h>

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

Public Member Functions

 GyrationTensorCV (const Label &atomids, GyrationTensor component)
 Constructor. 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 GyrationTensorCVBuild (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

Label atomids_
 IDs of the atoms used for calculation.
 
GyrationTensor component_
 Component of gyration tensor to compute.
 

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 on components of the gyration tensor.

Collective variable on components of gyration tensor. Depending on the user selection, this will specify a principal moment, radius of gyration, or another shape descriptor.

Definition at line 51 of file GyrationTensorCV.h.

Constructor & Destructor Documentation

SSAGES::GyrationTensorCV::GyrationTensorCV ( const Label atomids,
GyrationTensor  component 
)
inline

Constructor.

Parameters
atomidsIDs of the atoms defining gyration tensor.
componentSpecification of component to compute.

Construct a GyrationTensorCV.

Definition at line 66 of file GyrationTensorCV.h.

Referenced by Evaluate().

66  :
67  atomids_(atomids), component_(component)
68  {
69  }
GyrationTensor component_
Component of gyration tensor to compute.
Label atomids_
IDs of the atoms used for calculation.

Here is the caller graph for this function:

Member Function Documentation

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

Evaluate the CV.

Parameters
snapshotCurrent simulation snapshot.

Definition at line 104 of file GyrationTensorCV.h.

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

105  {
106  using namespace Eigen;
107 
108  // Get local atom indices and compute COM.
109  std::vector<int> idx;
110  snapshot.GetLocalIndices(atomids_, &idx);
111 
112  // Get data from snapshot.
113  auto n = snapshot.GetNumAtoms();
114  const auto& masses = snapshot.GetMasses();
115  const auto& pos = snapshot.GetPositions();
116 
117  // Initialize gradient.
118  std::fill(grad_.begin(), grad_.end(), Vector3{0,0,0});
119  grad_.resize(n, Vector3{0,0,0});
120 
121  // Compute total and center of mass.
122  auto masstot = snapshot.TotalMass(idx);
123  Vector3 com = snapshot.CenterOfMass(idx, masstot);
124 
125  // Gyration tensor and temporary vector to store positions in inertial frame.
126  Matrix3 S = Matrix3::Zero();
127  std::vector<Vector3> ris;
128  ris.reserve(idx.size());
129  for(auto& i : idx)
130  {
131  ris.emplace_back(snapshot.ApplyMinimumImage(pos[i] - com));
132  S.noalias() += masses[i]*ris.back()*ris.back().transpose();
133  }
134 
135  // Reduce gyration tensor across processors and normalize.
136  MPI_Allreduce(MPI_IN_PLACE, S.data(), S.size(), MPI_DOUBLE, MPI_SUM, snapshot.GetCommunicator());
137  S /= masstot;
138 
139  // Perform EVD. The columns are the eigenvectors.
140  // SelfAdjoint solver sorts in ascending order.
141  SelfAdjointEigenSolver<Matrix3> solver;
142  solver.computeDirect(S);
143  const auto& eigvals = solver.eigenvalues();
144  const auto& eigvecs = solver.eigenvectors();
145 
146  // Assign variables for clarity. l1 is largest.
147  auto l1 = eigvals[2],
148  l2 = eigvals[1],
149  l3 = eigvals[0];
150  const auto& n1 = eigvecs.col(2),
151  n2 = eigvecs.col(1),
152  n3 = eigvecs.col(0);
153 
154  // Compute gradient.
155  size_t j = 0;
156  val_ = 0;
157  for(auto& i : idx)
158  {
159  // Compute derivative of each eigenvalue and use combos in components.
160  auto dl1 = 2.*masses[i]/masstot*(1.-masses[i]/masstot)*ris[j].dot(n1)*n1;
161  auto dl2 = 2.*masses[i]/masstot*(1.-masses[i]/masstot)*ris[j].dot(n2)*n2;
162  auto dl3 = 2.*masses[i]/masstot*(1.-masses[i]/masstot)*ris[j].dot(n3)*n3;
163 
164  // It is inefficient to keep reassigning val, but it's better than having two
165  // switches, and the compiler will probably optimize it out anyways.
166  switch(component_)
167  {
168  case Rg:
169  grad_[i] = dl1 + dl2 + dl3;
170  val_ = l1 + l2 + l3;
171  break;
172  case principal1:
173  grad_[i] = dl1;
174  val_ = l1;
175  break;
176  case principal2:
177  grad_[i] = dl2;
178  val_ = l2;
179  break;
180  case principal3:
181  grad_[i] = dl3;
182  val_ = l3;
183  break;
184  case asphericity:
185  grad_[i] = dl1 - 0.5*(dl2 + dl3);
186  val_ = l1 - 0.5*(l2 + l3);
187  break;
188  case acylindricity:
189  grad_[i] = dl2 - dl3;
190  val_ = l2 - l3;
191  break;
192  case shapeaniso:
193  auto l1_2 = l1*l1, l2_2 = l2*l2, l3_2 = l3*l3;
194  auto sum = l1 + l2 + l3;
195  auto sqsum = l1_2 + l2_2 + l3_2;
196  grad_[i] = 3.*(l1*dl1+l2*dl2+l3*dl3)/(sum*sum) -
197  3.*sqsum*(dl1+dl2+dl3)/(sum*sum*sum);
198  val_ = 1.5*sqsum/(sum*sum) - 0.5;
199  break;
200  }
201 
202  ++j;
203  }
204  }
Eigen::Matrix3d Matrix3
3x3 matrix.
Definition: types.h:42
GyrationTensor component_
Component of gyration tensor to compute.
std::vector< Vector3 > grad_
Gradient vector dCv/dxi.
Label atomids_
IDs of the atoms used for calculation.
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::GyrationTensorCV::Initialize ( const Snapshot snapshot)
inlineoverride

Initialize necessary variables.

Parameters
snapshotCurrent simulation snapshot.

Definition at line 75 of file GyrationTensorCV.h.

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

76  {
77  using std::to_string;
78 
79  auto n = atomids_.size();
80 
81  // Make sure atom ID's are on at least one processor.
82  std::vector<int> found(n, 0);
83  for(size_t i = 0; i < n; ++i)
84  {
85  if(snapshot.GetLocalIndex(atomids_[i]) != -1)
86  found[i] = 1;
87  }
88 
89  MPI_Allreduce(MPI_IN_PLACE, found.data(), n, MPI_INT, MPI_SUM, snapshot.GetCommunicator());
90  unsigned ntot = std::accumulate(found.begin(), found.end(), 0, std::plus<int>());
91  if(ntot != n)
92  throw BuildException({
93  "GyrationTensorCV: Expected to find " +
94  to_string(n) +
95  " atoms, but only found " +
96  to_string(ntot) + "."
97  });
98  }
Label atomids_
IDs of the atoms used for calculation.

Here is the call graph for this function:


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