PSCF v1.5.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 - 2026, David Morse (morse012@um.edu) and co-authors
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include "AverageAnalyzer.h"
12#include <rp/fts/simulator/Simulator.h>
13#include <rp/system/System.h>
14
15#include <util/format/Int.h>
16#include <util/format/Dbl.h>
17#include <util/misc/FileMaster.h>
18#include <util/misc/ioUtil.h>
19
20namespace Pscf {
21namespace Rp {
22
23 using namespace Util;
24
25 /*
26 * Constructor.
27 */
28 template <int D, class T>
34
35 /*
36 * Read interval, outputFileName, and nSamplePerOutput.
37 */
38 template <int D, class T>
40 {
41 // Read interval and outputFileName
43
44 // Read nSamplePerOutput_
45 nSamplePerOutput_ = 1;
46 ParamComposite::readOptional(in,"nSamplePerOutput",
47 nSamplePerOutput_);
48
49 // Set the Average accumulator to compute block averages
50 // for blocks containing nSamplePerOutput_ sampled values
51 accumulator_.setNSamplePerBlock(nSamplePerOutput_);
52 }
53
54 /*
55 * Setup before main loop.
56 */
57 template <int D, class T>
59 {
60 accumulator_.clear();
61
62 if (nSamplePerOutput_ > 0) {
63 std::string fileName = Analyzer<D,T>::outputFileName(".dat");
64 Analyzer<D,T>::system().fileMaster().openOutputFile(fileName,
66 }
67 }
68
69 /*
70 * Compute and sample current values.
71 */
72 template <int D, class T>
74 {
75 if (!Analyzer<D,T>::isAtInterval(iStep)) return;
76
77 double value = compute();
78 accumulator_.sample(value);
79
80 // Output value or block average
81 if (nSamplePerOutput_ > 0) {
82 if (nSamplePerOutput_ == 1) {
83 outputValue(iStep, value);
84 } else
85 if (accumulator_.isBlockComplete()) {
87 int beginStep = iStep - (nSamplePerOutput_ - 1)*interval;
88 value = accumulator_.blockAverage();
89 outputValue(beginStep, value);
90 }
91 }
92
93 }
94
95 /*
96 * Write a sampled or block average value to file.
97 */
98 template <int D, class T>
99 void AverageAnalyzer<D,T>::outputValue(int step, double value)
100 {
101 UTIL_CHECK(outputFile_.is_open());
102 outputFile_ << Int(step);
103 outputFile_ << Dbl(value);
104 outputFile_ << "\n";
105 }
106
107 /*
108 * Output results after a system is completed.
109 */
110 template <int D, class T>
112 {
113 // Close data file, if any
114 if (outputFile_.is_open()) {
115 outputFile_.close();
116 }
117 std::string fileName;
118
119 #if 0
120 // Write parameter (*.prm) file
121 fileName = Analyzer<D,T>::outputFileName(".prm");
122 Analyzer<D,T>::system().fileMaster().openOutputFile(fileName,
125 outputFile_.close();
126 #endif
127
128 // Write average (*.ave) file
129 fileName = Analyzer<D,T>::outputFileName(".ave");
130 Analyzer<D,T>::system().fileMaster().openOutputFile(fileName,
132 double ave = accumulator_.average();
133 outputFile_ << "Average = ";
134 outputFile_ << Dbl(ave);
135 if (!Analyzer<D,T>::simulator().hasRamp()) {
136 double err = accumulator_.blockingError();
137 outputFile_ << " +- " << Dbl(err, 10, 3);
138 }
139 outputFile_ << "\n";
140
141 // Write error analysis to file
142 if (!Analyzer<D,T>::simulator().hasRamp()) {
143 outputFile_ << "\n";
144 std::string line;
145 line =
146 "----------------------------------------------------------------";
147 outputFile_ << line << std::endl;
149 outputFile_ << std::endl;
150 }
151
152 outputFile_.close();
153 }
154
155} // namespace Rp
156} // namespace Pscf
157#endif
Analyzer(Simulator< D, T > &simulator, System< D, T > &system)
Constructor.
Definition Analyzer.tpp:31
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
Simulator< D, T > & simulator()
Get the parent Simulator by reference.
Definition Analyzer.h:233
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
System< D, T > & system()
Get the parent System by reference.
Definition Analyzer.h:243
virtual void outputValue(int step, double value)
Output a sampled or block average 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.
Average accumulator_
Average object.
AverageAnalyzer(Simulator< D, T > &simulator, System< D, T > &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.
virtual double compute()=0
Compute value of sampled quantity.
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.