• ## Outreach

From: Alexei Podtelezhnikov (apodtele_at_kgi.edu)
Date: Fri Aug 05 2005 - 17:47:13 CDT

Hi!

atan2 forever! The folowing imlementation of angle function from utilities.C
is faster. 1 cross-prod and 2 dot_prods, seems more than 3 dot_prods, but we get rid of division, and don't use acos, which is slower than atan2.

float angle(const float *a, const float *b) {
float ab[3]

cross_prod(ab, a, b);

float psin = sqrtf(dot_prod(ab, ab));
float pcos = dot_prod(a, b);

return 57.2958f * atan2f(psin, pcos);
}

Alexei

-----Original Message-----
From: John Stone [mailto:johns_at_ks.uiuc.edu]
Sent: Thu 8/4/2005 9:08 AM
To: Alexei Podtelezhnikov
Cc: vmd-l_at_ks.uiuc.edu
Subject: Re: vmd-l: dihedral angles cleaned up

Hi,
Your code looks good, and it ought to be much faster than the old
code. A quick test fed with a few million random coordinates gives
good results so far so I'm inclined to put it in. I think the old
implementation was written that way as a result of one of the old
platforms VMD was originally written on having a deficient math
library, but I think that's long since past now so the atan2 method
should be faster and more reliable.

John Stone
vmd_at_ks.uiuc.edu

On Thu, Aug 04, 2005 at 08:09:38AM -0700, Alexei Podtelezhnikov wrote:
> Hi vmd-l!
>
> May I suggest the following optimization to dihedral angle calculations that relies on atan2f, rather than signed_angle. It's just faster and cleaner.
>
> float dihedral(const float *a1,const float *a2,const float *a3,const float *a4)
> {
> float r1[3], r2[3], r3[3], n1[3], n2[3];
> vec_sub(r1, a2, a1);
> vec_sub(r2, a3, a2);
> vec_sub(r3, a4, a3);
>
> cross_prod(n1, r1, r2);
> cross_prod(n2, r2, r3);
>
> float psin = dot_prod(n1, r3) * sqrtf(dot_prod(r2, r2));
> float pcos = dot_prod(n1, n2);
>
> return 57.2958f * atan2f(psin, pcos);
> }
>
> and we're done. I would drop signed_angle if nothing else uses it.
>
> Thanks for vmd,
> Alexei Podtelezhnikov, Ph.D.

```--
NIH Resource for Macromolecular Modeling and Bioinformatics
Beckman Institute for Advanced Science and Technology
University of Illinois, 405 N. Mathews Ave, Urbana, IL 61801
Email: johns_at_ks.uiuc.edu                 Phone: 217-244-3349
WWW: http://www.ks.uiuc.edu/~johns/      Fax: 217-244-6078
```