import random
##Initialize atoms
atomlist = []
atomnumb = input("number of atoms")
a = open('atoms.xyz','w')
a.write(str(atomnumb)+'\n')
a.write('\n')
[b]x=3.0*random.random()
y=3.0*random.random()
atomlist.append([x,y])
for n in range(atomnumb):
##Keep atoms from initializing too close together.(<1)
for m in range(len(atomlist)):
while (abs(x-atomlist[m][0])**2)+(abs(y-atomlist[m][1])**2)<1.0:##Pythagorean theorem
x=3.0*random.random()
y=3.0*random.random()
atomlist.append([x,y])[/b]
a.write('5 '+str(x)+' '+str(y)+' 0\n')
a.close()
Generating atoms far enough apart
Page 1 of 11 Replies - 428 Views - Last Post: 08 March 2013 - 11:17 PM
#1
Generating atoms far enough apart
Posted 08 March 2013 - 04:26 PM
I wrote this program to export the coordinates of randomly generated "atoms" to a .xyz file for viewing in Jmol, but the portion of code that I wrote to prevent new atoms from being generated too close to others hasn't worked despite my having tried everything that I can think of(without fail, there is always at least on pair of atoms that are MUCH closer together than 1Å). I'm fairly new to programming and am confident that I made an idiot's mistake somewhere, but hopefully somebody can help me see it. I appreciate your help.
Replies To: Generating atoms far enough apart
#2
Re: Generating atoms far enough apart
Posted 08 March 2013 - 11:17 PM
You are only checking how close your new point is to one other point at a time. If it finds it is too close it generates a new random set of coordinates that have no relation to the previously tested cells.
I wrote a simple version that does what you ask, but by the nature of this it is pretty inefficient. Also as every new cell is completely random as long as it isn't near any preexisting ones, it is possible for the program to be unable to find a solution in which case it never breaks out of the loop. Maybe you can add some constraints to limit how many attempts it will make.
Code:
Sample output:
I think that does what you are looking for. Probably a better solution though.
-Mek
I wrote a simple version that does what you ask, but by the nature of this it is pretty inefficient. Also as every new cell is completely random as long as it isn't near any preexisting ones, it is possible for the program to be unable to find a solution in which case it never breaks out of the loop. Maybe you can add some constraints to limit how many attempts it will make.
Code:
Spoiler
Sample output:
Spoiler
I think that does what you are looking for. Probably a better solution though.
-Mek
This post has been edited by Mekire: 08 March 2013 - 11:27 PM
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|