PSCF v1.5.0
cpp/FFT.tpp
1#ifndef PSCF_FFT_CP_TPP
2#define PSCF_FFT_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 "FFT.h"
12#include "RField.h"
13#include "CField.h"
14
15/*
16* A note about const_casts:
17*
18* The FFTW library is used in this file to perform discrete Fourier
19* transforms. FFTW's complex-to-real inverse transform overwrites its
20* input array, but all other out-of-place transforms leave the input
21* array unaltered. However, all transforms in the FFTW library require
22* non-const pointers to the input array, even though they do not alter
23* the array.
24*
25* In order to maintain const-correctness in PSCF, this FFT class accepts
26* const input arrays for its methods that perform a Fourier transform,
27* unless the transform is expected to modify / overwrite its input (as
28* is the case for complex-to-real inverse transforms). This denotes to
29* the caller of the method that the input array will not be altered,
30* which is an accurate representation of the expected behavior.
31*
32* However, the const-correctness of this FFT class creates a conflict
33* with the FFTW library's requirement of non-const inputs. This conflict
34* is resolved using a const_cast, in which the const pointer to the input
35* array is made non-const when passed into FFTW functions. The use of
36* const_cast is reserved only for these few select cases in which we are
37* confident that the input array will not be modified.
38*
39* For more information about the relevant FFTW methods, see the FFTW
40* documentation at https://www.fftw.org/fftw3_doc/index.html. In Section
41* 4.3.2, it is stated that, by default, "an out-of-place transform must
42* not change its input array," except for complex-to-real transforms, in
43* which case "no input-preserving algorithms are implemented." Finally,
44* we note that the unit tests for this FFT class check that the input
45* array is unaltered, allowing developers to continually ensure that
46* the FFTW functions do not modify their input unexpectedly.
47*/
48
49namespace Pscf {
50
51 using namespace Util;
52
53 /*
54 * Default constructor.
55 */
56 template <int D>
58 : meshDimensions_(0),
59 rSize_(0),
60 kSize_(0),
61 rcfPlan_(0),
62 criPlan_(0),
63 ccfPlan_(0),
64 cciPlan_(0),
65 isSetup_(false)
66 {}
67
68 /*
69 * Destructor.
70 */
71 template <int D>
73 {
74 if (rcfPlan_) {
75 fftw_destroy_plan(rcfPlan_);
76 }
77 if (criPlan_) {
78 fftw_destroy_plan(criPlan_);
79 }
80 if (ccfPlan_) {
81 fftw_destroy_plan(ccfPlan_);
82 }
83 if (cciPlan_) {
84 fftw_destroy_plan(cciPlan_);
85 }
86 }
87
88 /*
89 * Setup mesh dimensions, work memory and FFT plans.
90 */
91 template <int D>
93 {
94 // Precondition
95 UTIL_CHECK(!isSetup_);
96
97 // Set and check mesh dimensions
98 rSize_ = 1;
99 kSize_ = 1;
100 for (int i = 0; i < D; ++i) {
102 meshDimensions_[i] = meshDimensions[i];
103 rSize_ *= meshDimensions[i];
104 if (i < D - 1) {
105 kSize_ *= meshDimensions[i];
106 } else {
107 kSize_ *= (meshDimensions[i]/2 + 1);
108 }
109 }
110
111 // Allocate kFieldCopy_ array if necessary
112 if (!kFieldCopy_.isAllocated()) {
113 kFieldCopy_.allocate(meshDimensions);
114 } else {
115 if (kFieldCopy_.capacity() != kSize_) {
116 kFieldCopy_.deallocate();
117 kFieldCopy_.allocate(meshDimensions);
118 }
119 }
120 UTIL_CHECK(meshDimensions == kFieldCopy_.meshDimensions());
121 UTIL_CHECK(kFieldCopy_.capacity() == kSize_);
122
123 // Create temporary RField and CField objects used for plans
124 RField<D,CPT> rField;
125 rField.allocate(meshDimensions);
126 UTIL_CHECK(meshDimensions == rField.meshDimensions());
127 UTIL_CHECK(rField.capacity() == rSize_);
128
129 CField<D,CPT> cFieldIn;
130 cFieldIn.allocate(meshDimensions);
131 UTIL_CHECK(meshDimensions == cFieldIn.meshDimensions());
132 UTIL_CHECK(cFieldIn.capacity() == rSize_);
133
134 CField<D,CPT> cFieldOut;
135 cFieldOut.allocate(meshDimensions);
136 UTIL_CHECK(meshDimensions == cFieldOut.meshDimensions());
137 UTIL_CHECK(cFieldOut.capacity() == rSize_);
138
139 // Make FFTW plans (see explicit specializations in FFT.cpp)
140 makePlans(rField, kFieldCopy_, cFieldIn, cFieldOut);
141
142 isSetup_ = true;
143 }
144
145 // Real <-> Complex Transforms
146
147 /*
148 * Execute real-to-complex forward transform.
149 */
150 template <int D>
152 RFieldDft<D,CPT>& kField)
153 const
154 {
155 UTIL_CHECK(isSetup_)
156 UTIL_CHECK(rField.capacity() == rSize_);
157 UTIL_CHECK(kField.capacity() == kSize_);
158 UTIL_CHECK(rField.meshDimensions() == meshDimensions_);
159 UTIL_CHECK(kField.meshDimensions() == meshDimensions_);
160
161 // Execute preplanned forward transform
162 // (See note at top of file explaining this use of const_cast)
163 fftw_execute_dft_r2c(rcfPlan_, const_cast<double*>(&rField[0]),
164 &kField[0]);
165
166 // Rescale the resulting array
167 double scale = 1.0/double(rSize_);
168 for (int i = 0; i < kSize_; ++i) {
169 kField[i][0] *= scale;
170 kField[i][1] *= scale;
171 }
172 }
173
174 /*
175 * Execute inverse (complex-to-real) transform.
176 */
177 template <int D>
178 void
180 RFieldDft<D,CPT> & kField,
181 RField<D,CPT>& rField)
182 const
183 {
184 UTIL_CHECK(isSetup_)
185 UTIL_CHECK(rField.capacity() == rSize_);
186 UTIL_CHECK(kField.capacity() == kSize_);
187 UTIL_CHECK(rField.meshDimensions() == meshDimensions_);
188 UTIL_CHECK(kField.meshDimensions() == meshDimensions_);
189
190 // Execute preplanned inverse transform
191 fftw_execute_dft_c2r(criPlan_, &kField[0], &rField[0]);
192 }
193
194 /*
195 * Execute inverse (complex-to-real) transform without destroying input.
196 */
197 template <int D>
198 void
200 RFieldDft<D,CPT> const & kField,
201 RField<D,CPT>& rField) const
202 {
203 UTIL_CHECK(kFieldCopy_.capacity() == kField.capacity());
204
205 kFieldCopy_ = kField;
206 inverseTransformUnsafe(kFieldCopy_, rField);
207 }
208
209 // Complex <-> Complex Transforms
210
211 /*
212 * Execute complex-to-complex forward transform.
213 */
214 template <int D>
215 void
217 CField<D,CPT> const & rField,
218 CField<D,CPT>& kField) const
219 {
220 UTIL_CHECK(isSetup_)
221 UTIL_CHECK(rField.capacity() == rSize_);
222 UTIL_CHECK(rField.meshDimensions() == meshDimensions_);
223 UTIL_CHECK(kField.capacity() == rSize_);
224 UTIL_CHECK(kField.meshDimensions() == meshDimensions_);
225
226 // Execute preplanned forward transform
227 // (See note at top of file explaining this use of const_cast)
228 fftw_execute_dft(ccfPlan_, const_cast<fftw_complex*>(&rField[0]),
229 &kField[0]);
230
231 // Rescale the resulting array
232 double scale = 1.0/double(rSize_);
233 for (int i = 0; i < rSize_; ++i) {
234 kField[i][0] *= scale;
235 kField[i][1] *= scale;
236 }
237 }
238
239 /*
240 * Execute inverse (complex-to-complex) transform.
241 */
242 template <int D>
243 void
245 CField<D,CPT> const & kField,
246 CField<D,CPT>& rField)
247 const
248 {
249 UTIL_CHECK(isSetup_)
250 UTIL_CHECK(rField.capacity() == rSize_);
251 UTIL_CHECK(kField.capacity() == rSize_);
252 UTIL_CHECK(rField.meshDimensions() == meshDimensions_);
253 UTIL_CHECK(kField.meshDimensions() == meshDimensions_);
254
255 // Execute preplanned inverse transform
256 // (See note at top of file explaining this use of const_cast)
257 fftw_execute_dft(cciPlan_, const_cast<fftw_complex*>(&kField[0]),
258 &rField[0]);
259 }
260
261 // Static function
262
263 /*
264 * Compute dimensions and size of the k-size mesh (static)
265 */
266 template <int D>
268 IntVec<D> const & rMeshDimensions,
269 IntVec<D> & kMeshDimensions,
270 int & kSize )
271 {
272 kSize = 1;
273 for (int i = 0; i < D; ++i) {
274 if (i < D - 1) {
275 kMeshDimensions[i] = rMeshDimensions[i];
276 } else {
277 kMeshDimensions[i] = rMeshDimensions[i]/2 + 1;
278 }
279 kSize *= kMeshDimensions[i];
280 }
281 }
282
283} // namespace Pscf
284#endif
IntVec< D > const & meshDimensions() const
Return the dimensions of the grid for which this was setup.
Definition cpp/FFT.h:280
void inverseTransformUnsafe(RFieldDft< D, CPT > &in, RField< D, CPT > &out) const
Compute inverse (complex-to-real) DFT, overwriting the input.
Definition cpp/FFT.tpp:179
FFT()
Default constructor.
Definition cpp/FFT.tpp:57
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
PSCF package top-level namespace.