3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
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.
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.
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.
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.
29 * For more info, check our website at http://www.gromacs.org
31 /*! \page libtrajana Library for trajectory analysis
33 * This is a trajectory analysis library for Gromacs.
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.
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.
57 * \section main_using Using the library
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.
65 * \section libtrajana_impl Implementation details
67 * Some internal implementation details of the library are documented on
69 * - \subpage poscalcengine
70 * - \subpage selparser
71 * - \subpage selcompiler
73 /*! \page selengine Text-based selections
75 * \section selection_basics Basics
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
83 * These defaults, as well as some others, can be changed by specifying
84 * flags for gmx_ana_traj_create().
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.
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
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.
102 * \section selection_methods Implementing new keywords
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.
112 * \brief Implementation of functions in trajana.h.
114 /*! \internal \dir src/gmxlib/trajana
115 * \brief Source code for common trajectory analysis functions.
117 * Selection handling is found in \ref src/gmxlib/selection.
131 #include <statutil.h>
132 #include <typedefs.h>
137 #include <selection.h>
138 #include <selmethod.h>
142 * Data structure for trajectory analysis tools.
144 struct gmx_ana_traj_t
147 * Flags that alter the behavior of the analysis library.
149 * This variable stores the flags passed to gmx_ana_traj_create() for use
150 * in the other functions.
153 /** Number of input reference groups. */
156 * Number of input analysis groups.
158 * This is the number of groups in addition to the reference groups
160 * If -1, any number of groups (at least one) is acceptable.
164 * Flags for init_first_frame() to specify what to read from the
168 /** TRUE if molecules should be made whole for each frame. */
171 * TRUE if periodic boundary conditions should be used.
173 * If the flag is FALSE, the \p pbc pointer passed to gmx_analysisfunc()
178 /** Name of the trajectory file (NULL if not provided). */
180 /** Name of the topology file (NULL if no topology loaded). */
182 /** Non-NULL name of the topology file. */
183 char *topfile_notnull
;
184 /** Name of the index file (NULL if no index file provided). */
186 /** Name of the selection file (NULL if no file provided). */
188 /** The selection string (NULL if not provided). */
191 /** The topology structure, or \p NULL if no topology loaded. */
193 /** TRUE if full tpx file was loaded, FALSE otherwise. */
195 /** Coordinates from the topology (see \p bTopX). */
197 /** The box loaded from the topology file. */
199 /** The ePBC field loaded from the topology file. */
202 /** The current frame, or \p NULL if no frame loaded yet. */
204 /** Used to store the status variable from read_first_frame(). */
206 /** The number of frames read. */
209 /** Number of elements in the \p sel array. */
212 * Array of selection information (one element for each index group).
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.
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.
225 gmx_ana_selection_t
**sel
;
226 /** Array of names of the selections for convenience. */
228 /** Position calculation data. */
229 gmx_ana_poscalc_coll_t
*pcc
;
230 /** Selection data. */
231 gmx_ana_selcollection_t
*sc
;
233 /** Data for statutil.c utilities. */
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
);
242 static int add_fnmarg(int nfile
, t_filenm
*fnm
, t_filenm
*fnm_add
)
244 memcpy(&(fnm
[nfile
]), fnm_add
, sizeof(*fnm_add
));
248 /* Copied from src/gmxlib/statutil.c */
250 add_parg(int npargs
, t_pargs
*pa
, t_pargs
*pa_add
)
252 memcpy(&(pa
[npargs
]), pa_add
, sizeof(*pa_add
));
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.
262 gmx_ana_traj_create(gmx_ana_traj_t
**data
, unsigned long flags
)
271 d
->frflags
= TRX_NEED_X
;
293 d
->topfile_notnull
= NULL
;
294 rc
= gmx_ana_poscalc_coll_create(&d
->pcc
);
301 rc
= gmx_ana_selcollection_create(&d
->sc
, d
->pcc
);
304 gmx_ana_poscalc_coll_free(d
->pcc
);
317 * \param d Trajectory analysis data to free.
320 gmx_ana_traj_free(gmx_ana_traj_t
*d
)
326 sfree(d
->topfile_notnull
);
336 /* Gromacs does not seem to have a function for freeing frame data */
344 gmx_ana_selcollection_free(d
->sc
);
345 gmx_ana_poscalc_coll_free(d
->pcc
);
347 output_env_done(d
->oenv
);
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.
356 * Currently there are no checks whether the flags make sense or not.
359 gmx_ana_add_flags(gmx_ana_traj_t
*d
, unsigned long flags
)
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.
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.
377 * If this function is not called, the default is to use PBC.
379 * If PBC are not used, the \p pbc pointer passed to gmx_analysisfunc()
382 * \see \ref ANA_NOUSER_PBC
385 gmx_ana_set_pbc(gmx_ana_traj_t
*d
, bool bPBC
)
392 * \param[in] d Trajectory analysis data structure.
393 * \returns TRUE if periodic boundary conditions are set to be used.
396 gmx_ana_has_pbc(gmx_ana_traj_t
*d
)
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.
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.
413 * If this function is not called, the default is to make molecules whole.
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
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
422 * \see \ref ANA_NOUSER_RMPBC
425 gmx_ana_set_rmpbc(gmx_ana_traj_t
*d
, bool bRmPBC
)
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.
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
442 gmx_ana_set_frflags(gmx_ana_traj_t
*d
, int frflags
)
446 gmx_call("cannot set trajectory flags after initializing selections");
451 gmx_call("cannot set trajectory flags after the first frame has been read");
454 frflags
|= TRX_NEED_X
;
455 d
->frflags
= frflags
;
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.
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.
469 gmx_ana_set_nrefgrps(gmx_ana_traj_t
*d
, int nrefgrps
)
474 gmx_incons("number of reference groups is negative");
477 d
->nrefgrps
= nrefgrps
;
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.
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.
494 gmx_ana_set_nanagrps(gmx_ana_traj_t
*d
, int nanagrps
)
496 if (nanagrps
<= 0 && nanagrps
!= -1)
499 gmx_incons("number of analysis groups is invalid");
502 d
->nanagrps
= nanagrps
;
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.
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().
517 * Should only be called after gmx_ana_init_selections().
520 gmx_ana_get_ngrps(gmx_ana_traj_t
*d
, int *ngrps
)
522 if (d
->nanagrps
== -1)
525 gmx_call("gmx_ana_init_selections() not called");
528 *ngrps
= d
->nrefgrps
+ d
->nanagrps
;
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.
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.
542 * Should only be called after gmx_ana_init_selections().
545 gmx_ana_get_nanagrps(gmx_ana_traj_t
*d
, int *nanagrps
)
547 if (d
->nanagrps
== -1)
550 gmx_call("gmx_ana_init_selections() not called");
553 *nanagrps
= d
->nanagrps
;
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.
563 * The pointer returned in \p *sel should not be freed.
564 * Should only be called after gmx_ana_init_selections().
567 gmx_ana_get_refsel(gmx_ana_traj_t
*d
, int i
, gmx_ana_selection_t
**sel
)
569 if (i
< 0 || i
>= d
->nrefgrps
)
572 gmx_call("invalid reference group number");
575 *sel
= gmx_ana_selcollection_get_selection(d
->sc
, i
);
578 gmx_incons("gmx_ana_init_selections() not called");
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.
589 * The pointer returned in \p *sel should not be freed.
590 * Should only be called after gmx_ana_init_selections().
593 gmx_ana_get_anagrps(gmx_ana_traj_t
*d
, gmx_ana_selection_t
***sel
)
598 gmx_incons("gmx_ana_init_selections() not called");
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.
610 * The pointer returned in \p *grpnames should not be freed.
611 * Should only be called after gmx_ana_init_selections().
614 gmx_ana_get_grpnames(gmx_ana_traj_t
*d
, char ***grpnames
)
619 gmx_call("gmx_ana_init_selections() not called");
622 *grpnames
= d
->grpnames
;
627 * \param[in] d Trajectory analysis data structure.
628 * \param[out] sc Selection collection object.
629 * \returns 0 on success.
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.
635 * The pointer returned in \p *sc should not be freed.
636 * Can be called at any point.
639 gmx_ana_get_selcollection(gmx_ana_traj_t
*d
, gmx_ana_selcollection_t
**sc
)
646 * \param[in,out] d Trajectory analysis data structure.
647 * \returns 0 on success, a non-zero error code on error.
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.
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.
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
,
683 t_filenm
*all_fnm
= NULL
;
686 t_pargs
*all_pa
= NULL
;
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
},
701 {"-pbc", FALSE
, etBOOL
, {&bPBC
},
702 "Use periodic boundary conditions for distance calculation"},
705 t_pargs rmpbc_pa
[] = {
706 {"-rmpbc", FALSE
, etBOOL
, {&bRmPBC
},
707 "Make molecules whole for each frame"},
709 char *selection
= NULL
;
710 const char **rpost
= NULL
;
711 bool bSelDump
= FALSE
;
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"},
718 t_pargs dsel_pa
[] = {
719 {"-selrpos", FALSE
, etENUM
, {NULL
},
720 "Selection reference position"},
722 const char **spost
= NULL
;
723 t_pargs selpt_pa
[] = {
724 {"-seltype", FALSE
, etENUM
, {NULL
},
725 "Default analysis positions"},
727 #define MAX_PA asize(sel_pa)+asize(dsel_pa)+5
731 gmx_incons("number of reference groups is negative");
735 if (d
->flags
& ANA_DEBUG_SELECTION
)
740 rpost
= gmx_ana_poscalc_create_type_enum(!(d
->flags
& ANA_REQUIRE_WHOLE
));
745 spost
= gmx_ana_poscalc_create_type_enum(TRUE
);
752 /* Construct the file name argument array */
753 max_fnm
= nfile
+ asize(def_fnm
);
754 snew(all_fnm
, max_fnm
);
756 if (!(d
->flags
& ANA_REQUIRE_TOP
))
758 def_fnm
[1].flag
|= ffOPT
;
760 snew(fnm_map
, nfile
);
761 for (k
= 0; k
< nfile
; ++k
)
766 for (i
= 0; i
< asize(def_fnm
); ++i
)
768 for (k
= 0; k
< nfile
; ++k
)
770 if (fnm_map
[k
] == -1 && def_fnm
[i
].opt
== NULL
771 && fnm
[k
].opt
== NULL
&& fnm
[k
].ftp
== def_fnm
[i
].ftp
)
779 nfall
= add_fnmarg(nfall
, all_fnm
, &(fnm
[k
]));
783 nfall
= add_fnmarg(nfall
, all_fnm
, &(def_fnm
[i
]));
787 for (k
= 0; k
< nfile
; ++k
)
789 if (fnm_map
[k
] == -1)
792 nfall
= add_fnmarg(nfall
, all_fnm
, &(fnm
[k
]));
796 /* Construct the argument array */
797 max_pa
= npargs
+ MAX_PA
;
798 snew(all_pa
, max_pa
);
801 if (!(d
->flags
& ANA_NOUSER_RMPBC
))
803 for (i
= 0; i
< asize(rmpbc_pa
); ++i
)
805 npall
= add_parg(npall
, all_pa
, &(rmpbc_pa
[i
]));
808 if (!(d
->flags
& ANA_NOUSER_PBC
))
810 for (i
= 0; i
< asize(pbc_pa
); ++i
)
812 npall
= add_parg(npall
, all_pa
, &(pbc_pa
[i
]));
816 for (i
= 0; i
< asize(sel_pa
); ++i
)
818 npall
= add_parg(npall
, all_pa
, &(sel_pa
[i
]));
820 if (!(d
->flags
& ANA_NO_DYNSEL
))
822 dsel_pa
[0].u
.c
= rpost
;
823 for (i
= 0; i
< asize(dsel_pa
); ++i
)
825 npall
= add_parg(npall
, all_pa
, &(dsel_pa
[i
]));
829 if (!(d
->flags
& ANA_ONLY_ATOMPOS
))
831 selpt_pa
[0].u
.c
= spost
;
832 for (i
= 0; i
< asize(selpt_pa
); ++i
)
834 npall
= add_parg(npall
, all_pa
, &(selpt_pa
[i
]));
838 for (k
= 0; k
< npargs
; ++k
)
840 npall
= add_parg(npall
, all_pa
, &(pa
[k
]));
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
);
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
))
861 if (!(d
->flags
& ANA_NOUSER_PBC
))
865 d
->selection
= selection
;
866 tmp_fnm
= opt2fn_null("-sf", nfall
, all_fnm
);
867 d
->selfile
= tmp_fnm
? strdup(tmp_fnm
) : NULL
;
869 /* Copy the results back */
870 for (k
= 0; k
< nfile
; ++k
)
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
;
877 for (i
= 0, k
= npall
- npargs
; i
< (size_t)npargs
; ++i
, ++k
)
879 memcpy(&(pa
[i
]), &(all_pa
[k
]), sizeof(pa
[i
]));
882 /* Free memory we have allocated. */
883 done_filenms(nfall
, all_fnm
);
888 if (!(d
->flags
& ANA_NO_DYNSEL
))
890 gmx_ana_selcollection_set_refpostype(d
->sc
, rpost
[0]);
894 gmx_ana_selcollection_set_refpostype(d
->sc
, rpost
[1]);
899 d
->flags
|= ANA_DEBUG_SELECTION
;
903 d
->flags
&= ~ANA_DEBUG_SELECTION
;
906 if (!(d
->flags
& ANA_ONLY_ATOMPOS
))
908 gmx_ana_selcollection_set_outpostype(d
->sc
, spost
[0], d
->flags
& ANA_USE_POSMASK
);
912 gmx_ana_selcollection_set_outpostype(d
->sc
, spost
[1], d
->flags
& ANA_USE_POSMASK
);
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)
920 gmx_ana_init_selections(d
);
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. */
930 gmx_input("No trajectory or topology provided, nothing to do!");
933 d
->flags
|= ANA_REQUIRE_TOP
;
934 d
->flags
|= ANA_USE_TOPX
;
937 /* Load the topology if so requested. */
938 rc
= load_topology(d
, (d
->flags
& ANA_REQUIRE_TOP
));
944 /* Initialize the selections/index groups */
945 if (!(d
->flags
& ANA_USER_SELINIT
))
947 rc
= gmx_ana_init_selections(d
);
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.
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
964 * The function can be called multiple times safely; extra calls are
967 static int load_topology(gmx_ana_traj_t
*d
, bool bReq
)
971 /* Return if topology already loaded */
977 if (d
->topfile
|| bReq
)
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
))
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.
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.
1003 * The pointer returned in \p *top should not be freed.
1006 gmx_ana_get_topology(gmx_ana_traj_t
*d
, bool bReq
, t_topology
**top
, bool *bTop
)
1010 rc
= load_topology(d
, bReq
);
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.
1034 * If \ref ANA_USE_TOPX has not been specified, the \p x parameter should be
1037 * The pointer returned in \p *x should not be freed.
1040 gmx_ana_get_topconf(gmx_ana_traj_t
*d
, rvec
**x
, matrix box
, int *ePBC
)
1044 copy_mat(d
->boxtop
, box
);
1052 if (!(d
->flags
& ANA_USE_TOPX
))
1054 gmx_incons("topology coordinates requested by ANA_USE_TOPX not set");
1064 * Loads default index groups from a selection file.
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.
1071 init_default_selections(gmx_ana_traj_t
*d
, gmx_ana_indexgrps_t
**grps
)
1073 gmx_ana_selcollection_t
*sc
;
1075 int nr
, nr_notempty
, i
;
1078 /* If an index file is provided, just load it and exit. */
1081 gmx_ana_indexgrps_init(grps
, d
->top
, d
->ndxfile
);
1084 /* Initialize groups to NULL if we return prematurely. */
1086 /* Return immediately if no topology provided. */
1092 /* Find the default selection file, return if none found. */
1093 fnm
= low_gmxlibfn("defselection.dat", FALSE
);
1099 /* Create a temporary selection collection. */
1100 rc
= gmx_ana_selcollection_create(&sc
, d
->pcc
);
1106 rc
= gmx_ana_selmethod_register_defaults(sc
);
1109 gmx_ana_selcollection_free(sc
);
1111 gmx_fatal(FARGS
, "default selection method registration failed");
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
);
1118 /* Parse and compile the file with no external groups. */
1119 rc
= gmx_ana_selcollection_parse_file(sc
, fnm
, NULL
);
1123 gmx_ana_selcollection_free(sc
);
1124 fprintf(stderr
, "\nWARNING: default selection(s) could not be parsed\n");
1127 gmx_ana_selcollection_set_topology(sc
, d
->top
, -1);
1128 rc
= gmx_ana_selcollection_compile(sc
);
1131 gmx_ana_selcollection_free(sc
);
1132 fprintf(stderr
, "\nWARNING: default selection(s) could not be compiled\n");
1136 /* Count the non-empty groups and check that there are no dynamic
1138 nr
= gmx_ana_selcollection_get_count(sc
);
1140 for (i
= 0; i
< nr
; ++i
)
1142 gmx_ana_selection_t
*sel
;
1144 sel
= gmx_ana_selcollection_get_selection(sc
, i
);
1147 fprintf(stderr
, "\nWARNING: dynamic default selection ignored\n");
1149 else if (sel
->g
->isize
> 0)
1155 /* Copy the groups to the output structure */
1156 gmx_ana_indexgrps_alloc(grps
, nr_notempty
);
1158 for (i
= 0; i
< nr
; ++i
)
1160 gmx_ana_selection_t
*sel
;
1162 sel
= gmx_ana_selcollection_get_selection(sc
, i
);
1163 if (!sel
->bDynamic
&& sel
->g
->isize
> 0)
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
);
1174 gmx_ana_selcollection_free(sc
);
1179 * \param[in,out] d Trajectory analysis data structure.
1180 * \returns 0 on success, a non-zero error code on error.
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.
1185 * This function is called automatically by parse_trjana_args() and should
1186 * not be called directly unless \ref ANA_USER_SELINIT is specified.
1188 * \see ANA_USER_SELINIT
1191 gmx_ana_init_selections(gmx_ana_traj_t
*d
)
1196 gmx_ana_indexgrps_t
*grps
;
1204 gmx_call("init_selections called more than once\n"
1205 "perhaps you forgot ANA_USER_SELINIT");
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
))
1216 rc
= load_topology(d
, TRUE
);
1222 /* Initialize the default selection methods */
1223 rc
= gmx_ana_selmethod_register_defaults(d
->sc
);
1226 gmx_fatal(FARGS
, "default selection method registration failed");
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
));
1242 bInteractive
= bStdIn
;
1244 if (bStdIn
&& bInteractive
)
1246 /* Parse from stdin */
1247 /* First we parse the reference groups if there are any */
1248 if (d
->nrefgrps
> 0)
1250 fprintf(stderr
, "\nSpecify ");
1251 if (d
->nrefgrps
== 1)
1253 fprintf(stderr
, "a reference selection");
1257 fprintf(stderr
, "%d reference selections", d
->nrefgrps
);
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
)
1265 gmx_ana_traj_free(d
);
1266 gmx_input("unrecoverable error in selection parsing");
1270 /* Then, we parse the analysis groups */
1271 fprintf(stderr
, "\nSpecify ");
1272 if (d
->nanagrps
== 1)
1274 fprintf(stderr
, "a selection");
1276 else if (d
->nanagrps
== -1)
1278 fprintf(stderr
, "any number of selections");
1282 fprintf(stderr
, "%d selections", d
->nanagrps
);
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");
1292 rc
= gmx_ana_selcollection_parse_stdin(d
->sc
, -1, grps
, FALSE
);
1294 else if (d
->selection
)
1296 rc
= gmx_ana_selcollection_parse_str(d
->sc
, d
->selection
, grps
);
1300 rc
= gmx_ana_selcollection_parse_file(d
->sc
, d
->selfile
, grps
);
1304 gmx_ana_indexgrps_free(grps
);
1308 /* Free memory for memory leak checking */
1309 gmx_ana_traj_free(d
);
1310 gmx_input("selection(s) could not be parsed");
1314 /* Check the number of groups */
1315 nr
= gmx_ana_selcollection_get_count(d
->sc
);
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. */
1325 if (nr
<= d
->nrefgrps
)
1327 gmx_input("selection does not specify enough index groups");
1330 if (d
->nanagrps
<= 0)
1332 d
->nanagrps
= nr
- d
->nrefgrps
;
1334 else if (nr
!= d
->nrefgrps
+ d
->nanagrps
)
1336 gmx_input("selection does not specify the correct number of index groups");
1340 if (d
->flags
& ANA_DEBUG_SELECTION
)
1342 gmx_ana_selcollection_print_tree(stderr
, d
->sc
, FALSE
);
1344 if (gmx_ana_selcollection_requires_top(d
->sc
))
1346 rc
= load_topology(d
, TRUE
);
1358 rc
= init_first_frame(d
);
1363 natoms
= d
->fr
->natoms
;
1365 gmx_ana_selcollection_set_topology(d
->sc
, d
->top
, natoms
);
1366 rc
= gmx_ana_selcollection_compile(d
->sc
);
1369 /* Free memory for memory leak checking */
1370 gmx_ana_traj_free(d
);
1371 gmx_input("selection could not be compiled");
1374 /* Create the selection array */
1375 d
->ngrps
= gmx_ana_selcollection_get_count(d
->sc
);
1376 if (!(d
->flags
& ANA_USE_FULLGRPS
))
1378 d
->ngrps
-= d
->nrefgrps
;
1380 snew(d
->sel
, d
->ngrps
);
1381 for (i
= 0; i
< d
->ngrps
; ++i
)
1383 if (d
->flags
& ANA_USE_FULLGRPS
)
1385 d
->sel
[i
] = gmx_ana_selcollection_get_selection(d
->sc
, i
);
1389 d
->sel
[i
] = gmx_ana_selcollection_get_selection(d
->sc
, i
+ d
->nrefgrps
);
1392 if (d
->flags
& ANA_DEBUG_SELECTION
)
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");
1401 /* Initialize the position evaluation */
1402 gmx_ana_poscalc_init_eval(d
->pcc
);
1403 if (d
->flags
& ANA_DEBUG_SELECTION
)
1405 gmx_ana_poscalc_coll_print_tree(stderr
, d
->pcc
);
1406 fprintf(stderr
, "\n");
1409 /* Check that dynamic selections are not provided if not allowed */
1410 if (d
->flags
& ANA_NO_DYNSEL
)
1412 for (i
= 0; i
< d
->nrefgrps
+ d
->nanagrps
; ++i
)
1414 gmx_ana_selection_t
*sel
;
1416 sel
= gmx_ana_selcollection_get_selection(d
->sc
, i
);
1419 gmx_fatal(FARGS
, "%s does not support dynamic selections",
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
)
1429 for (i
= 0; i
< d
->nanagrps
; ++i
)
1431 gmx_ana_selection_t
*sel
;
1433 sel
= gmx_ana_selcollection_get_selection(d
->sc
, i
+ d
->nrefgrps
);
1434 if (sel
->p
.m
.type
!= INDEX_ATOM
)
1436 gmx_fatal(FARGS
, "%s does not support non-atom positions",
1442 /* Create the names array */
1443 snew(d
->grpnames
, d
->ngrps
);
1444 for (i
= 0; i
< d
->ngrps
; ++i
)
1446 d
->grpnames
[i
] = gmx_ana_selection_name(d
->sel
[i
]);
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.
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
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.
1468 * You only need to call this function if your program needs to have
1469 * information about the covered fractions, e.g., for normalization.
1471 * \see gmx_ana_selection_init_coverfrac()
1474 gmx_ana_init_coverfrac(gmx_ana_traj_t
*d
, e_coverfrac_t type
)
1478 if (type
== CFRAC_NONE
)
1483 for (g
= 0; g
< d
->ngrps
; ++g
)
1485 if (d
->sel
[g
]->cfractype
== CFRAC_NONE
)
1487 gmx_ana_selection_init_coverfrac(d
->sel
[g
], type
);
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.
1498 int xvgr_selections(FILE *out
, gmx_ana_traj_t
*d
)
1500 xvgr_selcollection(out
, d
->sc
, d
->oenv
);
1505 * \param[in,out] d Trajectory analysis data structure.
1506 * \returns 0 on success, a non-zero error code on error.
1508 static int init_first_frame(gmx_ana_traj_t
*d
)
1512 /* Return if we have already initialized the trajectory */
1518 d
->frflags
|= TRX_NEED_X
;
1524 if (!read_first_frame(d
->oenv
, &d
->status
, d
->trjfile
, d
->fr
, d
->frflags
))
1526 gmx_input("could not read coordinates from trajectory");
1530 if (d
->top
&& d
->fr
->natoms
> d
->top
->atoms
.nr
)
1532 gmx_fatal(FARGS
, "Trajectory (%d atoms) does not match topology (%d atoms)",
1533 d
->fr
->natoms
, d
->top
->atoms
.nr
);
1536 /* check index groups */
1537 for (i
= 0; i
< d
->ngrps
; ++i
)
1539 gmx_ana_index_check(d
->sel
[i
]->g
, d
->fr
->natoms
);
1544 /* Prepare a frame from topology information */
1545 /* TODO: Initialize more of the fields */
1546 if (d
->frflags
& (TRX_NEED_V
))
1548 gmx_impl("Velocity reading from a topology not implemented");
1551 if (d
->frflags
& (TRX_NEED_F
))
1553 gmx_input("Forces cannot be read from a topology");
1556 d
->fr
->flags
= d
->frflags
;
1557 d
->fr
->natoms
= d
->top
->atoms
.nr
;
1559 snew(d
->fr
->x
, d
->fr
->natoms
);
1560 memcpy(d
->fr
->x
, d
->xtop
, sizeof(*d
->fr
->x
)*d
->fr
->natoms
);
1562 copy_mat(d
->boxtop
, d
->fr
->box
);
1565 set_trxframe_ePBC(d
->fr
, d
->ePBC
);
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.
1575 * The pointer stored in \p *fr should not be freed by the caller.
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.
1582 int gmx_ana_get_first_frame(gmx_ana_traj_t
*d
, t_trxframe
**fr
)
1586 rc
= init_first_frame(d
);
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.
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.
1610 * This function also calculates the number of frames during the run.
1612 int gmx_ana_do(gmx_ana_traj_t
*d
, int flags
, gmx_analysisfunc analyze
, void *data
)
1618 rc
= init_first_frame(d
);
1624 ppbc
= d
->bPBC
? &pbc
: 0;
1635 rm_pbc(&d
->top
->idef
, d
->ePBC
, d
->fr
->natoms
, d
->fr
->box
,
1636 d
->fr
->x
, d
->fr
->x
);
1640 set_pbc(&pbc
, d
->ePBC
, d
->fr
->box
);
1643 gmx_ana_poscalc_init_frame(d
->pcc
);
1644 rc
= gmx_ana_selcollection_evaluate(d
->sc
, d
->fr
, ppbc
);
1647 close_trj(d
->status
);
1648 gmx_fatal(FARGS
, "selection evaluation failed");
1651 rc
= analyze(d
->top
, d
->fr
, ppbc
, d
->ngrps
, d
->sel
, data
);
1654 close_trj(d
->status
);
1660 while (d
->trjfile
&& read_next_frame(d
->oenv
, d
->status
, d
->fr
));
1664 close_trj(d
->status
);
1665 fprintf(stderr
, "Analyzed %d frames, last time %.3f\n",
1666 d
->nframes
, d
->fr
->time
);
1670 fprintf(stderr
, "Analyzed topology coordinates\n");
1673 /* Restore the maximal groups for dynamic selections */
1674 rc
= gmx_ana_selcollection_evaluate_fin(d
->sc
, d
->nframes
);
1677 gmx_fatal(FARGS
, "selection evaluation failed");
1684 * \param[in,out] d Trajectory analysis data structure.
1685 * \param[out] nframes Number of frames.
1686 * \returns 0 on success, a non-zero error code on error.
1688 * If called before gmx_ana_do(), the behavior is currently undefined.
1691 gmx_ana_get_nframes(gmx_ana_traj_t
*d
, int *nframes
)
1693 *nframes
= d
->nframes
;