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::Histogram< T > Class Template Reference

Basic Histogram. More...

#include <Histogram.h>

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

Classes

class  HistIterator
 Custom Iterator. More...
 

Public Types

typedef HistIterator< T > iterator
 Custom iterator over a histogram.
 
typedef HistIterator< const T > const_iterator
 Custom constant iterator over a histogram.
 

Public Member Functions

 Histogram (std::vector< int > numPoints, std::vector< double > lower, std::vector< double > upper, std::vector< bool > isPeriodic)
 Constructor. More...
 
iterator begin ()
 Return iterator at first bin of histogram. More...
 
iterator end ()
 Return iterator after last valid bin. More...
 
std::vector< T >::const_iterator begin () const
 Return const iterator at first bin of histogram. More...
 
std::vector< T >::const_iterator end () const
 Return const iterator after last valid bin. 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 Histogram< T > * BuildHistogram (const Json::Value &json)
 Set up the histogram. More...
 
static Histogram< T > * BuildHistogram (const Json::Value &json, const std::string &path)
 Set up the histogram. 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::Histogram< T >

Basic Histogram.

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

A Histogram is a method to store data in SSAGES. It is used to discretize a continuous number, typically a collective variable, into number_points bins. For each bin, an arbitrary type of data can be stored, specified via the template parameter T.

The histogram 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 histogram can be defined as periodic or non-periodic in the respective dimension. By default, the histogram is non-periodic. The bins are indexed from 0 to number_points-1 following the standard C/C++ convention. In contrast to the Grid, a Histogram additionally includes an under- and an overflow bin in each non-periodic dimension. These can be accessed via the indices -1 an number_points (see below).

The bin width Delta is given by (upper - lower)/number_points. Thus, bin 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().

In non-periodic dimensions, an overflow and an underflow interval exist. The underflow interval corresponds to the interval (-infinity, lower), i.e. all points below lower. Similarly, the overflow interval corresponds to the interval [upper, infinity). The underflow grid point can be accessed via the index -1, the overflow grid point via the index number_points.

Definition at line 64 of file Histogram.h.

Constructor & Destructor Documentation

template<typename T>
SSAGES::Histogram< T >::Histogram ( 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 116 of file Histogram.h.

Referenced by SSAGES::Histogram< int >::BuildHistogram().

120  : GridBase<T>(numPoints, lower, upper, isPeriodic)
121  {
122  size_t data_size = 1;
123  for (size_t d = 0; d < GridBase<T>::GetDimension(); ++d) {
124  size_t storage_size = GridBase<T>::GetNumPoints(d);
125  if (!GridBase<T>::GetPeriodic(d)) { storage_size += 2; }
126  data_size *= storage_size;
127  }
128 
129  GridBase<T>::data_.resize(data_size);
130  }
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::Histogram< T >::begin ( )
inline

Return iterator at first bin of histogram.

Returns
Iterator at first bin of the histogram.

The first bin is the bin that has the lowest allowed index in all dimensions, i.e. 0 in periodic and -1 in non-periodic dimensions.

Definition at line 549 of file Histogram.h.

Referenced by SSAGES::Basis::PrintBias(), and SSAGES::Basis::UpdateBias().

550  {
551  std::vector<int> indices(GridBase<T>::GetDimension());
552  for (size_t i = 0; i < indices.size(); ++i) {
553  if(GridBase<T>::GetPeriodic(i)) {
554  indices.at(i) = 0;
555  } else {
556  indices.at(i) = -1;
557  }
558  }
559 
560  return iterator(indices, this);
561  }
HistIterator< T > iterator
Custom iterator over a histogram.
Definition: Histogram.h:537

Here is the caller graph for this function:

template<typename T>
std::vector<T>::const_iterator SSAGES::Histogram< T >::begin ( ) const
inline

Return const iterator at first bin of histogram.

Returns
Const iterator at first bin of the histogram.

The first bin is the bin that has the lowest allowed index in all dimensions, i.e. 0 in periodic and -1 in non-periodic dimensions.

Definition at line 593 of file Histogram.h.

594  {
595  std::vector<int> indices(GridBase<T>::GetDimension());
596  for (size_t i = 0; i < indices.size(); ++i) {
597  if(GridBase<T>::GetPeriodic(i)) {
598  indices.at(i) = 0;
599  } else {
600  indices.at(i) = -1;
601  }
602  }
603 
604  return iterator(indices, this);
605  }
HistIterator< T > iterator
Custom iterator over a histogram.
Definition: Histogram.h:537
template<typename T>
static Histogram<T>* SSAGES::Histogram< T >::BuildHistogram ( const Json::Value &  json)
inlinestatic

Set up the histogram.

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

This function builds a histogram 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 141 of file Histogram.h.

Referenced by SSAGES::Histogram< int >::BuildHistogram().

142  {
143  return BuildHistogram(json, "#/Histogram");
144  }
static Histogram< T > * BuildHistogram(const Json::Value &json)
Set up the histogram.
Definition: Histogram.h:141

Here is the caller graph for this function:

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

Set up the histogram.

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

This function builds a histogram 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 156 of file Histogram.h.

157  {
158  Json::ObjectRequirement validator;
159  Json::Value schema;
160  Json::Reader reader;
161 
162  reader.parse(JsonSchema::grid, schema);
163  validator.Parse(schema, path);
164 
165  // Validate inputs.
166  validator.Validate(json, path);
167  if (validator.HasErrors()) {
168  throw BuildException(validator.GetErrors());
169  }
170 
171  // Read in Lower Grid edges.
172  std::vector<double> lower;
173  for (auto &lv : json["lower"]) {
174  lower.push_back(lv.asDouble());
175  }
176 
177  size_t dimension = lower.size();
178 
179  // Read in upper grid edges.
180  std::vector<double> upper;
181  for (auto &uv : json["upper"]) {
182  upper.push_back(uv.asDouble());
183  }
184 
185  if (upper.size() != dimension) {
186  throw BuildException({"Number of upper values does not match "
187  "number of lower values!"});
188  }
189 
190  // Read in number of points.
191  std::vector<int> number_points;
192  for (auto &np : json["number_points"]) {
193  number_points.push_back(np.asInt());
194  }
195 
196  if (number_points.size() != dimension) {
197  throw BuildException({"Arrays \"lower\" and \"number_points\" do "
198  "not have the same size!"});
199  }
200 
201  // Read in periodicity.
202  std::vector<bool> isPeriodic;
203  for (auto &periodic : json["periodic"]) {
204  isPeriodic.push_back(periodic.asBool());
205  }
206 
207  if (isPeriodic.size() == 0) {
208  isPeriodic = std::vector<bool>(dimension, false);
209  } else if (isPeriodic.size() != dimension) {
210  throw BuildException({"Arrays \"lower\" and \"periodic\" do not "
211  "have the same size!"});
212  }
213 
214  // Construct the grid.
215  Histogram<T>* hist = new Histogram(number_points, lower, upper, isPeriodic);
216 
217  return hist;
218  }
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.
Histogram(std::vector< int > numPoints, std::vector< double > lower, std::vector< double > upper, std::vector< bool > isPeriodic)
Constructor.
Definition: Histogram.h:116
virtual void Validate(const Value &json, const std::string &path) override
Validate JSON value.
template<typename T>
iterator SSAGES::Histogram< T >::end ( )
inline

Return iterator after last valid bin.

Returns
Iterator after last valid bin.

The last valid bin is the bin that has the highest allowed index in all dimensions, i.e. num_points - 1 in periodic and num_points in non-periodic dimensions.

Definition at line 571 of file Histogram.h.

572  {
573  std::vector<int> indices(GridBase<T>::GetDimension());
574  for (size_t i = 0; i < indices.size(); ++i) {
575  if (GridBase<T>::GetPeriodic(i)) {
576  indices.at(i) = GridBase<T>::GetNumPoints(i) - 1;
577  } else {
578  indices.at(i) = GridBase<T>::GetNumPoints(i);
579  }
580  }
581 
582  iterator it(indices, this);
583  return ++it;
584  }
HistIterator< T > iterator
Custom iterator over a histogram.
Definition: Histogram.h:537
const std::vector< int > & GetNumPoints() const
Get the number of points for all dimensions.
Definition: GridBase.h:138
template<typename T>
std::vector<T>::const_iterator SSAGES::Histogram< T >::end ( ) const
inline

Return const iterator after last valid bin.

Returns
Const iterator after last valid bin.

The last valid bin is the bin that has the highest allowed index in all dimensions, i.e. num_points - 1 in periodic and num_points in non-periodic dimensions.

Definition at line 615 of file Histogram.h.

616  {
617  std::vector<int> indices(GridBase<T>::GetDimension());
618  for (size_t i = 0; i < indices.size(); ++i) {
619  if (GridBase<T>::GetPeriodic(i)) {
620  indices.at(i) = GridBase<T>::GetNumPoints(i) - 1;
621  } else {
622  indices.at(i) = GridBase<T>::GetNumPoints(i);
623  }
624  }
625 
626  iterator it(indices, this);
627  return ++it;
628  }
HistIterator< T > iterator
Custom iterator over a histogram.
Definition: Histogram.h:537
const std::vector< int > & GetNumPoints() const
Get the number of points for all dimensions.
Definition: GridBase.h:138
template<typename T>
size_t SSAGES::Histogram< 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 76 of file Histogram.h.

77  {
78  // Check if an index is out of bounds
79  for (size_t i=0; i < GridBase<T>::GetDimension(); ++i) {
80  int index = indices.at(i);
81  bool periodic = GridBase<T>::GetPeriodic(i);
82  int numpoints = GridBase<T>::GetNumPoints(i);
83  if ( (periodic && (index < 0 || index >= numpoints)) ||
84  (periodic && (index < -1 || index > numpoints)) )
85  {
86  throw std::out_of_range("Bin index out of range.");
87  }
88  }
89 
90  size_t idx = 0;
91  size_t fac = 1;
92  for (size_t i=0; i < GridBase<T>::GetDimension(); ++i) {
93  if (GridBase<T>::GetPeriodic(i)) {
94  idx += indices.at(i) * fac;
95  fac *= GridBase<T>::GetNumPoints(i);
96  } else {
97  idx += (indices.at(i) + 1) * fac;
98  fac *= GridBase<T>::GetNumPoints(i) + 2;
99  }
100  }
101  return idx;
102  }
const std::vector< bool > & GetPeriodic() const
Return the periodicity of the Grid.
Definition: GridBase.h:219
const std::vector< int > & GetNumPoints() const
Get the number of points for all dimensions.
Definition: GridBase.h:138

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