1

I am new to python and numpy. I can generate cartesian coordinate of atoms. e.g. (CuO)n

I would like to calculate interatomic distance (Euclidean distance) between different species of atoms, e.g. Cu to O vice versa. NOT O to O or Cu to Cu.

This is example of (CuO)n n=2 cartesian coordinate. (For the convenience, (Cu) and (O) notations are added in this question)

 0.000410140    0.000000000 -1.1437349409 (Cu1)
 0.021984617    0.000000005  0.432069216 (Cu2)
 0.021984488    0.000000005  0.432067361 (O1)
-0.043697492    0.000000005  0.432252977(O2)

and as n size increases the two more of cartesian coordinated are generated.

So, my question is how to calculate iterative Euclidean distance, like Cu1 to O1 then Cu2 to O2, then Cu2 to O1, and Cu2 to O2?

a = np.loadtxt({file})
for {} in a:
    d = np.sqrt(np.sum((a[int(x)]-a[int(y)]**2))

n=2 x < n y >= n+1 a[0] to a[3], and a[1] to a[4]

I can see that my weakness in assigning multiple variables in a for loop.

Trial 1

a = np.loadtxt({data})
cation = a[:{n}]
anion = a[{n}:]

d = np.sqrt(np.sum((cation-anion)**2))
print(d)

this value is 1.5809706852417704. Pressumable it is wrong (WHY?)

However, the for loop in below gives all the value, 1.5759499815439955, 1.5766050227405235, 1.859480034843622e-06, 0.0656823660565985

for x in cation:
    for y in anion:
        d = np.sqrt(np.sum((x-y)**2))
    print (d)
DGKang
  • 172
  • 1
  • 13
  • 1
    Hope this helps https://stackoverflow.com/questions/1401712/how-can-the-euclidean-distance-be-calculated-with-numpy?rq=1 – Paul Jan 02 '20 at 12:33
  • @Paul I know how to calculate Euclidean distance I'd like to know iterative for loop on multiple variables – DGKang Jan 02 '20 at 12:42
  • 1
    To be clear, you have all of the data in a single file? How do you know which rows are for Cu and which are for O? Also, it looks like your Cu2 and O1 are extremely close; is that right? – Karl Knechtel Jan 02 '20 at 12:57

2 Answers2

2

The following is one approach to find the Euclidean distance between a list of elements with minimum computation. If you have two lists of CU and O atoms as mentioned in @Jan-Pieter's answer, you can find the distance using:

for atom1 in CUlist:
    print(np.linalg.norm(Olist - atom1, axis=1))

or you can use list comprehension,

distance_matrix=[np.linalg.norm(Olist - atom1, axis=1) for atom1 in CUlist]

What it does is that, It computes the euclidean distance between the whole array of Olist elements with one element from CUlist in each iteration.

Sreekiran A R
  • 3,123
  • 2
  • 20
  • 41
1

You could split the list into 2 lists, one with Cu atoms and one with O atoms. Then you can loop through the Cu-list and calculate the distance to every element in the O-list. This can be easily done using 2 for-loops nested.

for atom1 in CUlist:
   for atom2 in Olist:
      calculatedist(atom1, atom2)
Jan-Pieter
  • 137
  • 2
  • 10