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