나는 쌍 상관 함수를 계산하는 데 사용하는 임의의 위치 벡터를 생성하기위한 효율적인 코드를 생성하려고합니다. 내 상자에 두 점 사이에 허용되는 최소 거리에 대한 제약 조건을 설정하는 간단한 방법이 있는지 궁금합니다. 다음과 같이numpy.random.rand를 사용하여 포인트를 생성 할 때 최소 거리 제한을 설정할 수 있습니까?
내 코드 현재 : 나는 이전에이 만들어진 각 지점에 대한 모든 거리를 계산 한 후 승인 또는 거부하는 루프를 사용하여 시도
def pointRun(number, dr):
"""
Compute the 3D pair correlation function
for a random distribution of 'number' particles
placed into a 1.0x1.0x1.0 box.
"""
## Create array of distances over which to calculate.
r = np.arange(0., 1.0+dr, dr)
## Generate list of arrays to define the positions of all points,
## and calculate number density.
a = np.random.rand(number, 3)
numberDensity = len(a)/1.0**3
## Find reference points within desired region to avoid edge effects.
b = [s for s in a if all(s > 0.4) and all(s < 0.6) ]
## Compute pairwise correlation for each reference particle
dist = scipy.spatial.distance.cdist(a, b, 'euclidean')
allDists = dist[(dist < np.sqrt(3))]
## Create histogram to generate radial distribution function, (RDF) or R(r)
Rr, bins = np.histogram(allDists, bins=r, density=False)
## Make empty containers to hold radii and pair density values.
radii = []
rhor = []
## Normalize RDF values by distance and shell volume to get pair density.
for i in range(len(Rr)):
y = (r[i] + r[i+1])/2.
radii.append(y)
x = np.average(Rr[i])/(4./3.*np.pi*(r[i+1]**3 - r[i]**3))
rhor.append(x)
## Generate normalized pair density function, by total number density
gr = np.divide(rhor, numberDensity)
return radii, gr
. 이 방법은 많은 점을 사용하면 매우 느립니다.
[이 알고리즘] (http://stackoverflow.com/questions/6002407/placing-random-circles-without-overlap-and-without-using-brute-force/6002708)의 변형을 사용할 수 있습니다. # 6002708)하지만주의해야 할 점은 최소 해상도가 있다는 것입니다 (높을수록 비용이 많이 듭니다). – Rufflewind