Avoid division by 0 in nbnxn_search
authorBerk Hess <hess@kth.se>
Thu, 31 Jul 2014 11:08:34 +0000 (13:08 +0200)
committerBerk Hess <hess@kth.se>
Thu, 31 Jul 2014 11:08:34 +0000 (13:08 +0200)
Note that this issue did not affect any results.

Change-Id: I3c454783484c416b50808b9512e14072cb749784

src/gromacs/mdlib/nbnxn_search.c
src/gromacs/mdlib/nbnxn_search.h

index 1bc3f05847cd94782e1a67ceae0cc70ef57e3462..dc0b02013f645ed2f44acb66c6509f23e5350cb5 100644 (file)
@@ -419,6 +419,12 @@ static real grid_atom_density(int n, rvec corner0, rvec corner1)
 {
     rvec size;
 
+    if (n == 0)
+    {
+        /* To avoid zero density we use a minimum of 1 atom */
+        n = 1;
+    }
+
     rvec_sub(corner1, corner0, size);
 
     return n/(size[XX]*size[YY]*size[ZZ]);
@@ -439,6 +445,8 @@ static int set_grid_size_xy(const nbnxn_search_t nbs,
 
     if (n > grid->na_sc)
     {
+        assert(atom_density > 0);
+
         /* target cell length */
         if (grid->bSimple)
         {
@@ -1814,7 +1822,8 @@ void nbnxn_put_on_grid(nbnxn_search_t nbs,
         nbs->ePBC = ePBC;
         copy_mat(box, nbs->box);
 
-        if (atom_density >= 0)
+        /* Avoid zero density */
+        if (atom_density > 0)
         {
             grid->atom_density = atom_density;
         }
@@ -1830,12 +1839,21 @@ void nbnxn_put_on_grid(nbnxn_search_t nbs,
          * for the local atoms (dd_zone=0).
          */
         nbs->natoms_nonlocal = a1 - nmoved;
+
+        if (debug)
+        {
+            fprintf(debug, "natoms_local = %5d atom_density = %5.1f\n",
+                    nbs->natoms_local, grid->atom_density);
+        }
     }
     else
     {
         nbs->natoms_nonlocal = max(nbs->natoms_nonlocal, a1);
     }
 
+    /* We always use the home zone (grid[0]) for setting the cell size,
+     * since determining densities for non-local zones is difficult.
+     */
     nc_max_grid = set_grid_size_xy(nbs, grid,
                                    dd_zone, n-nmoved, corner0, corner1,
                                    nbs->grid[0].atom_density);
index 12576a096c94e4f235fc01abc1359478753613fa..6b3ab7c8d2c8744dad189d8c331cfbc02b14c79a 100644 (file)
@@ -65,7 +65,7 @@ void nbnxn_init_search(nbnxn_search_t    * nbs_ptr,
 /* Put the atoms on the pair search grid.
  * Only atoms a0 to a1 in x are put on the grid.
  * The atom_density is used to determine the grid size.
- * When atom_density=-1, the density is determined from a1-a0 and the corners.
+ * When atom_density<=0, the density is determined from a1-a0 and the corners.
  * With domain decomposition part of the n particles might have migrated,
  * but have not been removed yet. This count is given by nmoved.
  * When move[i] < 0 particle i has migrated and will not be put on the grid.