PSCF v1.4.0
ConcentrationWriter.tpp
1#ifndef RP_CONCENTRATION_WRITER_TPP
2#define RP_CONCENTRATION_WRITER_TPP
3/*
4* PSCF - Polymer Self-Consistent Field
5*
6* Copyright 2015 - 2025, The Regents of the University of Minnesota
7* Distributed under the terms of the GNU General Public License.
8*/
9
10#include "ConcentrationWriter.h"
11#include <prdc/crystal/UnitCell.h>
12#include <util/misc/FileMaster.h>
13#include <util/misc/ioUtil.h>
14
15#include <string>
16
17namespace Pscf {
18namespace Rp {
19
20 using namespace Util;
21
22 /*
23 * Constructor.
24 */
25 template <int D, class T>
27 typename T::Simulator& simulator,
28 typename T::System& system)
29 : AnalyzerT(simulator, system),
30 nSample_(0),
31 isInitialized_(false)
32 { ParamComposite::setClassName("ConcentrationWriter"); }
33
34 /*
35 * Read interval and outputFileName.
36 */
37 template <int D, class T>
39 {
40 AnalyzerT::readParameters(in);
41 isInitialized_ = true;
42 }
43
44 /*
45 * Initialize before main simulation loop.
46 */
47 template <int D, class T>
49 {
50 UTIL_CHECK(isInitialized_);
51 nSample_ = 0;
52 std::string filename = AnalyzerT::outputFileName();
53 system().fileMaster().openOutputFile(filename, outputFile_);
54 writeHeader(outputFile_);
55 }
56
57 template <int D, class T>
58 void ConcentrationWriter<D,T>::writeFrame(std::ofstream& out, long iStep)
59 {
60 UTIL_CHECK(isInitialized_);
61 UTIL_CHECK(system().w().hasData());
62 if (!system().c().hasData()){
63 system().compute();
64 }
65 out << "i = " << iStep << "\n";
66 bool writeHeader = false;
67 bool isSymmetric = false;
68 typename T::Domain const & domain = system().domain();
69 typename T::FieldIo const & fieldIo = domain.fieldIo();
70 fieldIo.writeFieldsRGrid(out, system().c().rgrid(),
71 domain.unitCell(),
72 writeHeader, isSymmetric);
73 out << "\n";
74 }
75
76 /*
77 * Write field file header.
78 */
79 template <int D, class T>
80 void ConcentrationWriter<D,T>::writeHeader(std::ofstream& out)
81 {
82 UTIL_CHECK(isInitialized_);
83 int nMonomer = system().mixture().nMonomer();
84 bool isSymmetric = false;
85 typename T::Domain const & domain = system().domain();
86 typename T::FieldIo const & fieldIo = domain.fieldIo();
87 fieldIo.writeFieldHeader(out, nMonomer,
88 domain.unitCell(), isSymmetric);
89 out << "\n";
90 }
91
92
93 /*
94 * Periodically write a frame to file
95 */
96 template <int D, class T>
98 {
99 UTIL_CHECK(isInitialized_);
100 if (AnalyzerT::isAtInterval(iStep)) {
101 writeFrame(outputFile_, iStep);
102 ++nSample_;
103 }
104 }
105
106 /*
107 * Close output file at end of simulation.
108 */
109 template <int D, class T>
111 { outputFile_.close(); }
112
113}
114}
115#endif
void readParameters(std::istream &in) override
Read interval and output file name.
ConcentrationWriter(typename T::Simulator &simulator, typename T::System &system)
Constructor.
void output() override
Close trajectory file after run.
void sample(long iStep) override
Write a frame/snapshot to trajectory file.
void setup() override
Initialize before main simulation loop.
void setClassName(const char *className)
Set class name string.
#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.