from pylab import * # ======================================================= # Wave equation: d2u/dt2 - c^2*d2u/dx2 = 0 # with perdiodic boundary condition # and a CTCS scheme (Centered in Time Centered in Space) # ======================================================= # equation properties -------------------------- L = ?? c = ?? # analytical solution -------------------------- def init(x): u = ?? dudt = ?? return u,dudt # space-sampling ------------------------------- nx = ?? dx = L/nx x = linspace(0,L-dx,nx) # time-sampling -------------------------------- tmax = ?? cfl = ?? dt = ?? # Small change in dt to match exactly the last time 'tmax' nt = int(tmax/dt); dt = tmax/nt; cfl = dt/dx*c print '--------------------------------------------------' print "CFL=%5.2f tmax =%7.4f --> %i iterations en temps"%(cfl,nt*dt,nt) print '--------------------------------------------------' # initial conditions --------------------------- t=0. ua,dudt = init(x) plot(x,ua,'-o',label='Initial') # Fist time step ub = ?? # Time loop ---------------------------------- for i in range(1,nt+1): # CTCS ua = ?? # Flip ua <-> ub ua, ub = ub, ua # 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 plot(x,ub,'--o') # Plot last time step --------------------------- plot(x,ub,'--o',label='Numerical') # Plot setup ------------------------------------ plot(x, ones(nx),color='k',ls='--') plot(x,zeros(nx),color='k',ls='--') xlabel('x') ylabel('u(t,x)') ylim([-.1,1.2]) title('Explicit scheme for the wave equation with CFL=%5.2f'%cfl) legend(loc='upper center',ncol=3) show()