# [Tutor] wave equation boundary conditions

David Craig dcdavemail at gmail.com
Sun Mar 18 16:05:44 CET 2012

```Hi, I have written some code which solve's the 2d wave equation (code is
below). It seems to work, however when the wave reaches the boundaries it
reflects off them and I would like to remove this effect. Does anyone know
of a method to do this?
Also, I would like to run it over a large area, but when I do the images
take a long time to render (I've tried saving them as .png files instead,
which is a bit faster) so if anyone has any suggestions how I can improve
this I'd appreciate it.
D

import matplotlib.pyplot as plt
import numpy as np
import pylab as py
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm

pi = np.pi

#Set up grid.

fig = py.figure()
ax = Axes3D(fig)

nx = 10
nz = 10

X = np.arange(0, nx, 1)
Y = np.arange(0, nz, 1)
X, Y = np.meshgrid(X, Y)

nsteps = 100

# Constants for equation.
c = 4000
dt = 1e-4
h = 1

# Set up source.
xs = 0
zs = 0

#fig2 = py.figure()
ts = np.arange(dt,nsteps*dt,dt)
s = 0.5*np.sin(2*pi*100*ts)
#py.plot(ts,s)
#py.show()

# Homogeneous pressure field.
p = np.zeros([nx, nz, nsteps])

# Solve equation.
for t in range(0,nsteps-1):

for z in range(0,nz-1):

for x in range(0,nx-1):

p[xs,zs,t] = s[t]

k = (c*dt/h)**2

p[x,z,t] = 2*p[x,z,t-1] - p[x,z,t-2] +
k*(p[x+1,z,t-1]-4*p[x,z,t-1]+p[x-1,z,t-1]+p[x,z+1,t-1]+p[x,z-1,t-1])

snap = p[:,:,t]
surf = ax.plot_surface(X,Y,snap, rstride=1, cstride=1, cmap=cm.jet,
linewidth=0)
#fig.colorbar(surf, shrink=0.5, aspect=5)
py.draw()
#py.savefig('/home/davcra/Desktop/plots/2Dwave/'+str(t))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120318/ac7ca3bc/attachment.html>
```