Python Forum
Calculating distances between adjacent points
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculating distances between adjacent points
#1
I have a code which will calculate the distances between all atoms in a cartesian coordinate system. However, I only want it to calculate it between the adjacent atoms e.g 1-2, 2-3, 3-4, 4-5, etc.
Can anyone recommend a way I can do this ? I have attached code below.
from scipy.spatial import distance

# Load data from file
with open('cartesian.csv') as datafile: 
	contents = [line.split() for line in datafile]

# Extract the xyz coordiantes, if there is an H in the last column
coords = []
for i, item in enumerate(contents):
    #if item[-1] == 'H':
	coords.append([[float(x) for x in item[0:2]], i+1])

# Show results
for i in range(len(coords)):
	for j in range(i+1, len(coords)):
		dist = distance.euclidean(coords[i][0], coords[j][0])
		print('({}, {}): {:.5f}'.format(coords[i][1], coords[j][1], dist))
Here is a small sample of my input file:

atom x y z

1	0.0	0.0	1.2
2	0.0	0.0	2.4
3	0.0	0.0	3.6
4	0.0	0.0	4.8
5	0.0	0.0	6.0
6	0.0	0.0	7.2
7	0.0	0.0	8.4
8	0.0	0.0	9.6
9	0.0	0.0	10.8
10	0.0	0.0	12.0
11	0.0	0.0	13.2
12	0.0	0.0	14.4
13	0.0	0.0	15.6
14	0.0	0.0	16.8
15	0.0	0.0	18.0
16	0.0	0.0	19.2
17	0.0	0.0	20.4
18	0.0	0.0	21.6
19	0.0	0.0	22.8
20	0.0	0.0	24.0
1	2.10602	4.30161	15.4191
2	1.63363	3.84311	14.6922
3	1.0001799999999998	3.49724	14.0046
4	1.4663	2.88937	13.3751
5	0.9758450000000001	2.6336	12.8105
6	0.65735	1.4835200000000002	12.7484
7	0.46493	0.5138590000000001	12.9022
8	0.08873760000000001	-0.398863	12.5105
9	0.201499	-1.09372	11.6219
10	0.7903600000000001	-1.72045	11.0056
11	0.241616	-2.2128900000000002	11.9763
12	-0.20944000000000002	-2.29307	11.1821
13	-0.89938	-1.56606	11.1211
14	-1.41711	-2.4558	11.2009
15	-1.8840700000000001	-2.52969	12.2187
16	-1.81385	-1.49946	11.8263
17	-0.789176	-1.79308	12.1988
18	-1.17975	-1.1326	12.8
19	-1.0421200000000002	-0.111174	12.8198
20	-0.39158699999999996	-0.355446	13.5661
1	-0.453186	-0.8214870000000001	19.3475
2	-0.701328	-1.75309	19.3045
3	0.259701	-1.77368	18.8392
4	0.15001099999999998	-0.755324	18.3631
5	0.564654	-0.5530970000000001	17.6175
6	1.14706	-0.608927	16.8936
7	0.984617	-0.22286999999999998	15.9514
8	0.800405	-0.139544	14.949000000000002
9	0.823068	0.11181500000000001	13.8241
10	0.553984	-0.08825939999999999	12.8295
11	-0.23958600000000002	0.412857	12.4171
12	0.34061199999999997	0.7038409999999999	11.6287
13	0.191371	1.40337	10.6792
14	-0.615445	1.38181	9.71448
15	-0.735794	1.7825900000000001	8.84641
16	-0.457958	1.25463	7.86102
17	-0.81839	0.7253310000000001	6.93755
18	-1.0938	0.509904	5.79469
19	-0.45148999999999995	-0.237525	5.35572
Reply
#2
The following is the example for randomly generated data:

import numpy as np
data = np.random.rand(30, 3)
distances = ((data[:-1, ...] - data[1:, ...]) ** 2).sum(axis=1) ** 0.5
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020