23 #include "CollectiveVariable.h"
24 #include "Validator/ObjectRequirement.h"
25 #include "Drivers/DriverException.h"
47 std::vector<Vector3>
r_;
68 for (
size_t i = 0; i <
N_; ++i) {
86 throw std::invalid_argument(
87 "RouseModeCV: Expected to find p to be less than " +
88 to_string(
groups_.size()) +
" but found p = " +
92 for (
size_t j = 0; j <
groups_.size(); ++j) {
95 std::vector<int> found(nj,0);
96 for (
size_t i = 0; i < nj; ++i) {
101 MPI_Allreduce(MPI_IN_PLACE, found.data(), nj, MPI_INT, MPI_SUM, snapshot.
GetCommunicator());
102 unsigned njtot = std::accumulate(found.begin(), found.end(), 0, std::plus<int>());
105 throw std::invalid_argument(
106 "RouseModeCV: Expected to find " +
108 " atoms in group " + to_string(j) +
", but only found " +
109 to_string(njtot) +
"."
126 const auto& masses = snapshot.
GetMasses();
129 double ppi_n =
p_ * M_PI /
N_;
136 std::vector<Vector3> rcom;
137 for (
size_t i = 0; i <
N_; ++i) {
148 for (
size_t i = 1; i <
N_; ++i) {
150 r_[i] =
r_[i-1] + dri;
158 for (
size_t i = 0; i <
N_; ++i) {
159 xp_ +=
r_[i]*cos ( ppi_n * (i+0.5) );
162 if (
p_ != 0 )
xp_ *= sqrt(2.0) ;
172 if (
p_ != 0) gradpcon *= sqrt(2.0);
173 for (
size_t i = 0; i <
N_; ++i) {
183 double cosval = cos(ppi_n*(i+0.5)) /
massg_[i];
184 for (
auto&
id : idi) {
185 grad_[id] += gradpcon* cosval * masses[id];
196 Json::CharReaderBuilder rbuilder;
197 Json::CharReader* reader = rbuilder.newCharReader();
199 reader->parse(JsonSchema::RouseModeCV.c_str(),
200 JsonSchema::RouseModeCV.c_str() + JsonSchema::RouseModeCV.size(),
202 validator.
Parse(schema, path);
209 std::vector<Label> groups;
210 for (
auto& group : json[
"groups"])
212 groups.push_back({});
213 for(
auto&
id : group)
214 groups.back().push_back(
id.asInt());
217 auto mode = json.get(
"mode",0).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.
std::vector< Vector3 > grad_
Gradient vector dCv/dxi.
double val_
Current value of CV.
Collective variable is a Rouse mode for a polymer chain comprised of N particle groups.
void Evaluate(const Snapshot &snapshot) override
Evaluate the CV.
static RouseModeCV * Build(const Json::Value &json, const std::string &path)
Set up collective variable.
size_t N_
number of Rouse beads in polymer chain
std::vector< Vector3 > r_
vector of coordinate positions for each bead
void setMasses(const std::vector< Label > &groups, const Snapshot &snapshot)
Helper function to determine masses of each group.
std::vector< Label > groups_
vector of groups of indices to define the particle groups
void Initialize(const Snapshot &snapshot) override
Initialize necessary variables.
Vector3 xp_
3d vector for containing vectorial rouse amplitude
RouseModeCV(const std::vector< Label > &groups, int p)
Basic Constructor for Rouse Mode CV.
size_t p_
index of mode of interest as CV
std::vector< double > massg_
vector of the total mass for each particle group
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.
double TotalMass(const Label &indices) const
Compute the total mass of a group of particles based on index.
const std::vector< double > & GetMasses() const
Const access to the particle masses.
Vector3 CenterOfMass(const Label &indices, bool mass_weight=true) const
Compute center of mass of a group of atoms based on index.
void ApplyMinimumImage(Vector3 *v) const
Apply minimum image to a vector.
Eigen::Vector3d Vector3
Three-dimensional vector.
std::vector< int > Label
List of integers.