Debian Codegraph @

School05/Monday/coef_cheb_fft.C

raw
1 /*
2 * Chebyshev transform via a FFT
3 *
4 */
5
6 /*
7 * Copyright (c) 2005 Eric Gourgoulhon & Jerome Novak
8 *
9 * This file is part of LORENE.
10 *
11 * LORENE is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * LORENE is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with LORENE; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
27 /*
28 * $Id: coef_cheb_fft.C,v 1.3 2014/10/06 15:09:47 j_novak Exp $
29 * $Log: coef_cheb_fft.C,v $
30 * Revision 1.3 2014/10/06 15:09:47 j_novak
31 * Modified #include directives to use c++ syntax.
32 *
33 * Revision 1.2 2005/11/14 14:12:10 e_gourgoulhon
34 * Added include <assert.h>
35 *
36 * Revision 1.1 2005/11/14 01:56:58 e_gourgoulhon
37 * First version
38 *
39 *
40 * $Header: /cvsroot/Lorene/School05/Monday/coef_cheb_fft.C,v 1.3 2014/10/06 15:09:47 j_novak Exp $
41 *
42 */
43
44
45 #include <iostream>
46
47 using namespace std ;
48
49 #include <cstdlib>
50 #include <cmath>
51 #include <cassert>
52 #include <fftw3.h>
53
54 fftw_plan prepare_fft(int n, double*& pg) {
55
56 static const int nmax = 50 ; //Maximal number of FFT sizes
57 static int nworked = 0 ;
58 static double* tab_tab[nmax] ;
59 static fftw_plan plan_fft[nmax] ;
60 static int nb_fft[nmax] ;
61
62 int index = -1 ;
63 for (int i=0; ((i<nworked) && (index<0)); i++)
64 if (nb_fft[i] == n) index = i ; //Has the plan already been estimated?
65
66 if (index <0) { //New plan needed
67 index = nworked ;
68 if (index >= nmax) {
69 cout << "prepare_fft: " << endl ;
70 cout << "too many plans!" << endl ;
71 abort() ;
72 }
73
74 double* tab = new double[n] ;
75 tab_tab[index] = tab ;
76
77 plan_fft[index] =
78 fftw_plan_r2r_1d(n, tab, tab, FFTW_R2HC, FFTW_MEASURE) ;
79
80 nb_fft[index] = n ;
81 nworked++ ;
82 }
83 assert((index>=0)&&(index<nmax)) ;
84 pg = tab_tab[index] ;
85 return plan_fft[index] ;
86 }
87
88
89 double* cheb_ini(int n) {
90
91 const int nmax = 30 ; /* Nombre maximun de dimensions differentes */
92 static double* table_sin[nmax] ; /* Tableau des pointeurs sur les tableaux */
93 static int nwork = 0 ; /* Nombre de tableaux deja initialises */
94 static int tbn[nmax] ; /* Tableau des points deja initialises */
95
96 // Ce nombre de points a-t-il deja ete utilise ?
97 int indice = -1 ;
98 for (int i=0 ; i < nwork ; i++ ) {
99 if ( tbn[i] == n ) indice = i ;
100 }
101
102 // Initialisation
103 if (indice == -1) { /* Il faut une nouvelle initialisation */
104 if ( nwork >= nmax ) {
105 cout << "cheb_ini : nwork >= nmax !" << endl ;
106 abort() ;
107 }
108 indice = nwork ; nwork++ ; tbn[indice] = n ;
109
110 int nm1s2 = (n-1) / 2 ;
111 table_sin[indice] = new double[nm1s2] ;
112
113 double xx = M_PI / double(n-1);
114 for (int i = 0; i < nm1s2 ; i++ ) {
115 table_sin[indice][i] = sin( xx * i );
116 }
117 }
118
119 return table_sin[indice] ;
120 }
121
122
123 void coef_cheb_fft(int nr, const double* ff, double* cf) {
124
125 // Nombre de points pour la FFT:
126 int nm1 = nr - 1;
127 int nm1s2 = nm1 / 2;
128
129 // Recherche des tables pour la FFT:
130 double* pg = 0x0 ;
131 fftw_plan p = prepare_fft(nm1, pg) ;
132 double* g = pg ;
133
134 // Recherche de la table des sin(psi) :
135 double* sinp = cheb_ini(nr);
136
137 /*
138 * NB: dans les commentaires qui suivent, psi designe la variable de [0, pi]
139 * reliee a x par x = - cos(psi) et F(psi) = f(x(psi)).
140 */
141
142 // Valeur en psi=0 de la partie antisymetrique de F, F_ :
143 double fmoins0 = 0.5 * ( ff[0] - ff[nm1] );
144
145 // Fonction G(psi) = F+(psi) + F_(psi) sin(psi)
146 //---------------------------------------------
147 for (int i = 1; i < nm1s2 ; i++ ) {
148 // ... indice du pt symetrique de psi par rapport a pi/2:
149 int isym = nm1 - i ;
150 // ... F+(psi)
151 double fp = 0.5 * ( ff[i] + ff[isym] ) ;
152 // ... F_(psi) sin(psi)
153 double fms = 0.5 * ( ff[i] - ff[isym] ) * sinp[i] ;
154 g[i] = fp + fms ;
155 g[isym] = fp - fms ;
156 }
157 //... cas particuliers:
158 g[0] = 0.5 * ( ff[0] + ff[nm1] );
159 g[nm1s2] = ff[nm1s2];
160
161 // Developpement de G en series de Fourier par une FFT
162 //----------------------------------------------------
163
164 fftw_execute(p) ;
165
166 // Coefficients pairs du developmt. de Tchebyshev de f
167 //----------------------------------------------------
168 // Ces coefficients sont egaux aux coefficients en cosinus du developpement
169 // de G en series de Fourier (le facteur 2/nm1 vient de la normalisation
170 // de fftw) :
171
172 double fac = 2./double(nm1) ;
173 cf[0] = g[0] / double(nm1) ;
174 for (int i=2; i<nm1; i += 2) cf[i] = fac*g[i/2] ;
175 cf[nm1] = g[nm1s2] / double(nm1) ;
176
177 // Coefficients impairs du developmt. de Tchebyshev de f
178 //------------------------------------------------------
179 // 1. Coef. c'_k (recurrence amorcee a partir de zero):
180 // NB: Le 4/nm1 en facteur de g(i) est du a la normalisation de fftw
181 // (si fftw donnait reellement les coef. en sinus, il faudrait le
182 // remplacer par un +2.)
183 fac *= -2. ;
184 cf[1] = 0 ;
185 double som = 0;
186 for (int i = 3; i < nr; i += 2 ) {
187 cf[i] = cf[i-2] + fac * g[nm1-i/2] ;
188 som += cf[i] ;
189 }
190
191 // 2. Calcul de c_1 :
192 double c1 = - ( fmoins0 + som ) / nm1s2 ;
193
194 // 3. Coef. c_k avec k impair:
195 cf[1] = c1 ;
196 for (int i = 3; i < nr; i += 2 ) cf[i] += c1 ;
197
198 }