23 #include "CVs/CollectiveVariable.h"
24 #include "Validator/ObjectRequirement.h"
25 #include "Drivers/DriverException.h"
28 #include <Eigen/Eigenvalues>
82 std::vector<int> found(n, 0);
83 for(
size_t i = 0; i < n; ++i)
89 MPI_Allreduce(MPI_IN_PLACE, found.data(), n, MPI_INT, MPI_SUM, snapshot.
GetCommunicator());
90 unsigned ntot = std::accumulate(found.begin(), found.end(), 0, std::plus<int>());
93 "GyrationTensorCV: Expected to find " +
95 " atoms, but only found " +
106 using namespace Eigen;
109 std::vector<int> idx;
114 const auto& masses = snapshot.
GetMasses();
127 std::vector<Vector3> ris;
128 ris.reserve(idx.size());
132 S.noalias() += masses[i]*ris.back()*ris.back().transpose();
136 MPI_Allreduce(MPI_IN_PLACE, S.data(), S.size(), MPI_DOUBLE, MPI_SUM, snapshot.
GetCommunicator());
141 SelfAdjointEigenSolver<Matrix3> solver;
142 solver.computeDirect(S);
143 const auto& eigvals = solver.eigenvalues();
144 const auto& eigvecs = solver.eigenvectors();
147 auto l1 = eigvals[2],
150 const auto& n1 = eigvecs.col(2),
160 auto dl1 = 2.*masses[i]/masstot*(1.-masses[i]/masstot)*ris[j].dot(n1)*n1;
161 auto dl2 = 2.*masses[i]/masstot*(1.-masses[i]/masstot)*ris[j].dot(n2)*n2;
162 auto dl3 = 2.*masses[i]/masstot*(1.-masses[i]/masstot)*ris[j].dot(n3)*n3;
169 grad_[i] = dl1 + dl2 + dl3;
185 grad_[i] = dl1 - 0.5*(dl2 + dl3);
186 val_ = l1 - 0.5*(l2 + l3);
189 grad_[i] = dl2 - dl3;
193 auto l1_2 = l1*l1, l2_2 = l2*l2, l3_2 = l3*l3;
194 auto sum = l1 + l2 + l3;
195 auto sqsum = l1_2 + l2_2 + l3_2;
196 grad_[i] = 3.*(l1*dl1+l2*dl2+l3*dl3)/(sum*sum) -
197 3.*sqsum*(dl1+dl2+dl3)/(sum*sum*sum);
198 val_ = 1.5*sqsum/(sum*sum) - 0.5;
206 static GyrationTensorCV* Build(
const Json::Value& json,
const std::string& path)
212 reader.parse(JsonSchema::GyrationTensorCV, schema);
213 validator.
Parse(schema, path);
220 std::vector<int> atomids;
221 for(
auto& s : json[
"atom_ids"])
222 atomids.push_back(s.asInt());
224 GyrationTensor component = Rg;
225 auto comp = json[
"component"].asString();
229 else if(comp ==
"principal1")
230 component = principal1;
231 else if(comp ==
"principal2")
232 component = principal2;
233 else if(comp ==
"principal3")
234 component = principal3;
235 else if(comp ==
"asphericity")
236 component = asphericity;
237 else if(comp ==
"acylindricity")
238 component = acylindricity;
239 else if(comp ==
"shapeaniso")
240 component = shapeaniso;
std::vector< int > Label
List of integers.
Eigen::Matrix3d Matrix3
3x3 matrix.
bool HasErrors()
Check if errors have occured.
unsigned GetNumAtoms() const
Get number of atoms in this snapshot.
GyrationTensor component_
Component of gyration tensor to compute.
GyrationTensorCV(const Label &atomids, GyrationTensor component)
Constructor.
double TotalMass(const Label &indices) const
Compute the total mass of a group of particles based on index.
Class containing a snapshot of the current simulation in time.
Collective variable on components of the gyration tensor.
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 Initialize(const Snapshot &snapshot) override
Initialize necessary variables.
void GetLocalIndices(const Label &ids, Label *indices) const
Exception to be thrown when building the Driver fails.
std::vector< std::string > GetErrors()
Get list of error messages.
Vector3 CenterOfMass(const Label &indices) const
const std::vector< double > & GetMasses() const
Const access to the particle masses.
Label atomids_
IDs of the atoms used for calculation.
Requirements on an object.
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.
void Evaluate(const Snapshot &snapshot) override
Evaluate the CV.
const std::vector< Vector3 > & GetPositions() const
Access the particle positions.
virtual void Validate(const Value &json, const std::string &path) override
Validate JSON value.