1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
11 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13 * Copyright (c) 2001-2004, The GROMACS development team,
14 * check out http://www.gromacs.org for more information.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * If you want to redistribute modifications, please consider that
22 * scientific software is very special. Version control is crucial -
23 * bugs must be traceable. We will be happy to consider code for
24 * inclusion in the official distribution, but derived work must not
25 * be called official GROMACS. Details are found in the README & COPYING
26 * files - if they are missing, get the official version at www.gromacs.org.
28 * To help us fund GROMACS development, we humbly ask that you cite
29 * the papers on the package - you can find them in the top README file.
31 * For more info, check our website at http://www.gromacs.org
34 * GROwing Monsters And Cloning Shrimps
36 /* This file is completely threadsafe - keep it that way! */
46 #include "gmx_fatal.h"
63 int get_ebin_space(t_ebin *eb,int nener,const char *enm[],const char *unit)
71 srenew(eb->e,eb->nener);
72 srenew(eb->e_sim,eb->nener);
73 srenew(eb->enm,eb->nener);
74 for(i=index; (i<eb->nener); i++)
81 eb->e_sim[i].esum = 0;
82 eb->enm[i].name = strdup(enm[i-index]);
85 eb->enm[i].unit = strdup(unit);
89 /* Determine the unit from the longname.
90 * These units should have been defined in ifunc.c
91 * But even better would be if all interactions functions
92 * return energies and all non-interaction function
93 * entries would be removed from the ifunc array.
96 for(f=0; f<F_NRE; f++)
98 if (strcmp(eb->enm[i].name,
99 interaction_function[f].longname) == 0)
101 /* Only the terms in this list are not energies */
103 case F_DISRESVIOL: u = unit_length; break;
104 case F_ORIRESDEV: u = "obs"; break;
105 case F_TEMP: u = unit_temp_K; break;
107 case F_PRES: u = unit_pres_bar; break;
111 eb->enm[i].unit = strdup(u);
118 void add_ebin(t_ebin *eb,int index,int nener,real ener[],gmx_bool bSum)
121 double e,sum,sigma,invmm,diff;
124 if ((index+nener > eb->nener) || (index < 0))
126 gmx_fatal(FARGS,"%s-%d: Energies out of range: index=%d nener=%d maxener=%d",
127 __FILE__,__LINE__,index,nener,eb->nener);
130 eg = &(eb->e[index]);
132 for(i=0; (i<nener); i++)
139 egs = &(eb->e_sim[index]);
145 for(i=0; (i<nener); i++)
148 eg[i].esum = ener[i];
149 egs[i].esum += ener[i];
154 invmm = (1.0/(double)m)/((double)m+1.0);
156 for(i=0; (i<nener); i++)
158 /* Value for this component */
161 /* first update sigma, then sum */
162 diff = eg[i].esum - m*e;
163 eg[i].eav += diff*diff*invmm;
171 void ebin_increase_count(t_ebin *eb,gmx_bool bSum)
183 void reset_ebin_sums(t_ebin *eb)
187 /* The actual sums are cleared when the next frame is stored */
190 void pr_ebin(FILE *fp,t_ebin *eb,int index,int nener,int nperline,
191 int prmode,gmx_bool bPrHead)
202 gmx_fatal(FARGS,"Invalid index in pr_ebin: %d",index);
210 nener = index + nener;
212 for(i=index; (i<nener) && rc>=0; )
217 for(j=0; (j<nperline) && (i<nener) && rc>=0; j++,i++)
219 if (strncmp(eb->enm[i].name,"Pres",4) == 0)
221 /* Print the pressure unit to avoid confusion */
222 sprintf(buf,"%s (%s)",eb->enm[i].name,unit_pres_bar);
223 rc = fprintf(fp,"%15s",buf);
227 rc = fprintf(fp,"%15s",eb->enm[i].name);
233 rc = fprintf(fp,"\n");
238 for(j=0; (j<nperline) && (i<nener) && rc>=0; j++,i++)
241 case eprNORMAL: ee = eb->e[i].e; break;
242 case eprAVER: ee = eb->e_sim[i].esum/eb->nsum_sim; break;
243 default: gmx_fatal(FARGS,"Invalid print mode %d in pr_ebin",
247 rc = fprintf(fp," %12.5e",ee);
251 rc = fprintf(fp,"\n");
256 gmx_fatal(FARGS,"Cannot write to logfile; maybe you are out of disk space?");
261 int main(int argc,char *argv[])
270 char *ce[NE],*ct[NT],*cs[NS];
271 real e[NE],t[NT],s[NS];
275 for(i=0; (i<NE); i++) {
277 sprintf(buf,"e%d",i);
280 ie=get_ebin_space(eb,NE,ce);
281 add_ebin(eb,ie,NE,e,0);
282 for(i=0; (i<NS); i++) {
284 sprintf(buf,"s%d",i);
287 is=get_ebin_space(eb,NS,cs);
288 add_ebin(eb,is,NS,s,0);
289 for(i=0; (i<NT); i++) {
291 sprintf(buf,"t%d",i);
294 it=get_ebin_space(eb,NT,ct);
295 add_ebin(eb,it,NT,t,0);
298 pr_ebin(stdout,eb,0,-1,5,eprNORMAL,1);
300 printf("Average:\n");
301 pr_ebin(stdout,eb,ie,NE,5,eprAVER,1);
302 pr_ebin(stdout,eb,is,NS,3,eprAVER,1);
303 pr_ebin(stdout,eb,it,NT,4,eprAVER,1);