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;
201 void set_trxframe_ePBC(t_trxframe *fr, int ePBC)
203 fr->bPBC = (ePBC == -1);
207 int write_trxframe_indexed(t_trxstatus *status, t_trxframe *fr, int nind,
208 atom_id *ind, gmx_conect gc)
211 rvec *xout = NULL, *vout = NULL, *fout = NULL;
224 switch (gmx_fio_getftp(status->fio))
232 gmx_fatal(FARGS, "Need coordinates to write a %s trajectory",
233 ftp2ext(gmx_fio_getftp(status->fio)));
238 switch (gmx_fio_getftp(status->fio))
245 for (i = 0; i < nind; i++)
247 copy_rvec(fr->v[ind[i]], vout[i]);
253 for (i = 0; i < nind; i++)
255 copy_rvec(fr->f[ind[i]], fout[i]);
264 for (i = 0; i < nind; i++)
266 copy_rvec(fr->x[ind[i]], xout[i]);
274 switch (gmx_fio_getftp(status->fio))
277 write_xtc(status->fio, nind, fr->step, fr->time, fr->box, xout, prec);
281 fwrite_trn(status->fio, nframes_read(status),
282 fr->time, fr->step, fr->box, nind, xout, vout, fout);
290 gmx_fatal(FARGS, "Can not write a %s file without atom names",
291 ftp2ext(gmx_fio_getftp(status->fio)));
293 sprintf(title, "frame t= %.3f", fr->time);
294 if (gmx_fio_getftp(status->fio) == efGRO)
296 write_hconf_indexed_p(gmx_fio_getfp(status->fio), title, fr->atoms, nind, ind,
298 fr->x, fr->bV ? fr->v : NULL, fr->box);
302 write_pdbfile_indexed(gmx_fio_getfp(status->fio), title, fr->atoms,
303 fr->x, -1, fr->box, ' ', fr->step, nind, ind, gc, TRUE);
307 write_gms(gmx_fio_getfp(status->fio), nind, xout, fr->box);
310 write_g96_conf(gmx_fio_getfp(status->fio), fr, nind, ind);
313 gmx_fatal(FARGS, "Sorry, write_trxframe_indexed can not write %s",
314 ftp2ext(gmx_fio_getftp(status->fio)));
318 switch (gmx_fio_getftp(status->fio))
343 int write_trxframe(t_trxstatus *status, t_trxframe *fr, gmx_conect gc)
357 switch (gmx_fio_getftp(status->fio))
365 gmx_fatal(FARGS, "Need coordinates to write a %s trajectory",
366 ftp2ext(gmx_fio_getftp(status->fio)));
371 switch (gmx_fio_getftp(status->fio))
374 write_xtc(status->fio, fr->natoms, fr->step, fr->time, fr->box, fr->x, prec);
378 fwrite_trn(status->fio, fr->step, fr->time, fr->lambda, fr->box, fr->natoms,
379 fr->bX ? fr->x : NULL, fr->bV ? fr->v : NULL, fr->bF ? fr->f : NULL);
387 gmx_fatal(FARGS, "Can not write a %s file without atom names",
388 ftp2ext(gmx_fio_getftp(status->fio)));
390 sprintf(title, "frame t= %.3f", fr->time);
391 if (gmx_fio_getftp(status->fio) == efGRO)
393 write_hconf_p(gmx_fio_getfp(status->fio), title, fr->atoms,
394 prec2ndec(prec), fr->x, fr->bV ? fr->v : NULL, fr->box);
398 write_pdbfile(gmx_fio_getfp(status->fio), title,
399 fr->atoms, fr->x, fr->bPBC ? fr->ePBC : -1, fr->box,
400 ' ', fr->step, gc, TRUE);
404 write_gms(gmx_fio_getfp(status->fio), fr->natoms, fr->x, fr->box);
407 write_g96_conf(gmx_fio_getfp(status->fio), fr, -1, NULL);
410 gmx_fatal(FARGS, "Sorry, write_trxframe can not write %s",
411 ftp2ext(gmx_fio_getftp(status->fio)));
418 int write_trx(t_trxstatus *status, int nind, atom_id *ind, t_atoms *atoms,
419 int step, real time, matrix box, rvec x[], rvec *v,
424 clear_trxframe(&fr, TRUE);
429 fr.bAtoms = atoms != NULL;
436 copy_mat(box, fr.box);
438 return write_trxframe_indexed(status, &fr, nind, ind, gc);
441 void close_trx(t_trxstatus *status)
443 gmx_fio_close(status->fio);
447 t_trxstatus *open_trx(const char *outfile, const char *filemode)
450 if (filemode[0] != 'w' && filemode[0] != 'a' && filemode[1] != '+')
452 gmx_fatal(FARGS, "Sorry, write_trx can only write");
458 stat->fio = gmx_fio_open(outfile, filemode);
462 static gmx_bool gmx_next_frame(t_trxstatus *status, t_trxframe *fr)
469 if (fread_trnheader(status->fio, &sh, &bOK))
471 fr->bDouble = sh.bDouble;
472 fr->natoms = sh.natoms;
478 fr->bFepState = TRUE;
479 fr->lambda = sh.lambda;
480 fr->bBox = sh.box_size > 0;
481 if (fr->flags & (TRX_READ_X | TRX_NEED_X))
485 snew(fr->x, sh.natoms);
487 fr->bX = sh.x_size > 0;
489 if (fr->flags & (TRX_READ_V | TRX_NEED_V))
493 snew(fr->v, sh.natoms);
495 fr->bV = sh.v_size > 0;
497 if (fr->flags & (TRX_READ_F | TRX_NEED_F))
501 snew(fr->f, sh.natoms);
503 fr->bF = sh.f_size > 0;
505 if (fread_htrn(status->fio, &sh, fr->box, fr->x, fr->v, fr->f))
511 fr->not_ok = DATA_NOT_OK;
517 fr->not_ok = HEADER_NOT_OK;
523 static void choose_file_format(FILE *fp)
531 printf(" Select File Format\n");
532 printf("---------------------------\n");
533 printf("1. XYZ File\n");
534 printf("2. XYZ File with Box\n");
535 printf("3. Gromos-87 Ascii Trajectory\n");
536 printf("4. Gromos-87 Ascii Trajectory with Box\n");
543 printf("\nChoice: ");
547 rc = scanf("%d", &i);
552 while ((i < 0) || (i >= effNR));
555 stat->eFF = (eFileFormat) i;
557 for (m = 0; (m < DIM); m++)
562 stat->bReadBox = (stat->eFF == effG87Box) || (stat->eFF == effXYZBox);
568 if (5 != fscanf(fp, "%d%lf%lf%lf%lf", &stat->NATOMS, &stat->BOX[XX], &stat->BOX[YY], &stat->BOX[ZZ], &stat->DT))
570 gmx_fatal(FARGS, "Error reading natoms/box in file");
575 printf("GROMOS! OH DEAR...\n\n");
576 printf("Number of atoms ? ");
578 if (1 != scanf("%d", &stat->NATOMS))
580 gmx_fatal(FARGS, "Error reading natoms in file");
583 printf("Time between timeframes ? ");
585 if (1 != scanf("%lf", &stat->DT))
587 gmx_fatal(FARGS, "Error reading dt from file");
590 if (stat->eFF == effG87)
592 printf("Box X Y Z ? ");
594 if (3 != scanf("%lf%lf%lf", &stat->BOX[XX], &stat->BOX[YY], &stat->BOX[ZZ]))
596 gmx_fatal(FARGS, "Error reading box in file");
609 printf("Hellow World\n");
613 static gmx_bool do_read_xyz(t_trxstatus *status, FILE *fp, int natoms,
614 rvec x[], matrix box)
619 for (i = 0; (i < natoms); i++)
621 for (m = 0; (m < DIM); m++)
623 if (fscanf(fp, "%lf", &x0) != 1)
627 fprintf(stderr, "error reading statusfile: x[%d][%d]\n", i, m);
635 if (status->bReadBox)
637 for (m = 0; (m < DIM); m++)
639 if (fscanf(fp, "%lf", &x0) != 1)
649 static gmx_bool xyz_next_x(t_trxstatus *status, FILE *fp, const output_env_t oenv,
650 real *t, int natoms, rvec x[], matrix box)
651 /* Reads until a new x can be found (return TRUE)
652 * or eof (return FALSE)
658 while (!bTimeSet(TBEGIN) || (*t < rTimeValue(TBEGIN)))
660 if (!do_read_xyz(status, fp, natoms, x, box))
664 printcount(status, oenv, *t, FALSE);
668 if (!bTimeSet(TEND) || (*t <= rTimeValue(TEND)))
670 if (!do_read_xyz(status, fp, natoms, x, box))
672 printlast(status, oenv, *t);
675 printcount(status, oenv, *t, FALSE);
680 printlast(status, oenv, pt);
684 static int xyz_first_x(t_trxstatus *status, FILE *fp, const output_env_t oenv,
685 real *t, rvec **x, matrix box)
686 /* Reads fp, mallocs x, and returns x and box
687 * Returns natoms when successful, FALSE otherwise
695 choose_file_format(fp);
697 for (m = 0; (m < DIM); m++)
699 box[m][m] = status->BOX[m];
702 snew(*x, status->NATOMS);
704 if (!xyz_next_x(status, fp, oenv, t, status->NATOMS, *x, box))
710 return status->NATOMS;
713 static gmx_bool pdb_next_x(t_trxstatus *status, FILE *fp, t_trxframe *fr)
717 int ePBC, model_nr, na;
718 char title[STRLEN], *time;
721 atoms.nr = fr->natoms;
723 atoms.pdbinfo = NULL;
724 /* the other pointers in atoms should not be accessed if these are NULL */
726 na = read_pdbfile(fp, title, &model_nr, &atoms, fr->x, &ePBC, boxpdb, TRUE, NULL);
727 set_trxframe_ePBC(fr, ePBC);
728 if (nframes_read(status) == 0)
730 fprintf(stderr, " '%s', %d atoms\n", title, fr->natoms);
735 fr->bBox = (boxpdb[XX][XX] != 0.0);
738 copy_mat(boxpdb, fr->box);
741 if (model_nr != NOTSET)
746 time = strstr(title, " t= ");
750 sscanf(time+4, "%lf", &dbl);
751 fr->time = (real)dbl;
756 /* this is a bit dirty, but it will work: if no time is read from
757 comment line in pdb file, set time to current frame number */
760 fr->time = (real)fr->step;
764 fr->time = (real)nframes_read(status);
773 if (na != fr->natoms)
775 gmx_fatal(FARGS, "Number of atoms in pdb frame %d is %d instead of %d",
776 nframes_read(status), na, fr->natoms);
782 static int pdb_first_x(t_trxstatus *status, FILE *fp, t_trxframe *fr)
786 fprintf(stderr, "Reading frames from pdb file");
788 get_pdb_coordnum(fp, &fr->natoms);
791 gmx_fatal(FARGS, "\nNo coordinates in pdb file\n");
794 snew(fr->x, fr->natoms);
795 pdb_next_x(status, fp, fr);
800 gmx_bool read_next_frame(const output_env_t oenv, t_trxstatus *status, t_trxframe *fr)
804 gmx_bool bOK, bRet, bMissingData = FALSE, bSkip = FALSE;
812 clear_trxframe(fr, FALSE);
816 switch (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);
831 bRet = xyz_next_x(status, gmx_fio_getfp(status->fio), oenv, &fr->time,
832 fr->natoms, fr->x, fr->box);
839 * Sometimes is off by one frame
840 * and sometimes reports frame not present/file not seekable
842 /* DvdS 2005-05-31: this has been fixed along with the increased
843 * accuracy of the control over -b and -e options.
845 if (bTimeSet(TBEGIN) && (fr->time < rTimeValue(TBEGIN)))
847 if (xtc_seek_time(status->fio, rTimeValue(TBEGIN), fr->natoms, TRUE))
849 gmx_fatal(FARGS, "Specified frame (time %f) doesn't exist or file corrupt/inconsistent.",
854 bRet = read_next_xtc(status->fio, fr->natoms, &fr->step, &fr->time, fr->box,
855 fr->x, &fr->prec, &bOK);
856 fr->bPrec = (bRet && fr->prec > 0);
863 /* Actually the header could also be not ok,
864 but from bOK from read_next_xtc this can't be distinguished */
865 fr->not_ok = DATA_NOT_OK;
869 bRet = pdb_next_x(status, gmx_fio_getfp(status->fio), fr);
872 bRet = gro_next_x_or_v(gmx_fio_getfp(status->fio), fr);
875 #ifdef GMX_USE_PLUGINS
876 bRet = read_next_vmd_frame(dummy, fr);
878 gmx_fatal(FARGS, "DEATH HORROR in read_next_frame ftp=%s,status=%s",
879 ftp2ext(gmx_fio_getftp(status->fio)),
880 gmx_fio_getname(status->fio));
886 bMissingData = (((fr->flags & TRX_NEED_X) && !fr->bX) ||
887 ((fr->flags & TRX_NEED_V) && !fr->bV) ||
888 ((fr->flags & TRX_NEED_F) && !fr->bF));
892 ct = check_times2(fr->time, fr->t0, fr->tpf, fr->tppf, fr->bDouble);
893 if (ct == 0 || ((fr->flags & TRX_DONT_SKIP) && ct < 0))
895 printcount(status, oenv, fr->time, FALSE);
903 printcount(status, oenv, fr->time, TRUE);
910 while (bRet && (bMissingData || bSkip));
914 printlast(status, oenv, pt);
917 printincomp(status, fr);
924 int read_first_frame(const output_env_t oenv, t_trxstatus **status,
925 const char *fn, t_trxframe *fr, int flags)
928 gmx_bool bFirst, bOK;
931 clear_trxframe(fr, TRUE);
938 status_init( *status );
939 (*status)->nxframe = 1;
942 fio = (*status)->fio = gmx_fio_open(fn, "r");
943 switch (gmx_fio_getftp(fio))
949 read_checkpoint_trxframe(fio, fr);
953 /* Can not rewind a compressed file, so open it twice */
954 if (!(*status)->persistent_line)
956 /* allocate the persistent line */
957 snew((*status)->persistent_line, STRLEN+1);
959 read_g96_conf(gmx_fio_getfp(fio), fn, fr, (*status)->persistent_line);
961 clear_trxframe(fr, FALSE);
962 if (flags & (TRX_READ_X | TRX_NEED_X))
964 snew(fr->x, fr->natoms);
966 if (flags & (TRX_READ_V | TRX_NEED_V))
968 snew(fr->v, fr->natoms);
970 fio = (*status)->fio = gmx_fio_open(fn, "r");
973 fr->natoms = xyz_first_x(*status, gmx_fio_getfp(fio), oenv, &fr->time,
980 printcount(*status, oenv, fr->time, FALSE);
985 if (read_first_xtc(fio, &fr->natoms, &fr->step, &fr->time, fr->box, &fr->x,
986 &fr->prec, &bOK) == 0)
990 gmx_fatal(FARGS, "No XTC!\n");
994 fr->not_ok = DATA_NOT_OK;
1000 printincomp(*status, fr);
1004 fr->bPrec = (fr->prec > 0);
1009 printcount(*status, oenv, fr->time, FALSE);
1014 pdb_first_x(*status, gmx_fio_getfp(fio), fr);
1017 printcount(*status, oenv, fr->time, FALSE);
1022 if (gro_first_x_or_v(gmx_fio_getfp(fio), fr))
1024 printcount(*status, oenv, fr->time, FALSE);
1029 #ifdef GMX_USE_PLUGINS
1030 fprintf(stderr, "The file format of %s is not a known trajectory format to GROMACS.\n"
1031 "Please make sure that the file is a trajectory!\n"
1032 "GROMACS will now assume it to be a trajectory and will try to open it using the VMD plug-ins.\n"
1033 "This will only work in case the VMD plugins are found and it is a trajectory format supported by VMD.\n", fn);
1034 gmx_fio_fp_close(fio); /*only close the file without removing FIO entry*/
1035 if (!read_first_vmd_frame(&dummy, fn, fr, flags))
1037 gmx_fatal(FARGS, "Not supported in read_first_frame: %s", fn);
1040 gmx_fatal(FARGS, "Not supported in read_first_frame: %s. Please make sure that the file is a trajectory.\n"
1041 "GROMACS is not compiled with plug-in support. Thus it cannot read non-GROMACS trajectory formats using the VMD plug-ins.\n"
1042 "Please compile with plug-in support if you want to read non-GROMACS trajectory formats.\n", fn);
1047 /* Return FALSE if we read a frame that's past the set ending time. */
1048 if (!bFirst && (!(fr->flags & TRX_DONT_SKIP) && check_times(fr->time) > 0))
1055 (!(fr->flags & TRX_DONT_SKIP) && check_times(fr->time) < 0))
1057 /* Read a frame when no frame was read or the first was skipped */
1058 if (!read_next_frame(oenv, *status, fr))
1065 return (fr->natoms > 0);
1068 /***** C O O R D I N A T E S T U F F *****/
1070 int read_first_x(const output_env_t oenv, t_trxstatus **status, const char *fn,
1071 real *t, rvec **x, matrix box)
1075 read_first_frame(oenv, status, fn, &fr, TRX_NEED_X);
1077 snew((*status)->xframe, 1);
1078 (*status)->nxframe = 1;
1079 (*(*status)->xframe) = fr;
1080 *t = (*status)->xframe->time;
1081 *x = (*status)->xframe->x;
1082 copy_mat((*status)->xframe->box, box);
1084 return (*status)->xframe->natoms;
1087 gmx_bool read_next_x(const output_env_t oenv, t_trxstatus *status, real *t,
1088 int natoms, rvec x[], matrix box)
1092 status->xframe->x = x;
1093 /*xframe[status].x = x;*/
1094 bRet = read_next_frame(oenv, status, status->xframe);
1095 *t = status->xframe->time;
1096 copy_mat(status->xframe->box, box);
1101 void close_trj(t_trxstatus *status)
1103 gmx_fio_close(status->fio);
1104 /* The memory in status->xframe is lost here,
1105 * but the read_first_x/read_next_x functions are deprecated anyhow.
1106 * read_first_frame/read_next_frame and close_trx should be used.
1111 void rewind_trj(t_trxstatus *status)
1115 gmx_fio_rewind(status->fio);
1118 /***** V E L O C I T Y S T U F F *****/
1120 static void clear_v(t_trxframe *fr)
1126 for (i = 0; i < fr->natoms; i++)
1128 clear_rvec(fr->v[i]);