Add constructor for t_corr in gmx_msd
authorKevin Boyd <kevin.boyd@uconn.edu>
Sat, 27 Oct 2018 15:27:47 +0000 (11:27 -0400)
committerKevin Boyd <kevin.boyd@uconn.edu>
Mon, 29 Oct 2018 21:13:00 +0000 (17:13 -0400)
Moves initialization of t_corr from an init_corr function into
a constructor, keeps all functionality. This is in preparation
to replace c-style arrays within t_corr with stl containers.

A few fields weren't initialized in the original function, added
initialization for them

Made *t_corr into a smart pointer

refs #2368

Change-Id: I37ba12474c5dc3ce989fd1ff369fb50337461750

src/gromacs/gmxana/gmx_msd.cpp

index ec7feb678f3c5790602819a41d4160b14025df60..0ffe400b984f6491d1ea6bfa4c037fc12b59e09b 100644 (file)
@@ -41,6 +41,7 @@
 
 #include "gromacs/commandline/pargs.h"
 #include "gromacs/commandline/viewit.h"
+#include "gromacs/compat/make_unique.h"
 #include "gromacs/fileio/confio.h"
 #include "gromacs/fileio/trxio.h"
 #include "gromacs/fileio/xvgr.h"
@@ -68,7 +69,7 @@ typedef enum {
     NOT_USED, NORMAL, X, Y, Z, LATERAL
 } msd_type;
 
-typedef struct {
+struct t_corr {
     real          t0;         /* start time and time increment between  */
     real          delta_t;    /* time between restart points */
     real          beginfit,   /* the begin/end time for fits as reals between */
@@ -94,82 +95,81 @@ typedef struct {
     int          *n_offs;
     int         **ndata;      /* the number of msds (particles/mols) per data
                                  point. */
-} t_corr;
-
-typedef real t_calc_func (t_corr *curr, int nx, const int index[], int nx0, rvec xc[],
-                          const rvec dcom, gmx_bool bTen, matrix mat);
-
-static real thistime(t_corr *curr)
-{
-    return curr->time[curr->nframes];
-}
-
-static int in_data(t_corr *curr, int nx00)
-{
-    return curr->nframes-curr->n_offs[nx00];
-}
-
-static t_corr *init_corr(int nrgrp, int type, int axis, real dim_factor,
-                         int nmol, gmx_bool bTen, gmx_bool bMass, real dt, const t_topology *top,
-                         real beginfit, real endfit)
-{
-    t_corr  *curr;
-    int      i;
-
-    snew(curr, 1);
-    curr->type       = static_cast<msd_type>(type);
-    curr->axis       = axis;
-    curr->ngrp       = nrgrp;
-    curr->nrestart   = 0;
-    curr->delta_t    = dt;
-    curr->beginfit   = (1 - 2*GMX_REAL_EPS)*beginfit;
-    curr->endfit     = (1 + 2*GMX_REAL_EPS)*endfit;
-    curr->x0         = nullptr;
-    curr->n_offs     = nullptr;
-    curr->nframes    = 0;
-    curr->nlast      = 0;
-    curr->dim_factor = dim_factor;
-
-    snew(curr->ndata, nrgrp);
-    snew(curr->data, nrgrp);
-    if (bTen)
+    t_corr(int nrgrp, int type, int axis, real dim_factor, int nmol,
+           gmx_bool bTen, gmx_bool bMass, real dt, const t_topology *top,
+           real beginfit, real endfit) :
+        t0(0),
+        delta_t(dt),
+        beginfit((1 - 2*GMX_REAL_EPS)*beginfit),
+        endfit((1 + 2*GMX_REAL_EPS)*endfit),
+        dim_factor(dim_factor),
+        data(nullptr),
+        time(nullptr),
+        mass(nullptr),
+        datam(nullptr),
+        x0(nullptr),
+        com(nullptr),
+        lsq(nullptr),
+        type(static_cast<msd_type>(type)),
+        axis(axis),
+        ncoords(0),
+        nrestart(0),
+        nmol(nmol),
+        nframes(0),
+        nlast(0),
+        ngrp(nrgrp),
+        n_offs(nullptr),
+        ndata(nullptr)
     {
-        snew(curr->datam, nrgrp);
-    }
-    for (i = 0; (i < nrgrp); i++)
-    {
-        curr->ndata[i] = nullptr;
-        curr->data[i]  = nullptr;
+        snew(ndata, nrgrp);
+        snew(data, nrgrp);
         if (bTen)
         {
-            curr->datam[i] = nullptr;
+            snew(datam, nrgrp);
         }
-    }
-    curr->time = nullptr;
-    curr->lsq  = nullptr;
-    curr->nmol = nmol;
-    if (curr->nmol > 0)
-    {
-        snew(curr->mass, curr->nmol);
-        for (i = 0; i < curr->nmol; i++)
+        for (int i = 0; (i < nrgrp); i++)
+        {
+            ndata[i] = nullptr;
+            data[i]  = nullptr;
+            if (bTen)
+            {
+                datam[i] = nullptr;
+            }
+        }
+        if (nmol > 0)
         {
-            curr->mass[i] = 1;
+            snew(mass, nmol);
+            for (int i = 0; i < nmol; i++)
+            {
+                mass[i] = 1;
+            }
         }
-    }
-    else
-    {
-        if (bMass)
+        else
         {
-            const t_atoms *atoms = &top->atoms;
-            snew(curr->mass, atoms->nr);
-            for (i = 0; (i < atoms->nr); i++)
+            if (bMass)
             {
-                curr->mass[i] = atoms->atom[i].m;
+                const t_atoms *atoms = &top->atoms;
+                snew(mass, atoms->nr);
+                for (int i = 0; (i < atoms->nr); i++)
+                {
+                    mass[i] = atoms->atom[i].m;
+                }
             }
         }
     }
+};
 
-    return curr;
+typedef real t_calc_func (t_corr *curr, int nx, const int index[], int nx0, rvec xc[],
+                          const rvec dcom, gmx_bool bTen, matrix mat);
+
+static real thistime(t_corr *curr)
+{
+    return curr->time[curr->nframes];
+}
+
+static int in_data(t_corr *curr, int nx00)
+{
+    return curr->nframes-curr->n_offs[nx00];
 }
 
 static void corr_print(t_corr *curr, gmx_bool bTen, const char *fn, const char *title,
@@ -878,17 +878,17 @@ static void do_corr(const char *trx_file, const char *ndx_file, const char *msd_
                     int type, real dim_factor, int axis,
                     real dt, real beginfit, real endfit, const gmx_output_env_t *oenv)
 {
-    t_corr        *msd;
-    int           *gnx;   /* the selected groups' sizes */
-    int          **index; /* selected groups' indices */
-    char         **grpname;
-    int            i, i0, i1, j, N, nat_trx;
-    real          *DD, *SigmaD, a, a2, b, r, chi2;
-    rvec          *x = nullptr;
-    matrix         box;
-    int           *gnx_com     = nullptr; /* the COM removal group size  */
-    int          **index_com   = nullptr; /* the COM removal group atom indices */
-    char         **grpname_com = nullptr; /* the COM removal group name */
+    std::unique_ptr<t_corr> msd;
+    int                    *gnx;   /* the selected groups' sizes */
+    int                   **index; /* selected groups' indices */
+    char                  **grpname;
+    int                     i, i0, i1, j, N, nat_trx;
+    real                   *DD, *SigmaD, a, a2, b, r, chi2;
+    rvec                   *x = nullptr;
+    matrix                  box;
+    int                    *gnx_com     = nullptr; /* the COM removal group size  */
+    int                   **index_com   = nullptr; /* the COM removal group atom indices */
+    char                  **grpname_com = nullptr; /* the COM removal group name */
 
     snew(gnx, nrgrp);
     snew(index, nrgrp);
@@ -912,12 +912,12 @@ static void do_corr(const char *trx_file, const char *ndx_file, const char *msd_
         index_atom2mol(&gnx[0], index[0], &top->mols);
     }
 
-    msd = init_corr(nrgrp, type, axis, dim_factor,
-                    mol_file == nullptr ? 0 : gnx[0], bTen, bMW, dt, top,
-                    beginfit, endfit);
+    msd = gmx::compat::make_unique<t_corr>(nrgrp, type, axis, dim_factor,
+                                           mol_file == nullptr ? 0 : gnx[0],
+                                           bTen, bMW, dt, top, beginfit, endfit);
 
     nat_trx =
-        corr_loop(msd, trx_file, top, ePBC, mol_file ? gnx[0] != 0 : false, gnx, index,
+        corr_loop(msd.get(), trx_file, top, ePBC, mol_file ? gnx[0] != 0 : false, gnx, index,
                   (mol_file != nullptr) ? calc1_mol : (bMW ? calc1_mw : calc1_norm),
                   bTen, gnx_com, index_com, dt, t_pdb,
                   pdb_file ? &x : nullptr, box, oenv);
@@ -948,7 +948,7 @@ static void do_corr(const char *trx_file, const char *ndx_file, const char *msd_
         {
             snew(top->atoms.pdbinfo, top->atoms.nr);
         }
-        printmol(msd, mol_file, pdb_file, index[0], top, x, ePBC, box, oenv);
+        printmol(msd.get(), mol_file, pdb_file, index[0], top, x, ePBC, box, oenv);
         top->atoms.nr = i;
     }
 
@@ -1021,7 +1021,7 @@ static void do_corr(const char *trx_file, const char *ndx_file, const char *msd_
         }
     }
     /* Print mean square displacement */
-    corr_print(msd, bTen, msd_file,
+    corr_print(msd.get(), bTen, msd_file,
                "Mean Square Displacement",
                "MSD (nm\\S2\\N)",
                msd->time[msd->nframes-1], beginfit, endfit, DD, SigmaD, grpname, oenv);