a2c3bdd29b611449c1634f93993047ccf75d94e8
[alexxy/gromacs.git] / include / statutil.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  * check out http://www.gromacs.org for more information.
7  * Copyright (c) 2012, by the GROMACS development team, led by
8  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9  * others, as listed in the AUTHORS file in the top-level source
10  * directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38
39 #ifndef _statutil_h
40 #define _statutil_h
41
42 #include <stdio.h>
43 #include "visibility.h"
44 #include "typedefs.h"
45 #include "filenm.h"
46 #include "readinp.h"
47 #include "wman.h"
48 #include "pdbio.h"
49 #include "oenv.h"
50 #include "gmxfio.h"
51
52
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56 #if 0 /* avoid screwing up indentation */
57 }
58 #endif
59
60
61 /* The code below is to facilitate controlled begin and end of
62  trajectory reading. Corresponding routines in
63  src/gmxlib/tcontrol.c
64  */
65 enum { TBEGIN, TEND, TDELTA, TNR };
66
67 gmx_bool bTimeSet(int tcontrol);
68
69 real rTimeValue(int tcontrol); 
70
71 void setTimeValue(int tcontrol,real value);
72
73 /* End trajectory time control */
74
75 /* a dedicated status type contains fp, etc. */
76 typedef struct t_trxstatus t_trxstatus;
77   
78 typedef int t_first_x(t_trxstatus **status,const char *fn,real *t,rvec **x,
79                       matrix box);
80
81 typedef gmx_bool t_next_x(t_trxstatus *status,real *t,int natoms,rvec x[],
82                       matrix box);
83
84 /* I/O function types */
85
86   
87 /* LEGACY FUNCTIONS 
88
89    The program names, command lines, etc. are now also set in the output_env
90    structure. That is now the preferred location, but the functions here
91    are still available as legacy functions. Because they all act on inherently
92    global informaion, their existence in a multi-threaded environment is not
93    a real problem. */
94     
95 /* Return the name of the program */
96 GMX_LIBGMX_EXPORT
97 const char *command_line(void);
98 void set_command_line(int argc, char *argv[]);
99
100 /* set the program name to the provided string, but note
101  * that it must be a real file - we determine the library
102  * directory from its location!
103  */    
104 GMX_LIBGMX_EXPORT
105 const char *Program(void);
106 GMX_LIBGMX_EXPORT
107 void set_program_name(const char *argvzero);
108 /* Id. without leading directory */
109 GMX_LIBGMX_EXPORT
110 const char *ShortProgram(void);
111
112 /************************************************
113  *             Trajectory functions
114  ************************************************/
115
116 GMX_LIBGMX_EXPORT
117 int prec2ndec(real prec);
118 /* Convert precision in 1/(nm) to number of decimal places */
119
120 GMX_LIBGMX_EXPORT
121 void clear_trxframe(t_trxframe *fr,gmx_bool bFirst);
122 /* Set all content gmx_booleans to FALSE.
123  * When bFirst = TRUE, set natoms=-1, all pointers to NULL
124  *                     and all data to zero.
125  */
126
127 GMX_LIBGMX_EXPORT
128 void set_trxframe_ePBC(t_trxframe *fr,int ePBC);
129 /* Set the type of periodic boundary conditions, ePBC=-1 is not set */
130
131 GMX_LIBGMX_EXPORT
132 int nframes_read(t_trxstatus *status);
133 /* Returns the number of frames read from the trajectory */
134
135 GMX_LIBGMX_EXPORT
136 int write_trxframe_indexed(t_trxstatus *status,t_trxframe *fr,int nind,
137                            atom_id *ind, gmx_conect gc);
138 /* Write an indexed frame to a TRX file, see write_trxframe. gc may be NULL */
139
140 GMX_LIBGMX_EXPORT
141 int write_trxframe(t_trxstatus *status,t_trxframe *fr,gmx_conect gc);
142 /* Write a frame to a TRX file. 
143  * Only entries for which the gmx_boolean is TRUE will be written,
144  * except for step, time, lambda and/or box, which may not be
145  * omitted for certain trajectory formats.
146  * The precision for .xtc and .gro is fr->prec, when fr->bPrec=FALSE,
147  * the precision is set to 1000.
148  * gc is important for pdb file writing only and may be NULL.
149  */
150
151 GMX_LIBGMX_EXPORT
152 int write_trx(t_trxstatus *status,int nind,atom_id *ind,t_atoms *atoms,
153               int step,real time,matrix box,rvec x[],rvec *v,
154               gmx_conect gc);
155 /* Write an indexed frame to a TRX file.
156  * v can be NULL. 
157  * atoms can be NULL for file types which don't need atom names.
158  */ 
159
160 GMX_LIBGMX_EXPORT
161 void close_trx(t_trxstatus *status);
162 /* Close trj file as opened with read_first_x, read_frist_frame
163  * or open_trx. Identical to close_trj.
164  */
165
166 GMX_LIBGMX_EXPORT
167 t_trxstatus *open_trx(const char *outfile,const char *filemode);
168 /* Open a TRX file and return an allocated status pointer */
169
170 /* get a fileio from a trxstatus */
171 GMX_LIBGMX_EXPORT
172 t_fileio *trx_get_fileio(t_trxstatus *status);
173
174
175 GMX_LIBGMX_EXPORT
176 gmx_bool bRmod_fd(double a, double b, double c,gmx_bool bDouble);
177 /* Returns TRUE when (a - b) MOD c = 0, using a margin which is slightly
178  * larger than the float/double precision.
179  */
180
181 #ifdef GMX_DOUBLE
182 #define bRmod(a,b,c) bRmod_fd(a,b,c,TRUE)
183 #else
184 #define bRmod(a,b,c) bRmod_fd(a,b,c,FALSE)
185 #endif
186
187 int check_times2(real t,real t0,real tp,real tpp,gmx_bool bDouble);
188 /* This routine checkes if the read-in time is correct or not;
189  * returns -1 if t<tbegin or t MOD dt = t0,
190  *          0 if tbegin <= t <=tend+margin,
191  *          1 if t>tend
192  * where margin is 0.1*min(t-tp,tp-tpp), if this positive, 0 otherwise.
193  * tp and tpp should be the time of the previous frame and the one before.
194  * The mod is done with single or double precision accuracy depending
195  * on the value of bDouble.
196  */
197
198 GMX_LIBGMX_EXPORT
199 int check_times(real t);
200 /* This routine checkes if the read-in time is correct or not;
201  * returns -1 if t<tbegin,
202  *          0 if tbegin <= t <=tend,
203  *          1 if t>tend
204  */
205
206
207
208
209
210 /* For trxframe.flags, used in trxframe read routines.
211  * When a READ flag is set, the field will be read when present,
212  * but a frame might be returned which does not contain the field.
213  * When a NEED flag is set, frames not containing the field will be skipped.
214  */
215 #define TRX_READ_X    (1<<0)
216 #define TRX_NEED_X    (1<<1)
217 #define TRX_READ_V    (1<<2)
218 #define TRX_NEED_V    (1<<3)
219 #define TRX_READ_F    (1<<4)
220 #define TRX_NEED_F    (1<<5)
221 /* Useful for reading natoms from a trajectory without skipping */
222 #define TRX_DONT_SKIP (1<<6)
223
224 /* For trxframe.not_ok */
225 #define HEADER_NOT_OK (1<<0)
226 #define DATA_NOT_OK   (1<<1)
227 #define FRAME_NOT_OK  (HEADER_NOT_OK | DATA_NOT_OK)
228
229 GMX_LIBGMX_EXPORT
230 int read_first_frame(const output_env_t oenv,t_trxstatus **status,
231                             const char *fn, t_trxframe *fr,int flags);
232   /* Read the first frame which is in accordance with flags, which are
233    * defined further up in this file. 
234    * Returns natoms when succeeded, 0 otherwise.
235    * Memory will be allocated for flagged entries.
236    * The flags are copied to fr for subsequent calls to read_next_frame.
237    * Returns TRUE when succeeded, FALSE otherwise.
238    */
239
240 GMX_LIBGMX_EXPORT
241 gmx_bool read_next_frame(const output_env_t oenv,t_trxstatus *status,
242                             t_trxframe *fr);
243   /* Reads the next frame which is in accordance with fr->flags.
244    * Returns TRUE when succeeded, FALSE otherwise.
245    */
246
247 GMX_LIBGMX_EXPORT
248 int read_first_x(const output_env_t oenv,t_trxstatus **status,
249                         const char *fn, real *t,rvec **x,matrix box);
250 /* These routines read first coordinates and box, and allocates 
251  * memory for the coordinates, for a trajectory file.
252  * The routine returns the number of atoms, or 0 when something is wrong.
253  * The integer in status should be passed to calls of read_next_x
254  */
255
256 GMX_LIBGMX_EXPORT
257 gmx_bool read_next_x(const output_env_t oenv,t_trxstatus *status,real *t,
258                         int natoms, rvec x[],matrix box);
259 /* Read coordinates and box from a trajectory file. Return TRUE when all well,
260  * or FALSE when end of file (or last frame requested by user).
261  * status is the integer set in read_first_x.
262  */
263
264 GMX_LIBGMX_EXPORT
265 void close_trj(t_trxstatus *status);
266 /* Close trj file as opened with read_first_x, read_frist_frame
267  * or open_trx. Identical to close_trx.
268  */
269
270 void rewind_trj(t_trxstatus *status);
271 /* Rewind trj file as opened with read_first_x */
272
273 GMX_LIBGMX_EXPORT
274 t_topology *read_top(const char *fn,int *ePBC);
275 /* Extract a topology data structure from a topology file.
276  * If ePBC!=NULL *ePBC gives the pbc type.
277  */
278
279 /*****************************************************
280  *         Some command line parsing routines 
281  *****************************************************/
282
283 #define PCA_CAN_VIEW       (1<<5)
284 /* add option -w to view output files (must be implemented in program) */
285 #define PCA_CAN_BEGIN      (1<<6)
286 #define PCA_CAN_END        (1<<7)
287 #define PCA_CAN_DT         (1<<14)
288 #define PCA_CAN_TIME       (PCA_CAN_BEGIN | PCA_CAN_END | PCA_CAN_DT)
289 /* adds options -b and -e for begin and end time for reading trajectories */
290 #define PCA_TIME_UNIT      (1<<15)
291 /* set time unit for output */
292 #define PCA_KEEP_ARGS      (1<<8)
293 /* keep parsed args in argv (doesn't make sense without NOEXIT_ON_ARGS) */
294 #define PCA_SILENT         (1<<9)
295 /* don't print options by default */
296 #define PCA_CAN_SET_DEFFNM (1<<10)
297 /* does something for non-master mdrun nodes */
298 #define PCA_NOEXIT_ON_ARGS (1<<11)
299 /* no fatal_error when invalid options are encountered */
300 #define PCA_QUIET          (1<<12)
301 /* does something for non-master mdrun nodes */
302 #define PCA_BE_NICE        (1<<13)
303 /* Default to low priority, unless configured with --disable-nice */
304 #define PCA_NOT_READ_NODE  (1<<16)
305 /* Is this node not reading: for parallel all nodes but the master */
306
307 int iscan(int argc,char *argv[],int *i);
308 /* Scan an int from the argument at *i. If the argument length
309  * is > 2, the int is assumed to be in the remainder of the arg,
310  * eg: -p32, else the int is assumed to be in the next argument
311  * eg: -p 32. If neither is the case the routine exits with an error,
312  * otherwise it returns the value found. If the value is in the next
313  * argument *i is incremented. You typically would want to pass
314  * a loop variable to this routine.
315  */
316 gmx_large_int_t istepscan(int argc,char *argv[],int *i);
317 /* Same as above, but for large integer values */
318
319 double dscan(int argc,char *argv[],int *i);
320 /* Routine similar to the above, but working on doubles. */
321
322 char *sscan(int argc,char *argv[],int *i);
323 /* Routine similar to the above, but working on strings. The pointer
324  * returned is a pointer to the argv field.
325  */
326
327 void vscan(int argc,char *argv[],int *i,rvec *vec);
328 /* Routine similar to the above, but working on rvecs. */
329
330 GMX_LIBGMX_EXPORT
331 int nenum(const char *const enumc[]);
332 /* returns ordinal number of selected enum from args 
333  * depends on enumc[0] pointing to one of the other elements
334  * array must be terminated by a NULL pointer 
335  */
336
337 GMX_LIBGMX_EXPORT
338 void parse_common_args(int *argc,char *argv[],unsigned long Flags,
339                               int nfile,t_filenm fnm[],int npargs,t_pargs *pa,
340                               int ndesc,const char **desc,
341                               int nbugs,const char **bugs, 
342                               output_env_t *oenv);
343 /* Get arguments from the arg-list. The arguments extracted
344  * are removed from the list. If manual is NULL a default message is displayed
345  * when errors are encountered. The Flags argument, when non-0 enables
346  * some input checks. Using this routine also means that the arguments
347  * -b and -e will be used for begin and end time, whether this is 
348  * appropriate or not!
349  */
350
351 #ifdef __cplusplus
352 }
353 #endif
354
355 #endif