23 #include "CollectiveVariable.h"
24 #include "Validator/ObjectRequirement.h"
25 #include "Drivers/DriverException.h"
53 AngleCV(
int atomid1,
int atomid2,
int atomid3) :
54 atomids_({atomid1, atomid2, atomid3})
67 std::vector<int> found;
69 int nfound = found.size();
70 MPI_Allreduce(MPI_IN_PLACE, &nfound, 1, MPI_INT, MPI_SUM, snapshot.
GetCommunicator());
74 "TorsionalCV: Expected to find " +
76 " atoms, but only found " +
77 to_string(nfound) +
"."
96 Vector3 xi{0, 0, 0}, xj{0, 0, 0}, xk{0, 0, 0};
102 if(iindex != -1) xi = pos[iindex];
103 if(jindex != -1) xj = pos[jindex];
104 if(kindex != -1) xk = pos[kindex];
108 MPI_Allreduce(MPI_IN_PLACE, xi.data(), 3, MPI_DOUBLE, MPI_SUM, comm);
109 MPI_Allreduce(MPI_IN_PLACE, xj.data(), 3, MPI_DOUBLE, MPI_SUM, comm);
110 MPI_Allreduce(MPI_IN_PLACE, xk.data(), 3, MPI_DOUBLE, MPI_SUM, comm);
116 auto dotP = rij.dot(rkj);
117 auto nrij = rij.norm();
118 auto nrkj = rkj.norm();
119 auto dotPnn = dotP/(nrij*nrkj);
124 auto prefactor = -1.0/(sqrt(1. - dotPnn*dotPnn)*nrij*nrkj);
126 Vector3 gradi{0, 0, 0}, gradk{0, 0, 0};
127 if(iindex != -1) gradi = prefactor*(rkj - dotP*rij/(nrij*nrij));
128 if(kindex != -1) gradk = prefactor*(rij - dotP*rkj/(nrkj*nrkj));
129 MPI_Allreduce(MPI_IN_PLACE, gradi.data(), 3, MPI_DOUBLE, MPI_SUM, comm);
130 MPI_Allreduce(MPI_IN_PLACE, gradk.data(), 3, MPI_DOUBLE, MPI_SUM, comm);
131 if(iindex != -1)
grad_[iindex] = gradi;
132 if(kindex != -1)
grad_[kindex] = gradk;
133 if(jindex != -1)
grad_[jindex] = -gradi - gradk;
137 static AngleCV*
Build(
const Json::Value& json,
const std::string& path)
141 Json::CharReaderBuilder rbuilder;
142 Json::CharReader* reader = rbuilder.newCharReader();
144 reader->parse(JsonSchema::AngleCV.c_str(),
145 JsonSchema::AngleCV.c_str() + JsonSchema::AngleCV.size(),
147 validator.
Parse(schema, path);
154 std::vector<int> atomids;
155 for(
auto& s : json[
"atom_ids"])
156 atomids.push_back(s.asInt());
158 return new AngleCV(atomids[0], atomids[1], atomids[2]);
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.
bool HasErrors()
Check if errors have occured.
Collective variable to calculate angle.
AngleCV(int atomid1, int atomid2, int atomid3)
Constructor.
Label atomids_
Vector of 3 atom ID's of interest.
void Initialize(const Snapshot &snapshot) override
Initialize necessary variables.
static AngleCV * Build(const Json::Value &json, const std::string &path)
Set up collective variable.
void Evaluate(const Snapshot &snapshot) override
Evaluate the CV.
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.
std::array< double, 2 > bounds_
Bounds on CV.
Class containing a snapshot of the current simulation in time.
unsigned GetNumAtoms() const
Get number of atoms in this snapshot.
void GetLocalIndices(const Label &ids, Label *indices) const
const mxx::comm & GetCommunicator() const
Get communicator for walker.
int GetLocalIndex(int id) const
Gets the local atom index corresponding to an atom ID.
const std::vector< Vector3 > & GetPositions() const
Access the particle positions.
void ApplyMinimumImage(Vector3 *v) const
Apply minimum image to a vector.
Eigen::Vector3d Vector3
Three-dimensional vector.
std::vector< int > Label
List of integers.