TBTK
Need a break? Support the development by playing Polarity Puzzles
FourierTransform.h
Go to the documentation of this file.
1 /* Copyright 2016 Kristofer Björnson
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
23 #ifndef COM_DAFER45_TBTK_FOURIER_TRANSFORM
24 #define COM_DAFER45_TBTK_FOURIER_TRANSFORM
25 
26 #include "TBTK/CArray.h"
27 #include "TBTK/Index.h"
28 
29 #include <fftw3.h>
30 
31 #include <complex>
32 #include <vector>
33 
34 namespace TBTK{
35 
46 public:
48  template<typename DataType>
49  class Plan{
50  public:
52  Plan(
53  const CArray<DataType> &in,
54  CArray<DataType> &out,
55  const std::vector<unsigned int> &ranges,
56  int sign
57  );
58 
60  Plan(const Plan &plan) = delete;
61 
63  Plan(Plan &&plan);
64 
66  ~Plan();
67 
69  Plan& operator=(const Plan &plan) = delete;
70 
72  Plan& operator=(Plan &&plan);
73 
75  void setNormalizationFactor(double normalizationFactor);
76 
78  double getNormalizationFactor() const;
79  private:
81  fftw_plan *plan;
82 
84  double normalizationFactor;
85 
87  unsigned int size;
88 
90  const CArray<DataType> &input;
91 
93  CArray<DataType> &output;
94 
96  fftw_plan& getFFTWPlan();
97 
99  unsigned int getSize() const;
100 
102  CArray<DataType>& getInput();
103 
105  CArray<DataType>& getOutput();
106 
108  friend class FourierTransform;
109  };
110 
112  template<typename DataType>
113  class ForwardPlan : public Plan<DataType>{
114  public:
117  const CArray<DataType> &in,
118  CArray<DataType> &out,
119  const std::vector<unsigned int> &ranges
120  ) : Plan<DataType>(in, out, ranges, -1){}
121  };
122 
124  template<typename DataType>
125  class InversePlan : public Plan<DataType>{
126  public:
129  const CArray<DataType> &in,
130  CArray<DataType> &out,
131  const std::vector<unsigned int> &ranges
132  ) : Plan<DataType>(in, out, ranges, 1
133  ){}
134  };
135 
143  static void transform(
144  const CArray<std::complex<double>> &in,
145  CArray<std::complex<double>> &out,
146  const std::vector<unsigned int> &ranges,
147  int sign
148  );
149 
153  template<typename DataType>
154  static void transform(Plan<DataType> &plan);
155 
161  static void forward(
162  const CArray<std::complex<double>> &in,
163  CArray<std::complex<double>> &out,
164  const std::vector<unsigned int> &ranges
165  );
166 
172  static void inverse(
173  const CArray<std::complex<double>> &in,
174  CArray<std::complex<double>> &out,
175  const std::vector<unsigned int> &ranges
176  );
177 
178 private:
179 };
180 
181 template<typename DataType>
183  fftw_execute(plan.getFFTWPlan());
184 
185  double normalizationFactor = plan.getNormalizationFactor();
186  if(normalizationFactor != 1.){
187  CArray<DataType> &output = plan.getOutput();
188  for(unsigned int n = 0; n < plan.getSize(); n++)
189  output[n] /= normalizationFactor;
190  }
191 }
192 
194  const CArray<std::complex<double>> &in,
195  CArray<std::complex<double>> &out,
196  const std::vector<unsigned int> &ranges
197 ){
198  transform(in, out, ranges, -1);
199 }
200 
202  const CArray<std::complex<double>> &in,
203  CArray<std::complex<double>> &out,
204  const std::vector<unsigned int> &ranges
205 ){
206  transform(in, out, ranges, 1);
207 }
208 
209 template<typename DataType>
211  this->plan = plan.plan;
212  plan.plan = nullptr;
213 
214  normalizationFactor = plan.normalizationFactor;
215  size = plan.size;
216  input = plan.input;
217  output = plan.output;
218 }
219 
220 template<typename DataType>
222  if(plan != nullptr){
223  #pragma omp critical (TBTK_FOURIER_TRANSFORM)
224  fftw_destroy_plan(*plan);
225 
226  delete plan;
227  }
228 
229 }
230 
231 template<typename DataType>
233  DataType
234 >::operator=(Plan &&rhs){
235  if(this != &rhs){
236  if(this->plan != nullptr){
237  #pragma omp critical (TBTK_FOURIER_TRANSFORM)
238  fftw_destroy_plan(*this->plan);
239 
240  delete this->plan;
241 
242  this->plan = rhs.plan;
243 
244  normalizationFactor = rhs.normalizationFactor;
245  size = rhs.size;
246  input = rhs.input;
247  output = rhs.output;
248  }
249  }
250 
251  return *this;
252 }
253 
254 template<typename DataType>
256  double normalizationFactor
257 ){
258  this->normalizationFactor = normalizationFactor;
259 }
260 
261 template<typename DataType>
263  return normalizationFactor;
264 }
265 
266 template<typename DataType>
268  return *plan;
269 }
270 
271 template<typename DataType>
272 inline unsigned int FourierTransform::Plan<DataType>::getSize() const{
273  return size;
274 }
275 
276 template<typename DataType>
278  return input;
279 }
280 
281 template<typename DataType>
283  return output;
284 }
285 
286 }; //End of namespace TBTK
287 
288 #endif
Plan & operator=(const Plan &plan)=delete
double getNormalizationFactor() const
Definition: FourierTransform.h:262
Physical index.
~Plan()
Definition: FourierTransform.h:221
Container for a C style array.
Definition: FourierTransform.h:49
InversePlan(const CArray< DataType > &in, CArray< DataType > &out, const std::vector< unsigned int > &ranges)
Definition: FourierTransform.h:128
static void inverse(const CArray< std::complex< double >> &in, CArray< std::complex< double >> &out, const std::vector< unsigned int > &ranges)
Definition: FourierTransform.h:201
ForwardPlan(const CArray< DataType > &in, CArray< DataType > &out, const std::vector< unsigned int > &ranges)
Definition: FourierTransform.h:116
Definition: FourierTransform.h:125
Plan(const CArray< DataType > &in, CArray< DataType > &out, const std::vector< unsigned int > &ranges, int sign)
void setNormalizationFactor(double normalizationFactor)
Definition: FourierTransform.h:255
Definition: FourierTransform.h:113
Container for a C style array.
Definition: CArray.h:44
Definition: Boolean.h:32
Fourier transform.
Definition: FourierTransform.h:45
static void forward(const CArray< std::complex< double >> &in, CArray< std::complex< double >> &out, const std::vector< unsigned int > &ranges)
Definition: FourierTransform.h:193
static void transform(const CArray< std::complex< double >> &in, CArray< std::complex< double >> &out, const std::vector< unsigned int > &ranges, int sign)