PSCF v1.4.0
RGridTrajectoryReader.tpp
1#ifndef RP_RGRID_TRAJECTORY_READER_TPP
2#define RP_RGRID_TRAJECTORY_READER_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 "RGridTrajectoryReader.h"
12#include <util/misc/ioUtil.h>
13#include <util/misc/FileMaster.h>
14
15namespace Pscf {
16namespace Rp {
17
18 using namespace Util;
19
20 /*
21 * Constructor.
22 */
23 template <int D, class T>
25 typename T::System& system)
26 : TrajectoryReaderT(system),
27 isAllocated_(false)
28 {}
29
30 /*
31 * Allocate required memory.
32 */
33 template <int D, class T>
34 void RGridTrajectoryReader<D,T>::allocate()
35 {
36 const int nMonomer = system().mixture().nMonomer();
37 UTIL_CHECK(nMonomer > 0);
38 meshDimensions_ = system().domain().mesh().dimensions();
39 if (!isAllocated_){
40 wField_.allocate(nMonomer);
41 for (int i = 0; i < nMonomer; ++i) {
42 wField_[i].allocate(meshDimensions_);
43 }
44 isAllocated_ = true;
45 }
46 }
47
48 /*
49 * Open file and allocate memory if necessary.
50 */
51 template <int D, class T>
52 void RGridTrajectoryReader<D,T>::open(std::string filename)
53 {
54 system().fileMaster().open(filename, inputfile_);
55 allocate();
56 }
57
58 /*
59 * Read trajectory file header section.
60 */
61 template <int D, class T>
63 {
64 int nMonomer = system().mixture().nMonomer();
65 typename T::FieldIo const & fieldIo = system().domain().fieldIo();
66 UnitCell<D> tmpUnitCell;
68 fieldIo.readFieldHeader(inputfile_, nMonomer, tmpUnitCell,
70 system().setUnitCell(tmpUnitCell);
71 Log::file() << "Read Header" << "\n";
72 }
73
74 /*
75 * Read frame, return false if end-of-file.
76 */
77 template <int D, class T>
79 {
80 // Preconditions
81 if (!isAllocated_) {
82 UTIL_THROW("Memory not allocated in RGridTrajectoryReader");
83 }
84
85 bool notEnd;
86 std::stringstream line;
87
88 // Attempt to read first line, check for end of file
89 notEnd = getNextLine(inputfile_, line);
90 if (!notEnd) {
91 return false;
92 }
93
94 // Read line containing time step
95 checkString(line, "i");
96 checkString(line, "=");
97 #if 0
98 std::string value;
99 line >> value;
100 int step;
101 step = std::stoi(value);
102 Log::file() << "step " << step << "\n";
103 #endif
104
105 // Read mesh dimensions
106 notEnd = getNextLine(inputfile_, line);
107 UTIL_CHECK(notEnd);
108 checkString(line, "mesh");
109
110 // Read empty line
111 notEnd = getNextLine(inputfile_, line);
112 UTIL_CHECK(notEnd);
113
114 // Read a w-field configuration in r-grid format
115 int nMonomer = system().mixture().nMonomer();
116 typename T::FieldIo const & fieldIo = system().domain().fieldIo();
117 fieldIo.readFieldsRGridData(inputfile_, wField_, nMonomer);
118
119 // Update system r-grid w fields
120 system().w().setRGrid(wField_);
121
122 return true;
123 }
124
125 /*
126 * Close trajectory file.
127 */
128 template <int D, class T>
130 { inputfile_.close(); }
131
132}
133}
134#endif
Base template for UnitCell<D> classes, D=1, 2 or 3.
Definition UnitCell.h:56
bool readFrame() override
Read a single frame from the trajectory file.
RGridTrajectoryReader(typename T::System &system)
Constructor.
void readHeader() override
Read header of trajectory file (if any).
void close() override
Close the trajectory file.
void open(std::string filename) override
Open trajectory file and read header, if any.
static std::ostream & file()
Get log ostream by reference.
Definition Log.cpp:59
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition global.h:49
void checkString(std::istream &in, const std::string &expected)
Extract string from stream, and compare to expected value.
Definition ioUtil.cpp:37
bool getNextLine(std::istream &in, std::string &line)
Read the next non-empty line into a string, strip trailing whitespace.
Definition ioUtil.cpp:79
bool hasSymmetry(AT const &in, Basis< D > const &basis, IntVec< D > const &dftDimensions, double epsilon=1.0e-8, bool verbose=true)
Check if a k-grid field has the declared space group symmetry.
Class templates for real-valued periodic fields.
PSCF package top-level namespace.