PSCF v1.4.0
rp/field/CFields.tpp
1#ifndef RP_C_FIELDS_TPP
2#define RP_C_FIELDS_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 "CFields.h"
12#include <prdc/crystal/UnitCell.h>
13#include <util/misc/FileMaster.h>
14
15namespace Pscf {
16namespace Rp {
17
18 using namespace Util;
19
20 /*
21 * Constructor.
22 */
23 template <int D, class RFT, class FIT>
25 : basis_(),
26 rgrid_(),
27 nMonomer_(0),
28 writeUnitCellPtr_(nullptr),
29 fieldIoPtr_(nullptr),
30 isAllocatedRGrid_(false),
31 isAllocatedBasis_(false),
32 hasData_(false),
33 isSymmetric_(false)
34 {}
35
36 /*
37 * Create an association with a FIT object.
38 */
39 template <int D, class RFT, class FIT>
40 void
42 { fieldIoPtr_ = &fieldIo; }
43
44 /*
45 * Set the unit cell used for parameters written to a field header.
46 */
47 template <int D, class RFT, class FIT>
49 {
50 UTIL_CHECK(!writeUnitCellPtr_);
51 writeUnitCellPtr_ = &cell;
52 }
53
54 /*
55 * Set the stored value of nMonomer (this may only be called once).
56 */
57 template <int D, class RFT, class FIT>
59 {
60 UTIL_CHECK(nMonomer_ == 0);
61 UTIL_CHECK(nMonomer > 0);
62 nMonomer_ = nMonomer;
63 }
64
65 /*
66 * Allocate memory for fields in r-grid format.
67 */
68 template <int D, class RFT, class FIT>
69 void
71 {
72 UTIL_CHECK(nMonomer_ > 0);
73 UTIL_CHECK(!isAllocatedRGrid_);
74
75 // Allocate arrays
76 rgrid_.allocate(nMonomer_);
77 for (int i = 0; i < nMonomer_; ++i) {
78 rgrid_[i].allocate(dimensions);
79 }
80 isAllocatedRGrid_ = true;
81 }
83 /*
84 * Allocate memory for fields in basis format.
85 */
86 template <int D, class RFT, class FIT>
88 {
89 UTIL_CHECK(nMonomer_ > 0);
90 UTIL_CHECK(!isAllocatedBasis_);
91
92 // Allocate
93 basis_.allocate(nMonomer_);
94 for (int i = 0; i < nMonomer_; ++i) {
95 basis_[i].allocate(nBasis);
96 }
97 isAllocatedBasis_ = true;
98 }
99
100 /*
101 * Allocate memory for all fields.
102 */
103 template <int D, class RFT, class FIT>
104 void
105 CFields<D,RFT,FIT>::allocate(int nMonomer, int nBasis,
106 IntVec<D> const & dimensions)
107 {
108 setNMonomer(nMonomer);
109 allocateRGrid(dimensions);
110 allocateBasis(nBasis);
111 }
112
113 // Field output to file
114
115 /*
116 * Write fields to an output stream in basis format.
117 */
118 template <int D, class RFT, class FIT>
119 void CFields<D,RFT,FIT>::writeBasis(std::ostream& out) const
120 {
121 // Preconditions
122 UTIL_CHECK(nMonomer_ > 0);
123 UTIL_CHECK(fieldIoPtr_);
124 UTIL_CHECK(writeUnitCellPtr_);
125 UTIL_CHECK(isAllocatedBasis_);
126 UTIL_CHECK(hasData_);
127 UTIL_CHECK(isSymmetric_);
128
129 fieldIo().writeFieldsBasis(out, basis_, *writeUnitCellPtr_);
130 }
131
132 /*
133 * Write fields to a file in basis format, by filename.
134 */
135 template <int D, class RFT, class FIT>
136 void CFields<D,RFT,FIT>::writeBasis(std::string filename) const
137 {
138 std::ofstream file;
139 fieldIo().fileMaster().openOutputFile(filename, file);
140 writeBasis(file);
141 file.close();
142 }
143
144 /*
145 * Write fields to an output stream in real-space (r-grid) format.
146 */
147 template <int D, class RFT, class FIT>
148 void CFields<D,RFT,FIT>::writeRGrid(std::ostream& out) const
149 {
150 // Preconditions
151 UTIL_CHECK(nMonomer_ > 0);
152 UTIL_CHECK(writeUnitCellPtr_);
153 UTIL_CHECK(fieldIoPtr_);
154 UTIL_CHECK(isAllocatedRGrid_);
155 UTIL_CHECK(hasData_);
156
157 bool writeHeader = true;
158
159 fieldIo().writeFieldsRGrid(out, rgrid_, *writeUnitCellPtr_, writeHeader,
160 isSymmetric_);
161 }
162
163 /*
164 * Write fields to a file in r-grid format, by filename.
165 */
166 template <int D, class RFT, class FIT>
167 void CFields<D,RFT,FIT>::writeRGrid(std::string filename) const
168 {
169 std::ofstream file;
170 fieldIo().fileMaster().openOutputFile(filename, file);
171 writeRGrid(file);
172 file.close();
173 }
174
175 // Boolean flag setter functions
176
177 // Set the hasData flag.
178 template <int D, class RFT, class FIT> inline
180 {
181 hasData_ = hasData;
182 if (!hasData_) {
183 isSymmetric_ = false;
184 }
185 }
186
187 // Set the isSymmetric flag.
188 template <int D, class RFT, class FIT> inline
190 {
191 UTIL_CHECK(hasData_);
192 isSymmetric_ = isSymmetric;
193 }
194
195} // namespace Rp
196} // namespace Pscf
197#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
void writeBasis(std::ostream &out) const
Write fields to an input stream in symmetrized basis format.
bool hasData() const
Does this container have up-to-date fields?
void setFieldIo(FIT const &fieldIo)
Create association with FIT (store pointer).
void writeRGrid(std::ostream &out) const
Writes fields to an input stream in real-space (r-grid) format.
void setNMonomer(int nMonomer)
Set stored value of nMonomer.
void allocate(int nMonomer, int nBasis, IntVec< D > const &dimensions)
Allocate memory for both r-grid and basis field formats.
void allocateRGrid(IntVec< D > const &dimensions)
Allocate memory for fields in rgrid format.
bool isSymmetric() const
Are the fields invariant under elements of the space group?
FIT const & fieldIo() const
Get associated FieldIo object (const reference).
void setHasData(bool hasData)
Set the hasData flag.
void setIsSymmetric(bool isSymmetric)
Set the isSymmetric flag.
void allocateBasis(int nBasis)
Allocate or re-allocate memory for fields in basis format.
void setWriteUnitCell(UnitCell< D > const &cell)
Set unit cell used when writing field files.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
Class templates for real-valued periodic fields.
PSCF package top-level namespace.