2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013,2014, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Implements gmx::TrajectoryAnalysisRunnerCommon.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_trajectoryanalysis
44 #include "runnercommon.h"
48 #include "gromacs/fileio/timecontrol.h"
49 #include "gromacs/fileio/tpxio.h"
50 #include "gromacs/fileio/trx.h"
51 #include "gromacs/fileio/trxio.h"
52 #include "gromacs/legacyheaders/oenv.h"
53 #include "gromacs/math/vec.h"
54 #include "gromacs/options/basicoptions.h"
55 #include "gromacs/options/filenameoption.h"
56 #include "gromacs/options/options.h"
57 #include "gromacs/pbcutil/rmpbc.h"
58 #include "gromacs/selection/indexutil.h"
59 #include "gromacs/selection/selectioncollection.h"
60 #include "gromacs/selection/selectionfileoption.h"
61 #include "gromacs/topology/topology.h"
62 #include "gromacs/trajectoryanalysis/analysissettings.h"
63 #include "gromacs/utility/exceptions.h"
64 #include "gromacs/utility/gmxassert.h"
65 #include "gromacs/utility/programcontext.h"
66 #include "gromacs/utility/smalloc.h"
67 #include "gromacs/utility/stringutil.h"
69 #include "analysissettings-impl.h"
74 class TrajectoryAnalysisRunnerCommon::Impl
77 Impl(TrajectoryAnalysisSettings *settings);
80 void finishTrajectory();
82 TrajectoryAnalysisSettings &settings_;
83 TopologyInformation topInfo_;
85 //! Name of the trajectory file (empty if not provided).
87 //! Name of the topology file (empty if no topology provided).
89 //! Name of the index file (empty if no index file provided).
95 gmx_ana_indexgrps_t *grps_;
97 //! The current frame, or \p NULL if no frame loaded yet.
100 //! Used to store the status variable from read_first_frame().
101 t_trxstatus *status_;
106 TrajectoryAnalysisRunnerCommon::Impl::Impl(TrajectoryAnalysisSettings *settings)
107 : settings_(*settings),
108 startTime_(0.0), endTime_(0.0), deltaTime_(0.0),
110 bTrajOpen_(false), fr(NULL), gpbc_(NULL), status_(NULL), oenv_(NULL)
115 TrajectoryAnalysisRunnerCommon::Impl::~Impl()
119 gmx_ana_indexgrps_free(grps_);
124 // There doesn't seem to be a function for freeing frame data
132 output_env_done(oenv_);
138 TrajectoryAnalysisRunnerCommon::Impl::finishTrajectory()
147 gmx_rmpbc_done(gpbc_);
152 /*********************************************************************
153 * TrajectoryAnalysisRunnerCommon
156 TrajectoryAnalysisRunnerCommon::TrajectoryAnalysisRunnerCommon(
157 TrajectoryAnalysisSettings *settings)
158 : impl_(new Impl(settings))
163 TrajectoryAnalysisRunnerCommon::~TrajectoryAnalysisRunnerCommon()
169 TrajectoryAnalysisRunnerCommon::initOptions(Options *options)
171 TrajectoryAnalysisSettings &settings = impl_->settings_;
173 // Add common file name arguments.
174 options->addOption(FileNameOption("f")
175 .filetype(eftTrajectory).inputFile()
176 .store(&impl_->trjfile_)
177 .defaultBasename("traj")
178 .description("Input trajectory or single configuration"));
179 options->addOption(FileNameOption("s")
180 .filetype(eftTopology).inputFile()
181 .store(&impl_->topfile_)
182 .defaultBasename("topol")
183 .description("Input structure"));
184 options->addOption(FileNameOption("n")
185 .filetype(eftIndex).inputFile()
186 .store(&impl_->ndxfile_)
187 .defaultBasename("index")
188 .description("Extra index groups"));
190 // Add options for trajectory time control.
191 options->addOption(DoubleOption("b").store(&impl_->startTime_).timeValue()
192 .description("First frame (%t) to read from trajectory"));
193 options->addOption(DoubleOption("e").store(&impl_->endTime_).timeValue()
194 .description("Last frame (%t) to read from trajectory"));
195 options->addOption(DoubleOption("dt").store(&impl_->deltaTime_).timeValue()
196 .description("Only use frame if t MOD dt == first time (%t)"));
198 // Add time unit option.
199 settings.impl_->timeUnitManager.addTimeUnitOption(options, "tu");
202 settings.impl_->plotSettings.addOptions(options);
204 // Add common options for trajectory processing.
205 if (!settings.hasFlag(TrajectoryAnalysisSettings::efNoUserRmPBC))
207 options->addOption(BooleanOption("rmpbc").store(&settings.impl_->bRmPBC)
208 .description("Make molecules whole for each frame"));
210 if (!settings.hasFlag(TrajectoryAnalysisSettings::efNoUserPBC))
212 options->addOption(BooleanOption("pbc").store(&settings.impl_->bPBC)
213 .description("Use periodic boundary conditions for distance calculation"));
216 options->addOption(SelectionFileOption("sf"));
221 TrajectoryAnalysisRunnerCommon::scaleTimeOptions(Options *options)
223 impl_->settings_.impl_->timeUnitManager.scaleTimeOptions(options);
228 TrajectoryAnalysisRunnerCommon::optionsFinished(Options *options)
230 impl_->settings_.impl_->plotSettings.setTimeUnit(
231 impl_->settings_.impl_->timeUnitManager.timeUnit());
233 if (impl_->trjfile_.empty() && impl_->topfile_.empty())
235 GMX_THROW(InconsistentInputError("No trajectory or topology provided, nothing to do!"));
238 if (options->isSet("b"))
240 setTimeValue(TBEGIN, impl_->startTime_);
242 if (options->isSet("e"))
244 setTimeValue(TEND, impl_->endTime_);
246 if (options->isSet("dt"))
248 setTimeValue(TDELTA, impl_->deltaTime_);
254 TrajectoryAnalysisRunnerCommon::initIndexGroups(SelectionCollection *selections,
257 if (impl_->ndxfile_.empty())
261 selections->setIndexGroups(NULL);
264 initTopology(selections);
266 const char *const ndxfile
267 = (!impl_->ndxfile_.empty() ? impl_->ndxfile_.c_str() : NULL);
268 gmx_ana_indexgrps_init(&impl_->grps_, impl_->topInfo_.topology(), ndxfile);
269 selections->setIndexGroups(impl_->grps_);
274 TrajectoryAnalysisRunnerCommon::doneIndexGroups(SelectionCollection *selections)
276 if (impl_->grps_ != NULL)
278 selections->setIndexGroups(NULL);
279 gmx_ana_indexgrps_free(impl_->grps_);
286 TrajectoryAnalysisRunnerCommon::initTopology(SelectionCollection *selections)
288 // Return immediately if the topology has already been loaded.
289 if (impl_->topInfo_.hasTopology())
294 const TrajectoryAnalysisSettings &settings = impl_->settings_;
295 const bool bRequireTop
296 = settings.hasFlag(TrajectoryAnalysisSettings::efRequireTop)
297 || selections->requiresTopology();
298 if (bRequireTop && impl_->topfile_.empty())
300 GMX_THROW(InconsistentInputError("No topology provided, but one is required for analysis"));
303 // Load the topology if requested.
304 if (!impl_->topfile_.empty())
308 snew(impl_->topInfo_.top_, 1);
309 impl_->topInfo_.bTop_ = read_tps_conf(impl_->topfile_.c_str(), title,
310 impl_->topInfo_.top_, &impl_->topInfo_.ePBC_,
311 &impl_->topInfo_.xtop_, NULL, impl_->topInfo_.boxtop_, TRUE);
313 && !settings.hasFlag(TrajectoryAnalysisSettings::efUseTopX))
315 sfree(impl_->topInfo_.xtop_);
316 impl_->topInfo_.xtop_ = NULL;
320 // Read the first frame if we don't know the maximum number of atoms
323 if (!impl_->topInfo_.hasTopology())
326 natoms = impl_->fr->natoms;
328 selections->setTopology(impl_->topInfo_.topology(), natoms);
333 gmx_ana_poscalc_coll_print_tree(stderr, impl_->pcc);
334 fprintf(stderr, "\n");
341 TrajectoryAnalysisRunnerCommon::initFirstFrame()
343 // Return if we have already initialized the trajectory.
348 time_unit_t time_unit
349 = static_cast<time_unit_t>(impl_->settings_.timeUnit() + 1);
350 output_env_init(&impl_->oenv_, getProgramContext(), time_unit, FALSE, exvgNONE, 0);
352 int frflags = impl_->settings_.frflags();
353 frflags |= TRX_NEED_X;
357 const TopologyInformation &top = impl_->topInfo_;
360 if (!read_first_frame(impl_->oenv_, &impl_->status_,
361 impl_->trjfile_.c_str(), impl_->fr, frflags))
363 GMX_THROW(FileIOError("Could not read coordinates from trajectory"));
365 impl_->bTrajOpen_ = true;
367 if (top.hasTopology() && impl_->fr->natoms > top.topology()->atoms.nr)
369 GMX_THROW(InconsistentInputError(formatString(
370 "Trajectory (%d atoms) does not match topology (%d atoms)",
371 impl_->fr->natoms, top.topology()->atoms.nr)));
376 // Prepare a frame from topology information.
377 // TODO: Initialize more of the fields.
378 if (frflags & (TRX_NEED_V))
380 GMX_THROW(NotImplementedError("Velocity reading from a topology not implemented"));
382 if (frflags & (TRX_NEED_F))
384 GMX_THROW(InvalidInputError("Forces cannot be read from a topology"));
386 impl_->fr->flags = frflags;
387 impl_->fr->natoms = top.topology()->atoms.nr;
388 impl_->fr->bX = TRUE;
389 snew(impl_->fr->x, impl_->fr->natoms);
390 memcpy(impl_->fr->x, top.xtop_,
391 sizeof(*impl_->fr->x) * impl_->fr->natoms);
392 impl_->fr->bBox = TRUE;
393 copy_mat(const_cast<rvec *>(top.boxtop_), impl_->fr->box);
396 set_trxframe_ePBC(impl_->fr, top.ePBC());
397 if (top.hasTopology() && impl_->settings_.hasRmPBC())
399 impl_->gpbc_ = gmx_rmpbc_init(&top.topology()->idef, top.ePBC(),
406 TrajectoryAnalysisRunnerCommon::readNextFrame()
408 bool bContinue = false;
411 bContinue = read_next_frame(impl_->oenv_, impl_->status_, impl_->fr);
415 impl_->finishTrajectory();
422 TrajectoryAnalysisRunnerCommon::initFrame()
424 if (impl_->gpbc_ != NULL)
426 gmx_rmpbc_trxfr(impl_->gpbc_, impl_->fr);
432 TrajectoryAnalysisRunnerCommon::hasTrajectory() const
434 return !impl_->trjfile_.empty();
438 const TopologyInformation &
439 TrajectoryAnalysisRunnerCommon::topologyInformation() const
441 return impl_->topInfo_;
446 TrajectoryAnalysisRunnerCommon::frame() const
448 GMX_RELEASE_ASSERT(impl_->fr != NULL, "Frame not available when accessed");