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 * check out http://www.gromacs.org for more information.
7 * Copyright (c) 2012,2013, by the GROMACS development team, led by
8 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9 * others, as listed in the AUTHORS file in the top-level source
10 * directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
59 #include "checkpoint.h"
63 /* defines for frame counter output */
68 /* Globals for gromos-87 input */
70 effXYZ, effXYZBox, effG87, effG87Box, effNR
83 char *persistent_line; /* Persistent line for reading g96 trajectories */
86 static void initcount(t_trxstatus *status)
91 static void status_init(t_trxstatus *status)
94 status->xframe = NULL;
97 status->persistent_line = NULL;
101 int nframes_read(t_trxstatus *status)
103 return status->__frame;
106 static void printcount_(t_trxstatus *status, const output_env_t oenv,
107 const char *l, real t)
109 if ((status->__frame < 2*SKIP1 || status->__frame % SKIP1 == 0) &&
110 (status->__frame < 2*SKIP2 || status->__frame % SKIP2 == 0) &&
111 (status->__frame < 2*SKIP3 || status->__frame % SKIP3 == 0))
113 fprintf(stderr, "\r%-14s %6d time %8.3f ", l, status->__frame,
114 output_env_conv_time(oenv, t));
118 static void printcount(t_trxstatus *status, const output_env_t oenv, real t,
122 printcount_(status, oenv, bSkip ? "Skipping frame" : "Reading frame", t);
125 static void printlast(t_trxstatus *status, const output_env_t oenv, real t)
127 printcount_(status, oenv, "Last frame", t);
128 fprintf(stderr, "\n");
131 static void printincomp(t_trxstatus *status, t_trxframe *fr)
133 if (fr->not_ok & HEADER_NOT_OK)
135 fprintf(stderr, "WARNING: Incomplete header: nr %d time %g\n",
136 status->__frame+1, fr->time);
140 fprintf(stderr, "WARNING: Incomplete frame: nr %d time %g\n",
141 status->__frame+1, fr->time);
145 int prec2ndec(real prec)
149 gmx_fatal(FARGS, "DEATH HORROR prec (%g) <= 0 in prec2ndec", prec);
152 return (int)(log(prec)/log(10.0)+0.5);
156 t_fileio *trx_get_fileio(t_trxstatus *status)
163 void clear_trxframe(t_trxframe *fr, gmx_bool bFirst)
170 fr->bFepState = FALSE;
202 void set_trxframe_ePBC(t_trxframe *fr, int ePBC)
204 fr->bPBC = (ePBC == -1);
208 int write_trxframe_indexed(t_trxstatus *status, t_trxframe *fr, int nind,
209 atom_id *ind, gmx_conect gc)
212 rvec *xout = NULL, *vout = NULL, *fout = NULL;
225 switch (gmx_fio_getftp(status->fio))
233 gmx_fatal(FARGS, "Need coordinates to write a %s trajectory",
234 ftp2ext(gmx_fio_getftp(status->fio)));
239 switch (gmx_fio_getftp(status->fio))
246 for (i = 0; i < nind; i++)
248 copy_rvec(fr->v[ind[i]], vout[i]);
254 for (i = 0; i < nind; i++)
256 copy_rvec(fr->f[ind[i]], fout[i]);
265 for (i = 0; i < nind; i++)
267 copy_rvec(fr->x[ind[i]], xout[i]);
275 switch (gmx_fio_getftp(status->fio))
278 write_xtc(status->fio, nind, fr->step, fr->time, fr->box, xout, prec);
282 fwrite_trn(status->fio, nframes_read(status),
283 fr->time, fr->step, fr->box, nind, xout, vout, fout);
291 gmx_fatal(FARGS, "Can not write a %s file without atom names",
292 ftp2ext(gmx_fio_getftp(status->fio)));
294 sprintf(title, "frame t= %.3f", fr->time);
295 if (gmx_fio_getftp(status->fio) == efGRO)
297 write_hconf_indexed_p(gmx_fio_getfp(status->fio), title, fr->atoms, nind, ind,
299 fr->x, fr->bV ? fr->v : NULL, fr->box);
303 write_pdbfile_indexed(gmx_fio_getfp(status->fio), title, fr->atoms,
304 fr->x, -1, fr->box, ' ', fr->step, nind, ind, gc, TRUE);
308 write_gms(gmx_fio_getfp(status->fio), nind, xout, fr->box);
311 write_g96_conf(gmx_fio_getfp(status->fio), fr, nind, ind);
314 gmx_fatal(FARGS, "Sorry, write_trxframe_indexed can not write %s",
315 ftp2ext(gmx_fio_getftp(status->fio)));
319 switch (gmx_fio_getftp(status->fio))
344 int write_trxframe(t_trxstatus *status, t_trxframe *fr, gmx_conect gc)
358 switch (gmx_fio_getftp(status->fio))
366 gmx_fatal(FARGS, "Need coordinates to write a %s trajectory",
367 ftp2ext(gmx_fio_getftp(status->fio)));
372 switch (gmx_fio_getftp(status->fio))
375 write_xtc(status->fio, fr->natoms, fr->step, fr->time, fr->box, fr->x, prec);
379 fwrite_trn(status->fio, fr->step, fr->time, fr->lambda, fr->box, fr->natoms,
380 fr->bX ? fr->x : NULL, fr->bV ? fr->v : NULL, fr->bF ? fr->f : NULL);
388 gmx_fatal(FARGS, "Can not write a %s file without atom names",
389 ftp2ext(gmx_fio_getftp(status->fio)));
391 sprintf(title, "frame t= %.3f", fr->time);
392 if (gmx_fio_getftp(status->fio) == efGRO)
394 write_hconf_p(gmx_fio_getfp(status->fio), title, fr->atoms,
395 prec2ndec(prec), fr->x, fr->bV ? fr->v : NULL, fr->box);
399 write_pdbfile(gmx_fio_getfp(status->fio), title,
400 fr->atoms, fr->x, fr->bPBC ? fr->ePBC : -1, fr->box,
401 ' ', fr->step, gc, TRUE);
405 write_gms(gmx_fio_getfp(status->fio), fr->natoms, fr->x, fr->box);
408 write_g96_conf(gmx_fio_getfp(status->fio), fr, -1, NULL);
411 gmx_fatal(FARGS, "Sorry, write_trxframe can not write %s",
412 ftp2ext(gmx_fio_getftp(status->fio)));
419 int write_trx(t_trxstatus *status, int nind, atom_id *ind, t_atoms *atoms,
420 int step, real time, matrix box, rvec x[], rvec *v,
425 clear_trxframe(&fr, TRUE);
430 fr.bAtoms = atoms != NULL;
437 copy_mat(box, fr.box);
439 return write_trxframe_indexed(status, &fr, nind, ind, gc);
442 void close_trx(t_trxstatus *status)
444 gmx_fio_close(status->fio);
448 t_trxstatus *open_trx(const char *outfile, const char *filemode)
451 if (filemode[0] != 'w' && filemode[0] != 'a' && filemode[1] != '+')
453 gmx_fatal(FARGS, "Sorry, write_trx can only write");
459 stat->fio = gmx_fio_open(outfile, filemode);
463 static gmx_bool gmx_next_frame(t_trxstatus *status, t_trxframe *fr)
470 if (fread_trnheader(status->fio, &sh, &bOK))
472 fr->bDouble = sh.bDouble;
473 fr->natoms = sh.natoms;
479 fr->bFepState = TRUE;
480 fr->lambda = sh.lambda;
481 fr->bBox = sh.box_size > 0;
482 if (fr->flags & (TRX_READ_X | TRX_NEED_X))
486 snew(fr->x, sh.natoms);
488 fr->bX = sh.x_size > 0;
490 if (fr->flags & (TRX_READ_V | TRX_NEED_V))
494 snew(fr->v, sh.natoms);
496 fr->bV = sh.v_size > 0;
498 if (fr->flags & (TRX_READ_F | TRX_NEED_F))
502 snew(fr->f, sh.natoms);
504 fr->bF = sh.f_size > 0;
506 if (fread_htrn(status->fio, &sh, fr->box, fr->x, fr->v, fr->f))
512 fr->not_ok = DATA_NOT_OK;
518 fr->not_ok = HEADER_NOT_OK;
524 static void choose_file_format(FILE *fp)
532 printf(" Select File Format\n");
533 printf("---------------------------\n");
534 printf("1. XYZ File\n");
535 printf("2. XYZ File with Box\n");
536 printf("3. Gromos-87 Ascii Trajectory\n");
537 printf("4. Gromos-87 Ascii Trajectory with Box\n");
544 printf("\nChoice: ");
548 rc = scanf("%d", &i);
553 while ((i < 0) || (i >= effNR));
556 stat->eFF = (eFileFormat) i;
558 for (m = 0; (m < DIM); m++)
563 stat->bReadBox = (stat->eFF == effG87Box) || (stat->eFF == effXYZBox);
569 if (5 != fscanf(fp, "%d%lf%lf%lf%lf", &stat->NATOMS, &stat->BOX[XX], &stat->BOX[YY], &stat->BOX[ZZ], &stat->DT))
571 gmx_fatal(FARGS, "Error reading natoms/box in file");
576 printf("GROMOS! OH DEAR...\n\n");
577 printf("Number of atoms ? ");
579 if (1 != scanf("%d", &stat->NATOMS))
581 gmx_fatal(FARGS, "Error reading natoms in file");
584 printf("Time between timeframes ? ");
586 if (1 != scanf("%lf", &stat->DT))
588 gmx_fatal(FARGS, "Error reading dt from file");
591 if (stat->eFF == effG87)
593 printf("Box X Y Z ? ");
595 if (3 != scanf("%lf%lf%lf", &stat->BOX[XX], &stat->BOX[YY], &stat->BOX[ZZ]))
597 gmx_fatal(FARGS, "Error reading box in file");
610 printf("Hellow World\n");
614 static gmx_bool do_read_xyz(t_trxstatus *status, FILE *fp, int natoms,
615 rvec x[], matrix box)
620 for (i = 0; (i < natoms); i++)
622 for (m = 0; (m < DIM); m++)
624 if (fscanf(fp, "%lf", &x0) != 1)
628 fprintf(stderr, "error reading statusfile: x[%d][%d]\n", i, m);
636 if (status->bReadBox)
638 for (m = 0; (m < DIM); m++)
640 if (fscanf(fp, "%lf", &x0) != 1)
650 static gmx_bool xyz_next_x(t_trxstatus *status, FILE *fp, const output_env_t oenv,
651 real *t, int natoms, rvec x[], matrix box)
652 /* Reads until a new x can be found (return TRUE)
653 * or eof (return FALSE)
659 while (!bTimeSet(TBEGIN) || (*t < rTimeValue(TBEGIN)))
661 if (!do_read_xyz(status, fp, natoms, x, box))
665 printcount(status, oenv, *t, FALSE);
669 if (!bTimeSet(TEND) || (*t <= rTimeValue(TEND)))
671 if (!do_read_xyz(status, fp, natoms, x, box))
673 printlast(status, oenv, *t);
676 printcount(status, oenv, *t, FALSE);
681 printlast(status, oenv, pt);
685 static int xyz_first_x(t_trxstatus *status, FILE *fp, const output_env_t oenv,
686 real *t, rvec **x, matrix box)
687 /* Reads fp, mallocs x, and returns x and box
688 * Returns natoms when successful, FALSE otherwise
696 choose_file_format(fp);
698 for (m = 0; (m < DIM); m++)
700 box[m][m] = status->BOX[m];
703 snew(*x, status->NATOMS);
705 if (!xyz_next_x(status, fp, oenv, t, status->NATOMS, *x, box))
711 return status->NATOMS;
714 static gmx_bool pdb_next_x(t_trxstatus *status, FILE *fp, t_trxframe *fr)
718 int ePBC, model_nr, na;
719 char title[STRLEN], *time;
722 atoms.nr = fr->natoms;
724 atoms.pdbinfo = NULL;
725 /* the other pointers in atoms should not be accessed if these are NULL */
727 na = read_pdbfile(fp, title, &model_nr, &atoms, fr->x, &ePBC, boxpdb, TRUE, NULL);
728 set_trxframe_ePBC(fr, ePBC);
729 if (nframes_read(status) == 0)
731 fprintf(stderr, " '%s', %d atoms\n", title, fr->natoms);
736 fr->bBox = (boxpdb[XX][XX] != 0.0);
739 copy_mat(boxpdb, fr->box);
742 if (model_nr != NOTSET)
747 time = strstr(title, " t= ");
751 sscanf(time+4, "%lf", &dbl);
752 fr->time = (real)dbl;
757 /* this is a bit dirty, but it will work: if no time is read from
758 comment line in pdb file, set time to current frame number */
761 fr->time = (real)fr->step;
765 fr->time = (real)nframes_read(status);
774 if (na != fr->natoms)
776 gmx_fatal(FARGS, "Number of atoms in pdb frame %d is %d instead of %d",
777 nframes_read(status), na, fr->natoms);
783 static int pdb_first_x(t_trxstatus *status, FILE *fp, t_trxframe *fr)
787 fprintf(stderr, "Reading frames from pdb file");
789 get_pdb_coordnum(fp, &fr->natoms);
792 gmx_fatal(FARGS, "\nNo coordinates in pdb file\n");
795 snew(fr->x, fr->natoms);
796 pdb_next_x(status, fp, fr);
801 gmx_bool read_next_frame(const output_env_t oenv, t_trxstatus *status, t_trxframe *fr)
805 gmx_bool bOK, bRet, bMissingData = FALSE, bSkip = FALSE;
813 clear_trxframe(fr, FALSE);
817 switch (gmx_fio_getftp(status->fio))
821 bRet = gmx_next_frame(status, fr);
824 /* Checkpoint files can not contain mulitple frames */
827 read_g96_conf(gmx_fio_getfp(status->fio), NULL, fr,
828 status->persistent_line);
829 bRet = (fr->natoms > 0);
832 bRet = xyz_next_x(status, gmx_fio_getfp(status->fio), oenv, &fr->time,
833 fr->natoms, fr->x, fr->box);
840 * Sometimes is off by one frame
841 * and sometimes reports frame not present/file not seekable
843 /* DvdS 2005-05-31: this has been fixed along with the increased
844 * accuracy of the control over -b and -e options.
846 if (bTimeSet(TBEGIN) && (fr->tf < rTimeValue(TBEGIN)))
848 if (xtc_seek_time(status->fio, rTimeValue(TBEGIN), fr->natoms, TRUE))
850 gmx_fatal(FARGS, "Specified frame (time %f) doesn't exist or file corrupt/inconsistent.",
855 bRet = read_next_xtc(status->fio, fr->natoms, &fr->step, &fr->time, fr->box,
856 fr->x, &fr->prec, &bOK);
857 fr->bPrec = (bRet && fr->prec > 0);
864 /* Actually the header could also be not ok,
865 but from bOK from read_next_xtc this can't be distinguished */
866 fr->not_ok = DATA_NOT_OK;
870 bRet = pdb_next_x(status, gmx_fio_getfp(status->fio), fr);
873 bRet = gro_next_x_or_v(gmx_fio_getfp(status->fio), fr);
876 #ifdef GMX_USE_PLUGINS
877 bRet = read_next_vmd_frame(dummy, fr);
879 gmx_fatal(FARGS, "DEATH HORROR in read_next_frame ftp=%s,status=%s",
880 ftp2ext(gmx_fio_getftp(status->fio)),
881 gmx_fio_getname(status->fio));
888 bMissingData = (((fr->flags & TRX_NEED_X) && !fr->bX) ||
889 ((fr->flags & TRX_NEED_V) && !fr->bV) ||
890 ((fr->flags & TRX_NEED_F) && !fr->bF));
894 ct = check_times2(fr->time, fr->t0, fr->tpf, fr->tppf, fr->bDouble);
895 if (ct == 0 || ((fr->flags & TRX_DONT_SKIP) && ct < 0))
897 printcount(status, oenv, fr->time, FALSE);
905 printcount(status, oenv, fr->time, TRUE);
912 while (bRet && (bMissingData || bSkip));
916 printlast(status, oenv, pt);
919 printincomp(status, fr);
926 int read_first_frame(const output_env_t oenv, t_trxstatus **status,
927 const char *fn, t_trxframe *fr, int flags)
930 gmx_bool bFirst, bOK;
933 clear_trxframe(fr, TRUE);
940 status_init( *status );
941 (*status)->nxframe = 1;
944 fio = (*status)->fio = gmx_fio_open(fn, "r");
945 switch (gmx_fio_getftp(fio))
951 read_checkpoint_trxframe(fio, fr);
955 /* Can not rewind a compressed file, so open it twice */
956 if (!(*status)->persistent_line)
958 /* allocate the persistent line */
959 snew((*status)->persistent_line, STRLEN+1);
961 read_g96_conf(gmx_fio_getfp(fio), fn, fr, (*status)->persistent_line);
963 clear_trxframe(fr, FALSE);
964 if (flags & (TRX_READ_X | TRX_NEED_X))
966 snew(fr->x, fr->natoms);
968 if (flags & (TRX_READ_V | TRX_NEED_V))
970 snew(fr->v, fr->natoms);
972 fio = (*status)->fio = gmx_fio_open(fn, "r");
975 fr->natoms = xyz_first_x(*status, gmx_fio_getfp(fio), oenv, &fr->time,
982 printcount(*status, oenv, fr->time, FALSE);
987 if (read_first_xtc(fio, &fr->natoms, &fr->step, &fr->time, fr->box, &fr->x,
988 &fr->prec, &bOK) == 0)
992 gmx_fatal(FARGS, "No XTC!\n");
996 fr->not_ok = DATA_NOT_OK;
1002 printincomp(*status, fr);
1006 fr->bPrec = (fr->prec > 0);
1011 printcount(*status, oenv, fr->time, FALSE);
1016 pdb_first_x(*status, gmx_fio_getfp(fio), fr);
1019 printcount(*status, oenv, fr->time, FALSE);
1024 if (gro_first_x_or_v(gmx_fio_getfp(fio), fr))
1026 printcount(*status, oenv, fr->time, FALSE);
1031 #ifdef GMX_USE_PLUGINS
1032 fprintf(stderr, "The file format of %s is not a known trajectory format to GROMACS.\n"
1033 "Please make sure that the file is a trajectory!\n"
1034 "GROMACS will now assume it to be a trajectory and will try to open it using the VMD plug-ins.\n"
1035 "This will only work in case the VMD plugins are found and it is a trajectory format supported by VMD.\n", fn);
1036 gmx_fio_fp_close(fio); /*only close the file without removing FIO entry*/
1037 if (!read_first_vmd_frame(&dummy, fn, fr, flags))
1039 gmx_fatal(FARGS, "Not supported in read_first_frame: %s", fn);
1042 gmx_fatal(FARGS, "Not supported in read_first_frame: %s. Please make sure that the file is a trajectory.\n"
1043 "GROMACS is not compiled with plug-in support. Thus it cannot read non-GROMACS trajectory formats using the VMD plug-ins.\n"
1044 "Please compile with plug-in support if you want to read non-GROMACS trajectory formats.\n", fn);
1050 /* Return FALSE if we read a frame that's past the set ending time. */
1051 if (!bFirst && (!(fr->flags & TRX_DONT_SKIP) && check_times(fr->time) > 0))
1058 (!(fr->flags & TRX_DONT_SKIP) && check_times(fr->time) < 0))
1060 /* Read a frame when no frame was read or the first was skipped */
1061 if (!read_next_frame(oenv, *status, fr))
1068 return (fr->natoms > 0);
1071 /***** C O O R D I N A T E S T U F F *****/
1073 int read_first_x(const output_env_t oenv, t_trxstatus **status, const char *fn,
1074 real *t, rvec **x, matrix box)
1078 read_first_frame(oenv, status, fn, &fr, TRX_NEED_X);
1080 snew((*status)->xframe, 1);
1081 (*status)->nxframe = 1;
1082 (*(*status)->xframe) = fr;
1083 *t = (*status)->xframe->time;
1084 *x = (*status)->xframe->x;
1085 copy_mat((*status)->xframe->box, box);
1087 return (*status)->xframe->natoms;
1090 gmx_bool read_next_x(const output_env_t oenv, t_trxstatus *status, real *t,
1091 int natoms, rvec x[], matrix box)
1095 status->xframe->x = x;
1096 /*xframe[status].x = x;*/
1097 bRet = read_next_frame(oenv, status, status->xframe);
1098 *t = status->xframe->time;
1099 copy_mat(status->xframe->box, box);
1104 void close_trj(t_trxstatus *status)
1106 gmx_fio_close(status->fio);
1107 /* The memory in status->xframe is lost here,
1108 * but the read_first_x/read_next_x functions are deprecated anyhow.
1109 * read_first_frame/read_next_frame and close_trx should be used.
1114 void rewind_trj(t_trxstatus *status)
1118 gmx_fio_rewind(status->fio);
1121 /***** V E L O C I T Y S T U F F *****/
1123 static void clear_v(t_trxframe *fr)
1129 for (i = 0; i < fr->natoms; i++)
1131 clear_rvec(fr->v[i]);