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, 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.
54 #include "gromacs/legacyheaders/statutil.h"
63 #include "checkpoint.h"
67 /* defines for frame counter output */
72 /* Globals for gromos-87 input */
74 effXYZ, effXYZBox, effG87, effG87Box, effNR
87 char *persistent_line; /* Persistent line for reading g96 trajectories */
90 /* utility functions */
92 gmx_bool bRmod_fd(double a, double b, double c, gmx_bool bDouble)
97 tol = 2*(bDouble ? GMX_DOUBLE_EPS : GMX_FLOAT_EPS);
99 iq = (int)((a - b + tol*a)/c);
101 if (fabs(a - b - c*iq) <= tol*fabs(a))
113 int check_times2(real t, real t0, gmx_bool bDouble)
118 /* since t is float, we can not use double precision for bRmod */
123 if ((!bTimeSet(TBEGIN) || (t >= rTimeValue(TBEGIN))) &&
124 (!bTimeSet(TEND) || (t <= rTimeValue(TEND))))
126 if (bTimeSet(TDELTA) && !bRmod_fd(t, t0, rTimeValue(TDELTA), bDouble))
135 else if (bTimeSet(TEND) && (t >= rTimeValue(TEND)))
141 fprintf(debug, "t=%g, t0=%g, b=%g, e=%g, dt=%g: r=%d\n",
142 t, t0, rTimeValue(TBEGIN), rTimeValue(TEND), rTimeValue(TDELTA), r);
147 int check_times(real t)
149 return check_times2(t, t, FALSE);
152 static void initcount(t_trxstatus *status)
154 status->__frame = -1;
157 static void status_init(t_trxstatus *status)
160 status->xframe = NULL;
162 status->__frame = -1;
163 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);
222 t_fileio *trx_get_fileio(t_trxstatus *status)
229 void clear_trxframe(t_trxframe *fr, gmx_bool bFirst)
236 fr->bFepState = FALSE;
267 void set_trxframe_ePBC(t_trxframe *fr, int ePBC)
269 fr->bPBC = (ePBC == -1);
273 int write_trxframe_indexed(t_trxstatus *status, t_trxframe *fr, int nind,
274 const atom_id *ind, gmx_conect gc)
277 rvec *xout = NULL, *vout = NULL, *fout = NULL;
290 switch (gmx_fio_getftp(status->fio))
298 gmx_fatal(FARGS, "Need coordinates to write a %s trajectory",
299 ftp2ext(gmx_fio_getftp(status->fio)));
304 switch (gmx_fio_getftp(status->fio))
311 for (i = 0; i < nind; i++)
313 copy_rvec(fr->v[ind[i]], vout[i]);
319 for (i = 0; i < nind; i++)
321 copy_rvec(fr->f[ind[i]], fout[i]);
330 for (i = 0; i < nind; i++)
332 copy_rvec(fr->x[ind[i]], xout[i]);
340 switch (gmx_fio_getftp(status->fio))
343 write_xtc(status->fio, nind, fr->step, fr->time, fr->box, xout, prec);
347 fwrite_trn(status->fio, nframes_read(status),
348 fr->time, fr->step, fr->box, nind, xout, vout, fout);
356 gmx_fatal(FARGS, "Can not write a %s file without atom names",
357 ftp2ext(gmx_fio_getftp(status->fio)));
359 sprintf(title, "frame t= %.3f", fr->time);
360 if (gmx_fio_getftp(status->fio) == efGRO)
362 write_hconf_indexed_p(gmx_fio_getfp(status->fio), title, fr->atoms, nind, ind,
364 fr->x, fr->bV ? fr->v : NULL, fr->box);
368 write_pdbfile_indexed(gmx_fio_getfp(status->fio), title, fr->atoms,
369 fr->x, -1, fr->box, ' ', fr->step, nind, ind, gc, TRUE);
373 write_g87(gmx_fio_getfp(status->fio), nind, xout, fr->box);
376 write_g96_conf(gmx_fio_getfp(status->fio), fr, nind, ind);
379 gmx_fatal(FARGS, "Sorry, write_trxframe_indexed can not write %s",
380 ftp2ext(gmx_fio_getftp(status->fio)));
384 switch (gmx_fio_getftp(status->fio))
409 int write_trxframe(t_trxstatus *status, t_trxframe *fr, gmx_conect gc)
423 switch (gmx_fio_getftp(status->fio))
431 gmx_fatal(FARGS, "Need coordinates to write a %s trajectory",
432 ftp2ext(gmx_fio_getftp(status->fio)));
437 switch (gmx_fio_getftp(status->fio))
440 write_xtc(status->fio, fr->natoms, fr->step, fr->time, fr->box, fr->x, prec);
444 fwrite_trn(status->fio, fr->step, fr->time, fr->lambda, fr->box, fr->natoms,
445 fr->bX ? fr->x : NULL, fr->bV ? fr->v : NULL, fr->bF ? fr->f : NULL);
453 gmx_fatal(FARGS, "Can not write a %s file without atom names",
454 ftp2ext(gmx_fio_getftp(status->fio)));
456 sprintf(title, "frame t= %.3f", fr->time);
457 if (gmx_fio_getftp(status->fio) == efGRO)
459 write_hconf_p(gmx_fio_getfp(status->fio), title, fr->atoms,
460 prec2ndec(prec), fr->x, fr->bV ? fr->v : NULL, fr->box);
464 write_pdbfile(gmx_fio_getfp(status->fio), title,
465 fr->atoms, fr->x, fr->bPBC ? fr->ePBC : -1, fr->box,
466 ' ', fr->step, gc, TRUE);
470 write_g87(gmx_fio_getfp(status->fio), fr->natoms, fr->x, fr->box);
473 write_g96_conf(gmx_fio_getfp(status->fio), fr, -1, NULL);
476 gmx_fatal(FARGS, "Sorry, write_trxframe can not write %s",
477 ftp2ext(gmx_fio_getftp(status->fio)));
484 int write_trx(t_trxstatus *status, int nind, const atom_id *ind, t_atoms *atoms,
485 int step, real time, matrix box, rvec x[], rvec *v,
490 clear_trxframe(&fr, TRUE);
495 fr.bAtoms = atoms != NULL;
502 copy_mat(box, fr.box);
504 return write_trxframe_indexed(status, &fr, nind, ind, gc);
507 void close_trx(t_trxstatus *status)
509 gmx_fio_close(status->fio);
513 t_trxstatus *open_trx(const char *outfile, const char *filemode)
516 if (filemode[0] != 'w' && filemode[0] != 'a' && filemode[1] != '+')
518 gmx_fatal(FARGS, "Sorry, write_trx can only write");
524 stat->fio = gmx_fio_open(outfile, filemode);
528 static gmx_bool gmx_next_frame(t_trxstatus *status, t_trxframe *fr)
535 if (fread_trnheader(status->fio, &sh, &bOK))
537 fr->bDouble = sh.bDouble;
538 fr->natoms = sh.natoms;
544 fr->bFepState = TRUE;
545 fr->lambda = sh.lambda;
546 fr->bBox = sh.box_size > 0;
547 if (fr->flags & (TRX_READ_X | TRX_NEED_X))
551 snew(fr->x, sh.natoms);
553 fr->bX = sh.x_size > 0;
555 if (fr->flags & (TRX_READ_V | TRX_NEED_V))
559 snew(fr->v, sh.natoms);
561 fr->bV = sh.v_size > 0;
563 if (fr->flags & (TRX_READ_F | TRX_NEED_F))
567 snew(fr->f, sh.natoms);
569 fr->bF = sh.f_size > 0;
571 if (fread_htrn(status->fio, &sh, fr->box, fr->x, fr->v, fr->f))
577 fr->not_ok = DATA_NOT_OK;
583 fr->not_ok = HEADER_NOT_OK;
589 static void choose_file_format(FILE *fp)
597 printf(" Select File Format\n");
598 printf("---------------------------\n");
599 printf("1. XYZ File\n");
600 printf("2. XYZ File with Box\n");
601 printf("3. Gromos-87 Ascii Trajectory\n");
602 printf("4. Gromos-87 Ascii Trajectory with Box\n");
609 printf("\nChoice: ");
613 rc = scanf("%d", &i);
618 while ((i < 0) || (i >= effNR));
621 stat->eFF = (eFileFormat) i;
623 for (m = 0; (m < DIM); m++)
628 stat->bReadBox = (stat->eFF == effG87Box) || (stat->eFF == effXYZBox);
634 if (5 != fscanf(fp, "%d%lf%lf%lf%lf", &stat->NATOMS, &stat->BOX[XX], &stat->BOX[YY], &stat->BOX[ZZ], &stat->DT))
636 gmx_fatal(FARGS, "Error reading natoms/box in file");
641 printf("GROMOS! OH DEAR...\n\n");
642 printf("Number of atoms ? ");
644 if (1 != scanf("%d", &stat->NATOMS))
646 gmx_fatal(FARGS, "Error reading natoms in file");
649 printf("Time between timeframes ? ");
651 if (1 != scanf("%lf", &stat->DT))
653 gmx_fatal(FARGS, "Error reading dt from file");
656 if (stat->eFF == effG87)
658 printf("Box X Y Z ? ");
660 if (3 != scanf("%lf%lf%lf", &stat->BOX[XX], &stat->BOX[YY], &stat->BOX[ZZ]))
662 gmx_fatal(FARGS, "Error reading box in file");
675 printf("Hellow World\n");
679 static gmx_bool do_read_xyz(t_trxstatus *status, FILE *fp, int natoms,
680 rvec x[], matrix box)
685 for (i = 0; (i < natoms); i++)
687 for (m = 0; (m < DIM); m++)
689 if (fscanf(fp, "%lf", &x0) != 1)
693 fprintf(stderr, "error reading statusfile: x[%d][%d]\n", i, m);
701 if (status->bReadBox)
703 for (m = 0; (m < DIM); m++)
705 if (fscanf(fp, "%lf", &x0) != 1)
715 static gmx_bool xyz_next_x(t_trxstatus *status, FILE *fp, const output_env_t oenv,
716 real *t, int natoms, rvec x[], matrix box)
717 /* Reads until a new x can be found (return TRUE)
718 * or eof (return FALSE)
724 while (!bTimeSet(TBEGIN) || (*t < rTimeValue(TBEGIN)))
726 if (!do_read_xyz(status, fp, natoms, x, box))
730 printcount(status, oenv, *t, FALSE);
734 if (!bTimeSet(TEND) || (*t <= rTimeValue(TEND)))
736 if (!do_read_xyz(status, fp, natoms, x, box))
738 printlast(status, oenv, *t);
741 printcount(status, oenv, *t, FALSE);
746 printlast(status, oenv, pt);
750 static int xyz_first_x(t_trxstatus *status, FILE *fp, const output_env_t oenv,
751 real *t, rvec **x, matrix box)
752 /* Reads fp, mallocs x, and returns x and box
753 * Returns natoms when successful, FALSE otherwise
761 choose_file_format(fp);
763 for (m = 0; (m < DIM); m++)
765 box[m][m] = status->BOX[m];
768 snew(*x, status->NATOMS);
770 if (!xyz_next_x(status, fp, oenv, t, status->NATOMS, *x, box))
776 return status->NATOMS;
779 static gmx_bool pdb_next_x(t_trxstatus *status, FILE *fp, t_trxframe *fr)
783 int ePBC, model_nr, na;
784 char title[STRLEN], *time;
787 atoms.nr = fr->natoms;
789 atoms.pdbinfo = NULL;
790 /* the other pointers in atoms should not be accessed if these are NULL */
792 na = read_pdbfile(fp, title, &model_nr, &atoms, fr->x, &ePBC, boxpdb, TRUE, NULL);
793 set_trxframe_ePBC(fr, ePBC);
794 if (nframes_read(status) == 0)
796 fprintf(stderr, " '%s', %d atoms\n", title, fr->natoms);
801 fr->bBox = (boxpdb[XX][XX] != 0.0);
804 copy_mat(boxpdb, fr->box);
807 if (model_nr != NOTSET)
812 time = strstr(title, " t= ");
816 sscanf(time+4, "%lf", &dbl);
817 fr->time = (real)dbl;
822 /* this is a bit dirty, but it will work: if no time is read from
823 comment line in pdb file, set time to current frame number */
826 fr->time = (real)fr->step;
830 fr->time = (real)nframes_read(status);
839 if (na != fr->natoms)
841 gmx_fatal(FARGS, "Number of atoms in pdb frame %d is %d instead of %d",
842 nframes_read(status), na, fr->natoms);
848 static int pdb_first_x(t_trxstatus *status, FILE *fp, t_trxframe *fr)
852 fprintf(stderr, "Reading frames from pdb file");
854 get_pdb_coordnum(fp, &fr->natoms);
857 gmx_fatal(FARGS, "\nNo coordinates in pdb file\n");
860 snew(fr->x, fr->natoms);
861 pdb_next_x(status, fp, fr);
866 gmx_bool read_next_frame(const output_env_t oenv, t_trxstatus *status, t_trxframe *fr)
870 gmx_bool bOK, bRet, bMissingData = FALSE, bSkip = FALSE;
878 clear_trxframe(fr, FALSE);
882 switch (gmx_fio_getftp(status->fio))
886 bRet = gmx_next_frame(status, fr);
889 /* Checkpoint files can not contain mulitple frames */
892 read_g96_conf(gmx_fio_getfp(status->fio), NULL, fr,
893 status->persistent_line);
894 bRet = (fr->natoms > 0);
897 bRet = xyz_next_x(status, gmx_fio_getfp(status->fio), oenv, &fr->time,
898 fr->natoms, fr->x, fr->box);
905 * Sometimes is off by one frame
906 * and sometimes reports frame not present/file not seekable
908 /* DvdS 2005-05-31: this has been fixed along with the increased
909 * accuracy of the control over -b and -e options.
911 if (bTimeSet(TBEGIN) && (fr->time < rTimeValue(TBEGIN)))
913 if (xtc_seek_time(status->fio, rTimeValue(TBEGIN), fr->natoms, TRUE))
915 gmx_fatal(FARGS, "Specified frame (time %f) doesn't exist or file corrupt/inconsistent.",
920 bRet = read_next_xtc(status->fio, fr->natoms, &fr->step, &fr->time, fr->box,
921 fr->x, &fr->prec, &bOK);
922 fr->bPrec = (bRet && fr->prec > 0);
929 /* Actually the header could also be not ok,
930 but from bOK from read_next_xtc this can't be distinguished */
931 fr->not_ok = DATA_NOT_OK;
935 bRet = pdb_next_x(status, gmx_fio_getfp(status->fio), fr);
938 bRet = gro_next_x_or_v(gmx_fio_getfp(status->fio), fr);
941 #ifdef GMX_USE_PLUGINS
942 bRet = read_next_vmd_frame(fr);
944 gmx_fatal(FARGS, "DEATH HORROR in read_next_frame ftp=%s,status=%s",
945 ftp2ext(gmx_fio_getftp(status->fio)),
946 gmx_fio_getname(status->fio));
952 bMissingData = (((fr->flags & TRX_NEED_X) && !fr->bX) ||
953 ((fr->flags & TRX_NEED_V) && !fr->bV) ||
954 ((fr->flags & TRX_NEED_F) && !fr->bF));
958 ct = check_times2(fr->time, fr->t0, fr->bDouble);
959 if (ct == 0 || ((fr->flags & TRX_DONT_SKIP) && ct < 0))
961 printcount(status, oenv, fr->time, FALSE);
969 printcount(status, oenv, fr->time, TRUE);
976 while (bRet && (bMissingData || bSkip));
980 printlast(status, oenv, pt);
983 printincomp(status, fr);
990 int read_first_frame(const output_env_t oenv, t_trxstatus **status,
991 const char *fn, t_trxframe *fr, int flags)
994 gmx_bool bFirst, bOK;
997 clear_trxframe(fr, TRUE);
1004 status_init( *status );
1005 (*status)->nxframe = 1;
1008 fio = (*status)->fio = gmx_fio_open(fn, "r");
1009 switch (gmx_fio_getftp(fio))
1015 read_checkpoint_trxframe(fio, fr);
1019 /* Can not rewind a compressed file, so open it twice */
1020 if (!(*status)->persistent_line)
1022 /* allocate the persistent line */
1023 snew((*status)->persistent_line, STRLEN+1);
1025 read_g96_conf(gmx_fio_getfp(fio), fn, fr, (*status)->persistent_line);
1027 clear_trxframe(fr, FALSE);
1028 if (flags & (TRX_READ_X | TRX_NEED_X))
1030 snew(fr->x, fr->natoms);
1032 if (flags & (TRX_READ_V | TRX_NEED_V))
1034 snew(fr->v, fr->natoms);
1036 fio = (*status)->fio = gmx_fio_open(fn, "r");
1039 fr->natoms = xyz_first_x(*status, gmx_fio_getfp(fio), oenv, &fr->time,
1046 printcount(*status, oenv, fr->time, FALSE);
1051 if (read_first_xtc(fio, &fr->natoms, &fr->step, &fr->time, fr->box, &fr->x,
1052 &fr->prec, &bOK) == 0)
1056 gmx_fatal(FARGS, "No XTC!\n");
1060 fr->not_ok = DATA_NOT_OK;
1066 printincomp(*status, fr);
1070 fr->bPrec = (fr->prec > 0);
1075 printcount(*status, oenv, fr->time, FALSE);
1080 pdb_first_x(*status, gmx_fio_getfp(fio), fr);
1083 printcount(*status, oenv, fr->time, FALSE);
1088 if (gro_first_x_or_v(gmx_fio_getfp(fio), fr))
1090 printcount(*status, oenv, fr->time, FALSE);
1095 #ifdef GMX_USE_PLUGINS
1096 fprintf(stderr, "The file format of %s is not a known trajectory format to GROMACS.\n"
1097 "Please make sure that the file is a trajectory!\n"
1098 "GROMACS will now assume it to be a trajectory and will try to open it using the VMD plug-ins.\n"
1099 "This will only work in case the VMD plugins are found and it is a trajectory format supported by VMD.\n", fn);
1100 gmx_fio_fp_close(fio); /*only close the file without removing FIO entry*/
1101 if (!read_first_vmd_frame(fn, fr))
1103 gmx_fatal(FARGS, "Not supported in read_first_frame: %s", fn);
1106 gmx_fatal(FARGS, "Not supported in read_first_frame: %s. Please make sure that the file is a trajectory.\n"
1107 "GROMACS is not compiled with plug-in support. Thus it cannot read non-GROMACS trajectory formats using the VMD plug-ins.\n"
1108 "Please compile with plug-in support if you want to read non-GROMACS trajectory formats.\n", fn);
1113 /* Return FALSE if we read a frame that's past the set ending time. */
1114 if (!bFirst && (!(fr->flags & TRX_DONT_SKIP) && check_times(fr->time) > 0))
1121 (!(fr->flags & TRX_DONT_SKIP) && check_times(fr->time) < 0))
1123 /* Read a frame when no frame was read or the first was skipped */
1124 if (!read_next_frame(oenv, *status, fr))
1131 return (fr->natoms > 0);
1134 /***** C O O R D I N A T E S T U F F *****/
1136 int read_first_x(const output_env_t oenv, t_trxstatus **status, const char *fn,
1137 real *t, rvec **x, matrix box)
1141 read_first_frame(oenv, status, fn, &fr, TRX_NEED_X);
1143 snew((*status)->xframe, 1);
1144 (*status)->nxframe = 1;
1145 (*(*status)->xframe) = fr;
1146 *t = (*status)->xframe->time;
1147 *x = (*status)->xframe->x;
1148 copy_mat((*status)->xframe->box, box);
1150 return (*status)->xframe->natoms;
1153 gmx_bool read_next_x(const output_env_t oenv, t_trxstatus *status, real *t,
1154 rvec x[], matrix box)
1158 status->xframe->x = x;
1159 /*xframe[status].x = x;*/
1160 bRet = read_next_frame(oenv, status, status->xframe);
1161 *t = status->xframe->time;
1162 copy_mat(status->xframe->box, box);
1167 void close_trj(t_trxstatus *status)
1169 gmx_fio_close(status->fio);
1170 /* The memory in status->xframe is lost here,
1171 * but the read_first_x/read_next_x functions are deprecated anyhow.
1172 * read_first_frame/read_next_frame and close_trx should be used.
1177 void rewind_trj(t_trxstatus *status)
1181 gmx_fio_rewind(status->fio);
1184 /***** T O P O L O G Y S T U F F ******/
1186 t_topology *read_top(const char *fn, int *ePBC)
1192 epbc = read_tpx_top(fn, NULL, NULL, &natoms, NULL, NULL, NULL, top);