Merge branch 'master' into pygromacs
[alexxy/gromacs.git] / src / gromacs / gmxana / gmx_trjconv.cpp
similarity index 95%
rename from src/gromacs/gmxana/gmx_trjconv.c
rename to src/gromacs/gmxana/gmx_trjconv.cpp
index 213eae951b78a56321bea554305c0c56c84f3303..a5e9913ee663bffb0412fd101389c9cff780e5f6 100644 (file)
  */
 #include "gmxpre.h"
 
-#include <math.h>
-#include <stdlib.h>
-#include <string.h>
+#include <cmath>
+#include <cstdlib>
+#include <cstring>
+
+#include <algorithm>
 
 #include "gromacs/commandline/pargs.h"
 #include "gromacs/fileio/confio.h"
@@ -85,7 +87,6 @@ static void calc_pbc_cluster(int ecenter, int nrefat, t_topology *top, int ePBC,
     gmx_bool *bMol, *bTmp;
     rvec     *m_com, *m_shift;
     t_pbc     pbc;
-    real     *com_dist2;
     int      *cluster;
     int      *added;
     int       ncluster, nadded;
@@ -94,7 +95,7 @@ static void calc_pbc_cluster(int ecenter, int nrefat, t_topology *top, int ePBC,
     calc_box_center(ecenter, box, box_center);
 
     /* Initiate the pbc structure */
-    memset(&pbc, 0, sizeof(pbc));
+    std::memset(&pbc, 0, sizeof(pbc));
     set_pbc(&pbc, ePBC, box);
 
     /* Convert atom index to molecular */
@@ -269,7 +270,7 @@ static void put_molecule_com_in_box(int unitcell_enum, int ecenter,
 {
     atom_id i, j;
     int     d;
-    rvec    com, new_com, shift, dx, box_center;
+    rvec    com, new_com, shift, box_center;
     real    m;
     double  mtot;
     t_pbc   pbc;
@@ -332,7 +333,7 @@ static void put_residue_com_in_box(int unitcell_enum, int ecenter,
                                    int natoms, t_atom atom[],
                                    int ePBC, matrix box, rvec x[])
 {
-    atom_id i, j, res_start, res_end, res_nat;
+    atom_id i, j, res_start, res_end;
     int     d, presnr;
     real    m;
     double  mtot;
@@ -350,7 +351,6 @@ static void put_residue_com_in_box(int unitcell_enum, int ecenter,
         {
             /* calculate final COM */
             res_end = i;
-            res_nat = res_end - res_start;
             svmul(1.0/mtot, com, com);
 
             /* check if COM is outside box */
@@ -445,7 +445,7 @@ static void mk_filenm(char *base, const char *ext, int ndigit, int file_nr,
     char nbuf[128];
     int  nd = 0, fnr;
 
-    strcpy(out_file, base);
+    std::strcpy(out_file, base);
     fnr = file_nr;
     do
     {
@@ -456,11 +456,11 @@ static void mk_filenm(char *base, const char *ext, int ndigit, int file_nr,
 
     if (nd < ndigit)
     {
-        strncat(out_file, "00000000000", ndigit-nd);
+        std::strncat(out_file, "00000000000", ndigit-nd);
     }
     sprintf(nbuf, "%d.", file_nr);
-    strcat(out_file, nbuf);
-    strcat(out_file, ext);
+    std::strcat(out_file, nbuf);
+    std::strcat(out_file, ext);
 }
 
 void check_trr(const char *fn)
@@ -522,7 +522,7 @@ void do_trunc(const char *fn, real t0)
             {
                 gmx_fatal(FARGS, "Error reading user input");
             }
-            if (strcmp(yesno, "YES") == 0)
+            if (std::strcmp(yesno, "YES") == 0)
             {
                 fprintf(stderr, "Once again, I'm gonna DO this...\n");
                 gmx_trr_close(in);
@@ -858,58 +858,57 @@ int gmx_trjconv(int argc, char *argv[])
     };
 #define NPA asize(pa)
 
-    FILE            *out    = NULL;
-    t_trxstatus     *trxout = NULL;
-    t_trxstatus     *trxin;
-    int              ftp, ftpin = 0, file_nr;
-    t_trxframe       fr, frout;
-    int              flags;
-    rvec            *xmem = NULL, *vmem = NULL, *fmem = NULL;
-    rvec            *xp   = NULL, x_shift, hbox, box_center, dx;
-    real             xtcpr, lambda, *w_rls = NULL;
-    int              m, i, d, frame, outframe, natoms, nout, ncent, nre, newstep = 0, model_nr;
+    FILE             *out    = NULL;
+    t_trxstatus      *trxout = NULL;
+    t_trxstatus      *trxin;
+    int               ftp, ftpin = 0, file_nr;
+    t_trxframe        fr, frout;
+    int               flags;
+    rvec             *xmem  = NULL, *vmem = NULL, *fmem = NULL;
+    rvec             *xp    = NULL, x_shift, hbox;
+    real             *w_rls = NULL;
+    int               m, i, d, frame, outframe, natoms, nout, ncent, newstep = 0, model_nr;
 #define SKIP 10
-    t_topology       top;
-    gmx_mtop_t      *mtop  = NULL;
-    gmx_conect       gc    = NULL;
-    int              ePBC  = -1;
-    t_atoms         *atoms = NULL, useatoms;
-    matrix           top_box;
-    atom_id         *index, *cindex;
-    char            *grpnm;
-    int             *frindex, nrfri;
-    char            *frname;
-    int              ifit, irms, my_clust = -1;
-    atom_id         *ind_fit, *ind_rms;
-    char            *gn_fit, *gn_rms;
-    t_cluster_ndx   *clust           = NULL;
-    t_trxstatus    **clust_status    = NULL;
-    int             *clust_status_id = NULL;
-    int              ntrxopen        = 0;
-    int             *nfwritten       = NULL;
-    int              ndrop           = 0, ncol, drop0 = 0, drop1 = 0, dropuse = 0;
-    double         **dropval;
-    real             tshift = 0, t0 = -1, dt = 0.001, prec;
-    gmx_bool         bFit, bFitXY, bPFit, bReset;
-    int              nfitdim;
-    gmx_rmpbc_t      gpbc = NULL;
-    gmx_bool         bRmPBC, bPBCWhole, bPBCcomRes, bPBCcomMol, bPBCcomAtom, bPBC, bNoJump, bCluster;
-    gmx_bool         bCopy, bDoIt, bIndex, bTDump, bSetTime, bTPS = FALSE, bDTset = FALSE;
-    gmx_bool         bExec, bTimeStep = FALSE, bDumpFrame = FALSE, bSetPrec, bNeedPrec;
-    gmx_bool         bHaveFirstFrame, bHaveNextFrame, bSetBox, bSetUR, bSplit = FALSE;
-    gmx_bool         bSubTraj = FALSE, bDropUnder = FALSE, bDropOver = FALSE, bTrans = FALSE;
-    gmx_bool         bWriteFrame, bSplitHere;
-    const char      *top_file, *in_file, *out_file = NULL;
-    char             out_file2[256], *charpt;
-    char            *outf_base = NULL;
-    const char      *outf_ext  = NULL;
-    char             top_title[256], title[256], command[256], filemode[5];
-    int              xdr          = 0;
-    gmx_bool         bWarnCompact = FALSE;
-    const char      *warn;
-    output_env_t     oenv;
-
-    t_filenm         fnm[] = {
+    t_topology        top;
+    gmx_mtop_t       *mtop  = NULL;
+    gmx_conect        gc    = NULL;
+    int               ePBC  = -1;
+    t_atoms          *atoms = NULL, useatoms;
+    matrix            top_box;
+    atom_id          *index, *cindex;
+    char             *grpnm;
+    int              *frindex, nrfri;
+    char             *frname;
+    int               ifit, my_clust = -1;
+    atom_id          *ind_fit;
+    char             *gn_fit;
+    t_cluster_ndx    *clust           = NULL;
+    t_trxstatus     **clust_status    = NULL;
+    int              *clust_status_id = NULL;
+    int               ntrxopen        = 0;
+    int              *nfwritten       = NULL;
+    int               ndrop           = 0, ncol, drop0 = 0, drop1 = 0, dropuse = 0;
+    double          **dropval;
+    real              tshift = 0, t0 = -1, dt = 0.001, prec;
+    gmx_bool          bFit, bPFit, bReset;
+    int               nfitdim;
+    gmx_rmpbc_t       gpbc = NULL;
+    gmx_bool          bRmPBC, bPBCWhole, bPBCcomRes, bPBCcomMol, bPBCcomAtom, bPBC, bNoJump, bCluster;
+    gmx_bool          bCopy, bDoIt, bIndex, bTDump, bSetTime, bTPS = FALSE, bDTset = FALSE;
+    gmx_bool          bExec, bTimeStep = FALSE, bDumpFrame = FALSE, bSetPrec, bNeedPrec;
+    gmx_bool          bHaveFirstFrame, bHaveNextFrame, bSetBox, bSetUR, bSplit = FALSE;
+    gmx_bool          bSubTraj = FALSE, bDropUnder = FALSE, bDropOver = FALSE, bTrans = FALSE;
+    gmx_bool          bWriteFrame, bSplitHere;
+    const char       *top_file, *in_file, *out_file = NULL;
+    char              out_file2[256], *charpt;
+    char             *outf_base = NULL;
+    const char       *outf_ext  = NULL;
+    char              top_title[256], title[256], filemode[5];
+    gmx_bool          bWarnCompact = FALSE;
+    const char       *warn;
+    output_env_t      oenv;
+
+    t_filenm          fnm[] = {
         { efTRX, "-f",   NULL,      ffREAD  },
         { efTRO, "-o",   NULL,      ffWRITE },
         { efTPS, NULL,   NULL,      ffOPTRD },
@@ -957,7 +956,6 @@ int gmx_trjconv(int argc, char *argv[])
         /* parse enum options */
         fit_enum      = nenum(fit);
         bFit          = (fit_enum == efFit || fit_enum == efFitXY);
-        bFitXY        = fit_enum == efFitXY;
         bReset        = (fit_enum == efReset || fit_enum == efResetXY);
         bPFit         = fit_enum == efPFit;
         pbc_enum      = nenum(pbc_opt);
@@ -996,7 +994,6 @@ int gmx_trjconv(int argc, char *argv[])
                         "         only has effect in combination with -pbc %s, %s or %s.\n"
                         "         Ingoring unitcell representation.\n\n",
                         unitcell_opt[0], pbc_opt[2], pbc_opt[3], pbc_opt[4]);
-                bSetUR = FALSE;
             }
         }
         if (bFit && bPBC)
@@ -1034,7 +1031,7 @@ int gmx_trjconv(int argc, char *argv[])
         }
         if (bSeparate || bSplit)
         {
-            outf_ext = strrchr(out_file, '.');
+            outf_ext = std::strrchr(out_file, '.');
             if (outf_ext == NULL)
             {
                 gmx_fatal(FARGS, "Output file name '%s' does not contain a '.'", out_file);
@@ -1111,7 +1108,7 @@ int gmx_trjconv(int argc, char *argv[])
              * the header in such a file is top_title t= ...
              * to prevent a double t=, remove it from top_title
              */
-            if ((charpt = strstr(top_title, " t= ")))
+            if ((charpt = std::strstr(top_title, " t= ")))
             {
                 charpt[0] = '\0';
             }
@@ -1258,7 +1255,7 @@ int gmx_trjconv(int argc, char *argv[])
                 {
                     useatoms.pdbinfo[i]  = atoms->pdbinfo[index[i]];
                 }
-                useatoms.nres        = max(useatoms.nres, useatoms.atom[i].resind+1);
+                useatoms.nres        = std::max(useatoms.nres, useatoms.atom[i].resind+1);
             }
             useatoms.nr = nout;
         }
@@ -1331,7 +1328,7 @@ int gmx_trjconv(int argc, char *argv[])
             }
 
             /* open output for writing */
-            strcpy(filemode, "w");
+            std::strcpy(filemode, "w");
             switch (ftp)
             {
                 case efTNG:
@@ -1558,8 +1555,8 @@ int gmx_trjconv(int argc, char *argv[])
                         drop0 = drop1;
                         drop1++;
                     }
-                    if (fabs(dropval[0][drop0] - fr.time)
-                        < fabs(dropval[0][drop1] - fr.time))
+                    if (std::abs(dropval[0][drop0] - fr.time)
+                        < std::abs(dropval[0][drop1] - fr.time))
                     {
                         dropuse = drop0;
                     }
@@ -1612,8 +1609,8 @@ int gmx_trjconv(int argc, char *argv[])
                         else
                         {
                             /* round() is not C89 compatible, so we do this:  */
-                            bDoIt = bRmod(floor(frout_time+0.5), floor(tzero+0.5),
-                                          floor(delta_t+0.5));
+                            bDoIt = bRmod(std::floor(frout_time+0.5), std::floor(tzero+0.5),
+                                          std::floor(delta_t+0.5));
                         }
                     }
 
@@ -1744,9 +1741,9 @@ int gmx_trjconv(int argc, char *argv[])
                         else
                         {
                             /* round() is not C89 compatible, so we do this: */
-                            bSplitHere = bSplit && bRmod(floor(frout.time+0.5),
-                                                         floor(tzero+0.5),
-                                                         floor(split_t+0.5));
+                            bSplitHere = bSplit && bRmod(std::floor(frout.time+0.5),
+                                                         std::floor(tzero+0.5),
+                                                         std::floor(split_t+0.5));
                         }
                         if (bSeparate || bSplitHere)
                         {