PSCF v1.3
rpc/fts/analyzer/AverageListAnalyzer.h
1#ifndef RPC_AVERAGE_LIST_ANALYZER_H
2#define RPC_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 "Analyzer.h"
12#include <util/accumulators/Average.h> // member
13
14namespace Pscf {
15namespace Rpc
16{
17
18 template <int D> class System;
19 template <int D> class Simulator;
20
21 using namespace Util;
22
33 template <int D>
34 class AverageListAnalyzer : public Analyzer<D>
35 {
36
37 public:
38
46
50 virtual ~AverageListAnalyzer();
51
61 virtual void readParameters(std::istream& in);
62
68 virtual void setup();
69
75 virtual void sample(long iStep);
76
80 virtual void output();
81
89 int nSamplePerOutput() const;
90
94 int nValue() const;
95
103 const std::string& name(int i) const;
104
110 const Average& accumulator(int i) const;
111
112 using Analyzer<D>::interval;
113 using Analyzer<D>::isAtInterval;
115
116 protected:
117
120 using Analyzer<D>::setClassName;
121 using Analyzer<D>::readInterval;
122
131 void setName(int i, std::string name);
132
140
146 void clearAccumulators();
147
153 virtual void compute() = 0;
154
161 void setValue(int i, double value);
162
170 double value(int i) const;
171
177 void updateAccumulators(long iStep);
178
182 void outputAccumulators();
183
188
192 System<D>& system();
193
195 std::ofstream outputFile_;
196
197 private:
198
200 DArray<Average> accumulators_;
201
203 DArray<double> values_;
204
206 DArray<std::string> names_;
207
209 Simulator<D>* simulatorPtr_;
210
212 System<D>* systemPtr_;
213
215 int nSamplePerOutput_;
216
218 int nValue_;
219
221 bool hasAccumulators_;
222
223 };
224
225 // Inline functions
226
227 /*
228 * Get nSamplePerOutput.
229 */
230 template <int D>
232 { return nSamplePerOutput_; }
233
234 /*
235 * Get nValue (number of variables).
236 */
237 template <int D>
239 {
240 UTIL_CHECK(hasAccumulators_);
241 return nValue_;
242 }
243
244 /*
245 * Get current value of a variable, set by compute function.
246 */
247 template <int D>
248 inline double AverageListAnalyzer<D>::value(int i) const
249 {
250 UTIL_CHECK(hasAccumulators_);
251 UTIL_CHECK(i >= 0 && i < nValue_);
252 return values_[i];
253 }
254
255 /*
256 * Get name of specific variable.
257 */
258 template <int D>
259 inline const std::string& AverageListAnalyzer<D>::name(int i) const
260 {
261 UTIL_CHECK(hasAccumulators_);
262 UTIL_CHECK(i >= 0 && i < nValue_);
263 return names_[i];
264 }
265
266 /*
267 * Get accumulator associated with a variable.
268 */
269 template <int D>
271 {
272 UTIL_CHECK(hasAccumulators_);
273 UTIL_CHECK(i >= 0 && i < nValue_);
274 return accumulators_[i];
275 }
276
277 /*
278 * Set current value of a variable.
279 */
280 template <int D>
281 inline void AverageListAnalyzer<D>::setValue(int i, double value)
282 {
283 UTIL_CHECK(hasAccumulators_);
284 UTIL_CHECK(i >= 0 && i < nValue_);
285 values_[i] = value;
286 }
287
288 // Get the parent system.
289 template <int D>
291 { return *systemPtr_; }
292
293 // Get parent Simulator object.
294 template <int D>
296 { return *simulatorPtr_; }
297
298 #ifndef RPC_AVERAGE_LIST_ANALYZER_TPP
299 // Suppress implicit instantiation
300 extern template class AverageListAnalyzer<1>;
301 extern template class AverageListAnalyzer<2>;
302 extern template class AverageListAnalyzer<3>;
303 #endif
304
305}
306}
307#endif
bool isAtInterval(long counter) const
Return true iff counter is a multiple of the interval.
void readInterval(std::istream &in)
Optionally read interval from file, with error checking.
const std::string & outputFileName() const
Return outputFileName string.
int interval() const
Get interval value.
Analyzer()
Default constructor.
Analyze averages and block averages of several real variables.
void updateAccumulators(long iStep)
Add current value to accumulator, output block average if needed.
void clearAccumulators()
Clear internal state of all accumulators.
int nSamplePerOutput() const
Get value of nSamplePerOutput.
const Average & accumulator(int i) const
Get Average accumulator for a specific value.
AverageListAnalyzer(Simulator< D > &simulator, System< D > &system)
Constructor.
void initializeAccumulators(int nValue)
Instantiate Average accumulators and set nValue.
virtual void output()
Write final results to file after a simulation.
double value(int i) const
Get current value of a specific variable.
const std::string & name(int i) const
Get name associated with value.
System< D > & system()
Return reference to parent system.
virtual void compute()=0
Compute value of sampled quantities.
void setName(int i, std::string name)
Set name of variable.
virtual void sample(long iStep)
Compute a sampled value and update the accumulator.
void outputAccumulators()
Write results of statistical analysis to files.
void setValue(int i, double value)
Set current value, used by compute function.
Simulator< D > & simulator()
Return reference to parent Simulator.
virtual void readParameters(std::istream &in)
Read interval, outputFileName and (optionally) nSamplePerOutput.
Field theoretic simulator (base class).
Main class, representing one complete system.
Calculates the average and variance of a sampled property.
Definition Average.h:44
Dynamically allocatable contiguous array template.
Definition DArray.h:32
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 > & readOptional(std::istream &in, const char *label, Type &value)
Add and read a new optional ScalarParam < Type > object.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
Real periodic fields, SCFT and PS-FTS (CPU).
Definition param_pc.dox:2
PSCF package top-level namespace.
Definition param_pc.dox:1