Debian Codegraph @

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