2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team,
6 * check out http://www.gromacs.org for more information.
7 * Copyright (c) 2012,2013, by the GROMACS development team, led by
8 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9 * others, as listed in the AUTHORS file in the top-level source
10 * directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
56 #include "gmx_fatal.h"
63 /* Suppress Cygwin compiler warnings from using newlib version of
70 /* Structure for the names of lambda vector components */
71 typedef struct lambda_components_t
73 char **names; /* Array of strings with names for the lambda vector
75 int N; /* The number of components */
76 int Nalloc; /* The number of allocated components */
77 } lambda_components_t;
79 /* Structure for a lambda vector or a dhdl derivative direction */
80 typedef struct lambda_vec_t
82 double *val; /* The lambda vector component values. Only valid if
84 int dhdl; /* The coordinate index for the derivative described by this
86 const lambda_components_t *lc; /* the associated lambda_components
88 int index; /* The state number (init-lambda-state) of this lambda
89 vector, if known. If not, it is set to -1 */
92 /* the dhdl.xvg data from a simulation */
96 int ftp; /* file type */
97 int nset; /* number of lambdas, including dhdl */
98 int *np; /* number of data points (du or hists) per lambda */
99 int np_alloc; /* number of points (du or hists) allocated */
100 double temp; /* temperature */
101 lambda_vec_t *lambda; /* the lambdas (of first index for y). */
102 double *t; /* the times (of second index for y) */
103 double **y; /* the dU values. y[0] holds the derivative, while
104 further ones contain the energy differences between
105 the native lambda and the 'foreign' lambdas. */
106 lambda_vec_t native_lambda; /* the native lambda */
108 struct xvg_t *next, *prev; /*location in the global linked list of xvg_ts*/
112 typedef struct hist_t
114 unsigned int *bin[2]; /* the (forward + reverse) histogram values */
115 double dx[2]; /* the histogram spacing. The reverse
116 dx is the negative of the forward dx.*/
117 gmx_large_int_t x0[2]; /* the (forward + reverse) histogram start
120 int nbin[2]; /* the (forward+reverse) number of bins */
121 gmx_large_int_t sum; /* the total number of counts. Must be
122 the same for forward + reverse. */
123 int nhist; /* number of hist datas (forward or reverse) */
125 double start_time, delta_time; /* start time, end time of histogram */
129 /* an aggregate of samples for partial free energy calculation */
130 typedef struct samples_t
132 lambda_vec_t *native_lambda; /* pointer to native lambda vector */
133 lambda_vec_t *foreign_lambda; /* pointer to foreign lambda vector */
134 double temp; /* the temperature */
135 gmx_bool derivative; /* whether this sample is a derivative */
137 /* The samples come either as either delta U lists: */
138 int ndu; /* the number of delta U samples */
139 double *du; /* the delta u's */
140 double *t; /* the times associated with those samples, or: */
141 double start_time, delta_time; /*start time and delta time for linear time*/
143 /* or as histograms: */
144 hist_t *hist; /* a histogram */
146 /* allocation data: (not NULL for data 'owned' by this struct) */
147 double *du_alloc, *t_alloc; /* allocated delta u arrays */
148 size_t ndu_alloc, nt_alloc; /* pre-allocated sizes */
149 hist_t *hist_alloc; /* allocated hist */
151 gmx_large_int_t ntot; /* total number of samples */
152 const char *filename; /* the file name this sample comes from */
155 /* a sample range (start to end for du-style data, or boolean
156 for both du-style data and histograms */
157 typedef struct sample_range_t
159 int start, end; /* start and end index for du style data */
160 gmx_bool use; /* whether to use this sample */
162 samples_t *s; /* the samples this range belongs to */
166 /* a collection of samples for a partial free energy calculation
167 (i.e. the collection of samples from one native lambda to one
169 typedef struct sample_coll_t
171 lambda_vec_t *native_lambda; /* these should be the same for all samples
173 lambda_vec_t *foreign_lambda; /* collection */
174 double temp; /* the temperature */
176 int nsamples; /* the number of samples */
177 samples_t **s; /* the samples themselves */
178 sample_range_t *r; /* the sample ranges */
179 int nsamples_alloc; /* number of allocated samples */
181 gmx_large_int_t ntot; /* total number of samples in the ranges of
184 struct sample_coll_t *next, *prev; /* next and previous in the list */
187 /* all the samples associated with a lambda point */
188 typedef struct lambda_data_t
190 lambda_vec_t *lambda; /* the native lambda (at start time if dynamic) */
191 double temp; /* temperature */
193 sample_coll_t *sc; /* the samples */
195 sample_coll_t sc_head; /*the pre-allocated list head for the linked list.*/
197 struct lambda_data_t *next, *prev; /* the next and prev in the list */
200 /* Top-level data structure of simulation data */
201 typedef struct sim_data_t
203 lambda_data_t *lb; /* a lambda data linked list */
204 lambda_data_t lb_head; /* The head element of the linked list */
206 lambda_components_t lc; /* the allowed components of the lambda
210 /* Top-level data structure with calculated values. */
212 sample_coll_t *a, *b; /* the simulation data */
214 double dg; /* the free energy difference */
215 double dg_err; /* the free energy difference */
217 double dg_disc_err; /* discretization error */
218 double dg_histrange_err; /* histogram range error */
220 double sa; /* relative entropy of b in state a */
221 double sa_err; /* error in sa */
222 double sb; /* relative entropy of a in state b */
223 double sb_err; /* error in sb */
225 double dg_stddev; /* expected dg stddev per sample */
226 double dg_stddev_err; /* error in dg_stddev */
230 /* Initialize a lambda_components structure */
231 static void lambda_components_init(lambda_components_t *lc)
235 snew(lc->names, lc->Nalloc);
238 /* Add a component to a lambda_components structure */
239 static void lambda_components_add(lambda_components_t *lc,
240 const char *name, size_t name_length)
242 while (lc->N + 1 > lc->Nalloc)
244 lc->Nalloc = (lc->Nalloc == 0) ? 2 : 2*lc->Nalloc;
245 srealloc( lc->names, lc->Nalloc );
247 snew(lc->names[lc->N], name_length+1);
248 strncpy(lc->names[lc->N], name, name_length);
252 /* check whether a component with index 'index' matches the given name, or
253 is also NULL. Returns TRUE if this is the case.
254 the string name does not need to end */
255 static gmx_bool lambda_components_check(const lambda_components_t *lc,
265 if (name == NULL && lc->names[index] == NULL)
269 if ((name == NULL) != (lc->names[index] == NULL))
273 len = strlen(lc->names[index]);
274 if (len != name_length)
278 if (strncmp(lc->names[index], name, name_length) == 0)
285 /* Find the index of a given lambda component name, or -1 if not found */
286 static int lambda_components_find(const lambda_components_t *lc,
292 for (i = 0; i < lc->N; i++)
294 if (strncmp(lc->names[i], name, name_length) == 0)
304 /* initialize a lambda vector */
305 static void lambda_vec_init(lambda_vec_t *lv, const lambda_components_t *lc)
307 snew(lv->val, lc->N);
313 static void lambda_vec_destroy(lambda_vec_t *lv)
318 static void lambda_vec_copy(lambda_vec_t *lv, const lambda_vec_t *orig)
322 lambda_vec_init(lv, orig->lc);
323 lv->dhdl = orig->dhdl;
324 lv->index = orig->index;
325 for (i = 0; i < lv->lc->N; i++)
327 lv->val[i] = orig->val[i];
331 /* write a lambda vec to a preallocated string */
332 static void lambda_vec_print(const lambda_vec_t *lv, char *str, gmx_bool named)
337 str[0] = 0; /* reset the string */
342 str += sprintf(str, "delta H to ");
346 str += sprintf(str, "(");
348 for (i = 0; i < lv->lc->N; i++)
350 str += sprintf(str, "%g", lv->val[i]);
353 str += sprintf(str, ", ");
358 str += sprintf(str, ")");
363 /* this lambda vector describes a derivative */
364 str += sprintf(str, "dH/dl");
365 if (strlen(lv->lc->names[lv->dhdl]) > 0)
367 str += sprintf(str, " (%s)", lv->lc->names[lv->dhdl]);
372 /* write a shortened version of the lambda vec to a preallocated string */
373 static void lambda_vec_print_short(const lambda_vec_t *lv, char *str)
380 sprintf(str, "%6d", lv->index);
386 sprintf(str, "%6.3f", lv->val[0]);
390 sprintf(str, "dH/dl[%d]", lv->dhdl);
395 /* write an intermediate version of two lambda vecs to a preallocated string */
396 static void lambda_vec_print_intermediate(const lambda_vec_t *a,
397 const lambda_vec_t *b, char *str)
403 if ( (a->index >= 0) && (b->index >= 0) )
405 sprintf(str, "%6.3f", ((double)a->index+(double)b->index)/2.);
409 if ( (a->dhdl < 0) && (b->dhdl < 0) )
411 sprintf(str, "%6.3f", (a->val[0]+b->val[0])/2.);
418 /* calculate the difference in lambda vectors: c = a-b.
419 c must be initialized already, and a and b must describe non-derivative
421 static void lambda_vec_diff(const lambda_vec_t *a, const lambda_vec_t *b,
426 if ( (a->dhdl > 0) || (b->dhdl > 0) )
429 "Trying to calculate the difference between derivatives instead of lambda points");
431 if ((a->lc != b->lc) || (a->lc != c->lc) )
434 "Trying to calculate the difference lambdas with differing basis set");
436 for (i = 0; i < a->lc->N; i++)
438 c->val[i] = a->val[i] - b->val[i];
442 /* calculate and return the absolute difference in lambda vectors: c = |a-b|.
443 a and b must describe non-derivative lambda points */
444 static double lambda_vec_abs_diff(const lambda_vec_t *a, const lambda_vec_t *b)
449 if ( (a->dhdl > 0) || (b->dhdl > 0) )
452 "Trying to calculate the difference between derivatives instead of lambda points");
457 "Trying to calculate the difference lambdas with differing basis set");
459 for (i = 0; i < a->lc->N; i++)
461 double df = a->val[i] - b->val[i];
468 /* check whether two lambda vectors are the same */
469 static gmx_bool lambda_vec_same(const lambda_vec_t *a, const lambda_vec_t *b)
479 for (i = 0; i < a->lc->N; i++)
481 if (!gmx_within_tol(a->val[i], b->val[i], 10*GMX_REAL_EPS))
490 /* they're derivatives, so we check whether the indices match */
491 return (a->dhdl == b->dhdl);
495 /* Compare the sort order of two foreign lambda vectors
497 returns 1 if a is 'bigger' than b,
498 returns 0 if they're the same,
499 returns -1 if a is 'smaller' than b.*/
500 static gmx_bool lambda_vec_cmp_foreign(const lambda_vec_t *a,
501 const lambda_vec_t *b)
504 double norm_a = 0, norm_b = 0;
505 gmx_bool different = FALSE;
509 gmx_fatal(FARGS, "Can't compare lambdas with differing basis sets");
511 /* if either one has an index we sort based on that */
512 if ((a->index >= 0) || (b->index >= 0))
514 if (a->index == b->index)
518 return (a->index > b->index) ? 1 : -1;
520 if (a->dhdl >= 0 || b->dhdl >= 0)
522 /* lambda vectors that are derivatives always sort higher than those
523 without derivatives */
524 if ((a->dhdl >= 0) != (b->dhdl >= 0) )
526 return (a->dhdl >= 0) ? 1 : -1;
528 return a->dhdl > b->dhdl;
531 /* neither has an index, so we can only sort on the lambda components,
532 which is only valid if there is one component */
533 for (i = 0; i < a->lc->N; i++)
535 if (!gmx_within_tol(a->val[i], b->val[i], 10*GMX_REAL_EPS))
539 norm_a += a->val[i]*a->val[i];
540 norm_b += b->val[i]*b->val[i];
546 return norm_a > norm_b;
549 /* Compare the sort order of two native lambda vectors
551 returns 1 if a is 'bigger' than b,
552 returns 0 if they're the same,
553 returns -1 if a is 'smaller' than b.*/
554 static gmx_bool lambda_vec_cmp_native(const lambda_vec_t *a,
555 const lambda_vec_t *b)
561 gmx_fatal(FARGS, "Can't compare lambdas with differing basis sets");
563 /* if either one has an index we sort based on that */
564 if ((a->index >= 0) || (b->index >= 0))
566 if (a->index == b->index)
570 return (a->index > b->index) ? 1 : -1;
572 /* neither has an index, so we can only sort on the lambda components,
573 which is only valid if there is one component */
577 "Can't compare lambdas with no index and > 1 component");
579 if (a->dhdl >= 0 || b->dhdl >= 0)
582 "Can't compare native lambdas that are derivatives");
584 if (gmx_within_tol(a->val[0], b->val[0], 10*GMX_REAL_EPS))
588 return a->val[0] > b->val[0] ? 1 : -1;
594 static void hist_init(hist_t *h, int nhist, int *nbin)
599 gmx_fatal(FARGS, "histogram with more than two sets of data!");
601 for (i = 0; i < nhist; i++)
603 snew(h->bin[i], nbin[i]);
605 h->nbin[i] = nbin[i];
606 h->start_time = h->delta_time = 0;
613 static void hist_destroy(hist_t *h)
619 static void xvg_init(xvg_t *ba)
628 static void samples_init(samples_t *s, lambda_vec_t *native_lambda,
629 lambda_vec_t *foreign_lambda, double temp,
630 gmx_bool derivative, const char *filename)
632 s->native_lambda = native_lambda;
633 s->foreign_lambda = foreign_lambda;
635 s->derivative = derivative;
640 s->start_time = s->delta_time = 0;
644 s->hist_alloc = NULL;
649 s->filename = filename;
652 static void sample_range_init(sample_range_t *r, samples_t *s)
660 static void sample_coll_init(sample_coll_t *sc, lambda_vec_t *native_lambda,
661 lambda_vec_t *foreign_lambda, double temp)
663 sc->native_lambda = native_lambda;
664 sc->foreign_lambda = foreign_lambda;
670 sc->nsamples_alloc = 0;
673 sc->next = sc->prev = NULL;
676 static void sample_coll_destroy(sample_coll_t *sc)
678 /* don't free the samples themselves */
684 static void lambda_data_init(lambda_data_t *l, lambda_vec_t *native_lambda,
687 l->lambda = native_lambda;
693 l->sc = &(l->sc_head);
695 sample_coll_init(l->sc, native_lambda, NULL, 0.);
700 static void barres_init(barres_t *br)
709 br->dg_stddev_err = 0;
716 /* calculate the total number of samples in a sample collection */
717 static void sample_coll_calc_ntot(sample_coll_t *sc)
722 for (i = 0; i < sc->nsamples; i++)
728 sc->ntot += sc->s[i]->ntot;
732 sc->ntot += sc->r[i].end - sc->r[i].start;
739 /* find the barsamples_t associated with a lambda that corresponds to
740 a specific foreign lambda */
741 static sample_coll_t *lambda_data_find_sample_coll(lambda_data_t *l,
742 lambda_vec_t *foreign_lambda)
744 sample_coll_t *sc = l->sc->next;
748 if (lambda_vec_same(sc->foreign_lambda, foreign_lambda))
758 /* insert li into an ordered list of lambda_colls */
759 static void lambda_data_insert_sample_coll(lambda_data_t *l, sample_coll_t *sc)
761 sample_coll_t *scn = l->sc->next;
762 while ( (scn != l->sc) )
764 if (lambda_vec_cmp_foreign(scn->foreign_lambda, sc->foreign_lambda) > 0)
770 /* now insert it before the found scn */
772 sc->prev = scn->prev;
773 scn->prev->next = sc;
777 /* insert li into an ordered list of lambdas */
778 static void lambda_data_insert_lambda(lambda_data_t *head, lambda_data_t *li)
780 lambda_data_t *lc = head->next;
783 if (lambda_vec_cmp_native(lc->lambda, li->lambda) > 0)
789 /* now insert ourselves before the found lc */
796 /* insert a sample and a sample_range into a sample_coll. The
797 samples are stored as a pointer, the range is copied. */
798 static void sample_coll_insert_sample(sample_coll_t *sc, samples_t *s,
801 /* first check if it belongs here */
802 if (sc->temp != s->temp)
804 gmx_fatal(FARGS, "Temperatures in files %s and %s are not the same!",
805 s->filename, sc->next->s[0]->filename);
807 if (!lambda_vec_same(sc->native_lambda, s->native_lambda))
809 gmx_fatal(FARGS, "Native lambda in files %s and %s are not the same (and they should be)!",
810 s->filename, sc->next->s[0]->filename);
812 if (!lambda_vec_same(sc->foreign_lambda, s->foreign_lambda))
814 gmx_fatal(FARGS, "Foreign lambda in files %s and %s are not the same (and they should be)!",
815 s->filename, sc->next->s[0]->filename);
818 /* check if there's room */
819 if ( (sc->nsamples + 1) > sc->nsamples_alloc)
821 sc->nsamples_alloc = max(2*sc->nsamples_alloc, 2);
822 srenew(sc->s, sc->nsamples_alloc);
823 srenew(sc->r, sc->nsamples_alloc);
825 sc->s[sc->nsamples] = s;
826 sc->r[sc->nsamples] = *r;
829 sample_coll_calc_ntot(sc);
832 /* insert a sample into a lambda_list, creating the right sample_coll if
834 static void lambda_data_list_insert_sample(lambda_data_t *head, samples_t *s)
836 gmx_bool found = FALSE;
840 lambda_data_t *l = head->next;
842 /* first search for the right lambda_data_t */
845 if (lambda_vec_same(l->lambda, s->native_lambda) )
855 snew(l, 1); /* allocate a new one */
856 lambda_data_init(l, s->native_lambda, s->temp); /* initialize it */
857 lambda_data_insert_lambda(head, l); /* add it to the list */
860 /* now look for a sample collection */
861 sc = lambda_data_find_sample_coll(l, s->foreign_lambda);
864 snew(sc, 1); /* allocate a new one */
865 sample_coll_init(sc, s->native_lambda, s->foreign_lambda, s->temp);
866 lambda_data_insert_sample_coll(l, sc);
869 /* now insert the samples into the sample coll */
870 sample_range_init(&r, s);
871 sample_coll_insert_sample(sc, s, &r);
875 /* make a histogram out of a sample collection */
876 static void sample_coll_make_hist(sample_coll_t *sc, int **bin,
877 int *nbin_alloc, int *nbin,
878 double *dx, double *xmin, int nbin_default)
881 gmx_bool dx_set = FALSE;
882 gmx_bool xmin_set = FALSE;
884 gmx_bool xmax_set = FALSE;
885 gmx_bool xmax_set_hard = FALSE; /* whether the xmax is bounded by the
886 limits of a histogram */
889 /* first determine dx and xmin; try the histograms */
890 for (i = 0; i < sc->nsamples; i++)
894 hist_t *hist = sc->s[i]->hist;
895 for (k = 0; k < hist->nhist; k++)
897 double hdx = hist->dx[k];
898 double xmax_now = (hist->x0[k]+hist->nbin[k])*hdx;
900 /* we use the biggest dx*/
901 if ( (!dx_set) || hist->dx[0] > *dx)
906 if ( (!xmin_set) || (hist->x0[k]*hdx) < *xmin)
909 *xmin = (hist->x0[k]*hdx);
912 if ( (!xmax_set) || (xmax_now > xmax && !xmax_set_hard) )
916 if (hist->bin[k][hist->nbin[k]-1] != 0)
918 xmax_set_hard = TRUE;
921 if (hist->bin[k][hist->nbin[k]-1] != 0 && (xmax_now < xmax) )
923 xmax_set_hard = TRUE;
929 /* and the delta us */
930 for (i = 0; i < sc->nsamples; i++)
932 if (sc->s[i]->ndu > 0)
934 /* determine min and max */
935 int starti = sc->r[i].start;
936 int endi = sc->r[i].end;
937 double du_xmin = sc->s[i]->du[starti];
938 double du_xmax = sc->s[i]->du[starti];
939 for (j = starti+1; j < endi; j++)
941 if (sc->s[i]->du[j] < du_xmin)
943 du_xmin = sc->s[i]->du[j];
945 if (sc->s[i]->du[j] > du_xmax)
947 du_xmax = sc->s[i]->du[j];
951 /* and now change the limits */
952 if ( (!xmin_set) || (du_xmin < *xmin) )
957 if ( (!xmax_set) || ((du_xmax > xmax) && !xmax_set_hard) )
965 if (!xmax_set || !xmin_set)
974 *nbin = nbin_default;
975 *dx = (xmax-(*xmin))/((*nbin)-2); /* -2 because we want the last bin to
976 be 0, and we count from 0 */
980 *nbin = (xmax-(*xmin))/(*dx);
983 if (*nbin > *nbin_alloc)
986 srenew(*bin, *nbin_alloc);
989 /* reset the histogram */
990 for (i = 0; i < (*nbin); i++)
995 /* now add the actual data */
996 for (i = 0; i < sc->nsamples; i++)
1000 hist_t *hist = sc->s[i]->hist;
1001 for (k = 0; k < hist->nhist; k++)
1003 double hdx = hist->dx[k];
1004 double xmin_hist = hist->x0[k]*hdx;
1005 for (j = 0; j < hist->nbin[k]; j++)
1007 /* calculate the bin corresponding to the middle of the
1009 double x = hdx*(j+0.5) + xmin_hist;
1010 int binnr = (int)((x-(*xmin))/(*dx));
1012 if (binnr >= *nbin || binnr < 0)
1017 (*bin)[binnr] += hist->bin[k][j];
1023 int starti = sc->r[i].start;
1024 int endi = sc->r[i].end;
1025 for (j = starti; j < endi; j++)
1027 int binnr = (int)((sc->s[i]->du[j]-(*xmin))/(*dx));
1028 if (binnr >= *nbin || binnr < 0)
1039 /* write a collection of histograms to a file */
1040 void sim_data_histogram(sim_data_t *sd, const char *filename,
1041 int nbin_default, const output_env_t oenv)
1043 char label_x[STRLEN];
1044 const char *dhdl = "dH/d\\lambda", *deltag = "\\DeltaH", *lambda = "\\lambda";
1045 const char *title = "N(\\DeltaH)";
1046 const char *label_y = "Samples";
1050 char **setnames = NULL;
1051 gmx_bool first_set = FALSE;
1052 /* histogram data: */
1059 lambda_data_t *bl_head = sd->lb;
1061 printf("\nWriting histogram to %s\n", filename);
1062 sprintf(label_x, "\\DeltaH (%s)", unit_energy);
1064 fp = xvgropen_type(filename, title, label_x, label_y, exvggtXNY, oenv);
1066 /* first get all the set names */
1068 /* iterate over all lambdas */
1069 while (bl != bl_head)
1071 sample_coll_t *sc = bl->sc->next;
1073 /* iterate over all samples */
1074 while (sc != bl->sc)
1076 char buf[STRLEN], buf2[STRLEN];
1079 srenew(setnames, nsets);
1080 snew(setnames[nsets-1], STRLEN);
1081 if (sc->foreign_lambda->dhdl < 0)
1083 lambda_vec_print(sc->native_lambda, buf, FALSE);
1084 lambda_vec_print(sc->foreign_lambda, buf2, FALSE);
1085 sprintf(setnames[nsets-1], "N(%s(%s=%s) | %s=%s)",
1086 deltag, lambda, buf2, lambda, buf);
1090 lambda_vec_print(sc->native_lambda, buf, FALSE);
1091 sprintf(setnames[nsets-1], "N(%s | %s=%s)",
1099 xvgr_legend(fp, nsets, (const char**)setnames, oenv);
1102 /* now make the histograms */
1104 /* iterate over all lambdas */
1105 while (bl != bl_head)
1107 sample_coll_t *sc = bl->sc->next;
1109 /* iterate over all samples */
1110 while (sc != bl->sc)
1114 xvgr_new_dataset(fp, 0, 0, NULL, oenv);
1117 sample_coll_make_hist(sc, &hist, &nbin_alloc, &nbin, &dx, &min,
1120 for (i = 0; i < nbin; i++)
1122 double xmin = i*dx + min;
1123 double xmax = (i+1)*dx + min;
1125 fprintf(fp, "%g %d\n%g %d\n", xmin, hist[i], xmax, hist[i]);
1143 /* create a collection (array) of barres_t object given a ordered linked list
1144 of barlamda_t sample collections */
1145 static barres_t *barres_list_create(sim_data_t *sd, int *nres,
1152 gmx_bool dhdl = FALSE;
1153 gmx_bool first = TRUE;
1154 lambda_data_t *bl_head = sd->lb;
1156 /* first count the lambdas */
1158 while (bl != bl_head)
1163 snew(res, nlambda-1);
1165 /* next put the right samples in the res */
1167 bl = bl_head->next->next; /* we start with the second one. */
1168 while (bl != bl_head)
1170 sample_coll_t *sc, *scprev;
1171 barres_t *br = &(res[*nres]);
1172 /* there is always a previous one. we search for that as a foreign
1174 scprev = lambda_data_find_sample_coll(bl->prev, bl->lambda);
1175 sc = lambda_data_find_sample_coll(bl, bl->prev->lambda);
1183 scprev = lambda_data_find_sample_coll(bl->prev, bl->prev->lambda);
1184 sc = lambda_data_find_sample_coll(bl, bl->lambda);
1188 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");
1193 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");
1196 else if (!scprev && !sc)
1198 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);
1201 /* normal delta H */
1204 gmx_fatal(FARGS, "Could not find a set for foreign lambda = %g\nin the files for lambda = %g", bl->lambda, bl->prev->lambda);
1208 gmx_fatal(FARGS, "Could not find a set for foreign lambda = %g\nin the files for lambda = %g", bl->prev->lambda, bl->lambda);
1220 /* estimate the maximum discretization error */
1221 static double barres_list_max_disc_err(barres_t *res, int nres)
1224 double disc_err = 0.;
1225 double delta_lambda;
1227 for (i = 0; i < nres; i++)
1229 barres_t *br = &(res[i]);
1231 delta_lambda = lambda_vec_abs_diff(br->b->native_lambda,
1232 br->a->native_lambda);
1234 for (j = 0; j < br->a->nsamples; j++)
1236 if (br->a->s[j]->hist)
1239 if (br->a->s[j]->derivative)
1241 Wfac = delta_lambda;
1244 disc_err = max(disc_err, Wfac*br->a->s[j]->hist->dx[0]);
1247 for (j = 0; j < br->b->nsamples; j++)
1249 if (br->b->s[j]->hist)
1252 if (br->b->s[j]->derivative)
1254 Wfac = delta_lambda;
1256 disc_err = max(disc_err, Wfac*br->b->s[j]->hist->dx[0]);
1264 /* impose start and end times on a sample collection, updating sample_ranges */
1265 static void sample_coll_impose_times(sample_coll_t *sc, double begin_t,
1269 for (i = 0; i < sc->nsamples; i++)
1271 samples_t *s = sc->s[i];
1272 sample_range_t *r = &(sc->r[i]);
1275 double end_time = s->hist->delta_time*s->hist->sum +
1276 s->hist->start_time;
1277 if (s->hist->start_time < begin_t || end_time > end_t)
1287 if (s->start_time < begin_t)
1289 r->start = (int)((begin_t - s->start_time)/s->delta_time);
1291 end_time = s->delta_time*s->ndu + s->start_time;
1292 if (end_time > end_t)
1294 r->end = (int)((end_t - s->start_time)/s->delta_time);
1300 for (j = 0; j < s->ndu; j++)
1302 if (s->t[j] < begin_t)
1307 if (s->t[j] >= end_t)
1314 if (r->start > r->end)
1320 sample_coll_calc_ntot(sc);
1323 static void sim_data_impose_times(sim_data_t *sd, double begin, double end)
1325 double first_t, last_t;
1326 double begin_t, end_t;
1328 lambda_data_t *head = sd->lb;
1331 if (begin <= 0 && end < 0)
1336 /* first determine the global start and end times */
1342 sample_coll_t *sc = lc->sc->next;
1343 while (sc != lc->sc)
1345 for (j = 0; j < sc->nsamples; j++)
1347 double start_t, end_t;
1349 start_t = sc->s[j]->start_time;
1350 end_t = sc->s[j]->start_time;
1353 end_t += sc->s[j]->delta_time*sc->s[j]->hist->sum;
1359 end_t = sc->s[j]->t[sc->s[j]->ndu-1];
1363 end_t += sc->s[j]->delta_time*sc->s[j]->ndu;
1367 if (start_t < first_t || first_t < 0)
1381 /* calculate the actual times */
1399 printf("\n Samples in time interval: %.3f - %.3f\n", first_t, last_t);
1401 if (begin_t > end_t)
1405 printf("Removing samples outside of: %.3f - %.3f\n", begin_t, end_t);
1407 /* then impose them */
1411 sample_coll_t *sc = lc->sc->next;
1412 while (sc != lc->sc)
1414 sample_coll_impose_times(sc, begin_t, end_t);
1422 /* create subsample i out of ni from an existing sample_coll */
1423 static gmx_bool sample_coll_create_subsample(sample_coll_t *sc,
1424 sample_coll_t *sc_orig,
1428 int hist_start, hist_end;
1430 gmx_large_int_t ntot_start;
1431 gmx_large_int_t ntot_end;
1432 gmx_large_int_t ntot_so_far;
1434 *sc = *sc_orig; /* just copy all fields */
1436 /* allocate proprietary memory */
1437 snew(sc->s, sc_orig->nsamples);
1438 snew(sc->r, sc_orig->nsamples);
1440 /* copy the samples */
1441 for (j = 0; j < sc_orig->nsamples; j++)
1443 sc->s[j] = sc_orig->s[j];
1444 sc->r[j] = sc_orig->r[j]; /* copy the ranges too */
1447 /* now fix start and end fields */
1448 /* the casts avoid possible overflows */
1449 ntot_start = (gmx_large_int_t)(sc_orig->ntot*(double)i/(double)ni);
1450 ntot_end = (gmx_large_int_t)(sc_orig->ntot*(double)(i+1)/(double)ni);
1452 for (j = 0; j < sc->nsamples; j++)
1454 gmx_large_int_t ntot_add;
1455 gmx_large_int_t new_start, new_end;
1461 ntot_add = sc->s[j]->hist->sum;
1465 ntot_add = sc->r[j].end - sc->r[j].start;
1473 if (!sc->s[j]->hist)
1475 if (ntot_so_far < ntot_start)
1477 /* adjust starting point */
1478 new_start = sc->r[j].start + (ntot_start - ntot_so_far);
1482 new_start = sc->r[j].start;
1484 /* adjust end point */
1485 new_end = sc->r[j].start + (ntot_end - ntot_so_far);
1486 if (new_end > sc->r[j].end)
1488 new_end = sc->r[j].end;
1491 /* check if we're in range at all */
1492 if ( (new_end < new_start) || (new_start > sc->r[j].end) )
1497 /* and write the new range */
1498 sc->r[j].start = (int)new_start;
1499 sc->r[j].end = (int)new_end;
1506 double ntot_start_norm, ntot_end_norm;
1507 /* calculate the amount of overlap of the
1508 desired range (ntot_start -- ntot_end) onto
1509 the histogram range (ntot_so_far -- ntot_so_far+ntot_add)*/
1511 /* first calculate normalized bounds
1512 (where 0 is the start of the hist range, and 1 the end) */
1513 ntot_start_norm = (ntot_start-ntot_so_far)/(double)ntot_add;
1514 ntot_end_norm = (ntot_end-ntot_so_far)/(double)ntot_add;
1516 /* now fix the boundaries */
1517 ntot_start_norm = min(1, max(0., ntot_start_norm));
1518 ntot_end_norm = max(0, min(1., ntot_end_norm));
1520 /* and calculate the overlap */
1521 overlap = ntot_end_norm - ntot_start_norm;
1523 if (overlap > 0.95) /* we allow for 5% slack */
1525 sc->r[j].use = TRUE;
1527 else if (overlap < 0.05)
1529 sc->r[j].use = FALSE;
1537 ntot_so_far += ntot_add;
1539 sample_coll_calc_ntot(sc);
1544 /* calculate minimum and maximum work values in sample collection */
1545 static void sample_coll_min_max(sample_coll_t *sc, double Wfac,
1546 double *Wmin, double *Wmax)
1553 for (i = 0; i < sc->nsamples; i++)
1555 samples_t *s = sc->s[i];
1556 sample_range_t *r = &(sc->r[i]);
1561 for (j = r->start; j < r->end; j++)
1563 *Wmin = min(*Wmin, s->du[j]*Wfac);
1564 *Wmax = max(*Wmax, s->du[j]*Wfac);
1569 int hd = 0; /* determine the histogram direction: */
1571 if ( (s->hist->nhist > 1) && (Wfac < 0) )
1575 dx = s->hist->dx[hd];
1577 for (j = s->hist->nbin[hd]-1; j >= 0; j--)
1579 *Wmin = min(*Wmin, Wfac*(s->hist->x0[hd])*dx);
1580 *Wmax = max(*Wmax, Wfac*(s->hist->x0[hd])*dx);
1581 /* look for the highest value bin with values */
1582 if (s->hist->bin[hd][j] > 0)
1584 *Wmin = min(*Wmin, Wfac*(j+s->hist->x0[hd]+1)*dx);
1585 *Wmax = max(*Wmax, Wfac*(j+s->hist->x0[hd]+1)*dx);
1594 /* Initialize a sim_data structure */
1595 static void sim_data_init(sim_data_t *sd)
1597 /* make linked list */
1598 sd->lb = &(sd->lb_head);
1599 sd->lb->next = sd->lb;
1600 sd->lb->prev = sd->lb;
1602 lambda_components_init(&(sd->lc));
1606 static double calc_bar_sum(int n, const double *W, double Wfac, double sbMmDG)
1613 for (i = 0; i < n; i++)
1615 sum += 1./(1. + exp(Wfac*W[i] + sbMmDG));
1621 /* calculate the BAR average given a histogram
1623 if type== 0, calculate the best estimate for the average,
1624 if type==-1, calculate the minimum possible value given the histogram
1625 if type== 1, calculate the maximum possible value given the histogram */
1626 static double calc_bar_sum_hist(const hist_t *hist, double Wfac, double sbMmDG,
1632 /* normalization factor multiplied with bin width and
1633 number of samples (we normalize through M): */
1635 int hd = 0; /* determine the histogram direction: */
1638 if ( (hist->nhist > 1) && (Wfac < 0) )
1643 max = hist->nbin[hd]-1;
1646 max = hist->nbin[hd]; /* we also add whatever was out of range */
1649 for (i = 0; i < max; i++)
1651 double x = Wfac*((i+hist->x0[hd])+0.5)*dx; /* bin middle */
1652 double pxdx = hist->bin[0][i]*normdx; /* p(x)dx */
1654 sum += pxdx/(1. + exp(x + sbMmDG));
1660 static double calc_bar_lowlevel(sample_coll_t *ca, sample_coll_t *cb,
1661 double temp, double tol, int type)
1666 double Wfac1, Wfac2, Wmin, Wmax;
1667 double DG0, DG1, DG2, dDG1;
1669 double n1, n2; /* numbers of samples as doubles */
1674 /* count the numbers of samples */
1680 /*if (!lambda_vec_same(ca->native_lambda, ca->foreign_lambda))*/
1681 if (ca->foreign_lambda->dhdl < 0)
1683 /* this is the case when the delta U were calculated directly
1684 (i.e. we're not scaling dhdl) */
1690 /* we're using dhdl, so delta_lambda needs to be a
1691 multiplication factor. */
1692 /*double delta_lambda=cb->native_lambda-ca->native_lambda;*/
1693 double delta_lambda = lambda_vec_abs_diff(cb->native_lambda,
1695 if (cb->native_lambda->lc->N > 1)
1698 "Can't (yet) do multi-component dhdl interpolation");
1701 Wfac1 = beta*delta_lambda;
1702 Wfac2 = -beta*delta_lambda;
1707 /* We print the output both in kT and kJ/mol.
1708 * Here we determine DG in kT, so when beta < 1
1709 * the precision has to be increased.
1714 /* Calculate minimum and maximum work to give an initial estimate of
1715 * delta G as their average.
1718 double Wmin1, Wmin2, Wmax1, Wmax2;
1719 sample_coll_min_max(ca, Wfac1, &Wmin1, &Wmax1);
1720 sample_coll_min_max(cb, Wfac2, &Wmin2, &Wmax2);
1722 Wmin = min(Wmin1, Wmin2);
1723 Wmax = max(Wmax1, Wmax2);
1731 fprintf(debug, "DG %9.5f %9.5f\n", DG0, DG2);
1733 /* We approximate by bisection: given our initial estimates
1734 we keep checking whether the halfway point is greater or
1735 smaller than what we get out of the BAR averages.
1737 For the comparison we can use twice the tolerance. */
1738 while (DG2 - DG0 > 2*tol)
1740 DG1 = 0.5*(DG0 + DG2);
1742 /*printf("Wfac1=%g, Wfac2=%g, beta=%g, DG1=%g\n",Wfac1,Wfac2,beta,
1745 /* calculate the BAR averages */
1748 for (i = 0; i < ca->nsamples; i++)
1750 samples_t *s = ca->s[i];
1751 sample_range_t *r = &(ca->r[i]);
1756 dDG1 += calc_bar_sum_hist(s->hist, Wfac1, (M-DG1), type);
1760 dDG1 += calc_bar_sum(r->end - r->start, s->du + r->start,
1765 for (i = 0; i < cb->nsamples; i++)
1767 samples_t *s = cb->s[i];
1768 sample_range_t *r = &(cb->r[i]);
1773 dDG1 -= calc_bar_sum_hist(s->hist, Wfac2, -(M-DG1), type);
1777 dDG1 -= calc_bar_sum(r->end - r->start, s->du + r->start,
1793 fprintf(debug, "DG %9.5f %9.5f\n", DG0, DG2);
1797 return 0.5*(DG0 + DG2);
1800 static void calc_rel_entropy(sample_coll_t *ca, sample_coll_t *cb,
1801 double temp, double dg, double *sa, double *sb)
1807 double Wfac1, Wfac2;
1813 /* count the numbers of samples */
1817 /* to ensure the work values are the same as during the delta_G */
1818 /*if (!lambda_vec_same(ca->native_lambda, ca->foreign_lambda))*/
1819 if (ca->foreign_lambda->dhdl < 0)
1821 /* this is the case when the delta U were calculated directly
1822 (i.e. we're not scaling dhdl) */
1828 /* we're using dhdl, so delta_lambda needs to be a
1829 multiplication factor. */
1830 double delta_lambda = lambda_vec_abs_diff(cb->native_lambda,
1832 Wfac1 = beta*delta_lambda;
1833 Wfac2 = -beta*delta_lambda;
1836 /* first calculate the average work in both directions */
1837 for (i = 0; i < ca->nsamples; i++)
1839 samples_t *s = ca->s[i];
1840 sample_range_t *r = &(ca->r[i]);
1845 for (j = r->start; j < r->end; j++)
1847 W_ab += Wfac1*s->du[j];
1852 /* normalization factor multiplied with bin width and
1853 number of samples (we normalize through M): */
1856 int hd = 0; /* histogram direction */
1857 if ( (s->hist->nhist > 1) && (Wfac1 < 0) )
1861 dx = s->hist->dx[hd];
1863 for (j = 0; j < s->hist->nbin[0]; j++)
1865 double x = Wfac1*((j+s->hist->x0[0])+0.5)*dx; /*bin ctr*/
1866 double pxdx = s->hist->bin[0][j]*normdx; /* p(x)dx */
1874 for (i = 0; i < cb->nsamples; i++)
1876 samples_t *s = cb->s[i];
1877 sample_range_t *r = &(cb->r[i]);
1882 for (j = r->start; j < r->end; j++)
1884 W_ba += Wfac1*s->du[j];
1889 /* normalization factor multiplied with bin width and
1890 number of samples (we normalize through M): */
1893 int hd = 0; /* histogram direction */
1894 if ( (s->hist->nhist > 1) && (Wfac2 < 0) )
1898 dx = s->hist->dx[hd];
1900 for (j = 0; j < s->hist->nbin[0]; j++)
1902 double x = Wfac1*((j+s->hist->x0[0])+0.5)*dx; /*bin ctr*/
1903 double pxdx = s->hist->bin[0][j]*normdx; /* p(x)dx */
1911 /* then calculate the relative entropies */
1916 static void calc_dg_stddev(sample_coll_t *ca, sample_coll_t *cb,
1917 double temp, double dg, double *stddev)
1921 double sigmafact = 0.;
1923 double Wfac1, Wfac2;
1929 /* count the numbers of samples */
1933 /* to ensure the work values are the same as during the delta_G */
1934 /*if (!lambda_vec_same(ca->native_lambda, ca->foreign_lambda))*/
1935 if (ca->foreign_lambda->dhdl < 0)
1937 /* this is the case when the delta U were calculated directly
1938 (i.e. we're not scaling dhdl) */
1944 /* we're using dhdl, so delta_lambda needs to be a
1945 multiplication factor. */
1946 double delta_lambda = lambda_vec_abs_diff(cb->native_lambda,
1948 Wfac1 = beta*delta_lambda;
1949 Wfac2 = -beta*delta_lambda;
1955 /* calculate average in both directions */
1956 for (i = 0; i < ca->nsamples; i++)
1958 samples_t *s = ca->s[i];
1959 sample_range_t *r = &(ca->r[i]);
1964 for (j = r->start; j < r->end; j++)
1966 sigmafact += 1./(2. + 2.*cosh((M + Wfac1*s->du[j] - dg)));
1971 /* normalization factor multiplied with bin width and
1972 number of samples (we normalize through M): */
1975 int hd = 0; /* histogram direction */
1976 if ( (s->hist->nhist > 1) && (Wfac1 < 0) )
1980 dx = s->hist->dx[hd];
1982 for (j = 0; j < s->hist->nbin[0]; j++)
1984 double x = Wfac1*((j+s->hist->x0[0])+0.5)*dx; /*bin ctr*/
1985 double pxdx = s->hist->bin[0][j]*normdx; /* p(x)dx */
1987 sigmafact += pxdx/(2. + 2.*cosh((M + x - dg)));
1992 for (i = 0; i < cb->nsamples; i++)
1994 samples_t *s = cb->s[i];
1995 sample_range_t *r = &(cb->r[i]);
2000 for (j = r->start; j < r->end; j++)
2002 sigmafact += 1./(2. + 2.*cosh((M - Wfac2*s->du[j] - dg)));
2007 /* normalization factor multiplied with bin width and
2008 number of samples (we normalize through M): */
2011 int hd = 0; /* histogram direction */
2012 if ( (s->hist->nhist > 1) && (Wfac2 < 0) )
2016 dx = s->hist->dx[hd];
2018 for (j = 0; j < s->hist->nbin[0]; j++)
2020 double x = Wfac2*((j+s->hist->x0[0])+0.5)*dx; /*bin ctr*/
2021 double pxdx = s->hist->bin[0][j]*normdx; /* p(x)dx */
2023 sigmafact += pxdx/(2. + 2.*cosh((M - x - dg)));
2029 sigmafact /= (n1 + n2);
2033 Shirts, Bair, Hooker & Pande, Phys. Rev. Lett 91, 140601 (2003): */
2034 *stddev = sqrt(((1./sigmafact) - ( (n1+n2)/n1 + (n1+n2)/n2 )));
2039 static void calc_bar(barres_t *br, double tol,
2040 int npee_min, int npee_max, gmx_bool *bEE,
2044 double dg_sig2, sa_sig2, sb_sig2, stddev_sig2; /* intermediate variance values
2045 for calculated quantities */
2046 int nsample1, nsample2;
2047 double temp = br->a->temp;
2049 double dg_min, dg_max;
2050 gmx_bool have_hist = FALSE;
2052 br->dg = calc_bar_lowlevel(br->a, br->b, temp, tol, 0);
2054 br->dg_disc_err = 0.;
2055 br->dg_histrange_err = 0.;
2057 /* check if there are histograms */
2058 for (i = 0; i < br->a->nsamples; i++)
2060 if (br->a->r[i].use && br->a->s[i]->hist)
2068 for (i = 0; i < br->b->nsamples; i++)
2070 if (br->b->r[i].use && br->b->s[i]->hist)
2078 /* calculate histogram-specific errors */
2081 dg_min = calc_bar_lowlevel(br->a, br->b, temp, tol, -1);
2082 dg_max = calc_bar_lowlevel(br->a, br->b, temp, tol, 1);
2084 if (fabs(dg_max - dg_min) > GMX_REAL_EPS*10)
2086 /* the histogram range error is the biggest of the differences
2087 between the best estimate and the extremes */
2088 br->dg_histrange_err = fabs(dg_max - dg_min);
2090 br->dg_disc_err = 0.;
2091 for (i = 0; i < br->a->nsamples; i++)
2093 if (br->a->s[i]->hist)
2095 br->dg_disc_err = max(br->dg_disc_err, br->a->s[i]->hist->dx[0]);
2098 for (i = 0; i < br->b->nsamples; i++)
2100 if (br->b->s[i]->hist)
2102 br->dg_disc_err = max(br->dg_disc_err, br->b->s[i]->hist->dx[0]);
2106 calc_rel_entropy(br->a, br->b, temp, br->dg, &(br->sa), &(br->sb));
2108 calc_dg_stddev(br->a, br->b, temp, br->dg, &(br->dg_stddev) );
2117 sample_coll_t ca, cb;
2119 /* initialize the samples */
2120 sample_coll_init(&ca, br->a->native_lambda, br->a->foreign_lambda,
2122 sample_coll_init(&cb, br->b->native_lambda, br->b->foreign_lambda,
2125 for (npee = npee_min; npee <= npee_max; npee++)
2134 double dstddev2 = 0;
2137 for (p = 0; p < npee; p++)
2144 cac = sample_coll_create_subsample(&ca, br->a, p, npee);
2145 cbc = sample_coll_create_subsample(&cb, br->b, p, npee);
2149 printf("WARNING: histogram number incompatible with block number for averaging: can't do error estimate\n");
2153 sample_coll_destroy(&ca);
2157 sample_coll_destroy(&cb);
2162 dgp = calc_bar_lowlevel(&ca, &cb, temp, tol, 0);
2166 partsum[npee*(npee_max+1)+p] += dgp;
2168 calc_rel_entropy(&ca, &cb, temp, dgp, &sac, &sbc);
2173 calc_dg_stddev(&ca, &cb, temp, dgp, &stddevc );
2176 dstddev2 += stddevc*stddevc;
2178 sample_coll_destroy(&ca);
2179 sample_coll_destroy(&cb);
2183 dg_sig2 += (dgs2-dgs*dgs)/(npee-1);
2189 sa_sig2 += (dsa2-dsa*dsa)/(npee-1);
2190 sb_sig2 += (dsb2-dsb*dsb)/(npee-1);
2194 stddev_sig2 += (dstddev2-dstddev*dstddev)/(npee-1);
2196 br->dg_err = sqrt(dg_sig2/(npee_max - npee_min + 1));
2197 br->sa_err = sqrt(sa_sig2/(npee_max - npee_min + 1));
2198 br->sb_err = sqrt(sb_sig2/(npee_max - npee_min + 1));
2199 br->dg_stddev_err = sqrt(stddev_sig2/(npee_max - npee_min + 1));
2204 static double bar_err(int nbmin, int nbmax, const double *partsum)
2207 double svar, s, s2, dg;
2210 for (nb = nbmin; nb <= nbmax; nb++)
2214 for (b = 0; b < nb; b++)
2216 dg = partsum[nb*(nbmax+1)+b];
2222 svar += (s2 - s*s)/(nb - 1);
2225 return sqrt(svar/(nbmax + 1 - nbmin));
2229 /* Seek the end of an identifier (consecutive non-spaces), followed by
2230 an optional number of spaces or '='-signs. Returns a pointer to the
2231 first non-space value found after that. Returns NULL if the string
2234 static const char *find_value(const char *str)
2236 gmx_bool name_end_found = FALSE;
2238 /* if the string is a NULL pointer, return a NULL pointer. */
2243 while (*str != '\0')
2245 /* first find the end of the name */
2246 if (!name_end_found)
2248 if (isspace(*str) || (*str == '=') )
2250 name_end_found = TRUE;
2255 if (!( isspace(*str) || (*str == '=') ))
2267 /* read a vector-notation description of a lambda vector */
2268 static gmx_bool read_lambda_compvec(const char *str,
2270 const lambda_components_t *lc_in,
2271 lambda_components_t *lc_out,
2275 gmx_bool initialize_lc = FALSE; /* whether to initialize the lambda
2276 components, or to check them */
2277 gmx_bool start_reached = FALSE; /* whether the start of component names
2279 gmx_bool vector = FALSE; /* whether there are multiple components */
2280 int n = 0; /* current component number */
2281 const char *val_start = NULL; /* start of the component name, or NULL
2282 if not in a value */
2292 if (lc_out && lc_out->N == 0)
2294 initialize_lc = TRUE;
2309 start_reached = TRUE;
2312 else if (*str == '(')
2315 start_reached = TRUE;
2317 else if (!isspace(*str))
2319 gmx_fatal(FARGS, "Error in lambda components in %s", fn);
2326 if (isspace(*str) || *str == ')' || *str == ',' || *str == '\0')
2333 lambda_components_add(lc_out, val_start,
2338 if (!lambda_components_check(lc_out, n, val_start,
2347 /* add a vector component to lv */
2348 lv->val[n] = strtod(val_start, &strtod_end);
2349 if (val_start == strtod_end)
2352 "Error reading lambda vector in %s", fn);
2355 /* reset for the next identifier */
2364 else if (isalnum(*str))
2377 gmx_fatal(FARGS, "Error in lambda components in %s", fn);
2385 else if (lv == NULL)
2391 gmx_fatal(FARGS, "Incomplete lambda vector data in %s",
2411 gmx_fatal(FARGS, "Incomplete lambda components data in %s", fn);
2417 /* read and check the component names from a string */
2418 static gmx_bool read_lambda_components(const char *str,
2419 lambda_components_t *lc,
2423 return read_lambda_compvec(str, NULL, NULL, lc, end, fn);
2426 /* read an initialized lambda vector from a string */
2427 static gmx_bool read_lambda_vector(const char *str,
2432 return read_lambda_compvec(str, lv, lv->lc, NULL, end, fn);
2437 /* deduce lambda value from legend.
2439 legend = the legend string
2441 lam = the initialized lambda vector
2442 returns whether to use the data in this set.
2444 static gmx_bool legend2lambda(const char *fn,
2450 const char *ptr = NULL, *ptr2 = NULL;
2451 gmx_bool ok = FALSE;
2452 gmx_bool bdhdl = FALSE;
2453 const char *tostr = " to ";
2457 gmx_fatal(FARGS, "There is no legend in file '%s', can not deduce lambda", fn);
2460 /* look for the last 'to': */
2464 ptr2 = strstr(ptr2, tostr);
2471 while (ptr2 != NULL && *ptr2 != '\0');
2475 ptr += strlen(tostr)-1; /* and advance past that 'to' */
2479 /* look for the = sign */
2480 ptr = strrchr(legend, '=');
2483 /* otherwise look for the last space */
2484 ptr = strrchr(legend, ' ');
2488 if (strstr(legend, "dH"))
2493 else if (strchr(legend, 'D') != NULL && strchr(legend, 'H') != NULL)
2498 else /*if (strstr(legend, "pV"))*/
2509 gmx_fatal(FARGS, "There is no proper lambda legend in file '%s', can not deduce lambda", fn);
2513 ptr = find_value(ptr);
2514 if (!ptr || !read_lambda_vector(ptr, lam, NULL, fn))
2516 gmx_fatal(FARGS, "lambda vector '%s' %s faulty", legend, fn);
2525 ptr = strrchr(legend, '=');
2529 /* there must be a component name */
2533 gmx_fatal(FARGS, "dhdl legend '%s' %s faulty", legend, fn);
2535 /* now backtrack to the start of the identifier */
2536 while (isspace(*ptr))
2542 gmx_fatal(FARGS, "dhdl legend '%s' %s faulty", legend, fn);
2545 while (!isspace(*ptr))
2550 gmx_fatal(FARGS, "dhdl legend '%s' %s faulty", legend, fn);
2554 strncpy(buf, ptr, (end-ptr));
2555 buf[(end-ptr)] = '\0';
2556 dhdl_index = lambda_components_find(lam->lc, ptr, (end-ptr));
2560 strncpy(buf, ptr, (end-ptr));
2561 buf[(end-ptr)] = '\0';
2563 "Did not find lambda component for '%s' in %s",
2572 "dhdl without component name with >1 lambda component in %s",
2577 lam->dhdl = dhdl_index;
2582 static gmx_bool subtitle2lambda(const char *subtitle, xvg_t *ba, const char *fn,
2583 lambda_components_t *lc)
2588 double native_lambda;
2592 /* first check for a state string */
2593 ptr = strstr(subtitle, "state");
2597 const char *val_end;
2599 /* the new 4.6 style lambda vectors */
2600 ptr = find_value(ptr);
2603 index = strtol(ptr, &end, 10);
2606 gmx_fatal(FARGS, "Incomplete state data in %s", fn);
2613 gmx_fatal(FARGS, "Incomplete state data in %s", fn);
2616 /* now find the lambda vector component names */
2617 while (*ptr != '(' && !isalnum(*ptr))
2623 "Incomplete lambda vector component data in %s", fn);
2628 if (!read_lambda_components(ptr, lc, &val_end, fn))
2631 "lambda vector components in %s don't match those previously read",
2634 ptr = find_value(val_end);
2637 gmx_fatal(FARGS, "Incomplete state data in %s", fn);
2640 lambda_vec_init(&(ba->native_lambda), lc);
2641 if (!read_lambda_vector(ptr, &(ba->native_lambda), NULL, fn))
2643 gmx_fatal(FARGS, "lambda vector in %s faulty", fn);
2645 ba->native_lambda.index = index;
2650 /* compatibility mode: check for lambda in other ways. */
2651 /* plain text lambda string */
2652 ptr = strstr(subtitle, "lambda");
2655 /* xmgrace formatted lambda string */
2656 ptr = strstr(subtitle, "\\xl\\f{}");
2660 /* xmgr formatted lambda string */
2661 ptr = strstr(subtitle, "\\8l\\4");
2665 ptr = strstr(ptr, "=");
2669 bFound = (sscanf(ptr+1, "%lf", &(native_lambda)) == 1);
2670 /* add the lambda component name as an empty string */
2673 if (!lambda_components_check(lc, 0, "", 0))
2676 "lambda vector components in %s don't match those previously read",
2682 lambda_components_add(lc, "", 0);
2684 lambda_vec_init(&(ba->native_lambda), lc);
2685 ba->native_lambda.val[0] = native_lambda;
2692 static void filename2lambda(const char *fn, xvg_t *ba)
2695 const char *ptr, *digitptr;
2699 /* go to the end of the path string and search backward to find the last
2700 directory in the path which has to contain the value of lambda
2702 while (ptr[1] != '\0')
2706 /* searching backward to find the second directory separator */
2711 if (ptr[0] != DIR_SEPARATOR && ptr[1] == DIR_SEPARATOR)
2719 /* save the last position of a digit between the last two
2720 separators = in the last dirname */
2721 if (dirsep > 0 && isdigit(*ptr))
2729 gmx_fatal(FARGS, "While trying to read the lambda value from the file path:"
2730 " last directory in the path '%s' does not contain a number", fn);
2732 if (digitptr[-1] == '-')
2736 lambda = strtod(digitptr, &endptr);
2737 if (endptr == digitptr)
2739 gmx_fatal(FARGS, "Malformed number in file path '%s'", fn);
2743 static void read_bar_xvg_lowlevel(const char *fn, real *temp, xvg_t *ba,
2744 lambda_components_t *lc)
2747 char *subtitle, **legend, *ptr;
2749 gmx_bool native_lambda_read = FALSE;
2757 np = read_xvg_legend(fn, &ba->y, &ba->nset, &subtitle, &legend);
2760 gmx_fatal(FARGS, "File %s contains no usable data.", fn);
2762 /* Reorder the data */
2764 for (i = 1; i < ba->nset; i++)
2766 ba->y[i-1] = ba->y[i];
2770 snew(ba->np, ba->nset);
2771 for (i = 0; i < ba->nset; i++)
2777 if (subtitle != NULL)
2779 /* try to extract temperature */
2780 ptr = strstr(subtitle, "T =");
2784 if (sscanf(ptr, "%lf", &ba->temp) == 1)
2788 gmx_fatal(FARGS, "Found temperature of %g in file '%s'",
2798 gmx_fatal(FARGS, "Did not find a temperature in the subtitle in file '%s', use the -temp option of [TT]g_bar[tt]", fn);
2803 /* Try to deduce lambda from the subtitle */
2806 if (subtitle2lambda(subtitle, ba, fn, lc))
2808 native_lambda_read = TRUE;
2811 snew(ba->lambda, ba->nset);
2814 /* Check if we have a single set, no legend, nset=1 means t and dH/dl */
2817 if (!native_lambda_read)
2819 /* Deduce lambda from the file name */
2820 filename2lambda(fn, ba);
2821 native_lambda_read = TRUE;
2823 ba->lambda[0] = ba->native_lambda;
2827 gmx_fatal(FARGS, "File %s contains multiple sets but no legends, can not determine the lambda values", fn);
2832 for (i = 0; i < ba->nset; )
2834 gmx_bool use = FALSE;
2835 /* Read lambda from the legend */
2836 lambda_vec_init( &(ba->lambda[i]), lc );
2837 lambda_vec_copy( &(ba->lambda[i]), &(ba->native_lambda));
2838 use = legend2lambda(fn, legend[i], ba, &(ba->lambda[i]));
2841 lambda_vec_print(&(ba->lambda[i]), buf, FALSE);
2847 printf("%s: Ignoring set '%s'.\n", fn, legend[i]);
2848 for (j = i+1; j < ba->nset; j++)
2850 ba->y[j-1] = ba->y[j];
2851 legend[j-1] = legend[j];
2858 if (!native_lambda_read)
2860 gmx_fatal(FARGS, "File %s contains multiple sets but no indication of the native lambda", fn);
2865 for (i = 0; i < ba->nset-1; i++)
2873 static void read_bar_xvg(char *fn, real *temp, sim_data_t *sd)
2882 read_bar_xvg_lowlevel(fn, temp, barsim, &(sd->lc));
2884 if (barsim->nset < 1)
2886 gmx_fatal(FARGS, "File '%s' contains fewer than two columns", fn);
2889 if (!gmx_within_tol(*temp, barsim->temp, GMX_FLOAT_EPS) && (*temp > 0) )
2891 gmx_fatal(FARGS, "Temperature in file %s different from earlier files or setting\n", fn);
2893 *temp = barsim->temp;
2895 /* now create a series of samples_t */
2896 snew(s, barsim->nset);
2897 for (i = 0; i < barsim->nset; i++)
2899 samples_init(s+i, &(barsim->native_lambda), &(barsim->lambda[i]),
2900 barsim->temp, lambda_vec_same(&(barsim->native_lambda),
2901 &(barsim->lambda[i])),
2903 s[i].du = barsim->y[i];
2904 s[i].ndu = barsim->np[i];
2907 lambda_data_list_insert_sample(sd->lb, s+i);
2912 lambda_vec_print(s[0].native_lambda, buf, FALSE);
2913 printf("%s: %.1f - %.1f; lambda = %s\n dH/dl & foreign lambdas:\n",
2914 fn, s[0].t[0], s[0].t[s[0].ndu-1], buf);
2915 for (i = 0; i < barsim->nset; i++)
2917 lambda_vec_print(s[i].foreign_lambda, buf, TRUE);
2918 printf(" %s (%d pts)\n", buf, s[i].ndu);
2924 static void read_edr_rawdh_block(samples_t **smp, int *ndu, t_enxblock *blk,
2925 double start_time, double delta_time,
2926 lambda_vec_t *native_lambda, double temp,
2927 double *last_t, const char *filename)
2931 double old_foreign_lambda;
2932 lambda_vec_t *foreign_lambda;
2934 samples_t *s; /* convenience pointer */
2937 /* check the block types etc. */
2938 if ( (blk->nsub < 3) ||
2939 (blk->sub[0].type != xdr_datatype_int) ||
2940 (blk->sub[1].type != xdr_datatype_double) ||
2942 (blk->sub[2].type != xdr_datatype_float) &&
2943 (blk->sub[2].type != xdr_datatype_double)
2945 (blk->sub[0].nr < 1) ||
2946 (blk->sub[1].nr < 1) )
2949 "Unexpected/corrupted block data in file %s around time %g.",
2950 filename, start_time);
2953 snew(foreign_lambda, 1);
2954 lambda_vec_init(foreign_lambda, native_lambda->lc);
2955 lambda_vec_copy(foreign_lambda, native_lambda);
2956 type = blk->sub[0].ival[0];
2959 for (i = 0; i < native_lambda->lc->N; i++)
2961 foreign_lambda->val[i] = blk->sub[1].dval[i];
2966 if (blk->sub[0].nr > 1)
2968 foreign_lambda->dhdl = blk->sub[0].ival[1];
2972 foreign_lambda->dhdl = 0;
2978 /* initialize the samples structure if it's empty. */
2980 samples_init(*smp, native_lambda, foreign_lambda, temp,
2981 type == dhbtDHDL, filename);
2982 (*smp)->start_time = start_time;
2983 (*smp)->delta_time = delta_time;
2986 /* set convenience pointer */
2989 /* now double check */
2990 if (!lambda_vec_same(s->foreign_lambda, foreign_lambda) )
2992 char buf[STRLEN], buf2[STRLEN];
2993 lambda_vec_print(foreign_lambda, buf, FALSE);
2994 lambda_vec_print(s->foreign_lambda, buf2, FALSE);
2995 fprintf(stderr, "Got foreign lambda=%s, expected: %s\n", buf, buf2);
2996 gmx_fatal(FARGS, "Corrupted data in file %s around t=%g.",
2997 filename, start_time);
3000 /* make room for the data */
3001 if (s->ndu_alloc < (size_t)(s->ndu + blk->sub[2].nr) )
3003 s->ndu_alloc += (s->ndu_alloc < (size_t)blk->sub[2].nr) ?
3004 blk->sub[2].nr*2 : s->ndu_alloc;
3005 srenew(s->du_alloc, s->ndu_alloc);
3006 s->du = s->du_alloc;
3009 s->ndu += blk->sub[2].nr;
3010 s->ntot += blk->sub[2].nr;
3011 *ndu = blk->sub[2].nr;
3013 /* and copy the data*/
3014 for (j = 0; j < blk->sub[2].nr; j++)
3016 if (blk->sub[2].type == xdr_datatype_float)
3018 s->du[startj+j] = blk->sub[2].fval[j];
3022 s->du[startj+j] = blk->sub[2].dval[j];
3025 if (start_time + blk->sub[2].nr*delta_time > *last_t)
3027 *last_t = start_time + blk->sub[2].nr*delta_time;
3031 static samples_t *read_edr_hist_block(int *nsamples, t_enxblock *blk,
3032 double start_time, double delta_time,
3033 lambda_vec_t *native_lambda, double temp,
3034 double *last_t, const char *filename)
3039 double old_foreign_lambda;
3040 lambda_vec_t *foreign_lambda;
3044 /* check the block types etc. */
3045 if ( (blk->nsub < 2) ||
3046 (blk->sub[0].type != xdr_datatype_double) ||
3047 (blk->sub[1].type != xdr_datatype_large_int) ||
3048 (blk->sub[0].nr < 2) ||
3049 (blk->sub[1].nr < 2) )
3052 "Unexpected/corrupted block data in file %s around time %g",
3053 filename, start_time);
3056 nhist = blk->nsub-2;
3064 "Unexpected/corrupted block data in file %s around time %g",
3065 filename, start_time);
3071 snew(foreign_lambda, 1);
3072 lambda_vec_init(foreign_lambda, native_lambda->lc);
3073 lambda_vec_copy(foreign_lambda, native_lambda);
3074 type = (int)(blk->sub[1].lval[1]);
3077 double old_foreign_lambda;
3079 old_foreign_lambda = blk->sub[0].dval[0];
3080 if (old_foreign_lambda >= 0)
3082 foreign_lambda->val[0] = old_foreign_lambda;
3083 if (foreign_lambda->lc->N > 1)
3086 "Single-component lambda in multi-component file %s",
3092 for (i = 0; i < native_lambda->lc->N; i++)
3094 foreign_lambda->val[i] = blk->sub[0].dval[i+2];
3100 if (foreign_lambda->lc->N > 1)
3102 if (blk->sub[1].nr < 3 + nhist)
3105 "Missing derivative coord in multi-component file %s",
3108 foreign_lambda->dhdl = blk->sub[1].lval[2 + nhist];
3112 foreign_lambda->dhdl = 0;
3116 samples_init(s, native_lambda, foreign_lambda, temp, type == dhbtDHDL,
3120 for (i = 0; i < nhist; i++)
3122 nbins[i] = blk->sub[i+2].nr;
3125 hist_init(s->hist, nhist, nbins);
3127 for (i = 0; i < nhist; i++)
3129 s->hist->x0[i] = blk->sub[1].lval[2+i];
3130 s->hist->dx[i] = blk->sub[0].dval[1];
3133 s->hist->dx[i] = -s->hist->dx[i];
3137 s->hist->start_time = start_time;
3138 s->hist->delta_time = delta_time;
3139 s->start_time = start_time;
3140 s->delta_time = delta_time;
3142 for (i = 0; i < nhist; i++)
3145 gmx_large_int_t sum = 0;
3147 for (j = 0; j < s->hist->nbin[i]; j++)
3149 int binv = (int)(blk->sub[i+2].ival[j]);
3151 s->hist->bin[i][j] = binv;
3164 gmx_fatal(FARGS, "Histogram counts don't match in %s",
3170 if (start_time + s->hist->sum*delta_time > *last_t)
3172 *last_t = start_time + s->hist->sum*delta_time;
3178 static void read_barsim_edr(char *fn, real *temp, sim_data_t *sd)
3184 gmx_enxnm_t *enm = NULL;
3185 double first_t = -1;
3187 samples_t **samples_rawdh = NULL; /* contains samples for raw delta_h */
3188 int *nhists = NULL; /* array to keep count & print at end */
3189 int *npts = NULL; /* array to keep count & print at end */
3190 lambda_vec_t **lambdas = NULL; /* array to keep count & print at end */
3191 lambda_vec_t *native_lambda;
3192 double end_time; /* the end time of the last batch of samples */
3194 lambda_vec_t start_lambda;
3196 fp = open_enx(fn, "r");
3197 do_enxnms(fp, &nre, &enm);
3200 snew(native_lambda, 1);
3201 start_lambda.lc = NULL;
3203 while (do_enx(fp, fr))
3205 /* count the data blocks */
3206 int nblocks_raw = 0;
3207 int nblocks_hist = 0;
3210 /* DHCOLL block information: */
3211 double start_time = 0, delta_time = 0, old_start_lambda = 0, delta_lambda = 0;
3214 /* count the blocks and handle collection information: */
3215 for (i = 0; i < fr->nblock; i++)
3217 if (fr->block[i].id == enxDHHIST)
3221 if (fr->block[i].id == enxDH)
3225 if (fr->block[i].id == enxDHCOLL)
3228 if ( (fr->block[i].nsub < 1) ||
3229 (fr->block[i].sub[0].type != xdr_datatype_double) ||
3230 (fr->block[i].sub[0].nr < 5))
3232 gmx_fatal(FARGS, "Unexpected block data in file %s", fn);
3235 /* read the data from the DHCOLL block */
3236 rtemp = fr->block[i].sub[0].dval[0];
3237 start_time = fr->block[i].sub[0].dval[1];
3238 delta_time = fr->block[i].sub[0].dval[2];
3239 old_start_lambda = fr->block[i].sub[0].dval[3];
3240 delta_lambda = fr->block[i].sub[0].dval[4];
3242 if (delta_lambda > 0)
3244 gmx_fatal(FARGS, "Lambda values not constant in %s: can't apply BAR method", fn);
3246 if ( ( *temp != rtemp) && (*temp > 0) )
3248 gmx_fatal(FARGS, "Temperature in file %s different from earlier files or setting\n", fn);
3252 if (old_start_lambda >= 0)
3256 if (!lambda_components_check(&(sd->lc), 0, "", 0))
3259 "lambda vector components in %s don't match those previously read",
3265 lambda_components_add(&(sd->lc), "", 0);
3267 if (!start_lambda.lc)
3269 lambda_vec_init(&start_lambda, &(sd->lc));
3271 start_lambda.val[0] = old_start_lambda;
3275 /* read lambda vector */
3277 gmx_bool check = (sd->lc.N > 0);
3278 if (fr->block[i].nsub < 2)
3281 "No lambda vector, but start_lambda=%g\n",
3284 n_lambda_vec = fr->block[i].sub[1].ival[1];
3285 for (j = 0; j < n_lambda_vec; j++)
3288 efpt_singular_names[fr->block[i].sub[1].ival[1+j]];
3291 /* check the components */
3292 lambda_components_check(&(sd->lc), j, name,
3297 lambda_components_add(&(sd->lc), name,
3301 lambda_vec_init(&start_lambda, &(sd->lc));
3302 start_lambda.index = fr->block[i].sub[1].ival[0];
3303 for (j = 0; j < n_lambda_vec; j++)
3305 start_lambda.val[j] = fr->block[i].sub[0].dval[5+j];
3310 first_t = start_time;
3317 gmx_fatal(FARGS, "Did not find delta H information in file %s", fn);
3319 if (nblocks_raw > 0 && nblocks_hist > 0)
3321 gmx_fatal(FARGS, "Can't handle both raw delta U data and histograms in the same file %s", fn);
3326 /* check the native lambda */
3327 if (!lambda_vec_same(&start_lambda, native_lambda) )
3329 gmx_fatal(FARGS, "Native lambda not constant in file %s: started at %g, and becomes %g at time %g",
3330 fn, native_lambda, start_lambda, start_time);
3332 /* check the number of samples against the previous number */
3333 if ( ((nblocks_raw+nblocks_hist) != nsamples) || (nlam != 1 ) )
3335 gmx_fatal(FARGS, "Unexpected block count in %s: was %d, now %d\n",
3336 fn, nsamples+1, nblocks_raw+nblocks_hist+nlam);
3338 /* check whether last iterations's end time matches with
3339 the currrent start time */
3340 if ( (fabs(last_t - start_time) > 2*delta_time) && last_t >= 0)
3342 /* it didn't. We need to store our samples and reallocate */
3343 for (i = 0; i < nsamples; i++)
3345 if (samples_rawdh[i])
3347 /* insert it into the existing list */
3348 lambda_data_list_insert_sample(sd->lb,
3350 /* and make sure we'll allocate a new one this time
3352 samples_rawdh[i] = NULL;
3359 /* this is the first round; allocate the associated data
3361 /*native_lambda=start_lambda;*/
3362 lambda_vec_init(native_lambda, &(sd->lc));
3363 lambda_vec_copy(native_lambda, &start_lambda);
3364 nsamples = nblocks_raw+nblocks_hist;
3365 snew(nhists, nsamples);
3366 snew(npts, nsamples);
3367 snew(lambdas, nsamples);
3368 snew(samples_rawdh, nsamples);
3369 for (i = 0; i < nsamples; i++)
3374 samples_rawdh[i] = NULL; /* init to NULL so we know which
3375 ones contain values */
3380 k = 0; /* counter for the lambdas, etc. arrays */
3381 for (i = 0; i < fr->nblock; i++)
3383 if (fr->block[i].id == enxDH)
3385 int type = (fr->block[i].sub[0].ival[0]);
3386 if (type == dhbtDH || type == dhbtDHDL)
3389 read_edr_rawdh_block(&(samples_rawdh[k]),
3392 start_time, delta_time,
3393 native_lambda, rtemp,
3396 if (samples_rawdh[k])
3398 lambdas[k] = samples_rawdh[k]->foreign_lambda;
3403 else if (fr->block[i].id == enxDHHIST)
3405 int type = (int)(fr->block[i].sub[1].lval[1]);
3406 if (type == dhbtDH || type == dhbtDHDL)
3410 samples_t *s; /* this is where the data will go */
3411 s = read_edr_hist_block(&nb, &(fr->block[i]),
3412 start_time, delta_time,
3413 native_lambda, rtemp,
3418 lambdas[k] = s->foreign_lambda;
3421 /* and insert the new sample immediately */
3422 for (j = 0; j < nb; j++)
3424 lambda_data_list_insert_sample(sd->lb, s+j);
3430 /* Now store all our extant sample collections */
3431 for (i = 0; i < nsamples; i++)
3433 if (samples_rawdh[i])
3435 /* insert it into the existing list */
3436 lambda_data_list_insert_sample(sd->lb, samples_rawdh[i]);
3444 lambda_vec_print(native_lambda, buf, FALSE);
3445 printf("%s: %.1f - %.1f; lambda = %s\n foreign lambdas:\n",
3446 fn, first_t, last_t, buf);
3447 for (i = 0; i < nsamples; i++)
3451 lambda_vec_print(lambdas[i], buf, TRUE);
3454 printf(" %s (%d hists)\n", buf, nhists[i]);
3458 printf(" %s (%d pts)\n", buf, npts[i]);
3470 int gmx_bar(int argc, char *argv[])
3472 static const char *desc[] = {
3473 "[TT]g_bar[tt] calculates free energy difference estimates through ",
3474 "Bennett's acceptance ratio method (BAR). It also automatically",
3475 "adds series of individual free energies obtained with BAR into",
3476 "a combined free energy estimate.[PAR]",
3478 "Every individual BAR free energy difference relies on two ",
3479 "simulations at different states: say state A and state B, as",
3480 "controlled by a parameter, [GRK]lambda[grk] (see the [TT].mdp[tt] parameter",
3481 "[TT]init_lambda[tt]). The BAR method calculates a ratio of weighted",
3482 "average of the Hamiltonian difference of state B given state A and",
3484 "The energy differences to the other state must be calculated",
3485 "explicitly during the simulation. This can be done with",
3486 "the [TT].mdp[tt] option [TT]foreign_lambda[tt].[PAR]",
3488 "Input option [TT]-f[tt] expects multiple [TT]dhdl.xvg[tt] files. ",
3489 "Two types of input files are supported:[BR]",
3490 "[TT]*[tt] Files with more than one [IT]y[it]-value. ",
3491 "The files should have columns ",
3492 "with dH/d[GRK]lambda[grk] and [GRK]Delta[grk][GRK]lambda[grk]. ",
3493 "The [GRK]lambda[grk] values are inferred ",
3494 "from the legends: [GRK]lambda[grk] of the simulation from the legend of ",
3495 "dH/d[GRK]lambda[grk] and the foreign [GRK]lambda[grk] values from the ",
3496 "legends of Delta H",
3498 "[TT]*[tt] Files with only one [IT]y[it]-value. Using the",
3499 "[TT]-extp[tt] option for these files, it is assumed",
3500 "that the [IT]y[it]-value is dH/d[GRK]lambda[grk] and that the ",
3501 "Hamiltonian depends linearly on [GRK]lambda[grk]. ",
3502 "The [GRK]lambda[grk] value of the simulation is inferred from the ",
3503 "subtitle (if present), otherwise from a number in the subdirectory ",
3504 "in the file name.[PAR]",
3506 "The [GRK]lambda[grk] of the simulation is parsed from ",
3507 "[TT]dhdl.xvg[tt] file's legend containing the string 'dH', the ",
3508 "foreign [GRK]lambda[grk] values from the legend containing the ",
3509 "capitalized letters 'D' and 'H'. The temperature is parsed from ",
3510 "the legend line containing 'T ='.[PAR]",
3512 "The input option [TT]-g[tt] expects multiple [TT].edr[tt] files. ",
3513 "These can contain either lists of energy differences (see the ",
3514 "[TT].mdp[tt] option [TT]separate_dhdl_file[tt]), or a series of ",
3515 "histograms (see the [TT].mdp[tt] options [TT]dh_hist_size[tt] and ",
3516 "[TT]dh_hist_spacing[tt]).", "The temperature and [GRK]lambda[grk] ",
3517 "values are automatically deduced from the [TT]ener.edr[tt] file.[PAR]",
3519 "In addition to the [TT].mdp[tt] option [TT]foreign_lambda[tt], ",
3520 "the energy difference can also be extrapolated from the ",
3521 "dH/d[GRK]lambda[grk] values. This is done with the[TT]-extp[tt]",
3522 "option, which assumes that the system's Hamiltonian depends linearly",
3523 "on [GRK]lambda[grk], which is not normally the case.[PAR]",
3525 "The free energy estimates are determined using BAR with bisection, ",
3526 "with the precision of the output set with [TT]-prec[tt]. ",
3527 "An error estimate taking into account time correlations ",
3528 "is made by splitting the data into blocks and determining ",
3529 "the free energy differences over those blocks and assuming ",
3530 "the blocks are independent. ",
3531 "The final error estimate is determined from the average variance ",
3532 "over 5 blocks. A range of block numbers for error estimation can ",
3533 "be provided with the options [TT]-nbmin[tt] and [TT]-nbmax[tt].[PAR]",
3535 "[TT]g_bar[tt] tries to aggregate samples with the same 'native' and ",
3536 "'foreign' [GRK]lambda[grk] values, but always assumes independent ",
3537 "samples. [BB]Note[bb] that when aggregating energy ",
3538 "differences/derivatives with different sampling intervals, this is ",
3539 "almost certainly not correct. Usually subsequent energies are ",
3540 "correlated and different time intervals mean different degrees ",
3541 "of correlation between samples.[PAR]",
3543 "The results are split in two parts: the last part contains the final ",
3544 "results in kJ/mol, together with the error estimate for each part ",
3545 "and the total. The first part contains detailed free energy ",
3546 "difference estimates and phase space overlap measures in units of ",
3547 "kT (together with their computed error estimate). The printed ",
3549 "[TT]*[tt] lam_A: the [GRK]lambda[grk] values for point A.[BR]",
3550 "[TT]*[tt] lam_B: the [GRK]lambda[grk] values for point B.[BR]",
3551 "[TT]*[tt] DG: the free energy estimate.[BR]",
3552 "[TT]*[tt] s_A: an estimate of the relative entropy of B in A.[BR]",
3553 "[TT]*[tt] s_A: an estimate of the relative entropy of A in B.[BR]",
3554 "[TT]*[tt] stdev: an estimate expected per-sample standard deviation.[PAR]",
3556 "The relative entropy of both states in each other's ensemble can be ",
3557 "interpreted as a measure of phase space overlap: ",
3558 "the relative entropy s_A of the work samples of lambda_B in the ",
3559 "ensemble of lambda_A (and vice versa for s_B), is a ",
3560 "measure of the 'distance' between Boltzmann distributions of ",
3561 "the two states, that goes to zero for identical distributions. See ",
3562 "Wu & Kofke, J. Chem. Phys. 123 084109 (2005) for more information.",
3564 "The estimate of the expected per-sample standard deviation, as given ",
3565 "in Bennett's original BAR paper: Bennett, J. Comp. Phys. 22, p 245 (1976).",
3566 "Eq. 10 therein gives an estimate of the quality of sampling (not directly",
3567 "of the actual statistical error, because it assumes independent samples).[PAR]",
3569 "To get a visual estimate of the phase space overlap, use the ",
3570 "[TT]-oh[tt] option to write series of histograms, together with the ",
3571 "[TT]-nbin[tt] option.[PAR]"
3573 static real begin = 0, end = -1, temp = -1;
3574 int nd = 2, nbmin = 5, nbmax = 5;
3576 gmx_bool use_dhdl = FALSE;
3577 gmx_bool calc_s, calc_v;
3579 { "-b", FALSE, etREAL, {&begin}, "Begin time for BAR" },
3580 { "-e", FALSE, etREAL, {&end}, "End time for BAR" },
3581 { "-temp", FALSE, etREAL, {&temp}, "Temperature (K)" },
3582 { "-prec", FALSE, etINT, {&nd}, "The number of digits after the decimal point" },
3583 { "-nbmin", FALSE, etINT, {&nbmin}, "Minimum number of blocks for error estimation" },
3584 { "-nbmax", FALSE, etINT, {&nbmax}, "Maximum number of blocks for error estimation" },
3585 { "-nbin", FALSE, etINT, {&nbin}, "Number of bins for histogram output"},
3586 { "-extp", FALSE, etBOOL, {&use_dhdl}, "Whether to linearly extrapolate dH/dl values to use as energies"}
3590 { efXVG, "-f", "dhdl", ffOPTRDMULT },
3591 { efEDR, "-g", "ener", ffOPTRDMULT },
3592 { efXVG, "-o", "bar", ffOPTWR },
3593 { efXVG, "-oi", "barint", ffOPTWR },
3594 { efXVG, "-oh", "histogram", ffOPTWR }
3596 #define NFILE asize(fnm)
3599 int nf = 0; /* file counter */
3601 int nfile_tot; /* total number of input files */
3606 sim_data_t sim_data; /* the simulation data */
3607 barres_t *results; /* the results */
3608 int nresults; /* number of results in results array */
3611 double prec, dg_tot, dg, sig, dg_tot_max, dg_tot_min;
3613 char dgformat[20], xvg2format[STRLEN], xvg3format[STRLEN];
3614 char buf[STRLEN], buf2[STRLEN];
3615 char ktformat[STRLEN], sktformat[STRLEN];
3616 char kteformat[STRLEN], skteformat[STRLEN];
3619 gmx_bool result_OK = TRUE, bEE = TRUE;
3621 gmx_bool disc_err = FALSE;
3622 double sum_disc_err = 0.; /* discretization error */
3623 gmx_bool histrange_err = FALSE;
3624 double sum_histrange_err = 0.; /* histogram range error */
3625 double stat_err = 0.; /* statistical error */
3627 CopyRight(stderr, argv[0]);
3628 parse_common_args(&argc, argv,
3630 NFILE, fnm, asize(pa), pa, asize(desc), desc, 0, NULL, &oenv);
3632 if (opt2bSet("-f", NFILE, fnm))
3634 nxvgfile = opt2fns(&fxvgnms, "-f", NFILE, fnm);
3636 if (opt2bSet("-g", NFILE, fnm))
3638 nedrfile = opt2fns(&fedrnms, "-g", NFILE, fnm);
3641 sim_data_init(&sim_data);
3643 /* make linked list */
3645 lambda_data_init(lb, 0, 0);
3651 nfile_tot = nxvgfile + nedrfile;
3655 gmx_fatal(FARGS, "No input files!");
3660 gmx_fatal(FARGS, "Can not have negative number of digits");
3662 prec = pow(10, -nd);
3664 snew(partsum, (nbmax+1)*(nbmax+1));
3667 /* read in all files. First xvg files */
3668 for (f = 0; f < nxvgfile; f++)
3670 read_bar_xvg(fxvgnms[f], &temp, &sim_data);
3673 /* then .edr files */
3674 for (f = 0; f < nedrfile; f++)
3676 read_barsim_edr(fedrnms[f], &temp, &sim_data);;
3680 /* fix the times to allow for equilibration */
3681 sim_data_impose_times(&sim_data, begin, end);
3683 if (opt2bSet("-oh", NFILE, fnm))
3685 sim_data_histogram(&sim_data, opt2fn("-oh", NFILE, fnm), nbin, oenv);
3688 /* assemble the output structures from the lambdas */
3689 results = barres_list_create(&sim_data, &nresults, use_dhdl);
3691 sum_disc_err = barres_list_max_disc_err(results, nresults);
3695 printf("\nNo results to calculate.\n");
3699 if (sum_disc_err > prec)
3701 prec = sum_disc_err;
3702 nd = ceil(-log10(prec));
3703 printf("WARNING: setting the precision to %g because that is the minimum\n reasonable number, given the expected discretization error.\n", prec);
3707 /*sprintf(lamformat,"%%6.3f");*/
3708 sprintf( dgformat, "%%%d.%df", 3+nd, nd);
3709 /* the format strings of the results in kT */
3710 sprintf( ktformat, "%%%d.%df", 5+nd, nd);
3711 sprintf( sktformat, "%%%ds", 6+nd);
3712 /* the format strings of the errors in kT */
3713 sprintf( kteformat, "%%%d.%df", 3+nd, nd);
3714 sprintf( skteformat, "%%%ds", 4+nd);
3715 sprintf(xvg2format, "%s %s\n", "%s", dgformat);
3716 sprintf(xvg3format, "%s %s %s\n", "%s", dgformat, dgformat);
3721 if (opt2bSet("-o", NFILE, fnm))
3723 sprintf(buf, "%s (%s)", "\\DeltaG", "kT");
3724 fpb = xvgropen_type(opt2fn("-o", NFILE, fnm), "Free energy differences",
3725 "\\lambda", buf, exvggtXYDY, oenv);
3729 if (opt2bSet("-oi", NFILE, fnm))
3731 sprintf(buf, "%s (%s)", "\\DeltaG", "kT");
3732 fpi = xvgropen(opt2fn("-oi", NFILE, fnm), "Free energy integral",
3733 "\\lambda", buf, oenv);
3743 /* first calculate results */
3746 for (f = 0; f < nresults; f++)
3748 /* Determine the free energy difference with a factor of 10
3749 * more accuracy than requested for printing.
3751 calc_bar(&(results[f]), 0.1*prec, nbmin, nbmax,
3754 if (results[f].dg_disc_err > prec/10.)
3758 if (results[f].dg_histrange_err > prec/10.)
3760 histrange_err = TRUE;
3764 /* print results in kT */
3768 printf("\nTemperature: %g K\n", temp);
3770 printf("\nDetailed results in kT (see help for explanation):\n\n");
3771 printf("%6s ", " lam_A");
3772 printf("%6s ", " lam_B");
3773 printf(sktformat, "DG ");
3776 printf(skteformat, "+/- ");
3780 printf(skteformat, "disc ");
3784 printf(skteformat, "range ");
3786 printf(sktformat, "s_A ");
3789 printf(skteformat, "+/- " );
3791 printf(sktformat, "s_B ");
3794 printf(skteformat, "+/- " );
3796 printf(sktformat, "stdev ");
3799 printf(skteformat, "+/- ");
3802 for (f = 0; f < nresults; f++)
3804 lambda_vec_print_short(results[f].a->native_lambda, buf);
3806 lambda_vec_print_short(results[f].b->native_lambda, buf);
3808 printf(ktformat, results[f].dg);
3812 printf(kteformat, results[f].dg_err);
3817 printf(kteformat, results[f].dg_disc_err);
3822 printf(kteformat, results[f].dg_histrange_err);
3825 printf(ktformat, results[f].sa);
3829 printf(kteformat, results[f].sa_err);
3832 printf(ktformat, results[f].sb);
3836 printf(kteformat, results[f].sb_err);
3839 printf(ktformat, results[f].dg_stddev);
3843 printf(kteformat, results[f].dg_stddev_err);
3847 /* Check for negative relative entropy with a 95% certainty. */
3848 if (results[f].sa < -2*results[f].sa_err ||
3849 results[f].sb < -2*results[f].sb_err)
3857 printf("\nWARNING: Some of these results violate the Second Law of "
3858 "Thermodynamics: \n"
3859 " This is can be the result of severe undersampling, or "
3861 " there is something wrong with the simulations.\n");
3865 /* final results in kJ/mol */
3866 printf("\n\nFinal results in kJ/mol:\n\n");
3868 for (f = 0; f < nresults; f++)
3873 lambda_vec_print_short(results[f].a->native_lambda, buf);
3874 fprintf(fpi, xvg2format, buf, dg_tot);
3880 lambda_vec_print_intermediate(results[f].a->native_lambda,
3881 results[f].b->native_lambda,
3884 fprintf(fpb, xvg3format, buf, results[f].dg, results[f].dg_err);
3888 lambda_vec_print_short(results[f].a->native_lambda, buf);
3889 lambda_vec_print_short(results[f].b->native_lambda, buf2);
3890 printf("%s - %s", buf, buf2);
3893 printf(dgformat, results[f].dg*kT);
3897 printf(dgformat, results[f].dg_err*kT);
3901 printf(" (max. range err. = ");
3902 printf(dgformat, results[f].dg_histrange_err*kT);
3904 sum_histrange_err += results[f].dg_histrange_err*kT;
3908 dg_tot += results[f].dg;
3912 lambda_vec_print_short(results[0].a->native_lambda, buf);
3913 lambda_vec_print_short(results[nresults-1].b->native_lambda, buf2);
3914 printf("%s - %s", buf, buf2);
3917 printf(dgformat, dg_tot*kT);
3920 stat_err = bar_err(nbmin, nbmax, partsum)*kT;
3922 printf(dgformat, max(max(stat_err, sum_disc_err), sum_histrange_err));
3927 printf("\nmaximum discretization error = ");
3928 printf(dgformat, sum_disc_err);
3929 if (bEE && stat_err < sum_disc_err)
3931 printf("WARNING: discretization error (%g) is larger than statistical error.\n Decrease histogram spacing for more accurate results\n", stat_err);
3936 printf("\nmaximum histogram range error = ");
3937 printf(dgformat, sum_histrange_err);
3938 if (bEE && stat_err < sum_histrange_err)
3940 printf("WARNING: histogram range error (%g) is larger than statistical error.\n Increase histogram range for more accurate results\n", stat_err);
3949 lambda_vec_print_short(results[nresults-1].b->native_lambda, buf);
3950 fprintf(fpi, xvg2format, buf, dg_tot);
3954 do_view(oenv, opt2fn_null("-o", NFILE, fnm), "-xydy");
3955 do_view(oenv, opt2fn_null("-oi", NFILE, fnm), "-xydy");