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 for
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
47 #include "gmx_fatal.h"
49 #define XTC_MAGIC 1995
52 static int xdr_r2f(XDR *xdrs, real *r, gmx_bool bRead)
62 ret = xdr_float(xdrs, &f);
70 return xdr_float(xdrs, (float *)r);
75 t_fileio *open_xtc(const char *fn, const char *mode)
77 return gmx_fio_open(fn, mode);
80 void close_xtc(t_fileio *fio)
85 static void check_xtc_magic(int magic)
87 if (magic != XTC_MAGIC)
89 gmx_fatal(FARGS, "Magic Number Error in XTC file (read %d, should be %d)",
94 int xtc_check(const char *str, gmx_bool bResult, const char *file, int line)
100 fprintf(debug, "\nXTC error: read/write of %s failed, "
101 "source file %s, line %d\n", str, file, line);
108 void xtc_check_fat_err(const char *str, gmx_bool bResult, const char *file, int line)
112 gmx_fatal(FARGS, "XTC read/write of %s failed, "
113 "source file %s, line %d\n", str, file, line);
117 static int xtc_header(XDR *xd, int *magic, int *natoms, int *step, real *time,
118 gmx_bool bRead, gmx_bool *bOK)
122 if (xdr_int(xd, magic) == 0)
126 result = XTC_CHECK("natoms", xdr_int(xd, natoms)); /* number of atoms */
129 result = XTC_CHECK("step", xdr_int(xd, step)); /* frame number */
133 result = XTC_CHECK("time", xdr_r2f(xd, time, bRead)); /* time */
135 *bOK = (result != 0);
140 static int xtc_coord(XDR *xd, int *natoms, matrix box, rvec *x, real *prec, gmx_bool bRead)
150 for (i = 0; ((i < DIM) && result); i++)
152 for (j = 0; ((j < DIM) && result); j++)
154 result = XTC_CHECK("box", xdr_r2f(xd, &(box[i][j]), bRead));
164 /* allocate temp. single-precision array */
165 snew(ftmp, (*natoms)*DIM);
167 /* Copy data to temp. array if writing */
170 for (i = 0; (i < *natoms); i++)
172 ftmp[DIM*i+XX] = x[i][XX];
173 ftmp[DIM*i+YY] = x[i][YY];
174 ftmp[DIM*i+ZZ] = x[i][ZZ];
178 result = XTC_CHECK("x", xdr3dfcoord(xd, ftmp, natoms, &fprec));
180 /* Copy from temp. array if reading */
183 for (i = 0; (i < *natoms); i++)
185 x[i][XX] = ftmp[DIM*i+XX];
186 x[i][YY] = ftmp[DIM*i+YY];
187 x[i][ZZ] = ftmp[DIM*i+ZZ];
193 result = XTC_CHECK("x", xdr3dfcoord(xd, x[0], natoms, prec));
201 int write_xtc(t_fileio *fio,
202 int natoms, int step, real time,
203 matrix box, rvec *x, real prec)
205 int magic_number = XTC_MAGIC;
210 xd = gmx_fio_getxdr(fio);
211 /* write magic number and xtc identidier */
212 if (xtc_header(xd, &magic_number, &natoms, &step, &time, FALSE, &bDum) == 0)
218 bOK = xtc_coord(xd, &natoms, box, x, &prec, FALSE); /* bOK will be 1 if writing went well */
222 if (gmx_fio_flush(fio) != 0)
227 return bOK; /* 0 if bad, 1 if writing went well */
230 int read_first_xtc(t_fileio *fio, int *natoms, int *step, real *time,
231 matrix box, rvec **x, real *prec, gmx_bool *bOK)
237 xd = gmx_fio_getxdr(fio);
239 /* read header and malloc x */
240 if (!xtc_header(xd, &magic, natoms, step, time, TRUE, bOK))
245 /* Check magic number */
246 check_xtc_magic(magic);
250 *bOK = xtc_coord(xd, natoms, box, *x, prec, TRUE);
255 int read_next_xtc(t_fileio* fio,
256 int natoms, int *step, real *time,
257 matrix box, rvec *x, real *prec, gmx_bool *bOK)
264 xd = gmx_fio_getxdr(fio);
267 if (!xtc_header(xd, &magic, &n, step, time, TRUE, bOK))
272 /* Check magic number */
273 check_xtc_magic(magic);
277 gmx_fatal(FARGS, "Frame contains more atoms (%d) than expected (%d)",
281 *bOK = xtc_coord(xd, &natoms, box, x, prec, TRUE);