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=%f -> %f 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 = %f\nin the files for lambda = %f", bl->lambda, bl->prev->lambda);
1208 gmx_fatal(FARGS, "Could not find a set for foreign lambda = %f\nin the files for lambda = %f", 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 /* calculate the BAR averages */
1745 for (i = 0; i < ca->nsamples; i++)
1747 samples_t *s = ca->s[i];
1748 sample_range_t *r = &(ca->r[i]);
1753 dDG1 += calc_bar_sum_hist(s->hist, Wfac1, (M-DG1), type);
1757 dDG1 += calc_bar_sum(r->end - r->start, s->du + r->start,
1762 for (i = 0; i < cb->nsamples; i++)
1764 samples_t *s = cb->s[i];
1765 sample_range_t *r = &(cb->r[i]);
1770 dDG1 -= calc_bar_sum_hist(s->hist, Wfac2, -(M-DG1), type);
1774 dDG1 -= calc_bar_sum(r->end - r->start, s->du + r->start,
1790 fprintf(debug, "DG %9.5f %9.5f\n", DG0, DG2);
1794 return 0.5*(DG0 + DG2);
1797 static void calc_rel_entropy(sample_coll_t *ca, sample_coll_t *cb,
1798 double temp, double dg, double *sa, double *sb)
1804 double Wfac1, Wfac2;
1810 /* count the numbers of samples */
1814 /* to ensure the work values are the same as during the delta_G */
1815 /*if (!lambda_vec_same(ca->native_lambda, ca->foreign_lambda))*/
1816 if (ca->foreign_lambda->dhdl < 0)
1818 /* this is the case when the delta U were calculated directly
1819 (i.e. we're not scaling dhdl) */
1825 /* we're using dhdl, so delta_lambda needs to be a
1826 multiplication factor. */
1827 double delta_lambda = lambda_vec_abs_diff(cb->native_lambda,
1829 Wfac1 = beta*delta_lambda;
1830 Wfac2 = -beta*delta_lambda;
1833 /* first calculate the average work in both directions */
1834 for (i = 0; i < ca->nsamples; i++)
1836 samples_t *s = ca->s[i];
1837 sample_range_t *r = &(ca->r[i]);
1842 for (j = r->start; j < r->end; j++)
1844 W_ab += Wfac1*s->du[j];
1849 /* normalization factor multiplied with bin width and
1850 number of samples (we normalize through M): */
1853 int hd = 0; /* histogram direction */
1854 if ( (s->hist->nhist > 1) && (Wfac1 < 0) )
1858 dx = s->hist->dx[hd];
1860 for (j = 0; j < s->hist->nbin[0]; j++)
1862 double x = Wfac1*((j+s->hist->x0[0])+0.5)*dx; /*bin ctr*/
1863 double pxdx = s->hist->bin[0][j]*normdx; /* p(x)dx */
1871 for (i = 0; i < cb->nsamples; i++)
1873 samples_t *s = cb->s[i];
1874 sample_range_t *r = &(cb->r[i]);
1879 for (j = r->start; j < r->end; j++)
1881 W_ba += Wfac1*s->du[j];
1886 /* normalization factor multiplied with bin width and
1887 number of samples (we normalize through M): */
1890 int hd = 0; /* histogram direction */
1891 if ( (s->hist->nhist > 1) && (Wfac2 < 0) )
1895 dx = s->hist->dx[hd];
1897 for (j = 0; j < s->hist->nbin[0]; j++)
1899 double x = Wfac1*((j+s->hist->x0[0])+0.5)*dx; /*bin ctr*/
1900 double pxdx = s->hist->bin[0][j]*normdx; /* p(x)dx */
1908 /* then calculate the relative entropies */
1913 static void calc_dg_stddev(sample_coll_t *ca, sample_coll_t *cb,
1914 double temp, double dg, double *stddev)
1918 double sigmafact = 0.;
1920 double Wfac1, Wfac2;
1926 /* count the numbers of samples */
1930 /* to ensure the work values are the same as during the delta_G */
1931 /*if (!lambda_vec_same(ca->native_lambda, ca->foreign_lambda))*/
1932 if (ca->foreign_lambda->dhdl < 0)
1934 /* this is the case when the delta U were calculated directly
1935 (i.e. we're not scaling dhdl) */
1941 /* we're using dhdl, so delta_lambda needs to be a
1942 multiplication factor. */
1943 double delta_lambda = lambda_vec_abs_diff(cb->native_lambda,
1945 Wfac1 = beta*delta_lambda;
1946 Wfac2 = -beta*delta_lambda;
1952 /* calculate average in both directions */
1953 for (i = 0; i < ca->nsamples; i++)
1955 samples_t *s = ca->s[i];
1956 sample_range_t *r = &(ca->r[i]);
1961 for (j = r->start; j < r->end; j++)
1963 sigmafact += 1./(2. + 2.*cosh((M + Wfac1*s->du[j] - dg)));
1968 /* normalization factor multiplied with bin width and
1969 number of samples (we normalize through M): */
1972 int hd = 0; /* histogram direction */
1973 if ( (s->hist->nhist > 1) && (Wfac1 < 0) )
1977 dx = s->hist->dx[hd];
1979 for (j = 0; j < s->hist->nbin[0]; j++)
1981 double x = Wfac1*((j+s->hist->x0[0])+0.5)*dx; /*bin ctr*/
1982 double pxdx = s->hist->bin[0][j]*normdx; /* p(x)dx */
1984 sigmafact += pxdx/(2. + 2.*cosh((M + x - dg)));
1989 for (i = 0; i < cb->nsamples; i++)
1991 samples_t *s = cb->s[i];
1992 sample_range_t *r = &(cb->r[i]);
1997 for (j = r->start; j < r->end; j++)
1999 sigmafact += 1./(2. + 2.*cosh((M - Wfac2*s->du[j] - dg)));
2004 /* normalization factor multiplied with bin width and
2005 number of samples (we normalize through M): */
2008 int hd = 0; /* histogram direction */
2009 if ( (s->hist->nhist > 1) && (Wfac2 < 0) )
2013 dx = s->hist->dx[hd];
2015 for (j = 0; j < s->hist->nbin[0]; j++)
2017 double x = Wfac2*((j+s->hist->x0[0])+0.5)*dx; /*bin ctr*/
2018 double pxdx = s->hist->bin[0][j]*normdx; /* p(x)dx */
2020 sigmafact += pxdx/(2. + 2.*cosh((M - x - dg)));
2026 sigmafact /= (n1 + n2);
2030 Shirts, Bair, Hooker & Pande, Phys. Rev. Lett 91, 140601 (2003): */
2031 *stddev = sqrt(((1./sigmafact) - ( (n1+n2)/n1 + (n1+n2)/n2 )));
2036 static void calc_bar(barres_t *br, double tol,
2037 int npee_min, int npee_max, gmx_bool *bEE,
2041 double dg_sig2, sa_sig2, sb_sig2, stddev_sig2; /* intermediate variance values
2042 for calculated quantities */
2043 int nsample1, nsample2;
2044 double temp = br->a->temp;
2046 double dg_min, dg_max;
2047 gmx_bool have_hist = FALSE;
2049 br->dg = calc_bar_lowlevel(br->a, br->b, temp, tol, 0);
2051 br->dg_disc_err = 0.;
2052 br->dg_histrange_err = 0.;
2054 /* check if there are histograms */
2055 for (i = 0; i < br->a->nsamples; i++)
2057 if (br->a->r[i].use && br->a->s[i]->hist)
2065 for (i = 0; i < br->b->nsamples; i++)
2067 if (br->b->r[i].use && br->b->s[i]->hist)
2075 /* calculate histogram-specific errors */
2078 dg_min = calc_bar_lowlevel(br->a, br->b, temp, tol, -1);
2079 dg_max = calc_bar_lowlevel(br->a, br->b, temp, tol, 1);
2081 if (fabs(dg_max - dg_min) > GMX_REAL_EPS*10)
2083 /* the histogram range error is the biggest of the differences
2084 between the best estimate and the extremes */
2085 br->dg_histrange_err = fabs(dg_max - dg_min);
2087 br->dg_disc_err = 0.;
2088 for (i = 0; i < br->a->nsamples; i++)
2090 if (br->a->s[i]->hist)
2092 br->dg_disc_err = max(br->dg_disc_err, br->a->s[i]->hist->dx[0]);
2095 for (i = 0; i < br->b->nsamples; i++)
2097 if (br->b->s[i]->hist)
2099 br->dg_disc_err = max(br->dg_disc_err, br->b->s[i]->hist->dx[0]);
2103 calc_rel_entropy(br->a, br->b, temp, br->dg, &(br->sa), &(br->sb));
2105 calc_dg_stddev(br->a, br->b, temp, br->dg, &(br->dg_stddev) );
2114 sample_coll_t ca, cb;
2116 /* initialize the samples */
2117 sample_coll_init(&ca, br->a->native_lambda, br->a->foreign_lambda,
2119 sample_coll_init(&cb, br->b->native_lambda, br->b->foreign_lambda,
2122 for (npee = npee_min; npee <= npee_max; npee++)
2131 double dstddev2 = 0;
2134 for (p = 0; p < npee; p++)
2141 cac = sample_coll_create_subsample(&ca, br->a, p, npee);
2142 cbc = sample_coll_create_subsample(&cb, br->b, p, npee);
2146 printf("WARNING: histogram number incompatible with block number for averaging: can't do error estimate\n");
2150 sample_coll_destroy(&ca);
2154 sample_coll_destroy(&cb);
2159 dgp = calc_bar_lowlevel(&ca, &cb, temp, tol, 0);
2163 partsum[npee*(npee_max+1)+p] += dgp;
2165 calc_rel_entropy(&ca, &cb, temp, dgp, &sac, &sbc);
2170 calc_dg_stddev(&ca, &cb, temp, dgp, &stddevc );
2173 dstddev2 += stddevc*stddevc;
2175 sample_coll_destroy(&ca);
2176 sample_coll_destroy(&cb);
2180 dg_sig2 += (dgs2-dgs*dgs)/(npee-1);
2186 sa_sig2 += (dsa2-dsa*dsa)/(npee-1);
2187 sb_sig2 += (dsb2-dsb*dsb)/(npee-1);
2191 stddev_sig2 += (dstddev2-dstddev*dstddev)/(npee-1);
2193 br->dg_err = sqrt(dg_sig2/(npee_max - npee_min + 1));
2194 br->sa_err = sqrt(sa_sig2/(npee_max - npee_min + 1));
2195 br->sb_err = sqrt(sb_sig2/(npee_max - npee_min + 1));
2196 br->dg_stddev_err = sqrt(stddev_sig2/(npee_max - npee_min + 1));
2201 static double bar_err(int nbmin, int nbmax, const double *partsum)
2204 double svar, s, s2, dg;
2207 for (nb = nbmin; nb <= nbmax; nb++)
2211 for (b = 0; b < nb; b++)
2213 dg = partsum[nb*(nbmax+1)+b];
2219 svar += (s2 - s*s)/(nb - 1);
2222 return sqrt(svar/(nbmax + 1 - nbmin));
2226 /* Seek the end of an identifier (consecutive non-spaces), followed by
2227 an optional number of spaces or '='-signs. Returns a pointer to the
2228 first non-space value found after that. Returns NULL if the string
2231 static const char *find_value(const char *str)
2233 gmx_bool name_end_found = FALSE;
2235 /* if the string is a NULL pointer, return a NULL pointer. */
2240 while (*str != '\0')
2242 /* first find the end of the name */
2243 if (!name_end_found)
2245 if (isspace(*str) || (*str == '=') )
2247 name_end_found = TRUE;
2252 if (!( isspace(*str) || (*str == '=') ))
2264 /* read a vector-notation description of a lambda vector */
2265 static gmx_bool read_lambda_compvec(const char *str,
2267 const lambda_components_t *lc_in,
2268 lambda_components_t *lc_out,
2272 gmx_bool initialize_lc = FALSE; /* whether to initialize the lambda
2273 components, or to check them */
2274 gmx_bool start_reached = FALSE; /* whether the start of component names
2276 gmx_bool vector = FALSE; /* whether there are multiple components */
2277 int n = 0; /* current component number */
2278 const char *val_start = NULL; /* start of the component name, or NULL
2279 if not in a value */
2289 if (lc_out && lc_out->N == 0)
2291 initialize_lc = TRUE;
2306 start_reached = TRUE;
2309 else if (*str == '(')
2312 start_reached = TRUE;
2314 else if (!isspace(*str))
2316 gmx_fatal(FARGS, "Error in lambda components in %s", fn);
2323 if (isspace(*str) || *str == ')' || *str == ',' || *str == '\0')
2330 lambda_components_add(lc_out, val_start,
2335 if (!lambda_components_check(lc_out, n, val_start,
2344 /* add a vector component to lv */
2345 lv->val[n] = strtod(val_start, &strtod_end);
2346 if (val_start == strtod_end)
2349 "Error reading lambda vector in %s", fn);
2352 /* reset for the next identifier */
2361 else if (isalnum(*str))
2374 gmx_fatal(FARGS, "Error in lambda components in %s", fn);
2382 else if (lv == NULL)
2388 gmx_fatal(FARGS, "Incomplete lambda vector data in %s",
2408 gmx_fatal(FARGS, "Incomplete lambda components data in %s", fn);
2414 /* read and check the component names from a string */
2415 static gmx_bool read_lambda_components(const char *str,
2416 lambda_components_t *lc,
2420 return read_lambda_compvec(str, NULL, NULL, lc, end, fn);
2423 /* read an initialized lambda vector from a string */
2424 static gmx_bool read_lambda_vector(const char *str,
2429 return read_lambda_compvec(str, lv, lv->lc, NULL, end, fn);
2434 /* deduce lambda value from legend.
2436 legend = the legend string
2438 lam = the initialized lambda vector
2439 returns whether to use the data in this set.
2441 static gmx_bool legend2lambda(const char *fn,
2447 const char *ptr = NULL, *ptr2 = NULL;
2448 gmx_bool ok = FALSE;
2449 gmx_bool bdhdl = FALSE;
2450 const char *tostr = " to ";
2454 gmx_fatal(FARGS, "There is no legend in file '%s', can not deduce lambda", fn);
2457 /* look for the last 'to': */
2461 ptr2 = strstr(ptr2, tostr);
2468 while (ptr2 != NULL && *ptr2 != '\0');
2472 ptr += strlen(tostr)-1; /* and advance past that 'to' */
2476 /* look for the = sign */
2477 ptr = strrchr(legend, '=');
2480 /* otherwise look for the last space */
2481 ptr = strrchr(legend, ' ');
2485 if (strstr(legend, "dH"))
2490 else if (strchr(legend, 'D') != NULL && strchr(legend, 'H') != NULL)
2495 else /*if (strstr(legend, "pV"))*/
2506 gmx_fatal(FARGS, "There is no proper lambda legend in file '%s', can not deduce lambda", fn);
2510 ptr = find_value(ptr);
2511 if (!ptr || !read_lambda_vector(ptr, lam, NULL, fn))
2513 gmx_fatal(FARGS, "lambda vector '%s' %s faulty", legend, fn);
2522 ptr = strrchr(legend, '=');
2526 /* there must be a component name */
2530 gmx_fatal(FARGS, "dhdl legend '%s' %s faulty", legend, fn);
2532 /* now backtrack to the start of the identifier */
2533 while (isspace(*ptr))
2539 gmx_fatal(FARGS, "dhdl legend '%s' %s faulty", legend, fn);
2542 while (!isspace(*ptr))
2547 gmx_fatal(FARGS, "dhdl legend '%s' %s faulty", legend, fn);
2551 strncpy(buf, ptr, (end-ptr));
2552 buf[(end-ptr)] = '\0';
2553 dhdl_index = lambda_components_find(lam->lc, ptr, (end-ptr));
2557 strncpy(buf, ptr, (end-ptr));
2558 buf[(end-ptr)] = '\0';
2560 "Did not find lambda component for '%s' in %s",
2569 "dhdl without component name with >1 lambda component in %s",
2574 lam->dhdl = dhdl_index;
2579 static gmx_bool subtitle2lambda(const char *subtitle, xvg_t *ba, const char *fn,
2580 lambda_components_t *lc)
2585 double native_lambda;
2589 /* first check for a state string */
2590 ptr = strstr(subtitle, "state");
2594 const char *val_end;
2596 /* the new 4.6 style lambda vectors */
2597 ptr = find_value(ptr);
2600 index = strtol(ptr, &end, 10);
2603 gmx_fatal(FARGS, "Incomplete state data in %s", fn);
2610 gmx_fatal(FARGS, "Incomplete state data in %s", fn);
2613 /* now find the lambda vector component names */
2614 while (*ptr != '(' && !isalnum(*ptr))
2620 "Incomplete lambda vector component data in %s", fn);
2625 if (!read_lambda_components(ptr, lc, &val_end, fn))
2628 "lambda vector components in %s don't match those previously read",
2631 ptr = find_value(val_end);
2634 gmx_fatal(FARGS, "Incomplete state data in %s", fn);
2637 lambda_vec_init(&(ba->native_lambda), lc);
2638 if (!read_lambda_vector(ptr, &(ba->native_lambda), NULL, fn))
2640 gmx_fatal(FARGS, "lambda vector in %s faulty", fn);
2642 ba->native_lambda.index = index;
2647 /* compatibility mode: check for lambda in other ways. */
2648 /* plain text lambda string */
2649 ptr = strstr(subtitle, "lambda");
2652 /* xmgrace formatted lambda string */
2653 ptr = strstr(subtitle, "\\xl\\f{}");
2657 /* xmgr formatted lambda string */
2658 ptr = strstr(subtitle, "\\8l\\4");
2662 ptr = strstr(ptr, "=");
2666 bFound = (sscanf(ptr+1, "%lf", &(native_lambda)) == 1);
2667 /* add the lambda component name as an empty string */
2670 if (!lambda_components_check(lc, 0, "", 0))
2673 "lambda vector components in %s don't match those previously read",
2679 lambda_components_add(lc, "", 0);
2681 lambda_vec_init(&(ba->native_lambda), lc);
2682 ba->native_lambda.val[0] = native_lambda;
2689 static void filename2lambda(const char *fn, xvg_t *ba)
2692 const char *ptr, *digitptr;
2696 /* go to the end of the path string and search backward to find the last
2697 directory in the path which has to contain the value of lambda
2699 while (ptr[1] != '\0')
2703 /* searching backward to find the second directory separator */
2708 if (ptr[0] != DIR_SEPARATOR && ptr[1] == DIR_SEPARATOR)
2716 /* save the last position of a digit between the last two
2717 separators = in the last dirname */
2718 if (dirsep > 0 && isdigit(*ptr))
2726 gmx_fatal(FARGS, "While trying to read the lambda value from the file path:"
2727 " last directory in the path '%s' does not contain a number", fn);
2729 if (digitptr[-1] == '-')
2733 lambda = strtod(digitptr, &endptr);
2734 if (endptr == digitptr)
2736 gmx_fatal(FARGS, "Malformed number in file path '%s'", fn);
2740 static void read_bar_xvg_lowlevel(const char *fn, real *temp, xvg_t *ba,
2741 lambda_components_t *lc)
2744 char *subtitle, **legend, *ptr;
2746 gmx_bool native_lambda_read = FALSE;
2754 np = read_xvg_legend(fn, &ba->y, &ba->nset, &subtitle, &legend);
2757 gmx_fatal(FARGS, "File %s contains no usable data.", fn);
2759 /* Reorder the data */
2761 for (i = 1; i < ba->nset; i++)
2763 ba->y[i-1] = ba->y[i];
2767 snew(ba->np, ba->nset);
2768 for (i = 0; i < ba->nset; i++)
2774 if (subtitle != NULL)
2776 /* try to extract temperature */
2777 ptr = strstr(subtitle, "T =");
2781 if (sscanf(ptr, "%lf", &ba->temp) == 1)
2785 gmx_fatal(FARGS, "Found temperature of %f in file '%s'",
2795 gmx_fatal(FARGS, "Did not find a temperature in the subtitle in file '%s', use the -temp option of [TT]g_bar[tt]", fn);
2800 /* Try to deduce lambda from the subtitle */
2803 if (subtitle2lambda(subtitle, ba, fn, lc))
2805 native_lambda_read = TRUE;
2808 snew(ba->lambda, ba->nset);
2811 /* Check if we have a single set, no legend, nset=1 means t and dH/dl */
2814 if (!native_lambda_read)
2816 /* Deduce lambda from the file name */
2817 filename2lambda(fn, ba);
2818 native_lambda_read = TRUE;
2820 ba->lambda[0] = ba->native_lambda;
2824 gmx_fatal(FARGS, "File %s contains multiple sets but no legends, can not determine the lambda values", fn);
2829 for (i = 0; i < ba->nset; )
2831 gmx_bool use = FALSE;
2832 /* Read lambda from the legend */
2833 lambda_vec_init( &(ba->lambda[i]), lc );
2834 lambda_vec_copy( &(ba->lambda[i]), &(ba->native_lambda));
2835 use = legend2lambda(fn, legend[i], ba, &(ba->lambda[i]));
2838 lambda_vec_print(&(ba->lambda[i]), buf, FALSE);
2844 printf("%s: Ignoring set '%s'.\n", fn, legend[i]);
2845 for (j = i+1; j < ba->nset; j++)
2847 ba->y[j-1] = ba->y[j];
2848 legend[j-1] = legend[j];
2855 if (!native_lambda_read)
2857 gmx_fatal(FARGS, "File %s contains multiple sets but no indication of the native lambda", fn);
2862 for (i = 0; i < ba->nset-1; i++)
2870 static void read_bar_xvg(char *fn, real *temp, sim_data_t *sd)
2879 read_bar_xvg_lowlevel(fn, temp, barsim, &(sd->lc));
2881 if (barsim->nset < 1)
2883 gmx_fatal(FARGS, "File '%s' contains fewer than two columns", fn);
2886 if (!gmx_within_tol(*temp, barsim->temp, GMX_FLOAT_EPS) && (*temp > 0) )
2888 gmx_fatal(FARGS, "Temperature in file %s different from earlier files or setting\n", fn);
2890 *temp = barsim->temp;
2892 /* now create a series of samples_t */
2893 snew(s, barsim->nset);
2894 for (i = 0; i < barsim->nset; i++)
2896 samples_init(s+i, &(barsim->native_lambda), &(barsim->lambda[i]),
2897 barsim->temp, lambda_vec_same(&(barsim->native_lambda),
2898 &(barsim->lambda[i])),
2900 s[i].du = barsim->y[i];
2901 s[i].ndu = barsim->np[i];
2904 lambda_data_list_insert_sample(sd->lb, s+i);
2909 lambda_vec_print(s[0].native_lambda, buf, FALSE);
2910 printf("%s: %.1f - %.1f; lambda = %s\n dH/dl & foreign lambdas:\n",
2911 fn, s[0].t[0], s[0].t[s[0].ndu-1], buf);
2912 for (i = 0; i < barsim->nset; i++)
2914 lambda_vec_print(s[i].foreign_lambda, buf, TRUE);
2915 printf(" %s (%d pts)\n", buf, s[i].ndu);
2921 static void read_edr_rawdh_block(samples_t **smp, int *ndu, t_enxblock *blk,
2922 double start_time, double delta_time,
2923 lambda_vec_t *native_lambda, double temp,
2924 double *last_t, const char *filename)
2928 double old_foreign_lambda;
2929 lambda_vec_t *foreign_lambda;
2931 samples_t *s; /* convenience pointer */
2934 /* check the block types etc. */
2935 if ( (blk->nsub < 3) ||
2936 (blk->sub[0].type != xdr_datatype_int) ||
2937 (blk->sub[1].type != xdr_datatype_double) ||
2939 (blk->sub[2].type != xdr_datatype_float) &&
2940 (blk->sub[2].type != xdr_datatype_double)
2942 (blk->sub[0].nr < 1) ||
2943 (blk->sub[1].nr < 1) )
2946 "Unexpected/corrupted block data in file %s around time %f.",
2947 filename, start_time);
2950 snew(foreign_lambda, 1);
2951 lambda_vec_init(foreign_lambda, native_lambda->lc);
2952 lambda_vec_copy(foreign_lambda, native_lambda);
2953 type = blk->sub[0].ival[0];
2956 for (i = 0; i < native_lambda->lc->N; i++)
2958 foreign_lambda->val[i] = blk->sub[1].dval[i];
2963 if (blk->sub[0].nr > 1)
2965 foreign_lambda->dhdl = blk->sub[0].ival[1];
2969 foreign_lambda->dhdl = 0;
2975 /* initialize the samples structure if it's empty. */
2977 samples_init(*smp, native_lambda, foreign_lambda, temp,
2978 type == dhbtDHDL, filename);
2979 (*smp)->start_time = start_time;
2980 (*smp)->delta_time = delta_time;
2983 /* set convenience pointer */
2986 /* now double check */
2987 if (!lambda_vec_same(s->foreign_lambda, foreign_lambda) )
2989 char buf[STRLEN], buf2[STRLEN];
2990 lambda_vec_print(foreign_lambda, buf, FALSE);
2991 lambda_vec_print(s->foreign_lambda, buf2, FALSE);
2992 fprintf(stderr, "Got foreign lambda=%s, expected: %s\n", buf, buf2);
2993 gmx_fatal(FARGS, "Corrupted data in file %s around t=%f.",
2994 filename, start_time);
2997 /* make room for the data */
2998 if (s->ndu_alloc < (size_t)(s->ndu + blk->sub[2].nr) )
3000 s->ndu_alloc += (s->ndu_alloc < (size_t)blk->sub[2].nr) ?
3001 blk->sub[2].nr*2 : s->ndu_alloc;
3002 srenew(s->du_alloc, s->ndu_alloc);
3003 s->du = s->du_alloc;
3006 s->ndu += blk->sub[2].nr;
3007 s->ntot += blk->sub[2].nr;
3008 *ndu = blk->sub[2].nr;
3010 /* and copy the data*/
3011 for (j = 0; j < blk->sub[2].nr; j++)
3013 if (blk->sub[2].type == xdr_datatype_float)
3015 s->du[startj+j] = blk->sub[2].fval[j];
3019 s->du[startj+j] = blk->sub[2].dval[j];
3022 if (start_time + blk->sub[2].nr*delta_time > *last_t)
3024 *last_t = start_time + blk->sub[2].nr*delta_time;
3028 static samples_t *read_edr_hist_block(int *nsamples, t_enxblock *blk,
3029 double start_time, double delta_time,
3030 lambda_vec_t *native_lambda, double temp,
3031 double *last_t, const char *filename)
3036 double old_foreign_lambda;
3037 lambda_vec_t *foreign_lambda;
3041 /* check the block types etc. */
3042 if ( (blk->nsub < 2) ||
3043 (blk->sub[0].type != xdr_datatype_double) ||
3044 (blk->sub[1].type != xdr_datatype_large_int) ||
3045 (blk->sub[0].nr < 2) ||
3046 (blk->sub[1].nr < 2) )
3049 "Unexpected/corrupted block data in file %s around time %f",
3050 filename, start_time);
3053 nhist = blk->nsub-2;
3061 "Unexpected/corrupted block data in file %s around time %f",
3062 filename, start_time);
3068 snew(foreign_lambda, 1);
3069 lambda_vec_init(foreign_lambda, native_lambda->lc);
3070 lambda_vec_copy(foreign_lambda, native_lambda);
3071 type = (int)(blk->sub[1].lval[1]);
3074 double old_foreign_lambda;
3076 old_foreign_lambda = blk->sub[0].dval[0];
3077 if (old_foreign_lambda >= 0)
3079 foreign_lambda->val[0] = old_foreign_lambda;
3080 if (foreign_lambda->lc->N > 1)
3083 "Single-component lambda in multi-component file %s",
3089 for (i = 0; i < native_lambda->lc->N; i++)
3091 foreign_lambda->val[i] = blk->sub[0].dval[i+2];
3097 if (foreign_lambda->lc->N > 1)
3099 if (blk->sub[1].nr < 3 + nhist)
3102 "Missing derivative coord in multi-component file %s",
3105 foreign_lambda->dhdl = blk->sub[1].lval[2 + nhist];
3109 foreign_lambda->dhdl = 0;
3113 samples_init(s, native_lambda, foreign_lambda, temp, type == dhbtDHDL,
3117 for (i = 0; i < nhist; i++)
3119 nbins[i] = blk->sub[i+2].nr;
3122 hist_init(s->hist, nhist, nbins);
3124 for (i = 0; i < nhist; i++)
3126 s->hist->x0[i] = blk->sub[1].lval[2+i];
3127 s->hist->dx[i] = blk->sub[0].dval[1];
3130 s->hist->dx[i] = -s->hist->dx[i];
3134 s->hist->start_time = start_time;
3135 s->hist->delta_time = delta_time;
3136 s->start_time = start_time;
3137 s->delta_time = delta_time;
3139 for (i = 0; i < nhist; i++)
3142 gmx_large_int_t sum = 0;
3144 for (j = 0; j < s->hist->nbin[i]; j++)
3146 int binv = (int)(blk->sub[i+2].ival[j]);
3148 s->hist->bin[i][j] = binv;
3161 gmx_fatal(FARGS, "Histogram counts don't match in %s",
3167 if (start_time + s->hist->sum*delta_time > *last_t)
3169 *last_t = start_time + s->hist->sum*delta_time;
3175 static void read_barsim_edr(char *fn, real *temp, sim_data_t *sd)
3181 gmx_enxnm_t *enm = NULL;
3182 double first_t = -1;
3184 samples_t **samples_rawdh = NULL; /* contains samples for raw delta_h */
3185 int *nhists = NULL; /* array to keep count & print at end */
3186 int *npts = NULL; /* array to keep count & print at end */
3187 lambda_vec_t **lambdas = NULL; /* array to keep count & print at end */
3188 lambda_vec_t *native_lambda;
3189 double end_time; /* the end time of the last batch of samples */
3191 lambda_vec_t start_lambda;
3193 fp = open_enx(fn, "r");
3194 do_enxnms(fp, &nre, &enm);
3197 snew(native_lambda, 1);
3198 start_lambda.lc = NULL;
3200 while (do_enx(fp, fr))
3202 /* count the data blocks */
3203 int nblocks_raw = 0;
3204 int nblocks_hist = 0;
3207 /* DHCOLL block information: */
3208 double start_time = 0, delta_time = 0, old_start_lambda = 0, delta_lambda = 0;
3211 /* count the blocks and handle collection information: */
3212 for (i = 0; i < fr->nblock; i++)
3214 if (fr->block[i].id == enxDHHIST)
3218 if (fr->block[i].id == enxDH)
3222 if (fr->block[i].id == enxDHCOLL)
3225 if ( (fr->block[i].nsub < 1) ||
3226 (fr->block[i].sub[0].type != xdr_datatype_double) ||
3227 (fr->block[i].sub[0].nr < 5))
3229 gmx_fatal(FARGS, "Unexpected block data in file %s", fn);
3232 /* read the data from the DHCOLL block */
3233 rtemp = fr->block[i].sub[0].dval[0];
3234 start_time = fr->block[i].sub[0].dval[1];
3235 delta_time = fr->block[i].sub[0].dval[2];
3236 old_start_lambda = fr->block[i].sub[0].dval[3];
3237 delta_lambda = fr->block[i].sub[0].dval[4];
3239 if (delta_lambda > 0)
3241 gmx_fatal(FARGS, "Lambda values not constant in %s: can't apply BAR method", fn);
3243 if ( ( *temp != rtemp) && (*temp > 0) )
3245 gmx_fatal(FARGS, "Temperature in file %s different from earlier files or setting\n", fn);
3249 if (old_start_lambda >= 0)
3253 if (!lambda_components_check(&(sd->lc), 0, "", 0))
3256 "lambda vector components in %s don't match those previously read",
3262 lambda_components_add(&(sd->lc), "", 0);
3264 if (!start_lambda.lc)
3266 lambda_vec_init(&start_lambda, &(sd->lc));
3268 start_lambda.val[0] = old_start_lambda;
3272 /* read lambda vector */
3274 gmx_bool check = (sd->lc.N > 0);
3275 if (fr->block[i].nsub < 2)
3278 "No lambda vector, but start_lambda=%f\n",
3281 n_lambda_vec = fr->block[i].sub[1].ival[1];
3282 for (j = 0; j < n_lambda_vec; j++)
3285 efpt_singular_names[fr->block[i].sub[1].ival[1+j]];
3288 /* check the components */
3289 lambda_components_check(&(sd->lc), j, name,
3294 lambda_components_add(&(sd->lc), name,
3298 lambda_vec_init(&start_lambda, &(sd->lc));
3299 start_lambda.index = fr->block[i].sub[1].ival[0];
3300 for (j = 0; j < n_lambda_vec; j++)
3302 start_lambda.val[j] = fr->block[i].sub[0].dval[5+j];
3307 first_t = start_time;
3314 gmx_fatal(FARGS, "Did not find delta H information in file %s", fn);
3316 if (nblocks_raw > 0 && nblocks_hist > 0)
3318 gmx_fatal(FARGS, "Can't handle both raw delta U data and histograms in the same file %s", fn);
3323 /* check the native lambda */
3324 if (!lambda_vec_same(&start_lambda, native_lambda) )
3326 gmx_fatal(FARGS, "Native lambda not constant in file %s: started at %f, and becomes %f at time %f",
3327 fn, native_lambda, start_lambda, start_time);
3329 /* check the number of samples against the previous number */
3330 if ( ((nblocks_raw+nblocks_hist) != nsamples) || (nlam != 1 ) )
3332 gmx_fatal(FARGS, "Unexpected block count in %s: was %d, now %d\n",
3333 fn, nsamples+1, nblocks_raw+nblocks_hist+nlam);
3335 /* check whether last iterations's end time matches with
3336 the currrent start time */
3337 if ( (fabs(last_t - start_time) > 2*delta_time) && last_t >= 0)
3339 /* it didn't. We need to store our samples and reallocate */
3340 for (i = 0; i < nsamples; i++)
3342 if (samples_rawdh[i])
3344 /* insert it into the existing list */
3345 lambda_data_list_insert_sample(sd->lb,
3347 /* and make sure we'll allocate a new one this time
3349 samples_rawdh[i] = NULL;
3356 /* this is the first round; allocate the associated data
3358 /*native_lambda=start_lambda;*/
3359 lambda_vec_init(native_lambda, &(sd->lc));
3360 lambda_vec_copy(native_lambda, &start_lambda);
3361 nsamples = nblocks_raw+nblocks_hist;
3362 snew(nhists, nsamples);
3363 snew(npts, nsamples);
3364 snew(lambdas, nsamples);
3365 snew(samples_rawdh, nsamples);
3366 for (i = 0; i < nsamples; i++)
3371 samples_rawdh[i] = NULL; /* init to NULL so we know which
3372 ones contain values */
3377 k = 0; /* counter for the lambdas, etc. arrays */
3378 for (i = 0; i < fr->nblock; i++)
3380 if (fr->block[i].id == enxDH)
3382 int type = (fr->block[i].sub[0].ival[0]);
3383 if (type == dhbtDH || type == dhbtDHDL)
3386 read_edr_rawdh_block(&(samples_rawdh[k]),
3389 start_time, delta_time,
3390 native_lambda, rtemp,
3393 if (samples_rawdh[k])
3395 lambdas[k] = samples_rawdh[k]->foreign_lambda;
3400 else if (fr->block[i].id == enxDHHIST)
3402 int type = (int)(fr->block[i].sub[1].lval[1]);
3403 if (type == dhbtDH || type == dhbtDHDL)
3407 samples_t *s; /* this is where the data will go */
3408 s = read_edr_hist_block(&nb, &(fr->block[i]),
3409 start_time, delta_time,
3410 native_lambda, rtemp,
3415 lambdas[k] = s->foreign_lambda;
3418 /* and insert the new sample immediately */
3419 for (j = 0; j < nb; j++)
3421 lambda_data_list_insert_sample(sd->lb, s+j);
3427 /* Now store all our extant sample collections */
3428 for (i = 0; i < nsamples; i++)
3430 if (samples_rawdh[i])
3432 /* insert it into the existing list */
3433 lambda_data_list_insert_sample(sd->lb, samples_rawdh[i]);
3441 lambda_vec_print(native_lambda, buf, FALSE);
3442 printf("%s: %.1f - %.1f; lambda = %s\n foreign lambdas:\n",
3443 fn, first_t, last_t, buf);
3444 for (i = 0; i < nsamples; i++)
3448 lambda_vec_print(lambdas[i], buf, TRUE);
3451 printf(" %s (%d hists)\n", buf, nhists[i]);
3455 printf(" %s (%d pts)\n", buf, npts[i]);
3467 int gmx_bar(int argc, char *argv[])
3469 static const char *desc[] = {
3470 "[TT]g_bar[tt] calculates free energy difference estimates through ",
3471 "Bennett's acceptance ratio method (BAR). It also automatically",
3472 "adds series of individual free energies obtained with BAR into",
3473 "a combined free energy estimate.[PAR]",
3475 "Every individual BAR free energy difference relies on two ",
3476 "simulations at different states: say state A and state B, as",
3477 "controlled by a parameter, [GRK]lambda[grk] (see the [TT].mdp[tt] parameter",
3478 "[TT]init_lambda[tt]). The BAR method calculates a ratio of weighted",
3479 "average of the Hamiltonian difference of state B given state A and",
3481 "The energy differences to the other state must be calculated",
3482 "explicitly during the simulation. This can be done with",
3483 "the [TT].mdp[tt] option [TT]foreign_lambda[tt].[PAR]",
3485 "Input option [TT]-f[tt] expects multiple [TT]dhdl.xvg[tt] files. ",
3486 "Two types of input files are supported:[BR]",
3487 "[TT]*[tt] Files with more than one [IT]y[it]-value. ",
3488 "The files should have columns ",
3489 "with dH/d[GRK]lambda[grk] and [GRK]Delta[grk][GRK]lambda[grk]. ",
3490 "The [GRK]lambda[grk] values are inferred ",
3491 "from the legends: [GRK]lambda[grk] of the simulation from the legend of ",
3492 "dH/d[GRK]lambda[grk] and the foreign [GRK]lambda[grk] values from the ",
3493 "legends of Delta H",
3495 "[TT]*[tt] Files with only one [IT]y[it]-value. Using the",
3496 "[TT]-extp[tt] option for these files, it is assumed",
3497 "that the [IT]y[it]-value is dH/d[GRK]lambda[grk] and that the ",
3498 "Hamiltonian depends linearly on [GRK]lambda[grk]. ",
3499 "The [GRK]lambda[grk] value of the simulation is inferred from the ",
3500 "subtitle (if present), otherwise from a number in the subdirectory ",
3501 "in the file name.[PAR]",
3503 "The [GRK]lambda[grk] of the simulation is parsed from ",
3504 "[TT]dhdl.xvg[tt] file's legend containing the string 'dH', the ",
3505 "foreign [GRK]lambda[grk] values from the legend containing the ",
3506 "capitalized letters 'D' and 'H'. The temperature is parsed from ",
3507 "the legend line containing 'T ='.[PAR]",
3509 "The input option [TT]-g[tt] expects multiple [TT].edr[tt] files. ",
3510 "These can contain either lists of energy differences (see the ",
3511 "[TT].mdp[tt] option [TT]separate_dhdl_file[tt]), or a series of ",
3512 "histograms (see the [TT].mdp[tt] options [TT]dh_hist_size[tt] and ",
3513 "[TT]dh_hist_spacing[tt]).", "The temperature and [GRK]lambda[grk] ",
3514 "values are automatically deduced from the [TT]ener.edr[tt] file.[PAR]",
3516 "In addition to the [TT].mdp[tt] option [TT]foreign_lambda[tt], ",
3517 "the energy difference can also be extrapolated from the ",
3518 "dH/d[GRK]lambda[grk] values. This is done with the[TT]-extp[tt]",
3519 "option, which assumes that the system's Hamiltonian depends linearly",
3520 "on [GRK]lambda[grk], which is not normally the case.[PAR]",
3522 "The free energy estimates are determined using BAR with bisection, ",
3523 "with the precision of the output set with [TT]-prec[tt]. ",
3524 "An error estimate taking into account time correlations ",
3525 "is made by splitting the data into blocks and determining ",
3526 "the free energy differences over those blocks and assuming ",
3527 "the blocks are independent. ",
3528 "The final error estimate is determined from the average variance ",
3529 "over 5 blocks. A range of block numbers for error estimation can ",
3530 "be provided with the options [TT]-nbmin[tt] and [TT]-nbmax[tt].[PAR]",
3532 "[TT]g_bar[tt] tries to aggregate samples with the same 'native' and ",
3533 "'foreign' [GRK]lambda[grk] values, but always assumes independent ",
3534 "samples. [BB]Note[bb] that when aggregating energy ",
3535 "differences/derivatives with different sampling intervals, this is ",
3536 "almost certainly not correct. Usually subsequent energies are ",
3537 "correlated and different time intervals mean different degrees ",
3538 "of correlation between samples.[PAR]",
3540 "The results are split in two parts: the last part contains the final ",
3541 "results in kJ/mol, together with the error estimate for each part ",
3542 "and the total. The first part contains detailed free energy ",
3543 "difference estimates and phase space overlap measures in units of ",
3544 "kT (together with their computed error estimate). The printed ",
3546 "[TT]*[tt] lam_A: the [GRK]lambda[grk] values for point A.[BR]",
3547 "[TT]*[tt] lam_B: the [GRK]lambda[grk] values for point B.[BR]",
3548 "[TT]*[tt] DG: the free energy estimate.[BR]",
3549 "[TT]*[tt] s_A: an estimate of the relative entropy of B in A.[BR]",
3550 "[TT]*[tt] s_B: an estimate of the relative entropy of A in B.[BR]",
3551 "[TT]*[tt] stdev: an estimate expected per-sample standard deviation.[PAR]",
3553 "The relative entropy of both states in each other's ensemble can be ",
3554 "interpreted as a measure of phase space overlap: ",
3555 "the relative entropy s_A of the work samples of lambda_B in the ",
3556 "ensemble of lambda_A (and vice versa for s_B), is a ",
3557 "measure of the 'distance' between Boltzmann distributions of ",
3558 "the two states, that goes to zero for identical distributions. See ",
3559 "Wu & Kofke, J. Chem. Phys. 123 084109 (2005) for more information.",
3561 "The estimate of the expected per-sample standard deviation, as given ",
3562 "in Bennett's original BAR paper: Bennett, J. Comp. Phys. 22, p 245 (1976).",
3563 "Eq. 10 therein gives an estimate of the quality of sampling (not directly",
3564 "of the actual statistical error, because it assumes independent samples).[PAR]",
3566 "To get a visual estimate of the phase space overlap, use the ",
3567 "[TT]-oh[tt] option to write series of histograms, together with the ",
3568 "[TT]-nbin[tt] option.[PAR]"
3570 static real begin = 0, end = -1, temp = -1;
3571 int nd = 2, nbmin = 5, nbmax = 5;
3573 gmx_bool use_dhdl = FALSE;
3574 gmx_bool calc_s, calc_v;
3576 { "-b", FALSE, etREAL, {&begin}, "Begin time for BAR" },
3577 { "-e", FALSE, etREAL, {&end}, "End time for BAR" },
3578 { "-temp", FALSE, etREAL, {&temp}, "Temperature (K)" },
3579 { "-prec", FALSE, etINT, {&nd}, "The number of digits after the decimal point" },
3580 { "-nbmin", FALSE, etINT, {&nbmin}, "Minimum number of blocks for error estimation" },
3581 { "-nbmax", FALSE, etINT, {&nbmax}, "Maximum number of blocks for error estimation" },
3582 { "-nbin", FALSE, etINT, {&nbin}, "Number of bins for histogram output"},
3583 { "-extp", FALSE, etBOOL, {&use_dhdl}, "Whether to linearly extrapolate dH/dl values to use as energies"}
3587 { efXVG, "-f", "dhdl", ffOPTRDMULT },
3588 { efEDR, "-g", "ener", ffOPTRDMULT },
3589 { efXVG, "-o", "bar", ffOPTWR },
3590 { efXVG, "-oi", "barint", ffOPTWR },
3591 { efXVG, "-oh", "histogram", ffOPTWR }
3593 #define NFILE asize(fnm)
3596 int nf = 0; /* file counter */
3598 int nfile_tot; /* total number of input files */
3603 sim_data_t sim_data; /* the simulation data */
3604 barres_t *results; /* the results */
3605 int nresults; /* number of results in results array */
3608 double prec, dg_tot, dg, sig, dg_tot_max, dg_tot_min;
3610 char dgformat[20], xvg2format[STRLEN], xvg3format[STRLEN];
3611 char buf[STRLEN], buf2[STRLEN];
3612 char ktformat[STRLEN], sktformat[STRLEN];
3613 char kteformat[STRLEN], skteformat[STRLEN];
3616 gmx_bool result_OK = TRUE, bEE = TRUE;
3618 gmx_bool disc_err = FALSE;
3619 double sum_disc_err = 0.; /* discretization error */
3620 gmx_bool histrange_err = FALSE;
3621 double sum_histrange_err = 0.; /* histogram range error */
3622 double stat_err = 0.; /* statistical error */
3624 CopyRight(stderr, argv[0]);
3625 parse_common_args(&argc, argv,
3627 NFILE, fnm, asize(pa), pa, asize(desc), desc, 0, NULL, &oenv);
3629 if (opt2bSet("-f", NFILE, fnm))
3631 nxvgfile = opt2fns(&fxvgnms, "-f", NFILE, fnm);
3633 if (opt2bSet("-g", NFILE, fnm))
3635 nedrfile = opt2fns(&fedrnms, "-g", NFILE, fnm);
3638 sim_data_init(&sim_data);
3640 /* make linked list */
3642 lambda_data_init(lb, 0, 0);
3648 nfile_tot = nxvgfile + nedrfile;
3652 gmx_fatal(FARGS, "No input files!");
3657 gmx_fatal(FARGS, "Can not have negative number of digits");
3659 prec = pow(10, -nd);
3661 snew(partsum, (nbmax+1)*(nbmax+1));
3664 /* read in all files. First xvg files */
3665 for (f = 0; f < nxvgfile; f++)
3667 read_bar_xvg(fxvgnms[f], &temp, &sim_data);
3670 /* then .edr files */
3671 for (f = 0; f < nedrfile; f++)
3673 read_barsim_edr(fedrnms[f], &temp, &sim_data);;
3677 /* fix the times to allow for equilibration */
3678 sim_data_impose_times(&sim_data, begin, end);
3680 if (opt2bSet("-oh", NFILE, fnm))
3682 sim_data_histogram(&sim_data, opt2fn("-oh", NFILE, fnm), nbin, oenv);
3685 /* assemble the output structures from the lambdas */
3686 results = barres_list_create(&sim_data, &nresults, use_dhdl);
3688 sum_disc_err = barres_list_max_disc_err(results, nresults);
3692 printf("\nNo results to calculate.\n");
3696 if (sum_disc_err > prec)
3698 prec = sum_disc_err;
3699 nd = ceil(-log10(prec));
3700 printf("WARNING: setting the precision to %g because that is the minimum\n reasonable number, given the expected discretization error.\n", prec);
3704 /*sprintf(lamformat,"%%6.3f");*/
3705 sprintf( dgformat, "%%%d.%df", 3+nd, nd);
3706 /* the format strings of the results in kT */
3707 sprintf( ktformat, "%%%d.%df", 5+nd, nd);
3708 sprintf( sktformat, "%%%ds", 6+nd);
3709 /* the format strings of the errors in kT */
3710 sprintf( kteformat, "%%%d.%df", 3+nd, nd);
3711 sprintf( skteformat, "%%%ds", 4+nd);
3712 sprintf(xvg2format, "%s %s\n", "%s", dgformat);
3713 sprintf(xvg3format, "%s %s %s\n", "%s", dgformat, dgformat);
3718 if (opt2bSet("-o", NFILE, fnm))
3720 sprintf(buf, "%s (%s)", "\\DeltaG", "kT");
3721 fpb = xvgropen_type(opt2fn("-o", NFILE, fnm), "Free energy differences",
3722 "\\lambda", buf, exvggtXYDY, oenv);
3726 if (opt2bSet("-oi", NFILE, fnm))
3728 sprintf(buf, "%s (%s)", "\\DeltaG", "kT");
3729 fpi = xvgropen(opt2fn("-oi", NFILE, fnm), "Free energy integral",
3730 "\\lambda", buf, oenv);
3740 /* first calculate results */
3743 for (f = 0; f < nresults; f++)
3745 /* Determine the free energy difference with a factor of 10
3746 * more accuracy than requested for printing.
3748 calc_bar(&(results[f]), 0.1*prec, nbmin, nbmax,
3751 if (results[f].dg_disc_err > prec/10.)
3755 if (results[f].dg_histrange_err > prec/10.)
3757 histrange_err = TRUE;
3761 /* print results in kT */
3765 printf("\nTemperature: %g K\n", temp);
3767 printf("\nDetailed results in kT (see help for explanation):\n\n");
3768 printf("%6s ", " lam_A");
3769 printf("%6s ", " lam_B");
3770 printf(sktformat, "DG ");
3773 printf(skteformat, "+/- ");
3777 printf(skteformat, "disc ");
3781 printf(skteformat, "range ");
3783 printf(sktformat, "s_A ");
3786 printf(skteformat, "+/- " );
3788 printf(sktformat, "s_B ");
3791 printf(skteformat, "+/- " );
3793 printf(sktformat, "stdev ");
3796 printf(skteformat, "+/- ");
3799 for (f = 0; f < nresults; f++)
3801 lambda_vec_print_short(results[f].a->native_lambda, buf);
3803 lambda_vec_print_short(results[f].b->native_lambda, buf);
3805 printf(ktformat, results[f].dg);
3809 printf(kteformat, results[f].dg_err);
3814 printf(kteformat, results[f].dg_disc_err);
3819 printf(kteformat, results[f].dg_histrange_err);
3822 printf(ktformat, results[f].sa);
3826 printf(kteformat, results[f].sa_err);
3829 printf(ktformat, results[f].sb);
3833 printf(kteformat, results[f].sb_err);
3836 printf(ktformat, results[f].dg_stddev);
3840 printf(kteformat, results[f].dg_stddev_err);
3844 /* Check for negative relative entropy with a 95% certainty. */
3845 if (results[f].sa < -2*results[f].sa_err ||
3846 results[f].sb < -2*results[f].sb_err)
3854 printf("\nWARNING: Some of these results violate the Second Law of "
3855 "Thermodynamics: \n"
3856 " This is can be the result of severe undersampling, or "
3858 " there is something wrong with the simulations.\n");
3862 /* final results in kJ/mol */
3863 printf("\n\nFinal results in kJ/mol:\n\n");
3865 for (f = 0; f < nresults; f++)
3870 lambda_vec_print_short(results[f].a->native_lambda, buf);
3871 fprintf(fpi, xvg2format, buf, dg_tot);
3877 lambda_vec_print_intermediate(results[f].a->native_lambda,
3878 results[f].b->native_lambda,
3881 fprintf(fpb, xvg3format, buf, results[f].dg, results[f].dg_err);
3885 lambda_vec_print_short(results[f].a->native_lambda, buf);
3886 lambda_vec_print_short(results[f].b->native_lambda, buf2);
3887 printf("%s - %s", buf, buf2);
3890 printf(dgformat, results[f].dg*kT);
3894 printf(dgformat, results[f].dg_err*kT);
3898 printf(" (max. range err. = ");
3899 printf(dgformat, results[f].dg_histrange_err*kT);
3901 sum_histrange_err += results[f].dg_histrange_err*kT;
3905 dg_tot += results[f].dg;
3909 lambda_vec_print_short(results[0].a->native_lambda, buf);
3910 lambda_vec_print_short(results[nresults-1].b->native_lambda, buf2);
3911 printf("%s - %s", buf, buf2);
3914 printf(dgformat, dg_tot*kT);
3917 stat_err = bar_err(nbmin, nbmax, partsum)*kT;
3919 printf(dgformat, max(max(stat_err, sum_disc_err), sum_histrange_err));
3924 printf("\nmaximum discretization error = ");
3925 printf(dgformat, sum_disc_err);
3926 if (bEE && stat_err < sum_disc_err)
3928 printf("WARNING: discretization error (%g) is larger than statistical error.\n Decrease histogram spacing for more accurate results\n", stat_err);
3933 printf("\nmaximum histogram range error = ");
3934 printf(dgformat, sum_histrange_err);
3935 if (bEE && stat_err < sum_histrange_err)
3937 printf("WARNING: histogram range error (%g) is larger than statistical error.\n Increase histogram range for more accurate results\n", stat_err);
3946 lambda_vec_print_short(results[nresults-1].b->native_lambda, buf);
3947 fprintf(fpi, xvg2format, buf, dg_tot);
3951 do_view(oenv, opt2fn_null("-o", NFILE, fnm), "-xydy");
3952 do_view(oenv, opt2fn_null("-oi", NFILE, fnm), "-xydy");