PSCF v1.3
rpg/fts/trajectory/RGridTrajectoryReader.tpp
1#ifndef RPG_RGRID_TRAJECTORY_READER_TPP
2#define RPG_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 <rpg/system/System.h>
13#include <pscf/math/IntVec.h>
14#include <pscf/mesh/MeshIterator.h>
15#include <util/misc/ioUtil.h>
16#include <iostream>
17#include <string>
18
19namespace Pscf {
20namespace Rpg
21{
22
23 using namespace Util;
24
25 /*
26 * Constructor.
27 */
28 template <int D>
31 systemPtr_(&system),
32 isAllocated_(false)
33 {}
34
35 template <int D>
37 {
38 const int nMonomer = system().mixture().nMonomer();
39 UTIL_CHECK(nMonomer > 0);
40 const IntVec<D> dimensions = system().domain().mesh().dimensions();
41 if (!isAllocated_){
42 wField_.allocate(nMonomer);
43 for (int i = 0; i < nMonomer; ++i) {
44 wField_[i].allocate(dimensions);
45 }
46 isAllocated_ = true;
47 }
48 }
49
50 /*
51 * Open file and setup memory.
52 */
53 template <int D>
54 void RGridTrajectoryReader<D>::open(std::string filename)
55 {
56 system().fileMaster().open(filename, inputfile_);
57 allocate();
58 }
59
60 template <int D>
62 {
63 // Read Header
64 int nMonomer = system().mixture().nMonomer();
65 FieldIo<D> const & fieldIo = system().domain().fieldIo();
66 UnitCell<D> tmpUnitCell;
67 bool hasSymmetry;
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>
79 {
80 // Preconditions
81 if (!isAllocated_) {
82 UTIL_THROW("Real Grid Field is not allocated");
83 }
84
85 bool notEnd;
86 std::stringstream line;
87
88 // Attempt to read first line
89 notEnd = getNextLine(inputfile_, line);
90 if (!notEnd) {
91 return false;
92 }
93
94 // Process ITEM: TIMESTEP
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 ITEM: NUMBER OF Mesh
106 notEnd = getNextLine(inputfile_, line);
107 UTIL_CHECK(notEnd);
108 checkString(line, "mesh");
109 notEnd = getNextLine(inputfile_, line);
110 UTIL_CHECK(notEnd);
111
112 // Read a single real-space grid field frame from trajectory file
113 int nMonomer = system().mixture().nMonomer();
114 FieldIo<D> const & fieldIo = system().domain().fieldIo();
115 fieldIo.readFieldsRGridData(inputfile_, wField_, nMonomer);
116
117 // Update system real-space grid field
118 system().w().setRGrid(wField_);
119
120 return true;
121 }
122
123 /*
124 * Close trajectory file.
125 */
126 template <int D>
128 { inputfile_.close();}
129
130}
131}
132#endif
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
Base template for UnitCell<D> classes, D=1, 2 or 3.
Definition UnitCell.h:56
File input/output operations and format conversions for fields.
void readFieldHeader(std::istream &in, int &nMonomer, UnitCell< D > &unitCell, bool &isSymmetric) const
Reader header of field file (fortran PSCF format)
void readFieldsRGridData(std::istream &in, DArray< RField< D > > &fields, int nMonomer) const override
Read data for an array of r-grid fields, with no header section.
void open(std::string filename)
Open trajectory file and read header, if any.
void allocate()
Allocate memory required by trajectory reader.
System< D > & system()
Return reference to parent system.
Main class, representing one complete system.
TrajectoryReader(System< D > &system)
Constructor.
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.
SCFT and PS-FTS with real periodic fields (GPU)
PSCF package top-level namespace.
Definition param_pc.dox:1