2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
37 * Implements classes in selectionoption.h and selectionoptionstorage.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_selection
44 #include "selectionoption.h"
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"
56 #include "selectionfileoptionstorage.h"
57 #include "selectionoptionstorage.h"
62 /********************************************************************
63 * SelectionOptionStorage
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_)
73 GMX_RELEASE_ASSERT(manager != NULL,
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);
81 std::string SelectionOptionStorage::formatSingleValue(const Selection &value) const
83 return value.selectionText();
87 void SelectionOptionStorage::addSelections(
88 const SelectionList &selections,
91 if (bFullValue && selections.size() < static_cast<size_t>(minValueCount()))
93 GMX_THROW(InvalidInputError("Too few selections provided"));
99 SelectionList::const_iterator i;
100 for (i = selections.begin(); i != selections.end(); ++i)
102 // TODO: Having this check in the parser would make interactive input
104 if (selectionFlags_.test(efSelection_OnlyStatic) && i->isDynamic())
106 GMX_THROW(InvalidInputError("Dynamic selections not supported"));
108 // Create a copy to allow modifications.
110 sel.data().setFlags(selectionFlags_);
121 void SelectionOptionStorage::convertValue(const std::string &value)
123 manager_.convertOptionValue(this, value, false);
126 void SelectionOptionStorage::processSetValues(ValueList *values)
128 if (values->size() == 0)
130 manager_.requestOptionDelayedParsing(this);
132 else if (values->size() < static_cast<size_t>(minValueCount()))
134 GMX_THROW(InvalidInputError("Too few (valid) values provided"));
138 void SelectionOptionStorage::processAll()
140 if (!isSet() && !defaultText_.empty())
142 manager_.convertOptionValue(this, defaultText_, true);
144 if (isRequired() && !isSet())
146 manager_.requestOptionDelayedParsing(this);
151 void SelectionOptionStorage::setAllowedValueCount(int count)
153 // TODO: It should be possible to have strong exception safety here.
154 MessageStringCollector errors;
155 errors.startContext("In option '" + name() + "'");
158 // Should not throw because efOption_DontCheckMinimumCount is set.
159 setMinValueCount(count);
160 if (valueCount() > 0 && valueCount() < count)
162 errors.append("Too few (valid) values provided");
167 setMaxValueCount(count);
169 catch (const UserInputError &ex)
171 errors.append(ex.what());
173 errors.finishContext();
174 if (!errors.isEmpty())
176 GMX_THROW(InvalidInputError(errors.toString()));
180 void SelectionOptionStorage::setSelectionFlag(SelectionFlag flag, bool bSet)
182 ValueList::iterator i;
183 for (i = values().begin(); i != values().end(); ++i)
185 if (flag == efSelection_OnlyStatic && bSet && i->isDynamic())
187 MessageStringCollector errors;
188 errors.startContext("In option '" + name() + "'");
189 errors.append("Dynamic selections not supported");
190 errors.finishContext();
191 GMX_THROW(InvalidInputError(errors.toString()));
194 selectionFlags_.set(flag, bSet);
195 for (i = values().begin(); i != values().end(); ++i)
197 i->data().setFlags(selectionFlags_);
202 /********************************************************************
203 * SelectionOptionInfo
206 SelectionOptionInfo::SelectionOptionInfo(SelectionOptionStorage *option)
211 SelectionOptionStorage &SelectionOptionInfo::option()
213 return static_cast<SelectionOptionStorage &>(OptionInfo::option());
216 const SelectionOptionStorage &SelectionOptionInfo::option() const
218 return static_cast<const SelectionOptionStorage &>(OptionInfo::option());
221 void SelectionOptionInfo::setValueCount(int count)
223 option().setAllowedValueCount(count);
226 void SelectionOptionInfo::setEvaluateVelocities(bool bEnabled)
228 option().setSelectionFlag(efSelection_EvaluateVelocities, bEnabled);
231 void SelectionOptionInfo::setEvaluateForces(bool bEnabled)
233 option().setSelectionFlag(efSelection_EvaluateForces, bEnabled);
236 void SelectionOptionInfo::setOnlyAtoms(bool bEnabled)
238 option().setSelectionFlag(efSelection_OnlyAtoms, bEnabled);
241 void SelectionOptionInfo::setOnlyStatic(bool bEnabled)
243 option().setSelectionFlag(efSelection_OnlyStatic, bEnabled);
246 void SelectionOptionInfo::setDynamicMask(bool bEnabled)
248 option().setSelectionFlag(efSelection_DynamicMask, bEnabled);
252 /********************************************************************
256 AbstractOptionStorage *
257 SelectionOption::createStorage(const OptionManagerContainer &managers) const
259 return new SelectionOptionStorage(
260 *this, managers.get<SelectionOptionManager>());
264 /********************************************************************
265 * SelectionFileOptionStorage
268 SelectionFileOptionStorage::SelectionFileOptionStorage(
269 const SelectionFileOption &settings, SelectionOptionManager *manager)
270 : AbstractOptionStorage(settings, OptionFlags() | efOption_MultipleTimes
271 | efOption_DontCheckMinimumCount),
272 info_(this), manager_(*manager), bValueParsed_(false)
274 GMX_RELEASE_ASSERT(manager != NULL,
275 "SelectionOptionManager must be added before SelectionFileOption");
278 void SelectionFileOptionStorage::clearSet()
280 bValueParsed_ = false;
283 void SelectionFileOptionStorage::convertValue(const std::string &value)
287 GMX_THROW(InvalidInputError("More than one file name provided"));
289 bValueParsed_ = true;
290 // TODO: Should we throw an InvalidInputError if the file does not exist?
291 manager_.parseRequestedFromFile(value);
294 void SelectionFileOptionStorage::processSet()
298 GMX_THROW(InvalidInputError("No file name provided"));
303 /********************************************************************
304 * SelectionFileOptionInfo
307 SelectionFileOptionInfo::SelectionFileOptionInfo(SelectionFileOptionStorage *option)
313 /********************************************************************
314 * SelectionFileOption
317 SelectionFileOption::SelectionFileOption(const char *name)
318 : AbstractOption(name)
320 setDescription("Provide selections from files");
323 AbstractOptionStorage *
324 SelectionFileOption::createStorage(const OptionManagerContainer &managers) const
326 return new SelectionFileOptionStorage(
327 *this, managers.get<SelectionOptionManager>());