| 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_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 |
// Verification of size : |
| 42 |
assert (so.get_ndim() == 1) ; |
| 43 |
int size = so.get_dim(0) ; |
| 44 |
|
| 45 |
// The result is set to zero |
| 46 |
Tbl res (size) ; |
| 47 |
res.annule_hard() ; |
| 48 |
|
| 49 |
// the computation |
| 50 |
for (int n=0 ; n<size ; n++) |
| 51 |
for (int p=n+1 ; p<size ; p++) |
| 52 |
if ((p+n)%2 == 1) |
| 53 |
res.set(n) += p*so(p)*2 ; |
| 54 |
|
| 55 |
// Normalisation of the first coef : |
| 56 |
res.set(0) /= 2. ; |
| 57 |
|
| 58 |
return res ; |
| 59 |
} |
| 60 |
//******************* |
| 61 |
// Second derivative |
| 62 |
//******************* |
| 63 |
Tbl ope_der_sec (const Tbl& so) { |
| 64 |
// Verification of size : |
| 65 |
assert (so.get_ndim() == 1) ; |
| 66 |
int size = so.get_dim(0) ; |
| 67 |
|
| 68 |
// The result is set to zero |
| 69 |
Tbl res (size) ; |
| 70 |
res.annule_hard() ; |
| 71 |
|
| 72 |
// the computation |
| 73 |
for (int n=0 ; n<size ; n++) |
| 74 |
for (int p=n+2 ; p<size ; p++) |
| 75 |
if ((p+n)%2 == 0) |
| 76 |
res.set(n) += p*(p*p-n*n)*so(p) ; |
| 77 |
|
| 78 |
// Normalisation of the first coef : |
| 79 |
res.set(0) /= 2. ; |
| 80 |
|
| 81 |
return res ; |
| 82 |
} |
| 83 |
|
| 84 |
//************************************** |
| 85 |
// The operator (multi domain case) |
| 86 |
//************************************** |
| 87 |
|
| 88 |
Matrice multi_ope (int n) { |
| 89 |
|
| 90 |
// The result : |
| 91 |
Matrice res(n,n) ; |
| 92 |
res.set_etat_qcq() ; |
| 93 |
|
| 94 |
// Work arrays : |
| 95 |
Tbl so (n) ; |
| 96 |
Tbl dd_so (n) ; |
| 97 |
|
| 98 |
// Column by column : |
| 99 |
for (int col=0 ; col<n ; col++) { |
| 100 |
so.annule_hard() ; |
| 101 |
so.set(col) = 1 ; |
| 102 |
|
| 103 |
// The derivative |
| 104 |
dd_so = ope_der_sec(so) ; |
| 105 |
|
| 106 |
// Put in the matrix : |
| 107 |
for (int line=0 ; line<n ; line++) |
| 108 |
res.set(line, col) = -4*dd_so(line) + 4*so(line) ; |
| 109 |
} |
| 110 |
|
| 111 |
return res ; |
| 112 |
} |
| 113 |
|
| 114 |
int main () { |
| 115 |
|
| 116 |
int nr ; |
| 117 |
cout << "Please enter the number of coefficients :" << endl ; |
| 118 |
cin >> nr ; |
| 119 |
|
| 120 |
cout << "The operator matrix is " << endl ; |
| 121 |
Matrice operator_mat (multi_ope(nr)) ; |
| 122 |
cout << operator_mat << endl ; |
| 123 |
|
| 124 |
// Resolution with a tau method : |
| 125 |
Matrice systeme(nr*2, nr*2) ; |
| 126 |
systeme.annule_hard() ; |
| 127 |
|
| 128 |
// Boundary conditions are inforced by additional equations: |
| 129 |
// Left boundary condition : |
| 130 |
for (int i=0 ; i<nr ; i++) |
| 131 |
systeme.set(0, i) = (i%2==0) ? 1 : -1 ; |
| 132 |
// Equation in the first domain : |
| 133 |
for (int i=0 ; i<nr-2 ; i++) |
| 134 |
for (int j=0 ; j<nr ; j++) |
| 135 |
systeme.set(i+1,j) = operator_mat(i,j) ; |
| 136 |
// Continuity of the solution : |
| 137 |
for (int i=0 ; i<nr ; i++) |
| 138 |
systeme.set(nr-1, i) = 1 ; |
| 139 |
for (int i=0 ; i<nr ; i++) |
| 140 |
systeme.set(nr-1, i+nr) = (i%2==0) ? -1 : 1 ; |
| 141 |
|
| 142 |
// Continuity of the first derivative : |
| 143 |
for (int i=0 ; i<nr ; i++) |
| 144 |
systeme.set(nr, i) = i*i ; |
| 145 |
for (int i=0 ; i<nr ; i++) |
| 146 |
systeme.set(nr, i+nr) = (i%2==0) ? i*i : -i*i ; |
| 147 |
|
| 148 |
// Equation in the second domain : |
| 149 |
for (int i=0 ; i<nr-2 ; i++) |
| 150 |
for (int j=0 ; j<nr ; j++) |
| 151 |
systeme.set(i+1+nr,j+nr) = operator_mat(i,j) ; |
| 152 |
|
| 153 |
// Right boundary condition : |
| 154 |
for (int i=0 ; i<nr ; i++) |
| 155 |
systeme.set(2*nr-1, i+nr) = 1 ; |
| 156 |
|
| 157 |
systeme.set_lu() ; |
| 158 |
|
| 159 |
cout << "Multi-domain systeme of equations :" << endl ; |
| 160 |
cout << systeme << endl ; |
| 161 |
|
| 162 |
// Coeficients of the source : |
| 163 |
// left hand side : |
| 164 |
Tbl conf (nr) ; |
| 165 |
conf.set_etat_qcq() ; |
| 166 |
Tbl colocation(coloc_cheb(nr)) ; |
| 167 |
for (int i=0 ; i<nr ; i++) |
| 168 |
conf.set(i) = f_left(0.5*(colocation(i)-1)) ; |
| 169 |
Tbl coefs_left (coef_cheb(conf)) ; |
| 170 |
for (int i=0 ; i<nr ; i++) |
| 171 |
conf.set(i) = f_right(0.5*(colocation(i)+1)) ; |
| 172 |
Tbl coefs_right (coef_cheb(conf)) ; |
| 173 |
|
| 174 |
|
| 175 |
cout << "Coefficients of the source, left and right" << endl ; |
| 176 |
cout << coefs_left << endl ; |
| 177 |
cout << coefs_right << endl ; |
| 178 |
|
| 179 |
// Second member of the system : |
| 180 |
Tbl sec_member (2*nr) ; |
| 181 |
sec_member.set_etat_qcq() ; |
| 182 |
// Leftoundary conditions : |
| 183 |
sec_member.set(0) = 0 ; |
| 184 |
// Source on the left: |
| 185 |
for (int i=1 ; i<nr-1 ; i++) |
| 186 |
sec_member.set(i) = coefs_left(i-1) ; |
| 187 |
// continuity of the function : |
| 188 |
sec_member.set(nr-1) = 0 ; |
| 189 |
// continuity of the first derivative : |
| 190 |
sec_member.set(nr) = 0 ; |
| 191 |
// Source on the right: |
| 192 |
for (int i=1 ; i<nr-1 ; i++) |
| 193 |
sec_member.set(i+nr) = coefs_right(i-1) ; |
| 194 |
// Right boundary condition : |
| 195 |
sec_member.set(2*nr-1) = 0 ; |
| 196 |
|
| 197 |
cout << "Second member for the multi-domain system" << endl ; |
| 198 |
cout << sec_member << endl ; |
| 199 |
|
| 200 |
// The system is inverted : |
| 201 |
Tbl coef_sol (systeme.inverse(sec_member)) ; |
| 202 |
|
| 203 |
cout << "Coefficients of the solution : " << endl ; |
| 204 |
cout << coef_sol << endl ; |
| 205 |
|
| 206 |
// Output in a file for plotting purposes : |
| 207 |
char name_out[30] ; |
| 208 |
sprintf(name_out, "plot_multi_tau_%i.dat", nr) ; |
| 209 |
ofstream fiche (name_out) ; |
| 210 |
|
| 211 |
int resolution = 200 ; |
| 212 |
double x=-1 ; |
| 213 |
double step = 2./resolution ; |
| 214 |
|
| 215 |
// We will also compute the maximum difference between the solution and its numerical value : |
| 216 |
double error_max = 0 ; |
| 217 |
|
| 218 |
double xi ; |
| 219 |
int offset ; |
| 220 |
for (int i=0 ; i<resolution+1 ; i++) { |
| 221 |
|
| 222 |
// in which domain ? |
| 223 |
if (x<0) { |
| 224 |
offset=0 ; |
| 225 |
xi = 2*x+1 ; |
| 226 |
} |
| 227 |
else { |
| 228 |
offset=nr ; |
| 229 |
xi = 2*x-1 ; |
| 230 |
} |
| 231 |
|
| 232 |
// One computes the solution at the current point |
| 233 |
double val_func = 0 ; |
| 234 |
for (int j=0 ; j<nr ; j++) |
| 235 |
val_func += coef_sol(j+offset)*cheby(j, xi) ; |
| 236 |
fiche << x << " " << val_func << " " << f_solution(x) << endl ; |
| 237 |
double error = fabs(val_func-f_solution(x)) ; |
| 238 |
if (error > error_max) |
| 239 |
error_max = error ; |
| 240 |
|
| 241 |
x += step ; |
| 242 |
} |
| 243 |
|
| 244 |
cout << "Error max : " << endl ; |
| 245 |
cout << error_max << endl ; |
| 246 |
|
| 247 |
return 0 ; |
| 248 |
} |