2635cbe78a856c4d7d0c8389381b3b0f75321d31
[alexxy/gromacs.git] / src / gromacs / selection / selectionoption.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2010,2011,2012,2013,2014,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 classes in selectionoption.h and selectionoptionstorage.h.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_selection
41  */
42 #include "gmxpre.h"
43
44 #include "selectionoption.h"
45
46 #include <string>
47
48 #include "gromacs/options/optionmanagercontainer.h"
49 #include "gromacs/selection/selection.h"
50 #include "gromacs/selection/selectionfileoption.h"
51 #include "gromacs/selection/selectionoptionmanager.h"
52 #include "gromacs/utility/exceptions.h"
53 #include "gromacs/utility/gmxassert.h"
54 #include "gromacs/utility/messagestringcollector.h"
55
56 #include "selectionfileoptionstorage.h"
57 #include "selectionoptionstorage.h"
58
59 namespace gmx
60 {
61
62 /********************************************************************
63  * SelectionOptionStorage
64  */
65
66 SelectionOptionStorage::SelectionOptionStorage(const SelectionOption  &settings,
67                                                SelectionOptionManager *manager)
68     : MyBase(settings, OptionFlags() | efOption_NoDefaultValue
69              | efOption_DontCheckMinimumCount),
70       info_(this), manager_(*manager), defaultText_(settings.defaultText_),
71       selectionFlags_(settings.selectionFlags_)
72 {
73     GMX_RELEASE_ASSERT(manager != nullptr,
74                        "SelectionOptionManager must be added before SelectionOption");
75     GMX_RELEASE_ASSERT(!hasFlag(efOption_MultipleTimes),
76                        "allowMultiple() is not supported for selection options");
77     manager_.registerOption(this);
78 }
79
80
81 std::string SelectionOptionStorage::formatSingleValue(const Selection &value) const
82 {
83     return value.selectionText();
84 }
85
86
87 std::vector<Variant>
88 SelectionOptionStorage::normalizeValues(const std::vector<Variant> & /*values*/) const
89 {
90     GMX_THROW(NotImplementedError("Selection options not supported in this context"));
91 }
92
93
94 void SelectionOptionStorage::addSelections(
95         const SelectionList &selections,
96         bool                 bFullValue)
97 {
98     if (bFullValue && selections.size() < static_cast<size_t>(minValueCount()))
99     {
100         GMX_THROW(InvalidInputError("Too few selections provided"));
101     }
102     if (bFullValue)
103     {
104         clearSet();
105     }
106     SelectionList::const_iterator i;
107     for (i = selections.begin(); i != selections.end(); ++i)
108     {
109         // TODO: Having this check in the parser would make interactive input
110         // behave better.
111         if (selectionFlags_.test(efSelection_OnlyStatic) && i->isDynamic())
112         {
113             GMX_THROW(InvalidInputError("Dynamic selections not supported"));
114         }
115         // Create a copy to allow modifications.
116         Selection sel(*i);
117         sel.data().setFlags(selectionFlags_);
118         addValue(sel);
119     }
120     if (bFullValue)
121     {
122         commitValues();
123         markAsSet();
124     }
125 }
126
127
128 void SelectionOptionStorage::convertValue(const Variant &value)
129 {
130     manager_.convertOptionValue(this, value.cast<std::string>(), false);
131 }
132
133 void SelectionOptionStorage::processSetValues(ValueList *values)
134 {
135     if (values->empty())
136     {
137         manager_.requestOptionDelayedParsing(this);
138     }
139     else if (values->size() < static_cast<size_t>(minValueCount()))
140     {
141         GMX_THROW(InvalidInputError("Too few (valid) values provided"));
142     }
143 }
144
145 void SelectionOptionStorage::processAll()
146 {
147     if (!isSet() && !defaultText_.empty())
148     {
149         manager_.convertOptionValue(this, defaultText_, true);
150     }
151     if (isRequired() && !isSet())
152     {
153         manager_.requestOptionDelayedParsing(this);
154         markAsSet();
155     }
156 }
157
158 void SelectionOptionStorage::setAllowedValueCount(int count)
159 {
160     // TODO: It should be possible to have strong exception safety here.
161     // TODO: Use ExceptionInitializer here.
162     MessageStringCollector errors;
163     errors.startContext("In option '" + name() + "'");
164     if (count >= 0)
165     {
166         // Should not throw because efOption_DontCheckMinimumCount is set.
167         setMinValueCount(count);
168         if (valueCount() > 0 && valueCount() < count)
169         {
170             errors.append("Too few (valid) values provided");
171         }
172     }
173     try
174     {
175         setMaxValueCount(count);
176     }
177     catch (const UserInputError &ex)
178     {
179         errors.append(ex.what());
180     }
181     errors.finishContext();
182     if (!errors.isEmpty())
183     {
184         GMX_THROW(InvalidInputError(errors.toString()));
185     }
186 }
187
188 void SelectionOptionStorage::setSelectionFlag(SelectionFlag flag, bool bSet)
189 {
190     for (const Selection &value : values())
191     {
192         if (flag == efSelection_OnlyStatic && bSet && value.isDynamic())
193         {
194             MessageStringCollector errors;
195             errors.startContext("In option '" + name() + "'");
196             errors.append("Dynamic selections not supported");
197             errors.finishContext();
198             GMX_THROW(InvalidInputError(errors.toString()));
199         }
200     }
201     selectionFlags_.set(flag, bSet);
202     for (Selection &value : values())
203     {
204         value.data().setFlags(selectionFlags_);
205     }
206 }
207
208
209 /********************************************************************
210  * SelectionOptionInfo
211  */
212
213 SelectionOptionInfo::SelectionOptionInfo(SelectionOptionStorage *option)
214     : OptionInfo(option)
215 {
216 }
217
218 SelectionOptionStorage &SelectionOptionInfo::option()
219 {
220     return static_cast<SelectionOptionStorage &>(OptionInfo::option());
221 }
222
223 const SelectionOptionStorage &SelectionOptionInfo::option() const
224 {
225     return static_cast<const SelectionOptionStorage &>(OptionInfo::option());
226 }
227
228 void SelectionOptionInfo::setValueCount(int count)
229 {
230     option().setAllowedValueCount(count);
231 }
232
233 void SelectionOptionInfo::setEvaluateVelocities(bool bEnabled)
234 {
235     option().setSelectionFlag(efSelection_EvaluateVelocities, bEnabled);
236 }
237
238 void SelectionOptionInfo::setEvaluateForces(bool bEnabled)
239 {
240     option().setSelectionFlag(efSelection_EvaluateForces, bEnabled);
241 }
242
243 void SelectionOptionInfo::setOnlyAtoms(bool bEnabled)
244 {
245     option().setSelectionFlag(efSelection_OnlyAtoms, bEnabled);
246 }
247
248 void SelectionOptionInfo::setOnlyStatic(bool bEnabled)
249 {
250     option().setSelectionFlag(efSelection_OnlyStatic, bEnabled);
251 }
252
253 void SelectionOptionInfo::setDynamicMask(bool bEnabled)
254 {
255     option().setSelectionFlag(efSelection_DynamicMask, bEnabled);
256 }
257
258
259 /********************************************************************
260  * SelectionOption
261  */
262
263 AbstractOptionStorage *
264 SelectionOption::createStorage(const OptionManagerContainer &managers) const
265 {
266     return new SelectionOptionStorage(
267             *this, managers.get<SelectionOptionManager>());
268 }
269
270
271 /********************************************************************
272  * SelectionFileOptionStorage
273  */
274
275 SelectionFileOptionStorage::SelectionFileOptionStorage(
276         const SelectionFileOption &settings, SelectionOptionManager *manager)
277     : AbstractOptionStorage(settings, OptionFlags() | efOption_MultipleTimes
278                             | efOption_DontCheckMinimumCount),
279       info_(this), manager_(*manager), bValueParsed_(false)
280 {
281     GMX_RELEASE_ASSERT(manager != nullptr,
282                        "SelectionOptionManager must be added before SelectionFileOption");
283 }
284
285 void SelectionFileOptionStorage::clearSet()
286 {
287     bValueParsed_ = false;
288 }
289
290 void SelectionFileOptionStorage::convertValue(const Variant &value)
291 {
292     if (bValueParsed_)
293     {
294         GMX_THROW(InvalidInputError("More than one file name provided"));
295     }
296     bValueParsed_ = true;
297     // TODO: Should we throw an InvalidInputError if the file does not exist?
298     manager_.parseRequestedFromFile(value.cast<std::string>());
299 }
300
301 void SelectionFileOptionStorage::processSet()
302 {
303     if (!bValueParsed_)
304     {
305         GMX_THROW(InvalidInputError("No file name provided"));
306     }
307 }
308
309
310 /********************************************************************
311  * SelectionFileOptionInfo
312  */
313
314 SelectionFileOptionInfo::SelectionFileOptionInfo(SelectionFileOptionStorage *option)
315     : OptionInfo(option)
316 {
317 }
318
319
320 /********************************************************************
321  * SelectionFileOption
322  */
323
324 SelectionFileOption::SelectionFileOption(const char *name)
325     : AbstractOption(name)
326 {
327     setDescription("Provide selections from files");
328 }
329
330 AbstractOptionStorage *
331 SelectionFileOption::createStorage(const OptionManagerContainer &managers) const
332 {
333     return new SelectionFileOptionStorage(
334             *this, managers.get<SelectionOptionManager>());
335 }
336
337 } // namespace gmx