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() | efNoDefaultValue | efDontCheckMinimumCount),
64 info_(this), manager_(NULL), selectionFlags_(settings._selectionFlags)
66 GMX_RELEASE_ASSERT(!hasFlag(efMulti),
67 "allowMultiple() is not supported for selection options");
68 if (settings._infoPtr != NULL)
70 *settings._infoPtr = &info_;
75 void SelectionOptionStorage::setManager(SelectionOptionManager *manager)
77 GMX_RELEASE_ASSERT(manager_ == NULL || manager_ == manager,
78 "Manager cannot be changed once set");
81 manager->registerOption(this);
87 std::string SelectionOptionStorage::formatSingleValue(const Selection &value) const
89 return value.selectionText();
93 void SelectionOptionStorage::addSelections(
94 const SelectionList &selections,
97 if (bFullValue && selections.size() < static_cast<size_t>(minValueCount()))
99 GMX_THROW(InvalidInputError("Too few selections provided"));
105 SelectionList::const_iterator i;
106 for (i = selections.begin(); i != selections.end(); ++i)
108 // TODO: Having this check in the parser would make interactive input
110 if (selectionFlags_.test(efOnlyStatic) && i->isDynamic())
112 GMX_THROW(InvalidInputError("Dynamic selections not supported"));
124 void SelectionOptionStorage::convertValue(const std::string &value)
126 GMX_RELEASE_ASSERT(manager_ != NULL, "Manager is not set");
128 manager_->convertOptionValue(this, value);
131 void SelectionOptionStorage::processSetValues(ValueList *values)
133 GMX_RELEASE_ASSERT(manager_ != NULL, "Manager is not set");
135 if (values->size() == 0)
137 manager_->requestOptionDelayedParsing(this);
139 else if (values->size() < static_cast<size_t>(minValueCount()))
141 GMX_THROW(InvalidInputError("Too few (valid) values provided"));
143 ValueList::iterator i;
144 for (i = values->begin(); i != values->end(); ++i)
146 i->data().setFlags(selectionFlags_);
150 void SelectionOptionStorage::processAll()
152 if (isRequired() && !isSet())
154 GMX_RELEASE_ASSERT(manager_ != NULL, "Manager is not set");
156 manager_->requestOptionDelayedParsing(this);
161 void SelectionOptionStorage::setAllowedValueCount(int count)
163 // TODO: It should be possible to have strong exception safety here.
164 MessageStringCollector errors;
165 errors.startContext("In option '" + name() + "'");
168 // Should not throw because efDontCheckMinimumCount is set
169 setMinValueCount(count);
170 if (valueCount() > 0 && valueCount() < count)
172 errors.append("Too few (valid) values provided");
177 setMaxValueCount(count);
179 catch (const UserInputError &ex)
181 errors.append(ex.what());
183 errors.finishContext();
184 if (!errors.isEmpty())
186 GMX_THROW(InvalidInputError(errors.toString()));
190 void SelectionOptionStorage::setSelectionFlag(SelectionFlag flag, bool bSet)
192 ValueList::iterator i;
193 for (i = values().begin(); i != values().end(); ++i)
195 if (flag == efOnlyStatic && bSet && i->isDynamic())
197 MessageStringCollector errors;
198 errors.startContext("In option '" + name() + "'");
199 errors.append("Dynamic selections not supported");
200 errors.finishContext();
201 GMX_THROW(InvalidInputError(errors.toString()));
204 selectionFlags_.set(flag, bSet);
205 for (i = values().begin(); i != values().end(); ++i)
207 i->data().setFlags(selectionFlags_);
212 /********************************************************************
213 * SelectionOptionInfo
216 SelectionOptionInfo::SelectionOptionInfo(SelectionOptionStorage *option)
221 SelectionOptionStorage &SelectionOptionInfo::option()
223 return static_cast<SelectionOptionStorage &>(OptionInfo::option());
226 const SelectionOptionStorage &SelectionOptionInfo::option() const
228 return static_cast<const SelectionOptionStorage &>(OptionInfo::option());
231 void SelectionOptionInfo::setManager(SelectionOptionManager *manager)
233 option().setManager(manager);
236 void SelectionOptionInfo::setValueCount(int count)
238 option().setAllowedValueCount(count);
241 void SelectionOptionInfo::setEvaluateVelocities(bool bEnabled)
243 option().setSelectionFlag(efEvaluateVelocities, bEnabled);
246 void SelectionOptionInfo::setEvaluateForces(bool bEnabled)
248 option().setSelectionFlag(efEvaluateForces, bEnabled);
251 void SelectionOptionInfo::setOnlyAtoms(bool bEnabled)
253 option().setSelectionFlag(efOnlyAtoms, bEnabled);
256 void SelectionOptionInfo::setOnlyStatic(bool bEnabled)
258 option().setSelectionFlag(efOnlyStatic, bEnabled);
261 void SelectionOptionInfo::setDynamicMask(bool bEnabled)
263 option().setSelectionFlag(efDynamicMask, bEnabled);
266 void SelectionOptionInfo::setDynamicOnlyWhole(bool bEnabled)
268 option().setSelectionFlag(efDynamicOnlyWhole, bEnabled);
272 /********************************************************************
276 AbstractOptionStoragePointer SelectionOption::createStorage() const
278 return AbstractOptionStoragePointer(new SelectionOptionStorage(*this));
282 /********************************************************************
283 * SelectionFileOptionStorage
286 SelectionFileOptionStorage::SelectionFileOptionStorage(const SelectionFileOption &settings)
287 : AbstractOptionStorage(settings, OptionFlags() | efMulti | efDontCheckMinimumCount),
288 info_(this), manager_(NULL), bValueParsed_(false)
292 void SelectionFileOptionStorage::clearSet()
294 bValueParsed_ = false;
297 void SelectionFileOptionStorage::convertValue(const std::string &value)
299 GMX_RELEASE_ASSERT(manager_ != NULL, "Manager is not set");
303 GMX_THROW(InvalidInputError("More than one file name provided"));
305 bValueParsed_ = true;
306 // TODO: Should we throw an InvalidInputError if the file does not exist?
307 manager_->parseRequestedFromFile(value);
310 void SelectionFileOptionStorage::processSet()
314 GMX_THROW(InvalidInputError("No file name provided"));
319 /********************************************************************
320 * SelectionFileOptionInfo
323 SelectionFileOptionInfo::SelectionFileOptionInfo(SelectionFileOptionStorage *option)
328 SelectionFileOptionStorage &SelectionFileOptionInfo::option()
330 return static_cast<SelectionFileOptionStorage &>(OptionInfo::option());
333 const SelectionFileOptionStorage &SelectionFileOptionInfo::option() const
335 return static_cast<const SelectionFileOptionStorage &>(OptionInfo::option());
338 void SelectionFileOptionInfo::setManager(SelectionOptionManager *manager)
340 option().setManager(manager);
344 /********************************************************************
345 * SelectionFileOption
348 SelectionFileOption::SelectionFileOption(const char *name)
349 : AbstractOption(name)
351 setDescription("Provide selections from files");
354 AbstractOptionStoragePointer SelectionFileOption::createStorage() const
356 return AbstractOptionStoragePointer(new SelectionFileOptionStorage(*this));
360 /********************************************************************
368 * Visitor that sets the manager for each selection option.
370 * \ingroup module_selection
372 class SelectionOptionManagerSetter : public OptionsModifyingVisitor
375 //! Construct a visitor that sets given manager.
376 explicit SelectionOptionManagerSetter(SelectionOptionManager *manager)
381 void visitSubSection(Options *section)
383 OptionsModifyingIterator iterator(section);
384 iterator.acceptSubSections(this);
385 iterator.acceptOptions(this);
388 void visitOption(OptionInfo *option)
390 SelectionOptionInfo *selOption
391 = option->toType<SelectionOptionInfo>();
392 if (selOption != NULL)
394 selOption->setManager(manager_);
396 SelectionFileOptionInfo *selFileOption
397 = option->toType<SelectionFileOptionInfo>();
398 if (selFileOption != NULL)
400 selFileOption->setManager(manager_);
405 SelectionOptionManager *manager_;
410 void setManagerForSelectionOptions(Options *options,
411 SelectionOptionManager *manager)
413 SelectionOptionManagerSetter(manager).visitSubSection(options);