Make sure duplicates bondeds are set from hackblocks
authorErik Lindahl <erik@kth.se>
Mon, 23 Jun 2014 00:01:56 +0000 (02:01 +0200)
committerGerrit Code Review <gerrit@gerrit.gromacs.org>
Tue, 24 Jun 2014 12:24:56 +0000 (14:24 +0200)
Previously the rtp file could end up containing two
entries when the N/C termini databases added interactions.
These were then quicksorted, which could make the
assignment random. We now check for duplicates and
discard the rtp entry in favor of the hackblock.

Fixes #1395.

Change-Id: I505cfbb7e6c8e8050e6e2935cc417e49a515d8d8

src/gromacs/gmxpreprocess/hackblock.c

index 262e308f72da17defb1fbe9bf05a35514b361571..a5cdc7a0afd71dfef7304f7c44add61506eb9635 100644 (file)
@@ -196,6 +196,33 @@ static gmx_bool contains_char(t_rbonded *s, char c)
     return bRet;
 }
 
+gmx_bool rbonded_atoms_exist_in_list(t_rbonded *b, t_rbonded blist[], int nlist, int natoms)
+{
+    int      i, k;
+    gmx_bool matchFound = FALSE;
+    gmx_bool atomsMatch;
+
+    for (i = 0; i < nlist && !matchFound; i++)
+    {
+        atomsMatch = TRUE;
+        for (k = 0; k < natoms && atomsMatch; k++)
+        {
+            atomsMatch = atomsMatch && !strcmp(b->a[k], blist[i].a[k]);
+        }
+        /* Try reverse if forward match did not work */
+        if (!atomsMatch)
+        {
+            atomsMatch = TRUE;
+            for (k = 0; k < natoms && atomsMatch; k++)
+            {
+                atomsMatch = atomsMatch && !strcmp(b->a[k], blist[i].a[natoms-1-k]);
+            }
+        }
+        matchFound = atomsMatch;
+    }
+    return matchFound;
+}
+
 gmx_bool merge_t_bondeds(t_rbondeds s[], t_rbondeds d[], gmx_bool bMin, gmx_bool bPlus)
 {
     int      i, j;
@@ -210,20 +237,26 @@ gmx_bool merge_t_bondeds(t_rbondeds s[], t_rbondeds d[], gmx_bool bMin, gmx_bool
             srenew(d[i].b, d[i].nb + s[i].nb);
             for (j = 0; j < s[i].nb; j++)
             {
-                if (!(bMin && contains_char(&s[i].b[j], '-'))
-                    && !(bPlus && contains_char(&s[i].b[j], '+')))
+                /* Check if this bonded string already exists before adding.
+                 * We are merging from the main rtp to the hackblocks, so this
+                 * will mean the hackblocks overwrite the man rtp, as intended.
+                 */
+                if (!rbonded_atoms_exist_in_list(&s[i].b[j], d[i].b, d[i].nb, btsNiatoms[i]))
                 {
-                    copy_t_rbonded(&s[i].b[j], &d[i].b[ d[i].nb ]);
-                    d[i].nb++;
-                }
-                else if (i == ebtsBONDS)
-                {
-                    bBondsRemoved = TRUE;
+                    if (!(bMin && contains_char(&s[i].b[j], '-'))
+                        && !(bPlus && contains_char(&s[i].b[j], '+')))
+                    {
+                        copy_t_rbonded(&s[i].b[j], &d[i].b[ d[i].nb ]);
+                        d[i].nb++;
+                    }
+                    else if (i == ebtsBONDS)
+                    {
+                        bBondsRemoved = TRUE;
+                    }
                 }
             }
         }
     }
-
     return bBondsRemoved;
 }