Fixes SEGV in gmx order.
authorDavid van der Spoel <spoel@xray.bmc.uu.se>
Wed, 1 Feb 2017 19:06:25 +0000 (20:06 +0100)
committerErik Lindahl <erik.lindahl@gmail.com>
Fri, 3 Feb 2017 21:42:30 +0000 (22:42 +0100)
gmx order used a cumbersome floating point method to compute
an index in a histogram leading to index -1. The present code
is simpler and robust, in fact the old code was likely wrong.

Fixes #2104

Change-Id: Ic3c15917eebe6c4964cd5cb053dfa4f05781cb73

src/gromacs/gmxana/gmx_order.c

index 92e29522779f1f68ea51661fadb272993ae73ebb..b5046f04d76bc54dc1d55a0c346a36a87b3cfb3e 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  * Copyright (c) 2001-2004, The GROMACS development team.
- * Copyright (c) 2013,2014,2015,2016, by the GROMACS development team, led by
+ * Copyright (c) 2013,2014,2015,2016,2017, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -637,16 +637,13 @@ void calc_order(const char *fn, atom_id *index, atom_id *a, rvec **order,
                     z1    = x1[a[index[i-1]+j]][axis];
                     z2    = x1[a[index[i+1]+j]][axis];
                     z_ave = 0.5 * (z1 + z2);
-                    if (z_ave < 0)
+                    slice = (int)((nslices*z_ave)/box[axis][axis]);
+                    while (slice < 0)
                     {
-                        z_ave += box[axis][axis];
-                    }
-                    if (z_ave > box[axis][axis])
-                    {
-                        z_ave -= box[axis][axis];
+                        slice += nslices;
                     }
+                    slice =  slice % nslices;
 
-                    slice  = (int)(0.5 + (z_ave / (*slWidth))) - 1;
                     slCount[slice]++;     /* determine slice, increase count */
 
                     slFrameorder[slice] += 0.5 * (3 * cossum[axis] - 1);