9d3a7645ee329868fad36ec232100e16ab088bd1
[alexxy/gromacs.git] / src / gmxlib / trajana / trajana.c
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  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11  * Copyright (c) 2001-2009, The GROMACS development team,
12  * check out http://www.gromacs.org for more information.
13
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * If you want to redistribute modifications, please consider that
20  * scientific software is very special. Version control is crucial -
21  * bugs must be traceable. We will be happy to consider code for
22  * inclusion in the official distribution, but derived work must not
23  * be called official GROMACS. Details are found in the README & COPYING
24  * files - if they are missing, get the official version at www.gromacs.org.
25  *
26  * To help us fund GROMACS development, we humbly ask that you cite
27  * the papers on the package - you can find them in the top README file.
28  *
29  * For more info, check our website at http://www.gromacs.org
30  */
31 /*! \page libtrajana Library for trajectory analysis
32  *
33  * This is a trajectory analysis library for Gromacs.
34  *
35  * The main features of the library are:
36  *  - \subpage selengine "Flexible handling of textual selections" that can
37  *    also be dynamic, i.e., depend of the trajectory frame through
38  *    positions of the particles.
39  *    Selections evaluate directly to positions, which can then be used in
40  *    the analysis program.
41  *  - \subpage selmethods "Custom selection keywords" can also be easily
42  *    implemented, also on a tool-by-tool basis.
43  *  - Efficient \subpage nbsearch "neighborhood searching"
44  *    (currently not very efficient...).
45  *  - \subpage displacements "On-the-fly calculation of displacement vectors"
46  *    during a single pass over the trajectory.
47  *  - \subpage histograms "Calculation of histograms" with error estimates
48  *    through block averaging.
49  *
50  * The functions also automate several things common to most analysis programs
51  * such as making molecules whole if required, reading input files, and
52  * setting up index groups for analysis.
53  * The library unifies the structure of analysis programs (at least a bit)
54  * and makes it easier to add common functionality to all analysis programs.
55  *
56  *
57  * \section main_using Using the library
58  *
59  * There is a \ref share/template/template.c "template" for writing
60  * analysis programs, the documentation for it and links from there should
61  * help getting started.
62  *
63  *
64  * \internal
65  * \section libtrajana_impl Implementation details
66  *
67  * Some internal implementation details of the library are documented on
68  * separate pages:
69  *  - \subpage poscalcengine
70  *  - \subpage selparser
71  *  - \subpage selcompiler
72  */
73 /*! \page selengine Text-based selections
74  *
75  * \section selection_basics Basics
76  *
77  * Selections are enabled automatically for an analysis program that uses
78  * the library. The selection syntax is described in an online help that is
79  * accessible from all tools that use the library.
80  * By default, dynamic selections are allowed, and the user can freely
81  * choose whether to analyze atoms or centers of mass/geometry of
82  * residues/molecules.
83  * These defaults, as well as some others, can be changed by specifying
84  * flags for gmx_ana_traj_create().
85  *
86  * The analysis program can then access the selected positions for each frame
87  * through a \p gmx_ana_selection_t array that is passed to the frame
88  * analysis function (see gmx_analysisfunc()).
89  * As long as the analysis program is written such that it does not assume
90  * that the number of positions or the atoms in the groups groups remain
91  * constant, any kind of selection expression can be used.
92  *
93  * Some analysis programs may require a special structure for the index groups
94  * (e.g., \c g_angle requires the index group to be made of groups of three or
95  * four atoms).
96  * For such programs, it is up to the user to provide a proper selection
97  * expression that always returns such positions.
98  * Such analysis program can define \ref ANA_REQUIRE_WHOLE to make the
99  * default behavior appropriate for the most common uses where the groups
100  * should consist of atoms within a single residue/molecule.
101  *
102  * \section selection_methods Implementing new keywords
103  *
104  * New selection keywords can be easily implemented, either directly into the
105  * library or as part of analysis programs (the latter may be useful for
106  * testing or methods very specific to some analysis).
107  * For both cases, you should first create a \c gmx_ana_selmethod_t structure
108  * and fill it with the necessary information.
109  * Details can be found on a separate page: \ref selmethods.
110  */
111 /*! \internal \file
112  * \brief Implementation of functions in trajana.h.
113  */
114 /*! \internal \dir src/gmxlib/trajana
115  * \brief Source code for common trajectory analysis functions.
116  *
117  * Selection handling is found in \ref src/gmxlib/selection.
118  */
119 #ifdef HAVE_CONFIG_H
120 #include <config.h>
121 #endif
122
123 #include <string.h>
124
125 #include <filenm.h>
126 #include <futil.h>
127 #include <macros.h>
128 #include <pbc.h>
129 #include <rmpbc.h>
130 #include <smalloc.h>
131 #include <statutil.h>
132 #include <typedefs.h>
133 #include <tpxio.h>
134 #include <vec.h>
135
136 #include <poscalc.h>
137 #include <selection.h>
138 #include <selmethod.h>
139 #include <trajana.h>
140
141 /*! \internal \brief
142  * Data structure for trajectory analysis tools.
143  */
144 struct gmx_ana_traj_t
145 {
146     /*! \brief
147      * Flags that alter the behavior of the analysis library.
148      *
149      * This variable stores the flags passed to gmx_ana_traj_create() for use
150      * in the other functions.
151      */
152     unsigned long             flags;
153     /** Number of input reference groups. */
154     int                       nrefgrps;
155     /*! \brief
156      * Number of input analysis groups.
157      *
158      * This is the number of groups in addition to the reference groups
159      * that are required.
160      * If -1, any number of groups (at least one) is acceptable.
161      */
162     int                       nanagrps;
163     /*! \brief
164      * Flags for init_first_frame() to specify what to read from the
165      * trajectory.
166      */
167     int                       frflags;
168     /** TRUE if molecules should be made whole for each frame. */
169     bool                      bRmPBC;
170     /*! \brief
171      * TRUE if periodic boundary conditions should be used.
172      *
173      * If the flag is FALSE, the \p pbc pointer passed to gmx_analysisfunc()
174      * is NULL.
175      */
176     bool                      bPBC;
177
178     /** Name of the trajectory file (NULL if not provided). */
179     char                     *trjfile;
180     /** Name of the topology file (NULL if no topology loaded). */
181     char                     *topfile;
182     /** Non-NULL name of the topology file. */
183     char                     *topfile_notnull;
184     /** Name of the index file (NULL if no index file provided). */
185     char                     *ndxfile;
186     /** Name of the selection file (NULL if no file provided). */
187     char                     *selfile;
188     /** The selection string (NULL if not provided). */
189     char                     *selection;
190
191     /** The topology structure, or \p NULL if no topology loaded. */
192     t_topology               *top;
193     /** TRUE if full tpx file was loaded, FALSE otherwise. */
194     bool                      bTop;
195     /** Coordinates from the topology (see \p bTopX). */
196     rvec                     *xtop;
197     /** The box loaded from the topology file. */
198     matrix                    boxtop;
199     /** The ePBC field loaded from the topology file. */
200     int                       ePBC;
201
202     /** The current frame, or \p NULL if no frame loaded yet. */
203     t_trxframe               *fr;
204     /** Used to store the status variable from read_first_frame(). */
205     t_trxstatus              *status;
206     /** The number of frames read. */
207     int                       nframes;
208
209     /** Number of elements in the \p sel array. */
210     int                       ngrps;
211     /*! \brief
212      * Array of selection information (one element for each index group).
213      *
214      * After the call to gmx_ana_init_selections(), this array contains
215      * information about the selections the user has provided.
216      * The array contains \p ngrps elements;
217      * if \p nanagrps was -1, the number may vary.
218      * See \c gmx_ana_selection_t for details of the contents.
219      *
220      * The largest possible index groups for dynamic selections can be found
221      * in \p sel[i]->g, i.e., the program can assume that any index group
222      * passed to gmx_analysisfunc() is a subset of the provided group.
223      * After gmx_ana_do(), the same groups can be used in post-processing.
224      */
225     gmx_ana_selection_t     **sel;
226     /** Array of names of the selections for convenience. */
227     char                    **grpnames;
228     /** Position calculation data. */
229     gmx_ana_poscalc_coll_t   *pcc;
230     /** Selection data. */
231     gmx_ana_selcollection_t  *sc;
232
233     /** Data for statutil.c utilities. */
234     output_env_t              oenv;
235 };
236
237 /** Loads the topology. */
238 static int load_topology(gmx_ana_traj_t *d, bool bReq);
239 /** Loads the first frame and does some checks. */
240 static int init_first_frame(gmx_ana_traj_t *d);
241
242 static int add_fnmarg(int nfile, t_filenm *fnm, t_filenm *fnm_add)
243 {
244     memcpy(&(fnm[nfile]), fnm_add, sizeof(*fnm_add));
245     return nfile + 1;
246 }
247
248 /* Copied from src/gmxlib/statutil.c */
249 static int
250 add_parg(int npargs, t_pargs *pa, t_pargs *pa_add)
251 {
252     memcpy(&(pa[npargs]), pa_add, sizeof(*pa_add));
253     return npargs + 1;
254 }
255
256 /*!
257  * \param[out] data  Trajectory analysis data structure poitner to initialize.
258  * \param[in]  flags Combination of flags (see \ref analysis_flags).
259  * \returns    0 on success.
260  */
261 int
262 gmx_ana_traj_create(gmx_ana_traj_t **data, unsigned long flags)
263 {
264     gmx_ana_traj_t     *d;
265     int                 rc;
266
267     snew(d, 1);
268
269     d->nrefgrps        = 0;
270     d->nanagrps        = 1;
271     d->frflags         = TRX_NEED_X;
272     d->bRmPBC          = TRUE;
273     d->bPBC            = TRUE;
274
275     d->trjfile         = NULL;
276     d->topfile         = NULL;
277     d->ndxfile         = NULL;
278     d->selfile         = NULL;
279     d->selection       = NULL;
280
281     d->top             = NULL;
282     d->bTop            = FALSE;
283     d->xtop            = NULL;
284     d->ePBC            = -1;
285     d->fr              = NULL;
286     d->nframes         = 0;
287
288     d->ngrps           = 0;
289     d->sel             = NULL;
290     d->grpnames        = NULL;
291
292     d->flags           = flags;
293     d->topfile_notnull = NULL;
294     rc = gmx_ana_poscalc_coll_create(&d->pcc);
295     if (rc != 0)
296     {
297         sfree(d);
298         *data = NULL;
299         return rc;
300     }
301     rc = gmx_ana_selcollection_create(&d->sc, d->pcc);
302     if (rc != 0)
303     {
304         gmx_ana_poscalc_coll_free(d->pcc);
305         sfree(d);
306         *data = NULL;
307         return rc;
308     }
309     d->status          = NULL;
310     d->oenv            = NULL;
311
312     *data              = d;
313     return 0;
314 }
315
316 /*!
317  * \param   d  Trajectory analysis data to free.
318  */
319 void
320 gmx_ana_traj_free(gmx_ana_traj_t *d)
321 {
322     int                 i;
323
324     sfree(d->trjfile);
325     sfree(d->topfile);
326     sfree(d->topfile_notnull);
327     sfree(d->ndxfile);
328     sfree(d->selfile);
329     if (d->top)
330     {
331         done_top(d->top);
332         sfree(d->top);
333     }
334     if (d->fr)
335     {
336         /* Gromacs does not seem to have a function for freeing frame data */
337         sfree(d->fr->x);
338         sfree(d->fr->v);
339         sfree(d->fr->f);
340         sfree(d->fr);
341     }
342     sfree(d->xtop);
343     sfree(d->sel);
344     gmx_ana_selcollection_free(d->sc);
345     gmx_ana_poscalc_coll_free(d->pcc);
346     sfree(d->grpnames);
347     output_env_done(d->oenv);
348     sfree(d);
349 }
350
351 /*!
352  * \param[in,out] d      Trajectory analysis data structure.
353  * \param[in]     flags  Additional flags to set.
354  * \returns       0 on success, a non-zero error code on error.
355  *
356  * Currently there are no checks whether the flags make sense or not.
357  */
358 int
359 gmx_ana_add_flags(gmx_ana_traj_t *d, unsigned long flags)
360 {
361     d->flags |= flags;
362     return 0;
363 }
364
365 /*!
366  * \param[in,out] d      Trajectory analysis data structure.
367  * \param[in]     bPBC   TRUE if periodic boundary conditions should be used.
368  * \returns       0 on success.
369  * 
370  * If this function is called before parse_trjana_args(), it sets the default
371  * for whether PBC are used in the analysis or not.
372  * If \ref ANA_NOUSER_PBC is not set, a command-line option is provided for the
373  * user to override the default value.
374  * If called after parse_trjana_args(), it overrides the setting provided by
375  * the user or an earlier call.
376  *
377  * If this function is not called, the default is to use PBC.
378  *
379  * If PBC are not used, the \p pbc pointer passed to gmx_analysisfunc()
380  * is NULL.
381  *
382  * \see \ref ANA_NOUSER_PBC
383  */
384 int
385 gmx_ana_set_pbc(gmx_ana_traj_t *d, bool bPBC)
386 {
387     d->bPBC = bPBC;
388     return 0;
389 }
390
391 /*!
392  * \param[in] d      Trajectory analysis data structure.
393  * \returns   TRUE if periodic boundary conditions are set to be used.
394  */
395 bool
396 gmx_ana_has_pbc(gmx_ana_traj_t *d)
397 {
398     return d->bPBC;
399 }
400
401 /*!
402  * \param[in,out] d      Trajectory analysis data structure.
403  * \param[in]     bRmPBC TRUE if molecules should be made whole.
404  * \returns       0 on success.
405  * 
406  * If this function is called before parse_trjana_args(), it sets the default
407  * for whether molecules are made whole.
408  * If \ref ANA_NOUSER_RMPBC is not set, a command-line option is provided for
409  * the user to override the default value.
410  * If called after parse_trjana_args(), it overrides the setting provided by
411  * the user or an earlier call.
412  *
413  * If this function is not called, the default is to make molecules whole.
414  *
415  * The main use of this function is to call it with FALSE if your analysis
416  * program does not require whole molecules as this can increase the
417  * performance.
418  * In such a case, you can also specify \ref ANA_NOUSER_RMPBC to not to
419  * confuse the user with an option that would only slow the program
420  * down.
421  *
422  * \see \ref ANA_NOUSER_RMPBC
423  */
424 int
425 gmx_ana_set_rmpbc(gmx_ana_traj_t *d, bool bRmPBC)
426 {
427     d->bRmPBC = bRmPBC;
428     return 0;
429 }
430
431 /*!
432  * \param[in,out] d       Trajectory analysis data structure.
433  * \param[in]     frflags Flags for what to read from the trajectory file.
434  * \returns       0 on success, an error code on error.
435  *
436  * The TRX_NEED_X flag is always set.
437  * If the analysis tools needs some other information (velocities, forces),
438  * it can call this function to load additional information from the
439  * trajectory.
440  */
441 int
442 gmx_ana_set_frflags(gmx_ana_traj_t *d, int frflags)
443 {
444     if (d->sel)
445     {
446         gmx_call("cannot set trajectory flags after initializing selections");
447         return -1;
448     }
449     if (d->fr)
450     {
451         gmx_call("cannot set trajectory flags after the first frame has been read");
452         return -1;
453     }
454     frflags |= TRX_NEED_X;
455     d->frflags = frflags;
456     return 0;
457 }
458
459 /*!
460  * \param[in,out] d        Trajectory analysis data structure.
461  * \param[in]     nrefgrps Number of reference groups required.
462  * \returns       0 on success, a non-zero error code on error.
463  *
464  * \p nrefgrps should be a non-negative integer.
465  * If this function is not called (or \p nrefgrps is 0), all selections are
466  * treated as reference groups.
467  */
468 int
469 gmx_ana_set_nrefgrps(gmx_ana_traj_t *d, int nrefgrps)
470 {
471     if (nrefgrps < 0)
472     {
473         d->nrefgrps = 0;
474         gmx_incons("number of reference groups is negative");
475         return EINVAL;
476     }
477     d->nrefgrps = nrefgrps;
478     return 0;
479 }
480
481 /*!
482  * \param[in,out] d        Trajectory analysis data structure.
483  * \param[in]     nanagrps Number of analysis groups required
484  *   (-1 stands for any number of analysis groups).
485  * \returns       0 on success, a non-zero error code on error.
486  *
487  * \p nanagrps should be a positive integer or -1.
488  * In the latter case, any number of groups (but at least one) is acceptable.
489  * gmx_ana_get_nanagrps() can be used to access the actual value after
490  * gmx_ana_init_selections() has been called.
491  * If this function is not called, a single analysis group is expected.
492  */
493 int
494 gmx_ana_set_nanagrps(gmx_ana_traj_t *d, int nanagrps)
495 {
496     if (nanagrps <= 0 && nanagrps != -1)
497     {
498         d->nanagrps = 1;
499         gmx_incons("number of analysis groups is invalid");
500         return EINVAL;
501     }
502     d->nanagrps = nanagrps;
503     return 0;
504 }
505
506 /*!
507  * \param[in]  d     Trajectory analysis data structure.
508  * \param[out] ngrps Total number of selections specified by the user.
509  * \returns    0 on success, a non-zero error code on error.
510  *
511  * The value stored in \p *ngrps is the sum of the number of reference groups
512  * and the number of analysis groups.
513  * If a specific number (not -1) of analysis groups has been set with
514  * gmx_ana_set_nanagrps(), the value is always the sum of the values provided
515  * to gmx_ana_set_nrefgrps() and gmx_ana_set_nanagrps().
516  *
517  * Should only be called after gmx_ana_init_selections().
518  */
519 int
520 gmx_ana_get_ngrps(gmx_ana_traj_t *d, int *ngrps)
521 {
522     if (d->nanagrps == -1)
523     {
524         *ngrps = 0;
525         gmx_call("gmx_ana_init_selections() not called");
526         return EINVAL;
527     }
528     *ngrps = d->nrefgrps + d->nanagrps;
529     return 0;
530 }
531
532 /*!
533  * \param[in]  d        Trajectory analysis data structure.
534  * \param[out] nanagrps Number of analysis groups specified by the user.
535  * \returns    0 on success, a non-zero error code on error.
536  *
537  * If a specific number (not -1) of analysis groups has been set with
538  * gmx_ana_set_nanagrps(), the value is always the same value.
539  * Hence, you only need to call this function if gmx_ana_set_nanagrps() has
540  * been called with \p nanagrps set to -1.
541  *
542  * Should only be called after gmx_ana_init_selections().
543  */
544 int
545 gmx_ana_get_nanagrps(gmx_ana_traj_t *d, int *nanagrps)
546 {
547     if (d->nanagrps == -1)
548     {
549         *nanagrps = 0;
550         gmx_call("gmx_ana_init_selections() not called");
551         return EINVAL;
552     }
553     *nanagrps = d->nanagrps;
554     return 0;
555 }
556
557 /*!
558  * \param[in]  d   Trajectory analysis data structure.
559  * \param[in]  i   Ordinal number of the reference selection to get.
560  * \param[out] sel Selection object for the \p i'th reference group.
561  * \returns    0 on success, a non-zero error code on error.
562  *
563  * The pointer returned in \p *sel should not be freed.
564  * Should only be called after gmx_ana_init_selections().
565  */
566 int
567 gmx_ana_get_refsel(gmx_ana_traj_t *d, int i, gmx_ana_selection_t **sel)
568 {
569     if (i < 0 || i >= d->nrefgrps)
570     {
571         *sel = NULL;
572         gmx_call("invalid reference group number");
573         return EINVAL;
574     }
575     *sel = gmx_ana_selcollection_get_selection(d->sc, i);
576     if (!*sel)
577     {
578         gmx_incons("gmx_ana_init_selections() not called");
579         return EINVAL;
580     }
581     return 0;
582 }
583
584 /*!
585  * \param[in]  d   Trajectory analysis data structure.
586  * \param[out] sel Array of analysis selections.
587  * \returns    0 on success, a non-zero error code on error.
588  *
589  * The pointer returned in \p *sel should not be freed.
590  * Should only be called after gmx_ana_init_selections().
591  */
592 int
593 gmx_ana_get_anagrps(gmx_ana_traj_t *d, gmx_ana_selection_t ***sel)
594 {
595     if (!d->sel)
596     {
597         *sel = NULL;
598         gmx_incons("gmx_ana_init_selections() not called");
599         return EINVAL;
600     }
601     *sel = d->sel;
602     return 0;
603 }
604
605 /*!
606  * \param[in]  d        Trajectory analysis data structure.
607  * \param[out] grpnames Array of selection names.
608  * \returns    0 on success, a non-zero error code on error.
609  *
610  * The pointer returned in \p *grpnames should not be freed.
611  * Should only be called after gmx_ana_init_selections().
612  */
613 int
614 gmx_ana_get_grpnames(gmx_ana_traj_t *d, char ***grpnames)
615 {
616     if (!d->grpnames)
617     {
618         *grpnames = NULL;
619         gmx_call("gmx_ana_init_selections() not called");
620         return EINVAL;
621     }
622     *grpnames = d->grpnames;
623     return 0;
624 }
625
626 /*!
627  * \param[in]  d   Trajectory analysis data structure.
628  * \param[out] sc  Selection collection object.
629  * \returns    0 on success.
630  *
631  * This function is mostly useful for debugging purposes.
632  * The information commonly required in analysis programs is accessible
633  * more conveniently through other means.
634  *
635  * The pointer returned in \p *sc should not be freed.
636  * Can be called at any point.
637  */
638 int
639 gmx_ana_get_selcollection(gmx_ana_traj_t *d, gmx_ana_selcollection_t **sc)
640 {
641     *sc = d->sc;
642     return 0;
643 }
644
645 /*!
646  * \param[in,out] d     Trajectory analysis data structure.
647  * \returns    0 on success, a non-zero error code on error.
648  *
649  * This function should be called first in the analysis program, much in
650  * the same way as parse_common_args() in traditional Gromacs analysis
651  * programs. It adds some command-line arguments of its own, and uses
652  * parse_common_args() to parse the command-line arguments.
653  * It also loads topology information if required or if a topology is
654  * provided on the command line.
655  * Selection handling is also initialized if it is enabled and
656  * the user has selected it on the command line.
657  *
658  * The rest of the parameters are passed on to the Gromacs routine
659  * parse_common_args(), and the use of this function should be identical
660  * to parse_common_args(), with the exception that for \p pca_flags,
661  * \p PCA_CAN_TIME and \p PCA_BE_NICE flags are automatically added.
662  * \param      argc
663  * \param      argv
664  * \param      pca_flags
665  * \param      nfile
666  * \param      fnm
667  * \param      npargs
668  * \param      pa
669  * \param      ndesc
670  * \param      desc
671  * \param      nbugs
672  * \param      bugs
673  * \param      oenv
674  */
675 int
676 parse_trjana_args(gmx_ana_traj_t *d,
677                   int *argc, char *argv[], unsigned long pca_flags,
678                   int nfile, t_filenm fnm[], int npargs, t_pargs *pa,
679                   int ndesc, const char **desc,
680                   int nbugs, const char **bugs,
681                   output_env_t *oenv)
682 {
683     t_filenm           *all_fnm = NULL;
684     int                 max_fnm, nfall;
685     int                *fnm_map;
686     t_pargs            *all_pa = NULL;
687     int                 max_pa, npall;
688     size_t              i;
689     int                 k;
690     int                 rc;
691     const char         *tmp_fnm;
692
693     t_filenm            def_fnm[] = {
694         {efTRX, NULL,  NULL,        ffOPTRD},
695         {efTPS, NULL,  NULL,        ffREAD},
696         {efDAT, "-sf", "selection", ffOPTRD},
697         {efNDX, NULL,  NULL,        ffOPTRD},
698     };
699     bool                bPBC = TRUE;
700     t_pargs             pbc_pa[] = {
701         {"-pbc",      FALSE, etBOOL, {&bPBC},
702          "Use periodic boundary conditions for distance calculation"},
703     };
704     bool                bRmPBC = TRUE;
705     t_pargs             rmpbc_pa[] = {
706         {"-rmpbc",    FALSE, etBOOL, {&bRmPBC},
707          "Make molecules whole for each frame"},
708     };
709     char               *selection = NULL;
710     const char        **rpost     = NULL;
711     bool                bSelDump  = FALSE;
712     t_pargs             sel_pa[] = {
713         {"-select",   FALSE, etSTR,  {&selection},
714          "Selection string (use 'help' for help)"},
715         {"-seldebug", FALSE, etBOOL, {&bSelDump},
716          "HIDDENPrint out the parsed and compiled selection trees"},
717     };
718     t_pargs             dsel_pa[] = {
719         {"-selrpos",  FALSE, etENUM, {NULL},
720          "Selection reference position"},
721     };
722     const char        **spost = NULL;
723     t_pargs             selpt_pa[] = {
724         {"-seltype",  FALSE, etENUM, {NULL},
725          "Default analysis positions"},
726     };
727 #define MAX_PA asize(sel_pa)+asize(dsel_pa)+5
728
729     if (d->nrefgrps < 0)
730     {
731         gmx_incons("number of reference groups is negative");
732         return EINVAL;
733     }
734
735     if (d->flags & ANA_DEBUG_SELECTION)
736     {
737         bSelDump = TRUE;
738     }
739
740     rpost = gmx_ana_poscalc_create_type_enum(!(d->flags & ANA_REQUIRE_WHOLE));
741     if (rpost == NULL)
742     {
743         return ENOMEM;
744     }
745     spost = gmx_ana_poscalc_create_type_enum(TRUE);
746     if (spost == NULL)
747     {
748         sfree(rpost);
749         return ENOMEM;
750     }
751
752     /* Construct the file name argument array */
753     max_fnm = nfile + asize(def_fnm);
754     snew(all_fnm, max_fnm);
755     nfall = 0;
756     if (!(d->flags & ANA_REQUIRE_TOP))
757     {
758         def_fnm[1].flag |= ffOPT;
759     }
760     snew(fnm_map, nfile);
761     for (k = 0; k < nfile; ++k)
762     {
763         fnm_map[k] = -1;
764     }
765
766     for (i = 0; i < asize(def_fnm); ++i)
767     {
768         for (k = 0; k < nfile; ++k)
769         {
770             if (fnm_map[k] == -1 && def_fnm[i].opt == NULL
771                 && fnm[k].opt == NULL && fnm[k].ftp == def_fnm[i].ftp)
772             {
773                 break;
774             }
775         }
776         if (k < nfile)
777         {
778             fnm_map[k] = nfall;
779             nfall = add_fnmarg(nfall, all_fnm, &(fnm[k]));
780         }
781         else
782         {
783             nfall = add_fnmarg(nfall, all_fnm, &(def_fnm[i]));
784         }
785     }
786
787     for (k = 0; k < nfile; ++k)
788     {
789         if (fnm_map[k] == -1)
790         {
791             fnm_map[k] = nfall;
792             nfall = add_fnmarg(nfall, all_fnm, &(fnm[k]));
793         }
794     }
795
796     /* Construct the argument array */
797     max_pa = npargs + MAX_PA;
798     snew(all_pa, max_pa);
799     npall = 0;
800
801     if (!(d->flags & ANA_NOUSER_RMPBC))
802     {
803         for (i = 0; i < asize(rmpbc_pa); ++i)
804         {
805             npall = add_parg(npall, all_pa, &(rmpbc_pa[i]));
806         }
807     }
808     if (!(d->flags & ANA_NOUSER_PBC))
809     {
810         for (i = 0; i < asize(pbc_pa); ++i)
811         {
812             npall = add_parg(npall, all_pa, &(pbc_pa[i]));
813         }
814     }
815
816     for (i = 0; i < asize(sel_pa); ++i)
817     {
818         npall = add_parg(npall, all_pa, &(sel_pa[i]));
819     }
820     if (!(d->flags & ANA_NO_DYNSEL))
821     {
822         dsel_pa[0].u.c = rpost;
823         for (i = 0; i < asize(dsel_pa); ++i)
824         {
825             npall = add_parg(npall, all_pa, &(dsel_pa[i]));
826         }
827     }
828
829     if (!(d->flags & ANA_ONLY_ATOMPOS))
830     {
831         selpt_pa[0].u.c = spost;
832         for (i = 0; i < asize(selpt_pa); ++i)
833         {
834             npall = add_parg(npall, all_pa, &(selpt_pa[i]));
835         }
836     }
837
838     for (k = 0; k < npargs; ++k)
839     {
840         npall = add_parg(npall, all_pa, &(pa[k]));
841     }
842
843     pca_flags |= PCA_CAN_TIME | PCA_BE_NICE;
844     parse_common_args(argc, argv, pca_flags, nfall, all_fnm, npall, all_pa,
845                       ndesc, desc, nbugs, bugs,oenv);
846     d->oenv = *oenv;
847
848     /* Process our own options.
849      * Make copies of file names for easier memory management. */
850     tmp_fnm            = ftp2fn_null(efTRX, nfall, all_fnm);
851     d->trjfile         = tmp_fnm ? strdup(tmp_fnm) : NULL;
852     tmp_fnm            = ftp2fn_null(efTPS, nfall, all_fnm);
853     d->topfile         = tmp_fnm ? strdup(tmp_fnm) : NULL;
854     d->topfile_notnull = strdup(ftp2fn(efTPS, nfall, all_fnm));
855     tmp_fnm            = ftp2fn_null(efNDX, nfall, all_fnm);
856     d->ndxfile         = tmp_fnm ? strdup(tmp_fnm) : NULL;
857     if (!(d->flags & ANA_NOUSER_RMPBC))
858     {
859         d->bRmPBC      = bRmPBC;
860     }
861     if (!(d->flags & ANA_NOUSER_PBC))
862     {
863         d->bPBC        = bPBC;
864     }
865     d->selection       = selection;
866     tmp_fnm            = opt2fn_null("-sf", nfall, all_fnm);
867     d->selfile         = tmp_fnm ? strdup(tmp_fnm) : NULL;
868
869     /* Copy the results back */
870     for (k = 0; k < nfile; ++k)
871     {
872         memcpy(&(fnm[k]), &(all_fnm[fnm_map[k]]), sizeof(fnm[k]));
873         /* Delegate responsibility of freeing the file names to caller. */
874         all_fnm[fnm_map[k]].nfiles = 0;
875         all_fnm[fnm_map[k]].fns    = NULL;
876     }
877     for (i = 0, k = npall - npargs; i < (size_t)npargs; ++i, ++k)
878     {
879         memcpy(&(pa[i]), &(all_pa[k]), sizeof(pa[i]));
880     }
881
882     /* Free memory we have allocated. */
883     done_filenms(nfall, all_fnm);
884     sfree(all_fnm);
885     sfree(fnm_map);
886     sfree(all_pa);
887
888     if (!(d->flags & ANA_NO_DYNSEL))
889     {
890         gmx_ana_selcollection_set_refpostype(d->sc, rpost[0]);
891     }
892     else
893     {
894         gmx_ana_selcollection_set_refpostype(d->sc, rpost[1]);
895     }
896     sfree(rpost);
897     if (bSelDump)
898     {
899         d->flags |= ANA_DEBUG_SELECTION;
900     }
901     else
902     {
903         d->flags &= ~ANA_DEBUG_SELECTION;
904     }
905
906     if (!(d->flags & ANA_ONLY_ATOMPOS))
907     {
908         gmx_ana_selcollection_set_outpostype(d->sc, spost[0], d->flags & ANA_USE_POSMASK);
909     }
910     else
911     {
912         gmx_ana_selcollection_set_outpostype(d->sc, spost[1], d->flags & ANA_USE_POSMASK);
913     }
914     sfree(spost);
915
916     /* Check if the user requested help on selections.
917      * If so, call gmx_ana_init_selections() to print the help and exit. */
918     if (selection && strncmp(selection, "help", 4) == 0)
919     {
920         gmx_ana_init_selections(d);
921     }
922
923     /* If no trajectory file is given, we need to set some flags to be able
924      * to prepare a frame from the loaded topology information. Also, check
925      * that a topology is provided. */
926     if (!d->trjfile)
927     {
928         if (!d->topfile)
929         {
930             gmx_input("No trajectory or topology provided, nothing to do!");
931             return -1;
932         }
933         d->flags |= ANA_REQUIRE_TOP;
934         d->flags |= ANA_USE_TOPX;
935     }
936
937     /* Load the topology if so requested. */
938     rc = load_topology(d, (d->flags & ANA_REQUIRE_TOP));
939     if (rc != 0)
940     {
941         return rc;
942     }
943
944     /* Initialize the selections/index groups */
945     if (!(d->flags & ANA_USER_SELINIT))
946     {
947         rc = gmx_ana_init_selections(d);
948     }
949
950     return rc;
951 }
952
953 /*!
954  * \param[in,out] d     Trajectory analysis data structure.
955  * \param[in]     bReq  If TRUE, topology loading is forced.
956  * \returns       0 on success, a non-zero error code on error.
957  *
958  * Initializes the \c gmx_ana_traj_t::top, \c gmx_ana_traj_t::bTop,
959  * \c gmx_ana_traj_t::boxtop and \c gmx_ana_traj_t::ePBC fields of the
960  * analysis structure.
961  * If \p bReq is TRUE, the topology is loaded even if it is not given on
962  * the command line.
963  *
964  * The function can be called multiple times safely; extra calls are
965  * ignored.
966  */
967 static int load_topology(gmx_ana_traj_t *d, bool bReq)
968 {
969     char                title[STRLEN];
970
971     /* Return if topology already loaded */
972     if (d->top)
973     {
974         return 0;
975     }
976
977     if (d->topfile || bReq)
978     {
979         snew(d->top, 1);
980         d->bTop = read_tps_conf(d->topfile_notnull, title, d->top,
981                                 &d->ePBC, &d->xtop, NULL, d->boxtop, TRUE);
982         if (!(d->flags & ANA_USE_TOPX))
983         {
984             sfree(d->xtop);
985             d->xtop = NULL;
986         }
987     }
988     return 0;
989 }
990
991 /*!
992  * \param[in]  d     Trajectory analysis data structure.
993  * \param[in]  bReq  If TRUE, topology loading is forced.
994  * \param[out] top   Topology data pointer to initialize.
995  * \param[out] bTop  TRUE if a full tpx file was loaded, FALSE otherwise
996  *   (can be NULL, in which case it is not used).
997  * \returns    0 on success, a non-zero error code on error.
998  *
999  * If \ref ANA_REQUIRE_TOP has not been specified and \p bReq is FALSE,
1000  * the pointer stored in \p *top may be NULL if no topology has been provided
1001  * on the command line.
1002  *
1003  * The pointer returned in \p *top should not be freed.
1004  */
1005 int
1006 gmx_ana_get_topology(gmx_ana_traj_t *d, bool bReq, t_topology **top, bool *bTop)
1007 {
1008     int rc;
1009
1010     rc = load_topology(d, bReq);
1011     if (rc != 0)
1012     {
1013         *top = NULL;
1014         return rc;
1015     }
1016     *top = d->top;
1017     if (bTop)
1018     {
1019         *bTop = d->bTop;
1020     }
1021     return 0;
1022 }
1023
1024 /*!
1025  * \param[in]  d     Trajectory analysis data structure.
1026  * \param[out] x     Topology data pointer to initialize.
1027  *   (can be NULL, in which case it is not used).
1028  * \param[out] box   Box size from the topology file
1029  *   (can be NULL, in which case it is not used).
1030  * \param[out] ePBC  The ePBC field from the topology
1031  *   (can be NULL, in which case it is not used).
1032  * \returns    0 on success, a non-zero error code on error.
1033  *
1034  * If \ref ANA_USE_TOPX has not been specified, the \p x parameter should be
1035  * NULL.
1036  *
1037  * The pointer returned in \p *x should not be freed.
1038  */
1039 int
1040 gmx_ana_get_topconf(gmx_ana_traj_t *d, rvec **x, matrix box, int *ePBC)
1041 {
1042     if (box)
1043     {
1044         copy_mat(d->boxtop, box);
1045     }
1046     if (ePBC)
1047     {
1048         *ePBC = d->ePBC;
1049     }
1050     if (x)
1051     {
1052         if (!(d->flags & ANA_USE_TOPX))
1053         {
1054             gmx_incons("topology coordinates requested by ANA_USE_TOPX not set");
1055             *x = NULL;
1056             return EINVAL;
1057         }
1058         *x = d->xtop;
1059     }
1060     return 0;
1061 }
1062
1063 /*! \brief
1064  * Loads default index groups from a selection file.
1065  *
1066  * \param[in,out] d     Trajectory analysis data structure.
1067  * \param[out]    grps  Pointer to receive the default groups.
1068  * \returns       0 on success, a non-zero error code on error.
1069  */
1070 static int
1071 init_default_selections(gmx_ana_traj_t *d, gmx_ana_indexgrps_t **grps)
1072 {
1073     gmx_ana_selcollection_t  *sc;
1074     char                     *fnm;
1075     int                       nr, nr_notempty, i;
1076     int                       rc;
1077
1078     /* If an index file is provided, just load it and exit. */
1079     if (d->ndxfile)
1080     {
1081         gmx_ana_indexgrps_init(grps, d->top, d->ndxfile);
1082         return 0;
1083     }
1084     /* Initialize groups to NULL if we return prematurely. */
1085     *grps = NULL;
1086     /* Return immediately if no topology provided. */
1087     if (!d->top)
1088     {
1089         return 0;
1090     }
1091
1092     /* Find the default selection file, return if none found. */
1093     fnm = low_gmxlibfn("defselection.dat", TRUE, FALSE);
1094     if (fnm == NULL)
1095     {
1096         return 0;
1097     }
1098
1099     /* Create a temporary selection collection. */
1100     rc = gmx_ana_selcollection_create(&sc, d->pcc);
1101     if (rc != 0)
1102     {
1103         sfree(fnm);
1104         return rc;
1105     }
1106     rc = gmx_ana_selmethod_register_defaults(sc);
1107     if (rc != 0)
1108     {
1109         gmx_ana_selcollection_free(sc);
1110         sfree(fnm);
1111         gmx_fatal(FARGS, "default selection method registration failed");
1112         return rc;
1113     }
1114     /* FIXME: It would be better to not have the strings here hard-coded. */
1115     gmx_ana_selcollection_set_refpostype(sc, "atom");
1116     gmx_ana_selcollection_set_outpostype(sc, "atom", FALSE);
1117
1118     /* Parse and compile the file with no external groups. */
1119     rc = gmx_ana_selcollection_parse_file(sc, fnm, NULL);
1120     sfree(fnm);
1121     if (rc != 0)
1122     {
1123         gmx_ana_selcollection_free(sc);
1124         fprintf(stderr, "\nWARNING: default selection(s) could not be parsed\n");
1125         return rc;
1126     }
1127     gmx_ana_selcollection_set_topology(sc, d->top, -1);
1128     rc = gmx_ana_selcollection_compile(sc);
1129     if (rc != 0)
1130     {
1131         gmx_ana_selcollection_free(sc);
1132         fprintf(stderr, "\nWARNING: default selection(s) could not be compiled\n");
1133         return rc;
1134     }
1135
1136     /* Count the non-empty groups and check that there are no dynamic
1137      * selections. */
1138     nr = gmx_ana_selcollection_get_count(sc);
1139     nr_notempty = 0;
1140     for (i = 0; i < nr; ++i)
1141     {
1142         gmx_ana_selection_t  *sel;
1143
1144         sel = gmx_ana_selcollection_get_selection(sc, i);
1145         if (sel->bDynamic)
1146         {
1147             fprintf(stderr, "\nWARNING: dynamic default selection ignored\n");
1148         }
1149         else if (sel->g->isize > 0)
1150         {
1151             ++nr_notempty;
1152         }
1153     }
1154
1155     /* Copy the groups to the output structure */
1156     gmx_ana_indexgrps_alloc(grps, nr_notempty);
1157     nr_notempty = 0;
1158     for (i = 0; i < nr; ++i)
1159     {
1160         gmx_ana_selection_t  *sel;
1161
1162         sel = gmx_ana_selcollection_get_selection(sc, i);
1163         if (!sel->bDynamic && sel->g->isize > 0)
1164         {
1165             gmx_ana_index_t  *g;
1166
1167             g = gmx_ana_indexgrps_get_grp(*grps, nr_notempty);
1168             gmx_ana_index_copy(g, sel->g, TRUE);
1169             g->name = strdup(sel->name);
1170             ++nr_notempty;
1171         }
1172     }
1173
1174     gmx_ana_selcollection_free(sc);
1175     return 0;
1176 }
1177
1178 /*!
1179  * \param[in,out] d     Trajectory analysis data structure.
1180  * \returns       0 on success, a non-zero error code on error.
1181  *
1182  * Initializes the selection data in \c gmx_ana_traj_t based on
1183  * the selection options and/or index files provided on the command line.
1184  *
1185  * This function is called automatically by parse_trjana_args() and should
1186  * not be called directly unless \ref ANA_USER_SELINIT is specified.
1187  *
1188  * \see ANA_USER_SELINIT
1189  */
1190 int
1191 gmx_ana_init_selections(gmx_ana_traj_t *d)
1192 {
1193     int                  rc;
1194     int                  i;
1195     int                  nr;
1196     gmx_ana_indexgrps_t *grps;
1197     int                  natoms;
1198     bool                 bStdIn;
1199     bool                 bInteractive;
1200     bool                 bOk;
1201
1202     if (d->sel)
1203     {
1204         gmx_call("init_selections called more than once\n"
1205                  "perhaps you forgot ANA_USER_SELINIT");
1206         return -1;
1207     }
1208
1209     gmx_ana_selcollection_set_veloutput(d->sc,
1210             d->frflags & (TRX_READ_V | TRX_NEED_V));
1211     gmx_ana_selcollection_set_forceoutput(d->sc,
1212             d->frflags & (TRX_READ_F | TRX_NEED_F));
1213     /* Check if we need some information from the topology */
1214     if (gmx_ana_selcollection_requires_top(d->sc))
1215     {
1216         rc = load_topology(d, TRUE);
1217         if (rc != 0)
1218         {
1219             return rc;
1220         }
1221     }
1222     /* Initialize the default selection methods */
1223     rc = gmx_ana_selmethod_register_defaults(d->sc);
1224     if (rc != 0)
1225     {
1226         gmx_fatal(FARGS, "default selection method registration failed");
1227         return rc;
1228     }
1229     /* Initialize index groups.
1230      * We ignore the return value to continue without the default groups if
1231      * there is an error there. */
1232     init_default_selections(d, &grps);
1233     /* Parse the selections */
1234     bStdIn = (d->selfile && d->selfile[0] == '-' && d->selfile[1] == 0)
1235              || (d->selection && d->selection[0] == 0)
1236              || (!d->selfile && !d->selection);
1237     /* Behavior is not very pretty if we cannot check for interactive input,
1238      * but at least it should compile and work in most cases. */
1239 #ifdef HAVE_UNISTD_H
1240     bInteractive = bStdIn && isatty(fileno(stdin));
1241 #else
1242     bInteractive = bStdIn;
1243 #endif
1244     if (bStdIn && bInteractive)
1245     {
1246         /* Parse from stdin */
1247         /* First we parse the reference groups if there are any */
1248         if (d->nrefgrps > 0)
1249         {
1250             fprintf(stderr, "\nSpecify ");
1251             if (d->nrefgrps == 1)
1252             {
1253                 fprintf(stderr, "a reference selection");
1254             }
1255             else
1256             {
1257                 fprintf(stderr, "%d reference selections", d->nrefgrps);
1258             }
1259             fprintf(stderr, ":\n");
1260             fprintf(stderr, "(one selection per line, 'help' for help)\n");
1261             rc = gmx_ana_selcollection_parse_stdin(d->sc, d->nrefgrps, grps, TRUE);
1262             nr = gmx_ana_selcollection_get_count(d->sc);
1263             if (rc != 0 || nr != d->nrefgrps)
1264             {
1265                 gmx_ana_traj_free(d);
1266                 gmx_input("unrecoverable error in selection parsing");
1267                 return rc;
1268             }
1269         }
1270         /* Then, we parse the analysis groups */
1271         fprintf(stderr, "\nSpecify ");
1272         if (d->nanagrps == 1)
1273         {
1274             fprintf(stderr, "a selection");
1275         }
1276         else if (d->nanagrps == -1)
1277         {
1278             fprintf(stderr, "any number of selections");
1279         }
1280         else
1281         {
1282             fprintf(stderr, "%d selections", d->nanagrps);
1283         }
1284         fprintf(stderr, " for analysis:\n");
1285         fprintf(stderr, "(one selection per line, 'help' for help%s)\n",
1286                 d->nanagrps == -1 ? ", Ctrl-D to end" : "");
1287         rc = gmx_ana_selcollection_parse_stdin(d->sc, d->nanagrps, grps, TRUE);
1288         fprintf(stderr, "\n");
1289     }
1290     else if (bStdIn)
1291     {
1292         rc = gmx_ana_selcollection_parse_stdin(d->sc, -1, grps, FALSE);
1293     }
1294     else if (d->selection)
1295     {
1296         rc = gmx_ana_selcollection_parse_str(d->sc, d->selection, grps);
1297     }
1298     else
1299     {
1300         rc = gmx_ana_selcollection_parse_file(d->sc, d->selfile, grps);
1301     }
1302     if (grps)
1303     {
1304         gmx_ana_indexgrps_free(grps);
1305     }
1306     if (rc != 0)
1307     {
1308         /* Free memory for memory leak checking */
1309         gmx_ana_traj_free(d);
1310         gmx_input("selection(s) could not be parsed");
1311         return rc;
1312     }
1313
1314     /* Check the number of groups */
1315     nr = gmx_ana_selcollection_get_count(d->sc);
1316     if (nr == 0)
1317     {
1318         /* TODO: Don't print this if the user has requested help */
1319         fprintf(stderr, "Nothing selected, finishing up.\n");
1320         gmx_ana_traj_free(d);
1321         /* TODO: It would be better to return some code that tells the caller
1322          * that one should exit. */
1323         exit(0);
1324     }
1325     if (nr <= d->nrefgrps)
1326     {
1327         gmx_input("selection does not specify enough index groups");
1328         return -1;
1329     }
1330     if (d->nanagrps <= 0)
1331     {
1332         d->nanagrps = nr - d->nrefgrps;
1333     }
1334     else if (nr != d->nrefgrps + d->nanagrps)
1335     {
1336         gmx_input("selection does not specify the correct number of index groups");
1337         return -1;
1338     }
1339
1340     if (d->flags & ANA_DEBUG_SELECTION)
1341     {
1342         gmx_ana_selcollection_print_tree(stderr, d->sc, FALSE);
1343     }
1344     if (gmx_ana_selcollection_requires_top(d->sc))
1345     {
1346         rc = load_topology(d, TRUE);
1347         if (rc != 0)
1348         {
1349             return rc;
1350         }
1351     }
1352     if (d->top)
1353     {
1354         natoms = -1;
1355     }
1356     else
1357     {
1358         rc = init_first_frame(d);
1359         if (rc != 0)
1360         {
1361             return rc;
1362         }
1363         natoms = d->fr->natoms;
1364     }
1365     gmx_ana_selcollection_set_topology(d->sc, d->top, natoms);
1366     rc = gmx_ana_selcollection_compile(d->sc);
1367     if (rc != 0)
1368     {
1369         /* Free memory for memory leak checking */
1370         gmx_ana_traj_free(d);
1371         gmx_input("selection could not be compiled");
1372         return rc;
1373     }
1374     /* Create the selection array */
1375     d->ngrps = gmx_ana_selcollection_get_count(d->sc);
1376     if (!(d->flags & ANA_USE_FULLGRPS))
1377     {
1378         d->ngrps -= d->nrefgrps;
1379     }
1380     snew(d->sel, d->ngrps);
1381     for (i = 0; i < d->ngrps; ++i)
1382     {
1383         if (d->flags & ANA_USE_FULLGRPS)
1384         {
1385             d->sel[i] = gmx_ana_selcollection_get_selection(d->sc, i);
1386         }
1387         else
1388         {
1389             d->sel[i] = gmx_ana_selcollection_get_selection(d->sc, i + d->nrefgrps);
1390         }
1391     }
1392     if (d->flags & ANA_DEBUG_SELECTION)
1393     {
1394         fprintf(stderr, "\n");
1395         gmx_ana_selcollection_print_tree(stderr, d->sc, FALSE);
1396         fprintf(stderr, "\n");
1397         gmx_ana_poscalc_coll_print_tree(stderr, d->pcc);
1398         fprintf(stderr, "\n");
1399     }
1400
1401     /* Initialize the position evaluation */
1402     gmx_ana_poscalc_init_eval(d->pcc);
1403     if (d->flags & ANA_DEBUG_SELECTION)
1404     {
1405         gmx_ana_poscalc_coll_print_tree(stderr, d->pcc);
1406         fprintf(stderr, "\n");
1407     }
1408
1409     /* Check that dynamic selections are not provided if not allowed */
1410     if (d->flags & ANA_NO_DYNSEL)
1411     {
1412         for (i = 0; i < d->nrefgrps + d->nanagrps; ++i)
1413         {
1414             gmx_ana_selection_t *sel;
1415
1416             sel = gmx_ana_selcollection_get_selection(d->sc, i);
1417             if (sel->bDynamic)
1418             {
1419                 gmx_fatal(FARGS, "%s does not support dynamic selections",
1420                           ShortProgram());
1421                 return -1;
1422             }
1423         }
1424     }
1425     /* Check that non-atom positions are not provided if not allowed.
1426      * TODO: It would be better to have these checks in the parser. */
1427     if (d->flags & ANA_ONLY_ATOMPOS)
1428     {
1429         for (i = 0; i < d->nanagrps; ++i)
1430         {
1431             gmx_ana_selection_t *sel;
1432
1433             sel = gmx_ana_selcollection_get_selection(d->sc, i + d->nrefgrps);
1434             if (sel->p.m.type != INDEX_ATOM)
1435             {
1436                 gmx_fatal(FARGS, "%s does not support non-atom positions",
1437                           ShortProgram());
1438                 return -1;
1439             }
1440         }
1441     }
1442     /* Create the names array */
1443     snew(d->grpnames, d->ngrps);
1444     for (i = 0; i < d->ngrps; ++i)
1445     {
1446         d->grpnames[i] = gmx_ana_selection_name(d->sel[i]);
1447     }
1448
1449     return 0;
1450 }
1451
1452 /*!
1453  * \param[in,out] d       Trajectory analysis data structure.
1454  * \param[in]     type    Type of covered fractions to calculate.
1455  * \returns       0 on success, a non-zero error code on error.
1456  *
1457  * By default, covered fractions are not calculated.
1458  * If this function is called, the covered fraction calculation is
1459  * initialize to calculate the fractions of type \p type for each selection.
1460  * The function must be called after gmx_ana_init_selections() has been
1461  * called.
1462  *
1463  * For more fine-grained control of the calculation, you can use
1464  * gmx_ana_selection_init_coverfrac(): if you initialize some selections
1465  * this function to something else than CFRAC_NONE before calling
1466  * gmx_ana_init_coverfrac(), these settings are not overwritten.
1467  *
1468  * You only need to call this function if your program needs to have
1469  * information about the covered fractions, e.g., for normalization.
1470  *
1471  * \see gmx_ana_selection_init_coverfrac()
1472  */
1473 int
1474 gmx_ana_init_coverfrac(gmx_ana_traj_t *d, e_coverfrac_t type)
1475 {
1476     int                 g;
1477
1478     if (type == CFRAC_NONE)
1479     {
1480         return 0;
1481     }
1482
1483     for (g = 0; g < d->ngrps; ++g)
1484     {
1485         if (d->sel[g]->cfractype == CFRAC_NONE)
1486         {
1487             gmx_ana_selection_init_coverfrac(d->sel[g], type);
1488         }
1489     }
1490     return 0;
1491 }
1492
1493 /*!
1494  * \param[in] out  Output file.
1495  * \param[in] d    Trajectory analysis data structure.
1496  * \returns   0 on success, a non-zero error code on error.
1497  */
1498 int xvgr_selections(FILE *out, gmx_ana_traj_t *d)
1499 {
1500     xvgr_selcollection(out, d->sc, d->oenv);
1501     return 0;
1502 }
1503
1504 /*!
1505  * \param[in,out] d       Trajectory analysis data structure.
1506  * \returns       0 on success, a non-zero error code on error.
1507  */
1508 static int init_first_frame(gmx_ana_traj_t *d)
1509 {
1510     int                 i;
1511
1512     /* Return if we have already initialized the trajectory */
1513     if (d->fr)
1514     {
1515         return 0;
1516     }
1517
1518     d->frflags |= TRX_NEED_X;
1519
1520     snew(d->fr, 1);
1521
1522     if (d->trjfile)
1523     {
1524         if (!read_first_frame(d->oenv, &d->status, d->trjfile, d->fr, d->frflags))
1525         {
1526             gmx_input("could not read coordinates from trajectory");
1527             return EIO;
1528         }
1529
1530         if (d->top && d->fr->natoms > d->top->atoms.nr)
1531         {
1532             gmx_fatal(FARGS, "Trajectory (%d atoms) does not match topology (%d atoms)",
1533                       d->fr->natoms, d->top->atoms.nr);
1534             return -1;
1535         }
1536         /* check index groups */
1537         for (i = 0; i < d->ngrps; ++i)
1538         {
1539             gmx_ana_index_check(d->sel[i]->g, d->fr->natoms);
1540         }
1541     }
1542     else
1543     {
1544         /* Prepare a frame from topology information */
1545         /* TODO: Initialize more of the fields */
1546         if (d->frflags & (TRX_NEED_V))
1547         {
1548             gmx_impl("Velocity reading from a topology not implemented");
1549             return -1;
1550         }
1551         if (d->frflags & (TRX_NEED_F))
1552         {
1553             gmx_input("Forces cannot be read from a topology");
1554             return -1;
1555         }
1556         d->fr->flags  = d->frflags;
1557         d->fr->natoms = d->top->atoms.nr;
1558         d->fr->bX     = TRUE;
1559         snew(d->fr->x, d->fr->natoms);
1560         memcpy(d->fr->x, d->xtop, sizeof(*d->fr->x)*d->fr->natoms);
1561         d->fr->bBox   = TRUE;
1562         copy_mat(d->boxtop, d->fr->box);
1563     }
1564
1565     set_trxframe_ePBC(d->fr, d->ePBC);
1566
1567     return 0;
1568 }
1569
1570 /*!
1571  * \param[in,out] d       Trajectory analysis data structure.
1572  * \param[out]    fr      First frame in the trajectory.
1573  * \returns       0 on success, a non-zero error code on error.
1574  *
1575  * The pointer stored in \p *fr should not be freed by the caller.
1576  *
1577  * You only need to call this function if your program needs to do some
1578  * initialization for which it requires data from the first frame.
1579  *
1580  * \see gmx_ana_do()
1581  */
1582 int gmx_ana_get_first_frame(gmx_ana_traj_t *d, t_trxframe **fr)
1583 {
1584     int rc;
1585
1586     rc = init_first_frame(d);
1587     if (rc != 0)
1588     {
1589         *fr = NULL;
1590         return rc;
1591     }
1592     *fr = d->fr;
1593     return 0;
1594 }
1595
1596 /*!
1597  * \param[in,out] d   Trajectory analysis data structure.
1598  * \param[in] flags   Combination of flags
1599  *      (currently, there are no flags defined).
1600  * \param[in] analyze Pointer to frame analysis function.
1601  * \param     data    User data to be passed to \p analyze.
1602  * \returns   0 on success, a non-zero error code on error.
1603  *
1604  * This function performs the actual analysis of the trajectory.
1605  * It loops through all the frames in the trajectory, and calls
1606  * \p analyze for each frame to perform the actual analysis.
1607  * Before calling \p analyze, selections are updated (if there are any).
1608  * See gmx_analysisfunc() for description of \p analyze parameters.
1609  *
1610  * This function also calculates the number of frames during the run.
1611  */
1612 int gmx_ana_do(gmx_ana_traj_t *d, int flags, gmx_analysisfunc analyze, void *data)
1613 {
1614     t_pbc               pbc;
1615     t_pbc              *ppbc;
1616     int                 rc;
1617     gmx_rmpbc_t         gpbc=NULL;
1618     
1619     rc = init_first_frame(d);
1620     if (rc != 0)
1621     {
1622         return rc;
1623     }
1624
1625     ppbc = d->bPBC ? &pbc : 0;
1626     if (!d->top)
1627     {
1628         d->bRmPBC = FALSE;
1629     }
1630     if (d->bRmPBC) 
1631     {
1632         gpbc = gmx_rmpbc_init(&d->top->idef,d->ePBC,d->fr->natoms,d->fr->box);
1633     }
1634     d->nframes = 0;
1635     do
1636     {
1637         if (d->bRmPBC)
1638         {
1639             gmx_rmpbc_trxfr(gpbc,d->fr);
1640         }
1641         if (ppbc)
1642         {
1643             set_pbc(&pbc, d->ePBC, d->fr->box);
1644         }
1645
1646         gmx_ana_poscalc_init_frame(d->pcc);
1647         rc = gmx_ana_selcollection_evaluate(d->sc, d->fr, ppbc);
1648         if (rc != 0)
1649         {
1650             close_trj(d->status);
1651             gmx_fatal(FARGS, "selection evaluation failed");
1652             return rc;
1653         }
1654         rc = analyze(d->top, d->fr, ppbc, d->ngrps, d->sel, data);
1655         if (rc != 0)
1656         {
1657             close_trj(d->status);
1658             return rc;
1659         }
1660
1661         d->nframes++;
1662     }
1663     while (d->trjfile && read_next_frame(d->oenv, d->status, d->fr));
1664     if (d->bRmPBC)
1665     {
1666         gmx_rmpbc_done(gpbc);
1667     }
1668     if (d->trjfile)
1669     {
1670         close_trj(d->status);
1671         fprintf(stderr, "Analyzed %d frames, last time %.3f\n",
1672                 d->nframes, d->fr->time);
1673     }
1674     else
1675     {
1676         fprintf(stderr, "Analyzed topology coordinates\n");
1677     }
1678
1679     /* Restore the maximal groups for dynamic selections */
1680     rc = gmx_ana_selcollection_evaluate_fin(d->sc, d->nframes);
1681     if (rc != 0)
1682     {
1683         gmx_fatal(FARGS, "selection evaluation failed");
1684     }
1685
1686     return rc;
1687 }
1688
1689 /*!
1690  * \param[in,out] d       Trajectory analysis data structure.
1691  * \param[out]    nframes Number of frames.
1692  * \returns   0 on success, a non-zero error code on error.
1693  *
1694  * If called before gmx_ana_do(), the behavior is currently undefined.
1695  */
1696 extern int
1697 gmx_ana_get_nframes(gmx_ana_traj_t *d, int *nframes)
1698 {
1699     *nframes = d->nframes;
1700     return 0;
1701 }