Merge branch 'master' into pygromacs
[alexxy/gromacs.git] / src / gromacs / gmxana / gmx_wham.cpp
index 3b6c2c161593d16fb8072d80eb26ed59fdfad059..6b883607cc13c01491dcfbfad2153a50bb1a530a 100644 (file)
 
 #include "config.h"
 
-#include <ctype.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+#include <cctype>
+#include <cmath>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
 
 #include <algorithm>
 #include <sstream>
@@ -400,7 +401,7 @@ void setup_tab(const char *fn, t_UmbrellaOptions *opt)
     }
     for (i = 0; i < nl-1; i++)
     {
-        if  (fabs(y[0][i+1]-y[0][i]-opt->tabDz) > opt->tabDz/1e6)
+        if  (std::abs(y[0][i+1]-y[0][i]-opt->tabDz) > opt->tabDz/1e6)
         {
             gmx_fatal(FARGS, "z-values in %s are not equally spaced.\n", ny, fn);
         }
@@ -431,13 +432,13 @@ void read_pdo_header(FILE * file, t_UmbrellaHeader * header, t_UmbrellaOptions *
     }
     ist.str(line);
     ist >> Buffer0 >> Buffer1 >> Buffer2;
-    if (strcmp(Buffer1, "UMBRELLA"))
+    if (std::strcmp(Buffer1, "UMBRELLA"))
     {
         gmx_fatal(FARGS, "This does not appear to be a valid pdo file. Found %s, expected %s\n"
                   "(Found in first line: `%s')\n",
                   Buffer1, "UMBRELLA", line);
     }
-    if (strcmp(Buffer2, "3.0"))
+    if (std::strcmp(Buffer2, "3.0"))
     {
         gmx_fatal(FARGS, "This does not appear to be a version 3.0 pdo file");
     }
@@ -511,7 +512,7 @@ void read_pdo_header(FILE * file, t_UmbrellaHeader * header, t_UmbrellaOptions *
     }
     ist.str(line);
     ist >> Buffer3;
-    if (strcmp(Buffer3, "#####") != 0)
+    if (std::strcmp(Buffer3, "#####") != 0)
     {
         gmx_fatal(FARGS, "Expected '#####', found %s. Hick.\n", Buffer3);
     }
@@ -528,7 +529,7 @@ static char *fgets3(FILE *fp, char ptr[], int *len)
         return NULL;
     }
     p = ptr;
-    while ((strchr(ptr, '\n') == NULL) && (!feof(fp)))
+    while ((std::strchr(ptr, '\n') == NULL) && (!feof(fp)))
     {
         /* This line is longer than len characters, let's increase len! */
         *len += STRLEN;
@@ -539,7 +540,7 @@ static char *fgets3(FILE *fp, char ptr[], int *len)
             break;
         }
     }
-    slen = strlen(ptr);
+    slen = std::strlen(ptr);
     if (ptr[slen-1] == '\n')
     {
         ptr[slen-1] = '\0';
@@ -560,7 +561,7 @@ void read_pdo_data(FILE * file, t_UmbrellaHeader * header,
                    gmx_bool bGetMinMax, real *mintmp, real *maxtmp)
 {
     int                i, inttemp, bins, count, ntot;
-    real               min, max, minfound = 1e20, maxfound = -1e20;
+    real               minval, maxval, minfound = 1e20, maxfound = -1e20;
     double             temp, time, time0 = 0, dt;
     char              *ptr    = 0;
     t_UmbrellaWindow * window = 0;
@@ -572,9 +573,9 @@ void read_pdo_data(FILE * file, t_UmbrellaHeader * header,
 
     if (!bGetMinMax)
     {
-        bins = opt->bins;
-        min  = opt->min;
-        max  = opt->max;
+        bins    = opt->bins;
+        minval  = opt->min;
+        maxval  = opt->max;
 
         window = win+fileno;
         /* Need to alocate memory and set up structure */
@@ -624,7 +625,7 @@ void read_pdo_data(FILE * file, t_UmbrellaHeader * header,
     {
         minfound = 1e20;
         maxfound = -1e20;
-        min      = max = bins = 0; /* Get rid of warnings */
+        minval   = maxval = bins = 0; /* Get rid of warnings */
     }
 
     count = 0;
@@ -633,14 +634,14 @@ void read_pdo_data(FILE * file, t_UmbrellaHeader * header,
     {
         trim(ptr);
 
-        if (ptr[0] == '#' || strlen(ptr) < 2)
+        if (ptr[0] == '#' || std::strlen(ptr) < 2)
         {
             continue;
         }
 
         /* Initiate format string */
         fmtign[0] = '\0';
-        strcat(fmtign, "%*s");
+        std::strcat(fmtign, "%*s");
 
         sscanf(ptr, fmtlf, &time); /* printf("Time %f\n",time); */
         /* Round time to fs */
@@ -679,9 +680,9 @@ void read_pdo_data(FILE * file, t_UmbrellaHeader * header,
         {
             for (i = 0; i < header->nPull; ++i)
             {
-                strcpy(fmt, fmtign);
-                strcat(fmt, "%lf");      /* Creating a format stings such as "%*s...%*s%lf" */
-                strcat(fmtign, "%*s");   /* ignoring one more entry in the next loop */
+                std::strcpy(fmt, fmtign);
+                std::strcat(fmt, "%lf");      /* Creating a format stings such as "%*s...%*s%lf" */
+                std::strcat(fmtign, "%*s");   /* ignoring one more entry in the next loop */
                 if (sscanf(ptr, fmt, &temp))
                 {
                     temp += header->UmbPos[i][0];
@@ -710,10 +711,10 @@ void read_pdo_data(FILE * file, t_UmbrellaHeader * header,
                             window->ztime[i][ntot] = temp;
                         }
 
-                        temp -= min;
-                        temp /= (max-min);
+                        temp -= minval;
+                        temp /= (maxval-minval);
                         temp *= bins;
-                        temp  = floor(temp);
+                        temp  = std::floor(temp);
 
                         inttemp = static_cast<int> (temp);
                         if (opt->bCycl)
@@ -796,7 +797,7 @@ double tabulated_pot(double dist, t_UmbrellaOptions *opt)
     int    jl, ju;
     double pl, pu, dz, dp;
 
-    jl = static_cast<int> (floor((dist-opt->tabMin)/opt->tabDz));
+    jl = static_cast<int> (std::floor((dist-opt->tabMin)/opt->tabDz));
     ju = jl+1;
     if (jl < 0 || ju >= opt->tabNbins)
     {
@@ -882,8 +883,8 @@ void setup_acc_wham(double *profile, t_UmbrellaWindow * window, int nWindows,
                 {
                     U = tabulated_pot(distance, opt);            /* Use tabulated potential     */
                 }
-                contrib1                 = profile[k]*exp(-U/(8.314e-3*opt->Temperature));
-                contrib2                 = window[i].N[j]*exp(-U/(8.314e-3*opt->Temperature) + window[i].z[j]);
+                contrib1                 = profile[k]*std::exp(-U/(8.314e-3*opt->Temperature));
+                contrib2                 = window[i].N[j]*std::exp(-U/(8.314e-3*opt->Temperature) + window[i].z[j]);
                 window[i].bContrib[j][k] = (contrib1 > wham_contrib_lim || contrib2 > wham_contrib_lim);
                 bAnyContrib              = (bAnyContrib | window[i].bContrib[j][k]);
                 if (window[i].bContrib[j][k])
@@ -973,7 +974,7 @@ void calc_profile(double *profile, t_UmbrellaWindow * window, int nWindows,
                     {
                         U = tabulated_pot(distance, opt);            /* Use tabulated potential     */
                     }
-                    denom += invg*window[j].N[k]*exp(-U/(8.314e-3*opt->Temperature) + window[j].z[k]);
+                    denom += invg*window[j].N[k]*std::exp(-U/(8.314e-3*opt->Temperature) + window[j].z[k]);
                 }
             }
             profile[i] = num/denom;
@@ -1036,18 +1037,18 @@ double calc_z(double * profile, t_UmbrellaWindow * window, int nWindows,
                     {
                         U = tabulated_pot(distance, opt);            /* Use tabulated potential     */
                     }
-                    total += profile[k]*exp(-U/(8.314e-3*opt->Temperature));
+                    total += profile[k]*std::exp(-U/(8.314e-3*opt->Temperature));
                 }
                 /* Avoid floating point exception if window is far outside min and max */
                 if (total != 0.0)
                 {
-                    total = -log(total);
+                    total = -std::log(total);
                 }
                 else
                 {
                     total = 1000.0;
                 }
-                temp = fabs(total - window[i].z[j]);
+                temp = std::abs(total - window[i].z[j]);
                 if (temp > maxloc)
                 {
                     maxloc = temp;
@@ -1091,7 +1092,7 @@ void symmetrizeProfile(double* profile, t_UmbrellaOptions *opt)
         z    = min+(i+0.5)*dz;
         zsym = -z;
         /* bin left of zsym */
-        j = static_cast<int> (floor((zsym-min)/dz-0.5));
+        j = static_cast<int> (std::floor((zsym-min)/dz-0.5));
         if (j >= 0 && (j+1) < bins)
         {
             /* interpolate profile linearly between bins j and j+1 */
@@ -1107,7 +1108,7 @@ void symmetrizeProfile(double* profile, t_UmbrellaOptions *opt)
         }
     }
 
-    memcpy(profile, prof2, bins*sizeof(double));
+    std::memcpy(profile, prof2, bins*sizeof(double));
     sfree(prof2);
 }
 
@@ -1148,7 +1149,7 @@ void prof_normalization_and_unit(double * profile, t_UmbrellaOptions *opt)
     {
         if (profile[i] > 0.0)
         {
-            profile[i] = -log(profile[i])*unit_factor;
+            profile[i] = -std::log(profile[i])*unit_factor;
         }
     }
 
@@ -1245,9 +1246,9 @@ void calc_cumulatives(t_UmbrellaWindow *window, int nWindows,
 
     if (opt->bs_verbose)
     {
-        snew(fn, strlen(fnhist)+10);
-        snew(buf, strlen(fnhist)+10);
-        sprintf(fn, "%s_cumul.xvg", strncpy(buf, fnhist, strlen(fnhist)-4));
+        snew(fn, std::strlen(fnhist)+10);
+        snew(buf, std::strlen(fnhist)+10);
+        sprintf(fn, "%s_cumul.xvg", std::strncpy(buf, fnhist, std::strlen(fnhist)-4));
         fp = xvgropen(fn, "CDFs of umbrella windows", xlabel, "CDF", opt->oenv);
     }
 
@@ -1363,11 +1364,11 @@ void create_synthetic_histo(t_UmbrellaWindow *synthWindow, t_UmbrellaWindow *thi
                 "cannot be predicted. You have 3 options:\n"
                 "1) Make g_wham estimate the ACTs (options -ac and -acsig).\n"
                 "2) Calculate the ACTs by yourself (e.g. with g_analyze) and provide them\n");
-        strcat(errstr,
-               "   with option -iiact for all umbrella windows.\n"
-               "3) If all ACTs are identical and know, you can define them with -bs-tau.\n"
-               "   Use option (3) only if you are sure what you're doing, you may severely\n"
-               "   underestimate the error if a too small ACT is given.\n");
+        std::strcat(errstr,
+                    "   with option -iiact for all umbrella windows.\n"
+                    "3) If all ACTs are identical and know, you can define them with -bs-tau.\n"
+                    "   Use option (3) only if you are sure what you're doing, you may severely\n"
+                    "   underestimate the error if a too small ACT is given.\n");
         gmx_fatal(FARGS, errstr);
     }
 
@@ -1406,9 +1407,9 @@ void create_synthetic_histo(t_UmbrellaWindow *synthWindow, t_UmbrellaWindow *thi
        Note: The ACT of the flat distribution and of the generated histogram is not
        100% exactly tau, but near tau (my test was 3.8 instead of 4).
      */
-    a        = exp(-1.0/tausteps);
-    ap       = sqrt(1-a*a);
-    invsqrt2 = 1./sqrt(2.0);
+    a        = std::exp(-1.0/tausteps);
+    ap       = std::sqrt(1.0-a*a);
+    invsqrt2 = 1.0/std::sqrt(2.0);
 
     /* init random sequence */
     x = gmx_rng_gaussian_table(opt->rng);
@@ -1441,7 +1442,7 @@ void create_synthetic_histo(t_UmbrellaWindow *synthWindow, t_UmbrellaWindow *thi
             y    = gmx_rng_gaussian_table(opt->rng);
             x    = a*x+ap*y;
             z    = x*sig+mu;
-            ibin = static_cast<int> (floor((z-opt->min)/opt->dz));
+            ibin = static_cast<int> (std::floor((z-opt->min)/opt->dz));
             if (opt->bCycl)
             {
                 if (ibin < 0)
@@ -1487,15 +1488,15 @@ void print_histograms(const char *fnhist, t_UmbrellaWindow * window, int nWindow
 
     if (bs_index >= 0)
     {
-        snew(fn, strlen(fnhist)+10);
-        snew(buf, strlen(fnhist)+1);
-        sprintf(fn, "%s_bs%d.xvg", strncpy(buf, fnhist, strlen(fnhist)-4), bs_index);
+        snew(fn, std::strlen(fnhist)+10);
+        snew(buf, std::strlen(fnhist)+1);
+        sprintf(fn, "%s_bs%d.xvg", std::strncpy(buf, fnhist, std::strlen(fnhist)-4), bs_index);
         sprintf(title, "Umbrella histograms. Bootstrap #%d", bs_index);
     }
     else
     {
         fn = gmx_strdup(fnhist);
-        strcpy(title, "Umbrella histograms");
+        std::strcpy(title, "Umbrella histograms");
     }
 
     fp   = xvgropen(fn, title, xlabel, "count", opt->oenv);
@@ -1504,7 +1505,7 @@ void print_histograms(const char *fnhist, t_UmbrellaWindow * window, int nWindow
     /* Write histograms */
     for (l = 0; l < bins; ++l)
     {
-        fprintf(fp, "%e\t", (double)(l+0.5)*opt->dz+opt->min);
+        fprintf(fp, "%e\t", (l+0.5)*opt->dz+opt->min);
         for (i = 0; i < nWindows; ++i)
         {
             for (j = 0; j < window[i].nPull; ++j)
@@ -1717,7 +1718,7 @@ void do_bootstrapping(const char *fnres, const char* fnprof, const char *fnhist,
         i         = 0;
         bExact    = FALSE;
         maxchange = 1e20;
-        memcpy(bsProfile, profile, opt->bins*sizeof(double)); /* use profile as guess */
+        std::memcpy(bsProfile, profile, opt->bins*sizeof(double)); /* use profile as guess */
         do
         {
             if ( (i%opt->stepUpdateContrib) == 0)
@@ -1772,7 +1773,7 @@ void do_bootstrapping(const char *fnres, const char* fnprof, const char *fnhist,
         bsProfiles_av [i] /= opt->nBootStrap;
         bsProfiles_av2[i] /= opt->nBootStrap;
         tmp                = bsProfiles_av2[i]-sqr(bsProfiles_av[i]);
-        stddev             = (tmp >= 0.) ? sqrt(tmp) : 0.; /* Catch rouding errors */
+        stddev             = (tmp >= 0.) ? std::sqrt(tmp) : 0.; /* Catch rouding errors */
         fprintf(fp, "%e\t%e\t%e\n", (i+0.5)*opt->dz+opt->min, bsProfiles_av [i], stddev);
     }
     xvgrclose(fp);
@@ -1783,16 +1784,16 @@ void do_bootstrapping(const char *fnres, const char* fnprof, const char *fnhist,
 int whaminFileType(char *fn)
 {
     int len;
-    len = strlen(fn);
-    if (strcmp(fn+len-3, "tpr") == 0)
+    len = std::strlen(fn);
+    if (std::strcmp(fn+len-3, "tpr") == 0)
     {
         return whamin_tpr;
     }
-    else if (strcmp(fn+len-3, "xvg") == 0 || strcmp(fn+len-6, "xvg.gz") == 0)
+    else if (std::strcmp(fn+len-3, "xvg") == 0 || std::strcmp(fn+len-6, "xvg.gz") == 0)
     {
         return whamin_pullxf;
     }
-    else if (strcmp(fn+len-3, "pdo") == 0 || strcmp(fn+len-6, "pdo.gz") == 0)
+    else if (std::strcmp(fn+len-3, "pdo") == 0 || std::strcmp(fn+len-6, "pdo.gz") == 0)
     {
         return whamin_pdo;
     }
@@ -1816,7 +1817,7 @@ void read_wham_in(const char *fn, char ***filenamesRet, int *nfilesRet,
     sizenow = 0;
     while (fgets(tmp, sizeof(tmp), fp) != NULL)
     {
-        if (strlen(tmp) >= WHAM_MAXFILELEN)
+        if (std::strlen(tmp) >= WHAM_MAXFILELEN)
         {
             gmx_fatal(FARGS, "Filename too long in %s. Only %d characters allowed.\n", fn, WHAM_MAXFILELEN);
         }
@@ -1830,11 +1831,11 @@ void read_wham_in(const char *fn, char ***filenamesRet, int *nfilesRet,
             }
         }
         /* remove newline if there is one */
-        if (tmp[strlen(tmp)-1] == '\n')
+        if (tmp[std::strlen(tmp)-1] == '\n')
         {
-            tmp[strlen(tmp)-1] = '\0';
+            tmp[std::strlen(tmp)-1] = '\0';
         }
-        strcpy(filename[nread], tmp);
+        std::strcpy(filename[nread], tmp);
         if (opt->verbose)
         {
             printf("Found file %s in %s\n", filename[nread], fn);
@@ -1853,7 +1854,7 @@ FILE *open_pdo_pipe(const char *fn, t_UmbrellaOptions *opt, gmx_bool *bPipeOpen)
     static gmx_bool bFirst = 1;
 
     /* gzipped pdo file? */
-    if ((strcmp(fn+strlen(fn)-3, ".gz") == 0))
+    if ((std::strcmp(fn+std::strlen(fn)-3, ".gz") == 0))
     {
         /* search gunzip executable */
         if (!(Path = getenv("GMX_PATH_GZIP")))
@@ -2172,7 +2173,7 @@ double dist_ndim(double **dx, int ndim, int line)
     {
         r2 += sqr(dx[i][line]);
     }
-    return sqrt(r2);
+    return std::sqrt(r2);
 }
 
 //! Read pullx.xvg or pullf.xvg
@@ -2459,7 +2460,7 @@ void read_pull_xf(const char *fn, const char *fntpr, t_UmbrellaHeader * header,
                         window->ztime[gUsed][ntot] = pos;
                     }
 
-                    ibin = static_cast<int> (floor((pos-min)/(max-min)*bins));
+                    ibin = static_cast<int> (std::floor((pos-min)/(max-min)*bins));
                     if (opt->bCycl)
                     {
                         if (ibin < 0)
@@ -2641,7 +2642,7 @@ void smoothIact(t_UmbrellaWindow *window, int nwins, t_UmbrellaOptions *opt)
     siglim  = 3.0*opt->sigSmoothIact;
     siglim2 = dsqr(siglim);
     /* pre-factor of Gaussian */
-    gaufact    = 1.0/(sqrt(2*M_PI)*opt->sigSmoothIact);
+    gaufact    = 1.0/(std::sqrt(2*M_PI)*opt->sigSmoothIact);
     invtwosig2 = 0.5/dsqr(opt->sigSmoothIact);
 
     for (i = 0; i < nwins; i++)
@@ -2659,7 +2660,7 @@ void smoothIact(t_UmbrellaWindow *window, int nwins, t_UmbrellaOptions *opt)
                     dpos2 = dsqr(window[j].pos[jg]-pos);
                     if (dpos2 < siglim2)
                     {
-                        w       = gaufact*exp(-dpos2*invtwosig2);
+                        w       = gaufact*std::exp(-dpos2*invtwosig2);
                         weight += w;
                         tausm  += w*window[j].tau[jg];
                         /*printf("Weight %g dpos2=%g pos=%g gaufact=%g invtwosig2=%g\n",
@@ -2878,7 +2879,7 @@ void averageSigma(t_UmbrellaWindow *window, int nwins)
                 diff  = ztime[k]-av;
                 sum2 += diff*diff;
             }
-            sig                = sqrt(sum2/ntot);
+            sig                = std::sqrt(sum2/ntot);
             window[i].aver[ig] = av;
 
             /* Note: This estimate for sigma is biased from the limited sampling.
@@ -3038,7 +3039,7 @@ void guessPotByIntegration(t_UmbrellaWindow *window, int nWindows, t_UmbrellaOpt
             for (ig = 0; ig < window[i].nPull; ig++)
             {
                 hispos = window[i].pos[ig];
-                dist   = fabs(hispos-pos);
+                dist   = std::abs(hispos-pos);
                 /* average force within bin */
                 if (dist < dz/2)
                 {
@@ -3096,7 +3097,7 @@ void guessPotByIntegration(t_UmbrellaWindow *window, int nWindows, t_UmbrellaOpt
      */
     for (j = 0; j < opt->bins; ++j)
     {
-        pot[j] = exp(-pot[j]/(8.314e-3*opt->Temperature));
+        pot[j] = std::exp(-pot[j]/(8.314e-3*opt->Temperature));
     }
     calc_z(pot, window, nWindows, opt, TRUE);
 
@@ -3110,7 +3111,7 @@ static int wordcount(char *ptr)
     int i, n, is[2];
     int cur = 0;
 
-    if (strlen(ptr) == 0)
+    if (std::strlen(ptr) == 0)
     {
         return 0;
     }
@@ -3163,8 +3164,8 @@ void readPullGroupSelection(t_UmbrellaOptions *opt, char **fnTpr, int nTpr)
         fmtign[0] = '\0';
         for (i = 0; i < n; i++)
         {
-            strcpy(fmt, fmtign);
-            strcat(fmt, "%d");
+            std::strcpy(fmt, fmtign);
+            std::strcat(fmt, "%d");
             if (sscanf(ptr, fmt, &temp))
             {
                 opt->groupsel[iline].bUse[i] = (temp > 0);
@@ -3173,7 +3174,7 @@ void readPullGroupSelection(t_UmbrellaOptions *opt, char **fnTpr, int nTpr)
                     opt->groupsel[iline].nUse++;
                 }
             }
-            strcat(fmtign, "%*s");
+            std::strcat(fmtign, "%*s");
         }
         iline++;
     }
@@ -3646,7 +3647,7 @@ int gmx_wham(int argc, char *argv[])
                        xlabel, "count", opt.oenv);
     for (l = 0; l < opt.bins; ++l)
     {
-        fprintf(histout, "%e\t", (double)(l+0.5)/opt.bins*(opt.max-opt.min)+opt.min);
+        fprintf(histout, "%e\t", (l+0.5)/opt.bins*(opt.max-opt.min)+opt.min);
         for (i = 0; i < nwins; ++i)
         {
             for (j = 0; j < window[i].nPull; ++j)
@@ -3736,13 +3737,13 @@ int gmx_wham(int argc, char *argv[])
     if (opt.bLog)
     {
         prof_normalization_and_unit(profile, &opt);
-        strcpy(ylabel, en_unit_label[opt.unit]);
-        strcpy(title, "Umbrella potential");
+        std::strcpy(ylabel, en_unit_label[opt.unit]);
+        std::strcpy(title, "Umbrella potential");
     }
     else
     {
-        strcpy(ylabel, "Density of states");
-        strcpy(title, "Density of states");
+        std::strcpy(ylabel, "Density of states");
+        std::strcpy(title, "Density of states");
     }
 
     /* symmetrize profile around z=0? */
@@ -3755,7 +3756,7 @@ int gmx_wham(int argc, char *argv[])
     profout = xvgropen(opt2fn("-o", NFILE, fnm), title, xlabel, ylabel, opt.oenv);
     for (i = 0; i < opt.bins; ++i)
     {
-        fprintf(profout, "%e\t%e\n", (double)(i+0.5)/opt.bins*(opt.max-opt.min)+opt.min, profile[i]);
+        fprintf(profout, "%e\t%e\n", (i+0.5)/opt.bins*(opt.max-opt.min)+opt.min, profile[i]);
     }
     xvgrclose(profout);
     printf("Wrote %s\n", opt2fn("-o", NFILE, fnm));