SSAGES  0.1
A MetaDynamics Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Groups Pages
ParticlePositionCV.h
1 
21 #pragma once
22 
23 #include "CollectiveVariable.h"
24 #include "Validator/ObjectRequirement.h"
25 #include "Drivers/DriverException.h"
26 #include "Snapshot.h"
27 #include "schema.h"
28 #include <numeric>
29 
30 namespace SSAGES
31 {
33 
40  {
41  private:
44 
47 
50 
51  public:
53 
67  ParticlePositionCV(const Label& atomids, const Vector3& position, bool fixx, bool fixy, bool fixz) :
68  atomids_(atomids), point_(position), fix_{fixx, fixy, fixz}
69  {
70  }
71 
73 
76  void Initialize(const Snapshot& snapshot) override
77  {
78  using std::to_string;
79 
80  auto n = atomids_.size();
81 
82  // Make sure atom ID's are on at least one processor.
83  std::vector<int> found(n, 0);
84  for(size_t i = 0; i < n; ++i)
85  {
86  if(snapshot.GetLocalIndex(atomids_[i]) != -1)
87  found[i] = 1;
88  }
89 
90  MPI_Allreduce(MPI_IN_PLACE, found.data(), n, MPI_INT, MPI_SUM, snapshot.GetCommunicator());
91  unsigned ntot = std::accumulate(found.begin(), found.end(), 0, std::plus<int>());
92  if(ntot != n)
93  throw BuildException({
94  "ParticlePositionCV: Expected to find " +
95  to_string(n) +
96  " atoms, but only found " +
97  to_string(ntot) + "."
98  });
99  }
100 
102 
105  void Evaluate(const Snapshot& snapshot) override
106  {
107  // Get local atom indices and compute COM.
108  std::vector<int> idx;
109  snapshot.GetLocalIndices(atomids_, &idx);
110 
111  // Get data from snapshot.
112  auto n = snapshot.GetNumAtoms();
113  const auto& masses = snapshot.GetMasses();
114 
115  // Initialize gradient.
116  std::fill(grad_.begin(), grad_.end(), Vector3{0,0,0});
117  grad_.resize(n, Vector3{0,0,0});
118 
119  // Compute total and center of mass.
120  auto masstot = snapshot.TotalMass(idx);
121  Vector3 com = snapshot.CenterOfMass(idx, masstot);
122 
123  // Compute difference between point and account for requested
124  // dimensions only.
125  Vector3 dx = snapshot.ApplyMinimumImage(com - point_).cwiseProduct(fix_.cast<double>());
126  val_ = dx.norm();
127 
128  // If distance is zero, we have nothing to do.
129  if(val_ == 0)
130  return;
131 
132  for(auto& id : idx)
133  grad_[id] = dx/val_*masses[id]/masstot;
134  }
135 
136  static ParticlePositionCV* Build(const Json::Value& json, const std::string& path)
137  {
138  Json::ObjectRequirement validator;
139  Json::Value schema;
140  Json::Reader reader;
141 
142  reader.parse(JsonSchema::ParticlePositionCV, schema);
143  validator.Parse(schema, path);
144 
145  // Validate inputs.
146  validator.Validate(json, path);
147  if(validator.HasErrors())
148  throw BuildException(validator.GetErrors());
149 
150  Label atomids;
151  for(auto& id : json["atom_ids"])
152  atomids.push_back(id.asInt());
153 
154  Vector3 position;
155  position[0] = json["position"][0].asDouble();
156  position[1] = json["position"][1].asDouble();
157  position[2] = json["position"][2].asDouble();
158 
159  auto fixx = json["fix"][0].asBool();
160  auto fixy = json["fix"][1].asBool();
161  auto fixz = json["fix"][2].asBool();
162 
163  return new ParticlePositionCV(atomids, position, fixx, fixy, fixz);
164  }
165  };
166 }
std::vector< int > Label
List of integers.
Definition: types.h:48
bool HasErrors()
Check if errors have occured.
Definition: Requirement.h:86
Vector3 point_
Target point in space.
unsigned GetNumAtoms() const
Get number of atoms in this snapshot.
Definition: Snapshot.h:200
double TotalMass(const Label &indices) const
Compute the total mass of a group of particles based on index.
Definition: Snapshot.h:451
Collective variable on an particle position.
Class containing a snapshot of the current simulation in time.
Definition: Snapshot.h:43
void Evaluate(const Snapshot &snapshot) override
Evaluate the CV.
Bool3 fix_
Each dimension determines if a constraint is applied by the CV.
virtual void Parse(Value json, const std::string &path) override
Parse JSON value to generate Requirement(s).
int GetLocalIndex(int id) const
Gets the local atom index corresponding to an atom ID.
Definition: Snapshot.h:555
std::vector< Vector3 > grad_
Gradient vector dCv/dxi.
const mxx::comm & GetCommunicator() const
Get communicator for walker.
Definition: Snapshot.h:184
void GetLocalIndices(const Label &ids, Label *indices) const
Definition: Snapshot.h:573
Exception to be thrown when building the Driver fails.
std::vector< std::string > GetErrors()
Get list of error messages.
Definition: Requirement.h:92
Vector3 CenterOfMass(const Label &indices) const
Definition: Snapshot.h:466
const std::vector< double > & GetMasses() const
Const access to the particle masses.
Definition: Snapshot.h:378
Label atomids_
Vector of atom ids of interest.
ParticlePositionCV(const Label &atomids, const Vector3 &position, bool fixx, bool fixy, bool fixz)
Constructor.
Requirements on an object.
void Initialize(const Snapshot &snapshot) override
Initialize necessary variables.
Eigen::Vector3d Vector3
Three-dimensional vector.
Definition: types.h:33
void ApplyMinimumImage(Vector3 *v) const
Apply minimum image to a vector.
Definition: Snapshot.h:418
double val_
Current value of CV.
Abstract class for a collective variable.
virtual void Validate(const Value &json, const std::string &path) override
Validate JSON value.
Eigen::Matrix< bool, 3, 1 > Bool3
Three-dimensional boolean.
Definition: types.h:36