2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013,2014,2015,2016,2017,2018,2019, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Implements classes in selectionoption.h and selectionoptionstorage.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_selection
44 #include "selectionoption.h"
48 #include "gromacs/options/optionmanagercontainer.h"
49 #include "gromacs/selection/selection.h"
50 #include "gromacs/selection/selectionfileoption.h"
51 #include "gromacs/selection/selectionoptionmanager.h"
52 #include "gromacs/utility/exceptions.h"
53 #include "gromacs/utility/gmxassert.h"
54 #include "gromacs/utility/messagestringcollector.h"
56 #include "selectionfileoptionstorage.h"
57 #include "selectionoptionstorage.h"
62 /********************************************************************
63 * SelectionOptionStorage
66 SelectionOptionStorage::SelectionOptionStorage(const SelectionOption
&settings
,
67 SelectionOptionManager
*manager
)
68 : MyBase(settings
, OptionFlags() | efOption_NoDefaultValue
69 | efOption_DontCheckMinimumCount
),
70 info_(this), manager_(*manager
), defaultText_(settings
.defaultText_
),
71 selectionFlags_(settings
.selectionFlags_
)
73 GMX_RELEASE_ASSERT(manager
!= nullptr,
74 "SelectionOptionManager must be added before SelectionOption");
75 GMX_RELEASE_ASSERT(!hasFlag(efOption_MultipleTimes
),
76 "allowMultiple() is not supported for selection options");
77 manager_
.registerOption(this);
81 std::string
SelectionOptionStorage::formatSingleValue(const Selection
&value
) const
83 return value
.selectionText();
88 SelectionOptionStorage::normalizeValues(const std::vector
<Any
> & /*values*/) const
90 GMX_THROW(NotImplementedError("Selection options not supported in this context"));
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"));
115 // Create a copy to allow modifications.
117 sel
.data().setFlags(selectionFlags_
);
128 void SelectionOptionStorage::convertValue(const Any
&value
)
130 manager_
.convertOptionValue(this, value
.cast
<std::string
>(), false);
133 void SelectionOptionStorage::processSetValues(ValueList
*values
)
137 manager_
.requestOptionDelayedParsing(this);
139 else if (values
->size() < static_cast<size_t>(minValueCount()))
141 GMX_THROW(InvalidInputError("Too few (valid) values provided"));
145 void SelectionOptionStorage::processAll()
147 if (!isSet() && !defaultText_
.empty())
149 manager_
.convertOptionValue(this, defaultText_
, true);
151 if (isRequired() && !isSet())
153 manager_
.requestOptionDelayedParsing(this);
158 void SelectionOptionStorage::setAllowedValueCount(int count
)
160 // TODO: It should be possible to have strong exception safety here.
161 // TODO: Use ExceptionInitializer here.
162 MessageStringCollector errors
;
163 errors
.startContext("In option '" + name() + "'");
166 // Should not throw because efOption_DontCheckMinimumCount is set.
167 setMinValueCount(count
);
168 if (valueCount() > 0 && valueCount() < count
)
170 errors
.append("Too few (valid) values provided");
175 setMaxValueCount(count
);
177 catch (const UserInputError
&ex
)
179 errors
.append(ex
.what());
181 errors
.finishContext();
182 if (!errors
.isEmpty())
184 GMX_THROW(InvalidInputError(errors
.toString()));
188 void SelectionOptionStorage::setSelectionFlag(SelectionFlag flag
, bool bSet
)
190 for (const Selection
&value
: values())
192 if (flag
== efSelection_OnlyStatic
&& bSet
&& value
.isDynamic())
194 MessageStringCollector errors
;
195 errors
.startContext("In option '" + name() + "'");
196 errors
.append("Dynamic selections not supported");
197 errors
.finishContext();
198 GMX_THROW(InvalidInputError(errors
.toString()));
201 selectionFlags_
.set(flag
, bSet
);
202 for (Selection
&value
: values())
204 value
.data().setFlags(selectionFlags_
);
209 /********************************************************************
210 * SelectionOptionInfo
213 SelectionOptionInfo::SelectionOptionInfo(SelectionOptionStorage
*option
)
218 SelectionOptionStorage
&SelectionOptionInfo::option()
220 return static_cast<SelectionOptionStorage
&>(OptionInfo::option());
223 const SelectionOptionStorage
&SelectionOptionInfo::option() const
225 return static_cast<const SelectionOptionStorage
&>(OptionInfo::option());
228 void SelectionOptionInfo::setValueCount(int count
)
230 option().setAllowedValueCount(count
);
233 void SelectionOptionInfo::setEvaluateVelocities(bool bEnabled
)
235 option().setSelectionFlag(efSelection_EvaluateVelocities
, bEnabled
);
238 void SelectionOptionInfo::setEvaluateForces(bool bEnabled
)
240 option().setSelectionFlag(efSelection_EvaluateForces
, bEnabled
);
243 void SelectionOptionInfo::setOnlyAtoms(bool bEnabled
)
245 option().setSelectionFlag(efSelection_OnlyAtoms
, bEnabled
);
248 void SelectionOptionInfo::setOnlyStatic(bool bEnabled
)
250 option().setSelectionFlag(efSelection_OnlyStatic
, bEnabled
);
253 void SelectionOptionInfo::setDynamicMask(bool bEnabled
)
255 option().setSelectionFlag(efSelection_DynamicMask
, bEnabled
);
259 /********************************************************************
263 AbstractOptionStorage
*
264 SelectionOption::createStorage(const OptionManagerContainer
&managers
) const
266 return new SelectionOptionStorage(
267 *this, managers
.get
<SelectionOptionManager
>());
271 /********************************************************************
272 * SelectionFileOptionStorage
275 SelectionFileOptionStorage::SelectionFileOptionStorage(
276 const SelectionFileOption
&settings
, SelectionOptionManager
*manager
)
277 : AbstractOptionStorage(settings
, OptionFlags() | efOption_MultipleTimes
278 | efOption_DontCheckMinimumCount
),
279 info_(this), manager_(*manager
), bValueParsed_(false)
281 GMX_RELEASE_ASSERT(manager
!= nullptr,
282 "SelectionOptionManager must be added before SelectionFileOption");
285 void SelectionFileOptionStorage::clearSet()
287 bValueParsed_
= false;
290 void SelectionFileOptionStorage::convertValue(const Any
&value
)
294 GMX_THROW(InvalidInputError("More than one file name provided"));
296 bValueParsed_
= true;
297 // TODO: Should we throw an InvalidInputError if the file does not exist?
298 manager_
.parseRequestedFromFile(value
.cast
<std::string
>());
301 void SelectionFileOptionStorage::processSet()
305 GMX_THROW(InvalidInputError("No file name provided"));
310 /********************************************************************
311 * SelectionFileOptionInfo
314 SelectionFileOptionInfo::SelectionFileOptionInfo(SelectionFileOptionStorage
*option
)
320 /********************************************************************
321 * SelectionFileOption
324 SelectionFileOption::SelectionFileOption(const char *name
)
325 : AbstractOption(name
)
327 setDescription("Provide selections from files");
330 AbstractOptionStorage
*
331 SelectionFileOption::createStorage(const OptionManagerContainer
&managers
) const
333 return new SelectionFileOptionStorage(
334 *this, managers
.get
<SelectionOptionManager
>());