be5f88990c3179e8c5a1a74db258bc1b12c88855
[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/exceptions.h"
57 #include "gromacs/utility/gmxassert.h"
58 #include "gromacs/utility/stringutil.h"
59
60 namespace
61 {
62
63 /*! \brief
64  * Enum values for a time unit.
65  *
66  * These must correspond to the TimeUnit enum in the header!
67  */
68 const char* const g_timeUnits[] = { "fs", "ps", "ns", "us", "ms", "s" };
69 /*! \brief
70  * Scaling factors from each time unit to internal units (=picoseconds).
71  *
72  * These must correspond to the TimeUnit enum in the header!
73  */
74 const double g_timeScaleFactors[] = { 1e-3, 1, 1e3, 1e6, 1e9, 1e12 };
75
76 } // namespace
77
78 namespace gmx
79 {
80
81 TimeUnitManager::TimeUnitManager() : timeUnit_(TimeUnit_Default) {}
82
83 TimeUnitManager::TimeUnitManager(TimeUnit unit) : timeUnit_(unit)
84 {
85     GMX_RELEASE_ASSERT(unit >= 0 && unit <= TimeUnit_s, "Invalid time unit");
86 }
87
88 void TimeUnitManager::setTimeUnit(TimeUnit unit)
89 {
90     GMX_RELEASE_ASSERT(unit >= 0 && unit <= TimeUnit_s, "Invalid time unit");
91     timeUnit_ = unit;
92 }
93
94 const char* TimeUnitManager::timeUnitAsString() const
95 {
96     GMX_RELEASE_ASSERT(timeUnit_ >= 0 && timeUnit_ <= TimeUnit_s, "Invalid time unit");
97     return g_timeUnits[timeUnit_];
98 }
99
100 double TimeUnitManager::timeScaleFactor() const
101 {
102     GMX_RELEASE_ASSERT(timeUnit_ >= 0
103                                && static_cast<size_t>(timeUnit_)
104                                           < sizeof(g_timeScaleFactors) / sizeof(g_timeScaleFactors[0]),
105                        "Time unit index has become out-of-range");
106     return g_timeScaleFactors[timeUnit_];
107 }
108
109 double TimeUnitManager::inverseTimeScaleFactor() const
110 {
111     return 1.0 / timeScaleFactor();
112 }
113
114 /********************************************************************
115  * TimeUnitBehavior
116  */
117
118 TimeUnitBehavior::TimeUnitBehavior() : timeUnit_(TimeUnit_Default), timeUnitStore_(nullptr) {}
119
120 void TimeUnitBehavior::setTimeUnit(TimeUnit unit)
121 {
122     GMX_RELEASE_ASSERT(unit >= 0 && unit <= TimeUnit_s, "Invalid time unit");
123     timeUnit_ = unit;
124     if (timeUnitStore_ != nullptr)
125     {
126         *timeUnitStore_ = unit;
127     }
128 }
129
130 void TimeUnitBehavior::setTimeUnitStore(TimeUnit* store)
131 {
132     timeUnitStore_ = store;
133     *store         = timeUnit();
134 }
135
136 void TimeUnitBehavior::setTimeUnitFromEnvironment()
137 {
138     const char* const value = std::getenv("GMXTIMEUNIT");
139     if (value != nullptr)
140     {
141         ArrayRef<const char* const>                 timeUnits(g_timeUnits);
142         ArrayRef<const char* const>::const_iterator i =
143                 std::find(timeUnits.begin(), timeUnits.end(), std::string(value));
144         if (i == timeUnits.end())
145         {
146             std::string message = formatString(
147                     "Time unit provided with environment variable GMXTIMEUNIT=%s "
148                     "is not recognized as a valid time unit.\n"
149                     "Possible values are: %s",
150                     value, joinStrings(timeUnits, ", ").c_str());
151             GMX_THROW(InvalidInputError(message));
152         }
153         setTimeUnit(static_cast<TimeUnit>(i - timeUnits.begin()));
154     }
155 }
156
157 void TimeUnitBehavior::addTimeUnitOption(IOptionsContainer* options, const char* name)
158 {
159     options->addOption(
160             EnumOption<TimeUnit>(name).enumValue(g_timeUnits).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