PSCF v1.3.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 <rpg/solvers/Mixture.h>
14#include <rpg/field/Domain.h>
15#include <pscf/math/IntVec.h>
16#include <pscf/mesh/MeshIterator.h>
17#include <util/misc/FileMaster.h>
18#include <util/misc/ioUtil.h>
19#include <iostream>
20#include <string>
21
22namespace Pscf {
23namespace Rpg
24{
25
26 using namespace Util;
27
28 /*
29 * Constructor.
30 */
31 template <int D>
34 systemPtr_(&system),
35 isAllocated_(false)
36 {}
37
38 template <int D>
40 {
41 const int nMonomer = system().mixture().nMonomer();
42 UTIL_CHECK(nMonomer > 0);
43 const IntVec<D> dimensions = system().domain().mesh().dimensions();
44 if (!isAllocated_){
45 wField_.allocate(nMonomer);
46 for (int i = 0; i < nMonomer; ++i) {
47 wField_[i].allocate(dimensions);
48 }
49 isAllocated_ = true;
50 }
51 }
52
53 /*
54 * Open file and setup memory.
55 */
56 template <int D>
57 void RGridTrajectoryReader<D>::open(std::string filename)
58 {
59 system().fileMaster().open(filename, inputfile_);
60 allocate();
61 }
62
63 template <int D>
65 {
66 // Read Header
67 int nMonomer = system().mixture().nMonomer();
68 FieldIo<D> const & fieldIo = system().domain().fieldIo();
69 UnitCell<D> tmpUnitCell;
70 bool hasSymmetry;
71 fieldIo.readFieldHeader(inputfile_, nMonomer, tmpUnitCell,
73 system().setUnitCell(tmpUnitCell);
74 Log::file() << "Read Header" << "\n";
75 }
76
77 /*
78 * Read frame, return false if end-of-file
79 */
80 template <int D>
82 {
83 // Preconditions
84 if (!isAllocated_) {
85 UTIL_THROW("Real Grid Field is not allocated");
86 }
87
88 bool notEnd;
89 std::stringstream line;
90
91 // Attempt to read first line
92 notEnd = getNextLine(inputfile_, line);
93 if (!notEnd) {
94 return false;
95 }
96
97 // Process ITEM: TIMESTEP
98 checkString(line, "i");
99 checkString(line, "=");
100 #if 0
101 std::string value;
102 line >> value;
103 int step;
104 step = std::stoi(value);
105 Log::file() << "step "<< step <<"\n";
106 #endif
107
108 // Read ITEM: NUMBER OF Mesh
109 notEnd = getNextLine(inputfile_, line);
110 UTIL_CHECK(notEnd);
111 checkString(line, "mesh");
112 notEnd = getNextLine(inputfile_, line);
113 UTIL_CHECK(notEnd);
114
115 // Read a single real-space grid field frame from trajectory file
116 int nMonomer = system().mixture().nMonomer();
117 FieldIo<D> const & fieldIo = system().domain().fieldIo();
118 fieldIo.readFieldsRGridData(inputfile_, wField_, nMonomer);
119
120 // Update system real-space grid field
121 system().w().setRGrid(wField_);
122
123 return true;
124 }
125
126 /*
127 * Close trajectory file.
128 */
129 template <int D>
131 { inputfile_.close();}
132
133}
134}
135#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 a complete physical 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.