2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
38 * Implements gmx::TimeUnitManager.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_options
45 #include "timeunitmanager.h"
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"
68 * Enum values for a time unit.
70 * These must correspond to the TimeUnit enum in the header!
72 const EnumerationArray<TimeUnit, const char*> c_timeUnitNames = { { "fs", "ps", "ns", "us", "ms",
75 * Scaling factors from each time unit to internal units (=picoseconds).
77 * These must correspond to the TimeUnit enum in the header!
79 const EnumerationArray<TimeUnit, double> c_timeUnitScaleFactors = { { 1e-3, 1, 1e3, 1e6, 1e9, 1e12 } };
83 TimeUnitManager::TimeUnitManager() : timeUnit_(TimeUnit::Default) {}
85 TimeUnitManager::TimeUnitManager(TimeUnit unit) : timeUnit_(unit) {}
87 void TimeUnitManager::setTimeUnit(TimeUnit unit)
92 const char* TimeUnitManager::timeUnitAsString() const
94 return c_timeUnitNames[timeUnit_];
97 double TimeUnitManager::timeScaleFactor() const
99 return c_timeUnitScaleFactors[timeUnit_];
102 double TimeUnitManager::inverseTimeScaleFactor() const
104 return 1.0 / timeScaleFactor();
107 /********************************************************************
111 TimeUnitBehavior::TimeUnitBehavior() : timeUnit_(TimeUnit::Default), timeUnitStore_(nullptr) {}
113 void TimeUnitBehavior::setTimeUnit(TimeUnit unit)
116 if (timeUnitStore_ != nullptr)
118 *timeUnitStore_ = unit;
122 void TimeUnitBehavior::setTimeUnitStore(TimeUnit* store)
124 timeUnitStore_ = store;
128 void TimeUnitBehavior::setTimeUnitFromEnvironment()
130 const char* const value = std::getenv("GMXTIMEUNIT");
131 if (value != nullptr)
133 TimeUnit result = TimeUnit::Count;
134 for (TimeUnit t : keysOf(c_timeUnitNames))
136 if (std::strcmp(value, c_timeUnitNames[t]) == 0)
142 if (result == TimeUnit::Count)
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));
155 void TimeUnitBehavior::addTimeUnitOption(IOptionsContainer* options, const char* name)
158 EnumOption<TimeUnit>(name).enumValue(c_timeUnitNames).store(&timeUnit_).description("Unit for time values"));
165 * Option visitor that scales time options.
167 * \tparam FloatingPointOptionInfo OptionInfo type for an option that provides
168 * isTime() and setScaleFactor() methods.
170 * \ingroup module_options
172 template<class FloatingPointOptionInfo>
173 class TimeOptionScaler : public OptionsModifyingTypeVisitor<FloatingPointOptionInfo>
176 //! Initializes a scaler with the given factor.
177 explicit TimeOptionScaler(double factor) : factor_(factor) {}
179 void visitSection(OptionSectionInfo* section) override
181 OptionsModifyingIterator iterator(section);
182 iterator.acceptSections(this);
183 iterator.acceptOptions(this);
186 void visitOptionType(FloatingPointOptionInfo* option) override
188 if (option->isTime())
190 option->setScaleFactor(factor_);
200 void TimeUnitBehavior::optionsFinishing(Options* options)
202 double factor = TimeUnitManager(timeUnit()).timeScaleFactor();
203 TimeOptionScaler<DoubleOptionInfo>(factor).visitSection(&options->rootSection());
204 TimeOptionScaler<FloatOptionInfo>(factor).visitSection(&options->rootSection());
205 if (timeUnitStore_ != nullptr)
207 *timeUnitStore_ = static_cast<TimeUnit>(timeUnit_);