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