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);
233 Json::CharReaderBuilder rbuilder;
234 Json::CharReader* reader = rbuilder.newCharReader();
236 reader->parse(JsonSchema::TorsionalCV.c_str(),
237 JsonSchema::TorsionalCV.c_str() + JsonSchema::TorsionalCV.size(),
239 validator.
Parse(schema, path);
246 std::vector<int> atomids;
247 for(
auto& s : json[
"atom_ids"])
248 atomids.push_back(s.asInt());
250 auto periodic = json.get(
"periodic",
true).asBool();
251 return new TorsionalCV(atomids[0], atomids[1], atomids[2], atomids[3], periodic);
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.
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.
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.
Collective variable on the torsion angles.
Label atomids_
Vector of 4 atom ID's of interest.
TorsionalCV(int atomid1, int atomid2, int atomid3, int atomid4, bool periodic)
Constructor.
bool periodic_
If True, use periodic boundary conditions.
void Initialize(const Snapshot &snapshot) override
Initialize necessary variables.
double GetPeriodicValue(double Location) const override
Return value taking periodic boundary conditions into account.
double GetDifference(const double Location) const override
Get difference taking periodic boundary conditions into account.
static TorsionalCV * Build(const Json::Value &json, const std::string &path)
Set up collective variable.
void Evaluate(const Snapshot &snapshot) override
Evaluate the CV.
double GetMinimumImage(const double Location) const override
Returns the minimum image of a CV based on the input location.
Eigen::Vector3d Vector3
Three-dimensional vector.
std::vector< int > Label
List of integers.