SYCL: Avoid using no_init read accessor in rocFFT
[alexxy/gromacs.git] / src / gromacs / options / timeunitmanager.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015,2016 by the GROMACS development team.
5  * Copyright (c) 2017,2018,2019,2020, 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 gmx::TimeUnitManager.
39  *
40  * \author Teemu Murtola <teemu.murtola@gmail.com>
41  * \ingroup module_options
42  */
43 #include "gmxpre.h"
44
45 #include "timeunitmanager.h"
46
47 #include <cstdlib>
48
49 #include <algorithm>
50
51 #include "gromacs/options/basicoptions.h"
52 #include "gromacs/options/ioptionscontainer.h"
53 #include "gromacs/options/options.h"
54 #include "gromacs/options/optionsvisitor.h"
55 #include "gromacs/utility/arrayref.h"
56 #include "gromacs/utility/enumerationhelpers.h"
57 #include "gromacs/utility/exceptions.h"
58 #include "gromacs/utility/gmxassert.h"
59 #include "gromacs/utility/stringutil.h"
60
61 namespace gmx
62 {
63
64 namespace
65 {
66
67 /*! \brief
68  * Enum values for a time unit.
69  *
70  * These must correspond to the TimeUnit enum in the header!
71  */
72 const EnumerationArray<TimeUnit, const char*> c_timeUnitNames = {
73     { "fs", "ps", "ns", "us", "ms", "s" }
74 };
75 /*! \brief
76  * Scaling factors from each time unit to internal units (=picoseconds).
77  *
78  * These must correspond to the TimeUnit enum in the header!
79  */
80 const EnumerationArray<TimeUnit, double> c_timeUnitScaleFactors = { { 1e-3, 1, 1e3, 1e6, 1e9, 1e12 } };
81
82 } // namespace
83
84 TimeUnitManager::TimeUnitManager() : timeUnit_(TimeUnit::Default) {}
85
86 TimeUnitManager::TimeUnitManager(TimeUnit unit) : timeUnit_(unit) {}
87
88 void TimeUnitManager::setTimeUnit(TimeUnit unit)
89 {
90     timeUnit_ = unit;
91 }
92
93 const char* TimeUnitManager::timeUnitAsString() const
94 {
95     return c_timeUnitNames[timeUnit_];
96 }
97
98 double TimeUnitManager::timeScaleFactor() const
99 {
100     return c_timeUnitScaleFactors[timeUnit_];
101 }
102
103 double TimeUnitManager::inverseTimeScaleFactor() const
104 {
105     return 1.0 / timeScaleFactor();
106 }
107
108 /********************************************************************
109  * TimeUnitBehavior
110  */
111
112 TimeUnitBehavior::TimeUnitBehavior() : timeUnit_(TimeUnit::Default), timeUnitStore_(nullptr) {}
113
114 void TimeUnitBehavior::setTimeUnit(TimeUnit unit)
115 {
116     timeUnit_ = unit;
117     if (timeUnitStore_ != nullptr)
118     {
119         *timeUnitStore_ = unit;
120     }
121 }
122
123 void TimeUnitBehavior::setTimeUnitStore(TimeUnit* store)
124 {
125     timeUnitStore_ = store;
126     *store         = timeUnit();
127 }
128
129 void TimeUnitBehavior::setTimeUnitFromEnvironment()
130 {
131     const char* const value = std::getenv("GMXTIMEUNIT");
132     if (value != nullptr)
133     {
134         TimeUnit result = TimeUnit::Count;
135         for (TimeUnit t : keysOf(c_timeUnitNames))
136         {
137             if (std::strcmp(value, c_timeUnitNames[t]) == 0)
138             {
139                 result = t;
140                 break;
141             }
142         }
143         if (result == TimeUnit::Count)
144         {
145             std::string message = formatString(
146                     "Time unit provided with environment variable GMXTIMEUNIT=%s "
147                     "is not recognized as a valid time unit.\n"
148                     "Possible values are: %s",
149                     value,
150                     joinStrings(c_timeUnitNames, ", ").c_str());
151             GMX_THROW(InvalidInputError(message));
152         }
153         setTimeUnit(result);
154     }
155 }
156
157 void TimeUnitBehavior::addTimeUnitOption(IOptionsContainer* options, const char* name)
158 {
159     options->addOption(
160             EnumOption<TimeUnit>(name).enumValue(c_timeUnitNames).store(&timeUnit_).description("Unit for time values"));
161 }
162
163 namespace
164 {
165
166 /*! \internal \brief
167  * Option visitor that scales time options.
168  *
169  * \tparam FloatingPointOptionInfo  OptionInfo type for an option that provides
170  *     isTime() and setScaleFactor() methods.
171  *
172  * \ingroup module_options
173  */
174 template<class FloatingPointOptionInfo>
175 class TimeOptionScaler : public OptionsModifyingTypeVisitor<FloatingPointOptionInfo>
176 {
177 public:
178     //! Initializes a scaler with the given factor.
179     explicit TimeOptionScaler(double factor) : factor_(factor) {}
180
181     void visitSection(OptionSectionInfo* section) override
182     {
183         OptionsModifyingIterator iterator(section);
184         iterator.acceptSections(this);
185         iterator.acceptOptions(this);
186     }
187
188     void visitOptionType(FloatingPointOptionInfo* option) override
189     {
190         if (option->isTime())
191         {
192             option->setScaleFactor(factor_);
193         }
194     }
195
196 private:
197     double factor_;
198 };
199
200 } // namespace
201
202 void TimeUnitBehavior::optionsFinishing(Options* options)
203 {
204     double factor = TimeUnitManager(timeUnit()).timeScaleFactor();
205     TimeOptionScaler<DoubleOptionInfo>(factor).visitSection(&options->rootSection());
206     TimeOptionScaler<FloatOptionInfo>(factor).visitSection(&options->rootSection());
207     if (timeUnitStore_ != nullptr)
208     {
209         *timeUnitStore_ = static_cast<TimeUnit>(timeUnit_);
210     }
211 }
212
213 } // namespace gmx