SSAGES  0.9.3
Software Suite for Advanced General Ensemble Simulations
Classes | Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | List of all members
SSAGES::Grid< T > Class Template Reference

Basic Grid. More...

#include <Grid.h>

Inheritance diagram for SSAGES::Grid< T >:
Inheritance graph
[legend]

Classes

class  GridIterator
 Custom Iterator. More...
 

Public Types

typedef GridIterator< T > iterator
 Custom iterator over a grid.
 
typedef GridIterator< const T > const_iterator
 Custom constant iterator over a grid.
 

Public Member Functions

 Grid (std::vector< int > numPoints, std::vector< double > lower, std::vector< double > upper, std::vector< bool > isPeriodic)
 Constructor. More...
 
iterator begin ()
 Return iterator at first grid point. More...
 
iterator end ()
 Return iterator after last valid grid point. More...
 
const_iterator begin () const
 Return const iterator at first grid point. More...
 
const_iterator end () const
 Return const iterator after last valid grid point. More...
 
void WriteToFile (const std::string &filename)
 Write grid out to file. More...
 
void LoadFromFile (const std::string &filename)
 Builds a grid from file. More...
 
- Public Member Functions inherited from SSAGES::GridBase< T >
void syncGrid ()
 Sync the grid.
 
size_t GetDimension () const
 Get the dimension. More...
 
const std::vector< int > GetNumPoints () const
 Get the number of points for all dimensions. More...
 
int GetNumPoints (size_t dim) const
 Get the number of points for a specific dimension. More...
 
const std::vector< double > GetLower () const
 Return the lower edges of the Grid. More...
 
double GetLower (size_t dim) const
 Get the lower edge for a specific dimension. More...
 
const std::vector< double > GetUpper () const
 Return the upper edges of the Grid. More...
 
double GetUpper (size_t dim) const
 Get the upper edge for a specific dimension. More...
 
const std::vector< bool > & GetPeriodic () const
 Return the periodicity of the Grid. More...
 
bool GetPeriodic (size_t dim) const
 Get the periodicity in a specific dimension. More...
 
size_t size () const
 Get the size of the internal storage vector. More...
 
T * data ()
 Get pointer to the internal data storage vector. More...
 
T const * data () const
 Get pointer to const of the internal data storage vector. More...
 
std::vector< int > GetIndices (const std::vector< double > &x) const
 Return the Grid indices for a given point. More...
 
int GetIndex (double x) const
 Return linear interpolation on a coordinate. More...
 
std::vector< double > GetCoordinates (const std::vector< int > &indices)
 ! Return the distance to adjacent grid center points from given coordinates More...
 
double GetCoordinate (int index)
 Return center point of 1d-grid. More...
 
const T & at (const std::vector< int > &indices) const
 Access Grid element read-only. More...
 
T & at (const std::vector< int > &indices)
 Access Grid element read/write. More...
 
template<typename R >
const T & at (std::initializer_list< R > &&x) const
 Const access of Grid element via initializer list. More...
 
template<typename R >
T & at (std::initializer_list< R > &&x)
 Access Grid element via initializer list. More...
 
const T & at (int index) const
 Access 1d Grid by index, read-only. More...
 
T & at (int index)
 Access 1d Grid by index, read-write. More...
 
const T & at (const std::vector< double > &x) const
 Access Grid element pertaining to a specific point – read-only. More...
 
T & at (const std::vector< double > &x)
 Access Grid element pertaining to a specific point – read/write. More...
 
const T & at (double x) const
 Access 1d-Grid by point - read-only. More...
 
T & at (double x)
 Access 1d-Grid by point - read-write. More...
 
const T & operator[] (const std::vector< int > &indices) const
 Access Grid element per [] read-only. More...
 
T & operator[] (const std::vector< int > &indices)
 Access Grid element per [] read-write. More...
 
template<typename R >
const T & operator[] (std::initializer_list< R > &&x) const
 Const access of Grid element via initializer list. More...
 
template<typename R >
T & operator[] (std::initializer_list< R > &&x)
 Access Grid element via initializer list. More...
 
const T & operator[] (int index) const
 Access 1d-Grid per [] operator, read-only. More...
 
T & operator[] (int index)
 Access 1d-Grid per [] operator, read-write. More...
 
const T & operator[] (const std::vector< double > &x) const
 Access Grid element pertaining to a specific point per [] read-only. More...
 
T & operator[] (const std::vector< double > &x)
 Access Grid element pertaining to a specific point per [] read-write. More...
 
const T & operator[] (double x) const
 Access 1d-Grid via specific point, read-only. More...
 
T & operator[] (double x)
 Access 1d-Grid via specific point, read-write. More...
 
virtual ~GridBase ()
 Destructor.
 

Static Public Member Functions

static Grid< T > * BuildGrid (const Json::Value &json)
 Set up the grid. More...
 
static Grid< T > * BuildGrid (const Json::Value &json, const std::string &path)
 Set up the grid. More...
 

Private Member Functions

size_t mapTo1d (const std::vector< int > &indices) const override
 Map d-dimensional indices to 1-d data vector. More...
 

Additional Inherited Members

- Protected Member Functions inherited from SSAGES::GridBase< T >
std::vector< int > wrapIndices (const std::vector< int > &indices) const
 Wrap the index around periodic boundaries. More...
 
 GridBase (std::vector< int > numPoints, std::vector< double > lower, std::vector< double > upper, std::vector< bool > isPeriodic)
 Constructor. More...
 
- Protected Attributes inherited from SSAGES::GridBase< T >
std::vector< T > data_
 Internal storage of the data.
 
size_t dimension_
 Dimension of the grid.
 
std::vector< int > numPoints_
 Number of points in each dimension.
 
std::pair< std::vector< double >, std::vector< double > > edges_
 Edges of the Grid in each dimension.
 
std::vector< bool > isPeriodic_
 Periodicity of the Grid.
 

Detailed Description

template<typename T>
class SSAGES::Grid< T >

Basic Grid.

Template Parameters
Ttype of data to be stored in the grid.

A Grid is a method to store data in SSAGES. It is used to discretize a continuous number, typically a collective variable or a function, into number_points grid points. At each grid point, an arbitrary type of data can be stored, specified via the template parameter T.

The grid can be of arbitrary dimension. For each dimension, the lower bound, the upper bound and the number of grid points need to be specified. Furthermore, the grid can be defined as periodic or non-periodic in the respective dimension. By default, the grid is non-periodic. The grid points are indexed from 0 to number_points-1 following the standard C/C++ convention.

The grid spacing Delta is given by (upper - lower)/number_points. Thus, grid point n corresponds to the interval [lower + n*Delta, lower + (n+1)*Delta). Note that n follows the C/C++ convention, i.e. n = 0 for the first interval. The bin indices pertaining to a given point can be obtained via GetIndices().

Definition at line 58 of file Grid.h.

Constructor & Destructor Documentation

◆ Grid()

template<typename T >
SSAGES::Grid< T >::Grid ( std::vector< int >  numPoints,
std::vector< double >  lower,
std::vector< double >  upper,
std::vector< bool >  isPeriodic 
)
inline

Constructor.

Parameters
numPointsNumber of grid points in each dimension.
lowerLower edges of the grid.
upperUpper edges of the grid.
isPeriodicBools specifying the periodicity in the respective dimension.

The dimension of the grid is determined by the size of the parameter vectors.

Definition at line 103 of file Grid.h.

107  : GridBase<T>(numPoints, lower, upper, isPeriodic)
108  {
109  size_t data_size = 1;
110  for (size_t d = 0; d < GridBase<T>::GetDimension(); ++d) {
111  size_t storage_size = GridBase<T>::numPoints_[d];
112  data_size *= storage_size;
113  }
114 
115  GridBase<T>::data_.resize(data_size);
116  }
std::vector< int > numPoints_
Number of points in each dimension.
Definition: GridBase.h:48
std::vector< T > data_
Internal storage of the data.
Definition: GridBase.h:42

Referenced by SSAGES::Grid< T >::BuildGrid().

Here is the caller graph for this function:

Member Function Documentation

◆ begin() [1/2]

template<typename T >
iterator SSAGES::Grid< T >::begin ( )
inline

Return iterator at first grid point.

Returns
Iterator at first grid point.

The first grid point is defined as the grid point with index 0 in all dimensions.

Definition at line 527 of file Grid.h.

528  {
529  std::vector<int> indices(GridBase<T>::GetDimension(), 0);
530 
531  return iterator(indices, this);
532  }
size_t GetDimension() const
Get the dimension.
Definition: GridBase.h:195
GridIterator< T > iterator
Custom iterator over a grid.
Definition: Grid.h:515

Referenced by SSAGES::ANN::ANN(), SSAGES::CFF::CFF(), SSAGES::ANN::PreSimulation(), SSAGES::BFS::PrintBias(), SSAGES::BFS::ProjectBias(), SSAGES::BasisEvaluator::UpdateBias(), SSAGES::BasisEvaluator::UpdateCoeff(), and SSAGES::Grid< T >::WriteToFile().

Here is the caller graph for this function:

◆ begin() [2/2]

template<typename T >
const_iterator SSAGES::Grid< T >::begin ( ) const
inline

Return const iterator at first grid point.

Returns
Const iterator at first grid point.

The first grid point is defined as the grid point with index 0 in all dimensions.

Definition at line 558 of file Grid.h.

559  {
560  std::vector<int> indices(GridBase<T>::GetDimension(), 0);
561 
562  return iterator(indices, this);
563  }

◆ BuildGrid() [1/2]

template<typename T >
static Grid<T>* SSAGES::Grid< T >::BuildGrid ( const Json::Value &  json)
inlinestatic

Set up the grid.

Parameters
jsonJSON value containing all input information.
Returns
Pointer to the newly built grid.

This function builds a grid from a JSON node. It will return a nullptr if an unknown error occured, but generally, it will throw a BuildException of failure.

Definition at line 127 of file Grid.h.

128  {
129  return BuildGrid(json, "#/Grid");
130  }
static Grid< T > * BuildGrid(const Json::Value &json)
Set up the grid.
Definition: Grid.h:127

Referenced by SSAGES::ANN::Build().

Here is the caller graph for this function:

◆ BuildGrid() [2/2]

template<typename T >
static Grid<T>* SSAGES::Grid< T >::BuildGrid ( const Json::Value &  json,
const std::string &  path 
)
inlinestatic

Set up the grid.

Parameters
jsonJSON Value containing all input information.
pathPath for JSON path specification.
Returns
Pointer to the newly built grid.

This function builds a grid from a JSON node. It will return a nullptr if an unknown error occured, but generally, it will throw a BuildException on failure.

Definition at line 142 of file Grid.h.

143  {
144  Json::ObjectRequirement validator;
145  Json::Value schema;
146  Json::CharReaderBuilder rbuilder;
147  Json::CharReader* reader = rbuilder.newCharReader();
148 
149  reader->parse(JsonSchema::grid.c_str(),
150  JsonSchema::grid.c_str() + JsonSchema::grid.size(),
151  &schema, nullptr);
152  validator.Parse(schema, path);
153 
154  // Validate inputs.
155  validator.Validate(json, path);
156  if (validator.HasErrors()) {
157  throw BuildException(validator.GetErrors());
158  }
159 
160  // Read in Lower Grid edges.
161  std::vector<double> lower;
162  for (auto &lv : json["lower"]) {
163  lower.push_back(lv.asDouble());
164  }
165 
166  size_t dimension = lower.size();
167 
168  // Read in upper grid edges.
169  std::vector<double> upper;
170  for (auto &uv : json["upper"]) {
171  upper.push_back(uv.asDouble());
172  }
173 
174  if (upper.size() != dimension) {
175  throw BuildException({"Number of upper values does not match "
176  "number of lower values!"});
177  }
178 
179  // Read in number of points.
180  std::vector<int> number_points;
181  for (auto &np : json["number_points"]) {
182  number_points.push_back(np.asInt());
183  }
184 
185  if (number_points.size() != dimension) {
186  throw BuildException({"Arrays \"lower\" and \"number_points\" do "
187  "not have the same size!"});
188  }
189 
190  // Read in periodicity.
191  std::vector<bool> isPeriodic;
192  for (auto &periodic : json["periodic"]) {
193  isPeriodic.push_back(periodic.asBool());
194  }
195 
196  if (isPeriodic.size() == 0) {
197  isPeriodic = std::vector<bool>(dimension, false);
198  } else if (isPeriodic.size() != dimension) {
199  throw BuildException({"Arrays \"lower\" and \"periodic\" do not "
200  "have the same size!"});
201  }
202 
203  // Construct the grid.
204  Grid<T>* grid = new Grid(number_points, lower, upper, isPeriodic);
205 
206  return grid;
207  }
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.
Definition: Requirement.h:92
bool HasErrors()
Check if errors have occured.
Definition: Requirement.h:86
size_t size() const
Get the size of the internal storage vector.
Definition: GridBase.h:326
Grid(std::vector< int > numPoints, std::vector< double > lower, std::vector< double > upper, std::vector< bool > isPeriodic)
Constructor.
Definition: Grid.h:103

References Json::Requirement::GetErrors(), SSAGES::Grid< T >::Grid(), Json::Requirement::HasErrors(), Json::ObjectRequirement::Parse(), SSAGES::GridBase< T >::size(), and Json::ObjectRequirement::Validate().

Here is the call graph for this function:

◆ end() [1/2]

template<typename T >
iterator SSAGES::Grid< T >::end ( )
inline

Return iterator after last valid grid point.

Returns
Iterator after last valid grid point.

The last valid grid point has index num_points - 1 in all dimensions.

Definition at line 540 of file Grid.h.

541  {
542  std::vector<int> indices(GridBase<T>::GetDimension());
543  for (size_t i = 0; i < indices.size(); ++i) {
544  indices.at(i) = GridBase<T>::numPoints_[i] - 1;
545  }
546 
547  iterator it(indices, this);
548  return ++it;
549  }

Referenced by SSAGES::ANN::ANN(), SSAGES::CFF::CFF(), SSAGES::ANN::PreSimulation(), SSAGES::BasisEvaluator::UpdateBias(), SSAGES::BasisEvaluator::UpdateCoeff(), and SSAGES::Grid< T >::WriteToFile().

Here is the caller graph for this function:

◆ end() [2/2]

template<typename T >
const_iterator SSAGES::Grid< T >::end ( ) const
inline

Return const iterator after last valid grid point.

Returns
Const iterator after last valid grid point.

The last valid grid point has index num_points - 1 in all dimensions.

Definition at line 571 of file Grid.h.

572  {
573  std::vector<int> indices(GridBase<T>::GetDimension());
574  for (size_t i = 0; i < indices.size(); ++i) {
575  indices.at(i) = GridBase<T>::numPoints_[i] - 1;
576  }
577 
578  iterator it(indices, this);
579  return ++it;
580  }

◆ LoadFromFile()

template<typename T >
void SSAGES::Grid< T >::LoadFromFile ( const std::string &  filename)
inline

Builds a grid from file.

Parameters
filenameFilename containing grid data.
Returns
Pointer to the newly built grid.

This function builds a grid from a text data file.

Definition at line 629 of file Grid.h.

630  {
631  std::ifstream file(filename);
632  if (!file)
633  throw BuildException ({"Attempt to load grid " + filename + " unsuccessful."});
634 
635  std::string line, buff;
636 
637  // Skip type for now.
638  std::getline(file, line);
639 
640  // Get dimension.
641  {
642  size_t dim = 0;
643  std::getline(file, line);
644  std::istringstream iss(line);
645  iss >> buff >> buff >> dim;
646  if(dim != GridBase<T>::dimension_)
647  throw std::invalid_argument(filename +
648  ": Expected dimension " + std::to_string(GridBase<T>::dimension_) +
649  " but got " + std::to_string(dim) + " instead.");
650  }
651 
652  // Get size.
653  {
654  std::getline(file, line);
655  std::istringstream iss(line);
656  int count = 0;
657  auto counts = GridBase<T>::numPoints_;
658  iss >> buff >> buff;
659  for(int i = 0; iss >> count; ++i)
660  {
661  if((count) != counts[i])
662  throw std::invalid_argument(filename +
663  ": Expected count " + std::to_string(counts[i]) +
664  " on dimension " + std::to_string(i) + " but got " +
665  std::to_string(count) + " instead.");
666  }
667  }
668 
669  // Get lower bounds.
670  {
671  std::getline(file, line);
672  std::istringstream iss(line);
673  double lower = 0;
674  auto lowers = GridBase<T>::edges_.first;
675  iss >> buff >> buff;
676  for(int i = 0; iss >> lower; ++i)
677  {
678  double spacing = 0;
680  {
681  spacing = (GridBase<T>::edges_.second[i] - GridBase<T>::edges_.first[i]) / (GridBase<T>::numPoints_[i]);
682  }
683  if(std::abs(lower-spacing/2 - lowers[i]) > 1e-8)
684  throw std::invalid_argument(filename +
685  ": Expected lower " + std::to_string(lowers[i]+spacing/2) +
686  " on dimension " + std::to_string(i) + " but got " +
687  std::to_string(lower) + " instead.");
688  }
689  }
690 
691  // Get upper bounds.
692  {
693  std::getline(file, line);
694  std::istringstream iss(line);
695  double upper = 0;
696  auto uppers = GridBase<T>::edges_.second;
697  iss >> buff >> buff;
698  for(int i = 0; iss >> upper; ++i)
699  {
700  double spacing = 0;
702  {
703  spacing = (GridBase<T>::edges_.second[i] - GridBase<T>::edges_.first[i]) / (GridBase<T>::numPoints_[i]);
704  }
705  if(std::abs((upper+spacing/2) - uppers[i]) > 1e-8)
706  throw std::invalid_argument(filename +
707  ": Expected upper " + std::to_string(uppers[i]-spacing/2) +
708  " on dimension " + std::to_string(i) + " but got " +
709  std::to_string(upper) + " instead.");
710  }
711  }
712 
713  // Get periodic.
714  {
715  std::getline(file, line);
716  std::istringstream iss(line);
717  bool periodic;
718  auto periodics = GridBase<T>::isPeriodic_;
719  iss >> buff >> buff;
720  for(int i = 0; iss >> periodic; ++i)
721  {
722  if(periodic != periodics[i])
723  throw std::invalid_argument(filename +
724  ": Expected periodic " + std::to_string(periodics[i]) +
725  " on dimension " + std::to_string(i) + " but got " +
726  std::to_string(periodic) + " instead.");
727  }
728  }
729 
730  // Finally load data.
731  for(auto& d : GridBase<T>::data_)
732  {
733  std::getline(file, line);
734  std::istringstream iss(line);
735 
736  // Skip coordinates.
737  for(size_t i = 0; i < GridBase<T>::dimension_; ++i)
738  iss >> buff;
739 
740  iss >> d;
741  }
742  }
std::vector< bool > isPeriodic_
Periodicity of the Grid.
Definition: GridBase.h:54
std::pair< std::vector< double >, std::vector< double > > edges_
Edges of the Grid in each dimension.
Definition: GridBase.h:51
size_t dimension_
Dimension of the grid.
Definition: GridBase.h:45

Referenced by SSAGES::ABF::Build().

Here is the caller graph for this function:

◆ mapTo1d()

template<typename T >
size_t SSAGES::Grid< T >::mapTo1d ( const std::vector< int > &  indices) const
inlineoverrideprivatevirtual

Map d-dimensional indices to 1-d data vector.

Parameters
indicesVector specifying the grid point.
Returns
Index of 1d data vector.

Map a set of indices to the index of the 1d data vector. Keep in mind, that the data includes underflow (index -1) and overflow (index numPoints) bins in periodic dimension.

Implements SSAGES::GridBase< T >.

Definition at line 70 of file Grid.h.

71  {
72  // Check if an index is out of bounds
73  for (size_t i = 0; i < GridBase<T>::GetDimension(); ++i) {
74  int index = indices.at(i);
75  int numpoints = GridBase<T>::numPoints_[i];
76  if ( index < 0 || index >= numpoints )
77  {
78  throw std::out_of_range("Grid index out of range.");
79  }
80  }
81 
82  size_t idx = 0;
83  size_t fac = 1;
84  for (size_t i = 0; i < GridBase<T>::GetDimension(); ++i) {
85  idx += indices.at(i) * fac;
86  fac *= GridBase<T>::numPoints_[i];
87  }
88  return idx;
89  }

◆ WriteToFile()

template<typename T >
void SSAGES::Grid< T >::WriteToFile ( const std::string &  filename)
inline

Write grid out to file.

Parameters
filenameName of the output file.

Definition at line 586 of file Grid.h.

587  {
588  std::ofstream output(filename.c_str(), std::ofstream::out);
589 
590  // Print out header.
591  output << "#! type grid\n";
592  output << "#! dim " << GridBase<T>::dimension_ << "\n";
593  output << "#! count ";
594  for(auto& c : this->GetNumPoints())
595  output << c << " ";
596  output << "\n";
597  output << "#! lower ";
598  for(auto& l : this->GetLower())
599  output << l << " ";
600  output << "\n";
601  output << "#! upper ";
602  for(auto& u : this->GetUpper())
603  output << u << " ";
604  output << "\n";
605  output << "#! periodic ";
606  for(auto p : GridBase<T>::isPeriodic_)
607  output << p << " ";
608  output << "\n";
609 
610  for(auto it = this->begin(); it != this->end(); ++it)
611  {
612  auto coords = it.coordinates();
613  for(auto& c : coords)
614  output << std::setprecision(8) << std::fixed << c << " ";
615  output.unsetf(std::ios_base::fixed);
616  output << std::setprecision(16) << *it << "\n";
617  }
618 
619  output.close();
620  }
const std::vector< double > GetLower() const
Return the lower edges of the Grid.
Definition: GridBase.h:231
const std::vector< double > GetUpper() const
Return the upper edges of the Grid.
Definition: GridBase.h:262
const std::vector< int > GetNumPoints() const
Get the number of points for all dimensions.
Definition: GridBase.h:205
iterator begin()
Return iterator at first grid point.
Definition: Grid.h:527
iterator end()
Return iterator after last valid grid point.
Definition: Grid.h:540

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

Here is the call graph for this function:

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