PSCF v1.5.0
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 - 2026, David Morse (morse012@um.edu) and co-authors
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <rp/fts/analyzer/Analyzer.h> // base class
12#include <util/accumulators/Average.h> // member
13#include <util/containers/DArray.h> // member
14
15#include <pscf/backend/TmplDeclare.h>
16#include <util/global.h>
17
18namespace Pscf {
19namespace Rp {
20
21 using namespace Util;
22
39 template <int D, class T>
40 class AverageListAnalyzer : public Analyzer<D,T>
41 {
42
43 public:
44
53
63 void readParameters(std::istream& in) override;
64
70 void setup() override;
71
77 void sample(long iStep) override;
78
82 void output() override;
83
91 int nSamplePerOutput() const;
92
96 int nValue() const;
97
103 const std::string& name(int i) const;
104
110 const Average& accumulator(int i) const;
111
112 protected:
113
115 std::ofstream outputFile_;
116
126
132 void clearAccumulators();
133
140 void setName(int i, std::string name);
141
148 void setValue(int i, double value);
149
153 virtual void compute() = 0;
154
160 double value(int i) const;
161
167 void updateAccumulators(long iStep);
168
172 void outputAccumulators();
173
174 using AnalyzerT = Analyzer<D,T>;
176 using AnalyzerT::system;
177
178 private:
179
181 DArray<Average> accumulators_;
182
184 DArray<double> values_;
185
187 DArray<std::string> names_;
188
190 int nSamplePerOutput_;
191
193 int nValue_;
194
196 bool hasAccumulators_;
197
198 };
199
200 // Inline functions
201
202 /*
203 * Get nSamplePerOutput.
204 */
205 template <int D, class T> inline
207 { return nSamplePerOutput_; }
208
209 /*
210 * Get nValue (number of variables).
211 */
212 template <int D, class T> inline
214 {
215 UTIL_CHECK(hasAccumulators_);
216 return nValue_;
217 }
218
219 /*
220 * Get current value of a variable, set by compute function.
221 */
222 template <int D, class T> inline
224 {
225 UTIL_CHECK(hasAccumulators_);
226 UTIL_CHECK(i >= 0 && i < nValue_);
227 return values_[i];
228 }
229
230 /*
231 * Get name of specific variable.
232 */
233 template <int D, class T> inline
234 const std::string& AverageListAnalyzer<D,T>::name(int i) const
235 {
236 UTIL_CHECK(hasAccumulators_);
237 UTIL_CHECK(i >= 0 && i < nValue_);
238 return names_[i];
239 }
240
241 /*
242 * Get accumulator associated with a variable.
243 */
244 template <int D, class T> inline
246 {
247 UTIL_CHECK(hasAccumulators_);
248 UTIL_CHECK(i >= 0 && i < nValue_);
249 return accumulators_[i];
250 }
251
252 /*
253 * Set current value of a variable.
254 */
255 template <int D, class T> inline
257 {
258 UTIL_CHECK(hasAccumulators_);
259 UTIL_CHECK(i >= 0 && i < nValue_);
260 values_[i] = value;
261 }
262
263 // Explicit instantiation declarations
264 PSCF_TMPL_DECLARE(AverageListAnalyzer)
265
266}
267}
268#endif
Analyzer(Simulator< D, T > &simulator, System< D, T > &system)
Constructor.
Definition Analyzer.tpp:31
Simulator< D, T > & simulator()
Get the parent Simulator by reference.
Definition Analyzer.h:233
System< D, T > & system()
Get the parent System by reference.
Definition Analyzer.h:243
Analyze averages and block averages of several real variables.
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.
double value(int i) const
Get current value of a specific variable.
void initializeAccumulators(int nValue)
Initialize Average accumulators and set nValue.
Simulator< D, T > & simulator()
Get the parent Simulator by reference.
Definition Analyzer.h:233
void output() override
Write final results to file after a simulation.
AverageListAnalyzer(Simulator< D, T > &simulator, System< D, T > &system)
Constructor.
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.
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.
System< D, T > & system()
Get the parent System by reference.
Definition Analyzer.h:243
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.
Base class for field theoretic PS-FTS simulator.
Definition Simulator.h:93
Base class template for classes that represent a complete system.
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
Code for programs that simulate real-valued periodic fields.
Definition param_pc.dox:2
PSCF package top-level namespace.