from pylab import * # ======================================================= # Advection equation: du/dt + v*du/dx = 0 # with perdiodic boundary condition # 3 explicit schemes : FTCS, Lax-Friedrichs, Upwind # ======================================================= # equation properties -------------------------- L = 1.0 v = 1.0 # analytical solution -------------------------- def uexacte(t,x): return ?? # space-sampling ------------------------------- nx = ?? dx = L/float(nx) x = linspace(0,L-dx,nx) # time-sampling -------------------------------- tmax = ?? cfl = ?? dt = ?? nt = int(tmax/dt)+1 print '--------------------------------------------------' print "CFL=%5.2f tmax =%7.4f --> %i iterations en temps"%(cfl,(nt+1)*dt,nt) print '--------------------------------------------------' # initial condition ---------------------------- t=0. u = uexacte(0.,x) plot(x,u,'-o',label='Initial') # Time loop ---------------------------------- for i in range(1,nt+1): # scheme including boundary conditions u = ?? # update time t=t+dt # Optional: plot intermediate some time steps intermediate = 0 if (intermediate): dtplot = 0.1 if (i % int(dtplot/dt) == 0): print "t=%5.2f"%t p = plot(x,uexacte(t,x)) plot(x,u,'--o',color=p[0].get_color()) # Plot last time step --------------------------- p = plot(x,uexacte(t,x),label='Analytical') plot(x,u,'--o',color=p[0].get_color(),label='Numerical') # Plot setup ------------------------------------ if (not intermediate): ylim([0,1.2*max(uexacte(t,x))]) plot(x, ones(nx),color='k',ls='--') plot(x,-ones(nx),color='k',ls='--') xlabel('x') ylabel('u(t,x)') ylim([-1.1,1.3]) title('Explicit scheme for the advection equation with CFL=%5.2f'%cfl) legend(loc='upper center',ncol=3) show()