SSAGES  0.1
A MetaDynamics Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Groups Pages
FiniteTempString.cpp
1 
21 #include "FiniteTempString.h"
22 #include "CVs/CVManager.h"
23 #include "Snapshot.h"
24 #include "spline.h"
25 #include <math.h>
26 #include <iostream>
27 #include <algorithm>
28 
29 namespace SSAGES
30 {
31  // Check whether CV values are within their respective Voronoi cell in CV space
32  bool FiniteTempString::InCell(const CVList& cvs) const
33  {
34  std::vector<double> dists (numnodes_, 0);
35 
36  // Record the difference between all cvs and all nodes
37  for (int i = 0; i < numnodes_; i++)
38  for(size_t j = 0; j < cvs.size(); j++)
39  dists[i]+= pow(cvs[j]->GetDifference(worldstring_[i][j]),2);
40 
41  if(std::min_element(dists.begin(), dists.end()) - dists.begin() == mpiid_)
42  return true;
43 
44  return false;
45  }
46 
47  // Post-integration hook.
48  void FiniteTempString::PostIntegration(Snapshot* snapshot, const CVManager& cvmanager)
49  {
50  auto cvs = cvmanager.GetCVs(cvmask_);
51  auto& forces = snapshot->GetForces();
52  bool insidecell;
53 
55  {
56  //If the system was not going to run the umbrella anymore, reset its position
57  for(auto& force : forces)
58  {
59  force.setZero();
60  }
61 
62  auto& Pos = snapshot->GetPositions();
63  auto& IDs = snapshot->GetAtomIDs();
64 
65  for(size_t i = 0; i < prev_IDs_[0].size(); i++)
66  {
67  auto localindex = snapshot->GetLocalIndex(prev_IDs_[0][i]);
68  if(localindex!= -1)
69  {
70  Pos[localindex][0] = prev_positions_[0][i*3];
71  Pos[localindex][1] = prev_positions_[0][i*3 + 1];
72  Pos[localindex][2] = prev_positions_[0][i*3 + 2];
73  }
74  }
75  }
76  for(auto& cv : cvs)
77  {
78  //Trigger a rebuild of the CVs since we reset the positions
79  cv->Evaluate(*snapshot);
80  }
81  insidecell = InCell(cvs);
82 
83  MPI_Allreduce(MPI_IN_PLACE, &run_umbrella_, 1, MPI_INT, MPI_LOR, world_);
84 
85  if(run_umbrella_)
86  {
88  {
89  if(insidecell)
90  {
91  run_umbrella_ = false;
92  reset_for_umbrella = true;
93 
94  //This node is done initializing; so store this snapshot
95  prev_positions_[0] = snapshot->SerializePositions();
96  prev_IDs_[0] = snapshot->SerializeIDs();
97  }
98  umbrella_iter_ = 1;
99  }
100  else
101  {
102  if(!reset_for_umbrella)
103  {
104  for(size_t i = 0; i < cvs.size(); i++)
105  {
106  // Get current cv and gradient
107  auto& cv = cvs[i];
108  auto& grad = cv->GetGradient();
109 
110  // Compute dV/dCV
111  auto D = cvspring_[i]*(cv->GetDifference(centers_[i]));
112 
113  // Update forces
114  for(size_t j = 0; j < forces.size(); j++)
115  forces[j] -= D*grad[j];
116  }
117  umbrella_iter_++;
118  }
119  else
120  {
121  run_umbrella_ = false;
122  }
123  }
124  return;
125  }
126  else
127  {
128  reset_for_umbrella = false;
129  }
130 
131  if(!insidecell)
132  {
133  for(auto& force : forces)
134  force.setZero();
135 
136  auto& Pos = snapshot->GetPositions();
137  auto& IDs = snapshot->GetAtomIDs();
138 
139  for(size_t i = 0; i < prev_IDs_[0].size(); i++)
140  {
141  auto localindex = snapshot->GetLocalIndex(prev_IDs_[0][i]);
142  if(localindex!= -1)
143  {
144  Pos[localindex][0] = prev_positions_[0][i*3];
145  Pos[localindex][1] = prev_positions_[0][i*3 + 1];
146  Pos[localindex][2] = prev_positions_[0][i*3 + 2];
147  }
148  }
149 
150  // Calculate running averages for each CV at each node based on previous CV
151  for(size_t i = 0; i < newcenters_.size(); i++)
152  {
155  }
156  }
157  else
158  {
159  // Calculate running averages for each CV at each node
160  for(size_t i = 0; i < newcenters_.size(); i++)
161  {
162  newcenters_[i] = newcenters_[i] * (iteration_ * blockiterations_ + iterator_ - 1) + cvs[i]->GetMinimumImage(centers_[i]);
164  }
165 
166  prev_CVs_.clear();
167  for(size_t i = 0; i < centers_.size(); i++)
168  {
169  prev_CVs_.push_back(cvs[i]->GetMinimumImage(centers_[i]));
170  }
171  prev_positions_[0] = snapshot->SerializePositions();
172  prev_IDs_[0] = snapshot->SerializeIDs();
173  }
174 
175  // Update the string, every blockiterations_ string method iterations
176  if(iterator_ % blockiterations_ == 0)
177  {
178  MPI_Barrier(world_);
179  StringUpdate();
180  CheckEnd(cvs);
181  MPI_Barrier(world_);
182  UpdateWorldString(cvs);
183  PrintString(cvs);
184 
185  iterator_ = 0;
186  iteration_++;
187 
188  if(!InCell(cvs))
189  {
190  run_umbrella_ = true;
191  reset_for_umbrella = false;
192  }
193  MPI_Allreduce(MPI_IN_PLACE, &run_umbrella_, 1, MPI_INT, MPI_LOR, world_);
194  }
195 
196  iterator_++;
197  }
198 
200  {
201  std::vector<double> lcv0, ucv0;
202  lcv0.resize(centers_.size(), 0);
203  ucv0.resize(centers_.size(), 0);
204 
205  GatherNeighbors(&lcv0, &ucv0);
206 
207  // Update node locations toward running averages:
208  for(size_t i = 0; i < centers_.size(); i++)
209  {
210  if(mpiid_ == 0 || mpiid_ == numnodes_ - 1)
211  centers_[i] =centers_[i] - tau_ * (centers_[i] - newcenters_[i]);
212  else
213  centers_[i] = centers_[i] - tau_ * (centers_[i] - newcenters_[i]) +
214  (kappa_ * numnodes_ * tau_ * (ucv0[i] + lcv0[i] - 2 * centers_[i]));
215  }
216 
217  GatherNeighbors(&lcv0, &ucv0);
218  double alphastar = distance(centers_, lcv0);
219  StringReparam(alphastar);
220  }
221 }
double distance(const std::vector< double > &x, const std::vector< double > &y) const
Helper function for calculating distances.
Definition: StringMethod.h:102
Collective variable manager.
Definition: CVManager.h:40
bool InCell(const CVList &cvs) const
Checks if CV is in voronoi cell.
int run_umbrella_
Flag to run umbrella or not during post-integration.
void PostIntegration(Snapshot *snapshot, const class CVManager &cvmanager) override
Post-integration hook.
std::vector< CollectiveVariable * > CVList
List of Collective Variables.
Definition: types.h:51
unsigned int min_num_umbrella_steps_
Minimum number of steps to apply umbrella sampling.
void StringUpdate()
Updates the string according to the FTS method.
Class containing a snapshot of the current simulation in time.
Definition: Snapshot.h:43
void GatherNeighbors(std::vector< double > *lcv0, std::vector< double > *ucv0)
Gather neighbors over MPI.
std::vector< double > centers_
CV starting location values.
Definition: StringMethod.h:43
uint iteration_
The global method iteration.
Definition: StringMethod.h:73
std::vector< double > newcenters_
CV starting location values.
Definition: StringMethod.h:46
std::vector< double > SerializePositions()
Return the serialized positions across all local cores.
Definition: Snapshot.h:648
mxx::comm world_
Global MPI communicator.
Definition: Method.h:46
unsigned int blockiterations_
Number of steps to block average the CV's postions over.
bool CheckEnd(const CVList &CV)
Check if method reached one of the exit criteria.
int numnodes_
Number of nodes on a string.
Definition: StringMethod.h:61
std::vector< int > SerializeIDs()
Return the serialized positions across all local cores.
Definition: Snapshot.h:713
int GetLocalIndex(int id) const
Gets the local atom index corresponding to an atom ID.
Definition: Snapshot.h:555
std::vector< std::vector< int > > prev_IDs_
Store atom IDs for starting trajectories.
Definition: StringMethod.h:91
int mpiid_
The node this belongs to.
Definition: StringMethod.h:55
void UpdateWorldString(const CVList &cvs)
Update the world string over MPI.
const Label & GetAtomIDs() const
Access the atom IDs.
Definition: Snapshot.h:541
void StringReparam(double alpha_star)
Reparameterize the string.
std::vector< CollectiveVariable * > GetCVs(const std::vector< uint > &mask=std::vector< uint >()) const
Get CV iterator.
Definition: CVManager.h:80
void PrintString(const CVList &CV)
Prints the CV positions to file.
unsigned int iterator_
The local method iterator.
Definition: StringMethod.h:70
std::vector< std::vector< double > > worldstring_
The world's strings centers for each CV.
Definition: StringMethod.h:52
double tau_
Time step of string change.
std::vector< double > cvspring_
Vector of spring constants.
Definition: StringMethod.h:67
std::vector< double > prev_CVs_
Stores the last positions of the CVs.
std::vector< uint > cvmask_
Mask which identifies which CVs to act on.
Definition: Method.h:50
bool reset_for_umbrella
Flag for whether a system was to run umbrella sampling before checking against other systems...
unsigned int umbrella_iter_
Iterator that keeps track of umbrella iterations.
const std::vector< Vector3 > & GetForces() const
Access the per-particle forces.
Definition: Snapshot.h:362
const std::vector< Vector3 > & GetPositions() const
Access the particle positions.
Definition: Snapshot.h:323
std::vector< std::vector< double > > prev_positions_
Store positions for starting trajectories.
Definition: StringMethod.h:85
double kappa_
String modification parameter.