3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2009, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
33 * Implements gmx::TrajectoryAnalysisRunnerCommon.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36 * \ingroup module_trajectoryanalysis
51 #include "gromacs/options/basicoptions.h"
52 #include "gromacs/options/filenameoption.h"
53 #include "gromacs/options/options.h"
54 #include "gromacs/selection/indexutil.h"
55 #include "gromacs/selection/selectioncollection.h"
56 #include "gromacs/selection/selectionfileoption.h"
57 #include "gromacs/trajectoryanalysis/analysissettings.h"
58 #include "gromacs/trajectoryanalysis/runnercommon.h"
59 #include "gromacs/utility/exceptions.h"
60 #include "gromacs/utility/gmxassert.h"
61 #include "gromacs/utility/stringutil.h"
63 #include "analysissettings-impl.h"
68 class TrajectoryAnalysisRunnerCommon::Impl
71 Impl(TrajectoryAnalysisSettings *settings);
74 void finishTrajectory();
76 TrajectoryAnalysisSettings &settings_;
77 TopologyInformation topInfo_;
82 //! Name of the trajectory file (empty if not provided).
84 //! Name of the topology file (empty if no topology provided).
86 //! Name of the index file (empty if no index file provided).
92 gmx_ana_indexgrps_t *grps_;
94 //! The current frame, or \p NULL if no frame loaded yet.
97 //! Used to store the status variable from read_first_frame().
103 TrajectoryAnalysisRunnerCommon::Impl::Impl(TrajectoryAnalysisSettings *settings)
104 : settings_(*settings),
105 bHelp_(false), bShowHidden_(false), bQuiet_(false),
106 startTime_(0.0), endTime_(0.0), deltaTime_(0.0),
108 bTrajOpen_(false), fr(NULL), gpbc_(NULL), status_(NULL), oenv_(NULL)
113 TrajectoryAnalysisRunnerCommon::Impl::~Impl()
117 gmx_ana_indexgrps_free(grps_);
122 // There doesn't seem to be a function for freeing frame data
130 output_env_done(oenv_);
136 TrajectoryAnalysisRunnerCommon::Impl::finishTrajectory()
145 gmx_rmpbc_done(gpbc_);
150 /*********************************************************************
151 * TrajectoryAnalysisRunnerCommon
154 TrajectoryAnalysisRunnerCommon::TrajectoryAnalysisRunnerCommon(
155 TrajectoryAnalysisSettings *settings)
156 : impl_(new Impl(settings))
161 TrajectoryAnalysisRunnerCommon::~TrajectoryAnalysisRunnerCommon()
167 TrajectoryAnalysisRunnerCommon::initOptions(Options *options)
169 TrajectoryAnalysisSettings &settings = impl_->settings_;
171 // Add options for help.
172 options->addOption(BooleanOption("h").store(&impl_->bHelp_)
173 .description("Print help and quit"));
174 options->addOption(BooleanOption("hidden").store(&impl_->bShowHidden_)
176 .description("Show hidden options"));
177 options->addOption(BooleanOption("quiet").store(&impl_->bQuiet_)
179 .description("Hide options in normal run"));
181 // Add common file name arguments.
182 options->addOption(FileNameOption("f")
183 .filetype(eftTrajectory).inputFile()
184 .store(&impl_->trjfile_)
185 .defaultBasename("traj")
186 .description("Input trajectory or single configuration"));
187 options->addOption(FileNameOption("s")
188 .filetype(eftTopology).inputFile()
189 .store(&impl_->topfile_)
190 .defaultBasename("topol")
191 .description("Input structure"));
192 options->addOption(FileNameOption("n")
193 .filetype(eftIndex).inputFile()
194 .store(&impl_->ndxfile_)
195 .defaultBasename("index")
196 .description("Extra index groups"));
197 options->addOption(SelectionFileOption("sf"));
199 // Add options for trajectory time control.
200 options->addOption(DoubleOption("b").store(&impl_->startTime_).timeValue()
201 .description("First frame (%t) to read from trajectory"));
202 options->addOption(DoubleOption("e").store(&impl_->endTime_).timeValue()
203 .description("Last frame (%t) to read from trajectory"));
204 options->addOption(DoubleOption("dt").store(&impl_->deltaTime_).timeValue()
205 .description("Only use frame if t MOD dt == first time (%t)"));
207 // Add time unit option.
208 settings.impl_->timeUnitManager.addTimeUnitOption(options, "tu");
211 settings.impl_->plotSettings.addOptions(options);
213 // Add common options for trajectory processing.
214 if (!settings.hasFlag(TrajectoryAnalysisSettings::efNoUserRmPBC))
216 options->addOption(BooleanOption("rmpbc").store(&settings.impl_->bRmPBC)
217 .description("Make molecules whole for each frame"));
219 if (!settings.hasFlag(TrajectoryAnalysisSettings::efNoUserPBC))
221 options->addOption(BooleanOption("pbc").store(&settings.impl_->bPBC)
222 .description("Use periodic boundary conditions for distance calculation"));
228 TrajectoryAnalysisRunnerCommon::scaleTimeOptions(Options *options)
230 impl_->settings_.impl_->timeUnitManager.scaleTimeOptions(options);
235 TrajectoryAnalysisRunnerCommon::optionsFinished(Options *options)
242 impl_->settings_.impl_->plotSettings.setTimeUnit(
243 impl_->settings_.impl_->timeUnitManager.timeUnit());
245 if (impl_->trjfile_.empty() && impl_->topfile_.empty())
247 GMX_THROW(InconsistentInputError("No trajectory or topology provided, nothing to do!"));
250 if (options->isSet("b"))
251 setTimeValue(TBEGIN, impl_->startTime_);
252 if (options->isSet("e"))
253 setTimeValue(TEND, impl_->endTime_);
254 if (options->isSet("dt"))
255 setTimeValue(TDELTA, impl_->deltaTime_);
262 TrajectoryAnalysisRunnerCommon::initIndexGroups(SelectionCollection *selections)
264 if (impl_->ndxfile_.empty())
266 // TODO: Initialize default selections
267 selections->setIndexGroups(NULL);
271 gmx_ana_indexgrps_init(&impl_->grps_, NULL, impl_->ndxfile_.c_str());
272 selections->setIndexGroups(impl_->grps_);
278 TrajectoryAnalysisRunnerCommon::doneIndexGroups(SelectionCollection *selections)
280 if (impl_->grps_ != NULL)
282 selections->setIndexGroups(NULL);
283 gmx_ana_indexgrps_free(impl_->grps_);
290 TrajectoryAnalysisRunnerCommon::initTopology(SelectionCollection *selections)
292 const TrajectoryAnalysisSettings &settings = impl_->settings_;
294 = settings.hasFlag(TrajectoryAnalysisSettings::efRequireTop)
295 || selections->requiresTopology();
296 if (bRequireTop && impl_->topfile_.empty())
298 GMX_THROW(InconsistentInputError("No topology provided, but one is required for analysis"));
301 // Load the topology if requested.
302 if (!impl_->topfile_.empty())
306 snew(impl_->topInfo_.top_, 1);
307 impl_->topInfo_.bTop_ = read_tps_conf(impl_->topfile_.c_str(), title,
308 impl_->topInfo_.top_, &impl_->topInfo_.ePBC_,
309 &impl_->topInfo_.xtop_, NULL, impl_->topInfo_.boxtop_, TRUE);
311 && !settings.hasFlag(TrajectoryAnalysisSettings::efUseTopX))
313 sfree(impl_->topInfo_.xtop_);
314 impl_->topInfo_.xtop_ = NULL;
318 // Read the first frame if we don't know the maximum number of atoms
321 if (!impl_->topInfo_.hasTopology())
324 natoms = impl_->fr->natoms;
326 selections->setTopology(impl_->topInfo_.topology(), natoms);
331 gmx_ana_poscalc_coll_print_tree(stderr, impl_->pcc);
332 fprintf(stderr, "\n");
339 TrajectoryAnalysisRunnerCommon::initFirstFrame()
341 // Return if we have already initialized the trajectory.
346 time_unit_t time_unit
347 = static_cast<time_unit_t>(impl_->settings_.timeUnit() + 1);
348 output_env_init(&impl_->oenv_, 0, NULL, time_unit, FALSE, exvgNONE, 0, 0);
350 int frflags = impl_->settings_.frflags();
351 frflags |= TRX_NEED_X;
355 const TopologyInformation &top = impl_->topInfo_;
358 if (!read_first_frame(impl_->oenv_, &impl_->status_,
359 impl_->trjfile_.c_str(), impl_->fr, frflags))
361 GMX_THROW(FileIOError("Could not read coordinates from trajectory"));
363 impl_->bTrajOpen_ = true;
365 if (top.hasTopology() && impl_->fr->natoms > top.topology()->atoms.nr)
367 GMX_THROW(InconsistentInputError(formatString(
368 "Trajectory (%d atoms) does not match topology (%d atoms)",
369 impl_->fr->natoms, top.topology()->atoms.nr)));
371 // Check index groups if they have been initialized based on the topology.
375 for (int i = 0; i < impl_->sel->nr(); ++i)
377 gmx_ana_index_check(impl_->sel->sel(i)->indexGroup(),
385 // Prepare a frame from topology information.
386 // TODO: Initialize more of the fields.
387 if (frflags & (TRX_NEED_V))
389 GMX_THROW(NotImplementedError("Velocity reading from a topology not implemented"));
391 if (frflags & (TRX_NEED_F))
393 GMX_THROW(InvalidInputError("Forces cannot be read from a topology"));
395 impl_->fr->flags = frflags;
396 impl_->fr->natoms = top.topology()->atoms.nr;
397 impl_->fr->bX = TRUE;
398 snew(impl_->fr->x, impl_->fr->natoms);
399 memcpy(impl_->fr->x, top.xtop_,
400 sizeof(*impl_->fr->x) * impl_->fr->natoms);
401 impl_->fr->bBox = TRUE;
402 copy_mat(const_cast<rvec *>(top.boxtop_), impl_->fr->box);
405 set_trxframe_ePBC(impl_->fr, top.ePBC());
406 if (top.hasTopology() && impl_->settings_.hasRmPBC())
408 impl_->gpbc_ = gmx_rmpbc_init(&top.topology()->idef, top.ePBC(),
409 impl_->fr->natoms, impl_->fr->box);
415 TrajectoryAnalysisRunnerCommon::readNextFrame()
417 bool bContinue = false;
420 bContinue = read_next_frame(impl_->oenv_, impl_->status_, impl_->fr);
424 impl_->finishTrajectory();
431 TrajectoryAnalysisRunnerCommon::initFrame()
433 if (impl_->gpbc_ != NULL)
435 gmx_rmpbc_trxfr(impl_->gpbc_, impl_->fr);
440 TrajectoryAnalysisRunnerCommon::HelpFlags
441 TrajectoryAnalysisRunnerCommon::helpFlags() const
447 flags |= efHelpShowOptions;
450 flags |= efHelpShowDescriptions;
452 if (impl_->bShowHidden_)
454 flags |= efHelpShowHidden;
461 TrajectoryAnalysisRunnerCommon::hasTrajectory() const
463 return !impl_->trjfile_.empty();
467 const TopologyInformation &
468 TrajectoryAnalysisRunnerCommon::topologyInformation() const
470 return impl_->topInfo_;
475 TrajectoryAnalysisRunnerCommon::frame() const
477 GMX_RELEASE_ASSERT(impl_->fr != NULL, "Frame not available when accessed");