我正在尝试创建一个程序,将直径为d的相同非重叠粒子放入具有周期性边界条件的立方体(3d)晶格中。
真正这意味着我需要一个程序来创建一个 XYZ 文件,该文件将如下所示,但将经历每个组合:
H 0.0000000.0000005.000000
H 0.0000000.0000006.000000
H 0.0000000.0000007.000000
H 0.0000000.0000008.000000
H 0.0000000.0000009.000000
现在出于某种原因,我下面的代码保持我的 z 值为 0,而不是通过这些值来创建其他组合......相反,它只通过 x 和 y。
#create a file and enter first two lines
text_file=open("question1.xyz","w")
text_file.write("n")
text_file.write("comment goes heren")
L=10
d=1
#first particle or line will be at 0,0,0, counter used to count how many lines
x,y,z = 0,0,0
counter=0
#placing the particles
while x<=L-d:
while y<=L-d:
while z<=L-d:
text_file.write('H ')
text_file.write('%f'%x)
text_file.write('%f'%y)
text_file.write('%fn'%z)
counter=counter+1
z=z+d
z=0
y=y+d
z,y=0,0
x=x+d
text_file.close()
with open("question1.xyz") as infile:
with open("outputfile.xyz","w") as outfile:
for i,line in enumerate(infile):
if i==0:
outfile.write('%fn'%counter)
else:
outfile.write(line)
关于为什么会发生这种情况的任何想法?我的while陈述有点混乱,但我不确定该怎么做
有一个更简单的方法。使用 itertools.product:
import itertools
L = 3
d = 1
counter = 0
with open("question1.xyz","w") as text_file:
text_file.write("ncomment goes heren")
for x,y,z in itertools.product(range(L),repeat = 3):
text_file.write('H %f %f %fn' % (x, y, z))
counter=counter+1