Debian Codegraph @

School05/Tuesday/sol_galerkin.C

raw
1 #include "headcpp.h"
2 #include "math.h"
3
4 #include "tbl.h"
5 #include "matrice.h"
6 #include "cheby.h"
7
8 double f_source (double x) {
9 static double constante = -4*exp(1.)/(1+exp(1.)*exp(1.)) ;
10 return constante + exp(x) ;
11 }
12
13 double f_solution (double x) {
14 static double constante = -4*exp(1.)/(1+exp(1.)*exp(1.)) ;
15 return exp(x) - sinh(1.)/sinh(2.)*exp(2*x) + constante/4 ;
16 }
17 //*******************
18 // First derivative
19 //*******************
20 Tbl ope_der (const Tbl& so) {
21 // Verification of size :
22 assert (so.get_ndim() == 1) ;
23 int size = so.get_dim(0) ;
24
25 // The result is set to zero
26 Tbl res (size) ;
27 res.annule_hard() ;
28
29 // the computation
30 for (int n=0 ; n<size ; n++)
31 for (int p=n+1 ; p<size ; p++)
32 if ((p+n)%2 == 1)
33 res.set(n) += p*so(p)*2 ;
34
35 // Normalisation of the first coef :
36 res.set(0) /= 2. ;
37
38 return res ;
39 }
40 //*******************
41 // Second derivative
42 //*******************
43 Tbl ope_der_sec (const Tbl& so) {
44 // Verification of size :
45 assert (so.get_ndim() == 1) ;
46 int size = so.get_dim(0) ;
47
48 // The result is set to zero
49 Tbl res (size) ;
50 res.annule_hard() ;
51
52 // the computation
53 for (int n=0 ; n<size ; n++)
54 for (int p=n+2 ; p<size ; p++)
55 if ((p+n)%2 == 0)
56 res.set(n) += p*(p*p-n*n)*so(p) ;
57
58 // Normalisation of the first coef :
59 res.set(0) /= 2. ;
60
61 return res ;
62 }
63
64 //*******************
65 // The operator
66 //*******************
67
68 Matrice matrix_ope (int n) {
69
70 // The result :
71 Matrice res(n,n) ;
72 res.set_etat_qcq() ;
73
74 // Work arrays :
75 Tbl so (n) ;
76 Tbl d_so (n) ;
77 Tbl dd_so (n) ;
78
79 // Column by column :
80 for (int col=0 ; col<n ; col++) {
81 so.annule_hard() ;
82 so.set(col) = 1 ;
83
84 // The derivatives
85 d_so = ope_der(so) ;
86 dd_so = ope_der_sec(so) ;
87
88 // Put in the matrix :
89 for (int line=0 ; line<n ; line++)
90 res.set(line, col) = dd_so(line) -4*d_so(line) + 4*so(line) ;
91 }
92
93 return res ;
94 }
95
96 int main () {
97
98 int nr ;
99 cout << "Please enter the number of coefficients :" << endl ;
100 cin >> nr ;
101
102 cout << "The operator matrix is " << endl ;
103 Matrice operator_mat (matrix_ope(nr)) ;
104 cout << operator_mat << endl ;
105
106 // Transformation matrix :
107 Matrice transfo (nr, nr-2) ;
108 transfo.annule_hard() ;
109 // Terms in T_0 and T_1 :
110 for (int i=0 ; i<nr-2 ; i++)
111 if (i%2==0)
112 transfo.set(0,i) = -1 ;
113 else
114 transfo.set(1,i) = -1 ;
115 // The rest is just identity :
116 for (int i=0 ; i<nr-2 ; i++)
117 transfo.set(i+2,i) = 1 ;
118
119 cout << "The transformation matrix :" << endl ;
120 cout << transfo << endl ;
121
122 // Resolution with a Galerkin method :
123 Matrice systeme(nr-2, nr-2) ;
124 systeme.annule_hard() ;
125 // The equations :
126 for (int n=0 ; n<nr-2 ; n++)
127 for (int k=0 ; k<nr-2 ; k++)
128 for (int i=0 ; i<nr ; i++)
129 for (int j=0 ; j<nr ; j++)
130 systeme.set(n,k) += transfo(i,n)*transfo(j,k)*operator_mat(i,j)*norme_cheb(i) ;
131
132 systeme.set_lu() ;
133
134 cout << "The Galerkin system is" << endl ;
135 cout << systeme << endl ;
136
137 // Coeficients of the source :
138 Tbl conf (nr) ;
139 conf.set_etat_qcq() ;
140 Tbl colocation(coloc_cheb(nr)) ;
141 for (int i=0 ; i<nr ; i++)
142 conf.set(i) = f_source(colocation(i)) ;
143 Tbl coefs (coef_cheb(conf)) ;
144
145 cout << "Coefficients of the source" << endl ;
146 cout << coefs << endl ;
147
148 // Second member of the system :
149 Tbl sec_member (nr-2) ;
150 sec_member.annule_hard() ;
151 for (int n=0 ; n<nr-2 ; n++)
152 for (int i=0 ; i<nr ; i++)
153 sec_member.set(n) += transfo(i,n)*coefs(i)*norme_cheb(i) ;
154
155 cout << "Second member for the colocation system" << endl ;
156 cout << sec_member << endl ;
157
158 // The system is inverted :
159 Tbl coef_galerkin (systeme.inverse(sec_member)) ;
160
161 cout << "Coefficients of the solution in the Galerkin basis : " << endl ;
162 cout << coef_galerkin << endl ;
163
164 Tbl coef_sol(nr) ;
165 coef_sol.annule_hard() ;
166 for (int k=0 ; k<nr ; k++)
167 for (int n=0 ; n<nr-2 ; n++)
168 coef_sol.set(k) += transfo(k,n)*coef_galerkin(n) ;
169
170 cout << "Coefficients of the solution in terms of Chebyshev :" << endl ;
171 cout << coef_sol << endl ;
172
173 // Output in a file for plotting purposes :
174 char name_out[30] ;
175 sprintf(name_out, "plot_galerkin_%i.dat", nr) ;
176 ofstream fiche (name_out) ;
177
178 int resolution = 200 ;
179 double x=-1 ;
180 double step = 2./resolution ;
181
182 // We will also compute the maximum difference between the solution and its numerical value :
183 double error_max = 0 ;
184
185 for (int i=0 ; i<resolution+1 ; i++) {
186 // One computes the solution at the current point
187 double val_func = 0 ;
188 for (int j=0 ; j<nr ; j++)
189 val_func += coef_sol(j)*cheby(j, x) ;
190 fiche << x << " " << val_func << " " << f_solution(x) << endl ;
191 double error = fabs(val_func-f_solution(x)) ;
192 if (error > error_max)
193 error_max = error ;
194
195 x += step ;
196 }
197
198 cout << "Error max : " << endl ;
199 cout << error_max << endl ;
200
201 return 0 ;
202 }