Debian Codegraph @

School05/Tuesday/sol_multi_variation.C

raw
1 #include "headcpp.h"
2 #include "math.h"
3
4 #include "tbl.h"
5 #include "matrice.h"
6 #include "leg.h"
7
8 double f_left (double) {
9 return 1. ;
10 }
11
12 double f_right (double) {
13 return 0. ;
14 }
15
16 double f_source (double x) {
17 if (x<0)
18 return 1. ;
19 else
20 if (x>0) return 0. ;
21 else
22 return 0.5 ;
23 }
24
25 double f_solution (double x) {
26 double ee = exp(1.) ;
27 static double bmoins = -1./8./(1+ee*ee) - ee*ee/8./(1+ee*ee*ee*ee) ;
28 static double bplus = ee*ee*ee*ee/8.*(ee*ee/(1+ee*ee*ee*ee)-1./(1+ee*ee)) ;
29
30 if (x<0)
31 return 0.25 -(ee*ee/4.+bmoins*ee*ee*ee*ee)*exp(2*x)
32 + bmoins*exp(-2*x) ;
33 else
34 return bplus*(exp(-2*x)-exp(2*x)/ee/ee/ee/ee) ;
35 }
36
37 //*******************
38 // First derivative
39 //*******************
40 Tbl ope_der (const Tbl& so) {
41
42 // Verification of size :
43 assert (so.get_ndim() == 1) ;
44 int size = so.get_dim(0) ;
45
46 // The result is set to zero
47 Tbl res (size) ;
48 res.annule_hard() ;
49
50 // the computation
51 for (int n=0 ; n<size ; n++) {
52 for (int p=n+1 ; p<size ; p++)
53 if ((p+n)%2 == 1)
54 res.set(n) += so(p) ;
55 res.set(n) *= (2*n+1) ;
56 }
57 return res ;
58 }
59
60
61 int main () {
62
63 int nr ;
64 cout << "Please enter the number of coefficients :" << endl ;
65 cin >> nr ;
66
67 // Collocation and weights
68 Tbl coloc (nr) ;
69 coloc.set_etat_qcq() ;
70 Tbl poids (nr) ;
71 poids.set_etat_qcq() ;
72 coloc_poids_leg(nr, coloc, poids) ;
73
74 // Computation of the matrix D :
75 Matrice ope_D (nr, nr) ;
76 ope_D.set_etat_qcq() ;
77
78 Tbl val (nr) ;
79 for (int i=0 ; i<nr ; i++) {
80 val.annule_hard() ;
81 val.set(i) = 1 ;
82 Tbl coef (coef_leg(val)) ;
83 Tbl der (ope_der(coef)) ;
84
85 // Value of derivative at point x_j :
86 for (int j=0 ; j<nr ; j++) {
87 double sum = 0 ;
88 for (int k=0 ; k<nr ; k++)
89 sum += der(k)*leg(k, coloc(j)) ;
90 ope_D.set(j,i) = 2*sum ;
91 }
92 }
93
94 cout << "Derivative operator D : " << endl ;
95 cout << ope_D << endl ;
96
97 // Matrice of the system :
98 Matrice systeme(2*nr-1, 2*nr-1) ;
99 systeme.annule_hard() ;
100
101 // Left boundary condition :
102 systeme.set(0,0) = 1 ;
103 // Equation inside left domain and at the interface:
104 for (int n=1 ; n<nr ; n++) {
105 for (int j=0 ; j<nr ; j++)
106 for (int i=0 ; i<nr ; i++)
107 systeme.set(n,j) += ope_D(i,j)*ope_D(i,n)*poids(i) ;
108 systeme.set(n,n) += 4*poids(n) ;
109 }
110 // equation at the interface and inside domain 1 :
111 for (int n=0 ; n<nr-1 ; n++) {
112 for (int j=0 ; j<nr ; j++)
113 for (int i=0 ; i<nr ; i++)
114 systeme.set(n+nr-1,j+nr-1) += ope_D(i,j)*ope_D(i,n)*poids(i) ;
115 systeme.set(n+nr-1,n+nr-1) += 4*poids(n) ;
116 }
117 // Boundary on the right :
118 systeme.set(2*nr-2, 2*nr-2) = 1 ;
119
120 cout << "Matrix of the system: " << endl ;
121 cout << systeme << endl ;
122 systeme.set_lu() ;
123
124 // Construction of the second member :
125 Tbl sec_member (2*nr-1) ;
126 sec_member.annule_hard() ;
127 // Left boundary condition :
128 sec_member.set(0) = 0 ;
129 // domain 1 ;
130 for (int i=1 ; i<nr ; i++)
131 sec_member.set(i) = f_left(coloc(i))*poids(i) ;
132 // domaine 2 ;
133 for (int i=0 ; i<nr-1 ; i++)
134 sec_member.set(i+nr-1) += f_right(coloc(i))*poids(i) ;
135 // right boundary condition :
136 sec_member.set(2*nr-2) = 0 ;
137
138 cout << "Second member of the system" << endl ;
139 cout << sec_member << endl ;
140
141 // Inversion of the system :
142 Tbl inv (systeme.inverse(sec_member)) ;
143 // Solution in both domain, at collocations points :
144 Tbl sol_left (nr) ;
145 sol_left.set_etat_qcq() ;
146 for (int i=0 ; i<nr ; i++)
147 sol_left.set(i) = inv(i) ;
148 Tbl sol_right (nr) ;
149 sol_right.set_etat_qcq() ;
150 for (int i=0 ; i<nr ; i++)
151 sol_right.set(i) = inv(nr-1+i) ;
152
153 cout << "Solutions in both domains, at collocation points : "<< endl ;
154 cout << sol_left << endl ;
155 cout << sol_right << endl ;
156
157 // Compute the coefficients :
158 Tbl coef_left (coef_leg(sol_left)) ;
159 Tbl coef_right (coef_leg(sol_right)) ;
160
161 // Outputs :
162 // Output in a file for plotting purposes :
163 char name_out[30] ;
164 sprintf(name_out, "plot_variational_%i.dat", nr) ;
165 ofstream fiche (name_out) ;
166
167 int resolution = 200 ;
168 double x=-1 ;
169 double step = 2./resolution ;
170
171 // We will also compute the maximum difference between the solution and its numerical value :
172 double error_max = 0 ;
173
174 double xi ;
175 for (int i=0 ; i<resolution+1 ; i++) {
176
177 // in which domain ?
178 if (x<0)
179 xi = 2*x+1 ;
180 else
181 xi = 2*x-1 ;
182
183 // One computes the solution at the current point
184 double val_func = 0 ;
185 for (int j=0 ; j<nr ; j++)
186 if (x<0)
187 val_func += coef_left(j)*leg(j, xi) ;
188 else
189 val_func += coef_right(j)*leg(j, xi) ;
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 }