2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010-2018, The GROMACS development team.
5 * Copyright (c) 2019, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
38 * Implements classes in selectionoption.h and selectionoptionstorage.h.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_selection
45 #include "selectionoption.h"
49 #include "gromacs/options/optionmanagercontainer.h"
50 #include "gromacs/selection/selection.h"
51 #include "gromacs/selection/selectionfileoption.h"
52 #include "gromacs/selection/selectionoptionmanager.h"
53 #include "gromacs/utility/exceptions.h"
54 #include "gromacs/utility/gmxassert.h"
55 #include "gromacs/utility/messagestringcollector.h"
57 #include "selectionfileoptionstorage.h"
58 #include "selectionoptionstorage.h"
63 /********************************************************************
64 * SelectionOptionStorage
67 SelectionOptionStorage::SelectionOptionStorage(const SelectionOption& settings,
68 SelectionOptionManager* manager) :
69 MyBase(settings, OptionFlags() | efOption_NoDefaultValue | efOption_DontCheckMinimumCount),
72 defaultText_(settings.defaultText_),
73 selectionFlags_(settings.selectionFlags_)
75 GMX_RELEASE_ASSERT(manager != nullptr,
76 "SelectionOptionManager must be added before SelectionOption");
77 GMX_RELEASE_ASSERT(!hasFlag(efOption_MultipleTimes),
78 "allowMultiple() is not supported for selection options");
79 manager_.registerOption(this);
83 std::string SelectionOptionStorage::formatSingleValue(const Selection& value) const
85 return value.selectionText();
89 std::vector<Any> SelectionOptionStorage::normalizeValues(const std::vector<Any>& /*values*/) const
91 GMX_THROW(NotImplementedError("Selection options not supported in this context"));
95 void SelectionOptionStorage::addSelections(const SelectionList& selections, bool bFullValue)
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(efSelection_OnlyStatic) && i->isDynamic())
112 GMX_THROW(InvalidInputError("Dynamic selections not supported"));
114 // Create a copy to allow modifications.
116 sel.data().setFlags(selectionFlags_);
127 void SelectionOptionStorage::convertValue(const Any& value)
129 manager_.convertOptionValue(this, value.cast<std::string>(), false);
132 void SelectionOptionStorage::processSetValues(ValueList* values)
136 manager_.requestOptionDelayedParsing(this);
138 else if (values->size() < static_cast<size_t>(minValueCount()))
140 GMX_THROW(InvalidInputError("Too few (valid) values provided"));
144 void SelectionOptionStorage::processAll()
146 if (!isSet() && !defaultText_.empty())
148 manager_.convertOptionValue(this, defaultText_, true);
150 if (isRequired() && !isSet())
152 manager_.requestOptionDelayedParsing(this);
157 void SelectionOptionStorage::setAllowedValueCount(int count)
159 // TODO: It should be possible to have strong exception safety here.
160 // TODO: Use ExceptionInitializer here.
161 MessageStringCollector errors;
162 errors.startContext("In option '" + name() + "'");
165 // Should not throw because efOption_DontCheckMinimumCount is set.
166 setMinValueCount(count);
167 if (valueCount() > 0 && valueCount() < count)
169 errors.append("Too few (valid) values provided");
174 setMaxValueCount(count);
176 catch (const UserInputError& ex)
178 errors.append(ex.what());
180 errors.finishContext();
181 if (!errors.isEmpty())
183 GMX_THROW(InvalidInputError(errors.toString()));
187 void SelectionOptionStorage::setSelectionFlag(SelectionFlag flag, bool bSet)
189 for (const Selection& value : values())
191 if (flag == efSelection_OnlyStatic && bSet && value.isDynamic())
193 MessageStringCollector errors;
194 errors.startContext("In option '" + name() + "'");
195 errors.append("Dynamic selections not supported");
196 errors.finishContext();
197 GMX_THROW(InvalidInputError(errors.toString()));
200 selectionFlags_.set(flag, bSet);
201 for (Selection& value : values())
203 value.data().setFlags(selectionFlags_);
208 /********************************************************************
209 * SelectionOptionInfo
212 SelectionOptionInfo::SelectionOptionInfo(SelectionOptionStorage* option) : OptionInfo(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::setValueCount(int count)
226 option().setAllowedValueCount(count);
229 void SelectionOptionInfo::setEvaluateVelocities(bool bEnabled)
231 option().setSelectionFlag(efSelection_EvaluateVelocities, bEnabled);
234 void SelectionOptionInfo::setEvaluateForces(bool bEnabled)
236 option().setSelectionFlag(efSelection_EvaluateForces, bEnabled);
239 void SelectionOptionInfo::setOnlyAtoms(bool bEnabled)
241 option().setSelectionFlag(efSelection_OnlyAtoms, bEnabled);
244 void SelectionOptionInfo::setOnlyStatic(bool bEnabled)
246 option().setSelectionFlag(efSelection_OnlyStatic, bEnabled);
249 void SelectionOptionInfo::setDynamicMask(bool bEnabled)
251 option().setSelectionFlag(efSelection_DynamicMask, bEnabled);
255 /********************************************************************
259 AbstractOptionStorage* SelectionOption::createStorage(const OptionManagerContainer& managers) const
261 return new SelectionOptionStorage(*this, managers.get<SelectionOptionManager>());
265 /********************************************************************
266 * SelectionFileOptionStorage
269 SelectionFileOptionStorage::SelectionFileOptionStorage(const SelectionFileOption& settings,
270 SelectionOptionManager* manager) :
271 AbstractOptionStorage(settings, OptionFlags() | efOption_MultipleTimes | efOption_DontCheckMinimumCount),
276 GMX_RELEASE_ASSERT(manager != nullptr,
277 "SelectionOptionManager must be added before SelectionFileOption");
280 void SelectionFileOptionStorage::clearSet()
282 bValueParsed_ = false;
285 void SelectionFileOptionStorage::convertValue(const Any& value)
289 GMX_THROW(InvalidInputError("More than one file name provided"));
291 bValueParsed_ = true;
292 // TODO: Should we throw an InvalidInputError if the file does not exist?
293 manager_.parseRequestedFromFile(value.cast<std::string>());
296 void SelectionFileOptionStorage::processSet()
300 GMX_THROW(InvalidInputError("No file name provided"));
305 /********************************************************************
306 * SelectionFileOptionInfo
309 SelectionFileOptionInfo::SelectionFileOptionInfo(SelectionFileOptionStorage* option) :
315 /********************************************************************
316 * SelectionFileOption
319 SelectionFileOption::SelectionFileOption(const char* name) : AbstractOption(name)
321 setDescription("Provide selections from files");
324 AbstractOptionStorage* SelectionFileOption::createStorage(const OptionManagerContainer& managers) const
326 return new SelectionFileOptionStorage(*this, managers.get<SelectionOptionManager>());