PSCF v1.5.0
cpp/WaveList.tpp
1#ifndef PSCF_WAVE_LIST_CP_TPP
2#define PSCF_WAVE_LIST_CP_TPP
3
4/*
5* PSCF - Polymer Self-Consistent Field
6*
7* Copyright 2015 - 2026, David Morse (morse012@um.edu) and co-authors
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include "WaveList.h"
12
13#include <pscf/field/cpp/FFT.h>
14#include <pscf/crystal/UnitCell.h>
15#include <pscf/crystal/shiftToMinimum.h>
16#include <pscf/crystal/hasVariableAngle.h>
17
18#include <pscf/mesh/MeshIterator.h>
19#include <pscf/mesh/Mesh.h>
20#include <pscf/math/Sort.h>
21
22namespace Pscf {
23
24 /*
25 * Constructor.
26 */
27 template <int D>
29 : kSize_(0),
30 nBunch_(0),
31 isAllocated_(false),
32 hasMinImages_(false),
33 hasKSq_(false),
34 hasdKSq_(false),
35 isSorted_(false),
36 isRealField_(isRealField),
37 unitCellPtr_(nullptr),
38 meshPtr_(nullptr)
39 {}
40
41 /*
42 * Destructor.
43 */
44 template <int D>
47
48 /*
49 * Allocate memory used by WaveList.
50 */
51 template <int D>
52 void WaveList<D,CPT>::allocate(Mesh<D> const & m, UnitCell<D> const & c)
53 {
54 UTIL_CHECK(m.size() > 0);
55 UTIL_CHECK(c.nParameter() > 0);
56 UTIL_CHECK(!isAllocated_);
57
58 // Create permanent associations with mesh and unit cell
59 unitCellPtr_ = &c;
60 meshPtr_ = &m;
61
62 // Local copies of properties
63 int nParams = unitCell().nParameter();
64 IntVec<D> const & meshDimensions = mesh().dimensions();
65
66 // Compute kMeshDimensions_ and kSize_
67 if (isRealField_) {
68 FFT<D,CPT>::computeKMesh(meshDimensions, kMeshDimensions_, kSize_);
69 } else {
70 kMeshDimensions_ = meshDimensions;
71 kSize_ = mesh().size();
72 }
73
74 // Allocate memory
75 minImages_.allocate(kSize_);
76 kSq_.allocate(kMeshDimensions_);
77 dKSq_.allocate(nParams);
78 for (int i = 0; i < nParams; i++) {
79 dKSq_[i].allocate(kMeshDimensions_);
80 }
81
82 // Allocate and set up implicitInverse_ array if isRealField_ == true
83 // (only depends on mesh dimensions, only used for real fields)
84 if (isRealField_) {
85 implicitInverse_.allocate(kSize_);
86
87 MeshIterator<D> kItr(kMeshDimensions_);
88 int rank;
89
90 for (kItr.begin(); !kItr.atEnd(); ++kItr) {
91 rank = kItr.rank();
92 implicitInverse_[rank] =
93 FFT<D,CPT>::hasImplicitInverse(kItr.position(), meshDimensions);
94 }
95 }
96
98 isAllocated_ = true;
99 }
100
101 /*
102 * Clear all data that depends on unit cell parameters.
103 */
104 template <int D>
106 {
107 hasKSq_ = false;
108 hasdKSq_ = false;
109 if (unitCell().nParameter() > 1) {
110 isSorted_ = false;
111 sortedBunches_.clear();
112 nBunch_ = 0;
113 }
114 if (hasVariableAngle<D>(unitCell().lattice())) {
115 hasMinImages_ = false;
116 }
117 }
118
119 /*
120 * Compute minimum image vectors for all wavevectors.
121 */
122 template <int D>
124 {
125 if (hasMinImages_) return; // min images already calculated
126
127 // Precondition
128 UTIL_CHECK(isAllocated_);
129 UTIL_CHECK(unitCell().lattice() != UnitCell<D>::Null);
130 UTIL_CHECK(unitCell().isInitialized());
131 UTIL_CHECK(minImages_.capacity() == kSize_);
132
133 MeshIterator<D> kItr(kMeshDimensions_);
134 int rank;
135 for (kItr.begin(); !kItr.atEnd(); ++kItr) {
136 rank = kItr.rank();
137 minImages_[rank] = shiftToMinimum(kItr.position(),
138 mesh().dimensions(), unitCell());
139 kSq_[rank] = unitCell().ksq(minImages_[rank]);
140 }
141
142 hasMinImages_ = true;
143 hasKSq_ = true;
144 }
145
146 /*
147 * Compute array of value of |k|^2
148 */
149 template <int D>
151 {
152 // If kSq_ is valid, return immediately without recomputing
153 if (hasKSq_) return;
154
155 // If necessary, compute minimum images.
156 if (!hasMinImages_) {
157 computeMinimumImages(); // computes both min images and kSq
158 return;
159 }
160
161 // Preconditions
162 UTIL_CHECK(isAllocated_);
163 UTIL_CHECK(unitCell().nParameter() > 0);
164 UTIL_CHECK(unitCell().lattice() != UnitCell<D>::Null);
165 UTIL_CHECK(unitCell().isInitialized());
166
167 // Compute kSq_
168 MeshIterator<D> kItr(kMeshDimensions_);
169 int rank;
170 for (kItr.begin(); !kItr.atEnd(); ++kItr) {
171 rank = kItr.rank();
172 kSq_[rank] = unitCell().ksq(minImages_[rank]);
173 }
174
175 hasKSq_ = true;
176 }
177
178 /*
179 * Compute derivatives of |k|^2 w/ respect to unit cell parameters.
180 */
181 template <int D>
183 {
184 if (hasdKSq_) return; // dKSq already calculated
185
186 // Compute minimum images if needed
187 if (!hasMinImages_) {
189 }
190
191 // Preconditions
192 UTIL_CHECK(isAllocated_);
193 UTIL_CHECK(unitCell().nParameter() > 0);
194 UTIL_CHECK(unitCell().lattice() != UnitCell<D>::Null);
195 UTIL_CHECK(unitCell().isInitialized());
196
197 MeshIterator<D> kItr(kMeshDimensions_);
198 int i, rank;
199 for (i = 0 ; i < unitCell().nParameter(); ++i) {
200 RField<D,CPT>& dksq = dKSq_[i];
201 for (kItr.begin(); !kItr.atEnd(); ++kItr) {
202 rank = kItr.rank();
203 dksq[rank] = unitCell().dksq(minImages_[rank], i);
204 if (isRealField_) {
205 if (implicitInverse_[rank]) {
206 dksq[rank] *= 2.0;
207 }
208 }
209 }
210 }
211
212 hasdKSq_ = true;
213 }
214
215 /*
216 * Sort waves by magnitude.
217 */
218 template <int D>
220 {
221 if (isSorted_) return;
222
223 // Compute wavenumbers if needed
224 UTIL_CHECK(isAllocated_);
225 if (!hasKSq_) {
226 computeKSq();
227 }
228
229 // Construct sorted array of items
230 std::vector< Sort::Item<double> > items;
232 for (int i = 0; i < kSize_; ++i) {
233 item.value = kSq_[i];
234 item.id = i;
235 items.push_back(item);
236 }
237 UTIL_CHECK((int)items.size() == kSize_);
238 Sort::sort(items);
239
240 // Fill sortedIds_ array with ids
241 if (!sortedIds_.isAllocated()) {
242 sortedIds_.allocate(kSize_);
243 }
244 UTIL_CHECK(sortedIds_.capacity() == kSize_);
245 for (int i = 0; i < kSize_; ++i) {
246 sortedIds_[i] = items[i].id;
247 }
248
249 // Construct sortedBunches_ array and set nBunch_
250 double epsilon = 1.0E-8;
251 sortedBunches_.clear();
252 Sort::findBunches(items, sortedBunches_, epsilon);
253 nBunch_ = sortedBunches_.size();
254
255 // Fill bunchIds_ array
256 UTIL_CHECK(nBunch_ > 0);
257 if (!bunchIds_.isAllocated()) {
258 bunchIds_.allocate(kSize_);
259 }
260 UTIL_CHECK(bunchIds_.capacity() == kSize_);
261 int begin = 0;
262 int end = 0;
263 int ib, iw;
264 for (ib = 0; ib < nBunch_; ++ib) {
265 begin = sortedBunches_[ib][0];
266 end = sortedBunches_[ib][1];
267 UTIL_CHECK(end > begin);
268 for (iw = begin; iw < end; ++iw) {
269 bunchIds_[sortedIds_[iw]] = ib;
270 }
271 }
272 UTIL_CHECK(end == kSize_);
273
274 isSorted_ = true;
275 }
276
277}
278#endif
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
Iterator over points in a Mesh<D>.
int rank() const
Get the rank of current element.
void begin()
Set iterator to the first point in the mesh.
bool atEnd() const
Is this the end (i.e., one past the last point)?
IntVec< D > position() const
Get current position in the grid, as integer vector.
Description of a regular grid of points in a periodic domain.
Definition Mesh.h:61
int size() const
Get total number of grid points.
Definition Mesh.h:229
int nParameter() const
Get the number of parameters in the unit cell.
Base template for UnitCell<D> classes, D=1, 2 or 3.
Definition UnitCell.h:55
void clearUnitCellData()
Clear all internal data that depends on lattice parameters.
bool isRealField() const
Is this WaveList set up for use with real-valued fields?
void computeMinimumImages()
Compute minimum images of wavevectors, and also calculates kSq.
WaveList(bool isRealField=true)
Constructor.
void computeKSq()
Compute square norm |k|^2 for all wavevectors.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
void findBunches(std::vector< Item< T > > const &items, GArray< Bunch > &bunches, T epsilon)
Identify "bunches" of equal values within a sorted vector.
Definition Sort.cpp:29
void sort(std::vector< Item< T > > &items)
Sort a std::vector< Item<T> > by ascending item value.
Definition Sort.cpp:22
PSCF package top-level namespace.
bool hasVariableAngle(typename UnitCell< D >::LatticeSystem lattice)
Return true if lattice type has variable angle parameters.
Struct with value and index, to keep track of permutation.
Definition Sort.h:37