Simpatico  v1.10
TensorAverage.cpp
1 /*
2 * Util Package - C++ Utilities for Scientific Computation
3 *
4 * Copyright 2010 - 2017, The Regents of the University of Minnesota
5 * Distributed under the terms of the GNU General Public License.
6 */
7 
8 #include "TensorAverage.h" // class header
9 #include <util/space/Tensor.h>
10 #include <util/format/Dbl.h>
11 #include <util/format/Int.h>
12 
13 #include <math.h>
14 
15 namespace Util
16 {
17 
18  /*
19  * Default constructor.
20  */
22  : ParamComposite(),
23  nSamplePerBlock_(0),
24  iBlock_(0)
25  {
26  setClassName("TensorAverage");
27  int i, j, k;
28  k = 0;
29  for (i = 0; i < Dimension; ++i) {
30  for (j = 0; j < Dimension; ++j) {
31  accumulators_[k].setBlockFactor(blockFactor);
32  ++k;
33  }
34  }
35  }
36 
37  /*
38  * Destructor.
39  */
41  {}
42 
43  /*
44  * Set nSamplePerBlock parameter.
45  */
47  {
48  if (nSamplePerBlock < 0) {
49  UTIL_THROW("Attempt to set nSamplePerBlock < 0");
50  }
51  nSamplePerBlock_ = nSamplePerBlock;
52  int i, j, k;
53  k = 0;
54  for (i = 0; i < Dimension; ++i) {
55  for (j = 0; j < Dimension; ++j) {
56  accumulators_[k].setNSamplePerBlock(nSamplePerBlock);
57  ++k;
58  }
59  }
60  }
61 
62  /*
63  * Read nSamplePerBlock from file.
64  */
65  void TensorAverage::readParameters(std::istream& in)
66  {
67  read<int>(in, "nSamplePerBlock", nSamplePerBlock_);
68  if (nSamplePerBlock_ < 0) {
69  UTIL_THROW("Invalid input: nSamplePerBlock < 0");
70  }
71  int i, j, k;
72  k = 0;
73  for (i = 0; i < Dimension; ++i) {
74  for (j = 0; j < Dimension; ++j) {
75  accumulators_[k].setNSamplePerBlock(nSamplePerBlock_);
76  ++k;
77  }
78  }
79  }
80 
81  /*
82  * Load internal state from archive.
83  */
85  {
86  loadParameter<int>(ar, "nSamplePerBlock", nSamplePerBlock_);
87  if (nSamplePerBlock_ < 0) {
88  UTIL_THROW("Loading value nSamplePerBlock < 0");
89  }
90  ar & iBlock_;
91  int i, j, k;
92  k = 0;
93  for (i = 0; i < Dimension; ++i) {
94  for (j = 0; j < Dimension; ++j) {
95  ar & accumulators_[k];
96  ++k;
97  }
98  }
99  }
100 
101  /*
102  * Save internal state to archive.
103  */
105  { ar & *this; }
106 
107  /*
108  * Reset all accumulators and counters to zero.
109  */
111  {
112  iBlock_ = 0;
113  int i, j, k;
114  k = 0;
115  for (i = 0; i < Dimension; ++i) {
116  for (j = 0; j < Dimension; ++j) {
117  accumulators_[k].clear();
118  ++k;
119  }
120  }
121  }
122 
123  /*
124  * Add a sampled value to the ensemble.
125  */
126  void TensorAverage::sample(const Tensor& value)
127  {
128  int i, j, k;
129  k = 0;
130  for (i = 0; i < Dimension; ++i) {
131  for (j = 0; j < Dimension; ++j) {
132  accumulators_[k].sample(value(i,j));
133  ++k;
134  }
135  }
136  if (nSamplePerBlock_) {
137  if (iBlock_ == nSamplePerBlock_) {
138  iBlock_ = 0;
139  }
140  ++iBlock_;
141  }
142  }
143 
144  /*
145  * Access accumulator associated with one component.
146  */
148  {
149  int k = i*Dimension + j;
150  return accumulators_[k];
151  }
152 
153 }
const Average & operator()(int i, int j)
Access the Average object for one tensor component.
const int Dimension
Dimensionality of space.
Definition: Dimension.h:19
Calculates the average and variance of a sampled property.
Definition: Average.h:43
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
A Tensor represents a Cartesian tensor.
Definition: Tensor.h:32
void setNSamplePerBlock(int nSamplePerBlock)
Set nSamplePerBlock.
Saving / output archive for binary ostream.
TensorAverage(int blockFactor=2)
Constructor.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
void readParameters(std::istream &in)
Read parameter nSamplePerBlock from file and initialize.
void clear()
Clear all accumulators, set to empty initial state.
Utility classes for scientific computation.
Definition: accumulators.mod:1
int nSamplePerBlock() const
Get number of samples per block average.
void sample(const Tensor &value)
Add a sampled value to the ensemble.
Saving archive for binary istream.
virtual ~TensorAverage()
Destructor.
void setClassName(const char *className)
Set class name string.
An object that can read multiple parameters from file.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.