From 9d54488b2f17e503e2252336ed17d0483ff713c5 Mon Sep 17 00:00:00 2001 From: Paul Bauer Date: Wed, 12 Sep 2018 11:40:22 +0200 Subject: [PATCH] Properly initialize idef in topologies 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 | 38 +++++++++++++++++++++++++++++ src/gromacs/topology/idef.h | 14 +++++++++++ src/gromacs/topology/topology.cpp | 40 ++++++++++--------------------- src/gromacs/topology/topology.h | 11 +++++++++ 4 files changed, 75 insertions(+), 28 deletions(-) diff --git a/src/gromacs/topology/idef.cpp b/src/gromacs/topology/idef.cpp index 2274ef87fc..5739cacef0 100644 --- a/src/gromacs/topology/idef.cpp +++ b/src/gromacs/topology/idef.cpp @@ -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); +} diff --git a/src/gromacs/topology/idef.h b/src/gromacs/topology/idef.h index 3ebb53784c..9059e089e4 100644 --- a/src/gromacs/topology/idef.h +++ b/src/gromacs/topology/idef.h @@ -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 diff --git a/src/gromacs/topology/topology.cpp b/src/gromacs/topology/topology.cpp index df6e857c57..568dbee391 100644 --- a/src/gromacs/topology/topology.cpp +++ b/src/gromacs/topology/topology.cpp @@ -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); } diff --git a/src/gromacs/topology/topology.h b/src/gromacs/topology/topology.h index 5655e73d7a..16eb265bd9 100644 --- a/src/gromacs/topology/topology.h +++ b/src/gromacs/topology/topology.h @@ -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); -- 2.22.0