test_FftwReal2dDftData.cxx
Go to the documentation of this file.
1 // test_FftwReal2dDftData.cxx
2 //
3 // David Adams
4 // February 2021
5 //
6 // Test FftwReal2dDftData.
7 
8 #include <string>
9 #include <iostream>
10 #include <iomanip>
11 #include <fstream>
12 #include <vector>
14 
15 #undef NDEBUG
16 #include <cassert>
17 
18 using std::string;
19 using std::cout;
20 using std::endl;
21 using std::setw;
22 using std::vector;
23 
30 
31 Index realval(Index idat) { return 2*idat + 1; }
32 Index imagval(Index idat) { return 2*idat + 2; }
33 
34 //**********************************************************************
35 
36 int test_FftwReal2dDftData(Index nsam0, Index nsam1) {
37  const string myname = "test_FftwReal2dDftData: ";
38 #ifdef NDEBUG
39  cout << myname << "NDEBUG must be off." << endl;
40  abort();
41 #endif
42  string line = myname + "-----------------------------";
43 
44  cout << line << endl;
45  cout << myname << "Sample sizes: " << nsam0 << ", " << nsam1 << endl;
46 
47  cout << line << endl;
48  cout << myname << "Checking index array." << endl;
49  IndexArray nsams({nsam0, nsam1});
50  assert( nsams[0] == nsam0 );
51  assert( nsams[1] == nsam1 );
52  Index nComplexExp = nsam0*(nsam1/2 + 1);
53  Index ndftComplex = DftData::dftComplexDataSize(nsams);
54  cout << myname << " Complex size: " << ndftComplex << endl;
55  Index ndftFloat = DftData::dftFloatDataSize(nsams);
56  cout << myname << " Float size: " << ndftFloat << endl;
57  assert( ndftComplex == nComplexExp );
58  assert( ndftFloat == 2*nComplexExp );
59 
60  cout << line << endl;
61  cout << myname << "Construct DFT object." << endl;
62  DftData::Norm norm(22);
63  DftData dft(norm, nsams);
64  assert( dft.rank() == 2 );
65  assert( dft.normalization() == norm );
66  assert( dft.size() == ndftComplex );
67  assert( dft.nSamples(0) == nsam0 );
68  assert( dft.size(0) == nsam0 );
69  assert( dft.nSamples(1) == nsam1 );
70  assert( dft.size(1) == nsam1 );
71 
72  cout << myname << "Check indexing." << endl;
73  Index cntij = 0;
74  for ( Index idat=0; idat<nComplexExp; ++idat ) {
75  IndexArrayVector idxas = dft.indexArrays(idat);
76  cout << myname << setw(3) << idat << ": ";
77  Index nerr = idxas.size() == 0;
78  bool first = true;
79  for ( IndexArray idxa : idxas ) {
80  Index jdat = dft.globalIndex(idxa);
81  if ( first ) first = false;
82  else cout << ", ";
83  cout << "[" << setw(3) << idxa[0] << "," << setw(3) << idxa[1] << "]";
84  if ( jdat != idat ) cout << " --> " << jdat << " (!)";
85  if ( jdat != idat ) ++nerr;
86  ++cntij;
87  }
88  cout << endl;
89  assert( nerr == 0 );
90  }
91  assert( cntij == nsam0*nsam1 );
92 
93  cout << line << endl;
94  cout << myname << "Create test data." << endl;
95  vector<Complex> datc(nComplexExp);
96  vector<Float> datf(2*nComplexExp);
97  Index iflt = 0;
98  Index idat;
99  for ( idat=0; idat<nComplexExp; ++idat ) {
100  IndexArrayVector arrs = dft.indexArrays(idat);
101  Float re = realval(idat);
102  Float im = imagval(idat);
103  datf[iflt++] = re;
104  datf[iflt++] = im;
105  Complex cval(re, im);
106  datc[idat] = cval;
107  }
108  assert( idat == datc.size() );
109  assert( iflt == datf.size() );
110 
111  cout << line << endl;
112  cout << myname << "Copy data in." << endl;
113  assert( dft.copyIn(nsams, datc) == 0 );
114  for ( Index idat=0; idat<nComplexExp; ++idat ) {
115  IndexArrayVector idxas = dft.indexArrays(idat);
116  assert( idxas.size() );
117  Complex cval = dft.value(idat);
118  Index ire = cval.real() + 0.1;
119  Index iim = cval.imag() + 0.1;
120  cout << myname << setw(3) << idat << ": ["
121  << setw(3) << idxas[0][0] << "," << setw(3) << idxas[0][1] << "]: "
122  << "(" << setw(3) << ire << "," << setw(3) << iim << ")"
123  << endl;
124  }
125 
126  cout << line << endl;
127  cout << myname << "Copy object." << endl;
128  DftData dft2(dft);
129  assert( dft2.rank() == 2 );
130  assert( dft2.normalization() == norm );
131  assert( dft2.size() == ndftComplex );
132  assert( dft2.nSamples(0) == nsam0 );
133  assert( dft2.size(0) == nsam0 );
134  assert( dft2.nSamples(1) == nsam1 );
135  assert( dft2.size(1) == nsam1 );
136  for ( Index idat=0; idat<nComplexExp; ++idat ) {
137  IndexArrayVector idxas = dft.indexArrays(idat);
138  assert( idxas.size() );
139  Complex cval = dft2.value(idat);
140  Index ire = cval.real() + 0.1;
141  Index iim = cval.imag() + 0.1;
142  cout << myname << setw(3) << idat << ": ["
143  << setw(3) << idxas[0][0] << "," << setw(3) << idxas[0][1] << "]: "
144  << "(" << setw(3) << ire << "," << setw(3) << iim << ")"
145  << endl;
146  }
147 
148  cout << line << endl;
149  cout << myname << "All tests passed." << endl;
150  return 0;
151 }
152 
153 //**********************************************************************
154 
155 int main(int argc, char* argv[]) {
156  if ( argc > 1 ) {
157  cout << "Usage: " << argv[0] << " [ARG]" << endl;
158  return 0;
159  }
160  Index stat = 0;
161  stat += test_FftwReal2dDftData(3, 4);
162  stat += test_FftwReal2dDftData(4, 3);
163  return stat;
164 }
165 
166 //**********************************************************************
std::array< Index, 2 > IndexArray
Definition: Real2dDftData.h:27
DftData::IndexArrayVector IndexArrayVector
Index imagval(Index idat)
Fw2dFFT::Complex Complex
int copyIn(const IndexArray nsams, const ComplexVector &data)
std::string string
Definition: nybbler.cc:12
IndexArrayVector indexArrays(Index idat) const
struct vector vector
Norm normalization() const override
int test_FftwReal2dDftData(Index nsam0, Index nsam1)
DftData::IndexArray IndexArray
unsigned int Index
static Index dftComplexDataSize(const std::array< Index, N > &nsams)
Index rank() const
Index realval(Index idat)
Index globalIndex(const IndexArray &ifrqs) const
FftwReal2dDftData< double > FftwDouble2dDftData
unsigned int Index
Definition: Real2dDftData.h:26
Index size() const
auto norm(Vector const &v)
Return norm of the specified vector.
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
std::complex< Float > Complex
Definition: Real2dDftData.h:29
static Index dftFloatDataSize(const std::array< Index, N > &nsams)
void line(double t, double *p, double &x, double &y, double &z)
const IndexArray & nSamples() const override
Complex value(Index idat) const
std::vector< IndexArray > IndexArrayVector
DftData::Float Float
int main(int argc, char *argv[])
QTextStream & endl(QTextStream &s)