3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2009, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
33 * Implements classes in selectionoption.h and selectionoptionstorage.h.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36 * \ingroup module_selection
38 #include "selectionoption.h"
39 #include "selectionfileoption.h"
40 #include "selectionoptionstorage.h"
41 #include "selectionfileoptionstorage.h"
45 #include "gromacs/selection/selection.h"
46 #include "gromacs/selection/selectionoptionmanager.h"
47 #include "gromacs/utility/exceptions.h"
48 #include "gromacs/utility/gmxassert.h"
49 #include "gromacs/utility/messagestringcollector.h"
54 /********************************************************************
55 * SelectionOptionStorage
58 SelectionOptionStorage::SelectionOptionStorage(const SelectionOption &settings)
59 : MyBase(settings, OptionFlags() | efOption_NoDefaultValue
60 | efOption_DontCheckMinimumCount),
61 info_(this), manager_(NULL), selectionFlags_(settings.selectionFlags_)
63 GMX_RELEASE_ASSERT(!hasFlag(efOption_MultipleTimes),
64 "allowMultiple() is not supported for selection options");
68 void SelectionOptionStorage::setManager(SelectionOptionManager *manager)
70 GMX_RELEASE_ASSERT(manager_ == NULL || manager_ == manager,
71 "Manager cannot be changed once set");
74 manager->registerOption(this);
80 std::string SelectionOptionStorage::formatSingleValue(const Selection &value) const
82 return value.selectionText();
86 void SelectionOptionStorage::addSelections(
87 const SelectionList &selections,
90 if (bFullValue && selections.size() < static_cast<size_t>(minValueCount()))
92 GMX_THROW(InvalidInputError("Too few selections provided"));
98 SelectionList::const_iterator i;
99 for (i = selections.begin(); i != selections.end(); ++i)
101 // TODO: Having this check in the parser would make interactive input
103 if (selectionFlags_.test(efSelection_OnlyStatic) && i->isDynamic())
105 GMX_THROW(InvalidInputError("Dynamic selections not supported"));
117 void SelectionOptionStorage::convertValue(const std::string &value)
119 GMX_RELEASE_ASSERT(manager_ != NULL, "Manager is not set");
121 manager_->convertOptionValue(this, value);
124 void SelectionOptionStorage::processSetValues(ValueList *values)
126 GMX_RELEASE_ASSERT(manager_ != NULL, "Manager is not set");
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"));
136 ValueList::iterator i;
137 for (i = values->begin(); i != values->end(); ++i)
139 i->data().setFlags(selectionFlags_);
143 void SelectionOptionStorage::processAll()
145 if (isRequired() && !isSet())
147 GMX_RELEASE_ASSERT(manager_ != NULL, "Manager is not set");
149 manager_->requestOptionDelayedParsing(this);
154 void SelectionOptionStorage::setAllowedValueCount(int count)
156 // TODO: It should be possible to have strong exception safety here.
157 MessageStringCollector errors;
158 errors.startContext("In option '" + name() + "'");
161 // Should not throw because efOption_DontCheckMinimumCount is set.
162 setMinValueCount(count);
163 if (valueCount() > 0 && valueCount() < count)
165 errors.append("Too few (valid) values provided");
170 setMaxValueCount(count);
172 catch (const UserInputError &ex)
174 errors.append(ex.what());
176 errors.finishContext();
177 if (!errors.isEmpty())
179 GMX_THROW(InvalidInputError(errors.toString()));
183 void SelectionOptionStorage::setSelectionFlag(SelectionFlag flag, bool bSet)
185 ValueList::iterator i;
186 for (i = values().begin(); i != values().end(); ++i)
188 if (flag == efSelection_OnlyStatic && bSet && i->isDynamic())
190 MessageStringCollector errors;
191 errors.startContext("In option '" + name() + "'");
192 errors.append("Dynamic selections not supported");
193 errors.finishContext();
194 GMX_THROW(InvalidInputError(errors.toString()));
197 selectionFlags_.set(flag, bSet);
198 for (i = values().begin(); i != values().end(); ++i)
200 i->data().setFlags(selectionFlags_);
205 /********************************************************************
206 * SelectionOptionInfo
209 SelectionOptionInfo::SelectionOptionInfo(SelectionOptionStorage *option)
214 SelectionOptionStorage &SelectionOptionInfo::option()
216 return static_cast<SelectionOptionStorage &>(OptionInfo::option());
219 const SelectionOptionStorage &SelectionOptionInfo::option() const
221 return static_cast<const SelectionOptionStorage &>(OptionInfo::option());
224 void SelectionOptionInfo::setManager(SelectionOptionManager *manager)
226 option().setManager(manager);
229 void SelectionOptionInfo::setValueCount(int count)
231 option().setAllowedValueCount(count);
234 void SelectionOptionInfo::setEvaluateVelocities(bool bEnabled)
236 option().setSelectionFlag(efSelection_EvaluateVelocities, bEnabled);
239 void SelectionOptionInfo::setEvaluateForces(bool bEnabled)
241 option().setSelectionFlag(efSelection_EvaluateForces, bEnabled);
244 void SelectionOptionInfo::setOnlyAtoms(bool bEnabled)
246 option().setSelectionFlag(efSelection_OnlyAtoms, bEnabled);
249 void SelectionOptionInfo::setOnlyStatic(bool bEnabled)
251 option().setSelectionFlag(efSelection_OnlyStatic, bEnabled);
254 void SelectionOptionInfo::setDynamicMask(bool bEnabled)
256 option().setSelectionFlag(efSelection_DynamicMask, bEnabled);
259 void SelectionOptionInfo::setDynamicOnlyWhole(bool bEnabled)
261 option().setSelectionFlag(efSelection_DynamicOnlyWhole, bEnabled);
265 /********************************************************************
269 AbstractOptionStoragePointer SelectionOption::createStorage() const
271 return AbstractOptionStoragePointer(new SelectionOptionStorage(*this));
275 /********************************************************************
276 * SelectionFileOptionStorage
279 SelectionFileOptionStorage::SelectionFileOptionStorage(const SelectionFileOption &settings)
280 : AbstractOptionStorage(settings, OptionFlags() | efOption_MultipleTimes
281 | efOption_DontCheckMinimumCount),
282 info_(this), manager_(NULL), bValueParsed_(false)
286 void SelectionFileOptionStorage::clearSet()
288 bValueParsed_ = false;
291 void SelectionFileOptionStorage::convertValue(const std::string &value)
293 GMX_RELEASE_ASSERT(manager_ != NULL, "Manager is not set");
297 GMX_THROW(InvalidInputError("More than one file name provided"));
299 bValueParsed_ = true;
300 // TODO: Should we throw an InvalidInputError if the file does not exist?
301 manager_->parseRequestedFromFile(value);
304 void SelectionFileOptionStorage::processSet()
308 GMX_THROW(InvalidInputError("No file name provided"));
313 /********************************************************************
314 * SelectionFileOptionInfo
317 SelectionFileOptionInfo::SelectionFileOptionInfo(SelectionFileOptionStorage *option)
322 SelectionFileOptionStorage &SelectionFileOptionInfo::option()
324 return static_cast<SelectionFileOptionStorage &>(OptionInfo::option());
327 const SelectionFileOptionStorage &SelectionFileOptionInfo::option() const
329 return static_cast<const SelectionFileOptionStorage &>(OptionInfo::option());
332 void SelectionFileOptionInfo::setManager(SelectionOptionManager *manager)
334 option().setManager(manager);
338 /********************************************************************
339 * SelectionFileOption
342 SelectionFileOption::SelectionFileOption(const char *name)
343 : AbstractOption(name)
345 setDescription("Provide selections from files");
348 AbstractOptionStoragePointer SelectionFileOption::createStorage() const
350 return AbstractOptionStoragePointer(new SelectionFileOptionStorage(*this));