Take over management of OpenCL context from PME and NBNXM
[gromacs.git] / src / gromacs / options / timeunitmanager.cpp
blobbe5f88990c3179e8c5a1a74db258bc1b12c88855
1 /*
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.
36 /*! \internal \file
37 * \brief
38 * Implements gmx::TimeUnitManager.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_options
43 #include "gmxpre.h"
45 #include "timeunitmanager.h"
47 #include <cstdlib>
49 #include <algorithm>
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"
60 namespace
63 /*! \brief
64 * Enum values for a time unit.
66 * These must correspond to the TimeUnit enum in the header!
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).
72 * These must correspond to the TimeUnit enum in the header!
74 const double g_timeScaleFactors[] = { 1e-3, 1, 1e3, 1e6, 1e9, 1e12 };
76 } // namespace
78 namespace gmx
81 TimeUnitManager::TimeUnitManager() : timeUnit_(TimeUnit_Default) {}
83 TimeUnitManager::TimeUnitManager(TimeUnit unit) : timeUnit_(unit)
85 GMX_RELEASE_ASSERT(unit >= 0 && unit <= TimeUnit_s, "Invalid time unit");
88 void TimeUnitManager::setTimeUnit(TimeUnit unit)
90 GMX_RELEASE_ASSERT(unit >= 0 && unit <= TimeUnit_s, "Invalid time unit");
91 timeUnit_ = unit;
94 const char* TimeUnitManager::timeUnitAsString() const
96 GMX_RELEASE_ASSERT(timeUnit_ >= 0 && timeUnit_ <= TimeUnit_s, "Invalid time unit");
97 return g_timeUnits[timeUnit_];
100 double TimeUnitManager::timeScaleFactor() const
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_];
109 double TimeUnitManager::inverseTimeScaleFactor() const
111 return 1.0 / timeScaleFactor();
114 /********************************************************************
115 * TimeUnitBehavior
118 TimeUnitBehavior::TimeUnitBehavior() : timeUnit_(TimeUnit_Default), timeUnitStore_(nullptr) {}
120 void TimeUnitBehavior::setTimeUnit(TimeUnit unit)
122 GMX_RELEASE_ASSERT(unit >= 0 && unit <= TimeUnit_s, "Invalid time unit");
123 timeUnit_ = unit;
124 if (timeUnitStore_ != nullptr)
126 *timeUnitStore_ = unit;
130 void TimeUnitBehavior::setTimeUnitStore(TimeUnit* store)
132 timeUnitStore_ = store;
133 *store = timeUnit();
136 void TimeUnitBehavior::setTimeUnitFromEnvironment()
138 const char* const value = std::getenv("GMXTIMEUNIT");
139 if (value != nullptr)
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())
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));
153 setTimeUnit(static_cast<TimeUnit>(i - timeUnits.begin()));
157 void TimeUnitBehavior::addTimeUnitOption(IOptionsContainer* options, const char* name)
159 options->addOption(
160 EnumOption<TimeUnit>(name).enumValue(g_timeUnits).store(&timeUnit_).description("Unit for time values"));
163 namespace
166 /*! \internal \brief
167 * Option visitor that scales time options.
169 * \tparam FloatingPointOptionInfo OptionInfo type for an option that provides
170 * isTime() and setScaleFactor() methods.
172 * \ingroup module_options
174 template<class FloatingPointOptionInfo>
175 class TimeOptionScaler : public OptionsModifyingTypeVisitor<FloatingPointOptionInfo>
177 public:
178 //! Initializes a scaler with the given factor.
179 explicit TimeOptionScaler(double factor) : factor_(factor) {}
181 void visitSection(OptionSectionInfo* section) override
183 OptionsModifyingIterator iterator(section);
184 iterator.acceptSections(this);
185 iterator.acceptOptions(this);
188 void visitOptionType(FloatingPointOptionInfo* option) override
190 if (option->isTime())
192 option->setScaleFactor(factor_);
196 private:
197 double factor_;
200 } // namespace
202 void TimeUnitBehavior::optionsFinishing(Options* options)
204 double factor = TimeUnitManager(timeUnit()).timeScaleFactor();
205 TimeOptionScaler<DoubleOptionInfo>(factor).visitSection(&options->rootSection());
206 TimeOptionScaler<FloatOptionInfo>(factor).visitSection(&options->rootSection());
207 if (timeUnitStore_ != nullptr)
209 *timeUnitStore_ = static_cast<TimeUnit>(timeUnit_);
213 } // namespace gmx