Merge branch 'master' into pygromacs
[alexxy/gromacs.git] / src / gromacs / gmxana / gmx_angle.cpp
similarity index 91%
rename from src/gromacs/gmxana/gmx_angle.c
rename to src/gromacs/gmxana/gmx_angle.cpp
index a12593a7b19b40b9b79eeca02d5c795342e6580a..7b149acb4b7d1a5a129ced173bf66f97478c782c 100644 (file)
  */
 #include "gmxpre.h"
 
-#include <math.h>
-#include <string.h>
+#include <cmath>
+#include <cstring>
+
+#include <algorithm>
 
 #include "gromacs/commandline/pargs.h"
 #include "gromacs/correlationfunctions/autocorr.h"
@@ -54,6 +56,7 @@
 #include "gromacs/topology/index.h"
 #include "gromacs/utility/fatalerror.h"
 #include "gromacs/utility/futil.h"
+#include "gromacs/utility/gmxassert.h"
 #include "gromacs/utility/smalloc.h"
 
 static void dump_dih_trr(int nframes, int nangles, real **dih, const char *fn,
@@ -84,7 +87,12 @@ static void dump_dih_trr(int nframes, int nangles, real **dih, const char *fn,
         {
             for (m = 0; (m < 2); m++)
             {
-                x[k][l] = (m == 0) ? cos(dih[j][i]) : sin(dih[j][i]);
+                // This is just because the compler and static-analyzer cannot
+                // know that dih[j][i] is always valid. Since it occurs in the innermost
+                // loop over angles and will only trigger on coding errors, we
+                // only enable it for debug builds.
+                GMX_ASSERT(dih != NULL && dih[j] != NULL, "Incorrect dihedral array data");
+                x[k][l] = (m == 0) ? std::cos(dih[j][i]) : std::sin(dih[j][i]);
                 l++;
                 if (l == DIM)
                 {
@@ -144,23 +152,22 @@ int gmx_g_angle(int argc, char *argv[])
     };
 
     FILE              *out;
-    real               tmp, dt;
-    int                status, isize;
+    real               dt;
+    int                isize;
     atom_id           *index;
     char              *grpname;
-    real               maxang, Jc, S2, norm_fac, maxstat;
+    real               maxang, S2, norm_fac, maxstat;
     unsigned long      mode;
     int                nframes, maxangstat, mult, *angstat;
-    int                i, j, total, nangles, natoms, nat2, first, last, angind;
+    int                i, j, nangles, first, last;
     gmx_bool           bAver, bRb, bPeriodic,
-                       bFrac,                           /* calculate fraction too?  */
-                       bTrans,                          /* worry about transtions too? */
-                       bCorr;                           /* correlation function ? */
-    real         t, aa, aver, aver2, aversig, fraction; /* fraction trans dihedrals */
+                       bFrac,          /* calculate fraction too?  */
+                       bTrans,         /* worry about transtions too? */
+                       bCorr;          /* correlation function ? */
+    real         aver, aver2, aversig; /* fraction trans dihedrals */
     double       tfrac = 0;
     char         title[256];
     real       **dih = NULL;      /* mega array with all dih. angles at all times*/
-    char         buf[80];
     real        *time, *trans_frac, *aver_angle;
     t_filenm     fnm[] = {
         { efTRX, "-f", NULL,  ffREAD  },
@@ -190,6 +197,9 @@ int gmx_g_angle(int argc, char *argv[])
     mult   = 4;
     maxang = 360.0;
     bRb    = FALSE;
+
+    GMX_RELEASE_ASSERT(opt[0] != NULL, "Internal option inconsistency; opt[0]==NULL after processing");
+
     switch (opt[0][0])
     {
         case 'a':
@@ -218,7 +228,7 @@ int gmx_g_angle(int argc, char *argv[])
     }
 
     /* Calculate bin size */
-    maxangstat = (int)(maxang/binwidth+0.5);
+    maxangstat = static_cast<int>(maxang/binwidth+0.5);
     binwidth   = maxang/maxangstat;
 
     rd_index(ftp2fn(efNDX, NFILE, fnm), 1, &isize, &index, &grpname);
@@ -295,7 +305,7 @@ int gmx_g_angle(int argc, char *argv[])
                     if (bPBC)
                     {
                         real dd = dih[j][i];
-                        fprintf(out, "  %8.3f", atan2(sin(dd), cos(dd))*RAD2DEG);
+                        fprintf(out, "  %8.3f", std::atan2(std::sin(dd), std::cos(dd))*RAD2DEG);
                     }
                     else
                     {
@@ -406,9 +416,9 @@ int gmx_g_angle(int argc, char *argv[])
         aver  += RAD2DEG*aver_angle[i];
         aver2 += sqr(RAD2DEG*aver_angle[i]);
     }
-    aver   /= (real) nframes;
-    aver2  /= (real) nframes;
-    aversig = sqrt(aver2-sqr(aver));
+    aver   /= nframes;
+    aver2  /= nframes;
+    aversig = std::sqrt(aver2-sqr(aver));
     printf("Found points in the range from %d to %d (max %d)\n",
            first, last, maxangstat);
     printf(" < angle >  = %g\n", aver);
@@ -440,7 +450,7 @@ int gmx_g_angle(int argc, char *argv[])
         maxstat = 0;
         for (i = first; (i <= last); i++)
         {
-            maxstat = max(maxstat, angstat[i]*norm_fac);
+            maxstat = std::max(maxstat, angstat[i]*norm_fac);
         }
         if (output_env_get_print_xvgr_codes(oenv))
         {