示例#1
0
gyplus = 0; gyminus = 0;

# Time-stepping by leap frog formula:
plotgap = int(round(2./dt)); dt = 2./plotgap;
for n in range(2*plotgap+1):
    t = n*dt
    if (n+.5)%plotgap < 1.:
        i = n/plotgap
        ax = subplot(2,2,i+1,projection='3d')
        ax.plot_surface(xx, yy, vv, cstride=1,rstride=1,cmap='summer')
        title('t = '+str('%.5f' % t)); ax.azim = -138; ax.elev = 38; ax.set_zlim3d(-0.2,1)

    # Fourier transform in x-direction
    uxx = zeros((Ny+1,Nx)); uyy = zeros((Ny+1,Nx));
    for i in range(Ny):                # 2nd derivs wrt x in each row
        v = vv[i,:];
        v_hat = fft(v);
        w_hat = (pi/A)**2*k2*v_hat
        w = real(ifft(w_hat)); 
        uxx[i,:]= w;
    for j in range(Nx):                # 2nd derivs wrt y in each column
        v = vv[:,j]; 
        # Impose Neumann BC:
        uyy[:,j] = chebfft(v); 
        uyy[0,j] = gyplus; uyy[-1,j] = gyminus;
        uyy[:,j] = chebfft(uyy[:,j]);
     
    vvnew = 2*vv - vvold + dt**2*(uxx+uyy);
    vvold = vv; vv = vvnew;

show()
示例#2
0
"""
Spectral methods in MATLAB. Lloyd
Program 18a
"""

# Chebyshev differentiation via FFT (compare program 11)

from numpy import *
from matplotlib import pyplot as plt
from chebfft import chebfft

xx = arange(-1,1,0.01)
ff = exp(xx)*sin(5*xx)
for N in [10, 20]:
    x = cos(pi*arange(0,N+1)/N)
    f = exp(x)*sin(5*x)
    
    plt.subplot(220+int(N/10.))
    plt.plot(x, f, 'o', xx,ff,'-')
    plt.title('f(x), N=' + str(N))
    
    error = chebfft(f)-exp(x)*(sin(5*x)+5*cos(5*x))
    plt.subplot(222+int(N/10.))
    plt.plot(x,error, marker='o', linestyle='-')
    plt.title('error in df(x)/dx, N=' + str(N))
    
plt.show()
示例#3
0
vvold = exp(-8*((xx+dt+1.5)**2+yy**2));
gplus = 0; gminus = 0;

# Time-stepping by leap frog formula:
plotgap = int(round(2./dt)); dt = 2./plotgap;
for n in range(2*plotgap+1):
    t = n*dt
    if (n+.5)%plotgap < 1.:
        i = n/plotgap
        ax = subplot(2,2,i+1,projection='3d')
        ax.plot_surface(xx, yy, vv, cstride=1,rstride=1,cmap='summer')
        title('t = '+str('%.5f' % t)); ax.azim = -138; ax.elev = 38; ax.set_zlim3d(-0.2,1)
        
    # Fourier transform in x-direction
    uxx = zeros((Ny+1,Nx)); uyy = zeros((Ny+1,Nx));
    for i in range(Ny):                # 2nd derivs wrt x in each row
        v = vv[i,:];
        v_hat = fft(v);
        w_hat = (pi/A)**2*k2*v_hat
        w = real(ifft(w_hat)); 
        uxx[i,:]= w;
    for j in range(Nx):                # 2nd derivs wrt y in each column
        v = vv[:,j]; 
        uyy[:,j] = chebfft(chebfft(v)); 
        # Impose Dirichlet BC:
        uyy[0,j]=gplus; uyy[-1,j]=gminus;
     
    vvnew = 2*vv - vvold + dt**2*(uxx+uyy);
    vvold = vv; vv = vvnew;

show()
示例#4
0
N = 80
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 = vstack((v, zeros((nplots, N + 1))))
tdata = 0
for i in range(0, nplots):
    for n in range(0, plotgap):
        w = chebfft(chebfft(v)).T
        w[0] = 0
        w[N] = 0
        vnew = 2 * v - vold + dt**2 * w
        vold = v
        v = vnew
    plotdata[i + 1, :] = v
    tdata = vstack((tdata, dt * i * plotgap))

# Plot results

fig = plt.figure()
ax = axes3d.Axes3D(fig)
X, Y = meshgrid(x, tdata)
ax.plot_wireframe(X, Y, plotdata)
ax.set_xlim(-1, 1)
示例#5
0
#Uold = initcond(x-cc*dt) # x is descending, so this is inward-propagating in r
V = 0.0 * x
Vx0 = np.zeros_like(x)

tmax = 4
tplot = 0.1
plotgap = int(np.round(tplot / dt))
dt = tplot / plotgap
nplots = int(np.round(tmax / tplot))
plotdata = np.vstack((U, np.zeros((nplots, N + 1))))
tdata = 0
for i in range(0, nplots):
    for n in range(0, plotgap):

        # Heun's method
        Ux0 = cc * chebfft(U).T  # Ux from current U

        Vx0 = cc * chebfft(V).T  # Vx from current V
        Vx0[0] = 0.0  # u_t = 0 at edges
        Vx0[N] = 0.0

        Ustar = U + dt * Vx0  # full step of U
        Uxstar = cc * chebfft(Ustar).T  # Vx from current V

        Vnew = V + dt * 0.5 * (Ux0 + Uxstar)  # V at i+1

        Vx1 = cc * chebfft(Vnew).T
        Vx1[0] = 0.0  # u_t = 0 at edges
        Vx1[N] = 0.0

        Unew = U + dt * 0.5 * (Vx0 + Vx1)  # U at i+1