76612b3ea3a87f17ba87de0de1e152d71b2cff77
[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-2018, The GROMACS development team.
5  * Copyright (c) 2019, by the GROMACS development team, led by
6  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7  * and including many others, as listed in the AUTHORS file in the
8  * top-level source directory and at http://www.gromacs.org.
9  *
10  * GROMACS is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  *
15  * GROMACS is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with GROMACS; if not, see
22  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24  *
25  * If you want to redistribute modifications to GROMACS, please
26  * consider that scientific software is very special. Version
27  * control is crucial - bugs must be traceable. We will be happy to
28  * consider code for inclusion in the official distribution, but
29  * derived work must not be called official GROMACS. Details are found
30  * in the README & COPYING files - if they are missing, get the
31  * official version at http://www.gromacs.org.
32  *
33  * To help us fund GROMACS development, we humbly ask that you cite
34  * the research papers on the package. Check out http://www.gromacs.org.
35  */
36 /*! \internal \file
37  * \brief
38  * Implements classes in selectionoption.h and selectionoptionstorage.h.
39  *
40  * \author Teemu Murtola <teemu.murtola@gmail.com>
41  * \ingroup module_selection
42  */
43 #include "gmxpre.h"
44
45 #include "selectionoption.h"
46
47 #include <string>
48
49 #include "gromacs/options/optionmanagercontainer.h"
50 #include "gromacs/selection/selection.h"
51 #include "gromacs/selection/selectionfileoption.h"
52 #include "gromacs/selection/selectionoptionmanager.h"
53 #include "gromacs/utility/exceptions.h"
54 #include "gromacs/utility/gmxassert.h"
55 #include "gromacs/utility/messagestringcollector.h"
56
57 #include "selectionfileoptionstorage.h"
58 #include "selectionoptionstorage.h"
59
60 namespace gmx
61 {
62
63 /********************************************************************
64  * SelectionOptionStorage
65  */
66
67 SelectionOptionStorage::SelectionOptionStorage(const SelectionOption&  settings,
68                                                SelectionOptionManager* manager) :
69     MyBase(settings, OptionFlags() | efOption_NoDefaultValue | efOption_DontCheckMinimumCount),
70     info_(this),
71     manager_(*manager),
72     defaultText_(settings.defaultText_),
73     selectionFlags_(settings.selectionFlags_)
74 {
75     GMX_RELEASE_ASSERT(manager != nullptr,
76                        "SelectionOptionManager must be added before SelectionOption");
77     GMX_RELEASE_ASSERT(!hasFlag(efOption_MultipleTimes),
78                        "allowMultiple() is not supported for selection options");
79     manager_.registerOption(this);
80 }
81
82
83 std::string SelectionOptionStorage::formatSingleValue(const Selection& value) const
84 {
85     return value.selectionText();
86 }
87
88
89 std::vector<Any> SelectionOptionStorage::normalizeValues(const std::vector<Any>& /*values*/) const
90 {
91     GMX_THROW(NotImplementedError("Selection options not supported in this context"));
92 }
93
94
95 void SelectionOptionStorage::addSelections(const SelectionList& selections, bool bFullValue)
96 {
97     if (bFullValue && selections.size() < static_cast<size_t>(minValueCount()))
98     {
99         GMX_THROW(InvalidInputError("Too few selections provided"));
100     }
101     if (bFullValue)
102     {
103         clearSet();
104     }
105     SelectionList::const_iterator i;
106     for (i = selections.begin(); i != selections.end(); ++i)
107     {
108         // TODO: Having this check in the parser would make interactive input
109         // behave better.
110         if (selectionFlags_.test(efSelection_OnlyStatic) && i->isDynamic())
111         {
112             GMX_THROW(InvalidInputError("Dynamic selections not supported"));
113         }
114         // Create a copy to allow modifications.
115         Selection sel(*i);
116         sel.data().setFlags(selectionFlags_);
117         addValue(sel);
118     }
119     if (bFullValue)
120     {
121         commitValues();
122         markAsSet();
123     }
124 }
125
126
127 void SelectionOptionStorage::convertValue(const Any& value)
128 {
129     manager_.convertOptionValue(this, value.cast<std::string>(), false);
130 }
131
132 void SelectionOptionStorage::processSetValues(ValueList* values)
133 {
134     if (values->empty())
135     {
136         manager_.requestOptionDelayedParsing(this);
137     }
138     else if (values->size() < static_cast<size_t>(minValueCount()))
139     {
140         GMX_THROW(InvalidInputError("Too few (valid) values provided"));
141     }
142 }
143
144 void SelectionOptionStorage::processAll()
145 {
146     if (!isSet() && !defaultText_.empty())
147     {
148         manager_.convertOptionValue(this, defaultText_, true);
149     }
150     if (isRequired() && !isSet())
151     {
152         manager_.requestOptionDelayedParsing(this);
153         markAsSet();
154     }
155 }
156
157 void SelectionOptionStorage::setAllowedValueCount(int count)
158 {
159     // TODO: It should be possible to have strong exception safety here.
160     // TODO: Use ExceptionInitializer here.
161     MessageStringCollector errors;
162     errors.startContext("In option '" + name() + "'");
163     if (count >= 0)
164     {
165         // Should not throw because efOption_DontCheckMinimumCount is set.
166         setMinValueCount(count);
167         if (valueCount() > 0 && valueCount() < count)
168         {
169             errors.append("Too few (valid) values provided");
170         }
171     }
172     try
173     {
174         setMaxValueCount(count);
175     }
176     catch (const UserInputError& ex)
177     {
178         errors.append(ex.what());
179     }
180     errors.finishContext();
181     if (!errors.isEmpty())
182     {
183         GMX_THROW(InvalidInputError(errors.toString()));
184     }
185 }
186
187 void SelectionOptionStorage::setSelectionFlag(SelectionFlag flag, bool bSet)
188 {
189     for (const Selection& value : values())
190     {
191         if (flag == efSelection_OnlyStatic && bSet && value.isDynamic())
192         {
193             MessageStringCollector errors;
194             errors.startContext("In option '" + name() + "'");
195             errors.append("Dynamic selections not supported");
196             errors.finishContext();
197             GMX_THROW(InvalidInputError(errors.toString()));
198         }
199     }
200     selectionFlags_.set(flag, bSet);
201     for (Selection& value : values())
202     {
203         value.data().setFlags(selectionFlags_);
204     }
205 }
206
207
208 /********************************************************************
209  * SelectionOptionInfo
210  */
211
212 SelectionOptionInfo::SelectionOptionInfo(SelectionOptionStorage* option) : OptionInfo(option) {}
213
214 SelectionOptionStorage& SelectionOptionInfo::option()
215 {
216     return static_cast<SelectionOptionStorage&>(OptionInfo::option());
217 }
218
219 const SelectionOptionStorage& SelectionOptionInfo::option() const
220 {
221     return static_cast<const SelectionOptionStorage&>(OptionInfo::option());
222 }
223
224 void SelectionOptionInfo::setValueCount(int count)
225 {
226     option().setAllowedValueCount(count);
227 }
228
229 void SelectionOptionInfo::setEvaluateVelocities(bool bEnabled)
230 {
231     option().setSelectionFlag(efSelection_EvaluateVelocities, bEnabled);
232 }
233
234 void SelectionOptionInfo::setEvaluateForces(bool bEnabled)
235 {
236     option().setSelectionFlag(efSelection_EvaluateForces, bEnabled);
237 }
238
239 void SelectionOptionInfo::setOnlyAtoms(bool bEnabled)
240 {
241     option().setSelectionFlag(efSelection_OnlyAtoms, bEnabled);
242 }
243
244 void SelectionOptionInfo::setOnlyStatic(bool bEnabled)
245 {
246     option().setSelectionFlag(efSelection_OnlyStatic, bEnabled);
247 }
248
249 void SelectionOptionInfo::setDynamicMask(bool bEnabled)
250 {
251     option().setSelectionFlag(efSelection_DynamicMask, bEnabled);
252 }
253
254
255 /********************************************************************
256  * SelectionOption
257  */
258
259 AbstractOptionStorage* SelectionOption::createStorage(const OptionManagerContainer& managers) const
260 {
261     return new SelectionOptionStorage(*this, managers.get<SelectionOptionManager>());
262 }
263
264
265 /********************************************************************
266  * SelectionFileOptionStorage
267  */
268
269 SelectionFileOptionStorage::SelectionFileOptionStorage(const SelectionFileOption& settings,
270                                                        SelectionOptionManager*    manager) :
271     AbstractOptionStorage(settings, OptionFlags() | efOption_MultipleTimes | efOption_DontCheckMinimumCount),
272     info_(this),
273     manager_(*manager),
274     bValueParsed_(false)
275 {
276     GMX_RELEASE_ASSERT(manager != nullptr,
277                        "SelectionOptionManager must be added before SelectionFileOption");
278 }
279
280 void SelectionFileOptionStorage::clearSet()
281 {
282     bValueParsed_ = false;
283 }
284
285 void SelectionFileOptionStorage::convertValue(const Any& value)
286 {
287     if (bValueParsed_)
288     {
289         GMX_THROW(InvalidInputError("More than one file name provided"));
290     }
291     bValueParsed_ = true;
292     // TODO: Should we throw an InvalidInputError if the file does not exist?
293     manager_.parseRequestedFromFile(value.cast<std::string>());
294 }
295
296 void SelectionFileOptionStorage::processSet()
297 {
298     if (!bValueParsed_)
299     {
300         GMX_THROW(InvalidInputError("No file name provided"));
301     }
302 }
303
304
305 /********************************************************************
306  * SelectionFileOptionInfo
307  */
308
309 SelectionFileOptionInfo::SelectionFileOptionInfo(SelectionFileOptionStorage* option) :
310     OptionInfo(option)
311 {
312 }
313
314
315 /********************************************************************
316  * SelectionFileOption
317  */
318
319 SelectionFileOption::SelectionFileOption(const char* name) : AbstractOption(name)
320 {
321     setDescription("Provide selections from files");
322 }
323
324 AbstractOptionStorage* SelectionFileOption::createStorage(const OptionManagerContainer& managers) const
325 {
326     return new SelectionFileOptionStorage(*this, managers.get<SelectionOptionManager>());
327 }
328
329 } // namespace gmx