minor fixes in ditribution files
[gromacs/qmmm-gamess-us.git] / src / gmxlib / trajana / trajana.c
blob950b4e16eee1386128b00f6e5ba70704e619824e
1 /*
3 * This source code is part of
5 * G R O M A C S
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.
64 * \internal
65 * \section libtrajana_impl Implementation details
67 * Some internal implementation details of the library are documented on
68 * separate pages:
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
82 * residues/molecules.
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
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.
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.
111 /*! \internal \file
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.
119 #ifdef HAVE_CONFIG_H
120 #include <config.h>
121 #endif
123 #include <string.h>
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>
136 #include <poscalc.h>
137 #include <selection.h>
138 #include <selmethod.h>
139 #include <trajana.h>
141 /*! \internal \brief
142 * Data structure for trajectory analysis tools.
144 struct gmx_ana_traj_t
146 /*! \brief
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.
152 unsigned long flags;
153 /** Number of input reference groups. */
154 int nrefgrps;
155 /*! \brief
156 * Number of input analysis groups.
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.
162 int nanagrps;
163 /*! \brief
164 * Flags for init_first_frame() to specify what to read from the
165 * trajectory.
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.
173 * If the flag is FALSE, the \p pbc pointer passed to gmx_analysisfunc()
174 * is NULL.
176 bool bPBC;
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;
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;
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 int status;
206 /** The number of frames read. */
207 int nframes;
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).
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. */
227 char **grpnames;
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. */
234 output_env_t oenv;
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));
245 return nfile + 1;
248 /* Copied from src/gmxlib/statutil.c */
249 static int
250 add_parg(int npargs, t_pargs *pa, t_pargs *pa_add)
252 memcpy(&(pa[npargs]), pa_add, sizeof(*pa_add));
253 return npargs + 1;
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)
264 gmx_ana_traj_t *d;
265 int rc;
267 snew(d, 1);
269 d->nrefgrps = 0;
270 d->nanagrps = 1;
271 d->frflags = TRX_NEED_X;
272 d->bRmPBC = TRUE;
273 d->bPBC = TRUE;
275 d->trjfile = NULL;
276 d->topfile = NULL;
277 d->ndxfile = NULL;
278 d->selfile = NULL;
279 d->selection = NULL;
281 d->top = NULL;
282 d->bTop = FALSE;
283 d->xtop = NULL;
284 d->ePBC = -1;
285 d->fr = NULL;
286 d->nframes = 0;
288 d->ngrps = 0;
289 d->sel = NULL;
290 d->grpnames = NULL;
292 d->flags = flags;
293 d->topfile_notnull = NULL;
294 rc = gmx_ana_poscalc_coll_create(&d->pcc);
295 if (rc != 0)
297 sfree(d);
298 *data = NULL;
299 return rc;
301 rc = gmx_ana_selcollection_create(&d->sc, d->pcc);
302 if (rc != 0)
304 gmx_ana_poscalc_coll_free(d->pcc);
305 sfree(d);
306 *data = NULL;
307 return rc;
309 d->status = -1;
310 d->oenv = NULL;
312 *data = d;
313 return 0;
317 * \param d Trajectory analysis data to free.
319 void
320 gmx_ana_traj_free(gmx_ana_traj_t *d)
322 int i;
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)
331 done_top(d->top);
332 sfree(d->top);
334 if (d->fr)
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);
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);
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)
361 d->flags |= flags;
362 return 0;
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()
380 * is NULL.
382 * \see \ref ANA_NOUSER_PBC
385 gmx_ana_set_pbc(gmx_ana_traj_t *d, bool bPBC)
387 d->bPBC = bPBC;
388 return 0;
392 * \param[in] d Trajectory analysis data structure.
393 * \returns TRUE if periodic boundary conditions are set to be used.
395 bool
396 gmx_ana_has_pbc(gmx_ana_traj_t *d)
398 return d->bPBC;
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
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.
422 * \see \ref ANA_NOUSER_RMPBC
425 gmx_ana_set_rmpbc(gmx_ana_traj_t *d, bool bRmPBC)
427 d->bRmPBC = bRmPBC;
428 return 0;
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
439 * trajectory.
442 gmx_ana_set_frflags(gmx_ana_traj_t *d, int frflags)
444 if (d->sel)
446 gmx_call("cannot set trajectory flags after initializing selections");
447 return -1;
449 if (d->fr)
451 gmx_call("cannot set trajectory flags after the first frame has been read");
452 return -1;
454 frflags |= TRX_NEED_X;
455 d->frflags = frflags;
456 return 0;
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)
471 if (nrefgrps < 0)
473 d->nrefgrps = 0;
474 gmx_incons("number of reference groups is negative");
475 return EINVAL;
477 d->nrefgrps = nrefgrps;
478 return 0;
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)
498 d->nanagrps = 1;
499 gmx_incons("number of analysis groups is invalid");
500 return EINVAL;
502 d->nanagrps = nanagrps;
503 return 0;
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)
524 *ngrps = 0;
525 gmx_call("gmx_ana_init_selections() not called");
526 return EINVAL;
528 *ngrps = d->nrefgrps + d->nanagrps;
529 return 0;
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)
549 *nanagrps = 0;
550 gmx_call("gmx_ana_init_selections() not called");
551 return EINVAL;
553 *nanagrps = d->nanagrps;
554 return 0;
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)
571 *sel = NULL;
572 gmx_call("invalid reference group number");
573 return EINVAL;
575 *sel = gmx_ana_selcollection_get_selection(d->sc, i);
576 if (!*sel)
578 gmx_incons("gmx_ana_init_selections() not called");
579 return EINVAL;
581 return 0;
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)
595 if (!d->sel)
597 *sel = NULL;
598 gmx_incons("gmx_ana_init_selections() not called");
599 return EINVAL;
601 *sel = d->sel;
602 return 0;
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)
616 if (!d->grpnames)
618 *grpnames = NULL;
619 gmx_call("gmx_ana_init_selections() not called");
620 return EINVAL;
622 *grpnames = d->grpnames;
623 return 0;
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)
641 *sc = d->sc;
642 return 0;
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.
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
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)
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;
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},
699 bool bPBC = TRUE;
700 t_pargs pbc_pa[] = {
701 {"-pbc", FALSE, etBOOL, {&bPBC},
702 "Use periodic boundary conditions for distance calculation"},
704 bool bRmPBC = TRUE;
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;
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"},
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
729 if (d->nrefgrps < 0)
731 gmx_incons("number of reference groups is negative");
732 return EINVAL;
735 if (d->flags & ANA_DEBUG_SELECTION)
737 bSelDump = TRUE;
740 rpost = gmx_ana_poscalc_create_type_enum(!(d->flags & ANA_REQUIRE_WHOLE));
741 if (rpost == NULL)
743 return ENOMEM;
745 spost = gmx_ana_poscalc_create_type_enum(TRUE);
746 if (spost == NULL)
748 sfree(rpost);
749 return ENOMEM;
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))
758 def_fnm[1].flag |= ffOPT;
760 snew(fnm_map, nfile);
761 for (k = 0; k < nfile; ++k)
763 fnm_map[k] = -1;
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)
773 break;
776 if (k < nfile)
778 fnm_map[k] = nfall;
779 nfall = add_fnmarg(nfall, all_fnm, &(fnm[k]));
781 else
783 nfall = add_fnmarg(nfall, all_fnm, &(def_fnm[i]));
787 for (k = 0; k < nfile; ++k)
789 if (fnm_map[k] == -1)
791 fnm_map[k] = nfall;
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);
799 npall = 0;
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);
846 d->oenv = *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))
859 d->bRmPBC = bRmPBC;
861 if (!(d->flags & ANA_NOUSER_PBC))
863 d->bPBC = bPBC;
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);
884 sfree(all_fnm);
885 sfree(fnm_map);
886 sfree(all_pa);
888 if (!(d->flags & ANA_NO_DYNSEL))
890 gmx_ana_selcollection_set_refpostype(d->sc, rpost[0]);
892 else
894 gmx_ana_selcollection_set_refpostype(d->sc, rpost[1]);
896 sfree(rpost);
897 if (bSelDump)
899 d->flags |= ANA_DEBUG_SELECTION;
901 else
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);
910 else
912 gmx_ana_selcollection_set_outpostype(d->sc, spost[1], d->flags & ANA_USE_POSMASK);
914 sfree(spost);
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. */
926 if (!d->trjfile)
928 if (!d->topfile)
930 gmx_input("No trajectory or topology provided, nothing to do!");
931 return -1;
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));
939 if (rc != 0)
941 return rc;
944 /* Initialize the selections/index groups */
945 if (!(d->flags & ANA_USER_SELINIT))
947 rc = gmx_ana_init_selections(d);
950 return rc;
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
962 * the command line.
964 * The function can be called multiple times safely; extra calls are
965 * ignored.
967 static int load_topology(gmx_ana_traj_t *d, bool bReq)
969 char title[STRLEN];
971 /* Return if topology already loaded */
972 if (d->top)
974 return 0;
977 if (d->topfile || bReq)
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))
984 sfree(d->xtop);
985 d->xtop = NULL;
988 return 0;
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)
1008 int rc;
1010 rc = load_topology(d, bReq);
1011 if (rc != 0)
1013 *top = NULL;
1014 return rc;
1016 *top = d->top;
1017 if (bTop)
1019 *bTop = d->bTop;
1021 return 0;
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
1035 * NULL.
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)
1042 if (box)
1044 copy_mat(d->boxtop, box);
1046 if (ePBC)
1048 *ePBC = d->ePBC;
1050 if (x)
1052 if (!(d->flags & ANA_USE_TOPX))
1054 gmx_incons("topology coordinates requested by ANA_USE_TOPX not set");
1055 *x = NULL;
1056 return EINVAL;
1058 *x = d->xtop;
1060 return 0;
1063 /*! \brief
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.
1070 static int
1071 init_default_selections(gmx_ana_traj_t *d, gmx_ana_indexgrps_t **grps)
1073 gmx_ana_selcollection_t *sc;
1074 char *fnm;
1075 int nr, nr_notempty, i;
1076 int rc;
1078 /* If an index file is provided, just load it and exit. */
1079 if (d->ndxfile)
1081 gmx_ana_indexgrps_init(grps, d->top, d->ndxfile);
1082 return 0;
1084 /* Initialize groups to NULL if we return prematurely. */
1085 *grps = NULL;
1086 /* Return immediately if no topology provided. */
1087 if (!d->top)
1089 return 0;
1092 /* Find the default selection file, return if none found. */
1093 fnm = low_gmxlibfn("defselection.dat", FALSE);
1094 if (fnm == NULL)
1096 return 0;
1099 /* Create a temporary selection collection. */
1100 rc = gmx_ana_selcollection_create(&sc, d->pcc);
1101 if (rc != 0)
1103 sfree(fnm);
1104 return rc;
1106 rc = gmx_ana_selmethod_register_defaults(sc);
1107 if (rc != 0)
1109 gmx_ana_selcollection_free(sc);
1110 sfree(fnm);
1111 gmx_fatal(FARGS, "default selection method registration failed");
1112 return rc;
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);
1120 sfree(fnm);
1121 if (rc != 0)
1123 gmx_ana_selcollection_free(sc);
1124 fprintf(stderr, "\nWARNING: default selection(s) could not be parsed\n");
1125 return rc;
1127 gmx_ana_selcollection_set_topology(sc, d->top, -1);
1128 rc = gmx_ana_selcollection_compile(sc);
1129 if (rc != 0)
1131 gmx_ana_selcollection_free(sc);
1132 fprintf(stderr, "\nWARNING: default selection(s) could not be compiled\n");
1133 return rc;
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)
1142 gmx_ana_selection_t *sel;
1144 sel = gmx_ana_selcollection_get_selection(sc, i);
1145 if (sel->bDynamic)
1147 fprintf(stderr, "\nWARNING: dynamic default selection ignored\n");
1149 else if (sel->g->isize > 0)
1151 ++nr_notempty;
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)
1160 gmx_ana_selection_t *sel;
1162 sel = gmx_ana_selcollection_get_selection(sc, i);
1163 if (!sel->bDynamic && sel->g->isize > 0)
1165 gmx_ana_index_t *g;
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;
1174 gmx_ana_selcollection_free(sc);
1175 return 0;
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)
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;
1202 if (d->sel)
1204 gmx_call("init_selections called more than once\n"
1205 "perhaps you forgot ANA_USER_SELINIT");
1206 return -1;
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);
1217 if (rc != 0)
1219 return rc;
1222 /* Initialize the default selection methods */
1223 rc = gmx_ana_selmethod_register_defaults(d->sc);
1224 if (rc != 0)
1226 gmx_fatal(FARGS, "default selection method registration failed");
1227 return rc;
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)
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");
1255 else
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");
1267 return rc;
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");
1280 else
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");
1290 else if (bStdIn)
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);
1298 else
1300 rc = gmx_ana_selcollection_parse_file(d->sc, d->selfile, grps);
1302 if (grps)
1304 gmx_ana_indexgrps_free(grps);
1306 if (rc != 0)
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;
1314 /* Check the number of groups */
1315 nr = gmx_ana_selcollection_get_count(d->sc);
1316 if (nr == 0)
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);
1325 if (nr <= d->nrefgrps)
1327 gmx_input("selection does not specify enough index groups");
1328 return -1;
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");
1337 return -1;
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);
1347 if (rc != 0)
1349 return rc;
1352 if (d->top)
1354 natoms = -1;
1356 else
1358 rc = init_first_frame(d);
1359 if (rc != 0)
1361 return rc;
1363 natoms = d->fr->natoms;
1365 gmx_ana_selcollection_set_topology(d->sc, d->top, natoms);
1366 rc = gmx_ana_selcollection_compile(d->sc);
1367 if (rc != 0)
1369 /* Free memory for memory leak checking */
1370 gmx_ana_traj_free(d);
1371 gmx_input("selection could not be compiled");
1372 return rc;
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);
1387 else
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);
1417 if (sel->bDynamic)
1419 gmx_fatal(FARGS, "%s does not support dynamic selections",
1420 ShortProgram());
1421 return -1;
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",
1437 ShortProgram());
1438 return -1;
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]);
1449 return 0;
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
1461 * called.
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)
1476 int g;
1478 if (type == CFRAC_NONE)
1480 return 0;
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);
1490 return 0;
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);
1501 return 0;
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)
1510 int i;
1512 /* Return if we have already initialized the trajectory */
1513 if (d->fr)
1515 return 0;
1518 d->frflags |= TRX_NEED_X;
1520 snew(d->fr, 1);
1522 if (d->trjfile)
1524 if (!read_first_frame(d->oenv, &d->status, d->trjfile, d->fr, d->frflags))
1526 gmx_input("could not read coordinates from trajectory");
1527 return EIO;
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);
1534 return -1;
1536 /* check index groups */
1537 for (i = 0; i < d->ngrps; ++i)
1539 gmx_ana_index_check(d->sel[i]->g, d->fr->natoms);
1542 else
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");
1549 return -1;
1551 if (d->frflags & (TRX_NEED_F))
1553 gmx_input("Forces cannot be read from a topology");
1554 return -1;
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);
1565 set_trxframe_ePBC(d->fr, d->ePBC);
1567 return 0;
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.
1580 * \see gmx_ana_do()
1582 int gmx_ana_get_first_frame(gmx_ana_traj_t *d, t_trxframe **fr)
1584 int rc;
1586 rc = init_first_frame(d);
1587 if (rc != 0)
1589 *fr = NULL;
1590 return rc;
1592 *fr = d->fr;
1593 return 0;
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)
1614 t_pbc pbc;
1615 t_pbc *ppbc;
1616 int rc;
1618 rc = init_first_frame(d);
1619 if (rc != 0)
1621 return rc;
1624 ppbc = d->bPBC ? &pbc : 0;
1625 if (!d->top)
1627 d->bRmPBC = FALSE;
1630 d->nframes = 0;
1633 if (d->bRmPBC)
1635 rm_pbc(&d->top->idef, d->ePBC, d->fr->natoms, d->fr->box,
1636 d->fr->x, d->fr->x);
1638 if (ppbc)
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);
1645 if (rc != 0)
1647 close_trj(d->status);
1648 gmx_fatal(FARGS, "selection evaluation failed");
1649 return rc;
1651 rc = analyze(d->top, d->fr, ppbc, d->ngrps, d->sel, data);
1652 if (rc != 0)
1654 close_trj(d->status);
1655 return rc;
1658 d->nframes++;
1660 while (d->trjfile && read_next_frame(d->oenv, d->status, d->fr));
1662 if (d->trjfile)
1664 close_trj(d->status);
1665 fprintf(stderr, "Analyzed %d frames, last time %.3f\n",
1666 d->nframes, d->fr->time);
1668 else
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);
1675 if (rc != 0)
1677 gmx_fatal(FARGS, "selection evaluation failed");
1680 return rc;
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.
1690 extern int
1691 gmx_ana_get_nframes(gmx_ana_traj_t *d, int *nframes)
1693 *nframes = d->nframes;
1694 return 0;