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

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

Reorganisation - PeidaInit() -> SophyaInit() - Reza 2/3/2000

File size: 5.9 KB
Line 
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
15static bool inp_typ_random = false ; // true -> random input
16
17template <class T>
18inline T module(complex<T> c)
19{
20 return (sqrt(c.real()*c.real()+c.imag()*c.imag()));
21}
22
23// Max Matrix print elts
24static int nprt = 2;
25static int nprtfc = 8;
26
27template <class T>
28void 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 << endl;
58 in.Print(0,0,nprt,0,nprt);
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: " << endl;
67 in.Print(0,0,nprtfc,0,nprtfc);
68 cout << endl;
69 fftp.fftb(in.NElts(), in.Data());
70 cout << " fftp.fftb(in.NElts(), in.Data()) BACKWARD: " << endl;
71 in.Print(0,0,nprt,0,nprt);
72 cout << endl;
73 dif = ino-in;
74 cout << " dif , NElts= " << dif.NElts() << endl;
75 dif.Print(0,0,nprt,0,nprt);
76
77 int ndiff = 0;
78 T maxdif=0., vdif;
79 for(i=0; i<num; i++) {
80 vdif = fabs(dif(i));
81 if (vdif > seuil) ndiff++;
82 if (vdif > maxdif) maxdif = vdif;
83 }
84 cout << " Difference, Seuil= " << seuil << " NDiff= " << ndiff
85 << " MaxDiff= " << maxdif << endl;
86
87}
88
89template <class T>
90void TestFFTS(T seuil, FFTServerInterface & ffts, int num)
91{
92
93 cout <<" ===> TestFFTS " << ffts.getInfo() << " ArrSz= " << num << endl;
94 int i;
95
96 T fact = 1.;
97
98 TVector< complex<T> > inc(num), bkc(num), difc(num);
99 TVector< T > in(num), ino(num), bk(num),dif(num);
100 TVector< complex<T> > outc(num);
101
102 for (int i=0; i<num ; i++){
103 ino[i] = in[i] = 0.5 + cos(2*M_PI*(double)i/(double)num)
104 + 2*sin(4*M_PI*(double)i/(double)num);
105 inc[i] = complex<T> (in[i], 0.);
106 }
107
108
109 cout << " Testing FFTServer " << ffts.getInfo() << endl;
110
111 cout << "Input / L = " << num << endl;
112 in.Print(0,0,nprt,0,nprt);
113 cout << endl;
114
115 int ndiff = 0;
116
117 cout << "\n ---- Testing FFT(T, complex<T>) ---- " << endl;
118 ffts.FFTForward(in, outc);
119 cout << " FourierCoefs , NElts= " << outc.NElts() << endl;
120 outc.Print(0,0,nprtfc,0,nprtfc);
121
122 ffts.FFTBackward(outc, bk);
123 cout << " Backward , NElts= " << bk.NElts() << endl;
124 bk.Print(0,0,nprt,0,nprt);
125
126 dif = bk*fact - in;
127 cout << " Difference , NElts= " << dif.NElts() << endl;
128 dif.Print(0,0,nprt,0,nprt);
129
130 ndiff = 0;
131 T maxdif=0., vdif;
132 for(i=0; i<num; i++) {
133 vdif = fabs(dif(i));
134 if (vdif > seuil) ndiff++;
135 if (vdif > maxdif) maxdif = vdif;
136 }
137 cout << " Difference, Seuil= " << seuil << " NDiff= " << ndiff
138 << " MaxDiff= " << maxdif << endl;
139
140 cout << "\n ---- Testing FFT(complex<T>, complex<T>) ---- " << endl;
141 ffts.FFTForward(inc, outc);
142 cout << " FourierCoef , NElts= " << outc.NElts() << endl;
143 outc.Print(0,0,nprtfc,0,nprtfc);
144
145 ffts.FFTBackward(outc, bkc);
146 cout << " Backward , NElts= " << bkc.NElts() << endl;
147 bkc.Print(0,0,nprt,0,nprt);
148
149 difc = bkc*complex<T>(fact,0.) - inc;
150 cout << " Difference , NElts= " << difc.NElts() << endl;
151 difc.Print(0,0,nprt,0,nprt);
152
153 ndiff = 0;
154 maxdif=0., vdif;
155 for(i=0; i<num; i++) {
156 vdif = fabs(module(difc(i)));
157 if (vdif > seuil) ndiff++;
158 if (vdif > maxdif) maxdif = vdif;
159 }
160 cout << " Difference, Seuil= " << seuil << " NDiff= " << ndiff
161 << " MaxDiff= " << maxdif << endl;
162}
163
164
165
166
167
168int main(int narg, char* arg[])
169{
170
171 SophyaInit();
172 InitTim(); // Initializing the CPU timer
173
174 if (narg < 4) {
175 cout << "tfft/ args error - \n Usage tfft size px/Px/Mx/Wx f/d [NPrtFC NPrt] \n"
176 << " p=FFTPackTest P=FFTPack, M=FFTMayer, W= FFTWServer"
177 << " x=0 -> Random input - f=float, d=double " << endl;
178 exit(0);
179 }
180
181 FFTPackServer fftp;
182 FFTMayerServer fftm;
183
184 FFTWServer fftw;
185
186 inp_typ_random = false;
187 if (arg[2][1] == '0') inp_typ_random = true;
188
189 int sz = atoi(arg[1]);
190 if (narg > 4) nprtfc = atoi(arg[4]);
191 if (narg > 5) nprt = atoi(arg[4]);
192
193 if (sz < 2) sz = 2;
194 FFTServerInterface * ffts;
195 if (*arg[2] == 'M') ffts = fftm.Clone();
196 else if (*arg[2] == 'W') ffts = fftw.Clone();
197 else ffts = fftp.Clone();
198
199 float fs = 1.e-4;
200 double ds = 1.e-6;
201
202 try {
203 if (*arg[3] == 'd') {
204 cout << "\n ========================================== \n"
205 << " ------ Testing FFTServer for double ----- \n"
206 << " ============================================ " << endl;
207 if (*arg[2] == 'p') TestFFTPack(ds, sz);
208 else TestFFTS(ds, *ffts, sz);
209 }
210 else {
211 cout << "\n ========================================= \n"
212 << " ------ Testing FFTServer for float ----- \n"
213 << " =========================================== " << endl;
214 if (*arg[2] == 'p') TestFFTPack(fs, sz);
215 else TestFFTS(fs, *ffts, sz);
216 }
217 }
218 catch(PThrowable exc ) {
219 cerr << "TestFFT-main() , Catched exception: \n" << exc.Msg() << endl;
220 }
221 catch(std::exception ex) {
222 cerr << "TestFFT-main() , Catched exception ! " << (string)(ex.what()) << endl;
223 }
224
225 /*
226 catch(...) {
227 cerr << "TestFFT-main() , Catched ... exception ! " << endl;
228 }
229 */
230 PrtTim("End of tfft ");
231 delete ffts;
232}
Note: See TracBrowser for help on using the repository browser.