Removed include of types/ifunc.h from typedefs.h
[gromacs.git] / src / gromacs / mdlib / ebin.cpp
blob22b21d42510b64f0ccaffe44074b68e146a88c42
1 /*
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 * Copyright (c) 2012,2014,2015, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
37 /* This file is completely threadsafe - keep it that way! */
38 #include "gmxpre.h"
40 #include "gromacs/legacyheaders/ebin.h"
42 #include <math.h>
43 #include <string.h>
45 #include "gromacs/legacyheaders/typedefs.h"
46 #include "gromacs/legacyheaders/types/ifunc.h"
47 #include "gromacs/math/units.h"
48 #include "gromacs/math/utilities.h"
49 #include "gromacs/math/vec.h"
50 #include "gromacs/utility/cstringutil.h"
51 #include "gromacs/utility/fatalerror.h"
52 #include "gromacs/utility/smalloc.h"
54 t_ebin *mk_ebin(void)
56 t_ebin *eb;
58 snew(eb, 1);
60 return eb;
63 int get_ebin_space(t_ebin *eb, int nener, const char *enm[], const char *unit)
65 int index;
66 int i, f;
67 const char *u;
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++)
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 = gmx_strdup(enm[i-index]);
83 if (unit != NULL)
85 eb->enm[i].unit = gmx_strdup(unit);
87 else
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.
95 u = unit_energy;
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 */
102 switch (f)
104 case F_DISRESVIOL: u = unit_length; break;
105 case F_ORIRESDEV: u = "obs"; break;
106 case F_TEMP: u = unit_temp_K; break;
107 case F_PDISPCORR:
108 case F_PRES: u = unit_pres_bar; break;
112 eb->enm[i].unit = gmx_strdup(u);
116 return index;
119 void add_ebin(t_ebin *eb, int index, int nener, real ener[], gmx_bool bSum)
121 int i, m;
122 double e, invmm, diff;
123 t_energy *eg, *egs;
125 if ((index+nener > eb->nener) || (index < 0))
127 gmx_fatal(FARGS, "%s-%d: Energies out of range: index=%d nener=%d maxener=%d",
128 __FILE__, __LINE__, index, nener, eb->nener);
131 eg = &(eb->e[index]);
133 for (i = 0; (i < nener); i++)
135 eg[i].e = ener[i];
138 if (bSum)
140 egs = &(eb->e_sim[index]);
142 m = eb->nsum;
144 if (m == 0)
146 for (i = 0; (i < nener); i++)
148 eg[i].eav = 0;
149 eg[i].esum = ener[i];
150 egs[i].esum += ener[i];
153 else
155 invmm = (1.0/(double)m)/((double)m+1.0);
157 for (i = 0; (i < nener); i++)
159 /* Value for this component */
160 e = ener[i];
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;
172 void ebin_increase_count(t_ebin *eb, gmx_bool bSum)
174 eb->nsteps++;
175 eb->nsteps_sim++;
177 if (bSum)
179 eb->nsum++;
180 eb->nsum_sim++;
184 void reset_ebin_sums(t_ebin *eb)
186 eb->nsteps = 0;
187 eb->nsum = 0;
188 /* The actual sums are cleared when the next frame is stored */
191 void pr_ebin(FILE *fp, t_ebin *eb, int index, int nener, int nperline,
192 int prmode, gmx_bool bPrHead)
194 int i, j, i0;
195 real ee = 0;
196 int rc;
197 char buf[30];
199 rc = 0;
201 if (index < 0)
203 gmx_fatal(FARGS, "Invalid index in pr_ebin: %d", index);
205 if (nener == -1)
207 nener = eb->nener;
209 else
211 nener = index + nener;
213 for (i = index; (i < nener) && rc >= 0; )
215 if (bPrHead)
217 i0 = i;
218 for (j = 0; (j < nperline) && (i < nener) && rc >= 0; j++, i++)
220 if (strncmp(eb->enm[i].name, "Pres", 4) == 0)
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);
226 else
228 rc = fprintf(fp, "%15s", eb->enm[i].name);
232 if (rc >= 0)
234 rc = fprintf(fp, "\n");
237 i = i0;
239 for (j = 0; (j < nperline) && (i < nener) && rc >= 0; j++, i++)
241 switch (prmode)
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",
246 prmode);
249 rc = fprintf(fp, " %12.5e", ee);
251 if (rc >= 0)
253 rc = fprintf(fp, "\n");
256 if (rc < 0)
258 gmx_fatal(FARGS, "Cannot write to logfile; maybe you are out of disk space?");
262 #ifdef DEBUGEBIN
263 int main(int argc, char *argv[])
265 #define NE 12
266 #define NT 7
267 #define NS 5
269 t_ebin *eb;
270 int i;
271 char buf[25];
272 char *ce[NE], *ct[NT], *cs[NS];
273 real e[NE], t[NT], s[NS];
274 int ie, it, is;
276 eb = mk_ebin();
277 for (i = 0; (i < NE); i++)
279 e[i] = i;
280 sprintf(buf, "e%d", i);
281 ce[i] = gmx_strdup(buf);
283 ie = get_ebin_space(eb, NE, ce);
284 add_ebin(eb, ie, NE, e, 0);
285 for (i = 0; (i < NS); i++)
287 s[i] = i;
288 sprintf(buf, "s%d", i);
289 cs[i] = gmx_strdup(buf);
291 is = get_ebin_space(eb, NS, cs);
292 add_ebin(eb, is, NS, s, 0);
293 for (i = 0; (i < NT); i++)
295 t[i] = i;
296 sprintf(buf, "t%d", i);
297 ct[i] = gmx_strdup(buf);
299 it = get_ebin_space(eb, NT, ct);
300 add_ebin(eb, it, NT, t, 0);
302 printf("Normal:\n");
303 pr_ebin(stdout, eb, 0, -1, 5, eprNORMAL, 1);
305 printf("Average:\n");
306 pr_ebin(stdout, eb, ie, NE, 5, eprAVER, 1);
307 pr_ebin(stdout, eb, is, NS, 3, eprAVER, 1);
308 pr_ebin(stdout, eb, it, NT, 4, eprAVER, 1);
310 #endif