| 1 |
/* |
| 2 |
* Solving a 1-D differential equation with spectral methods |
| 3 |
* (Chebyshev polynomials) |
| 4 |
* |
| 5 |
*/ |
| 6 |
|
| 7 |
/* |
| 8 |
* Copyright (c) 2002 Eric Gourgoulhon |
| 9 |
* |
| 10 |
* This file is part of LORENE. |
| 11 |
* |
| 12 |
* LORENE is free software; you can redistribute it and/or modify |
| 13 |
* it under the terms of the GNU General Public License version 2 |
| 14 |
* as published by the Free Software Foundation. |
| 15 |
* |
| 16 |
* LORENE is distributed in the hope that it will be useful, |
| 17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
* GNU General Public License for more details. |
| 20 |
* |
| 21 |
* You should have received a copy of the GNU General Public License |
| 22 |
* along with LORENE; if not, write to the Free Software |
| 23 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 24 |
* |
| 25 |
*/ |
| 26 |
|
| 27 |
char cheby_C[] = "$Header: /cvsroot/Lorene/Codes/Spectral/cheby.C,v 1.6 2014/10/13 08:53:59 j_novak Exp $" ; |
| 28 |
|
| 29 |
/* |
| 30 |
* $Id: cheby.C,v 1.6 2014/10/13 08:53:59 j_novak Exp $ |
| 31 |
* $Log: cheby.C,v $ |
| 32 |
* Revision 1.6 2014/10/13 08:53:59 j_novak |
| 33 |
* Lorene classes and functions now belong to the namespace Lorene. |
| 34 |
* |
| 35 |
* Revision 1.5 2014/10/06 15:09:46 j_novak |
| 36 |
* Modified #include directives to use c++ syntax. |
| 37 |
* |
| 38 |
* Revision 1.4 2003/01/09 11:07:52 j_novak |
| 39 |
* headcpp.h is now compliant with C++ norm. |
| 40 |
* The include files have been ordered, as well as the local_settings_linux |
| 41 |
* |
| 42 |
* Revision 1.3 2002/09/24 22:10:40 e_gourgoulhon |
| 43 |
* |
| 44 |
* Added computation of infinite norm of the error for each method |
| 45 |
* |
| 46 |
* Revision 1.2 2002/09/24 13:15:31 e_gourgoulhon |
| 47 |
* |
| 48 |
* First operational version. |
| 49 |
* |
| 50 |
* Revision 1.1 2002/09/24 08:38:11 e_gourgoulhon |
| 51 |
* |
| 52 |
* Simple code for illustrating various Chebyshev spectral methods: |
| 53 |
* Galerkin, tau and collocation. |
| 54 |
* |
| 55 |
* |
| 56 |
* |
| 57 |
* $Header: /cvsroot/Lorene/Codes/Spectral/cheby.C,v 1.6 2014/10/13 08:53:59 j_novak Exp $ |
| 58 |
* |
| 59 |
*/ |
| 60 |
|
| 61 |
// C headers |
| 62 |
#include <cstdlib> |
| 63 |
#include <cmath> |
| 64 |
|
| 65 |
// Lorene headers |
| 66 |
#include "map.h" |
| 67 |
#include "matrice.h" |
| 68 |
#include "graphique.h" |
| 69 |
#include "utilitaires.h" |
| 70 |
|
| 71 |
using namespace Lorene ; |
| 72 |
|
| 73 |
int main() { |
| 74 |
|
| 75 |
int nn = 5 ; // Number of Chebyshev coefficients |
| 76 |
|
| 77 |
// Grid of collocation points |
| 78 |
// -------------------------- |
| 79 |
int nbr[1] ; |
| 80 |
int nbt[1] ; |
| 81 |
int nbp[1] ; |
| 82 |
nbr[0] = nn ; // Number of degrees of freedom in r |
| 83 |
nbt[0] = 1 ; // Number of degrees of freedom in theta |
| 84 |
nbp[0] = 1 ; // Number of degrees of freedom in phi |
| 85 |
|
| 86 |
int typr[1] ; |
| 87 |
typr[0] = FIN ; // Type of sampling in r : |
| 88 |
// FIN = ordinary Chebychev sampling in [-1,1] |
| 89 |
|
| 90 |
int typt = SYM ; // Type of sampling in theta |
| 91 |
int typp = SYM ; // Type of sampling in theta |
| 92 |
|
| 93 |
Mg3d grid(1, nbr, typr, nbt, typt, nbp, typp) ; |
| 94 |
|
| 95 |
cout << "Grid of collocation points: " << grid << endl ; |
| 96 |
|
| 97 |
// Trivial mapping |
| 98 |
// --------------- |
| 99 |
|
| 100 |
double bornes[2] ; |
| 101 |
bornes[0] = -1. ; |
| 102 |
bornes[1] = 1. ; |
| 103 |
|
| 104 |
Map_af map(grid, bornes) ; |
| 105 |
|
| 106 |
Mtbl x = map.r ; |
| 107 |
|
| 108 |
cout << "Collocation points : " << x << endl ; |
| 109 |
|
| 110 |
// Source of the equation |
| 111 |
// ---------------------- |
| 112 |
|
| 113 |
Valeur ss(grid) ; |
| 114 |
|
| 115 |
double cc = - 4. * exp(1.) / (1. + exp(1.)*exp(1.)) ; |
| 116 |
|
| 117 |
ss = exp(x) + cc ; |
| 118 |
|
| 119 |
cout << "Values of the source s at the collation points : " |
| 120 |
<< endl << ss << endl ; |
| 121 |
|
| 122 |
// Chebyshev expansion of the source |
| 123 |
// --------------------------------- |
| 124 |
ss.set_base_r(0, R_CHEB) ; |
| 125 |
ss.set_base_t(T_COS_P) ; |
| 126 |
ss.set_base_p(P_COSSIN_P) ; |
| 127 |
ss.coef() ; |
| 128 |
cout << "Coef of the source : " << endl ; |
| 129 |
ss.affiche_seuil(cout, 0, 4, 1.e-15) ; |
| 130 |
|
| 131 |
// Chebyshev expansion of the source with the large number of coef |
| 132 |
// --------------------------------------------------------------- |
| 133 |
nbr[0] = 33 ; |
| 134 |
Mg3d grid_big(1, nbr, typr, nbt, typt, nbp, typp) ; |
| 135 |
|
| 136 |
Map_af map_big(grid_big, bornes) ; |
| 137 |
|
| 138 |
Mtbl x_big = map_big.r ; |
| 139 |
|
| 140 |
Valeur ss_big(grid_big) ; |
| 141 |
ss_big = exp(x_big) + cc ; |
| 142 |
ss_big.set_base_r(0, R_CHEB) ; |
| 143 |
ss_big.set_base_t(T_COS_P) ; |
| 144 |
ss_big.set_base_p(P_COSSIN_P) ; |
| 145 |
ss_big.coef() ; |
| 146 |
cout << "Coef of the source with a large number of Chebyshev polynomials: " << endl ; |
| 147 |
ss_big.affiche_seuil(cout, 0, 4, 1.e-15) ; |
| 148 |
|
| 149 |
// des_coef_xi(ss_big, 0, 0, 0, 1.e-14, 0x0, "Coef of s") ; |
| 150 |
|
| 151 |
|
| 152 |
// Orthogonal projection: P_N s: |
| 153 |
Mtbl_cf cf_proj(grid, ss.base) ; |
| 154 |
cf_proj.annule_hard() ; |
| 155 |
for (int i=0; i<nn; i++) { |
| 156 |
cf_proj.set(0,0,0,i) = (*(ss_big.c_cf))(0,0,0,i) ; |
| 157 |
} |
| 158 |
|
| 159 |
Mtbl_cf cf_alias = *(ss.c_cf) - cf_proj ; |
| 160 |
cout << "Aliasing error : " << endl ; |
| 161 |
cout << "--------------" << endl ; |
| 162 |
cf_alias.affiche_seuil(cout) ; |
| 163 |
|
| 164 |
ofstream file("ss.d") ; |
| 165 |
file << "# x s(x) Is(x) Is(x)-s(x) Ps(x) - s(x) Is(x)-Ps(x) " << endl ; |
| 166 |
file.precision(8) ; |
| 167 |
int ndes = 200 ; |
| 168 |
double h = double(2) / double(ndes-1) ; |
| 169 |
for (int i=0; i<ndes; i++) { |
| 170 |
double xd = -1. + h * i ; |
| 171 |
double yfd = exp(xd) + cc ; |
| 172 |
double yid = ss.val_point(0, xd, 0., 0.) ; |
| 173 |
double ypd = cf_proj.val_point(0, xd, 0., 0.) ; |
| 174 |
file << xd << " " << yfd << " " |
| 175 |
<< yid << " " << yid - yfd << " " << ypd - yfd << " " << yid - ypd << endl ; |
| 176 |
} |
| 177 |
file.close() ; |
| 178 |
|
| 179 |
file.open("colloc.d") ; |
| 180 |
file.precision(8) ; |
| 181 |
for (int i=0; i<nn; i++) { |
| 182 |
file << x(0,0,0,i) << " 0." << endl ; |
| 183 |
} |
| 184 |
file.close() ; |
| 185 |
|
| 186 |
|
| 187 |
// Exact solution of the equation |
| 188 |
// ------------------------------ |
| 189 |
|
| 190 |
Valeur uu_exact(grid) ; |
| 191 |
uu_exact = exp(x) - sinh(1.) / sinh(2.) * exp(2*x) + cc / 4. ; |
| 192 |
uu_exact.set_base(ss.base) ; |
| 193 |
uu_exact.coef() ; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
// First derivative matrix |
| 198 |
// ----------------------- |
| 199 |
|
| 200 |
Matrice mat_dx(nn,nn) ; |
| 201 |
mat_dx.set_etat_qcq() ; |
| 202 |
|
| 203 |
Mtbl_cf cf_cheb(grid, ss.base) ; |
| 204 |
|
| 205 |
for (int i=0; i<nn; i++) { |
| 206 |
cf_cheb.annule_hard() ; // fills with zeros |
| 207 |
cf_cheb.set(0,0,0,i) = 1. ; |
| 208 |
|
| 209 |
cf_cheb.dsdx() ; |
| 210 |
for (int j=0; j<nn; j++) { |
| 211 |
mat_dx.set(j,i) = cf_cheb(0,0,0,j) ; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
cout << "d/dx matrix : " << mat_dx << endl ; |
| 216 |
|
| 217 |
// Second derivative matrix |
| 218 |
// ------------------------ |
| 219 |
|
| 220 |
Matrice mat_dx2(nn,nn) ; |
| 221 |
mat_dx2.set_etat_qcq() ; |
| 222 |
|
| 223 |
for (int i=0; i<nn; i++) { |
| 224 |
cf_cheb.annule_hard() ; // fills with zeros |
| 225 |
cf_cheb.set(0,0,0,i) = 1. ; |
| 226 |
|
| 227 |
cf_cheb.d2sdx2() ; |
| 228 |
for (int j=0; j<nn; j++) { |
| 229 |
mat_dx2.set(j,i) = cf_cheb(0,0,0,j) ; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
cout << "d^2/dx^2 matrix : " << mat_dx2 << endl ; |
| 234 |
|
| 235 |
// Identity matrix |
| 236 |
// --------------- |
| 237 |
|
| 238 |
Matrice mat_id(nn,nn) ; |
| 239 |
mat_id.set_etat_qcq() ; |
| 240 |
for (int i=0; i<nn; i++) { |
| 241 |
for (int j=0; j<nn; j++) { |
| 242 |
mat_id.set(i,j) = 0. ; |
| 243 |
} |
| 244 |
mat_id.set(i,i) = 1. ; |
| 245 |
} |
| 246 |
|
| 247 |
cout << "Identity matrix : " << mat_id << endl ; |
| 248 |
|
| 249 |
// Full operator matrix |
| 250 |
// -------------------- |
| 251 |
|
| 252 |
Matrice mat_op = mat_dx2 - 4 * mat_dx + 4 * mat_id ; |
| 253 |
cout << "Matrix of the operator d^2/dx^2 - 4 d/dx + 4 Id : " |
| 254 |
<< mat_op << endl ; |
| 255 |
|
| 256 |
arrete() ; |
| 257 |
|
| 258 |
//------------------ |
| 259 |
// Galerkin method |
| 260 |
//------------------ |
| 261 |
|
| 262 |
cout << "========================================================================" |
| 263 |
<< endl << " Galerkin method" << endl |
| 264 |
<< "========================================================================" |
| 265 |
<< endl ; |
| 266 |
|
| 267 |
// Matrix Chebyshev basis --> Galerkin basis |
| 268 |
Matrice mat_gal(nn,nn-2) ; |
| 269 |
mat_gal.set_etat_qcq() ; |
| 270 |
for (int j=0; j<nn-2; j++) { |
| 271 |
|
| 272 |
for (int i=0; i<nn; i++) { |
| 273 |
mat_gal.set(i,j) = 0 ; |
| 274 |
} |
| 275 |
|
| 276 |
if (j%2 == 0) { |
| 277 |
mat_gal.set(0,j) = -1. ; // - T_0(x) |
| 278 |
} |
| 279 |
else { |
| 280 |
mat_gal.set(1,j) = -1. ; // - T_1(x) |
| 281 |
} |
| 282 |
|
| 283 |
mat_gal.set(j+2,j) = 1. ; // T_{j+2}(x) |
| 284 |
|
| 285 |
} |
| 286 |
|
| 287 |
cout << "Matrix Chebyshev basis --> Galerkin basis : " |
| 288 |
<< mat_gal << endl ; |
| 289 |
|
| 290 |
// Transpose |
| 291 |
Matrice mat_tg = mat_gal.transpose() ; |
| 292 |
|
| 293 |
// Normalization of T_0(x) : |
| 294 |
for (int i=0; i<nn-2; i++) { |
| 295 |
mat_tg.set(i,0) = 2.* mat_tg(i,0) ; |
| 296 |
} |
| 297 |
|
| 298 |
cout << "Transpose Matrix : " << mat_tg << endl ; |
| 299 |
|
| 300 |
// Matrix of the Galerkin linear system |
| 301 |
Matrice mat_lin = mat_tg * mat_op * mat_gal ; |
| 302 |
|
| 303 |
cout << |
| 304 |
"Matrix of the linear system to solve in the Galerkin method: " |
| 305 |
<< endl << mat_lin << endl ; |
| 306 |
|
| 307 |
// Right hand side |
| 308 |
Matrice mat_ss_cf(nn, 1) ; |
| 309 |
mat_ss_cf.set_etat_qcq() ; |
| 310 |
for (int i=0; i<nn; i++) { |
| 311 |
mat_ss_cf.set(i,0) = (*(ss.c_cf))(0,0,0,i) ; |
| 312 |
} |
| 313 |
|
| 314 |
Matrice mat_rhs = mat_tg * mat_ss_cf ; |
| 315 |
|
| 316 |
cout << "Right-hand side : " << mat_rhs << endl ; |
| 317 |
|
| 318 |
Tbl rhs_gal(mat_rhs) ; |
| 319 |
|
| 320 |
cout << "Right-hand side (Tbl version) : " << rhs_gal << endl ; |
| 321 |
|
| 322 |
// Resolution of the linear system |
| 323 |
mat_lin.set_band(nn-3, nn-3) ; |
| 324 |
mat_lin.set_lu() ; |
| 325 |
|
| 326 |
Tbl tuu_cf_gal = mat_lin.inverse(rhs_gal) ; |
| 327 |
|
| 328 |
cout << "Coefficients of the solution onto the Galerkin basis : " |
| 329 |
<< endl << tuu_cf_gal << endl ; |
| 330 |
|
| 331 |
// Verif |
| 332 |
Matrice verif_gal = mat_lin * Matrice(tuu_cf_gal) - mat_rhs ; |
| 333 |
cout << "Check of the resolution of the linear system : " << verif_gal << endl ; |
| 334 |
|
| 335 |
// Chebyshev coefficients |
| 336 |
|
| 337 |
Matrice mat_uu_cf_gal(tuu_cf_gal) ; |
| 338 |
Matrice mat_uu_cf_cheb = mat_gal * mat_uu_cf_gal ; |
| 339 |
|
| 340 |
Mtbl_cf uu_cf_gal(grid, ss.base) ; |
| 341 |
uu_cf_gal.annule_hard() ; |
| 342 |
for (int i=0; i<nn; i++) { |
| 343 |
uu_cf_gal.set(0,0,0,i) = mat_uu_cf_cheb(i,0) ; |
| 344 |
} |
| 345 |
|
| 346 |
cout << "Coefficients of the solution onto the Chebyshev basis : " << endl ; |
| 347 |
uu_cf_gal.affiche_seuil(cout, 4, 1.e-15) ; |
| 348 |
|
| 349 |
// Solution |
| 350 |
Valeur uu_gal(grid) ; |
| 351 |
uu_gal = uu_cf_gal ; |
| 352 |
uu_gal.coef_i() ; |
| 353 |
|
| 354 |
// Comparison with the exact solution |
| 355 |
cout << "Comparison with the exact solution : " << endl ; |
| 356 |
cout << "----------------------------------" << endl ; |
| 357 |
// cout << "uu_gal : " << uu_gal << endl ; |
| 358 |
// cout << "uu_exact : " << uu_exact << endl ; |
| 359 |
Valeur diff = uu_gal - uu_exact ; |
| 360 |
diff.coef() ; |
| 361 |
diff.affiche_seuil(cout,2) ; |
| 362 |
|
| 363 |
file.open("sol_gal.d") ; |
| 364 |
file << "# x uu_exact(x) uu(x) uu(x) - uu_exact(x) " << endl ; |
| 365 |
file.precision(8) ; |
| 366 |
double err_max_gal = 0 ; |
| 367 |
for (int i=0; i<ndes; i++) { |
| 368 |
double xd = -1. + h * i ; |
| 369 |
double yuu_exact = exp(xd) - sinh(1.) / sinh(2.) * exp(2*xd) + cc / 4. ; |
| 370 |
double yuu = uu_gal.val_point(0, xd, 0., 0.) ; |
| 371 |
double err = fabs(yuu - yuu_exact) ; |
| 372 |
if (err > err_max_gal ) err_max_gal = err ; |
| 373 |
file << xd << " " << yuu_exact << " " << yuu << " " |
| 374 |
<< yuu - yuu_exact << endl ; |
| 375 |
} |
| 376 |
file.close() ; |
| 377 |
|
| 378 |
file.open("uu_gal_colloc.d") ; |
| 379 |
file.precision(8) ; |
| 380 |
for (int i=0; i<nn; i++) { |
| 381 |
file << x(0,0,0,i) << " " << uu_gal(0,0,0,i) << endl ; |
| 382 |
} |
| 383 |
file.close() ; |
| 384 |
|
| 385 |
arrete() ; |
| 386 |
|
| 387 |
//--------------- |
| 388 |
// Tau method |
| 389 |
//--------------- |
| 390 |
|
| 391 |
cout << "========================================================================" |
| 392 |
<< endl << " Tau method" << endl |
| 393 |
<< "========================================================================" |
| 394 |
<< endl ; |
| 395 |
|
| 396 |
// Matrix of the 1-form which gives the values at x=1 |
| 397 |
Matrice mat_valp1(1,nn) ; |
| 398 |
mat_valp1 = 1. ; |
| 399 |
|
| 400 |
// Matrix of the 1-form which gives the values at x=-1 |
| 401 |
Matrice mat_valm1(1,nn) ; |
| 402 |
mat_valm1.set_etat_qcq() ; |
| 403 |
|
| 404 |
for (int j=0; j<nn; j++) { |
| 405 |
mat_valm1.set(0,j) = (j%2 == 0) ? 1. : -1. ; |
| 406 |
} |
| 407 |
|
| 408 |
cout << "Matrix of the 1-form which gives the values at x=1 : " << mat_valp1 << endl ; |
| 409 |
cout << "Matrix of the 1-form which gives the values at x=-1 : " << mat_valm1 << endl ; |
| 410 |
|
| 411 |
// Insertion in the last two rows of the operator matrix |
| 412 |
Matrice mat_op_tau = mat_op ; |
| 413 |
for (int j=0; j<nn; j++) { |
| 414 |
mat_op_tau.set(nn-2,j) = mat_valp1(0,j) ; |
| 415 |
mat_op_tau.set(nn-1,j) = mat_valm1(0,j) ; |
| 416 |
} |
| 417 |
|
| 418 |
cout << "Matrix of the operator for the tau method : " << mat_op_tau << endl ; |
| 419 |
|
| 420 |
// The last two coefficients of the right-hand side are set to zero |
| 421 |
Tbl tss_cf(mat_ss_cf) ; |
| 422 |
tss_cf.set(nn-2) = 0 ; |
| 423 |
tss_cf.set(nn-1) = 0 ; |
| 424 |
|
| 425 |
// Resolution of the linear system |
| 426 |
mat_op_tau.set_band(nn-1, nn-1) ; |
| 427 |
mat_op_tau.set_lu() ; |
| 428 |
|
| 429 |
Tbl tuu_cf_tau = mat_op_tau.inverse( tss_cf ) ; |
| 430 |
|
| 431 |
cout << "Coefficients of the solution by the tau method : " |
| 432 |
<< endl << tuu_cf_tau << endl ; |
| 433 |
|
| 434 |
// Verif |
| 435 |
Matrice verif_tau = mat_op_tau * Matrice(tuu_cf_tau) - Matrice(tss_cf) ; |
| 436 |
cout << "Check of the resolution of the linear system : " << verif_tau << endl ; |
| 437 |
|
| 438 |
// Coefficients of the solution stored as a Mtbl_cf |
| 439 |
Mtbl_cf uu_cf_tau(grid, ss.base) ; |
| 440 |
uu_cf_tau.annule_hard() ; |
| 441 |
for (int i=0; i<nn; i++) { |
| 442 |
uu_cf_tau.set(0,0,0,i) = tuu_cf_tau(i) ; |
| 443 |
} |
| 444 |
|
| 445 |
cout << "Coefficients of the solution by the tau method : " << endl ; |
| 446 |
uu_cf_tau.affiche_seuil(cout, 4, 1.e-15) ; |
| 447 |
|
| 448 |
// Solution |
| 449 |
Valeur uu_tau(grid) ; |
| 450 |
uu_tau = uu_cf_tau ; |
| 451 |
uu_tau.coef_i() ; |
| 452 |
|
| 453 |
// Comparison with the exact solution |
| 454 |
cout << "Comparison with the exact solution : " << endl ; |
| 455 |
cout << "----------------------------------" << endl ; |
| 456 |
// cout << "uu_tau : " << uu_tau << endl ; |
| 457 |
// cout << "uu_exact : " << uu_exact << endl ; |
| 458 |
diff = uu_tau - uu_exact ; |
| 459 |
diff.coef() ; |
| 460 |
diff.affiche_seuil(cout,2) ; |
| 461 |
|
| 462 |
file.open("sol_tau.d") ; |
| 463 |
file << "# x uu_exact(x) uu(x) uu(x) - uu_exact(x) uu(x) - uu_gal(x)" << endl ; |
| 464 |
file.precision(8) ; |
| 465 |
double err_max_tau = 0 ; |
| 466 |
for (int i=0; i<ndes; i++) { |
| 467 |
double xd = -1. + h * i ; |
| 468 |
double yuu_exact = exp(xd) - sinh(1.) / sinh(2.) * exp(2*xd) + cc / 4. ; |
| 469 |
double yuu = uu_tau.val_point(0, xd, 0., 0.) ; |
| 470 |
double yuu_gal = uu_gal.val_point(0, xd, 0., 0.) ; |
| 471 |
double err = fabs(yuu - yuu_exact) ; |
| 472 |
if (err > err_max_tau ) err_max_tau = err ; |
| 473 |
file << xd << " " << yuu_exact << " " << yuu << " " |
| 474 |
<< yuu - yuu_exact << " " << yuu - yuu_gal << endl ; |
| 475 |
} |
| 476 |
file.close() ; |
| 477 |
|
| 478 |
file.open("uu_tau_colloc.d") ; |
| 479 |
file.precision(8) ; |
| 480 |
for (int i=0; i<nn; i++) { |
| 481 |
file << x(0,0,0,i) << " " << uu_tau(0,0,0,i) << endl ; |
| 482 |
} |
| 483 |
file.close() ; |
| 484 |
|
| 485 |
arrete() ; |
| 486 |
|
| 487 |
//------------------------ |
| 488 |
// Pseudo-spectral method |
| 489 |
//------------------------ |
| 490 |
|
| 491 |
cout << "========================================================================" |
| 492 |
<< endl << " Pseudo-spectral method" << endl |
| 493 |
<< "========================================================================" |
| 494 |
<< endl ; |
| 495 |
|
| 496 |
// Matrix T_j(x_i) |
| 497 |
|
| 498 |
Matrice mat_cheb_col(nn,nn) ; |
| 499 |
mat_cheb_col.set_etat_qcq() ; |
| 500 |
|
| 501 |
for (int j=0; j<nn; j++) { |
| 502 |
cf_cheb.annule_hard() ; // fills with zeros |
| 503 |
cf_cheb.set(0,0,0,j) = 1. ; |
| 504 |
|
| 505 |
Valeur cheb(grid) ; |
| 506 |
cheb = cf_cheb ; |
| 507 |
cheb.coef_i() ; |
| 508 |
|
| 509 |
for (int i=0; i<nn; i++) { |
| 510 |
mat_cheb_col.set(i,j) = cheb(0,0,0,i) ; |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
cout << "Matrix T_{ij} = T_j(x_i) : " << mat_cheb_col << endl ; |
| 515 |
|
| 516 |
// Matrix of the pseudo-spectral linear system |
| 517 |
Matrice mat_op_psp = mat_cheb_col * mat_op ; |
| 518 |
|
| 519 |
// Insertion of the boundary conditions at the first and last |
| 520 |
// rows of the matrix |
| 521 |
|
| 522 |
for (int j=0; j<nn; j++) { |
| 523 |
mat_op_psp.set(0,j) = mat_valm1(0,j) ; |
| 524 |
mat_op_psp.set(nn-1,j) = mat_valp1(0,j) ; |
| 525 |
} |
| 526 |
|
| 527 |
cout << "Matrix of the operator for the pseudo-spectral method : " << mat_op_psp << endl ; |
| 528 |
|
| 529 |
// Right-hand side |
| 530 |
|
| 531 |
Tbl rhs_psp(nn) ; |
| 532 |
rhs_psp.set_etat_qcq() ; |
| 533 |
|
| 534 |
for (int i=0; i<nn; i++) { |
| 535 |
rhs_psp.set(i) = ss(0,0,0,i) ; |
| 536 |
} |
| 537 |
|
| 538 |
rhs_psp.set(0) = 0. ; // boundary conditions |
| 539 |
rhs_psp.set(nn-1) = 0. ; |
| 540 |
|
| 541 |
// Resolution of the linear system |
| 542 |
mat_op_psp.set_band(nn-1, nn-1) ; |
| 543 |
mat_op_psp.set_lu() ; |
| 544 |
|
| 545 |
Tbl tuu_cf_psp = mat_op_psp.inverse( rhs_psp ) ; |
| 546 |
|
| 547 |
cout << "Coefficients of the solution by the pseudo-spectral method : " |
| 548 |
<< endl << tuu_cf_psp << endl ; |
| 549 |
|
| 550 |
// Verif |
| 551 |
Matrice verif_psp = mat_op_psp * Matrice(tuu_cf_psp) - Matrice(rhs_psp) ; |
| 552 |
cout << "Check of the resolution of the linear system : " << verif_psp << endl ; |
| 553 |
|
| 554 |
// Coefficients of the solution stored as a Mtbl_cf |
| 555 |
Mtbl_cf uu_cf_psp(grid, ss.base) ; |
| 556 |
uu_cf_psp.annule_hard() ; |
| 557 |
for (int i=0; i<nn; i++) { |
| 558 |
uu_cf_psp.set(0,0,0,i) = tuu_cf_psp(i) ; |
| 559 |
} |
| 560 |
|
| 561 |
cout << "Coefficients of the solution by the psp method : " << endl ; |
| 562 |
uu_cf_psp.affiche_seuil(cout, 4, 1.e-15) ; |
| 563 |
|
| 564 |
// Solution |
| 565 |
Valeur uu_psp(grid) ; |
| 566 |
uu_psp = uu_cf_psp ; |
| 567 |
uu_psp.coef_i() ; |
| 568 |
|
| 569 |
// Comparison with the exact solution |
| 570 |
cout << "Comparison with the exact solution : " << endl ; |
| 571 |
cout << "----------------------------------" << endl ; |
| 572 |
// cout << "uu_psp : " << uu_psp << endl ; |
| 573 |
// cout << "uu_exact : " << uu_exact << endl ; |
| 574 |
diff = uu_psp - uu_exact ; |
| 575 |
diff.coef() ; |
| 576 |
diff.affiche_seuil(cout, 2) ; |
| 577 |
|
| 578 |
file.open("sol_psp.d") ; |
| 579 |
file << |
| 580 |
"# x uu_exact(x) uu(x) uu(x)-uu_exact(x) uu(x)-uu_gal(x) uu(x)-uu_tau(x)" << endl ; |
| 581 |
file.precision(8) ; |
| 582 |
double err_max_psp = 0 ; |
| 583 |
for (int i=0; i<ndes; i++) { |
| 584 |
double xd = -1. + h * i ; |
| 585 |
double yuu_exact = exp(xd) - sinh(1.) / sinh(2.) * exp(2*xd) + cc / 4. ; |
| 586 |
double yuu = uu_psp.val_point(0, xd, 0., 0.) ; |
| 587 |
double yuu_gal = uu_gal.val_point(0, xd, 0., 0.) ; |
| 588 |
double yuu_tau = uu_tau.val_point(0, xd, 0., 0.) ; |
| 589 |
double err = fabs(yuu - yuu_exact) ; |
| 590 |
if (err > err_max_psp ) err_max_psp = err ; |
| 591 |
file << xd << " " << yuu_exact << " " << yuu << " " |
| 592 |
<< yuu - yuu_exact << " " << yuu - yuu_gal << " " << yuu - yuu_tau << endl ; |
| 593 |
} |
| 594 |
file.close() ; |
| 595 |
|
| 596 |
file.open("uu_psp_colloc.d") ; |
| 597 |
file.precision(8) ; |
| 598 |
for (int i=0; i<nn; i++) { |
| 599 |
file << x(0,0,0,i) << " " << uu_psp(0,0,0,i) << endl ; |
| 600 |
} |
| 601 |
file.close() ; |
| 602 |
|
| 603 |
|
| 604 |
cout << "Max of the error for Galerkin, tau and pseudospectral:" << endl ; |
| 605 |
cout << nn-1 << " " << err_max_gal << " " << err_max_tau |
| 606 |
<< " " << err_max_psp << endl ; |
| 607 |
|
| 608 |
return EXIT_SUCCESS ; |
| 609 |
|
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|