Guest User

Untitled

a guest
Aug 20th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. #First the standard python list style code:
  2.  
  3. #Calculate the distance between 2 atoms:
  4. def dist(p1,p2):
  5. return sum([(a-b)*(a-b) for a,b in zip(p1,p2)])**0.5
  6.  
  7. #Calculate the angle between 3 atoms:
  8. def ang(p1,p2,p3):
  9. P12=dist(p1,p2)
  10. P13=dist(p1,p3)
  11. P232=dist(p2,p3)**2
  12. x=(P12*P12 + P13*P13 - P232)/(2*P12*P13)
  13. if fabs(fabs(x) - 1.0) <= 1e-9:
  14. return 0.0
  15. return acos(x)
  16.  
  17.  
  18. #Now in scipy with numpy.array and weave:
  19.  
  20. #Calculate the distance between 2 atoms:
  21. distcode = """
  22. double c=0.0;
  23. for(int i=0;i<3;i++)
  24. c = c + (a[i]-b[i])*(a[i]-b[i]);
  25. return_val=sqrt(c);
  26. """
  27. def dist(a,b):
  28. return weave.inline(distcode,['a','b'])
  29.  
  30. #Calculate the angle between 3 atoms:
  31. angcode = """
  32. double ang,x,dab=0.0,dac=0.0,dbc=0.0;
  33. for(int i=0;i<3;i++)
  34. dab = dab + (a[i]-b[i])*(a[i]-b[i]);
  35. for(int i=0;i<3;i++)
  36. dac = dac + (a[i]-c[i])*(a[i]-c[i]);
  37. for(int i=0;i<3;i++)
  38. dbc = dbc + (b[i]-c[i])*(b[i]-c[i]);
  39. x=(dab + dac - dbc)/(2.0*sqrt(dab)*sqrt(dac));
  40. if(int(fabs(x*1e9)) == int(1e-9))
  41. return_val=0.0;
  42. else
  43. return_val=acos(x);
  44. """
  45. def ang(a,b,c):
  46. return weave.inline(angcode,['a','b','c'])
Add Comment
Please, Sign In to add comment