Merge release-4-6 into release-5-0
[alexxy/gromacs.git] / src / gromacs / selection / selectioncollection.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2010,2011,2012,2013,2014, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \internal \file
36  * \brief
37  * Implements gmx::SelectionCollection.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_selection
41  */
42 #include "selectioncollection.h"
43
44 #include <cctype>
45 #include <cstdio>
46
47 #include <string>
48 #include <vector>
49
50 #include <boost/shared_ptr.hpp>
51
52 #include "gromacs/legacyheaders/oenv.h"
53 #include "gromacs/legacyheaders/xvgr.h"
54
55 #include "gromacs/onlinehelp/helpmanager.h"
56 #include "gromacs/onlinehelp/helpwritercontext.h"
57 #include "gromacs/options/basicoptions.h"
58 #include "gromacs/options/options.h"
59 #include "gromacs/selection/selection.h"
60 #include "gromacs/utility/exceptions.h"
61 #include "gromacs/utility/file.h"
62 #include "gromacs/utility/gmxassert.h"
63 #include "gromacs/utility/messagestringcollector.h"
64 #include "gromacs/utility/smalloc.h"
65 #include "gromacs/utility/stringutil.h"
66
67 #include "compiler.h"
68 #include "mempool.h"
69 #include "parser.h"
70 #include "poscalc.h"
71 #include "scanner.h"
72 #include "selection.h"
73 #include "selectioncollection-impl.h"
74 #include "selelem.h"
75 #include "selhelp.h"
76 #include "selmethod.h"
77 #include "symrec.h"
78
79 namespace gmx
80 {
81
82 /********************************************************************
83  * SelectionCollection::Impl
84  */
85
86 SelectionCollection::Impl::Impl()
87     : debugLevel_(0), bExternalGroupsSet_(false), grps_(NULL)
88 {
89     sc_.nvars     = 0;
90     sc_.varstrs   = NULL;
91     sc_.top       = NULL;
92     gmx_ana_index_clear(&sc_.gall);
93     sc_.mempool   = NULL;
94     sc_.symtab.reset(new SelectionParserSymbolTable);
95     gmx_ana_selmethod_register_defaults(sc_.symtab.get());
96 }
97
98
99 SelectionCollection::Impl::~Impl()
100 {
101     clearSymbolTable();
102     // The tree must be freed before the SelectionData objects, since the
103     // tree may hold references to the position data in SelectionData.
104     sc_.root.reset();
105     sc_.sel.clear();
106     for (int i = 0; i < sc_.nvars; ++i)
107     {
108         sfree(sc_.varstrs[i]);
109     }
110     sfree(sc_.varstrs);
111     gmx_ana_index_deinit(&sc_.gall);
112     if (sc_.mempool)
113     {
114         _gmx_sel_mempool_destroy(sc_.mempool);
115     }
116 }
117
118
119 void
120 SelectionCollection::Impl::clearSymbolTable()
121 {
122     sc_.symtab.reset();
123 }
124
125
126 namespace
127 {
128
129 /*! \brief
130  * Reads a single selection line from stdin.
131  *
132  * \param[in]  infile        File to read from (typically File::standardInput()).
133  * \param[in]  bInteractive  Whether to print interactive prompts.
134  * \param[out] line          The read line in stored here.
135  * \returns true if something was read, false if at end of input.
136  *
137  * Handles line continuation, reading also the continuing line(s) in one call.
138  */
139 bool promptLine(File *infile, bool bInteractive, std::string *line)
140 {
141     if (bInteractive)
142     {
143         fprintf(stderr, "> ");
144     }
145     if (!infile->readLineWithTrailingSpace(line))
146     {
147         return false;
148     }
149     while (endsWith(*line, "\\\n"))
150     {
151         line->resize(line->length() - 2);
152         if (bInteractive)
153         {
154             fprintf(stderr, "... ");
155         }
156         std::string buffer;
157         // Return value ignored, buffer remains empty and works correctly
158         // if there is nothing to read.
159         infile->readLineWithTrailingSpace(&buffer);
160         line->append(buffer);
161     }
162     if (endsWith(*line, "\n"))
163     {
164         line->resize(line->length() - 1);
165     }
166     else if (bInteractive)
167     {
168         fprintf(stderr, "\n");
169     }
170     return true;
171 }
172
173 /*! \brief
174  * Helper function for tokenizing the input and pushing them to the parser.
175  *
176  * \param     scanner       Tokenizer data structure.
177  * \param     parserState   Parser data structure.
178  * \param[in] bInteractive  Whether to operate in interactive mode.
179  *
180  * Repeatedly reads tokens using \p scanner and pushes them to the parser with
181  * \p parserState until there is no more input, or until enough input is given
182  * (only in interactive mode).
183  */
184 int runParserLoop(yyscan_t scanner, _gmx_sel_yypstate *parserState,
185                   bool bInteractive)
186 {
187     int status    = YYPUSH_MORE;
188     int prevToken = 0;
189     do
190     {
191         YYSTYPE value;
192         int     token = _gmx_sel_yylex(&value, scanner);
193         if (bInteractive)
194         {
195             if (token == 0)
196             {
197                 break;
198             }
199             // Empty commands cause the interactive parser to print out
200             // status information. This avoids producing those unnecessarily,
201             // e.g., from "resname RA;;".
202             if (prevToken == CMD_SEP && token == CMD_SEP)
203             {
204                 continue;
205             }
206             prevToken = token;
207         }
208         status = _gmx_sel_yypush_parse(parserState, token, &value, scanner);
209     }
210     while (status == YYPUSH_MORE);
211     _gmx_sel_lexer_rethrow_exception_if_occurred(scanner);
212     return status;
213 }
214
215 /*! \brief
216  * Print current status in response to empty line in interactive input.
217  *
218  * \param[in] sc             Selection collection data structure.
219  * \param[in] grps           Available index groups.
220  * \param[in] firstSelection Index of first selection from this interactive
221  *     session.
222  * \param[in] maxCount       Maximum number of selections.
223  * \param[in] context        Context to print for what the selections are for.
224  * \param[in] bFirst         Whether this is the header that is printed before
225  *     any user input.
226  *
227  * Prints the available index groups and currently provided selections.
228  */
229 void printCurrentStatus(gmx_ana_selcollection_t *sc, gmx_ana_indexgrps_t *grps,
230                         size_t firstSelection, int maxCount,
231                         const std::string &context, bool bFirst)
232 {
233     if (grps != NULL)
234     {
235         std::fprintf(stderr, "Available static index groups:\n");
236         gmx_ana_indexgrps_print(stderr, grps, 0);
237     }
238     std::fprintf(stderr, "Specify ");
239     if (maxCount < 0)
240     {
241         std::fprintf(stderr, "any number of selections");
242     }
243     else if (maxCount == 1)
244     {
245         std::fprintf(stderr, "a selection");
246     }
247     else
248     {
249         std::fprintf(stderr, "%d selections", maxCount);
250     }
251     std::fprintf(stderr, "%s%s:\n",
252                  context.empty() ? "" : " ", context.c_str());
253     std::fprintf(stderr,
254                  "(one per line, <enter> for status/groups, 'help' for help%s)\n",
255                  maxCount < 0 ? ", Ctrl-D to end" : "");
256     if (!bFirst && (sc->nvars > 0 || sc->sel.size() > firstSelection))
257     {
258         std::fprintf(stderr, "Currently provided selections:\n");
259         for (int i = 0; i < sc->nvars; ++i)
260         {
261             std::fprintf(stderr, "     %s\n", sc->varstrs[i]);
262         }
263         for (size_t i = firstSelection; i < sc->sel.size(); ++i)
264         {
265             std::fprintf(stderr, " %2d. %s\n",
266                          static_cast<int>(i - firstSelection + 1),
267                          sc->sel[i]->selectionText());
268         }
269         if (maxCount > 0)
270         {
271             const int remaining
272                 = maxCount - static_cast<int>(sc->sel.size() - firstSelection);
273             std::fprintf(stderr, "(%d more selection%s required)\n",
274                          remaining, remaining > 1 ? "s" : "");
275         }
276     }
277 }
278
279 /*! \brief
280  * Prints selection help in interactive selection input.
281  *
282  * \param[in] sc    Selection collection data structure.
283  * \param[in] line  Line of user input requesting help (starting with `help`).
284  *
285  * Initializes the selection help if not yet initialized, and finds the help
286  * topic based on words on the input line.
287  */
288 void printHelp(gmx_ana_selcollection_t *sc, const std::string &line)
289 {
290     if (sc->rootHelp.get() == NULL)
291     {
292         sc->rootHelp = createSelectionHelpTopic();
293     }
294     HelpWriterContext context(&File::standardError(),
295                               eHelpOutputFormat_Console);
296     HelpManager       manager(*sc->rootHelp, context);
297     try
298     {
299         std::vector<std::string>                 topic = splitString(line);
300         std::vector<std::string>::const_iterator value;
301         // First item in the list is the 'help' token.
302         for (value = topic.begin() + 1; value != topic.end(); ++value)
303         {
304             manager.enterTopic(*value);
305         }
306     }
307     catch (const InvalidInputError &ex)
308     {
309         fprintf(stderr, "%s\n", ex.what());
310         return;
311     }
312     manager.writeCurrentTopic();
313 }
314
315 /*! \brief
316  * Helper function that runs the parser once the tokenizer has been
317  * initialized.
318  *
319  * \param[in,out] scanner Scanner data structure.
320  * \param[in]     bStdIn  Whether to use a line-based reading
321  *      algorithm designed for interactive input.
322  * \param[in]     maxnr   Maximum number of selections to parse
323  *      (if -1, parse as many as provided by the user).
324  * \param[in]     context Context to print for what the selections are for.
325  * \returns       Vector of parsed selections.
326  * \throws        std::bad_alloc if out of memory.
327  * \throws        InvalidInputError if there is a parsing error.
328  *
329  * Used internally to implement parseFromStdin(), parseFromFile() and
330  * parseFromString().
331  */
332 SelectionList runParser(yyscan_t scanner, bool bStdIn, int maxnr,
333                         const std::string &context)
334 {
335     boost::shared_ptr<void>  scannerGuard(scanner, &_gmx_sel_free_lexer);
336     gmx_ana_selcollection_t *sc   = _gmx_sel_lexer_selcollection(scanner);
337     gmx_ana_indexgrps_t     *grps = _gmx_sel_lexer_indexgrps(scanner);
338
339     MessageStringCollector   errors;
340     _gmx_sel_set_lexer_error_reporter(scanner, &errors);
341
342     size_t oldCount = sc->sel.size();
343     bool   bOk      = false;
344     {
345         boost::shared_ptr<_gmx_sel_yypstate> parserState(
346                 _gmx_sel_yypstate_new(), &_gmx_sel_yypstate_delete);
347         if (bStdIn)
348         {
349             File       &stdinFile(File::standardInput());
350             const bool  bInteractive = _gmx_sel_is_lexer_interactive(scanner);
351             if (bInteractive)
352             {
353                 printCurrentStatus(sc, grps, oldCount, maxnr, context, true);
354             }
355             std::string line;
356             int         status;
357             while (promptLine(&stdinFile, bInteractive, &line))
358             {
359                 if (bInteractive)
360                 {
361                     line = stripString(line);
362                     if (line.empty())
363                     {
364                         printCurrentStatus(sc, grps, oldCount, maxnr, context, false);
365                         continue;
366                     }
367                     if (startsWith(line, "help")
368                         && (line[4] == 0 || std::isspace(line[4])))
369                     {
370                         printHelp(sc, line);
371                         continue;
372                     }
373                 }
374                 line.append("\n");
375                 _gmx_sel_set_lex_input_str(scanner, line.c_str());
376                 status = runParserLoop(scanner, parserState.get(), true);
377                 if (status != YYPUSH_MORE)
378                 {
379                     // TODO: Check if there is more input, and issue an
380                     // error/warning if some input was ignored.
381                     goto early_termination;
382                 }
383                 if (!errors.isEmpty() && bInteractive)
384                 {
385                     fprintf(stderr, "%s", errors.toString().c_str());
386                     errors.clear();
387                 }
388             }
389             status = _gmx_sel_yypush_parse(parserState.get(), 0, NULL,
390                                            scanner);
391             _gmx_sel_lexer_rethrow_exception_if_occurred(scanner);
392 early_termination:
393             bOk = (status == 0);
394         }
395         else
396         {
397             int status = runParserLoop(scanner, parserState.get(), false);
398             bOk = (status == 0);
399         }
400     }
401     scannerGuard.reset();
402     int nr = sc->sel.size() - oldCount;
403     if (maxnr > 0 && nr != maxnr)
404     {
405         bOk = false;
406         errors.append("Too few selections provided");
407     }
408
409     // TODO: Remove added selections from the collection if parsing failed?
410     if (!bOk || !errors.isEmpty())
411     {
412         GMX_ASSERT(!bOk && !errors.isEmpty(), "Inconsistent error reporting");
413         GMX_THROW(InvalidInputError(errors.toString()));
414     }
415
416     SelectionList                     result;
417     SelectionDataList::const_iterator i;
418     result.reserve(nr);
419     for (i = sc->sel.begin() + oldCount; i != sc->sel.end(); ++i)
420     {
421         result.push_back(Selection(i->get()));
422     }
423     return result;
424 }
425
426 }   // namespace
427
428
429 void SelectionCollection::Impl::resolveExternalGroups(
430         const SelectionTreeElementPointer &root,
431         ExceptionInitializer              *errors)
432 {
433
434     if (root->type == SEL_GROUPREF)
435     {
436         try
437         {
438             root->resolveIndexGroupReference(grps_);
439         }
440         catch (const UserInputError &)
441         {
442             errors->addCurrentExceptionAsNested();
443         }
444     }
445
446     SelectionTreeElementPointer child = root->child;
447     while (child)
448     {
449         resolveExternalGroups(child, errors);
450         root->flags |= (child->flags & SEL_UNSORTED);
451         child        = child->next;
452     }
453 }
454
455
456 /********************************************************************
457  * SelectionCollection
458  */
459
460 SelectionCollection::SelectionCollection()
461     : impl_(new Impl)
462 {
463 }
464
465
466 SelectionCollection::~SelectionCollection()
467 {
468 }
469
470
471 void
472 SelectionCollection::initOptions(Options *options)
473 {
474     const char * const debug_levels[]
475         = { "no", "basic", "compile", "eval", "full" };
476
477     bool bAllowNonAtomOutput = false;
478     SelectionDataList::const_iterator iter;
479     for (iter = impl_->sc_.sel.begin(); iter != impl_->sc_.sel.end(); ++iter)
480     {
481         const internal::SelectionData &sel = **iter;
482         if (!sel.hasFlag(efSelection_OnlyAtoms))
483         {
484             bAllowNonAtomOutput = true;
485         }
486     }
487
488     const char *const *postypes = PositionCalculationCollection::typeEnumValues;
489     options->addOption(StringOption("selrpos")
490                            .enumValueFromNullTerminatedArray(postypes)
491                            .store(&impl_->rpost_).defaultValue(postypes[0])
492                            .description("Selection reference positions"));
493     if (bAllowNonAtomOutput)
494     {
495         options->addOption(StringOption("seltype")
496                                .enumValueFromNullTerminatedArray(postypes)
497                                .store(&impl_->spost_).defaultValue(postypes[0])
498                                .description("Default selection output positions"));
499     }
500     else
501     {
502         impl_->spost_ = postypes[0];
503     }
504     GMX_RELEASE_ASSERT(impl_->debugLevel_ >= 0 && impl_->debugLevel_ <= 4,
505                        "Debug level out of range");
506     options->addOption(StringOption("seldebug").hidden(impl_->debugLevel_ == 0)
507                            .enumValue(debug_levels)
508                            .defaultValue(debug_levels[impl_->debugLevel_])
509                            .storeEnumIndex(&impl_->debugLevel_)
510                            .description("Print out selection trees for debugging"));
511 }
512
513
514 void
515 SelectionCollection::setReferencePosType(const char *type)
516 {
517     GMX_RELEASE_ASSERT(type != NULL, "Cannot assign NULL position type");
518     // Check that the type is valid, throw if it is not.
519     e_poscalc_t  dummytype;
520     int          dummyflags;
521     PositionCalculationCollection::typeFromEnum(type, &dummytype, &dummyflags);
522     impl_->rpost_ = type;
523 }
524
525
526 void
527 SelectionCollection::setOutputPosType(const char *type)
528 {
529     GMX_RELEASE_ASSERT(type != NULL, "Cannot assign NULL position type");
530     // Check that the type is valid, throw if it is not.
531     e_poscalc_t  dummytype;
532     int          dummyflags;
533     PositionCalculationCollection::typeFromEnum(type, &dummytype, &dummyflags);
534     impl_->spost_ = type;
535 }
536
537
538 void
539 SelectionCollection::setDebugLevel(int debugLevel)
540 {
541     impl_->debugLevel_ = debugLevel;
542 }
543
544
545 void
546 SelectionCollection::setTopology(t_topology *top, int natoms)
547 {
548     GMX_RELEASE_ASSERT(natoms > 0 || top != NULL,
549                        "The number of atoms must be given if there is no topology");
550     // Get the number of atoms from the topology if it is not given.
551     if (natoms <= 0)
552     {
553         natoms = top->atoms.nr;
554     }
555     gmx_ana_selcollection_t *sc = &impl_->sc_;
556     // Do this first, as it allocates memory, while the others don't throw.
557     gmx_ana_index_init_simple(&sc->gall, natoms);
558     sc->pcc.setTopology(top);
559     sc->top = top;
560 }
561
562
563 void
564 SelectionCollection::setIndexGroups(gmx_ana_indexgrps_t *grps)
565 {
566     GMX_RELEASE_ASSERT(grps == NULL || !impl_->bExternalGroupsSet_,
567                        "Can only set external groups once or clear them afterwards");
568     impl_->grps_               = grps;
569     impl_->bExternalGroupsSet_ = true;
570
571     ExceptionInitializer        errors("Invalid index group reference(s)");
572     SelectionTreeElementPointer root = impl_->sc_.root;
573     while (root)
574     {
575         impl_->resolveExternalGroups(root, &errors);
576         root->checkUnsortedAtoms(true, &errors);
577         root = root->next;
578     }
579     if (errors.hasNestedExceptions())
580     {
581         GMX_THROW(InconsistentInputError(errors));
582     }
583     for (size_t i = 0; i < impl_->sc_.sel.size(); ++i)
584     {
585         impl_->sc_.sel[i]->refreshName();
586     }
587 }
588
589
590 bool
591 SelectionCollection::requiresTopology() const
592 {
593     e_poscalc_t  type;
594     int          flags;
595
596     if (!impl_->rpost_.empty())
597     {
598         flags = 0;
599         // Should not throw, because has been checked earlier.
600         PositionCalculationCollection::typeFromEnum(impl_->rpost_.c_str(),
601                                                     &type, &flags);
602         if (type != POS_ATOM)
603         {
604             return true;
605         }
606     }
607     if (!impl_->spost_.empty())
608     {
609         flags = 0;
610         // Should not throw, because has been checked earlier.
611         PositionCalculationCollection::typeFromEnum(impl_->spost_.c_str(),
612                                                     &type, &flags);
613         if (type != POS_ATOM)
614         {
615             return true;
616         }
617     }
618
619     SelectionTreeElementPointer sel = impl_->sc_.root;
620     while (sel)
621     {
622         if (_gmx_selelem_requires_top(*sel))
623         {
624             return true;
625         }
626         sel = sel->next;
627     }
628     return false;
629 }
630
631
632 SelectionList
633 SelectionCollection::parseFromStdin(int nr, bool bInteractive,
634                                     const std::string &context)
635 {
636     yyscan_t scanner;
637
638     _gmx_sel_init_lexer(&scanner, &impl_->sc_, bInteractive, nr,
639                         impl_->bExternalGroupsSet_,
640                         impl_->grps_);
641     return runParser(scanner, true, nr, context);
642 }
643
644
645 SelectionList
646 SelectionCollection::parseFromFile(const std::string &filename)
647 {
648
649     try
650     {
651         yyscan_t scanner;
652         File     file(filename, "r");
653         // TODO: Exception-safe way of using the lexer.
654         _gmx_sel_init_lexer(&scanner, &impl_->sc_, false, -1,
655                             impl_->bExternalGroupsSet_,
656                             impl_->grps_);
657         _gmx_sel_set_lex_input_file(scanner, file.handle());
658         return runParser(scanner, false, -1, std::string());
659     }
660     catch (GromacsException &ex)
661     {
662         ex.prependContext(formatString(
663                                   "Error in parsing selections from file '%s'",
664                                   filename.c_str()));
665         throw;
666     }
667 }
668
669
670 SelectionList
671 SelectionCollection::parseFromString(const std::string &str)
672 {
673     yyscan_t scanner;
674
675     _gmx_sel_init_lexer(&scanner, &impl_->sc_, false, -1,
676                         impl_->bExternalGroupsSet_,
677                         impl_->grps_);
678     _gmx_sel_set_lex_input_str(scanner, str.c_str());
679     return runParser(scanner, false, -1, std::string());
680 }
681
682
683 void
684 SelectionCollection::compile()
685 {
686     if (impl_->sc_.top == NULL && requiresTopology())
687     {
688         GMX_THROW(InconsistentInputError("Selection requires topology information, but none provided"));
689     }
690     if (!impl_->bExternalGroupsSet_)
691     {
692         setIndexGroups(NULL);
693     }
694     if (impl_->debugLevel_ >= 1)
695     {
696         printTree(stderr, false);
697     }
698
699     SelectionCompiler compiler;
700     compiler.compile(this);
701
702     if (impl_->debugLevel_ >= 1)
703     {
704         std::fprintf(stderr, "\n");
705         printTree(stderr, false);
706         std::fprintf(stderr, "\n");
707         impl_->sc_.pcc.printTree(stderr);
708         std::fprintf(stderr, "\n");
709     }
710     impl_->sc_.pcc.initEvaluation();
711     if (impl_->debugLevel_ >= 1)
712     {
713         impl_->sc_.pcc.printTree(stderr);
714         std::fprintf(stderr, "\n");
715     }
716
717     // TODO: It would be nicer to associate the name of the selection option
718     // (if available) to the error message.
719     SelectionDataList::const_iterator iter;
720     for (iter = impl_->sc_.sel.begin(); iter != impl_->sc_.sel.end(); ++iter)
721     {
722         const internal::SelectionData &sel = **iter;
723         if (sel.hasFlag(efSelection_OnlyAtoms))
724         {
725             if (sel.type() != INDEX_ATOM)
726             {
727                 std::string message = formatString(
728                             "Selection '%s' does not evaluate to individual atoms. "
729                             "This is not allowed in this context.",
730                             sel.selectionText());
731                 GMX_THROW(InvalidInputError(message));
732             }
733         }
734         if (sel.hasFlag(efSelection_DisallowEmpty))
735         {
736             if (sel.posCount() == 0)
737             {
738                 std::string message = formatString(
739                             "Selection '%s' never matches any atoms.",
740                             sel.selectionText());
741                 GMX_THROW(InvalidInputError(message));
742             }
743         }
744     }
745 }
746
747
748 void
749 SelectionCollection::evaluate(t_trxframe *fr, t_pbc *pbc)
750 {
751     impl_->sc_.pcc.initFrame();
752
753     SelectionEvaluator evaluator;
754     evaluator.evaluate(this, fr, pbc);
755
756     if (impl_->debugLevel_ >= 3)
757     {
758         std::fprintf(stderr, "\n");
759         printTree(stderr, true);
760     }
761 }
762
763
764 void
765 SelectionCollection::evaluateFinal(int nframes)
766 {
767     SelectionEvaluator evaluator;
768     evaluator.evaluateFinal(this, nframes);
769 }
770
771
772 void
773 SelectionCollection::printTree(FILE *fp, bool bValues) const
774 {
775     SelectionTreeElementPointer sel = impl_->sc_.root;
776     while (sel)
777     {
778         _gmx_selelem_print_tree(fp, *sel, bValues, 0);
779         sel = sel->next;
780     }
781 }
782
783
784 void
785 SelectionCollection::printXvgrInfo(FILE *out, output_env_t oenv) const
786 {
787     if (output_env_get_xvg_format(oenv) != exvgNONE)
788     {
789         const gmx_ana_selcollection_t &sc = impl_->sc_;
790         std::fprintf(out, "# Selections:\n");
791         for (int i = 0; i < sc.nvars; ++i)
792         {
793             std::fprintf(out, "#   %s\n", sc.varstrs[i]);
794         }
795         for (size_t i = 0; i < sc.sel.size(); ++i)
796         {
797             std::fprintf(out, "#   %s\n", sc.sel[i]->selectionText());
798         }
799         std::fprintf(out, "#\n");
800     }
801 }
802
803 // static
804 HelpTopicPointer
805 SelectionCollection::createDefaultHelpTopic()
806 {
807     return createSelectionHelpTopic();
808 }
809
810 } // namespace gmx