| 1 | #include "machdefs.h"
 | 
|---|
| 2 | 
 | 
|---|
| 3 | #include <math.h>
 | 
|---|
| 4 | #include <iostream.h>
 | 
|---|
| 5 | 
 | 
|---|
| 6 | #include "nbrandom.h"
 | 
|---|
| 7 | #include "matharr.h"
 | 
|---|
| 8 | #include "fftpserver.h"
 | 
|---|
| 9 | #include "fftmserver.h"
 | 
|---|
| 10 | #include "fftwserver.h"
 | 
|---|
| 11 | #include "ntoolsinit.h"
 | 
|---|
| 12 | 
 | 
|---|
| 13 | #include "timing.h"
 | 
|---|
| 14 | 
 | 
|---|
| 15 | 
 | 
|---|
| 16 | static bool inp_typ_random = false ;  // true -> random input
 | 
|---|
| 17 | 
 | 
|---|
| 18 | template <class T>
 | 
|---|
| 19 | inline T module(complex<T> c) 
 | 
|---|
| 20 | {
 | 
|---|
| 21 |   return (sqrt(c.real()*c.real()+c.imag()*c.imag()));
 | 
|---|
| 22 | }
 | 
|---|
| 23 | 
 | 
|---|
| 24 | // Max Matrix print elts
 | 
|---|
| 25 | static int nprt = 2;
 | 
|---|
| 26 | static int nprtfc = 8;
 | 
|---|
| 27 | 
 | 
|---|
| 28 | template <class T>
 | 
|---|
| 29 | void TestFFTPack(T seuil, int num)
 | 
|---|
| 30 | {
 | 
|---|
| 31 |   int i;
 | 
|---|
| 32 |   T fact = 1./num;
 | 
|---|
| 33 | 
 | 
|---|
| 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);
 | 
|---|
| 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 | 
 | 
|---|
| 46 |   if (inp_typ_random) 
 | 
|---|
| 47 |     for (i=0; i<num ; i++){
 | 
|---|
| 48 |       ino[i] = in[i] = GauRnd(0., 1.);
 | 
|---|
| 49 |       inc[i] = complex<T> (in[i], 0.);
 | 
|---|
| 50 |     }
 | 
|---|
| 51 |   else for (i=0; i<num ; i++){
 | 
|---|
| 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);
 | 
|---|
| 54 |     inc[i] = complex<T> (in[i], 0.);
 | 
|---|
| 55 |   }
 | 
|---|
| 56 |   
 | 
|---|
| 57 | 
 | 
|---|
| 58 |   cout << "Input / L = " << num << in; 
 | 
|---|
| 59 |   cout << endl;
 | 
|---|
| 60 | 
 | 
|---|
| 61 |   cout << " >>>> Testing FFTPackServer "  << endl;
 | 
|---|
| 62 |   FFTPackServer fftp;
 | 
|---|
| 63 |   cout << " Testing FFTPackServer "  << endl;
 | 
|---|
| 64 |   fftp.fftf(in.NElts(), in.Data());
 | 
|---|
| 65 |   //  in /= (num/2.);
 | 
|---|
| 66 |   cout << " fftp.fftf(in.NElts(), in.Data()) FORWARD: " << in << endl; 
 | 
|---|
| 67 |   cout << endl;
 | 
|---|
| 68 |   fftp.fftb(in.NElts(), in.Data());
 | 
|---|
| 69 |   cout << " fftp.fftb(in.NElts(), in.Data()) BACKWARD: " << in <<endl; 
 | 
|---|
| 70 |   cout << endl;
 | 
|---|
| 71 |   dif = ino-in;
 | 
|---|
| 72 |   cout << " dif , NElts= " << dif.NElts() << dif << endl;
 | 
|---|
| 73 | 
 | 
|---|
| 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;
 | 
|---|
| 83 | 
 | 
|---|
| 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 | 
 | 
|---|
| 99 |   for (i=0; i<num ; i++){
 | 
|---|
| 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 | 
 | 
|---|
| 108 |   cout << "Input / L = " << num << in << endl; 
 | 
|---|
| 109 |   cout << endl;
 | 
|---|
| 110 | 
 | 
|---|
| 111 |   int ndiff = 0;
 | 
|---|
| 112 | 
 | 
|---|
| 113 |   cout << "\n ----  Testing FFT(T, complex<T>) ---- " << endl;
 | 
|---|
| 114 |   ffts.FFTForward(in, outc);
 | 
|---|
| 115 |   cout << " FourierCoefs , NElts= " << outc.NElts() << outc << endl;
 | 
|---|
| 116 | 
 | 
|---|
| 117 |   ffts.FFTBackward(outc, bk);
 | 
|---|
| 118 |   cout << " Backward , NElts= " << bk.NElts() << bk << endl;
 | 
|---|
| 119 |  
 | 
|---|
| 120 |   dif = bk*fact - in;
 | 
|---|
| 121 |   cout << " Difference , NElts= " << dif.NElts() << dif << endl;
 | 
|---|
| 122 | 
 | 
|---|
| 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;
 | 
|---|
| 132 |  
 | 
|---|
| 133 |   cout << "\n ----  Testing FFT(complex<T>, complex<T>) ---- " << endl;
 | 
|---|
| 134 |   ffts.FFTForward(inc, outc);
 | 
|---|
| 135 |   cout << " FourierCoef , NElts= " << outc.NElts() << outc << endl;
 | 
|---|
| 136 | 
 | 
|---|
| 137 |   ffts.FFTBackward(outc, bkc);
 | 
|---|
| 138 |   cout << " Backward , NElts= " << bkc.NElts() << bkc << endl;
 | 
|---|
| 139 |  
 | 
|---|
| 140 |   difc = bkc*complex<T>(fact,0.) - inc;
 | 
|---|
| 141 |   cout << " Difference , NElts= " << difc.NElts() << difc << endl;
 | 
|---|
| 142 | 
 | 
|---|
| 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;
 | 
|---|
| 152 | }
 | 
|---|
| 153 | 
 | 
|---|
| 154 | 
 | 
|---|
| 155 | 
 | 
|---|
| 156 | 
 | 
|---|
| 157 | 
 | 
|---|
| 158 | int main(int narg, char* arg[])
 | 
|---|
| 159 | {
 | 
|---|
| 160 | 
 | 
|---|
| 161 |   SophyaInit();
 | 
|---|
| 162 |   InitTim();   // Initializing the CPU timer
 | 
|---|
| 163 | 
 | 
|---|
| 164 |   if (narg < 4) {
 | 
|---|
| 165 |     cout << "tfft/ args error - \n  Usage tfft size px/Px/Mx/Wx f/d [NPrt=50 PrtLev=0] \n" 
 | 
|---|
| 166 |          << " p=FFTPackTest  P=FFTPack, M=FFTMayer, W= FFTWServer" 
 | 
|---|
| 167 |          << " x=0 -> Random input - f=float, d=double " << endl;
 | 
|---|
| 168 |     exit(0);
 | 
|---|
| 169 |   }
 | 
|---|
| 170 | 
 | 
|---|
| 171 |   FFTPackServer fftp;
 | 
|---|
| 172 |   FFTMayerServer fftm;
 | 
|---|
| 173 | 
 | 
|---|
| 174 |   FFTWServer fftw;
 | 
|---|
| 175 | 
 | 
|---|
| 176 |   inp_typ_random = false;
 | 
|---|
| 177 |   if (arg[2][1] == '0')  inp_typ_random = true;
 | 
|---|
| 178 | 
 | 
|---|
| 179 |   int sz = atoi(arg[1]);
 | 
|---|
| 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);
 | 
|---|
| 185 | 
 | 
|---|
| 186 |   if (sz < 2) sz = 2;
 | 
|---|
| 187 |   FFTServerInterface * ffts;
 | 
|---|
| 188 |   if (*arg[2] == 'M')  ffts = fftm.Clone();
 | 
|---|
| 189 |   else if (*arg[2] == 'W')  ffts = fftw.Clone();
 | 
|---|
| 190 |   else ffts = fftp.Clone();
 | 
|---|
| 191 | 
 | 
|---|
| 192 |   float fs = 1.e-4;
 | 
|---|
| 193 |   double ds = 1.e-6;
 | 
|---|
| 194 | 
 | 
|---|
| 195 |   try {
 | 
|---|
| 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 |     }
 | 
|---|
| 210 |   }
 | 
|---|
| 211 |   catch(PThrowable exc ) {
 | 
|---|
| 212 |     cerr << "TestFFT-main() , Catched exception: \n" << exc.Msg() << endl;
 | 
|---|
| 213 |   }
 | 
|---|
| 214 |   catch(std::exception ex) {
 | 
|---|
| 215 |     cerr << "TestFFT-main() , Catched exception ! " << (string)(ex.what()) << endl;
 | 
|---|
| 216 |   }
 | 
|---|
| 217 | 
 | 
|---|
| 218 |   /*
 | 
|---|
| 219 |   catch(...) {
 | 
|---|
| 220 |     cerr << "TestFFT-main() , Catched ... exception ! " << endl;
 | 
|---|
| 221 |   }
 | 
|---|
| 222 |   */
 | 
|---|
| 223 |   PrtTim("End of tfft ");
 | 
|---|
| 224 |   delete ffts;
 | 
|---|
| 225 | }
 | 
|---|