简体   繁体   中英

Python: Writing a program to simulate 1D wave motion along a string

I am working on a program that simulates wave motion along a 1-dimensional string to eventually simulate different wave packets. I found a program in the book "Python Scripting for Computational Science" that claims to describe wave motion, though I'm not certain how to implement it (the book was on Google Books and won't show me the text before/after the code).

For example, I understand that "f" is a function of x and t and that "I" is a function of x but what functions are actually needed to produce a wave?

I= 
f= 
c= 
L= 
n=
dt=
tstop=


x = linespace(0,L,n+1) #grid points in x dir
dx = L/float(n)
if dt <= 0: dt = dx/float(c) #max step time
C2 = (c*dt/dx)**2  #help variable in the scheme
dt2 = dt*dt

up = zeros(n+1) #NumPy solution array
u = up.copy()   #solution at t-dt
um = up.copy()    #solution at t-2*dt

t = 0.0
for i in iseq(0,n):
u[i] +0.5*C2*(u[i-1] - 2*u[i] +u[i+1]) + \
    dt2*f(x[i], t)
um[0] = 0;   um[n] = 0

while t<= tstop:
t_old = t; t+=dt
#update all inner points:
for i in iseq(start=1, stop= n-1):
up[i] = -um[i] +2*u[i] + \
    C2*(u[i-1] - 2*u[i] + u[i+1]) + \
    dt2*f(x[i], t_old)

#insert boundary conditions
up[0] = 0; up[n] = 0
#updata data structures for next step
um = u.copy(); u = up.copy()

The code below should work:

from math import sin, pi
from numpy import zeros, linspace
from scitools.numpyutils import iseq

def I(x):   
    return sin(2*x*pi/L)  

def f(x,t): 
    return 0

def solver0(I, f, c, L, n, dt, tstop):
    # f is a function of x and t, I is a function of x
    x = linspace(0, L, n+1) # grid points in x dir
    dx = L/float(n)
    if dt <= 0: dt = dx/float(c) # max time step
    C2 = (c*dt/dx)**2 # help variable in the scheme
    dt2 = dt*dt

    up = zeros(n+1) # NumPy solution array
    u = up.copy() # solution at t-dt
    um = up.copy() # solution at t-2*dt

    t = 0.0
    for i in iseq(0,n):
        u[i] = I(x[i])
    for i in iseq(1,n-1):
        um[i] = u[i] + 0.5*C2*(u[i-1] - 2*u[i] + u[i+1]) + \
                dt2*f(x[i], t)

    um[0] = 0; um[n] = 0

    while t <= tstop:
          t_old = t; t += dt
          # update all inner points:
          for i in iseq(start=1, stop=n-1):
              up[i] = - um[i] + 2*u[i] + \
                      C2*(u[i-1] - 2*u[i] + u[i+1]) + \
                      dt2*f(x[i], t_old)

          # insert boundary conditions:
          up[0] = 0; up[n] = 0
          # update data structures for next step
          um = u.copy(); u = up.copy()
    return u

if __name__ == '__main__':

# When choosing the parameters you should also check that the units are correct

   c = 5100
   L = 1
   n = 10
   dt = 0.1
   tstop = 1
   a = solver0(I, f, c, L, n, dt, tstop)  

It returns an array with the values of the wave at time tstop and at all the points in our solution grid.

Before you apply it to a practical situation, you should read up both about the wave equation and the finite element method to understand what the code does. It can be used to find the numerical solutions of the wave equation:

Utt + beta*Ut = c^2*Uxx + f(x,t)

which is one of most important differential equations in physics. The solution of this PDE or wave, is given by a function which is a function of space and time u(x,t) .

To visualize the concept of wave, consider two dimensions, space and time. If you fix the time, eg t1, you will get a function of x:

U(x) = U(x,t=t1) 

However, at a particular point of space, x1, the wave is a function of time:

U(t) = U(x=x1, t)

This should help you to understand how the wave propagates. In order to find a solution, you need to impose some initial and boundary conditions to restrict all the possibles waves to the one you are interested in. For this particular case:

  • I = I(xi) is the initial force that we will apply to get the perturbation/wave going.
  • The term f = f(x,t) accounts for any external force that generates waves.
  • c is wave velocity; it is a constant (assuming the medium is homogeneous).
  • L is the length of the domain where we want to solve the PDE; also a constant.
  • n is the number of grid points in space.
  • dt is a time step.
  • tstop is the stop time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM