PSCF v1.4.0
cp/field/CFields.tpp
1#ifndef PRDC_CL_C_FIELDS_TPP
2#define PRDC_CL_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/field/cFieldIo.h>
13#include <prdc/crystal/UnitCell.h>
14#include <pscf/mesh/Mesh.h>
15#include <util/misc/FileMaster.h>
16
17namespace Pscf {
18namespace Cp {
19
20 using namespace Util;
21 using namespace Prdc;
22
23 // Public member functions
24
25 /*
26 * Constructor.
27 */
28 template <int D, class CFT, class FIT>
30 : fields_(),
31 meshDimensions_(),
32 meshSize_(0),
33 nMonomer_(0),
34 writeUnitCellPtr_(nullptr),
35 fieldIoPtr_(nullptr),
36 isAllocated_(false),
37 hasData_(false)
38 {}
39
40 /*
41 * Destructor.
42 */
43 template <int D, class CFT, class FIT>
46
47 /*
48 * Create an association with a field Io (FIT) object.
49 */
50 template <int D, class CFT, class FIT>
52 { fieldIoPtr_ = &fieldIo; }
53
54 /*
55 * Set the unit cell that is written to a field file header.
56 */
57 template <int D, class CFT, class FIT>
59 {
60 UTIL_CHECK(!writeUnitCellPtr_);
61 writeUnitCellPtr_ = &cell;
62 }
63
64 /*
65 * Allocate memory for fields.
66 */
67 template <int D, class CFT, class FIT>
70 {
71 UTIL_CHECK(nMonomer_ == 0);
72 UTIL_CHECK(!hasData_);
73 UTIL_CHECK(!isAllocated_);
75
76 // Store nMonomer and mesh dimensions
77 nMonomer_ = nMonomer;
78 meshDimensions_ = meshDimensions;
79 meshSize_ = 1;
80 for (int i = 0; i < D; ++i) {
82 meshSize_ *= meshDimensions[i];
83 }
84
85 // Allocate arrays
86 fields_.allocate(nMonomer_);
87 for (int i = 0; i < nMonomer_; ++i) {
88 fields_[i].allocate(meshDimensions);
89 }
90
91 isAllocated_ = true;
92 }
93
94 // Write fields to file
95
96 /*
97 * Write fields to an output stream.
98 */
99 template <int D, class CFT, class FIT>
100 void CFields<D,CFT,FIT>::writeFields(std::ostream& out) const
101 {
102 // Preconditions
103 UTIL_CHECK(nMonomer_ > 0);
104 UTIL_CHECK(writeUnitCellPtr_);
105 UTIL_CHECK(fieldIoPtr_);
106 UTIL_CHECK(isAllocated_);
107 UTIL_CHECK(hasData_);
108
109 bool writeHeader = true;
110
111 fieldIo().writeFields(out, fields_, *writeUnitCellPtr_,
112 writeHeader);
113 }
114
115 /*
116 * Write fields to a named file.
117 */
118 template <int D, class CFT, class FIT>
119 void CFields<D,CFT,FIT>::writeFields(std::string const & filename) const
120 {
121 std::ofstream file;
122 fieldIo().fileMaster().openOutputFile(filename, file);
123 writeFields(file);
124 file.close();
125 }
126
127 // Boolean flag setter
128
129 /*
130 * Set the hasData flag.
131 */
132 template <int D, class CFT, class FIT>
134 { hasData_ = hasData; }
135
136} // namespace Cp
137} // namespace Pscf
138#endif
void setFieldIo(FIT const &fieldIo)
Create association with FIT (store pointer).
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 setHasData(bool hasData)
Set the hasData boolean flag.
FIT const & fieldIo() const
Get associated FIT field IO object (const reference).
void allocate(int nMonomer, IntVec< D > const &dimensions)
Allocate memory for fields.
int nMonomer() const
Get number of monomer types.
void setWriteUnitCell(UnitCell< D > const &cell)
Set unit cell used when writing field files.
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
Complex-valued periodic fields (class templates).
Definition cp.mod:6
Periodic fields and crystallography.
Definition complex.cpp:11
PSCF package top-level namespace.