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