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