PSCF v1.3.3
rpc/fts/trajectory/RGridTrajectoryReader.tpp
1#ifndef RPC_RGRID_TRAJECTORY_READER_TPP
2#define RPC_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
13#include <rpc/system/System.h>
14#include <rpc/solvers/Mixture.h>
15#include <rpc/field/Domain.h>
16#include <pscf/mesh/MeshIterator.h>
17#include <util/misc/ioUtil.h>
18#include <util/misc/FileMaster.h>
19
20#include <sstream>
21#include <iostream>
22#include <string>
23
24namespace Pscf {
25namespace Rpc {
26
27 using namespace Util;
28
29 /*
30 * Constructor.
31 */
32 template <int D>
37
38 template <int D>
40 {
41 const int nMonomer = system().mixture().nMonomer();
42 UTIL_CHECK(nMonomer > 0);
43
44 meshDimensions_ = system().domain().mesh().dimensions();
45 if (!isAllocated_){
46 wField_.allocate(nMonomer);
47 for (int i = 0; i < nMonomer; ++i) {
48 wField_[i].allocate(meshDimensions_);
49 }
50 isAllocated_ = true;
51 }
52 }
53
54 /*
55 * Open file and setup memory.
56 */
57 template <int D>
58 void RGridTrajectoryReader<D>::open(std::string filename)
59 {
60 system().fileMaster().open(filename, inputfile_);
61 allocate();
62 }
63
64 template <int D>
66 {
67 // Read Header
68 int nMonomer = system().mixture().nMonomer();
69 FieldIo<D> const & fieldIo = system().domain().fieldIo();
70 UnitCell<D> tmpUnitCell;
71 bool hasSymmetry;
72 fieldIo.readFieldHeader(inputfile_, nMonomer, tmpUnitCell,
74 system().setUnitCell(tmpUnitCell);
75 Log::file() << "Read Header" << "\n";
76 }
77
78 /*
79 * Read frame, return false if end-of-file
80 */
81 template <int D>
83 {
84 // Preconditions
85 if (!isAllocated_) {
86 UTIL_THROW("R-grid Field is not allocated");
87 }
88
89 bool notEnd;
90 std::stringstream line;
91
92 // Attempt to read first line, check for end of file
93 notEnd = getNextLine(inputfile_, line);
94 if (!notEnd) {
95 return false;
96 }
97
98 // Read line containing time step
99 checkString(line, "i");
100 checkString(line, "=");
101 #if 0
102 std::string value;
103 line >> value;
104 int step;
105 step = std::stoi(value);
106 Log::file()<< "step "<< step <<"\n";
107 #endif
108
109 // Read mesh dimensions
110 notEnd = getNextLine(inputfile_, line);
111 UTIL_CHECK(notEnd);
112 checkString(line, "mesh");
113
114 // Read empty line
115 notEnd = getNextLine(inputfile_, line);
116 UTIL_CHECK(notEnd);
117
118 // Read w-field configuration in r-grid format
119 int nMonomer = system().mixture().nMonomer();
120 FieldIo<D> const & fieldIo = system().domain().fieldIo();
121 fieldIo.readFieldsRGridData(inputfile_, wField_, nMonomer);
122
123 // Update system r-grid field
124 system().w().setRGrid(wField_);
125
126 return true;
127 }
128
129 /*
130 * Close trajectory file.
131 */
132 template <int D>
134 { inputfile_.close();}
135
136}
137}
138#endif
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.
Main class, representing a complete physical system.
TrajectoryReader(System< D > &system)
Constructor.
System< D > & system()
Return reference to parent system.
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.
Real periodic fields, SCFT and PS-FTS (CPU).
Definition param_pc.dox:2
PSCF package top-level namespace.