Create fileio module
[alexxy/gromacs.git] / src / gromacs / fileio / enxio.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_ENXIO_H
39 #define GMX_FILEIO_ENXIO_H
40
41 #include "../legacyheaders/sysstuff.h"
42 #include "../legacyheaders/typedefs.h"
43 #include "../legacyheaders/pbc.h"
44 #include "gmxfio.h"
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49
50 /**************************************************************
51  * These are the base datatypes + functions for reading and
52  * writing energy files (.edr). They are either called directly
53  * (as in the processing tools), or indirectly through mdebin.c
54  * during mdrun.
55  *
56  * The routines in the corresponding c-file enxio.c
57  * are based on the lower level routines in gmxfio.c.
58  * The file pointer returned from open_enx
59  * can also be used with the routines in gmxfio.h
60  *
61  **************************************************************/
62
63 typedef struct {
64     char *name;
65     char *unit;
66 } gmx_enxnm_t;
67
68 /*
69  * Index for the IDs of additional blocks in the energy file.
70  * Blocks can be added without sacrificing backward and forward
71  * compatibility of the energy files.
72  *
73  * For backward compatibility, the order of these should not be changed.
74  */
75 enum {
76     enxOR,     /* Time and ensemble averaged data for orientation restraints */
77     enxORI,    /* Instantaneous data for orientation restraints              */
78     enxORT,    /* Order tensor(s) for orientation restraints                 */
79     enxDISRE,  /* Distance restraint blocks                                  */
80
81     enxDHCOLL, /* Data about the free energy blocks in this frame.           */
82     enxDHHIST, /* BAR histogram                                              */
83     enxDH,     /* BAR raw delta H data                                       */
84
85     enxNR      /* Total number of extra blocks in the current code,
86                 * note that the enxio code can read files written by
87                 * future code which contain more blocks.
88                 */
89 };
90
91 /* names for the above enum */
92 extern const char *enx_block_id_name[];
93
94
95 /* the subblocks that are contained in energy file blocks. Each of these
96    has a number of values of a single data type in a .edr file. */
97 typedef struct
98 {
99     int          nr;        /* number of items in subblock */
100     xdr_datatype type;      /* the block type */
101
102     /* the values: pointers for each type */
103     float*             fval;
104     double*            dval;
105     int*               ival;
106     gmx_large_int_t*   lval;
107     unsigned char*     cval;
108     char**             sval;
109
110     /* the allocated sizes, defined separately.
111        (nonzero sizes can be free()d later): */
112     int fval_alloc;
113     int dval_alloc;
114     int ival_alloc;
115     int lval_alloc;
116     int cval_alloc;
117     int sval_alloc;
118 } t_enxsubblock;
119
120
121 /* the energy file blocks. Each block contains a number of sub-blocks
122    of a single type that contain the actual data. */
123 typedef struct t_enxblock{
124     int            id;         /* block id, from the enx enums above */
125     int            nsub;       /* number of subblocks */
126     t_enxsubblock *sub;        /* the subblocks */
127     int            nsub_alloc; /* number of allocated subblocks */
128 } t_enxblock;
129
130
131 /* The frames that are read/written */
132 typedef struct {
133     double          t;            /* Timestamp of this frame                         */
134     gmx_large_int_t step;         /* MD step                                 */
135     gmx_large_int_t nsteps;       /* The number of steps between frames            */
136     double          dt;           /* The MD time step                              */
137     int             nsum;         /* The number of terms for the sums in ener      */
138     int             nre;          /* Number of energies                      */
139     int             e_size;       /* Size (in bytes) of energies                     */
140     int             e_alloc;      /* Allocated size (in elements) of ener          */
141     t_energy       *ener;         /* The energies                                  */
142     int             nblock;       /* Number of following energy blocks             */
143     t_enxblock     *block;        /* The blocks                                    */
144     int             nblock_alloc; /* The number of blocks allocated                */
145 } t_enxframe;
146
147 /* file handle */
148 typedef struct ener_file *ener_file_t;
149
150 /*
151  * An energy file is read like this:
152  *
153  * ener_file_t fp;
154  * t_enxframe *fr;
155  *
156  * fp = open_enx(...);
157  * do_enxnms(fp,...);
158  * snew(fr,1);
159  * while (do_enx(fp,fr)) {
160  * ...
161  * }
162  * free_enxframe(fr);
163  * sfree(fr);
164  */
165 /* New energy reading and writing interface */
166
167
168 /* initialize a pre-allocated frame */
169 void init_enxframe(t_enxframe *ef);
170 /* delete a frame's memory (except the ef itself) */
171 void free_enxframe(t_enxframe *ef);
172
173
174 ener_file_t open_enx(const char *fn, const char *mode);
175
176 t_fileio *enx_file_pointer(const ener_file_t ef);
177
178 void close_enx(ener_file_t ef);
179
180 void do_enxnms(ener_file_t ef, int *nre, gmx_enxnm_t **enms);
181
182 void free_enxnms(int n, gmx_enxnm_t *nms);
183 /* Frees nms and all strings in it */
184
185 gmx_bool do_enx(ener_file_t ef, t_enxframe *fr);
186 /* Reads enx_frames, memory in fr is (re)allocated if necessary */
187
188 void get_enx_state(const char *fn, real t,
189                    gmx_groups_t *groups, t_inputrec *ir,
190                    t_state *state);
191 /*
192  * Reads state variables from enx file fn at time t.
193  * atoms and ir are required for determining which things must be read.
194  * Currently pcoupl and tcoupl state are read from enx.
195  */
196
197
198 /* block funtions */
199
200 /* allocate n blocks to a frame (if neccesary). Don't touch existing blocks */
201 void add_blocks_enxframe(t_enxframe *ef, int n);
202
203 /* find a block by id number; if prev!=NULL, it searches from
204    that block's next block.
205    Returns NULL if no block is found with the given id. */
206 t_enxblock *find_block_id_enxframe(t_enxframe *ef, int id, t_enxblock *prev);
207
208
209 /* allocate n subblocks to a block (if neccesary). Don't touch existing
210    subbblocks. */
211 void add_subblocks_enxblock(t_enxblock *eb, int n);
212
213
214
215 #ifdef __cplusplus
216 }
217 #endif
218
219 #endif  /* GMX_FILEIO_ENERIO_H */