4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
11 * Copyright (c) 1991-2001, University of Groningen, The Netherlands
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * If you want to redistribute modifications, please consider that
18 * scientific software is very special. Version control is crucial -
19 * bugs must be traceable. We will be happy to consider code for
20 * inclusion in the official distribution, but derived work must not
21 * be called official GROMACS. Details are found in the README & COPYING
22 * files - if they are missing, get the official version at www.gromacs.org.
24 * To help us fund GROMACS development, we humbly ask that you cite
25 * the papers on the package - you can find them in the top README file.
27 * For more info, check our website at http://www.gromacs.org
30 * Gnomes, ROck Monsters And Chili Sauce
43 #define XTC_MAGIC 1995
45 int open_xtc(char *fn
,char *mode
)
47 return fio_open(fn
,mode
);
50 void close_xtc(int fp
)
55 static void check_xtc_magic(int magic
)
57 if (magic
!= XTC_MAGIC
)
58 fatal_error(0,"Magic Number Error in XTC file (read %d, should be %d)",
62 int xtc_check(char *str
,bool bResult
,char *file
,int line
)
66 fprintf(debug
,"\nXTC error: read/write of %s failed, "
67 "source file %s, line %d\n",str
,file
,line
);
73 void xtc_check_fat_err(char *str
,bool bResult
,char *file
,int line
)
76 fatal_error(0,"XTC read/write of %s failed, "
77 "source file %s, line %d\n",str
,file
,line
);
81 static int xtc_header(XDR
*xd
,int *magic
,int *natoms
,int *step
,real
*time
,
86 if (xdr_int(xd
,magic
) == 0)
88 result
=XTC_CHECK("natoms", xdr_int(xd
,natoms
)); /* number of atoms */
90 result
=XTC_CHECK("step", xdr_int(xd
,step
)); /* frame number */
92 result
=XTC_CHECK("time", xdr_real(xd
,time
)); /* time */
98 static int xtc_coord(XDR
*xd
,int *natoms
,matrix box
,rvec
*x
,real
*prec
)
104 for(i
=0; ((i
<DIM
) && result
); i
++)
105 for(j
=0; ((j
<DIM
) && result
); j
++)
106 result
=XTC_CHECK("box",xdr_real(xd
,&(box
[i
][j
])));
110 result
=XTC_CHECK("x",xdr3drcoord(xd
,x
[0],natoms
,prec
));
115 static int xtc_io(XDR
*xd
,int *magic
,
116 int *natoms
,int *step
,real
*time
,
117 matrix box
,rvec
*x
,real
*prec
,bool *bOK
)
119 if (!xtc_header(xd
,magic
,natoms
,step
,time
,bOK
))
121 return xtc_coord(xd
,natoms
,box
,x
,prec
);
124 int write_xtc(int fp
,
125 int natoms
,int step
,real time
,
126 matrix box
,rvec
*x
,real prec
)
128 int magic_number
= XTC_MAGIC
;
133 /* write magic number and xtc identidier */
134 if (!xtc_header(xd
,&magic_number
,&natoms
,&step
,&time
,&bDum
))
138 return xtc_coord(xd
,&natoms
,box
,x
,&prec
);
141 int read_first_xtc(int fp
,int *natoms
,int *step
,real
*time
,
142 matrix box
,rvec
**x
,real
*prec
,bool *bOK
)
150 /* read header and malloc x */
151 if ( !xtc_header(xd
,&magic
,natoms
,step
,time
,bOK
))
154 /* Check magic number */
155 check_xtc_magic(magic
);
159 *bOK
=xtc_coord(xd
,natoms
,box
,*x
,prec
);
164 int read_next_xtc(int fp
,
165 int natoms
,int *step
,real
*time
,
166 matrix box
,rvec
*x
,real
*prec
,bool *bOK
)
176 if (!xtc_header(xd
,&magic
,&n
,step
,time
,bOK
))
179 fatal_error(0, "Frame contains more atoms (%d) than expected (%d)",
182 /* Check magic number */
183 check_xtc_magic(magic
);
185 *bOK
=xtc_coord(xd
,&natoms
,box
,x
,prec
);