PSCF v1.5.0
AverageListAnalyzer.tpp
1#ifndef RP_AVERAGE_LIST_ANALYZER_TPP
2#define RP_AVERAGE_LIST_ANALYZER_TPP
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 "AverageListAnalyzer.h"
12
13#include <rp/system/System.h>
14#include <util/format/Int.h>
15#include <util/format/Dbl.h>
16#include <util/misc/FileMaster.h>
17#include <util/misc/ioUtil.h>
18
19namespace Pscf {
20namespace Rp {
21
22 using namespace Util;
23
24 /*
25 * Constructor.
26 */
27 template <int D, class T>
31 : AnalyzerT(simulator, system),
32 nSamplePerOutput_(1),
33 nValue_(0),
34 hasAccumulators_(false)
36
37 /*
38 * Read interval and outputFileName.
39 */
40 template <int D, class T>
42 {
44 nSamplePerOutput_ = 1;
45 ParamComposite::readOptional(in,"nSamplePerOutput",
46 nSamplePerOutput_);
47 UTIL_CHECK(nSamplePerOutput_ >= 0);
48 }
49
50 /*
51 * Setup before simulation.
52 */
53 template <int D, class T>
55 {
56 UTIL_CHECK(hasAccumulators_);
58
59 if (nSamplePerOutput() > 0) {
60 std::string fileName = AnalyzerT::outputFileName(".dat");
61 system().fileMaster().openOutputFile(fileName, outputFile_);
62 }
63 // Note: ReadParameters method of derived classes should call this,
64 // determine nValue and then call initializeAccumulators(nValue).
65 }
66
67 /*
68 * Compute and sample current values.
69 */
70 template <int D, class T>
72 {
73 UTIL_CHECK(hasAccumulators_);
74 if (AnalyzerT::isAtInterval(iStep)) {
75 compute();
76 updateAccumulators(iStep);
77 }
78 }
79
80 /*
81 * Output results after a simulation is completed.
82 */
83 template <int D, class T>
85 {
86 UTIL_CHECK(hasAccumulators_);
87
88 // Close data file, if any
89 if (outputFile_.is_open()) {
90 outputFile_.close();
91 }
92
93 #if 0
94 // Write parameter (*.prm) file
95 std::string filename = AnalyzerT::outputFileName(".prm");
96 system().fileMaster().openOutputFile(filename, outputFile_);
98 outputFile_.close();
99 #endif
100
101 // Write average (*.ave) and error analysis (*.aer) files
103 }
104
108 template <int D, class T>
110 {
111 UTIL_CHECK(nValue > 0);
112 UTIL_CHECK(nValue_ == 0);
113 UTIL_CHECK(nSamplePerOutput_ >= 0);
114
115 // Allocate arrays
116 accumulators_.allocate(nValue);
117 names_.allocate(nValue);
118 values_.allocate(nValue);
119 nValue_ = nValue;
120 hasAccumulators_ = true;
121
122 // Set the accumulators to compute block averages with
123 // nSamplePerOutput_ sampled values per block
124 for (int i = 0; i < nValue_; ++i) {
125 accumulators_[i].setNSamplePerBlock(nSamplePerOutput_);
126 }
127
129 }
130
131 /*
132 * Clear accumulators.
133 */
134 template <int D, class T>
136 {
137 UTIL_CHECK(hasAccumulators_);
138 UTIL_CHECK(nValue_ > 0);
139 for (int i = 0; i < nValue_; ++i) {
140 accumulators_[i].clear();
141 }
142 }
143
144 /*
145 * Set the name string for variable with index i.
146 */
147 template <int D, class T>
149 {
150 UTIL_CHECK(hasAccumulators_);
151 UTIL_CHECK(i >= 0 && i < nValue_);
152 names_[i] = name;
153 }
154
155 /*
156 * Update accumulators for all current values.
157 */
158 template <int D, class T>
160 {
161 UTIL_CHECK(hasAccumulators_);
162 UTIL_CHECK(accumulators_.capacity() == nValue_);
163
164 // Update accumulators.
165 for (int i = 0; i < nValue(); ++i) {
166 double data = value(i);
167 accumulators_[i].sample(data);
168 }
169
170 // Output block averages
171 if (nSamplePerOutput_ > 0) {
172 if (accumulators_[0].isBlockComplete()) {
173 UTIL_CHECK(outputFile_.is_open());
175 int beginStep = iStep - (nSamplePerOutput_ - 1) * interval;
176 outputFile_ << Int(beginStep);
177 for (int i = 0; i < nValue(); ++i) {
178 UTIL_CHECK(accumulators_[i].isBlockComplete());
179 double block = accumulators_[i].blockAverage();
180 outputFile_ << Dbl(block);
181 }
182 outputFile_ << "\n";
183 }
184 }
185
186 }
187
188 /*
189 * Output results to file after simulation is completed.
190 */
191 template <int D, class T>
193 {
194 UTIL_CHECK(hasAccumulators_);
195
196 // Close data (*.dat) file, if any
197 if (outputFile_.is_open()) {
198 outputFile_.close();
199 }
200
201 // Compute maximum length of name field
202 int nameWidth = 0;
203 int length;
204 for (int i = 0; i < nValue_; ++i) {
205 length = names_[i].length();
206 if (length > nameWidth) {
207 nameWidth = length;
208 }
209 }
210 nameWidth += 2;
211
212 // Write average (*.ave) file
213 std::string fileName = AnalyzerT::outputFileName(".ave");
214 system().fileMaster().openOutputFile(fileName, outputFile_);
215 double ave, err;
216 for (int i = 0; i < nValue_; ++i) {
217 ave = accumulators_[i].average();
218 err = accumulators_[i].blockingError();
219 outputFile_ << " " << std::left << std::setw(nameWidth)
220 << names_[i] << " ";
221 outputFile_ << Dbl(ave) << " +- " << Dbl(err, 9, 2) << "\n";
222 }
223 outputFile_.close();
224
225 // Write error analysis (*.aer) file
226 fileName = AnalyzerT::outputFileName(".aer");
227 system().fileMaster().openOutputFile(fileName, outputFile_);
228 std::string line;
229 line =
230 "------------------------------------------------------------------";
231 for (int i = 0; i < nValue_; ++i) {
232 outputFile_ << line << std::endl;
233 outputFile_ << names_[i] << " :" << std::endl;
234 accumulators_[i].output(outputFile_);
235 outputFile_ << std::endl;
236 }
237 outputFile_.close();
238
239 #if 0
240 // Write data format file (*.dfm) file
241 fileName = AnalyzerT::outputFileName();
242 fileName += ".dfm";
243 system().fileMaster().openOutputFile(fileName, outputFile_);
244 outputFile_ << "Value = " << nValue() << std::endl;
245 outputFile_ << "iStep ";
246 for (int i = 0; i < nValue_; ++i) {
247 outputFile_ << names_[i] << " ";
248 }
249 outputFile_ << std::endl;
250 outputFile_.close();
251 #endif
252
253 }
254
255}
256}
257#endif
bool isAtInterval(long counter) const
Return true iff counter is a multiple of the interval.
Definition Analyzer.h:219
void setFileMaster(FileMaster &fileMaster)
Set the FileMaster to use to open files.
Definition Analyzer.tpp:92
std::string const & outputFileName() const
Get the outputFileName string.
Definition Analyzer.h:226
virtual void readParameters(std::istream &in)
Read parameters from archive.
Definition Analyzer.tpp:45
int interval() const
Get the interval value.
Definition Analyzer.h:212
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.
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.
Base class for field theoretic PS-FTS simulator.
Definition Simulator.h:93
Base class template for classes that represent a complete system.
FileMaster const & fileMaster() const
Get the FileMaster (const).
Dbl()
Default constructor.
Definition Dbl.cpp:17
Int()
Default constructor.
Definition Int.cpp:19
virtual void writeParam(std::ostream &out) const
Write all parameters to an output stream.
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
Code for programs that simulate real-valued periodic fields.
Definition param_pc.dox:2
PSCF package top-level namespace.