SSAGES  0.1
A MetaDynamics Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Groups Pages
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
SSAGES::Basis Class Reference

Basis Function Sampling Algorithm. More...

#include <BasisFunc.h>

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

Public Member Functions

 Basis (const MPI_Comm &world, const MPI_Comm &comm, Histogram< int > *hist, const std::vector< unsigned int > &polyord, const std::vector< double > &restraint, const std::vector< double > &boundUp, const std::vector< double > &boundLow, unsigned int cyclefreq, unsigned int frequency, const std::string bnme, const std::string cnme, const double temperature, const double tol, const double weight, bool converge)
 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 SetIteration (const int iter)
 Set the current iteration. More...
 
void SetBasis (const std::vector< double > &coeff, std::vector< double > &unbias)
 Set the values for the basis. More...
 
 ~Basis ()
 Destructor.
 
- Public Member Functions inherited from SSAGES::Method
 Method (uint frequency, const MPI_Comm &world, const MPI_Comm &comm)
 Constructor. More...
 
void SetCVMask (const std::vector< uint > &mask)
 Sets the collective variable mask.
 
virtual ~Method ()
 Destructor.
 
- Public Member Functions inherited from SSAGES::EventListener
 EventListener (uint frequency)
 Constructor. More...
 
uint GetFrequency () const
 Get frequency of event listener. More...
 
virtual ~EventListener ()
 Destructor.
 

Static Public Member Functions

static BasisBuild (const Json::Value &json, const MPI_Comm &world, const MPI_Comm &comm, const std::string &path)
 
- 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

void UpdateBias (const CVList &cvs, const double)
 Updates the bias projection of the PMF. More...
 
void CalcBiasForce (const CVList &cvs)
 Computes the bias force. More...
 
void PrintBias (const CVList &cvs, const double beta)
 Prints the current bias to a defined file from the JSON. More...
 
void BasisInit (const CVList &cvs)
 Initializes the look up tables for polynomials. More...
 

Private Attributes

Histogram< int > * hist_
 Histogram of visited states. More...
 
std::vector< Mapcoeff_
 Globally located coefficient values. More...
 
std::vector< double > unbias_
 The biased histogram of states. More...
 
std::vector< double > coeff_arr_
 The coefficient array for restart runs.
 
std::vector< BasisLUTLUT_
 The Basis set lookup table, also defined globally.
 
std::vector< double > derivatives_
 Derivatives of the bias potential. More...
 
std::vector< unsigned int > polyords_
 The order of the basis polynomials.
 
std::vector< double > restraint_
 Spring constants for restrained system. More...
 
std::vector< double > boundUp_
 Upper position of the spring restraint.
 
std::vector< double > boundLow_
 Lower position of the spring restraint.
 
unsigned int cyclefreq_
 Frequency of coefficient updates.
 
unsigned int mpiid_
 The node that the current system belongs to, primarily for printing and debugging.
 
double weight_
 Weighting for potentially faster sampling. More...
 
double temperature_
 Self-defined temperature. More...
 
double tol_
 The tolerance criteria for the system .
 
bool bounds_
 A variable to check to see if the simulation is in bounds or not.
 
bool converge_exit_
 A check to see if you want the system to end when it reaches the convergence criteria.
 
std::ofstream basisout_
 Output stream for basis projection data.
 
std::ofstream coeffout_
 Output stream for coefficients (for reading purposes)
 
std::string bnme_
 
std::string cnme_
 Coefficient filename.
 
uint iteration_
 Iteration counter.
 

Additional Inherited Members

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

Detailed Description

Basis Function Sampling Algorithm.

Implementation of the Basis Function Sampling Method based on [4].

Definition at line 89 of file BasisFunc.h.

Constructor & Destructor Documentation

SSAGES::Basis::Basis ( const MPI_Comm &  world,
const MPI_Comm &  comm,
Histogram< int > *  hist,
const std::vector< unsigned int > &  polyord,
const std::vector< double > &  restraint,
const std::vector< double > &  boundUp,
const std::vector< double > &  boundLow,
unsigned int  cyclefreq,
unsigned int  frequency,
const std::string  bnme,
const std::string  cnme,
const double  temperature,
const double  tol,
const double  weight,
bool  converge 
)
inline

Constructor.

Parameters
worldMPI global communicator.
commMPI local communicator.
polyordOrder of Legendre polynomials.
restraintRestraint spring constants.
boundUpUpper bounds of restraint springs.
boundLowLower bounds of restraint springs.
cyclefreqCycle frequency.
frequencyFrequency with which this Method is applied.
bnmeBasis file name.
cnmeCoefficient file name.
temperatureAutomatically set temperature.
tolThreshold for tolerance criterion.
weightWeight for improved sampling.
convergeIf True quit on convergence.

Constructs an instance of the Basis function sampling method. The coefficients describes the basis projection of the system. This is updated once every cyclefreq_. For now, only the Legendre polynomial is implemented. Others will be added later.

Definition at line 240 of file BasisFunc.h.

254  :
255  Method(frequency, world, comm), hist_(hist),
256  coeff_(), unbias_(), coeff_arr_(), LUT_(), derivatives_(), polyords_(polyord),
257  restraint_(restraint), boundUp_(boundUp), boundLow_(boundLow),
258  cyclefreq_(cyclefreq), mpiid_(0), weight_(weight),
259  temperature_(temperature), tol_(tol),
260  converge_exit_(converge), bnme_(bnme), cnme_(cnme), iteration_(0)
261  {
262  }
Histogram< int > * hist_
Histogram of visited states.
Definition: BasisFunc.h:97
std::vector< double > derivatives_
Derivatives of the bias potential.
Definition: BasisFunc.h:125
unsigned int cyclefreq_
Frequency of coefficient updates.
Definition: BasisFunc.h:145
uint iteration_
Iteration counter.
Definition: BasisFunc.h:215
std::vector< double > boundUp_
Upper position of the spring restraint.
Definition: BasisFunc.h:139
std::vector< BasisLUT > LUT_
The Basis set lookup table, also defined globally.
Definition: BasisFunc.h:118
std::vector< double > coeff_arr_
The coefficient array for restart runs.
Definition: BasisFunc.h:115
std::string bnme_
Definition: BasisFunc.h:209
Method(uint frequency, const MPI_Comm &world, const MPI_Comm &comm)
Constructor.
Definition: Method.h:61
unsigned int mpiid_
The node that the current system belongs to, primarily for printing and debugging.
Definition: BasisFunc.h:148
double tol_
The tolerance criteria for the system .
Definition: BasisFunc.h:167
std::vector< double > restraint_
Spring constants for restrained system.
Definition: BasisFunc.h:136
std::vector< unsigned int > polyords_
The order of the basis polynomials.
Definition: BasisFunc.h:128
std::vector< Map > coeff_
Globally located coefficient values.
Definition: BasisFunc.h:104
double temperature_
Self-defined temperature.
Definition: BasisFunc.h:164
double weight_
Weighting for potentially faster sampling.
Definition: BasisFunc.h:156
bool converge_exit_
A check to see if you want the system to end when it reaches the convergence criteria.
Definition: BasisFunc.h:173
std::vector< double > boundLow_
Lower position of the spring restraint.
Definition: BasisFunc.h:142
std::vector< double > unbias_
The biased histogram of states.
Definition: BasisFunc.h:112
std::string cnme_
Coefficient filename.
Definition: BasisFunc.h:212

Member Function Documentation

void SSAGES::Basis::BasisInit ( const CVList cvs)
private

Initializes the look up tables for polynomials.

Parameters
cvsList of collective variables.

Definition at line 198 of file BasisFunc.cpp.

199  {
200  for( size_t k = 0; k < cvs.size(); k++)
201  {
202  size_t ncoeff = polyords_[k]+1;
203  int nbins = hist_->GetNumPoints(k);
204 
205  std::vector<double> dervs(nbins*ncoeff,0);
206  std::vector<double> vals(nbins*ncoeff,0);
207  std::vector<double> x(nbins,0);
208 
209  /*As the values for Legendre polynomials can be defined recursively, \
210  *both the derivatives and values are defined at the same time,
211  */
212  for (int i = 0; i < nbins; ++i)
213  {
214  x[i] = (2.0*i + 1.0)/nbins - 1.0;
215  vals[i] = 1.0;
216  dervs[i] = 0.0;
217  }
218 
219  for (int i = 0; i < nbins; ++i)
220  {
221  vals[i+nbins] = x[i];
222  dervs[i+nbins] = 1.0;
223  }
224 
225  for (size_t j = 2; j < ncoeff; j++)
226  {
227  for (int i = 0; i < nbins; i++)
228  {
229  //Evaluate the values of the Legendre polynomial at each bin
230  vals[i+j*nbins] = ( ( 2.0*j - 1.0 ) * x[i] * vals[i+(j-1)*nbins]
231  - (j - 1.0) * vals[i+(j-2)*nbins] ) / j;
232 
233  //Evaluate the derivatives of the Legendre polynomial at each bin
234  dervs[i+j*nbins] = ( ( 2.0*j - 1.0 ) * ( vals[i+(j-1)*nbins] + x[i] * dervs[i+(j-1)*nbins] )
235  - (j - 1.0) * dervs[i+(j-2)*nbins] ) / j;
236  }
237  }
238  BasisLUT TempLUT(vals,dervs);
239  LUT_.push_back(TempLUT);
240  }
241  }
Histogram< int > * hist_
Histogram of visited states.
Definition: BasisFunc.h:97
std::vector< BasisLUT > LUT_
The Basis set lookup table, also defined globally.
Definition: BasisFunc.h:118
const std::vector< int > & GetNumPoints() const
Get the number of points for all dimensions.
Definition: GridBase.h:138
std::vector< unsigned int > polyords_
The order of the basis polynomials.
Definition: BasisFunc.h:128
Basis * SSAGES::Basis::Build ( const Json::Value &  json,
const MPI_Comm &  world,
const MPI_Comm &  comm,
const std::string &  path 
)
static

Definition at line 521 of file BasisFunc.cpp.

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

525  {
526  ObjectRequirement validator;
527  Value schema;
528  Reader reader;
529 
530  reader.parse(JsonSchema::BFSMethod, schema);
531  validator.Parse(schema, path);
532 
533  //Validate Inputs
534  validator.Validate(json, path);
535  if(validator.HasErrors())
536  throw BuildException(validator.GetErrors());
537 
538  std::vector<unsigned int> coefsCV(0);
539  for(auto& coefs : json["CV_coefficients"])
540  coefsCV.push_back(coefs.asInt());
541 
542  std::vector<double> restrCV(0);
543  for(auto& restr : json["CV_restraint_spring_constants"])
544  restrCV.push_back(restr.asDouble());
545 
546  std::vector<double> boundLow(0);
547  for(auto& bndl : json["CV_restraint_minimums"])
548  boundLow.push_back(bndl.asDouble());
549 
550  std::vector<double> boundUp(0);
551  for(auto& bndu : json["CV_restraint_maximums"])
552  boundUp.push_back(bndu.asDouble());
553 
554  auto cyclefreq = json.get("cycle_frequency", 100000).asInt();
555  auto freq = json.get("frequency", 1).asInt();
556  auto wght = json.get("weight", 1.0).asDouble();
557  auto bnme = json.get("basis_filename", "").asString();
558  auto cnme = json.get("coeff_filename", "").asString();
559  auto temp = json.get("temperature", 0.0).asDouble();
560  auto tol = json.get("tolerance", 1e-6).asDouble();
561  auto conv = json.get("convergence_exit", false).asBool();
562 
563  Histogram<int> *hist = Histogram<int>::BuildHistogram(
564  json.get("grid", Json::Value()) );
565 
566  auto* m = new Basis(world, comm, hist, coefsCV, restrCV, boundUp, boundLow,
567  cyclefreq, freq, bnme, cnme, temp, tol, wght,
568  conv);
569 
570  if(json.isMember("iteration"))
571  m->SetIteration(json.get("iteration",0).asInt());
572 
573  if(json.isMember("coefficients") && json.isMember("bias hist"))
574  {
575  std::vector<double> coeff;
576  std::vector<double> unbias;
577 
578  for(auto& c : json["coefficients"])
579  coeff.push_back(c.asDouble());
580 
581  for(auto& u : json["bias hist"])
582  unbias.push_back(u.asDouble());
583 
584  m->SetBasis(coeff, unbias);
585  }
586 
587  return m;
588  }
bool HasErrors()
Check if errors have occured.
Definition: Requirement.h:86
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.
Basis(const MPI_Comm &world, const MPI_Comm &comm, Histogram< int > *hist, const std::vector< unsigned int > &polyord, const std::vector< double > &restraint, const std::vector< double > &boundUp, const std::vector< double > &boundLow, unsigned int cyclefreq, unsigned int frequency, const std::string bnme, const std::string cnme, const double temperature, const double tol, const double weight, bool converge)
Constructor.
Definition: BasisFunc.h:240
static Histogram< T > * BuildHistogram(const Json::Value &json)
Set up the histogram.
Definition: Histogram.h:141
virtual void Validate(const Value &json, const std::string &path) override
Validate JSON value.

Here is the call graph for this function:

void SSAGES::Basis::CalcBiasForce ( const CVList cvs)
private

Computes the bias force.

Parameters
cvsList of collective variables.

Definition at line 440 of file BasisFunc.cpp.

441  {
442  // Reset derivatives
443  std::fill(derivatives_.begin(), derivatives_.end(), 0);
444  std::vector<double> x(cvs.size(),0);
445 
446  double temp = 1.0;
447 
448  //This is calculating the derivatives for the bias force
449  for (size_t j = 0; j < cvs.size(); ++j)
450  {
451  x[j] = cvs[j]->GetValue();
452  double min = hist_->GetLower(j);
453  double max = hist_->GetUpper(j);
454 
455  if(!hist_->GetPeriodic(j))
456  {
457  // In order to prevent the index for the histogram from going out of bounds a check is in place
458  if(x[j] > max && bounds_)
459  {
460  std::cout<<"::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"<<std::endl;
461  std::cout<<"WARNING: CV is above the maximum boundary."<<std::endl;
462  std::cout<<"Statistics will not be gathered during this interval"<<std::endl;
463  std::cout<<"::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"<<std::endl;
464  bounds_ = false;
465  }
466  else if(x[j] < min && bounds_)
467  {
468  std::cout<<"::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"<<std::endl;
469  std::cout<<"WARNING: CV is below the minimum boundary."<<std::endl;
470  std::cout<<"Statistics will not be gathered during this interval"<<std::endl;
471  std::cout<<"::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"<<std::endl;
472  bounds_ = false;
473  }
474  else if(x[j] < max && x[j] > min && !bounds_)
475  {
476  std::cout<<"::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"<<std::endl;
477  std::cout<<"CV has returned in between bounds. Run is resuming"<<std::endl;
478  std::cout<<"::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"<<std::endl;
479  bounds_ = true;
480  }
481  }
482  }
483 
484  // Only apply soft wall potential in the event that it has left the boundaries
485  if(bounds_)
486  {
487  for (size_t i = 1; i < coeff_.size(); ++i)
488  {
489  for (size_t j = 0; j < cvs.size(); ++j)
490  {
491  temp = 1.0;
492  for (size_t k = 0; k < cvs.size(); ++k)
493  {
494  int nbins = hist_->GetNumPoints(k);
495  temp *= j == k ? LUT_[k].derivs[hist_->GetIndices(x)[k] + coeff_[i].map[k]*nbins] * 2.0 / (hist_->GetUpper(j) - hist_->GetLower(j))
496  : LUT_[k].values[hist_->GetIndices(x)[k] + coeff_[i].map[k]*nbins];
497  }
498  derivatives_[j] -= coeff_[i].value * temp;
499  }
500  }
501  }
502 
503  // This is where the wall potentials are going to be thrown into the method if the system is not a periodic CV
504  for(size_t j = 0; j < cvs.size(); ++j)
505  {
506  // Are these used?
507  // double min = hist_->GetLower(j);
508  // double max = hist_->GetUpper(j);
509 
510  if(!hist_->GetPeriodic(j))
511  {
512  if(x[j] > boundUp_[j])
513  derivatives_[j] -= restraint_[j] * (x[j] - boundUp_[j]);
514  else if(x[j] < boundLow_[j])
515  derivatives_[j] -= restraint_[j] * (x[j] - boundLow_[j]);
516  }
517  }
518  }
Histogram< int > * hist_
Histogram of visited states.
Definition: BasisFunc.h:97
std::vector< double > derivatives_
Derivatives of the bias potential.
Definition: BasisFunc.h:125
std::vector< double > boundUp_
Upper position of the spring restraint.
Definition: BasisFunc.h:139
std::vector< BasisLUT > LUT_
The Basis set lookup table, also defined globally.
Definition: BasisFunc.h:118
std::vector< int > GetIndices(const std::vector< double > &x) const
Return the Grid indices for a given point.
Definition: GridBase.h:296
const std::vector< bool > & GetPeriodic() const
Return the periodicity of the Grid.
Definition: GridBase.h:219
const std::vector< double > & GetLower() const
Return the lower edges of the Grid.
Definition: GridBase.h:165
std::vector< double > restraint_
Spring constants for restrained system.
Definition: BasisFunc.h:136
const std::vector< double > & GetUpper() const
Return the upper edges of the Grid.
Definition: GridBase.h:191
const std::vector< int > & GetNumPoints() const
Get the number of points for all dimensions.
Definition: GridBase.h:138
std::vector< Map > coeff_
Globally located coefficient values.
Definition: BasisFunc.h:104
bool bounds_
A variable to check to see if the simulation is in bounds or not.
Definition: BasisFunc.h:170
std::vector< double > boundLow_
Lower position of the spring restraint.
Definition: BasisFunc.h:142
void SSAGES::Basis::PostIntegration ( Snapshot snapshot,
const class CVManager cvmanager 
)
overridevirtual

Post-integration hook.

Parameters
snapshotSimulation snapshot.
cvmanagerCollective variable manager.

Implements SSAGES::Method.

Definition at line 124 of file BasisFunc.cpp.

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

125  {
126  auto cvs = cvmanager.GetCVs(cvmask_);
127  std::vector<double> x(cvs.size(),0);
128 
129  /*The binned cv space is updated at every step
130  *After a certain number of steps has been passed, the system updates a
131  *bias projection based on the visited histogram states
132  */
133  for(size_t i = 0; i < cvs.size(); ++i)
134  {
135  x[i] = cvs[i]->GetValue();
136  }
137 
138  // The histogram is updated based on the index
139  hist_->at(x) += 1;
140 
141  // Update the basis projection after a predefined number of steps
142  if(snapshot->GetIteration() % cyclefreq_ == 0) {
143  double beta;
144  beta = 1.0 / (snapshot->GetTemperature() * snapshot->GetKb());
145 
146  // For systems with poorly defined temperature (ie: 1 particle) the
147  // user needs to define their own temperature. This is a hack that
148  // will be removed in future versions.
149 
150  if(snapshot->GetTemperature() == 0)
151  {
152  beta = temperature_;
153  if(temperature_ == 0)
154  {
155  std::cout<<std::endl;
156  std::cerr<<"::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"<<std::endl;
157  std::cerr<<"ERROR: Input temperature needs to be defined for this simulation"<<std::endl;
158  std::cerr<<"Exiting on node ["<<mpiid_<<"]"<<std::endl;
159  std::cout<<"::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"<<std::endl;
160  exit(EXIT_FAILURE);
161  }
162  }
163  iteration_+= 1;
164  UpdateBias(cvs,beta);
165  std::cout<<"Node: ["<<mpiid_<<"]"<<std::setw(10)<<"\tSweep: "<<iteration_<<std::endl;
166  }
167 
168  // This calculates the bias force based on the existing basis projection.
169  CalcBiasForce(cvs);
170 
171  // Take each CV and add its biased forces to the atoms using the chain rule
172  auto& forces = snapshot->GetForces();
173  auto& virial = snapshot->GetVirial();
174  for(size_t i = 0; i < cvs.size(); ++i)
175  {
176  auto& grad = cvs[i]->GetGradient();
177  auto& boxgrad = cvs[i]->GetBoxGradient();
178 
179  /* Update the forces in snapshot by adding in the force bias from each
180  *CV to each atom based on the gradient of the CV.
181  */
182  for (size_t j = 0; j < forces.size(); ++j)
183  forces[j] += derivatives_[i]*grad[j];
184 
185  virial -= derivatives_[i]*boxgrad;
186  }
187  }
Histogram< int > * hist_
Histogram of visited states.
Definition: BasisFunc.h:97
std::vector< double > derivatives_
Derivatives of the bias potential.
Definition: BasisFunc.h:125
unsigned int cyclefreq_
Frequency of coefficient updates.
Definition: BasisFunc.h:145
uint iteration_
Iteration counter.
Definition: BasisFunc.h:215
void UpdateBias(const CVList &cvs, const double)
Updates the bias projection of the PMF.
Definition: BasisFunc.cpp:244
unsigned int mpiid_
The node that the current system belongs to, primarily for printing and debugging.
Definition: BasisFunc.h:148
std::vector< uint > cvmask_
Mask which identifies which CVs to act on.
Definition: Method.h:50
void CalcBiasForce(const CVList &cvs)
Computes the bias force.
Definition: BasisFunc.cpp:440
const T & at(const std::vector< int > &indices) const
Access Grid element read-only.
Definition: GridBase.h:452
double temperature_
Self-defined temperature.
Definition: BasisFunc.h:164

Here is the call graph for this function:

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

Post-simulation hook.

Parameters
snapshotSimulation snapshot.
cvmanagerCollective variable manager.

Implements SSAGES::Method.

Definition at line 190 of file BasisFunc.cpp.

191  {
192  std::cout<<"Run has finished"<<std::endl;
193  }
void SSAGES::Basis::PreSimulation ( Snapshot snapshot,
const class CVManager cvmanager 
)
overridevirtual

Pre-simulation hook.

Parameters
snapshotSimulation snapshot.
cvmanagerCollective variable manager.

Resize the polynomial vector so that it doesn't crash here

And now reinitialize the vector

Implements SSAGES::Method.

Definition at line 36 of file BasisFunc.cpp.

References SSAGES::CVManager::GetCVs(), and SSAGES::Snapshot::GetWalkerID().

37  {
38  auto cvs = cvmanager.GetCVs(cvmask_);
39  // For print statements and file I/O, the walker IDs are used
40  mpiid_ = snapshot->GetWalkerID();
41 
42  // Make sure the iteration index is set correctly
43  iteration_ = 0;
44 
45  // There are a few error messages / checks that are in place with
46  // defining CVs and grids
47  if(hist_->GetDimension() != cvs.size())
48  {
49  std::cerr<<"::::::::::::::::::::::::::::::::::::::::::::::::::::::"<<std::endl;
50  std::cerr<<"ERROR: Histogram dimensions doesn't match number of CVS."<<std::endl;
51  std::cerr<<"Exiting on node ["<<mpiid_<<"]"<<std::endl;
52  std::cerr<<"::::::::::::::::::::::::::::::::::::::::::::::::::::::"<<std::endl;
53  MPI_Abort(world_, EXIT_FAILURE);
54  }
55  else if(cvs.size() != polyords_.size())
56  {
57  std::cout<<cvs.size()<<std::endl;
58  std::cout<<polyords_.size()<<std::endl;
59  std::cout<<"::::::::::::::::::::::::::::::::::::::::::::::::::::::"<<std::endl;
60  std::cout<<"WARNING: The number of polynomial orders is not the same"<<std::endl;
61  std::cout<<"as the number of CVs"<<std::endl;
62  std::cout<<"The simulation will take the first defined input"<<std::endl;
63  std::cout<<"as the same for all CVs. ["<<polyords_[0]<<"]"<<std::endl;
64  std::cout<<"::::::::::::::::::::::::::::::::::::::::::::::::::::::"<<std::endl;
65 
67  polyords_.resize(cvs.size());
69  for(size_t i = 0; i < cvs.size(); ++i)
70  {
71  polyords_[i] = polyords_[0];
72  }
73  }
74 
75  // This is to check for non-periodic bounds. It comes into play in the update bias function
76  bounds_ = true;
77 
78  size_t coeff_size = 1;
79 
80  for(size_t i = 0; i < cvs.size(); ++i)
81  {
82  coeff_size *= polyords_[i]+1;
83  }
84 
85  derivatives_.resize(cvs.size());
86  unbias_.resize(hist_->size(),0);
87  coeff_arr_.resize(coeff_size,0);
88 
89  std::vector<int> idx(cvs.size(), 0);
90  std::vector<int> jdx(cvs.size(), 0);
91  Map temp_map(idx,0.0);
92 
93  // Initialize the mapping for the hist function
94  for (int &val : *hist_) {
95  val = 0;
96  }
97 
98  //Initialize the mapping for the coeff function
99  for(size_t i = 0; i < coeff_size; ++i)
100  {
101  for(size_t j = 0; j < jdx.size(); ++j)
102  {
103  if(jdx[j] > 0 && jdx[j] % (polyords_[j]+1) == 0)
104  {
105  if(j != cvs.size() - 1)
106  {
107  jdx[j+1]++;
108  jdx[j] = 0;
109  }
110  }
111  temp_map.map[j] = jdx[j];
112  temp_map.value = 0;
113  }
114  coeff_.push_back(temp_map);
115  coeff_[i].value = coeff_arr_[i];
116  jdx[0]++;
117  }
118 
119  //Initialize the look-up table.
120  BasisInit(cvs);
121  }
Histogram< int > * hist_
Histogram of visited states.
Definition: BasisFunc.h:97
std::vector< double > derivatives_
Derivatives of the bias potential.
Definition: BasisFunc.h:125
uint iteration_
Iteration counter.
Definition: BasisFunc.h:215
mxx::comm world_
Global MPI communicator.
Definition: Method.h:46
std::vector< double > coeff_arr_
The coefficient array for restart runs.
Definition: BasisFunc.h:115
size_t size() const
Get the size of the internal storage vector.
Definition: GridBase.h:250
void BasisInit(const CVList &cvs)
Initializes the look up tables for polynomials.
Definition: BasisFunc.cpp:198
unsigned int mpiid_
The node that the current system belongs to, primarily for printing and debugging.
Definition: BasisFunc.h:148
size_t GetDimension() const
Get the dimension.
Definition: GridBase.h:128
std::vector< uint > cvmask_
Mask which identifies which CVs to act on.
Definition: Method.h:50
std::vector< unsigned int > polyords_
The order of the basis polynomials.
Definition: BasisFunc.h:128
std::vector< Map > coeff_
Globally located coefficient values.
Definition: BasisFunc.h:104
bool bounds_
A variable to check to see if the simulation is in bounds or not.
Definition: BasisFunc.h:170
std::vector< double > unbias_
The biased histogram of states.
Definition: BasisFunc.h:112

Here is the call graph for this function:

void SSAGES::Basis::PrintBias ( const CVList cvs,
const double  beta 
)
private

Prints the current bias to a defined file from the JSON.

Parameters
cvsList of collective variables.
betaScale parameter.

Definition at line 365 of file BasisFunc.cpp.

References SSAGES::Histogram< T >::begin().

366  {
367  std::vector<double> bias(hist_->size(), 0);
368  std::vector<double> x(cvs.size(), 0);
369  double temp = 1.0;
370 
371  /* Since the coefficients are the only piece that needs to be
372  *updated, the bias is only evaluated when printing
373  */
374  size_t i = 0;
375  for(Histogram<int>::iterator it = hist_->begin(); it != hist_->end(); ++it, ++i)
376  {
377  if (it.isUnderOverflowBin()) {
378  --i;
379  continue;
380  }
381 
382  for(size_t j = 1; j < coeff_.size(); ++j)
383  {
384  for(size_t k = 0; k < cvs.size(); ++k)
385  {
386  int nbins = hist_->GetNumPoints(k);
387  temp *= LUT_[k].values[it.index(k) + coeff_[j].map[k] * nbins];
388  }
389  bias[i] += coeff_[j].value*temp;
390  temp = 1.0;
391  }
392  }
393 
394  // The filenames will have a standard name, with a user-defined suffix
395  std::string filename1 = "basis"+bnme_+".out";
396  std::string filename2 = "coeff"+cnme_+".out";
397 
398  basisout_.precision(5);
399  coeffout_.precision(5);
400  basisout_.open(filename1.c_str());
401  coeffout_.open(filename2.c_str());
402 
403  // The CV values, PMF projection, PMF, and biased histogram are output for the user
404  coeffout_ << iteration_ <<std::endl;
405  basisout_ << "CV Values" << std::setw(35*cvs.size()) << "Basis Set Bias" << std::setw(35) << "PMF Estimate" << std::setw(35) << "Biased Histogram" << std::endl;
406 
407  size_t j = 0;
408  for(Histogram<int>::iterator it = hist_->begin(); it != hist_->end(); ++it, ++j)
409  {
410  if (it.isUnderOverflowBin()) {
411  --j;
412  continue;
413  }
414 
415  for(size_t k = 0; k < cvs.size(); ++k)
416  {
417  // Evaluate the CV values for printing purposes
418  basisout_ << it.coordinate(k) << std::setw(35);
419  }
420  basisout_ << -bias[j] << std::setw(35);
421  if(unbias_[j])
422  basisout_ << -log(unbias_[j]) / beta << std::setw(35);
423  else
424  basisout_ << "0" << std::setw(35);
425  basisout_ << unbias_[j];
426  basisout_ << std::endl;
427  }
428 
429  for(size_t k = 0; k < coeff_.size(); ++k)
430  {
431  coeffout_ <<coeff_[k].value << std::endl;
432  }
433 
434  basisout_ << std::endl;
435  basisout_.close();
436  coeffout_.close();
437  }
Histogram< int > * hist_
Histogram of visited states.
Definition: BasisFunc.h:97
uint iteration_
Iteration counter.
Definition: BasisFunc.h:215
HistIterator< int > iterator
Custom iterator over a histogram.
Definition: Histogram.h:537
std::vector< BasisLUT > LUT_
The Basis set lookup table, also defined globally.
Definition: BasisFunc.h:118
size_t size() const
Get the size of the internal storage vector.
Definition: GridBase.h:250
std::string bnme_
Definition: BasisFunc.h:209
iterator end()
Return iterator after last valid bin.
Definition: Histogram.h:571
std::ofstream basisout_
Output stream for basis projection data.
Definition: BasisFunc.h:202
const std::vector< int > & GetNumPoints() const
Get the number of points for all dimensions.
Definition: GridBase.h:138
std::ofstream coeffout_
Output stream for coefficients (for reading purposes)
Definition: BasisFunc.h:205
iterator begin()
Return iterator at first bin of histogram.
Definition: Histogram.h:549
std::vector< Map > coeff_
Globally located coefficient values.
Definition: BasisFunc.h:104
std::vector< double > unbias_
The biased histogram of states.
Definition: BasisFunc.h:112
std::string cnme_
Coefficient filename.
Definition: BasisFunc.h:212

Here is the call graph for this function:

void SSAGES::Basis::SetBasis ( const std::vector< double > &  coeff,
std::vector< double > &  unbias 
)
inline

Set the values for the basis.

Parameters
coeffList of coefficients.
unbiasList of unbiased values.

This function is used to set starting values at the beginning of a run. For example when continuing from a restart value.

Definition at line 305 of file BasisFunc.h.

References coeff_arr_, and unbias_.

306  {
307  coeff_arr_ = coeff;
308  unbias_ = unbias;
309  }
std::vector< double > coeff_arr_
The coefficient array for restart runs.
Definition: BasisFunc.h:115
std::vector< double > unbias_
The biased histogram of states.
Definition: BasisFunc.h:112
void SSAGES::Basis::SetIteration ( const int  iter)
inline

Set the current iteration.

Parameters
iterNew value for the current iteration.

This function is used to set the current iteration, for example when continuing from a restart.

Definition at line 292 of file BasisFunc.h.

References iteration_.

293  {
294  iteration_ = iter;
295  }
uint iteration_
Iteration counter.
Definition: BasisFunc.h:215
void SSAGES::Basis::UpdateBias ( const CVList cvs,
const double  beta 
)
private

Updates the bias projection of the PMF.

Parameters
cvsList of collective variables.
betaTemperature equivalent.

Definition at line 244 of file BasisFunc.cpp.

References SSAGES::Histogram< T >::begin(), and SSAGES::GridBase< T >::data().

245  {
246  std::vector<double> x(cvs.size(), 0);
247  std::vector<double> coeffTemp(coeff_.size(), 0);
248  double sum = 0.0;
249  double bias = 0.0;
250  double basis = 1.0;
251 
252  // For multiple walkers, the struct is unpacked
253  Histogram<int> histlocal(*hist_);
254 
255  // Summed between all walkers
256  MPI_Allreduce(histlocal.data(), hist_->data(), hist_->size(), MPI_INT, MPI_SUM, world_);
257 
258  // Construct the biased histogram
259  size_t i = 0;
260  for (Histogram<int>::iterator it2 = hist_->begin(); it2 != hist_->end(); ++it2, ++i)
261  {
262  if (it2.isUnderOverflowBin()) {
263  --i;
264  continue;
265  }
266 
267  // This is to make sure that the CV projects across the entire surface
268  if (*it2 == 0) { *it2 = 1; }
269 
270  // The loop builds the previous basis projection for each bin of the histogram
271  for(size_t k = 1; k < coeff_.size(); ++k)
272  {
273  auto& coeff = coeff_[k];
274  for(size_t l = 0; l < cvs.size(); ++l)
275  {
276  // The previous bias is only calculated after each sweep has happened
277  int nbins = hist_->GetNumPoints(l);
278  basis *= LUT_[l].values[it2.index(l) + coeff.map[l]*nbins];
279  }
280  bias += coeff.value*basis;
281  basis = 1.0;
282  }
283 
284  /* The evaluation of the biased histogram which projects the histogram to the
285  * current bias of CV space.
286  */
287  unbias_[i] += (*it2) * exp(bias) * weight_ / (double)(cyclefreq_);
288  bias = 0.0;
289 
290  }
291 
292  // The coefficients and histograms are reset after evaluating the biased histogram values
293  for(size_t i = 0; i < coeff_.size(); ++i)
294  {
295  coeffTemp[i] = coeff_[i].value;
296  coeff_[i].value = 0.0;
297  }
298 
299  // Reset histogram
300  for (int &val : *hist_) {
301  val = 0;
302  }
303 
304  // The loop that evaluates the new coefficients by integrating the CV space
305  for(size_t i = 1; i < coeff_.size(); ++i)
306  {
307  auto& coeff = coeff_[i];
308 
309  // The method uses a standard integration with trap rule weights
310  size_t j = 0;
311  for(Histogram<int>::iterator it2 = hist_->begin(); it2 != hist_->end(); ++it2, ++j)
312  {
313  if (it2.isUnderOverflowBin()) {
314  --j;
315  continue;
316  }
317 
318  double weight = std::pow(2.0,cvs.size());
319 
320  // This adds in a trap-rule type weighting which lowers error significantly at the boundaries
321  for(size_t k = 0; k < cvs.size(); ++k)
322  {
323  if( it2.index(k) == 0 ||
324  it2.index(k) == hist_->GetNumPoints(k)-1)
325  weight /= 2.0;
326  }
327 
328  /*The numerical integration of the biased histogram across the entirety of CV space
329  *All calculations include the normalization as well
330  */
331  for(size_t l = 0; l < cvs.size(); l++)
332  {
333  int nbins = hist_->GetNumPoints(l);
334  basis *= LUT_[l].values[it2.index(l) + coeff.map[l]*nbins] / nbins;
335  basis *= 2.0 * coeff.map[l] + 1.0;
336  }
337  coeff.value += basis * log(unbias_[j]) * weight/std::pow(2.0,cvs.size());
338  basis = 1.0;
339  }
340  coeffTemp[i] -= coeff.value;
341  coeff_arr_[i] = coeff.value;
342  sum += coeffTemp[i]*coeffTemp[i];
343  }
344 
345  if(world_.rank() == 0)
346  // Write coeff at this step, but only one walker
347  PrintBias(cvs,beta);
348 
349  // The convergence tolerance and whether the user wants to exit are incorporated here
350  if(sum < tol_)
351  {
352  std::cout<<"System has converged"<<std::endl;
353  if(converge_exit_)
354  {
355  std::cout<<"User has elected to exit. System is now exiting"<<std::endl;
356  exit(EXIT_SUCCESS);
357  }
358  }
359  }
Histogram< int > * hist_
Histogram of visited states.
Definition: BasisFunc.h:97
unsigned int cyclefreq_
Frequency of coefficient updates.
Definition: BasisFunc.h:145
void PrintBias(const CVList &cvs, const double beta)
Prints the current bias to a defined file from the JSON.
Definition: BasisFunc.cpp:365
HistIterator< int > iterator
Custom iterator over a histogram.
Definition: Histogram.h:537
std::vector< BasisLUT > LUT_
The Basis set lookup table, also defined globally.
Definition: BasisFunc.h:118
mxx::comm world_
Global MPI communicator.
Definition: Method.h:46
std::vector< double > coeff_arr_
The coefficient array for restart runs.
Definition: BasisFunc.h:115
size_t size() const
Get the size of the internal storage vector.
Definition: GridBase.h:250
double tol_
The tolerance criteria for the system .
Definition: BasisFunc.h:167
iterator end()
Return iterator after last valid bin.
Definition: Histogram.h:571
const std::vector< int > & GetNumPoints() const
Get the number of points for all dimensions.
Definition: GridBase.h:138
iterator begin()
Return iterator at first bin of histogram.
Definition: Histogram.h:549
std::vector< Map > coeff_
Globally located coefficient values.
Definition: BasisFunc.h:104
T * data()
Get pointer to the internal data storage vector.
Definition: GridBase.h:262
double weight_
Weighting for potentially faster sampling.
Definition: BasisFunc.h:156
bool converge_exit_
A check to see if you want the system to end when it reaches the convergence criteria.
Definition: BasisFunc.h:173
std::vector< double > unbias_
The biased histogram of states.
Definition: BasisFunc.h:112

Here is the call graph for this function:

Member Data Documentation

std::string SSAGES::Basis::bnme_
private

The option to name both the basis and coefficient files will be given Basis filename

Definition at line 209 of file BasisFunc.h.

std::vector<Map> SSAGES::Basis::coeff_
private

Globally located coefficient values.

As coefficients are updated at the same time, the coefficients should only be defined globally.

Definition at line 104 of file BasisFunc.h.

std::vector<double> SSAGES::Basis::derivatives_
private

Derivatives of the bias potential.

The derivatives of the bias potential imposed on the system. They are evaluated by using the lookup table.

Definition at line 125 of file BasisFunc.h.

Histogram<int>* SSAGES::Basis::hist_
private

Histogram of visited states.

Histogram is stored locally.

Definition at line 97 of file BasisFunc.h.

Referenced by ~Basis().

std::vector<double> SSAGES::Basis::restraint_
private

Spring constants for restrained system.

The system uses this to determine if the system is to be restrained on the defined interval. The user inputs the spring constants if the system is not periodic.

Definition at line 136 of file BasisFunc.h.

double SSAGES::Basis::temperature_
private

Self-defined temperature.

In the case of the MD engine using a poorly defined temperature, the option to throw it into the method is available. If not provided it takes the value from the engine.

Definition at line 164 of file BasisFunc.h.

std::vector<double> SSAGES::Basis::unbias_
private

The biased histogram of states.

The biased histogram of states has the form hist_*exp(phi*beta), where phi is the bias potential and beta is the inverse of the temperature. It is defined globally.

Definition at line 112 of file BasisFunc.h.

Referenced by SetBasis().

double SSAGES::Basis::weight_
private

Weighting for potentially faster sampling.

Weighting can be used to potentially sample faster, however it can cause the simulation to explode. By default this value will be set to 1.0

Definition at line 156 of file BasisFunc.h.


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