3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2004, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
33 * Green Red Orange Magenta Azure Cyan Skyblue
43 #include "gmx_fatal.h"
59 static int search_str2(int nstr, char **str, char *key)
62 int keylen = strlen(key);
65 while ( (n < keylen) && ((key[n] < '0') || (key[n] > '9')) )
69 for (i = 0; (i < nstr); i++)
71 if (gmx_strncasecmp(str[i], key, n) == 0)
80 int gmx_enemat(int argc, char *argv[])
82 const char *desc[] = {
83 "[TT]g_enemat[tt] extracts an energy matrix from the energy file ([TT]-f[tt]).",
84 "With [TT]-groups[tt] a file must be supplied with on each",
85 "line a group of atoms to be used. For these groups matrix of",
86 "interaction energies will be extracted from the energy file",
87 "by looking for energy groups with names corresponding to pairs",
88 "of groups of atoms, e.g. if your [TT]-groups[tt] file contains:[BR]",
90 "[TT]Protein[tt][BR]",
92 "then energy groups with names like 'Coul-SR:Protein-SOL' and ",
93 "'LJ:Protein-SOL' are expected in the energy file (although",
94 "[TT]g_enemat[tt] is most useful if many groups are analyzed",
95 "simultaneously). Matrices for different energy types are written",
96 "out separately, as controlled by the",
97 "[TT]-[no]coul[tt], [TT]-[no]coulr[tt], [TT]-[no]coul14[tt], ",
98 "[TT]-[no]lj[tt], [TT]-[no]lj14[tt], ",
99 "[TT]-[no]bham[tt] and [TT]-[no]free[tt] options.",
100 "Finally, the total interaction energy energy per group can be ",
101 "calculated ([TT]-etot[tt]).[PAR]",
103 "An approximation of the free energy can be calculated using:",
104 "[MATH]E[SUB]free[sub] = E[SUB]0[sub] + kT [LOG][CHEVRON][EXP](E-E[SUB]0[sub])/kT[exp][chevron][log][math], where '[MATH][CHEVRON][chevron][math]'",
105 "stands for time-average. A file with reference free energies",
106 "can be supplied to calculate the free energy difference",
107 "with some reference state. Group names (e.g. residue names)",
108 "in the reference file should correspond to the group names",
109 "as used in the [TT]-groups[tt] file, but a appended number",
110 "(e.g. residue number) in the [TT]-groups[tt] will be ignored",
113 static gmx_bool bSum = FALSE;
114 static gmx_bool bMeanEmtx = TRUE;
115 static int skip = 0, nlevels = 20;
116 static real cutmax = 1e20, cutmin = -1e20, reftemp = 300.0;
117 static gmx_bool bCoulSR = TRUE, bCoulLR = FALSE, bCoul14 = FALSE;
118 static gmx_bool bLJSR = TRUE, bLJLR = FALSE, bLJ14 = FALSE, bBhamSR = FALSE, bBhamLR = FALSE,
121 { "-sum", FALSE, etBOOL, {&bSum},
122 "Sum the energy terms selected rather than display them all" },
123 { "-skip", FALSE, etINT, {&skip},
124 "Skip number of frames between data points" },
125 { "-mean", FALSE, etBOOL, {&bMeanEmtx},
126 "with [TT]-groups[tt] extracts matrix of mean energies instead of "
127 "matrix for each timestep" },
128 { "-nlevels", FALSE, etINT, {&nlevels}, "number of levels for matrix colors"},
129 { "-max", FALSE, etREAL, {&cutmax}, "max value for energies"},
130 { "-min", FALSE, etREAL, {&cutmin}, "min value for energies"},
131 { "-coulsr", FALSE, etBOOL, {&bCoulSR}, "extract Coulomb SR energies"},
132 { "-coullr", FALSE, etBOOL, {&bCoulLR}, "extract Coulomb LR energies"},
133 { "-coul14", FALSE, etBOOL, {&bCoul14}, "extract Coulomb 1-4 energies"},
134 { "-ljsr", FALSE, etBOOL, {&bLJSR}, "extract Lennard-Jones SR energies"},
135 { "-ljlr", FALSE, etBOOL, {&bLJLR}, "extract Lennard-Jones LR energies"},
136 { "-lj14", FALSE, etBOOL, {&bLJ14}, "extract Lennard-Jones 1-4 energies"},
137 { "-bhamsr", FALSE, etBOOL, {&bBhamSR}, "extract Buckingham SR energies"},
138 { "-bhamlr", FALSE, etBOOL, {&bBhamLR}, "extract Buckingham LR energies"},
139 { "-free", FALSE, etBOOL, {&bFree}, "calculate free energy"},
140 { "-temp", FALSE, etREAL, {&reftemp},
141 "reference temperature for free energy calculation"}
143 /* We will define egSP more energy-groups:
144 egTotal (total energy) */
147 gmx_bool egrp_use[egNR+egSP];
151 gmx_enxnm_t *enm = NULL;
155 gmx_bool bCont, bRef;
156 gmx_bool bCutmax, bCutmin;
157 real **eneset, *time = NULL;
158 int *set, i, j, k, prevk, m = 0, n, nre, nset, nenergy;
159 char **groups = NULL;
160 char groupname[255], fn[255];
162 t_rgb rlo, rhi, rmid;
163 real emax, emid, emin;
164 real ***emat, **etot, *groupnr;
165 double beta, expE, **e, *eaver, *efree = NULL, edum;
167 char **ereflines, **erefres = NULL;
168 real *eref = NULL, *edif = NULL;
173 { efEDR, "-f", NULL, ffOPTRD },
174 { efDAT, "-groups", "groups.dat", ffREAD },
175 { efDAT, "-eref", "eref.dat", ffOPTRD },
176 { efXPM, "-emat", "emat", ffWRITE },
177 { efXVG, "-etot", "energy", ffWRITE }
179 #define NFILE asize(fnm)
181 parse_common_args(&argc, argv, PCA_CAN_VIEW | PCA_CAN_TIME | PCA_BE_NICE,
182 NFILE, fnm, asize(pa), pa, asize(desc), desc, 0, NULL, &oenv);
184 egrp_use[egCOULSR] = bCoulSR;
185 egrp_use[egLJSR] = bLJSR;
186 egrp_use[egBHAMSR] = bBhamSR;
187 egrp_use[egCOULLR] = bCoulLR;
188 egrp_use[egLJLR] = bLJLR;
189 egrp_use[egBHAMLR] = bBhamLR;
190 egrp_use[egCOUL14] = bCoul14;
191 egrp_use[egLJ14] = bLJ14;
192 egrp_use[egTotal] = TRUE;
194 bRef = opt2bSet("-eref", NFILE, fnm);
195 in = open_enx(ftp2fn(efEDR, NFILE, fnm), "r");
196 do_enxnms(in, &nre, &enm);
200 gmx_fatal(FARGS, "No energies!\n");
203 bCutmax = opt2parg_bSet("-max", asize(pa), pa);
204 bCutmin = opt2parg_bSet("-min", asize(pa), pa);
208 /* Read groupnames from input file and construct selection of
209 energy groups from it*/
211 fprintf(stderr, "Will read groupnames from inputfile\n");
212 ngroups = get_lines(opt2fn("-groups", NFILE, fnm), &groups);
213 fprintf(stderr, "Read %d groups\n", ngroups);
214 snew(set, sqr(ngroups)*egNR/2);
217 for (i = 0; (i < ngroups); i++)
219 fprintf(stderr, "\rgroup %d", i);
220 for (j = i; (j < ngroups); j++)
222 for (m = 0; (m < egNR); m++)
226 sprintf(groupname, "%s:%s-%s", egrp_nm[m], groups[i], groups[j]);
228 fprintf(stderr, "\r%-15s %5d", groupname, n);
230 for (k = prevk; (k < prevk+nre); k++)
232 if (strcmp(enm[k%nre].name, groupname) == 0)
240 fprintf(stderr, "WARNING! could not find group %s (%d,%d)"
241 "in energy file\n", groupname, i, j);
251 fprintf(stderr, "\n");
253 snew(eneset, nset+1);
254 fprintf(stderr, "Will select half-matrix of energies with %d elements\n", n);
256 /* Start reading energy frames */
262 bCont = do_enx(in, fr);
265 timecheck = check_times(fr->t);
268 while (bCont && (timecheck < 0));
272 #define DONTSKIP(cnt) (skip) ? ((cnt % skip) == 0) : TRUE
276 fprintf(stderr, "\rRead frame: %d, Time: %.3f", teller, fr->t);
278 if ((nenergy % 1000) == 0)
280 srenew(time, nenergy+1000);
281 for (i = 0; (i <= nset); i++)
283 srenew(eneset[i], nenergy+1000);
286 time[nenergy] = fr->t;
288 for (i = 0; (i < nset); i++)
290 eneset[i][nenergy] = fr->ener[set[i]].e;
291 sum += fr->ener[set[i]].e;
295 eneset[nset][nenergy] = sum;
302 while (bCont && (timecheck == 0));
304 fprintf(stderr, "\n");
306 fprintf(stderr, "Will build energy half-matrix of %d groups, %d elements, "
307 "over %d frames\n", ngroups, nset, nenergy);
309 snew(emat, egNR+egSP);
310 for (j = 0; (j < egNR+egSP); j++)
314 snew(emat[j], ngroups);
315 for (i = 0; (i < ngroups); i++)
317 snew(emat[j][i], ngroups);
321 snew(groupnr, ngroups);
322 for (i = 0; (i < ngroups); i++)
326 rlo.r = 1.0, rlo.g = 0.0, rlo.b = 0.0;
327 rmid.r = 1.0, rmid.g = 1.0, rmid.b = 1.0;
328 rhi.r = 0.0, rhi.g = 0.0, rhi.b = 1.0;
332 for (i = 0; (i < ngroups); i++)
337 for (i = 0; (i < ngroups); i++)
339 for (j = i; (j < ngroups); j++)
341 for (m = 0; (m < egNR); m++)
345 for (k = 0; (k < nenergy); k++)
347 emat[m][i][j] += eneset[n][k];
348 e[i][k] += eneset[n][k]; /* *0.5; */
349 e[j][k] += eneset[n][k]; /* *0.5; */
352 emat[egTotal][i][j] += emat[m][i][j];
353 emat[m][i][j] /= nenergy;
354 emat[m][j][i] = emat[m][i][j];
357 emat[egTotal][i][j] /= nenergy;
358 emat[egTotal][j][i] = emat[egTotal][i][j];
365 fprintf(stderr, "Will read reference energies from inputfile\n");
366 neref = get_lines(opt2fn("-eref", NFILE, fnm), &ereflines);
367 fprintf(stderr, "Read %d reference energies\n", neref);
369 snew(erefres, neref);
370 for (i = 0; (i < neref); i++)
373 sscanf(ereflines[i], "%s %lf", erefres[i], &edum);
377 snew(eaver, ngroups);
378 for (i = 0; (i < ngroups); i++)
380 for (k = 0; (k < nenergy); k++)
386 beta = 1.0/(BOLTZ*reftemp);
387 snew(efree, ngroups);
389 for (i = 0; (i < ngroups); i++)
392 for (k = 0; (k < nenergy); k++)
394 expE += exp(beta*(e[i][k]-eaver[i]));
396 efree[i] = log(expE/nenergy)/beta + eaver[i];
399 n = search_str2(neref, erefres, groups[i]);
402 edif[i] = efree[i]-eref[n];
407 fprintf(stderr, "WARNING: group %s not found "
408 "in reference energies.\n", groups[i]);
418 emid = 0.0; /*(emin+emax)*0.5;*/
419 egrp_nm[egTotal] = "total";
420 for (m = 0; (m < egNR+egSP); m++)
426 for (i = 0; (i < ngroups); i++)
428 for (j = i; (j < ngroups); j++)
430 if (emat[m][i][j] > emax)
432 emax = emat[m][i][j];
434 else if (emat[m][i][j] < emin)
436 emin = emat[m][i][j];
442 fprintf(stderr, "Matrix of %s energy is uniform at %f "
443 "(will not produce output).\n", egrp_nm[m], emax);
447 fprintf(stderr, "Matrix of %s energy ranges from %f to %f\n",
448 egrp_nm[m], emin, emax);
449 if ((bCutmax) || (emax > cutmax))
453 if ((bCutmin) || (emin < cutmin))
457 if ((emax == cutmax) || (emin == cutmin))
459 fprintf(stderr, "Energy range adjusted: %f to %f\n", emin, emax);
462 sprintf(fn, "%s%s", egrp_nm[m], ftp2fn(efXPM, NFILE, fnm));
463 sprintf(label, "%s Interaction Energies", egrp_nm[m]);
464 out = ffopen(fn, "w");
467 write_xpm(out, 0, label, "Energy (kJ/mol)",
468 "Residue Index", "Residue Index",
469 ngroups, ngroups, groupnr, groupnr, emat[m],
470 emid, emax, rmid, rhi, &nlevels);
472 else if (emax <= emid)
474 write_xpm(out, 0, label, "Energy (kJ/mol)",
475 "Residue Index", "Residue Index",
476 ngroups, ngroups, groupnr, groupnr, emat[m],
477 emin, emid, rlo, rmid, &nlevels);
481 write_xpm3(out, 0, label, "Energy (kJ/mol)",
482 "Residue Index", "Residue Index",
483 ngroups, ngroups, groupnr, groupnr, emat[m],
484 emin, emid, emax, rlo, rmid, rhi, &nlevels);
490 snew(etot, egNR+egSP);
491 for (m = 0; (m < egNR+egSP); m++)
493 snew(etot[m], ngroups);
494 for (i = 0; (i < ngroups); i++)
496 for (j = 0; (j < ngroups); j++)
498 etot[m][i] += emat[m][i][j];
503 out = xvgropen(ftp2fn(efXVG, NFILE, fnm), "Mean Energy", "Residue", "kJ/mol",
505 xvgr_legend(out, 0, NULL, oenv);
507 for (m = 0; (m < egNR+egSP); m++)
511 fprintf(out, "@ legend string %d \"%s\"\n", j++, egrp_nm[m]);
516 fprintf(out, "@ legend string %d \"%s\"\n", j++, "Free");
520 fprintf(out, "@ legend string %d \"%s\"\n", j++, "Diff");
522 fprintf(out, "@TYPE xy\n");
523 fprintf(out, "#%3s", "grp");
524 for (m = 0; (m < egNR+egSP); m++)
528 fprintf(out, " %9s", egrp_nm[m]);
533 fprintf(out, " %9s", "Free");
537 fprintf(out, " %9s", "Diff");
540 for (i = 0; (i < ngroups); i++)
542 fprintf(out, "%3.0f", groupnr[i]);
543 for (m = 0; (m < egNR+egSP); m++)
547 fprintf(out, " %9.5g", etot[m][i]);
552 fprintf(out, " %9.5g", efree[i]);
556 fprintf(out, " %9.5g", edif[i]);
564 fprintf(stderr, "While typing at your keyboard, suddenly...\n"
565 "...nothing happens.\nWARNING: Not Implemented Yet\n");
567 out=ftp2FILE(efMAT,NFILE,fnm,"w");
570 for (k=0; (k<nenergy); k++) {
571 for (i=0; (i<ngroups); i++)
572 for (j=i+1; (j<ngroups); j++)
573 emat[i][j]=eneset[n][k];
574 sprintf(label,"t=%.0f ps",time[k]);
575 write_matrix(out,ngroups,1,ngroups,groupnr,emat,label,emin,emax,nlevels);