Debian Codegraph @

School05/Tuesday/sol_tau.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 //*******************
19 // First derivative
20 //*******************
21 Tbl ope_der (const Tbl& so) {
22 // Verification of size :
23 assert (so.get_ndim() == 1) ;
24 int size = so.get_dim(0) ;
25
26 // The result is set to zero
27 Tbl res (size) ;
28 res.annule_hard() ;
29
30 // the computation
31 for (int n=0 ; n<size ; n++)
32 for (int p=n+1 ; p<size ; p++)
33 if ((p+n)%2 == 1)
34 res.set(n) += p*so(p)*2 ;
35
36 // Normalisation of the first coef :
37 res.set(0) /= 2. ;
38
39 return res ;
40 }
41 //*******************
42 // Second derivative
43 //*******************
44 Tbl ope_der_sec (const Tbl& so) {
45 // Verification of size :
46 assert (so.get_ndim() == 1) ;
47 int size = so.get_dim(0) ;
48
49 // The result is set to zero
50 Tbl res (size) ;
51 res.annule_hard() ;
52
53 // the computation
54 for (int n=0 ; n<size ; n++)
55 for (int p=n+2 ; p<size ; p++)
56 if ((p+n)%2 == 0)
57 res.set(n) += p*(p*p-n*n)*so(p) ;
58
59 // Normalisation of the first coef :
60 res.set(0) /= 2. ;
61
62 return res ;
63 }
64
65 //*******************
66 // The operator
67 //*******************
68
69 Matrice matrix_ope (int n) {
70
71 // The result :
72 Matrice res(n,n) ;
73 res.set_etat_qcq() ;
74
75 // Work arrays :
76 Tbl so (n) ;
77 Tbl d_so (n) ;
78 Tbl dd_so (n) ;
79
80 // Column by column :
81 for (int col=0 ; col<n ; col++) {
82 so.annule_hard() ;
83 so.set(col) = 1 ;
84
85 // The derivatives
86 d_so = ope_der(so) ;
87 dd_so = ope_der_sec(so) ;
88
89 // Put in the matrix :
90 for (int line=0 ; line<n ; line++)
91 res.set(line, col) = dd_so(line) -4*d_so(line) + 4*so(line) ;
92 }
93
94 return res ;
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 // Resolution with a tau method :
107 Matrice systeme(nr, nr) ;
108 systeme.set_etat_qcq() ;
109 // Boundary conditions are inforced by additional equations:
110 // Left boundary condition :
111 for (int i=0 ; i<nr ; i++)
112 systeme.set(0, i) = (i%2==0) ? 1 : -1 ;
113 // Right boundary condition :
114 for (int i=0 ; i<nr ; i++)
115 systeme.set(1, i) = 1 ;
116 // The rest of the equations :
117 for (int i=2 ; i<nr ; i++)
118 for (int j=0 ; j<nr ; j++)
119 systeme.set(i,j) = operator_mat(i-2, j) ;
120
121 systeme.set_lu() ;
122
123 cout << "The Tau matrix is" << endl ;
124 cout << systeme << endl ;
125
126 // Coeficients of the source :
127 Tbl conf (nr) ;
128 conf.set_etat_qcq() ;
129 Tbl colocation(coloc_cheb(nr)) ;
130 for (int i=0 ; i<nr ; i++)
131 conf.set(i) = f_source(colocation(i)) ;
132 Tbl coefs (coef_cheb(conf)) ;
133
134 cout << "Coefficients of the source" << endl ;
135 cout << coefs << endl ;
136
137 // Second member of the system :
138 Tbl sec_member (nr) ;
139 sec_member.set_etat_qcq() ;
140 // Boundary conditions :
141 sec_member.set(0) = 0 ;
142 sec_member.set(1) = 0 ;
143 // Coefficients of the source :
144 for (int i=2 ; i<nr ; i++)
145 sec_member.set(i) = coefs(i-2) ;
146
147 cout << "Second member for the Tau system" << endl ;
148 cout << sec_member << endl ;
149
150 // The system is inverted :
151 Tbl coef_sol (systeme.inverse(sec_member)) ;
152
153 cout << "Coefficients of the solution : " << endl ;
154 cout << coef_sol << endl ;
155
156 // Output in a file for plotting purposes :
157 char name_out[30] ;
158 sprintf(name_out, "plot_tau_%i.dat", nr) ;
159 ofstream fiche (name_out) ;
160
161 int resolution = 200 ;
162 double x=-1 ;
163 double step = 2./resolution ;
164
165 // We will also compute the maximum difference between the solution and its numerical value :
166 double error_max = 0 ;
167
168 for (int i=0 ; i<resolution+1 ; i++) {
169 // One computes the solution at the current point
170 double val_func = 0 ;
171 for (int j=0 ; j<nr ; j++)
172 val_func += coef_sol(j)*cheby(j, x) ;
173 fiche << x << " " << val_func << " " << f_solution(x) << endl ;
174 double error = fabs(val_func-f_solution(x)) ;
175 if (error > error_max)
176 error_max = error ;
177
178 x += step ;
179 }
180
181 cout << "Error max : " << endl ;
182 cout << error_max << endl ;
183
184 return 0 ;
185 }