Remove exclusions from t_topology
authorBerk Hess <hess@kth.se>
Tue, 10 Dec 2019 14:57:08 +0000 (15:57 +0100)
committerBerk Hess <hess@kth.se>
Thu, 19 Dec 2019 09:39:36 +0000 (10:39 +0100)
Exclusions in t_topology were not used anywhere.
To achieve this, changed convert-tpr to use gmx_localtop_t as an
intermediate instead to t_topology.

Change-Id: I87338ff8d0fac8ccdae0377018b88bc25738f5be

src/gromacs/tools/convert_tpr.cpp
src/gromacs/topology/mtop_util.cpp
src/gromacs/topology/topology.cpp
src/gromacs/topology/topology.h

index d4d4685a5878de5d60f684147358f9ae04935d73..905b3443cb003cd59d9283724f4b3fafc71b4a41 100644 (file)
               "of atoms in the tpr file (%d)",                                                   \
               (i), (n))
 
-static gmx_bool* bKeepIt(int gnx, int natoms, int index[])
+static std::vector<bool> bKeepIt(int gnx, int natoms, int index[])
 {
-    gmx_bool* b;
-    int       i;
+    std::vector<bool> b(natoms);
 
-    snew(b, natoms);
-    for (i = 0; (i < gnx); i++)
+    for (int i = 0; (i < gnx); i++)
     {
         RANGECHK(index[i], natoms);
-        b[index[i]] = TRUE;
+        b[index[i]] = true;
     }
 
     return b;
 }
 
-static int* invind(int gnx, int natoms, int index[])
+static std::vector<int> invind(int gnx, int natoms, int index[])
 {
-    int* inv;
-    int  i;
+    std::vector<int> inv(natoms);
 
-    snew(inv, natoms);
-    for (i = 0; (i < gnx); i++)
+    for (int i = 0; (i < gnx); i++)
     {
         RANGECHK(index[i], natoms);
         inv[index[i]] = i;
@@ -99,61 +95,32 @@ static int* invind(int gnx, int natoms, int index[])
     return inv;
 }
 
-static void reduce_block(const gmx_bool bKeep[], t_block* block, const char* name)
-{
-    int* index;
-    int  i, j, newi, newj;
-
-    snew(index, block->nr);
-
-    newi = newj = 0;
-    for (i = 0; (i < block->nr); i++)
-    {
-        for (j = block->index[i]; (j < block->index[i + 1]); j++)
-        {
-            if (bKeep[j])
-            {
-                newj++;
-            }
-        }
-        if (newj > index[newi])
-        {
-            newi++;
-            index[newi] = newj;
-        }
-    }
-
-    fprintf(stderr, "Reduced block %8s from %6d to %6d index-, %6d to %6d a-entries\n", name,
-            block->nr, newi, block->index[block->nr], newj);
-    block->index = index;
-    block->nr    = newi;
-}
-
-static gmx::ListOfLists<int>
-reduce_blocka(const int invindex[], const gmx_bool bKeep[], const t_blocka& block, const char* name)
+static gmx::ListOfLists<int> reduce_listoflists(gmx::ArrayRef<const int>     invindex,
+                                                const std::vector<bool>&     bKeep,
+                                                const gmx::ListOfLists<int>& src,
+                                                const char*                  name)
 {
     gmx::ListOfLists<int> lists;
 
     std::vector<int> exclusionsForAtom;
-    for (int i = 0; i < block.nr; i++)
+    for (gmx::index i = 0; i < src.ssize(); i++)
     {
         if (bKeep[i])
         {
             exclusionsForAtom.clear();
-            for (int j = block.index[i]; j < block.index[i + 1]; j++)
+            for (const int j : src[i])
             {
-                const int k = block.a[j];
-                if (bKeep[k])
+                if (bKeep[j])
                 {
-                    exclusionsForAtom.push_back(invindex[k]);
+                    exclusionsForAtom.push_back(invindex[j]);
                 }
             }
             lists.pushBack(exclusionsForAtom);
         }
     }
 
-    fprintf(stderr, "Reduced block %8s from %6d to %6zu index-, %6d to %6d a-entries\n", name,
-            block.nr, lists.size(), block.nra, lists.numElements());
+    fprintf(stderr, "Reduced block %8s from %6zu to %6zu index-, %6d to %6d a-entries\n", name,
+            src.size(), lists.size(), src.numElements(), lists.numElements());
 
     return lists;
 }
@@ -214,7 +181,11 @@ static void reduce_atom(int gnx, const int index[], t_atom atom[], char*** atomn
     sfree(rinfo);
 }
 
-static void reduce_ilist(const int invindex[], const gmx_bool bKeep[], t_ilist* il, int nratoms, const char* name)
+static void reduce_ilist(gmx::ArrayRef<const int> invindex,
+                         const std::vector<bool>& bKeep,
+                         t_ilist*                 il,
+                         int                      nratoms,
+                         const char*              name)
 {
     t_iatom* ia;
     int      i, j, newnr;
@@ -255,33 +226,30 @@ static void reduce_ilist(const int invindex[], const gmx_bool bKeep[], t_ilist*
 
 static void reduce_topology_x(int gnx, int index[], gmx_mtop_t* mtop, rvec x[], rvec v[])
 {
-    t_topology top;
-    gmx_bool*  bKeep;
-    int*       invindex;
-    int        i;
+    gmx_localtop_t top;
+    gmx_mtop_generate_local_top(*mtop, &top, false);
+    t_atoms atoms = gmx_mtop_global_atoms(mtop);
 
-    top      = gmx_mtop_t_to_t_topology(mtop, false);
-    bKeep    = bKeepIt(gnx, top.atoms.nr, index);
-    invindex = invind(gnx, top.atoms.nr, index);
+    const std::vector<bool> bKeep    = bKeepIt(gnx, atoms.nr, index);
+    const std::vector<int>  invindex = invind(gnx, atoms.nr, index);
 
-    reduce_block(bKeep, &(top.mols), "mols");
-    gmx::ListOfLists<int> excls = reduce_blocka(invindex, bKeep, top.excls, "excls");
     reduce_rvec(gnx, index, x);
     reduce_rvec(gnx, index, v);
-    reduce_atom(gnx, index, top.atoms.atom, top.atoms.atomname, &(top.atoms.nres), top.atoms.resinfo);
+    reduce_atom(gnx, index, atoms.atom, atoms.atomname, &(atoms.nres), atoms.resinfo);
 
-    for (i = 0; (i < F_NRE); i++)
+    for (int i = 0; (i < F_NRE); i++)
     {
         reduce_ilist(invindex, bKeep, &(top.idef.il[i]), interaction_function[i].nratoms,
                      interaction_function[i].name);
     }
 
-    top.atoms.nr = gnx;
+    atoms.nr = gnx;
 
     mtop->moltype.resize(1);
     mtop->moltype[0].name  = mtop->name;
-    mtop->moltype[0].atoms = top.atoms;
-    for (i = 0; i < F_NRE; i++)
+    mtop->moltype[0].atoms = atoms;
+    mtop->moltype[0].excls = reduce_listoflists(invindex, bKeep, top.excls, "excls");
+    for (int i = 0; i < F_NRE; i++)
     {
         InteractionList& ilist = mtop->moltype[0].ilist[i];
         ilist.iatoms.resize(top.idef.il[i].nr);
@@ -290,14 +258,12 @@ static void reduce_topology_x(int gnx, int index[], gmx_mtop_t* mtop, rvec x[],
             ilist.iatoms[j] = top.idef.il[i].iatoms[j];
         }
     }
-    mtop->moltype[0].atoms = top.atoms;
-    mtop->moltype[0].excls = excls;
 
     mtop->molblock.resize(1);
     mtop->molblock[0].type = 0;
     mtop->molblock[0].nmol = 1;
 
-    mtop->natoms = top.atoms.nr;
+    mtop->natoms = atoms.nr;
 }
 
 static void zeroq(const int index[], gmx_mtop_t* mtop)
index 541788aa602fa85b26c2ca7bfef057f0f1b10545..bdee8d1ea65fc8c89b529d5a22457220234721e0 100644 (file)
@@ -650,47 +650,6 @@ t_atoms gmx_mtop_global_atoms(const gmx_mtop_t* mtop)
  * The cat routines below are old code from src/kernel/topcat.c
  */
 
-static void blockacat(t_blocka* dest, const gmx::ListOfLists<int>& src, int copies, int dnum, int snum)
-{
-    int j, l;
-    int destnr  = dest->nr;
-    int destnra = dest->nra;
-
-    if (!src.empty())
-    {
-        size_t size = (dest->nr + copies * src.size() + 1);
-        srenew(dest->index, size);
-    }
-    gmx::ArrayRef<const int> srcElements = src.elementsView();
-    if (!srcElements.empty())
-    {
-        size_t size = (dest->nra + copies * srcElements.size());
-        srenew(dest->a, size);
-    }
-
-    gmx::ArrayRef<const int> srcListRanges = src.listRangesView();
-    for (l = destnr, j = 0; (j < copies); j++)
-    {
-        for (gmx::index i = 0; i < src.ssize(); i++)
-        {
-            dest->index[l++] = dest->nra + srcListRanges[i];
-        }
-        dest->nra += src.ssize();
-    }
-    for (l = destnra, j = 0; (j < copies); j++)
-    {
-        for (const int srcElement : srcElements)
-        {
-            dest->a[l++] = dnum + srcElement;
-        }
-        dnum += snum;
-        dest->nr += src.ssize();
-    }
-    dest->index[dest->nr] = dest->nra;
-    dest->nalloc_index    = dest->nr;
-    dest->nalloc_a        = dest->nra;
-}
-
 static void ilistcat(int ftype, t_ilist* dest, const InteractionList& src, int copies, int dnum, int snum)
 {
     int nral, c, i, a;
@@ -935,17 +894,14 @@ static void copyAtomtypesFromMtop(const gmx_mtop_t& mtop, t_atomtypes* atomtypes
     }
 }
 
-/*! \brief Copy excls from mtop.
- *
- * Makes a deep copy of excls(t_blocka) from gmx_mtop_t.
- * Used to initialize legacy topology types.
+/*! \brief Generate a single list of lists of exclusions for the whole system
  *
  * \param[in] mtop  Reference to input mtop.
- * \param[in] excls Pointer to final excls data structure.
  */
-static void copyExclsFromMtop(const gmx_mtop_t& mtop, gmx::ListOfLists<int>* excls)
+static gmx::ListOfLists<int> globalExclusionLists(const gmx_mtop_t& mtop)
 {
-    excls->clear();
+    gmx::ListOfLists<int> excls;
+
     int atomIndex = 0;
     for (const gmx_molblock_t& molb : mtop.molblock)
     {
@@ -953,36 +909,13 @@ static void copyExclsFromMtop(const gmx_mtop_t& mtop, gmx::ListOfLists<int>* exc
 
         for (int mol = 0; mol < molb.nmol; mol++)
         {
-            excls->appendListOfLists(molt.excls, atomIndex);
+            excls.appendListOfLists(molt.excls, atomIndex);
 
             atomIndex += molt.atoms.nr;
         }
     }
-}
 
-/*! \brief Copy excls from mtop.
- *
- * Makes a deep copy of excls(t_blocka) from gmx_mtop_t.
- * Used to initialize legacy t_topology.
- *
- * \param[in] mtop  Reference to input mtop.
- * \param[in] excls Pointer to final excls data structure.
- */
-static void copyExclsFromMtop(const gmx_mtop_t& mtop, t_blocka* excls)
-{
-    init_blocka(excls);
-    int natoms = 0;
-    for (const gmx_molblock_t& molb : mtop.molblock)
-    {
-        const gmx_moltype_t& molt = mtop.moltype[molb.type];
-
-        int srcnr  = molt.atoms.nr;
-        int destnr = natoms;
-
-        blockacat(excls, molt.excls, molb.nmol, destnr, srcnr);
-
-        natoms += molb.nmol * srcnr;
-    }
+    return excls;
 }
 
 /*! \brief Updates inter-molecular exclusion lists
@@ -1053,7 +986,7 @@ static void gen_local_top(const gmx_mtop_t& mtop,
 {
     copyAtomtypesFromMtop(mtop, &top->atomtypes);
     copyIdefFromMtop(mtop, &top->idef, freeEnergyInteractionsAtEnd, bMergeConstr);
-    copyExclsFromMtop(mtop, &top->excls);
+    top->excls = globalExclusionLists(mtop);
     if (!mtop.intermolecularExclusionGroup.empty())
     {
         addMimicExclusions(&top->excls, mtop.intermolecularExclusionGroup);
@@ -1127,7 +1060,6 @@ static void gen_t_topology(const gmx_mtop_t& mtop,
 {
     copyAtomtypesFromMtop(mtop, &top->atomtypes);
     copyIdefFromMtop(mtop, &top->idef, freeEnergyInteractionsAtEnd, bMergeConstr);
-    copyExclsFromMtop(mtop, &top->excls);
 
     top->name                        = mtop.name;
     top->atoms                       = gmx_mtop_global_atoms(&mtop);
index 3a1b82eb502faccf6bb25a456a31a7b2c8f33ec1..fbc0d3cef3c6097dfcf400ccfbd27f9ae73ef963 100644 (file)
@@ -71,7 +71,6 @@ void init_top(t_topology* top)
     init_atom(&(top->atoms));
     init_atomtypes(&(top->atomtypes));
     init_block(&top->mols);
-    init_blocka(&top->excls);
     open_symtab(&top->symtab);
 }
 
@@ -111,7 +110,6 @@ void done_top(t_topology* top)
 
     done_symtab(&(top->symtab));
     done_block(&(top->mols));
-    done_blocka(&(top->excls));
 }
 
 void done_top_mtop(t_topology* top, gmx_mtop_t* mtop)
@@ -122,7 +120,6 @@ void done_top_mtop(t_topology* top, gmx_mtop_t* mtop)
         {
             done_idef(&top->idef);
             done_atom(&top->atoms);
-            done_blocka(&top->excls);
             done_block(&top->mols);
             done_symtab(&top->symtab);
             open_symtab(&mtop->symtab);
@@ -362,7 +359,6 @@ void pr_top(FILE* fp, int indent, const char* title, const t_topology* top, gmx_
         pr_block(fp, indent, "mols", &top->mols, bShowNumbers);
         pr_str(fp, indent, "bIntermolecularInteractions",
                gmx::boolToString(top->bIntermolecularInteractions));
-        pr_blocka(fp, indent, "excls", &top->excls, bShowNumbers);
         pr_idef(fp, indent, "idef", &top->idef, bShowNumbers, bShowParameters);
     }
 }
index 0e5c0d9e17e099b4b137f9c270e8450ae7dc3a8e..ac35171337026b1c4c67c1f8cf64e372b0c04c6e 100644 (file)
@@ -229,8 +229,8 @@ typedef struct t_topology
     t_atomtypes atomtypes;                   /* Atomtype properties                  */
     t_block     mols;                        /* The molecules                        */
     gmx_bool    bIntermolecularInteractions; /* Inter.mol. int. ?   */
-    t_blocka    excls;                       /* The exclusions                       */
-    t_symtab    symtab;                      /* The symbol table                     */
+    /* Note that the exclusions are not stored in t_topology */
+    t_symtab symtab; /* The symbol table                     */
 } t_topology;
 
 void init_top(t_topology* top);