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