Author Topic: How to change positions of atoms by script  (Read 8190 times)

0 Members and 1 Guest are viewing this topic.

Offline serhan

  • Heavy QuantumATK user
  • ***
  • Posts: 98
  • Reputation: 1
    • View Profile
How to change positions of atoms by script
« on: January 24, 2009, 08:48 »
Hello all, I'm trying to re-position the atoms of any structure (any periodic or molecule configuration) in nanolanguage. In other words, I'm trying to modify coordinates of atoms according to some rule, for example, "increase the x position by 2 Angstrom while keeping other positions unchanged" or "double the x and z coordinate while y coordinate is unchanged", in fact this corresponds the modifying the origin in Atomic Manipulator. Since atom coordinates are given as a list of lists, I have to loop over each element of list of lists. I studied the script below to modify the coordinates of (5,5) nanotube such that "double the x and y coordinate while z coordinate is unchanged". But I could not complete the script. Could you help please?
Code
coordinates = [[  1.23252581e+01,   8.93048313e+00,   0.00000000e+00],
               [  1.16769137e+01,   1.09258818e+01,   1.23148812e+00],
               [  9.97952627e+00,   1.21591059e+01,  -2.22044605e-16],
               [  7.88143998e+00,   1.21591059e+01,   1.23148812e+00],
               [  6.18405251e+00,   1.09258818e+01,  -4.44089210e-16],
               [  5.53570819e+00,   8.93048313e+00,   1.23148812e+00],
               [  6.18405251e+00,   6.93508448e+00,  -8.88178420e-16],
               [  7.88143998e+00,   5.70186030e+00,   1.23148812e+00],
               [  9.97952627e+00,   5.70186030e+00,  -8.88178420e-16],
               [  1.16769137e+01,   6.93508448e+00,   1.23148812e+00],
               [  1.20317643e+01,   1.03112625e+01,  -5.55111512e-17],
               [  1.06278706e+01,   1.18704445e+01,   1.23148812e+00],
               [  8.57563252e+00,   1.23066611e+01,  -2.22044605e-16],
               [  6.65893531e+00,   1.14532926e+01,   1.23148812e+00],
               [  5.60989217e+00,   9.63629652e+00,   0.00000000e+00],
               [  5.82920190e+00,   7.54970376e+00,   1.23148812e+00],
               [  7.23309566e+00,   5.99052179e+00,  -8.88178420e-16],
               [  9.28533373e+00,   5.55430512e+00,   1.23148812e+00],
               [  1.12020309e+01,   6.40767370e+00,  -8.88178420e-16],
               [  1.22510741e+01,   8.22466973e+00,   1.23148812e+00]]

for index1, item1 in enumerate(coordinates):
  for index2, item2 in enumerate(item1):
    if index2==0:
      item_new[index2]=2*item1[index2]
    elif index2==1:
      item_new[index2]=2*item1[index2]
    elif index2==2:
      item_new[index2]=item1[index2]
Regards Serhan

Offline Nordland

  • QuantumATK Staff
  • Supreme QuantumATK Wizard
  • *****
  • Posts: 812
  • Reputation: 18
    • View Profile
Re: How to change positions of atoms by script
« Reply #1 on: January 24, 2009, 12:00 »
In this case they are many ways to do it. I have selected two of the ways you can do since I think they are the fastest and easy to see what is going on.
Code
new_coordinates = [ (2*xyz[0], 2*xyz[1], xyz[2]) for xyz in coordinates ]
Alternative If you are into numpy - ( and everyone should be :) ) then you can do this way also.
Code
new_coordinates_numpy = numpy.array(coordinates) * numpy.array([2,2,1])

Offline serhan

  • Heavy QuantumATK user
  • ***
  • Posts: 98
  • Reputation: 1
    • View Profile
Re: How to change positions of atoms by script
« Reply #2 on: January 24, 2009, 13:14 »
Thank you very much  :)

Offline Anders Blom

  • QuantumATK Staff
  • Supreme QuantumATK Wizard
  • *****
  • Posts: 5576
  • Country: dk
  • Reputation: 96
    • View Profile
    • QuantumATK at Synopsys
Re: How to change positions of atoms by script
« Reply #3 on: January 25, 2009, 14:11 »
Numpy is indeed our friend :) To take the other example, "increase the x position by 2 Angstrom while keeping other positions unchanged", we would do
Code
import numpy
new_coordinates = numpy.array(coordinates) + numpy.array([2,0,0])
This also works with units in NanoLanguage, by the way:
Code
import numpy
coordinates = [[1,2,3],[4,5,6]]*Angstrom
new_coordinates = numpy.array(coordinates) + numpy.array([2,0,0])*Angstrom
« Last Edit: January 25, 2009, 17:12 by Anders Blom »

Offline serhan

  • Heavy QuantumATK user
  • ***
  • Posts: 98
  • Reputation: 1
    • View Profile
Re: How to change positions of atoms by script
« Reply #4 on: January 25, 2009, 17:11 »
Thanks  ;)