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