Create fileio module
[alexxy/gromacs.git] / src / gromacs / fileio / trxio.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team,
6  * Copyright (c) 2013, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37
38 #ifndef GMX_FILEIO_TRXIO_H
39 #define GMX_FILEIO_TRXIO_H
40
41 #include "../legacyheaders/typedefs.h"
42 #include "filenm.h"
43 #include "../legacyheaders/readinp.h"
44 #include "pdbio.h"
45 #include "../legacyheaders/oenv.h"
46 #include "gmxfio.h"
47
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 #if 0 /* avoid screwing up indentation */
52 }
53 #endif
54
55 /* a dedicated status type contains fp, etc. */
56 typedef struct t_trxstatus t_trxstatus;
57
58 /* I/O function types */
59
60 /************************************************
61  *             Trajectory functions
62  ************************************************/
63
64 int prec2ndec(real prec);
65 /* Convert precision in 1/(nm) to number of decimal places */
66
67 void clear_trxframe(t_trxframe *fr, gmx_bool bFirst);
68 /* Set all content gmx_booleans to FALSE.
69  * When bFirst = TRUE, set natoms=-1, all pointers to NULL
70  *                     and all data to zero.
71  */
72
73 void set_trxframe_ePBC(t_trxframe *fr, int ePBC);
74 /* Set the type of periodic boundary conditions, ePBC=-1 is not set */
75
76 int nframes_read(t_trxstatus *status);
77 /* Returns the number of frames read from the trajectory */
78
79 int write_trxframe_indexed(t_trxstatus *status, t_trxframe *fr, int nind,
80                            const atom_id *ind, gmx_conect gc);
81 /* Write an indexed frame to a TRX file, see write_trxframe. gc may be NULL */
82
83 int write_trxframe(t_trxstatus *status, t_trxframe *fr, gmx_conect gc);
84 /* Write a frame to a TRX file.
85  * Only entries for which the gmx_boolean is TRUE will be written,
86  * except for step, time, lambda and/or box, which may not be
87  * omitted for certain trajectory formats.
88  * The precision for .xtc and .gro is fr->prec, when fr->bPrec=FALSE,
89  * the precision is set to 1000.
90  * gc is important for pdb file writing only and may be NULL.
91  */
92
93 int write_trx(t_trxstatus *status, int nind, const atom_id *ind, t_atoms *atoms,
94               int step, real time, matrix box, rvec x[], rvec *v,
95               gmx_conect gc);
96 /* Write an indexed frame to a TRX file.
97  * v can be NULL.
98  * atoms can be NULL for file types which don't need atom names.
99  */
100
101 void close_trx(t_trxstatus *status);
102 /* Close trj file as opened with read_first_x, read_frist_frame
103  * or open_trx. Identical to close_trj.
104  */
105
106 t_trxstatus *open_trx(const char *outfile, const char *filemode);
107 /* Open a TRX file and return an allocated status pointer */
108
109 /* get a fileio from a trxstatus */
110 t_fileio *trx_get_fileio(t_trxstatus *status);
111
112
113 gmx_bool bRmod_fd(double a, double b, double c, gmx_bool bDouble);
114 /* Returns TRUE when (a - b) MOD c = 0, using a margin which is slightly
115  * larger than the float/double precision.
116  */
117
118 #ifdef GMX_DOUBLE
119 #define bRmod(a, b, c) bRmod_fd(a, b, c, TRUE)
120 #else
121 #define bRmod(a, b, c) bRmod_fd(a, b, c, FALSE)
122 #endif
123
124 int check_times2(real t, real t0, gmx_bool bDouble);
125 /* This routine checkes if the read-in time is correct or not;
126  * returns -1 if t<tbegin or t MOD dt = t0,
127  *          0 if tbegin <= t <=tend+margin,
128  *          1 if t>tend
129  * where margin is 0.1*min(t-tp,tp-tpp), if this positive, 0 otherwise.
130  * tp and tpp should be the time of the previous frame and the one before.
131  * The mod is done with single or double precision accuracy depending
132  * on the value of bDouble.
133  */
134
135 int check_times(real t);
136 /* This routine checkes if the read-in time is correct or not;
137  * returns -1 if t<tbegin,
138  *          0 if tbegin <= t <=tend,
139  *          1 if t>tend
140  */
141
142
143
144
145
146 /* For trxframe.flags, used in trxframe read routines.
147  * When a READ flag is set, the field will be read when present,
148  * but a frame might be returned which does not contain the field.
149  * When a NEED flag is set, frames not containing the field will be skipped.
150  */
151 #define TRX_READ_X    (1<<0)
152 #define TRX_NEED_X    (1<<1)
153 #define TRX_READ_V    (1<<2)
154 #define TRX_NEED_V    (1<<3)
155 #define TRX_READ_F    (1<<4)
156 #define TRX_NEED_F    (1<<5)
157 /* Useful for reading natoms from a trajectory without skipping */
158 #define TRX_DONT_SKIP (1<<6)
159
160 /* For trxframe.not_ok */
161 #define HEADER_NOT_OK (1<<0)
162 #define DATA_NOT_OK   (1<<1)
163 #define FRAME_NOT_OK  (HEADER_NOT_OK | DATA_NOT_OK)
164
165 int read_first_frame(const output_env_t oenv, t_trxstatus **status,
166                      const char *fn, t_trxframe *fr, int flags);
167 /* Read the first frame which is in accordance with flags, which are
168  * defined further up in this file.
169  * Returns natoms when succeeded, 0 otherwise.
170  * Memory will be allocated for flagged entries.
171  * The flags are copied to fr for subsequent calls to read_next_frame.
172  * Returns TRUE when succeeded, FALSE otherwise.
173  */
174
175 gmx_bool read_next_frame(const output_env_t oenv, t_trxstatus *status,
176                          t_trxframe *fr);
177 /* Reads the next frame which is in accordance with fr->flags.
178  * Returns TRUE when succeeded, FALSE otherwise.
179  */
180
181 int read_first_x(const output_env_t oenv, t_trxstatus **status,
182                  const char *fn, real *t, rvec **x, matrix box);
183 /* These routines read first coordinates and box, and allocates
184  * memory for the coordinates, for a trajectory file.
185  * The routine returns the number of atoms, or 0 when something is wrong.
186  * The integer in status should be passed to calls of read_next_x
187  */
188
189 gmx_bool read_next_x(const output_env_t oenv, t_trxstatus *status, real *t, rvec x[], matrix box);
190 /* Read coordinates and box from a trajectory file. Return TRUE when all well,
191  * or FALSE when end of file (or last frame requested by user).
192  * status is the integer set in read_first_x.
193  */
194
195 void close_trj(t_trxstatus *status);
196 /* Close trj file as opened with read_first_x, read_frist_frame
197  * or open_trx. Identical to close_trx.
198  */
199
200 void rewind_trj(t_trxstatus *status);
201 /* Rewind trj file as opened with read_first_x */
202
203 t_topology *read_top(const char *fn, int *ePBC);
204 /* Extract a topology data structure from a topology file.
205  * If ePBC!=NULL *ePBC gives the pbc type.
206  */
207
208 #ifdef __cplusplus
209 }
210 #endif
211
212 #endif