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