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