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) 2013,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.
45 #include "gromacs/commandline/pargs.h"
46 #include "gromacs/fileio/enxio.h"
47 #include "gromacs/fileio/trxio.h"
48 #include "gromacs/gmxana/gmx_ana.h"
49 #include "gromacs/legacyheaders/disre.h"
50 #include "gromacs/legacyheaders/macros.h"
51 #include "gromacs/legacyheaders/names.h"
52 #include "gromacs/legacyheaders/typedefs.h"
53 #include "gromacs/math/vec.h"
54 #include "gromacs/utility/cstringutil.h"
55 #include "gromacs/utility/fatalerror.h"
56 #include "gromacs/utility/smalloc.h"
58 #define TIME_EXPLICIT 0
59 #define TIME_CONTINUE 1
65 static int *select_it(int nre, gmx_enxnm_t *nm, int *nset)
70 gmx_bool bVerbose = TRUE;
72 if ((getenv("GMX_ENER_VERBOSE")) != NULL)
77 fprintf(stderr, "Select the terms you want to scale from the following list\n");
78 fprintf(stderr, "End your selection with 0\n");
82 for (k = 0; (k < nre); )
84 for (j = 0; (j < 4) && (k < nre); j++, k++)
86 fprintf(stderr, " %3d=%14s", k+1, nm[k].name);
88 fprintf(stderr, "\n");
95 if (1 != scanf("%d", &n))
97 gmx_fatal(FARGS, "Cannot read energy term");
99 if ((n > 0) && (n <= nre))
107 for (i = (*nset) = 0; (i < nre); i++)
120 static void sort_files(char **fnms, real *settime, int nfile)
126 for (i = 0; i < nfile; i++)
129 for (j = i+1; j < nfile; j++)
131 if (settime[j] < settime[minidx])
138 timeswap = settime[i];
139 settime[i] = settime[minidx];
140 settime[minidx] = timeswap;
142 fnms[i] = fnms[minidx];
143 fnms[minidx] = chptr;
149 static int scan_ene_files(char **fnms, int nfiles,
150 real *readtime, real *timestep, int *nremax)
152 /* Check number of energy terms and start time of all files */
153 int f, nre, nremin = 0, nresav = 0;
156 char inputstring[STRLEN];
162 for (f = 0; f < nfiles; f++)
164 in = open_enx(fnms[f], "r");
166 do_enxnms(in, &nre, &enm);
183 nremin = std::min(nremin, fr->nre);
184 *nremax = std::max(*nremax, fr->nre);
188 "Energy files don't match, different number of energies:\n"
189 " %s: %d\n %s: %d\n", fnms[f-1], nresav, fnms[f], fr->nre);
191 "\nContinue conversion using only the first %d terms (n/y)?\n"
192 "(you should be sure that the energy terms match)\n", nremin);
193 if (NULL == fgets(inputstring, STRLEN-1, stdin))
195 gmx_fatal(FARGS, "Error reading user input");
197 if (inputstring[0] != 'y' && inputstring[0] != 'Y')
199 fprintf(stderr, "Will not convert\n");
208 fprintf(stderr, "\n");
209 free_enxnms(nre, enm);
219 static void edit_files(char **fnms, int nfiles, real *readtime,
220 real *settime, int *cont_type, gmx_bool bSetTime, gmx_bool bSort)
224 char inputstring[STRLEN], *chptr;
230 fprintf(stderr, "\n\nEnter the new start time:\n\n");
234 fprintf(stderr, "\n\nEnter the new start time for each file.\n"
235 "There are two special options, both disables sorting:\n\n"
236 "c (continue) - The start time is taken from the end\n"
237 "of the previous file. Use it when your continuation run\n"
238 "restarts with t=0 and there is no overlap.\n\n"
239 "l (last) - The time in this file will be changed the\n"
240 "same amount as in the previous. Use it when the time in the\n"
241 "new run continues from the end of the previous one,\n"
242 "since this takes possible overlap into account.\n\n");
245 fprintf(stderr, " File Current start New start\n"
246 "---------------------------------------------------------\n");
248 for (i = 0; i < nfiles; i++)
250 fprintf(stderr, "%25s %10.3f ", fnms[i], readtime[i]);
254 if (NULL == fgets(inputstring, STRLEN-1, stdin))
256 gmx_fatal(FARGS, "Error reading user input");
258 inputstring[std::strlen(inputstring)-1] = 0;
260 if (inputstring[0] == 'c' || inputstring[0] == 'C')
262 cont_type[i] = TIME_CONTINUE;
265 settime[i] = FLT_MAX;
267 else if (inputstring[0] == 'l' ||
268 inputstring[0] == 'L')
270 cont_type[i] = TIME_LAST;
273 settime[i] = FLT_MAX;
277 settime[i] = strtod(inputstring, &chptr);
278 if (chptr == inputstring)
280 fprintf(stderr, "Try that again: ");
284 cont_type[i] = TIME_EXPLICIT;
291 if (cont_type[0] != TIME_EXPLICIT)
293 cont_type[0] = TIME_EXPLICIT;
299 for (i = 0; i < nfiles; i++)
301 settime[i] = readtime[i];
305 if (bSort && (nfiles > 1))
307 sort_files(fnms, settime, nfiles);
311 fprintf(stderr, "Sorting disabled.\n");
315 /* Write out the new order and start times */
316 fprintf(stderr, "\nSummary of files and start times used:\n\n"
318 "-----------------------------------------\n");
319 for (i = 0; i < nfiles; i++)
321 switch (cont_type[i])
324 fprintf(stderr, "%25s %10.3f\n", fnms[i], settime[i]);
327 fprintf(stderr, "%25s Continue from end of last file\n", fnms[i]);
330 fprintf(stderr, "%25s Change by same amount as last file\n", fnms[i]);
334 fprintf(stderr, "\n");
336 settime[nfiles] = FLT_MAX;
337 cont_type[nfiles] = TIME_EXPLICIT;
338 readtime[nfiles] = FLT_MAX;
342 static void copy_ee(t_energy *src, t_energy *dst, int nre)
346 for (i = 0; i < nre; i++)
349 dst[i].esum = src[i].esum;
350 dst[i].eav = src[i].eav;
354 static void update_ee(t_energy *lastee, gmx_int64_t laststep,
355 t_energy *startee, gmx_int64_t startstep,
356 t_energy *ee, int step,
357 t_energy *outee, int nre)
360 double sigmacorr, nom, denom;
361 double prestart_esum;
362 double prestart_sigma;
364 for (i = 0; i < nre; i++)
366 outee[i].e = ee[i].e;
367 /* add statistics from earlier file if present */
370 outee[i].esum = lastee[i].esum+ee[i].esum;
371 nom = (lastee[i].esum*(step+1)-ee[i].esum*(laststep));
372 denom = ((step+1.0)*(laststep)*(step+1.0+laststep));
373 sigmacorr = nom*nom/denom;
374 outee[i].eav = lastee[i].eav+ee[i].eav+sigmacorr;
378 /* otherwise just copy to output */
379 outee[i].esum = ee[i].esum;
380 outee[i].eav = ee[i].eav;
383 /* if we didnt start to write at the first frame
384 * we must compensate the statistics for this
385 * there are laststep frames in the earlier file
386 * and step+1 frames in this one.
390 gmx_int64_t q = laststep+step;
391 gmx_int64_t p = startstep+1;
392 prestart_esum = startee[i].esum-startee[i].e;
393 sigmacorr = prestart_esum-(p-1)*startee[i].e;
394 prestart_sigma = startee[i].eav-
395 sigmacorr*sigmacorr/(p*(p-1));
396 sigmacorr = prestart_esum/(p-1)-
398 outee[i].esum -= prestart_esum;
401 outee[i].eav = outee[i].eav-prestart_sigma-
402 sigmacorr*sigmacorr*((p-1)*q)/(q-p+1);
406 if ((outee[i].eav/(laststep+step+1)) < (GMX_REAL_EPS))
413 static void update_ee_sum(int nre,
414 gmx_int64_t *ee_sum_step,
415 gmx_int64_t *ee_sum_nsteps,
416 gmx_int64_t *ee_sum_nsum,
418 t_enxframe *fr, int out_step)
420 gmx_int64_t nsteps, nsum, fr_nsum;
423 nsteps = *ee_sum_nsteps;
436 for (i = 0; i < nre; i++)
438 ee_sum[i].esum = fr->ener[i].e;
444 for (i = 0; i < nre; i++)
446 ee_sum[i].esum = fr->ener[i].esum;
447 ee_sum[i].eav = fr->ener[i].eav;
453 else if (out_step + *ee_sum_nsum - *ee_sum_step == nsteps + fr->nsteps)
457 for (i = 0; i < nre; i++)
460 dsqr(ee_sum[i].esum/nsum
461 - (ee_sum[i].esum + fr->ener[i].e)/(nsum + 1))*nsum*(nsum + 1);
462 ee_sum[i].esum += fr->ener[i].e;
467 for (i = 0; i < fr->nre; i++)
471 dsqr(ee_sum[i].esum/nsum
472 - (ee_sum[i].esum + fr->ener[i].esum)/(nsum + fr->nsum))*
473 nsum*(nsum + fr->nsum)/static_cast<double>(fr->nsum);
474 ee_sum[i].esum += fr->ener[i].esum;
477 nsteps += fr->nsteps;
484 fprintf(stderr, "\nWARNING: missing energy sums at time %f\n", fr->t);
490 *ee_sum_step = out_step;
491 *ee_sum_nsteps = nsteps;
495 int gmx_eneconv(int argc, char *argv[])
497 const char *desc[] = {
498 "With [IT]multiple files[it] specified for the [TT]-f[tt] option:[PAR]",
499 "Concatenates several energy files in sorted order.",
500 "In the case of double time frames, the one",
501 "in the later file is used. By specifying [TT]-settime[tt] you will be",
502 "asked for the start time of each file. The input files are taken",
503 "from the command line,",
504 "such that the command [TT]gmx eneconv -f *.edr -o fixed.edr[tt] should do",
506 "With [IT]one file[it] specified for [TT]-f[tt]:[PAR]",
507 "Reads one energy file and writes another, applying the [TT]-dt[tt],",
508 "[TT]-offset[tt], [TT]-t0[tt] and [TT]-settime[tt] options and",
509 "converting to a different format if necessary (indicated by file",
511 "[TT]-settime[tt] is applied first, then [TT]-dt[tt]/[TT]-offset[tt]",
512 "followed by [TT]-b[tt] and [TT]-e[tt] to select which frames to write."
514 const char *bugs[] = {
515 "When combining trajectories the sigma and E^2 (necessary for statistics) are not updated correctly. Only the actual energy is correct. One thus has to compute statistics in another way."
517 ener_file_t in = NULL, out = NULL;
518 gmx_enxnm_t *enm = NULL;
520 ener_file_t in, out = NULL;
521 gmx_enxnm_t *enm = NULL;
523 t_enxframe *fr, *fro;
524 gmx_int64_t ee_sum_step = 0, ee_sum_nsteps, ee_sum_nsum;
526 gmx_int64_t lastfilestep, laststep, startstep_file = 0;
528 int nre, nremax, this_nre, nfile, f, i, kkk, nset, *set = NULL;
531 real *readtime, *settime, timestep, tadjust;
532 char buf[22], buf2[22];
534 gmx_bool bNewFile, bFirst, bNewOutput;
536 gmx_bool warned_about_dh = FALSE;
537 t_enxblock *blocks = NULL;
539 int nblocks_alloc = 0;
542 { efEDR, "-f", NULL, ffRDMULT },
543 { efEDR, "-o", "fixed", ffWRITE },
546 #define NFILE asize(fnm)
548 static real delta_t = 0.0, toffset = 0, scalefac = 1;
549 static gmx_bool bSetTime = FALSE;
550 static gmx_bool bSort = TRUE, bError = TRUE;
551 static real begin = -1;
552 static real end = -1;
553 gmx_bool remove_dh = FALSE;
556 { "-b", FALSE, etREAL, {&begin},
557 "First time to use"},
558 { "-e", FALSE, etREAL, {&end},
560 { "-dt", FALSE, etREAL, {&delta_t},
561 "Only write out frame when t MOD dt = offset" },
562 { "-offset", FALSE, etREAL, {&toffset},
563 "Time offset for [TT]-dt[tt] option" },
564 { "-settime", FALSE, etBOOL, {&bSetTime},
565 "Change starting time interactively" },
566 { "-sort", FALSE, etBOOL, {&bSort},
567 "Sort energy files (not frames)"},
568 { "-rmdh", FALSE, etBOOL, {&remove_dh},
569 "Remove free energy block data" },
570 { "-scalefac", FALSE, etREAL, {&scalefac},
571 "Multiply energy component by this factor" },
572 { "-error", FALSE, etBOOL, {&bError},
573 "Stop on errors in the file" }
576 if (!parse_common_args(&argc, argv, 0, NFILE, fnm, asize(pa),
577 pa, asize(desc), desc, asize(bugs), bugs, &oenv))
589 nfile = opt2fns(&fnms, "-f", NFILE, fnm);
593 gmx_fatal(FARGS, "No input files!");
596 snew(settime, nfile+1);
597 snew(readtime, nfile+1);
598 snew(cont_type, nfile+1);
600 nre = scan_ene_files(fnms, nfile, readtime, ×tep, &nremax);
601 edit_files(fnms, nfile, readtime, settime, cont_type, bSetTime, bSort);
605 snew(ee_sum, nremax);
611 snew(fro->ener, nremax);
617 for (f = 0; f < nfile; f++)
621 in = open_enx(fnms[f], "r");
623 do_enxnms(in, &this_nre, &enm);
628 set = select_it(nre, enm, &nset);
631 /* write names to the output file */
632 out = open_enx(opt2fn("-o", NFILE, fnm), "w");
633 do_enxnms(out, &nre, &enm);
636 /* start reading from the next file */
637 while ((fro->t <= (settime[f+1] + GMX_REAL_EPS)) &&
642 startstep_file = fr->step;
643 tadjust = settime[f] - fr->t;
644 if (cont_type[f+1] == TIME_LAST)
646 settime[f+1] = readtime[f+1]-readtime[f]+settime[f];
647 cont_type[f+1] = TIME_EXPLICIT;
652 if (tadjust + fr->t <= last_t)
654 /* Skip this frame, since we already have it / past it */
657 fprintf(debug, "fr->step %s, fr->t %.4f\n",
658 gmx_step_str(fr->step, buf), fr->t);
659 fprintf(debug, "tadjust %12.6e + fr->t %12.6e <= t %12.6e\n",
660 tadjust, fr->t, last_t);
665 fro->step = lastfilestep + fr->step - startstep_file;
666 fro->t = tadjust + fr->t;
668 bWrite = ((begin < 0 || (begin >= 0 && (fro->t >= begin-GMX_REAL_EPS))) &&
669 (end < 0 || (end >= 0 && (fro->t <= end +GMX_REAL_EPS))) &&
670 (fro->t <= settime[f+1]+0.5*timestep));
675 "fr->step %s, fr->t %.4f, fro->step %s fro->t %.4f, w %d\n",
676 gmx_step_str(fr->step, buf), fr->t,
677 gmx_step_str(fro->step, buf2), fro->t, bWrite);
682 if ((end > 0) && (fro->t > end+GMX_REAL_EPS))
689 if (fro->t >= begin-GMX_REAL_EPS)
697 update_ee_sum(nre, &ee_sum_step, &ee_sum_nsteps, &ee_sum_nsum, ee_sum,
702 /* determine if we should write it */
703 if (bWrite && (delta_t == 0 || bRmod(fro->t, toffset, delta_t)))
705 laststep = fro->step;
710 fprintf(stderr, "\nContinue writing frames from t=%g, step=%s\n",
711 fro->t, gmx_step_str(fro->step, buf));
714 /* Copy the energies */
715 for (i = 0; i < nre; i++)
717 fro->ener[i].e = fr->ener[i].e;
720 fro->nsteps = ee_sum_nsteps;
723 if (ee_sum_nsum <= 1)
729 fro->nsum = gmx_int64_to_int(ee_sum_nsum,
730 "energy average summation");
731 /* Copy the energy sums */
732 for (i = 0; i < nre; i++)
734 fro->ener[i].esum = ee_sum[i].esum;
735 fro->ener[i].eav = ee_sum[i].eav;
738 /* We wrote the energies, so reset the counts */
744 for (kkk = 0; kkk < nset; kkk++)
746 fro->ener[set[kkk]].e *= scalefac;
749 fro->ener[set[kkk]].eav *= scalefac*scalefac;
750 fro->ener[set[kkk]].esum *= scalefac;
754 /* Copy restraint stuff */
755 /*fro->ndisre = fr->ndisre;
756 fro->disre_rm3tav = fr->disre_rm3tav;
757 fro->disre_rt = fr->disre_rt;*/
758 fro->nblock = fr->nblock;
759 /*fro->nr = fr->nr;*/
760 fro->block = fr->block;
762 /* check if we have blocks with delta_h data and are throwing
769 if (!blocks || nblocks_alloc < fr->nblock)
771 /* we pre-allocate the blocks */
772 nblocks_alloc = fr->nblock;
773 snew(blocks, nblocks_alloc);
775 nblocks = 0; /* number of blocks so far */
777 for (i = 0; i < fr->nblock; i++)
779 if ( (fr->block[i].id != enxDHCOLL) &&
780 (fr->block[i].id != enxDH) &&
781 (fr->block[i].id != enxDHHIST) )
783 /* copy everything verbatim */
784 blocks[nblocks] = fr->block[i];
788 /* now set the block pointer to the new blocks */
789 fro->nblock = nblocks;
792 else if (delta_t > 0)
794 if (!warned_about_dh)
796 for (i = 0; i < fr->nblock; i++)
798 if (fr->block[i].id == enxDH ||
799 fr->block[i].id == enxDHHIST)
802 if (fr->block[i].id == enxDH)
804 size = fr->block[i].sub[2].nr;
812 printf("\nWARNING: %s contains delta H blocks or histograms for which\n"
813 " some data is thrown away on a block-by-block basis, where each block\n"
814 " contains up to %d samples.\n"
815 " This is almost certainly not what you want.\n"
816 " Use the -rmdh option to throw all delta H samples away.\n"
817 " Use g_energy -odh option to extract these samples.\n",
819 warned_about_dh = TRUE;
829 if (noutfr % 1000 == 0)
831 fprintf(stderr, "Writing frame time %g ", fro->t);
840 printf("\nLast step written from %s: t %g, step %s\n",
841 fnms[f], last_t, gmx_step_str(laststep, buf));
842 lastfilestep = laststep;
844 /* set the next time from the last in previous file */
845 if (cont_type[f+1] == TIME_CONTINUE)
847 settime[f+1] = fro->t;
848 /* in this case we have already written the last frame of
849 * previous file, so update begin to avoid doubling it
850 * with the start of the next file
852 begin = fro->t+0.5*timestep;
853 /* cont_type[f+1]==TIME_EXPLICIT; */
856 if ((fro->t < end) && (f < nfile-1) &&
857 (fro->t < settime[f+1]-1.5*timestep))
860 "\nWARNING: There might be a gap around t=%g\n", fro->t);
863 /* move energies to lastee */
865 free_enxnms(this_nre, enm);
867 fprintf(stderr, "\n");
871 fprintf(stderr, "No frames written.\n");
875 fprintf(stderr, "Last frame written was at step %s, time %f\n",
876 gmx_step_str(fro->step, buf), fro->t);
877 fprintf(stderr, "Wrote %d frames\n", noutfr);