QMCPACK
FftContainer.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois / NCSA Open Source License.
3 // See LICENSE file in top directory for details .
4 //
5 // Copyright ( c ) 2018 QMCPACK developers
6 //
7 // File developed by : Luke Shulenburger, lshulen@sandia.gov, Sandia National Laboratories
8 //
9 // File created by : Luke Shulenburger, lshulen@sandia.gov, Sandia National Laboratories
10 /////////////////////////////////////////////////////////////////////////////////////////
11 
12 #ifndef FFT_CONTAINER_H
13 #define FFT_CONTAINER_H
14 #include "fftw3.h"
15 
17 {
18 private:
19  fftw_plan plan_;
20  int nx_, ny_, nz_;
21 
22 public:
23  int fullSize;
24  fftw_complex* rspace;
25  fftw_complex* kspace;
26 
27 public:
28  FftContainer(int nx, int ny, int nz);
29  double getRsValue(int x, int y, int z, int cplex) const;
30  void setRsValue(int x, int y, int z, int cplex, double value);
31  double getKsValue(int x, int y, int z, int cplex) const;
32  void setKsValue(int x, int y, int z, int cplex, double value);
33 
34  int getNx() const { return nx_; }
35  int getNy() const { return ny_; }
36  int getNz() const { return nz_; }
37 
38  int getIndex(int x, int y, int z) const { return z + y * nz_ + x * ny_ * nz_; }
39  int getQboxIndex(int x, int y, int z) const { return x + y * nx_ + z * ny_ * nx_; }
40 
41  ~FftContainer();
42  void executeFFT();
43  void fixRsNorm(double factor)
44  {
45  for (int i = 0; i < fullSize; i++)
46  {
47  rspace[i][0] *= factor;
48  rspace[i][1] *= factor;
49  }
50  }
51  void fixKsNorm(double factor)
52  {
53  for (int i = 0; i < fullSize; i++)
54  {
55  kspace[i][0] *= factor;
56  kspace[i][1] *= factor;
57  }
58  }
59 
60  double getL2NormRS() const;
61  double getL2NormKS() const;
62 };
63 
64 
65 FftContainer::FftContainer(int nx, int ny, int nz)
66 {
67  nx_ = nx;
68  ny_ = ny;
69  nz_ = nz;
70  fullSize = nx_ * ny_ * nz_;
71  rspace = (fftw_complex*)fftw_malloc(fullSize * sizeof(fftw_complex));
72  kspace = (fftw_complex*)fftw_malloc(fullSize * sizeof(fftw_complex));
73  plan_ = fftw_plan_dft_3d(nx_, ny_, nz_, rspace, kspace, -1, FFTW_ESTIMATE);
74 }
75 
77 {
78  fftw_destroy_plan(plan_);
79  fftw_free(rspace);
80  fftw_free(kspace);
81 }
82 
83 void FftContainer::executeFFT() { fftw_execute(plan_); }
84 
86 {
87  double l2norm = 0.0;
88  for (int i = 0; i < fullSize; i++)
89  {
90  l2norm += (rspace[i][0] * rspace[i][0] + rspace[i][1] * rspace[i][1]);
91  }
92  return l2norm;
93 }
94 
96 {
97  double l2norm = 0.0;
98  for (int i = 0; i < fullSize; i++)
99  {
100  l2norm += (kspace[i][0] * kspace[i][0] + kspace[i][1] * kspace[i][1]);
101  }
102  return l2norm;
103 }
104 
105 double FftContainer::getRsValue(int x, int y, int z, int cplex) const { return rspace[getIndex(x, y, z)][cplex]; }
106 
107 void FftContainer::setRsValue(int x, int y, int z, int cplex, double value)
108 {
109  rspace[getIndex(x, y, z)][cplex] = value;
110 }
111 
112 double FftContainer::getKsValue(int x, int y, int z, int cplex) const { return kspace[getIndex(x, y, z)][cplex]; }
113 
114 void FftContainer::setKsValue(int x, int y, int z, int cplex, double value)
115 {
116  kspace[getIndex(x, y, z)][cplex] = value;
117 }
118 
119 
120 #endif
int getNx() const
Definition: FftContainer.h:34
double getL2NormRS() const
Definition: FftContainer.h:85
fftw_plan plan_
Definition: FftContainer.h:19
void setKsValue(int x, int y, int z, int cplex, double value)
Definition: FftContainer.h:114
double getKsValue(int x, int y, int z, int cplex) const
Definition: FftContainer.h:112
void fixKsNorm(double factor)
Definition: FftContainer.h:51
double getL2NormKS() const
Definition: FftContainer.h:95
double getRsValue(int x, int y, int z, int cplex) const
Definition: FftContainer.h:105
void fixRsNorm(double factor)
Definition: FftContainer.h:43
int getQboxIndex(int x, int y, int z) const
Definition: FftContainer.h:39
int getNy() const
Definition: FftContainer.h:35
FftContainer(int nx, int ny, int nz)
Definition: FftContainer.h:65
fftw_complex * kspace
Definition: FftContainer.h:25
void executeFFT()
Definition: FftContainer.h:83
void setRsValue(int x, int y, int z, int cplex, double value)
Definition: FftContainer.h:107
int getNz() const
Definition: FftContainer.h:36
int getIndex(int x, int y, int z) const
Definition: FftContainer.h:38
fftw_complex * rspace
Definition: FftContainer.h:24