SSAGES  0.8.3
Software Suite for Advanced General Ensemble Simulations
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
SSAGES::Umbrella Class Reference

Umbrella sampling method. More...

#include <Umbrella.h>

Inheritance diagram for SSAGES::Umbrella:
Inheritance graph
[legend]

Public Member Functions

 Umbrella (const MPI_Comm &world, const MPI_Comm &comm, const std::vector< double > &kspring, const std::vector< double > &centers, std::string name, unsigned int frequency)
 Constructor. More...
 
 Umbrella (const MPI_Comm &world, const MPI_Comm &comm, const std::vector< double > &kspring, const std::vector< double > &centers0, const std::vector< double > &centers1, size_t timesteps, std::string name, unsigned int frequency)
 Constructor. More...
 
void PreSimulation (Snapshot *snapshot, const class CVManager &cvmanager) override
 Pre-simulation hook. More...
 
void PostIntegration (Snapshot *snapshot, const class CVManager &cvmanager) override
 Post-integration hook. More...
 
void PostSimulation (Snapshot *snapshot, const class CVManager &cvmanager) override
 Post-simulation hook. More...
 
void SetOutputFrequency (int outfreq)
 Set output frequency. More...
 
void SetAppend (bool append)
 Set append mode. More...
 
- Public Member Functions inherited from SSAGES::Method
 Method (unsigned int frequency, const MPI_Comm &world, const MPI_Comm &comm)
 Constructor. More...
 
void SetCVMask (const std::vector< unsigned int > &mask)
 Sets the collective variable mask.
 
virtual ~Method ()
 Destructor.
 
- Public Member Functions inherited from SSAGES::EventListener
 EventListener (unsigned int frequency)
 Constructor. More...
 
unsigned int GetFrequency () const
 Get frequency of event listener. More...
 
virtual ~EventListener ()
 Destructor.
 

Static Public Member Functions

static UmbrellaBuild (const Json::Value &json, const MPI_Comm &world, const MPI_Comm &comm, const std::string &path)
 Build a derived method from JSON node. More...
 
- Static Public Member Functions inherited from SSAGES::Method
static MethodBuildMethod (const Json::Value &json, const MPI_Comm &world, const MPI_Comm &comm, const std::string &path)
 Build a derived method from JSON node. More...
 

Private Member Functions

double GetCurrentCenter (size_t iteration, size_t i)
 Get the current center of the umbrella at a given iteration. More...
 
void PrintUmbrella (const CVList &cvs, size_t iteration)
 Print umbrella values. More...
 

Private Attributes

std::vector< double > kspring_
 Vector of spring constants.
 
size_t time_
 Amount of time over which to scale centers.
 
std::string filename_
 Output filename.
 
int outfreq_
 Frequency of outputting data.
 
std::ofstream umbrella_
 Output stream for umbrella data.
 
bool append_
 Append to output files?
 
std::vector< double > centers0_
 
std::vector< double > centers1_
 

Additional Inherited Members

- Protected Attributes inherited from SSAGES::Method
mxx::comm world_
 Global MPI communicator.
 
mxx::comm comm_
 Local MPI communicator.
 
std::vector< unsigned int > cvmask_
 Mask which identifies which CVs to act on.
 

Detailed Description

Umbrella sampling method.

Umbrella sampling method to constrain an arbitrary number of CVs at specified equilibrium distances.

Definition at line 35 of file Umbrella.h.

Constructor & Destructor Documentation

◆ Umbrella() [1/2]

SSAGES::Umbrella::Umbrella ( const MPI_Comm &  world,
const MPI_Comm &  comm,
const std::vector< double > &  kspring,
const std::vector< double > &  centers,
std::string  name,
unsigned int  frequency 
)
inline

Constructor.

Parameters
worldMPI global communicator.
commMPI local communicator.
kspringList of spring constants.
centersList of spring equilibrium positions.
nameFilename.
frequencyFrequency with which this method is applied.

Create instance of umbrella with spring constants "kspring", and centers "centers". Note the sizes of the vectors should be commensurate with the number of CVs.

Definition at line 98 of file Umbrella.h.

103  :
104  Method(frequency, world, comm), kspring_(kspring), centers0_(centers),
105  centers1_(centers), time_(0), filename_(name), outfreq_(1), append_(false)
106  {
107  }
std::vector< double > centers0_
Definition: Umbrella.h:43
size_t time_
Amount of time over which to scale centers.
Definition: Umbrella.h:47
std::vector< double > kspring_
Vector of spring constants.
Definition: Umbrella.h:39
bool append_
Append to output files?
Definition: Umbrella.h:59
int outfreq_
Frequency of outputting data.
Definition: Umbrella.h:53
std::string filename_
Output filename.
Definition: Umbrella.h:50
Method(unsigned int frequency, const MPI_Comm &world, const MPI_Comm &comm)
Constructor.
Definition: Method.h:61

◆ Umbrella() [2/2]

SSAGES::Umbrella::Umbrella ( const MPI_Comm &  world,
const MPI_Comm &  comm,
const std::vector< double > &  kspring,
const std::vector< double > &  centers0,
const std::vector< double > &  centers1,
size_t  timesteps,
std::string  name,
unsigned int  frequency 
)
inline

Constructor.

Parameters
worldMPI global communicator.
commMPI local communicator.
kspringList of spring constants.
centers0List of starting spring equilibrium positions.
centers1List of ending spring equilibrium positions.
timestepsNumber of timesteps over which to scale centers.
nameFilename.
frequencyFrequency with which this method is applied.

Create instance of umbrella with spring constants "kspring", and centers "centers". Note the sizes of the vectors should be commensurate with the number of CVs.

Definition at line 124 of file Umbrella.h.

References PostIntegration(), PostSimulation(), and PreSimulation().

131  :
132  Method(frequency, world, comm), kspring_(kspring), centers0_(centers0),
133  centers1_(centers1), time_(timesteps), filename_(name), outfreq_(1)
134  {
135  }
std::vector< double > centers0_
Definition: Umbrella.h:43
size_t time_
Amount of time over which to scale centers.
Definition: Umbrella.h:47
std::vector< double > kspring_
Vector of spring constants.
Definition: Umbrella.h:39
int outfreq_
Frequency of outputting data.
Definition: Umbrella.h:53
std::string filename_
Output filename.
Definition: Umbrella.h:50
Method(unsigned int frequency, const MPI_Comm &world, const MPI_Comm &comm)
Constructor.
Definition: Method.h:61
Here is the call graph for this function:

Member Function Documentation

◆ Build()

Umbrella * SSAGES::Umbrella::Build ( const Json::Value &  json,
const MPI_Comm &  world,
const MPI_Comm &  comm,
const std::string &  path 
)
static

Build a derived method from JSON node.

Parameters
jsonJSON Value containing all input information.
worldMPI global communicator.
commMPI local communicator.
pathPath for JSON path specification.
Returns
Pointer to the Method built. nullptr if an unknown error occurred.

This function builds a registered method from a JSON node. The difference between this function and "Build" is that this automatically determines the appropriate derived type based on the JSON node information.

Note
Object lifetime is the caller's responsibility.

Definition at line 112 of file Umbrella.cpp.

References Json::Requirement::GetErrors(), Json::Requirement::HasErrors(), Json::ObjectRequirement::Parse(), SetAppend(), SetOutputFrequency(), and Json::ObjectRequirement::Validate().

Referenced by SetAppend().

116  {
117  ObjectRequirement validator;
118  Value schema;
119  CharReaderBuilder rbuilder;
120  CharReader* reader = rbuilder.newCharReader();
121 
122  reader->parse(JsonSchema::UmbrellaMethod.c_str(),
123  JsonSchema::UmbrellaMethod.c_str() + JsonSchema::UmbrellaMethod.size(),
124  &schema, NULL);
125  validator.Parse(schema, path);
126 
127  // Validate inputs.
128  validator.Validate(json, path);
129  if(validator.HasErrors())
130  throw BuildException(validator.GetErrors());
131 
132  //TODO walker id should be obtainable in method as
133  // opposed to calculated like this.
134  unsigned int wid = mxx::comm(world).rank()/mxx::comm(comm).size();
135  bool ismulti = mxx::comm(world).size() > mxx::comm(comm).size();
136  unsigned int wcount = mxx::comm(world).size() / mxx::comm(comm).size();
137 
138  std::vector<std::vector<double>> ksprings;
139  for(auto& s : json["ksprings"])
140  {
141  std::vector<double> kspring;
142  if(s.isArray())
143  for(auto& k : s)
144  kspring.push_back(k.asDouble());
145  else
146  kspring.push_back(s.asDouble());
147 
148  ksprings.push_back(kspring);
149  }
150 
151  std::vector<std::vector<double>> centers0, centers1;
152  if(json.isMember("centers"))
153  {
154  for(auto& s : json["centers"])
155  {
156  std::vector<double> center;
157  if(s.isArray())
158  for(auto& k : s)
159  center.push_back(k.asDouble());
160  else
161  center.push_back(s.asDouble());
162 
163  centers0.push_back(center);
164  }
165  }
166  else if(json.isMember("centers0") && json.isMember("centers1") && json.isMember("timesteps"))
167  {
168  for(auto& s : json["centers0"])
169  {
170  std::vector<double> center;
171  if(s.isArray())
172  for(auto& k : s)
173  center.push_back(k.asDouble());
174  else
175  center.push_back(s.asDouble());
176 
177  centers0.push_back(center);
178  }
179 
180  for(auto& s : json["centers1"])
181  {
182  std::vector<double> center;
183  if(s.isArray())
184  for(auto& k : s)
185  center.push_back(k.asDouble());
186  else
187  center.push_back(s.asDouble());
188 
189  centers1.push_back(center);
190  }
191  }
192  else
193  throw BuildException({"Either \"centers\" or \"timesteps\", \"centers0\" and \"centers1\" must be defined for umbrella."});
194 
195  if(ksprings[0].size() != centers0[0].size())
196  throw BuildException({"Need to define a spring for every center or a center for every spring!"});
197 
198  // If only one set of center/ksprings are specified. Fill it up for multi.
199  if(ismulti)
200  {
201  if(ksprings.size() == 1)
202  for(size_t i = 1; i < wcount; ++i)
203  ksprings.push_back(ksprings[0]);
204  else if(ksprings.size() != wcount)
205  throw std::invalid_argument(path + ": Multi-walker simulations requires that the number of \"ksprings\" match the number of walkers.");
206  if(centers0.size() == 1)
207  for(size_t i = 1; i < wcount; ++i)
208  centers0.push_back(centers0[0]);
209  else if(centers0.size() != wcount)
210  throw std::invalid_argument(path + ": Multi-walker simulations requires that the number of \"centers\"/\"centers0\" match the number of walkers.");
211  if(centers1.size() == 1)
212  for(size_t i = 1; i < wcount; ++i)
213  centers1.push_back(centers1[0]);
214  else if(centers1.size()) // centers1 is optional.
215  throw std::invalid_argument(path + ": Multi-walker simulations requires that the number of \"centers1\" match the number of walkers.");
216  }
217 
218  auto freq = json.get("frequency", 1).asInt();
219 
220  size_t timesteps = 0;
221  if(json.isMember("timesteps"))
222  {
223  if(json["timesteps"].isArray())
224  timesteps = json["timesteps"][wid].asUInt();
225  else
226  timesteps = json["timesteps"].asUInt();
227  }
228 
229  std::string name = "umbrella.dat";
230  if(json["output_file"].isArray())
231  name = json["output_file"][wid].asString();
232  else if(ismulti)
233  throw std::invalid_argument(path + ": Multi-walker simulations require a separate output file for each.");
234  else
235  name = json["output_file"].asString();
236 
237  Umbrella* m = nullptr;
238  if(timesteps == 0)
239  m = new Umbrella(world, comm, ksprings[wid], centers0[wid], name, freq);
240  else
241  m = new Umbrella(world, comm, ksprings[wid], centers0[wid], centers1[wid], timesteps, name, freq);
242 
243  m->SetOutputFrequency(json.get("output_frequency",0).asInt());
244  m->SetAppend(json.get("append", false).asBool());
245 
246  return m;
247  }
bool HasErrors()
Check if errors have occured.
Definition: Requirement.h:86
Umbrella(const MPI_Comm &world, const MPI_Comm &comm, const std::vector< double > &kspring, const std::vector< double > &centers, std::string name, unsigned int frequency)
Constructor.
Definition: Umbrella.h:98
virtual void Parse(Value json, const std::string &path) override
Parse JSON value to generate Requirement(s).
std::vector< std::string > GetErrors()
Get list of error messages.
Definition: Requirement.h:92
Requirements on an object.
virtual void Validate(const Value &json, const std::string &path) override
Validate JSON value.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetCurrentCenter()

double SSAGES::Umbrella::GetCurrentCenter ( size_t  iteration,
size_t  i 
)
inlineprivate

Get the current center of the umbrella at a given iteration.

Parameters
iterationCurrent iteration of the Snapshot.
iindex (iterator) for CV.
Returns
Center of umbrella at current iteration

Definition at line 68 of file Umbrella.h.

References PrintUmbrella().

69  {
70  // We are at the end.
71  if(iteration >= time_) return centers1_[i];
72 
73  // Scale linearly.
74  return (centers1_[i] - centers0_[i])/time_*iteration + centers0_[i];
75  }
std::vector< double > centers0_
Definition: Umbrella.h:43
size_t time_
Amount of time over which to scale centers.
Definition: Umbrella.h:47
Here is the call graph for this function:

◆ PostIntegration()

void SSAGES::Umbrella::PostIntegration ( Snapshot snapshot,
const class CVManager cvmanager 
)
overridevirtual

Post-integration hook.

Parameters
snapshotSimulation snapshot.
cvmanagerCollective variable manager.

Implements SSAGES::Method.

Definition at line 57 of file Umbrella.cpp.

References SSAGES::CVManager::GetCVs(), SSAGES::Snapshot::GetForces(), SSAGES::Snapshot::GetIteration(), and SSAGES::Snapshot::GetVirial().

Referenced by Umbrella().

58  {
59  // Get necessary info.
60  auto cvs = cvmanager.GetCVs(cvmask_);
61  auto& forces = snapshot->GetForces();
62  auto& virial = snapshot->GetVirial();
63 
64  for(size_t i = 0; i < cvs.size(); ++i)
65  {
66  // Get current CV and gradient.
67  auto& cv = cvs[i];
68  auto& grad = cv->GetGradient();
69  auto& boxgrad = cv->GetBoxGradient();
70  // Compute dV/dCV.
71  auto center = GetCurrentCenter(snapshot->GetIteration(), i);
72  auto D = kspring_[i]*cv->GetDifference(center);
73 
74  // Update forces.
75  for(size_t j = 0; j < forces.size(); ++j)
76  forces[j] -= D*grad[j];
77 
78  // Update virial.
79  virial += D*boxgrad;
80  }
81 
82  if(snapshot->GetIteration() % outfreq_ == 0)
83  PrintUmbrella(cvs, snapshot->GetIteration());
84  }
void PrintUmbrella(const CVList &cvs, size_t iteration)
Print umbrella values.
Definition: Umbrella.cpp:92
double GetCurrentCenter(size_t iteration, size_t i)
Get the current center of the umbrella at a given iteration.
Definition: Umbrella.h:68
std::vector< double > kspring_
Vector of spring constants.
Definition: Umbrella.h:39
int outfreq_
Frequency of outputting data.
Definition: Umbrella.h:53
std::vector< unsigned int > cvmask_
Mask which identifies which CVs to act on.
Definition: Method.h:50
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PostSimulation()

void SSAGES::Umbrella::PostSimulation ( Snapshot snapshot,
const class CVManager cvmanager 
)
overridevirtual

Post-simulation hook.

Parameters
snapshotSimulation snapshot.
cvmanagerCollective variable manager.

Implements SSAGES::Method.

Definition at line 86 of file Umbrella.cpp.

Referenced by Umbrella().

87  {
88  if(comm_.rank() ==0)
89  umbrella_.close();
90  }
mxx::comm comm_
Local MPI communicator.
Definition: Method.h:47
std::ofstream umbrella_
Output stream for umbrella data.
Definition: Umbrella.h:56
Here is the caller graph for this function:

◆ PreSimulation()

void SSAGES::Umbrella::PreSimulation ( Snapshot snapshot,
const class CVManager cvmanager 
)
overridevirtual

Pre-simulation hook.

Parameters
snapshotSimulation snapshot.
cvmanagerCollective variable manager.

Implements SSAGES::Method.

Definition at line 33 of file Umbrella.cpp.

References SSAGES::CVManager::GetCVs().

Referenced by Umbrella().

34  {
35  if(comm_.rank() == 0)
36  {
37  if(append_)
38  umbrella_.open(filename_.c_str(), std::ofstream::out | std::ofstream::app);
39  else
40  {
41  // Write out header.
42  umbrella_.open(filename_.c_str(), std::ofstream::out);
43  umbrella_ << "#";
44  umbrella_ << "Iteration ";
45 
46  auto cvs = cvmanager.GetCVs(cvmask_);
47  for(size_t i = 0; i < cvs.size(); ++i)
48  umbrella_ << "cv_" + std::to_string(i) << " ";
49 
50  for(size_t i = 0; i < cvs.size() - 1; ++i)
51  umbrella_ << "center_" + std::to_string(i) << " ";
52  umbrella_ << "center_" + std::to_string(cvs.size() - 1) << std::endl;
53  }
54  }
55  }
mxx::comm comm_
Local MPI communicator.
Definition: Method.h:47
bool append_
Append to output files?
Definition: Umbrella.h:59
std::vector< unsigned int > cvmask_
Mask which identifies which CVs to act on.
Definition: Method.h:50
std::string filename_
Output filename.
Definition: Umbrella.h:50
std::ofstream umbrella_
Output stream for umbrella data.
Definition: Umbrella.h:56
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PrintUmbrella()

void SSAGES::Umbrella::PrintUmbrella ( const CVList cvs,
size_t  iteration 
)
private

Print umbrella values.

Parameters
cvsList of CVs.
iterationCurrent iteration.

Definition at line 92 of file Umbrella.cpp.

Referenced by GetCurrentCenter().

93  {
94  if(comm_.rank() ==0)
95  {
96  umbrella_.precision(8);
97  umbrella_ << iteration << " ";
98 
99  // Print out CV values first.
100  for(auto& cv : cvs)
101  umbrella_ << cv->GetValue() << " ";
102 
103  // Print out target (center) of each CV.
104  for(size_t i = 0; i < cvs.size() - 1; ++i)
105  umbrella_ << GetCurrentCenter(iteration, i) << " ";
106  umbrella_ << GetCurrentCenter(iteration, cvs.size() - 1);
107 
108  umbrella_ << std::endl;
109  }
110  }
mxx::comm comm_
Local MPI communicator.
Definition: Method.h:47
double GetCurrentCenter(size_t iteration, size_t i)
Get the current center of the umbrella at a given iteration.
Definition: Umbrella.h:68
std::ofstream umbrella_
Output stream for umbrella data.
Definition: Umbrella.h:56
Here is the caller graph for this function:

◆ SetAppend()

void SSAGES::Umbrella::SetAppend ( bool  append)
inline

Set append mode.

Parameters
appendWhether to enable or disable append mode.

Definition at line 171 of file Umbrella.h.

References Build().

Referenced by Build().

172  {
173  append_ = append;
174  }
bool append_
Append to output files?
Definition: Umbrella.h:59
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetOutputFrequency()

void SSAGES::Umbrella::SetOutputFrequency ( int  outfreq)
inline

Set output frequency.

Parameters
outfreqNew value for output frequency.

Definition at line 162 of file Umbrella.h.

Referenced by Build().

163  {
164  outfreq_ = outfreq;
165  }
int outfreq_
Frequency of outputting data.
Definition: Umbrella.h:53
Here is the caller graph for this function:

Member Data Documentation

◆ centers0_

std::vector<double> SSAGES::Umbrella::centers0_
private

Vector of equilibrium distances.

Definition at line 43 of file Umbrella.h.


The documentation for this class was generated from the following files: