SSAGES  0.1
A MetaDynamics Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Groups Pages
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 &file)
 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 >
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...
 
double GetInterpolated (const std::vector< double > &x)
 Return linear interpolation on a coordinate. More...
 
int GetIndex (double x) const
 Return the Grid index for a one-dimensional grid. More...
 
std::vector< double > GetCoordinates (const std::vector< int > &indices)
 Return coordinates of the grid center points. 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...
 

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.
 
 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

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.

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

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>::GetNumPoints(d);
112  data_size *= storage_size;
113  }
114 
115  GridBase<T>::data_.resize(data_size);
116  }
const std::vector< int > & GetNumPoints() const
Get the number of points for all dimensions.
Definition: GridBase.h:138

Here is the caller graph for this function:

Member Function Documentation

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 524 of file Grid.h.

Referenced by SSAGES::Grid< int >::WriteToFile().

525  {
526  std::vector<int> indices(GridBase<T>::GetDimension(), 0);
527 
528  return iterator(indices, this);
529  }
GridIterator< T > iterator
Custom iterator over a grid.
Definition: Grid.h:512

Here is the caller graph for this function:

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 555 of file Grid.h.

556  {
557  std::vector<int> indices(GridBase<T>::GetDimension(), 0);
558 
559  return iterator(indices, this);
560  }
GridIterator< T > iterator
Custom iterator over a grid.
Definition: Grid.h:512
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.

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

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

Here is the caller graph for this function:

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

Referenced by SSAGES::Grid< int >::WriteToFile().

538  {
539  std::vector<int> indices(GridBase<T>::GetDimension());
540  for (size_t i = 0; i < indices.size(); ++i) {
541  indices.at(i) = GridBase<T>::GetNumPoints(i) - 1;
542  }
543 
544  iterator it(indices, this);
545  return ++it;
546  }
GridIterator< T > iterator
Custom iterator over a grid.
Definition: Grid.h:512
const std::vector< int > & GetNumPoints() const
Get the number of points for all dimensions.
Definition: GridBase.h:138

Here is the caller graph for this function:

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 568 of file Grid.h.

569  {
570  std::vector<int> indices(GridBase<T>::GetDimension());
571  for (size_t i = 0; i < indices.size(); ++i) {
572  indices.at(i) = GridBase<T>::GetNumPoints(i) - 1;
573  }
574 
575  iterator it(indices, this);
576  return ++it;
577  }
GridIterator< T > iterator
Custom iterator over a grid.
Definition: Grid.h:512
const std::vector< int > & GetNumPoints() const
Get the number of points for all dimensions.
Definition: GridBase.h:138
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 626 of file Grid.h.

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

627  {
628  std::ifstream file(filename);
629  std::string line, buff;
630 
631  // Skip type for now.
632  std::getline(file, line);
633 
634  // Get dimension.
635  {
636  int dim = 0;
637  std::getline(file, line);
638  std::istringstream iss(line);
639  iss >> buff >> buff >> dim;
640  if(dim != GridBase<T>::dimension_)
641  throw std::invalid_argument(filename +
642  ": Expected dimension " + std::to_string(GridBase<T>::dimension_) +
643  " but got " + std::to_string(dim) + " instead.");
644  }
645 
646  // Get size.
647  {
648  std::getline(file, line);
649  std::istringstream iss(line);
650  int count = 0;
651  auto counts = GridBase<T>::numPoints_;
652  iss >> buff >> buff;
653  for(int i = 0; iss >> count; ++i)
654  {
655  if(count != counts[i])
656  throw std::invalid_argument(filename +
657  ": Expected count " + std::to_string(counts[i]) +
658  " on dimension " + std::to_string(i) + " but got " +
659  std::to_string(count) + " instead.");
660  }
661  }
662 
663  // Get lower bounds.
664  {
665  std::getline(file, line);
666  std::istringstream iss(line);
667  double lower = 0;
668  auto lowers = GridBase<T>::edges_.first;
669  iss >> buff >> buff;
670  for(int i = 0; iss >> lower; ++i)
671  {
672  if(std::abs(lower - lowers[i]) > 1e-8)
673  throw std::invalid_argument(filename +
674  ": Expected lower " + std::to_string(lowers[i]) +
675  " on dimension " + std::to_string(i) + " but got " +
676  std::to_string(lower) + " instead.");
677  }
678  }
679 
680  // Get upper bounds.
681  {
682  std::getline(file, line);
683  std::istringstream iss(line);
684  double upper = 0;
685  auto uppers = GridBase<T>::edges_.second;
686  iss >> buff >> buff;
687  for(int i = 0; iss >> upper; ++i)
688  {
689  if(std::abs(upper - uppers[i]) > 1e-8)
690  throw std::invalid_argument(filename +
691  ": Expected upper " + std::to_string(uppers[i]) +
692  " on dimension " + std::to_string(i) + " but got " +
693  std::to_string(upper) + " instead.");
694  }
695  }
696 
697  // Get periodic.
698  {
699  std::getline(file, line);
700  std::istringstream iss(line);
701  bool periodic;
702  auto periodics = GridBase<T>::isPeriodic_;
703  iss >> buff >> buff;
704  for(int i = 0; iss >> periodic; ++i)
705  {
706  if(periodic != periodics[i])
707  throw std::invalid_argument(filename +
708  ": Expected periodic " + std::to_string(periodics[i]) +
709  " on dimension " + std::to_string(i) + " but got " +
710  std::to_string(periodic) + " instead.");
711  }
712  }
713 
714  // Finally load data.
715  for(auto& d : GridBase<T>::data_)
716  {
717  std::getline(file, line);
718  std::istringstream iss(line);
719 
720  // Skip coordinates.
721  for(int i = 0; i < GridBase<T>::dimension_; ++i)
722  iss >> buff;
723 
724  iss >> d;
725  }
726  }

Here is the caller graph for this function:

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>::GetNumPoints(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>::GetNumPoints(i);
87  }
88  return idx;
89  }
const std::vector< int > & GetNumPoints() const
Get the number of points for all dimensions.
Definition: GridBase.h:138
template<typename T>
void SSAGES::Grid< T >::WriteToFile ( const std::string &  file)
inline

Write grid out to file.

Parameters
filenameName of the output file.

Definition at line 583 of file Grid.h.

584  {
585  std::ofstream output(file.c_str(), std::ofstream::out);
586 
587  // Print out header.
588  output << "#! type grid\n";
589  output << "#! dim " << GridBase<T>::dimension_ << "\n";
590  output << "#! count ";
591  for(auto& c : GridBase<T>::numPoints_)
592  output << c << " ";
593  output << "\n";
594  output << "#! lower ";
595  for(auto& l : GridBase<T>::edges_.first)
596  output << l << " ";
597  output << "\n";
598  output << "#! upper ";
599  for(auto& u : GridBase<T>::edges_.second)
600  output << u << " ";
601  output << "\n";
602  output << "#! periodic ";
603  for(auto p : GridBase<T>::isPeriodic_)
604  output << p << " ";
605  output << "\n";
606 
607  for(auto it = this->begin(); it != this->end(); ++it)
608  {
609  auto coords = it.coordinates();
610  for(auto& c : coords)
611  output << std::setprecision(8) << std::fixed << c << " ";
612  output.unsetf(std::ios_base::fixed);
613  output << std::setprecision(16) << *it << "\n";
614  }
615 
616  output.close();
617  }
iterator begin()
Return iterator at first grid point.
Definition: Grid.h:524
iterator end()
Return iterator after last valid grid point.
Definition: Grid.h:537

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