Initialize unused velocity constraint components
authorBerk Hess <hess@kth.se>
Fri, 22 Aug 2014 10:37:49 +0000 (12:37 +0200)
committerGerrit Code Review <gerrit@gerrit.gromacs.org>
Sun, 24 Aug 2014 17:20:50 +0000 (19:20 +0200)
With domain decomposition, velocity components for communicated
atoms could be uninitialized. These components were never used, but
this could lead to valgrind warnings and floating point exceptions.

Fixes #1576.

Change-Id: Ic673680699578ddf62c2d240342138cbc16490c8

src/gromacs/mdlib/constr.c

index fa8275083dd126e2334755dcbc796210799e48de..b6878e6bd12107d9bb4faf3e298b2f8c4853a758 100644 (file)
@@ -203,6 +203,18 @@ int n_flexible_constraints(struct gmx_constr *constr)
     return nflexcon;
 }
 
+static void clear_constraint_quantity_nonlocal(gmx_domdec_t *dd, rvec *q)
+{
+    int nonlocal_at_start, nonlocal_at_end, at;
+
+    dd_get_constraint_range(dd, &nonlocal_at_start, &nonlocal_at_end);
+
+    for (at = nonlocal_at_start; at < nonlocal_at_end; at++)
+    {
+        clear_rvec(q[at]);
+    }
+}
+
 void too_many_constraint_warnings(int eConstrAlg, int warncount)
 {
     const char *abort = "- aborting to avoid logfile runaway.\n"
@@ -431,6 +443,15 @@ gmx_bool constrain(FILE *fplog, gmx_bool bLog, gmx_bool bEner,
     if (cr->dd)
     {
         dd_move_x_constraints(cr->dd, box, x, xprime, econq == econqCoord);
+
+        if (v != NULL)
+        {
+            /* We need to initialize the non-local components of v.
+             * We never actually use these values, but we do increment them,
+             * so we should avoid uninitialized variables and overflows.
+             */
+            clear_constraint_quantity_nonlocal(cr->dd, v);
+        }
     }
 
     if (constr->lincsd != NULL)