[英]Randomizing (x,y,z) coordinates within a box
I am fairly new to python and in my current assignment it looked at particles in 3D. 我对python相当陌生,在我目前的作业中,它研究3D粒子。
The first part of the question asked to create a program that put identical, non overlapping particles in a cubic lattice. 问题的第一部分要求创建一个程序,该程序将相同的,非重叠的粒子置于立方晶格中。 So my code below just iterates through every combination possible, puts it in an XYZ file.
因此,我下面的代码仅遍历所有可能的组合,并将其放入XYZ文件中。
The xyz file is in this format: xyz文件的格式如下:
1000.000000
comment goes here
H 0.000000 0.000000 0.000000
H 0.000000 0.000000 1.000000
H 0.000000 0.000000 2.000000
H 0.000000 0.000000 3.000000
H 0.000000 0.000000 4.000000
H 0.000000 0.000000 5.000000
H 0.000000 0.000000 6.000000
H 0.000000 0.000000 7.000000
H 0.000000 0.000000 8.000000
The next part which I am stuck on wants me to do the same thing but put them in a random fashion and limit the number of particles that I have. 下一个我要坚持的部分希望我做同样的事情,但是将它们随机放置,并限制我拥有的粒子数量。 This is the code I had for the first part.
这是我第一部分的代码。
import itertools #will help iterating particles
#we must set the values of diameter and Length
L=10
d=.5
#this is the intial coordinates of the first particle
x,y,z = 0,0,0
counter=0
#The particles will be spread across, the below ensures that there is no overlap of particles
with open("question1.xyz","w") as file:
file.write("\ncomment goes here\n") #add comments into the 2nd line of the xyz file
for x,y,z in itertools.product(range(L),repeat = 3):
file.write('H %f %f %f\n' % (x, y, z))
counter=counter+1
#this will put the number of particles as the first line
with open("question1.xyz") as infile:
with open("q1Output.xyz","w") as outfile:
for i,line in enumerate(infile):
if i==0:
outfile.write('%f\n'%counter)
else:
outfile.write(line)
The part I am confused on is how I should randomize my particles/coordinates and ensure there is no overlap. 我困惑的部分是如何随机化粒子/坐标并确保没有重叠。
Any ideas? 有任何想法吗? Thanks in advance!
提前致谢!
**Edit: This is what I have so far, apart of the questions askts to vary L and try to minimize the amount of time it takes to run the program - not sure whats **编辑:到目前为止,这是我所拥有的,除了要改变L并试图最大程度地减少运行程序所需的时间之外,其他所有问题都不知道。
import random
import time
start_time=time.time()
text_file=open("textfile.xyz","w")
text_file.write("512\n")
text_file.write("Comment goes here\n")
L=12.5
d=1
#place particles
for partciles in range(0,512)
proxcheck=1
while proxcheck !=2 #when the particles has been placed, proxcheck will be set=2, and the program will move onto next
x,y,z=random.random()*L,random.random()*L,random.random()*L #random numbers for positions
skipfirsttwolines,proxcheck=0,1;
For line in text_file
skipfirsttwolines=skipfirsttwolines+1;
if skipfirsttwolines>2: #in xyz file we dont look at first two lines
proxcheck=0 #if no overlap particle will be placed
oldvals=line.split(none)
oldx=float(oldvals[1])
oldy=float(oldvals[2])
oldz=float(oldvals[3])
Not sure where to go from here or if I should even take this approach in ensuring no overlap 不知道从这里走到哪里,或者我是否应该采取这种方法来确保没有重叠
@david: @大卫:
This is what I was thinking, any suggestions? 这就是我的想法,有什么建议吗?
x,y,z=[],[],[]
for j in range(0,512):
x.append(0)
y.append(0)
z.append(0)
xyz_line = '\n{0} {1} {2} {3}'.format('O',x[0],y[0],z[0])
f.write(xyz_line)
You can create a random point with the code 您可以使用代码创建一个随机点
import random
p = (random.randint(0, L), random.randint(0, L), random.randint(0, L))
But if you need to prevent having two in the same place, you can do (after setting num_points
to the number of points you want): 但是,如果需要避免在同一位置放置两个,则可以这样做(将
num_points
设置为所需的点数之后):
points = set()
while len(points) < num_points:
p = (random.randint(0, L), random.randint(0, L), random.randint(0, L))
if p not in points:
points.add(p)
To write the resulting points to a file: 要将结果点写入文件:
for p in points:
f.write("%s %s %s\n" % p)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.