## Bug #3225

### gmx angle tool erroneously calculate torsion angles (dihedrals) near 180 degrees boundary

**Description**

I found that gmx angle calculate torsion angles erroneously.

This effect is particularly sharp near the 180 degree sign change boundary.

For example, if the measured torsion angles are -179 and 179, then the correct average

of the torsion angles must be 180 (or -180), but not an arithmetic average zero at all.

The following workaround is proposed to solve this problem.

On each step we calculate the delta - an angle of rotation from one state to another in the range [-PI, PI].

One can generally sum the differences from the first point and calculate the average as the first point + (sum of differences)/N

When calculating the average value, the first angle is taken as the base value, and then at each sum accumulation step

we take the delta of the current and basic value given to range [-PI, PI]. This delta is added to the base value.

The base value is updated and added to the middle integrator.

On the example of [-179, 179] degrees:

Base: -179;

Delta = 179 - (-179) = 358;

=> in range [-PI, PI]: Delta =-2

Second point = Base + Delta = -179 + (-2) = -181

Sum = -179 + -181 = -360

Base = -181;

etc.

Average = (-360/2) = -180.

The average given to range [-PI, PI] = 180

On example [-1, 1] degrees:

Base: -1;

Delta = 1 - (-1) = 2;

=> in range [-PI, PI]: Delta = 2

Second point = Base + Delta = -1 + 2 = 1

Sum = -1 + 1 = 0

Base = 1

etc.

Average = 0/2 = 0.

### Associated revisions

Fix formatting error

Fix originally proposed by Boris Timofeev.

Refs #3225

Change-Id: Ie742f2c751473d17fc8f69e9ed79faacdd6fc56c

### History

#### #1 Updated by Boris Timofeev about 2 months ago

See also issue #3206

#### #2 Updated by Boris Timofeev about 2 months ago

More precisely, the error is not in calculating torsion angles, but in calculating their average.

#### #3 Updated by Paul Bauer about 2 months ago

**Status**changed from*New*to*Fix uploaded*

please check out https://gerrit.gromacs.org/c/gromacs/+/14661

#### #4 Updated by Boris Timofeev about 2 months ago

It's ok, as I see

#### #5 Updated by Artem Zhmurov about 2 months ago

Boris Timofeev wrote:

It's ok, as I see

Boris, Can you please try running the fixed version to confirm that it works? Thanks!

#### #6 Updated by Boris Timofeev about 2 months ago

file 6gmy.pdb, chain A, for all models (1-10), for index peparation see #3206

**DATASET**

H2_C2_N3_C4

178.717 -179.92 178.643 178.034 178.979 178.137 178.662 177.061 178.342 179.86

**Before patch**

Select a group: Selected 212: 'H2_C2_N3_C4'

Reading frame 0 time 0.000 '', 701 atoms

Reading frame 9 time 0.000 WARNING: all CONECT records are ignored

Last frame 9 time 0.000

Found points in the range from 356 to 359 (max 360)

< angle > = 142.651

< angle^2 > = 31911.3

Std. Dev. = 107.526

Order parameter S^2 = 0.999753

**After patch**

Select a group: Selected 212: 'H2_C2_N3_C4'

Reading frame 0 time 0.000 '', 701 atoms

Reading frame 9 time 0.000 WARNING: all CONECT records are ignored

Last frame 9 time 0.000

Found points in the range from 356 to 359 (max 360)

< angle > = 178.651

< angle^2 > = 31917

Std. Dev. = 0.829602

Order parameter S^2 = 0.999753

Now it's work fine.

#### #7 Updated by Boris Timofeev about 2 months ago

**File**pdbio.patch pdbio.patch added

And please correct the typo originating from migration from char * to std: string in pdbio.cpp:

#### #8 Updated by Paul Bauer about 1 month ago

**Status**changed from*Fix uploaded*to*Resolved*

Applied in changeset 63a8c47536dbb0867309e527b72f6935449a51be.

#### #9 Updated by Paul Bauer about 1 month ago

**Status**changed from*Resolved*to*Closed*

Fix torsion angle calculation

Fix originally contributed by Boris Timofeev.

Fixes #3225

Change-Id: I4862679e4aeae514736df53f0eff44bf3b85f928