Minor refactoring in vsite_parm
authorPaul Bauer <paul.bauer.q@gmail.com>
Wed, 6 Mar 2019 14:35:29 +0000 (15:35 +0100)
committerMark Abraham <mark.j.abraham@gmail.com>
Fri, 8 Mar 2019 09:44:31 +0000 (10:44 +0100)
Refs #2833

Change-Id: I0658c9c380d4a0723e5a283cd6372f4ad8a6c6b8

src/gromacs/gmxpreprocess/vsite_parm.cpp

index d047c3bb9530cd12dfb8403535e2fa16075ee255..a20d19353ddd0d405d947a30aa4f8bbdc600b64f 100644 (file)
@@ -78,11 +78,13 @@ typedef struct {
     t_param *param;
 } vsitebondparam_t;
 
-typedef struct {
-    int               nr;
-    int               ftype;
-    vsitebondparam_t *vsbp;
-} at2vsitebond_t;
+struct Atom2VsiteBond
+{
+    //! Function type for conversion.
+    int                           ftype;
+    //! The vsite parameters in a list.
+    std::vector<vsitebondparam_t> vSiteBondedParameters;
+};
 
 typedef struct {
     int  nr;
@@ -106,7 +108,7 @@ static int vsite_bond_nrcheck(int ftype)
 }
 
 static void enter_bonded(int nratoms, int *nrbonded, t_mybonded **bondeds,
-                         t_param *param)
+                         const t_param *param)
 {
     int j;
 
@@ -124,21 +126,18 @@ static void enter_bonded(int nratoms, int *nrbonded, t_mybonded **bondeds,
 }
 
 static void get_bondeds(int nrat, const t_iatom atoms[],
-                        at2vsitebond_t *at2vb,
+                        gmx::ArrayRef<const Atom2VsiteBond> at2vb,
                         int *nrbond, t_mybonded **bonds,
                         int *nrang,  t_mybonded **angles,
                         int *nridih, t_mybonded **idihs )
 {
-    int      k, i, ftype, nrcheck;
-    t_param *param;
-
-    for (k = 0; k < nrat; k++)
+    for (int k = 0; k < nrat; k++)
     {
-        for (i = 0; i < at2vb[atoms[k]].nr; i++)
+        for (auto &vsite : at2vb[atoms[k]].vSiteBondedParameters)
         {
-            ftype   = at2vb[atoms[k]].vsbp[i].ftype;
-            param   = at2vb[atoms[k]].vsbp[i].param;
-            nrcheck = vsite_bond_nrcheck(ftype);
+            int            ftype   = vsite.ftype;
+            const t_param *param   = vsite.param;
+            int            nrcheck = vsite_bond_nrcheck(ftype);
             /* abuse nrcheck to see if we're adding bond, angle or idih */
             switch (nrcheck)
             {
@@ -150,23 +149,22 @@ static void get_bondeds(int nrat, const t_iatom atoms[],
     }
 }
 
-static at2vsitebond_t *make_at2vsitebond(int natoms, gmx::ArrayRef<InteractionTypeParameters> plist)
+static std::vector<Atom2VsiteBond>
+make_at2vsitebond(int natoms, gmx::ArrayRef<InteractionTypeParameters> plist)
 {
-    bool           *bVSI;
-    int             ftype, i, j, nrcheck, nr;
-    t_iatom        *aa;
-    at2vsitebond_t *at2vb;
+    bool                       *bVSI;
+    t_iatom                    *aa;
 
-    snew(at2vb, natoms);
+    std::vector<Atom2VsiteBond> at2vb(natoms);
 
     snew(bVSI, natoms);
-    for (ftype = 0; (ftype < F_NRE); ftype++)
+    for (int ftype = 0; (ftype < F_NRE); ftype++)
     {
         if ((interaction_function[ftype].flags & IF_VSITE) && ftype != F_VSITEN)
         {
-            for (i = 0; (i < plist[ftype].nr); i++)
+            for (int i = 0; (i < plist[ftype].nr); i++)
             {
-                for (j = 0; j < NRAL(ftype); j++)
+                for (int j = 0; j < NRAL(ftype); j++)
                 {
                     bVSI[plist[ftype].param[i].a[j]] = TRUE;
                 }
@@ -174,26 +172,21 @@ static at2vsitebond_t *make_at2vsitebond(int natoms, gmx::ArrayRef<InteractionTy
         }
     }
 
-    for (ftype = 0; (ftype < F_NRE); ftype++)
+    for (int ftype = 0; (ftype < F_NRE); ftype++)
     {
-        nrcheck = vsite_bond_nrcheck(ftype);
+        int nrcheck = vsite_bond_nrcheck(ftype);
         if (nrcheck > 0)
         {
-            for (i = 0; (i < plist[ftype].nr); i++)
+            for (int i = 0; (i < plist[ftype].nr); i++)
             {
                 aa = plist[ftype].param[i].a;
-                for (j = 0; j < nrcheck; j++)
+                for (int j = 0; j < nrcheck; j++)
                 {
                     if (bVSI[aa[j]])
                     {
-                        nr = at2vb[aa[j]].nr;
-                        if (nr % 10 == 0)
-                        {
-                            srenew(at2vb[aa[j]].vsbp, nr+10);
-                        }
-                        at2vb[aa[j]].vsbp[nr].ftype = ftype;
-                        at2vb[aa[j]].vsbp[nr].param = &plist[ftype].param[i];
-                        at2vb[aa[j]].nr++;
+                        at2vb[aa[j]].vSiteBondedParameters.emplace_back();
+                        at2vb[aa[j]].vSiteBondedParameters.back().ftype = ftype;
+                        at2vb[aa[j]].vSiteBondedParameters.back().param = &plist[ftype].param[i];
                     }
                 }
             }
@@ -204,20 +197,6 @@ static at2vsitebond_t *make_at2vsitebond(int natoms, gmx::ArrayRef<InteractionTy
     return at2vb;
 }
 
-static void done_at2vsitebond(int natoms, at2vsitebond_t *at2vb)
-{
-    int i;
-
-    for (i = 0; i < natoms; i++)
-    {
-        if (at2vb[i].nr)
-        {
-            sfree(at2vb[i].vsbp);
-        }
-    }
-    sfree(at2vb);
-}
-
 static at2vsitecon_t *make_at2vsitecon(int natoms, gmx::ArrayRef<InteractionTypeParameters> plist)
 {
     bool          *bVSI;
@@ -773,7 +752,6 @@ int set_vsites(bool bVerbose, t_atoms *atoms, gpp_atomtype *atype,
     int             i, j, ftype;
     int             nvsite, nrbond, nrang, nridih, nrset;
     bool            bFirst, bSet, bERROR;
-    at2vsitebond_t *at2vb;
     t_mybonded     *bonds;
     t_mybonded     *angles;
     t_mybonded     *idihs;
@@ -782,7 +760,7 @@ int set_vsites(bool bVerbose, t_atoms *atoms, gpp_atomtype *atype,
     nvsite = 0;
 
     /* Make a reverse list to avoid ninteractions^2 operations */
-    at2vb = make_at2vsitebond(atoms->nr, plist);
+    std::vector<Atom2VsiteBond> at2vb = make_at2vsitebond(atoms->nr, plist);
 
     for (ftype = 0; (ftype < F_NRE); ftype++)
     {
@@ -889,8 +867,6 @@ int set_vsites(bool bVerbose, t_atoms *atoms, gpp_atomtype *atype,
         }         /* if IF_VSITE */
 
     }
-    done_at2vsitebond(atoms->nr, at2vb);
-
     return nvsite;
 }