1 Replies - 1475 Views - Last Post: 08 March 2013 - 11:17 PM Rate Topic: -----

#1 Fittesbane   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-March 13

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.

import random

##Initialize atoms

atomlist = []

atomnumb = input("number of atoms")

a = open('atoms.xyz','w')






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




    a.write('5           '+str(x)+'            '+str(y)+'             0\n')


Is This A Good Question/Topic? 0
  • +

Replies To: Generating atoms far enough apart

#2 Mekire   User is offline

  • D.I.C Head

Reputation: 118
  • View blog
  • Posts: 216
  • Joined: 11-January 13

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.


Sample output:

I think that does what you are looking for. Probably a better solution though.

This post has been edited by Mekire: 08 March 2013 - 11:27 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1