Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / trajectoryanalysis / runnercommon.cpp
index e33b4adca07394ce5f8fa9041b706f7d9e3847b3..4d9513ed28d6268e8245c7eebea9125b1f535e7f 100644 (file)
@@ -1,63 +1,69 @@
 /*
+ * This file is part of the GROMACS molecular simulation package.
  *
- *                This source code is part of
+ * Copyright (c) 2010,2011,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.
  *
- *                 G   R   O   M   A   C   S
+ * GROMACS is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
  *
- *          GROningen MAchine for Chemical Simulations
+ * GROMACS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
  *
- * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
- * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
- * Copyright (c) 2001-2009, The GROMACS development team,
- * check out http://www.gromacs.org for more information.
-
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GROMACS; if not, see
+ * http://www.gnu.org/licenses, or write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
  *
- * If you want to redistribute modifications, please consider that
- * scientific software is very special. Version control is crucial -
- * bugs must be traceable. We will be happy to consider code for
- * inclusion in the official distribution, but derived work must not
- * be called official GROMACS. Details are found in the README & COPYING
- * files - if they are missing, get the official version at www.gromacs.org.
+ * If you want to redistribute modifications to GROMACS, please
+ * consider that scientific software is very special. Version
+ * control is crucial - bugs must be traceable. We will be happy to
+ * consider code for inclusion in the official distribution, but
+ * derived work must not be called official GROMACS. Details are found
+ * in the README & COPYING files - if they are missing, get the
+ * official version at http://www.gromacs.org.
  *
  * To help us fund GROMACS development, we humbly ask that you cite
- * the papers on the package - you can find them in the top README file.
- *
- * For more info, check our website at http://www.gromacs.org
+ * the research papers on the package. Check out http://www.gromacs.org.
  */
 /*! \internal \file
  * \brief
  * Implements gmx::TrajectoryAnalysisRunnerCommon.
  *
- * \author Teemu Murtola <teemu.murtola@cbr.su.se>
+ * \author Teemu Murtola <teemu.murtola@gmail.com>
  * \ingroup module_trajectoryanalysis
  */
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
+#include "gmxpre.h"
 
-#include <string.h>
+#include "runnercommon.h"
 
-#include "oenv.h"
-#include "rmpbc.h"
-#include "smalloc.h"
-#include "statutil.h"
-#include "tpxio.h"
-#include "vec.h"
+#include <string.h>
 
+#include "gromacs/fileio/timecontrol.h"
+#include "gromacs/fileio/tpxio.h"
+#include "gromacs/fileio/trx.h"
+#include "gromacs/fileio/trxio.h"
+#include "gromacs/legacyheaders/oenv.h"
+#include "gromacs/math/vec.h"
 #include "gromacs/options/basicoptions.h"
 #include "gromacs/options/filenameoption.h"
 #include "gromacs/options/options.h"
+#include "gromacs/pbcutil/rmpbc.h"
 #include "gromacs/selection/indexutil.h"
 #include "gromacs/selection/selectioncollection.h"
 #include "gromacs/selection/selectionfileoption.h"
+#include "gromacs/topology/topology.h"
 #include "gromacs/trajectoryanalysis/analysissettings.h"
-#include "gromacs/trajectoryanalysis/runnercommon.h"
 #include "gromacs/utility/exceptions.h"
 #include "gromacs/utility/gmxassert.h"
+#include "gromacs/utility/programcontext.h"
+#include "gromacs/utility/smalloc.h"
 #include "gromacs/utility/stringutil.h"
 
 #include "analysissettings-impl.h"
@@ -74,35 +80,31 @@ class TrajectoryAnalysisRunnerCommon::Impl
         void finishTrajectory();
 
         TrajectoryAnalysisSettings &settings_;
-        TopologyInformation     topInfo_;
+        TopologyInformation         topInfo_;
 
-        bool                    bHelp_;
-        bool                    bShowHidden_;
-        bool                    bQuiet_;
         //! Name of the trajectory file (empty if not provided).
-        std::string             trjfile_;
+        std::string                 trjfile_;
         //! Name of the topology file (empty if no topology provided).
-        std::string             topfile_;
+        std::string                 topfile_;
         //! Name of the index file (empty if no index file provided).
-        std::string             ndxfile_;
-        double                  startTime_;
-        double                  endTime_;
-        double                  deltaTime_;
+        std::string                 ndxfile_;
+        double                      startTime_;
+        double                      endTime_;
+        double                      deltaTime_;
 
-        gmx_ana_indexgrps_t    *grps_;
-        bool                    bTrajOpen_;
+        gmx_ana_indexgrps_t        *grps_;
+        bool                        bTrajOpen_;
         //! The current frame, or \p NULL if no frame loaded yet.
-        t_trxframe          *fr;
-        gmx_rmpbc_t             gpbc_;
+        t_trxframe                 *fr;
+        gmx_rmpbc_t                 gpbc_;
         //! Used to store the status variable from read_first_frame().
-        t_trxstatus            *status_;
-        output_env_t            oenv_;
+        t_trxstatus                *status_;
+        output_env_t                oenv_;
 };
 
 
 TrajectoryAnalysisRunnerCommon::Impl::Impl(TrajectoryAnalysisSettings *settings)
     : settings_(*settings),
-      bHelp_(false), bShowHidden_(false), bQuiet_(false),
       startTime_(0.0), endTime_(0.0), deltaTime_(0.0),
       grps_(NULL),
       bTrajOpen_(false), fr(NULL), gpbc_(NULL), status_(NULL), oenv_(NULL)
@@ -168,16 +170,6 @@ TrajectoryAnalysisRunnerCommon::initOptions(Options *options)
 {
     TrajectoryAnalysisSettings &settings = impl_->settings_;
 
-    // Add options for help.
-    options->addOption(BooleanOption("h").store(&impl_->bHelp_)
-                           .description("Print help and quit"));
-    options->addOption(BooleanOption("hidden").store(&impl_->bShowHidden_)
-                           .hidden()
-                           .description("Show hidden options"));
-    options->addOption(BooleanOption("quiet").store(&impl_->bQuiet_)
-                           .hidden()
-                           .description("Hide options in normal run"));
-
     // Add common file name arguments.
     options->addOption(FileNameOption("f")
                            .filetype(eftTrajectory).inputFile()
@@ -194,7 +186,6 @@ TrajectoryAnalysisRunnerCommon::initOptions(Options *options)
                            .store(&impl_->ndxfile_)
                            .defaultBasename("index")
                            .description("Extra index groups"));
-    options->addOption(SelectionFileOption("sf"));
 
     // Add options for trajectory time control.
     options->addOption(DoubleOption("b").store(&impl_->startTime_).timeValue()
@@ -221,6 +212,8 @@ TrajectoryAnalysisRunnerCommon::initOptions(Options *options)
         options->addOption(BooleanOption("pbc").store(&settings.impl_->bPBC)
                                .description("Use periodic boundary conditions for distance calculation"));
     }
+
+    options->addOption(SelectionFileOption("sf"));
 }
 
 
@@ -231,14 +224,9 @@ TrajectoryAnalysisRunnerCommon::scaleTimeOptions(Options *options)
 }
 
 
-bool
+void
 TrajectoryAnalysisRunnerCommon::optionsFinished(Options *options)
 {
-    if (impl_->bHelp_)
-    {
-        return false;
-    }
-
     impl_->settings_.impl_->plotSettings.setTimeUnit(
             impl_->settings_.impl_->timeUnitManager.timeUnit());
 
@@ -248,29 +236,37 @@ TrajectoryAnalysisRunnerCommon::optionsFinished(Options *options)
     }
 
     if (options->isSet("b"))
+    {
         setTimeValue(TBEGIN, impl_->startTime_);
+    }
     if (options->isSet("e"))
+    {
         setTimeValue(TEND, impl_->endTime_);
+    }
     if (options->isSet("dt"))
+    {
         setTimeValue(TDELTA, impl_->deltaTime_);
-
-    return true;
+    }
 }
 
 
 void
-TrajectoryAnalysisRunnerCommon::initIndexGroups(SelectionCollection *selections)
+TrajectoryAnalysisRunnerCommon::initIndexGroups(SelectionCollection *selections,
+                                                bool                 bUseDefaults)
 {
     if (impl_->ndxfile_.empty())
     {
-        // TODO: Initialize default selections
-        selections->setIndexGroups(NULL);
-    }
-    else
-    {
-        gmx_ana_indexgrps_init(&impl_->grps_, NULL, impl_->ndxfile_.c_str());
-        selections->setIndexGroups(impl_->grps_);
+        if (!bUseDefaults)
+        {
+            selections->setIndexGroups(NULL);
+            return;
+        }
+        initTopology(selections);
     }
+    const char *const ndxfile
+        = (!impl_->ndxfile_.empty() ? impl_->ndxfile_.c_str() : NULL);
+    gmx_ana_indexgrps_init(&impl_->grps_, impl_->topInfo_.topology(), ndxfile);
+    selections->setIndexGroups(impl_->grps_);
 }
 
 
@@ -289,10 +285,16 @@ TrajectoryAnalysisRunnerCommon::doneIndexGroups(SelectionCollection *selections)
 void
 TrajectoryAnalysisRunnerCommon::initTopology(SelectionCollection *selections)
 {
+    // Return immediately if the topology has already been loaded.
+    if (impl_->topInfo_.hasTopology())
+    {
+        return;
+    }
+
     const TrajectoryAnalysisSettings &settings = impl_->settings_;
-    bool bRequireTop
+    const bool bRequireTop
         = settings.hasFlag(TrajectoryAnalysisSettings::efRequireTop)
-          || selections->requiresTopology();
+            || selections->requiresTopology();
     if (bRequireTop && impl_->topfile_.empty())
     {
         GMX_THROW(InconsistentInputError("No topology provided, but one is required for analysis"));
@@ -305,8 +307,8 @@ TrajectoryAnalysisRunnerCommon::initTopology(SelectionCollection *selections)
 
         snew(impl_->topInfo_.top_, 1);
         impl_->topInfo_.bTop_ = read_tps_conf(impl_->topfile_.c_str(), title,
-                impl_->topInfo_.top_, &impl_->topInfo_.ePBC_,
-                &impl_->topInfo_.xtop_, NULL, impl_->topInfo_.boxtop_, TRUE);
+                                              impl_->topInfo_.top_, &impl_->topInfo_.ePBC_,
+                                              &impl_->topInfo_.xtop_, NULL, impl_->topInfo_.boxtop_, TRUE);
         if (hasTrajectory()
             && !settings.hasFlag(TrajectoryAnalysisSettings::efUseTopX))
         {
@@ -326,12 +328,12 @@ TrajectoryAnalysisRunnerCommon::initTopology(SelectionCollection *selections)
     selections->setTopology(impl_->topInfo_.topology(), natoms);
 
     /*
-    if (impl_->bSelDump)
-    {
+       if (impl_->bSelDump)
+       {
         gmx_ana_poscalc_coll_print_tree(stderr, impl_->pcc);
         fprintf(stderr, "\n");
-    }
-    */
+       }
+     */
 }
 
 
@@ -345,7 +347,7 @@ TrajectoryAnalysisRunnerCommon::initFirstFrame()
     }
     time_unit_t time_unit
         = static_cast<time_unit_t>(impl_->settings_.timeUnit() + 1);
-    output_env_init(&impl_->oenv_, 0, NULL, time_unit, FALSE, exvgNONE, 0, 0);
+    output_env_init(&impl_->oenv_, getProgramContext(), time_unit, FALSE, exvgNONE, 0);
 
     int frflags = impl_->settings_.frflags();
     frflags |= TRX_NEED_X;
@@ -365,20 +367,9 @@ TrajectoryAnalysisRunnerCommon::initFirstFrame()
         if (top.hasTopology() && impl_->fr->natoms > top.topology()->atoms.nr)
         {
             GMX_THROW(InconsistentInputError(formatString(
-                      "Trajectory (%d atoms) does not match topology (%d atoms)",
-                      impl_->fr->natoms, top.topology()->atoms.nr)));
-        }
-        // Check index groups if they have been initialized based on the topology.
-        /*
-        if (top)
-        {
-            for (int i = 0; i < impl_->sel->nr(); ++i)
-            {
-                gmx_ana_index_check(impl_->sel->sel(i)->indexGroup(),
-                                    impl_->fr->natoms);
-            }
+                                                     "Trajectory (%d atoms) does not match topology (%d atoms)",
+                                                     impl_->fr->natoms, top.topology()->atoms.nr)));
         }
-        */
     }
     else
     {
@@ -406,7 +397,7 @@ TrajectoryAnalysisRunnerCommon::initFirstFrame()
     if (top.hasTopology() && impl_->settings_.hasRmPBC())
     {
         impl_->gpbc_ = gmx_rmpbc_init(&top.topology()->idef, top.ePBC(),
-                                      impl_->fr->natoms, impl_->fr->box);
+                                      impl_->fr->natoms);
     }
 }
 
@@ -437,26 +428,6 @@ TrajectoryAnalysisRunnerCommon::initFrame()
 }
 
 
-TrajectoryAnalysisRunnerCommon::HelpFlags
-TrajectoryAnalysisRunnerCommon::helpFlags() const
-{
-    HelpFlags flags = 0;
-
-    if (!impl_->bQuiet_)
-    {
-        flags |= efHelpShowOptions;
-        if (impl_->bHelp_)
-        {
-            flags |= efHelpShowDescriptions;
-        }
-        if (impl_->bShowHidden_)
-        {
-            flags |= efHelpShowHidden;
-        }
-    }
-    return flags;
-}
-
 bool
 TrajectoryAnalysisRunnerCommon::hasTrajectory() const
 {