#!/usr/bin/env python # coding: utf-8 # # p19: Second order Wave Equation on Chebyshev Grid # We solve # $$ # u_{tt} = u_{xx}, \qquad -1 < x < 1, \qquad t > 0 # $$ # with boundary condition # $$ # u(\pm 1,t) = 0 # $$ # and initial condition # $$ # u(x,0) = e^{-200 x^2} # $$ # In[3]: get_ipython().run_line_magic('matplotlib', 'inline') get_ipython().run_line_magic('config', "InlineBackend.figure_format = 'svg'") from chebfftPy import chebfft from numpy import arange,cos,zeros,round,exp,pi from mpl_toolkits.mplot3d import Axes3D from matplotlib.collections import LineCollection from matplotlib.pyplot import figure # In[4]: # Time-stepping by Leap Frog Formula: N = 80; t = 0.0 ;x = cos(pi*arange(0,N+1)/N);dt = 8.0/(N**2); v = exp(-200*x**2); vold = exp(-200*(x-dt)**2); tmax = 4 ; tplot = 0.075; plotgap = int(round(tplot/dt)); dt = tplot/plotgap; nplots = int(round(tmax/tplot)); plotdata = []; plotdata.append(list(zip(x,v))); tdata = []; tdata.append(0.0) for i in range(1,nplots): for n in range(plotgap): t = t + dt w = chebfft(chebfft(v)); w[0] = 0.0; w[N] = 0.0; vnew = 2*v - vold + dt**2*w; vold = v; v = vnew; plotdata.append(list(zip(x,v))); tdata.append(t); fig = figure(figsize=(10,12)) ax = fig.add_subplot(111,projection='3d') poly = LineCollection(plotdata) poly.set_alpha(0.5) ax.add_collection3d(poly, zs=tdata, zdir='y') ax.set_xlabel('X') ax.set_xlim3d(-1, 1) ax.set_ylabel('Y') ax.set_ylim3d(0, tmax) ax.set_zlabel('Z') ax.set_zlim3d(-2, 2) ax.view_init(60,-70)