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
Line 
1#include <math.h>
2#include <iostream.h>
3
4#include "fftpserver.h"
5#include "fftmserver.h"
6#include "sambainit.h"
7
8#include "timing.h"
9
10
11template <class T>
12inline T module(complex<T> c)
13{
14 return (sqrt(c.real()*c.real()+c.imag()*c.imag()));
15}
16
17// Max Matrix print elts
18static int nprt = 2;
19static int nprtfc = 8;
20
21template <class T>
22void TestFFTPack(T seuil, int num)
23{
24 int i;
25 T fact = 1./num;
26
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);
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
39 for (int i=0; i<num ; i++){
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);
42 inc[i] = complex<T> (in[i], 0.);
43 }
44
45
46 cout << "Input / L = " << num << endl;
47 in.Print(0,0,nprt,0,nprt);
48 cout << endl;
49
50 cout << " >>>> Testing FFTPackServer " << endl;
51 FFTPackServer fftp;
52 cout << " Testing FFTPackServer " << endl;
53 fftp.fftf(in.NElts(), in.Data());
54 // in /= (num/2.);
55 cout << " fftp.fftf(in.NElts(), in.Data()) FORWARD: " << endl;
56 in.Print(0,0,nprtfc,0,nprtfc);
57 cout << endl;
58 fftp.fftb(in.NElts(), in.Data());
59 cout << " fftp.fftb(in.NElts(), in.Data()) BACKWARD: " << endl;
60 in.Print(0,0,nprt,0,nprt);
61 cout << endl;
62 dif = ino-in;
63 cout << " dif , NElts= " << dif.NElts() << endl;
64 dif.Print(0,0,nprt,0,nprt);
65
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;
75
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
106 cout << "\n ---- Testing FFT(T, complex<T>) ---- " << endl;
107 ffts.FFTForward(in, outc);
108 cout << " FourierCoefs , NElts= " << outc.NElts() << endl;
109 outc.Print(0,0,nprtfc,0,nprtfc);
110
111 ffts.FFTBackward(outc, bk);
112 cout << " Backward , NElts= " << bk.NElts() << endl;
113 bk.Print(0,0,nprt,0,nprt);
114
115 dif = bk*fact - in;
116 cout << " Difference , NElts= " << dif.NElts() << endl;
117 dif.Print(0,0,nprt,0,nprt);
118
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;
128
129 cout << "\n ---- Testing FFT(complex<T>, complex<T>) ---- " << endl;
130 ffts.FFTForward(inc, outc);
131 cout << " FourierCoef , NElts= " << outc.NElts() << endl;
132 outc.Print(0,0,nprtfc,0,nprtfc);
133
134 ffts.FFTBackward(outc, bkc);
135 cout << " Backward , NElts= " << bkc.NElts() << endl;
136 bkc.Print(0,0,nprt,0,nprt);
137
138 difc = bkc*complex<T>(fact,0.) - inc;
139 cout << " Difference , NElts= " << difc.NElts() << endl;
140 difc.Print(0,0,nprt,0,nprt);
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
157int main(int narg, char* arg[])
158{
159
160 PeidaInit();
161 InitTim(); // Initializing the CPU timer
162
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 }
168
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
184 try {
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 }
199 }
200 catch(PThrowable exc ) {
201 cerr << "TestFFT-main() , Catched exception: \n" << exc.Msg() << endl;
202 }
203 catch(std::exception ex) {
204 cerr << "TestFFT-main() , Catched exception ! " << (string)(ex.what()) << endl;
205 }
206
207 /*
208 catch(...) {
209 cerr << "TestFFT-main() , Catched ... exception ! " << endl;
210 }
211 */
212 PrtTim("End of tfft ");
213 delete ffts;
214}
Note: See TracBrowser for help on using the repository browser.