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