Merge release-4-6 into master
[alexxy/gromacs.git] / src / tools / gmx_bar.c
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
2  *
3  * 
4  *                This source code is part of
5  * 
6  *                 G   R   O   M   A   C   S
7  * 
8  *          GROningen MAchine for Chemical Simulations
9  * 
10  *                        VERSION 3.2.0
11  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13  * Copyright (c) 2001-2004, The GROMACS development team,
14  * check out http://www.gromacs.org for more information.
15
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  * 
21  * If you want to redistribute modifications, please consider that
22  * scientific software is very special. Version control is crucial -
23  * bugs must be traceable. We will be happy to consider code for
24  * inclusion in the official distribution, but derived work must not
25  * be called official GROMACS. Details are found in the README & COPYING
26  * files - if they are missing, get the official version at www.gromacs.org.
27  * 
28  * To help us fund GROMACS development, we humbly ask that you cite
29  * the papers on the package - you can find them in the top README file.
30  * 
31  * For more info, check our website at http://www.gromacs.org
32  * 
33  * And Hey:
34  * Green Red Orange Magenta Azure Cyan Skyblue
35  */
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include <math.h>
41 #include <string.h>
42 #include <ctype.h>
43 #include <math.h>
44 #include <float.h>
45
46 #include "sysstuff.h"
47 #include "typedefs.h"
48 #include "smalloc.h"
49 #include "futil.h"
50 #include "statutil.h"
51 #include "copyrite.h"
52 #include "macros.h"
53 #include "enxio.h"
54 #include "physics.h"
55 #include "gmx_fatal.h"
56 #include "xvgr.h"
57 #include "gmx_ana.h"
58 #include "maths.h"
59 #include "string2.h"
60 #include "names.h"
61 #include "mdebin.h"
62
63
64 /* Structure for the names of lambda vector components */
65 typedef struct lambda_components_t
66 {
67     char **names; /* Array of strings with names for the lambda vector
68                      components */
69     int N;              /* The number of components */
70     int Nalloc;         /* The number of allocated components */
71 } lambda_components_t;
72
73 /* Structure for a lambda vector or a dhdl derivative direction */
74 typedef struct lambda_vec_t
75 {
76     double *val;    /* The lambda vector component values. Only valid if
77                        dhdl == -1 */
78     int dhdl;       /* The coordinate index for the derivative described by this
79                        structure, or -1 */
80     const lambda_components_t *lc; /* the associated lambda_components
81                                       structure */
82     int index;      /* The state number (init-lambda-state) of this lambda
83                        vector, if known. If not, it is set to -1 */
84 } lambda_vec_t;
85
86 /* the dhdl.xvg data from a simulation */
87 typedef struct xvg_t
88 {
89     const char   *filename;
90     int    ftp;     /* file type */
91     int    nset;    /* number of lambdas, including dhdl */
92     int *np;        /* number of data points (du or hists) per lambda */
93     int  np_alloc;  /* number of points (du or hists) allocated */
94     double temp;    /* temperature */
95     lambda_vec_t *lambda; /* the lambdas (of first index for y). */
96     double *t;      /* the times (of second index for y) */
97     double **y;     /* the dU values. y[0] holds the derivative, while
98                        further ones contain the energy differences between
99                        the native lambda and the 'foreign' lambdas. */
100     lambda_vec_t native_lambda; /* the native lambda */
101
102     struct xvg_t *next, *prev; /*location in the global linked list of xvg_ts*/
103 } xvg_t;
104
105
106 typedef struct hist_t
107 {
108     unsigned int *bin[2];       /* the (forward + reverse) histogram values */
109     double dx[2];               /* the histogram spacing. The reverse
110                                    dx is the negative of the forward dx.*/
111     gmx_large_int_t x0[2];      /* the (forward + reverse) histogram start 
112                                    point(s) as int */
113
114     int nbin[2];                /* the (forward+reverse) number of bins */
115     gmx_large_int_t sum;        /* the total number of counts. Must be
116                                    the same for forward + reverse.  */
117     int nhist;                  /* number of hist datas (forward or reverse) */
118
119     double start_time, delta_time;  /* start time, end time of histogram */
120 } hist_t;
121
122
123 /* an aggregate of samples for partial free energy calculation */
124 typedef struct samples_t 
125 {    
126     lambda_vec_t *native_lambda;  /* pointer to native lambda vector */
127     lambda_vec_t *foreign_lambda; /* pointer to foreign lambda vector */
128     double temp; /* the temperature */
129     gmx_bool derivative; /* whether this sample is a derivative */
130
131     /* The samples come either as either delta U lists: */
132     int ndu; /* the number of delta U samples */
133     double *du; /* the delta u's */
134     double *t; /* the times associated with those samples, or: */
135     double start_time,delta_time;/*start time and delta time for linear time*/
136
137     /* or as histograms: */
138     hist_t *hist; /* a histogram */
139
140     /* allocation data: (not NULL for data 'owned' by this struct) */
141     double *du_alloc, *t_alloc; /* allocated delta u arrays  */
142     size_t ndu_alloc, nt_alloc; /* pre-allocated sizes */
143     hist_t *hist_alloc; /* allocated hist */
144
145     gmx_large_int_t ntot; /* total number of samples */
146     const char *filename; /* the file name this sample comes from */
147 } samples_t;
148
149 /* a sample range (start to end for du-style data, or boolean 
150     for both du-style data and histograms */
151 typedef struct sample_range_t
152 {
153     int start, end; /* start and end index for du style data */
154     gmx_bool use; /* whether to use this sample */
155
156     samples_t *s; /* the samples this range belongs to */
157 } sample_range_t;
158
159
160 /* a collection of samples for a partial free energy calculation 
161     (i.e. the collection of samples from one native lambda to one 
162     foreign lambda) */
163 typedef struct sample_coll_t
164 {
165     lambda_vec_t *native_lambda;  /* these should be the same for all samples
166                                      in the histogram */
167     lambda_vec_t *foreign_lambda; /* collection */
168     double temp; /* the temperature */
169
170     int nsamples; /* the number of samples */
171     samples_t **s; /* the samples themselves */
172     sample_range_t *r; /* the sample ranges */
173     int nsamples_alloc; /* number of allocated samples */ 
174
175     gmx_large_int_t ntot; /* total number of samples in the ranges of 
176                              this collection */
177
178     struct sample_coll_t *next, *prev; /* next and previous in the list */
179 } sample_coll_t;
180
181 /* all the samples associated with a lambda point */
182 typedef struct lambda_data_t
183 {
184     lambda_vec_t *lambda; /* the native lambda (at start time if dynamic) */
185     double temp; /* temperature */
186
187     sample_coll_t *sc; /* the samples */
188
189     sample_coll_t sc_head; /*the pre-allocated list head for the linked list.*/
190
191     struct lambda_data_t *next, *prev; /* the next and prev in the list */
192 } lambda_data_t;
193
194 /* Top-level data structure of simulation data */
195 typedef struct sim_data_t
196 {
197     lambda_data_t *lb; /* a lambda data linked list */
198     lambda_data_t lb_head; /* The head element of the linked list */
199
200     lambda_components_t lc; /* the allowed components of the lambda
201                                vectors */
202 } sim_data_t;
203
204 /* Top-level data structure with calculated values. */
205 typedef struct {
206     sample_coll_t *a, *b; /* the simulation data */
207
208     double dg; /* the free energy difference */
209     double dg_err; /* the free energy difference */
210
211     double dg_disc_err; /* discretization error */
212     double dg_histrange_err; /* histogram range error */
213
214     double sa; /* relative entropy of b in state a */
215     double sa_err; /* error in sa */
216     double sb; /* relative entropy of a in state b */
217     double sb_err; /* error in sb */
218
219     double dg_stddev; /* expected dg stddev per sample */
220     double dg_stddev_err; /* error in dg_stddev */
221 } barres_t;
222
223
224 /* Initialize a lambda_components structure */
225 static void lambda_components_init(lambda_components_t *lc)
226 {
227     lc->N=0;
228     lc->Nalloc=2;
229     snew(lc->names, lc->Nalloc);
230 }
231
232 /* Add a component to a lambda_components structure */
233 static void lambda_components_add(lambda_components_t *lc,
234                                   const char *name, size_t name_length)
235 {
236     while (lc->N + 1 > lc->Nalloc)
237     {
238         lc->Nalloc = (lc->Nalloc == 0) ? 2 : 2*lc->Nalloc;
239         srealloc( lc->names, lc->Nalloc );
240     }
241     snew(lc->names[lc->N], name_length+1);
242     strncpy(lc->names[lc->N], name, name_length);
243     lc->N++;
244 }
245
246 /* check whether a component with index 'index' matches the given name, or
247    is also NULL. Returns TRUE if this is the case.
248    the string name does not need to end */
249 static gmx_bool lambda_components_check(const lambda_components_t *lc,
250                                         int index,
251                                         const char *name,
252                                         size_t name_length)
253 {
254     size_t len;
255     if (index >= lc->N)
256         return FALSE;
257     if (name == NULL && lc->names[index] == NULL)
258         return TRUE;
259     if ((name == NULL) != (lc->names[index] == NULL))
260         return FALSE;
261     len = strlen(lc->names[index]);
262     if (len != name_length)
263         return FALSE;
264     if (strncmp(lc->names[index], name, name_length)== 0)
265         return TRUE;
266     return FALSE;
267 }
268
269 /* Find the index of a given lambda component name, or -1 if not found */
270 static int lambda_components_find(const lambda_components_t *lc,
271                                   const char *name,
272                                   size_t name_length)
273 {
274     int i;
275
276     for(i=0;i<lc->N;i++)
277     {
278         if (strncmp(lc->names[i], name, name_length) == 0)
279             return i;
280     }
281     return -1;
282 }
283
284
285
286 /* initialize a lambda vector */
287 static void lambda_vec_init(lambda_vec_t *lv, const lambda_components_t *lc)
288 {
289     snew(lv->val, lc->N);
290     lv->index = -1;
291     lv->dhdl = -1;
292     lv->lc = lc;
293 }
294
295 static void lambda_vec_destroy(lambda_vec_t *lv)
296 {
297     sfree(lv->val);
298 }
299
300 static void lambda_vec_copy(lambda_vec_t *lv, const lambda_vec_t *orig)
301 {
302     int i;
303
304     lambda_vec_init(lv, orig->lc);
305     lv->dhdl=orig->dhdl;
306     lv->index=orig->index;
307     for(i=0;i<lv->lc->N;i++)
308     {
309         lv->val[i] = orig->val[i];
310     }
311 }
312
313 /* write a lambda vec to a preallocated string */
314 static void lambda_vec_print(const lambda_vec_t *lv, char *str, gmx_bool named)
315 {
316     int i;
317     size_t np;
318
319     str[0]=0; /* reset the string */
320     if (lv -> dhdl < 0)
321     {
322         if (named)
323         {
324             str += sprintf(str, "delta H to ");
325         }
326         if (lv->lc->N > 1)
327         {
328             str += sprintf(str, "(");
329         }
330         for(i=0;i<lv->lc->N;i++)
331         {
332             str += sprintf(str, "%g", lv->val[i]);
333             if (i < lv->lc->N-1)
334             {
335                 str += sprintf(str, ", ");
336             }
337         }
338         if (lv->lc->N > 1)
339         {
340             str += sprintf(str, ")");
341         }
342     }
343     else
344     {
345         /* this lambda vector describes a derivative */
346         str += sprintf(str, "dH/dl");
347         if (strlen(lv->lc->names[lv->dhdl]) > 0)
348         {
349             str += sprintf(str, " (%s)", lv->lc->names[lv->dhdl]);
350         }
351     }
352 }
353
354 /* write a shortened version of the lambda vec to a preallocated string */
355 static void lambda_vec_print_short(const lambda_vec_t *lv, char *str)
356 {
357     int i;
358     size_t np;
359
360     if (lv->index >= 0)
361     {
362         sprintf(str, "%6d", lv->index);
363     }
364     else
365     {
366         if (lv->dhdl < 0)
367         {
368             sprintf(str, "%6.3f", lv->val[0]);
369         }
370         else
371         {
372             sprintf(str, "dH/dl[%d]", lv->dhdl);
373         }
374     }
375 }
376
377 /* write an intermediate version of two lambda vecs to a preallocated string */
378 static void lambda_vec_print_intermediate(const lambda_vec_t *a,
379                                           const lambda_vec_t *b, char *str)
380 {
381     int i;
382     size_t np;
383
384     str[0]=0;
385     if ( (a->index >= 0) && (b->index>=0) )
386     {
387         sprintf(str, "%6.3f", ((double)a->index+(double)b->index)/2.);
388     }
389     else
390     {
391         if ( (a->dhdl < 0) && (b->dhdl < 0) )
392         {
393             sprintf(str, "%6.3f", (a->val[0]+b->val[0])/2.);
394         }
395     }
396 }
397
398
399
400 /* calculate the difference in lambda vectors: c = a-b.
401    c must be initialized already, and a and b must describe non-derivative
402    lambda points */
403 static void lambda_vec_diff(const lambda_vec_t *a, const lambda_vec_t *b,
404                             lambda_vec_t *c)
405 {
406     int i;
407
408     if ( (a->dhdl > 0)  || (b->dhdl > 0) )
409     {
410         gmx_fatal(FARGS,
411                   "Trying to calculate the difference between derivatives instead of lambda points");
412     }
413     if ((a->lc != b->lc) || (a->lc != c->lc) )
414     {
415         gmx_fatal(FARGS,
416                   "Trying to calculate the difference lambdas with differing basis set");
417     }
418     for(i=0;i<a->lc->N;i++)
419     {
420         c->val[i] = a->val[i] - b->val[i];
421     }
422 }
423
424 /* calculate and return the absolute difference in lambda vectors: c = |a-b|. 
425    a and b must describe non-derivative lambda points */
426 static double lambda_vec_abs_diff(const lambda_vec_t *a, const lambda_vec_t *b)
427 {
428     int i;
429     double ret=0.;
430
431     if ( (a->dhdl > 0)  || (b->dhdl > 0) )
432     {
433         gmx_fatal(FARGS,
434                   "Trying to calculate the difference between derivatives instead of lambda points");
435     }
436     if (a->lc != b->lc) 
437     {
438         gmx_fatal(FARGS,
439                   "Trying to calculate the difference lambdas with differing basis set");
440     }
441     for(i=0;i<a->lc->N;i++)
442     {
443         double df=a->val[i] - b->val[i];
444         ret += df*df;
445     }
446     return sqrt(ret);
447 }
448
449
450 /* check whether two lambda vectors are the same */
451 static gmx_bool lambda_vec_same(const lambda_vec_t *a, const lambda_vec_t *b)
452 {
453     int i;
454
455     if (a->lc != b->lc)
456         return FALSE;
457     if (a->dhdl < 0)
458     {
459         for(i=0;i<a->lc->N;i++)
460         {
461             if (!gmx_within_tol(a->val[i], b->val[i], 10*GMX_REAL_EPS))
462             {
463                 return FALSE;
464             }
465         }
466         return TRUE;
467     }
468     else
469     {
470         /* they're derivatives, so we check whether the indices match */
471         return (a->dhdl == b->dhdl);
472     }
473 }
474
475 /* Compare the sort order of two foreign lambda vectors
476
477     returns 1 if a is 'bigger' than b,
478     returns 0 if they're the same,
479     returns -1 if a is 'smaller' than b.*/
480 static gmx_bool lambda_vec_cmp_foreign(const lambda_vec_t *a,
481                                        const lambda_vec_t *b)
482 {
483     int i;
484     double norm_a=0, norm_b=0;
485     gmx_bool different=FALSE;
486
487     if (a->lc != b->lc)
488     {
489         gmx_fatal(FARGS, "Can't compare lambdas with differing basis sets");
490     }
491     /* if either one has an index we sort based on that */
492     if ((a->index >= 0) || (b->index >= 0))
493     {
494         if (a->index == b->index)
495             return 0;
496         return (a->index > b->index) ? 1 : -1;
497     }
498     if (a->dhdl >= 0 || b->dhdl >= 0)
499     {
500         /* lambda vectors that are derivatives always sort higher than those
501            without derivatives */
502         if ((a->dhdl >= 0)  != (b->dhdl >= 0) )
503         {
504             return (a->dhdl >= 0) ? 1 : -1 ;
505         }
506         return a->dhdl > b->dhdl;
507     }
508
509     /* neither has an index, so we can only sort on the lambda components,
510        which is only valid if there is one component */
511     for(i=0;i<a->lc->N;i++)
512     {
513         if (!gmx_within_tol(a->val[i], b->val[i], 10*GMX_REAL_EPS))
514         {
515             different=TRUE;
516         }
517         norm_a += a->val[i]*a->val[i];
518         norm_b += b->val[i]*b->val[i];
519     }
520     if (!different)
521     {
522         return 0;
523     }
524     return norm_a > norm_b;
525 }
526
527 /* Compare the sort order of two native lambda vectors
528
529     returns 1 if a is 'bigger' than b,
530     returns 0 if they're the same,
531     returns -1 if a is 'smaller' than b.*/
532 static gmx_bool lambda_vec_cmp_native(const lambda_vec_t *a,
533                                       const lambda_vec_t *b)
534 {
535     int i;
536
537     if (a->lc != b->lc)
538     {
539         gmx_fatal(FARGS, "Can't compare lambdas with differing basis sets");
540     }
541     /* if either one has an index we sort based on that */
542     if ((a->index >= 0) || (b->index >= 0))
543     {
544         if (a->index == b->index)
545             return 0;
546         return (a->index > b->index) ? 1 : -1;
547     }
548     /* neither has an index, so we can only sort on the lambda components,
549        which is only valid if there is one component */
550     if (a->lc->N > 1)
551     {
552         gmx_fatal(FARGS,
553                   "Can't compare lambdas with no index and > 1 component");
554     }
555     if (a->dhdl >= 0 || b->dhdl >= 0)
556     {
557         gmx_fatal(FARGS,
558                   "Can't compare native lambdas that are derivatives");
559     }
560     if (gmx_within_tol(a->val[0], b->val[0], 10*GMX_REAL_EPS))
561     {
562         return 0;
563     }
564     return a->val[0] > b->val[0] ? 1 : -1;
565 }
566
567
568
569
570 static void hist_init(hist_t *h, int nhist, int *nbin)
571 {
572     int i;
573     if (nhist>2)
574     {
575         gmx_fatal(FARGS, "histogram with more than two sets of data!");
576     }
577     for(i=0;i<nhist;i++)
578     {
579         snew(h->bin[i], nbin[i]);
580         h->x0[i]=0;
581         h->nbin[i]=nbin[i];
582         h->start_time=h->delta_time=0;
583         h->dx[i]=0;
584     }
585     h->sum=0;
586     h->nhist=nhist;
587 }
588
589 static void hist_destroy(hist_t *h)
590 {
591     sfree(h->bin);
592 }
593
594
595 static void xvg_init(xvg_t *ba)
596 {
597     ba->filename=NULL;
598     ba->nset=0;
599     ba->np_alloc=0;
600     ba->np=NULL;
601     ba->y=NULL;
602 }
603
604 static void samples_init(samples_t *s, lambda_vec_t *native_lambda,
605                          lambda_vec_t *foreign_lambda, double temp,
606                          gmx_bool derivative, const char *filename)
607 {
608     s->native_lambda=native_lambda;
609     s->foreign_lambda=foreign_lambda;
610     s->temp=temp;
611     s->derivative=derivative;
612
613     s->ndu=0;
614     s->du=NULL;
615     s->t=NULL;
616     s->start_time = s->delta_time = 0;
617     s->hist=NULL;
618     s->du_alloc=NULL;
619     s->t_alloc=NULL;
620     s->hist_alloc=NULL;
621     s->ndu_alloc=0;
622     s->nt_alloc=0;
623
624     s->ntot=0;
625     s->filename=filename;
626 }
627
628 static void sample_range_init(sample_range_t *r, samples_t *s)
629 {
630     r->start=0;
631     r->end=s->ndu;
632     r->use=TRUE;
633     r->s=NULL;
634 }
635
636 static void sample_coll_init(sample_coll_t *sc, lambda_vec_t *native_lambda,
637                              lambda_vec_t *foreign_lambda, double temp)
638 {
639     sc->native_lambda = native_lambda;
640     sc->foreign_lambda = foreign_lambda;
641     sc->temp = temp;
642
643     sc->nsamples=0;
644     sc->s=NULL;
645     sc->r=NULL;
646     sc->nsamples_alloc=0;
647
648     sc->ntot=0;
649     sc->next=sc->prev=NULL;
650 }
651
652 static void sample_coll_destroy(sample_coll_t *sc)
653 {
654     /* don't free the samples themselves */
655     sfree(sc->r);
656     sfree(sc->s);
657 }
658
659
660 static void lambda_data_init(lambda_data_t *l, lambda_vec_t *native_lambda,
661                              double temp)
662 {
663     l->lambda=native_lambda;
664     l->temp=temp;
665
666     l->next=NULL;
667     l->prev=NULL;
668
669     l->sc=&(l->sc_head);
670
671     sample_coll_init(l->sc, native_lambda, NULL, 0.);
672     l->sc->next=l->sc;
673     l->sc->prev=l->sc;
674 }
675
676 static void barres_init(barres_t *br)
677 {
678     br->dg=0;
679     br->dg_err=0;
680     br->sa=0;
681     br->sa_err=0;
682     br->sb=0;
683     br->sb_err=0;
684     br->dg_stddev=0;
685     br->dg_stddev_err=0;
686
687     br->a=NULL;
688     br->b=NULL;
689 }
690
691
692 /* calculate the total number of samples in a sample collection */
693 static void sample_coll_calc_ntot(sample_coll_t *sc)
694 {
695     int i;
696
697     sc->ntot=0;
698     for(i=0;i<sc->nsamples;i++)
699     {
700         if (sc->r[i].use)
701         {
702             if (sc->s[i]->hist)
703             {
704                 sc->ntot += sc->s[i]->ntot;
705             }
706             else
707             {
708                 sc->ntot += sc->r[i].end - sc->r[i].start;
709             }
710         }
711     }
712 }
713
714
715 /* find the barsamples_t associated with a lambda that corresponds to
716    a specific foreign lambda */
717 static sample_coll_t *lambda_data_find_sample_coll(lambda_data_t *l,
718                                                    lambda_vec_t *foreign_lambda)
719 {
720     sample_coll_t *sc=l->sc->next;
721
722     while(sc != l->sc)
723     {
724         if (lambda_vec_same(sc->foreign_lambda,foreign_lambda))
725         {
726             return sc;
727         }
728         sc=sc->next;
729     }
730
731     return NULL;
732 }
733
734 /* insert li into an ordered list of lambda_colls */
735 static void lambda_data_insert_sample_coll(lambda_data_t *l, sample_coll_t *sc)
736 {
737     sample_coll_t *scn=l->sc->next;
738     while ( (scn!=l->sc) )
739     {
740         if (lambda_vec_cmp_foreign(scn->foreign_lambda, sc->foreign_lambda) > 0)
741             break;
742         scn=scn->next;
743     }
744     /* now insert it before the found scn */
745     sc->next=scn;
746     sc->prev=scn->prev;
747     scn->prev->next=sc;
748     scn->prev=sc;
749 }
750
751 /* insert li into an ordered list of lambdas */
752 static void lambda_data_insert_lambda(lambda_data_t *head, lambda_data_t *li)
753 {
754     lambda_data_t *lc=head->next;
755     while (lc!=head) 
756     {
757         if (lambda_vec_cmp_native(lc->lambda, li->lambda) > 0)
758             break;
759         lc=lc->next;
760     }
761     /* now insert ourselves before the found lc */
762     li->next=lc;
763     li->prev=lc->prev;
764     lc->prev->next=li;
765     lc->prev=li;
766 }
767
768 /* insert a sample and a sample_range into a sample_coll. The
769     samples are stored as a pointer, the range is copied. */
770 static void sample_coll_insert_sample(sample_coll_t *sc, samples_t *s,
771                                       sample_range_t *r)
772 {
773     /* first check if it belongs here */
774     if (sc->temp != s->temp)
775     {
776         gmx_fatal(FARGS, "Temperatures in files %s and %s are not the same!",
777                    s->filename, sc->next->s[0]->filename);
778     }
779     if (!lambda_vec_same(sc->native_lambda, s->native_lambda))
780     {
781         gmx_fatal(FARGS, "Native lambda in files %s and %s are not the same (and they should be)!",
782                    s->filename, sc->next->s[0]->filename);
783     }
784     if (!lambda_vec_same(sc->foreign_lambda,s->foreign_lambda))
785     {
786         gmx_fatal(FARGS, "Foreign lambda in files %s and %s are not the same (and they should be)!",
787                    s->filename, sc->next->s[0]->filename);
788     }
789
790     /* check if there's room */
791     if ( (sc->nsamples + 1) > sc->nsamples_alloc)
792     {
793         sc->nsamples_alloc = max(2*sc->nsamples_alloc, 2);
794         srenew(sc->s, sc->nsamples_alloc);
795         srenew(sc->r, sc->nsamples_alloc);
796     }
797     sc->s[sc->nsamples]=s;
798     sc->r[sc->nsamples]=*r;
799     sc->nsamples++;
800
801     sample_coll_calc_ntot(sc);
802 }
803
804 /* insert a sample into a lambda_list, creating the right sample_coll if 
805    neccesary */
806 static void lambda_data_list_insert_sample(lambda_data_t *head, samples_t *s)
807 {
808     gmx_bool found=FALSE;
809     sample_coll_t *sc;
810     sample_range_t r;
811
812     lambda_data_t *l=head->next;
813
814     /* first search for the right lambda_data_t */
815     while(l != head)
816     {
817         if (lambda_vec_same(l->lambda, s->native_lambda) )
818         {
819             found=TRUE;
820             break;
821         }
822         l=l->next;
823     }
824
825     if (!found)
826     {
827         snew(l, 1); /* allocate a new one */
828         lambda_data_init(l, s->native_lambda, s->temp); /* initialize it */
829         lambda_data_insert_lambda(head, l); /* add it to the list */
830     }
831
832     /* now look for a sample collection */
833     sc=lambda_data_find_sample_coll(l, s->foreign_lambda);
834     if (!sc)
835     {
836         snew(sc, 1); /* allocate a new one */
837         sample_coll_init(sc, s->native_lambda, s->foreign_lambda, s->temp);
838         lambda_data_insert_sample_coll(l, sc);
839     }
840
841     /* now insert the samples into the sample coll */
842     sample_range_init(&r, s);
843     sample_coll_insert_sample(sc, s, &r);
844 }
845
846
847 /* make a histogram out of a sample collection */
848 static void sample_coll_make_hist(sample_coll_t *sc, int **bin, 
849                                   int *nbin_alloc, int *nbin, 
850                                   double *dx, double *xmin, int nbin_default)
851 {
852     int i,j,k;
853     gmx_bool dx_set=FALSE;
854     gmx_bool xmin_set=FALSE;
855
856     gmx_bool xmax_set=FALSE;
857     gmx_bool xmax_set_hard=FALSE; /* whether the xmax is bounded by the 
858                                      limits of a histogram */
859     double xmax=-1;
860
861     /* first determine dx and xmin; try the histograms */
862     for(i=0;i<sc->nsamples;i++)
863     {
864         if (sc->s[i]->hist)
865         {
866             hist_t *hist=sc->s[i]->hist;
867             for(k=0;k<hist->nhist;k++)
868             {
869                 double hdx=hist->dx[k];
870                 double xmax_now=(hist->x0[k]+hist->nbin[k])*hdx;
871
872                 /* we use the biggest dx*/
873                 if ( (!dx_set) || hist->dx[0] > *dx)
874                 {
875                     dx_set=TRUE;
876                     *dx = hist->dx[0];
877                 }
878                 if ( (!xmin_set) || (hist->x0[k]*hdx) < *xmin)
879                 {
880                     xmin_set=TRUE;
881                     *xmin = (hist->x0[k]*hdx);
882                 }
883
884                 if ( (!xmax_set) || (xmax_now>xmax && !xmax_set_hard) )
885                 {
886                     xmax_set=TRUE;
887                     xmax = xmax_now;
888                     if (hist->bin[k][hist->nbin[k]-1] != 0)
889                         xmax_set_hard=TRUE;
890                 }
891                 if ( hist->bin[k][hist->nbin[k]-1]!=0 && (xmax_now < xmax) )
892                 {
893                     xmax_set_hard=TRUE;
894                     xmax = xmax_now;
895                 }
896             }
897         }
898     }
899     /* and the delta us */
900     for(i=0;i<sc->nsamples;i++)
901     {
902         if (sc->s[i]->ndu > 0)
903         {
904             /* determine min and max */
905             int starti=sc->r[i].start;
906             int endi=sc->r[i].end;
907             double du_xmin=sc->s[i]->du[starti]; 
908             double du_xmax=sc->s[i]->du[starti];
909             for(j=starti+1;j<endi;j++)
910             {
911                 if (sc->s[i]->du[j] < du_xmin)
912                     du_xmin = sc->s[i]->du[j];
913                 if (sc->s[i]->du[j] > du_xmax)
914                     du_xmax = sc->s[i]->du[j];
915             }
916
917             /* and now change the limits */
918             if ( (!xmin_set) || (du_xmin < *xmin) )
919             {
920                 xmin_set=TRUE;
921                 *xmin=du_xmin;
922             }
923             if ( (!xmax_set) || ((du_xmax > xmax) &&  !xmax_set_hard) )
924             {
925                 xmax_set=TRUE;
926                 xmax=du_xmax;
927             }
928         }
929     }
930
931     if (!xmax_set || !xmin_set)
932     {
933         *nbin=0;
934         return;
935     }
936
937
938     if (!dx_set)
939     {
940         *nbin=nbin_default;
941         *dx=(xmax-(*xmin))/((*nbin)-2); /* -2 because we want the last bin to
942                                            be 0, and we count from 0 */
943     }
944     else
945     {
946         *nbin=(xmax-(*xmin))/(*dx);
947     }
948
949     if (*nbin > *nbin_alloc)
950     {
951         *nbin_alloc=*nbin;
952         srenew(*bin, *nbin_alloc);
953     }
954
955     /* reset the histogram */
956     for(i=0;i<(*nbin);i++)
957     {
958         (*bin)[i] = 0;
959     }
960
961     /* now add the actual data */   
962     for(i=0;i<sc->nsamples;i++)
963     {
964         if (sc->s[i]->hist)
965         {
966             hist_t *hist=sc->s[i]->hist;
967             for(k=0;k<hist->nhist;k++)
968             {
969                 double hdx = hist->dx[k];
970                 double xmin_hist=hist->x0[k]*hdx;
971                 for(j=0;j<hist->nbin[k];j++)
972                 {
973                     /* calculate the bin corresponding to the middle of the 
974                        original bin */
975                     double x=hdx*(j+0.5) + xmin_hist;
976                     int binnr=(int)((x-(*xmin))/(*dx));
977
978                     if (binnr >= *nbin || binnr<0)
979                         binnr = (*nbin)-1;
980
981                     (*bin)[binnr] += hist->bin[k][j]; 
982                 }
983             }
984         }
985         else
986         {
987             int starti=sc->r[i].start;
988             int endi=sc->r[i].end;
989             for(j=starti;j<endi;j++)
990             {
991                 int binnr=(int)((sc->s[i]->du[j]-(*xmin))/(*dx));
992                 if (binnr >= *nbin || binnr<0)
993                     binnr = (*nbin)-1;
994
995                 (*bin)[binnr] ++;
996             }
997         }
998     }
999 }
1000
1001 /* write a collection of histograms to a file */
1002 void sim_data_histogram(sim_data_t *sd, const char *filename,
1003                         int nbin_default, const output_env_t oenv)
1004 {
1005     char label_x[STRLEN];
1006     const char *dhdl="dH/d\\lambda",*deltag="\\DeltaH",*lambda="\\lambda";
1007     const char *title="N(\\DeltaH)";
1008     const char *label_y="Samples";
1009     FILE *fp;
1010     lambda_data_t *bl;
1011     int nsets=0;
1012     char **setnames=NULL;
1013     gmx_bool first_set=FALSE;
1014     /* histogram data: */
1015     int *hist=NULL;
1016     int nbin=0;
1017     int nbin_alloc=0;
1018     double dx=0;
1019     double min=0;
1020     int i;
1021     lambda_data_t *bl_head = sd->lb;
1022
1023     printf("\nWriting histogram to %s\n", filename);
1024     sprintf(label_x, "\\DeltaH (%s)", unit_energy);
1025
1026     fp=xvgropen_type(filename, title, label_x, label_y, exvggtXNY, oenv);
1027
1028     /* first get all the set names */
1029     bl=bl_head->next;
1030     /* iterate over all lambdas */
1031     while(bl!=bl_head)
1032     {
1033         sample_coll_t *sc=bl->sc->next;
1034
1035         /* iterate over all samples */
1036         while(sc!=bl->sc)
1037         {
1038             char buf[STRLEN], buf2[STRLEN];
1039
1040             nsets++;
1041             srenew(setnames, nsets); 
1042             snew(setnames[nsets-1], STRLEN);
1043             if (sc->foreign_lambda->dhdl < 0)
1044             {
1045                 lambda_vec_print(sc->native_lambda, buf, FALSE);
1046                 lambda_vec_print(sc->foreign_lambda, buf2, FALSE);
1047                 sprintf(setnames[nsets-1], "N(%s(%s=%s) | %s=%s)",
1048                         deltag, lambda, buf2, lambda, buf);
1049             }
1050             else
1051             {
1052                 lambda_vec_print(sc->native_lambda, buf, FALSE);
1053                 sprintf(setnames[nsets-1], "N(%s | %s=%s)",
1054                         dhdl, lambda, buf);
1055             }
1056             sc=sc->next;
1057         }
1058
1059         bl=bl->next;
1060     }
1061     xvgr_legend(fp, nsets, (const char**)setnames, oenv);
1062
1063
1064     /* now make the histograms */
1065     bl=bl_head->next;
1066     /* iterate over all lambdas */
1067     while(bl!=bl_head)
1068     {
1069         sample_coll_t *sc=bl->sc->next;
1070
1071         /* iterate over all samples */
1072         while(sc!=bl->sc)
1073         {
1074             if (!first_set)
1075             {
1076                 xvgr_new_dataset(fp, 0, 0, NULL, oenv);
1077             }
1078     
1079             sample_coll_make_hist(sc, &hist, &nbin_alloc, &nbin, &dx, &min,
1080                                   nbin_default);
1081
1082             for(i=0;i<nbin;i++)
1083             {
1084                 double xmin=i*dx + min;
1085                 double xmax=(i+1)*dx + min;
1086
1087                 fprintf(fp, "%g %d\n%g %d\n", xmin, hist[i], xmax, hist[i]);
1088             }
1089
1090             first_set=FALSE;
1091             sc=sc->next;
1092         }
1093
1094         bl=bl->next;
1095     }
1096
1097     if(hist)
1098         sfree(hist);    
1099
1100     xvgrclose(fp); 
1101 }
1102
1103 /* create a collection (array) of barres_t object given a ordered linked list 
1104    of barlamda_t sample collections */
1105 static barres_t *barres_list_create(sim_data_t *sd, int *nres,
1106                                     gmx_bool use_dhdl)
1107 {
1108     lambda_data_t *bl;
1109     int nlambda=0;
1110     barres_t *res;
1111     int i;
1112     gmx_bool dhdl=FALSE;
1113     gmx_bool first=TRUE;
1114     lambda_data_t *bl_head=sd->lb;
1115
1116     /* first count the lambdas */
1117     bl=bl_head->next;
1118     while(bl!=bl_head)
1119     {
1120         nlambda++;    
1121         bl=bl->next;
1122     }
1123     snew(res, nlambda-1);
1124
1125     /* next put the right samples in the res */
1126     *nres=0;
1127     bl=bl_head->next->next; /* we start with the second one. */
1128     while(bl!=bl_head)
1129     {
1130         sample_coll_t *sc, *scprev;
1131         barres_t *br=&(res[*nres]);
1132         /* there is always a previous one. we search for that as a foreign 
1133            lambda: */
1134         scprev=lambda_data_find_sample_coll(bl->prev, bl->lambda);
1135         sc=lambda_data_find_sample_coll(bl, bl->prev->lambda);
1136
1137         barres_init(br);
1138
1139         if (use_dhdl)
1140         {
1141             /* we use dhdl */
1142
1143             scprev=lambda_data_find_sample_coll(bl->prev, bl->prev->lambda);
1144             sc=lambda_data_find_sample_coll(bl, bl->lambda);
1145
1146             if (first)
1147             {
1148                 printf("\nWARNING: Using the derivative data (dH/dlambda) to extrapolate delta H values.\nThis will only work if the Hamiltonian is linear in lambda.\n");
1149                 dhdl=TRUE;
1150             }
1151             if (!dhdl)
1152             {
1153                 gmx_fatal(FARGS,"Some dhdl files contain only one value (dH/dl), while others \ncontain multiple values (dH/dl and/or Delta H), will not proceed \nbecause of possible inconsistencies.\n");
1154             }
1155         } 
1156         else if (!scprev && !sc)
1157         {
1158             gmx_fatal(FARGS,"There is no path from lambda=%g -> %g that is covered by foreign lambdas:\ncannot proceed with BAR.\nUse thermodynamic integration of dH/dl by calculating the averages of dH/dl\nwith g_analyze and integrating them.\nAlternatively, use the -extp option if (and only if) the Hamiltonian\ndepends linearly on lambda, which is NOT normally the case.\n", bl->prev->lambda, bl->lambda);
1159         }
1160         
1161         /* normal delta H */
1162         if (!scprev)
1163         {
1164             gmx_fatal(FARGS,"Could not find a set for foreign lambda = %g\nin the files for lambda = %g",bl->lambda,bl->prev->lambda);
1165         }
1166         if (!sc)
1167         {
1168             gmx_fatal(FARGS,"Could not find a set for foreign lambda = %g\nin the files for lambda = %g",bl->prev->lambda,bl->lambda);
1169         }
1170         br->a = scprev;
1171         br->b = sc;
1172
1173         first=FALSE;
1174         (*nres)++;
1175         bl=bl->next;
1176     }
1177     return res;
1178 }
1179
1180 /* estimate the maximum discretization error */
1181 static double barres_list_max_disc_err(barres_t *res, int nres)
1182 {
1183     int i,j;
1184     double disc_err=0.;
1185     double delta_lambda;
1186
1187     for(i=0;i<nres;i++)
1188     {
1189         barres_t *br=&(res[i]);
1190
1191         delta_lambda=lambda_vec_abs_diff(br->b->native_lambda,
1192                                          br->a->native_lambda);
1193
1194         for(j=0;j<br->a->nsamples;j++)
1195         {
1196             if (br->a->s[j]->hist)
1197             {
1198                 double Wfac=1.;
1199                 if (br->a->s[j]->derivative)
1200                     Wfac =  delta_lambda;
1201
1202                 disc_err=max(disc_err, Wfac*br->a->s[j]->hist->dx[0]);
1203             }
1204         }
1205         for(j=0;j<br->b->nsamples;j++)
1206         {
1207             if (br->b->s[j]->hist)
1208             {
1209                 double Wfac=1.;
1210                 if (br->b->s[j]->derivative)
1211                     Wfac =  delta_lambda;
1212                 disc_err=max(disc_err, Wfac*br->b->s[j]->hist->dx[0]);
1213             }
1214         }
1215     } 
1216     return disc_err;
1217 }
1218
1219
1220 /* impose start and end times on a sample collection, updating sample_ranges */
1221 static void sample_coll_impose_times(sample_coll_t *sc, double begin_t, 
1222                                      double end_t)
1223 {
1224     int i;
1225     for(i=0;i<sc->nsamples;i++)
1226     {
1227         samples_t *s=sc->s[i];
1228         sample_range_t *r=&(sc->r[i]);
1229         if (s->hist)
1230         {
1231             double end_time=s->hist->delta_time*s->hist->sum + 
1232                             s->hist->start_time;
1233             if (s->hist->start_time < begin_t || end_time > end_t)
1234             {
1235                 r->use=FALSE;
1236             }
1237         }
1238         else
1239         {
1240             if (!s->t)
1241             {
1242                 double end_time;
1243                 if (s->start_time < begin_t)
1244                 {
1245                     r->start=(int)((begin_t - s->start_time)/s->delta_time);
1246                 }
1247                 end_time=s->delta_time*s->ndu + s->start_time;
1248                 if (end_time > end_t)
1249                 {
1250                     r->end=(int)((end_t - s->start_time)/s->delta_time);
1251                 }
1252             }
1253             else
1254             {
1255                 int j;
1256                 for(j=0;j<s->ndu;j++)
1257                 {
1258                     if (s->t[j] <begin_t)
1259                     {
1260                         r->start = j;
1261                     }
1262
1263                     if (s->t[j] >= end_t)
1264                     {
1265                         r->end = j;
1266                         break;
1267                     }
1268                 }
1269             }
1270             if (r->start > r->end)
1271             {
1272                 r->use=FALSE;
1273             }
1274         }
1275     }
1276     sample_coll_calc_ntot(sc);
1277 }
1278
1279 static void sim_data_impose_times(sim_data_t *sd, double begin, double end)
1280 {
1281     double first_t, last_t;
1282     double begin_t, end_t;
1283     lambda_data_t *lc;
1284     lambda_data_t *head=sd->lb;
1285     int j;
1286
1287     if (begin<=0 && end<0) 
1288     {
1289         return;
1290     }
1291
1292     /* first determine the global start and end times */
1293     first_t = -1;
1294     last_t = -1;
1295     lc=head->next;
1296     while(lc!=head)
1297     {
1298         sample_coll_t *sc=lc->sc->next;
1299         while(sc != lc->sc)
1300         {
1301             for(j=0;j<sc->nsamples;j++)
1302             {
1303                 double start_t,end_t;
1304
1305                 start_t = sc->s[j]->start_time;
1306                 end_t =   sc->s[j]->start_time;
1307                 if (sc->s[j]->hist)
1308                 {
1309                     end_t += sc->s[j]->delta_time*sc->s[j]->hist->sum;
1310                 }
1311                 else
1312                 {
1313                     if (sc->s[j]->t)
1314                     {
1315                         end_t = sc->s[j]->t[sc->s[j]->ndu-1];
1316                     }
1317                     else
1318                     {
1319                         end_t += sc->s[j]->delta_time*sc->s[j]->ndu;
1320                     }
1321                 }
1322
1323                 if (start_t < first_t || first_t<0)
1324                 {
1325                     first_t=start_t;
1326                 }
1327                 if (end_t > last_t)
1328                 {
1329                     last_t=end_t;
1330                 }
1331             }
1332             sc=sc->next;
1333         }
1334         lc=lc->next;
1335     }
1336
1337     /* calculate the actual times */
1338     if (begin > 0 )
1339     {
1340         begin_t = begin;
1341     }
1342     else
1343     {
1344         begin_t = first_t;
1345     }
1346
1347     if (end >0 )
1348     {
1349         end_t = end;
1350     }
1351     else
1352     {
1353         end_t = last_t;
1354     }
1355     printf("\n   Samples in time interval: %.3f - %.3f\n", first_t, last_t);
1356
1357     if (begin_t > end_t)
1358     {
1359         return;
1360     }
1361     printf("Removing samples outside of: %.3f - %.3f\n", begin_t, end_t);
1362
1363     /* then impose them */
1364     lc=head->next;
1365     while(lc!=head)
1366     {
1367         sample_coll_t *sc=lc->sc->next;
1368         while(sc != lc->sc)
1369         {
1370             sample_coll_impose_times(sc, begin_t, end_t);
1371             sc=sc->next;
1372         }
1373         lc=lc->next;
1374     }
1375 }
1376
1377
1378 /* create subsample i out of ni from an existing sample_coll */
1379 static gmx_bool sample_coll_create_subsample(sample_coll_t  *sc, 
1380                                              sample_coll_t *sc_orig, 
1381                                              int i, int ni)
1382 {
1383     int j;
1384     int hist_start, hist_end;
1385
1386     gmx_large_int_t ntot_start;
1387     gmx_large_int_t ntot_end;
1388     gmx_large_int_t ntot_so_far;
1389
1390     *sc = *sc_orig; /* just copy all fields */
1391
1392     /* allocate proprietary memory */
1393     snew(sc->s, sc_orig->nsamples);
1394     snew(sc->r, sc_orig->nsamples);
1395
1396     /* copy the samples */
1397     for(j=0;j<sc_orig->nsamples;j++)
1398     {
1399         sc->s[j] = sc_orig->s[j];
1400         sc->r[j] = sc_orig->r[j]; /* copy the ranges too */
1401     }
1402     
1403     /* now fix start and end fields */
1404     /* the casts avoid possible overflows */
1405     ntot_start=(gmx_large_int_t)(sc_orig->ntot*(double)i/(double)ni);
1406     ntot_end  =(gmx_large_int_t)(sc_orig->ntot*(double)(i+1)/(double)ni);
1407     ntot_so_far = 0;
1408     for(j=0;j<sc->nsamples;j++)
1409     {
1410         gmx_large_int_t ntot_add;
1411         gmx_large_int_t new_start, new_end;
1412
1413         if (sc->r[j].use)
1414         {
1415             if (sc->s[j]->hist)
1416             {
1417                 ntot_add = sc->s[j]->hist->sum;
1418             }
1419             else 
1420             {
1421                 ntot_add = sc->r[j].end - sc->r[j].start;
1422             }
1423         }
1424         else
1425         {
1426             ntot_add = 0;
1427         }
1428
1429         if (!sc->s[j]->hist)
1430         { 
1431             if (ntot_so_far < ntot_start)
1432             {
1433                 /* adjust starting point */
1434                 new_start = sc->r[j].start + (ntot_start - ntot_so_far);
1435             }
1436             else
1437             {
1438                 new_start = sc->r[j].start;
1439             }
1440             /* adjust end point */
1441             new_end = sc->r[j].start + (ntot_end - ntot_so_far);
1442             if (new_end > sc->r[j].end)
1443             {
1444                 new_end=sc->r[j].end;
1445             }
1446
1447             /* check if we're in range at all */
1448             if ( (new_end < new_start) || (new_start > sc->r[j].end) )
1449             {
1450                 new_start=0;
1451                 new_end=0;
1452             }
1453             /* and write the new range */
1454             sc->r[j].start=(int)new_start;
1455             sc->r[j].end=(int)new_end;
1456         }
1457         else
1458         {
1459             if (sc->r[j].use)
1460             {
1461                 double overlap;
1462                 double ntot_start_norm, ntot_end_norm;
1463                 /* calculate the amount of overlap of the 
1464                    desired range (ntot_start -- ntot_end) onto
1465                    the histogram range (ntot_so_far -- ntot_so_far+ntot_add)*/
1466
1467                 /* first calculate normalized bounds 
1468                    (where 0 is the start of the hist range, and 1 the end) */
1469                 ntot_start_norm = (ntot_start-ntot_so_far)/(double)ntot_add;
1470                 ntot_end_norm = (ntot_end-ntot_so_far)/(double)ntot_add;
1471
1472                 /* now fix the boundaries */
1473                 ntot_start_norm = min(1, max(0., ntot_start_norm));
1474                 ntot_end_norm = max(0, min(1., ntot_end_norm));
1475
1476                 /* and calculate the overlap */
1477                 overlap = ntot_end_norm - ntot_start_norm;
1478
1479                 if (overlap > 0.95) /* we allow for 5% slack */
1480                 {
1481                     sc->r[j].use = TRUE;
1482                 }
1483                 else if (overlap < 0.05)
1484                 {
1485                     sc->r[j].use = FALSE;
1486                 }
1487                 else
1488                 {
1489                     return FALSE;
1490                 }
1491             }
1492         }
1493         ntot_so_far += ntot_add;
1494     }
1495     sample_coll_calc_ntot(sc);
1496
1497     return TRUE;
1498 }
1499
1500 /* calculate minimum and maximum work values in sample collection */
1501 static void sample_coll_min_max(sample_coll_t *sc, double Wfac,
1502                                 double *Wmin, double *Wmax)
1503 {
1504     int i,j;
1505
1506     *Wmin=FLT_MAX;
1507     *Wmax=-FLT_MAX;
1508
1509     for(i=0;i<sc->nsamples;i++)
1510     {
1511         samples_t *s=sc->s[i];
1512         sample_range_t *r=&(sc->r[i]);
1513         if (r->use)
1514         {
1515             if (!s->hist)
1516             {
1517                 for(j=r->start; j<r->end; j++)
1518                 {
1519                     *Wmin = min(*Wmin,s->du[j]*Wfac);
1520                     *Wmax = max(*Wmax,s->du[j]*Wfac);
1521                 }
1522             }
1523             else
1524             {
1525                 int hd=0; /* determine the histogram direction: */
1526                 double dx;
1527                 if ( (s->hist->nhist>1) && (Wfac<0) )
1528                 {
1529                     hd=1;
1530                 }
1531                 dx=s->hist->dx[hd];
1532
1533                 for(j=s->hist->nbin[hd]-1;j>=0;j--)
1534                 {
1535                     *Wmin=min(*Wmin,Wfac*(s->hist->x0[hd])*dx);
1536                     *Wmax=max(*Wmax,Wfac*(s->hist->x0[hd])*dx);
1537                     /* look for the highest value bin with values */
1538                     if (s->hist->bin[hd][j]>0)
1539                     {
1540                         *Wmin=min(*Wmin,Wfac*(j+s->hist->x0[hd]+1)*dx);
1541                         *Wmax=max(*Wmax,Wfac*(j+s->hist->x0[hd]+1)*dx);
1542                         break;
1543                     }
1544                 }
1545             }
1546         }
1547     }
1548 }
1549
1550 /* Initialize a sim_data structure */
1551 static void sim_data_init(sim_data_t *sd)
1552 {
1553     /* make linked list */
1554     sd->lb = &(sd->lb_head);
1555     sd->lb->next = sd->lb;
1556     sd->lb->prev = sd->lb;
1557
1558     lambda_components_init(&(sd->lc));
1559 }
1560
1561
1562 static double calc_bar_sum(int n,const double *W,double Wfac,double sbMmDG)
1563 {
1564     int    i;
1565     double sum;
1566     
1567     sum = 0;
1568     
1569     for(i=0; i<n; i++)
1570     {
1571         sum += 1./(1. + exp(Wfac*W[i] + sbMmDG));
1572     }
1573     
1574     return sum;
1575 }
1576
1577 /* calculate the BAR average given a histogram 
1578
1579     if type== 0, calculate the best estimate for the average,
1580     if type==-1, calculate the minimum possible value given the histogram 
1581     if type== 1, calculate the maximum possible value given the histogram */
1582 static double calc_bar_sum_hist(const hist_t *hist, double Wfac, double sbMmDG,
1583                                 int type)
1584 {
1585     double sum=0.;
1586     int i;
1587     int max; 
1588     /* normalization factor multiplied with bin width and
1589        number of samples (we normalize through M): */
1590     double normdx = 1.;
1591     int hd=0; /* determine the histogram direction: */
1592     double dx;
1593
1594     if ( (hist->nhist>1) && (Wfac<0) )
1595     {
1596         hd=1;
1597     }
1598     dx=hist->dx[hd];
1599     max=hist->nbin[hd]-1;
1600     if (type==1) 
1601     {
1602         max=hist->nbin[hd]; /* we also add whatever was out of range */
1603     }
1604
1605     for(i=0;i<max;i++)
1606     {
1607         double x=Wfac*((i+hist->x0[hd])+0.5)*dx; /* bin middle */
1608         double pxdx=hist->bin[0][i]*normdx; /* p(x)dx */
1609    
1610         sum += pxdx/(1. + exp(x + sbMmDG));
1611     }
1612
1613     return sum;
1614 }
1615
1616 static double calc_bar_lowlevel(sample_coll_t *ca, sample_coll_t *cb,
1617                                 double temp, double tol, int type)
1618 {
1619     double kT,beta,M;
1620     double DG;
1621     int i,j;
1622     double Wfac1,Wfac2,Wmin,Wmax;
1623     double DG0,DG1,DG2,dDG1;
1624     double sum1,sum2;
1625     double n1, n2; /* numbers of samples as doubles */
1626     
1627     kT   = BOLTZ*temp;
1628     beta = 1/kT;
1629   
1630     /* count the numbers of samples */ 
1631     n1 = ca->ntot;
1632     n2 = cb->ntot;
1633
1634     M = log(n1/n2);
1635
1636     /*if (!lambda_vec_same(ca->native_lambda, ca->foreign_lambda))*/
1637     if (ca->foreign_lambda->dhdl<0)
1638     {
1639         /* this is the case when the delta U were calculated directly
1640            (i.e. we're not scaling dhdl) */
1641         Wfac1 = beta;
1642         Wfac2 = beta;
1643     }
1644     else
1645     {
1646         /* we're using dhdl, so delta_lambda needs to be a 
1647            multiplication factor.  */
1648         /*double delta_lambda=cb->native_lambda-ca->native_lambda;*/
1649         double delta_lambda=lambda_vec_abs_diff(cb->native_lambda,
1650                                                 ca->native_lambda);
1651         if (cb->native_lambda->lc->N > 1)
1652         {
1653             gmx_fatal(FARGS,
1654                       "Can't (yet) do multi-component dhdl interpolation");
1655         }
1656
1657         Wfac1 =  beta*delta_lambda;
1658         Wfac2 = -beta*delta_lambda;
1659     }
1660
1661     if (beta < 1)
1662     {
1663         /* We print the output both in kT and kJ/mol.
1664          * Here we determine DG in kT, so when beta < 1
1665          * the precision has to be increased.
1666          */
1667         tol *= beta;
1668     }
1669
1670     /* Calculate minimum and maximum work to give an initial estimate of 
1671      * delta G  as their average.
1672      */
1673     {
1674         double Wmin1, Wmin2, Wmax1, Wmax2;
1675         sample_coll_min_max(ca, Wfac1, &Wmin1, &Wmax1);
1676         sample_coll_min_max(cb, Wfac2, &Wmin2, &Wmax2);
1677
1678         Wmin=min(Wmin1, Wmin2);
1679         Wmax=max(Wmax1, Wmax2);
1680     }
1681
1682     DG0 = Wmin;
1683     DG2 = Wmax;
1684     
1685     if (debug)
1686     {
1687         fprintf(debug,"DG %9.5f %9.5f\n",DG0,DG2);
1688     }
1689     /* We approximate by bisection: given our initial estimates
1690        we keep checking whether the halfway point is greater or
1691        smaller than what we get out of the BAR averages.
1692
1693        For the comparison we can use twice the tolerance. */
1694     while (DG2 - DG0 > 2*tol)
1695     {
1696         DG1 = 0.5*(DG0 + DG2);
1697
1698         /*printf("Wfac1=%g, Wfac2=%g, beta=%g, DG1=%g\n",Wfac1,Wfac2,beta,
1699           DG1);*/
1700
1701         /* calculate the BAR averages */
1702         dDG1=0.;
1703
1704         for(i=0;i<ca->nsamples;i++)
1705         {
1706             samples_t *s=ca->s[i];
1707             sample_range_t *r=&(ca->r[i]);
1708             if (r->use)
1709             {
1710                 if (s->hist)
1711                 {
1712                     dDG1 += calc_bar_sum_hist(s->hist, Wfac1, (M-DG1), type);
1713                 }
1714                 else
1715                 {
1716                     dDG1 += calc_bar_sum(r->end - r->start, s->du + r->start,
1717                                          Wfac1, (M-DG1));
1718                 }
1719             }
1720         }
1721         for(i=0;i<cb->nsamples;i++)
1722         {
1723             samples_t *s=cb->s[i];
1724             sample_range_t *r=&(cb->r[i]);
1725             if (r->use)
1726             {
1727                 if (s->hist)
1728                 {
1729                     dDG1 -= calc_bar_sum_hist(s->hist, Wfac2, -(M-DG1), type);
1730                 }
1731                 else
1732                 {
1733                     dDG1 -= calc_bar_sum(r->end - r->start, s->du + r->start,
1734                                          Wfac2, -(M-DG1));
1735                 }
1736             }
1737         }
1738        
1739         if (dDG1 < 0)
1740         {
1741             DG0 = DG1;
1742         }
1743         else
1744         {
1745             DG2 = DG1;
1746         }
1747         if (debug)
1748         {
1749             fprintf(debug,"DG %9.5f %9.5f\n",DG0,DG2);
1750         }
1751     }
1752     
1753     return 0.5*(DG0 + DG2);
1754 }
1755
1756 static void calc_rel_entropy(sample_coll_t *ca, sample_coll_t *cb,
1757                              double temp, double dg, double *sa, double *sb)
1758 {
1759     int i,j;
1760     double W_ab=0.;
1761     double W_ba=0.;
1762     double kT, beta;
1763     double Wfac1, Wfac2;
1764     double n1,n2;
1765
1766     kT   = BOLTZ*temp;
1767     beta = 1/kT;
1768
1769     /* count the numbers of samples */ 
1770     n1 = ca->ntot;
1771     n2 = cb->ntot;
1772
1773     /* to ensure the work values are the same as during the delta_G */
1774     /*if (!lambda_vec_same(ca->native_lambda, ca->foreign_lambda))*/
1775     if (ca->foreign_lambda->dhdl<0)
1776     {
1777         /* this is the case when the delta U were calculated directly
1778            (i.e. we're not scaling dhdl) */
1779         Wfac1 = beta;
1780         Wfac2 = beta;
1781     }
1782     else
1783     {
1784         /* we're using dhdl, so delta_lambda needs to be a 
1785            multiplication factor.  */
1786         double delta_lambda=lambda_vec_abs_diff(cb->native_lambda,
1787                                                 ca->native_lambda);
1788         Wfac1 =  beta*delta_lambda;
1789         Wfac2 = -beta*delta_lambda;
1790     }
1791
1792     /* first calculate the average work in both directions */
1793     for(i=0;i<ca->nsamples;i++)
1794     {
1795         samples_t *s=ca->s[i];
1796         sample_range_t *r=&(ca->r[i]);
1797         if (r->use)
1798         {
1799             if (!s->hist)
1800             {
1801                 for(j=r->start;j<r->end;j++)
1802                     W_ab += Wfac1*s->du[j];
1803             }
1804             else
1805             {
1806                 /* normalization factor multiplied with bin width and
1807                    number of samples (we normalize through M): */
1808                 double normdx = 1.;
1809                 double dx;
1810                 int hd=0; /* histogram direction */
1811                 if ( (s->hist->nhist>1) && (Wfac1<0) )
1812                 {
1813                     hd=1;
1814                 }
1815                 dx=s->hist->dx[hd];
1816
1817                 for(j=0;j<s->hist->nbin[0];j++)
1818                 {
1819                     double x=Wfac1*((j+s->hist->x0[0])+0.5)*dx; /*bin ctr*/
1820                     double pxdx=s->hist->bin[0][j]*normdx; /* p(x)dx */
1821                     W_ab += pxdx*x;
1822                 }
1823             }
1824         }
1825     }
1826     W_ab/=n1;
1827
1828     for(i=0;i<cb->nsamples;i++)
1829     {
1830         samples_t *s=cb->s[i];
1831         sample_range_t *r=&(cb->r[i]);
1832         if (r->use)
1833         {
1834             if (!s->hist)
1835             {
1836                 for(j=r->start;j<r->end;j++)
1837                     W_ba += Wfac1*s->du[j];
1838             }
1839             else
1840             {
1841                 /* normalization factor multiplied with bin width and
1842                    number of samples (we normalize through M): */
1843                 double normdx = 1.;
1844                 double dx;
1845                 int hd=0; /* histogram direction */
1846                 if ( (s->hist->nhist>1) && (Wfac2<0) )
1847                 {
1848                     hd=1;
1849                 }
1850                 dx=s->hist->dx[hd];
1851
1852                 for(j=0;j<s->hist->nbin[0];j++)
1853                 {
1854                     double x=Wfac1*((j+s->hist->x0[0])+0.5)*dx;/*bin ctr*/
1855                     double pxdx=s->hist->bin[0][j]*normdx; /* p(x)dx */
1856                     W_ba += pxdx*x;
1857                 }
1858             }
1859         }
1860     }
1861     W_ba/=n2;
1862    
1863     /* then calculate the relative entropies */
1864     *sa = (W_ab - dg);
1865     *sb = (W_ba + dg);
1866 }
1867
1868 static void calc_dg_stddev(sample_coll_t *ca, sample_coll_t *cb,
1869                            double temp, double dg, double *stddev)
1870 {
1871     int i,j;
1872     double M;
1873     double sigmafact=0.;
1874     double kT, beta;
1875     double Wfac1, Wfac2;
1876     double n1, n2;
1877
1878     kT   = BOLTZ*temp;
1879     beta = 1/kT;
1880
1881     /* count the numbers of samples */ 
1882     n1 = ca->ntot;
1883     n2 = cb->ntot;
1884
1885     /* to ensure the work values are the same as during the delta_G */
1886     /*if (!lambda_vec_same(ca->native_lambda, ca->foreign_lambda))*/
1887     if (ca->foreign_lambda->dhdl<0)
1888     {
1889         /* this is the case when the delta U were calculated directly
1890            (i.e. we're not scaling dhdl) */
1891         Wfac1 = beta;
1892         Wfac2 = beta;
1893     }
1894     else
1895     {
1896         /* we're using dhdl, so delta_lambda needs to be a 
1897            multiplication factor.  */
1898         double delta_lambda=lambda_vec_abs_diff(cb->native_lambda,
1899                                                 ca->native_lambda);
1900         Wfac1 =  beta*delta_lambda;
1901         Wfac2 = -beta*delta_lambda;
1902     }
1903
1904     M = log(n1/n2);
1905
1906
1907     /* calculate average in both directions */
1908     for(i=0;i<ca->nsamples;i++)
1909     {
1910         samples_t *s=ca->s[i];
1911         sample_range_t *r=&(ca->r[i]);
1912         if (r->use)
1913         {
1914             if (!s->hist)
1915             {
1916                 for(j=r->start;j<r->end;j++)
1917                 {
1918                     sigmafact += 1./(2. + 2.*cosh((M + Wfac1*s->du[j] - dg)));
1919                 }
1920             }
1921             else
1922             {
1923                 /* normalization factor multiplied with bin width and
1924                    number of samples (we normalize through M): */
1925                 double normdx = 1.;
1926                 double dx;
1927                 int hd=0; /* histogram direction */
1928                 if ( (s->hist->nhist>1) && (Wfac1<0) )
1929                 {
1930                     hd=1;
1931                 }
1932                 dx=s->hist->dx[hd];
1933
1934                 for(j=0;j<s->hist->nbin[0];j++)
1935                 {
1936                     double x=Wfac1*((j+s->hist->x0[0])+0.5)*dx; /*bin ctr*/
1937                     double pxdx=s->hist->bin[0][j]*normdx; /* p(x)dx */
1938
1939                     sigmafact += pxdx/(2. + 2.*cosh((M + x - dg)));
1940                 }
1941             }
1942         }
1943     }
1944     for(i=0;i<cb->nsamples;i++)
1945     {
1946         samples_t *s=cb->s[i];
1947         sample_range_t *r=&(cb->r[i]);
1948         if (r->use)
1949         {
1950             if (!s->hist)
1951             {
1952                 for(j=r->start;j<r->end;j++)
1953                 {
1954                     sigmafact += 1./(2. + 2.*cosh((M - Wfac2*s->du[j] - dg)));
1955                 }
1956             }
1957             else
1958             {
1959                 /* normalization factor multiplied with bin width and
1960                    number of samples (we normalize through M): */
1961                 double normdx = 1.;
1962                 double dx;
1963                 int hd=0; /* histogram direction */
1964                 if ( (s->hist->nhist>1) && (Wfac2<0) )
1965                 {
1966                     hd=1;
1967                 }
1968                 dx=s->hist->dx[hd];
1969
1970                 for(j=0;j<s->hist->nbin[0];j++)
1971                 {
1972                     double x=Wfac2*((j+s->hist->x0[0])+0.5)*dx;/*bin ctr*/
1973                     double pxdx=s->hist->bin[0][j]*normdx; /* p(x)dx */
1974
1975                     sigmafact += pxdx/(2. + 2.*cosh((M - x - dg)));
1976                 }
1977             }
1978         }
1979     }
1980
1981     sigmafact /= (n1 + n2);
1982  
1983   
1984     /* Eq. 10 from 
1985        Shirts, Bair, Hooker & Pande, Phys. Rev. Lett 91, 140601 (2003): */
1986     *stddev = sqrt(((1./sigmafact) - ( (n1+n2)/n1 + (n1+n2)/n2 )));
1987 }
1988
1989
1990
1991 static void calc_bar(barres_t *br, double tol, 
1992                      int npee_min, int npee_max, gmx_bool *bEE, 
1993                      double *partsum)
1994 {
1995     int npee,p;
1996     double dg_sig2,sa_sig2,sb_sig2,stddev_sig2; /* intermediate variance values
1997                                                    for calculated quantities */
1998     int nsample1, nsample2;
1999     double temp=br->a->temp;
2000     int i,j;
2001     double dg_min, dg_max;
2002     gmx_bool have_hist=FALSE;
2003
2004     br->dg=calc_bar_lowlevel(br->a, br->b, temp, tol, 0);
2005
2006     br->dg_disc_err = 0.;
2007     br->dg_histrange_err = 0.;
2008
2009     /* check if there are histograms */
2010     for(i=0;i<br->a->nsamples;i++)
2011     {
2012         if (br->a->r[i].use && br->a->s[i]->hist)
2013         {
2014             have_hist=TRUE;
2015             break;
2016         }
2017     }
2018     if (!have_hist)
2019     {
2020         for(i=0;i<br->b->nsamples;i++)
2021         {
2022             if (br->b->r[i].use && br->b->s[i]->hist)
2023             {
2024                 have_hist=TRUE;
2025                 break;
2026             }
2027         }
2028     }
2029
2030     /* calculate histogram-specific errors */
2031     if (have_hist)
2032     {
2033         dg_min=calc_bar_lowlevel(br->a, br->b, temp, tol, -1);
2034         dg_max=calc_bar_lowlevel(br->a, br->b, temp, tol, 1);
2035
2036         if (fabs(dg_max - dg_min) > GMX_REAL_EPS*10)
2037         {
2038             /* the histogram range  error is the biggest of the differences 
2039                between the best estimate and the extremes */
2040             br->dg_histrange_err = fabs(dg_max - dg_min);
2041         }
2042         br->dg_disc_err = 0.;
2043         for(i=0;i<br->a->nsamples;i++)
2044         {
2045             if (br->a->s[i]->hist)
2046                 br->dg_disc_err=max(br->dg_disc_err, br->a->s[i]->hist->dx[0]);
2047         }
2048         for(i=0;i<br->b->nsamples;i++)
2049         {
2050             if (br->b->s[i]->hist)
2051                 br->dg_disc_err=max(br->dg_disc_err, br->b->s[i]->hist->dx[0]);
2052         }
2053     }
2054     calc_rel_entropy(br->a, br->b, temp, br->dg, &(br->sa), &(br->sb));
2055                      
2056     calc_dg_stddev(br->a, br->b, temp, br->dg, &(br->dg_stddev) );
2057
2058     dg_sig2 = 0;
2059     sa_sig2 = 0;
2060     sb_sig2 = 0;
2061     stddev_sig2 = 0;
2062
2063     *bEE=TRUE;
2064     {
2065         sample_coll_t ca, cb;
2066
2067         /* initialize the samples */
2068         sample_coll_init(&ca, br->a->native_lambda, br->a->foreign_lambda, 
2069                         br->a->temp);
2070         sample_coll_init(&cb, br->b->native_lambda, br->b->foreign_lambda, 
2071                         br->b->temp);
2072
2073         for(npee=npee_min; npee<=npee_max; npee++)
2074         {
2075             double dgs      = 0;
2076             double dgs2     = 0;
2077             double dsa      = 0;
2078             double dsb      = 0;
2079             double dsa2     = 0;
2080             double dsb2     = 0;
2081             double dstddev  = 0;
2082             double dstddev2 = 0;
2083  
2084             
2085             for(p=0; p<npee; p++)
2086             {
2087                 double dgp;
2088                 double stddevc;
2089                 double sac, sbc;
2090                 gmx_bool cac, cbc;
2091
2092                 cac=sample_coll_create_subsample(&ca, br->a, p, npee);
2093                 cbc=sample_coll_create_subsample(&cb, br->b, p, npee);
2094
2095                 if (!cac || !cbc)
2096                 {
2097                     printf("WARNING: histogram number incompatible with block number for averaging: can't do error estimate\n");
2098                     *bEE=FALSE;
2099                     if (cac)
2100                         sample_coll_destroy(&ca);
2101                     if (cbc)
2102                         sample_coll_destroy(&cb);
2103                     return;
2104                 }
2105
2106                 dgp=calc_bar_lowlevel(&ca, &cb, temp, tol, 0);
2107                 dgs  += dgp;
2108                 dgs2 += dgp*dgp;
2109
2110                 partsum[npee*(npee_max+1)+p] += dgp;
2111
2112                 calc_rel_entropy(&ca, &cb, temp, dgp, &sac, &sbc); 
2113                 dsa  += sac;
2114                 dsa2 += sac*sac;
2115                 dsb  += sbc;
2116                 dsb2 += sbc*sbc;
2117                 calc_dg_stddev(&ca, &cb, temp, dgp, &stddevc );
2118
2119                 dstddev  += stddevc;
2120                 dstddev2 += stddevc*stddevc;
2121
2122                 sample_coll_destroy(&ca);
2123                 sample_coll_destroy(&cb);
2124             }
2125             dgs  /= npee;
2126             dgs2 /= npee;
2127             dg_sig2 += (dgs2-dgs*dgs)/(npee-1);
2128
2129             dsa  /= npee;
2130             dsa2 /= npee;
2131             dsb  /= npee;
2132             dsb2 /= npee;
2133             sa_sig2 += (dsa2-dsa*dsa)/(npee-1);
2134             sb_sig2 += (dsb2-dsb*dsb)/(npee-1);
2135
2136             dstddev  /= npee;
2137             dstddev2 /= npee;
2138             stddev_sig2 += (dstddev2-dstddev*dstddev)/(npee-1);
2139         }
2140         br->dg_err = sqrt(dg_sig2/(npee_max - npee_min + 1));
2141         br->sa_err = sqrt(sa_sig2/(npee_max - npee_min + 1));
2142         br->sb_err = sqrt(sb_sig2/(npee_max - npee_min + 1));
2143         br->dg_stddev_err = sqrt(stddev_sig2/(npee_max - npee_min + 1));
2144     }
2145 }
2146
2147
2148 static double bar_err(int nbmin, int nbmax, const double *partsum)
2149 {
2150     int nb,b;
2151     double svar,s,s2,dg;
2152
2153     svar = 0;
2154     for(nb=nbmin; nb<=nbmax; nb++)
2155     {
2156         s  = 0;
2157         s2 = 0;
2158         for(b=0; b<nb; b++)
2159         {
2160             dg  = partsum[nb*(nbmax+1)+b];
2161             s  += dg;
2162             s2 += dg*dg;
2163         }
2164         s  /= nb;
2165         s2 /= nb;
2166         svar += (s2 - s*s)/(nb - 1);
2167     }
2168
2169     return sqrt(svar/(nbmax + 1 - nbmin));
2170 }
2171
2172
2173 /* Seek the end of an identifier (consecutive non-spaces), followed by
2174    an optional number of spaces or '='-signs. Returns a pointer to the
2175    first non-space value found after that. Returns NULL if the string
2176    ends before that.
2177    */
2178 static const char *find_value(const char *str)
2179 {
2180     gmx_bool name_end_found=FALSE;
2181
2182     /* if the string is a NULL pointer, return a NULL pointer. */
2183     if (str==NULL)
2184     {
2185         return NULL;
2186     }
2187     while(*str != '\0')
2188     {
2189         /* first find the end of the name */
2190         if (! name_end_found)
2191         {
2192             if ( isspace(*str) || (*str == '=') )
2193             {
2194                 name_end_found=TRUE;
2195             }
2196         }
2197         else
2198         {
2199             if (! ( isspace(*str) || (*str == '=') ))
2200             {
2201                 return str;
2202             }
2203         }
2204         str++;
2205     }
2206     return NULL;
2207 }
2208
2209
2210
2211 /* read a vector-notation description of a lambda vector */
2212 static gmx_bool read_lambda_compvec(const char *str,
2213                                     lambda_vec_t *lv,
2214                                     const lambda_components_t *lc_in,
2215                                     lambda_components_t *lc_out,
2216                                     const char **end,
2217                                     const char *fn)
2218 {
2219     gmx_bool initialize_lc = FALSE; /* whether to initialize the lambda
2220                                        components, or to check them */
2221     gmx_bool start_reached=FALSE; /* whether the start of component names
2222                                      has been reached */
2223     gmx_bool vector=FALSE; /* whether there are multiple components */
2224     int n = 0; /* current component number */
2225     const char *val_start=NULL; /* start of the component name, or NULL
2226                                    if not in a value */
2227     char *strtod_end;
2228     gmx_bool OK=TRUE;
2229
2230     if (end)
2231         *end=str;
2232
2233
2234     if (lc_out && lc_out->N == 0)
2235     {
2236         initialize_lc = TRUE;
2237     }
2238
2239     if (lc_in == NULL)
2240         lc_in = lc_out;
2241
2242     while(1)
2243     {
2244         if (!start_reached)
2245         {
2246             if (isalnum(*str))
2247             {
2248                 vector=FALSE;
2249                 start_reached=TRUE;
2250                 val_start=str;
2251             }
2252             else if (*str=='(')
2253             {
2254                 vector=TRUE;
2255                 start_reached=TRUE;
2256             }
2257             else if (! isspace(*str))
2258             {
2259                 gmx_fatal(FARGS, "Error in lambda components in %s", fn);
2260             }
2261         }
2262         else
2263         {
2264             if (val_start)
2265             {
2266                 if (isspace(*str) || *str==')' || *str==',' || *str=='\0')
2267                 {
2268                     /* end of value */
2269                     if (lv == NULL)
2270                     {
2271                         if (initialize_lc)
2272                         {
2273                             lambda_components_add(lc_out, val_start, 
2274                                                   (str-val_start));
2275                         }
2276                         else
2277                         {
2278                             if (!lambda_components_check(lc_out, n, val_start, 
2279                                                          (str-val_start)))
2280                             {
2281                                 return FALSE;
2282                             }
2283                         }
2284                     }
2285                     else
2286                     {
2287                         /* add a vector component to lv */
2288                         lv->val[n] = strtod(val_start, &strtod_end);
2289                         if (val_start == strtod_end)
2290                         {
2291                             gmx_fatal(FARGS,
2292                                       "Error reading lambda vector in %s", fn);
2293                         }
2294                     }
2295                     /* reset for the next identifier */
2296                     val_start=NULL;
2297                     n++;
2298                     if (!vector)
2299                     {
2300                         return OK;
2301                     }
2302                 }
2303             }
2304             else if (isalnum(*str))
2305             {
2306                 val_start=str;
2307             }
2308             if (*str == ')')
2309             {
2310                 str++;
2311                 if (end)
2312                     *end=str;
2313                 if (!vector)
2314                 {
2315                     gmx_fatal(FARGS, "Error in lambda components in %s", fn);
2316                 }
2317                 else
2318                 {
2319                     if (n == lc_in->N)
2320                     {
2321                         return OK;
2322                     }
2323                     else if (lv == NULL)
2324                     {
2325                         return FALSE;
2326                     }
2327                     else
2328                     {
2329                         gmx_fatal(FARGS, "Incomplete lambda vector data in %s", 
2330                                   fn);
2331                         return FALSE;
2332                     }
2333
2334                 }
2335             }
2336         }
2337         if (*str == '\0')
2338         {
2339             break;
2340         }
2341         str++;
2342         if (end)
2343         {
2344             *end=str;
2345         }
2346     }
2347     if (vector)
2348     {
2349         gmx_fatal(FARGS, "Incomplete lambda components data in %s", fn);
2350         return FALSE;
2351     }
2352     return OK;
2353 }
2354
2355 /* read and check the component names from a string */
2356 static gmx_bool read_lambda_components(const char *str,
2357                                         lambda_components_t *lc,
2358                                         const char **end,
2359                                         const char *fn)
2360 {
2361     return read_lambda_compvec(str, NULL, NULL, lc, end, fn);
2362 }
2363
2364 /* read an initialized lambda vector from a string */
2365 static gmx_bool read_lambda_vector(const char *str,
2366                                    lambda_vec_t *lv,
2367                                    const char **end,
2368                                    const char *fn)
2369 {
2370     return read_lambda_compvec(str, lv, lv->lc, NULL, end, fn);
2371 }
2372
2373
2374
2375 /* deduce lambda value from legend. 
2376     fn = the file name
2377     legend = the legend string
2378     ba = the xvg data
2379     lam = the initialized lambda vector
2380 returns whether to use the data in this set.
2381     */
2382 static gmx_bool legend2lambda(const char *fn,
2383                               const char *legend,
2384                               xvg_t *ba,
2385                               lambda_vec_t *lam)
2386 {
2387     double lambda=0;
2388     const char   *ptr=NULL, *ptr2=NULL;
2389     gmx_bool ok=FALSE;
2390     gmx_bool bdhdl=FALSE;
2391     const char *tostr=" to ";
2392
2393     if (legend == NULL)
2394     {
2395         gmx_fatal(FARGS,"There is no legend in file '%s', can not deduce lambda",fn);
2396     }
2397
2398     /* look for the last 'to': */
2399     ptr2=legend;
2400     do
2401     {
2402         ptr2 = strstr(ptr2, tostr);
2403         if (ptr2!=NULL)
2404         {
2405             ptr=ptr2;
2406             ptr2++;
2407         }
2408     }
2409     while(ptr2!=NULL && *ptr2!= '\0' );
2410
2411     if (ptr)
2412     {
2413         ptr += strlen(tostr)-1; /* and advance past that 'to' */
2414     }
2415     else
2416     {
2417         /* look for the = sign */
2418         ptr = strrchr(legend,'=');
2419         if (!ptr)
2420         {
2421             /* otherwise look for the last space */
2422             ptr = strrchr(legend,' ');
2423         }
2424     }
2425
2426     if (strstr(legend,"dH"))
2427     {
2428         ok=TRUE;
2429         bdhdl=TRUE;
2430     }
2431     else if (strchr(legend,'D') != NULL && strchr(legend,'H') != NULL)
2432     {
2433         ok=TRUE;
2434         bdhdl=FALSE;
2435     }
2436     else /*if (strstr(legend, "pV"))*/
2437     {
2438         return FALSE;
2439     }
2440     if (!ptr)
2441     {
2442         ok=FALSE;
2443     }
2444
2445     if (!ok)
2446     {
2447         gmx_fatal(FARGS,"There is no proper lambda legend in file '%s', can not deduce lambda",fn);
2448     }
2449     if (!bdhdl)
2450     {
2451         ptr=find_value(ptr);
2452         if (!ptr || !read_lambda_vector(ptr, lam, NULL, fn))
2453         {
2454             gmx_fatal(FARGS, "lambda vector '%s' %s faulty", legend, fn);
2455         }
2456     }
2457     else
2458     {
2459         int dhdl_index;
2460         const char *end;
2461         char buf[STRLEN];
2462
2463         ptr = strrchr(legend, '=');
2464         end=ptr;
2465         if (ptr)
2466         {
2467             /* there must be a component name */
2468             ptr--;
2469             if (ptr < legend)
2470             {
2471                 gmx_fatal(FARGS, "dhdl legend '%s' %s faulty", legend, fn);
2472             }
2473             /* now backtrack to the start of the identifier */
2474             while(isspace(*ptr))
2475             {
2476                 end=ptr;
2477                 ptr--;
2478                 if (ptr < legend)
2479                 {
2480                     gmx_fatal(FARGS, "dhdl legend '%s' %s faulty", legend, fn);
2481                 }
2482             }
2483             while(!isspace(*ptr))
2484             {
2485                 ptr--;
2486                 if (ptr < legend)
2487                 {
2488                     gmx_fatal(FARGS, "dhdl legend '%s' %s faulty", legend, fn);
2489                 }
2490             }
2491             ptr++;
2492             strncpy(buf, ptr, (end-ptr));
2493             buf[(end-ptr)]='\0';
2494             dhdl_index=lambda_components_find(lam->lc, ptr, (end-ptr));
2495             if (dhdl_index < 0)
2496             {
2497                 char buf[STRLEN];
2498                 strncpy(buf, ptr, (end-ptr));
2499                 buf[(end-ptr)]='\0';
2500                 gmx_fatal(FARGS,
2501                           "Did not find lambda component for '%s' in %s",
2502                           buf, fn);
2503             }
2504         }
2505         else
2506         {
2507             if (lam->lc->N > 1)
2508             {
2509                 gmx_fatal(FARGS,
2510                    "dhdl without component name with >1 lambda component in %s",
2511                    fn);
2512             }
2513             dhdl_index=0;
2514         }
2515         lam->dhdl = dhdl_index;
2516     }
2517     return TRUE;
2518 }
2519
2520 static gmx_bool subtitle2lambda(const char *subtitle, xvg_t *ba, const char *fn,
2521                                 lambda_components_t *lc)
2522 {
2523     gmx_bool bFound;
2524     const char *ptr;
2525     char *end;
2526     double native_lambda;
2527
2528     bFound = FALSE;
2529
2530     /* first check for a state string */
2531     ptr = strstr(subtitle, "state");
2532     if (ptr)
2533     {
2534         int index=-1;
2535         const char *val_end;
2536
2537         /* the new 4.6 style lambda vectors */
2538         ptr = find_value(ptr);
2539         if (ptr)
2540         {
2541             index = strtol(ptr, &end, 10);
2542             if (ptr == end)
2543             {
2544                 gmx_fatal(FARGS,"Incomplete state data in %s", fn);
2545                 return FALSE;
2546             }
2547             ptr=end;
2548         }
2549         else
2550         {
2551             gmx_fatal(FARGS,"Incomplete state data in %s", fn);
2552             return FALSE;
2553         }
2554         /* now find the lambda vector component names */
2555         while(*ptr!='(' && ! isalnum(*ptr))
2556         {
2557             ptr++;
2558             if (*ptr == '\0')
2559             {
2560                 gmx_fatal(FARGS,
2561                           "Incomplete lambda vector component data in %s", fn);
2562                 return FALSE;
2563             }
2564         }
2565         val_end=ptr;
2566         if (!read_lambda_components(ptr, lc, &val_end, fn))
2567         {
2568             gmx_fatal(FARGS,
2569                       "lambda vector components in %s don't match those previously read", 
2570                       fn);
2571         }
2572         ptr=find_value(val_end);
2573         if (!ptr)
2574         {
2575             gmx_fatal(FARGS,"Incomplete state data in %s", fn);
2576             return FALSE;
2577         }
2578         lambda_vec_init(&(ba->native_lambda), lc);
2579         if (!read_lambda_vector(ptr, &(ba->native_lambda), NULL, fn))
2580         {
2581             gmx_fatal(FARGS, "lambda vector in %s faulty", fn);
2582         }
2583         ba->native_lambda.index=index;
2584         bFound=TRUE;
2585     }
2586     else
2587     {
2588         /* compatibility mode: check for lambda in other ways. */
2589         /* plain text lambda string */
2590         ptr = strstr(subtitle,"lambda");
2591         if (ptr == NULL)
2592         {
2593             /* xmgrace formatted lambda string */
2594             ptr = strstr(subtitle,"\\xl\\f{}");
2595         }
2596         if (ptr == NULL)
2597         {
2598             /* xmgr formatted lambda string */
2599             ptr = strstr(subtitle,"\\8l\\4");
2600         }
2601         if (ptr != NULL)
2602         {
2603             ptr = strstr(ptr,"=");
2604         }
2605         if (ptr != NULL)
2606         {
2607             bFound = (sscanf(ptr+1,"%lf",&(native_lambda)) == 1);
2608             /* add the lambda component name as an empty string */
2609             if (lc->N > 0)
2610             {
2611                 if (!lambda_components_check(lc, 0, "", 0))
2612                 {
2613                     gmx_fatal(FARGS,
2614                               "lambda vector components in %s don't match those previously read", 
2615                               fn);
2616                 }
2617             }
2618             else
2619             {
2620                 lambda_components_add(lc, "", 0);
2621             }
2622             lambda_vec_init(&(ba->native_lambda), lc);
2623             ba->native_lambda.val[0]=native_lambda;
2624         }
2625     }
2626
2627     return bFound;
2628 }
2629
2630 static void filename2lambda(const char *fn, xvg_t *ba)
2631 {
2632     double lambda;
2633     const char   *ptr,*digitptr;
2634     char *endptr;
2635     int     dirsep;
2636     ptr = fn;
2637     /* go to the end of the path string and search backward to find the last 
2638        directory in the path which has to contain the value of lambda 
2639      */
2640     while (ptr[1] != '\0')
2641     {
2642         ptr++;
2643     }
2644     /* searching backward to find the second directory separator */
2645     dirsep = 0;
2646     digitptr = NULL;
2647     while (ptr >= fn)
2648     {
2649         if (ptr[0] != DIR_SEPARATOR && ptr[1] == DIR_SEPARATOR)
2650         {            
2651             if (dirsep == 1) break;
2652             dirsep++;
2653         }
2654         /* save the last position of a digit between the last two 
2655            separators = in the last dirname */
2656         if (dirsep > 0 && isdigit(*ptr))
2657         {
2658             digitptr = ptr;
2659         }
2660         ptr--;
2661     }
2662     if (!digitptr)
2663     {
2664         gmx_fatal(FARGS,"While trying to read the lambda value from the file path:"
2665                     " last directory in the path '%s' does not contain a number",fn);
2666     }
2667     if (digitptr[-1] == '-')
2668     {
2669         digitptr--;
2670     }
2671     lambda = strtod(digitptr,&endptr);
2672     if (endptr == digitptr)
2673     {
2674         gmx_fatal(FARGS,"Malformed number in file path '%s'",fn);
2675     }
2676 }
2677
2678 static void read_bar_xvg_lowlevel(const char *fn, real *temp, xvg_t *ba,
2679                                   lambda_components_t *lc)
2680 {
2681     int  i;
2682     char *subtitle,**legend,*ptr;
2683     int np;
2684     gmx_bool native_lambda_read=FALSE;
2685     char buf[STRLEN];
2686     lambda_vec_t lv;
2687
2688     xvg_init(ba);
2689
2690     ba->filename = fn;
2691
2692     np = read_xvg_legend(fn,&ba->y,&ba->nset,&subtitle,&legend);
2693     if (!ba->y)
2694     {
2695         gmx_fatal(FARGS,"File %s contains no usable data.",fn);
2696     }
2697     /* Reorder the data */
2698     ba->t  = ba->y[0];
2699     for(i=1; i<ba->nset; i++)
2700     {
2701         ba->y[i-1] = ba->y[i];
2702     }
2703     ba->nset--;
2704
2705     snew(ba->np,ba->nset);
2706     for(i=0;i<ba->nset;i++)
2707         ba->np[i]=np;
2708
2709     ba->temp = -1;
2710     if (subtitle != NULL)
2711     {
2712         /* try to extract temperature */
2713         ptr = strstr(subtitle,"T =");
2714         if (ptr != NULL)
2715         {
2716             ptr += 3;
2717             if (sscanf(ptr,"%lf",&ba->temp) == 1)
2718             {
2719                 if (ba->temp <= 0)
2720                 {
2721                     gmx_fatal(FARGS,"Found temperature of %g in file '%s'",
2722                               ba->temp,fn);
2723                 }
2724             }
2725         }
2726     }
2727     if (ba->temp < 0)
2728     {
2729         if (*temp <= 0)
2730         {
2731             gmx_fatal(FARGS,"Did not find a temperature in the subtitle in file '%s', use the -temp option of [TT]g_bar[tt]",fn);
2732         }
2733         ba->temp = *temp;
2734     }
2735
2736     /* Try to deduce lambda from the subtitle */
2737     if (subtitle)
2738     {
2739         if (subtitle2lambda(subtitle,ba, fn, lc))
2740         {
2741             native_lambda_read=TRUE;
2742         }
2743     }
2744     snew(ba->lambda,ba->nset);
2745     if (legend == NULL)
2746     {
2747         /* Check if we have a single set, no legend, nset=1 means t and dH/dl */
2748         if (ba->nset == 1)
2749         {
2750             if (!native_lambda_read)
2751             {
2752                 /* Deduce lambda from the file name */
2753                 filename2lambda(fn, ba);
2754                 native_lambda_read=TRUE;
2755             }
2756             ba->lambda[0] = ba->native_lambda;
2757         }
2758         else
2759         {
2760             gmx_fatal(FARGS,"File %s contains multiple sets but no legends, can not determine the lambda values",fn);
2761         }
2762     }
2763     else
2764     {
2765         for(i=0; i<ba->nset; )
2766         {
2767             gmx_bool use=FALSE;
2768             /* Read lambda from the legend */
2769             lambda_vec_init( &(ba->lambda[i]), lc );
2770             lambda_vec_copy( &(ba->lambda[i]), &(ba->native_lambda));
2771             use=legend2lambda(fn,legend[i], ba, &(ba->lambda[i]));
2772             if (use)
2773             {
2774                 lambda_vec_print(&(ba->lambda[i]), buf, FALSE);
2775                 i++;
2776             }
2777             else
2778             {
2779                 int j;
2780                 printf("%s: Ignoring set '%s'.\n", fn, legend[i]);
2781                 for(j=i+1; j<ba->nset; j++)
2782                 {
2783                     ba->y[j-1] = ba->y[j];
2784                     legend[j-1] = legend[j];
2785                 }
2786                 ba->nset--;
2787             }
2788         }
2789     }
2790
2791     if (!native_lambda_read)
2792     {
2793         gmx_fatal(FARGS,"File %s contains multiple sets but no indication of the native lambda",fn);
2794     }
2795     
2796     if (legend != NULL)
2797     {
2798         for(i=0; i<ba->nset-1; i++)
2799         {
2800             sfree(legend[i]);
2801         }
2802         sfree(legend);
2803     }
2804 }
2805
2806 static void read_bar_xvg(char *fn, real *temp, sim_data_t *sd)
2807 {
2808     xvg_t *barsim;
2809     samples_t *s;
2810     int i;
2811     double *lambda;
2812
2813     snew(barsim,1);
2814
2815     read_bar_xvg_lowlevel(fn, temp, barsim, &(sd->lc));
2816
2817     if (barsim->nset <1 )
2818     {
2819         gmx_fatal(FARGS,"File '%s' contains fewer than two columns", fn);
2820     }
2821
2822     if ( !gmx_within_tol(*temp,barsim->temp,GMX_FLOAT_EPS) && (*temp > 0) )
2823     {
2824         gmx_fatal(FARGS,"Temperature in file %s different from earlier files or setting\n", fn);
2825     }
2826     *temp=barsim->temp;
2827
2828     /* now create a series of samples_t */
2829     snew(s, barsim->nset);
2830     for(i=0;i<barsim->nset;i++)
2831     {
2832         samples_init(s+i, &(barsim->native_lambda), &(barsim->lambda[i]),
2833                      barsim->temp, lambda_vec_same(&(barsim->native_lambda),
2834                                                    &(barsim->lambda[i])),
2835                      fn);
2836         s[i].du=barsim->y[i];
2837         s[i].ndu=barsim->np[i];
2838         s[i].t=barsim->t;
2839
2840         lambda_data_list_insert_sample(sd->lb, s+i);
2841     }
2842     {
2843         char buf[STRLEN];
2844
2845         lambda_vec_print(s[0].native_lambda, buf, FALSE);
2846         printf("%s: %.1f - %.1f; lambda = %s\n    dH/dl & foreign lambdas:\n", 
2847                fn, s[0].t[0], s[0].t[s[0].ndu-1], buf);
2848         for(i=0;i<barsim->nset;i++)
2849         {
2850             lambda_vec_print(s[i].foreign_lambda, buf, TRUE);
2851             printf("        %s (%d pts)\n", buf, s[i].ndu);
2852         }
2853     }
2854     printf("\n\n");
2855 }
2856
2857 static void read_edr_rawdh_block(samples_t **smp, int *ndu, t_enxblock *blk, 
2858                                  double start_time, double delta_time,
2859                                  lambda_vec_t *native_lambda, double temp,
2860                                  double *last_t, const char *filename)
2861 {
2862     int i,j;
2863     gmx_bool allocated;
2864     double old_foreign_lambda;
2865     lambda_vec_t *foreign_lambda;
2866     int type;
2867     samples_t *s; /* convenience pointer */
2868     int startj;
2869
2870     /* check the block types etc. */
2871     if ( (blk->nsub < 3) ||
2872          (blk->sub[0].type != xdr_datatype_int) ||
2873          (blk->sub[1].type != xdr_datatype_double) ||
2874          (
2875           (blk->sub[2].type != xdr_datatype_float) &&
2876           (blk->sub[2].type != xdr_datatype_double) 
2877          ) ||
2878          (blk->sub[0].nr < 1) ||
2879          (blk->sub[1].nr < 1) )
2880     {
2881         gmx_fatal(FARGS, 
2882                   "Unexpected/corrupted block data in file %s around time %g.", 
2883                   filename, start_time);
2884     }
2885
2886     snew(foreign_lambda, 1);
2887     lambda_vec_init(foreign_lambda, native_lambda->lc);
2888     lambda_vec_copy(foreign_lambda, native_lambda);
2889     type = blk->sub[0].ival[0];
2890     if (type == dhbtDH)
2891     {
2892         for(i=0;i<native_lambda->lc->N;i++)
2893         {
2894             foreign_lambda->val[i] = blk->sub[1].dval[i];
2895         }
2896     }
2897     else
2898     {
2899         if (blk->sub[0].nr > 1)
2900         {
2901             foreign_lambda->dhdl = blk->sub[0].ival[1];
2902         }
2903         else
2904             foreign_lambda->dhdl = 0;
2905     }
2906
2907     if (! *smp)
2908     {
2909         /* initialize the samples structure if it's empty. */
2910         snew(*smp, 1);
2911         samples_init(*smp, native_lambda, foreign_lambda, temp,
2912                      type==dhbtDHDL, filename);
2913         (*smp)->start_time=start_time;
2914         (*smp)->delta_time=delta_time;
2915     }
2916
2917     /* set convenience pointer */
2918     s=*smp;
2919
2920     /* now double check */
2921     if ( ! lambda_vec_same(s->foreign_lambda, foreign_lambda) )
2922     {
2923         char buf[STRLEN], buf2[STRLEN];
2924         lambda_vec_print(foreign_lambda, buf, FALSE);
2925         lambda_vec_print(s->foreign_lambda, buf2, FALSE);
2926         fprintf(stderr, "Got foreign lambda=%s, expected: %s\n", buf, buf2);
2927         gmx_fatal(FARGS, "Corrupted data in file %s around t=%g.", 
2928                   filename, start_time);
2929     }
2930
2931     /* make room for the data */
2932     if (s->ndu_alloc < (size_t)(s->ndu + blk->sub[2].nr) )
2933     {
2934         s->ndu_alloc += (s->ndu_alloc < (size_t)blk->sub[2].nr) ?  
2935                             blk->sub[2].nr*2 : s->ndu_alloc;
2936         srenew(s->du_alloc, s->ndu_alloc);
2937         s->du=s->du_alloc;
2938     }
2939     startj = s->ndu;
2940     s->ndu += blk->sub[2].nr;
2941     s->ntot += blk->sub[2].nr;
2942     *ndu = blk->sub[2].nr;
2943
2944     /* and copy the data*/
2945     for(j=0;j<blk->sub[2].nr;j++)
2946     {
2947         if (blk->sub[2].type == xdr_datatype_float)
2948         {
2949             s->du[startj+j] = blk->sub[2].fval[j];
2950         }
2951         else
2952         {
2953             s->du[startj+j] = blk->sub[2].dval[j];
2954         }
2955     }
2956     if (start_time + blk->sub[2].nr*delta_time > *last_t)
2957     {
2958         *last_t = start_time + blk->sub[2].nr*delta_time;
2959     }
2960 }
2961
2962 static samples_t *read_edr_hist_block(int *nsamples, t_enxblock *blk,
2963                                       double start_time, double delta_time,
2964                                       lambda_vec_t *native_lambda, double temp,
2965                                       double *last_t, const char *filename)
2966 {
2967     int i,j;
2968     samples_t *s;
2969     int nhist;
2970     double old_foreign_lambda;
2971     lambda_vec_t *foreign_lambda;
2972     int type;
2973     int nbins[2];
2974
2975     /* check the block types etc. */
2976     if ( (blk->nsub < 2) ||
2977          (blk->sub[0].type != xdr_datatype_double) ||
2978          (blk->sub[1].type != xdr_datatype_large_int) ||
2979          (blk->sub[0].nr < 2)  ||
2980          (blk->sub[1].nr < 2) )
2981     {
2982         gmx_fatal(FARGS, 
2983                   "Unexpected/corrupted block data in file %s around time %g", 
2984                   filename, start_time);
2985     }
2986
2987     nhist=blk->nsub-2;
2988     if (nhist == 0)
2989     {
2990         return NULL;
2991     }
2992     if (nhist > 2)
2993     {
2994         gmx_fatal(FARGS, 
2995                   "Unexpected/corrupted block data in file %s around time %g", 
2996                   filename, start_time);
2997     }
2998
2999     snew(s, 1);
3000     *nsamples=1;
3001
3002     snew(foreign_lambda, 1);
3003     lambda_vec_init(foreign_lambda, native_lambda->lc);
3004     lambda_vec_copy(foreign_lambda, native_lambda);
3005     type = (int)(blk->sub[1].lval[1]);
3006     if (type == dhbtDH)
3007     {
3008         double old_foreign_lambda;
3009
3010         old_foreign_lambda=blk->sub[0].dval[0];
3011         if (old_foreign_lambda >= 0)
3012         {
3013             foreign_lambda->val[0]=old_foreign_lambda;
3014             if (foreign_lambda->lc->N > 1)
3015             {
3016                 gmx_fatal(FARGS,
3017                           "Single-component lambda in multi-component file %s", 
3018                           filename);
3019             }
3020         }
3021         else
3022         {
3023             for(i=0;i<native_lambda->lc->N;i++)
3024             {
3025                 foreign_lambda->val[i] = blk->sub[0].dval[i+2];
3026             }
3027         }
3028     }
3029     else
3030     {
3031         if (foreign_lambda->lc->N > 1)
3032         {
3033             if (blk->sub[1].nr < 3 + nhist)
3034             {
3035                 gmx_fatal(FARGS,
3036                         "Missing derivative coord in multi-component file %s", 
3037                         filename);
3038             }
3039             foreign_lambda->dhdl = blk->sub[1].lval[2 + nhist];
3040         }
3041         else
3042         {
3043             foreign_lambda->dhdl = 0;
3044         }
3045     }
3046
3047     samples_init(s, native_lambda, foreign_lambda, temp, type==dhbtDHDL,
3048                  filename);
3049     snew(s->hist, 1);
3050
3051     for(i=0;i<nhist;i++)
3052     {
3053         nbins[i] = blk->sub[i+2].nr;
3054     }
3055
3056     hist_init(s->hist, nhist, nbins);
3057
3058     for(i=0;i<nhist;i++)
3059     {
3060         s->hist->x0[i] = blk->sub[1].lval[2+i];
3061         s->hist->dx[i] = blk->sub[0].dval[1];
3062         if (i==1)
3063         {
3064             s->hist->dx[i] = - s->hist->dx[i];
3065         }
3066     }
3067
3068     s->hist->start_time = start_time;
3069     s->hist->delta_time = delta_time;
3070     s->start_time = start_time;
3071     s->delta_time = delta_time;
3072
3073     for(i=0;i<nhist;i++)
3074     {
3075         int nbin;
3076         gmx_large_int_t sum=0;
3077
3078         for(j=0;j<s->hist->nbin[i];j++)
3079         { 
3080             int binv=(int)(blk->sub[i+2].ival[j]);
3081
3082             s->hist->bin[i][j] = binv;
3083             sum += binv;
3084
3085         }
3086         if (i==0)
3087         {
3088             s->ntot = sum;
3089             s->hist->sum = sum;
3090         }
3091         else
3092         {
3093             if (s->ntot != sum)
3094             {
3095                 gmx_fatal(FARGS, "Histogram counts don't match in %s", 
3096                           filename);
3097             }
3098         }
3099     }
3100
3101     if (start_time + s->hist->sum*delta_time > *last_t)
3102     {
3103         *last_t = start_time + s->hist->sum*delta_time;
3104     }
3105     return s;
3106 }
3107
3108
3109 static void read_barsim_edr(char *fn, real *temp, sim_data_t *sd)
3110 {
3111     int i,j;
3112     ener_file_t fp;
3113     t_enxframe *fr; 
3114     int nre;
3115     gmx_enxnm_t *enm=NULL;
3116     double first_t=-1;
3117     double last_t=-1;
3118     samples_t **samples_rawdh=NULL; /* contains samples for raw delta_h  */
3119     int *nhists=NULL;       /* array to keep count & print at end */
3120     int *npts=NULL;         /* array to keep count & print at end */
3121     lambda_vec_t **lambdas=NULL;   /* array to keep count & print at end */
3122     lambda_vec_t *native_lambda;
3123     double end_time;        /* the end time of the last batch of samples */
3124     int nsamples=0;
3125     lambda_vec_t start_lambda;
3126
3127     fp = open_enx(fn,"r");
3128     do_enxnms(fp,&nre,&enm);
3129     snew(fr, 1);
3130
3131     snew(native_lambda, 1);
3132     start_lambda.lc = NULL;
3133
3134     while(do_enx(fp, fr))
3135     {
3136         /* count the data blocks */
3137         int nblocks_raw=0;
3138         int nblocks_hist=0;
3139         int nlam=0;
3140         int k;
3141         /* DHCOLL block information: */
3142         double start_time=0, delta_time=0, old_start_lambda=0, delta_lambda=0;
3143         double rtemp=0;
3144
3145         /* count the blocks and handle collection information: */
3146         for(i=0;i<fr->nblock;i++)
3147         {
3148             if (fr->block[i].id == enxDHHIST )
3149                 nblocks_hist++;
3150             if ( fr->block[i].id == enxDH )
3151                 nblocks_raw++;
3152             if (fr->block[i].id == enxDHCOLL )
3153             {
3154                 nlam++;
3155                 if ( (fr->block[i].nsub < 1) || 
3156                      (fr->block[i].sub[0].type != xdr_datatype_double) ||
3157                      (fr->block[i].sub[0].nr < 5))
3158                 {
3159                     gmx_fatal(FARGS, "Unexpected block data in file %s", fn);
3160                 }
3161
3162                 /* read the data from the DHCOLL block */
3163                 rtemp =        fr->block[i].sub[0].dval[0];
3164                 start_time =   fr->block[i].sub[0].dval[1];
3165                 delta_time =   fr->block[i].sub[0].dval[2];
3166                 old_start_lambda = fr->block[i].sub[0].dval[3];
3167                 delta_lambda = fr->block[i].sub[0].dval[4];
3168
3169                 if (delta_lambda>0)
3170                 {
3171                     gmx_fatal(FARGS, "Lambda values not constant in %s: can't apply BAR method", fn);
3172                 }
3173                 if ( ( *temp != rtemp) && (*temp > 0) )
3174                 {
3175                     gmx_fatal(FARGS,"Temperature in file %s different from earlier files or setting\n", fn);
3176                 }
3177                 *temp=rtemp;
3178
3179                 if (old_start_lambda >= 0)
3180                 {
3181                     if (sd->lc.N > 0)
3182                     {
3183                         if (!lambda_components_check(&(sd->lc), 0, "", 0))
3184                         {
3185                             gmx_fatal(FARGS,
3186                                       "lambda vector components in %s don't match those previously read",
3187                                       fn);
3188                         }
3189                     }
3190                     else
3191                     {
3192                         lambda_components_add(&(sd->lc), "", 0);
3193                     }
3194                     if (!start_lambda.lc)
3195                     {
3196                         lambda_vec_init(&start_lambda, &(sd->lc));
3197                     }
3198                     start_lambda.val[0]=old_start_lambda;
3199                 }
3200                 else
3201                 {
3202                     /* read lambda vector */
3203                     int n_lambda_vec;
3204                     gmx_bool check=(sd->lc.N > 0);
3205                     if (fr->block[i].nsub < 2)
3206                     {
3207                         gmx_fatal(FARGS, 
3208                                   "No lambda vector, but start_lambda=%g\n",
3209                                   old_start_lambda);
3210                     }
3211                     n_lambda_vec=fr->block[i].sub[1].ival[1];
3212                     for(j=0;j<n_lambda_vec;j++)
3213                     {
3214                         const char *name=
3215                              efpt_singular_names[fr->block[i].sub[1].ival[1+j]];
3216                         if (check)
3217                         {
3218                             /* check the components */
3219                             lambda_components_check(&(sd->lc), j, name,
3220                                                     strlen(name));
3221                         }
3222                         else
3223                         {
3224                             lambda_components_add(&(sd->lc), name, 
3225                                                   strlen(name));
3226                         }
3227                     }
3228                     lambda_vec_init(&start_lambda, &(sd->lc));
3229                     start_lambda.index=fr->block[i].sub[1].ival[0];
3230                     for(j=0;j<n_lambda_vec;j++)
3231                     {
3232                         start_lambda.val[j]=fr->block[i].sub[0].dval[5+j];
3233                     }
3234                 }
3235                 if (first_t < 0)
3236                     first_t=start_time;
3237             }
3238         }
3239
3240         if (nlam != 1)
3241         {
3242             gmx_fatal(FARGS, "Did not find delta H information in file %s", fn);
3243         }
3244         if (nblocks_raw > 0 && nblocks_hist > 0 )
3245         {
3246             gmx_fatal(FARGS, "Can't handle both raw delta U data and histograms in the same file %s", fn);
3247         }
3248
3249         if (nsamples > 0)
3250         {
3251             /* check the native lambda */
3252             if (!lambda_vec_same(&start_lambda, native_lambda) )
3253             {
3254                 gmx_fatal(FARGS, "Native lambda not constant in file %s: started at %g, and becomes %g at time %g", 
3255                           fn, native_lambda, start_lambda, start_time);
3256             }
3257             /* check the number of samples against the previous number */
3258             if ( ((nblocks_raw+nblocks_hist)!=nsamples) || (nlam!=1 ) )
3259             {
3260                 gmx_fatal(FARGS, "Unexpected block count in %s: was %d, now %d\n",
3261                           fn, nsamples+1, nblocks_raw+nblocks_hist+nlam);
3262             }
3263             /* check whether last iterations's end time matches with 
3264                the currrent start time */
3265             if ( (fabs(last_t - start_time) > 2*delta_time)  && last_t>=0)
3266             {
3267                 /* it didn't. We need to store our samples and reallocate */
3268                 for(i=0;i<nsamples;i++)
3269                 {
3270                     if (samples_rawdh[i])
3271                     {
3272                         /* insert it into the existing list */
3273                         lambda_data_list_insert_sample(sd->lb,
3274                                                        samples_rawdh[i]);
3275                         /* and make sure we'll allocate a new one this time
3276                            around */
3277                         samples_rawdh[i]=NULL;
3278                     }
3279                 }
3280             }
3281         }
3282         else
3283         {
3284             /* this is the first round; allocate the associated data 
3285                structures */
3286             /*native_lambda=start_lambda;*/
3287             lambda_vec_init(native_lambda, &(sd->lc));
3288             lambda_vec_copy(native_lambda, &start_lambda);
3289             nsamples=nblocks_raw+nblocks_hist;
3290             snew(nhists, nsamples);
3291             snew(npts, nsamples);
3292             snew(lambdas, nsamples);
3293             snew(samples_rawdh, nsamples);
3294             for(i=0;i<nsamples;i++)
3295             {
3296                 nhists[i]=0;
3297                 npts[i]=0;
3298                 lambdas[i]=NULL;
3299                 samples_rawdh[i]=NULL; /* init to NULL so we know which
3300                                           ones contain values */
3301             }
3302         }
3303
3304         /* and read them */
3305         k=0; /* counter for the lambdas, etc. arrays */
3306         for(i=0;i<fr->nblock;i++)
3307         {
3308             if (fr->block[i].id == enxDH) 
3309             {
3310                 int type=(fr->block[i].sub[0].ival[0]);
3311                 if (type == dhbtDH || type == dhbtDHDL)
3312                 {
3313                     int ndu;
3314                     read_edr_rawdh_block(&(samples_rawdh[k]),
3315                                          &ndu,
3316                                          &(fr->block[i]),
3317                                          start_time, delta_time,
3318                                          native_lambda, rtemp,
3319                                          &last_t, fn);
3320                     npts[k] += ndu;
3321                     if (samples_rawdh[k])
3322                     {
3323                         lambdas[k]=samples_rawdh[k]->foreign_lambda;
3324                     }
3325                     k++;
3326                 }
3327             }
3328             else if (fr->block[i].id == enxDHHIST)
3329             {
3330                 int type=(int)(fr->block[i].sub[1].lval[1]);
3331                 if (type == dhbtDH || type == dhbtDHDL)
3332                 {
3333                     int j;
3334                     int nb=0;
3335                     samples_t *s; /* this is where the data will go */
3336                     s=read_edr_hist_block(&nb, &(fr->block[i]),
3337                                           start_time, delta_time,
3338                                           native_lambda, rtemp,
3339                                           &last_t, fn);
3340                     nhists[k] += nb;
3341                     if (nb>0)
3342                     {
3343                         lambdas[k]=s->foreign_lambda;
3344                     }
3345                     k++;
3346                     /* and insert the new sample immediately */
3347                     for(j=0;j<nb;j++)
3348                     {
3349                         lambda_data_list_insert_sample(sd->lb, s+j);
3350                     }
3351                 }
3352             }
3353         }
3354     }
3355     /* Now store all our extant sample collections */
3356     for(i=0;i<nsamples;i++)
3357     {
3358         if (samples_rawdh[i])
3359         {
3360             /* insert it into the existing list */
3361             lambda_data_list_insert_sample(sd->lb, samples_rawdh[i]);
3362         }
3363     }
3364
3365
3366     {
3367         char buf[STRLEN];
3368         printf("\n");
3369         lambda_vec_print(native_lambda, buf, FALSE);
3370         printf("%s: %.1f - %.1f; lambda = %s\n    foreign lambdas:\n",
3371                fn, first_t, last_t, buf);
3372         for(i=0;i<nsamples;i++)
3373         {
3374             if (lambdas[i])
3375             {
3376                 lambda_vec_print(lambdas[i], buf, TRUE);
3377                 if (nhists[i] > 0)
3378                 {
3379                     printf("        %s (%d hists)\n", buf, nhists[i]);
3380                 }
3381                 else
3382                 {
3383                     printf("        %s (%d pts)\n", buf, npts[i]);
3384                 }
3385             }
3386         }
3387     }
3388     printf("\n\n");
3389     sfree(npts);
3390     sfree(nhists);
3391     sfree(lambdas);
3392 }
3393
3394
3395 int gmx_bar(int argc,char *argv[])
3396 {
3397     static const char *desc[] = {
3398         "[TT]g_bar[tt] calculates free energy difference estimates through ",
3399         "Bennett's acceptance ratio method (BAR). It also automatically",
3400         "adds series of individual free energies obtained with BAR into",
3401         "a combined free energy estimate.[PAR]",
3402
3403         "Every individual BAR free energy difference relies on two ",
3404         "simulations at different states: say state A and state B, as",
3405         "controlled by a parameter, [GRK]lambda[grk] (see the [TT].mdp[tt] parameter",
3406         "[TT]init_lambda[tt]). The BAR method calculates a ratio of weighted",
3407         "average of the Hamiltonian difference of state B given state A and",
3408         "vice versa.",
3409         "The energy differences to the other state must be calculated",
3410         "explicitly during the simulation. This can be done with",
3411         "the [TT].mdp[tt] option [TT]foreign_lambda[tt].[PAR]",
3412
3413         "Input option [TT]-f[tt] expects multiple [TT]dhdl.xvg[tt] files. ",
3414         "Two types of input files are supported:[BR]",
3415         "[TT]*[tt]  Files with more than one [IT]y[it]-value. ",
3416         "The files should have columns ",
3417         "with dH/d[GRK]lambda[grk] and [GRK]Delta[grk][GRK]lambda[grk]. ",
3418         "The [GRK]lambda[grk] values are inferred ",
3419         "from the legends: [GRK]lambda[grk] of the simulation from the legend of ",
3420         "dH/d[GRK]lambda[grk] and the foreign [GRK]lambda[grk] values from the ",
3421         "legends of Delta H",
3422         "[BR]",
3423         "[TT]*[tt]  Files with only one [IT]y[it]-value. Using the",
3424         "[TT]-extp[tt] option for these files, it is assumed",
3425         "that the [IT]y[it]-value is dH/d[GRK]lambda[grk] and that the ",
3426         "Hamiltonian depends linearly on [GRK]lambda[grk]. ",
3427         "The [GRK]lambda[grk] value of the simulation is inferred from the ",
3428         "subtitle (if present), otherwise from a number in the subdirectory ",
3429         "in the file name.[PAR]",
3430
3431         "The [GRK]lambda[grk] of the simulation is parsed from ",
3432         "[TT]dhdl.xvg[tt] file's legend containing the string 'dH', the ",
3433         "foreign [GRK]lambda[grk] values from the legend containing the ",
3434         "capitalized letters 'D' and 'H'. The temperature is parsed from ",
3435         "the legend line containing 'T ='.[PAR]",
3436
3437         "The input option [TT]-g[tt] expects multiple [TT].edr[tt] files. ",
3438         "These can contain either lists of energy differences (see the ",
3439         "[TT].mdp[tt] option [TT]separate_dhdl_file[tt]), or a series of ",
3440         "histograms (see the [TT].mdp[tt] options [TT]dh_hist_size[tt] and ",
3441         "[TT]dh_hist_spacing[tt]).", "The temperature and [GRK]lambda[grk] ",
3442         "values are automatically deduced from the [TT]ener.edr[tt] file.[PAR]",
3443
3444         "In addition to the [TT].mdp[tt] option [TT]foreign_lambda[tt], ",
3445         "the energy difference can also be extrapolated from the ",
3446         "dH/d[GRK]lambda[grk] values. This is done with the[TT]-extp[tt]",
3447         "option, which assumes that the system's Hamiltonian depends linearly",
3448         "on [GRK]lambda[grk], which is not normally the case.[PAR]",
3449
3450         "The free energy estimates are determined using BAR with bisection, ",
3451         "with the precision of the output set with [TT]-prec[tt]. ",
3452         "An error estimate taking into account time correlations ",
3453         "is made by splitting the data into blocks and determining ",
3454         "the free energy differences over those blocks and assuming ",
3455         "the blocks are independent. ",
3456         "The final error estimate is determined from the average variance ",
3457         "over 5 blocks. A range of block numbers for error estimation can ",
3458         "be provided with the options [TT]-nbmin[tt] and [TT]-nbmax[tt].[PAR]",
3459
3460         "[TT]g_bar[tt] tries to aggregate samples with the same 'native' and ",
3461         "'foreign' [GRK]lambda[grk] values, but always assumes independent ",
3462         "samples. [BB]Note[bb] that when aggregating energy ",
3463         "differences/derivatives with different sampling intervals, this is ",
3464         "almost certainly not correct. Usually subsequent energies are ",
3465         "correlated and different time intervals mean different degrees ",
3466         "of correlation between samples.[PAR]",
3467
3468         "The results are split in two parts: the last part contains the final ",
3469         "results in kJ/mol, together with the error estimate for each part ",
3470         "and the total. The first part contains detailed free energy ",
3471         "difference estimates and phase space overlap measures in units of ",
3472         "kT (together with their computed error estimate). The printed ",
3473         "values are:[BR]",
3474         "[TT]*[tt]  lam_A: the [GRK]lambda[grk] values for point A.[BR]",
3475         "[TT]*[tt]  lam_B: the [GRK]lambda[grk] values for point B.[BR]",
3476         "[TT]*[tt]     DG: the free energy estimate.[BR]",
3477         "[TT]*[tt]    s_A: an estimate of the relative entropy of B in A.[BR]",
3478         "[TT]*[tt]    s_A: an estimate of the relative entropy of A in B.[BR]",
3479         "[TT]*[tt]  stdev: an estimate expected per-sample standard deviation.[PAR]",
3480         
3481         "The relative entropy of both states in each other's ensemble can be ",
3482         "interpreted as a measure of phase space overlap: ", 
3483         "the relative entropy s_A of the work samples of lambda_B in the ",
3484         "ensemble of lambda_A (and vice versa for s_B), is a ", 
3485         "measure of the 'distance' between Boltzmann distributions of ",
3486         "the two states, that goes to zero for identical distributions. See ",
3487         "Wu & Kofke, J. Chem. Phys. 123 084109 (2005) for more information.",
3488         "[PAR]",
3489         "The estimate of the expected per-sample standard deviation, as given ",
3490         "in Bennett's original BAR paper: Bennett, J. Comp. Phys. 22, p 245 (1976).", 
3491         "Eq. 10 therein gives an estimate of the quality of sampling (not directly",  
3492         "of the actual statistical error, because it assumes independent samples).[PAR]",
3493
3494         "To get a visual estimate of the phase space overlap, use the ",
3495         "[TT]-oh[tt] option to write series of histograms, together with the ",
3496         "[TT]-nbin[tt] option.[PAR]"
3497     };
3498     static real begin=0,end=-1,temp=-1;
3499     int nd=2,nbmin=5,nbmax=5;
3500     int nbin=100;
3501     gmx_bool use_dhdl=FALSE;
3502     gmx_bool calc_s,calc_v;
3503     t_pargs pa[] = {
3504         { "-b",    FALSE, etREAL, {&begin},  "Begin time for BAR" },
3505         { "-e",    FALSE, etREAL, {&end},    "End time for BAR" },
3506         { "-temp", FALSE, etREAL, {&temp},   "Temperature (K)" },
3507         { "-prec", FALSE, etINT,  {&nd},     "The number of digits after the decimal point" },
3508         { "-nbmin",  FALSE, etINT,  {&nbmin}, "Minimum number of blocks for error estimation" },
3509         { "-nbmax",  FALSE, etINT,  {&nbmax}, "Maximum number of blocks for error estimation" },
3510         { "-nbin",  FALSE, etINT, {&nbin}, "Number of bins for histogram output"},
3511         { "-extp",  FALSE, etBOOL, {&use_dhdl}, "Whether to linearly extrapolate dH/dl values to use as energies"}
3512     };
3513     
3514     t_filenm   fnm[] = {
3515         { efXVG, "-f",  "dhdl",   ffOPTRDMULT },
3516         { efEDR, "-g",  "ener",   ffOPTRDMULT },
3517         { efXVG, "-o",  "bar",    ffOPTWR },
3518         { efXVG, "-oi", "barint", ffOPTWR }, 
3519         { efXVG, "-oh", "histogram", ffOPTWR }
3520     };
3521 #define NFILE asize(fnm)
3522     
3523     int      f,i,j;
3524     int      nf=0; /* file counter */
3525     int      nbs;
3526     int      nfile_tot; /* total number of input files */
3527     int      nxvgfile=0;
3528     int      nedrfile=0;
3529     char     **fxvgnms;
3530     char     **fedrnms;
3531     sim_data_t sim_data; /* the simulation data */
3532     barres_t *results;  /* the results */
3533     int    nresults;  /* number of results in results array */
3534
3535     double   *partsum;
3536     double   prec,dg_tot,dg,sig, dg_tot_max, dg_tot_min;
3537     FILE     *fpb,*fpi;
3538     char     dgformat[20],xvg2format[STRLEN],xvg3format[STRLEN];
3539     char     buf[STRLEN], buf2[STRLEN];
3540     char     ktformat[STRLEN], sktformat[STRLEN];
3541     char     kteformat[STRLEN], skteformat[STRLEN];
3542     output_env_t oenv;
3543     double   kT, beta;
3544     gmx_bool     result_OK=TRUE,bEE=TRUE;
3545
3546     gmx_bool     disc_err=FALSE;
3547     double   sum_disc_err=0.; /* discretization error */
3548     gmx_bool     histrange_err=FALSE;
3549     double   sum_histrange_err=0.; /* histogram range error */
3550     double   stat_err=0.; /* statistical error */
3551     
3552     parse_common_args(&argc,argv,
3553                       PCA_CAN_VIEW,
3554                       NFILE,fnm,asize(pa),pa,asize(desc),desc,0,NULL,&oenv);
3555
3556     if (opt2bSet("-f", NFILE, fnm))
3557     {
3558         nxvgfile = opt2fns(&fxvgnms,"-f",NFILE,fnm);
3559     }
3560     if (opt2bSet("-g", NFILE, fnm))
3561     {
3562         nedrfile = opt2fns(&fedrnms,"-g",NFILE,fnm);
3563     }
3564
3565     sim_data_init(&sim_data);
3566 #if 0
3567     /* make linked list */
3568     lb=&lambda_head;
3569     lambda_data_init(lb, 0, 0);
3570     lb->next=lb;
3571     lb->prev=lb;
3572 #endif
3573
3574
3575     nfile_tot = nxvgfile + nedrfile;
3576
3577     if (nfile_tot == 0)
3578     {
3579         gmx_fatal(FARGS,"No input files!");
3580     }
3581
3582     if (nd < 0)
3583     {
3584         gmx_fatal(FARGS,"Can not have negative number of digits");
3585     }
3586     prec = pow(10,-nd);
3587
3588     snew(partsum,(nbmax+1)*(nbmax+1));
3589     nf = 0;
3590
3591     /* read in all files. First xvg files */
3592     for(f=0; f<nxvgfile; f++)
3593     {
3594         read_bar_xvg(fxvgnms[f],&temp,&sim_data);
3595         nf++;
3596     }
3597     /* then .edr files */
3598     for(f=0; f<nedrfile; f++)
3599     {
3600         read_barsim_edr(fedrnms[f],&temp,&sim_data);;
3601         nf++;
3602     }
3603
3604     /* fix the times to allow for equilibration */
3605     sim_data_impose_times(&sim_data, begin, end);
3606
3607     if (opt2bSet("-oh",NFILE,fnm))
3608     {
3609         sim_data_histogram(&sim_data, opt2fn("-oh",NFILE,fnm), nbin, oenv);
3610     }
3611    
3612     /* assemble the output structures from the lambdas */
3613     results=barres_list_create(&sim_data, &nresults, use_dhdl);
3614
3615     sum_disc_err=barres_list_max_disc_err(results, nresults);
3616
3617     if (nresults == 0)
3618     {
3619         printf("\nNo results to calculate.\n");
3620         return 0;
3621     }
3622
3623     if (sum_disc_err > prec)
3624     {
3625         prec=sum_disc_err;
3626         nd = ceil(-log10(prec));
3627         printf("WARNING: setting the precision to %g because that is the minimum\n         reasonable number, given the expected discretization error.\n", prec);
3628     }
3629
3630
3631     /*sprintf(lamformat,"%%6.3f");*/
3632     sprintf( dgformat,"%%%d.%df",3+nd,nd);
3633     /* the format strings of the results in kT */
3634     sprintf( ktformat,"%%%d.%df",5+nd,nd);
3635     sprintf( sktformat,"%%%ds",6+nd);
3636     /* the format strings of the errors in kT */
3637     sprintf( kteformat,"%%%d.%df",3+nd,nd);
3638     sprintf( skteformat,"%%%ds",4+nd);
3639     sprintf(xvg2format,"%s %s\n","%s",dgformat);
3640     sprintf(xvg3format,"%s %s %s\n","%s",dgformat,dgformat);
3641
3642
3643
3644     fpb = NULL;
3645     if (opt2bSet("-o",NFILE,fnm))
3646     {
3647         sprintf(buf,"%s (%s)","\\DeltaG","kT");
3648         fpb = xvgropen_type(opt2fn("-o",NFILE,fnm),"Free energy differences",
3649                             "\\lambda",buf,exvggtXYDY,oenv);
3650     }
3651
3652     fpi = NULL;
3653     if (opt2bSet("-oi",NFILE,fnm))
3654     {
3655         sprintf(buf,"%s (%s)","\\DeltaG","kT");
3656         fpi = xvgropen(opt2fn("-oi",NFILE,fnm),"Free energy integral",
3657                       "\\lambda",buf,oenv);
3658     }
3659
3660
3661
3662     if (nbmin > nbmax)
3663         nbmin=nbmax;
3664
3665     /* first calculate results */
3666     bEE = TRUE;
3667     disc_err = FALSE;
3668     for(f=0; f<nresults; f++)
3669     {
3670         /* Determine the free energy difference with a factor of 10
3671          * more accuracy than requested for printing.
3672          */
3673         calc_bar(&(results[f]), 0.1*prec, nbmin, nbmax,
3674                  &bEE, partsum);
3675
3676         if (results[f].dg_disc_err > prec/10.)
3677             disc_err=TRUE;
3678         if (results[f].dg_histrange_err > prec/10.)
3679             histrange_err=TRUE;
3680     }
3681
3682     /* print results in kT */
3683     kT   = BOLTZ*temp;
3684     beta = 1/kT;
3685
3686     printf("\nTemperature: %g K\n", temp);
3687
3688     printf("\nDetailed results in kT (see help for explanation):\n\n");
3689     printf("%6s ", " lam_A");
3690     printf("%6s ", " lam_B");
3691     printf(sktformat,  "DG ");
3692     if (bEE)
3693         printf(skteformat, "+/- ");
3694     if (disc_err)
3695         printf(skteformat, "disc ");
3696     if (histrange_err)
3697         printf(skteformat, "range ");
3698     printf(sktformat,  "s_A ");
3699     if (bEE)
3700         printf(skteformat, "+/- " );
3701     printf(sktformat,  "s_B ");
3702     if (bEE)
3703         printf(skteformat, "+/- " );
3704     printf(sktformat,  "stdev ");
3705     if (bEE)
3706         printf(skteformat, "+/- ");
3707     printf("\n");
3708     for(f=0; f<nresults; f++)
3709     {
3710         lambda_vec_print_short(results[f].a->native_lambda, buf);
3711         printf("%s ", buf);
3712         lambda_vec_print_short(results[f].b->native_lambda, buf);
3713         printf("%s ", buf);
3714         printf(ktformat,  results[f].dg);
3715         printf(" ");
3716         if (bEE)
3717         {
3718             printf(kteformat, results[f].dg_err);
3719             printf(" ");
3720         }
3721         if (disc_err)
3722         {
3723             printf(kteformat, results[f].dg_disc_err);
3724             printf(" ");
3725         }
3726         if (histrange_err)
3727         {
3728             printf(kteformat, results[f].dg_histrange_err);
3729             printf(" ");
3730         }
3731         printf(ktformat,  results[f].sa);
3732         printf(" ");
3733         if (bEE)
3734         {
3735             printf(kteformat, results[f].sa_err);
3736             printf(" ");
3737         }
3738         printf(ktformat,  results[f].sb);
3739         printf(" ");
3740         if (bEE)
3741         {
3742             printf(kteformat, results[f].sb_err);
3743             printf(" ");
3744         }
3745         printf(ktformat,  results[f].dg_stddev);
3746         printf(" ");
3747         if (bEE)
3748         {
3749             printf(kteformat, results[f].dg_stddev_err);
3750         }
3751         printf("\n");
3752
3753         /* Check for negative relative entropy with a 95% certainty. */
3754         if (results[f].sa < -2*results[f].sa_err ||
3755             results[f].sb < -2*results[f].sb_err)
3756         {
3757             result_OK=FALSE;
3758         }
3759     }
3760
3761     if (!result_OK)
3762     {
3763         printf("\nWARNING: Some of these results violate the Second Law of "
3764                "Thermodynamics: \n"
3765                "         This is can be the result of severe undersampling, or "
3766                "(more likely)\n" 
3767                "         there is something wrong with the simulations.\n");
3768     }
3769  
3770
3771     /* final results in kJ/mol */
3772     printf("\n\nFinal results in kJ/mol:\n\n");
3773     dg_tot  = 0;
3774     for(f=0; f<nresults; f++)
3775     {
3776         
3777         if (fpi != NULL)
3778         {
3779             lambda_vec_print_short(results[f].a->native_lambda, buf);
3780             fprintf(fpi, xvg2format, buf, dg_tot);
3781         }
3782
3783
3784         if (fpb != NULL)
3785         {
3786             lambda_vec_print_intermediate(results[f].a->native_lambda,
3787                                           results[f].b->native_lambda,
3788                                           buf);
3789
3790             fprintf(fpb, xvg3format, buf, results[f].dg,results[f].dg_err);
3791         }
3792
3793         printf("point ");
3794         lambda_vec_print_short(results[f].a->native_lambda, buf);
3795         lambda_vec_print_short(results[f].b->native_lambda, buf2);
3796         printf("%s - %s", buf, buf2);
3797         printf(",   DG ");
3798
3799         printf(dgformat,results[f].dg*kT);
3800         if (bEE)
3801         {
3802             printf(" +/- ");
3803             printf(dgformat,results[f].dg_err*kT);
3804         }
3805         if (histrange_err)
3806         {
3807             printf(" (max. range err. = ");
3808             printf(dgformat,results[f].dg_histrange_err*kT);
3809             printf(")");
3810             sum_histrange_err += results[f].dg_histrange_err*kT;
3811         }
3812
3813         printf("\n");
3814         dg_tot += results[f].dg;
3815     }
3816     printf("\n");
3817     printf("total ");
3818     lambda_vec_print_short(results[0].a->native_lambda, buf);
3819     lambda_vec_print_short(results[nresults-1].b->native_lambda, buf2);
3820     printf("%s - %s", buf, buf2);
3821     printf(",   DG ");
3822
3823     printf(dgformat,dg_tot*kT);
3824     if (bEE)
3825     {
3826         stat_err=bar_err(nbmin,nbmax,partsum)*kT;
3827         printf(" +/- ");
3828         printf(dgformat, max(max(stat_err, sum_disc_err), sum_histrange_err));
3829     }
3830     printf("\n");
3831     if (disc_err)
3832     {
3833         printf("\nmaximum discretization error = ");
3834         printf(dgformat,sum_disc_err);
3835         if (bEE && stat_err < sum_disc_err)
3836         {
3837             printf("WARNING: discretization error (%g) is larger than statistical error.\n       Decrease histogram spacing for more accurate results\n", stat_err);
3838         }
3839     }
3840     if (histrange_err)
3841     {
3842         printf("\nmaximum histogram range error = ");
3843         printf(dgformat,sum_histrange_err);
3844         if (bEE && stat_err < sum_histrange_err)
3845         {
3846             printf("WARNING: histogram range error (%g) is larger than statistical error.\n       Increase histogram range for more accurate results\n", stat_err);
3847         }
3848
3849     }
3850     printf("\n");
3851
3852
3853     if (fpi != NULL)
3854     {
3855         lambda_vec_print_short(results[nresults-1].b->native_lambda, buf);
3856         fprintf(fpi, xvg2format, buf, dg_tot);
3857         ffclose(fpi);
3858     }
3859
3860     do_view(oenv,opt2fn_null("-o",NFILE,fnm),"-xydy");
3861     do_view(oenv,opt2fn_null("-oi",NFILE,fnm),"-xydy");
3862     
3863     thanx(stderr);
3864     
3865     return 0;
3866 }