PSCF v1.5.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 - 2026, David Morse (morse012@um.edu) and co-authors
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include "TrajectoryWriter.h"
12
13#include <rp/fts/simulator/Simulator.h>
14#include <rp/system/System.h>
15#include <rp/solvers/Mixture.h>
16#include <rp/field/Domain.h>
17#include <rp/field/FieldIo.h>
18#include <rp/field/WFields.h>
19
20#include <util/misc/FileMaster.h>
21#include <util/misc/ioUtil.h>
22
23namespace Pscf {
24namespace Rp {
25
26 using namespace Util;
27
28 /*
29 * Constructor.
30 */
31 template <int D, class T>
34 : Analyzer<D,T>(simulator, system),
35 nSample_(0),
36 isInitialized_(false)
37 { ParamComposite::setClassName("TrajectoryWriter"); }
38
39 /*
40 * Read interval and outputFileName.
41 */
42 template <int D, class T>
44 {
46 isInitialized_ = true;
47 }
48
49 /*
50 * Setup before the main loop.
51 */
52 template <int D, class T>
54 {
55 nSample_ = 0;
56 std::string filename = Analyzer<D,T>::outputFileName();
57 system().fileMaster().openOutputFile(filename, outputFile_);
58 writeHeader(outputFile_);
59 }
60
61 /*
62 * Periodically write a frame to file.
63 */
64 template <int D, class T>
66 {
67 if (Analyzer<D,T>::isAtInterval(iStep)) {
68 writeFrame(outputFile_, iStep);
69 ++nSample_;
70 }
71 }
72
73 /*
74 * Close the output file at end of simulation.
75 */
76 template <int D, class T>
78 { outputFile_.close(); }
79
80 /*
81 * Write the trajectory file header.
82 */
83 template <int D, class T>
84 void TrajectoryWriter<D,T>::writeHeader(std::ofstream& out)
85 {
86 int nMonomer = system().mixture().nMonomer();
87 bool isSymmetric = false;
88 Domain<D,T> const & domain = system().domain();
89 FieldIo<D,T> const & fieldIo = domain.fieldIo();
90 fieldIo.writeFieldHeader(out, nMonomer, domain.unitCell(),
91 isSymmetric);
92 out << "\n";
93 }
94
95 /*
96 * Write a frame in r-grid file format.
97 */
98 template <int D, class T>
99 void TrajectoryWriter<D,T>::writeFrame(std::ofstream& out, long iStep)
100 {
101 out << "i = " << iStep << "\n";
102 bool writeHeader = false;
103 bool isSymmetric = false;
104 Domain<D,T> const & domain = system().domain();
105 FieldIo<D,T> const & fieldIo = domain.fieldIo();
106 fieldIo.writeFieldsRGrid(out, system().w().rgrid(),
107 domain.unitCell(),
108 writeHeader, isSymmetric);
109 out << "\n";
110 }
111
112}
113}
114#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
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
System< D, T > & system()
Get the parent System by reference.
Definition Analyzer.h:243
Spatial domain for a periodic structure with real fields.
FieldIo< D, T > & fieldIo()
Get the FieldIo by non-const reference.
UnitCell< D > & unitCell()
Get the UnitCell by non-const reference.
File input/output, format conversions and other utilities for fields.
virtual void writeFieldsRGrid(std::ostream &out, DArray< RField< D, T > > const &fields, UnitCell< D > const &unitCell, bool writeHeader=true, bool isSymmetric=true, bool writeMeshSize=true) const
Write an array of r-grid fields to an output stream.
Definition FieldIo.tpp:426
void writeFieldHeader(std::ostream &out, int nMonomer, UnitCell< D > const &unitCell, bool isSymmetric=true) const
Write header for field file (fortran pscf format).
Definition FieldIo.tpp:1504
Base class for field theoretic PS-FTS simulator.
Definition Simulator.h:93
Base class template for classes that represent a complete system.
void setup() override
Setup before main simulation loop.
void output() override
Close trajectory file after run.
TrajectoryWriter(Simulator< D, T > &simulator, System< D, T > &system)
Constructor.
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.
void writeHeader(std::ofstream &out)
Write data that should appear once, at beginning of the file.
void setClassName(const char *className)
Set class name string.
Code for programs that simulate real-valued periodic fields.
Definition param_pc.dox:2
PSCF package top-level namespace.