source: Sophya/trunk/SophyaProg/Tests/tfft.cc@ 715

Last change on this file since 715 was 715, checked in by ansari, 26 years ago

amelioration programme test FFT, Reza 5/2/99

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