![]() |
![]() |
![]() |
This code illustrates the numerical solution of a 1-D linear transport equation using a finite difference scheme.
const double L = 10., c = 4.; ofstream pf("output.dat"); |
size_t nx = atoi(argv[1]); double h = L/double(nx); theTimeStep = atof(argv[2]); theFinalTime = 1.; |
double cfl = c*theTimeStep/h; Vect<double> x, u(nx+1), v(nx+1); x.setUniform(0.,L,nx+1); |
u.set("exp(-20*(x-2)^2)",x); |
TimeLoop { v(1) = u(1) - cfl*(0.5*u(2)-cfl*(u(2)-2*u(1))); for (size_t i=2; i <=nx+1; i++) v(i) = u(i) - cfl*(0.5*(u(i+1)-u(i-1))-cfl*(u(i+1)-2*u(i)+u(i-1))); u = v; |
for (size_t i=1; i<=nx+1; i++) pf << x(i) << " " << u(i) << endl; pf << endl; } |
cout << "Number of grid intervals: " << nx << endl; cout << "Time step: " << theTimeStep << endl; cout << "CFL: " << cfl << endl; return 0; |
![]() |
![]() |
![]() |