PSCF v1.3
rpc/fts/analyzer/TrajectoryWriter.tpp
1#ifndef RPC_TRAJECTORY_WRITER_TPP
2#define RPC_TRAJECTORY_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 "TrajectoryWriter.h"
11#include "Analyzer.h"
12#include <rpc/fts/simulator/Simulator.h>
13#include <rpc/system/System.h>
14#include <util/misc/FileMaster.h>
15//#include <util/archives/Serializable_includes.h>
16#include <util/misc/ioUtil.h>
17#include <sstream>
18
19namespace Pscf {
20namespace Rpc
21{
22
23 using namespace Util;
24
25 /*
26 * Constructor.
27 */
28 template <int D>
31 : Analyzer<D>(),
32 nSample_(0),
33 isInitialized_(false),
34 simulatorPtr_(&simulator),
35 systemPtr_(&(simulator.system()))
36 { setClassName("TrajectoryWriter"); }
37
38 /*
39 * Read interval and outputFileName.
40 */
41 template <int D>
43 {
45 isInitialized_ = true;
46 }
47
48 /*
49 * Read interval and outputFileName.
50 */
51 template <int D>
53 {
54 nSample_ = 0;
55 std::string filename;
56 filename_ = outputFileName();
57 system().fileMaster().openOutputFile(filename_ , outputFile_);
58 writeHeader(outputFile_);
59 }
60
61 template <int D>
62 void TrajectoryWriter<D>::writeFrame(std::ofstream& out, long iStep)
63 {
64 out << "i = " << iStep << "\n";
65 bool writeHeader = false;
66 bool isSymmetric = false;
67 Domain<D> const & domain = system().domain();
68 FieldIo<D> const & fieldIo = domain.fieldIo();
69 fieldIo.writeFieldsRGrid(out, system().w().rgrid(),
70 domain.unitCell(),
71 writeHeader, isSymmetric);
72 out << "\n";
73 }
74
75
76 template <int D>
77 void TrajectoryWriter<D>::writeHeader(std::ofstream& out)
78 {
79 int nMonomer = system().mixture().nMonomer();
80 bool isSymmetric = false;
81 Domain<D> const & domain = system().domain();
82 FieldIo<D> const & fieldIo = domain.fieldIo();
83 fieldIo.writeFieldHeader(out, nMonomer,
84 domain.unitCell(), isSymmetric);
85 out << "\n";
86 }
87
88
89 /*
90 * Periodically write a frame to file
91 */
92 template <int D>
94 {
95 if (isAtInterval(iStep)) {
96 writeFrame(outputFile_, iStep);
97 ++nSample_;
98 }
99 }
100
101 /*
102 * Close output file at end of simulation.
103 */
104 template <int D>
106 { outputFile_.close(); }
107
108}
109}
110#endif
bool isAtInterval(long counter) const
Return true iff counter is a multiple of the interval.
virtual void readParameters(std::istream &in)
Read parameters from archive.
const std::string & outputFileName() const
Return outputFileName string.
Analyzer()
Default constructor.
Spatial domain for a periodic structure with real fields, on a CPU.
FieldIo< D > & fieldIo()
Get the FieldIo by non-const reference.
UnitCell< D > & unitCell()
Get the UnitCell by non-const reference.
File input/output operations and format conversions for fields.
void writeFieldHeader(std::ostream &out, int nMonomer, UnitCell< D > const &unitCell, bool isSymmetric=true) const
Write header for field file (fortran pscf format).
void writeFieldsRGrid(std::ostream &out, DArray< RField< D > > const &fields, UnitCell< D > const &unitCell, bool writeHeader=true, bool isSymmetric=true, bool writeMeshSize=true) const override
Write array of RField objects (fields on r-space grid) to a stream.
Field theoretic simulator (base class).
Main class, representing one complete system.
virtual void readParameters(std::istream &in)
Read interval and output file name.
void setClassName(const char *className)
Set class name string.
Simulator< D > & simulator()
Return reference to parent Simulator.
void writeHeader(std::ofstream &out)
Write data that should appear once, at beginning of the file.
virtual void output()
Close trajectory file after run.
void writeFrame(std::ofstream &out, long iStep)
Write data that should appear in every frame.
TrajectoryWriter(Simulator< D > &simulator, System< D > &system)
Constructor.
virtual void setup()
Clear nSample counter.
System< D > & system()
Return reference to parent system.
virtual void sample(long iStep)
Write a frame/snapshot to trajectory file.
Real periodic fields, SCFT and PS-FTS (CPU).
Definition param_pc.dox:2
PSCF package top-level namespace.
Definition param_pc.dox:1