PSCF v1.4.0
TrajectoryWriter.tpp
1#ifndef RP_TRAJECTORY_WRITER_TPP
2#define RP_TRAJECTORY_WRITER_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 "TrajectoryWriter.h"
12#include <util/misc/FileMaster.h>
13#include <util/misc/ioUtil.h>
14
15namespace Pscf {
16namespace Rp {
17
18 using namespace Util;
19
20 /*
21 * Constructor.
22 */
23 template <int D, class T>
24 TrajectoryWriter<D,T>::TrajectoryWriter(typename T::Simulator& simulator,
25 typename T::System& system)
26 : AnalyzerT(simulator, system),
27 nSample_(0),
28 isInitialized_(false)
29 { ParamComposite::setClassName("TrajectoryWriter"); }
30
31 /*
32 * Read interval and outputFileName.
33 */
34 template <int D, class T>
36 {
37 AnalyzerT::readParameters(in);
38 isInitialized_ = true;
39 }
40
41 /*
42 * Setup before the main loop.
43 */
44 template <int D, class T>
46 {
47 nSample_ = 0;
48 std::string filename = AnalyzerT::outputFileName();
49 system().fileMaster().openOutputFile(filename, outputFile_);
50 writeHeader(outputFile_);
51 }
52
53 /*
54 * Periodically write a frame to file.
55 */
56 template <int D, class T>
58 {
59 if (AnalyzerT::isAtInterval(iStep)) {
60 writeFrame(outputFile_, iStep);
61 ++nSample_;
62 }
63 }
64
65 /*
66 * Close the output file at end of simulation.
67 */
68 template <int D, class T>
70 { outputFile_.close(); }
72 /*
73 * Write the trajectory file header.
74 */
75 template <int D, class T>
76 void TrajectoryWriter<D,T>::writeHeader(std::ofstream& out)
77 {
78 int nMonomer = system().mixture().nMonomer();
79 bool isSymmetric = false;
80 typename T::Domain const & domain = system().domain();
81 typename T::FieldIo const & fieldIo = domain.fieldIo();
82 fieldIo.writeFieldHeader(out, nMonomer, domain.unitCell(),
83 isSymmetric);
84 out << "\n";
85 }
86
87 /*
88 * Write a frame in r-grid file format.
89 */
90 template <int D, class T>
91 void TrajectoryWriter<D,T>::writeFrame(std::ofstream& out, long iStep)
92 {
93 out << "i = " << iStep << "\n";
94 bool writeHeader = false;
95 bool isSymmetric = false;
96 typename T::Domain const & domain = system().domain();
97 typename T::FieldIo const & fieldIo = domain.fieldIo();
98 fieldIo.writeFieldsRGrid(out, system().w().rgrid(),
99 domain.unitCell(),
100 writeHeader, isSymmetric);
101 out << "\n";
102 }
103
104}
105}
106#endif
void setup() override
Setup before main simulation loop.
void output() override
Close trajectory file after run.
void writeFrame(std::ofstream &out, long iStep)
Write data that should appear in every frame.
void readParameters(std::istream &in) override
Read interval and output file name.
void sample(long iStep) override
Write a frame/snapshot to the trajectory file.
TrajectoryWriter(typename T::Simulator &simulator, typename T::System &system)
Constructor.
void setClassName(const char *className)
Set class name string.
Class templates for real-valued periodic fields.
PSCF package top-level namespace.