PSCF v1.4.0
cp/field/WFields.h
1#ifndef PRDC_CL_W_FIELDS_H
2#define PRDC_CL_W_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 Util {
16 template <typename T> class Signal;
17 template <> class Signal<void>;
18}
19namespace Pscf {
20 namespace Prdc {
21 template <int D> class UnitCell;
22 }
23}
24
25namespace Pscf {
26namespace Cp {
27
28 using namespace Util;
29 using namespace Prdc;
30
69 template <int D, class CFT, class FIT>
70 class WFields
71 {
72
73 public:
74
79
84
87
93 void setFieldIo(FIT const & fieldIo);
94
106
117 void setWriteUnitCell(UnitCell<D> const & cell);
118
127 void allocate(int nMonomer, IntVec<D> const & dimensions);
128
132
141
147 void readFields(std::istream& in);
148
154 void readFields(std::string const & filename);
155
159 void clear();
160
165
169
175 void writeFields(std::ostream& out) const;
176
182 void writeFields(std::string const & filename) const;
183
187
193 DArray< CFT > const & fields() const;
194
200 CFT const & field(int monomerId) const;
201
205
209 bool isAllocated() const;
210
217 bool hasData() const;
218
220
221 protected:
222
226 IntVec<D> const & meshDimensions() const;
227
231 int meshSize() const;
232
236 int nMonomer() const;
237
241 FIT const & fieldIo() const;
242
243 private:
244
245 /*
246 * Array of complex valued fields.
247 *
248 * Element fields_[i] is a CFT (complex field type) object that
249 * contains values of the field associated with monomer i on the
250 * nodes of a regular mesh.
251 */
252 DArray< CFT > fields_;
253
254 /*
255 * Integer vector of grid dimensions.
256 *
257 * Element i is the number of grid points along direction i.
258 */
259 IntVec<D> meshDimensions_;
260
261 /*
262 * Total number grid points (product of all mesh dimensions).
263 */
264 int meshSize_;
265
266 /*
267 * Number of monomer types (number of fields).
268 */
269 int nMonomer_;
270
271 /*
272 * Pointer to unit cell modified by the read functions.
273 */
274 UnitCell<D> * readUnitCellPtr_;
275
276 /*
277 * Pointer to unit cell written by the write functions.
278 */
279 UnitCell<D> const * writeUnitCellPtr_;
280
281 /*
282 * Pointer to an associated FIT (i.e., FieldIo<D>) object.
283 */
284 FIT const * fieldIoPtr_;
285
286 /*
287 * Pointer to a Signal that is triggered by field modification.
288 *
289 * The Signal is constructed and owned by this container.
290 */
291 Signal<void>* signalPtr_;
292
293 /*
294 * Has memory been allocated for the fields?
295 */
296 bool isAllocated_;
297
298 /*
299 * Has field data been set since it was last cleared?
300 */
301 bool hasData_;
302
303 /*
304 * Assign one complex field (CFT) to another: lhs = rhs.
305 *
306 * \param lhs left hand side of assignment
307 * \param rhs right hand side of assignment
308 */
309 virtual void assignField(CFT& lhs, CFT const & rhs) const;
310
311 };
312
313 // Inline member functions
314
315 // Mark field data stored in this object as invalid or outdated.
316 template <int D, class CFT, class FIT> inline
318 { hasData_ = false; }
319
320 // Get the array of all fields (const reference).
321 template <int D, class CFT, class FIT> inline
323 {
324 UTIL_ASSERT(isAllocated_);
325 return fields_;
326 }
327
328 // Get a single field (const reference).
329 template <int D, class CFT, class FIT> inline
330 CFT const & WFields<D,CFT,FIT>::field(int id) const
331 {
332 UTIL_ASSERT(isAllocated_);
333 return fields_[id];
334 }
335
336 // Has memory been allocated for fields ?
337 template <int D, class CFT, class FIT> inline
339 { return isAllocated_; }
340
341 // Has field data been initialized ?
342 template <int D, class CFT, class FIT> inline
344 { return hasData_; }
345
346 // Protected inline member functions
347
348 // Get mesh dimensions in each direction.
349 template <int D, class CFT, class FIT> inline
350 IntVec<D> const &
352 { return meshDimensions_; }
353
354 // Get mesh size (number of grid points).
355 template <int D, class CFT, class FIT> inline
357 { return meshSize_; }
358
359 // Get number of monomer types.
360 template <int D, class CFT, class FIT> inline
362 { return nMonomer_; }
363
364 // Get the associated FIT (FieldIo<D>) object (const reference).
365 template <int D, class CFT, class FIT> inline
366 FIT const & WFields<D,CFT,FIT>::fieldIo() const
367 {
368 UTIL_CHECK(fieldIoPtr_);
369 return *fieldIoPtr_;
370 }
371
372} // namespace Cp
373} // namespace Pscf
374#endif
void writeFields(std::ostream &out) const
Write fields to an output stream.
void setFields(DArray< CFT > const &fields)
Set values for all fields.
CFT const & field(int monomerId) const
Get the field for one monomer type.
void setWriteUnitCell(UnitCell< D > const &cell)
Set unit cell used when writing field files.
void readFields(std::istream &in)
Read all fields from an input file.
bool hasData() const
Has field data been set since it was last cleared?
int meshSize() const
Get mesh size (number of grid points), set on r-grid allocation.
void setFieldIo(FIT const &fieldIo)
Create association with FIT (store pointer).
void readFields(std::string const &filename)
Read all fields from a named file.
void clear()
Clear data stored in this object without deallocating.
IntVec< D > const & meshDimensions() const
Get mesh dimensions in each direction, set on r-grid allocation.
Signal< void > & signal()
Get the signal that notifies observers of w-field modification.
bool isAllocated() const
Has memory been allocated for fields ?
void writeFields(std::string const &filename) const
Write fields to a named file.
void allocate(int nMonomer, IntVec< D > const &dimensions)
Allocate memory for fields.
DArray< CField< D > > const & fields() const
void setReadUnitCell(UnitCell< D > &cell)
Set unit cell used when reading field files.
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
Notifier (or subject) in the Observer design pattern.
Definition Signal.h:39
#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.
Utility classes for scientific computation.