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 "selectionfileoption.h"
39 #include "selectionfileoptioninfo.h"
40 #include "selectionoption.h"
41 #include "selectionoptioninfo.h"
45 #include "gromacs/options/optionsvisitor.h"
46 #include "gromacs/selection/selection.h"
47 #include "gromacs/selection/selectionoptionmanager.h"
48 #include "gromacs/utility/exceptions.h"
49 #include "gromacs/utility/gmxassert.h"
50 #include "gromacs/utility/messagestringcollector.h"
52 #include "selectionfileoptionstorage.h"
53 #include "selectionoptionstorage.h"
58 /********************************************************************
59 * SelectionOptionStorage
62 SelectionOptionStorage::SelectionOptionStorage(const SelectionOption &settings)
63 : MyBase(settings, OptionFlags() | efOption_NoDefaultValue
64 | efOption_DontCheckMinimumCount),
65 info_(this), manager_(NULL), selectionFlags_(settings.selectionFlags_)
67 GMX_RELEASE_ASSERT(!hasFlag(efOption_MultipleTimes),
68 "allowMultiple() is not supported for selection options");
69 if (settings.infoPtr_ != NULL)
71 *settings.infoPtr_ = &info_;
76 void SelectionOptionStorage::setManager(SelectionOptionManager *manager)
78 GMX_RELEASE_ASSERT(manager_ == NULL || manager_ == manager,
79 "Manager cannot be changed once set");
82 manager->registerOption(this);
88 std::string SelectionOptionStorage::formatSingleValue(const Selection &value) const
90 return value.selectionText();
94 void SelectionOptionStorage::addSelections(
95 const SelectionList &selections,
98 if (bFullValue && selections.size() < static_cast<size_t>(minValueCount()))
100 GMX_THROW(InvalidInputError("Too few selections provided"));
106 SelectionList::const_iterator i;
107 for (i = selections.begin(); i != selections.end(); ++i)
109 // TODO: Having this check in the parser would make interactive input
111 if (selectionFlags_.test(efSelection_OnlyStatic) && i->isDynamic())
113 GMX_THROW(InvalidInputError("Dynamic selections not supported"));
125 void SelectionOptionStorage::convertValue(const std::string &value)
127 GMX_RELEASE_ASSERT(manager_ != NULL, "Manager is not set");
129 manager_->convertOptionValue(this, value);
132 void SelectionOptionStorage::processSetValues(ValueList *values)
134 GMX_RELEASE_ASSERT(manager_ != NULL, "Manager is not set");
136 if (values->size() == 0)
138 manager_->requestOptionDelayedParsing(this);
140 else if (values->size() < static_cast<size_t>(minValueCount()))
142 GMX_THROW(InvalidInputError("Too few (valid) values provided"));
144 ValueList::iterator i;
145 for (i = values->begin(); i != values->end(); ++i)
147 i->data().setFlags(selectionFlags_);
151 void SelectionOptionStorage::processAll()
153 if (isRequired() && !isSet())
155 GMX_RELEASE_ASSERT(manager_ != NULL, "Manager is not set");
157 manager_->requestOptionDelayedParsing(this);
162 void SelectionOptionStorage::setAllowedValueCount(int count)
164 // TODO: It should be possible to have strong exception safety here.
165 MessageStringCollector errors;
166 errors.startContext("In option '" + name() + "'");
169 // Should not throw because efOption_DontCheckMinimumCount is set.
170 setMinValueCount(count);
171 if (valueCount() > 0 && valueCount() < count)
173 errors.append("Too few (valid) values provided");
178 setMaxValueCount(count);
180 catch (const UserInputError &ex)
182 errors.append(ex.what());
184 errors.finishContext();
185 if (!errors.isEmpty())
187 GMX_THROW(InvalidInputError(errors.toString()));
191 void SelectionOptionStorage::setSelectionFlag(SelectionFlag flag, bool bSet)
193 ValueList::iterator i;
194 for (i = values().begin(); i != values().end(); ++i)
196 if (flag == efSelection_OnlyStatic && bSet && i->isDynamic())
198 MessageStringCollector errors;
199 errors.startContext("In option '" + name() + "'");
200 errors.append("Dynamic selections not supported");
201 errors.finishContext();
202 GMX_THROW(InvalidInputError(errors.toString()));
205 selectionFlags_.set(flag, bSet);
206 for (i = values().begin(); i != values().end(); ++i)
208 i->data().setFlags(selectionFlags_);
213 /********************************************************************
214 * SelectionOptionInfo
217 SelectionOptionInfo::SelectionOptionInfo(SelectionOptionStorage *option)
222 SelectionOptionStorage &SelectionOptionInfo::option()
224 return static_cast<SelectionOptionStorage &>(OptionInfo::option());
227 const SelectionOptionStorage &SelectionOptionInfo::option() const
229 return static_cast<const SelectionOptionStorage &>(OptionInfo::option());
232 void SelectionOptionInfo::setManager(SelectionOptionManager *manager)
234 option().setManager(manager);
237 void SelectionOptionInfo::setValueCount(int count)
239 option().setAllowedValueCount(count);
242 void SelectionOptionInfo::setEvaluateVelocities(bool bEnabled)
244 option().setSelectionFlag(efSelection_EvaluateVelocities, bEnabled);
247 void SelectionOptionInfo::setEvaluateForces(bool bEnabled)
249 option().setSelectionFlag(efSelection_EvaluateForces, bEnabled);
252 void SelectionOptionInfo::setOnlyAtoms(bool bEnabled)
254 option().setSelectionFlag(efSelection_OnlyAtoms, bEnabled);
257 void SelectionOptionInfo::setOnlyStatic(bool bEnabled)
259 option().setSelectionFlag(efSelection_OnlyStatic, bEnabled);
262 void SelectionOptionInfo::setDynamicMask(bool bEnabled)
264 option().setSelectionFlag(efSelection_DynamicMask, bEnabled);
267 void SelectionOptionInfo::setDynamicOnlyWhole(bool bEnabled)
269 option().setSelectionFlag(efSelection_DynamicOnlyWhole, bEnabled);
273 /********************************************************************
277 AbstractOptionStoragePointer SelectionOption::createStorage() const
279 return AbstractOptionStoragePointer(new SelectionOptionStorage(*this));
283 /********************************************************************
284 * SelectionFileOptionStorage
287 SelectionFileOptionStorage::SelectionFileOptionStorage(const SelectionFileOption &settings)
288 : AbstractOptionStorage(settings, OptionFlags() | efOption_MultipleTimes
289 | efOption_DontCheckMinimumCount),
290 info_(this), manager_(NULL), bValueParsed_(false)
294 void SelectionFileOptionStorage::clearSet()
296 bValueParsed_ = false;
299 void SelectionFileOptionStorage::convertValue(const std::string &value)
301 GMX_RELEASE_ASSERT(manager_ != NULL, "Manager is not set");
305 GMX_THROW(InvalidInputError("More than one file name provided"));
307 bValueParsed_ = true;
308 // TODO: Should we throw an InvalidInputError if the file does not exist?
309 manager_->parseRequestedFromFile(value);
312 void SelectionFileOptionStorage::processSet()
316 GMX_THROW(InvalidInputError("No file name provided"));
321 /********************************************************************
322 * SelectionFileOptionInfo
325 SelectionFileOptionInfo::SelectionFileOptionInfo(SelectionFileOptionStorage *option)
330 SelectionFileOptionStorage &SelectionFileOptionInfo::option()
332 return static_cast<SelectionFileOptionStorage &>(OptionInfo::option());
335 const SelectionFileOptionStorage &SelectionFileOptionInfo::option() const
337 return static_cast<const SelectionFileOptionStorage &>(OptionInfo::option());
340 void SelectionFileOptionInfo::setManager(SelectionOptionManager *manager)
342 option().setManager(manager);
346 /********************************************************************
347 * SelectionFileOption
350 SelectionFileOption::SelectionFileOption(const char *name)
351 : AbstractOption(name)
353 setDescription("Provide selections from files");
356 AbstractOptionStoragePointer SelectionFileOption::createStorage() const
358 return AbstractOptionStoragePointer(new SelectionFileOptionStorage(*this));
362 /********************************************************************
370 * Visitor that sets the manager for each selection option.
372 * \ingroup module_selection
374 class SelectionOptionManagerSetter : public OptionsModifyingVisitor
377 //! Construct a visitor that sets given manager.
378 explicit SelectionOptionManagerSetter(SelectionOptionManager *manager)
383 void visitSubSection(Options *section)
385 OptionsModifyingIterator iterator(section);
386 iterator.acceptSubSections(this);
387 iterator.acceptOptions(this);
390 void visitOption(OptionInfo *option)
392 SelectionOptionInfo *selOption
393 = option->toType<SelectionOptionInfo>();
394 if (selOption != NULL)
396 selOption->setManager(manager_);
398 SelectionFileOptionInfo *selFileOption
399 = option->toType<SelectionFileOptionInfo>();
400 if (selFileOption != NULL)
402 selFileOption->setManager(manager_);
407 SelectionOptionManager *manager_;
412 void setManagerForSelectionOptions(Options *options,
413 SelectionOptionManager *manager)
415 SelectionOptionManagerSetter(manager).visitSubSection(options);