Convert gmx_mtop_t to C++
[alexxy/gromacs.git] / src / gromacs / selection / selectionoptionbehavior.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2015,2016,2017,2018, 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::SelectionOptionBehavior.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_selection
41  */
42 #include "gmxpre.h"
43
44 #include "selectionoptionbehavior.h"
45
46 #include <cstdio>
47
48 #include <string>
49
50 #include "gromacs/options/filenameoption.h"
51 #include "gromacs/options/ioptionscontainer.h"
52 #include "gromacs/options/options.h"
53 #include "gromacs/selection/indexutil.h"
54 #include "gromacs/selection/selectioncollection.h"
55 #include "gromacs/selection/selectionfileoption.h"
56 #include "gromacs/selection/selectionoptionmanager.h"
57 #include "gromacs/topology/atoms.h"
58 #include "gromacs/topology/topology.h"
59 #include "gromacs/utility/exceptions.h"
60 #include "gromacs/utility/filestream.h"
61
62 namespace gmx
63 {
64
65 /********************************************************************
66  * ITopologyProvider
67  */
68
69 ITopologyProvider::~ITopologyProvider()
70 {
71 }
72
73 /********************************************************************
74  * SelectionOptionBehavior
75  */
76
77 class SelectionOptionBehavior::Impl
78 {
79     public:
80         Impl(SelectionCollection *selections,
81              ITopologyProvider   *topologyProvider)
82             : selections_(*selections), topologyProvider_(*topologyProvider),
83               manager_(selections), grps_(nullptr)
84         {
85         }
86         ~Impl()
87         {
88             if (grps_ != nullptr)
89             {
90                 gmx_ana_indexgrps_free(grps_);
91             }
92         }
93
94         void promptSelections()
95         {
96             const bool isInteractive = StandardInputStream::instance().isInteractive();
97             initIndexGroups();
98             manager_.parseRequestedFromStdin(isInteractive);
99             doneIndexGroups();
100         }
101         void initIndexGroups()
102         {
103             if (!selections_.requiresIndexGroups()
104                 && !manager_.hasRequestedSelections())
105             {
106                 if (!ndxfile_.empty())
107                 {
108                     std::fprintf(stderr, "NOTE: You provided an index file\n"
109                                  "  %s\n(with -n), but it was not used by any selection.\n",
110                                  ndxfile_.c_str());
111                 }
112                 selections_.setIndexGroups(nullptr);
113                 return;
114             }
115             if (ndxfile_.empty())
116             {
117                 gmx_mtop_t *top = topologyProvider_.getTopology(false);
118                 gmx_ana_indexgrps_init(&grps_, top, nullptr);
119             }
120             else
121             {
122                 gmx_ana_indexgrps_init(&grps_, nullptr, ndxfile_.c_str());
123             }
124             selections_.setIndexGroups(grps_);
125         }
126         void doneIndexGroups()
127         {
128             if (grps_ != nullptr)
129             {
130                 selections_.setIndexGroups(nullptr);
131                 gmx_ana_indexgrps_free(grps_);
132                 grps_ = nullptr;
133             }
134         }
135
136         void compileSelections()
137         {
138             const bool  topRequired = selections_.requiredTopologyProperties().needsTopology;
139             gmx_mtop_t *top         = topologyProvider_.getTopology(topRequired);
140             int         natoms      = -1;
141             if (top == nullptr)
142             {
143                 natoms = topologyProvider_.getAtomCount();
144             }
145             getMassesIfRequired(top);
146             selections_.setTopology(top, natoms);
147             selections_.compile();
148             // Situation may have changed after compilation.
149             getMassesIfRequired(top);
150         }
151
152         void getMassesIfRequired(gmx_mtop_t *top)
153         {
154             const bool massRequired = selections_.requiredTopologyProperties().needsMasses;
155             if (!massRequired)
156             {
157                 return;
158             }
159             // TODO: There can be some corner cases that still hit this assert
160             // when the user has not provided the topology.
161             GMX_RELEASE_ASSERT(top != nullptr,
162                                "Masses are required, but no topology is loaded");
163             for (gmx_moltype_t &moltype : top->moltype)
164             {
165                 if (!moltype.atoms.haveMass)
166                 {
167                     atomsSetMassesBasedOnNames(&moltype.atoms, TRUE);
168                     if (!moltype.atoms.haveMass)
169                     {
170                         GMX_THROW(InconsistentInputError("Selections require mass information for evaluation, but it is not available in the input and could not be determined for all atoms based on atom names."));
171                     }
172                 }
173             }
174         }
175
176         SelectionCollection    &selections_;
177         ITopologyProvider      &topologyProvider_;
178         SelectionOptionManager  manager_;
179         //! Name of the index file (empty if no index file provided).
180         std::string             ndxfile_;
181         gmx_ana_indexgrps_t    *grps_;
182 };
183
184 SelectionOptionBehavior::SelectionOptionBehavior(
185         SelectionCollection *selections,
186         ITopologyProvider   *topologyProvider)
187     : impl_(new Impl(selections, topologyProvider))
188 {
189 }
190
191 SelectionOptionBehavior::~SelectionOptionBehavior()
192 {
193 }
194
195 void
196 SelectionOptionBehavior::initOptions(IOptionsContainer *options)
197 {
198     options->addOption(FileNameOption("n")
199                            .filetype(eftIndex).inputFile()
200                            .store(&impl_->ndxfile_)
201                            .defaultBasename("index")
202                            .description("Extra index groups"));
203     options->addOption(SelectionFileOption("sf"));
204     impl_->manager_.initOptions(options);
205 }
206
207 void
208 SelectionOptionBehavior::initBehavior(Options *options)
209 {
210     options->addManager(&impl_->manager_);
211 }
212
213 void
214 SelectionOptionBehavior::optionsFinished()
215 {
216     impl_->promptSelections();
217     impl_->compileSelections();
218 }
219
220 } // namespace gmx