PSCF v1.5.0
TensorAverage.cpp
1/*
2* Util Package - C++ Utilities for Scientific Computation
3*
4* Copyright 2010 - 2017, David Morse (morse012@um.edu)
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
15namespace Util
16{
17
18 /*
19 * Default constructor.
20 */
23 nSamplePerBlock_(0),
24 iBlock_(0)
25 {
26 setClassName("TensorAverage");
27 accumulators_.allocate(Dimension*Dimension);
28 int i, j, k;
29 k = 0;
30 for (i = 0; i < Dimension; ++i) {
31 for (j = 0; j < Dimension; ++j) {
32 accumulators_[k].setBlockFactor(blockFactor);
33 ++k;
34 }
35 }
36 }
37
38 /*
39 * Destructor.
40 */
43
44 /*
45 * Set nSamplePerBlock parameter.
46 */
48 {
49 if (nSamplePerBlock < 0) {
50 UTIL_THROW("Attempt to set nSamplePerBlock < 0");
51 }
52 nSamplePerBlock_ = nSamplePerBlock;
53 int i, j, k;
54 k = 0;
55 for (i = 0; i < Dimension; ++i) {
56 for (j = 0; j < Dimension; ++j) {
57 accumulators_[k].setNSamplePerBlock(nSamplePerBlock);
58 ++k;
59 }
60 }
61 }
62
63 /*
64 * Read nSamplePerBlock from file.
65 */
66 void TensorAverage::readParameters(std::istream& in)
67 {
68 read<int>(in, "nSamplePerBlock", nSamplePerBlock_);
69 if (nSamplePerBlock_ < 0) {
70 UTIL_THROW("Invalid input: nSamplePerBlock < 0");
71 }
72 int i, j, k;
73 k = 0;
74 for (i = 0; i < Dimension; ++i) {
75 for (j = 0; j < Dimension; ++j) {
76 accumulators_[k].setNSamplePerBlock(nSamplePerBlock_);
77 ++k;
78 }
79 }
80 }
81
82 /*
83 * Load internal state from archive.
84 */
86 {
87 loadParameter<int>(ar, "nSamplePerBlock", nSamplePerBlock_);
88 if (nSamplePerBlock_ < 0) {
89 UTIL_THROW("Loading value nSamplePerBlock < 0");
90 }
91 ar & iBlock_;
92 int i, j, k;
93 k = 0;
94 for (i = 0; i < Dimension; ++i) {
95 for (j = 0; j < Dimension; ++j) {
96 ar & accumulators_[k];
97 ++k;
98 }
99 }
100 }
101
102 /*
103 * Save internal state to archive.
104 */
106 { ar & *this; }
107
108 /*
109 * Reset all accumulators and counters to zero.
110 */
112 {
113 iBlock_ = 0;
114 int i, j, k;
115 k = 0;
116 for (i = 0; i < Dimension; ++i) {
117 for (j = 0; j < Dimension; ++j) {
118 accumulators_[k].clear();
119 ++k;
120 }
121 }
122 }
123
124 /*
125 * Add a sampled value to the ensemble.
126 */
127 void TensorAverage::sample(const Tensor& value)
128 {
129 int i, j, k;
130 k = 0;
131 for (i = 0; i < Dimension; ++i) {
132 for (j = 0; j < Dimension; ++j) {
133 accumulators_[k].sample(value(i,j));
134 ++k;
135 }
136 }
137 if (nSamplePerBlock_) {
138 if (iBlock_ == nSamplePerBlock_) {
139 iBlock_ = 0;
140 }
141 ++iBlock_;
142 }
143 }
144
145 /*
146 * Access accumulator associated with one component.
147 */
149 {
150 int k = i*Dimension + j;
151 return accumulators_[k];
152 }
153
154}
Calculates the average and variance of a sampled property.
Definition Average.h:44
ScalarParam< Type > & read(std::istream &in, const char *label, Type &value)
Add and read a new required ScalarParam < Type > object.
void setClassName(const char *className)
Set class name string.
ScalarParam< Type > & loadParameter(Serializable::IArchive &ar, const char *label, Type &value, bool isRequired)
Add and load a new ScalarParam < Type > object.
ParamComposite()
Constructor.
BinaryFileIArchive IArchive
Type of input archive used by load method.
BinaryFileOArchive OArchive
Type of output archive used by save method.
int nSamplePerBlock() const
Get number of samples per block average.
const Average & operator()(int i, int j)
Access the Average object for one tensor component.
TensorAverage(int blockFactor=2)
Constructor.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
void setNSamplePerBlock(int nSamplePerBlock)
Set nSamplePerBlock.
void clear()
Clear all accumulators, set to empty initial state.
void readParameters(std::istream &in)
Read parameter nSamplePerBlock from file and initialize.
virtual ~TensorAverage()
Destructor.
void sample(const Tensor &value)
Add a sampled value to the ensemble.
A Tensor represents a Cartesian tensor.
Definition Tensor.h:33
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition global.h:49
const int Dimension
Dimensionality of space.
Definition Dimension.h:19
Utility classes for scientific computation.