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
38 #include "runnercommon.h"
46 #include "gromacs/legacyheaders/oenv.h"
47 #include "gromacs/legacyheaders/rmpbc.h"
48 #include "gromacs/legacyheaders/smalloc.h"
49 #include "gromacs/legacyheaders/statutil.h"
50 #include "gromacs/legacyheaders/tpxio.h"
51 #include "gromacs/legacyheaders/vec.h"
53 #include "gromacs/options/basicoptions.h"
54 #include "gromacs/options/filenameoption.h"
55 #include "gromacs/options/options.h"
56 #include "gromacs/selection/indexutil.h"
57 #include "gromacs/selection/selectioncollection.h"
58 #include "gromacs/selection/selectionfileoption.h"
59 #include "gromacs/trajectoryanalysis/analysissettings.h"
60 #include "gromacs/utility/exceptions.h"
61 #include "gromacs/utility/gmxassert.h"
62 #include "gromacs/utility/stringutil.h"
64 #include "analysissettings-impl.h"
69 class TrajectoryAnalysisRunnerCommon::Impl
72 Impl(TrajectoryAnalysisSettings *settings);
75 void finishTrajectory();
77 TrajectoryAnalysisSettings &settings_;
78 TopologyInformation topInfo_;
83 //! Name of the trajectory file (empty if not provided).
85 //! Name of the topology file (empty if no topology provided).
87 //! Name of the index file (empty if no index file provided).
93 gmx_ana_indexgrps_t *grps_;
95 //! The current frame, or \p NULL if no frame loaded yet.
98 //! Used to store the status variable from read_first_frame().
104 TrajectoryAnalysisRunnerCommon::Impl::Impl(TrajectoryAnalysisSettings *settings)
105 : settings_(*settings),
106 bHelp_(false), bShowHidden_(false), bQuiet_(false),
107 startTime_(0.0), endTime_(0.0), deltaTime_(0.0),
109 bTrajOpen_(false), fr(NULL), gpbc_(NULL), status_(NULL), oenv_(NULL)
114 TrajectoryAnalysisRunnerCommon::Impl::~Impl()
118 gmx_ana_indexgrps_free(grps_);
123 // There doesn't seem to be a function for freeing frame data
131 output_env_done(oenv_);
137 TrajectoryAnalysisRunnerCommon::Impl::finishTrajectory()
146 gmx_rmpbc_done(gpbc_);
151 /*********************************************************************
152 * TrajectoryAnalysisRunnerCommon
155 TrajectoryAnalysisRunnerCommon::TrajectoryAnalysisRunnerCommon(
156 TrajectoryAnalysisSettings *settings)
157 : impl_(new Impl(settings))
162 TrajectoryAnalysisRunnerCommon::~TrajectoryAnalysisRunnerCommon()
168 TrajectoryAnalysisRunnerCommon::initOptions(Options *options)
170 TrajectoryAnalysisSettings &settings = impl_->settings_;
172 // Add options for help.
173 options->addOption(BooleanOption("h").store(&impl_->bHelp_)
174 .description("Print help and quit"));
175 options->addOption(BooleanOption("hidden").store(&impl_->bShowHidden_)
177 .description("Show hidden options"));
178 options->addOption(BooleanOption("quiet").store(&impl_->bQuiet_)
180 .description("Hide options in normal run"));
182 // Add common file name arguments.
183 options->addOption(FileNameOption("f")
184 .filetype(eftTrajectory).inputFile()
185 .store(&impl_->trjfile_)
186 .defaultBasename("traj")
187 .description("Input trajectory or single configuration"));
188 options->addOption(FileNameOption("s")
189 .filetype(eftTopology).inputFile()
190 .store(&impl_->topfile_)
191 .defaultBasename("topol")
192 .description("Input structure"));
193 options->addOption(FileNameOption("n")
194 .filetype(eftIndex).inputFile()
195 .store(&impl_->ndxfile_)
196 .defaultBasename("index")
197 .description("Extra index groups"));
198 options->addOption(SelectionFileOption("sf"));
200 // Add options for trajectory time control.
201 options->addOption(DoubleOption("b").store(&impl_->startTime_).timeValue()
202 .description("First frame (%t) to read from trajectory"));
203 options->addOption(DoubleOption("e").store(&impl_->endTime_).timeValue()
204 .description("Last frame (%t) to read from trajectory"));
205 options->addOption(DoubleOption("dt").store(&impl_->deltaTime_).timeValue()
206 .description("Only use frame if t MOD dt == first time (%t)"));
208 // Add time unit option.
209 settings.impl_->timeUnitManager.addTimeUnitOption(options, "tu");
212 settings.impl_->plotSettings.addOptions(options);
214 // Add common options for trajectory processing.
215 if (!settings.hasFlag(TrajectoryAnalysisSettings::efNoUserRmPBC))
217 options->addOption(BooleanOption("rmpbc").store(&settings.impl_->bRmPBC)
218 .description("Make molecules whole for each frame"));
220 if (!settings.hasFlag(TrajectoryAnalysisSettings::efNoUserPBC))
222 options->addOption(BooleanOption("pbc").store(&settings.impl_->bPBC)
223 .description("Use periodic boundary conditions for distance calculation"));
229 TrajectoryAnalysisRunnerCommon::scaleTimeOptions(Options *options)
231 impl_->settings_.impl_->timeUnitManager.scaleTimeOptions(options);
236 TrajectoryAnalysisRunnerCommon::optionsFinished(Options *options)
243 impl_->settings_.impl_->plotSettings.setTimeUnit(
244 impl_->settings_.impl_->timeUnitManager.timeUnit());
246 if (impl_->trjfile_.empty() && impl_->topfile_.empty())
248 GMX_THROW(InconsistentInputError("No trajectory or topology provided, nothing to do!"));
251 if (options->isSet("b"))
253 setTimeValue(TBEGIN, impl_->startTime_);
255 if (options->isSet("e"))
257 setTimeValue(TEND, impl_->endTime_);
259 if (options->isSet("dt"))
261 setTimeValue(TDELTA, impl_->deltaTime_);
269 TrajectoryAnalysisRunnerCommon::initIndexGroups(SelectionCollection *selections)
271 if (impl_->ndxfile_.empty())
273 // TODO: Initialize default selections
274 selections->setIndexGroups(NULL);
278 gmx_ana_indexgrps_init(&impl_->grps_, NULL, impl_->ndxfile_.c_str());
279 selections->setIndexGroups(impl_->grps_);
285 TrajectoryAnalysisRunnerCommon::doneIndexGroups(SelectionCollection *selections)
287 if (impl_->grps_ != NULL)
289 selections->setIndexGroups(NULL);
290 gmx_ana_indexgrps_free(impl_->grps_);
297 TrajectoryAnalysisRunnerCommon::initTopology(SelectionCollection *selections)
299 const TrajectoryAnalysisSettings &settings = impl_->settings_;
301 = settings.hasFlag(TrajectoryAnalysisSettings::efRequireTop)
302 || selections->requiresTopology();
303 if (bRequireTop && impl_->topfile_.empty())
305 GMX_THROW(InconsistentInputError("No topology provided, but one is required for analysis"));
308 // Load the topology if requested.
309 if (!impl_->topfile_.empty())
313 snew(impl_->topInfo_.top_, 1);
314 impl_->topInfo_.bTop_ = read_tps_conf(impl_->topfile_.c_str(), title,
315 impl_->topInfo_.top_, &impl_->topInfo_.ePBC_,
316 &impl_->topInfo_.xtop_, NULL, impl_->topInfo_.boxtop_, TRUE);
318 && !settings.hasFlag(TrajectoryAnalysisSettings::efUseTopX))
320 sfree(impl_->topInfo_.xtop_);
321 impl_->topInfo_.xtop_ = NULL;
325 // Read the first frame if we don't know the maximum number of atoms
328 if (!impl_->topInfo_.hasTopology())
331 natoms = impl_->fr->natoms;
333 selections->setTopology(impl_->topInfo_.topology(), natoms);
338 gmx_ana_poscalc_coll_print_tree(stderr, impl_->pcc);
339 fprintf(stderr, "\n");
346 TrajectoryAnalysisRunnerCommon::initFirstFrame()
348 // Return if we have already initialized the trajectory.
353 time_unit_t time_unit
354 = static_cast<time_unit_t>(impl_->settings_.timeUnit() + 1);
355 output_env_init(&impl_->oenv_, 0, NULL, time_unit, FALSE, exvgNONE, 0, 0);
357 int frflags = impl_->settings_.frflags();
358 frflags |= TRX_NEED_X;
362 const TopologyInformation &top = impl_->topInfo_;
365 if (!read_first_frame(impl_->oenv_, &impl_->status_,
366 impl_->trjfile_.c_str(), impl_->fr, frflags))
368 GMX_THROW(FileIOError("Could not read coordinates from trajectory"));
370 impl_->bTrajOpen_ = true;
372 if (top.hasTopology() && impl_->fr->natoms > top.topology()->atoms.nr)
374 GMX_THROW(InconsistentInputError(formatString(
375 "Trajectory (%d atoms) does not match topology (%d atoms)",
376 impl_->fr->natoms, top.topology()->atoms.nr)));
378 // Check index groups if they have been initialized based on the topology.
382 for (int i = 0; i < impl_->sel->nr(); ++i)
384 gmx_ana_index_check(impl_->sel->sel(i)->indexGroup(),
392 // Prepare a frame from topology information.
393 // TODO: Initialize more of the fields.
394 if (frflags & (TRX_NEED_V))
396 GMX_THROW(NotImplementedError("Velocity reading from a topology not implemented"));
398 if (frflags & (TRX_NEED_F))
400 GMX_THROW(InvalidInputError("Forces cannot be read from a topology"));
402 impl_->fr->flags = frflags;
403 impl_->fr->natoms = top.topology()->atoms.nr;
404 impl_->fr->bX = TRUE;
405 snew(impl_->fr->x, impl_->fr->natoms);
406 memcpy(impl_->fr->x, top.xtop_,
407 sizeof(*impl_->fr->x) * impl_->fr->natoms);
408 impl_->fr->bBox = TRUE;
409 copy_mat(const_cast<rvec *>(top.boxtop_), impl_->fr->box);
412 set_trxframe_ePBC(impl_->fr, top.ePBC());
413 if (top.hasTopology() && impl_->settings_.hasRmPBC())
415 impl_->gpbc_ = gmx_rmpbc_init(&top.topology()->idef, top.ePBC(),
416 impl_->fr->natoms, impl_->fr->box);
422 TrajectoryAnalysisRunnerCommon::readNextFrame()
424 bool bContinue = false;
427 bContinue = read_next_frame(impl_->oenv_, impl_->status_, impl_->fr);
431 impl_->finishTrajectory();
438 TrajectoryAnalysisRunnerCommon::initFrame()
440 if (impl_->gpbc_ != NULL)
442 gmx_rmpbc_trxfr(impl_->gpbc_, impl_->fr);
447 TrajectoryAnalysisRunnerCommon::HelpFlags
448 TrajectoryAnalysisRunnerCommon::helpFlags() const
454 flags |= efHelpShowOptions;
457 flags |= efHelpShowDescriptions;
459 if (impl_->bShowHidden_)
461 flags |= efHelpShowHidden;
468 TrajectoryAnalysisRunnerCommon::hasTrajectory() const
470 return !impl_->trjfile_.empty();
474 const TopologyInformation &
475 TrajectoryAnalysisRunnerCommon::topologyInformation() const
477 return impl_->topInfo_;
482 TrajectoryAnalysisRunnerCommon::frame() const
484 GMX_RELEASE_ASSERT(impl_->fr != NULL, "Frame not available when accessed");