23 #include "CVs/CollectiveVariable.h"
24 #include "Validator/ObjectRequirement.h"
25 #include "Drivers/DriverException.h"
26 #include "Utility/PairwiseKernel.h"
72 std::vector<int> found1(n1, 0), found2(n2, 0);
73 for(
size_t i = 0; i < n1; ++i)
79 for(
size_t i = 0; i < n2; ++i)
85 MPI_Allreduce(MPI_IN_PLACE, found1.data(), n1, MPI_INT, MPI_SUM, snapshot.
GetCommunicator());
86 MPI_Allreduce(MPI_IN_PLACE, found2.data(), n2, MPI_INT, MPI_SUM, snapshot.
GetCommunicator());
88 unsigned ntot1 = std::accumulate(found1.begin(), found1.end(), 0, std::plus<int>());
89 unsigned ntot2 = std::accumulate(found2.begin(), found2.end(), 0, std::plus<int>());
92 "PairwiseCV: Expected to find " +
94 " atoms in group 1, but only found " +
95 to_string(ntot1) +
"."
100 "PairwiseCV: Expected to find " +
102 " atoms in group 2, but only found " +
103 to_string(ntot2) +
"."
114 std::vector<int> idx1, idx2;
130 std::vector<double> pos1(3*idx1.size()), pos2(3*idx2.size());
131 std::vector<int> id1(idx1.size()), id2(idx2.size());
132 for(
size_t i = 0; i < idx1.size(); ++i)
134 pos1[3*i+0] = positions[idx1[i]][0];
135 pos1[3*i+1] = positions[idx1[i]][1];
136 pos1[3*i+2] = positions[idx1[i]][2];
138 id1[i] = atomids[idx1[i]];
141 for(
size_t i = 0; i < idx2.size(); ++i)
143 pos2[3*i+0] = positions[idx2[i]][0];
144 pos2[3*i+1] = positions[idx2[i]][1];
145 pos2[3*i+2] = positions[idx2[i]][2];
147 id2[i] = atomids[idx2[i]];
151 pos1 = mxx::allgatherv(pos1.data(), pos1.size(), comm);
152 pos2 = mxx::allgatherv(pos2.data(), pos2.size(), comm);
153 id1 = mxx::allgatherv(id1.data(), id1.size(), comm);
154 id2 = mxx::allgatherv(id2.data(), id2.size(), comm);
157 using Map = Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, 3, Eigen::RowMajor>>;
158 Map gpos1(pos1.data(),
group1_.size(), 3), gpos2(pos2.data(),
group2_.size(), 3);
163 for(
size_t i = 0; i <
group1_.size(); ++i)
166 const auto& pi = gpos1.row(i);
167 for(
size_t j = 0; j <
group2_.size(); ++j)
170 const auto& pj = gpos2.row(j);
185 grad_[lid1] += df*rij/r;
189 grad_[lid2] -= df*rij/r;
193 boxgrad_ += df*rij/r*rij.transpose();
198 static PairwiseCV* Build(
const Json::Value& json,
const std::string& path)
204 reader.parse(JsonSchema::PairwiseCV, schema);
205 validator.
Parse(schema, path);
212 std::vector<int> group1, group2;
214 for(
auto& s : json[
"group1"])
215 group1.push_back(s.asInt());
217 for(
auto& s : json[
"group2"])
218 group2.push_back(s.asInt());
std::vector< int > Label
List of integers.
bool HasErrors()
Check if errors have occured.
unsigned GetNumAtoms() const
Get number of atoms in this snapshot.
Class containing a snapshot of the current simulation in time.
Label group1_
IDs of the first group of atoms.
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.
Map for histogram and coefficients.
void GetLocalIndices(const Label &ids, Label *indices) const
Exception to be thrown when building the Driver fails.
virtual double Evaluate(double rij, double &df) const =0
Evaluate the pairwise kernel function.
const Label & GetAtomIDs() const
Access the atom IDs.
PairwiseCV(const Label &group1, const Label &group2, PairwiseKernel *pk)
Constructor.
std::vector< std::string > GetErrors()
Get list of error messages.
void Initialize(const Snapshot &snapshot) override
Initialize necessary variables.
Requirements on an object.
Pairwise kernel base class.
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.
Matrix3 boxgrad_
Gradient w.r.t box vectors dCv/dHij.
Generalized collective variable based on pairwise properties of atoms.
const std::vector< Vector3 > & GetPositions() const
Access the particle positions.
PairwiseKernel * pk_
Pairwise kernel function used for CV.
Label group2_
IDs of the second group of atoms.
virtual void Validate(const Value &json, const std::string &path) override
Validate JSON value.
static PairwiseKernel * Build(const Json::Value &json, const std::string &path)
Build PairwiseKernel from JSON value.