[768] | 1 | #include "machdefs.h"
|
---|
| 2 |
|
---|
[460] | 3 | #include <math.h>
|
---|
[2322] | 4 | #include <iostream>
|
---|
[460] | 5 |
|
---|
[768] | 6 | #include "nbrandom.h"
|
---|
[1519] | 7 | #include "matharr.h"
|
---|
[705] | 8 | #include "fftpserver.h"
|
---|
[715] | 9 | #include "fftmserver.h"
|
---|
[768] | 10 | #include "fftwserver.h"
|
---|
| 11 | #include "ntoolsinit.h"
|
---|
[460] | 12 |
|
---|
[705] | 13 | #include "timing.h"
|
---|
[460] | 14 |
|
---|
[705] | 15 |
|
---|
[768] | 16 | static bool inp_typ_random = false ; // true -> random input
|
---|
| 17 |
|
---|
[705] | 18 | template <class T>
|
---|
| 19 | inline T module(complex<T> c)
|
---|
[460] | 20 | {
|
---|
[705] | 21 | return (sqrt(c.real()*c.real()+c.imag()*c.imag()));
|
---|
[460] | 22 | }
|
---|
| 23 |
|
---|
[715] | 24 | // Max Matrix print elts
|
---|
| 25 | static int nprt = 2;
|
---|
| 26 | static int nprtfc = 8;
|
---|
| 27 |
|
---|
[705] | 28 | template <class T>
|
---|
[715] | 29 | void TestFFTPack(T seuil, int num)
|
---|
[460] | 30 | {
|
---|
[705] | 31 | int i;
|
---|
| 32 | T fact = 1./num;
|
---|
[460] | 33 |
|
---|
[705] | 34 | TVector< complex<T> > inc(num), bkc(num), difc(num);
|
---|
| 35 | TVector< T > in(num), ino(num), bk(num),dif(num);
|
---|
| 36 | TVector< complex<T> > outc(num);
|
---|
[715] | 37 |
|
---|
| 38 | cout << " DBG/1 outc " << outc.NElts() << endl;
|
---|
| 39 | outc.ReSize(32);
|
---|
| 40 | cout << " DBG/2 outc " << outc.NElts() << endl;
|
---|
| 41 | outc.ReSize(10);
|
---|
| 42 | cout << " DBG/3 outc " << outc.NElts() << endl;
|
---|
| 43 | outc.ReSize(48);
|
---|
| 44 | cout << " DBG/4 outc " << outc.NElts() << endl;
|
---|
| 45 |
|
---|
[768] | 46 | if (inp_typ_random)
|
---|
[1408] | 47 | for (i=0; i<num ; i++){
|
---|
[768] | 48 | ino[i] = in[i] = GauRnd(0., 1.);
|
---|
| 49 | inc[i] = complex<T> (in[i], 0.);
|
---|
| 50 | }
|
---|
[1408] | 51 | else for (i=0; i<num ; i++){
|
---|
[715] | 52 | ino[i] = in[i] = 0.5 + cos(2*M_PI*(double)i/(double)num)
|
---|
| 53 | + 2*sin(4*M_PI*(double)i/(double)num);
|
---|
[705] | 54 | inc[i] = complex<T> (in[i], 0.);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[460] | 57 |
|
---|
[836] | 58 | cout << "Input / L = " << num << in;
|
---|
[460] | 59 | cout << endl;
|
---|
| 60 |
|
---|
[705] | 61 | cout << " >>>> Testing FFTPackServer " << endl;
|
---|
| 62 | FFTPackServer fftp;
|
---|
| 63 | cout << " Testing FFTPackServer " << endl;
|
---|
| 64 | fftp.fftf(in.NElts(), in.Data());
|
---|
[715] | 65 | // in /= (num/2.);
|
---|
[836] | 66 | cout << " fftp.fftf(in.NElts(), in.Data()) FORWARD: " << in << endl;
|
---|
[705] | 67 | cout << endl;
|
---|
| 68 | fftp.fftb(in.NElts(), in.Data());
|
---|
[836] | 69 | cout << " fftp.fftb(in.NElts(), in.Data()) BACKWARD: " << in <<endl;
|
---|
[705] | 70 | cout << endl;
|
---|
[715] | 71 | dif = ino-in;
|
---|
[836] | 72 | cout << " dif , NElts= " << dif.NElts() << dif << endl;
|
---|
[460] | 73 |
|
---|
[715] | 74 | int ndiff = 0;
|
---|
| 75 | T maxdif=0., vdif;
|
---|
| 76 | for(i=0; i<num; i++) {
|
---|
| 77 | vdif = fabs(dif(i));
|
---|
| 78 | if (vdif > seuil) ndiff++;
|
---|
| 79 | if (vdif > maxdif) maxdif = vdif;
|
---|
| 80 | }
|
---|
| 81 | cout << " Difference, Seuil= " << seuil << " NDiff= " << ndiff
|
---|
| 82 | << " MaxDiff= " << maxdif << endl;
|
---|
[705] | 83 |
|
---|
[715] | 84 | }
|
---|
| 85 |
|
---|
| 86 | template <class T>
|
---|
| 87 | void TestFFTS(T seuil, FFTServerInterface & ffts, int num)
|
---|
| 88 | {
|
---|
| 89 |
|
---|
| 90 | cout <<" ===> TestFFTS " << ffts.getInfo() << " ArrSz= " << num << endl;
|
---|
| 91 | int i;
|
---|
| 92 |
|
---|
| 93 | T fact = 1.;
|
---|
| 94 |
|
---|
| 95 | TVector< complex<T> > inc(num), bkc(num), difc(num);
|
---|
| 96 | TVector< T > in(num), ino(num), bk(num),dif(num);
|
---|
| 97 | TVector< complex<T> > outc(num);
|
---|
| 98 |
|
---|
[1408] | 99 | for (i=0; i<num ; i++){
|
---|
[715] | 100 | ino[i] = in[i] = 0.5 + cos(2*M_PI*(double)i/(double)num)
|
---|
| 101 | + 2*sin(4*M_PI*(double)i/(double)num);
|
---|
| 102 | inc[i] = complex<T> (in[i], 0.);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 |
|
---|
| 106 | cout << " Testing FFTServer " << ffts.getInfo() << endl;
|
---|
| 107 |
|
---|
[836] | 108 | cout << "Input / L = " << num << in << endl;
|
---|
[715] | 109 | cout << endl;
|
---|
| 110 |
|
---|
| 111 | int ndiff = 0;
|
---|
| 112 |
|
---|
[705] | 113 | cout << "\n ---- Testing FFT(T, complex<T>) ---- " << endl;
|
---|
| 114 | ffts.FFTForward(in, outc);
|
---|
[836] | 115 | cout << " FourierCoefs , NElts= " << outc.NElts() << outc << endl;
|
---|
[705] | 116 |
|
---|
| 117 | ffts.FFTBackward(outc, bk);
|
---|
[836] | 118 | cout << " Backward , NElts= " << bk.NElts() << bk << endl;
|
---|
[705] | 119 |
|
---|
| 120 | dif = bk*fact - in;
|
---|
[836] | 121 | cout << " Difference , NElts= " << dif.NElts() << dif << endl;
|
---|
[705] | 122 |
|
---|
[715] | 123 | ndiff = 0;
|
---|
| 124 | T maxdif=0., vdif;
|
---|
| 125 | for(i=0; i<num; i++) {
|
---|
| 126 | vdif = fabs(dif(i));
|
---|
| 127 | if (vdif > seuil) ndiff++;
|
---|
| 128 | if (vdif > maxdif) maxdif = vdif;
|
---|
| 129 | }
|
---|
| 130 | cout << " Difference, Seuil= " << seuil << " NDiff= " << ndiff
|
---|
| 131 | << " MaxDiff= " << maxdif << endl;
|
---|
[705] | 132 |
|
---|
| 133 | cout << "\n ---- Testing FFT(complex<T>, complex<T>) ---- " << endl;
|
---|
| 134 | ffts.FFTForward(inc, outc);
|
---|
[836] | 135 | cout << " FourierCoef , NElts= " << outc.NElts() << outc << endl;
|
---|
[705] | 136 |
|
---|
| 137 | ffts.FFTBackward(outc, bkc);
|
---|
[836] | 138 | cout << " Backward , NElts= " << bkc.NElts() << bkc << endl;
|
---|
[705] | 139 |
|
---|
| 140 | difc = bkc*complex<T>(fact,0.) - inc;
|
---|
[836] | 141 | cout << " Difference , NElts= " << difc.NElts() << difc << endl;
|
---|
[705] | 142 |
|
---|
[715] | 143 | ndiff = 0;
|
---|
| 144 | maxdif=0., vdif;
|
---|
| 145 | for(i=0; i<num; i++) {
|
---|
| 146 | vdif = fabs(module(difc(i)));
|
---|
| 147 | if (vdif > seuil) ndiff++;
|
---|
| 148 | if (vdif > maxdif) maxdif = vdif;
|
---|
| 149 | }
|
---|
| 150 | cout << " Difference, Seuil= " << seuil << " NDiff= " << ndiff
|
---|
| 151 | << " MaxDiff= " << maxdif << endl;
|
---|
[460] | 152 | }
|
---|
[705] | 153 |
|
---|
| 154 |
|
---|
[715] | 155 |
|
---|
| 156 |
|
---|
| 157 |
|
---|
| 158 | int main(int narg, char* arg[])
|
---|
[705] | 159 | {
|
---|
| 160 |
|
---|
[768] | 161 | SophyaInit();
|
---|
[705] | 162 | InitTim(); // Initializing the CPU timer
|
---|
| 163 |
|
---|
[715] | 164 | if (narg < 4) {
|
---|
[836] | 165 | cout << "tfft/ args error - \n Usage tfft size px/Px/Mx/Wx f/d [NPrt=50 PrtLev=0] \n"
|
---|
[768] | 166 | << " p=FFTPackTest P=FFTPack, M=FFTMayer, W= FFTWServer"
|
---|
| 167 | << " x=0 -> Random input - f=float, d=double " << endl;
|
---|
[715] | 168 | exit(0);
|
---|
| 169 | }
|
---|
[705] | 170 |
|
---|
[715] | 171 | FFTPackServer fftp;
|
---|
| 172 | FFTMayerServer fftm;
|
---|
| 173 |
|
---|
[768] | 174 | FFTWServer fftw;
|
---|
| 175 |
|
---|
| 176 | inp_typ_random = false;
|
---|
| 177 | if (arg[2][1] == '0') inp_typ_random = true;
|
---|
| 178 |
|
---|
[715] | 179 | int sz = atoi(arg[1]);
|
---|
[836] | 180 | int nprt = 50;
|
---|
| 181 | int prtlev = 0;
|
---|
| 182 | if (narg > 4) nprt = atoi(arg[4]);
|
---|
| 183 | if (narg > 5) prtlev = atoi(arg[5]);
|
---|
| 184 | BaseArray::SetMaxPrint(nprt, prtlev);
|
---|
[715] | 185 |
|
---|
| 186 | if (sz < 2) sz = 2;
|
---|
| 187 | FFTServerInterface * ffts;
|
---|
| 188 | if (*arg[2] == 'M') ffts = fftm.Clone();
|
---|
[768] | 189 | else if (*arg[2] == 'W') ffts = fftw.Clone();
|
---|
[715] | 190 | else ffts = fftp.Clone();
|
---|
| 191 |
|
---|
| 192 | float fs = 1.e-4;
|
---|
| 193 | double ds = 1.e-6;
|
---|
| 194 |
|
---|
[705] | 195 | try {
|
---|
[715] | 196 | if (*arg[3] == 'd') {
|
---|
| 197 | cout << "\n ========================================== \n"
|
---|
| 198 | << " ------ Testing FFTServer for double ----- \n"
|
---|
| 199 | << " ============================================ " << endl;
|
---|
| 200 | if (*arg[2] == 'p') TestFFTPack(ds, sz);
|
---|
| 201 | else TestFFTS(ds, *ffts, sz);
|
---|
| 202 | }
|
---|
| 203 | else {
|
---|
| 204 | cout << "\n ========================================= \n"
|
---|
| 205 | << " ------ Testing FFTServer for float ----- \n"
|
---|
| 206 | << " =========================================== " << endl;
|
---|
| 207 | if (*arg[2] == 'p') TestFFTPack(fs, sz);
|
---|
| 208 | else TestFFTS(fs, *ffts, sz);
|
---|
| 209 | }
|
---|
[705] | 210 | }
|
---|
| 211 | catch(PThrowable exc ) {
|
---|
[715] | 212 | cerr << "TestFFT-main() , Catched exception: \n" << exc.Msg() << endl;
|
---|
[705] | 213 | }
|
---|
| 214 | catch(std::exception ex) {
|
---|
| 215 | cerr << "TestFFT-main() , Catched exception ! " << (string)(ex.what()) << endl;
|
---|
| 216 | }
|
---|
[715] | 217 |
|
---|
[705] | 218 | /*
|
---|
| 219 | catch(...) {
|
---|
| 220 | cerr << "TestFFT-main() , Catched ... exception ! " << endl;
|
---|
| 221 | }
|
---|
| 222 | */
|
---|
| 223 | PrtTim("End of tfft ");
|
---|
[715] | 224 | delete ffts;
|
---|
[705] | 225 | }
|
---|