24 #include "CollectiveVariable.h"
25 #include "Validator/ObjectRequirement.h"
26 #include "Drivers/DriverException.h"
64 TorsionalCV(
int atomid1,
int atomid2,
int atomid3,
int atomid4,
bool periodic) :
77 std::vector<int> found;
79 int nfound = found.size();
80 MPI_Allreduce(MPI_IN_PLACE, &nfound, 1, MPI_INT, MPI_SUM, snapshot.
GetCommunicator());
84 "TorsionalCV: Expected to find " +
86 " atoms, but only found " +
87 to_string(nfound) +
"."
106 Vector3 xi{0, 0, 0}, xj{0, 0, 0}, xk{0, 0, 0}, xl{0, 0, 0};
113 if(iindex != -1) xi = pos[iindex];
114 if(jindex != -1) xj = pos[jindex];
115 if(kindex != -1) xk = pos[kindex];
116 if(lindex != -1) xl = pos[lindex];
120 MPI_Allreduce(MPI_IN_PLACE, xi.data(), 3, MPI_DOUBLE, MPI_SUM, comm);
121 MPI_Allreduce(MPI_IN_PLACE, xj.data(), 3, MPI_DOUBLE, MPI_SUM, comm);
122 MPI_Allreduce(MPI_IN_PLACE, xk.data(), 3, MPI_DOUBLE, MPI_SUM, comm);
123 MPI_Allreduce(MPI_IN_PLACE, xl.data(), 3, MPI_DOUBLE, MPI_SUM, comm);
132 auto y = B.cross(A).dot(G.normalized());
137 auto Zed = F.dot(G.normalized())/A.dot(A);
138 auto Ned = H.dot(G.normalized())/B.dot(B);
140 Vector3 gradi{0,0,0}, gradl{0,0,0};
141 if(iindex != -1) gradi = -G.norm()*A/A.dot(A);
142 if(lindex != -1) gradl = G.norm()*B/B.dot(B);
143 MPI_Allreduce(MPI_IN_PLACE, gradi.data(), 3, MPI_DOUBLE, MPI_SUM, comm);
144 MPI_Allreduce(MPI_IN_PLACE, gradl.data(), 3, MPI_DOUBLE, MPI_SUM, comm);
145 if(iindex != -1)
grad_[iindex] = gradi;
146 if(lindex != -1)
grad_[lindex] = gradl;
147 if(jindex != -1)
grad_[jindex] = Zed*A - Ned*B - gradi;
148 if(kindex != -1)
grad_[kindex] = Ned*B - Zed*A - gradl;
166 int n = (int)(Location/(2.0*M_PI));
167 double PeriodicLocation;
169 PeriodicLocation = Location - n*M_PI;
170 if(PeriodicLocation < -M_PI)
171 PeriodicLocation += 2.0*M_PI;
172 else if (PeriodicLocation > M_PI)
173 PeriodicLocation -= 2.0*M_PI;
175 return PeriodicLocation;
190 double PeriodicDiff =
val_ - Location;
197 if(PeriodicDiff > M_PI)
198 PeriodicDiff -= 2.0*M_PI;
199 else if(PeriodicDiff < -M_PI)
200 PeriodicDiff += 2.0*M_PI;
215 double PeriodicDiff =
val_ - Location;
220 if(PeriodicDiff > M_PI)
221 return (
val_ - 2.0*M_PI);
222 else if(PeriodicDiff < -M_PI)
223 return (
val_ + 2.0*M_PI);
228 static TorsionalCV* Build(
const Json::Value& json,
const std::string& path)
234 reader.parse(JsonSchema::TorsionalCV, schema);
235 validator.
Parse(schema, path);
242 std::vector<int> atomids;
243 for(
auto& s : json[
"atom_ids"])
244 atomids.push_back(s.asInt());
246 auto periodic = json.get(
"periodic",
true).asBool();
247 return new TorsionalCV(atomids[0], atomids[1], atomids[2], atomids[3], periodic);
std::vector< int > Label
List of integers.
bool HasErrors()
Check if errors have occured.
unsigned GetNumAtoms() const
Get number of atoms in this snapshot.
bool periodic_
If True, use periodic boundary conditions.
double GetMinimumImage(const double Location) const override
Returns the minimum image of a CV based on the input location.
Class containing a snapshot of the current simulation in time.
Label atomids_
Vector of 4 atom ID's of interest.
double GetPeriodicValue(double Location) const override
Return value taking periodic boundary conditions into account.
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.
std::vector< Vector3 > grad_
Gradient vector dCv/dxi.
const mxx::comm & GetCommunicator() const
Get communicator for walker.
void GetLocalIndices(const Label &ids, Label *indices) const
Exception to be thrown when building the Driver fails.
Collective variable on the torsion angles.
TorsionalCV(int atomid1, int atomid2, int atomid3, int atomid4, bool periodic)
Constructor.
std::vector< std::string > GetErrors()
Get list of error messages.
Requirements on an object.
void Initialize(const Snapshot &snapshot) override
Initialize necessary variables.
double GetDifference(const double Location) const override
Get difference taking periodic boundary conditions into account.
void Evaluate(const Snapshot &snapshot) override
Evaluate the CV.
Eigen::Vector3d Vector3
Three-dimensional vector.
void ApplyMinimumImage(Vector3 *v) const
Apply minimum image to a vector.
double val_
Current value of CV.
Abstract class for a collective variable.
const std::vector< Vector3 > & GetPositions() const
Access the particle positions.
virtual void Validate(const Value &json, const std::string &path) override
Validate JSON value.