23 #include "CVs/CollectiveVariable.h"
24 #include "Validator/ObjectRequirement.h"
25 #include "Drivers/DriverException.h"
26 #include "Utility/PairwiseKernel.h"
73 std::vector<int> found1(n1, 0), found2(n2, 0);
74 for(
size_t i = 0; i < n1; ++i)
80 for(
size_t i = 0; i < n2; ++i)
86 MPI_Allreduce(MPI_IN_PLACE, found1.data(), n1, MPI_INT, MPI_SUM, snapshot.
GetCommunicator());
87 MPI_Allreduce(MPI_IN_PLACE, found2.data(), n2, MPI_INT, MPI_SUM, snapshot.
GetCommunicator());
89 unsigned ntot1 = std::accumulate(found1.begin(), found1.end(), 0, std::plus<int>());
90 unsigned ntot2 = std::accumulate(found2.begin(), found2.end(), 0, std::plus<int>());
93 "Pairwise: Expected to find " +
95 " atoms in group 1, but only found " +
96 to_string(ntot1) +
"."
101 "Pairwise: Expected to find " +
103 " atoms in group 2, but only found " +
104 to_string(ntot2) +
"."
115 std::vector<int> idx1, idx2;
131 std::vector<double> pos1(3*idx1.size()), pos2(3*idx2.size());
132 std::vector<int> id1(idx1.size()), id2(idx2.size());
133 for(
size_t i = 0; i < idx1.size(); ++i)
135 pos1[3*i+0] = positions[idx1[i]][0];
136 pos1[3*i+1] = positions[idx1[i]][1];
137 pos1[3*i+2] = positions[idx1[i]][2];
139 id1[i] = atomids[idx1[i]];
142 for(
size_t i = 0; i < idx2.size(); ++i)
144 pos2[3*i+0] = positions[idx2[i]][0];
145 pos2[3*i+1] = positions[idx2[i]][1];
146 pos2[3*i+2] = positions[idx2[i]][2];
148 id2[i] = atomids[idx2[i]];
152 pos1 = mxx::allgatherv(pos1.data(), pos1.size(), comm);
153 pos2 = mxx::allgatherv(pos2.data(), pos2.size(), comm);
154 id1 = mxx::allgatherv(id1.data(), id1.size(), comm);
155 id2 = mxx::allgatherv(id2.data(), id2.size(), comm);
158 using Map = Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, 3, Eigen::RowMajor>>;
159 Map gpos1(pos1.data(),
group1_.size(), 3), gpos2(pos2.data(),
group2_.size(), 3);
164 for(
size_t i = 0; i <
group1_.size(); ++i)
167 const auto& pi = gpos1.row(i);
168 for(
size_t j = 0; j <
group2_.size(); ++j)
171 const auto& pj = gpos2.row(j);
186 grad_[lid1] += df*rij/r;
190 grad_[lid2] -= df*rij/r;
194 boxgrad_ += df*rij/r*rij.transpose();
204 Json::CharReaderBuilder rbuilder;
205 Json::CharReader* reader = rbuilder.newCharReader();
207 reader->parse(JsonSchema::PairwiseCV.c_str(),
208 JsonSchema::PairwiseCV.c_str() + JsonSchema::PairwiseCV.size(),
210 validator.
Parse(schema, path);
217 std::vector<int> group1, group2;
219 for(
auto& s : json[
"group1"])
220 group1.push_back(s.asInt());
222 for(
auto& s : json[
"group2"])
223 group2.push_back(s.asInt());
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.
Matrix3 boxgrad_
Gradient w.r.t box vectors dCv/dHij.
std::vector< Vector3 > grad_
Gradient vector dCv/dxi.
double val_
Current value of CV.
Generalized collective variable based on pairwise properties of atoms.
Label group1_
IDs of the first group of atoms.
void Initialize(const Snapshot &snapshot) override
Initialize necessary variables.
Label group2_
IDs of the second group of atoms.
PairwiseCV(const Label &group1, const Label &group2, PairwiseKernel *pk)
Constructor.
void Evaluate(const Snapshot &snapshot) override
Evaluate the CV.
static PairwiseCV * Build(const Json::Value &json, const std::string &path)
Set up collective variable.
PairwiseKernel * pk_
Pairwise kernel function used for CV.
Pairwise kernel base class.
virtual double Evaluate(double rij, double &df) const =0
Evaluate the pairwise kernel function.
static PairwiseKernel * Build(const Json::Value &json, const std::string &path)
Build PairwiseKernel from JSON value.
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.
const Label & GetAtomIDs() const
Access the atom IDs.
void ApplyMinimumImage(Vector3 *v) const
Apply minimum image to a vector.
Eigen::Vector3d Vector3
Three-dimensional vector.
std::vector< int > Label
List of integers.
Map for histogram and coefficients.