Merge remote-tracking branch 'gerrit/release-4-6'
[alexxy/gromacs.git] / src / gromacs / mdlib / ebin.c
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
2  *
3  * 
4  *                This source code is part of
5  * 
6  *                 G   R   O   M   A   C   S
7  * 
8  *          GROningen MAchine for Chemical Simulations
9  * 
10  *                        VERSION 3.2.0
11  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13  * Copyright (c) 2001-2004, The GROMACS development team,
14  * check out http://www.gromacs.org for more information.
15
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  * 
21  * If you want to redistribute modifications, please consider that
22  * scientific software is very special. Version control is crucial -
23  * bugs must be traceable. We will be happy to consider code for
24  * inclusion in the official distribution, but derived work must not
25  * be called official GROMACS. Details are found in the README & COPYING
26  * files - if they are missing, get the official version at www.gromacs.org.
27  * 
28  * To help us fund GROMACS development, we humbly ask that you cite
29  * the papers on the package - you can find them in the top README file.
30  * 
31  * For more info, check our website at http://www.gromacs.org
32  * 
33  * And Hey:
34  * GROwing Monsters And Cloning Shrimps
35  */
36 /* This file is completely threadsafe - keep it that way! */
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #include <math.h>
42 #include <string.h>
43 #include "sysstuff.h"
44 #include "smalloc.h"
45 #include "typedefs.h"
46 #include "gmx_fatal.h"
47 #include "string2.h"
48 #include "ebin.h"
49 #include "main.h"
50 #include "maths.h"
51 #include "vec.h"
52 #include "physics.h"
53
54 t_ebin *mk_ebin(void)
55 {
56   t_ebin *eb;
57   
58   snew(eb,1);
59   
60   return eb;
61 }
62
63 int get_ebin_space(t_ebin *eb,int nener,const char *enm[],const char *unit)
64 {
65     int  index;
66     int  i,f;
67     const char *u;
68
69     index = eb->nener;
70     eb->nener += nener;
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++)
75     {
76         eb->e[i].e        = 0;
77         eb->e[i].eav      = 0;
78         eb->e[i].esum     = 0;
79         eb->e_sim[i].e    = 0;
80         eb->e_sim[i].eav  = 0;
81         eb->e_sim[i].esum = 0;
82         eb->enm[i].name = strdup(enm[i-index]);
83         if (unit != NULL)
84         {
85             eb->enm[i].unit = strdup(unit);
86         }
87         else
88         {
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.
94              */
95             u = unit_energy;
96             for(f=0; f<F_NRE; f++)
97             {
98                 if (strcmp(eb->enm[i].name,
99                            interaction_function[f].longname) == 0)
100                 {
101                     /* Only the terms in this list are not energies */
102                     switch (f) {
103                     case F_DISRESVIOL: u = unit_length;   break;
104                     case F_ORIRESDEV:  u = "obs";         break;
105                     case F_TEMP:       u = unit_temp_K;   break;
106                     case F_VTEMP:       u = unit_temp_K;   break;
107                     case F_PDISPCORR:
108                     case F_PRES:       u = unit_pres_bar; break;
109                     }
110                 }
111             }
112             eb->enm[i].unit = strdup(u);
113         }
114     }
115     
116     return index;
117 }
118
119 void add_ebin(t_ebin *eb,int index,int nener,real ener[],gmx_bool bSum)
120 {
121     int      i,m;
122     double   e,sum,sigma,invmm,diff;
123     t_energy *eg,*egs;
124     
125     if ((index+nener > eb->nener) || (index < 0))
126     {
127         gmx_fatal(FARGS,"%s-%d: Energies out of range: index=%d nener=%d maxener=%d",
128                   __FILE__,__LINE__,index,nener,eb->nener);
129     }
130     
131     eg = &(eb->e[index]);
132     
133     for(i=0; (i<nener); i++)
134     {
135         eg[i].e      = ener[i];
136     }
137     
138     if (bSum)
139     {
140         egs = &(eb->e_sim[index]);
141         
142         m = eb->nsum;
143         
144         if (m == 0)
145         {
146             for(i=0; (i<nener); i++)
147             {
148                 eg[i].eav    = 0;
149                 eg[i].esum   = ener[i];
150                 egs[i].esum += ener[i];
151             }
152         }
153         else
154         {
155             invmm = (1.0/(double)m)/((double)m+1.0);
156             
157             for(i=0; (i<nener); i++)
158             {
159                 /* Value for this component */
160                 e = ener[i];
161                 
162                 /* first update sigma, then sum */
163                 diff         = eg[i].esum - m*e;
164                 eg[i].eav   += diff*diff*invmm;
165                 eg[i].esum  += e;
166                 egs[i].esum += e;
167             }
168         }
169     }
170 }
171
172 void ebin_increase_count(t_ebin *eb,gmx_bool bSum)
173 {
174     eb->nsteps++;
175     eb->nsteps_sim++;
176
177     if (bSum)
178     {
179         eb->nsum++;
180         eb->nsum_sim++;
181     }
182 }
183
184 void reset_ebin_sums(t_ebin *eb)
185 {
186     eb->nsteps = 0;
187     eb->nsum   = 0;
188     /* The actual sums are cleared when the next frame is stored */
189 }
190
191 void pr_ebin(FILE *fp,t_ebin *eb,int index,int nener,int nperline,
192              int prmode,gmx_bool bPrHead)
193 {
194     int  i,j,i0;
195     real ee=0;
196     int  rc;
197     char buf[30];
198
199     rc = 0;
200
201     if (index < 0)
202     {
203         gmx_fatal(FARGS,"Invalid index in pr_ebin: %d",index);
204     }
205     if (nener == -1)
206     {
207         nener = eb->nener;
208     }
209     else
210     {
211         nener = index + nener;
212     }
213     for(i=index; (i<nener) && rc>=0; ) 
214     {
215         if (bPrHead)
216         {
217             i0=i;
218             for(j=0; (j<nperline) && (i<nener) && rc>=0; j++,i++)
219             {
220                 if (strncmp(eb->enm[i].name,"Pres",4) == 0)
221                 {
222                     /* Print the pressure unit to avoid confusion */
223                     sprintf(buf,"%s (%s)",eb->enm[i].name,unit_pres_bar);
224                     rc = fprintf(fp,"%15s",buf);
225                 }
226                 else
227                 {
228                     rc = fprintf(fp,"%15s",eb->enm[i].name);
229                 }
230             }
231
232             if (rc >= 0)
233             {
234                 rc = fprintf(fp,"\n");
235             }
236
237             i=i0;
238         }
239         for(j=0; (j<nperline) && (i<nener) && rc>=0; j++,i++)
240         {
241             switch (prmode) {
242                 case eprNORMAL: ee = eb->e[i].e; break;
243                 case eprAVER:   ee = eb->e_sim[i].esum/eb->nsum_sim; break;
244                 default: gmx_fatal(FARGS,"Invalid print mode %d in pr_ebin",
245                                    prmode);
246             }
247
248             rc = fprintf(fp,"   %12.5e",ee);
249         }
250         if (rc >= 0)
251         {
252             rc = fprintf(fp,"\n");
253         }
254     }
255     if (rc < 0)
256     { 
257         gmx_fatal(FARGS,"Cannot write to logfile; maybe you are out of disk space?");
258     }
259 }
260
261 #ifdef DEBUGEBIN
262 int main(int argc,char *argv[])
263 {
264 #define NE 12
265 #define NT 7
266 #define NS 5
267
268   t_ebin *eb;
269   int    i;
270   char   buf[25];
271   char   *ce[NE],*ct[NT],*cs[NS];
272   real   e[NE],t[NT],s[NS];
273   int    ie,it,is;
274   
275   eb=mk_ebin();
276   for(i=0; (i<NE); i++) {
277     e[i]=i;
278     sprintf(buf,"e%d",i);
279     ce[i]=strdup(buf);
280   }
281   ie=get_ebin_space(eb,NE,ce);
282   add_ebin(eb,ie,NE,e,0);
283   for(i=0; (i<NS); i++) {
284     s[i]=i;
285     sprintf(buf,"s%d",i);
286     cs[i]=strdup(buf);
287   }
288   is=get_ebin_space(eb,NS,cs);
289   add_ebin(eb,is,NS,s,0);
290   for(i=0; (i<NT); i++) {
291     t[i]=i;
292     sprintf(buf,"t%d",i);
293     ct[i]=strdup(buf);
294   }
295   it=get_ebin_space(eb,NT,ct);
296   add_ebin(eb,it,NT,t,0);
297   
298   printf("Normal:\n");
299   pr_ebin(stdout,eb,0,-1,5,eprNORMAL,1);
300
301   printf("Average:\n");
302   pr_ebin(stdout,eb,ie,NE,5,eprAVER,1);
303   pr_ebin(stdout,eb,is,NS,3,eprAVER,1);
304   pr_ebin(stdout,eb,it,NT,4,eprAVER,1);
305 }
306 #endif