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