1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*-
5 * Copyright (c) Erik Lindahl, David van der Spoel 2003,2004.
6 * Coordinate compression (c) by Frans van Hoesel.
8 * IN contrast to the rest of Gromacs, XDRFILE is distributed under the
9 * BSD license, so you can use it any way you wish, including closed source:
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
32 #include "xdrfile_xtc.h"
38 static int xtc_header(XDRFILE
*xd
,int *natoms
,int *step
,float *time
,bool bRead
)
42 /* Note: read is same as write. He he he */
44 if ((result
= xdrfile_write_int(&magic
,n
,xd
)) != n
)
53 if ((result
= xdrfile_write_int(natoms
,n
,xd
)) != n
)
55 if ((result
= xdrfile_write_int(step
,n
,xd
)) != n
)
57 if ((result
= xdrfile_write_float(time
,n
,xd
)) != n
)
63 static int xtc_coord(XDRFILE
*xd
,int *natoms
,matrix box
,rvec
*x
,float *prec
,
69 result
= xdrfile_read_float(box
[0],DIM
*DIM
,xd
);
70 if (DIM
*DIM
!= result
)
76 result
= xdrfile_decompress_coord_float(x
[0],natoms
,prec
,xd
);
77 if (result
!= *natoms
)
82 result
= xdrfile_compress_coord_float(x
[0],*natoms
,*prec
,xd
);
83 if (result
!= *natoms
)
90 int read_xtc_natoms(char *fn
,int *natoms
)
96 xd
= xdrfile_open(fn
,"r");
98 return exdrFILENOTFOUND
;
99 result
= xtc_header(xd
,natoms
,&step
,&time
,TRUE
);
105 int read_xtc(XDRFILE
*xd
,
106 int natoms
,int *step
,float *time
,
107 matrix box
,rvec
*x
,float *prec
)
108 /* Read subsequent frames */
112 if ((result
= xtc_header(xd
,&natoms
,step
,time
,TRUE
)) != exdrOK
)
115 if ((result
= xtc_coord(xd
,&natoms
,box
,x
,prec
,1)) != exdrOK
)
121 int write_xtc(XDRFILE
*xd
,
122 int natoms
,int step
,float time
,
123 matrix box
,rvec
*x
,float prec
)
124 /* Write a frame to xtc file */
128 if ((result
= xtc_header(xd
,&natoms
,&step
,&time
,FALSE
)) != exdrOK
)
131 if ((result
= xtc_coord(xd
,&natoms
,box
,x
,&prec
,0)) != exdrOK
)