1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
#include <cmath>
#include <iomanip>
#include <complex>
#include <cstring>
#include <iostream>
#include <fftw3.h>
static const int nx = 8;
static const int ny = 8;
static const int ncomp = 2;
static const int nyk = ny/2 + 1;
static const int nxk = nx/2 + 1;
using namespace std;
// NOTE(mbozzi): Non-static member data was removed from these classes because
// it is only used in the constructor.
class MyFftwClass {
public:
MyFftwClass(void);
// ~MyFftwClass(void);
void r2cexecute(double rArr[], double cArr[][ncomp]);
private:
static fftw_plan s_plan; // <-- shared by all instances
// double *m_buffer_in;
// fftw_complex *m_buffer_out;
};
class MyiFftwClass { //inverse
public:
MyiFftwClass(void);
// ~MyiFftwClass(void);
void c2rexecute(double cArr[][ncomp],double rArr[]);
private:
static fftw_plan s_plan1; // <-- shared by all instances
// fftw_complex *m_buffer_in1;
// double *m_buffer_out1;
};
MyFftwClass r2c;
MyiFftwClass c2r;
// Assumes row-major storage where i = row index, j = column index.
int ij(int i, int j, int number_of_cols) { return j + i * number_of_cols; }
void print(double* m, int width, int height)
{
std::cout << std::fixed << std::setprecision(2);
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
std::cout << std::setw(6) << m[ij(i, j, width)] << ' ';
std::cout << '\n';
}
std::cout << '\n';
}
void print(fftw_complex* m, int width, int height)
{
std::cout << std::fixed << std::setprecision(2);
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
std::cout << std::setw(6) << std::complex{m[ij(i, j, width)][0], m[ij(i, j, width)][1]} << ' ';
std::cout << '\n';
}
std::cout << '\n';
}
int main()
{
double *uFun;
uFun = (double*) fftw_malloc((nx*(ny+1))*sizeof(double));
// for(int i = 0; i< nx; i++){
// for(int j = 0; j< ny+1; j++){
// uFun[j + (ny+1)*i] =//some real function
// }
// }
// NOTE(mbozzi): Using the same test function as earlier
for(int i = 0; i < ny + 1; i++)
for(int j = 0; j < nx; j++)
{
double const XX = +std::sin(j * 2.0 * 3.14159 / nx);
double const YY = -std::cos(i * 3.14159 / ny) - 0.5;
uFun[ij(i, j, nx)] = YY * YY * XX;
}
print(uFun, nx, ny + 1);
// fftw_complex *uFunk;
// uFunk = (fftw_complex*) fftw_malloc((nx*nyk) * sizeof(fftw_complex));
// memset(uFunk, 42, (nx*nyk)* sizeof(fftw_complex));
fftw_complex *uFunk;
uFunk = (fftw_complex*) fftw_malloc((nxk * (ny + 1)) * sizeof(fftw_complex));
memset(uFunk, 42, (nxk*(ny + 1))* sizeof(fftw_complex));
r2c.r2cexecute(uFun,uFunk); //take forward FFT
print(uFunk, nxk, ny + 1);
double *Out;
Out = (double*) fftw_malloc((nx*(ny+1))*sizeof(double));
memset(Out, 42, (nx*(ny+1)) * sizeof(double));
c2r.c2rexecute(uFunk,Out);
print(Out, nx, ny + 1);
}
MyFftwClass::MyFftwClass(void)
{
// m_buffer_in = fftw_alloc_real((nx*ny));
// m_buffer_out = fftw_alloc_complex((nx*nyk));
double* buffer_in = fftw_alloc_real(nx * (ny + 1));
fftw_complex* buffer_out = fftw_alloc_complex(nxk * (ny + 1));
if (!(buffer_in && buffer_out))
{
// throw new std::runtime_error("Failed to allocate memory!");
throw std::runtime_error("Failed to allocate memory!");
}
if (!s_plan)
{
// s_plan = fftw_plan_many_dft_r2c(1,&nx, (ny+1),m_buffer_in,nullptr,(ny+1),1,m_buffer_out,nullptr, (ny+1),1, FFTW_MEASURE);
s_plan = fftw_plan_many_dft_r2c(
1, // int rank,
&nx, // const int *n,
ny + 1, // int howmany,
buffer_in, // double *in,
nullptr, // const int *inembed,
1, // int istride, // adjacent elements within a row are adjacent.
nx, // int idist, // adjacent elements within a column are separate
buffer_out, // fftw_complex *out,
nullptr, // const int *onembed,
1, // int ostride,
nxk, // int odist,
FFTW_MEASURE // unsigned flags
);
if (!s_plan)
{
// throw new std::runtime_error("Failed to create plan!");
throw std::runtime_error("Failed to create plan!");
}
}
fftw_free(buffer_out);
fftw_free(buffer_in);
}
// MyFftwClass::~MyFftwClass(void)
// {
// fftw_free(m_buffer_in);
// fftw_free(m_buffer_out);
// }
// void MyFftwClass::r2cexecute(double rArr[], double cArr[][ncomp]) MyFftwClass::execute(double *const in, fftw_complex *const out)
// {
// memcpy(m_buffer_in, rArr, sizeof(double) * (nx*ny));
// fftw_execute_dft_r2c(s_plan, m_buffer_in, m_buffer_out);
// memcpy(cArr, m_buffer_out, sizeof(fftw_complex) * (nx*nyk));
// }
void MyFftwClass::r2cexecute(double *const rArr, fftw_complex *const cArr)
{
// NOTE(mbozzi): Use cArr anr rArr directly, instead of using
// m_buffer_in/m_buffer_out followed by copying their contents.
fftw_execute_dft_r2c(s_plan, rArr, cArr);
}
MyiFftwClass::MyiFftwClass(void)
{
// m_buffer_in1 = fftw_alloc_complex((nx*nyk));
// m_buffer_out1 = fftw_alloc_real((nx*ny));
fftw_complex* buffer_in1 = fftw_alloc_complex(nxk * (ny + 1));
double* buffer_out1 = fftw_alloc_real(nx*(ny + 1));
if (!(buffer_in1 && buffer_out1))
{
// NOTE(mbozzi): prefer to "throw" instead of "throw new". It's a
// particularly bad idea in the case of out-of-memory error, because "new"
// itself allocates memory.
//
// If you did "throw new", and care about error recovery, you ought to catch
// the pointer and delete it:
// try { throw new std::runtime_error(""); } catch(std::runtime_error* pe) { delete pe; }
// Else the new exception object will not be freed (a small memory leak).
// NOTE(mbozzi): Consider calling std::abort instead of throwing exceptions.
// throw new std::runtime_error("Failed to allocate memory!");
throw std::runtime_error("Failed to allocate memory!");
}
if (!s_plan1)
{
// s_plan1 = fftw_plan_many_dft_c2r(1,&nx, (nyk),m_buffer_in1,nullptr,(nyk),1,m_buffer_out1,nullptr, (ny+1),1, FFTW_MEASURE);
s_plan1 = fftw_plan_many_dft_c2r(
1, // int rank,
&nx, // const int *n,
ny + 1, // int howmany,
buffer_in1, // double *in,
nullptr, // const int *inembed,
1, // int istride, // adjacent elements within a row are adjacent.
nxk, // int idist, // adjacent elements within a column are separate
buffer_out1, // fftw_complex *out,
nullptr, // const int *onembed,
1, // int ostride,
nx, // int odist,
FFTW_MEASURE // unsigned flags
);
if (!s_plan1)
{
// throw new std::runtime_error("Failed to create plan!");
throw std::runtime_error("Failed to create plan!");
}
}
fftw_free(buffer_out1);
fftw_free(buffer_in1);
}
// MyiFftwClass::~MyiFftwClass(void)
// {
// fftw_free(m_buffer_in1);
// fftw_free(m_buffer_out1);
// }
//void MyiFftwClass::c2rexecute(double cArr[][ncomp],double rArr[]) void MyFftwClass::execute(double *const in, fftw_complex *const out)
// {
// memcpy(m_buffer_in1,cArr, sizeof(fftw_complex) * (nx*nyk));
// fftw_execute_dft_c2r(s_plan1, m_buffer_in1, m_buffer_out1); //instead of fftw_excute(plan)
// memcpy(rArr, m_buffer_out1,sizeof(double) * (nx*ny));
// //renormalize
// for(int i = 0; i < nx; i++){
// for( int j = 0; j < ny+1; j++){
// //rArr[j + (ny+1)*i] = rArr[j + (ny+1)*i] * 1.0 / (nx);
// }
// }
// }
void MyiFftwClass::c2rexecute(double cArr[][ncomp],double rArr[])
{
fftw_execute_dft_c2r(s_plan1, cArr, rArr); // instead of fftw_excute(plan)
// renormalize
for (int i = 0; i < ny + 1; ++i)
for (int j = 0; j < nx; ++j)
rArr[ij(i, j, nx)] /= nx;
}
fftw_plan MyiFftwClass::s_plan1 = NULL;
fftw_plan MyFftwClass::s_plan = NULL;
|