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.
38 /* This file is completely threadsafe - keep it that way! */
48 #include "gmx_fatal.h"
65 int get_ebin_space(t_ebin *eb,int nener,const char *enm[],const char *unit)
73 srenew(eb->e,eb->nener);
74 srenew(eb->e_sim,eb->nener);
75 srenew(eb->enm,eb->nener);
76 for(i=index; (i<eb->nener); i++)
83 eb->e_sim[i].esum = 0;
84 eb->enm[i].name = strdup(enm[i-index]);
87 eb->enm[i].unit = strdup(unit);
91 /* Determine the unit from the longname.
92 * These units should have been defined in ifunc.c
93 * But even better would be if all interactions functions
94 * return energies and all non-interaction function
95 * entries would be removed from the ifunc array.
98 for(f=0; f<F_NRE; f++)
100 if (strcmp(eb->enm[i].name,
101 interaction_function[f].longname) == 0)
103 /* Only the terms in this list are not energies */
105 case F_DISRESVIOL: u = unit_length; break;
106 case F_ORIRESDEV: u = "obs"; break;
107 case F_TEMP: u = unit_temp_K; break;
109 case F_PRES: u = unit_pres_bar; break;
113 eb->enm[i].unit = strdup(u);
120 void add_ebin(t_ebin *eb,int index,int nener,real ener[],gmx_bool bSum)
123 double e,sum,sigma,invmm,diff;
126 if ((index+nener > eb->nener) || (index < 0))
128 gmx_fatal(FARGS,"%s-%d: Energies out of range: index=%d nener=%d maxener=%d",
129 __FILE__,__LINE__,index,nener,eb->nener);
132 eg = &(eb->e[index]);
134 for(i=0; (i<nener); i++)
141 egs = &(eb->e_sim[index]);
147 for(i=0; (i<nener); i++)
150 eg[i].esum = ener[i];
151 egs[i].esum += ener[i];
156 invmm = (1.0/(double)m)/((double)m+1.0);
158 for(i=0; (i<nener); i++)
160 /* Value for this component */
163 /* first update sigma, then sum */
164 diff = eg[i].esum - m*e;
165 eg[i].eav += diff*diff*invmm;
173 void ebin_increase_count(t_ebin *eb,gmx_bool bSum)
185 void reset_ebin_sums(t_ebin *eb)
189 /* The actual sums are cleared when the next frame is stored */
192 void pr_ebin(FILE *fp,t_ebin *eb,int index,int nener,int nperline,
193 int prmode,gmx_bool bPrHead)
204 gmx_fatal(FARGS,"Invalid index in pr_ebin: %d",index);
212 nener = index + nener;
214 for(i=index; (i<nener) && rc>=0; )
219 for(j=0; (j<nperline) && (i<nener) && rc>=0; j++,i++)
221 if (strncmp(eb->enm[i].name,"Pres",4) == 0)
223 /* Print the pressure unit to avoid confusion */
224 sprintf(buf,"%s (%s)",eb->enm[i].name,unit_pres_bar);
225 rc = fprintf(fp,"%15s",buf);
229 rc = fprintf(fp,"%15s",eb->enm[i].name);
235 rc = fprintf(fp,"\n");
240 for(j=0; (j<nperline) && (i<nener) && rc>=0; j++,i++)
243 case eprNORMAL: ee = eb->e[i].e; break;
244 case eprAVER: ee = eb->e_sim[i].esum/eb->nsum_sim; break;
245 default: gmx_fatal(FARGS,"Invalid print mode %d in pr_ebin",
249 rc = fprintf(fp," %12.5e",ee);
253 rc = fprintf(fp,"\n");
258 gmx_fatal(FARGS,"Cannot write to logfile; maybe you are out of disk space?");
263 int main(int argc,char *argv[])
272 char *ce[NE],*ct[NT],*cs[NS];
273 real e[NE],t[NT],s[NS];
277 for(i=0; (i<NE); i++) {
279 sprintf(buf,"e%d",i);
282 ie=get_ebin_space(eb,NE,ce);
283 add_ebin(eb,ie,NE,e,0);
284 for(i=0; (i<NS); i++) {
286 sprintf(buf,"s%d",i);
289 is=get_ebin_space(eb,NS,cs);
290 add_ebin(eb,is,NS,s,0);
291 for(i=0; (i<NT); i++) {
293 sprintf(buf,"t%d",i);
296 it=get_ebin_space(eb,NT,ct);
297 add_ebin(eb,it,NT,t,0);
300 pr_ebin(stdout,eb,0,-1,5,eprNORMAL,1);
302 printf("Average:\n");
303 pr_ebin(stdout,eb,ie,NE,5,eprAVER,1);
304 pr_ebin(stdout,eb,is,NS,3,eprAVER,1);
305 pr_ebin(stdout,eb,it,NT,4,eprAVER,1);