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, 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.
48 #ifdef GMX_USE_PLUGINS
51 #include "gromacs/utility/smalloc.h"
58 #include "tngio_for_tools.h"
65 #include "checkpoint.h"
68 #include "gromacs/fileio/timecontrol.h"
69 #include "gromacs/legacyheaders/gmx_fatal.h"
71 /* defines for frame counter output */
86 char *persistent_line; /* Persistent line for reading g96 trajectories */
89 /* utility functions */
91 gmx_bool bRmod_fd(double a, double b, double c, gmx_bool bDouble)
96 tol = 2*(bDouble ? GMX_DOUBLE_EPS : GMX_FLOAT_EPS);
98 iq = (int)((a - b + tol*a)/c);
100 if (fabs(a - b - c*iq) <= tol*fabs(a))
112 int check_times2(real t, real t0, gmx_bool bDouble)
117 /* since t is float, we can not use double precision for bRmod */
122 if ((!bTimeSet(TBEGIN) || (t >= rTimeValue(TBEGIN))) &&
123 (!bTimeSet(TEND) || (t <= rTimeValue(TEND))))
125 if (bTimeSet(TDELTA) && !bRmod_fd(t, t0, rTimeValue(TDELTA), bDouble))
134 else if (bTimeSet(TEND) && (t >= rTimeValue(TEND)))
140 fprintf(debug, "t=%g, t0=%g, b=%g, e=%g, dt=%g: r=%d\n",
141 t, t0, rTimeValue(TBEGIN), rTimeValue(TEND), rTimeValue(TDELTA), r);
146 int check_times(real t)
148 return check_times2(t, t, FALSE);
151 static void initcount(t_trxstatus *status)
153 status->__frame = -1;
156 static void status_init(t_trxstatus *status)
159 status->xframe = NULL;
161 status->__frame = -1;
162 status->persistent_line = NULL;
167 int nframes_read(t_trxstatus *status)
169 return status->__frame;
172 static void printcount_(t_trxstatus *status, const output_env_t oenv,
173 const char *l, real t)
175 if ((status->__frame < 2*SKIP1 || status->__frame % SKIP1 == 0) &&
176 (status->__frame < 2*SKIP2 || status->__frame % SKIP2 == 0) &&
177 (status->__frame < 2*SKIP3 || status->__frame % SKIP3 == 0))
179 fprintf(stderr, "\r%-14s %6d time %8.3f ", l, status->__frame,
180 output_env_conv_time(oenv, t));
184 static void printcount(t_trxstatus *status, const output_env_t oenv, real t,
188 printcount_(status, oenv, bSkip ? "Skipping frame" : "Reading frame", t);
191 static void printlast(t_trxstatus *status, const output_env_t oenv, real t)
193 printcount_(status, oenv, "Last frame", t);
194 fprintf(stderr, "\n");
197 static void printincomp(t_trxstatus *status, t_trxframe *fr)
199 if (fr->not_ok & HEADER_NOT_OK)
201 fprintf(stderr, "WARNING: Incomplete header: nr %d time %g\n",
202 status->__frame+1, fr->time);
206 fprintf(stderr, "WARNING: Incomplete frame: nr %d time %g\n",
207 status->__frame+1, fr->time);
211 int prec2ndec(real prec)
215 gmx_fatal(FARGS, "DEATH HORROR prec (%g) <= 0 in prec2ndec", prec);
218 return (int)(log(prec)/log(10.0)+0.5);
221 real ndec2prec(int ndec)
223 return pow(10.0, ndec);
226 t_fileio *trx_get_fileio(t_trxstatus *status)
231 float trx_get_time_of_final_frame(t_trxstatus *status)
233 t_fileio *stfio = trx_get_fileio(status);
234 int filetype = gmx_fio_getftp(stfio);
238 if (filetype == efXTC)
241 xdr_xtc_get_last_frame_time(gmx_fio_getfp(stfio),
242 gmx_fio_getxdr(stfio),
243 status->xframe->natoms, &bOK);
246 gmx_fatal(FARGS, "Error reading last frame. Maybe seek not supported." );
249 else if (filetype == efTNG)
251 tng_trajectory_t tng = status->tng;
254 gmx_fatal(FARGS, "Error opening TNG file.");
256 lasttime = gmx_tng_get_time_of_final_frame(tng);
260 gmx_incons("Only supported for TNG and XTC");
265 void clear_trxframe(t_trxframe *fr, gmx_bool bFirst)
272 fr->bFepState = FALSE;
304 void set_trxframe_ePBC(t_trxframe *fr, int ePBC)
306 fr->bPBC = (ePBC == -1);
310 int write_trxframe_indexed(t_trxstatus *status, t_trxframe *fr, int nind,
311 const atom_id *ind, gmx_conect gc)
314 rvec *xout = NULL, *vout = NULL, *fout = NULL;
331 else if (status->fio)
333 ftp = gmx_fio_getftp(status->fio);
337 gmx_incons("No input file available");
349 gmx_fatal(FARGS, "Need coordinates to write a %s trajectory",
363 for (i = 0; i < nind; i++)
365 copy_rvec(fr->v[ind[i]], vout[i]);
371 for (i = 0; i < nind; i++)
373 copy_rvec(fr->f[ind[i]], fout[i]);
381 for (i = 0; i < nind; i++)
383 copy_rvec(fr->x[ind[i]], xout[i]);
394 gmx_write_tng_from_trxframe(status->tng, fr, nind);
397 write_xtc(status->fio, nind, fr->step, fr->time, fr->box, xout, prec);
401 fwrite_trn(status->fio, nframes_read(status),
402 fr->time, fr->step, fr->box, nind, xout, vout, fout);
410 gmx_fatal(FARGS, "Can not write a %s file without atom names",
413 sprintf(title, "frame t= %.3f", fr->time);
416 write_hconf_indexed_p(gmx_fio_getfp(status->fio), title, fr->atoms, nind, ind,
418 fr->x, fr->bV ? fr->v : NULL, fr->box);
422 write_pdbfile_indexed(gmx_fio_getfp(status->fio), title, fr->atoms,
423 fr->x, -1, fr->box, ' ', fr->step, nind, ind, gc, TRUE);
427 write_g96_conf(gmx_fio_getfp(status->fio), fr, nind, ind);
430 gmx_fatal(FARGS, "Sorry, write_trxframe_indexed can not write %s",
460 void trjtools_gmx_prepare_tng_writing(const char *filename,
466 const gmx_mtop_t *mtop,
467 const atom_id *index,
468 const char *index_group_name)
470 if (filemode != 'w' && filemode != 'a')
472 gmx_incons("Sorry, can only prepare for TNG output.");
483 gmx_prepare_tng_writing(filename,
492 else if (efTNG == fn2ftp(infile))
494 tng_trajectory_t tng_in;
495 gmx_tng_open(infile, 'r', &tng_in);
497 gmx_prepare_tng_writing(filename,
508 void write_tng_frame(t_trxstatus *status,
511 gmx_write_tng_from_trxframe(status->tng, frame, -1);
514 int write_trxframe(t_trxstatus *status, t_trxframe *fr, gmx_conect gc)
530 gmx_tng_set_compression_precision(status->tng, prec);
531 write_tng_frame(status, fr);
536 switch (gmx_fio_getftp(status->fio))
544 gmx_fatal(FARGS, "Need coordinates to write a %s trajectory",
545 ftp2ext(gmx_fio_getftp(status->fio)));
550 switch (gmx_fio_getftp(status->fio))
553 write_xtc(status->fio, fr->natoms, fr->step, fr->time, fr->box, fr->x, prec);
557 fwrite_trn(status->fio, fr->step, fr->time, fr->lambda, fr->box, fr->natoms,
558 fr->bX ? fr->x : NULL, fr->bV ? fr->v : NULL, fr->bF ? fr->f : NULL);
566 gmx_fatal(FARGS, "Can not write a %s file without atom names",
567 ftp2ext(gmx_fio_getftp(status->fio)));
569 sprintf(title, "frame t= %.3f", fr->time);
570 if (gmx_fio_getftp(status->fio) == efGRO)
572 write_hconf_p(gmx_fio_getfp(status->fio), title, fr->atoms,
573 prec2ndec(prec), fr->x, fr->bV ? fr->v : NULL, fr->box);
577 write_pdbfile(gmx_fio_getfp(status->fio), title,
578 fr->atoms, fr->x, fr->bPBC ? fr->ePBC : -1, fr->box,
579 ' ', fr->step, gc, TRUE);
583 write_g96_conf(gmx_fio_getfp(status->fio), fr, -1, NULL);
586 gmx_fatal(FARGS, "Sorry, write_trxframe can not write %s",
587 ftp2ext(gmx_fio_getftp(status->fio)));
594 int write_trx(t_trxstatus *status, int nind, const atom_id *ind, t_atoms *atoms,
595 int step, real time, matrix box, rvec x[], rvec *v,
600 clear_trxframe(&fr, TRUE);
605 fr.bAtoms = atoms != NULL;
612 copy_mat(box, fr.box);
614 return write_trxframe_indexed(status, &fr, nind, ind, gc);
617 void close_trx(t_trxstatus *status)
619 gmx_tng_close(&status->tng);
622 gmx_fio_close(status->fio);
627 t_trxstatus *open_trx(const char *outfile, const char *filemode)
630 if (filemode[0] != 'w' && filemode[0] != 'a' && filemode[1] != '+')
632 gmx_fatal(FARGS, "Sorry, write_trx can only write");
638 stat->fio = gmx_fio_open(outfile, filemode);
642 static gmx_bool gmx_next_frame(t_trxstatus *status, t_trxframe *fr)
649 if (fread_trnheader(status->fio, &sh, &bOK))
651 fr->bDouble = sh.bDouble;
652 fr->natoms = sh.natoms;
658 fr->bFepState = TRUE;
659 fr->lambda = sh.lambda;
660 fr->bBox = sh.box_size > 0;
661 if (fr->flags & (TRX_READ_X | TRX_NEED_X))
665 snew(fr->x, sh.natoms);
667 fr->bX = sh.x_size > 0;
669 if (fr->flags & (TRX_READ_V | TRX_NEED_V))
673 snew(fr->v, sh.natoms);
675 fr->bV = sh.v_size > 0;
677 if (fr->flags & (TRX_READ_F | TRX_NEED_F))
681 snew(fr->f, sh.natoms);
683 fr->bF = sh.f_size > 0;
685 if (fread_htrn(status->fio, &sh, fr->box, fr->x, fr->v, fr->f))
691 fr->not_ok = DATA_NOT_OK;
697 fr->not_ok = HEADER_NOT_OK;
703 static gmx_bool pdb_next_x(t_trxstatus *status, FILE *fp, t_trxframe *fr)
707 int ePBC, model_nr, na;
708 char title[STRLEN], *time;
711 atoms.nr = fr->natoms;
713 atoms.pdbinfo = NULL;
714 /* the other pointers in atoms should not be accessed if these are NULL */
716 na = read_pdbfile(fp, title, &model_nr, &atoms, fr->x, &ePBC, boxpdb, TRUE, NULL);
717 set_trxframe_ePBC(fr, ePBC);
718 if (nframes_read(status) == 0)
720 fprintf(stderr, " '%s', %d atoms\n", title, fr->natoms);
725 fr->bBox = (boxpdb[XX][XX] != 0.0);
728 copy_mat(boxpdb, fr->box);
731 if (model_nr != NOTSET)
736 time = strstr(title, " t= ");
740 sscanf(time+4, "%lf", &dbl);
741 fr->time = (real)dbl;
746 /* this is a bit dirty, but it will work: if no time is read from
747 comment line in pdb file, set time to current frame number */
750 fr->time = (real)fr->step;
754 fr->time = (real)nframes_read(status);
763 if (na != fr->natoms)
765 gmx_fatal(FARGS, "Number of atoms in pdb frame %d is %d instead of %d",
766 nframes_read(status), na, fr->natoms);
772 static int pdb_first_x(t_trxstatus *status, FILE *fp, t_trxframe *fr)
776 fprintf(stderr, "Reading frames from pdb file");
778 get_pdb_coordnum(fp, &fr->natoms);
781 gmx_fatal(FARGS, "\nNo coordinates in pdb file\n");
784 snew(fr->x, fr->natoms);
785 pdb_next_x(status, fp, fr);
790 gmx_bool read_next_frame(const output_env_t oenv, t_trxstatus *status, t_trxframe *fr)
794 gmx_bool bOK, bRet, bMissingData = FALSE, bSkip = FALSE;
803 clear_trxframe(fr, FALSE);
809 /* Special treatment for TNG files */
814 ftp = gmx_fio_getftp(status->fio);
820 bRet = gmx_next_frame(status, fr);
823 /* Checkpoint files can not contain mulitple frames */
826 read_g96_conf(gmx_fio_getfp(status->fio), NULL, fr,
827 status->persistent_line);
828 bRet = (fr->natoms > 0);
832 * Sometimes is off by one frame
833 * and sometimes reports frame not present/file not seekable
835 /* DvdS 2005-05-31: this has been fixed along with the increased
836 * accuracy of the control over -b and -e options.
838 if (bTimeSet(TBEGIN) && (fr->tf < rTimeValue(TBEGIN)))
840 if (xtc_seek_time(status->fio, rTimeValue(TBEGIN), fr->natoms, TRUE))
842 gmx_fatal(FARGS, "Specified frame (time %f) doesn't exist or file corrupt/inconsistent.",
847 bRet = read_next_xtc(status->fio, fr->natoms, &fr->step, &fr->time, fr->box,
848 fr->x, &fr->prec, &bOK);
849 fr->bPrec = (bRet && fr->prec > 0);
856 /* Actually the header could also be not ok,
857 but from bOK from read_next_xtc this can't be distinguished */
858 fr->not_ok = DATA_NOT_OK;
862 bRet = gmx_read_next_tng_frame(status->tng, fr, NULL, 0);
865 bRet = pdb_next_x(status, gmx_fio_getfp(status->fio), fr);
868 bRet = gro_next_x_or_v(gmx_fio_getfp(status->fio), fr);
871 #ifdef GMX_USE_PLUGINS
872 bRet = read_next_vmd_frame(fr);
874 gmx_fatal(FARGS, "DEATH HORROR in read_next_frame ftp=%s,status=%s",
875 ftp2ext(gmx_fio_getftp(status->fio)),
876 gmx_fio_getname(status->fio));
883 bMissingData = (((fr->flags & TRX_NEED_X) && !fr->bX) ||
884 ((fr->flags & TRX_NEED_V) && !fr->bV) ||
885 ((fr->flags & TRX_NEED_F) && !fr->bF));
889 ct = check_times2(fr->time, fr->t0, fr->bDouble);
890 if (ct == 0 || ((fr->flags & TRX_DONT_SKIP) && ct < 0))
892 printcount(status, oenv, fr->time, FALSE);
900 printcount(status, oenv, fr->time, TRUE);
907 while (bRet && (bMissingData || bSkip));
911 printlast(status, oenv, pt);
914 printincomp(status, fr);
921 int read_first_frame(const output_env_t oenv, t_trxstatus **status,
922 const char *fn, t_trxframe *fr, int flags)
925 gmx_bool bFirst, bOK;
927 int ftp = fn2ftp(fn);
928 gmx_int64_t *tng_ids;
930 clear_trxframe(fr, TRUE);
937 status_init( *status );
938 (*status)->nxframe = 1;
943 /* Special treatment for TNG files */
944 gmx_tng_open(fn, 'r', &(*status)->tng);
948 fio = (*status)->fio = gmx_fio_open(fn, "r");
956 read_checkpoint_trxframe(fio, fr);
960 /* Can not rewind a compressed file, so open it twice */
961 if (!(*status)->persistent_line)
963 /* allocate the persistent line */
964 snew((*status)->persistent_line, STRLEN+1);
966 read_g96_conf(gmx_fio_getfp(fio), fn, fr, (*status)->persistent_line);
968 clear_trxframe(fr, FALSE);
969 if (flags & (TRX_READ_X | TRX_NEED_X))
971 snew(fr->x, fr->natoms);
973 if (flags & (TRX_READ_V | TRX_NEED_V))
975 snew(fr->v, fr->natoms);
977 fio = (*status)->fio = gmx_fio_open(fn, "r");
980 if (read_first_xtc(fio, &fr->natoms, &fr->step, &fr->time, fr->box, &fr->x,
981 &fr->prec, &bOK) == 0)
984 fr->not_ok = DATA_NOT_OK;
989 printincomp(*status, fr);
993 fr->bPrec = (fr->prec > 0);
998 printcount(*status, oenv, fr->time, FALSE);
1004 if (!gmx_read_next_tng_frame((*status)->tng, fr, NULL, 0))
1006 fr->not_ok = DATA_NOT_OK;
1008 printincomp(*status, fr);
1012 printcount(*status, oenv, fr->time, FALSE);
1017 pdb_first_x(*status, gmx_fio_getfp(fio), fr);
1020 printcount(*status, oenv, fr->time, FALSE);
1025 if (gro_first_x_or_v(gmx_fio_getfp(fio), fr))
1027 printcount(*status, oenv, fr->time, FALSE);
1032 #ifdef GMX_USE_PLUGINS
1033 fprintf(stderr, "The file format of %s is not a known trajectory format to GROMACS.\n"
1034 "Please make sure that the file is a trajectory!\n"
1035 "GROMACS will now assume it to be a trajectory and will try to open it using the VMD plug-ins.\n"
1036 "This will only work in case the VMD plugins are found and it is a trajectory format supported by VMD.\n", fn);
1037 gmx_fio_fp_close(fio); /*only close the file without removing FIO entry*/
1038 if (!read_first_vmd_frame(fn, fr))
1040 gmx_fatal(FARGS, "Not supported in read_first_frame: %s", fn);
1043 gmx_fatal(FARGS, "Not supported in read_first_frame: %s. Please make sure that the file is a trajectory.\n"
1044 "GROMACS is not compiled with plug-in support. Thus it cannot read non-GROMACS trajectory formats using the VMD plug-ins.\n"
1045 "Please compile with plug-in support if you want to read non-GROMACS trajectory formats.\n", fn);
1051 /* Return FALSE if we read a frame that's past the set ending time. */
1052 if (!bFirst && (!(fr->flags & TRX_DONT_SKIP) && check_times(fr->time) > 0))
1059 (!(fr->flags & TRX_DONT_SKIP) && check_times(fr->time) < 0))
1061 /* Read a frame when no frame was read or the first was skipped */
1062 if (!read_next_frame(oenv, *status, fr))
1069 return (fr->natoms > 0);
1072 /***** C O O R D I N A T E S T U F F *****/
1074 int read_first_x(const output_env_t oenv, t_trxstatus **status, const char *fn,
1075 real *t, rvec **x, matrix box)
1079 read_first_frame(oenv, status, fn, &fr, TRX_NEED_X);
1081 snew((*status)->xframe, 1);
1082 (*status)->nxframe = 1;
1083 (*(*status)->xframe) = fr;
1084 *t = (*status)->xframe->time;
1085 *x = (*status)->xframe->x;
1086 copy_mat((*status)->xframe->box, box);
1088 return (*status)->xframe->natoms;
1091 gmx_bool read_next_x(const output_env_t oenv, t_trxstatus *status, real *t,
1092 rvec x[], matrix box)
1096 status->xframe->x = x;
1097 /*xframe[status].x = x;*/
1098 bRet = read_next_frame(oenv, status, status->xframe);
1099 *t = status->xframe->time;
1100 copy_mat(status->xframe->box, box);
1105 void close_trj(t_trxstatus *status)
1107 gmx_tng_close(&status->tng);
1110 gmx_fio_close(status->fio);
1113 /* The memory in status->xframe is lost here,
1114 * but the read_first_x/read_next_x functions are deprecated anyhow.
1115 * read_first_frame/read_next_frame and close_trx should be used.
1120 void rewind_trj(t_trxstatus *status)
1124 gmx_fio_rewind(status->fio);
1127 /***** T O P O L O G Y S T U F F ******/
1129 t_topology *read_top(const char *fn, int *ePBC)
1135 epbc = read_tpx_top(fn, NULL, NULL, &natoms, NULL, NULL, NULL, top);