This is the release 1.0 version
[gromacs/libxdrfile.git] / src / xdrfile_xtc.c
blobd775979a855f9f6011d1e9395054daed6a8bccce
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*-
3 * $Id$
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.
30 #include <stdlib.h>
31 #include "xdrfile.h"
32 #include "xdrfile_xtc.h"
34 #define MAGIC 1995
36 enum { FALSE, TRUE };
38 static int xtc_header(XDRFILE *xd,int *natoms,int *step,float *time,bool bRead)
40 int result,magic,n=1;
42 /* Note: read is same as write. He he he */
43 magic = MAGIC;
44 if ((result = xdrfile_write_int(&magic,n,xd)) != n)
46 if (bRead)
47 return exdrENDOFFILE;
48 else
49 return exdrINT;
51 if (magic != MAGIC)
52 return exdrMAGIC;
53 if ((result = xdrfile_write_int(natoms,n,xd)) != n)
54 return exdrINT;
55 if ((result = xdrfile_write_int(step,n,xd)) != n)
56 return exdrINT;
57 if ((result = xdrfile_write_float(time,n,xd)) != n)
58 return exdrFLOAT;
60 return exdrOK;
63 static int xtc_coord(XDRFILE *xd,int *natoms,matrix box,rvec *x,float *prec,
64 bool bRead)
66 int i,j,result;
68 /* box */
69 result = xdrfile_read_float(box[0],DIM*DIM,xd);
70 if (DIM*DIM != result)
71 return exdrFLOAT;
72 else
74 if (bRead)
76 result = xdrfile_decompress_coord_float(x[0],natoms,prec,xd);
77 if (result != *natoms)
78 return exdr3DX;
80 else
82 result = xdrfile_compress_coord_float(x[0],*natoms,*prec,xd);
83 if (result != *natoms)
84 return exdr3DX;
87 return exdrOK;
90 int read_xtc_natoms(char *fn,int *natoms)
92 XDRFILE *xd;
93 int step,result;
94 float time;
96 xd = xdrfile_open(fn,"r");
97 if (NULL == xd)
98 return exdrFILENOTFOUND;
99 result = xtc_header(xd,natoms,&step,&time,TRUE);
100 xdrfile_close(xd);
102 return result;
105 int read_xtc(XDRFILE *xd,
106 int natoms,int *step,float *time,
107 matrix box,rvec *x,float *prec)
108 /* Read subsequent frames */
110 int result;
112 if ((result = xtc_header(xd,&natoms,step,time,TRUE)) != exdrOK)
113 return result;
115 if ((result = xtc_coord(xd,&natoms,box,x,prec,1)) != exdrOK)
116 return result;
118 return 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 */
126 int result;
128 if ((result = xtc_header(xd,&natoms,&step,&time,FALSE)) != exdrOK)
129 return result;
131 if ((result = xtc_coord(xd,&natoms,box,x,&prec,0)) != exdrOK)
132 return result;
134 return exdrOK;