PSCF v1.5.0
fieldHeader.tpp
1#ifndef PSCF_FIELD_HEADER_TPP
2#define PSCF_FIELD_HEADER_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 <pscf/crystal/UnitCell.h>
12#include <pscf/crystal/unitCellHeader.h>
13
14#include <pscf/mesh/Mesh.h>
15#include <pscf/math/IntVec.h>
16#include <pscf/math/arithmetic.h>
17
18#include <util/misc/Log.h>
19#include <util/format/Int.h>
20#include <util/format/Dbl.h>
21#include <util/math/Constants.h>
22
23#include <string>
24
25namespace Pscf {
26
27 /*
28 * Read common part of field header (fortran PSCF format).
29 */
30 template <int D>
31 void readFieldHeader(std::istream& in,
32 int& ver1, int& ver2,
33 UnitCell<D>& cell,
34 std::string& groupName,
35 int& nMonomer)
36 {
37 std::string label;
38
39 // Read format v1 v2
40 in >> label;
41 UTIL_CHECK(label == "format");
42 in >> ver1;
43 in >> ver2;
44
45 // Read dimension of space
46 in >> label;
47 UTIL_CHECK(label == "dim");
48 int dim;
49 in >> dim;
50 UTIL_CHECK(dim == D);
51
52 // Read unit cell lattice type, number of parameters, parameters
53 // Function declared in UnitCell.h and defined in UnitCell.tpp
54 readUnitCellHeader(in, cell);
55
56 // Optionally read groupName
57 in >> label;
58 groupName = "";
59 if (label == "group_name") {
60 in >> groupName;
61 in >> label;
62 }
63
64 // Read nMonomer value
65 UTIL_CHECK(label == "N_monomer");
66 in >> nMonomer;
67 UTIL_CHECK(nMonomer > 0);
68 }
69
70 /*
71 * Write common part of field header (fortran PSCF format).
72 */
73 template <int D>
74 void writeFieldHeader(std::ostream &out,
75 int ver1, int ver2,
76 UnitCell<D> const & cell,
77 std::string const & groupName,
78 int nMonomer)
79 {
80 // Write file format id and dimension of space D
81 out << "format " << Int(ver1,3) << " " << Int(ver2,3) << std::endl;
82 out << "dim" << std::endl
83 << " " << D << std::endl;
84
85 // Write unit cell lattice type, number of parameters, parameters
86 // Function declared in UnitCell.h and defined in UnitCell.tpp
87 writeUnitCellHeader(out, cell);
88
89 // Write group_name if not empty
90 if (groupName != "") {
91 out << "group_name" << std::endl
92 << " " << groupName << std::endl;
93 }
94
95 // Write nMonomer, number of monomer types
96 if (nMonomer != 0) {
97 UTIL_CHECK(nMonomer > 0);
98 out << "N_monomer" << std::endl
99 << " " << nMonomer << std::endl;
100 }
101
102 // Note: The option to not write nMonomer when the value is zero
103 // is designed to allow this function to be used in contexts in
104 // which the value of N_monomer is irrelevant and possibly unknown,
105 // such as files that contain information about stars in a basis
106 // or group operations.
107
108 }
109
110 template <int D>
111 void readMeshDimensions(std::istream& in,
112 IntVec<D> const& meshDimensions)
113 {
114 // Read and check input stream mesh dimensions
115 std::string label;
116 in >> label;
117 UTIL_ASSERT(in.good());
118 if (label != "mesh" && label != "ngrid") {
119 std::string msg = "\n";
120 msg += "Error reading field file:\n";
121 msg += "Expected mesh or ngrid, but found [";
122 msg += label;
123 msg += "]";
124 UTIL_THROW(msg.c_str());
125 }
126 IntVec<D> meshDimensionsIn;
127 in >> meshDimensionsIn;
128 UTIL_CHECK(in.good());
129 if (meshDimensionsIn != meshDimensions) {
130 Log::file()
131 << "Inconsistent mesh dimensions:\n"
132 << "meshDimensions (expected) = " << meshDimensions << "\n"
133 << "meshDimensions (from file) = " << meshDimensionsIn << "\n";
134 UTIL_THROW("Unexpected mesh dimensions in field file header");
135 }
136 }
137
138 template <int D>
139 void writeMeshDimensions(std::ostream &out,
140 IntVec<D> const& meshDimensions)
141 {
142 out << "mesh " << std::endl
143 << " " << meshDimensions << std::endl;
144 }
145
146} // namespace Pscf
147#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:55
Int()
Default constructor.
Definition Int.cpp:19
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
#define UTIL_ASSERT(condition)
Assertion macro suitable for debugging serial or parallel code.
Definition global.h:75
void writeUnitCellHeader(std::ostream &out, UnitCell< D > const &cell)
Write UnitCell<D> to a field file header (fortran PSCF format).
void readUnitCellHeader(std::istream &in, UnitCell< D > &cell)
Read UnitCell<D> from a field file header (fortran PSCF format).
void readFieldHeader(std::istream &in, int &ver1, int &ver2, UnitCell< D > &cell, std::string &groupName, int &nMonomer)
Read common part of field header (fortran PSCF format).
void readMeshDimensions(std::istream &in, IntVec< D > const &meshDimensions)
Read mesh dimensions from a field file header.
void writeMeshDimensions(std::ostream &out, IntVec< D > const &meshDimensions)
Write mesh dimensions to a field file header.
void writeFieldHeader(std::ostream &out, int ver1, int ver2, UnitCell< D > const &cell, std::string const &groupName, int nMonomer)
Write common part of field header (fortran PSCF format).
PSCF package top-level namespace.