4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
12 * Copyright (c) 1991-1999
13 * BIOSON Research Institute, Dept. of Biophysical Chemistry
14 * University of Groningen, The Netherlands
17 * GROMACS: A message-passing parallel molecular dynamics implementation
18 * H.J.C. Berendsen, D. van der Spoel and R. van Drunen
19 * Comp. Phys. Comm. 91, 43-56 (1995)
21 * Also check out our WWW page:
22 * http://md.chem.rug.nl/~gmx
27 * Green Red Orange Magenta Azure Cyan Skyblue
29 static char *SRCID_xtcio_c
= "$Id$";
41 #define XTC_MAGIC 1995
43 int open_xtc(char *fn
,char *mode
)
45 return fio_open(fn
,mode
);
48 void close_xtc(int fp
)
53 static void check_xtc_magic(int magic
)
55 if (magic
!= XTC_MAGIC
)
56 fatal_error(0,"Magic Number Error in XTC file (read %d, should be %d)",
60 int xtc_check(char *str
,bool bResult
,char *file
,int line
)
64 fprintf(debug
,"\nXTC error: read/write of %s failed, "
65 "source file %s, line %d\n",str
,file
,line
);
71 void xtc_check_fat_err(char *str
,bool bResult
,char *file
,int line
)
74 fatal_error(0,"XTC read/write of %s failed, "
75 "source file %s, line %d\n",str
,file
,line
);
79 static int xtc_header(XDR
*xd
,int *magic
,int *natoms
,int *step
,real
*time
,
84 if (xdr_int(xd
,magic
) == 0)
86 result
=XTC_CHECK("natoms", xdr_int(xd
,natoms
)); /* number of atoms */
88 result
=XTC_CHECK("step", xdr_int(xd
,step
)); /* frame number */
90 result
=XTC_CHECK("time", xdr_real(xd
,time
)); /* time */
96 static int xtc_coord(XDR
*xd
,int *natoms
,matrix box
,rvec
*x
,real
*prec
)
102 for(i
=0; ((i
<DIM
) && result
); i
++)
103 for(j
=0; ((j
<DIM
) && result
); j
++)
104 result
=XTC_CHECK("box",xdr_real(xd
,&(box
[i
][j
])));
108 result
=XTC_CHECK("x",xdr3drcoord(xd
,x
[0],natoms
,prec
));
113 static int xtc_io(XDR
*xd
,int *magic
,
114 int *natoms
,int *step
,real
*time
,
115 matrix box
,rvec
*x
,real
*prec
,bool *bOK
)
117 if (!xtc_header(xd
,magic
,natoms
,step
,time
,bOK
))
119 return xtc_coord(xd
,natoms
,box
,x
,prec
);
122 int write_xtc(int fp
,
123 int natoms
,int step
,real time
,
124 matrix box
,rvec
*x
,real prec
)
126 int magic_number
= XTC_MAGIC
;
131 /* write magic number and xtc identidier */
132 if (!xtc_header(xd
,&magic_number
,&natoms
,&step
,&time
,&bDum
))
136 return xtc_coord(xd
,&natoms
,box
,x
,&prec
);
139 int read_first_xtc(int fp
,int *natoms
,int *step
,real
*time
,
140 matrix box
,rvec
**x
,real
*prec
,bool *bOK
)
148 /* read header and malloc x */
149 if ( !xtc_header(xd
,&magic
,natoms
,step
,time
,bOK
))
152 /* Check magic number */
153 check_xtc_magic(magic
);
157 *bOK
=xtc_coord(xd
,natoms
,box
,*x
,prec
);
162 int read_next_xtc(int fp
,
163 int *natoms
,int *step
,real
*time
,
164 matrix box
,rvec
*x
,real
*prec
,bool *bOK
)
173 if (!xtc_header(xd
,&magic
,natoms
,step
,time
,bOK
))
176 /* Check magic number */
177 check_xtc_magic(magic
);
179 *bOK
=xtc_coord(xd
,natoms
,box
,x
,prec
);