タイトルがあいまいなのかもしれません。
numpy と matplotlib を使用して、Python でパーティクル シミュレーターを少し使いこなしました。coloumb、重力、風を実装できました。温度と圧力を追加したいだけですが、最適化前の質問があります (すべての悪の根源)。 )。パーティクルがいつクラッシュするかを確認したい:
Q:ブール条件に基づいて、配列と独自の要素のそれぞれの違いを取得することは可能ですか? ループは避けたい。
例:(x - any element in x) < a
次のようなものを返す必要があります
[True, True, False, True]
x の要素 0、1、3 が条件を満たす場合。
編集:
同等のループは次のようになります。
for i in len(x):
for j in in len(x):
#!= not so important
##earlier question I asked lets me figure that one out
if i!=j:
if x[j] - x[i] < a:
True
numpy 操作は if テストよりもはるかに高速であることに気付きました。これにより、物事が大幅に高速化されました。
誰かがそれを試してみたい場合は、ここにサンプルコードがあります。
#Simple circular box simulator, part of part_sim
#Restructure to import into gravity() or coloumb () or wind() or pressure()
#Or to use all forces: sim_full()
#Note: Implement crashing as backbone to all forces
import numpy as np
import matplotlib.pyplot as plt
N = 1000 #Number of particles
R = 8000 #Radius of box
r = np.random.randint(0,R/2,2*N).reshape(N,2)
v = np.random.randint(-200,200,r.shape)
v_limit = 10000 #Speedlimit
plt.ion()
line, = plt.plot([],'o')
plt.axis([-10000,10000,-10000,10000])
while True:
r_hit = np.sqrt(np.sum(r**2,axis=1))>R #Who let the dogs out, who, who?
r_nhit = ~r_hit
N_rhit = r_hit[r_hit].shape[0]
r[r_hit] = r[r_hit] - 0.1*v[r_hit] #Get the dogs back inside
r[r_nhit] = r[r_nhit] +0.1*v[r_nhit]
#Dogs should turn tail before they crash!
#---
#---crash code here....
#---crash end
#---
vmin, vmax = np.min(v), np.max(v)
#Give the particles a random kick when they hit the wall
v[r_hit] = -v[r_hit] + np.random.randint(vmin, vmax, (N_rhit,2))
#Slow down honey
v_abs = np.abs(v) > v_limit
#Hit the wall at too high v honey? You are getting a speed reduction
v[v_abs] *=0.5
line.set_ydata(r[:,1])
line.set_xdata(r[:,0])
plt.draw()
大きなボックスで高速粒子を簡単に区別できる方法がわかったら、上記のデータポイントに色を追加する予定です。