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.
46 #include "gromacs/fileio/confio.h"
47 #include "gromacs/fileio/gmxfio.h"
48 #include "gromacs/fileio/pdbio.h"
49 #include "gromacs/fileio/timecontrol.h"
50 #include "gromacs/fileio/tngio.h"
51 #include "gromacs/fileio/tngio_for_tools.h"
52 #include "gromacs/fileio/tpxio.h"
53 #include "gromacs/fileio/trnio.h"
54 #include "gromacs/fileio/trx.h"
55 #include "gromacs/fileio/xdrf.h"
56 #include "gromacs/fileio/xtcio.h"
57 #include "gromacs/legacyheaders/checkpoint.h"
58 #include "gromacs/legacyheaders/names.h"
59 #include "gromacs/math/vec.h"
60 #include "gromacs/topology/atoms.h"
61 #include "gromacs/utility/fatalerror.h"
62 #include "gromacs/utility/futil.h"
63 #include "gromacs/utility/smalloc.h"
65 #ifdef GMX_USE_PLUGINS
66 #include "gromacs/fileio/vmdio.h"
69 /* defines for frame counter output */
84 char *persistent_line; /* Persistent line for reading g96 trajectories */
87 /* utility functions */
89 gmx_bool bRmod_fd(double a, double b, double c, gmx_bool bDouble)
94 tol = 2*(bDouble ? GMX_DOUBLE_EPS : GMX_FLOAT_EPS);
96 iq = (int)((a - b + tol*a)/c);
98 if (fabs(a - b - c*iq) <= tol*fabs(a))
110 int check_times2(real t, real t0, gmx_bool bDouble)
115 /* since t is float, we can not use double precision for bRmod */
120 if ((!bTimeSet(TBEGIN) || (t >= rTimeValue(TBEGIN))) &&
121 (!bTimeSet(TEND) || (t <= rTimeValue(TEND))))
123 if (bTimeSet(TDELTA) && !bRmod_fd(t, t0, rTimeValue(TDELTA), bDouble))
132 else if (bTimeSet(TEND) && (t >= rTimeValue(TEND)))
138 fprintf(debug, "t=%g, t0=%g, b=%g, e=%g, dt=%g: r=%d\n",
139 t, t0, rTimeValue(TBEGIN), rTimeValue(TEND), rTimeValue(TDELTA), r);
144 int check_times(real t)
146 return check_times2(t, t, FALSE);
149 static void initcount(t_trxstatus *status)
151 status->__frame = -1;
154 static void status_init(t_trxstatus *status)
157 status->xframe = NULL;
159 status->__frame = -1;
160 status->persistent_line = NULL;
165 int nframes_read(t_trxstatus *status)
167 return status->__frame;
170 static void printcount_(t_trxstatus *status, const output_env_t oenv,
171 const char *l, real t)
173 if ((status->__frame < 2*SKIP1 || status->__frame % SKIP1 == 0) &&
174 (status->__frame < 2*SKIP2 || status->__frame % SKIP2 == 0) &&
175 (status->__frame < 2*SKIP3 || status->__frame % SKIP3 == 0))
177 fprintf(stderr, "\r%-14s %6d time %8.3f ", l, status->__frame,
178 output_env_conv_time(oenv, t));
182 static void printcount(t_trxstatus *status, const output_env_t oenv, real t,
186 printcount_(status, oenv, bSkip ? "Skipping frame" : "Reading frame", t);
189 static void printlast(t_trxstatus *status, const output_env_t oenv, real t)
191 printcount_(status, oenv, "Last frame", t);
192 fprintf(stderr, "\n");
195 static void printincomp(t_trxstatus *status, t_trxframe *fr)
197 if (fr->not_ok & HEADER_NOT_OK)
199 fprintf(stderr, "WARNING: Incomplete header: nr %d time %g\n",
200 status->__frame+1, fr->time);
204 fprintf(stderr, "WARNING: Incomplete frame: nr %d time %g\n",
205 status->__frame+1, fr->time);
209 int prec2ndec(real prec)
213 gmx_fatal(FARGS, "DEATH HORROR prec (%g) <= 0 in prec2ndec", prec);
216 return (int)(log(prec)/log(10.0)+0.5);
219 real ndec2prec(int ndec)
221 return pow(10.0, ndec);
224 t_fileio *trx_get_fileio(t_trxstatus *status)
229 float trx_get_time_of_final_frame(t_trxstatus *status)
231 t_fileio *stfio = trx_get_fileio(status);
232 int filetype = gmx_fio_getftp(stfio);
236 if (filetype == efXTC)
239 xdr_xtc_get_last_frame_time(gmx_fio_getfp(stfio),
240 gmx_fio_getxdr(stfio),
241 status->xframe->natoms, &bOK);
244 gmx_fatal(FARGS, "Error reading last frame. Maybe seek not supported." );
247 else if (filetype == efTNG)
249 tng_trajectory_t tng = status->tng;
252 gmx_fatal(FARGS, "Error opening TNG file.");
254 lasttime = gmx_tng_get_time_of_final_frame(tng);
258 gmx_incons("Only supported for TNG and XTC");
263 void clear_trxframe(t_trxframe *fr, gmx_bool bFirst)
270 fr->bFepState = FALSE;
302 void set_trxframe_ePBC(t_trxframe *fr, int ePBC)
304 fr->bPBC = (ePBC == -1);
308 int write_trxframe_indexed(t_trxstatus *status, t_trxframe *fr, int nind,
309 const atom_id *ind, gmx_conect gc)
312 rvec *xout = NULL, *vout = NULL, *fout = NULL;
329 else if (status->fio)
331 ftp = gmx_fio_getftp(status->fio);
335 gmx_incons("No input file available");
346 gmx_fatal(FARGS, "Need coordinates to write a %s trajectory",
359 for (i = 0; i < nind; i++)
361 copy_rvec(fr->v[ind[i]], vout[i]);
367 for (i = 0; i < nind; i++)
369 copy_rvec(fr->f[ind[i]], fout[i]);
377 for (i = 0; i < nind; i++)
379 copy_rvec(fr->x[ind[i]], xout[i]);
390 gmx_write_tng_from_trxframe(status->tng, fr, nind);
393 write_xtc(status->fio, nind, fr->step, fr->time, fr->box, xout, prec);
396 fwrite_trn(status->fio, nframes_read(status),
397 fr->time, fr->step, fr->box, nind, xout, vout, fout);
405 gmx_fatal(FARGS, "Can not write a %s file without atom names",
408 sprintf(title, "frame t= %.3f", fr->time);
411 write_hconf_indexed_p(gmx_fio_getfp(status->fio), title, fr->atoms, nind, ind,
413 fr->x, fr->bV ? fr->v : NULL, fr->box);
417 write_pdbfile_indexed(gmx_fio_getfp(status->fio), title, fr->atoms,
418 fr->x, -1, fr->box, ' ', fr->step, nind, ind, gc, TRUE);
422 write_g96_conf(gmx_fio_getfp(status->fio), fr, nind, ind);
425 gmx_fatal(FARGS, "Sorry, write_trxframe_indexed can not write %s",
453 void trjtools_gmx_prepare_tng_writing(const char *filename,
459 const gmx_mtop_t *mtop,
460 const atom_id *index,
461 const char *index_group_name)
463 if (filemode != 'w' && filemode != 'a')
465 gmx_incons("Sorry, can only prepare for TNG output.");
476 gmx_prepare_tng_writing(filename,
485 else if (efTNG == fn2ftp(infile))
487 tng_trajectory_t tng_in;
488 gmx_tng_open(infile, 'r', &tng_in);
490 gmx_prepare_tng_writing(filename,
501 void write_tng_frame(t_trxstatus *status,
504 gmx_write_tng_from_trxframe(status->tng, frame, -1);
507 int write_trxframe(t_trxstatus *status, t_trxframe *fr, gmx_conect gc)
523 gmx_tng_set_compression_precision(status->tng, prec);
524 write_tng_frame(status, fr);
529 switch (gmx_fio_getftp(status->fio))
536 gmx_fatal(FARGS, "Need coordinates to write a %s trajectory",
537 ftp2ext(gmx_fio_getftp(status->fio)));
542 switch (gmx_fio_getftp(status->fio))
545 write_xtc(status->fio, fr->natoms, fr->step, fr->time, fr->box, fr->x, prec);
548 fwrite_trn(status->fio, fr->step, fr->time, fr->lambda, fr->box, fr->natoms,
549 fr->bX ? fr->x : NULL, fr->bV ? fr->v : NULL, fr->bF ? fr->f : NULL);
557 gmx_fatal(FARGS, "Can not write a %s file without atom names",
558 ftp2ext(gmx_fio_getftp(status->fio)));
560 sprintf(title, "frame t= %.3f", fr->time);
561 if (gmx_fio_getftp(status->fio) == efGRO)
563 write_hconf_p(gmx_fio_getfp(status->fio), title, fr->atoms,
564 prec2ndec(prec), fr->x, fr->bV ? fr->v : NULL, fr->box);
568 write_pdbfile(gmx_fio_getfp(status->fio), title,
569 fr->atoms, fr->x, fr->bPBC ? fr->ePBC : -1, fr->box,
570 ' ', fr->step, gc, TRUE);
574 write_g96_conf(gmx_fio_getfp(status->fio), fr, -1, NULL);
577 gmx_fatal(FARGS, "Sorry, write_trxframe can not write %s",
578 ftp2ext(gmx_fio_getftp(status->fio)));
585 int write_trx(t_trxstatus *status, int nind, const atom_id *ind, t_atoms *atoms,
586 int step, real time, matrix box, rvec x[], rvec *v,
591 clear_trxframe(&fr, TRUE);
596 fr.bAtoms = atoms != NULL;
603 copy_mat(box, fr.box);
605 return write_trxframe_indexed(status, &fr, nind, ind, gc);
608 void close_trx(t_trxstatus *status)
610 gmx_tng_close(&status->tng);
613 gmx_fio_close(status->fio);
618 t_trxstatus *open_trx(const char *outfile, const char *filemode)
621 if (filemode[0] != 'w' && filemode[0] != 'a' && filemode[1] != '+')
623 gmx_fatal(FARGS, "Sorry, write_trx can only write");
629 stat->fio = gmx_fio_open(outfile, filemode);
633 static gmx_bool gmx_next_frame(t_trxstatus *status, t_trxframe *fr)
640 if (fread_trnheader(status->fio, &sh, &bOK))
642 fr->bDouble = sh.bDouble;
643 fr->natoms = sh.natoms;
649 fr->bFepState = TRUE;
650 fr->lambda = sh.lambda;
651 fr->bBox = sh.box_size > 0;
652 if (fr->flags & (TRX_READ_X | TRX_NEED_X))
656 snew(fr->x, sh.natoms);
658 fr->bX = sh.x_size > 0;
660 if (fr->flags & (TRX_READ_V | TRX_NEED_V))
664 snew(fr->v, sh.natoms);
666 fr->bV = sh.v_size > 0;
668 if (fr->flags & (TRX_READ_F | TRX_NEED_F))
672 snew(fr->f, sh.natoms);
674 fr->bF = sh.f_size > 0;
676 if (fread_htrn(status->fio, &sh, fr->box, fr->x, fr->v, fr->f))
682 fr->not_ok = DATA_NOT_OK;
688 fr->not_ok = HEADER_NOT_OK;
694 static gmx_bool pdb_next_x(t_trxstatus *status, FILE *fp, t_trxframe *fr)
698 int ePBC, model_nr, na;
699 char title[STRLEN], *time;
702 atoms.nr = fr->natoms;
704 atoms.pdbinfo = NULL;
705 /* the other pointers in atoms should not be accessed if these are NULL */
707 na = read_pdbfile(fp, title, &model_nr, &atoms, fr->x, &ePBC, boxpdb, TRUE, NULL);
708 set_trxframe_ePBC(fr, ePBC);
709 if (nframes_read(status) == 0)
711 fprintf(stderr, " '%s', %d atoms\n", title, fr->natoms);
716 fr->bBox = (boxpdb[XX][XX] != 0.0);
719 copy_mat(boxpdb, fr->box);
722 if (model_nr != NOTSET)
727 time = strstr(title, " t= ");
731 sscanf(time+4, "%lf", &dbl);
732 fr->time = (real)dbl;
737 /* this is a bit dirty, but it will work: if no time is read from
738 comment line in pdb file, set time to current frame number */
741 fr->time = (real)fr->step;
745 fr->time = (real)nframes_read(status);
754 if (na != fr->natoms)
756 gmx_fatal(FARGS, "Number of atoms in pdb frame %d is %d instead of %d",
757 nframes_read(status), na, fr->natoms);
763 static int pdb_first_x(t_trxstatus *status, FILE *fp, t_trxframe *fr)
767 fprintf(stderr, "Reading frames from pdb file");
769 get_pdb_coordnum(fp, &fr->natoms);
772 gmx_fatal(FARGS, "\nNo coordinates in pdb file\n");
775 snew(fr->x, fr->natoms);
776 pdb_next_x(status, fp, fr);
781 gmx_bool read_next_frame(const output_env_t oenv, t_trxstatus *status, t_trxframe *fr)
785 gmx_bool bOK, bRet, bMissingData = FALSE, bSkip = FALSE;
794 clear_trxframe(fr, FALSE);
800 /* Special treatment for TNG files */
805 ftp = gmx_fio_getftp(status->fio);
810 bRet = gmx_next_frame(status, fr);
813 /* Checkpoint files can not contain mulitple frames */
816 read_g96_conf(gmx_fio_getfp(status->fio), NULL, fr,
817 status->persistent_line);
818 bRet = (fr->natoms > 0);
822 * Sometimes is off by one frame
823 * and sometimes reports frame not present/file not seekable
825 /* DvdS 2005-05-31: this has been fixed along with the increased
826 * accuracy of the control over -b and -e options.
828 if (bTimeSet(TBEGIN) && (fr->tf < rTimeValue(TBEGIN)))
830 if (xtc_seek_time(status->fio, rTimeValue(TBEGIN), fr->natoms, TRUE))
832 gmx_fatal(FARGS, "Specified frame (time %f) doesn't exist or file corrupt/inconsistent.",
837 bRet = read_next_xtc(status->fio, fr->natoms, &fr->step, &fr->time, fr->box,
838 fr->x, &fr->prec, &bOK);
839 fr->bPrec = (bRet && fr->prec > 0);
846 /* Actually the header could also be not ok,
847 but from bOK from read_next_xtc this can't be distinguished */
848 fr->not_ok = DATA_NOT_OK;
852 bRet = gmx_read_next_tng_frame(status->tng, fr, NULL, 0);
855 bRet = pdb_next_x(status, gmx_fio_getfp(status->fio), fr);
858 bRet = gro_next_x_or_v(gmx_fio_getfp(status->fio), fr);
861 #ifdef GMX_USE_PLUGINS
862 bRet = read_next_vmd_frame(fr);
864 gmx_fatal(FARGS, "DEATH HORROR in read_next_frame ftp=%s,status=%s",
865 ftp2ext(gmx_fio_getftp(status->fio)),
866 gmx_fio_getname(status->fio));
873 bMissingData = (((fr->flags & TRX_NEED_X) && !fr->bX) ||
874 ((fr->flags & TRX_NEED_V) && !fr->bV) ||
875 ((fr->flags & TRX_NEED_F) && !fr->bF));
879 ct = check_times2(fr->time, fr->t0, fr->bDouble);
880 if (ct == 0 || ((fr->flags & TRX_DONT_SKIP) && ct < 0))
882 printcount(status, oenv, fr->time, FALSE);
890 printcount(status, oenv, fr->time, TRUE);
897 while (bRet && (bMissingData || bSkip));
901 printlast(status, oenv, pt);
904 printincomp(status, fr);
911 int read_first_frame(const output_env_t oenv, t_trxstatus **status,
912 const char *fn, t_trxframe *fr, int flags)
915 gmx_bool bFirst, bOK;
917 int ftp = fn2ftp(fn);
918 gmx_int64_t *tng_ids;
920 clear_trxframe(fr, TRUE);
927 status_init( *status );
928 (*status)->nxframe = 1;
933 /* Special treatment for TNG files */
934 gmx_tng_open(fn, 'r', &(*status)->tng);
938 fio = (*status)->fio = gmx_fio_open(fn, "r");
945 read_checkpoint_trxframe(fio, fr);
949 /* Can not rewind a compressed file, so open it twice */
950 if (!(*status)->persistent_line)
952 /* allocate the persistent line */
953 snew((*status)->persistent_line, STRLEN+1);
955 read_g96_conf(gmx_fio_getfp(fio), fn, fr, (*status)->persistent_line);
957 clear_trxframe(fr, FALSE);
958 if (flags & (TRX_READ_X | TRX_NEED_X))
960 snew(fr->x, fr->natoms);
962 if (flags & (TRX_READ_V | TRX_NEED_V))
964 snew(fr->v, fr->natoms);
966 fio = (*status)->fio = gmx_fio_open(fn, "r");
969 if (read_first_xtc(fio, &fr->natoms, &fr->step, &fr->time, fr->box, &fr->x,
970 &fr->prec, &bOK) == 0)
973 fr->not_ok = DATA_NOT_OK;
978 printincomp(*status, fr);
982 fr->bPrec = (fr->prec > 0);
987 printcount(*status, oenv, fr->time, FALSE);
993 if (!gmx_read_next_tng_frame((*status)->tng, fr, NULL, 0))
995 fr->not_ok = DATA_NOT_OK;
997 printincomp(*status, fr);
1001 printcount(*status, oenv, fr->time, FALSE);
1006 pdb_first_x(*status, gmx_fio_getfp(fio), fr);
1009 printcount(*status, oenv, fr->time, FALSE);
1014 if (gro_first_x_or_v(gmx_fio_getfp(fio), fr))
1016 printcount(*status, oenv, fr->time, FALSE);
1021 #ifdef GMX_USE_PLUGINS
1022 fprintf(stderr, "The file format of %s is not a known trajectory format to GROMACS.\n"
1023 "Please make sure that the file is a trajectory!\n"
1024 "GROMACS will now assume it to be a trajectory and will try to open it using the VMD plug-ins.\n"
1025 "This will only work in case the VMD plugins are found and it is a trajectory format supported by VMD.\n", fn);
1026 gmx_fio_fp_close(fio); /*only close the file without removing FIO entry*/
1027 if (!read_first_vmd_frame(fn, fr))
1029 gmx_fatal(FARGS, "Not supported in read_first_frame: %s", fn);
1032 gmx_fatal(FARGS, "Not supported in read_first_frame: %s. Please make sure that the file is a trajectory.\n"
1033 "GROMACS is not compiled with plug-in support. Thus it cannot read non-GROMACS trajectory formats using the VMD plug-ins.\n"
1034 "Please compile with plug-in support if you want to read non-GROMACS trajectory formats.\n", fn);
1040 /* Return FALSE if we read a frame that's past the set ending time. */
1041 if (!bFirst && (!(fr->flags & TRX_DONT_SKIP) && check_times(fr->time) > 0))
1048 (!(fr->flags & TRX_DONT_SKIP) && check_times(fr->time) < 0))
1050 /* Read a frame when no frame was read or the first was skipped */
1051 if (!read_next_frame(oenv, *status, fr))
1058 return (fr->natoms > 0);
1061 /***** C O O R D I N A T E S T U F F *****/
1063 int read_first_x(const output_env_t oenv, t_trxstatus **status, const char *fn,
1064 real *t, rvec **x, matrix box)
1068 read_first_frame(oenv, status, fn, &fr, TRX_NEED_X);
1070 snew((*status)->xframe, 1);
1071 (*status)->nxframe = 1;
1072 (*(*status)->xframe) = fr;
1073 *t = (*status)->xframe->time;
1074 *x = (*status)->xframe->x;
1075 copy_mat((*status)->xframe->box, box);
1077 return (*status)->xframe->natoms;
1080 gmx_bool read_next_x(const output_env_t oenv, t_trxstatus *status, real *t,
1081 rvec x[], matrix box)
1085 status->xframe->x = x;
1086 /*xframe[status].x = x;*/
1087 bRet = read_next_frame(oenv, status, status->xframe);
1088 *t = status->xframe->time;
1089 copy_mat(status->xframe->box, box);
1094 void close_trj(t_trxstatus *status)
1096 gmx_tng_close(&status->tng);
1099 gmx_fio_close(status->fio);
1102 /* The memory in status->xframe is lost here,
1103 * but the read_first_x/read_next_x functions are deprecated anyhow.
1104 * read_first_frame/read_next_frame and close_trx should be used.
1109 void rewind_trj(t_trxstatus *status)
1113 gmx_fio_rewind(status->fio);
1116 /***** T O P O L O G Y S T U F F ******/
1118 t_topology *read_top(const char *fn, int *ePBC)
1124 epbc = read_tpx_top(fn, NULL, NULL, &natoms, NULL, NULL, NULL, top);