PSCF v1.4.0
AverageAnalyzer.tpp
1#ifndef RP_AVERAGE_ANALYZER_TPP
2#define RP_AVERAGE_ANALYZER_TPP
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 "AverageAnalyzer.h"
12
13#include <util/format/Int.h>
14#include <util/format/Dbl.h>
15#include <util/misc/FileMaster.h>
16#include <util/misc/ioUtil.h>
17
18namespace Pscf {
19namespace Rp {
20
21 using namespace Util;
22
23 /*
24 * Constructor.
25 */
26 template <int D, class T>
27 AverageAnalyzer<D,T>::AverageAnalyzer(typename T::Simulator& simulator,
28 typename T::System& system)
29 : AnalyzerT(simulator, system),
30 nSamplePerOutput_(1)
31 { AnalyzerT::setFileMaster(system.fileMaster()); }
32
33 /*
34 * Read interval, outputFileName, and nSamplePerOutput.
35 */
36 template <int D, class T>
38 {
39 // Read interval and outputFileName
40 AnalyzerT::readParameters(in);
41
42 // Read nSamplePerOutput_
43 nSamplePerOutput_ = 1;
44 ParamComposite::readOptional(in,"nSamplePerOutput",
45 nSamplePerOutput_);
46 if (nSamplePerOutput_ > 0) {
47 std::string fileName = AnalyzerT::outputFileName(".dat");
48 AnalyzerT::system().fileMaster().openOutputFile(fileName, outputFile_);
49 }
50
51 // Set the Average accumulator to compute block averages
52 // for blocks containing nSamplePerOutput_ sampled values
53 accumulator_.setNSamplePerBlock(nSamplePerOutput_);
54 }
55
56 /*
57 * Setup before system.
58 */
59 template <int D, class T>
62
63 /*
64 * Compute and sample current values.
65 */
66 template <int D, class T>
68 {
69 if (!AnalyzerT::isAtInterval(iStep)) return;
70
71 double value = compute();
72 accumulator_.sample(value);
73
74 // Output value or block average
75 if (nSamplePerOutput_ > 0) {
76 if (nSamplePerOutput_ == 1) {
77 outputValue(iStep, value);
78 } else
79 if (accumulator_.isBlockComplete()) {
80 int interval = AnalyzerT::interval();
81 int beginStep = iStep - (nSamplePerOutput_ - 1)*interval;
82 value = accumulator_.blockAverage();
83 outputValue(beginStep, value);
84 }
85 }
86
87 }
88
89 /*
90 * Write a sampled or block average value to file.
91 */
92 template <int D, class T>
93 void AverageAnalyzer<D,T>::outputValue(int step, double value)
94 {
95 UTIL_CHECK(outputFile_.is_open());
96 outputFile_ << Int(step);
97 outputFile_ << Dbl(value);
98 outputFile_ << "\n";
99 }
100
101 /*
102 * Output results after a system is completed.
103 */
104 template <int D, class T>
106 {
107 // Close data file, if any
108 if (outputFile_.is_open()) {
109 outputFile_.close();
110 }
111 std::string fileName;
112
113 #if 0
114 // Write parameter (*.prm) file
115 fileName = AnalyzerT::outputFileName(".prm");
116 AnalyzerT::system().fileMaster().openOutputFile(fileName,
117 outputFile_);
118 ParamComposite::writeParam(outputFile_);
119 outputFile_.close();
120 #endif
121
122 // Write average (*.ave) file
123 fileName = AnalyzerT::outputFileName(".ave");
124 AnalyzerT::system().fileMaster().openOutputFile(fileName,
125 outputFile_);
126 double ave = accumulator_.average();
127 outputFile_ << "Average = ";
129 if (!AnalyzerT::simulator().hasRamp()) {
130 double err = accumulator_.blockingError();
131 outputFile_ << " +- " << Dbl(err, 10, 3);
132 }
133 outputFile_ << "\n";
134
135 // Write error analysis to file
136 if (!AnalyzerT::simulator().hasRamp()) {
137 outputFile_ << "\n";
138 std::string line;
139 line =
140 "----------------------------------------------------------------";
141 outputFile_ << line << std::endl;
142 accumulator_.output(outputFile_);
143 outputFile_ << std::endl;
144 }
145
146 outputFile_.close();
147 }
148
149} // namespace Rp
150} // namespace Rpc
151#endif
virtual void outputValue(int step, double value)
virtual void sample(long iStep)
Compute a sampled value and update the accumulator.
virtual void readParameters(std::istream &in)
Read interval, outputFileName and (optionally) nSamplePerOutput.
AverageAnalyzer(typename T::Simulator &simulator, typename T::System &system)
Constructor.
virtual void output()
Write final results to file after a simulation.
std::ofstream outputFile_
Output file stream.
virtual void setup()
Setup before loop.
Wrapper for a double precision number, for formatted ostream output.
Definition Dbl.h:40
Wrapper for an int, for formatted ostream output.
Definition Int.h:37
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
Class templates for real-valued periodic fields.
PSCF package top-level namespace.