对于matplotlib.animation(FuncAnimation)内部的循环来评估所有点的每一步的变量

时间:2017-10-12 17:23:09

标签: python loops for-loop animation matplotlib

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

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

我改变了“r”(半径),因此颗粒的大小没有变化,因此可以更清楚地观察到浓度对颗粒沉降速度的影响(目前根本不起作用)。但是,for循环中的“c”必须正在执行某些操作,因为动画正在运行(如果没有“c”则无法工作)。

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()

0 个答案:

没有答案