PSCF v1.5.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 - 2026, David Morse (morse012@um.edu) and co-authors
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include "RGridTrajectoryReader.h"
12
13#include <rp/system/System.h>
14#include <rp/solvers/Mixture.h>
15#include <rp/field/Domain.h>
16#include <rp/field/FieldIo.h>
17#include <rp/field/WFields.h>
18#include <rp/field/CFields.h>
19
20#include <pscf/crystal/UnitCell.h>
21#include <util/misc/ioUtil.h>
22#include <util/misc/FileMaster.h>
23
24namespace Pscf {
25namespace Rp {
26
27 template <int D, class T> class FieldIo;
28
29 using namespace Util;
30
31 /*
32 * Constructor.
33 */
34 template <int D, class T>
40
41 /*
42 * Allocate required memory.
43 */
44 template <int D, class T>
45 void RGridTrajectoryReader<D,T>::allocate()
46 {
47 const int nMonomer = system().mixture().nMonomer();
48 UTIL_CHECK(nMonomer > 0);
49 meshDimensions_ = system().domain().mesh().dimensions();
50 if (!isAllocated_){
51 wField_.allocate(nMonomer);
52 for (int i = 0; i < nMonomer; ++i) {
53 wField_[i].allocate(meshDimensions_);
54 }
55 isAllocated_ = true;
56 }
57 }
58
59 /*
60 * Open file and allocate memory if necessary.
61 */
62 template <int D, class T>
63 void RGridTrajectoryReader<D,T>::open(std::string filename)
64 {
65 system().fileMaster().open(filename, inputfile_);
66 allocate();
67 }
68
69 /*
70 * Read trajectory file header section.
71 */
72 template <int D, class T>
74 {
75 FieldIo<D,T> const & fieldIo = system().domain().fieldIo();
76 UnitCell<D> tmpUnitCell;
77 int nMonomer;
78 bool hasSymmetry;
79 fieldIo.readFieldHeader(inputfile_, nMonomer, tmpUnitCell,
81 // readFieldHeader sets nMonomer to value in the header file
82 UTIL_CHECK(nMonomer == system().mixture().nMonomer());
83 system().setUnitCell(tmpUnitCell);
84 Log::file() << "Read Header" << "\n";
85 }
86
87 /*
88 * Read frame, return false if end-of-file.
89 */
90 template <int D, class T>
92 {
93 // Preconditions
94 if (!isAllocated_) {
95 UTIL_THROW("Memory not allocated in RGridTrajectoryReader");
96 }
97
98 bool notEnd;
99 std::stringstream line;
100
101 // Attempt to read first line, check for end of file
102 notEnd = getNextLine(inputfile_, line);
103 if (!notEnd) {
104 return false;
105 }
106
107 // Read line containing time step
108 checkString(line, "i");
109 checkString(line, "=");
110 #if 0
111 std::string value;
112 line >> value;
113 int step;
114 step = std::stoi(value);
115 Log::file() << "step " << step << "\n";
116 #endif
117
118 // Read and check the mesh dimensions
119 notEnd = getNextLine(inputfile_, line);
120 UTIL_CHECK(notEnd);
121 checkString(line, "mesh");
122 notEnd = getNextLine(inputfile_, line);
123 UTIL_CHECK(notEnd);
124 IntVec<D> fileMeshDimensions;
125 line >> fileMeshDimensions;
126 UTIL_CHECK(!line.fail());
127 UTIL_CHECK(fileMeshDimensions == meshDimensions_);
128
129 // Read a w-field configuration in r-grid format
130 int nMonomer = system().mixture().nMonomer();
131 FieldIo<D,T> const & fieldIo = system().domain().fieldIo();
132 fieldIo.readFieldsRGridData(inputfile_, wField_, nMonomer);
133
134 // Update system r-grid w fields
135 system().w().setRGrid(wField_);
136
137 return true;
138 }
139
140 /*
141 * Close trajectory file.
142 */
143 template <int D, class T>
145 { inputfile_.close(); }
146
147}
148}
149#endif
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
File input/output, format conversions and other utilities for fields.
void readFieldHeader(std::istream &in, int &nMonomer, UnitCell< D > &unitCell, bool &isSymmetric) const
Reader header of field file (fortran PSCF format).
Definition FieldIo.tpp:1418
virtual void readFieldsRGridData(std::istream &in, DArray< RField< D, T > > &fields, int nMonomer) const
Read data for array of r-grid fields, with no header section.
Definition FieldIo.tpp:352
bool readFrame() override
Read a single frame from the trajectory file.
RGridTrajectoryReader(System< D, T > &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.
Base class template for classes that represent a complete system.
TrajectoryReader(System< D, T > &system)
Constructor.
System< D, T > & system()
Return reference to parent system.
Base template for UnitCell<D> classes, D=1, 2 or 3.
Definition UnitCell.h:55
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:77
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.
Code for programs that simulate real-valued periodic fields.
Definition param_pc.dox:2
PSCF package top-level namespace.