SSAGES  0.9.3
Software Suite for Advanced General Ensemble Simulations
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 
61  ParticlePositionCV(const Label& atomids, const Vector3& position) :
62  atomids_(atomids), point_(position), dim_{true, true, true}
63  {
64  }
65 
67 
78  ParticlePositionCV(const Label& atomids, const Vector3& position, bool dimx, bool dimy, bool dimz) :
79  atomids_(atomids), point_(position), dim_{dimx, dimy, dimz}
80  {
81  }
82 
84 
87  void Initialize(const Snapshot& snapshot) override
88  {
89  using std::to_string;
90 
91  auto n = atomids_.size();
92 
93  // Make sure atom ID's are on at least one processor.
94  std::vector<int> found(n, 0);
95  for(size_t i = 0; i < n; ++i)
96  {
97  if(snapshot.GetLocalIndex(atomids_[i]) != -1)
98  found[i] = 1;
99  }
100 
101  MPI_Allreduce(MPI_IN_PLACE, found.data(), n, MPI_INT, MPI_SUM, snapshot.GetCommunicator());
102  unsigned ntot = std::accumulate(found.begin(), found.end(), 0, std::plus<int>());
103  if(ntot != n)
104  throw BuildException({
105  "ParticlePositionCV: Expected to find " +
106  to_string(n) +
107  " atoms, but only found " +
108  to_string(ntot) + "."
109  });
110  }
111 
113 
116  void Evaluate(const Snapshot& snapshot) override
117  {
118  // Get local atom indices and compute COM.
119  std::vector<int> idx;
120  snapshot.GetLocalIndices(atomids_, &idx);
121 
122  // Get data from snapshot.
123  auto n = snapshot.GetNumAtoms();
124  const auto& masses = snapshot.GetMasses();
125 
126  // Initialize gradient.
127  std::fill(grad_.begin(), grad_.end(), Vector3{0,0,0});
128  grad_.resize(n, Vector3{0,0,0});
129 
130  // Compute total and center of mass.
131  auto masstot = snapshot.TotalMass(idx);
132  Vector3 com = snapshot.CenterOfMass(idx);
133 
134  // Compute difference between point and account for requested
135  // dimensions only.
136  Vector3 dx = snapshot.ApplyMinimumImage(com - point_).cwiseProduct(dim_.cast<double>());
137  val_ = dx.norm();
138 
139  // If distance is zero, we have nothing to do.
140  if(val_ == 0)
141  return;
142 
143  for(auto& id : idx)
144  grad_[id] = dx/val_*masses[id]/masstot;
145  }
146 
148  static ParticlePositionCV* Build(const Json::Value& json, const std::string& path)
149  {
150  Json::ObjectRequirement validator;
151  Json::Value schema;
152  Json::CharReaderBuilder rbuilder;
153  Json::CharReader* reader = rbuilder.newCharReader();
154 
155  reader->parse(JsonSchema::ParticlePositionCV.c_str(),
156  JsonSchema::ParticlePositionCV.c_str() + JsonSchema::ParticlePositionCV.size(),
157  &schema, nullptr);
158  validator.Parse(schema, path);
159 
160  // Validate inputs.
161  validator.Validate(json, path);
162  if(validator.HasErrors())
163  throw BuildException(validator.GetErrors());
164 
165  Label atomids;
166  for(auto& id : json["atom_ids"])
167  atomids.push_back(id.asInt());
168 
169  Vector3 position;
170  position[0] = json["position"][0].asDouble();
171  position[1] = json["position"][1].asDouble();
172  position[2] = json["position"][2].asDouble();
173 
175 
176  if(json.isMember("dimension"))
177  {
178  auto dimx = json["dimension"][0].asBool();
179  auto dimy = json["dimension"][1].asBool();
180  auto dimz = json["dimension"][2].asBool();
181  c = new ParticlePositionCV(atomids, position, dimx, dimy, dimz);
182  }
183  else
184  c = new ParticlePositionCV(atomids, position);
185 
186  return c;
187  }
188  };
189 }
Requirements on an object.
virtual void Parse(Value json, const std::string &path) override
Parse JSON value to generate Requirement(s).
virtual void Validate(const Value &json, const std::string &path) override
Validate JSON value.
std::vector< std::string > GetErrors()
Get list of error messages.
Definition: Requirement.h:92
bool HasErrors()
Check if errors have occured.
Definition: Requirement.h:86
Exception to be thrown when building the Driver fails.
Abstract class for a collective variable.
std::vector< Vector3 > grad_
Gradient vector dCv/dxi.
double val_
Current value of CV.
Collective variable on an particle position.
Label atomids_
Vector of atom ids of interest.
void Initialize(const Snapshot &snapshot) override
Initialize necessary variables.
Bool3 dim_
Each dimension determines if a constraint is applied by the CV.
ParticlePositionCV(const Label &atomids, const Vector3 &position)
Constructor.
ParticlePositionCV(const Label &atomids, const Vector3 &position, bool dimx, bool dimy, bool dimz)
Constructor.
void Evaluate(const Snapshot &snapshot) override
Evaluate the CV.
static ParticlePositionCV * Build(const Json::Value &json, const std::string &path)
Set up collective variable.
Vector3 point_
Target point in space.
Class containing a snapshot of the current simulation in time.
Definition: Snapshot.h:48
unsigned GetNumAtoms() const
Get number of atoms in this snapshot.
Definition: Snapshot.h:202
void GetLocalIndices(const Label &ids, Label *indices) const
Definition: Snapshot.h:537
const mxx::comm & GetCommunicator() const
Get communicator for walker.
Definition: Snapshot.h:186
int GetLocalIndex(int id) const
Gets the local atom index corresponding to an atom ID.
Definition: Snapshot.h:519
double TotalMass(const Label &indices) const
Compute the total mass of a group of particles based on index.
Definition: Snapshot.h:423
const std::vector< double > & GetMasses() const
Const access to the particle masses.
Definition: Snapshot.h:367
Vector3 CenterOfMass(const Label &indices, bool mass_weight=true) const
Compute center of mass of a group of atoms based on index.
Definition: Snapshot.h:442
void ApplyMinimumImage(Vector3 *v) const
Apply minimum image to a vector.
Definition: Snapshot.h:390
Eigen::Vector3d Vector3
Three-dimensional vector.
Definition: types.h:33
Eigen::Matrix< bool, 3, 1 > Bool3
Three-dimensional boolean.
Definition: types.h:36
std::vector< int > Label
List of integers.
Definition: types.h:48