2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010-2018, The GROMACS development team.
5 * Copyright (c) 2019,2021, 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/message_string_collector.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 errors.appendIf((valueCount() > 0 && valueCount() < count),
168 "Too few (valid) values provided");
172 setMaxValueCount(count);
174 catch (const UserInputError& ex)
176 errors.append(ex.what());
178 errors.finishContext();
179 if (!errors.isEmpty())
181 GMX_THROW(InvalidInputError(errors.toString()));
185 void SelectionOptionStorage::setSelectionFlag(SelectionFlag flag, bool bSet)
187 for (const Selection& value : values())
189 if (flag == efSelection_OnlyStatic && bSet && value.isDynamic())
191 MessageStringCollector errors;
192 errors.startContext("In option '" + name() + "'");
193 errors.append("Dynamic selections not supported");
194 errors.finishContext();
195 GMX_THROW(InvalidInputError(errors.toString()));
198 selectionFlags_.set(flag, bSet);
199 for (Selection& value : values())
201 value.data().setFlags(selectionFlags_);
206 /********************************************************************
207 * SelectionOptionInfo
210 SelectionOptionInfo::SelectionOptionInfo(SelectionOptionStorage* option) : OptionInfo(option) {}
212 SelectionOptionStorage& SelectionOptionInfo::option()
214 return static_cast<SelectionOptionStorage&>(OptionInfo::option());
217 const SelectionOptionStorage& SelectionOptionInfo::option() const
219 return static_cast<const SelectionOptionStorage&>(OptionInfo::option());
222 void SelectionOptionInfo::setValueCount(int count)
224 option().setAllowedValueCount(count);
227 void SelectionOptionInfo::setEvaluateVelocities(bool bEnabled)
229 option().setSelectionFlag(efSelection_EvaluateVelocities, bEnabled);
232 void SelectionOptionInfo::setEvaluateForces(bool bEnabled)
234 option().setSelectionFlag(efSelection_EvaluateForces, bEnabled);
237 void SelectionOptionInfo::setOnlyAtoms(bool bEnabled)
239 option().setSelectionFlag(efSelection_OnlyAtoms, bEnabled);
242 void SelectionOptionInfo::setOnlyStatic(bool bEnabled)
244 option().setSelectionFlag(efSelection_OnlyStatic, bEnabled);
247 void SelectionOptionInfo::setDynamicMask(bool bEnabled)
249 option().setSelectionFlag(efSelection_DynamicMask, bEnabled);
253 /********************************************************************
257 AbstractOptionStorage* SelectionOption::createStorage(const OptionManagerContainer& managers) const
259 return new SelectionOptionStorage(*this, managers.get<SelectionOptionManager>());
263 /********************************************************************
264 * SelectionFileOptionStorage
267 SelectionFileOptionStorage::SelectionFileOptionStorage(const SelectionFileOption& settings,
268 SelectionOptionManager* manager) :
269 AbstractOptionStorage(settings, OptionFlags() | efOption_MultipleTimes | efOption_DontCheckMinimumCount),
274 GMX_RELEASE_ASSERT(manager != nullptr,
275 "SelectionOptionManager must be added before SelectionFileOption");
278 void SelectionFileOptionStorage::clearSet()
280 bValueParsed_ = false;
283 void SelectionFileOptionStorage::convertValue(const Any& 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.cast<std::string>());
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) : AbstractOption(name)
319 setDescription("Provide selections from files");
322 AbstractOptionStorage* SelectionFileOption::createStorage(const OptionManagerContainer& managers) const
324 return new SelectionFileOptionStorage(*this, managers.get<SelectionOptionManager>());