PSCF v1.4.0
fts/analyzer/AverageListAnalyzer.h
1#ifndef RP_AVERAGE_LIST_ANALYZER_H
2#define RP_AVERAGE_LIST_ANALYZER_H
3
4/*
5* PSCF - Polymer Self-Consistent Field
6*
7* Copyright 2015 - 2025, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <util/accumulators/Average.h> // member
12#include <util/global.h>
13
14namespace Pscf {
15namespace Rp {
16
17 using namespace Util;
18
40 template <int D, class T>
41 class AverageListAnalyzer : public T::Analyzer
42 {
43
44 public:
45
52 AverageListAnalyzer(typename T::Simulator& simulator,
53 typename T::System& system);
54
64 void readParameters(std::istream& in) override;
65
71 void setup() override;
72
78 void sample(long iStep) override;
79
83 void output() override;
84
92 int nSamplePerOutput() const;
93
97 int nValue() const;
98
104 const std::string& name(int i) const;
105
111 const Average& accumulator(int i) const;
112
113 protected:
114
116 std::ofstream outputFile_;
117
127
133 void clearAccumulators();
134
141 void setName(int i, std::string name);
142
149 void setValue(int i, double value);
150
154 virtual void compute() = 0;
155
161 double value(int i) const;
162
168 void updateAccumulators(long iStep);
169
173 void outputAccumulators();
174
175 using AnalyzerT = typename T::Analyzer;
176 using AnalyzerT::simulator;
177 using AnalyzerT::system;
178
179 private:
180
182 DArray<Average> accumulators_;
183
185 DArray<double> values_;
186
188 DArray<std::string> names_;
189
191 int nSamplePerOutput_;
192
194 int nValue_;
195
197 bool hasAccumulators_;
198
199 };
200
201 // Inline functions
202
203 /*
204 * Get nSamplePerOutput.
205 */
206 template <int D, class T> inline
208 { return nSamplePerOutput_; }
209
210 /*
211 * Get nValue (number of variables).
212 */
213 template <int D, class T> inline
215 {
216 UTIL_CHECK(hasAccumulators_);
217 return nValue_;
218 }
219
220 /*
221 * Get current value of a variable, set by compute function.
222 */
223 template <int D, class T> inline
225 {
226 UTIL_CHECK(hasAccumulators_);
227 UTIL_CHECK(i >= 0 && i < nValue_);
228 return values_[i];
229 }
230
231 /*
232 * Get name of specific variable.
233 */
234 template <int D, class T> inline
235 const std::string& AverageListAnalyzer<D,T>::name(int i) const
236 {
237 UTIL_CHECK(hasAccumulators_);
238 UTIL_CHECK(i >= 0 && i < nValue_);
239 return names_[i];
240 }
241
242 /*
243 * Get accumulator associated with a variable.
244 */
245 template <int D, class T> inline
247 {
248 UTIL_CHECK(hasAccumulators_);
249 UTIL_CHECK(i >= 0 && i < nValue_);
250 return accumulators_[i];
251 }
252
253 /*
254 * Set current value of a variable.
255 */
256 template <int D, class T> inline
258 {
259 UTIL_CHECK(hasAccumulators_);
260 UTIL_CHECK(i >= 0 && i < nValue_);
261 values_[i] = value;
262 }
263
264}
265}
266#endif
const std::string & name(int i) const
Get name associated with a variable.
void setup() override
Setup before loop.
void outputAccumulators()
Write results of statistical analysis to files.
void sample(long iStep) override
Compute sampled values and update the accumulators.
void initializeAccumulators(int nValue)
Initialize Average accumulators and set nValue.
void output() override
Write final results to file after a simulation.
std::ofstream outputFile_
Output file stream.
void clearAccumulators()
Clear internal state of all accumulators.
void setName(int i, std::string name)
Set name of variable.
const Average & accumulator(int i) const
Get Average accumulator for a specific variable.
int nValue() const
Get the number of variables.
AverageListAnalyzer(typename T::Simulator &simulator, typename T::System &system)
Constructor.
void updateAccumulators(long iStep)
Add current value to accumulator, output block average if needed.
int nSamplePerOutput() const
Get value of nSamplePerOutput.
virtual void compute()=0
Compute values of sampled quantities.
void readParameters(std::istream &in) override
Read interval, outputFileName and (optionally) nSamplePerOutput.
void setValue(int i, double value)
Set current value, used by compute function.
Calculates the average and variance of a sampled property.
Definition Average.h:44
Dynamically allocatable contiguous array template.
Definition DArray.h:32
File containing preprocessor macros for error handling.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
Class templates for real-valued periodic fields.
PSCF package top-level namespace.