KDTree在matplotlib.animation期间计数

时间:2017-10-08 00:02:22

标签: python animation matplotlib kdtree particles

我正在尝试找到使用KDTree.query_ball_point函数查找动画每个步骤中每个粒子的'r'距离内的粒子浓度的最佳方法,并使用此计数的数值在conc等式中。基本上我试图使沉降(斯托克斯)速度成为周围颗粒浓度的函数。

到目前为止,我的尝试没有奏效,下面是我正在使用的代码(不包括我失败的KDTree尝试)

更新:我已经在def“浓度”中添加了以评估动画的每个步骤中最近的相邻粒子的浓度。另外,我已经将“r”(半径)改为恒定,因此颗粒的大小没有变化,因此可以更清楚地观察到相邻颗粒浓度对颗粒沉降速度的影响(目前不适用于所有)。

问题:我无法弄清楚def concentration出了什么问题。似乎有一个错误,因为动画不会运行。

import numpy as np
import matplotlib.pyplot as plt
from pylab import *
from matplotlib.animation import FuncAnimation
import random
import pdb
from scipy import spatial
from scipy.spatial import KDTree

n=5
sigma=0.01


#m = np.random.uniform(n)

pp = 2.56 # pp = particle density (Sphene=3.53) (Feldspar=2.56) 
#(g/cm^3)

pf = 2.7 # pf = fluid density(g/cm^3)

g = 9.8 # g = gravity (m/s^2)

r = 0.003 # r = radius of sphere (meter)

mu = 0.53 # mu = dynamic viscosity of fluid (log10Poise)

rp = 0.0005 #radius around particle to check for nearest neighbor

fig, ax = plt.subplots()
az = plt.axes(xlim=(-1, 1), ylim=(-100, 0))
xdata, ydata = [0.0], [0.0]
ln, = plt.plot([], [], 'ro', animated=True)

#pdb.set_trace()
def v_stokes(pp,pf,g,r,mu):
    top=2*(pp-pf)*g*(r**2)
    bottom=9*mu
    ws=top/bottom
    return ws

def init():
    ax.set_xlim(  -2, 2)
    ax.set_ylim(-10, 0)
    return ln,

def concentration(xdata, ydata, rp):
    pdb.set_trace()
    coords = list(zip(xdata, ydata))
    tree = spatial.KDTree(coords)
    test = np.column_stack([xdata, ydata])
    nnl = tree.query_ball_point(test, rp) #nearest neighbors as a list 
                    #(had tree in here before test but shape was wrong)
    nnt = np.zeros(len(nnl)) #nearest neighbors total
    for i in range(len(nnt)): 
        nnt[i] = len(nnl[i])
    return nnt,

def update(frame):
    global xdata
    global ydata
    global rp
    global concentration
    xdata = xdata + np.random.normal(0, sigma, n)
    #ydata = ydata + np.random.normal(-0.1, sigma, n)
    wss = v_stokes(pp,pf,g,r,mu)
    if frame == 0.0:
        ydata = np.zeros(len(xdata)) #makes the ydata length = xdata at 
                                     #time 0
    cp = concentration(xdata, ydata, rp)
    ydata = ydata + (wss*(1-cp))
    ln.set_data(xdata, ydata)
    return ln,

ani = FuncAnimation(fig, update, frames=np.linspace(0, 2*np.pi, 128),
                init_func=init, blit=True, interval=50)
plt.show()

下面是我开始使用的代码,粒子的基本动画。

import numpy as np
import matplotlib.pyplot as plt
from pylab import *
from matplotlib.animation import FuncAnimation
import random

n=100
sigma=0.1
nt=2000

fig, ax = plt.subplots()
xdata, ydata = [0.0], [0.0]
ln, = plt.plot([], [], 'ro', animated=True)

def init():
    ax.set_xlim(  -5, 5)
    ax.set_ylim(-10, 0)
    return ln,

def update(frame):
    global xdata
    global ydata
    xdata = xdata + np.random.normal(0, sigma, n)
    ydata = ydata + np.random.normal(-0.1, sigma, n)
    ln.set_data(xdata, ydata)
    return ln,

ani = FuncAnimation(fig, update, frames=np.linspace(0, 2*np.pi, 128),
                    init_func=init, blit=True, in)
plt.show()

0 个答案:

没有答案