PSCF v1.5.0
SymmTensorAverage.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 "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 accumulators_.allocate(Dimension * (Dimension + 1) / 2);
26 int i, j, k;
27 k = 0;
28 for (i = 0; i < Dimension; ++i) {
29 for (j = 0; j <= i; ++j) {
30 accumulators_[k].setBlockFactor(blockFactor);
31 ++k;
32 }
33 }
34 }
35
36 /*
37 * Destructor.
38 */
41
42 /*
43 * Set nSamplePerBlock parameter.
44 */
46 {
47 if (nSamplePerBlock < 0) {
48 UTIL_THROW("Attempt to set nSamplePerBlock < 0");
49 }
50 nSamplePerBlock_ = nSamplePerBlock;
51 int i, j, k;
52 k = 0;
53 for (i = 0; i < Dimension; ++i) {
54 for (j = 0; j <= i; ++j) {
55 accumulators_[k].setNSamplePerBlock(nSamplePerBlock);
56 ++k;
57 }
58 }
59 }
60
61 /*
62 * Read nSamplePerBlock from file.
63 */
64 void SymmTensorAverage::readParameters(std::istream& in)
65 {
66 read<int>(in, "nSamplePerBlock", nSamplePerBlock_);
67 if (nSamplePerBlock_ < 0) {
68 UTIL_THROW("Invalid input: nSamplePerBlock < 0");
69 }
70 int i, j, k;
71 k = 0;
72 for (i = 0; i < Dimension; ++i) {
73 for (j = 0; j <= i; ++j) {
74 accumulators_[k].setNSamplePerBlock(nSamplePerBlock_);
75 ++k;
76 }
77 }
78 }
79
80 /*
81 * Load internal state from archive.
82 */
84 {
85 loadParameter<int>(ar, "nSamplePerBlock", nSamplePerBlock_);
86 if (nSamplePerBlock_ < 0) {
87 UTIL_THROW("Loading value nSamplePerBlock < 0");
88 }
89 ar & iBlock_;
90 int i, j, k;
91 k = 0;
92 for (i = 0; i < Dimension; ++i) {
93 for (j = 0; j <= i; ++j) {
94 ar & accumulators_[k];
95 ++k;
96 }
97 }
98 }
99
100 /*
101 * Save internal state to archive.
102 */
104 { ar & *this; }
105
106 /*
107 * Reset all accumulators and counters to zero.
108 */
110 {
111 iBlock_ = 0;
112 int i, j, k;
113 k = 0;
114 for (i = 0; i < Dimension; ++i) {
115 for (j = 0; j <= i; ++j) {
116 accumulators_[k].clear();
117 ++k;
118 }
119 }
120 }
121
122 /*
123 * Add a sampled value to the ensemble.
124 */
126 {
127 int i, j, k;
128 k = 0;
129 for (i = 0; i < Dimension; ++i) {
130 for (j = 0; j <= i; ++j) {
131 accumulators_[k].sample(value(i,j));
132 ++k;
133 }
134 }
135 if (nSamplePerBlock_) {
136 if (iBlock_ == nSamplePerBlock_) {
137 iBlock_ = 0;
138 }
139 ++iBlock_;
140 }
141 }
142
143 /*
144 * Access accumulator associated with one component.
145 */
147 {
148 int k;
149 if (j > i) {
150 k = i;
151 i = j;
152 j = k;
153 }
154 k = i*(i+1)/2 + j;
155 return accumulators_[k];
156 }
157
158}
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.
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:49
const int Dimension
Dimensionality of space.
Definition Dimension.h:19
Utility classes for scientific computation.