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.
63 #include "checkpoint.h"
66 #include "gromacs/fileio/timecontrol.h"
68 /* defines for frame counter output */
73 /* Globals for gromos-87 input */
75 effXYZ, effXYZBox, effG87, effG87Box, effNR
88 char *persistent_line; /* Persistent line for reading g96 trajectories */
91 /* utility functions */
93 gmx_bool bRmod_fd(double a, double b, double c, gmx_bool bDouble)
98 tol = 2*(bDouble ? GMX_DOUBLE_EPS : GMX_FLOAT_EPS);
100 iq = (int)((a - b + tol*a)/c);
102 if (fabs(a - b - c*iq) <= tol*fabs(a))
114 int check_times2(real t, real t0, gmx_bool bDouble)
119 /* since t is float, we can not use double precision for bRmod */
124 if ((!bTimeSet(TBEGIN) || (t >= rTimeValue(TBEGIN))) &&
125 (!bTimeSet(TEND) || (t <= rTimeValue(TEND))))
127 if (bTimeSet(TDELTA) && !bRmod_fd(t, t0, rTimeValue(TDELTA), bDouble))
136 else if (bTimeSet(TEND) && (t >= rTimeValue(TEND)))
142 fprintf(debug, "t=%g, t0=%g, b=%g, e=%g, dt=%g: r=%d\n",
143 t, t0, rTimeValue(TBEGIN), rTimeValue(TEND), rTimeValue(TDELTA), r);
148 int check_times(real t)
150 return check_times2(t, t, FALSE);
153 static void initcount(t_trxstatus *status)
155 status->__frame = -1;
158 static void status_init(t_trxstatus *status)
161 status->xframe = NULL;
163 status->__frame = -1;
164 status->persistent_line = NULL;
168 int nframes_read(t_trxstatus *status)
170 return status->__frame;
173 static void printcount_(t_trxstatus *status, const output_env_t oenv,
174 const char *l, real t)
176 if ((status->__frame < 2*SKIP1 || status->__frame % SKIP1 == 0) &&
177 (status->__frame < 2*SKIP2 || status->__frame % SKIP2 == 0) &&
178 (status->__frame < 2*SKIP3 || status->__frame % SKIP3 == 0))
180 fprintf(stderr, "\r%-14s %6d time %8.3f ", l, status->__frame,
181 output_env_conv_time(oenv, t));
185 static void printcount(t_trxstatus *status, const output_env_t oenv, real t,
189 printcount_(status, oenv, bSkip ? "Skipping frame" : "Reading frame", t);
192 static void printlast(t_trxstatus *status, const output_env_t oenv, real t)
194 printcount_(status, oenv, "Last frame", t);
195 fprintf(stderr, "\n");
198 static void printincomp(t_trxstatus *status, t_trxframe *fr)
200 if (fr->not_ok & HEADER_NOT_OK)
202 fprintf(stderr, "WARNING: Incomplete header: nr %d time %g\n",
203 status->__frame+1, fr->time);
207 fprintf(stderr, "WARNING: Incomplete frame: nr %d time %g\n",
208 status->__frame+1, fr->time);
212 int prec2ndec(real prec)
216 gmx_fatal(FARGS, "DEATH HORROR prec (%g) <= 0 in prec2ndec", prec);
219 return (int)(log(prec)/log(10.0)+0.5);
223 t_fileio *trx_get_fileio(t_trxstatus *status)
230 void clear_trxframe(t_trxframe *fr, gmx_bool bFirst)
237 fr->bFepState = FALSE;
268 void set_trxframe_ePBC(t_trxframe *fr, int ePBC)
270 fr->bPBC = (ePBC == -1);
274 int write_trxframe_indexed(t_trxstatus *status, t_trxframe *fr, int nind,
275 const atom_id *ind, gmx_conect gc)
278 rvec *xout = NULL, *vout = NULL, *fout = NULL;
291 switch (gmx_fio_getftp(status->fio))
299 gmx_fatal(FARGS, "Need coordinates to write a %s trajectory",
300 ftp2ext(gmx_fio_getftp(status->fio)));
305 switch (gmx_fio_getftp(status->fio))
312 for (i = 0; i < nind; i++)
314 copy_rvec(fr->v[ind[i]], vout[i]);
320 for (i = 0; i < nind; i++)
322 copy_rvec(fr->f[ind[i]], fout[i]);
331 for (i = 0; i < nind; i++)
333 copy_rvec(fr->x[ind[i]], xout[i]);
341 switch (gmx_fio_getftp(status->fio))
344 write_xtc(status->fio, nind, fr->step, fr->time, fr->box, xout, prec);
348 fwrite_trn(status->fio, nframes_read(status),
349 fr->time, fr->step, fr->box, nind, xout, vout, fout);
357 gmx_fatal(FARGS, "Can not write a %s file without atom names",
358 ftp2ext(gmx_fio_getftp(status->fio)));
360 sprintf(title, "frame t= %.3f", fr->time);
361 if (gmx_fio_getftp(status->fio) == efGRO)
363 write_hconf_indexed_p(gmx_fio_getfp(status->fio), title, fr->atoms, nind, ind,
365 fr->x, fr->bV ? fr->v : NULL, fr->box);
369 write_pdbfile_indexed(gmx_fio_getfp(status->fio), title, fr->atoms,
370 fr->x, -1, fr->box, ' ', fr->step, nind, ind, gc, TRUE);
374 write_g87(gmx_fio_getfp(status->fio), nind, xout, fr->box);
377 write_g96_conf(gmx_fio_getfp(status->fio), fr, nind, ind);
380 gmx_fatal(FARGS, "Sorry, write_trxframe_indexed can not write %s",
381 ftp2ext(gmx_fio_getftp(status->fio)));
385 switch (gmx_fio_getftp(status->fio))
410 int write_trxframe(t_trxstatus *status, t_trxframe *fr, gmx_conect gc)
424 switch (gmx_fio_getftp(status->fio))
432 gmx_fatal(FARGS, "Need coordinates to write a %s trajectory",
433 ftp2ext(gmx_fio_getftp(status->fio)));
438 switch (gmx_fio_getftp(status->fio))
441 write_xtc(status->fio, fr->natoms, fr->step, fr->time, fr->box, fr->x, prec);
445 fwrite_trn(status->fio, fr->step, fr->time, fr->lambda, fr->box, fr->natoms,
446 fr->bX ? fr->x : NULL, fr->bV ? fr->v : NULL, fr->bF ? fr->f : NULL);
454 gmx_fatal(FARGS, "Can not write a %s file without atom names",
455 ftp2ext(gmx_fio_getftp(status->fio)));
457 sprintf(title, "frame t= %.3f", fr->time);
458 if (gmx_fio_getftp(status->fio) == efGRO)
460 write_hconf_p(gmx_fio_getfp(status->fio), title, fr->atoms,
461 prec2ndec(prec), fr->x, fr->bV ? fr->v : NULL, fr->box);
465 write_pdbfile(gmx_fio_getfp(status->fio), title,
466 fr->atoms, fr->x, fr->bPBC ? fr->ePBC : -1, fr->box,
467 ' ', fr->step, gc, TRUE);
471 write_g87(gmx_fio_getfp(status->fio), fr->natoms, fr->x, fr->box);
474 write_g96_conf(gmx_fio_getfp(status->fio), fr, -1, NULL);
477 gmx_fatal(FARGS, "Sorry, write_trxframe can not write %s",
478 ftp2ext(gmx_fio_getftp(status->fio)));
485 int write_trx(t_trxstatus *status, int nind, const atom_id *ind, t_atoms *atoms,
486 int step, real time, matrix box, rvec x[], rvec *v,
491 clear_trxframe(&fr, TRUE);
496 fr.bAtoms = atoms != NULL;
503 copy_mat(box, fr.box);
505 return write_trxframe_indexed(status, &fr, nind, ind, gc);
508 void close_trx(t_trxstatus *status)
510 gmx_fio_close(status->fio);
514 t_trxstatus *open_trx(const char *outfile, const char *filemode)
517 if (filemode[0] != 'w' && filemode[0] != 'a' && filemode[1] != '+')
519 gmx_fatal(FARGS, "Sorry, write_trx can only write");
525 stat->fio = gmx_fio_open(outfile, filemode);
529 static gmx_bool gmx_next_frame(t_trxstatus *status, t_trxframe *fr)
536 if (fread_trnheader(status->fio, &sh, &bOK))
538 fr->bDouble = sh.bDouble;
539 fr->natoms = sh.natoms;
545 fr->bFepState = TRUE;
546 fr->lambda = sh.lambda;
547 fr->bBox = sh.box_size > 0;
548 if (fr->flags & (TRX_READ_X | TRX_NEED_X))
552 snew(fr->x, sh.natoms);
554 fr->bX = sh.x_size > 0;
556 if (fr->flags & (TRX_READ_V | TRX_NEED_V))
560 snew(fr->v, sh.natoms);
562 fr->bV = sh.v_size > 0;
564 if (fr->flags & (TRX_READ_F | TRX_NEED_F))
568 snew(fr->f, sh.natoms);
570 fr->bF = sh.f_size > 0;
572 if (fread_htrn(status->fio, &sh, fr->box, fr->x, fr->v, fr->f))
578 fr->not_ok = DATA_NOT_OK;
584 fr->not_ok = HEADER_NOT_OK;
590 static void choose_file_format(FILE *fp)
598 printf(" Select File Format\n");
599 printf("---------------------------\n");
600 printf("1. XYZ File\n");
601 printf("2. XYZ File with Box\n");
602 printf("3. Gromos-87 Ascii Trajectory\n");
603 printf("4. Gromos-87 Ascii Trajectory with Box\n");
610 printf("\nChoice: ");
614 rc = scanf("%d", &i);
619 while ((i < 0) || (i >= effNR));
622 stat->eFF = (eFileFormat) i;
624 for (m = 0; (m < DIM); m++)
629 stat->bReadBox = (stat->eFF == effG87Box) || (stat->eFF == effXYZBox);
635 if (5 != fscanf(fp, "%d%lf%lf%lf%lf", &stat->NATOMS, &stat->BOX[XX], &stat->BOX[YY], &stat->BOX[ZZ], &stat->DT))
637 gmx_fatal(FARGS, "Error reading natoms/box in file");
642 printf("GROMOS! OH DEAR...\n\n");
643 printf("Number of atoms ? ");
645 if (1 != scanf("%d", &stat->NATOMS))
647 gmx_fatal(FARGS, "Error reading natoms in file");
650 printf("Time between timeframes ? ");
652 if (1 != scanf("%lf", &stat->DT))
654 gmx_fatal(FARGS, "Error reading dt from file");
657 if (stat->eFF == effG87)
659 printf("Box X Y Z ? ");
661 if (3 != scanf("%lf%lf%lf", &stat->BOX[XX], &stat->BOX[YY], &stat->BOX[ZZ]))
663 gmx_fatal(FARGS, "Error reading box in file");
676 printf("Hellow World\n");
680 static gmx_bool do_read_xyz(t_trxstatus *status, FILE *fp, int natoms,
681 rvec x[], matrix box)
686 for (i = 0; (i < natoms); i++)
688 for (m = 0; (m < DIM); m++)
690 if (fscanf(fp, "%lf", &x0) != 1)
694 fprintf(stderr, "error reading statusfile: x[%d][%d]\n", i, m);
702 if (status->bReadBox)
704 for (m = 0; (m < DIM); m++)
706 if (fscanf(fp, "%lf", &x0) != 1)
716 static gmx_bool xyz_next_x(t_trxstatus *status, FILE *fp, const output_env_t oenv,
717 real *t, int natoms, rvec x[], matrix box)
718 /* Reads until a new x can be found (return TRUE)
719 * or eof (return FALSE)
725 while (!bTimeSet(TBEGIN) || (*t < rTimeValue(TBEGIN)))
727 if (!do_read_xyz(status, fp, natoms, x, box))
731 printcount(status, oenv, *t, FALSE);
735 if (!bTimeSet(TEND) || (*t <= rTimeValue(TEND)))
737 if (!do_read_xyz(status, fp, natoms, x, box))
739 printlast(status, oenv, *t);
742 printcount(status, oenv, *t, FALSE);
747 printlast(status, oenv, pt);
751 static int xyz_first_x(t_trxstatus *status, FILE *fp, const output_env_t oenv,
752 real *t, rvec **x, matrix box)
753 /* Reads fp, mallocs x, and returns x and box
754 * Returns natoms when successful, FALSE otherwise
762 choose_file_format(fp);
764 for (m = 0; (m < DIM); m++)
766 box[m][m] = status->BOX[m];
769 snew(*x, status->NATOMS);
771 if (!xyz_next_x(status, fp, oenv, t, status->NATOMS, *x, box))
777 return status->NATOMS;
780 static gmx_bool pdb_next_x(t_trxstatus *status, FILE *fp, t_trxframe *fr)
784 int ePBC, model_nr, na;
785 char title[STRLEN], *time;
788 atoms.nr = fr->natoms;
790 atoms.pdbinfo = NULL;
791 /* the other pointers in atoms should not be accessed if these are NULL */
793 na = read_pdbfile(fp, title, &model_nr, &atoms, fr->x, &ePBC, boxpdb, TRUE, NULL);
794 set_trxframe_ePBC(fr, ePBC);
795 if (nframes_read(status) == 0)
797 fprintf(stderr, " '%s', %d atoms\n", title, fr->natoms);
802 fr->bBox = (boxpdb[XX][XX] != 0.0);
805 copy_mat(boxpdb, fr->box);
808 if (model_nr != NOTSET)
813 time = strstr(title, " t= ");
817 sscanf(time+4, "%lf", &dbl);
818 fr->time = (real)dbl;
823 /* this is a bit dirty, but it will work: if no time is read from
824 comment line in pdb file, set time to current frame number */
827 fr->time = (real)fr->step;
831 fr->time = (real)nframes_read(status);
840 if (na != fr->natoms)
842 gmx_fatal(FARGS, "Number of atoms in pdb frame %d is %d instead of %d",
843 nframes_read(status), na, fr->natoms);
849 static int pdb_first_x(t_trxstatus *status, FILE *fp, t_trxframe *fr)
853 fprintf(stderr, "Reading frames from pdb file");
855 get_pdb_coordnum(fp, &fr->natoms);
858 gmx_fatal(FARGS, "\nNo coordinates in pdb file\n");
861 snew(fr->x, fr->natoms);
862 pdb_next_x(status, fp, fr);
867 gmx_bool read_next_frame(const output_env_t oenv, t_trxstatus *status, t_trxframe *fr)
871 gmx_bool bOK, bRet, bMissingData = FALSE, bSkip = FALSE;
879 clear_trxframe(fr, FALSE);
883 switch (gmx_fio_getftp(status->fio))
887 bRet = gmx_next_frame(status, fr);
890 /* Checkpoint files can not contain mulitple frames */
893 read_g96_conf(gmx_fio_getfp(status->fio), NULL, fr,
894 status->persistent_line);
895 bRet = (fr->natoms > 0);
898 bRet = xyz_next_x(status, gmx_fio_getfp(status->fio), oenv, &fr->time,
899 fr->natoms, fr->x, fr->box);
906 * Sometimes is off by one frame
907 * and sometimes reports frame not present/file not seekable
909 /* DvdS 2005-05-31: this has been fixed along with the increased
910 * accuracy of the control over -b and -e options.
912 if (bTimeSet(TBEGIN) && (fr->time < rTimeValue(TBEGIN)))
914 if (xtc_seek_time(status->fio, rTimeValue(TBEGIN), fr->natoms, TRUE))
916 gmx_fatal(FARGS, "Specified frame (time %f) doesn't exist or file corrupt/inconsistent.",
921 bRet = read_next_xtc(status->fio, fr->natoms, &fr->step, &fr->time, fr->box,
922 fr->x, &fr->prec, &bOK);
923 fr->bPrec = (bRet && fr->prec > 0);
930 /* Actually the header could also be not ok,
931 but from bOK from read_next_xtc this can't be distinguished */
932 fr->not_ok = DATA_NOT_OK;
936 bRet = pdb_next_x(status, gmx_fio_getfp(status->fio), fr);
939 bRet = gro_next_x_or_v(gmx_fio_getfp(status->fio), fr);
942 #ifdef GMX_USE_PLUGINS
943 bRet = read_next_vmd_frame(fr);
945 gmx_fatal(FARGS, "DEATH HORROR in read_next_frame ftp=%s,status=%s",
946 ftp2ext(gmx_fio_getftp(status->fio)),
947 gmx_fio_getname(status->fio));
953 bMissingData = (((fr->flags & TRX_NEED_X) && !fr->bX) ||
954 ((fr->flags & TRX_NEED_V) && !fr->bV) ||
955 ((fr->flags & TRX_NEED_F) && !fr->bF));
959 ct = check_times2(fr->time, fr->t0, fr->bDouble);
960 if (ct == 0 || ((fr->flags & TRX_DONT_SKIP) && ct < 0))
962 printcount(status, oenv, fr->time, FALSE);
970 printcount(status, oenv, fr->time, TRUE);
977 while (bRet && (bMissingData || bSkip));
981 printlast(status, oenv, pt);
984 printincomp(status, fr);
991 int read_first_frame(const output_env_t oenv, t_trxstatus **status,
992 const char *fn, t_trxframe *fr, int flags)
995 gmx_bool bFirst, bOK;
998 clear_trxframe(fr, TRUE);
1005 status_init( *status );
1006 (*status)->nxframe = 1;
1009 fio = (*status)->fio = gmx_fio_open(fn, "r");
1010 switch (gmx_fio_getftp(fio))
1016 read_checkpoint_trxframe(fio, fr);
1020 /* Can not rewind a compressed file, so open it twice */
1021 if (!(*status)->persistent_line)
1023 /* allocate the persistent line */
1024 snew((*status)->persistent_line, STRLEN+1);
1026 read_g96_conf(gmx_fio_getfp(fio), fn, fr, (*status)->persistent_line);
1028 clear_trxframe(fr, FALSE);
1029 if (flags & (TRX_READ_X | TRX_NEED_X))
1031 snew(fr->x, fr->natoms);
1033 if (flags & (TRX_READ_V | TRX_NEED_V))
1035 snew(fr->v, fr->natoms);
1037 fio = (*status)->fio = gmx_fio_open(fn, "r");
1040 fr->natoms = xyz_first_x(*status, gmx_fio_getfp(fio), oenv, &fr->time,
1047 printcount(*status, oenv, fr->time, FALSE);
1052 if (read_first_xtc(fio, &fr->natoms, &fr->step, &fr->time, fr->box, &fr->x,
1053 &fr->prec, &bOK) == 0)
1056 fr->not_ok = DATA_NOT_OK;
1061 printincomp(*status, fr);
1065 fr->bPrec = (fr->prec > 0);
1070 printcount(*status, oenv, fr->time, FALSE);
1075 pdb_first_x(*status, gmx_fio_getfp(fio), fr);
1078 printcount(*status, oenv, fr->time, FALSE);
1083 if (gro_first_x_or_v(gmx_fio_getfp(fio), fr))
1085 printcount(*status, oenv, fr->time, FALSE);
1090 #ifdef GMX_USE_PLUGINS
1091 fprintf(stderr, "The file format of %s is not a known trajectory format to GROMACS.\n"
1092 "Please make sure that the file is a trajectory!\n"
1093 "GROMACS will now assume it to be a trajectory and will try to open it using the VMD plug-ins.\n"
1094 "This will only work in case the VMD plugins are found and it is a trajectory format supported by VMD.\n", fn);
1095 gmx_fio_fp_close(fio); /*only close the file without removing FIO entry*/
1096 if (!read_first_vmd_frame(fn, fr))
1098 gmx_fatal(FARGS, "Not supported in read_first_frame: %s", fn);
1101 gmx_fatal(FARGS, "Not supported in read_first_frame: %s. Please make sure that the file is a trajectory.\n"
1102 "GROMACS is not compiled with plug-in support. Thus it cannot read non-GROMACS trajectory formats using the VMD plug-ins.\n"
1103 "Please compile with plug-in support if you want to read non-GROMACS trajectory formats.\n", fn);
1108 /* Return FALSE if we read a frame that's past the set ending time. */
1109 if (!bFirst && (!(fr->flags & TRX_DONT_SKIP) && check_times(fr->time) > 0))
1116 (!(fr->flags & TRX_DONT_SKIP) && check_times(fr->time) < 0))
1118 /* Read a frame when no frame was read or the first was skipped */
1119 if (!read_next_frame(oenv, *status, fr))
1126 return (fr->natoms > 0);
1129 /***** C O O R D I N A T E S T U F F *****/
1131 int read_first_x(const output_env_t oenv, t_trxstatus **status, const char *fn,
1132 real *t, rvec **x, matrix box)
1136 read_first_frame(oenv, status, fn, &fr, TRX_NEED_X);
1138 snew((*status)->xframe, 1);
1139 (*status)->nxframe = 1;
1140 (*(*status)->xframe) = fr;
1141 *t = (*status)->xframe->time;
1142 *x = (*status)->xframe->x;
1143 copy_mat((*status)->xframe->box, box);
1145 return (*status)->xframe->natoms;
1148 gmx_bool read_next_x(const output_env_t oenv, t_trxstatus *status, real *t,
1149 rvec x[], matrix box)
1153 status->xframe->x = x;
1154 /*xframe[status].x = x;*/
1155 bRet = read_next_frame(oenv, status, status->xframe);
1156 *t = status->xframe->time;
1157 copy_mat(status->xframe->box, box);
1162 void close_trj(t_trxstatus *status)
1164 gmx_fio_close(status->fio);
1165 /* The memory in status->xframe is lost here,
1166 * but the read_first_x/read_next_x functions are deprecated anyhow.
1167 * read_first_frame/read_next_frame and close_trx should be used.
1172 void rewind_trj(t_trxstatus *status)
1176 gmx_fio_rewind(status->fio);
1179 /***** T O P O L O G Y S T U F F ******/
1181 t_topology *read_top(const char *fn, int *ePBC)
1187 epbc = read_tpx_top(fn, NULL, NULL, &natoms, NULL, NULL, NULL, top);