I am trying to calculate number of hydrogen bonds for water in a simulation where I have 3000 molecules of water ( 1000 oxygen and 2000 hydrogen ) . So I have written a code for it.
I have a dataframe (df1) in which I oxygen atoms at a position which is a multiple of 3 (0,3,6 ...) and at other position hydrogen atom and the x,y,z coordinates of an atom (xi,yi,zi). Now for HB calculation I need two oxygen atoms whose distance is less than 3.5 i.e. d1<3.5 and one angle which I am calculating should be less than 30 degree i.e. final < pi/6 . Everytime my both conditions are satisfied i'm increasing the count variable by one. But now here I need to keep two checks that between two molecules there can be only one hydrogen bond ( flag is used for it ) and a single molecule can form atmost four HB Bonds ( record dictionary is used for it and in checking this condition error is coming in the last if statement) .
Now I have written the entire logic , but in the ending if statement I am getting an error: KeyError: 3
import math
count=0
record={}
for i in range(0,3000,3):
for j in range (i+3,3000,3):
flag=0
x1=0
y1=0
z1=0
d1=0
x1=df1['xi'][i]-df1['xi'][j]
y1=df1['yi'][i]-df1['yi'][j]
z1=df1['zi'][i]-df1['zi'][j]
d1=math.sqrt((x1**2)+(y1**2)+(z1**2))
if (d1<3.5):
if(flag==0):
for k in range(1,3,1):
x2=0
y2=0
z2=0
d2=0
x2=df1['xi'][i+k]-df1['xi'][i]
y2=df1['yi'][i+k]-df1['yi'][i]
z2=df1['zi'][i+k]-df1['zi'][i]
d2=math.sqrt((x2**2)+(y2**2)+(z2**2))
x3=0
y3=0
z3=0
d3=0
x3=df1['xi'][i+k]-df1['xi'][j]
y3=df1['yi'][i+k]-df1['yi'][j]
z3=df1['zi'][i+k]-df1['zi'][j]
d3=math.sqrt((x3**2)+(y3**2)+(z3**2))
final=0
final=math.acos(((d2**2)+(d3**2)-(d1**2))/(2*(d2*d3)))
if (final<0.523):
if j not in record:
record.update({j:1})
else :
record[j]=record[j]+1
if i not in record:
record.update({i:1})
else :
record[i]=record[i]+1
count=count+1
flag=1
if (flag==0):
for l in range(1,3,1):
x2=0
y2=0
z2=0
d2=0
x2=df1['xi'][i]-df1['xi'][j+l]
y2=df1['yi'][i]-df1['yi'][j+l]
z2=df1['zi'][i]-df1['zi'][j+l]
d2=math.sqrt((x2**2)+(y2**2)+(z2**2))
x3=0
y3=0
z3=0
d3=0
x3=df1['xi'][j]-df1['xi'][j+l]
y3=df1['yi'][j]-df1['yi'][j+l]
z3=df1['zi'][j]-df1['zi'][j+l]
d3=math.sqrt((x3**2)+(y3**2)+(z3**2))
final=0
final=math.acos(((d2**2)+(d3**2)-(d1**2))/(2*(d2*d3)))
if (final<0.523):
if j not in record:
record.update({j:1})
else :
record[j]=record[j]+1
if i not in record:
record.update({i:1})
else :
record[i]=record[i]+1
count=count+1
flag=1
if (record[j]==4 or record[i]==4):
break
else:
continue
Aucun commentaire:
Enregistrer un commentaire