Properly initialize idef in topologies
authorPaul Bauer <paul.bauer.q@gmail.com>
Wed, 12 Sep 2018 09:40:22 +0000 (11:40 +0200)
committerMark Abraham <mark.j.abraham@gmail.com>
Mon, 17 Sep 2018 15:39:06 +0000 (17:39 +0200)
Adds and uses functions to properly initialize and clean up
idef structure in topologies.

This is preparing for changes that no longer use snew for
allocating topology datastructures.

Change-Id: Iabad6d95bc994f4b292c063572a6d83e7f02036d

src/gromacs/topology/idef.cpp
src/gromacs/topology/idef.h
src/gromacs/topology/topology.cpp
src/gromacs/topology/topology.h

index 2274ef87fcdbef1510add66e35d6a01a62bd2fd6..5739cacef04a902c24d2bd984f6823edf3e0ad79 100644 (file)
@@ -42,6 +42,7 @@
 
 #include "gromacs/topology/ifunc.h"
 #include "gromacs/utility/fatalerror.h"
+#include "gromacs/utility/smalloc.h"
 #include "gromacs/utility/txtdump.h"
 
 static void pr_harm(FILE *fp, const t_iparams *iparams, const char *r, const char *kr)
@@ -450,3 +451,40 @@ void pr_idef(FILE *fp, int indent, const char *title, const t_idef *idef,
         }
     }
 }
+
+void init_idef(t_idef *idef)
+{
+    idef->ntypes           = 0;
+    idef->atnr             = 0;
+    idef->functype         = nullptr;
+    idef->iparams          = nullptr;
+    idef->fudgeQQ          = 0.0;
+    idef->iparams_posres   = nullptr;
+    idef->iparams_fbposres = nullptr;
+    for (int f = 0; f < F_NRE; ++f)
+    {
+        idef->il[f].iatoms          = nullptr;
+        idef->il[f].nalloc          = 0;
+        idef->il[f].nr              = 0;
+        idef->il[f].nr_nonperturbed = 0;
+    }
+    idef->cmap_grid.cmapdata      = nullptr;
+    idef->iparams_posres_nalloc   = 0;
+    idef->iparams_fbposres_nalloc = 0;
+    idef->ilsort                  = 0;
+}
+
+void done_idef(t_idef *idef)
+{
+    sfree(idef->functype);
+    sfree(idef->iparams);
+    sfree(idef->iparams_posres);
+    sfree(idef->iparams_fbposres);
+    for (int f = 0; f < F_NRE; ++f)
+    {
+        sfree(idef->il[f].iatoms);
+    }
+
+    sfree(idef->cmap_grid.cmapdata);
+    init_idef(idef);
+}
index 3ebb53784cd4999fbd5dfa7e9d291fe98918cd50..9059e089e41569e534b88224a4986715bed2110f 100644 (file)
@@ -328,4 +328,18 @@ void pr_ffparams(FILE *fp, int indent, const char *title,
 void pr_idef(FILE *fp, int indent, const char *title, const t_idef *idef,
              gmx_bool bShowNumbers, gmx_bool bShowParameters);
 
+/*! \brief
+ * Properly initialize idef struct.
+ *
+ * \param[in] idef Pointer to idef struct to initialize.
+ */
+void init_idef(t_idef *idef);
+
+/*! \brief
+ * Properly clean up idef struct.
+ *
+ * \param[in] idef Pointer to idef struct to clean up.
+ */
+void done_idef(t_idef *idef);
+
 #endif
index df6e857c57a5eda44afa86c76ec6936d5835f99a..568dbee391cad1f6c2e703dfcca4fd259a1a662b 100644 (file)
@@ -100,6 +100,7 @@ void init_mtop(gmx_mtop_t *mtop)
 void init_top(t_topology *top)
 {
     top->name = nullptr;
+    init_idef(&top->idef);
     init_atom(&(top->atoms));
     init_atomtypes(&(top->atomtypes));
     init_block(&top->cgs);
@@ -170,16 +171,7 @@ gmx_mtop_t::~gmx_mtop_t()
 
 void done_top(t_topology *top)
 {
-    sfree(top->idef.functype);
-    sfree(top->idef.iparams);
-    sfree(top->idef.cmap_grid.cmapdata);
-    for (int f = 0; f < F_NRE; ++f)
-    {
-        sfree(top->idef.il[f].iatoms);
-        top->idef.il[f].iatoms = nullptr;
-        top->idef.il[f].nalloc = 0;
-    }
-
+    done_idef(&top->idef);
     done_atom(&(top->atoms));
 
     /* For GB */
@@ -197,21 +189,13 @@ void done_top_mtop(t_topology *top, gmx_mtop_t *mtop)
     {
         if (top != nullptr)
         {
-            for (int f = 0; f < F_NRE; ++f)
-            {
-                sfree(top->idef.il[f].iatoms);
-                top->idef.il[f].iatoms = nullptr;
-                top->idef.il[f].nalloc = 0;
-            }
+            done_idef(&top->idef);
             done_atom(&top->atoms);
             done_block(&top->cgs);
             done_blocka(&top->excls);
             done_block(&top->mols);
             done_symtab(&top->symtab);
             open_symtab(&mtop->symtab);
-            sfree(top->idef.functype);
-            sfree(top->idef.iparams);
-            sfree(top->idef.cmap_grid.cmapdata);
             done_atomtypes(&(top->atomtypes));
         }
 
@@ -219,23 +203,23 @@ void done_top_mtop(t_topology *top, gmx_mtop_t *mtop)
     }
 }
 
+void init_localtop(gmx_localtop_t *top)
+{
+    init_block(&top->cgs);
+    init_blocka(&top->excls);
+    init_idef(&top->idef);
+    init_atomtypes(&top->atomtypes);
+}
+
 void done_localtop(gmx_localtop_t *top)
 {
     if (top == nullptr)
     {
         return;
     }
-    sfree(top->idef.cmap_grid.cmapdata);
-    sfree(top->idef.functype);
-    sfree(top->idef.iparams);
+    done_idef(&top->idef);
     done_block(&top->cgs);
     done_blocka(&top->excls);
-    for (int f = 0; f < F_NRE; f++)
-    {
-        sfree(top->idef.il[f].iatoms);
-    }
-    sfree(top->idef.iparams_posres);
-    sfree(top->idef.iparams_fbposres);
     done_atomtypes(&top->atomtypes);
 }
 
index 5655e73d7a93f22ab901d7a42c57bf317e6f64fc..16eb265bd9f15b90471051dcd020b4ad36101ec1 100644 (file)
@@ -182,6 +182,17 @@ void done_top(t_topology *top);
 // Frees both t_topology and gmx_mtop_t when the former has been created from
 // the latter.
 void done_top_mtop(t_topology *top, gmx_mtop_t *mtop);
+/*! \brief
+ * Properly initialize local topology.
+ *
+ * \param[in] top Pointer to topology to initialize.
+ */
+void init_localtop(gmx_localtop_t *top);
+/*! \brief
+ * Properly clear up local topology,
+ *
+ * \param[in] top Pointer to topology to clear up.
+ */
 void done_localtop(gmx_localtop_t *top);
 void done_and_sfree_localtop(gmx_localtop_t *top);