3da677e2b12ae2488989730bd18f7091a66be3f8
[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,2019,2020,2021, 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  * SelectionOptionBehavior
73  */
74
75 class SelectionOptionBehavior::Impl
76 {
77 public:
78     Impl(SelectionCollection* selections, ITopologyProvider* topologyProvider) :
79         selections_(*selections),
80         topologyProvider_(*topologyProvider),
81         manager_(selections),
82         grps_(nullptr)
83     {
84     }
85     ~Impl()
86     {
87         if (grps_ != nullptr)
88         {
89             gmx_ana_indexgrps_free(grps_);
90         }
91     }
92
93     void promptSelections()
94     {
95         const bool isInteractive = StandardInputStream::isInteractive();
96         initIndexGroups();
97         manager_.parseRequestedFromStdin(isInteractive);
98         doneIndexGroups();
99     }
100     void initIndexGroups()
101     {
102         if (!selections_.requiresIndexGroups() && !manager_.hasRequestedSelections())
103         {
104             if (!ndxfile_.empty())
105             {
106                 std::fprintf(stderr,
107                              "NOTE: You provided an index file\n"
108                              "  %s\n(with -n), but it was not used by any selection.\n",
109                              ndxfile_.c_str());
110             }
111             selections_.setIndexGroups(nullptr);
112             return;
113         }
114         if (ndxfile_.empty())
115         {
116             gmx_mtop_t* top = topologyProvider_.getTopology(false);
117             gmx_ana_indexgrps_init(&grps_, top, nullptr);
118         }
119         else
120         {
121             gmx_ana_indexgrps_init(&grps_, nullptr, ndxfile_.c_str());
122         }
123         selections_.setIndexGroups(grps_);
124     }
125     void doneIndexGroups()
126     {
127         if (grps_ != nullptr)
128         {
129             selections_.setIndexGroups(nullptr);
130             gmx_ana_indexgrps_free(grps_);
131             grps_ = nullptr;
132         }
133     }
134
135     void compileSelections()
136     {
137         const bool  topRequired = selections_.requiredTopologyProperties().needsTopology;
138         gmx_mtop_t* top         = topologyProvider_.getTopology(topRequired);
139         int         natoms      = -1;
140         if (top == nullptr)
141         {
142             natoms = topologyProvider_.getAtomCount();
143         }
144         getMassesIfRequired(top);
145         selections_.setTopology(top, natoms);
146         selections_.compile();
147         // Situation may have changed after compilation.
148         getMassesIfRequired(top);
149     }
150
151     void getMassesIfRequired(gmx_mtop_t* top) const
152     {
153         const bool massRequired = selections_.requiredTopologyProperties().needsMasses;
154         if (!massRequired)
155         {
156             return;
157         }
158         // TODO: There can be some corner cases that still hit this assert
159         // when the user has not provided the topology.
160         GMX_RELEASE_ASSERT(top != nullptr, "Masses are required, but no topology is loaded");
161         for (gmx_moltype_t& moltype : top->moltype)
162         {
163             if (!moltype.atoms.haveMass)
164             {
165                 atomsSetMassesBasedOnNames(&moltype.atoms, TRUE);
166                 if (!moltype.atoms.haveMass)
167                 {
168                     GMX_THROW(InconsistentInputError(
169                             "Selections require mass information for evaluation, but it is not "
170                             "available in the input and could not be determined for all atoms "
171                             "based on atom names."));
172                 }
173             }
174         }
175     }
176
177     SelectionCollection&   selections_;
178     ITopologyProvider&     topologyProvider_;
179     SelectionOptionManager manager_;
180     //! Name of the index file (empty if no index file provided).
181     std::string          ndxfile_;
182     gmx_ana_indexgrps_t* grps_;
183 };
184
185 SelectionOptionBehavior::SelectionOptionBehavior(SelectionCollection* selections,
186                                                  ITopologyProvider*   topologyProvider) :
187     impl_(new Impl(selections, topologyProvider))
188 {
189 }
190
191 SelectionOptionBehavior::~SelectionOptionBehavior() {}
192
193 void SelectionOptionBehavior::initOptions(IOptionsContainer* options)
194 {
195     options->addOption(FileNameOption("n")
196                                .filetype(OptionFileType::Index)
197                                .inputFile()
198                                .store(&impl_->ndxfile_)
199                                .defaultBasename("index")
200                                .description("Extra index groups"));
201     options->addOption(SelectionFileOption("sf"));
202     impl_->manager_.initOptions(options);
203 }
204
205 void SelectionOptionBehavior::initBehavior(Options* options)
206 {
207     options->addManager(&impl_->manager_);
208 }
209
210 void SelectionOptionBehavior::optionsFinished()
211 {
212     impl_->promptSelections();
213     impl_->compileSelections();
214 }
215
216 } // namespace gmx