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

Calculates the inner product of all the basis functions and the histogram. More...

#include <Basis.h>

Public Member Functions

 BasisEvaluator (const std::vector< BasisFunction * > &functions)
 Constructor. More...
 
void CoeffInit ()
 Initializes coefficient lookup vector. More...
 
void BasisInit ()
 Creates lookup table for basis functions.
 
void UpdateBias (Grid< double > *bias, Grid< std::vector< double >> *grad)
 Outputs the basis projection at a specific coordinate. More...
 
double UpdateCoeff (const std::vector< double > &array, Grid< unsigned int > *hist)
 
std::vector< double > GetCoeff (void)
 Gets the coefficient array. More...
 
void SetCoeff (const std::vector< double > &coeff)
 Set the coefficient array in the event of a restart run. More...
 

Private Attributes

std::vector< Mapcoeff_
 Vector of basis function coefficients.
 
std::vector< BasisFunction * > functions_
 Vector of basis functions.
 
std::vector< BasisLUTlookup_
 Lookup table for basis functions.
 

Detailed Description

Calculates the inner product of all the basis functions and the histogram.

Definition at line 355 of file Basis.h.

Constructor & Destructor Documentation

SSAGES::BasisEvaluator::BasisEvaluator ( const std::vector< BasisFunction * > &  functions)
inline

Constructor.

Parameters
functionsVector of BasisFunction members.

Initialize the evaluator.

Definition at line 369 of file Basis.h.

369  :
370  functions_(functions)
371  {
372  CoeffInit();
373  BasisInit();
374  }
std::vector< BasisFunction * > functions_
Vector of basis functions.
Definition: Basis.h:359
void CoeffInit()
Initializes coefficient lookup vector.
Definition: Basis.cpp:100
void BasisInit()
Creates lookup table for basis functions.
Definition: Basis.cpp:132

Member Function Documentation

void SSAGES::BasisEvaluator::CoeffInit ( void  )

Initializes coefficient lookup vector.

For now, when the basis evaluator is called it will store all the values of the basis functions into a lookup table. [Subject to change]

Definition at line 100 of file Basis.cpp.

References SSAGES::BasisFunction::GetOrder().

101  {
102  double coeff_size = 1;
103  //Get the size of the number of coefficients + 1
104  for(size_t i = 0; i < functions_.size(); ++i)
105  {
106  coeff_size *= functions_[i]->GetOrder()+1;
107  }
108 
109  std::vector<int> jdx(functions_.size(), 0);
110  Map temp_map(jdx,0.0);
111  //Initialize the mapping for the coeff function
112  for(size_t i = 0; i < coeff_size; ++i)
113  {
114  for(size_t j = 0; j < jdx.size(); ++j)
115  {
116  if(jdx[j] > 0 && jdx[j] % (functions_[j]->GetOrder()+1) == 0)
117  {
118  if(j != functions_.size() - 1)
119  {
120  jdx[j+1]++;
121  jdx[j] = 0;
122  }
123  }
124  temp_map.map[j] = jdx[j];
125  temp_map.value = 0;
126  }
127  coeff_.push_back(temp_map);
128  jdx[0]++;
129  }
130  }
std::vector< BasisFunction * > functions_
Vector of basis functions.
Definition: Basis.h:359
std::vector< Map > coeff_
Vector of basis function coefficients.
Definition: Basis.h:358

Here is the call graph for this function:

std::vector<double> SSAGES::BasisEvaluator::GetCoeff ( void  )
inline

Gets the coefficient array.

Returns
Vector of current coefficients

Definition at line 402 of file Basis.h.

403  {
404  std::vector<double> coeffArray (coeff_.size(),0);
405  for (size_t i=0; i<coeff_.size(); i++)
406  {
407  coeffArray[i] = coeff_[i].value;
408  }
409  return coeffArray;
410  }
std::vector< Map > coeff_
Vector of basis function coefficients.
Definition: Basis.h:358
void SSAGES::BasisEvaluator::SetCoeff ( const std::vector< double > &  coeff)
inline

Set the coefficient array in the event of a restart run.

Parameters
coeffVector of coefficients to set

Definition at line 416 of file Basis.h.

Referenced by SSAGES::BFS::SetBasis().

417  {
418  size_t ii = 0;
419  for(auto& val : coeff_)
420  {
421  val.value = coeff[ii];
422  ii++;
423  }
424  }
std::vector< Map > coeff_
Vector of basis function coefficients.
Definition: Basis.h:358

Here is the caller graph for this function:

void SSAGES::BasisEvaluator::UpdateBias ( Grid< double > *  bias,
Grid< std::vector< double >> *  grad 
)

Outputs the basis projection at a specific coordinate.

Parameters
biasGrid of current bias
gradGrid of gradient to be applied

Definition at line 154 of file Basis.cpp.

References SSAGES::Grid< T >::begin(), SSAGES::Grid< T >::end(), SSAGES::GridBase< T >::GetLower(), SSAGES::GridBase< T >::GetNumPoints(), and SSAGES::GridBase< T >::GetUpper().

155  {
156  double basis;
157  double temp;
158  size_t j = 0;
159  int nbins;
160 
161  for(Grid<double>::iterator it = bias->begin(); it != bias->end(); ++it, ++j)
162  {
163  std::vector<double> tmp_grad (functions_.size(),0);
164  double tmp_bias = 0;
165  for (size_t i = 1; i < coeff_.size(); ++i)
166  {
167  basis = 1.0;
168  for (size_t l = 0; l < functions_.size(); l++)
169  {
170  temp = 1.0;
171  //For the gradients we only evaluate the gradient for diagonal terms
172  //Off diagonals are the basis set value
173  for (size_t k = 0; k < functions_.size(); k++)
174  {
175  nbins = bias->GetNumPoints(k);
176  temp *= l == k ? lookup_[k].derivs[it.index(k) + coeff_[i].map[k]*nbins] * functions_[l]->GetRange() / (bias->GetUpper(l) - bias->GetLower(l))
177  : lookup_[k].values[it.index(k) + coeff_[i].map[k]*nbins];
178  }
179  tmp_grad[l] -= coeff_[i].value * temp;
180  nbins = bias->GetNumPoints(l);
181  basis *= lookup_[l].values[it.index(l) + coeff_[i].map[l]*nbins];
182  }
183  //Update the bias values
184  tmp_bias += coeff_[i].value * basis;
185  //Store the gradient values
186  }
187  grad->at(it.coordinates()) = tmp_grad;
188  *it = tmp_bias;
189  }
190  }
std::vector< BasisFunction * > functions_
Vector of basis functions.
Definition: Basis.h:359
GridIterator< double > iterator
Custom iterator over a grid.
Definition: Grid.h:515
iterator begin()
Return iterator at first grid point.
Definition: Grid.h:527
std::vector< Map > coeff_
Vector of basis function coefficients.
Definition: Basis.h:358
const std::vector< double > GetLower() const
Return the lower edges of the Grid.
Definition: GridBase.h:231
const std::vector< int > GetNumPoints() const
Get the number of points for all dimensions.
Definition: GridBase.h:205
const std::vector< double > GetUpper() const
Return the upper edges of the Grid.
Definition: GridBase.h:262
std::vector< BasisLUT > lookup_
Lookup table for basis functions.
Definition: Basis.h:360
iterator end()
Return iterator after last valid grid point.
Definition: Grid.h:540

Here is the call graph for this function:

double SSAGES::BasisEvaluator::UpdateCoeff ( const std::vector< double > &  array,
Grid< unsigned int > *  hist 
)

Calculates the inner product of the basis set and the biased histogram This function then returns the coefficients from this calculation

Parameters
arrayBasis set.
histBiased histrogram.
Returns
Inner product of the basis set and the biased histogram.

Definition at line 194 of file Basis.cpp.

References SSAGES::Grid< T >::begin(), SSAGES::Grid< T >::end(), and SSAGES::GridBase< T >::GetNumPoints().

195  {
196  double coeffTemp = 0;
197  double sum = 0;
198 
199  for(auto& coeff : coeff_)
200  {
201  // The method uses a standard integration
202  size_t j = 0;
203  coeffTemp = coeff.value;
204  coeff.value = 0.0;
205  for(Grid<unsigned int>::iterator it2 = hist->begin(); it2 != hist->end(); ++it2, ++j)
206  {
207 
208  double weight = std::pow(2.0,functions_.size());
209 
210  // This adds in a trap-rule type weighting which lowers error significantly at the boundaries
211  for(size_t k = 0; k < functions_.size(); ++k)
212  {
213  if( it2.index(k) == 0 ||
214  it2.index(k) == hist->GetNumPoints(k)-1)
215  weight /= 2.0;
216  }
217  /*The numerical integration of the biased histogram across the entirety of CV space
218  *All calculations include the normalization as well
219  */
220  double basis = 1.0;
221 
222  for(size_t l = 0; l < functions_.size(); l++)
223  {
224  int nbins = hist->GetNumPoints(l);
225  basis *= lookup_[l].values[it2.index(l) + coeff.map[l]*nbins]*functions_[l]->GetRange()/nbins;
226  //Normalize the values by the associated value
227  basis *= functions_[l]->GetNorm(coeff.map[l])*functions_[l]->Weight(it2.coordinate(l));
228  }
229  coeff.value += basis * array[j] * weight/std::pow(2.0,functions_.size());
230  }
231  coeffTemp -= coeff.value;
232  //std::cout<<coeffTemp<<std::endl;
233  sum += fabs(coeffTemp);
234  }
235  return sum;
236  }
std::vector< BasisFunction * > functions_
Vector of basis functions.
Definition: Basis.h:359
GridIterator< unsigned int > iterator
Custom iterator over a grid.
Definition: Grid.h:515
iterator begin()
Return iterator at first grid point.
Definition: Grid.h:527
std::vector< Map > coeff_
Vector of basis function coefficients.
Definition: Basis.h:358
const std::vector< int > GetNumPoints() const
Get the number of points for all dimensions.
Definition: GridBase.h:205
std::vector< BasisLUT > lookup_
Lookup table for basis functions.
Definition: Basis.h:360
iterator end()
Return iterator after last valid grid point.
Definition: Grid.h:540

Here is the call graph for this function:


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