PSCF v1.1
Mask.tpp
1#ifndef PSPC_MASK_TPP
2#define PSPC_MASK_TPP
3
4/*
5* PSCF - Polymer Self-Consistent Field Theory
6*
7* Copyright 2016 - 2022, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include "Mask.h"
12#include <pspc/field/FieldIo.h>
13#include <pspc/field/RFieldDft.h>
14
15namespace Pscf {
16namespace Pspc
17{
18
19 using namespace Util;
20
21 /*
22 * Constructor.
23 */
24 template <int D>
26 : basis_(),
27 rgrid_(),
28 fieldIoPtr_(0),
29 meshDimensions_(),
30 meshSize_(0),
31 nBasis_(0),
32 isAllocated_(false),
33 hasData_(false),
34 isSymmetric_(false)
35 {}
36
37 /*
38 * Destructor.
39 */
40 template <int D>
42 {}
43
44 /*
45 * Create an association with a FieldIo object.
46 */
47 template <int D>
48 void Mask<D>::setFieldIo(FieldIo<D> const & fieldIo)
49 { fieldIoPtr_ = &fieldIo; }
50
51 /*
52 * Allocate memory for field.
53 */
54 template <int D>
55 void Mask<D>::allocate(int nBasis, IntVec<D> const & meshDimensions)
56 {
57 UTIL_CHECK(!isAllocated_);
58
59 // Set mesh and basis dimensions
60 nBasis_ = nBasis;
61 meshDimensions_ = meshDimensions;
62 meshSize_ = 1;
63 for (int i = 0; i < D; ++i) {
64 UTIL_CHECK(meshDimensions[i] > 0);
65 meshSize_ *= meshDimensions[i];
66 }
67
68 // Allocate field arrays
69 basis_.allocate(nBasis);
70 rgrid_.allocate(meshDimensions);
71 isAllocated_ = true;
72 }
73
74 /*
75 * Set new w-field values.
76 */
77 template <int D>
79 {
80 UTIL_CHECK(field.capacity() == nBasis_);
81 for (int j = 0; j < nBasis_; ++j) {
82 basis_[j] = field[j];
83 }
84 fieldIoPtr_->convertBasisToRGrid(basis_, rgrid_);
85 hasData_ = true;
86 isSymmetric_ = true;
87 }
88
89 /*
90 * Set new field values, using r-grid field as inputs.
91 */
92 template <int D>
93 void Mask<D>::setRGrid(RField<D> const & field,
94 bool isSymmetric)
95 {
96 for (int j = 0; j < meshSize_; ++j) {
97 rgrid_[j] = field[j];
98 }
99 if (isSymmetric) {
100 fieldIoPtr_->convertRGridToBasis(rgrid_, basis_);
101 }
102 hasData_ = true;
103 isSymmetric_ = isSymmetric;
104 }
105
106 /*
107 * Read field from input stream, in symmetrized Fourier format.
108 *
109 * This function also computes and stores the corresponding
110 * r-grid representation. On return, hasData and isSymmetric
111 * are both true.
112 */
113 template <int D>
114 void Mask<D>::readBasis(std::istream& in, UnitCell<D>& unitCell)
115 {
116 fieldIoPtr_->readFieldBasis(in, basis_, unitCell);
117
118 // Update system wFieldsRGrid
119 fieldIoPtr_->convertBasisToRGrid(basis_, rgrid_);
120
121 hasData_ = true;
122 isSymmetric_ = true;
123 }
124
125 /*
126 * Read field from file, in symmetrized Fourier format.
127 *
128 * This function also computes and stores the corresponding
129 * r-grid representation. On return, hasData and isSymmetric
130 * are both true.
131 */
132 template <int D>
133 void Mask<D>::readBasis(std::string filename, UnitCell<D>& unitCell)
134 {
135 fieldIoPtr_->readFieldBasis(filename, basis_, unitCell);
136
137 // Update system wFieldsRGrid
138 fieldIoPtr_->convertBasisToRGrid(basis_, rgrid_);
139
140 hasData_ = true;
141 isSymmetric_ = true;
142 }
143
144 /*
145 * Reads field from an input stream in real-space (r-grid) format.
146 *
147 * If the isSymmetric parameter is true, this function assumes that
148 * the field is known to be symmetric and so computes and stores
149 * the corresponding basis format. If isSymmetric is false, it
150 * only sets the values in the r-grid format.
151 *
152 * On return, hasData is true and the persistent isSymmetric flag
153 * defined by the class is set to the value of the isSymmetric
154 * input parameter.
155 */
156 template <int D>
157 void Mask<D>::readRGrid(std::istream& in, UnitCell<D>& unitCell,
158 bool isSymmetric)
159 {
160 fieldIoPtr_->readFieldRGrid(in, rgrid_, unitCell);
161
162 if (isSymmetric) {
163 fieldIoPtr_->convertRGridToBasis(rgrid_, basis_);
164 }
165
166 hasData_ = true;
167 isSymmetric_ = isSymmetric;
168 }
169
170 /*
171 * Reads field from a file in real-space (r-grid) format.
172 *
173 * If the isSymmetric parameter is true, this function assumes that
174 * the field is known to be symmetric and so computes and stores
175 * the corresponding basis format. If isSymmetric is false, it
176 * only sets the values in the r-grid format.
177 *
178 * On return, hasData is true and the persistent isSymmetric flag
179 * defined by the class is set to the value of the isSymmetric
180 * input parameter.
181 */
182 template <int D>
183 void Mask<D>::readRGrid(std::string filename, UnitCell<D>& unitCell,
184 bool isSymmetric)
185 {
186 fieldIoPtr_->readFieldRGrid(filename, rgrid_, unitCell);
187
188 if (isSymmetric) {
189 fieldIoPtr_->convertRGridToBasis(rgrid_, basis_);
190 }
191
192 hasData_ = true;
193 isSymmetric_ = isSymmetric;
194 }
195
196 /*
197 * Return volume fraction of the unit cell occupied by the
198 * polymers/solvents.
199 */
200 template <int D>
201 double Mask<D>::phiTot() const
202 {
203 if (isSymmetric() && hasData()) {
204 // Data in basis format is available
205 return basis()[0];
206 } else if (!hasData()) {
207 // system does not have a mask
208 return 1.0;
209 } else { // Data is only available in r-grid format
210 RFieldDft<D> kgrid;
211 kgrid.allocate(rgrid_.meshDimensions());
212 fieldIoPtr_->convertRGridToKGrid(rgrid_, kgrid);
213 UTIL_CHECK(kgrid[0][1] < 1e-8);
214 return kgrid[0][0];
215 }
216 }
217
218} // namespace Pspc
219} // namespace Pscf
220#endif
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition: IntVec.h:27
File input/output operations and format conversions for fields.
void setFieldIo(FieldIo< D > const &fieldIo)
Create association with FieldIo (store pointer).
Definition: Mask.tpp:48
void setBasis(DArray< double > const &field)
Set field component values, in symmetrized Fourier format.
Definition: Mask.tpp:78
Mask()
Constructor.
Definition: Mask.tpp:25
void setRGrid(RField< D > const &field, bool isSymmetric=false)
Set field values in real-space (r-grid) format.
Definition: Mask.tpp:93
void readRGrid(std::istream &in, UnitCell< D > &unitCell, bool isSymmetric=false)
Reads field from an input stream in real-space (r-grid) format.
Definition: Mask.tpp:157
~Mask()
Destructor.
Definition: Mask.tpp:41
void readBasis(std::istream &in, UnitCell< D > &unitCell)
Read field from input stream in symmetrized Fourier format.
Definition: Mask.tpp:114
void allocate(int nBasis, IntVec< D > const &dimensions)
Allocate memory for the field.
Definition: Mask.tpp:55
double phiTot() const
Volume fraction of the unit cell occupied by the polymers/solvents.
Definition: Mask.tpp:201
Fourier transform of a real field on an FFT mesh.
Definition: RFieldDft.h:31
void allocate(IntVec< D > const &meshDimensions)
Allocate the underlying C array for an FFT grid.
Definition: RFieldDft.h:116
Field of real double precision values on an FFT mesh.
Definition: RField.h:29
Base template for UnitCell<D> classes, D=1, 2 or 3.
Definition: UnitCell.h:44
int capacity() const
Return allocated size.
Definition: Array.h:159
Dynamically allocatable contiguous array template.
Definition: DArray.h:32
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition: global.h:68
C++ namespace for polymer self-consistent field theory (PSCF).
Utility classes for scientific computation.
Definition: accumulators.mod:1