PSCF v1.3.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 <rpc/solvers/Mixture.h>
15#include <rpc/field/Domain.h>
16#include <util/misc/FileMaster.h>
17//#include <util/archives/Serializable_includes.h>
18#include <util/misc/ioUtil.h>
19#include <sstream>
20
21namespace Pscf {
22namespace Rpc
23{
24
25 using namespace Util;
26
27 /*
28 * Constructor.
29 */
30 template <int D>
33 : Analyzer<D>(),
34 nSample_(0),
35 isInitialized_(false),
36 simulatorPtr_(&simulator),
37 systemPtr_(&(simulator.system()))
38 { setClassName("TrajectoryWriter"); }
39
40 /*
41 * Read interval and outputFileName.
42 */
43 template <int D>
45 {
47 isInitialized_ = true;
48 }
49
50 /*
51 * Read interval and outputFileName.
52 */
53 template <int D>
55 {
56 nSample_ = 0;
57 std::string filename;
58 filename_ = outputFileName();
59 system().fileMaster().openOutputFile(filename_ , outputFile_);
60 writeHeader(outputFile_);
61 }
62
63 template <int D>
64 void TrajectoryWriter<D>::writeFrame(std::ofstream& out, long iStep)
65 {
66 out << "i = " << iStep << "\n";
67 bool writeHeader = false;
68 bool isSymmetric = false;
69 Domain<D> const & domain = system().domain();
70 FieldIo<D> const & fieldIo = domain.fieldIo();
71 fieldIo.writeFieldsRGrid(out, system().w().rgrid(),
72 domain.unitCell(),
73 writeHeader, isSymmetric);
74 out << "\n";
75 }
76
77
78 template <int D>
79 void TrajectoryWriter<D>::writeHeader(std::ofstream& out)
80 {
81 int nMonomer = system().mixture().nMonomer();
82 bool isSymmetric = false;
83 Domain<D> const & domain = system().domain();
84 FieldIo<D> const & fieldIo = domain.fieldIo();
85 fieldIo.writeFieldHeader(out, nMonomer,
86 domain.unitCell(), isSymmetric);
87 out << "\n";
88 }
89
90
91 /*
92 * Periodically write a frame to file
93 */
94 template <int D>
96 {
97 if (isAtInterval(iStep)) {
98 writeFrame(outputFile_, iStep);
99 ++nSample_;
100 }
101 }
102
103 /*
104 * Close output file at end of simulation.
105 */
106 template <int D>
108 { outputFile_.close(); }
109
110}
111}
112#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.
UnitCell< D > & unitCell()
Get the UnitCell by non-const reference.
FieldIo< D > & fieldIo()
Get the FieldIo 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 a complete physical 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.