From 20286f9986a548fa31800a3d36d46b49c811cb2e Mon Sep 17 00:00:00 2001 From: Teemu Murtola Date: Thu, 27 Aug 2015 06:33:41 +0300 Subject: [PATCH] Enable time unit and selections in ICommandLineOptionsModule Make it possible to use TimeUnitManager and SelectionOptionManager from an module that implements ICommandLineOptionsModule. The interface to actually create and associate the new options behaviors may change in the future for easier usage, but let's first see where this goes when more functionality is moved to SelectionOptionBehavior. Prerequisite for making TrajectoryAnalysisCommandLineRunner actually use ICommandLineOptionsModule, but split out from the main commit for that. Some use of C++11 range-for is introduced; it is easy to get rid of if necessary, it's just less readable and more typing. Change-Id: I57f6de0f452e6d241c2e0223aec3722812b54a27 --- src/gromacs/analysisdata/modules/plot.cpp | 2 +- src/gromacs/commandline/cmdlinehelpwriter.cpp | 21 +--- src/gromacs/commandline/cmdlinehelpwriter.h | 11 --- src/gromacs/commandline/cmdlineoptionsmodule.cpp | 22 ++++- src/gromacs/commandline/cmdlineoptionsmodule.h | 14 ++- src/gromacs/commandline/pargs.cpp | 32 +++--- src/gromacs/options.h | 2 + src/gromacs/options/CMakeLists.txt | 1 + .../behaviorcollection.cpp} | 64 ++++++------ .../behaviorcollection.h} | 77 +++++++++------ .../ioptionsbehavior.h} | 77 ++++++++------- src/gromacs/options/tests/timeunitmanager.cpp | 75 ++++++++------- src/gromacs/options/timeunitmanager.cpp | 46 +++++++-- src/gromacs/options/timeunitmanager.h | 107 ++++++++++++++------- src/gromacs/selection/selectionoptionmanager.cpp | 17 +++- src/gromacs/selection/selectionoptionmanager.h | 30 ++++++ .../trajectoryanalysis/analysissettings-impl.h | 8 +- .../trajectoryanalysis/analysissettings.cpp | 6 +- src/gromacs/trajectoryanalysis/analysissettings.h | 4 +- src/gromacs/trajectoryanalysis/cmdlinerunner.cpp | 9 +- src/gromacs/trajectoryanalysis/runnercommon.cpp | 17 ++-- src/gromacs/trajectoryanalysis/runnercommon.h | 8 +- 22 files changed, 396 insertions(+), 254 deletions(-) copy src/gromacs/{trajectoryanalysis/analysissettings-impl.h => options/behaviorcollection.cpp} (54%) copy src/gromacs/{trajectoryanalysis/analysissettings-impl.h => options/behaviorcollection.h} (50%) copy src/gromacs/{trajectoryanalysis/analysissettings-impl.h => options/ioptionsbehavior.h} (51%) diff --git a/src/gromacs/analysisdata/modules/plot.cpp b/src/gromacs/analysisdata/modules/plot.cpp index 3d9290d82f..e5dfcffe56 100644 --- a/src/gromacs/analysisdata/modules/plot.cpp +++ b/src/gromacs/analysisdata/modules/plot.cpp @@ -82,7 +82,7 @@ namespace gmx */ AnalysisDataPlotSettings::AnalysisDataPlotSettings() - : selections_(NULL), timeUnit_(eTimeUnit_ps), plotFormat_(1) + : selections_(NULL), timeUnit_(TimeUnit_Default), plotFormat_(1) { } diff --git a/src/gromacs/commandline/cmdlinehelpwriter.cpp b/src/gromacs/commandline/cmdlinehelpwriter.cpp index 4cda572678..0d1535ee23 100644 --- a/src/gromacs/commandline/cmdlinehelpwriter.cpp +++ b/src/gromacs/commandline/cmdlinehelpwriter.cpp @@ -508,7 +508,10 @@ class CommandLineHelpWriter::Impl { public: //! Sets the Options object to use for generating help. - explicit Impl(const Options &options); + explicit Impl(const Options &options) + : options_(options) + { + } //! Format the list of known issues. void formatBugs(const HelpWriterContext &context); @@ -519,15 +522,8 @@ class CommandLineHelpWriter::Impl std::string helpText_; //! List of bugs/knows issues. ConstArrayRef bugs_; - //! Time unit to show in descriptions. - std::string timeUnit_; }; -CommandLineHelpWriter::Impl::Impl(const Options &options) - : options_(options), timeUnit_(TimeUnitManager().timeUnitAsString()) -{ -} - void CommandLineHelpWriter::Impl::formatBugs(const HelpWriterContext &context) { if (bugs_.empty()) @@ -558,13 +554,6 @@ CommandLineHelpWriter::~CommandLineHelpWriter() } CommandLineHelpWriter & -CommandLineHelpWriter::setTimeUnitString(const char *timeUnit) -{ - impl_->timeUnit_ = timeUnit; - return *this; -} - -CommandLineHelpWriter & CommandLineHelpWriter::setHelpText(const std::string &help) { impl_->helpText_ = help; @@ -618,7 +607,7 @@ void CommandLineHelpWriter::writeHelp(const CommandLineHelpContext &context) writerContext.writeTextBlock(impl_->helpText_); writerContext.outputFile().writeLine(); } - CommonFormatterData common(impl_->timeUnit_.c_str()); + CommonFormatterData common(TimeUnitManager().timeUnitAsString()); OptionsListFormatter formatter(writerContext, common, "Options"); formatter.startSection("Options to specify input files:"); filter.formatSelected(OptionsFilter::eSelectInputFileOptions, diff --git a/src/gromacs/commandline/cmdlinehelpwriter.h b/src/gromacs/commandline/cmdlinehelpwriter.h index 93725fa3c8..84efd79d3b 100644 --- a/src/gromacs/commandline/cmdlinehelpwriter.h +++ b/src/gromacs/commandline/cmdlinehelpwriter.h @@ -73,17 +73,6 @@ class CommandLineHelpWriter ~CommandLineHelpWriter(); /*! \brief - * Sets time unit to show in descriptions. - * - * \param[in] timeUnit Time unit to show in descriptions. - * \throws std::bad_alloc if out of memory. - * - * For each time parameter, any "%t" in the description is replaced - * with \p timeunit. - * If not called, uses a default "ps". - */ - CommandLineHelpWriter &setTimeUnitString(const char *timeUnit); - /*! \brief * Sets the help text to print as description. * * \param[in] help Help text to show. diff --git a/src/gromacs/commandline/cmdlineoptionsmodule.cpp b/src/gromacs/commandline/cmdlineoptionsmodule.cpp index d4ad9d6964..64a697e81e 100644 --- a/src/gromacs/commandline/cmdlineoptionsmodule.cpp +++ b/src/gromacs/commandline/cmdlineoptionsmodule.cpp @@ -48,7 +48,9 @@ #include "gromacs/commandline/cmdlinehelpwriter.h" #include "gromacs/commandline/cmdlinemodulemanager.h" #include "gromacs/commandline/cmdlineparser.h" +#include "gromacs/options/behaviorcollection.h" #include "gromacs/options/filenameoptionmanager.h" +#include "gromacs/options/ioptionsbehavior.h" #include "gromacs/options/options.h" #include "gromacs/utility/arrayref.h" #include "gromacs/utility/gmxassert.h" @@ -67,15 +69,26 @@ namespace class CommandLineOptionsModuleSettings : public ICommandLineOptionsModuleSettings { public: + explicit CommandLineOptionsModuleSettings( + OptionsBehaviorCollection *behaviors) + : behaviors_(*behaviors) + { + } + const std::string &helpText() const { return helpText_; } virtual void setHelpText(const ConstArrayRef &help) { helpText_ = joinStrings(help, "\n"); } + virtual void addOptionsBehavior(const OptionsBehaviorPointer &behavior) + { + behaviors_.addBehavior(behavior); + } private: - std::string helpText_; + std::string helpText_; + OptionsBehaviorCollection &behaviors_; }; /******************************************************************** @@ -143,7 +156,8 @@ void CommandLineOptionsModule::writeHelp(const CommandLineHelpContext &context) module = moduleGuard.get(); } Options options(name(), shortDescription()); - CommandLineOptionsModuleSettings settings; + OptionsBehaviorCollection behaviors(&options); + CommandLineOptionsModuleSettings settings(&behaviors); module->initOptions(&options, &settings); CommandLineHelpWriter(options) .setHelpText(settings.helpText()) @@ -157,11 +171,13 @@ void CommandLineOptionsModule::parseOptions(int argc, char *argv[]) options.addManager(&fileoptManager); - CommandLineOptionsModuleSettings settings; + OptionsBehaviorCollection behaviors(&options); + CommandLineOptionsModuleSettings settings(&behaviors); module_->initOptions(&options, &settings); { CommandLineParser parser(&options); parser.parse(&argc, argv); + behaviors.optionsFinishing(); options.finish(); } module_->optionsFinished(); diff --git a/src/gromacs/commandline/cmdlineoptionsmodule.h b/src/gromacs/commandline/cmdlineoptionsmodule.h index 2aa8dbebdc..78e5eabfa8 100644 --- a/src/gromacs/commandline/cmdlineoptionsmodule.h +++ b/src/gromacs/commandline/cmdlineoptionsmodule.h @@ -43,6 +43,8 @@ #ifndef GMX_COMMANDLINE_CMDLINEOPTIONSMODULE_H #define GMX_COMMANDLINE_CMDLINEOPTIONSMODULE_H +#include + #include "gromacs/commandline/cmdlinemodule.h" namespace gmx @@ -52,8 +54,8 @@ template class ConstArrayRef; class CommandLineModuleManager; class ICommandLineModule; +class IOptionsBehavior; class IOptionsContainer; -class Options; /*! \brief * Settings to pass information between a CommandLineOptionsModule and generic @@ -84,6 +86,16 @@ class ICommandLineOptionsModuleSettings \endcode */ virtual void setHelpText(const ConstArrayRef &help) = 0; + /*! \brief + * Adds an option behavior that performs actions before + * ICommandLineOptionsModule::run() is called. + * + * For now, this takes a shared_ptr to make it easier for the caller to + * keep a reference to the behavior, but the behavior should be treated + * as owned by the options module after this call. + */ + virtual void addOptionsBehavior( + const boost::shared_ptr &behavior) = 0; protected: // Disallow deletion through the interface. diff --git a/src/gromacs/commandline/pargs.cpp b/src/gromacs/commandline/pargs.cpp index c310860a55..353c979552 100644 --- a/src/gromacs/commandline/pargs.cpp +++ b/src/gromacs/commandline/pargs.cpp @@ -50,6 +50,7 @@ #include "gromacs/fileio/oenv.h" #include "gromacs/fileio/timecontrol.h" #include "gromacs/options/basicoptions.h" +#include "gromacs/options/behaviorcollection.h" #include "gromacs/options/filenameoption.h" #include "gromacs/options/filenameoptionmanager.h" #include "gromacs/options/options.h" @@ -488,14 +489,14 @@ gmx_bool parse_common_args(int *argc, char *argv[], unsigned long Flags, try { - double tbegin = 0.0, tend = 0.0, tdelta = 0.0; - bool bBeginTimeSet = false, bEndTimeSet = false, bDtSet = false; - bool bView = false; - int xvgFormat = 0; - gmx::TimeUnitManager timeUnitManager; - gmx::OptionsAdapter adapter(*argc, argv); - gmx::Options options(NULL, NULL); - gmx::FileNameOptionManager fileOptManager; + double tbegin = 0.0, tend = 0.0, tdelta = 0.0; + bool bBeginTimeSet = false, bEndTimeSet = false, bDtSet = false; + bool bView = false; + int xvgFormat = 0; + gmx::OptionsAdapter adapter(*argc, argv); + gmx::Options options(NULL, NULL); + gmx::OptionsBehaviorCollection behaviors(&options); + gmx::FileNameOptionManager fileOptManager; fileOptManager.disableInputOptionChecking( FF(PCA_NOT_READ_NODE) || FF(PCA_DISABLE_INPUT_FILE_CHECKING)); @@ -526,10 +527,15 @@ gmx_bool parse_common_args(int *argc, char *argv[], unsigned long Flags, .store(&tdelta).storeIsSet(&bDtSet).timeValue() .description("Only use frame when t MOD dt = first time (%t)")); } + gmx::TimeUnit timeUnit = gmx::TimeUnit_Default; if (FF(PCA_TIME_UNIT)) { - timeUnitManager.setTimeUnitFromEnvironment(); - timeUnitManager.addTimeUnitOption(&options, "tu"); + boost::shared_ptr timeUnitBehavior( + new gmx::TimeUnitBehavior()); + timeUnitBehavior->setTimeUnitStore(&timeUnit); + timeUnitBehavior->setTimeUnitFromEnvironment(); + timeUnitBehavior->addTimeUnitOption(&options, "tu"); + behaviors.addBehavior(timeUnitBehavior); } if (FF(PCA_CAN_VIEW)) { @@ -572,7 +578,6 @@ gmx_bool parse_common_args(int *argc, char *argv[], unsigned long Flags, "only get called only on the master rank"); gmx::CommandLineHelpWriter(options) .setHelpText(gmx::constArrayRefFromArray(desc, ndesc)) - .setTimeUnitString(timeUnitManager.timeUnitAsString()) .setKnownIssues(gmx::constArrayRefFromArray(bugs, nbugs)) .writeHelp(*context); return FALSE; @@ -581,15 +586,14 @@ gmx_bool parse_common_args(int *argc, char *argv[], unsigned long Flags, /* Now parse all the command-line options */ gmx::CommandLineParser(&options).skipUnknown(FF(PCA_NOEXIT_ON_ARGS)) .parse(argc, argv); + behaviors.optionsFinishing(); options.finish(); /* set program name, command line, and default values for output options */ output_env_init(oenv, gmx::getProgramContext(), - (time_unit_t)(timeUnitManager.timeUnit() + 1), bView, + (time_unit_t)(timeUnit + 1), bView, (xvg_format_t)(xvgFormat + 1), 0); - timeUnitManager.scaleTimeOptions(&options); - /* Extract Time info from arguments */ if (bBeginTimeSet) { diff --git a/src/gromacs/options.h b/src/gromacs/options.h index 01993c4d81..966a4b7955 100644 --- a/src/gromacs/options.h +++ b/src/gromacs/options.h @@ -159,6 +159,8 @@ #include "gromacs/options/basicoptions.h" #include "gromacs/options/filenameoption.h" #include "gromacs/options/filenameoptionmanager.h" +#include "gromacs/options/ioptionsbehavior.h" +#include "gromacs/options/ioptionscontainer.h" #include "gromacs/options/options.h" #endif diff --git a/src/gromacs/options/CMakeLists.txt b/src/gromacs/options/CMakeLists.txt index d565ace779..62d006445c 100644 --- a/src/gromacs/options/CMakeLists.txt +++ b/src/gromacs/options/CMakeLists.txt @@ -40,6 +40,7 @@ gmx_install_headers( basicoptions.h filenameoption.h filenameoptionmanager.h + ioptionsbehavior.h ioptionscontainer.h optionfiletype.h optionflags.h diff --git a/src/gromacs/trajectoryanalysis/analysissettings-impl.h b/src/gromacs/options/behaviorcollection.cpp similarity index 54% copy from src/gromacs/trajectoryanalysis/analysissettings-impl.h copy to src/gromacs/options/behaviorcollection.cpp index 0939204c8f..65a6d68d33 100644 --- a/src/gromacs/trajectoryanalysis/analysissettings-impl.h +++ b/src/gromacs/options/behaviorcollection.cpp @@ -1,7 +1,7 @@ /* * This file is part of the GROMACS molecular simulation package. * - * Copyright (c) 2010,2011,2012,2014,2015, by the GROMACS development team, led by + * Copyright (c) 2015, 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. @@ -34,52 +34,46 @@ */ /*! \internal \file * \brief - * Declares private implementation class for gmx::TrajectoryAnalysisSettings. + * Implements gmx::OptionsBehaviorCollection. * - * \ingroup module_trajectoryanalysis * \author Teemu Murtola + * \ingroup module_options */ -#ifndef GMX_TRAJECTORYANALYSIS_ANALYSISSETTINGS_IMPL_H -#define GMX_TRAJECTORYANALYSIS_ANALYSISSETTINGS_IMPL_H +#include "gmxpre.h" -#include +#include "behaviorcollection.h" -#include "gromacs/analysisdata/modules/plot.h" -#include "gromacs/options/timeunitmanager.h" -#include "gromacs/trajectoryanalysis/analysissettings.h" +#include "gromacs/options/ioptionsbehavior.h" namespace gmx { -/*! \internal \brief - * Private implementation class for TrajectoryAnalysisSettings. - * - * \ingroup module_trajectoryanalysis - */ -class TrajectoryAnalysisSettings::Impl +IOptionsBehavior::~IOptionsBehavior() +{ +} + +OptionsBehaviorCollection::OptionsBehaviorCollection(Options *options) + : options_(options) { - public: - //! Initializes the default values for the settings object. - Impl() : flags(0), frflags(0), bRmPBC(true), bPBC(true) {} +} - //! Global time unit setting for the analysis module. - TimeUnitManager timeUnitManager; - //! Global plotting settings for the analysis module. - AnalysisDataPlotSettings plotSettings; - //! Flags for the analysis module. - unsigned long flags; - //! Frame reading flags for the analysis module. - int frflags; +OptionsBehaviorCollection::~OptionsBehaviorCollection() +{ +} - //! Whether to make molecules whole for each frame. - bool bRmPBC; - //! Whether to pass PBC information to the analysis module. - bool bPBC; +void OptionsBehaviorCollection::addBehavior(const OptionsBehaviorPointer &behavior) +{ + behaviors_.reserve(behaviors_.size() + 1); + behavior->initOptions(options_); + behaviors_.push_back(behavior); +} - //! Help text for the module. - std::string helpText_; -}; +void OptionsBehaviorCollection::optionsFinishing() +{ + for (const OptionsBehaviorPointer &behavior : behaviors_) + { + behavior->optionsFinishing(options_); + } +} } // namespace gmx - -#endif diff --git a/src/gromacs/trajectoryanalysis/analysissettings-impl.h b/src/gromacs/options/behaviorcollection.h similarity index 50% copy from src/gromacs/trajectoryanalysis/analysissettings-impl.h copy to src/gromacs/options/behaviorcollection.h index 0939204c8f..d5a3b6848b 100644 --- a/src/gromacs/trajectoryanalysis/analysissettings-impl.h +++ b/src/gromacs/options/behaviorcollection.h @@ -1,7 +1,7 @@ /* * This file is part of the GROMACS molecular simulation package. * - * Copyright (c) 2010,2011,2012,2014,2015, by the GROMACS development team, led by + * Copyright (c) 2015, 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. @@ -32,54 +32,69 @@ * To help us fund GROMACS development, we humbly ask that you cite * the research papers on the package. Check out http://www.gromacs.org. */ -/*! \internal \file +/*! \libinternal \file * \brief - * Declares private implementation class for gmx::TrajectoryAnalysisSettings. + * Declares gmx::OptionsBehaviorCollection. * - * \ingroup module_trajectoryanalysis * \author Teemu Murtola + * \inlibraryapi + * \ingroup module_options */ -#ifndef GMX_TRAJECTORYANALYSIS_ANALYSISSETTINGS_IMPL_H -#define GMX_TRAJECTORYANALYSIS_ANALYSISSETTINGS_IMPL_H +#ifndef GMX_OPTIONS_BEHAVIORCOLLECTION_H +#define GMX_OPTIONS_BEHAVIORCOLLECTION_H -#include +#include -#include "gromacs/analysisdata/modules/plot.h" -#include "gromacs/options/timeunitmanager.h" -#include "gromacs/trajectoryanalysis/analysissettings.h" +#include + +#include "gromacs/utility/classhelpers.h" namespace gmx { -/*! \internal \brief - * Private implementation class for TrajectoryAnalysisSettings. +class IOptionsBehavior; +class Options; + +//! Smart pointer for behaviors stored in OptionsBehaviorCollection. +typedef boost::shared_ptr OptionsBehaviorPointer; + +/*! \libinternal \brief + * Container for IOptionsBehavior objects. + * + * This class provides a container to keep IOptionsBehavior objects, and to + * call the IOptionsBehavior methods for the contained objects. + * + * IOptionsBehavior methods are called for the contained objects in the same + * order as in which the behaviors were inserted. * - * \ingroup module_trajectoryanalysis + * \inlibraryapi + * \ingroup module_options */ -class TrajectoryAnalysisSettings::Impl +class OptionsBehaviorCollection { public: - //! Initializes the default values for the settings object. - Impl() : flags(0), frflags(0), bRmPBC(true), bPBC(true) {} + /*! \brief + * Constructs a container for storing behaviors associated with given + * Options. + * + * Caller needs to ensure that provided Options remains in existence + * while the container exists. + */ + explicit OptionsBehaviorCollection(Options *options); + ~OptionsBehaviorCollection(); - //! Global time unit setting for the analysis module. - TimeUnitManager timeUnitManager; - //! Global plotting settings for the analysis module. - AnalysisDataPlotSettings plotSettings; - //! Flags for the analysis module. - unsigned long flags; - //! Frame reading flags for the analysis module. - int frflags; + //! Adds a behavior to the collection. + void addBehavior(const OptionsBehaviorPointer &behavior); + //! Calls IOptionsBehavior::optionsFinishing() on all behaviors. + void optionsFinishing(); - //! Whether to make molecules whole for each frame. - bool bRmPBC; - //! Whether to pass PBC information to the analysis module. - bool bPBC; + private: + Options *options_; + std::vector behaviors_; - //! Help text for the module. - std::string helpText_; + GMX_DISALLOW_COPY_AND_ASSIGN(OptionsBehaviorCollection); }; -} // namespace gmx +} // namespace #endif diff --git a/src/gromacs/trajectoryanalysis/analysissettings-impl.h b/src/gromacs/options/ioptionsbehavior.h similarity index 51% copy from src/gromacs/trajectoryanalysis/analysissettings-impl.h copy to src/gromacs/options/ioptionsbehavior.h index 0939204c8f..6bbc7dbe1c 100644 --- a/src/gromacs/trajectoryanalysis/analysissettings-impl.h +++ b/src/gromacs/options/ioptionsbehavior.h @@ -1,7 +1,7 @@ /* * This file is part of the GROMACS molecular simulation package. * - * Copyright (c) 2010,2011,2012,2014,2015, by the GROMACS development team, led by + * Copyright (c) 2015, 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. @@ -32,54 +32,61 @@ * To help us fund GROMACS development, we humbly ask that you cite * the research papers on the package. Check out http://www.gromacs.org. */ -/*! \internal \file +/*! \file * \brief - * Declares private implementation class for gmx::TrajectoryAnalysisSettings. + * Declares gmx::IOptionsBehavior. * - * \ingroup module_trajectoryanalysis * \author Teemu Murtola + * \inpublicapi + * \ingroup module_options */ -#ifndef GMX_TRAJECTORYANALYSIS_ANALYSISSETTINGS_IMPL_H -#define GMX_TRAJECTORYANALYSIS_ANALYSISSETTINGS_IMPL_H - -#include - -#include "gromacs/analysisdata/modules/plot.h" -#include "gromacs/options/timeunitmanager.h" -#include "gromacs/trajectoryanalysis/analysissettings.h" +#ifndef GMX_OPTIONS_IOPTIONSBEHAVIOR_H +#define GMX_OPTIONS_IOPTIONSBEHAVIOR_H namespace gmx { -/*! \internal \brief - * Private implementation class for TrajectoryAnalysisSettings. +class Options; + +/*! \brief + * Interface to provide extension points for options parsing. + * + * Currently, this is only used in the context of ICommandLineOptionsModule and + * some other command-line handling, but it is declared in the options module + * for the lack of a better place: most implementations of the interface are in + * modules that do not otherwise depend on the commandline module. * - * \ingroup module_trajectoryanalysis + * \if libapi + * Any code that wants to support these extension points needs to use + * OptionsBehaviorCollection and call the methods there at appropriate points. + * This is not (at least, not currently) integrated in any automatic way to the + * actual Options object. + * \endif + * + * \inpublicapi + * \ingroup module_options */ -class TrajectoryAnalysisSettings::Impl +class IOptionsBehavior { public: - //! Initializes the default values for the settings object. - Impl() : flags(0), frflags(0), bRmPBC(true), bPBC(true) {} - - //! Global time unit setting for the analysis module. - TimeUnitManager timeUnitManager; - //! Global plotting settings for the analysis module. - AnalysisDataPlotSettings plotSettings; - //! Flags for the analysis module. - unsigned long flags; - //! Frame reading flags for the analysis module. - int frflags; - - //! Whether to make molecules whole for each frame. - bool bRmPBC; - //! Whether to pass PBC information to the analysis module. - bool bPBC; + virtual ~IOptionsBehavior(); - //! Help text for the module. - std::string helpText_; + /*! \brief + * Called when the behavior is associated with an options object. + * + * This method can, e.g., use Options::addManager() to associate + * managers with the options object. + */ + virtual void initOptions(Options *options) = 0; + /*! \brief + * Called when all option values have been assigned. + * + * This is called just before Options::finish(), and can, e.g., do + * operations that still influence the option values. + */ + virtual void optionsFinishing(Options *options) = 0; }; -} // namespace gmx +} // namespace #endif diff --git a/src/gromacs/options/tests/timeunitmanager.cpp b/src/gromacs/options/tests/timeunitmanager.cpp index 9e5f57fd18..a66206b2e7 100644 --- a/src/gromacs/options/tests/timeunitmanager.cpp +++ b/src/gromacs/options/tests/timeunitmanager.cpp @@ -1,7 +1,7 @@ /* * This file is part of the GROMACS molecular simulation package. * - * Copyright (c) 2012,2013,2014, by the GROMACS development team, led by + * Copyright (c) 2012,2013,2014,2015, 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. @@ -34,7 +34,8 @@ */ /*! \internal \file * \brief - * Tests handling of time units with gmx::TimeUnitManager. + * Tests handling of time units with gmx::TimeUnitManager and + * gmx::TimeUnitBehavior. * * Also related functionality in gmx::DoubleOptionStorage is tested. * @@ -59,24 +60,24 @@ namespace TEST(TimeUnitManagerTest, BasicOperations) { gmx::TimeUnitManager manager; - EXPECT_EQ(gmx::eTimeUnit_ps, manager.timeUnit()); + EXPECT_EQ(gmx::TimeUnit_ps, manager.timeUnit()); EXPECT_DOUBLE_EQ(1.0, manager.timeScaleFactor()); - manager.setTimeUnit(gmx::eTimeUnit_ns); - EXPECT_EQ(gmx::eTimeUnit_ns, manager.timeUnit()); + manager.setTimeUnit(gmx::TimeUnit_ns); + EXPECT_EQ(gmx::TimeUnit_ns, manager.timeUnit()); EXPECT_DOUBLE_EQ(1e3, manager.timeScaleFactor()); EXPECT_DOUBLE_EQ(1e-3, manager.inverseTimeScaleFactor()); } -TEST(TimeUnitManagerTest, ScalesAssignedOptionValue) +TEST(TimeUnitBehaviorTest, ScalesAssignedOptionValue) { - gmx::TimeUnitManager manager; + gmx::TimeUnitBehavior behavior; - gmx::Options options(NULL, NULL); - double value = 0.0; + gmx::Options options(NULL, NULL); + double value = 0.0; using gmx::DoubleOption; ASSERT_NO_THROW_GMX(options.addOption(DoubleOption("p").store(&value).timeValue())); - gmx::OptionsAssigner assigner(&options); + gmx::OptionsAssigner assigner(&options); EXPECT_NO_THROW_GMX(assigner.start()); ASSERT_NO_THROW_GMX(assigner.startOption("p")); ASSERT_NO_THROW_GMX(assigner.appendValue("1.5")); @@ -84,31 +85,31 @@ TEST(TimeUnitManagerTest, ScalesAssignedOptionValue) EXPECT_NO_THROW_GMX(assigner.finish()); EXPECT_DOUBLE_EQ(1.5, value); - manager.setTimeUnit(gmx::eTimeUnit_ns); - manager.scaleTimeOptions(&options); + behavior.setTimeUnit(gmx::TimeUnit_ns); + behavior.optionsFinishing(&options); EXPECT_DOUBLE_EQ(1500, value); EXPECT_NO_THROW_GMX(options.finish()); - manager.setTimeUnit(gmx::eTimeUnit_us); - manager.scaleTimeOptions(&options); + behavior.setTimeUnit(gmx::TimeUnit_us); + behavior.optionsFinishing(&options); EXPECT_DOUBLE_EQ(1500000, value); - manager.setTimeUnit(gmx::eTimeUnit_fs); - manager.scaleTimeOptions(&options); + behavior.setTimeUnit(gmx::TimeUnit_fs); + behavior.optionsFinishing(&options); EXPECT_DOUBLE_EQ(0.0015, value); - manager.setTimeUnit(gmx::eTimeUnit_ps); - manager.scaleTimeOptions(&options); + behavior.setTimeUnit(gmx::TimeUnit_ps); + behavior.optionsFinishing(&options); EXPECT_DOUBLE_EQ(1.5, value); } -TEST(TimeUnitManagerTest, DoesNotScaleDefaultValues) +TEST(TimeUnitBehaviorTest, DoesNotScaleDefaultValues) { - gmx::TimeUnitManager manager; + gmx::TimeUnitBehavior behavior; - gmx::Options options(NULL, NULL); - double value = 1.5, value2 = 0.0; + gmx::Options options(NULL, NULL); + double value = 1.5, value2 = 0.0; using gmx::DoubleOption; ASSERT_NO_THROW_GMX(options.addOption(DoubleOption("p").store(&value).timeValue())); ASSERT_NO_THROW_GMX(options.addOption(DoubleOption("q").store(&value2).timeValue() @@ -122,18 +123,18 @@ TEST(TimeUnitManagerTest, DoesNotScaleDefaultValues) EXPECT_NO_THROW_GMX(options.finish()); EXPECT_DOUBLE_EQ(2.5, value2); - manager.setTimeUnit(gmx::eTimeUnit_ns); - manager.scaleTimeOptions(&options); + behavior.setTimeUnit(gmx::TimeUnit_ns); + behavior.optionsFinishing(&options); EXPECT_DOUBLE_EQ(1.5, value); EXPECT_DOUBLE_EQ(2.5, value2); } -TEST(TimeUnitManagerTest, ScalesUserInputWithMultipleSources) +TEST(TimeUnitBehaviorTest, ScalesUserInputWithMultipleSources) { - gmx::TimeUnitManager manager; + gmx::TimeUnitBehavior behavior; - gmx::Options options(NULL, NULL); - double value = 0.0; + gmx::Options options(NULL, NULL); + double value = 0.0; using gmx::DoubleOption; ASSERT_NO_THROW_GMX(options.addOption(DoubleOption("p").store(&value).timeValue())); @@ -149,20 +150,20 @@ TEST(TimeUnitManagerTest, ScalesUserInputWithMultipleSources) EXPECT_NO_THROW_GMX(options.finish()); EXPECT_DOUBLE_EQ(1.5, value); - manager.setTimeUnit(gmx::eTimeUnit_ns); - manager.scaleTimeOptions(&options); + behavior.setTimeUnit(gmx::TimeUnit_ns); + behavior.optionsFinishing(&options); EXPECT_DOUBLE_EQ(1500, value); } -TEST(TimeUnitManagerTest, TimeUnitOptionWorks) +TEST(TimeUnitBehaviorTest, TimeUnitOptionWorks) { - gmx::TimeUnitManager manager; + gmx::TimeUnitBehavior behavior; - gmx::Options options(NULL, NULL); - double value = 0.0; + gmx::Options options(NULL, NULL); + double value = 0.0; using gmx::DoubleOption; ASSERT_NO_THROW_GMX(options.addOption(DoubleOption("p").store(&value).timeValue())); - ASSERT_NO_THROW_GMX(manager.addTimeUnitOption(&options, "tu")); + ASSERT_NO_THROW_GMX(behavior.addTimeUnitOption(&options, "tu")); gmx::OptionsAssigner assigner(&options); EXPECT_NO_THROW_GMX(assigner.start()); @@ -175,8 +176,8 @@ TEST(TimeUnitManagerTest, TimeUnitOptionWorks) EXPECT_NO_THROW_GMX(assigner.finish()); EXPECT_DOUBLE_EQ(1.5, value); - EXPECT_EQ(gmx::eTimeUnit_ns, manager.timeUnit()); - manager.scaleTimeOptions(&options); + EXPECT_EQ(gmx::TimeUnit_ns, behavior.timeUnit()); + behavior.optionsFinishing(&options); EXPECT_DOUBLE_EQ(1500, value); EXPECT_NO_THROW_GMX(options.finish()); diff --git a/src/gromacs/options/timeunitmanager.cpp b/src/gromacs/options/timeunitmanager.cpp index 87b5e477b7..fed2332546 100644 --- a/src/gromacs/options/timeunitmanager.cpp +++ b/src/gromacs/options/timeunitmanager.cpp @@ -82,7 +82,7 @@ namespace gmx { TimeUnitManager::TimeUnitManager() - : timeUnit_(eTimeUnit_ps) + : timeUnit_(TimeUnit_Default) { } @@ -93,14 +93,14 @@ TimeUnitManager::TimeUnitManager(TimeUnit unit) void TimeUnitManager::setTimeUnit(TimeUnit unit) { - GMX_RELEASE_ASSERT(unit >= 0 && unit <= eTimeUnit_s, + GMX_RELEASE_ASSERT(unit >= 0 && unit <= TimeUnit_s, "Invalid time unit"); timeUnit_ = unit; } const char *TimeUnitManager::timeUnitAsString() const { - GMX_RELEASE_ASSERT(timeUnit_ >= 0 && timeUnit_ <= eTimeUnit_s, + GMX_RELEASE_ASSERT(timeUnit_ >= 0 && timeUnit_ <= TimeUnit_s, "Invalid time unit"); return g_timeUnits[timeUnit_]; } @@ -118,7 +118,33 @@ double TimeUnitManager::inverseTimeScaleFactor() const return 1.0 / timeScaleFactor(); } -void TimeUnitManager::setTimeUnitFromEnvironment() +/******************************************************************** + * TimeUnitBehavior + */ + +TimeUnitBehavior::TimeUnitBehavior() + : timeUnit_(TimeUnit_Default), timeUnitStore_(NULL) +{ +} + +void TimeUnitBehavior::setTimeUnit(TimeUnit timeUnit) +{ + GMX_RELEASE_ASSERT(timeUnit >= 0 && timeUnit <= TimeUnit_s, + "Invalid time unit"); + timeUnit_ = timeUnit; + if (timeUnitStore_ != NULL) + { + *timeUnitStore_ = timeUnit; + } +} + +void TimeUnitBehavior::setTimeUnitStore(TimeUnit *store) +{ + timeUnitStore_ = store; + *store = timeUnit(); +} + +void TimeUnitBehavior::setTimeUnitFromEnvironment() { const char *const value = std::getenv("GMXTIMEUNIT"); if (value != NULL) @@ -135,11 +161,11 @@ void TimeUnitManager::setTimeUnitFromEnvironment() value, joinStrings(timeUnits, ", ").c_str()); GMX_THROW(InvalidInputError(message)); } - timeUnit_ = i - timeUnits.begin(); + setTimeUnit(static_cast(i - timeUnits.begin())); } } -void TimeUnitManager::addTimeUnitOption(IOptionsContainer *options, const char *name) +void TimeUnitBehavior::addTimeUnitOption(IOptionsContainer *options, const char *name) { options->addOption(StringOption(name).enumValue(g_timeUnits) .defaultValue(g_timeUnits[timeUnit()]) @@ -186,11 +212,15 @@ class TimeOptionScaler : public OptionsModifyingTypeVisitor(factor).visitSubSection(options); TimeOptionScaler(factor).visitSubSection(options); + if (timeUnitStore_ != NULL) + { + *timeUnitStore_ = static_cast(timeUnit_); + } } } // namespace gmx diff --git a/src/gromacs/options/timeunitmanager.h b/src/gromacs/options/timeunitmanager.h index d35f6e5ae9..d0b6bd78cd 100644 --- a/src/gromacs/options/timeunitmanager.h +++ b/src/gromacs/options/timeunitmanager.h @@ -43,6 +43,8 @@ #ifndef GMX_OPTIONS_TIMEUNITMANAGER_H #define GMX_OPTIONS_TIMEUNITMANAGER_H +#include "gromacs/options/ioptionsbehavior.h" +#include "gromacs/utility/classhelpers.h" #include "gromacs/utility/gmxassert.h" namespace gmx @@ -63,12 +65,13 @@ class Options; */ enum TimeUnit { - eTimeUnit_fs, //!< Femtoseconds. - eTimeUnit_ps, //!< Picoseconds. - eTimeUnit_ns, //!< Nanoseconds. - eTimeUnit_us, //!< Microseconds. - eTimeUnit_ms, //!< Milliseconds. - eTimeUnit_s //!< Seconds. + TimeUnit_fs, //!< Femtoseconds. + TimeUnit_ps, //!< Picoseconds. + TimeUnit_ns, //!< Nanoseconds. + TimeUnit_us, //!< Microseconds. + TimeUnit_ms, //!< Milliseconds. + TimeUnit_s, //!< Seconds. + TimeUnit_Default = TimeUnit_ps //!< Default time unit. }; /*! \brief @@ -83,10 +86,9 @@ enum TimeUnit * * \if internal * \todo - * Most of this class is independent of the options implementation. - * To ease reuse, it could be split such that the generic part is moved to the - * utility module, and only the options-specific parts left in the options - * module. + * This class is independent of the options implementation. + * To ease reuse, it could be moved to the utility module, and only + * TimeUnitBehavior left here. * \endif * * \inpublicapi @@ -103,9 +105,9 @@ class TimeUnitManager //! Returns the currently selected time unit. TimeUnit timeUnit() const { - GMX_ASSERT(timeUnit_ >= 0 && timeUnit_ <= eTimeUnit_s, + GMX_ASSERT(timeUnit_ >= 0 && timeUnit_ <= TimeUnit_s, "Time unit index has become out-of-range"); - return static_cast(timeUnit_); + return timeUnit_; } //! Set a new time unit for the manager. void setTimeUnit(TimeUnit unit); @@ -118,8 +120,55 @@ class TimeUnitManager //! Returns the scaling factor to convert times from ps. double inverseTimeScaleFactor() const; + private: + //! Currently set time unit for this manager. + TimeUnit timeUnit_; +}; + +/*! \brief + * Options behavior to add a time unit option. + * + * This class provides functionality to add a time unit option that affects the + * input unit for time options (specified with FloatOption::timeValue() or + * DoubleOption::timeValue()). When options are finished, it scales each time + * option such that any user-given values are interpreted as given in the time + * unit specified by the user, and scaled to picoseconds. Programmatically + * given values (e.g., as default values for the options) are not scaled. + * + * \inpublicapi + * \ingroup module_options + */ +class TimeUnitBehavior : public IOptionsBehavior +{ + public: + TimeUnitBehavior(); + + //! Returns the current time unit. + TimeUnit timeUnit() const + { + GMX_ASSERT(timeUnit_ >= 0 && timeUnit_ <= TimeUnit_s, + "Time unit index has become out-of-range"); + return static_cast(timeUnit_); + } + //! Sets the time unit. + void setTimeUnit(TimeUnit unit); + /*! \brief - * Sets the time unit in this manager from an environment variable. + * Sets a storage location for the selected time unit. + * + * \param[in] store Location that will receive the selected time unit. + * + * \p *store will be set to the time unit selected by the user (or + * programmatically). The value is guaranteed to be set once the + * options have been finished. + */ + void setTimeUnitStore(TimeUnit *store); + + /*! \brief + * Sets the default time unit from an environment variable. + * + * This should be called before addTimeUnitOption() for consistent + * behavior. */ void setTimeUnitFromEnvironment(); /*! \brief @@ -129,31 +178,21 @@ class TimeUnitManager * \param[in] name Name of the option to add. * * Adds an enum option to \p options to select the time unit for this - * manager. + * behavior. */ void addTimeUnitOption(IOptionsContainer *options, const char *name); - /*! \brief - * Scales user input values given to time options. - * - * \param[in,out] options Options in which to scale times. - * - * Scales each time option (see DoubleOption::timeValue()) in - * \p options such that any user-given values are interpreted as given - * in the time unit specified by this manager, and scaled to - * picoseconds. Programmatically given values (e.g., as default values - * for the options) are not scaled. - */ - void scaleTimeOptions(Options *options) const; + + virtual void initOptions(Options * /*options*/) {} + virtual void optionsFinishing(Options *options); private: - /*! \brief - * Currently set time unit for this manager. - * - * Type is int to make it possible to use it with - * StringOption::storeEnumIndex(), but it should always one of the - * allowed values for TimeUnit. - */ - int timeUnit_; + // Type is int to make it possible to use it with + // StringOption::storeEnumIndex(), but it should always one of the + // allowed values for TimeUnit. + int timeUnit_; + TimeUnit *timeUnitStore_; + + GMX_DISALLOW_COPY_AND_ASSIGN(TimeUnitBehavior); }; } // namespace gmx diff --git a/src/gromacs/selection/selectionoptionmanager.cpp b/src/gromacs/selection/selectionoptionmanager.cpp index ee1e486d0c..943d7c35fa 100644 --- a/src/gromacs/selection/selectionoptionmanager.cpp +++ b/src/gromacs/selection/selectionoptionmanager.cpp @@ -1,7 +1,7 @@ /* * This file is part of the GROMACS molecular simulation package. * - * Copyright (c) 2012,2013,2014, by the GROMACS development team, led by + * Copyright (c) 2012,2013,2014,2015, 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. @@ -332,4 +332,19 @@ SelectionOptionManager::parseRequestedFromString(const std::string &str) impl_->placeSelectionsInRequests(selections); } +/******************************************************************** + * SelectionOptionBehavior + */ + +SelectionOptionBehavior::SelectionOptionBehavior(SelectionOptionManager *manager) + : manager_(*manager) +{ +} + +void +SelectionOptionBehavior::initOptions(Options *options) +{ + options->addManager(&manager_); +} + } // namespace gmx diff --git a/src/gromacs/selection/selectionoptionmanager.h b/src/gromacs/selection/selectionoptionmanager.h index 82e91749f4..47b1316666 100644 --- a/src/gromacs/selection/selectionoptionmanager.h +++ b/src/gromacs/selection/selectionoptionmanager.h @@ -45,6 +45,7 @@ #include +#include "gromacs/options/ioptionsbehavior.h" #include "gromacs/options/options.h" #include "gromacs/utility/classhelpers.h" @@ -210,6 +211,35 @@ class SelectionOptionManager : public IOptionManager friend class SelectionOptionStorage; }; +/*! \brief + * Options behavior to allow using SelectionOptions. + * + * The behavior needs to be added before any options are created. + * + * The current implementation is a minimal version that just makes it possible + * to use SelectionOptions in an ICommandLineOptionsModule. In the future, the + * plan is to expand it to make it also handle interactive prompting and other + * selection initialization automatically, so that the module would not need to + * explicitly use SelectionOptionManager. + * + * \inpublicapi + * \ingroup module_selection + */ +class SelectionOptionBehavior : public IOptionsBehavior +{ + public: + //! Creates a behavior to use a selected manager. + explicit SelectionOptionBehavior(SelectionOptionManager *manager); + + virtual void initOptions(Options *options); + virtual void optionsFinishing(Options * /*options*/) {} + + private: + SelectionOptionManager &manager_; + + GMX_DISALLOW_COPY_AND_ASSIGN(SelectionOptionBehavior); +}; + } // namespace gmx #endif diff --git a/src/gromacs/trajectoryanalysis/analysissettings-impl.h b/src/gromacs/trajectoryanalysis/analysissettings-impl.h index 0939204c8f..283fbfa656 100644 --- a/src/gromacs/trajectoryanalysis/analysissettings-impl.h +++ b/src/gromacs/trajectoryanalysis/analysissettings-impl.h @@ -60,10 +60,14 @@ class TrajectoryAnalysisSettings::Impl { public: //! Initializes the default values for the settings object. - Impl() : flags(0), frflags(0), bRmPBC(true), bPBC(true) {} + Impl() + : timeUnit(TimeUnit_Default), flags(0), frflags(0), + bRmPBC(true), bPBC(true) + { + } //! Global time unit setting for the analysis module. - TimeUnitManager timeUnitManager; + TimeUnit timeUnit; //! Global plotting settings for the analysis module. AnalysisDataPlotSettings plotSettings; //! Flags for the analysis module. diff --git a/src/gromacs/trajectoryanalysis/analysissettings.cpp b/src/gromacs/trajectoryanalysis/analysissettings.cpp index 1622abf870..5a379f6d38 100644 --- a/src/gromacs/trajectoryanalysis/analysissettings.cpp +++ b/src/gromacs/trajectoryanalysis/analysissettings.cpp @@ -73,10 +73,10 @@ TrajectoryAnalysisSettings::~TrajectoryAnalysisSettings() } -const TimeUnitManager & -TrajectoryAnalysisSettings::timeUnitManager() const +TimeUnit +TrajectoryAnalysisSettings::timeUnit() const { - return impl_->timeUnitManager; + return impl_->timeUnit; } diff --git a/src/gromacs/trajectoryanalysis/analysissettings.h b/src/gromacs/trajectoryanalysis/analysissettings.h index ba2b6a1225..ab18659852 100644 --- a/src/gromacs/trajectoryanalysis/analysissettings.h +++ b/src/gromacs/trajectoryanalysis/analysissettings.h @@ -125,10 +125,8 @@ class TrajectoryAnalysisSettings TrajectoryAnalysisSettings(); ~TrajectoryAnalysisSettings(); - //! Returns the time unit manager with time unit timeUnit(). - const TimeUnitManager &timeUnitManager() const; //! Returns the time unit the user has requested. - TimeUnit timeUnit() { return timeUnitManager().timeUnit(); } + TimeUnit timeUnit() const; //! Returns common settings for analysis data plot modules. const AnalysisDataPlotSettings &plotSettings() const; diff --git a/src/gromacs/trajectoryanalysis/cmdlinerunner.cpp b/src/gromacs/trajectoryanalysis/cmdlinerunner.cpp index aabcc3f39f..764c45fdc2 100644 --- a/src/gromacs/trajectoryanalysis/cmdlinerunner.cpp +++ b/src/gromacs/trajectoryanalysis/cmdlinerunner.cpp @@ -110,6 +110,7 @@ TrajectoryAnalysisCommandLineRunner::Impl::parseOptions( { FileNameOptionManager fileoptManager; SelectionOptionManager seloptManager(selections); + TimeUnitBehavior timeUnitBehavior; Options options(NULL, NULL); options.addManager(&fileoptManager); @@ -118,7 +119,7 @@ TrajectoryAnalysisCommandLineRunner::Impl::parseOptions( IOptionsContainer &moduleOptions = options.addGroup(); module_->initOptions(&moduleOptions, settings); - common->initOptions(&commonOptions); + common->initOptions(&commonOptions, &timeUnitBehavior); selections->initOptions(&commonOptions); { @@ -126,7 +127,7 @@ TrajectoryAnalysisCommandLineRunner::Impl::parseOptions( // TODO: Print the help if user provides an invalid option? // Or just add a message advising the user to invoke the help? parser.parse(argc, argv); - common->scaleTimeOptions(&options); + timeUnitBehavior.optionsFinishing(&options); options.finish(); } @@ -254,6 +255,7 @@ TrajectoryAnalysisCommandLineRunner::writeHelp(const CommandLineHelpContext &con TrajectoryAnalysisRunnerCommon common(&settings); SelectionOptionManager seloptManager(&selections); + TimeUnitBehavior timeUnitBehavior; Options options(NULL, NULL); options.addManager(&seloptManager); @@ -261,12 +263,11 @@ TrajectoryAnalysisCommandLineRunner::writeHelp(const CommandLineHelpContext &con IOptionsContainer &moduleOptions = options.addGroup(); impl_->module_->initOptions(&moduleOptions, &settings); - common.initOptions(&commonOptions); + common.initOptions(&commonOptions, &timeUnitBehavior); selections.initOptions(&commonOptions); CommandLineHelpWriter(options) .setHelpText(settings.helpText()) - .setTimeUnitString(settings.timeUnitManager().timeUnitAsString()) .writeHelp(context); } diff --git a/src/gromacs/trajectoryanalysis/runnercommon.cpp b/src/gromacs/trajectoryanalysis/runnercommon.cpp index 76c9a420ff..baf772ac49 100644 --- a/src/gromacs/trajectoryanalysis/runnercommon.cpp +++ b/src/gromacs/trajectoryanalysis/runnercommon.cpp @@ -172,7 +172,8 @@ TrajectoryAnalysisRunnerCommon::~TrajectoryAnalysisRunnerCommon() void -TrajectoryAnalysisRunnerCommon::initOptions(IOptionsContainer *options) +TrajectoryAnalysisRunnerCommon::initOptions(IOptionsContainer *options, + TimeUnitBehavior *timeUnitBehavior) { TrajectoryAnalysisSettings &settings = impl_->settings_; @@ -208,7 +209,9 @@ TrajectoryAnalysisRunnerCommon::initOptions(IOptionsContainer *options) .description("Only use frame if t MOD dt == first time (%t)")); // Add time unit option. - settings.impl_->timeUnitManager.addTimeUnitOption(options, "tu"); + timeUnitBehavior->setTimeUnitFromEnvironment(); + timeUnitBehavior->addTimeUnitOption(options, "tu"); + timeUnitBehavior->setTimeUnitStore(&impl_->settings_.impl_->timeUnit); // Add plot options. settings.impl_->plotSettings.initOptions(options); @@ -230,17 +233,9 @@ TrajectoryAnalysisRunnerCommon::initOptions(IOptionsContainer *options) void -TrajectoryAnalysisRunnerCommon::scaleTimeOptions(Options *options) -{ - impl_->settings_.impl_->timeUnitManager.scaleTimeOptions(options); -} - - -void TrajectoryAnalysisRunnerCommon::optionsFinished() { - impl_->settings_.impl_->plotSettings.setTimeUnit( - impl_->settings_.impl_->timeUnitManager.timeUnit()); + impl_->settings_.impl_->plotSettings.setTimeUnit(impl_->settings_.timeUnit()); if (impl_->trjfile_.empty() && impl_->topfile_.empty()) { diff --git a/src/gromacs/trajectoryanalysis/runnercommon.h b/src/gromacs/trajectoryanalysis/runnercommon.h index 119aa5a66e..8e79d42922 100644 --- a/src/gromacs/trajectoryanalysis/runnercommon.h +++ b/src/gromacs/trajectoryanalysis/runnercommon.h @@ -50,8 +50,8 @@ namespace gmx { class IOptionsContainer; -class Options; class SelectionCollection; +class TimeUnitBehavior; class TopologyInformation; class TrajectoryAnalysisSettings; @@ -79,10 +79,10 @@ class TrajectoryAnalysisRunnerCommon * Initializes common options for trajectory analysis. * * \param[in,out] options Options object to add the options to. + * \param[in,out] timeUnitBehavior Time unit behavior to use for adding + * and handling the `-tu` option. */ - void initOptions(IOptionsContainer *options); - //! Scales time option values according to the time unit set. - void scaleTimeOptions(Options *options); + void initOptions(IOptionsContainer *options, TimeUnitBehavior *timeUnitBehavior); //! Processes common option values after they have been parsed. void optionsFinished(); //! Initialize index groups for selections. -- 2.11.4.GIT