Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / options / timeunitmanager.cpp
index 9c0baeded878c0856cf27a96170cdb8f7f0458bc..b687c31bb7518ac606a33b23edfd94edd296683a 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2012,2013, by the GROMACS development team, led by
- * David van der Spoel, Berk Hess, Erik Lindahl, and including many
- * others, as listed in the AUTHORS file in the top-level source
- * directory and at http://www.gromacs.org.
+ * Copyright (c) 2012,2013,2014, by the GROMACS development team, led by
+ * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
+ * and including many others, as listed in the AUTHORS file in the
+ * top-level source directory and at http://www.gromacs.org.
  *
  * GROMACS is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
  * \author Teemu Murtola <teemu.murtola@gmail.com>
  * \ingroup module_options
  */
-#include "gromacs/options/timeunitmanager.h"
+#include "gmxpre.h"
+
+#include "timeunitmanager.h"
+
+#include <cstdlib>
+
+#include <algorithm>
 
 #include "gromacs/options/basicoptions.h"
 #include "gromacs/options/options.h"
 #include "gromacs/options/optionsvisitor.h"
+#include "gromacs/utility/arrayref.h"
+#include "gromacs/utility/exceptions.h"
 #include "gromacs/utility/gmxassert.h"
+#include "gromacs/utility/stringutil.h"
 
 namespace
 {
@@ -108,6 +117,27 @@ double TimeUnitManager::inverseTimeScaleFactor() const
     return 1.0 / timeScaleFactor();
 }
 
+void TimeUnitManager::setTimeUnitFromEnvironment()
+{
+    const char *const value = std::getenv("GMXTIMEUNIT");
+    if (value != NULL)
+    {
+        ConstArrayRef<const char *>                 timeUnits(g_timeUnits);
+        ConstArrayRef<const char *>::const_iterator i =
+            std::find(timeUnits.begin(), timeUnits.end(), std::string(value));
+        if (i == timeUnits.end())
+        {
+            std::string message = formatString(
+                        "Time unit provided with environment variable GMXTIMEUNIT=%s "
+                        "is not recognized as a valid time unit.\n"
+                        "Possible values are: %s",
+                        value, joinStrings(timeUnits, ", ").c_str());
+            GMX_THROW(InvalidInputError(message));
+        }
+        timeUnit_ = i - timeUnits.begin();
+    }
+}
+
 void TimeUnitManager::addTimeUnitOption(Options *options, const char *name)
 {
     options->addOption(StringOption(name).enumValue(g_timeUnits)
@@ -122,9 +152,13 @@ namespace
 /*! \internal \brief
  * Option visitor that scales time options.
  *
+ * \tparam FloatingPointOptionInfo  OptionInfo type for an option that provides
+ *     isTime() and setScaleFactor() methods.
+ *
  * \ingroup module_options
  */
-class TimeOptionScaler : public OptionsModifyingTypeVisitor<DoubleOptionInfo>
+template <class FloatingPointOptionInfo>
+class TimeOptionScaler : public OptionsModifyingTypeVisitor<FloatingPointOptionInfo>
 {
     public:
         //! Initializes a scaler with the given factor.
@@ -137,7 +171,7 @@ class TimeOptionScaler : public OptionsModifyingTypeVisitor<DoubleOptionInfo>
             iterator.acceptOptions(this);
         }
 
-        void visitOptionType(DoubleOptionInfo *option)
+        void visitOptionType(FloatingPointOptionInfo *option)
         {
             if (option->isTime())
             {
@@ -154,7 +188,8 @@ class TimeOptionScaler : public OptionsModifyingTypeVisitor<DoubleOptionInfo>
 void TimeUnitManager::scaleTimeOptions(Options *options) const
 {
     double factor = timeScaleFactor();
-    TimeOptionScaler(factor).visitSubSection(options);
+    TimeOptionScaler<DoubleOptionInfo>(factor).visitSubSection(options);
+    TimeOptionScaler<FloatOptionInfo>(factor).visitSubSection(options);
 }
 
 } // namespace gmx