简体   繁体   English

Python:编写程序模拟沿弦的一维波运动

[英]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).我在“Python Scripting for Computational Science”一书中找到了一个程序,它声称可以描述波动,但我不确定如何实现它(这本书在谷歌图书上,不会向我展示之前/之后的文字代码)。

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?例如,我知道“f”是 x 和 t 的 function,“I”是 x 的 function,但实际上需要什么函数才能产生波?

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.它返回一个数组,其中包含波在时间 tstop 和我们解决方案网格中所有点的值。

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) .此 PDE 或波的解由 function 给出,它是空间和时间u(x,t)的 function。

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:如果你固定时间,例如 t1,你将得到 x 的 function:

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

However, at a particular point of space, x1, the wave is a function of time:然而,在空间的特定点 x1,波是 function 的时间:

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. I = I(xi)是我们将施加的使扰动/波运行的初始力。
  • The term f = f(x,t) accounts for any external force that generates waves.术语f = f(x,t)解释了产生波的任何外力。
  • c is wave velocity; c为波速; it is a constant (assuming the medium is homogeneous).它是一个常数(假设介质是均匀的)。
  • L is the length of the domain where we want to solve the PDE; L是我们要求解 PDE 的域的长度; also a constant.也是一个常数。
  • n is the number of grid points in space. n是空间中的网格点数。
  • dt is a time step. dt是一个时间步长。
  • tstop is the stop time. tstop是停止时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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