SYCL: Avoid using no_init read accessor in rocFFT
[alexxy/gromacs.git] / src / gromacs / selection / selectionoption.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
9  *
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.
14  *
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.
19  *
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.
24  *
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.
32  *
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.
35  */
36 /*! \internal \file
37  * \brief
38  * Implements classes in selectionoption.h and selectionoptionstorage.h.
39  *
40  * \author Teemu Murtola <teemu.murtola@gmail.com>
41  * \ingroup module_selection
42  */
43 #include "gmxpre.h"
44
45 #include "selectionoption.h"
46
47 #include <string>
48
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"
56
57 #include "selectionfileoptionstorage.h"
58 #include "selectionoptionstorage.h"
59
60 namespace gmx
61 {
62
63 /********************************************************************
64  * SelectionOptionStorage
65  */
66
67 SelectionOptionStorage::SelectionOptionStorage(const SelectionOption&  settings,
68                                                SelectionOptionManager* manager) :
69     MyBase(settings, OptionFlags() | efOption_NoDefaultValue | efOption_DontCheckMinimumCount),
70     info_(this),
71     manager_(*manager),
72     defaultText_(settings.defaultText_),
73     selectionFlags_(settings.selectionFlags_)
74 {
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);
80 }
81
82
83 std::string SelectionOptionStorage::formatSingleValue(const Selection& value) const
84 {
85     return value.selectionText();
86 }
87
88
89 std::vector<Any> SelectionOptionStorage::normalizeValues(const std::vector<Any>& /*values*/) const
90 {
91     GMX_THROW(NotImplementedError("Selection options not supported in this context"));
92 }
93
94
95 void SelectionOptionStorage::addSelections(const SelectionList& selections, bool bFullValue)
96 {
97     if (bFullValue && selections.size() < static_cast<size_t>(minValueCount()))
98     {
99         GMX_THROW(InvalidInputError("Too few selections provided"));
100     }
101     if (bFullValue)
102     {
103         clearSet();
104     }
105     SelectionList::const_iterator i;
106     for (i = selections.begin(); i != selections.end(); ++i)
107     {
108         // TODO: Having this check in the parser would make interactive input
109         // behave better.
110         if (selectionFlags_.test(efSelection_OnlyStatic) && i->isDynamic())
111         {
112             GMX_THROW(InvalidInputError("Dynamic selections not supported"));
113         }
114         // Create a copy to allow modifications.
115         Selection sel(*i);
116         sel.data().setFlags(selectionFlags_);
117         addValue(sel);
118     }
119     if (bFullValue)
120     {
121         commitValues();
122         markAsSet();
123     }
124 }
125
126
127 void SelectionOptionStorage::convertValue(const Any& value)
128 {
129     manager_.convertOptionValue(this, value.cast<std::string>(), false);
130 }
131
132 void SelectionOptionStorage::processSetValues(ValueList* values)
133 {
134     if (values->empty())
135     {
136         manager_.requestOptionDelayedParsing(this);
137     }
138     else if (values->size() < static_cast<size_t>(minValueCount()))
139     {
140         GMX_THROW(InvalidInputError("Too few (valid) values provided"));
141     }
142 }
143
144 void SelectionOptionStorage::processAll()
145 {
146     if (!isSet() && !defaultText_.empty())
147     {
148         manager_.convertOptionValue(this, defaultText_, true);
149     }
150     if (isRequired() && !isSet())
151     {
152         manager_.requestOptionDelayedParsing(this);
153         markAsSet();
154     }
155 }
156
157 void SelectionOptionStorage::setAllowedValueCount(int count)
158 {
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() + "'");
163     if (count >= 0)
164     {
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");
169     }
170     try
171     {
172         setMaxValueCount(count);
173     }
174     catch (const UserInputError& ex)
175     {
176         errors.append(ex.what());
177     }
178     errors.finishContext();
179     if (!errors.isEmpty())
180     {
181         GMX_THROW(InvalidInputError(errors.toString()));
182     }
183 }
184
185 void SelectionOptionStorage::setSelectionFlag(SelectionFlag flag, bool bSet)
186 {
187     for (const Selection& value : values())
188     {
189         if (flag == efSelection_OnlyStatic && bSet && value.isDynamic())
190         {
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()));
196         }
197     }
198     selectionFlags_.set(flag, bSet);
199     for (Selection& value : values())
200     {
201         value.data().setFlags(selectionFlags_);
202     }
203 }
204
205
206 /********************************************************************
207  * SelectionOptionInfo
208  */
209
210 SelectionOptionInfo::SelectionOptionInfo(SelectionOptionStorage* option) : OptionInfo(option) {}
211
212 SelectionOptionStorage& SelectionOptionInfo::option()
213 {
214     return static_cast<SelectionOptionStorage&>(OptionInfo::option());
215 }
216
217 const SelectionOptionStorage& SelectionOptionInfo::option() const
218 {
219     return static_cast<const SelectionOptionStorage&>(OptionInfo::option());
220 }
221
222 void SelectionOptionInfo::setValueCount(int count)
223 {
224     option().setAllowedValueCount(count);
225 }
226
227 void SelectionOptionInfo::setEvaluateVelocities(bool bEnabled)
228 {
229     option().setSelectionFlag(efSelection_EvaluateVelocities, bEnabled);
230 }
231
232 void SelectionOptionInfo::setEvaluateForces(bool bEnabled)
233 {
234     option().setSelectionFlag(efSelection_EvaluateForces, bEnabled);
235 }
236
237 void SelectionOptionInfo::setOnlyAtoms(bool bEnabled)
238 {
239     option().setSelectionFlag(efSelection_OnlyAtoms, bEnabled);
240 }
241
242 void SelectionOptionInfo::setOnlyStatic(bool bEnabled)
243 {
244     option().setSelectionFlag(efSelection_OnlyStatic, bEnabled);
245 }
246
247 void SelectionOptionInfo::setDynamicMask(bool bEnabled)
248 {
249     option().setSelectionFlag(efSelection_DynamicMask, bEnabled);
250 }
251
252
253 /********************************************************************
254  * SelectionOption
255  */
256
257 AbstractOptionStorage* SelectionOption::createStorage(const OptionManagerContainer& managers) const
258 {
259     return new SelectionOptionStorage(*this, managers.get<SelectionOptionManager>());
260 }
261
262
263 /********************************************************************
264  * SelectionFileOptionStorage
265  */
266
267 SelectionFileOptionStorage::SelectionFileOptionStorage(const SelectionFileOption& settings,
268                                                        SelectionOptionManager*    manager) :
269     AbstractOptionStorage(settings, OptionFlags() | efOption_MultipleTimes | efOption_DontCheckMinimumCount),
270     info_(this),
271     manager_(*manager),
272     bValueParsed_(false)
273 {
274     GMX_RELEASE_ASSERT(manager != nullptr,
275                        "SelectionOptionManager must be added before SelectionFileOption");
276 }
277
278 void SelectionFileOptionStorage::clearSet()
279 {
280     bValueParsed_ = false;
281 }
282
283 void SelectionFileOptionStorage::convertValue(const Any& value)
284 {
285     if (bValueParsed_)
286     {
287         GMX_THROW(InvalidInputError("More than one file name provided"));
288     }
289     bValueParsed_ = true;
290     // TODO: Should we throw an InvalidInputError if the file does not exist?
291     manager_.parseRequestedFromFile(value.cast<std::string>());
292 }
293
294 void SelectionFileOptionStorage::processSet()
295 {
296     if (!bValueParsed_)
297     {
298         GMX_THROW(InvalidInputError("No file name provided"));
299     }
300 }
301
302
303 /********************************************************************
304  * SelectionFileOptionInfo
305  */
306
307 SelectionFileOptionInfo::SelectionFileOptionInfo(SelectionFileOptionStorage* option) :
308     OptionInfo(option)
309 {
310 }
311
312
313 /********************************************************************
314  * SelectionFileOption
315  */
316
317 SelectionFileOption::SelectionFileOption(const char* name) : AbstractOption(name)
318 {
319     setDescription("Provide selections from files");
320 }
321
322 AbstractOptionStorage* SelectionFileOption::createStorage(const OptionManagerContainer& managers) const
323 {
324     return new SelectionFileOptionStorage(*this, managers.get<SelectionOptionManager>());
325 }
326
327 } // namespace gmx