Verlet algorithm implementation in Python

本秂侑毒 提交于 2019-12-11 04:35:16

问题


I have problem with implementation of verlet algorithm in Python. I tried this code:

x[0] = 1
v[0] = 0
t[0] = 0
a[0] = 1
for i in range (0, 1000):
    x[i+1] = x[i] - v[i] * dt + (a[i] * (dt**2) * 0.5)
    v[i] = (x[i+1] - x[i-1]) * 0.5 * dt
    t[i+1] = t[i] + dt

But it is not working properly. What is wrong? I am looking for general code for Verlet algorithm.


回答1:


Your question isn't very clear, but there are a few sources of error in your code.

Eg, for i > 0

x[i+1] = x[i]-v[i]*dt+(a[i]*(dt**2)*0.5)

tries to use the value of v[i], but that element doesn't exist yet in the v list.

To give a concrete example, when i = 1, you need v[1], but the only thing in the v list at that stage is v[0]; v[1] isn't computed until the following line.

That error should cause the Python interpreter to display an error message:

IndexError: list index out of range

When asking for help on Stack Overflow please post the error messages with your question, preferably starting with the line:

Traceback (most recent call last):

That makes it a lot easier for people reading your question to debug your code.

FWIW, on the first iteration of your for loop, when i == 0, x[i+1] and x[i-1] both refer to the same element, since there are two elements in the x list at that stage, so x[-1] is x[1].

Also, it's strange that you are storing your t values in a list. You don't need to do that. Just store t as a simple float value and increment it by dt each time through the loop; note that t itself isn't used in any of the calculations, but you may want to print it.

Your formulas don't appear to match those given on the Wikipedia page, either for the Basic Störmer–Verlet, or the Velocity Verlet. For example, in the code line I quoted earlier you are subtracting v[i]*dt but you should be adding it.

Perhaps you should consider implementing the related Leapfrog integration method. The synchronised version of Leapfrog is easy to code, and quite effective, IME.

From the Wikipedia link:

x[i+1] = x[i] + v[i] * dt + 0.5 * a[i] * dt * dt
v[i+1] = v[i] + 0.5 * (a[i] + a[i+1]) * dt

Generally, it's not necessary to actually store the a values in a list, since they will be calculated from the other parameters using the relevant force equation.




回答2:


There are several issues with your implementation,

  1. At step 0, you are trying to access index -1 which does not exist, and neither does the index i+1 for the last index.
  2. The acceleration is only defined at the first step and not for the other ones.
  3. There is a sign error in the calculation of the next position.

Something like this should fix it,

import numpy as np
N = 1000
dt  = 0.1 
x = np.zeros(N)
v = np.zeros(N)
t = np.arange(0,(N+0.5)*dt, dt)
a = np.ones(N)*1.0  # initial condition
x[[0,1]] = 1
v[1] = v[0]+a[0]*dt

for i in range (1,N-1):
    x[i+1] = x[i]+v[i]*dt+(a[i]*(dt**2)*0.5)
    v[i+1] = v[i] + a[i]*dt

This won't be terribly efficient though, and it's better to use scipy.integrate.ode to solve the underlying differential equation of motion.



来源:https://stackoverflow.com/questions/29009771/verlet-algorithm-implementation-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!