3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2004, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code forxd
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
33 * GROningen Mixture of Alchemy and Childrens' Stories
56 #include "checkpoint.h"
60 /* defines for frame counter output */
65 /* Globals for gromos-87 input */
67 effXYZ, effXYZBox, effG87, effG87Box, effNR
80 char *persistent_line; /* Persistent line for reading g96 trajectories */
83 static void initcount(t_trxstatus *status)
88 static void status_init(t_trxstatus *status)
91 status->xframe = NULL;
94 status->persistent_line = NULL;
98 int nframes_read(t_trxstatus *status)
100 return status->__frame;
103 static void printcount_(t_trxstatus *status, const output_env_t oenv,
104 const char *l, real t)
106 if ((status->__frame < 2*SKIP1 || status->__frame % SKIP1 == 0) &&
107 (status->__frame < 2*SKIP2 || status->__frame % SKIP2 == 0) &&
108 (status->__frame < 2*SKIP3 || status->__frame % SKIP3 == 0))
110 fprintf(stderr, "\r%-14s %6d time %8.3f ", l, status->__frame,
111 output_env_conv_time(oenv, t));
115 static void printcount(t_trxstatus *status, const output_env_t oenv, real t,
119 printcount_(status, oenv, bSkip ? "Skipping frame" : "Reading frame", t);
122 static void printlast(t_trxstatus *status, const output_env_t oenv, real t)
124 printcount_(status, oenv, "Last frame", t);
125 fprintf(stderr, "\n");
128 static void printincomp(t_trxstatus *status, t_trxframe *fr)
130 if (fr->not_ok & HEADER_NOT_OK)
132 fprintf(stderr, "WARNING: Incomplete header: nr %d time %g\n",
133 status->__frame+1, fr->time);
137 fprintf(stderr, "WARNING: Incomplete frame: nr %d time %g\n",
138 status->__frame+1, fr->time);
142 int prec2ndec(real prec)
146 gmx_fatal(FARGS, "DEATH HORROR prec (%g) <= 0 in prec2ndec", prec);
149 return (int)(log(prec)/log(10.0)+0.5);
153 t_fileio *trx_get_fileio(t_trxstatus *status)
160 void clear_trxframe(t_trxframe *fr, gmx_bool bFirst)
167 fr->bFepState = FALSE;
198 void set_trxframe_ePBC(t_trxframe *fr, int ePBC)
200 fr->bPBC = (ePBC == -1);
204 int write_trxframe_indexed(t_trxstatus *status, t_trxframe *fr, int nind,
205 const atom_id *ind, gmx_conect gc)
208 rvec *xout = NULL, *vout = NULL, *fout = NULL;
221 switch (gmx_fio_getftp(status->fio))
229 gmx_fatal(FARGS, "Need coordinates to write a %s trajectory",
230 ftp2ext(gmx_fio_getftp(status->fio)));
235 switch (gmx_fio_getftp(status->fio))
242 for (i = 0; i < nind; i++)
244 copy_rvec(fr->v[ind[i]], vout[i]);
250 for (i = 0; i < nind; i++)
252 copy_rvec(fr->f[ind[i]], fout[i]);
261 for (i = 0; i < nind; i++)
263 copy_rvec(fr->x[ind[i]], xout[i]);
271 switch (gmx_fio_getftp(status->fio))
274 write_xtc(status->fio, nind, fr->step, fr->time, fr->box, xout, prec);
278 fwrite_trn(status->fio, nframes_read(status),
279 fr->time, fr->step, fr->box, nind, xout, vout, fout);
287 gmx_fatal(FARGS, "Can not write a %s file without atom names",
288 ftp2ext(gmx_fio_getftp(status->fio)));
290 sprintf(title, "frame t= %.3f", fr->time);
291 if (gmx_fio_getftp(status->fio) == efGRO)
293 write_hconf_indexed_p(gmx_fio_getfp(status->fio), title, fr->atoms, nind, ind,
295 fr->x, fr->bV ? fr->v : NULL, fr->box);
299 write_pdbfile_indexed(gmx_fio_getfp(status->fio), title, fr->atoms,
300 fr->x, -1, fr->box, ' ', fr->step, nind, ind, gc, TRUE);
304 write_gms(gmx_fio_getfp(status->fio), nind, xout, fr->box);
307 write_g96_conf(gmx_fio_getfp(status->fio), fr, nind, ind);
310 gmx_fatal(FARGS, "Sorry, write_trxframe_indexed can not write %s",
311 ftp2ext(gmx_fio_getftp(status->fio)));
315 switch (gmx_fio_getftp(status->fio))
340 int write_trxframe(t_trxstatus *status, t_trxframe *fr, gmx_conect gc)
354 switch (gmx_fio_getftp(status->fio))
362 gmx_fatal(FARGS, "Need coordinates to write a %s trajectory",
363 ftp2ext(gmx_fio_getftp(status->fio)));
368 switch (gmx_fio_getftp(status->fio))
371 write_xtc(status->fio, fr->natoms, fr->step, fr->time, fr->box, fr->x, prec);
375 fwrite_trn(status->fio, fr->step, fr->time, fr->lambda, fr->box, fr->natoms,
376 fr->bX ? fr->x : NULL, fr->bV ? fr->v : NULL, fr->bF ? fr->f : NULL);
384 gmx_fatal(FARGS, "Can not write a %s file without atom names",
385 ftp2ext(gmx_fio_getftp(status->fio)));
387 sprintf(title, "frame t= %.3f", fr->time);
388 if (gmx_fio_getftp(status->fio) == efGRO)
390 write_hconf_p(gmx_fio_getfp(status->fio), title, fr->atoms,
391 prec2ndec(prec), fr->x, fr->bV ? fr->v : NULL, fr->box);
395 write_pdbfile(gmx_fio_getfp(status->fio), title,
396 fr->atoms, fr->x, fr->bPBC ? fr->ePBC : -1, fr->box,
397 ' ', fr->step, gc, TRUE);
401 write_gms(gmx_fio_getfp(status->fio), fr->natoms, fr->x, fr->box);
404 write_g96_conf(gmx_fio_getfp(status->fio), fr, -1, NULL);
407 gmx_fatal(FARGS, "Sorry, write_trxframe can not write %s",
408 ftp2ext(gmx_fio_getftp(status->fio)));
415 int write_trx(t_trxstatus *status, int nind, const atom_id *ind, t_atoms *atoms,
416 int step, real time, matrix box, rvec x[], rvec *v,
421 clear_trxframe(&fr, TRUE);
426 fr.bAtoms = atoms != NULL;
433 copy_mat(box, fr.box);
435 return write_trxframe_indexed(status, &fr, nind, ind, gc);
438 void close_trx(t_trxstatus *status)
440 gmx_fio_close(status->fio);
444 t_trxstatus *open_trx(const char *outfile, const char *filemode)
447 if (filemode[0] != 'w' && filemode[0] != 'a' && filemode[1] != '+')
449 gmx_fatal(FARGS, "Sorry, write_trx can only write");
455 stat->fio = gmx_fio_open(outfile, filemode);
459 static gmx_bool gmx_next_frame(t_trxstatus *status, t_trxframe *fr)
466 if (fread_trnheader(status->fio, &sh, &bOK))
468 fr->bDouble = sh.bDouble;
469 fr->natoms = sh.natoms;
475 fr->bFepState = TRUE;
476 fr->lambda = sh.lambda;
477 fr->bBox = sh.box_size > 0;
478 if (fr->flags & (TRX_READ_X | TRX_NEED_X))
482 snew(fr->x, sh.natoms);
484 fr->bX = sh.x_size > 0;
486 if (fr->flags & (TRX_READ_V | TRX_NEED_V))
490 snew(fr->v, sh.natoms);
492 fr->bV = sh.v_size > 0;
494 if (fr->flags & (TRX_READ_F | TRX_NEED_F))
498 snew(fr->f, sh.natoms);
500 fr->bF = sh.f_size > 0;
502 if (fread_htrn(status->fio, &sh, fr->box, fr->x, fr->v, fr->f))
508 fr->not_ok = DATA_NOT_OK;
514 fr->not_ok = HEADER_NOT_OK;
520 static void choose_file_format(FILE *fp)
528 printf(" Select File Format\n");
529 printf("---------------------------\n");
530 printf("1. XYZ File\n");
531 printf("2. XYZ File with Box\n");
532 printf("3. Gromos-87 Ascii Trajectory\n");
533 printf("4. Gromos-87 Ascii Trajectory with Box\n");
540 printf("\nChoice: ");
544 rc = scanf("%d", &i);
549 while ((i < 0) || (i >= effNR));
552 stat->eFF = (eFileFormat) i;
554 for (m = 0; (m < DIM); m++)
559 stat->bReadBox = (stat->eFF == effG87Box) || (stat->eFF == effXYZBox);
565 if (5 != fscanf(fp, "%d%lf%lf%lf%lf", &stat->NATOMS, &stat->BOX[XX], &stat->BOX[YY], &stat->BOX[ZZ], &stat->DT))
567 gmx_fatal(FARGS, "Error reading natoms/box in file");
572 printf("GROMOS! OH DEAR...\n\n");
573 printf("Number of atoms ? ");
575 if (1 != scanf("%d", &stat->NATOMS))
577 gmx_fatal(FARGS, "Error reading natoms in file");
580 printf("Time between timeframes ? ");
582 if (1 != scanf("%lf", &stat->DT))
584 gmx_fatal(FARGS, "Error reading dt from file");
587 if (stat->eFF == effG87)
589 printf("Box X Y Z ? ");
591 if (3 != scanf("%lf%lf%lf", &stat->BOX[XX], &stat->BOX[YY], &stat->BOX[ZZ]))
593 gmx_fatal(FARGS, "Error reading box in file");
606 printf("Hellow World\n");
610 static gmx_bool do_read_xyz(t_trxstatus *status, FILE *fp, int natoms,
611 rvec x[], matrix box)
616 for (i = 0; (i < natoms); i++)
618 for (m = 0; (m < DIM); m++)
620 if (fscanf(fp, "%lf", &x0) != 1)
624 fprintf(stderr, "error reading statusfile: x[%d][%d]\n", i, m);
632 if (status->bReadBox)
634 for (m = 0; (m < DIM); m++)
636 if (fscanf(fp, "%lf", &x0) != 1)
646 static gmx_bool xyz_next_x(t_trxstatus *status, FILE *fp, const output_env_t oenv,
647 real *t, int natoms, rvec x[], matrix box)
648 /* Reads until a new x can be found (return TRUE)
649 * or eof (return FALSE)
655 while (!bTimeSet(TBEGIN) || (*t < rTimeValue(TBEGIN)))
657 if (!do_read_xyz(status, fp, natoms, x, box))
661 printcount(status, oenv, *t, FALSE);
665 if (!bTimeSet(TEND) || (*t <= rTimeValue(TEND)))
667 if (!do_read_xyz(status, fp, natoms, x, box))
669 printlast(status, oenv, *t);
672 printcount(status, oenv, *t, FALSE);
677 printlast(status, oenv, pt);
681 static int xyz_first_x(t_trxstatus *status, FILE *fp, const output_env_t oenv,
682 real *t, rvec **x, matrix box)
683 /* Reads fp, mallocs x, and returns x and box
684 * Returns natoms when successful, FALSE otherwise
692 choose_file_format(fp);
694 for (m = 0; (m < DIM); m++)
696 box[m][m] = status->BOX[m];
699 snew(*x, status->NATOMS);
701 if (!xyz_next_x(status, fp, oenv, t, status->NATOMS, *x, box))
707 return status->NATOMS;
710 static gmx_bool pdb_next_x(t_trxstatus *status, FILE *fp, t_trxframe *fr)
714 int ePBC, model_nr, na;
715 char title[STRLEN], *time;
718 atoms.nr = fr->natoms;
720 atoms.pdbinfo = NULL;
721 /* the other pointers in atoms should not be accessed if these are NULL */
723 na = read_pdbfile(fp, title, &model_nr, &atoms, fr->x, &ePBC, boxpdb, TRUE, NULL);
724 set_trxframe_ePBC(fr, ePBC);
725 if (nframes_read(status) == 0)
727 fprintf(stderr, " '%s', %d atoms\n", title, fr->natoms);
732 fr->bBox = (boxpdb[XX][XX] != 0.0);
735 copy_mat(boxpdb, fr->box);
738 if (model_nr != NOTSET)
743 time = strstr(title, " t= ");
747 sscanf(time+4, "%lf", &dbl);
748 fr->time = (real)dbl;
753 /* this is a bit dirty, but it will work: if no time is read from
754 comment line in pdb file, set time to current frame number */
757 fr->time = (real)fr->step;
761 fr->time = (real)nframes_read(status);
770 if (na != fr->natoms)
772 gmx_fatal(FARGS, "Number of atoms in pdb frame %d is %d instead of %d",
773 nframes_read(status), na, fr->natoms);
779 static int pdb_first_x(t_trxstatus *status, FILE *fp, t_trxframe *fr)
783 fprintf(stderr, "Reading frames from pdb file");
785 get_pdb_coordnum(fp, &fr->natoms);
788 gmx_fatal(FARGS, "\nNo coordinates in pdb file\n");
791 snew(fr->x, fr->natoms);
792 pdb_next_x(status, fp, fr);
797 gmx_bool read_next_frame(const output_env_t oenv, t_trxstatus *status, t_trxframe *fr)
801 gmx_bool bOK, bRet, bMissingData = FALSE, bSkip = FALSE;
809 clear_trxframe(fr, FALSE);
813 switch (gmx_fio_getftp(status->fio))
817 bRet = gmx_next_frame(status, fr);
820 /* Checkpoint files can not contain mulitple frames */
823 read_g96_conf(gmx_fio_getfp(status->fio), NULL, fr,
824 status->persistent_line);
825 bRet = (fr->natoms > 0);
828 bRet = xyz_next_x(status, gmx_fio_getfp(status->fio), oenv, &fr->time,
829 fr->natoms, fr->x, fr->box);
836 * Sometimes is off by one frame
837 * and sometimes reports frame not present/file not seekable
839 /* DvdS 2005-05-31: this has been fixed along with the increased
840 * accuracy of the control over -b and -e options.
842 if (bTimeSet(TBEGIN) && (fr->time < rTimeValue(TBEGIN)))
844 if (xtc_seek_time(status->fio, rTimeValue(TBEGIN), fr->natoms, TRUE))
846 gmx_fatal(FARGS, "Specified frame (time %f) doesn't exist or file corrupt/inconsistent.",
851 bRet = read_next_xtc(status->fio, fr->natoms, &fr->step, &fr->time, fr->box,
852 fr->x, &fr->prec, &bOK);
853 fr->bPrec = (bRet && fr->prec > 0);
860 /* Actually the header could also be not ok,
861 but from bOK from read_next_xtc this can't be distinguished */
862 fr->not_ok = DATA_NOT_OK;
866 bRet = pdb_next_x(status, gmx_fio_getfp(status->fio), fr);
869 bRet = gro_next_x_or_v(gmx_fio_getfp(status->fio), fr);
872 #ifdef GMX_USE_PLUGINS
873 bRet = read_next_vmd_frame(fr);
875 gmx_fatal(FARGS, "DEATH HORROR in read_next_frame ftp=%s,status=%s",
876 ftp2ext(gmx_fio_getftp(status->fio)),
877 gmx_fio_getname(status->fio));
883 bMissingData = (((fr->flags & TRX_NEED_X) && !fr->bX) ||
884 ((fr->flags & TRX_NEED_V) && !fr->bV) ||
885 ((fr->flags & TRX_NEED_F) && !fr->bF));
889 ct = check_times2(fr->time, fr->t0, fr->bDouble);
890 if (ct == 0 || ((fr->flags & TRX_DONT_SKIP) && ct < 0))
892 printcount(status, oenv, fr->time, FALSE);
900 printcount(status, oenv, fr->time, TRUE);
907 while (bRet && (bMissingData || bSkip));
911 printlast(status, oenv, pt);
914 printincomp(status, fr);
921 int read_first_frame(const output_env_t oenv, t_trxstatus **status,
922 const char *fn, t_trxframe *fr, int flags)
925 gmx_bool bFirst, bOK;
928 clear_trxframe(fr, TRUE);
935 status_init( *status );
936 (*status)->nxframe = 1;
939 fio = (*status)->fio = gmx_fio_open(fn, "r");
940 switch (gmx_fio_getftp(fio))
946 read_checkpoint_trxframe(fio, fr);
950 /* Can not rewind a compressed file, so open it twice */
951 if (!(*status)->persistent_line)
953 /* allocate the persistent line */
954 snew((*status)->persistent_line, STRLEN+1);
956 read_g96_conf(gmx_fio_getfp(fio), fn, fr, (*status)->persistent_line);
958 clear_trxframe(fr, FALSE);
959 if (flags & (TRX_READ_X | TRX_NEED_X))
961 snew(fr->x, fr->natoms);
963 if (flags & (TRX_READ_V | TRX_NEED_V))
965 snew(fr->v, fr->natoms);
967 fio = (*status)->fio = gmx_fio_open(fn, "r");
970 fr->natoms = xyz_first_x(*status, gmx_fio_getfp(fio), oenv, &fr->time,
977 printcount(*status, oenv, fr->time, FALSE);
982 if (read_first_xtc(fio, &fr->natoms, &fr->step, &fr->time, fr->box, &fr->x,
983 &fr->prec, &bOK) == 0)
987 gmx_fatal(FARGS, "No XTC!\n");
991 fr->not_ok = DATA_NOT_OK;
997 printincomp(*status, fr);
1001 fr->bPrec = (fr->prec > 0);
1006 printcount(*status, oenv, fr->time, FALSE);
1011 pdb_first_x(*status, gmx_fio_getfp(fio), fr);
1014 printcount(*status, oenv, fr->time, FALSE);
1019 if (gro_first_x_or_v(gmx_fio_getfp(fio), fr))
1021 printcount(*status, oenv, fr->time, FALSE);
1026 #ifdef GMX_USE_PLUGINS
1027 fprintf(stderr, "The file format of %s is not a known trajectory format to GROMACS.\n"
1028 "Please make sure that the file is a trajectory!\n"
1029 "GROMACS will now assume it to be a trajectory and will try to open it using the VMD plug-ins.\n"
1030 "This will only work in case the VMD plugins are found and it is a trajectory format supported by VMD.\n", fn);
1031 gmx_fio_fp_close(fio); /*only close the file without removing FIO entry*/
1032 if (!read_first_vmd_frame(fn, fr))
1034 gmx_fatal(FARGS, "Not supported in read_first_frame: %s", fn);
1037 gmx_fatal(FARGS, "Not supported in read_first_frame: %s. Please make sure that the file is a trajectory.\n"
1038 "GROMACS is not compiled with plug-in support. Thus it cannot read non-GROMACS trajectory formats using the VMD plug-ins.\n"
1039 "Please compile with plug-in support if you want to read non-GROMACS trajectory formats.\n", fn);
1044 /* Return FALSE if we read a frame that's past the set ending time. */
1045 if (!bFirst && (!(fr->flags & TRX_DONT_SKIP) && check_times(fr->time) > 0))
1052 (!(fr->flags & TRX_DONT_SKIP) && check_times(fr->time) < 0))
1054 /* Read a frame when no frame was read or the first was skipped */
1055 if (!read_next_frame(oenv, *status, fr))
1062 return (fr->natoms > 0);
1065 /***** C O O R D I N A T E S T U F F *****/
1067 int read_first_x(const output_env_t oenv, t_trxstatus **status, const char *fn,
1068 real *t, rvec **x, matrix box)
1072 read_first_frame(oenv, status, fn, &fr, TRX_NEED_X);
1074 snew((*status)->xframe, 1);
1075 (*status)->nxframe = 1;
1076 (*(*status)->xframe) = fr;
1077 *t = (*status)->xframe->time;
1078 *x = (*status)->xframe->x;
1079 copy_mat((*status)->xframe->box, box);
1081 return (*status)->xframe->natoms;
1084 gmx_bool read_next_x(const output_env_t oenv, t_trxstatus *status, real *t,
1085 rvec x[], matrix box)
1089 status->xframe->x = x;
1090 /*xframe[status].x = x;*/
1091 bRet = read_next_frame(oenv, status, status->xframe);
1092 *t = status->xframe->time;
1093 copy_mat(status->xframe->box, box);
1098 void close_trj(t_trxstatus *status)
1100 gmx_fio_close(status->fio);
1101 /* The memory in status->xframe is lost here,
1102 * but the read_first_x/read_next_x functions are deprecated anyhow.
1103 * read_first_frame/read_next_frame and close_trx should be used.
1108 void rewind_trj(t_trxstatus *status)
1112 gmx_fio_rewind(status->fio);