PSCF v1.4.0
cp/field/CFields.h
1#ifndef PRDC_CL_C_FIELDS_H
2#define PRDC_CL_C_FIELDS_H
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 <util/containers/DArray.h> // member template
12#include <pscf/math/IntVec.h> // member
13
14// Forward declarations
15namespace Pscf {
16 namespace Prdc {
17 template <int D> class UnitCell;
18 }
19}
20
21namespace Pscf {
22namespace Cp {
23
24 using namespace Util;
25 using namespace Prdc;
26
56 template <int D, class CFT, class FIT>
57 class CFields
58 {
59
60 public:
61
66
71
74
80 void setFieldIo(FIT const & fieldIo);
81
92 void setWriteUnitCell(UnitCell<D> const & cell);
93
102 void allocate(int nMonomer, IntVec<D> const & dimensions);
103
107
113 void writeFields(std::ostream& out) const;
114
120 void writeFields(std::string const & filename) const;
121
125
132
138 DArray< CFT > const & fields() const;
139
145 CFT & field(int monomerId);
146
152 CFT const & field(int monomerId) const;
153
157
161 bool isAllocated() const;
162
166 bool hasData() const;
167
175 void setHasData(bool hasData);
176
178
179 protected:
180
184 IntVec<D> const & meshDimensions() const;
185
189 int meshSize() const;
190
194 int nMonomer() const;
195
199 FIT const & fieldIo() const;
200
201 private:
202
203 /*
204 * Array of complex valued fields.
205 *
206 * Element fields_[i] is a CFT (complex field type) object that
207 * contains values of the field associated with monomer i on the
208 * nodes of a regular mesh.
209 */
210 DArray< CFT > fields_;
211
212 /*
213 * Integer vector of grid dimensions.
214 *
215 * Element i is the number of grid points along direction i.
216 */
217 IntVec<D> meshDimensions_;
218
219 /*
220 * Total number grid points (product of all mesh dimensions).
221 */
222 int meshSize_;
223
224 /*
225 * Number of monomer types (number of fields).
226 */
227 int nMonomer_;
228
229 /*
230 * Pointer to unit cell written by the write functions.
231 */
232 UnitCell<D> const * writeUnitCellPtr_;
233
234 /*
235 * Pointer to an associated FIT (field IO) object.
236 */
237 FIT const * fieldIoPtr_;
238
239 /*
240 * Has memory been allocated for the fields?
241 */
242 bool isAllocated_;
243
244 /*
245 * Does this container hold up-to-date field data?
246 */
247 bool hasData_;
248
249 };
250
251 // Inline member functions
252
253 // Get the array of all fields (const reference)
254 template <int D, class CFT, class FIT> inline
256 {
257 UTIL_ASSERT(isAllocated_);
258 return fields_;
259 }
260
261 // Get the array of all fields (non-const reference)
262 template <int D, class CFT, class FIT> inline
264 {
265 UTIL_ASSERT(isAllocated_);
266 return fields_;
267 }
268
269 // Get a single field (const reference)
270 template <int D, class CFT, class FIT> inline
271 CFT const & CFields<D,CFT,FIT>::field(int id) const
272 {
273 UTIL_ASSERT(isAllocated_);
274 return fields_[id];
275 }
276
277 // Get a single field (non-const reference)
278 template <int D, class CFT, class FIT> inline
280 {
281 UTIL_ASSERT(isAllocated_);
282 return fields_[id];
283 }
284
285 // Has memory been allocated for fields ?
286 template <int D, class CFT, class FIT> inline
288 { return isAllocated_; }
289
290 // Is data up to date ?
291 template <int D, class CFT, class FIT> inline
293 { return hasData_; }
294
295 // Protected inline member functions
296
297 // Get mesh dimensions in each direction.
298 template <int D, class CFT, class FIT> inline
299 IntVec<D> const &
301 { return meshDimensions_; }
302
303 // Get mesh size (number of grid points).
304 template <int D, class CFT, class FIT> inline
306 { return meshSize_; }
307
308 // Get number of monomer types.
309 template <int D, class CFT, class FIT> inline
311 { return nMonomer_; }
312
313 // Get the associated the field Io (FIT) object (const reference).
314 template <int D, class CFT, class FIT> inline
315 FIT const & CFields<D,CFT,FIT>::fieldIo() const
316 {
317 UTIL_CHECK(fieldIoPtr_);
318 return *fieldIoPtr_;
319 }
320
321} // namespace Cp
322} // namespace Pscf
323#endif
void setFieldIo(FIT const &fieldIo)
Create association with FIT (store pointer).
int meshSize() const
Get mesh size (number of grid points), set on r-grid allocation.
bool isAllocated() const
Has memory been allocated for fields ?
IntVec< D > const & meshDimensions() const
Get mesh dimensions in each direction, set on r-grid allocation.
bool hasData() const
Does this container have up-to-date field data ?
void writeFields(std::string const &filename) const
Write fields to a named file.
CFT & field(int monomerId)
Get the field for one monomer type (non-const reference).
void setHasData(bool hasData)
Set the hasData boolean flag.
DArray< CFT > const & fields() const
Get the array of all fields (const reference).
CFT const & field(int monomerId) const
Get the field for one monomer type (const reference).
void allocate(int nMonomer, IntVec< D > const &dimensions)
Allocate memory for fields.
void setWriteUnitCell(UnitCell< D > const &cell)
Set unit cell used when writing field files.
DArray< CFT > & fields()
Get the array of all fields (non-const reference).
void writeFields(std::ostream &out) const
Write fields to an output stream.
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
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
#define UTIL_ASSERT(condition)
Assertion macro suitable for debugging serial or parallel code.
Definition global.h:75
Complex-valued periodic fields (class templates).
Definition cp.mod:6
Periodic fields and crystallography.
Definition complex.cpp:11
PSCF package top-level namespace.