Basic support for titles within tool help
[alexxy/gromacs.git] / src / gromacs / gmxana / gmx_wham.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014,2015, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37
38 /*! \internal \file
39  *  \brief Implementation of the Weighted Histogram Analysis Method (WHAM)
40  *
41  *  \author Jochen Hub <jhub@gwdg.de>
42  */
43 #include "gmxpre.h"
44
45 #include "config.h"
46
47 #include <ctype.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51
52 #include <sstream>
53
54 #include "gromacs/commandline/pargs.h"
55 #include "gromacs/fileio/tpxio.h"
56 #include "gromacs/fileio/xvgr.h"
57 #include "gromacs/gmxana/gmx_ana.h"
58 #include "gromacs/legacyheaders/copyrite.h"
59 #include "gromacs/legacyheaders/macros.h"
60 #include "gromacs/legacyheaders/names.h"
61 #include "gromacs/legacyheaders/typedefs.h"
62 #include "gromacs/math/vec.h"
63 #include "gromacs/random/random.h"
64 #include "gromacs/utility/cstringutil.h"
65 #include "gromacs/utility/fatalerror.h"
66 #include "gromacs/utility/gmxomp.h"
67 #include "gromacs/utility/smalloc.h"
68
69 //! longest file names allowed in input files
70 #define WHAM_MAXFILELEN 2048
71
72 /*! \brief
73  * x-axis legend for output files
74  */
75 static const char *xlabel = "\\xx\\f{} (nm)";
76
77 /*! \brief
78  * enum for energy units
79  */
80 enum {
81     enSel, en_kJ, en_kCal, en_kT, enNr
82 };
83 /*! \brief
84  * enum for type of input files (pdos, tpr, or pullf)
85  */
86 enum {
87     whamin_unknown, whamin_tpr, whamin_pullxf, whamin_pdo
88 };
89
90 /*! \brief enum for bootstrapping method
91  *
92  * These bootstrap methods are supported:
93  *  - bootstrap complete histograms with continuous weights (Bayesian bootstrap)
94  *    (bsMethod_BayesianHist)
95  *  - bootstrap complete histograms (bsMethod_hist)
96  *  - bootstrap trajectories from given umbrella histograms. This generates new
97  *    "synthetic" histograms (bsMethod_traj)
98  *  - bootstrap trajectories from Gaussian with mu/sigma computed from
99  *    the respective histogram (bsMethod_trajGauss). This gives very similar
100  *    results compared to bsMethod_traj.
101  *
102  *  ********************************************************************
103  *  FOR MORE DETAILS ON THE BOOTSTRAP METHODS (INCLUDING EXAMPLES), SEE
104  *  JS Hub, BL de Groot, D van der Spoel
105  *  g_wham - A free weighted histogram analysis implementation including
106  *  robust error and autocorrelation estimates,
107  *  J Chem Theory Comput, 6(12), 3713-3720 (2010)
108  *  ********************************************************************
109  */
110 enum {
111     bsMethod_unknown, bsMethod_BayesianHist, bsMethod_hist,
112     bsMethod_traj, bsMethod_trajGauss
113 };
114
115
116 //! Parameters of the umbrella potentials
117 typedef struct
118 {
119     /*!
120      * \name Using umbrella pull code since gromacs 4.x
121      */
122     /*!\{*/
123     int      npullcrds_tot; //!< nr of pull coordinates in tpr file
124     int      npullcrds;     //!< nr of umbrella pull coordinates for reading
125     int      pull_geometry; //!< such as distance, direction
126     ivec     pull_dim;      //!< pull dimension with geometry distance
127     int      pull_ndim;     //!< nr of pull_dim != 0
128     gmx_bool bPrintRef;     //!< Coordinates of reference groups written to pullx.xvg ?
129     gmx_bool bPrintComp;    //!< Components of pull distance written to pullx.xvg ?
130     real    *k;             //!< force constants in tpr file
131     real    *init_dist;     //!< reference displacements
132     real    *umbInitDist;   //!< reference displacement in umbrella direction
133     /*!\}*/
134     /*!
135      * \name Using PDO files common until gromacs 3.x
136      */
137     /*!\{*/
138     int    nSkip;
139     char   Reference[256];
140     int    nPull;
141     int    nDim;
142     ivec   Dims;
143     char   PullName[4][256];
144     double UmbPos[4][3];
145     double UmbCons[4][3];
146     /*!\}*/
147 } t_UmbrellaHeader;
148
149 //! Data in the umbrella histograms
150 typedef struct
151 {
152     int      nPull;       //!< nr of pull groups in this pdo or pullf/x file
153     double **Histo;       //!< nPull histograms
154     double **cum;         //!< nPull cumulative distribution functions
155     int      nBin;        //!< nr of bins. identical to opt->bins
156     double  *k;           //!< force constants for the nPull groups
157     double  *pos;         //!< umbrella positions for the nPull groups
158     double  *z;           //!< z=(-Fi/kT) for the nPull groups. These values are iteratively computed during wham
159     int     *N;           //!< nr of data points in nPull histograms.
160     int     *Ntot;        //!< also nr of data points. N and Ntot only differ if bHistEq==TRUE
161
162     /*! \brief  g = 1 + 2*tau[int]/dt where tau is the integrated autocorrelation time.
163      *
164      * Compare, e.g. Ferrenberg/Swendsen, PRL 63:1195 (1989),
165      * Kumar et al, J Comp Chem 13, 1011-1021 (1992), eq. 28
166      */
167     double *g;
168     double *tau;         //!< intetrated autocorrelation time (IACT)
169     double *tausmooth;   //!< smoothed IACT
170
171     double  dt;          //!< timestep in the input data. Can be adapted with g_wham option -dt
172
173     /*! \brief TRUE, if any data point of the histogram is within min and max, otherwise FALSE */
174     gmx_bool **bContrib;
175     real     **ztime;     //!< input data z(t) as a function of time. Required to compute ACTs
176
177     /*! \brief average force estimated from average displacement, fAv=dzAv*k
178      *
179      *  Used for integration to guess the potential.
180      */
181     real   *forceAv;
182     real   *aver;         //!< average of histograms
183     real   *sigma;        //!< stddev of histograms
184     double *bsWeight;     //!< for bootstrapping complete histograms with continuous weights
185 } t_UmbrellaWindow;
186
187 //! Selection of pull groups to be used in WHAM (one structure for each tpr file)
188 typedef struct
189 {
190     int       n;         //!< total nr of pull groups in this tpr file
191     int       nUse;      //!< nr of pull groups used
192     gmx_bool *bUse;      //!< boolean array of size n. =1 if used, =0 if not
193 } t_groupselection;
194
195 //! Parameters of WHAM
196 typedef struct
197 {
198     /*!
199      * \name Input stuff
200      */
201     /*!\{*/
202     const char       *fnTpr, *fnPullf, *fnGroupsel;
203     const char       *fnPdo, *fnPullx;            //!< file names of input
204     gmx_bool          bTpr, bPullf, bPdo, bPullx; //!< input file types given?
205     real              tmin, tmax, dt;             //!< only read input within tmin and tmax with dt
206
207     gmx_bool          bInitPotByIntegration;      //!< before WHAM, guess potential by force integration. Yields 1.5 to 2 times faster convergence
208     int               stepUpdateContrib;          //!< update contribution table every ... iterations. Accelerates WHAM.
209     int               nGroupsel;                  //!< if >0: use only certain group in WHAM, if ==0: use all groups
210     t_groupselection *groupsel;                   //!< for each tpr file: which pull groups to use in WHAM?
211     /*!\}*/
212     /*!
213      * \name Basic WHAM options
214      */
215     /*!\{*/
216     int      bins;                   //!< nr of bins, min, max, and dz of profile
217     real     min, max, dz;
218     real     Temperature, Tolerance; //!< temperature, converged when probability changes less than Tolerance
219     gmx_bool bCycl;                  //!< generate cyclic (periodic) PMF
220     /*!\}*/
221     /*!
222      * \name Output control
223      */
224     /*!\{*/
225     gmx_bool bLog;                   //!< energy output (instead of probability) for profile
226     int      unit;                   //!< unit for PMF output kJ/mol or kT or kCal/mol
227     gmx_bool bSym;                   //!< symmetrize PMF around z=0 after WHAM, useful for membranes etc.
228     /*! \brief after wham, set prof to zero at this z-position.
229      * When bootstrapping, set zProf0 to a "stable" reference position.
230      */
231     real         zProf0;
232     gmx_bool     bProf0Set;              //!< setting profile to 0 at zProf0?
233
234     gmx_bool     bBoundsOnly, bHistOnly; //!< determine min and max, or write histograms and exit
235     gmx_bool     bAuto;                  //!< determine min and max automatically but do not exit
236
237     gmx_bool     verbose;                //!< more noisy wham mode
238     int          stepchange;             //!< print maximum change in prof after how many interations
239     output_env_t oenv;                   //!< xvgr options
240     /*!\}*/
241     /*!
242      * \name Autocorrelation stuff
243      */
244     /*!\{*/
245     gmx_bool bTauIntGiven, bCalcTauInt; //!< IACT given or should be calculated?
246     real     sigSmoothIact;             //!< sigma of Gaussian to smooth ACTs
247     gmx_bool bAllowReduceIact;          //!< Allow to reduce ACTs during smoothing. Otherwise ACT are only increased during smoothing
248     real     acTrestart;                //!< when computing ACT, time between restarting points
249
250     /* \brief Enforce the same weight for each umbella window, that is
251      *  calculate with the same number of data points for
252      *  each window. That can be reasonable, if the histograms
253      *  have different length, but due to autocorrelation,
254      *  a longer simulation should not have larger weightin wham.
255      */
256     gmx_bool bHistEq;
257     /*!\}*/
258
259     /*!
260      * \name Bootstrapping stuff
261      */
262     /*!\{*/
263     int nBootStrap;              //!< nr of bootstraps (50 is usually enough)
264
265     /* \brief bootstrap method
266      *
267      * if == bsMethod_hist, consider complete histograms as independent
268      * data points and, hence, only mix complete histograms.
269      * if == bsMethod_BayesianHist, consider complete histograms
270      * as independent data points, but assign random weights
271      * to the histograms during the bootstrapping ("Bayesian bootstrap")
272      * In case of long correlations (e.g., inside a channel), these
273      * will yield a more realistic error.
274      * if == bsMethod_traj(Gauss), generate synthetic histograms
275      * for each given
276      * histogram by generating an autocorrelated random sequence
277      * that is distributed according to the respective given
278      * histogram. With bsMethod_trajGauss, bootstrap from a Gaussian
279      * (instead of from the umbrella histogram) to generate a new
280      * histogram.
281      */
282     int bsMethod;
283
284     /* \brief  autocorrelation time (ACT) used to generate synthetic histograms. If ==0, use calculated ACF */
285     real tauBootStrap;
286
287     /* \brief when mixing histograms, mix only histograms withing blocks
288               long the reaction coordinate xi. Avoids gaps along xi. */
289     int histBootStrapBlockLength;
290
291     int bsSeed;                    //!< random seed for bootstrapping
292
293     /* \brief Write cumulative distribution functions (CDFs) of histograms
294               and write the generated histograms for each bootstrap */
295     gmx_bool bs_verbose;
296     /*!\}*/
297     /*!
298      * \name tabulated umbrella potential stuff
299      */
300     /*!\{*/
301     gmx_bool  bTab;
302     double   *tabX, *tabY, tabMin, tabMax, tabDz;
303     int       tabNbins;
304     /*!\}*/
305     gmx_rng_t rng;                  //!< gromacs random number generator
306 } t_UmbrellaOptions;
307
308 //! Make an umbrella window (may contain several histograms)
309 t_UmbrellaWindow * initUmbrellaWindows(int nwin)
310 {
311     t_UmbrellaWindow *win;
312     int               i;
313     snew(win, nwin);
314     for (i = 0; i < nwin; i++)
315     {
316         win[i].Histo    = win[i].cum  = 0;
317         win[i].k        = win[i].pos  = win[i].z = 0;
318         win[i].N        = win[i].Ntot = 0;
319         win[i].g        = win[i].tau  = win[i].tausmooth = 0;
320         win[i].bContrib = 0;
321         win[i].ztime    = 0;
322         win[i].forceAv  = 0;
323         win[i].aver     = win[i].sigma = 0;
324         win[i].bsWeight = 0;
325     }
326     return win;
327 }
328
329 //! Delete an umbrella window (may contain several histograms)
330 void freeUmbrellaWindows(t_UmbrellaWindow *win, int nwin)
331 {
332     int i, j;
333     for (i = 0; i < nwin; i++)
334     {
335         if (win[i].Histo)
336         {
337             for (j = 0; j < win[i].nPull; j++)
338             {
339                 sfree(win[i].Histo[j]);
340             }
341         }
342         if (win[i].cum)
343         {
344             for (j = 0; j < win[i].nPull; j++)
345             {
346                 sfree(win[i].cum[j]);
347             }
348         }
349         if (win[i].bContrib)
350         {
351             for (j = 0; j < win[i].nPull; j++)
352             {
353                 sfree(win[i].bContrib[j]);
354             }
355         }
356         sfree(win[i].Histo);
357         sfree(win[i].cum);
358         sfree(win[i].k);
359         sfree(win[i].pos);
360         sfree(win[i].z);
361         sfree(win[i].N);
362         sfree(win[i].Ntot);
363         sfree(win[i].g);
364         sfree(win[i].tau);
365         sfree(win[i].tausmooth);
366         sfree(win[i].bContrib);
367         sfree(win[i].ztime);
368         sfree(win[i].forceAv);
369         sfree(win[i].aver);
370         sfree(win[i].sigma);
371         sfree(win[i].bsWeight);
372     }
373     sfree(win);
374 }
375
376 /*! \brief
377  * Read and setup tabulated umbrella potential
378  */
379 void setup_tab(const char *fn, t_UmbrellaOptions *opt)
380 {
381     int      i, ny, nl;
382     double **y;
383
384     printf("Setting up tabulated potential from file %s\n", fn);
385     nl            = read_xvg(fn, &y, &ny);
386     opt->tabNbins = nl;
387     if (ny != 2)
388     {
389         gmx_fatal(FARGS, "Found %d columns in %s. Expected 2.\n", ny, fn);
390     }
391     opt->tabMin = y[0][0];
392     opt->tabMax = y[0][nl-1];
393     opt->tabDz  = (opt->tabMax-opt->tabMin)/(nl-1);
394     if (opt->tabDz <= 0)
395     {
396         gmx_fatal(FARGS, "The tabulated potential in %s must be provided in \n"
397                   "ascending z-direction", fn);
398     }
399     for (i = 0; i < nl-1; i++)
400     {
401         if  (fabs(y[0][i+1]-y[0][i]-opt->tabDz) > opt->tabDz/1e6)
402         {
403             gmx_fatal(FARGS, "z-values in %s are not equally spaced.\n", ny, fn);
404         }
405     }
406     snew(opt->tabY, nl);
407     snew(opt->tabX, nl);
408     for (i = 0; i < nl; i++)
409     {
410         opt->tabX[i] = y[0][i];
411         opt->tabY[i] = y[1][i];
412     }
413     printf("Found equally spaced tabulated potential from %g to %g, spacing %g\n",
414            opt->tabMin, opt->tabMax, opt->tabDz);
415 }
416
417 //! Read the header of an PDO file (position, force const, nr of groups)
418 void read_pdo_header(FILE * file, t_UmbrellaHeader * header, t_UmbrellaOptions *opt)
419 {
420     char               line[2048];
421     char               Buffer0[256], Buffer1[256], Buffer2[256], Buffer3[256], Buffer4[256];
422     int                i;
423     std::istringstream ist;
424
425     /*  line 1 */
426     if (fgets(line, 2048, file) == NULL)
427     {
428         gmx_fatal(FARGS, "Error reading header from pdo file\n");
429     }
430     ist.str(line);
431     ist >> Buffer0 >> Buffer1 >> Buffer2;
432     if (strcmp(Buffer1, "UMBRELLA"))
433     {
434         gmx_fatal(FARGS, "This does not appear to be a valid pdo file. Found %s, expected %s\n"
435                   "(Found in first line: `%s')\n",
436                   Buffer1, "UMBRELLA", line);
437     }
438     if (strcmp(Buffer2, "3.0"))
439     {
440         gmx_fatal(FARGS, "This does not appear to be a version 3.0 pdo file");
441     }
442
443     /*  line 2 */
444     if (fgets(line, 2048, file) == NULL)
445     {
446         gmx_fatal(FARGS, "Error reading header from pdo file\n");
447     }
448     ist.str(line);
449     ist >> Buffer0 >> Buffer1 >> Buffer2 >> header->Dims[0] >> header->Dims[1] >> header->Dims[2];
450     /* printf("%d %d %d\n", header->Dims[0],header->Dims[1],header->Dims[2]); */
451
452     header->nDim = header->Dims[0] + header->Dims[1] + header->Dims[2];
453     if (header->nDim != 1)
454     {
455         gmx_fatal(FARGS, "Currently only supports one dimension");
456     }
457
458     /* line3 */
459     if (fgets(line, 2048, file) == NULL)
460     {
461         gmx_fatal(FARGS, "Error reading header from pdo file\n");
462     }
463     ist.str(line);
464     ist >> Buffer0 >> Buffer1 >> header->nSkip;
465
466     /* line 4 */
467     if (fgets(line, 2048, file) == NULL)
468     {
469         gmx_fatal(FARGS, "Error reading header from pdo file\n");
470     }
471     ist.str(line);
472     ist >> Buffer0 >> Buffer1 >> Buffer2 >> header->Reference;
473
474     /* line 5 */
475     if (fgets(line, 2048, file) == NULL)
476     {
477         gmx_fatal(FARGS, "Error reading header from pdo file\n");
478     }
479     ist.str(line);
480     ist >> Buffer0 >> Buffer1 >> Buffer2 >> Buffer3 >> Buffer4 >> header->nPull;
481
482     if (opt->verbose)
483     {
484         printf("\tFound nPull=%d , nSkip=%d, ref=%s\n", header->nPull, header->nSkip,
485                header->Reference);
486     }
487
488     for (i = 0; i < header->nPull; ++i)
489     {
490         if (fgets(line, 2048, file) == NULL)
491         {
492             gmx_fatal(FARGS, "Error reading header from pdo file\n");
493         }
494         ist.str(line);
495         ist >> Buffer0 >> Buffer1 >> Buffer2 >> header->PullName[i];
496         ist >> Buffer0 >> Buffer1 >> header->UmbPos[i][0];
497         ist >> Buffer0 >> Buffer1 >> header->UmbCons[i][0];
498
499         if (opt->verbose)
500         {
501             printf("\tpullgroup %d, pullname = %s, UmbPos = %g, UmbConst = %g\n",
502                    i, header->PullName[i], header->UmbPos[i][0], header->UmbCons[i][0]);
503         }
504     }
505
506     if (fgets(line, 2048, file) == NULL)
507     {
508         gmx_fatal(FARGS, "Cannot read from file\n");
509     }
510     ist.str(line);
511     ist >> Buffer3;
512     if (strcmp(Buffer3, "#####") != 0)
513     {
514         gmx_fatal(FARGS, "Expected '#####', found %s. Hick.\n", Buffer3);
515     }
516 }
517
518 //! smarter fgets
519 static char *fgets3(FILE *fp, char ptr[], int *len)
520 {
521     char *p;
522     int   slen;
523
524     if (fgets(ptr, *len-1, fp) == NULL)
525     {
526         return NULL;
527     }
528     p = ptr;
529     while ((strchr(ptr, '\n') == NULL) && (!feof(fp)))
530     {
531         /* This line is longer than len characters, let's increase len! */
532         *len += STRLEN;
533         p    += STRLEN;
534         srenew(ptr, *len);
535         if (fgets(p-1, STRLEN, fp) == NULL)
536         {
537             break;
538         }
539     }
540     slen = strlen(ptr);
541     if (ptr[slen-1] == '\n')
542     {
543         ptr[slen-1] = '\0';
544     }
545
546     return ptr;
547 }
548
549 /*! \brief Read the data columns of and PDO file.
550  *
551  *  TO DO: Get rid of the scanf function to avoid the clang warning.
552  *         At the moment, this warning is avoided by hiding the format string
553  *         the variable fmtlf.
554  */
555 void read_pdo_data(FILE * file, t_UmbrellaHeader * header,
556                    int fileno, t_UmbrellaWindow * win,
557                    t_UmbrellaOptions *opt,
558                    gmx_bool bGetMinMax, real *mintmp, real *maxtmp)
559 {
560     int                i, inttemp, bins, count, ntot;
561     real               min, max, minfound = 1e20, maxfound = -1e20;
562     double             temp, time, time0 = 0, dt;
563     char              *ptr    = 0;
564     t_UmbrellaWindow * window = 0;
565     gmx_bool           timeok, dt_ok = 1;
566     char              *tmpbuf   = 0, fmt[256], fmtign[256], fmtlf[5] = "%lf";
567     int                len      = STRLEN, dstep = 1;
568     const int          blocklen = 4096;
569     int               *lennow   = 0;
570
571     if (!bGetMinMax)
572     {
573         bins = opt->bins;
574         min  = opt->min;
575         max  = opt->max;
576
577         window = win+fileno;
578         /* Need to alocate memory and set up structure */
579         window->nPull = header->nPull;
580         window->nBin  = bins;
581
582         snew(window->Histo, window->nPull);
583         snew(window->z, window->nPull);
584         snew(window->k, window->nPull);
585         snew(window->pos, window->nPull);
586         snew(window->N, window->nPull);
587         snew(window->Ntot, window->nPull);
588         snew(window->g, window->nPull);
589         snew(window->bsWeight, window->nPull);
590
591         window->bContrib = 0;
592
593         if (opt->bCalcTauInt)
594         {
595             snew(window->ztime, window->nPull);
596         }
597         else
598         {
599             window->ztime = 0;
600         }
601         snew(lennow, window->nPull);
602
603         for (i = 0; i < window->nPull; ++i)
604         {
605             window->z[i]        = 1;
606             window->bsWeight[i] = 1.;
607             snew(window->Histo[i], bins);
608             window->k[i]    = header->UmbCons[i][0];
609             window->pos[i]  = header->UmbPos[i][0];
610             window->N[i]    = 0;
611             window->Ntot[i] = 0;
612             window->g[i]    = 1.;
613             if (opt->bCalcTauInt)
614             {
615                 window->ztime[i] = 0;
616             }
617         }
618
619         /* Done with setup */
620     }
621     else
622     {
623         minfound = 1e20;
624         maxfound = -1e20;
625         min      = max = bins = 0; /* Get rid of warnings */
626     }
627
628     count = 0;
629     snew(tmpbuf, len);
630     while ( (ptr = fgets3(file, tmpbuf, &len)) != NULL)
631     {
632         trim(ptr);
633
634         if (ptr[0] == '#' || strlen(ptr) < 2)
635         {
636             continue;
637         }
638
639         /* Initiate format string */
640         fmtign[0] = '\0';
641         strcat(fmtign, "%*s");
642
643         sscanf(ptr, fmtlf, &time); /* printf("Time %f\n",time); */
644         /* Round time to fs */
645         time = 1.0/1000*( static_cast<int> (time*1000+0.5) );
646
647         /* get time step of pdo file */
648         if (count == 0)
649         {
650             time0 = time;
651         }
652         else if (count == 1)
653         {
654             dt = time-time0;
655             if (opt->dt > 0.0)
656             {
657                 dstep = static_cast<int>(opt->dt/dt+0.5);
658                 if (dstep == 0)
659                 {
660                     dstep = 1;
661                 }
662             }
663             if (!bGetMinMax)
664             {
665                 window->dt = dt*dstep;
666             }
667         }
668         count++;
669
670         dt_ok  = ((count-1)%dstep == 0);
671         timeok = (dt_ok && time >= opt->tmin && time <= opt->tmax);
672         /* if (opt->verbose)
673            printf(" time = %f, (tmin,tmax)=(%e,%e), dt_ok=%d timeok=%d\n",
674            time,opt->tmin, opt->tmax, dt_ok,timeok); */
675
676         if (timeok)
677         {
678             for (i = 0; i < header->nPull; ++i)
679             {
680                 strcpy(fmt, fmtign);
681                 strcat(fmt, "%lf");      /* Creating a format stings such as "%*s...%*s%lf" */
682                 strcat(fmtign, "%*s");   /* ignoring one more entry in the next loop */
683                 if (sscanf(ptr, fmt, &temp))
684                 {
685                     temp += header->UmbPos[i][0];
686                     if (bGetMinMax)
687                     {
688                         if (temp < minfound)
689                         {
690                             minfound = temp;
691                         }
692                         if (temp > maxfound)
693                         {
694                             maxfound = temp;
695                         }
696                     }
697                     else
698                     {
699                         if (opt->bCalcTauInt)
700                         {
701                             /* save time series for autocorrelation analysis */
702                             ntot = window->Ntot[i];
703                             if (ntot >= lennow[i])
704                             {
705                                 lennow[i] += blocklen;
706                                 srenew(window->ztime[i], lennow[i]);
707                             }
708                             window->ztime[i][ntot] = temp;
709                         }
710
711                         temp -= min;
712                         temp /= (max-min);
713                         temp *= bins;
714                         temp  = floor(temp);
715
716                         inttemp = static_cast<int> (temp);
717                         if (opt->bCycl)
718                         {
719                             if (inttemp < 0)
720                             {
721                                 inttemp += bins;
722                             }
723                             else if (inttemp >= bins)
724                             {
725                                 inttemp -= bins;
726                             }
727                         }
728
729                         if (inttemp >= 0 && inttemp < bins)
730                         {
731                             window->Histo[i][inttemp] += 1.;
732                             window->N[i]++;
733                         }
734                         window->Ntot[i]++;
735                     }
736                 }
737             }
738         }
739         if (time > opt->tmax)
740         {
741             if (opt->verbose)
742             {
743                 printf("time %f larger than tmax %f, stop reading pdo file\n", time, opt->tmax);
744             }
745             break;
746         }
747     }
748
749     if (bGetMinMax)
750     {
751         *mintmp = minfound;
752         *maxtmp = maxfound;
753     }
754
755     sfree(lennow);
756     sfree(tmpbuf);
757 }
758
759 /*! \brief Set identical weights for all histograms
760  *
761  * Normally, the weight is given by the number data points in each
762  * histogram, together with the autocorrelation time. This can be overwritten
763  * by this routine (not recommended). Since we now support autocorrelations, it is better to set
764  * an appropriate autocorrelation times instead of using this function.
765  */
766 void enforceEqualWeights(t_UmbrellaWindow * window, int nWindows)
767 {
768     int    i, k, j, NEnforced;
769     double ratio;
770
771     NEnforced = window[0].Ntot[0];
772     printf("\nFound -hist-eq. Enforcing equal weights for all histograms, \ni.e. doing a "
773            "non-weighted histogram analysis method. Ndata = %d\n", NEnforced);
774     /* enforce all histograms to have the same weight as the very first histogram */
775
776     for (j = 0; j < nWindows; ++j)
777     {
778         for (k = 0; k < window[j].nPull; ++k)
779         {
780             ratio = 1.0*NEnforced/window[j].Ntot[k];
781             for (i = 0; i < window[0].nBin; ++i)
782             {
783                 window[j].Histo[k][i] *= ratio;
784             }
785             window[j].N[k] = static_cast<int>(ratio*window[j].N[k] + 0.5);
786         }
787     }
788 }
789
790 /*! \brief Simple linear interpolation between two given tabulated points
791  */
792 double tabulated_pot(double dist, t_UmbrellaOptions *opt)
793 {
794     int    jl, ju;
795     double pl, pu, dz, dp;
796
797     jl = static_cast<int> (floor((dist-opt->tabMin)/opt->tabDz));
798     ju = jl+1;
799     if (jl < 0 || ju >= opt->tabNbins)
800     {
801         gmx_fatal(FARGS, "Distance %f out of bounds of tabulated potential (jl=%d, ju=%d).\n"
802                   "Provide an extended table.", dist, jl, ju);
803     }
804     pl = opt->tabY[jl];
805     pu = opt->tabY[ju];
806     dz = dist-opt->tabX[jl];
807     dp = (pu-pl)*dz/opt->tabDz;
808     return pl+dp;
809 }
810
811
812 /*! \brief
813  * Check which bins substiantially contribute (accelerates WHAM)
814  *
815  * Don't worry, that routine does not mean we compute the PMF in limited precision.
816  * After rapid convergence (using only substiantal contributions), we always switch to
817  * full precision.
818  */
819 void setup_acc_wham(double *profile, t_UmbrellaWindow * window, int nWindows,
820                     t_UmbrellaOptions *opt)
821 {
822     int           i, j, k, nGrptot = 0, nContrib = 0, nTot = 0;
823     double        U, min = opt->min, dz = opt->dz, temp, ztot_half, distance, ztot, contrib1, contrib2;
824     gmx_bool      bAnyContrib;
825     static int    bFirst = 1;
826     static double wham_contrib_lim;
827
828     if (bFirst)
829     {
830         for (i = 0; i < nWindows; ++i)
831         {
832             nGrptot += window[i].nPull;
833         }
834         wham_contrib_lim = opt->Tolerance/nGrptot;
835     }
836
837     ztot      = opt->max-opt->min;
838     ztot_half = ztot/2;
839
840     for (i = 0; i < nWindows; ++i)
841     {
842         if (!window[i].bContrib)
843         {
844             snew(window[i].bContrib, window[i].nPull);
845         }
846         for (j = 0; j < window[i].nPull; ++j)
847         {
848             if (!window[i].bContrib[j])
849             {
850                 snew(window[i].bContrib[j], opt->bins);
851             }
852             bAnyContrib = FALSE;
853             for (k = 0; k < opt->bins; ++k)
854             {
855                 temp     = (1.0*k+0.5)*dz+min;
856                 distance = temp - window[i].pos[j];   /* distance to umbrella center */
857                 if (opt->bCycl)
858                 {                                     /* in cyclic wham:             */
859                     if (distance > ztot_half)         /*    |distance| < ztot_half   */
860                     {
861                         distance -= ztot;
862                     }
863                     else if (distance < -ztot_half)
864                     {
865                         distance += ztot;
866                     }
867                 }
868                 /* Note: there are two contributions to bin k in the wham equations:
869                    i)  N[j]*exp(- U/(8.314e-3*opt->Temperature) + window[i].z[j])
870                    ii) exp(- U/(8.314e-3*opt->Temperature))
871                    where U is the umbrella potential
872                    If any of these number is larger wham_contrib_lim, I set contrib=TRUE
873                  */
874
875                 if (!opt->bTab)
876                 {
877                     U = 0.5*window[i].k[j]*sqr(distance);       /* harmonic potential assumed. */
878                 }
879                 else
880                 {
881                     U = tabulated_pot(distance, opt);            /* Use tabulated potential     */
882                 }
883                 contrib1                 = profile[k]*exp(-U/(8.314e-3*opt->Temperature));
884                 contrib2                 = window[i].N[j]*exp(-U/(8.314e-3*opt->Temperature) + window[i].z[j]);
885                 window[i].bContrib[j][k] = (contrib1 > wham_contrib_lim || contrib2 > wham_contrib_lim);
886                 bAnyContrib              = (bAnyContrib | window[i].bContrib[j][k]);
887                 if (window[i].bContrib[j][k])
888                 {
889                     nContrib++;
890                 }
891                 nTot++;
892             }
893             /* If this histo is far outside min and max all bContrib may be FALSE,
894                causing a floating point exception later on. To avoid that, switch
895                them all to true.*/
896             if (!bAnyContrib)
897             {
898                 for (k = 0; k < opt->bins; ++k)
899                 {
900                     window[i].bContrib[j][k] = TRUE;
901                 }
902             }
903         }
904     }
905     if (bFirst)
906     {
907         printf("Initialized rapid wham stuff (contrib tolerance %g)\n"
908                "Evaluating only %d of %d expressions.\n\n", wham_contrib_lim, nContrib, nTot);
909     }
910
911     if (opt->verbose)
912     {
913         printf("Updated rapid wham stuff. (evaluating only %d of %d contributions)\n",
914                nContrib, nTot);
915     }
916     bFirst = 0;
917 }
918
919 //! Compute the PMF (one of the two main WHAM routines)
920 void calc_profile(double *profile, t_UmbrellaWindow * window, int nWindows,
921                   t_UmbrellaOptions *opt, gmx_bool bExact)
922 {
923     double ztot_half, ztot, min = opt->min, dz = opt->dz;
924
925     ztot      = opt->max-opt->min;
926     ztot_half = ztot/2;
927
928 #pragma omp parallel
929     {
930         int nthreads  = gmx_omp_get_max_threads();
931         int thread_id = gmx_omp_get_thread_num();
932         int i;
933         int i0        = thread_id*opt->bins/nthreads;
934         int i1        = std::min(opt->bins, ((thread_id+1)*opt->bins)/nthreads);
935
936         for (i = i0; i < i1; ++i)
937         {
938             int    j, k;
939             double num, denom, invg, temp = 0, distance, U = 0;
940             num = denom = 0.;
941             for (j = 0; j < nWindows; ++j)
942             {
943                 for (k = 0; k < window[j].nPull; ++k)
944                 {
945                     invg = 1.0/window[j].g[k] * window[j].bsWeight[k];
946                     temp = (1.0*i+0.5)*dz+min;
947                     num += invg*window[j].Histo[k][i];
948
949                     if (!(bExact || window[j].bContrib[k][i]))
950                     {
951                         continue;
952                     }
953                     distance = temp - window[j].pos[k];   /* distance to umbrella center */
954                     if (opt->bCycl)
955                     {                                     /* in cyclic wham:             */
956                         if (distance > ztot_half)         /*    |distance| < ztot_half   */
957                         {
958                             distance -= ztot;
959                         }
960                         else if (distance < -ztot_half)
961                         {
962                             distance += ztot;
963                         }
964                     }
965
966                     if (!opt->bTab)
967                     {
968                         U = 0.5*window[j].k[k]*sqr(distance);       /* harmonic potential assumed. */
969                     }
970                     else
971                     {
972                         U = tabulated_pot(distance, opt);            /* Use tabulated potential     */
973                     }
974                     denom += invg*window[j].N[k]*exp(-U/(8.314e-3*opt->Temperature) + window[j].z[k]);
975                 }
976             }
977             profile[i] = num/denom;
978         }
979     }
980 }
981
982 //! Compute the free energy offsets z (one of the two main WHAM routines)
983 double calc_z(double * profile, t_UmbrellaWindow * window, int nWindows,
984               t_UmbrellaOptions *opt, gmx_bool bExact)
985 {
986     double min     = opt->min, dz = opt->dz, ztot_half, ztot;
987     double maxglob = -1e20;
988
989     ztot      = opt->max-opt->min;
990     ztot_half = ztot/2;
991
992 #pragma omp parallel
993     {
994         int    nthreads  = gmx_omp_get_max_threads();
995         int    thread_id = gmx_omp_get_thread_num();
996         int    i;
997         int    i0        = thread_id*nWindows/nthreads;
998         int    i1        = std::min(nWindows, ((thread_id+1)*nWindows)/nthreads);
999         double maxloc    = -1e20;
1000
1001         for (i = i0; i < i1; ++i)
1002         {
1003             double total     = 0, temp, distance, U = 0;
1004             int    j, k;
1005
1006             for (j = 0; j < window[i].nPull; ++j)
1007             {
1008                 total = 0;
1009                 for (k = 0; k < window[i].nBin; ++k)
1010                 {
1011                     if (!(bExact || window[i].bContrib[j][k]))
1012                     {
1013                         continue;
1014                     }
1015                     temp     = (1.0*k+0.5)*dz+min;
1016                     distance = temp - window[i].pos[j];   /* distance to umbrella center */
1017                     if (opt->bCycl)
1018                     {                                     /* in cyclic wham:             */
1019                         if (distance > ztot_half)         /*    |distance| < ztot_half   */
1020                         {
1021                             distance -= ztot;
1022                         }
1023                         else if (distance < -ztot_half)
1024                         {
1025                             distance += ztot;
1026                         }
1027                     }
1028
1029                     if (!opt->bTab)
1030                     {
1031                         U = 0.5*window[i].k[j]*sqr(distance);       /* harmonic potential assumed. */
1032                     }
1033                     else
1034                     {
1035                         U = tabulated_pot(distance, opt);            /* Use tabulated potential     */
1036                     }
1037                     total += profile[k]*exp(-U/(8.314e-3*opt->Temperature));
1038                 }
1039                 /* Avoid floating point exception if window is far outside min and max */
1040                 if (total != 0.0)
1041                 {
1042                     total = -log(total);
1043                 }
1044                 else
1045                 {
1046                     total = 1000.0;
1047                 }
1048                 temp = fabs(total - window[i].z[j]);
1049                 if (temp > maxloc)
1050                 {
1051                     maxloc = temp;
1052                 }
1053                 window[i].z[j] = total;
1054             }
1055         }
1056         /* Now get maximum maxloc from the threads and put in maxglob */
1057         if (maxloc > maxglob)
1058         {
1059 #pragma omp critical
1060             {
1061                 if (maxloc > maxglob)
1062                 {
1063                     maxglob = maxloc;
1064                 }
1065             }
1066         }
1067     }
1068
1069     return maxglob;
1070 }
1071
1072 //! Make PMF symmetric around 0 (useful e.g. for membranes)
1073 void symmetrizeProfile(double* profile, t_UmbrellaOptions *opt)
1074 {
1075     int     i, j, bins = opt->bins;
1076     double *prof2, min = opt->min, max = opt->max, dz = opt->dz, zsym, deltaz, profsym;
1077     double  z, z1;
1078
1079     if (min > 0. || max < 0.)
1080     {
1081         gmx_fatal(FARGS, "Cannot symmetrize profile around z=0 with min=%f and max=%f\n",
1082                   opt->min, opt->max);
1083     }
1084
1085     snew(prof2, bins);
1086
1087     for (i = 0; i < bins; i++)
1088     {
1089         z    = min+(i+0.5)*dz;
1090         zsym = -z;
1091         /* bin left of zsym */
1092         j = static_cast<int> (floor((zsym-min)/dz-0.5));
1093         if (j >= 0 && (j+1) < bins)
1094         {
1095             /* interpolate profile linearly between bins j and j+1 */
1096             z1      = min+(j+0.5)*dz;
1097             deltaz  = zsym-z1;
1098             profsym = profile[j] + (profile[j+1]-profile[j])/dz*deltaz;
1099             /* average between left and right */
1100             prof2[i] = 0.5*(profsym+profile[i]);
1101         }
1102         else
1103         {
1104             prof2[i] = profile[i];
1105         }
1106     }
1107
1108     memcpy(profile, prof2, bins*sizeof(double));
1109     sfree(prof2);
1110 }
1111
1112 //! Set energy unit (kJ/mol,kT,kCal/mol) and set it to zero at opt->zProf0
1113 void prof_normalization_and_unit(double * profile, t_UmbrellaOptions *opt)
1114 {
1115     int    i, bins, imin;
1116     double unit_factor = 1., R_MolarGasConst, diff;
1117
1118     R_MolarGasConst = 8.314472e-3; /* in kJ/(mol*K) */
1119     bins            = opt->bins;
1120
1121     /* Not log? Nothing to do! */
1122     if (!opt->bLog)
1123     {
1124         return;
1125     }
1126
1127     /* Get profile in units of kT, kJ/mol, or kCal/mol */
1128     if (opt->unit == en_kT)
1129     {
1130         unit_factor = 1.0;
1131     }
1132     else if (opt->unit == en_kJ)
1133     {
1134         unit_factor = R_MolarGasConst*opt->Temperature;
1135     }
1136     else if (opt->unit == en_kCal)
1137     {
1138         unit_factor = R_MolarGasConst*opt->Temperature/4.1868;
1139     }
1140     else
1141     {
1142         gmx_fatal(FARGS, "Sorry, I don't know this energy unit.");
1143     }
1144
1145     for (i = 0; i < bins; i++)
1146     {
1147         if (profile[i] > 0.0)
1148         {
1149             profile[i] = -log(profile[i])*unit_factor;
1150         }
1151     }
1152
1153     /* shift to zero at z=opt->zProf0 */
1154     if (!opt->bProf0Set)
1155     {
1156         diff = profile[0];
1157     }
1158     else
1159     {
1160         /* Get bin with shortest distance to opt->zProf0
1161            (-0.5 from bin position and +0.5 from rounding cancel) */
1162         imin = static_cast<int>((opt->zProf0-opt->min)/opt->dz);
1163         if (imin < 0)
1164         {
1165             imin = 0;
1166         }
1167         else if (imin >= bins)
1168         {
1169             imin = bins-1;
1170         }
1171         diff = profile[imin];
1172     }
1173
1174     /* Shift to zero */
1175     for (i = 0; i < bins; i++)
1176     {
1177         profile[i] -= diff;
1178     }
1179 }
1180
1181 //! Make an array of random integers (used for bootstrapping)
1182 void getRandomIntArray(int nPull, int blockLength, int* randomArray, gmx_rng_t rng)
1183 {
1184     int ipull, blockBase, nr, ipullRandom;
1185
1186     if (blockLength == 0)
1187     {
1188         blockLength = nPull;
1189     }
1190
1191     for (ipull = 0; ipull < nPull; ipull++)
1192     {
1193         blockBase = (ipull/blockLength)*blockLength;
1194         do
1195         {      /* make sure nothing bad happens in the last block */
1196             nr          = static_cast<int>(gmx_rng_uniform_real(rng)*blockLength);
1197             ipullRandom = blockBase + nr;
1198         }
1199         while (ipullRandom >= nPull);
1200         if (ipullRandom < 0 || ipullRandom >= nPull)
1201         {
1202             gmx_fatal(FARGS, "Ups, random iWin = %d, nPull = %d, nr = %d, "
1203                       "blockLength = %d, blockBase = %d\n",
1204                       ipullRandom, nPull, nr, blockLength, blockBase);
1205         }
1206         randomArray[ipull] = ipullRandom;
1207     }
1208     /*for (ipull=0; ipull<nPull; ipull++)
1209        printf("%d ",randomArray[ipull]); printf("\n"); */
1210 }
1211
1212 /*! \brief Set pull group information of a synthetic histogram
1213  *
1214  * This is used when bootstapping new trajectories and thereby create new histogtrams,
1215  * but it is not required if we bootstrap complete histograms.
1216  */
1217 void copy_pullgrp_to_synthwindow(t_UmbrellaWindow *synthWindow,
1218                                  t_UmbrellaWindow *thisWindow, int pullid)
1219 {
1220     synthWindow->N       [0] = thisWindow->N        [pullid];
1221     synthWindow->Histo   [0] = thisWindow->Histo    [pullid];
1222     synthWindow->pos     [0] = thisWindow->pos      [pullid];
1223     synthWindow->z       [0] = thisWindow->z        [pullid];
1224     synthWindow->k       [0] = thisWindow->k        [pullid];
1225     synthWindow->bContrib[0] = thisWindow->bContrib [pullid];
1226     synthWindow->g       [0] = thisWindow->g        [pullid];
1227     synthWindow->bsWeight[0] = thisWindow->bsWeight [pullid];
1228 }
1229
1230 /*! \brief Calculate cumulative distribution function of of all histograms.
1231  *
1232  * This allow to create random number sequences
1233  * which are distributed according to the histograms. Required to generate
1234  * the "synthetic" histograms for the Bootstrap method
1235  */
1236 void calc_cumulatives(t_UmbrellaWindow *window, int nWindows,
1237                       t_UmbrellaOptions *opt, const char *fnhist)
1238 {
1239     int    i, j, k, nbin;
1240     double last;
1241     char  *fn = 0, *buf = 0;
1242     FILE  *fp = 0;
1243
1244     if (opt->bs_verbose)
1245     {
1246         snew(fn, strlen(fnhist)+10);
1247         snew(buf, strlen(fnhist)+10);
1248         sprintf(fn, "%s_cumul.xvg", strncpy(buf, fnhist, strlen(fnhist)-4));
1249         fp = xvgropen(fn, "CDFs of umbrella windows", xlabel, "CDF", opt->oenv);
1250     }
1251
1252     nbin = opt->bins;
1253     for (i = 0; i < nWindows; i++)
1254     {
1255         snew(window[i].cum, window[i].nPull);
1256         for (j = 0; j < window[i].nPull; j++)
1257         {
1258             snew(window[i].cum[j], nbin+1);
1259             window[i].cum[j][0] = 0.;
1260             for (k = 1; k <= nbin; k++)
1261             {
1262                 window[i].cum[j][k] = window[i].cum[j][k-1]+window[i].Histo[j][k-1];
1263             }
1264
1265             /* normalize CDFs. Ensure cum[nbin]==1 */
1266             last = window[i].cum[j][nbin];
1267             for (k = 0; k <= nbin; k++)
1268             {
1269                 window[i].cum[j][k] /= last;
1270             }
1271         }
1272     }
1273
1274     printf("Cumulative distriubtion functions of all histograms created.\n");
1275     if (opt->bs_verbose)
1276     {
1277         for (k = 0; k <= nbin; k++)
1278         {
1279             fprintf(fp, "%g\t", opt->min+k*opt->dz);
1280             for (i = 0; i < nWindows; i++)
1281             {
1282                 for (j = 0; j < window[i].nPull; j++)
1283                 {
1284                     fprintf(fp, "%g\t", window[i].cum[j][k]);
1285                 }
1286             }
1287             fprintf(fp, "\n");
1288         }
1289         printf("Wrote cumulative distribution functions to %s\n", fn);
1290         xvgrclose(fp);
1291         sfree(fn);
1292         sfree(buf);
1293     }
1294 }
1295
1296
1297 /*! \brief Return j such that xx[j] <= x < xx[j+1]
1298  *
1299  *  This is used to generate a random sequence distributed according to a histogram
1300  */
1301 void searchCumulative(double xx[], int n, double x, int *j)
1302 {
1303     int ju, jm, jl;
1304
1305     jl = -1;
1306     ju = n;
1307     while (ju-jl > 1)
1308     {
1309         jm = (ju+jl) >> 1;
1310         if (x >= xx[jm])
1311         {
1312             jl = jm;
1313         }
1314         else
1315         {
1316             ju = jm;
1317         }
1318     }
1319     if (x == xx[0])
1320     {
1321         *j = 0;
1322     }
1323     else if (x == xx[n-1])
1324     {
1325         *j = n-2;
1326     }
1327     else
1328     {
1329         *j = jl;
1330     }
1331 }
1332
1333 //! Bootstrap new trajectories and thereby generate new (bootstrapped) histograms
1334 void create_synthetic_histo(t_UmbrellaWindow *synthWindow, t_UmbrellaWindow *thisWindow,
1335                             int pullid, t_UmbrellaOptions *opt)
1336 {
1337     int    N, i, nbins, r_index, ibin;
1338     double r, tausteps = 0.0, a, ap, dt, x, invsqrt2, g, y, sig = 0., z, mu = 0.;
1339     char   errstr[1024];
1340
1341     N     = thisWindow->N[pullid];
1342     dt    = thisWindow->dt;
1343     nbins = thisWindow->nBin;
1344
1345     /* tau = autocorrelation time */
1346     if (opt->tauBootStrap > 0.0)
1347     {
1348         tausteps = opt->tauBootStrap/dt;
1349     }
1350     else if (opt->bTauIntGiven || opt->bCalcTauInt)
1351     {
1352         /* calc tausteps from g=1+2tausteps */
1353         g        = thisWindow->g[pullid];
1354         tausteps = (g-1)/2;
1355     }
1356     else
1357     {
1358         sprintf(errstr,
1359                 "When generating hypothetical trajctories from given umbrella histograms,\n"
1360                 "autocorrelation times (ACTs) are required. Otherwise the statistical error\n"
1361                 "cannot be predicted. You have 3 options:\n"
1362                 "1) Make g_wham estimate the ACTs (options -ac and -acsig).\n"
1363                 "2) Calculate the ACTs by yourself (e.g. with g_analyze) and provide them\n");
1364         strcat(errstr,
1365                "   with option -iiact for all umbrella windows.\n"
1366                "3) If all ACTs are identical and know, you can define them with -bs-tau.\n"
1367                "   Use option (3) only if you are sure what you're doing, you may severely\n"
1368                "   underestimate the error if a too small ACT is given.\n");
1369         gmx_fatal(FARGS, errstr);
1370     }
1371
1372     synthWindow->N       [0] = N;
1373     synthWindow->pos     [0] = thisWindow->pos[pullid];
1374     synthWindow->z       [0] = thisWindow->z[pullid];
1375     synthWindow->k       [0] = thisWindow->k[pullid];
1376     synthWindow->bContrib[0] = thisWindow->bContrib[pullid];
1377     synthWindow->g       [0] = thisWindow->g       [pullid];
1378     synthWindow->bsWeight[0] = thisWindow->bsWeight[pullid];
1379
1380     for (i = 0; i < nbins; i++)
1381     {
1382         synthWindow->Histo[0][i] = 0.;
1383     }
1384
1385     if (opt->bsMethod == bsMethod_trajGauss)
1386     {
1387         sig = thisWindow->sigma [pullid];
1388         mu  = thisWindow->aver  [pullid];
1389     }
1390
1391     /* Genrate autocorrelated Gaussian random variable with autocorrelation time tau
1392        Use the following:
1393        If x and y are random numbers from N(0,1) (Gaussian with average 0 and sigma=1),
1394        then
1395        z = a*x + sqrt(1-a^2)*y
1396        is also from N(0,1), and cov(z,x) = a. Thus, by gerenating a sequence
1397        x' = a*x + sqrt(1-a^2)*y, the sequnce x(t) is from N(0,1) and has an autocorrelation
1398        function
1399        C(t) = exp(-t/tau) with tau=-1/ln(a)
1400
1401        Then, use error function to turn the Gaussian random variable into a uniformly
1402        distributed one in [0,1]. Eventually, use cumulative distribution function of
1403        histogram to get random variables distributed according to histogram.
1404        Note: The ACT of the flat distribution and of the generated histogram is not
1405        100% exactly tau, but near tau (my test was 3.8 instead of 4).
1406      */
1407     a        = exp(-1.0/tausteps);
1408     ap       = sqrt(1-a*a);
1409     invsqrt2 = 1./sqrt(2.0);
1410
1411     /* init random sequence */
1412     x = gmx_rng_gaussian_table(opt->rng);
1413
1414     if (opt->bsMethod == bsMethod_traj)
1415     {
1416         /* bootstrap points from the umbrella histograms */
1417         for (i = 0; i < N; i++)
1418         {
1419             y = gmx_rng_gaussian_table(opt->rng);
1420             x = a*x+ap*y;
1421             /* get flat distribution in [0,1] using cumulative distribution function of Gauusian
1422                Note: CDF(Gaussian) = 0.5*{1+erf[x/sqrt(2)]}
1423              */
1424             r = 0.5*(1+gmx_erf(x*invsqrt2));
1425             searchCumulative(thisWindow->cum[pullid], nbins+1, r, &r_index);
1426             synthWindow->Histo[0][r_index] += 1.;
1427         }
1428     }
1429     else if (opt->bsMethod == bsMethod_trajGauss)
1430     {
1431         /* bootstrap points from a Gaussian with the same average and sigma
1432            as the respective umbrella histogram. The idea was, that -given
1433            limited sampling- the bootstrapped histograms are otherwise biased
1434            from the limited sampling of the US histos. However, bootstrapping from
1435            the Gaussian seems to yield a similar estimate. */
1436         i = 0;
1437         while (i < N)
1438         {
1439             y    = gmx_rng_gaussian_table(opt->rng);
1440             x    = a*x+ap*y;
1441             z    = x*sig+mu;
1442             ibin = static_cast<int> (floor((z-opt->min)/opt->dz));
1443             if (opt->bCycl)
1444             {
1445                 if (ibin < 0)
1446                 {
1447                     while ( (ibin += nbins) < 0)
1448                     {
1449                         ;
1450                     }
1451                 }
1452                 else if (ibin >= nbins)
1453                 {
1454                     while ( (ibin -= nbins) >= nbins)
1455                     {
1456                         ;
1457                     }
1458                 }
1459             }
1460
1461             if (ibin >= 0 && ibin < nbins)
1462             {
1463                 synthWindow->Histo[0][ibin] += 1.;
1464                 i++;
1465             }
1466         }
1467     }
1468     else
1469     {
1470         gmx_fatal(FARGS, "Unknown bsMethod (id %d). That should not happen.\n", opt->bsMethod);
1471     }
1472 }
1473
1474 /*! \brief Write all histograms to a file
1475  *
1476  * If bs_index>=0, a number is added to the output file name to allow the ouput of all
1477  * sets of bootstrapped histograms.
1478  */
1479 void print_histograms(const char *fnhist, t_UmbrellaWindow * window, int nWindows,
1480                       int bs_index, t_UmbrellaOptions *opt)
1481 {
1482     char *fn = 0, *buf = 0, title[256];
1483     FILE *fp;
1484     int   bins, l, i, j;
1485
1486     if (bs_index >= 0)
1487     {
1488         snew(fn, strlen(fnhist)+10);
1489         snew(buf, strlen(fnhist)+1);
1490         sprintf(fn, "%s_bs%d.xvg", strncpy(buf, fnhist, strlen(fnhist)-4), bs_index);
1491         sprintf(title, "Umbrella histograms. Bootstrap #%d", bs_index);
1492     }
1493     else
1494     {
1495         fn = gmx_strdup(fnhist);
1496         strcpy(title, "Umbrella histograms");
1497     }
1498
1499     fp   = xvgropen(fn, title, xlabel, "count", opt->oenv);
1500     bins = opt->bins;
1501
1502     /* Write histograms */
1503     for (l = 0; l < bins; ++l)
1504     {
1505         fprintf(fp, "%e\t", (double)(l+0.5)*opt->dz+opt->min);
1506         for (i = 0; i < nWindows; ++i)
1507         {
1508             for (j = 0; j < window[i].nPull; ++j)
1509             {
1510                 fprintf(fp, "%e\t", window[i].Histo[j][l]);
1511             }
1512         }
1513         fprintf(fp, "\n");
1514     }
1515
1516     xvgrclose(fp);
1517     printf("Wrote %s\n", fn);
1518     if (bs_index >= 0)
1519     {
1520         sfree(buf);
1521     }
1522     sfree(fn);
1523 }
1524
1525 //! Used for qsort to sort random numbers
1526 int func_wham_is_larger(const void *a, const void *b)
1527 {
1528     double *aa, *bb;
1529     aa = (double*)a;
1530     bb = (double*)b;
1531     if (*aa < *bb)
1532     {
1533         return -1;
1534     }
1535     else if (*aa > *bb)
1536     {
1537         return 1;
1538     }
1539     else
1540     {
1541         return 0;
1542     }
1543 }
1544
1545 //! Make random weights for histograms for the Bayesian bootstrap of complete histograms)
1546 void setRandomBsWeights(t_UmbrellaWindow *synthwin, int nAllPull, t_UmbrellaOptions *opt)
1547 {
1548     int     i;
1549     double *r;
1550
1551     snew(r, nAllPull);
1552
1553     /* generate ordered random numbers between 0 and nAllPull  */
1554     for (i = 0; i < nAllPull-1; i++)
1555     {
1556         r[i] = gmx_rng_uniform_real(opt->rng) * nAllPull;
1557     }
1558     qsort((void *)r, nAllPull-1, sizeof(double), &func_wham_is_larger);
1559     r[nAllPull-1] = 1.0*nAllPull;
1560
1561     synthwin[0].bsWeight[0] = r[0];
1562     for (i = 1; i < nAllPull; i++)
1563     {
1564         synthwin[i].bsWeight[0] = r[i]-r[i-1];
1565     }
1566
1567     /* avoid to have zero weight by adding a tiny value */
1568     for (i = 0; i < nAllPull; i++)
1569     {
1570         if (synthwin[i].bsWeight[0] < 1e-5)
1571         {
1572             synthwin[i].bsWeight[0] = 1e-5;
1573         }
1574     }
1575
1576     sfree(r);
1577 }
1578
1579 //! The main bootstrapping routine
1580 void do_bootstrapping(const char *fnres, const char* fnprof, const char *fnhist,
1581                       char* ylabel, double *profile,
1582                       t_UmbrellaWindow * window, int nWindows, t_UmbrellaOptions *opt)
1583 {
1584     t_UmbrellaWindow * synthWindow;
1585     double            *bsProfile, *bsProfiles_av, *bsProfiles_av2, maxchange = 1e20, tmp, stddev;
1586     int                i, j, *randomArray = 0, winid, pullid, ib;
1587     int                iAllPull, nAllPull, *allPull_winId, *allPull_pullId;
1588     FILE              *fp;
1589     gmx_bool           bExact = FALSE;
1590
1591     /* init random generator */
1592     if (opt->bsSeed == -1)
1593     {
1594         opt->rng = gmx_rng_init(gmx_rng_make_seed());
1595     }
1596     else
1597     {
1598         opt->rng = gmx_rng_init(opt->bsSeed);
1599     }
1600
1601     snew(bsProfile,     opt->bins);
1602     snew(bsProfiles_av, opt->bins);
1603     snew(bsProfiles_av2, opt->bins);
1604
1605     /* Create array of all pull groups. Note that different windows
1606        may have different nr of pull groups
1607        First: Get total nr of pull groups */
1608     nAllPull = 0;
1609     for (i = 0; i < nWindows; i++)
1610     {
1611         nAllPull += window[i].nPull;
1612     }
1613     snew(allPull_winId, nAllPull);
1614     snew(allPull_pullId, nAllPull);
1615     iAllPull = 0;
1616     /* Setup one array of all pull groups */
1617     for (i = 0; i < nWindows; i++)
1618     {
1619         for (j = 0; j < window[i].nPull; j++)
1620         {
1621             allPull_winId[iAllPull]  = i;
1622             allPull_pullId[iAllPull] = j;
1623             iAllPull++;
1624         }
1625     }
1626
1627     /* setup stuff for synthetic windows */
1628     snew(synthWindow, nAllPull);
1629     for (i = 0; i < nAllPull; i++)
1630     {
1631         synthWindow[i].nPull = 1;
1632         synthWindow[i].nBin  = opt->bins;
1633         snew(synthWindow[i].Histo, 1);
1634         if (opt->bsMethod == bsMethod_traj || opt->bsMethod == bsMethod_trajGauss)
1635         {
1636             snew(synthWindow[i].Histo[0], opt->bins);
1637         }
1638         snew(synthWindow[i].N, 1);
1639         snew(synthWindow[i].pos, 1);
1640         snew(synthWindow[i].z, 1);
1641         snew(synthWindow[i].k, 1);
1642         snew(synthWindow[i].bContrib, 1);
1643         snew(synthWindow[i].g, 1);
1644         snew(synthWindow[i].bsWeight, 1);
1645     }
1646
1647     switch (opt->bsMethod)
1648     {
1649         case bsMethod_hist:
1650             snew(randomArray, nAllPull);
1651             printf("\n\nWhen computing statistical errors by bootstrapping entire histograms:\n");
1652             please_cite(stdout, "Hub2006");
1653             break;
1654         case bsMethod_BayesianHist:
1655             /* just copy all histogams into synthWindow array */
1656             for (i = 0; i < nAllPull; i++)
1657             {
1658                 winid  = allPull_winId [i];
1659                 pullid = allPull_pullId[i];
1660                 copy_pullgrp_to_synthwindow(synthWindow+i, window+winid, pullid);
1661             }
1662             break;
1663         case bsMethod_traj:
1664         case bsMethod_trajGauss:
1665             calc_cumulatives(window, nWindows, opt, fnhist);
1666             break;
1667         default:
1668             gmx_fatal(FARGS, "Unknown bootstrap method. That should not have happened.\n");
1669     }
1670
1671     /* do bootstrapping */
1672     fp = xvgropen(fnprof, "Boot strap profiles", xlabel, ylabel, opt->oenv);
1673     for (ib = 0; ib < opt->nBootStrap; ib++)
1674     {
1675         printf("  *******************************************\n"
1676                "  ******** Start bootstrap nr %d ************\n"
1677                "  *******************************************\n", ib+1);
1678
1679         switch (opt->bsMethod)
1680         {
1681             case bsMethod_hist:
1682                 /* bootstrap complete histograms from given histograms */
1683                 getRandomIntArray(nAllPull, opt->histBootStrapBlockLength, randomArray, opt->rng);
1684                 for (i = 0; i < nAllPull; i++)
1685                 {
1686                     winid  = allPull_winId [randomArray[i]];
1687                     pullid = allPull_pullId[randomArray[i]];
1688                     copy_pullgrp_to_synthwindow(synthWindow+i, window+winid, pullid);
1689                 }
1690                 break;
1691             case bsMethod_BayesianHist:
1692                 /* keep histos, but assign random weights ("Bayesian bootstrap") */
1693                 setRandomBsWeights(synthWindow, nAllPull, opt);
1694                 break;
1695             case bsMethod_traj:
1696             case bsMethod_trajGauss:
1697                 /* create new histos from given histos, that is generate new hypothetical
1698                    trajectories */
1699                 for (i = 0; i < nAllPull; i++)
1700                 {
1701                     winid  = allPull_winId[i];
1702                     pullid = allPull_pullId[i];
1703                     create_synthetic_histo(synthWindow+i, window+winid, pullid, opt);
1704                 }
1705                 break;
1706         }
1707
1708         /* write histos in case of verbose output */
1709         if (opt->bs_verbose)
1710         {
1711             print_histograms(fnhist, synthWindow, nAllPull, ib, opt);
1712         }
1713
1714         /* do wham */
1715         i         = 0;
1716         bExact    = FALSE;
1717         maxchange = 1e20;
1718         memcpy(bsProfile, profile, opt->bins*sizeof(double)); /* use profile as guess */
1719         do
1720         {
1721             if ( (i%opt->stepUpdateContrib) == 0)
1722             {
1723                 setup_acc_wham(bsProfile, synthWindow, nAllPull, opt);
1724             }
1725             if (maxchange < opt->Tolerance)
1726             {
1727                 bExact = TRUE;
1728             }
1729             if (((i%opt->stepchange) == 0 || i == 1) && i != 0)
1730             {
1731                 printf("\t%4d) Maximum change %e\n", i, maxchange);
1732             }
1733             calc_profile(bsProfile, synthWindow, nAllPull, opt, bExact);
1734             i++;
1735         }
1736         while ( (maxchange = calc_z(bsProfile, synthWindow, nAllPull, opt, bExact)) > opt->Tolerance || !bExact);
1737         printf("\tConverged in %d iterations. Final maximum change %g\n", i, maxchange);
1738
1739         if (opt->bLog)
1740         {
1741             prof_normalization_and_unit(bsProfile, opt);
1742         }
1743
1744         /* symmetrize profile around z=0 */
1745         if (opt->bSym)
1746         {
1747             symmetrizeProfile(bsProfile, opt);
1748         }
1749
1750         /* save stuff to get average and stddev */
1751         for (i = 0; i < opt->bins; i++)
1752         {
1753             tmp                = bsProfile[i];
1754             bsProfiles_av[i]  += tmp;
1755             bsProfiles_av2[i] += tmp*tmp;
1756             fprintf(fp, "%e\t%e\n", (i+0.5)*opt->dz+opt->min, tmp);
1757         }
1758         fprintf(fp, "%s\n", output_env_get_print_xvgr_codes(opt->oenv) ? "&" : "");
1759     }
1760     xvgrclose(fp);
1761
1762     /* write average and stddev */
1763     fp = xvgropen(fnres, "Average and stddev from bootstrapping", xlabel, ylabel, opt->oenv);
1764     if (output_env_get_print_xvgr_codes(opt->oenv))
1765     {
1766         fprintf(fp, "@TYPE xydy\n");
1767     }
1768     for (i = 0; i < opt->bins; i++)
1769     {
1770         bsProfiles_av [i] /= opt->nBootStrap;
1771         bsProfiles_av2[i] /= opt->nBootStrap;
1772         tmp                = bsProfiles_av2[i]-sqr(bsProfiles_av[i]);
1773         stddev             = (tmp >= 0.) ? sqrt(tmp) : 0.; /* Catch rouding errors */
1774         fprintf(fp, "%e\t%e\t%e\n", (i+0.5)*opt->dz+opt->min, bsProfiles_av [i], stddev);
1775     }
1776     xvgrclose(fp);
1777     printf("Wrote boot strap result to %s\n", fnres);
1778 }
1779
1780 //! Return type of input file based on file extension (xvg, pdo, or tpr)
1781 int whaminFileType(char *fn)
1782 {
1783     int len;
1784     len = strlen(fn);
1785     if (strcmp(fn+len-3, "tpr") == 0)
1786     {
1787         return whamin_tpr;
1788     }
1789     else if (strcmp(fn+len-3, "xvg") == 0 || strcmp(fn+len-6, "xvg.gz") == 0)
1790     {
1791         return whamin_pullxf;
1792     }
1793     else if (strcmp(fn+len-3, "pdo") == 0 || strcmp(fn+len-6, "pdo.gz") == 0)
1794     {
1795         return whamin_pdo;
1796     }
1797     else
1798     {
1799         gmx_fatal(FARGS, "Unknown file type of %s. Should be tpr, xvg, or pdo.\n", fn);
1800     }
1801     return whamin_unknown;
1802 }
1803
1804 //! Read the files names in pdo-files.dat, pullf/x-files.dat, tpr-files.dat
1805 void read_wham_in(const char *fn, char ***filenamesRet, int *nfilesRet,
1806                   t_UmbrellaOptions *opt)
1807 {
1808     char **filename = 0, tmp[WHAM_MAXFILELEN+2];
1809     int    nread, sizenow, i, block = 1;
1810     FILE  *fp;
1811
1812     fp      = gmx_ffopen(fn, "r");
1813     nread   = 0;
1814     sizenow = 0;
1815     while (fgets(tmp, sizeof(tmp), fp) != NULL)
1816     {
1817         if (strlen(tmp) >= WHAM_MAXFILELEN)
1818         {
1819             gmx_fatal(FARGS, "Filename too long in %s. Only %d characters allowed.\n", fn, WHAM_MAXFILELEN);
1820         }
1821         if (nread >= sizenow)
1822         {
1823             sizenow += block;
1824             srenew(filename, sizenow);
1825             for (i = sizenow-block; i < sizenow; i++)
1826             {
1827                 snew(filename[i], WHAM_MAXFILELEN);
1828             }
1829         }
1830         /* remove newline if there is one */
1831         if (tmp[strlen(tmp)-1] == '\n')
1832         {
1833             tmp[strlen(tmp)-1] = '\0';
1834         }
1835         strcpy(filename[nread], tmp);
1836         if (opt->verbose)
1837         {
1838             printf("Found file %s in %s\n", filename[nread], fn);
1839         }
1840         nread++;
1841     }
1842     *filenamesRet = filename;
1843     *nfilesRet    = nread;
1844 }
1845
1846 //! Open a file or a pipe to a gzipped file
1847 FILE *open_pdo_pipe(const char *fn, t_UmbrellaOptions *opt, gmx_bool *bPipeOpen)
1848 {
1849     char            Buffer[1024], gunzip[1024], *Path = 0;
1850     FILE           *pipe   = 0;
1851     static gmx_bool bFirst = 1;
1852
1853     /* gzipped pdo file? */
1854     if ((strcmp(fn+strlen(fn)-3, ".gz") == 0))
1855     {
1856         /* search gunzip executable */
1857         if (!(Path = getenv("GMX_PATH_GZIP")))
1858         {
1859             if (gmx_fexist("/bin/gunzip"))
1860             {
1861                 sprintf(gunzip, "%s", "/bin/gunzip");
1862             }
1863             else if (gmx_fexist("/usr/bin/gunzip"))
1864             {
1865                 sprintf(gunzip, "%s", "/usr/bin/gunzip");
1866             }
1867             else
1868             {
1869                 gmx_fatal(FARGS, "Cannot find executable gunzip in /bin or /usr/bin.\n"
1870                           "You may want to define the path to gunzip "
1871                           "with the environment variable GMX_PATH_GZIP.", gunzip);
1872             }
1873         }
1874         else
1875         {
1876             sprintf(gunzip, "%s/gunzip", Path);
1877             if (!gmx_fexist(gunzip))
1878             {
1879                 gmx_fatal(FARGS, "Cannot find executable %s. Please define the path to gunzip"
1880                           " in the environmental varialbe GMX_PATH_GZIP.", gunzip);
1881             }
1882         }
1883         if (bFirst)
1884         {
1885             printf("Using gunzig executable %s\n", gunzip);
1886             bFirst = 0;
1887         }
1888         if (!gmx_fexist(fn))
1889         {
1890             gmx_fatal(FARGS, "File %s does not exist.\n", fn);
1891         }
1892         sprintf(Buffer, "%s -c < %s", gunzip, fn);
1893         if (opt->verbose)
1894         {
1895             printf("Executing command '%s'\n", Buffer);
1896         }
1897 #ifdef HAVE_PIPES
1898         if ((pipe = popen(Buffer, "r")) == NULL)
1899         {
1900             gmx_fatal(FARGS, "Unable to open pipe to `%s'\n", Buffer);
1901         }
1902 #else
1903         gmx_fatal(FARGS, "Cannot open a compressed file on platform without pipe support");
1904 #endif
1905         *bPipeOpen = TRUE;
1906     }
1907     else
1908     {
1909         pipe       = gmx_ffopen(fn, "r");
1910         *bPipeOpen = FALSE;
1911     }
1912
1913     return pipe;
1914 }
1915
1916 //! Close file or pipe
1917 void pdo_close_file(FILE *fp)
1918 {
1919 #ifdef HAVE_PIPES
1920     pclose(fp);
1921 #else
1922     gmx_ffclose(fp);
1923 #endif
1924 }
1925
1926 //! Reading all pdo files
1927 void read_pdo_files(char **fn, int nfiles, t_UmbrellaHeader* header,
1928                     t_UmbrellaWindow *window, t_UmbrellaOptions *opt)
1929 {
1930     FILE    *file;
1931     real     mintmp, maxtmp, done = 0.;
1932     int      i;
1933     gmx_bool bPipeOpen;
1934     /* char Buffer0[1000]; */
1935
1936     if (nfiles < 1)
1937     {
1938         gmx_fatal(FARGS, "No files found. Hick.");
1939     }
1940
1941     /* if min and max are not given, get min and max from the input files */
1942     if (opt->bAuto)
1943     {
1944         printf("Automatic determination of boundaries from %d pdo files...\n", nfiles);
1945         opt->min = 1e20;
1946         opt->max = -1e20;
1947         for (i = 0; i < nfiles; ++i)
1948         {
1949             file = open_pdo_pipe(fn[i], opt, &bPipeOpen);
1950             /*fgets(Buffer0,999,file);
1951                fprintf(stderr,"First line '%s'\n",Buffer0); */
1952             done = 100.0*(i+1)/nfiles;
1953             printf("\rOpening %s ... [%2.0f%%]", fn[i], done); fflush(stdout);
1954             if (opt->verbose)
1955             {
1956                 printf("\n");
1957             }
1958             read_pdo_header(file, header, opt);
1959             /* here only determine min and max of this window */
1960             read_pdo_data(file, header, i, NULL, opt, TRUE, &mintmp, &maxtmp);
1961             if (maxtmp > opt->max)
1962             {
1963                 opt->max = maxtmp;
1964             }
1965             if (mintmp < opt->min)
1966             {
1967                 opt->min = mintmp;
1968             }
1969             if (bPipeOpen)
1970             {
1971                 pdo_close_file(file);
1972             }
1973             else
1974             {
1975                 gmx_ffclose(file);
1976             }
1977         }
1978         printf("\n");
1979         printf("\nDetermined boundaries to %f and %f\n\n", opt->min, opt->max);
1980         if (opt->bBoundsOnly)
1981         {
1982             printf("Found option -boundsonly, now exiting.\n");
1983             exit (0);
1984         }
1985     }
1986     /* store stepsize in profile */
1987     opt->dz = (opt->max-opt->min)/opt->bins;
1988
1989     /* Having min and max, we read in all files */
1990     /* Loop over all files */
1991     for (i = 0; i < nfiles; ++i)
1992     {
1993         done = 100.0*(i+1)/nfiles;
1994         printf("\rOpening %s ... [%2.0f%%]", fn[i], done); fflush(stdout);
1995         if (opt->verbose)
1996         {
1997             printf("\n");
1998         }
1999         file = open_pdo_pipe(fn[i], opt, &bPipeOpen);
2000         read_pdo_header(file, header, opt);
2001         /* load data into window */
2002         read_pdo_data(file, header, i, window, opt, FALSE, NULL, NULL);
2003         if ((window+i)->Ntot[0] == 0)
2004         {
2005             fprintf(stderr, "\nWARNING, no data points read from file %s (check -b option)\n", fn[i]);
2006         }
2007         if (bPipeOpen)
2008         {
2009             pdo_close_file(file);
2010         }
2011         else
2012         {
2013             gmx_ffclose(file);
2014         }
2015     }
2016     printf("\n");
2017     for (i = 0; i < nfiles; ++i)
2018     {
2019         sfree(fn[i]);
2020     }
2021     sfree(fn);
2022 }
2023
2024 //! translate 0/1 to N/Y to write pull dimensions
2025 #define int2YN(a) (((a) == 0) ? ("N") : ("Y"))
2026
2027 //! Read pull groups from a tpr file (including position, force const, geometry, number of groups)
2028 void read_tpr_header(const char *fn, t_UmbrellaHeader* header, t_UmbrellaOptions *opt)
2029 {
2030     t_inputrec  ir;
2031     int         i, ncrd;
2032     t_state     state;
2033     static int  first = 1;
2034
2035     /* printf("Reading %s \n",fn); */
2036     read_tpx_state(fn, &ir, &state, NULL, NULL);
2037
2038     if (!ir.bPull)
2039     {
2040         gmx_fatal(FARGS, "This is not a tpr with COM pulling");
2041     }
2042
2043     /* nr of pull groups */
2044     ncrd = 0;
2045     for (i = 0; i < ir.pull->ncoord; i++)
2046     {
2047         if (ir.pull->coord[i].eType == epullUMBRELLA)
2048         {
2049             int m;
2050
2051             if (ncrd == 0)
2052             {
2053                 header->pull_geometry = ir.pull->coord[i].eGeom;
2054                 copy_ivec(ir.pull->coord[i].dim, header->pull_dim);
2055             }
2056
2057             if (ncrd != i)
2058             {
2059                 /* TODO: remove this restriction */
2060                 gmx_fatal(FARGS, "Currently tpr files where a non-umbrella pull coordinate is followed by an umbrella pull coordinate are not supported");
2061             }
2062
2063             for (m = 0; m < DIM; m++)
2064             {
2065                 if (ir.pull->coord[i].eGeom != header->pull_geometry)
2066                 {
2067                     /* TODO: remove the restriction */
2068                     gmx_fatal(FARGS, "Currently all umbrella pull coordinates should use the same geometry");
2069                 }
2070
2071                 if (ir.pull->coord[i].dim[m] != header->pull_dim[m])
2072                 {
2073                     /* TODO: remove the restriction */
2074                     gmx_fatal(FARGS, "Currently all umbrella pull coordinates should operate on the same dimensions");
2075                 }
2076             }
2077
2078             ncrd++;
2079         }
2080     }
2081
2082     if (ncrd < 1)
2083     {
2084         gmx_fatal(FARGS, "This is not a tpr of umbrella simulation. Found only %d umbrella pull coordinates\n", ncrd);
2085     }
2086
2087     header->npullcrds_tot = ir.pull->ncoord;
2088     header->npullcrds     = ncrd;
2089     header->bPrintRef     = ir.pull->bPrintCOM1;
2090     if (ir.pull->bPrintCOM2)
2091     {
2092         gmx_fatal(FARGS, "Reading pull output with printing of the COM of group 2 is currently not supported");
2093     }
2094     if (ir.pull->bPrintRefValue)
2095     {
2096         gmx_fatal(FARGS, "Reading pull output with printing of the reference value of the coordinates is currently not supported");
2097     }
2098     header->bPrintComp    = ir.pull->bPrintComp;
2099     header->pull_ndim     = header->pull_dim[0]+header->pull_dim[1]+header->pull_dim[2];
2100     /* We should add a struct for each pull coord with all pull coord data
2101        instead of allocating many arrays for each property */
2102     snew(header->k, ncrd);
2103     snew(header->init_dist, ncrd);
2104     snew(header->umbInitDist, ncrd);
2105
2106     /* only z-direction with epullgCYL? */
2107     if (header->pull_geometry == epullgCYL)
2108     {
2109         if (header->pull_dim[XX] || header->pull_dim[YY] || (!header->pull_dim[ZZ]))
2110         {
2111             gmx_fatal(FARGS, "With pull geometry 'cylinder', expected pulling in Z direction only.\n"
2112                       "However, found dimensions [%s %s %s]\n",
2113                       int2YN(header->pull_dim[XX]), int2YN(header->pull_dim[YY]),
2114                       int2YN(header->pull_dim[ZZ]));
2115         }
2116     }
2117
2118     for (i = 0; i < ncrd; i++)
2119     {
2120         header->k[i] = ir.pull->coord[i].k;
2121         if (header->k[i] == 0.0)
2122         {
2123             gmx_fatal(FARGS, "Pull coordinate %d has force constant of of 0.0 in %s.\n"
2124                       "That doesn't seem to be an Umbrella tpr.\n",
2125                       i, fn);
2126         }
2127         header->init_dist[i] =  ir.pull->coord[i].init;
2128
2129         /* initial distance to reference */
2130         switch (header->pull_geometry)
2131         {
2132             case epullgCYL:
2133             /* umbrella distance stored in init_dist[i] for geometry cylinder (not in ...[i][ZZ]) */
2134             case epullgDIST:
2135             case epullgDIR:
2136             case epullgDIRPBC:
2137                 header->umbInitDist[i] = header->init_dist[i];
2138                 break;
2139             default:
2140                 gmx_fatal(FARGS, "Pull geometry %s not supported\n", epullg_names[header->pull_geometry]);
2141         }
2142     }
2143
2144     if (opt->verbose || first)
2145     {
2146         printf("File %s, %d coordinates, geometry \"%s\", dimensions [%s %s %s], (%d dimensions)\n"
2147                "\tPull group coordinates%s expected in pullx files.\n",
2148                fn, header->npullcrds, epullg_names[header->pull_geometry],
2149                int2YN(header->pull_dim[0]), int2YN(header->pull_dim[1]), int2YN(header->pull_dim[2]),
2150                header->pull_ndim, (header->bPrintRef ? "" : " not"));
2151         for (i = 0; i < ncrd; i++)
2152         {
2153             printf("\tcrd %d) k = %-5g  position = %g\n", i, header->k[i], header->umbInitDist[i]);
2154         }
2155     }
2156     if (!opt->verbose && first)
2157     {
2158         printf("\tUse option -v to see this output for all input tpr files\n\n");
2159     }
2160
2161     first = 0;
2162 }
2163
2164 //! 2-norm in a ndim-dimensional space
2165 double dist_ndim(double **dx, int ndim, int line)
2166 {
2167     int    i;
2168     double r2 = 0.;
2169     for (i = 0; i < ndim; i++)
2170     {
2171         r2 += sqr(dx[i][line]);
2172     }
2173     return sqrt(r2);
2174 }
2175
2176 //! Read pullx.xvg or pullf.xvg
2177 void read_pull_xf(const char *fn, const char *fntpr, t_UmbrellaHeader * header,
2178                   t_UmbrellaWindow * window,
2179                   t_UmbrellaOptions *opt,
2180                   gmx_bool bGetMinMax, real *mintmp, real *maxtmp,
2181                   t_groupselection *groupsel)
2182 {
2183     double        **y = 0, pos = 0., t, force, time0 = 0., dt;
2184     int             ny, nt, bins, ibin, i, g, gUsed, dstep = 1, nColPerCrd, nColRefEachCrd, nColExpect, ntot, column;
2185     real            min, max, minfound = 1e20, maxfound = -1e20;
2186     gmx_bool        dt_ok, timeok, bHaveForce;
2187     const char     *quantity;
2188     const int       blocklen = 4096;
2189     int            *lennow   = 0;
2190     static gmx_bool bFirst   = TRUE;
2191
2192     /*
2193      * Data columns in pull output:
2194      *  - in force output pullf.xvg:
2195      *    No reference columns, one column per pull coordinate
2196      *
2197      *  - in position output pullx.xvg
2198      *    bPrintRef == TRUE:  for each pull coordinate: ndim reference columns, and ndim dx columns
2199      *    bPrintRef == FALSE: for each pull coordinate: no   reference columns, but ndim dx columns
2200      */
2201
2202     nColPerCrd = 1;
2203     if (opt->bPullx && header->bPrintComp)
2204     {
2205         nColPerCrd += header->pull_ndim;
2206     }
2207     quantity   = opt->bPullx ? "position" : "force";
2208
2209     if (opt->bPullx && header->bPrintRef)
2210     {
2211         nColRefEachCrd = header->pull_ndim;
2212     }
2213     else
2214     {
2215         nColRefEachCrd = 0;
2216     }
2217
2218     nColExpect = 1 + header->npullcrds*(nColRefEachCrd+nColPerCrd);
2219     bHaveForce = opt->bPullf;
2220
2221     /* With geometry "distance" or "distance_periodic", only force reading is supported so far.
2222        That avoids the somewhat tedious extraction of the reaction coordinate from the pullx files.
2223        Sorry for the laziness, this is a To-do. */
2224     if  ( (header->pull_geometry == epullgDIR || header->pull_geometry == epullgDIRPBC)
2225           && opt->bPullx)
2226     {
2227         gmx_fatal(FARGS, "With pull geometries \"direction\" and \"direction_periodic\", only pull force "
2228                   "reading \n(option -if) is supported at present, "
2229                   "not pull position reading (options -ix).\nMake sure mdrun writes the pull "
2230                   "forces (pullf.xvg files)\nand provide them to g_wham with option -if.",
2231                   epullg_names[header->pull_geometry]);
2232     }
2233
2234     nt = read_xvg(fn, &y, &ny);
2235
2236     /* Check consistency */
2237     if (nt < 1)
2238     {
2239         gmx_fatal(FARGS, "Empty pull %s file %s\n", quantity, fn);
2240     }
2241     if (bFirst)
2242     {
2243         printf("Reading pull %s file with pull geometry %s and %d pull dimensions\n",
2244                bHaveForce ? "force" : "position", epullg_names[header->pull_geometry],
2245                header->pull_ndim);
2246         /* Since this tool code has not updated yet to allow difference pull
2247          * dimensions per pull coordinate, we can't easily check the exact
2248          * number of expected columns, so we only check what we expect for
2249          * the pull coordinates selected for the WHAM analysis.
2250          */
2251         printf("Expecting these columns in pull file:\n"
2252                "\t%d reference columns for each individual pull coordinate\n"
2253                "\t%d data columns for each pull coordinate\n", nColRefEachCrd, nColPerCrd);
2254         printf("With %d pull groups, expect %s%d columns (including the time column)\n",
2255                header->npullcrds,
2256                header->npullcrds < header->npullcrds_tot ? "at least " : "",
2257                nColExpect);
2258         bFirst = FALSE;
2259     }
2260     if (ny < nColExpect ||
2261         (header->npullcrds == header->npullcrds_tot && ny > nColExpect))
2262     {
2263         gmx_fatal(FARGS, "Using %d pull coodinates from %s,\n but found %d data columns in %s (expected %s%d)\n"
2264                   "\nMaybe you confused options -ix and -if ?\n",
2265                   header->npullcrds, fntpr, ny-1, fn,
2266                   header->npullcrds < header->npullcrds_tot ? "at least " : "",
2267                   nColExpect-1);
2268     }
2269
2270     if (opt->verbose)
2271     {
2272         printf("Found %d times and %d %s sets %s\n", nt, (ny-1)/nColPerCrd, quantity, fn);
2273     }
2274
2275     if (!bGetMinMax)
2276     {
2277         bins = opt->bins;
2278         min  = opt->min;
2279         max  = opt->max;
2280         if (nt > 1)
2281         {
2282             window->dt = y[0][1]-y[0][0];
2283         }
2284         else if (opt->nBootStrap && opt->tauBootStrap != 0.0)
2285         {
2286             fprintf(stderr, "\n *** WARNING, Could not determine time step in %s\n", fn);
2287         }
2288
2289         /* Need to alocate memory and set up structure */
2290
2291         if (groupsel)
2292         {
2293             /* Use only groups selected with option -is file */
2294             if (header->npullcrds != groupsel->n)
2295             {
2296                 gmx_fatal(FARGS, "tpr file contains %d pull groups, but expected %d from group selection file\n",
2297                           header->npullcrds, groupsel->n);
2298             }
2299             window->nPull = groupsel->nUse;
2300         }
2301         else
2302         {
2303             window->nPull = header->npullcrds;
2304         }
2305
2306         window->nBin = bins;
2307         snew(window->Histo, window->nPull);
2308         snew(window->z, window->nPull);
2309         snew(window->k, window->nPull);
2310         snew(window->pos, window->nPull);
2311         snew(window->N, window->nPull);
2312         snew(window->Ntot, window->nPull);
2313         snew(window->g, window->nPull);
2314         snew(window->bsWeight, window->nPull);
2315         window->bContrib = 0;
2316
2317         if (opt->bCalcTauInt)
2318         {
2319             snew(window->ztime, window->nPull);
2320         }
2321         else
2322         {
2323             window->ztime = NULL;
2324         }
2325         snew(lennow, window->nPull);
2326
2327         for (g = 0; g < window->nPull; ++g)
2328         {
2329             window->z[g]        = 1;
2330             window->bsWeight[g] = 1.;
2331             snew(window->Histo[g], bins);
2332             window->N[g]    = 0;
2333             window->Ntot[g] = 0;
2334             window->g[g]    = 1.;
2335             if (opt->bCalcTauInt)
2336             {
2337                 window->ztime[g] = NULL;
2338             }
2339         }
2340
2341         /* Copying umbrella center and force const is more involved since not
2342            all pull groups from header (tpr file) may be used in window variable */
2343         for (g = 0, gUsed = 0; g < header->npullcrds; ++g)
2344         {
2345             if (groupsel && (groupsel->bUse[g] == FALSE))
2346             {
2347                 continue;
2348             }
2349             window->k[gUsed]   = header->k[g];
2350             window->pos[gUsed] = header->umbInitDist[g];
2351             gUsed++;
2352         }
2353     }
2354     else
2355     {   /* only determine min and max */
2356         minfound = 1e20;
2357         maxfound = -1e20;
2358         min      = max = bins = 0; /* Get rid of warnings */
2359     }
2360
2361
2362     for (i = 0; i < nt; i++)
2363     {
2364         /* Do you want that time frame? */
2365         t = 1.0/1000*( static_cast<int> ((y[0][i]*1000) + 0.5)); /* round time to fs */
2366
2367         /* get time step of pdo file and get dstep from opt->dt */
2368         if (i == 0)
2369         {
2370             time0 = t;
2371         }
2372         else if (i == 1)
2373         {
2374             dt = t-time0;
2375             if (opt->dt > 0.0)
2376             {
2377                 dstep = static_cast<int>(opt->dt/dt+0.5);
2378                 if (dstep == 0)
2379                 {
2380                     dstep = 1;
2381                 }
2382             }
2383             if (!bGetMinMax)
2384             {
2385                 window->dt = dt*dstep;
2386             }
2387         }
2388
2389         dt_ok  = (i%dstep == 0);
2390         timeok = (dt_ok && t >= opt->tmin && t <= opt->tmax);
2391         /*if (opt->verbose)
2392            printf(" time = %f, (tmin,tmax)=(%e,%e), dt_ok=%d timeok=%d\n",
2393            t,opt->tmin, opt->tmax, dt_ok,timeok); */
2394         if (timeok)
2395         {
2396             /* Note: if groupsel == NULL:
2397              *          all groups in pullf/x file are stored in this window, and gUsed == g
2398              *       if groupsel != NULL:
2399              *          only groups with groupsel.bUse[g]==TRUE are stored. gUsed is not always equal g
2400              */
2401             gUsed = -1;
2402             for (g = 0; g < header->npullcrds; ++g)
2403             {
2404                 /* was this group selected for application in WHAM? */
2405                 if (groupsel && (groupsel->bUse[g] == FALSE))
2406                 {
2407                     continue;
2408                 }
2409
2410                 gUsed++;
2411
2412                 if (bHaveForce)
2413                 {
2414                     /* y has 1 time column y[0] and one column per force y[1],...,y[nCrds] */
2415                     force = y[g+1][i];
2416                     pos   = -force/header->k[g] + header->umbInitDist[g];
2417                 }
2418                 else
2419                 {
2420                     /* pick the right column from:
2421                      * time ref1[ndim] coord1[1(ndim)] ref2[ndim] coord2[1(ndim)] ...
2422                      */
2423                     column = 1 +  nColRefEachCrd + g*(nColRefEachCrd+nColPerCrd);
2424                     pos    = y[column][i];
2425                 }
2426
2427                 /* printf("crd %d dpos %f poseq %f pos %f \n",g,dpos,poseq,pos); */
2428                 if (bGetMinMax)
2429                 {
2430                     if (pos < minfound)
2431                     {
2432                         minfound = pos;
2433                     }
2434                     if (pos > maxfound)
2435                     {
2436                         maxfound = pos;
2437                     }
2438                 }
2439                 else
2440                 {
2441                     if (gUsed >= window->nPull)
2442                     {
2443                         gmx_fatal(FARGS, "gUsed too large (%d, nPull=%d). This error should have been catched before.\n",
2444                                   gUsed, window->nPull);
2445                     }
2446
2447                     if (opt->bCalcTauInt && !bGetMinMax)
2448                     {
2449                         /* save time series for autocorrelation analysis */
2450                         ntot = window->Ntot[gUsed];
2451                         /* printf("i %d, ntot %d, lennow[g] = %d\n",i,ntot,lennow[g]); */
2452                         if (ntot >= lennow[gUsed])
2453                         {
2454                             lennow[gUsed] += blocklen;
2455                             srenew(window->ztime[gUsed], lennow[gUsed]);
2456                         }
2457                         window->ztime[gUsed][ntot] = pos;
2458                     }
2459
2460                     ibin = static_cast<int> (floor((pos-min)/(max-min)*bins));
2461                     if (opt->bCycl)
2462                     {
2463                         if (ibin < 0)
2464                         {
2465                             while ( (ibin += bins) < 0)
2466                             {
2467                                 ;
2468                             }
2469                         }
2470                         else if (ibin >= bins)
2471                         {
2472                             while ( (ibin -= bins) >= bins)
2473                             {
2474                                 ;
2475                             }
2476                         }
2477                     }
2478                     if (ibin >= 0 && ibin < bins)
2479                     {
2480                         window->Histo[gUsed][ibin] += 1.;
2481                         window->N[gUsed]++;
2482                     }
2483                     window->Ntot[gUsed]++;
2484                 }
2485             }
2486         }
2487         else if (t > opt->tmax)
2488         {
2489             if (opt->verbose)
2490             {
2491                 printf("time %f larger than tmax %f, stop reading this pullx/pullf file\n", t, opt->tmax);
2492             }
2493             break;
2494         }
2495     }
2496
2497     if (bGetMinMax)
2498     {
2499         *mintmp = minfound;
2500         *maxtmp = maxfound;
2501     }
2502     sfree(lennow);
2503     for (i = 0; i < ny; i++)
2504     {
2505         sfree(y[i]);
2506     }
2507 }
2508
2509 //! read pullf-files.dat or pullx-files.dat and tpr-files.dat
2510 void read_tpr_pullxf_files(char **fnTprs, char **fnPull, int nfiles,
2511                            t_UmbrellaHeader* header,
2512                            t_UmbrellaWindow *window, t_UmbrellaOptions *opt)
2513 {
2514     int  i;
2515     real mintmp, maxtmp;
2516
2517     printf("Reading %d tpr and pullf files\n", nfiles/2);
2518
2519     /* min and max not given? */
2520     if (opt->bAuto)
2521     {
2522         printf("Automatic determination of boundaries...\n");
2523         opt->min = 1e20;
2524         opt->max = -1e20;
2525         for (i = 0; i < nfiles; i++)
2526         {
2527             if (whaminFileType(fnTprs[i]) != whamin_tpr)
2528             {
2529                 gmx_fatal(FARGS, "Expected the %d'th file in input file to be a tpr file\n", i);
2530             }
2531             read_tpr_header(fnTprs[i], header, opt);
2532             if (whaminFileType(fnPull[i]) != whamin_pullxf)
2533             {
2534                 gmx_fatal(FARGS, "Expected the %d'th file in input file to be a xvg (pullx/pullf) file\n", i);
2535             }
2536             read_pull_xf(fnPull[i], fnTprs[i], header, NULL, opt, TRUE, &mintmp, &maxtmp,
2537                          (opt->nGroupsel > 0) ? &opt->groupsel[i] : NULL);
2538             if (maxtmp > opt->max)
2539             {
2540                 opt->max = maxtmp;
2541             }
2542             if (mintmp < opt->min)
2543             {
2544                 opt->min = mintmp;
2545             }
2546         }
2547         printf("\nDetermined boundaries to %f and %f\n\n", opt->min, opt->max);
2548         if (opt->bBoundsOnly)
2549         {
2550             printf("Found option -boundsonly, now exiting.\n");
2551             exit (0);
2552         }
2553     }
2554     /* store stepsize in profile */
2555     opt->dz = (opt->max-opt->min)/opt->bins;
2556
2557     for (i = 0; i < nfiles; i++)
2558     {
2559         if (whaminFileType(fnTprs[i]) != whamin_tpr)
2560         {
2561             gmx_fatal(FARGS, "Expected the %d'th file in input file to be a tpr file\n", i);
2562         }
2563         read_tpr_header(fnTprs[i], header, opt);
2564         if (whaminFileType(fnPull[i]) != whamin_pullxf)
2565         {
2566             gmx_fatal(FARGS, "Expected the %d'th file in input file to be a xvg (pullx/pullf) file\n", i);
2567         }
2568         read_pull_xf(fnPull[i], fnTprs[i], header, window+i, opt, FALSE, NULL, NULL,
2569                      (opt->nGroupsel > 0) ? &opt->groupsel[i] : NULL);
2570         if (window[i].Ntot[0] == 0)
2571         {
2572             fprintf(stderr, "\nWARNING, no data points read from file %s (check -b option)\n", fnPull[i]);
2573         }
2574     }
2575
2576     for (i = 0; i < nfiles; i++)
2577     {
2578         sfree(fnTprs[i]);
2579         sfree(fnPull[i]);
2580     }
2581     sfree(fnTprs);
2582     sfree(fnPull);
2583 }
2584
2585 /*! \brief Read integrated autocorrelation times from input file (option -iiact)
2586  *
2587  * Note: Here we consider tau[int] := int_0^inf ACF(t) as the integrated autocorrelation times.
2588  * The factor `g := 1 + 2*tau[int]` subsequently enters the uncertainty.
2589  */
2590 void readIntegratedAutocorrelationTimes(t_UmbrellaWindow *window, int nwins, const char* fn)
2591 {
2592     int      nlines, ny, i, ig;
2593     double **iact;
2594
2595     printf("Readging Integrated autocorrelation times from %s ...\n", fn);
2596     nlines = read_xvg(fn, &iact, &ny);
2597     if (nlines != nwins)
2598     {
2599         gmx_fatal(FARGS, "Found %d lines with integrated autocorrelation times in %s.\nExpected %d",
2600                   nlines, fn, nwins);
2601     }
2602     for (i = 0; i < nlines; i++)
2603     {
2604         if (window[i].nPull != ny)
2605         {
2606             gmx_fatal(FARGS, "You are providing autocorrelation times with option -iiact and the\n"
2607                       "number of pull groups is different in different simulations. That is not\n"
2608                       "supported yet. Sorry.\n");
2609         }
2610         for (ig = 0; ig < window[i].nPull; ig++)
2611         {
2612             /* compare Kumar et al, J Comp Chem 13, 1011-1021 (1992) */
2613             window[i].g[ig] = 1+2*iact[ig][i]/window[i].dt;
2614
2615             if (iact[ig][i] <= 0.0)
2616             {
2617                 fprintf(stderr, "\nWARNING, IACT = %f (window %d, group %d)\n", iact[ig][i], i, ig);
2618             }
2619         }
2620     }
2621 }
2622
2623
2624 /*! \brief Smooth autocorreltion times along the reaction coordinate.
2625  *
2626  * This is useful
2627  * if the ACT is subject to high uncertainty in case if limited sampling. Note
2628  * that -in case of limited sampling- the ACT may be severely underestimated.
2629  * Note: the g=1+2tau are overwritten.
2630  * If opt->bAllowReduceIact==FALSE, the ACTs are never reduced, only increased
2631  * by the smoothing
2632  */
2633 void smoothIact(t_UmbrellaWindow *window, int nwins, t_UmbrellaOptions *opt)
2634 {
2635     int    i, ig, j, jg;
2636     double pos, dpos2, siglim, siglim2, gaufact, invtwosig2, w, weight, tausm;
2637
2638     /* only evaluate within +- 3sigma of the Gausian */
2639     siglim  = 3.0*opt->sigSmoothIact;
2640     siglim2 = dsqr(siglim);
2641     /* pre-factor of Gaussian */
2642     gaufact    = 1.0/(sqrt(2*M_PI)*opt->sigSmoothIact);
2643     invtwosig2 = 0.5/dsqr(opt->sigSmoothIact);
2644
2645     for (i = 0; i < nwins; i++)
2646     {
2647         snew(window[i].tausmooth, window[i].nPull);
2648         for (ig = 0; ig < window[i].nPull; ig++)
2649         {
2650             tausm  = 0.;
2651             weight = 0;
2652             pos    = window[i].pos[ig];
2653             for (j = 0; j < nwins; j++)
2654             {
2655                 for (jg = 0; jg < window[j].nPull; jg++)
2656                 {
2657                     dpos2 = dsqr(window[j].pos[jg]-pos);
2658                     if (dpos2 < siglim2)
2659                     {
2660                         w       = gaufact*exp(-dpos2*invtwosig2);
2661                         weight += w;
2662                         tausm  += w*window[j].tau[jg];
2663                         /*printf("Weight %g dpos2=%g pos=%g gaufact=%g invtwosig2=%g\n",
2664                            w,dpos2,pos,gaufact,invtwosig2); */
2665                     }
2666                 }
2667             }
2668             tausm /= weight;
2669             if (opt->bAllowReduceIact || tausm > window[i].tau[ig])
2670             {
2671                 window[i].tausmooth[ig] = tausm;
2672             }
2673             else
2674             {
2675                 window[i].tausmooth[ig] = window[i].tau[ig];
2676             }
2677             window[i].g[ig] = 1+2*tausm/window[i].dt;
2678         }
2679     }
2680 }
2681
2682 //! Stop integrating autoccorelation function when ACF drops under this value
2683 #define WHAM_AC_ZERO_LIMIT 0.05
2684
2685 /*! \brief Try to compute the autocorrelation time for each umbrealla window
2686  */
2687 void calcIntegratedAutocorrelationTimes(t_UmbrellaWindow *window, int nwins,
2688                                         t_UmbrellaOptions *opt, const char *fn)
2689 {
2690     int   i, ig, ncorr, ntot, j, k, *count, restart;
2691     real *corr, c0, dt, tmp;
2692     real *ztime, av, tausteps;
2693     FILE *fp, *fpcorr = 0;
2694
2695     if (opt->verbose)
2696     {
2697         fpcorr = xvgropen("hist_autocorr.xvg", "Autocorrelation functions of umbrella windows",
2698                           "time [ps]", "autocorrelation function", opt->oenv);
2699     }
2700
2701     printf("\n");
2702     for (i = 0; i < nwins; i++)
2703     {
2704         printf("\rEstimating integrated autocorreltion times ... [%2.0f%%] ...", 100.*(i+1)/nwins);
2705         fflush(stdout);
2706         ntot = window[i].Ntot[0];
2707
2708         /* using half the maximum time as length of autocorrelation function */
2709         ncorr = ntot/2;
2710         if (ntot < 10)
2711         {
2712             gmx_fatal(FARGS, "Tryig to estimtate autocorrelation time from only %d"
2713                       " points. Provide more pull data!", ntot);
2714         }
2715         snew(corr, ncorr);
2716         /* snew(corrSq,ncorr); */
2717         snew(count, ncorr);
2718         dt = window[i].dt;
2719         snew(window[i].tau, window[i].nPull);
2720         restart = static_cast<int>(opt->acTrestart/dt+0.5);
2721         if (restart == 0)
2722         {
2723             restart = 1;
2724         }
2725
2726         for (ig = 0; ig < window[i].nPull; ig++)
2727         {
2728             if (ntot != window[i].Ntot[ig])
2729             {
2730                 gmx_fatal(FARGS, "Encountered different nr of frames in different pull groups.\n"
2731                           "That should not happen. (%d and %d)\n", ntot, window[i].Ntot[ig]);
2732             }
2733             ztime = window[i].ztime[ig];
2734
2735             /* calc autocorrelation function C(t) = < [z(tau)-<z>]*[z(tau+t)-<z>]> */
2736             for (j = 0, av = 0; (j < ntot); j++)
2737             {
2738                 av += ztime[j];
2739             }
2740             av /= ntot;
2741             for (k = 0; (k < ncorr); k++)
2742             {
2743                 corr[k]  = 0.;
2744                 count[k] = 0;
2745             }
2746             for (j = 0; (j < ntot); j += restart)
2747             {
2748                 for (k = 0; (k < ncorr) && (j+k < ntot); k++)
2749                 {
2750                     tmp        = (ztime[j]-av)*(ztime[j+k]-av);
2751                     corr  [k] += tmp;
2752                     /* corrSq[k] += tmp*tmp; */
2753                     count[k]++;
2754                 }
2755             }
2756             /* divide by nr of frames for each time displacement */
2757             for (k = 0; (k < ncorr); k++)
2758             {
2759                 /* count probably = (ncorr-k+(restart-1))/restart; */
2760                 corr[k] = corr[k]/count[k];
2761                 /* variance of autocorrelation function */
2762                 /* corrSq[k]=corrSq[k]/count[k]; */
2763             }
2764             /* normalize such that corr[0] == 0 */
2765             c0 = 1./corr[0];
2766             for (k = 0; (k < ncorr); k++)
2767             {
2768                 corr[k] *= c0;
2769                 /* corrSq[k]*=c0*c0; */
2770             }
2771
2772             /* write ACFs in verbose mode */
2773             if (fpcorr)
2774             {
2775                 for (k = 0; (k < ncorr); k++)
2776                 {
2777                     fprintf(fpcorr, "%g  %g\n", k*dt, corr[k]);
2778                 }
2779                 fprintf(fpcorr, "%s\n", output_env_get_print_xvgr_codes(opt->oenv) ? "&" : "");
2780             }
2781
2782             /* esimate integrated correlation time, fitting is too unstable */
2783             tausteps = 0.5*corr[0];
2784             /* consider corr below WHAM_AC_ZERO_LIMIT as noise */
2785             for (j = 1; (j < ncorr) && (corr[j] > WHAM_AC_ZERO_LIMIT); j++)
2786             {
2787                 tausteps += corr[j];
2788             }
2789
2790             /* g = 1+2*tau, see. Ferrenberg/Swendsen, PRL 63:1195 (1989) or
2791                Kumar et al, eq. 28 ff. */
2792             window[i].tau[ig] = tausteps*dt;
2793             window[i].g[ig]   = 1+2*tausteps;
2794             /* printf("win %d, group %d, estimated correlation time = %g ps\n",i,ig,window[i].tau[ig]); */
2795         } /* ig loop */
2796         sfree(corr);
2797         sfree(count);
2798     }
2799     printf(" done\n");
2800     if (fpcorr)
2801     {
2802         xvgrclose(fpcorr);
2803     }
2804
2805     /* plot IACT along reaction coordinate */
2806     fp = xvgropen(fn, "Integrated autocorrelation times", xlabel, "IACT [ps]", opt->oenv);
2807     if (output_env_get_print_xvgr_codes(opt->oenv))
2808     {
2809         fprintf(fp, "@    s0 symbol 1\n@    s0 symbol size 0.5\n@    s0 line linestyle 0\n");
2810         fprintf(fp, "#  WIN   tau(gr1)  tau(gr2) ...\n");
2811         for (i = 0; i < nwins; i++)
2812         {
2813             fprintf(fp, "# %3d   ", i);
2814             for (ig = 0; ig < window[i].nPull; ig++)
2815             {
2816                 fprintf(fp, " %11g", window[i].tau[ig]);
2817             }
2818             fprintf(fp, "\n");
2819         }
2820     }
2821     for (i = 0; i < nwins; i++)
2822     {
2823         for (ig = 0; ig < window[i].nPull; ig++)
2824         {
2825             fprintf(fp, "%8g %8g\n", window[i].pos[ig], window[i].tau[ig]);
2826         }
2827     }
2828     if (opt->sigSmoothIact > 0.0)
2829     {
2830         printf("Smoothing autocorrelation times along reaction coordinate with Gaussian of sig = %g\n",
2831                opt->sigSmoothIact);
2832         /* smooth IACT along reaction coordinate and overwrite g=1+2tau */
2833         smoothIact(window, nwins, opt);
2834         fprintf(fp, "%s\n", output_env_get_print_xvgr_codes(opt->oenv) ? "&" : "");
2835         if (output_env_get_print_xvgr_codes(opt->oenv))
2836         {
2837             fprintf(fp, "@    s1 symbol 1\n@    s1 symbol size 0.5\n@    s1 line linestyle 0\n");
2838             fprintf(fp, "@    s1 symbol color 2\n");
2839         }
2840         for (i = 0; i < nwins; i++)
2841         {
2842             for (ig = 0; ig < window[i].nPull; ig++)
2843             {
2844                 fprintf(fp, "%8g %8g\n", window[i].pos[ig], window[i].tausmooth[ig]);
2845             }
2846         }
2847     }
2848     xvgrclose(fp);
2849     printf("Wrote %s\n", fn);
2850 }
2851
2852 /*! \brief
2853  * compute average and sigma of each umbrella histogram
2854  */
2855 void averageSigma(t_UmbrellaWindow *window, int nwins)
2856 {
2857     int  i, ig, ntot, k;
2858     real av, sum2, sig, diff, *ztime, nSamplesIndep;
2859
2860     for (i = 0; i < nwins; i++)
2861     {
2862         snew(window[i].aver, window[i].nPull);
2863         snew(window[i].sigma, window[i].nPull);
2864
2865         ntot = window[i].Ntot[0];
2866         for (ig = 0; ig < window[i].nPull; ig++)
2867         {
2868             ztime = window[i].ztime[ig];
2869             for (k = 0, av = 0.; k < ntot; k++)
2870             {
2871                 av += ztime[k];
2872             }
2873             av /= ntot;
2874             for (k = 0, sum2 = 0.; k < ntot; k++)
2875             {
2876                 diff  = ztime[k]-av;
2877                 sum2 += diff*diff;
2878             }
2879             sig                = sqrt(sum2/ntot);
2880             window[i].aver[ig] = av;
2881
2882             /* Note: This estimate for sigma is biased from the limited sampling.
2883                Correct sigma by n/(n-1) where n = number of independent
2884                samples. Only possible if IACT is known.
2885              */
2886             if (window[i].tau)
2887             {
2888                 nSamplesIndep       = window[i].N[ig]/(window[i].tau[ig]/window[i].dt);
2889                 window[i].sigma[ig] = sig * nSamplesIndep/(nSamplesIndep-1);
2890             }
2891             else
2892             {
2893                 window[i].sigma[ig] = sig;
2894             }
2895             printf("win %d, aver = %f  sig = %f\n", i, av, window[i].sigma[ig]);
2896         }
2897     }
2898 }
2899
2900
2901 /*! \brief
2902  * Use histograms to compute average force on pull group.
2903  */
2904 void computeAverageForce(t_UmbrellaWindow *window, int nWindows, t_UmbrellaOptions *opt)
2905 {
2906     int    i, j, bins = opt->bins, k;
2907     double dz, min = opt->min, max = opt->max, displAv, temp, distance, ztot, ztot_half, w, weight;
2908     double posmirrored;
2909
2910     dz        = (max-min)/bins;
2911     ztot      = opt->max-min;
2912     ztot_half = ztot/2;
2913
2914     /* Compute average displacement from histograms */
2915     for (j = 0; j < nWindows; ++j)
2916     {
2917         snew(window[j].forceAv, window[j].nPull);
2918         for (k = 0; k < window[j].nPull; ++k)
2919         {
2920             displAv = 0.0;
2921             weight  = 0.0;
2922             for (i = 0; i < opt->bins; ++i)
2923             {
2924                 temp     = (1.0*i+0.5)*dz+min;
2925                 distance = temp - window[j].pos[k];
2926                 if (opt->bCycl)
2927                 {                                       /* in cyclic wham:             */
2928                     if (distance > ztot_half)           /*    |distance| < ztot_half   */
2929                     {
2930                         distance -= ztot;
2931                     }
2932                     else if (distance < -ztot_half)
2933                     {
2934                         distance += ztot;
2935                     }
2936                 }
2937                 w         = window[j].Histo[k][i]/window[j].g[k];
2938                 displAv  += w*distance;
2939                 weight   += w;
2940                 /* Are we near min or max? We are getting wrong forces from the histgrams since
2941                    the histograms are zero outside [min,max). Therefore, assume that the position
2942                    on the other side of the histomgram center is equally likely. */
2943                 if (!opt->bCycl)
2944                 {
2945                     posmirrored = window[j].pos[k]-distance;
2946                     if (posmirrored >= max || posmirrored < min)
2947                     {
2948                         displAv  += -w*distance;
2949                         weight   += w;
2950                     }
2951                 }
2952             }
2953             displAv  /= weight;
2954
2955             /* average force from average displacement */
2956             window[j].forceAv[k] = displAv*window[j].k[k];
2957             /* sigma from average square displacement */
2958             /* window[j].sigma  [k] = sqrt(displAv2); */
2959             /* printf("Win %d, sigma = %f\n",j,sqrt(displAv2)); */
2960         }
2961     }
2962 }
2963
2964 /*! \brief
2965  * Check if the complete reaction coordinate is covered by the histograms
2966  */
2967 void  checkReactionCoordinateCovered(t_UmbrellaWindow *window, int nwins,
2968                                      t_UmbrellaOptions *opt)
2969 {
2970     int  i, ig, j, bins = opt->bins, bBoundary;
2971     real avcount = 0, z, relcount, *count;
2972     snew(count, opt->bins);
2973
2974     for (j = 0; j < opt->bins; ++j)
2975     {
2976         for (i = 0; i < nwins; i++)
2977         {
2978             for (ig = 0; ig < window[i].nPull; ig++)
2979             {
2980                 count[j] += window[i].Histo[ig][j];
2981             }
2982         }
2983         avcount += 1.0*count[j];
2984     }
2985     avcount /= bins;
2986     for (j = 0; j < bins; ++j)
2987     {
2988         relcount  = count[j]/avcount;
2989         z         = (j+0.5)*opt->dz+opt->min;
2990         bBoundary = ( j<bins/20 || (bins-j)>bins/20 );
2991         /* check for bins with no data */
2992         if (count[j] == 0)
2993         {
2994             fprintf(stderr, "\nWARNING, no data point in bin %d (z=%g) !\n"
2995                     "You may not get a reasonable profile. Check your histograms!\n", j, z);
2996         }
2997         /* and check for poor sampling */
2998         else if (relcount < 0.005 && !bBoundary)
2999         {
3000             fprintf(stderr, "Warning, poor sampling bin %d (z=%g). Check your histograms!\n", j, z);
3001         }
3002     }
3003     sfree(count);
3004 }
3005
3006 /*! \brief Compute initial potential by integrating the average force
3007  *
3008  * This speeds up the convergence by roughly a factor of 2
3009  */
3010 void guessPotByIntegration(t_UmbrellaWindow *window, int nWindows, t_UmbrellaOptions *opt)
3011 {
3012     int    i, j, ig, bins = opt->bins, nHist, winmin, groupmin;
3013     double dz, min = opt->min, *pot, pos, hispos, dist, diff, fAv, distmin, *f;
3014     FILE  *fp;
3015
3016     dz = (opt->max-min)/bins;
3017
3018     printf("Getting initial potential by integration.\n");
3019
3020     /* Compute average displacement from histograms */
3021     computeAverageForce(window, nWindows, opt);
3022
3023     /* Get force for each bin from all histograms in this bin, or, alternatively,
3024        if no histograms are inside this bin, from the closest histogram */
3025     snew(pot, bins);
3026     snew(f, bins);
3027     for (j = 0; j < opt->bins; ++j)
3028     {
3029         pos      = (1.0*j+0.5)*dz+min;
3030         nHist    = 0;
3031         fAv      = 0.;
3032         distmin  = 1e20;
3033         groupmin = winmin = 0;
3034         for (i = 0; i < nWindows; i++)
3035         {
3036             for (ig = 0; ig < window[i].nPull; ig++)
3037             {
3038                 hispos = window[i].pos[ig];
3039                 dist   = fabs(hispos-pos);
3040                 /* average force within bin */
3041                 if (dist < dz/2)
3042                 {
3043                     nHist++;
3044                     fAv += window[i].forceAv[ig];
3045                 }
3046                 /* at the same time, remember closest histogram */
3047                 if (dist < distmin)
3048                 {
3049                     winmin   = i;
3050                     groupmin = ig;
3051                     distmin  = dist;
3052                 }
3053             }
3054         }
3055         /* if no histogram found in this bin, use closest histogram */
3056         if (nHist > 0)
3057         {
3058             fAv = fAv/nHist;
3059         }
3060         else
3061         {
3062             fAv = window[winmin].forceAv[groupmin];
3063         }
3064         f[j] = fAv;
3065     }
3066     for (j = 1; j < opt->bins; ++j)
3067     {
3068         pot[j] = pot[j-1] - 0.5*dz*(f[j-1]+f[j]);
3069     }
3070
3071     /* cyclic wham: linearly correct possible offset */
3072     if (opt->bCycl)
3073     {
3074         diff = (pot[bins-1]-pot[0])/(bins-1);
3075         for (j = 1; j < opt->bins; ++j)
3076         {
3077             pot[j] -= j*diff;
3078         }
3079     }
3080     if (opt->verbose)
3081     {
3082         fp = xvgropen("pmfintegrated.xvg", "PMF from force integration", xlabel, "PMF (kJ/mol)", opt->oenv);
3083         for (j = 0; j < opt->bins; ++j)
3084         {
3085             fprintf(fp, "%g  %g\n", (j+0.5)*dz+opt->min, pot[j]);
3086         }
3087         xvgrclose(fp);
3088         printf("verbose mode: wrote %s with PMF from interated forces\n", "pmfintegrated.xvg");
3089     }
3090
3091     /* get initial z=exp(-F[i]/kT) from integrated potential, where F[i] denote the free
3092        energy offsets which are usually determined by wham
3093        First: turn pot into probabilities:
3094      */
3095     for (j = 0; j < opt->bins; ++j)
3096     {
3097         pot[j] = exp(-pot[j]/(8.314e-3*opt->Temperature));
3098     }
3099     calc_z(pot, window, nWindows, opt, TRUE);
3100
3101     sfree(pot);
3102     sfree(f);
3103 }
3104
3105 //! Count number of words in an line
3106 static int wordcount(char *ptr)
3107 {
3108     int i, n, is[2];
3109     int cur = 0;
3110
3111     if (strlen(ptr) == 0)
3112     {
3113         return 0;
3114     }
3115     /* fprintf(stderr,"ptr='%s'\n",ptr); */
3116     n = 1;
3117     for (i = 0; (ptr[i] != '\0'); i++)
3118     {
3119         is[cur] = isspace(ptr[i]);
3120         if ((i > 0)  && (is[cur] && !is[1-cur]))
3121         {
3122             n++;
3123         }
3124         cur = 1-cur;
3125     }
3126     return n;
3127 }
3128
3129 /*! \brief Read input file for pull group selection (option -is)
3130  *
3131  * TO DO: ptr=fgets(...) is never freed (small memory leak)
3132  */
3133 void readPullGroupSelection(t_UmbrellaOptions *opt, char **fnTpr, int nTpr)
3134 {
3135     FILE *fp;
3136     int   i, iline, n, len = STRLEN, temp;
3137     char *ptr = 0, *tmpbuf = 0;
3138     char  fmt[1024], fmtign[1024];
3139     int   block = 1, sizenow;
3140
3141     fp            = gmx_ffopen(opt->fnGroupsel, "r");
3142     opt->groupsel = NULL;
3143
3144     snew(tmpbuf, len);
3145     sizenow = 0;
3146     iline   = 0;
3147     while ( (ptr = fgets3(fp, tmpbuf, &len)) != NULL)
3148     {
3149         trim(ptr);
3150         n = wordcount(ptr);
3151
3152         if (iline >= sizenow)
3153         {
3154             sizenow += block;
3155             srenew(opt->groupsel, sizenow);
3156         }
3157         opt->groupsel[iline].n    = n;
3158         opt->groupsel[iline].nUse = 0;
3159         snew(opt->groupsel[iline].bUse, n);
3160
3161         fmtign[0] = '\0';
3162         for (i = 0; i < n; i++)
3163         {
3164             strcpy(fmt, fmtign);
3165             strcat(fmt, "%d");
3166             if (sscanf(ptr, fmt, &temp))
3167             {
3168                 opt->groupsel[iline].bUse[i] = (temp > 0);
3169                 if (opt->groupsel[iline].bUse[i])
3170                 {
3171                     opt->groupsel[iline].nUse++;
3172                 }
3173             }
3174             strcat(fmtign, "%*s");
3175         }
3176         iline++;
3177     }
3178     opt->nGroupsel = iline;
3179     if (nTpr != opt->nGroupsel)
3180     {
3181         gmx_fatal(FARGS, "Found %d tpr files but %d lines in %s\n", nTpr, opt->nGroupsel,
3182                   opt->fnGroupsel);
3183     }
3184
3185     printf("\nUse only these pull groups:\n");
3186     for (iline = 0; iline < nTpr; iline++)
3187     {
3188         printf("%s (%d of %d groups):", fnTpr[iline], opt->groupsel[iline].nUse, opt->groupsel[iline].n);
3189         for (i = 0; i < opt->groupsel[iline].n; i++)
3190         {
3191             if (opt->groupsel[iline].bUse[i])
3192             {
3193                 printf(" %d", i+1);
3194             }
3195         }
3196         printf("\n");
3197     }
3198     printf("\n");
3199
3200     sfree(tmpbuf);
3201 }
3202
3203 //! Boolean XOR
3204 #define WHAMBOOLXOR(a, b) ( ((!(a)) && (b)) || ((a) && (!(b))))
3205
3206 //! Number of elements in fnm (used for command line parsing)
3207 #define NFILE asize(fnm)
3208
3209 //! The main g_wham routine
3210 int gmx_wham(int argc, char *argv[])
3211 {
3212     const char              *desc[] = {
3213         "[THISMODULE] is an analysis program that implements the Weighted",
3214         "Histogram Analysis Method (WHAM). It is intended to analyze",
3215         "output files generated by umbrella sampling simulations to ",
3216         "compute a potential of mean force (PMF).[PAR]",
3217         "",
3218         "[THISMODULE] is currently not fully up to date. It only supports pull setups",
3219         "where the first pull coordinate(s) is/are umbrella pull coordinates",
3220         "and, if multiple coordinates need to be analyzed, all used the same",
3221         "geometry and dimensions. In most cases this is not an issue.[PAR]",
3222         "",
3223         "At present, three input modes are supported.",
3224         "",
3225         "* With option [TT]-it[tt], the user provides a file which contains the",
3226         "  file names of the umbrella simulation run-input files ([REF].tpr[ref] files),",
3227         "  AND, with option [TT]-ix[tt], a file which contains file names of",
3228         "  the pullx [TT]mdrun[tt] output files. The [REF].tpr[ref] and pullx files must",
3229         "  be in corresponding order, i.e. the first [REF].tpr[ref] created the",
3230         "  first pullx, etc.",
3231         "* Same as the previous input mode, except that the the user",
3232         "  provides the pull force output file names ([TT]pullf.xvg[tt]) with option [TT]-if[tt].",
3233         "  From the pull force the position in the umbrella potential is",
3234         "  computed. This does not work with tabulated umbrella potentials.",
3235         "* With option [TT]-ip[tt], the user provides file names of (gzipped) [REF].pdo[ref] files, i.e.",
3236         "  the GROMACS 3.3 umbrella output files. If you have some unusual"
3237         "  reaction coordinate you may also generate your own [REF].pdo[ref] files and",
3238         "  feed them with the [TT]-ip[tt] option into to [THISMODULE]. The [REF].pdo[ref] file header",
3239         "  must be similar to the following::",
3240         "",
3241         "    # UMBRELLA      3.0",
3242         "    # Component selection: 0 0 1",
3243         "    # nSkip 1",
3244         "    # Ref. Group 'TestAtom'",
3245         "    # Nr. of pull groups 2",
3246         "    # Group 1 'GR1'  Umb. Pos. 5.0 Umb. Cons. 1000.0",
3247         "    # Group 2 'GR2'  Umb. Pos. 2.0 Umb. Cons. 500.0",
3248         "    #####",
3249         "",
3250         "  The number of pull groups, umbrella positions, force constants, and names ",
3251         "  may (of course) differ. Following the header, a time column and ",
3252         "  a data column for each pull group follows (i.e. the displacement",
3253         "  with respect to the umbrella center). Up to four pull groups are possible ",
3254         "  per [REF].pdo[ref] file at present.",
3255         "",
3256         "By default, all pull groups found in all pullx/pullf files are used in WHAM. If only ",
3257         "some of the pull groups should be used, a pull group selection file (option [TT]-is[tt]) can ",
3258         "be provided. The selection file must contain one line for each tpr file in tpr-files.dat.",
3259         "Each of these lines must contain one digit (0 or 1) for each pull group in the tpr file. ",
3260         "Here, 1 indicates that the pull group is used in WHAM, and 0 means it is omitted. Example:",
3261         "If you have three tpr files, each containing 4 pull groups, but only pull group 1 and 2 should be ",
3262         "used, groupsel.dat looks like this::",
3263         "",
3264         "    1 1 0 0",
3265         "    1 1 0 0",
3266         "    1 1 0 0",
3267         "",
3268         "By default, the output files are",
3269         "",
3270         "* [TT]-o[tt]      PMF output file",
3271         "* [TT]-hist[tt]   Histograms output file",
3272         "",
3273         "Always check whether the histograms sufficiently overlap.[PAR]",
3274         "The umbrella potential is assumed to be harmonic and the force constants are ",
3275         "read from the [REF].tpr[ref] or [REF].pdo[ref] files. If a non-harmonic umbrella force was applied ",
3276         "a tabulated potential can be provided with [TT]-tab[tt].",
3277         "",
3278         "WHAM options",
3279         "^^^^^^^^^^^^",
3280         "",
3281         "* [TT]-bins[tt]   Number of bins used in analysis",
3282         "* [TT]-temp[tt]   Temperature in the simulations",
3283         "* [TT]-tol[tt]    Stop iteration if profile (probability) changed less than tolerance",
3284         "* [TT]-auto[tt]   Automatic determination of boundaries",
3285         "* [TT]-min,-max[tt]   Boundaries of the profile",
3286         "",
3287         "The data points that are used to compute the profile",
3288         "can be restricted with options [TT]-b[tt], [TT]-e[tt], and [TT]-dt[tt]. ",
3289         "Adjust [TT]-b[tt] to ensure sufficient equilibration in each ",
3290         "umbrella window.[PAR]",
3291         "With [TT]-log[tt] (default) the profile is written in energy units, otherwise ",
3292         "(with [TT]-nolog[tt]) as probability. The unit can be specified with [TT]-unit[tt]. ",
3293         "With energy output, the energy in the first bin is defined to be zero. ",
3294         "If you want the free energy at a different ",
3295         "position to be zero, set [TT]-zprof0[tt] (useful with bootstrapping, see below).[PAR]",
3296         "For cyclic or periodic reaction coordinates (dihedral angle, channel PMF",
3297         "without osmotic gradient), the option [TT]-cycl[tt] is useful.",
3298         "[THISMODULE] will make use of the",
3299         "periodicity of the system and generate a periodic PMF. The first and the last bin of the",
3300         "reaction coordinate will assumed be be neighbors.[PAR]",
3301         "Option [TT]-sym[tt] symmetrizes the profile around z=0 before output, ",
3302         "which may be useful for, e.g. membranes.",
3303         "",
3304         "Parallelization",
3305         "^^^^^^^^^^^^^^^",
3306         "",
3307         "If available, the number of OpenMP threads used by g_wham is controlled with [TT]-nt[tt].",
3308         "",
3309         "Autocorrelations",
3310         "^^^^^^^^^^^^^^^^",
3311         "",
3312         "With [TT]-ac[tt], [THISMODULE] estimates the integrated autocorrelation ",
3313         "time (IACT) [GRK]tau[grk] for each umbrella window and weights the respective ",
3314         "window with 1/[1+2*[GRK]tau[grk]/dt]. The IACTs are written ",
3315         "to the file defined with [TT]-oiact[tt]. In verbose mode, all ",
3316         "autocorrelation functions (ACFs) are written to [TT]hist_autocorr.xvg[tt]. ",
3317         "Because the IACTs can be severely underestimated in case of limited ",
3318         "sampling, option [TT]-acsig[tt] allows one to smooth the IACTs along the ",
3319         "reaction coordinate with a Gaussian ([GRK]sigma[grk] provided with [TT]-acsig[tt], ",
3320         "see output in [TT]iact.xvg[tt]). Note that the IACTs are estimated by simple ",
3321         "integration of the ACFs while the ACFs are larger 0.05.",
3322         "If you prefer to compute the IACTs by a more sophisticated (but possibly ",
3323         "less robust) method such as fitting to a double exponential, you can ",
3324         "compute the IACTs with [gmx-analyze] and provide them to [THISMODULE] with the file ",
3325         "[TT]iact-in.dat[tt] (option [TT]-iiact[tt]), which should contain one line per ",
3326         "input file ([REF].pdo[ref] or pullx/f file) and one column per pull group in the respective file.",
3327         "",
3328         "Error analysis",
3329         "^^^^^^^^^^^^^^",
3330         "",
3331         "Statistical errors may be estimated with bootstrap analysis. Use it with care, ",
3332         "otherwise the statistical error may be substantially underestimated. ",
3333         "More background and examples for the bootstrap technique can be found in ",
3334         "Hub, de Groot and Van der Spoel, JCTC (2010) 6: 3713-3720.",
3335         "[TT]-nBootstrap[tt] defines the number of bootstraps (use, e.g., 100). ",
3336         "Four bootstrapping methods are supported and ",
3337         "selected with [TT]-bs-method[tt].",
3338         "",
3339         "* [TT]b-hist[tt]   Default: complete histograms are considered as independent ",
3340         "  data points, and the bootstrap is carried out by assigning random weights to the ",
3341         "  histograms (\"Bayesian bootstrap\"). Note that each point along the reaction coordinate",
3342         "  must be covered by multiple independent histograms (e.g. 10 histograms), otherwise the ",
3343         "  statistical error is underestimated.",
3344         "* [TT]hist[tt]    Complete histograms are considered as independent data points. ",
3345         "  For each bootstrap, N histograms are randomly chosen from the N given histograms ",
3346         "  (allowing duplication, i.e. sampling with replacement).",
3347         "  To avoid gaps without data along the reaction coordinate blocks of histograms ",
3348         "  ([TT]-histbs-block[tt]) may be defined. In that case, the given histograms are ",
3349         "  divided into blocks and only histograms within each block are mixed. Note that ",
3350         "  the histograms within each block must be representative for all possible histograms, ",
3351         "  otherwise the statistical error is underestimated.",
3352         "* [TT]traj[tt]  The given histograms are used to generate new random trajectories,",
3353         "  such that the generated data points are distributed according the given histograms ",
3354         "  and properly autocorrelated. The autocorrelation time (ACT) for each window must be ",
3355         "  known, so use [TT]-ac[tt] or provide the ACT with [TT]-iiact[tt]. If the ACT of all ",
3356         "  windows are identical (and known), you can also provide them with [TT]-bs-tau[tt]. ",
3357         "  Note that this method may severely underestimate the error in case of limited sampling, ",
3358         "  that is if individual histograms do not represent the complete phase space at ",
3359         "  the respective positions.",
3360         "* [TT]traj-gauss[tt]  The same as method [TT]traj[tt], but the trajectories are ",
3361         "  not bootstrapped from the umbrella histograms but from Gaussians with the average ",
3362         "  and width of the umbrella histograms. That method yields similar error estimates ",
3363         "  like method [TT]traj[tt].",
3364         "",
3365         "Bootstrapping output:",
3366         "",
3367         "* [TT]-bsres[tt]   Average profile and standard deviations",
3368         "* [TT]-bsprof[tt]  All bootstrapping profiles",
3369         "",
3370         "With [TT]-vbs[tt] (verbose bootstrapping), the histograms of each bootstrap are written, ",
3371         "and, with bootstrap method [TT]traj[tt], the cumulative distribution functions of ",
3372         "the histograms."
3373     };
3374
3375     const char              *en_unit[]       = {NULL, "kJ", "kCal", "kT", NULL};
3376     const char              *en_unit_label[] = {"", "E (kJ mol\\S-1\\N)", "E (kcal mol\\S-1\\N)", "E (kT)", NULL};
3377     const char              *en_bsMethod[]   = { NULL, "b-hist", "hist", "traj", "traj-gauss", NULL };
3378     static t_UmbrellaOptions opt;
3379     static int               nthreads = -1;
3380
3381     t_pargs                  pa[] = {
3382 #ifdef GMX_OPENMP
3383         { "-nt", FALSE, etINT, {&nthreads},
3384           "Number of threads used by g_wham (if -1, all threads will be used or what is specified by the environment variable OMP_NUM_THREADS)"},
3385 #endif
3386         { "-min", FALSE, etREAL, {&opt.min},
3387           "Minimum coordinate in profile"},
3388         { "-max", FALSE, etREAL, {&opt.max},
3389           "Maximum coordinate in profile"},
3390         { "-auto", FALSE, etBOOL, {&opt.bAuto},
3391           "Determine min and max automatically"},
3392         { "-bins", FALSE, etINT, {&opt.bins},
3393           "Number of bins in profile"},
3394         { "-temp", FALSE, etREAL, {&opt.Temperature},
3395           "Temperature"},
3396         { "-tol", FALSE, etREAL, {&opt.Tolerance},
3397           "Tolerance"},
3398         { "-v", FALSE, etBOOL, {&opt.verbose},
3399           "Verbose mode"},
3400         { "-b", FALSE, etREAL, {&opt.tmin},
3401           "First time to analyse (ps)"},
3402         { "-e", FALSE, etREAL, {&opt.tmax},
3403           "Last time to analyse (ps)"},
3404         { "-dt", FALSE, etREAL, {&opt.dt},
3405           "Analyse only every dt ps"},
3406         { "-histonly", FALSE, etBOOL, {&opt.bHistOnly},
3407           "Write histograms and exit"},
3408         { "-boundsonly", FALSE, etBOOL, {&opt.bBoundsOnly},
3409           "Determine min and max and exit (with [TT]-auto[tt])"},
3410         { "-log", FALSE, etBOOL, {&opt.bLog},
3411           "Calculate the log of the profile before printing"},
3412         { "-unit", FALSE,  etENUM, {en_unit},
3413           "Energy unit in case of log output" },
3414         { "-zprof0", FALSE, etREAL, {&opt.zProf0},
3415           "Define profile to 0.0 at this position (with [TT]-log[tt])"},
3416         { "-cycl", FALSE, etBOOL, {&opt.bCycl},
3417           "Create cyclic/periodic profile. Assumes min and max are the same point."},
3418         { "-sym", FALSE, etBOOL, {&opt.bSym},
3419           "Symmetrize profile around z=0"},
3420         { "-hist-eq", FALSE, etBOOL, {&opt.bHistEq},
3421           "HIDDENEnforce equal weight for all histograms. (Non-Weighed-HAM)"},
3422         { "-ac", FALSE, etBOOL, {&opt.bCalcTauInt},
3423           "Calculate integrated autocorrelation times and use in wham"},
3424         { "-acsig", FALSE, etREAL, {&opt.sigSmoothIact},
3425           "Smooth autocorrelation times along reaction coordinate with Gaussian of this [GRK]sigma[grk]"},
3426         { "-ac-trestart", FALSE, etREAL, {&opt.acTrestart},
3427           "When computing autocorrelation functions, restart computing every .. (ps)"},
3428         { "-acred", FALSE, etBOOL, {&opt.bAllowReduceIact},
3429           "HIDDENWhen smoothing the ACTs, allow to reduce ACTs. Otherwise, only increase ACTs "
3430           "during smoothing"},
3431         { "-nBootstrap", FALSE,  etINT, {&opt.nBootStrap},
3432           "nr of bootstraps to estimate statistical uncertainty (e.g., 200)" },
3433         { "-bs-method", FALSE,  etENUM, {en_bsMethod},
3434           "Bootstrap method" },
3435         { "-bs-tau", FALSE, etREAL, {&opt.tauBootStrap},
3436           "Autocorrelation time (ACT) assumed for all histograms. Use option [TT]-ac[tt] if ACT is unknown."},
3437         { "-bs-seed", FALSE, etINT, {&opt.bsSeed},
3438           "Seed for bootstrapping. (-1 = use time)"},
3439         { "-histbs-block", FALSE, etINT, {&opt.histBootStrapBlockLength},
3440           "When mixing histograms only mix within blocks of [TT]-histbs-block[tt]."},
3441         { "-vbs", FALSE, etBOOL, {&opt.bs_verbose},
3442           "Verbose bootstrapping. Print the CDFs and a histogram file for each bootstrap."},
3443         { "-stepout", FALSE, etINT, {&opt.stepchange},
3444           "HIDDENWrite maximum change every ... (set to 1 with [TT]-v[tt])"},
3445         { "-updateContr", FALSE, etINT, {&opt.stepUpdateContrib},
3446           "HIDDENUpdate table with significan contributions to WHAM every ... iterations"},
3447     };
3448
3449     t_filenm                 fnm[] = {
3450         { efDAT, "-ix", "pullx-files", ffOPTRD}, /* wham input: pullf.xvg's and tprs           */
3451         { efDAT, "-if", "pullf-files", ffOPTRD}, /* wham input: pullf.xvg's and tprs           */
3452         { efDAT, "-it", "tpr-files", ffOPTRD},   /* wham input: tprs                           */
3453         { efDAT, "-ip", "pdo-files", ffOPTRD},   /* wham input: pdo files (gmx3 style)         */
3454         { efDAT, "-is", "groupsel", ffOPTRD},    /* input: select pull groups to use           */
3455         { efXVG, "-o", "profile", ffWRITE },     /* output file for profile                     */
3456         { efXVG, "-hist", "histo", ffWRITE},     /* output file for histograms                  */
3457         { efXVG, "-oiact", "iact", ffOPTWR},     /* writing integrated autocorrelation times    */
3458         { efDAT, "-iiact", "iact-in", ffOPTRD},  /* reading integrated autocorrelation times   */
3459         { efXVG, "-bsres", "bsResult", ffOPTWR}, /* average and errors of bootstrap analysis    */
3460         { efXVG, "-bsprof", "bsProfs", ffOPTWR}, /* output file for bootstrap profiles          */
3461         { efDAT, "-tab", "umb-pot", ffOPTRD},    /* Tabulated umbrella potential (if not harmonic) */
3462     };
3463
3464     int                      i, j, l, nfiles, nwins, nfiles2;
3465     t_UmbrellaHeader         header;
3466     t_UmbrellaWindow       * window = NULL;
3467     double                  *profile, maxchange = 1e20;
3468     gmx_bool                 bMinSet, bMaxSet, bAutoSet, bExact = FALSE;
3469     char                   **fninTpr, **fninPull, **fninPdo;
3470     const char              *fnPull;
3471     FILE                    *histout, *profout;
3472     char                     ylabel[256], title[256];
3473
3474     opt.bins      = 200;
3475     opt.verbose   = FALSE;
3476     opt.bHistOnly = FALSE;
3477     opt.bCycl     = FALSE;
3478     opt.tmin      = 50;
3479     opt.tmax      = 1e20;
3480     opt.dt        = 0.0;
3481     opt.min       = 0;
3482     opt.max       = 0;
3483     opt.bAuto     = TRUE;
3484     opt.nGroupsel = 0;
3485
3486     /* bootstrapping stuff */
3487     opt.nBootStrap               = 0;
3488     opt.bsMethod                 = bsMethod_hist;
3489     opt.tauBootStrap             = 0.0;
3490     opt.bsSeed                   = -1;
3491     opt.histBootStrapBlockLength = 8;
3492     opt.bs_verbose               = FALSE;
3493
3494     opt.bLog                  = TRUE;
3495     opt.unit                  = en_kJ;
3496     opt.zProf0                = 0.;
3497     opt.Temperature           = 298;
3498     opt.Tolerance             = 1e-6;
3499     opt.bBoundsOnly           = FALSE;
3500     opt.bSym                  = FALSE;
3501     opt.bCalcTauInt           = FALSE;
3502     opt.sigSmoothIact         = 0.0;
3503     opt.bAllowReduceIact      = TRUE;
3504     opt.bInitPotByIntegration = TRUE;
3505     opt.acTrestart            = 1.0;
3506     opt.stepchange            = 100;
3507     opt.stepUpdateContrib     = 100;
3508
3509     if (!parse_common_args(&argc, argv, 0,
3510                            NFILE, fnm, asize(pa), pa, asize(desc), desc, 0, NULL, &opt.oenv))
3511     {
3512         return 0;
3513     }
3514
3515     opt.unit     = nenum(en_unit);
3516     opt.bsMethod = nenum(en_bsMethod);
3517
3518     opt.bProf0Set = opt2parg_bSet("-zprof0",  asize(pa), pa);
3519
3520     opt.bTab         = opt2bSet("-tab", NFILE, fnm);
3521     opt.bPdo         = opt2bSet("-ip", NFILE, fnm);
3522     opt.bTpr         = opt2bSet("-it", NFILE, fnm);
3523     opt.bPullx       = opt2bSet("-ix", NFILE, fnm);
3524     opt.bPullf       = opt2bSet("-if", NFILE, fnm);
3525     opt.bTauIntGiven = opt2bSet("-iiact", NFILE, fnm);
3526     if  (opt.bTab && opt.bPullf)
3527     {
3528         gmx_fatal(FARGS, "Force input does not work with tabulated potentials. "
3529                   "Provide pullx.xvg or pdo files!");
3530     }
3531
3532     if (!opt.bPdo && !WHAMBOOLXOR(opt.bPullx, opt.bPullf))
3533     {
3534         gmx_fatal(FARGS, "Give either pullx (-ix) OR pullf (-if) data. Not both.");
3535     }
3536     if (!opt.bPdo && !(opt.bTpr || opt.bPullf || opt.bPullx))
3537     {
3538         gmx_fatal(FARGS, "g_wham supports three input modes, pullx, pullf, or pdo file input."
3539                   "\n\n Check g_wham -h !");
3540     }
3541
3542     opt.fnPdo      = opt2fn("-ip", NFILE, fnm);
3543     opt.fnTpr      = opt2fn("-it", NFILE, fnm);
3544     opt.fnPullf    = opt2fn("-if", NFILE, fnm);
3545     opt.fnPullx    = opt2fn("-ix", NFILE, fnm);
3546     opt.fnGroupsel = opt2fn_null("-is", NFILE, fnm);
3547
3548     bMinSet  = opt2parg_bSet("-min",  asize(pa), pa);
3549     bMaxSet  = opt2parg_bSet("-max",  asize(pa), pa);
3550     bAutoSet = opt2parg_bSet("-auto",  asize(pa), pa);
3551     if ( (bMinSet || bMaxSet) && bAutoSet)
3552     {
3553         gmx_fatal(FARGS, "With -auto, do not give -min or -max\n");
3554     }
3555
3556     if ( (bMinSet && !bMaxSet) || (!bMinSet && bMaxSet))
3557     {
3558         gmx_fatal(FARGS, "When giving -min, you must give -max (and vice versa), too\n");
3559     }
3560
3561     if (bMinSet && opt.bAuto)
3562     {
3563         printf("Note: min and max given, switching off -auto.\n");
3564         opt.bAuto = FALSE;
3565     }
3566
3567     if (opt.bTauIntGiven && opt.bCalcTauInt)
3568     {
3569         gmx_fatal(FARGS, "Either read (option -iiact) or calculate (option -ac) the\n"
3570                   "the autocorrelation times. Not both.");
3571     }
3572
3573     if (opt.tauBootStrap > 0.0 && opt2parg_bSet("-ac", asize(pa), pa))
3574     {
3575         gmx_fatal(FARGS, "Either compute autocorrelation times (ACTs) (option -ac) or "
3576                   "provide it with -bs-tau for bootstrapping. Not Both.\n");
3577     }
3578     if (opt.tauBootStrap > 0.0 && opt2bSet("-iiact", NFILE, fnm))
3579     {
3580         gmx_fatal(FARGS, "Either provide autocorrelation times (ACTs) with file iact-in.dat "
3581                   "(option -iiact) or define all ACTs with -bs-tau for bootstrapping\n. Not Both.");
3582     }
3583
3584     /* Set # of OpenMP threads */
3585     if (nthreads > 0)
3586     {
3587         gmx_omp_set_num_threads(nthreads);
3588     }
3589     else
3590     {
3591         nthreads = gmx_omp_get_max_threads();
3592     }
3593     if (nthreads > 1)
3594     {
3595         printf("\nNote: Will use %d OpenMP threads.\n\n", nthreads);
3596     }
3597
3598     /* Reading gmx4 pull output and tpr files */
3599     if (opt.bTpr || opt.bPullf || opt.bPullx)
3600     {
3601         read_wham_in(opt.fnTpr, &fninTpr, &nfiles, &opt);
3602
3603         fnPull = opt.bPullf ? opt.fnPullf : opt.fnPullx;
3604         read_wham_in(fnPull, &fninPull, &nfiles2, &opt);
3605         printf("Found %d tpr and %d pull %s files in %s and %s, respectively\n",
3606                nfiles, nfiles2, opt.bPullf ? "force" : "position", opt.fnTpr, fnPull);
3607         if (nfiles != nfiles2)
3608         {
3609             gmx_fatal(FARGS, "Found %d file names in %s, but %d in %s\n", nfiles,
3610                       opt.fnTpr, nfiles2, fnPull);
3611         }
3612
3613         /* Read file that selects the pull group to be used */
3614         if (opt.fnGroupsel != NULL)
3615         {
3616             readPullGroupSelection(&opt, fninTpr, nfiles);
3617         }
3618
3619         window = initUmbrellaWindows(nfiles);
3620         read_tpr_pullxf_files(fninTpr, fninPull, nfiles, &header, window, &opt);
3621     }
3622     else
3623     {   /* reading pdo files */
3624         if  (opt.fnGroupsel != NULL)
3625         {
3626             gmx_fatal(FARGS, "Reading a -is file is not supported with PDO input files.\n"
3627                       "Use awk or a similar tool to pick the required pull groups from your PDO files\n");
3628         }
3629         read_wham_in(opt.fnPdo, &fninPdo, &nfiles, &opt);
3630         printf("Found %d pdo files in %s\n", nfiles, opt.fnPdo);
3631         window = initUmbrellaWindows(nfiles);
3632         read_pdo_files(fninPdo, nfiles, &header, window, &opt);
3633     }
3634     nwins = nfiles;
3635
3636     /* enforce equal weight for all histograms? */
3637     if (opt.bHistEq)
3638     {
3639         enforceEqualWeights(window, nwins);
3640     }
3641
3642     /* write histograms */
3643     histout = xvgropen(opt2fn("-hist", NFILE, fnm), "Umbrella histograms",
3644                        xlabel, "count", opt.oenv);
3645     for (l = 0; l < opt.bins; ++l)
3646     {
3647         fprintf(histout, "%e\t", (double)(l+0.5)/opt.bins*(opt.max-opt.min)+opt.min);
3648         for (i = 0; i < nwins; ++i)
3649         {
3650             for (j = 0; j < window[i].nPull; ++j)
3651             {
3652                 fprintf(histout, "%e\t", window[i].Histo[j][l]);
3653             }
3654         }
3655         fprintf(histout, "\n");
3656     }
3657     xvgrclose(histout);
3658     printf("Wrote %s\n", opt2fn("-hist", NFILE, fnm));
3659     if (opt.bHistOnly)
3660     {
3661         printf("Wrote histograms to %s, now exiting.\n", opt2fn("-hist", NFILE, fnm));
3662         return 0;
3663     }
3664
3665     /* Using tabulated umbrella potential */
3666     if (opt.bTab)
3667     {
3668         setup_tab(opt2fn("-tab", NFILE, fnm), &opt);
3669     }
3670
3671     /* Integrated autocorrelation times provided ? */
3672     if (opt.bTauIntGiven)
3673     {
3674         readIntegratedAutocorrelationTimes(window, nwins, opt2fn("-iiact", NFILE, fnm));
3675     }
3676
3677     /* Compute integrated autocorrelation times */
3678     if (opt.bCalcTauInt)
3679     {
3680         calcIntegratedAutocorrelationTimes(window, nwins, &opt, opt2fn("-oiact", NFILE, fnm));
3681     }
3682
3683     /* calc average and sigma for each histogram
3684        (maybe required for bootstrapping. If not, this is fast anyhow) */
3685     if (opt.nBootStrap && opt.bsMethod == bsMethod_trajGauss)
3686     {
3687         averageSigma(window, nwins);
3688     }
3689
3690     /* Get initial potential by simple integration */
3691     if (opt.bInitPotByIntegration)
3692     {
3693         guessPotByIntegration(window, nwins, &opt);
3694     }
3695
3696     /* Check if complete reaction coordinate is covered */
3697     checkReactionCoordinateCovered(window, nwins, &opt);
3698
3699     /* Calculate profile */
3700     snew(profile, opt.bins);
3701     if (opt.verbose)
3702     {
3703         opt.stepchange = 1;
3704     }
3705     i = 0;
3706     do
3707     {
3708         if ( (i%opt.stepUpdateContrib) == 0)
3709         {
3710             setup_acc_wham(profile, window, nwins, &opt);
3711         }
3712         if (maxchange < opt.Tolerance)
3713         {
3714             bExact = TRUE;
3715             /* if (opt.verbose) */
3716             printf("Switched to exact iteration in iteration %d\n", i);
3717         }
3718         calc_profile(profile, window, nwins, &opt, bExact);
3719         if (((i%opt.stepchange) == 0 || i == 1) && i != 0)
3720         {
3721             printf("\t%4d) Maximum change %e\n", i, maxchange);
3722         }
3723         i++;
3724     }
3725     while ( (maxchange = calc_z(profile, window, nwins, &opt, bExact)) > opt.Tolerance || !bExact);
3726     printf("Converged in %d iterations. Final maximum change %g\n", i, maxchange);
3727
3728     /* calc error from Kumar's formula */
3729     /* Unclear how the error propagates along reaction coordinate, therefore
3730        commented out  */
3731     /* calc_error_kumar(profile,window, nwins,&opt); */
3732
3733     /* Write profile in energy units? */
3734     if (opt.bLog)
3735     {
3736         prof_normalization_and_unit(profile, &opt);
3737         strcpy(ylabel, en_unit_label[opt.unit]);
3738         strcpy(title, "Umbrella potential");
3739     }
3740     else
3741     {
3742         strcpy(ylabel, "Density of states");
3743         strcpy(title, "Density of states");
3744     }
3745
3746     /* symmetrize profile around z=0? */
3747     if (opt.bSym)
3748     {
3749         symmetrizeProfile(profile, &opt);
3750     }
3751
3752     /* write profile or density of states */
3753     profout = xvgropen(opt2fn("-o", NFILE, fnm), title, xlabel, ylabel, opt.oenv);
3754     for (i = 0; i < opt.bins; ++i)
3755     {
3756         fprintf(profout, "%e\t%e\n", (double)(i+0.5)/opt.bins*(opt.max-opt.min)+opt.min, profile[i]);
3757     }
3758     xvgrclose(profout);
3759     printf("Wrote %s\n", opt2fn("-o", NFILE, fnm));
3760
3761     /* Bootstrap Method */
3762     if (opt.nBootStrap)
3763     {
3764         do_bootstrapping(opt2fn("-bsres", NFILE, fnm), opt2fn("-bsprof", NFILE, fnm),
3765                          opt2fn("-hist", NFILE, fnm),
3766                          ylabel, profile, window, nwins, &opt);
3767     }
3768
3769     sfree(profile);
3770     freeUmbrellaWindows(window, nfiles);
3771
3772     printf("\nIn case you use results from g_wham for a publication, please cite:\n");
3773     please_cite(stdout, "Hub2010");
3774
3775     return 0;
3776 }