Replace EnumOption with EnumerationArrayOption
[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 = { { "fs", "ps", "ns", "us", "ms",
73                                                                     "s" } };
74 /*! \brief
75  * Scaling factors from each time unit to internal units (=picoseconds).
76  *
77  * These must correspond to the TimeUnit enum in the header!
78  */
79 const EnumerationArray<TimeUnit, double> c_timeUnitScaleFactors = { { 1e-3, 1, 1e3, 1e6, 1e9, 1e12 } };
80
81 } // namespace
82
83 TimeUnitManager::TimeUnitManager() : timeUnit_(TimeUnit::Default) {}
84
85 TimeUnitManager::TimeUnitManager(TimeUnit unit) : timeUnit_(unit) {}
86
87 void TimeUnitManager::setTimeUnit(TimeUnit unit)
88 {
89     timeUnit_ = unit;
90 }
91
92 const char* TimeUnitManager::timeUnitAsString() const
93 {
94     return c_timeUnitNames[timeUnit_];
95 }
96
97 double TimeUnitManager::timeScaleFactor() const
98 {
99     return c_timeUnitScaleFactors[timeUnit_];
100 }
101
102 double TimeUnitManager::inverseTimeScaleFactor() const
103 {
104     return 1.0 / timeScaleFactor();
105 }
106
107 /********************************************************************
108  * TimeUnitBehavior
109  */
110
111 TimeUnitBehavior::TimeUnitBehavior() : timeUnit_(TimeUnit::Default), timeUnitStore_(nullptr) {}
112
113 void TimeUnitBehavior::setTimeUnit(TimeUnit unit)
114 {
115     timeUnit_ = unit;
116     if (timeUnitStore_ != nullptr)
117     {
118         *timeUnitStore_ = unit;
119     }
120 }
121
122 void TimeUnitBehavior::setTimeUnitStore(TimeUnit* store)
123 {
124     timeUnitStore_ = store;
125     *store         = timeUnit();
126 }
127
128 void TimeUnitBehavior::setTimeUnitFromEnvironment()
129 {
130     const char* const value = std::getenv("GMXTIMEUNIT");
131     if (value != nullptr)
132     {
133         TimeUnit result = TimeUnit::Count;
134         for (TimeUnit t : keysOf(c_timeUnitNames))
135         {
136             if (std::strcmp(value, c_timeUnitNames[t]) == 0)
137             {
138                 result = t;
139                 break;
140             }
141         }
142         if (result == TimeUnit::Count)
143         {
144             std::string message = formatString(
145                     "Time unit provided with environment variable GMXTIMEUNIT=%s "
146                     "is not recognized as a valid time unit.\n"
147                     "Possible values are: %s",
148                     value, joinStrings(c_timeUnitNames, ", ").c_str());
149             GMX_THROW(InvalidInputError(message));
150         }
151         setTimeUnit(result);
152     }
153 }
154
155 void TimeUnitBehavior::addTimeUnitOption(IOptionsContainer* options, const char* name)
156 {
157     options->addOption(
158             EnumOption<TimeUnit>(name).enumValue(c_timeUnitNames).store(&timeUnit_).description("Unit for time values"));
159 }
160
161 namespace
162 {
163
164 /*! \internal \brief
165  * Option visitor that scales time options.
166  *
167  * \tparam FloatingPointOptionInfo  OptionInfo type for an option that provides
168  *     isTime() and setScaleFactor() methods.
169  *
170  * \ingroup module_options
171  */
172 template<class FloatingPointOptionInfo>
173 class TimeOptionScaler : public OptionsModifyingTypeVisitor<FloatingPointOptionInfo>
174 {
175 public:
176     //! Initializes a scaler with the given factor.
177     explicit TimeOptionScaler(double factor) : factor_(factor) {}
178
179     void visitSection(OptionSectionInfo* section) override
180     {
181         OptionsModifyingIterator iterator(section);
182         iterator.acceptSections(this);
183         iterator.acceptOptions(this);
184     }
185
186     void visitOptionType(FloatingPointOptionInfo* option) override
187     {
188         if (option->isTime())
189         {
190             option->setScaleFactor(factor_);
191         }
192     }
193
194 private:
195     double factor_;
196 };
197
198 } // namespace
199
200 void TimeUnitBehavior::optionsFinishing(Options* options)
201 {
202     double factor = TimeUnitManager(timeUnit()).timeScaleFactor();
203     TimeOptionScaler<DoubleOptionInfo>(factor).visitSection(&options->rootSection());
204     TimeOptionScaler<FloatOptionInfo>(factor).visitSection(&options->rootSection());
205     if (timeUnitStore_ != nullptr)
206     {
207         *timeUnitStore_ = static_cast<TimeUnit>(timeUnit_);
208     }
209 }
210
211 } // namespace gmx