2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013, by the GROMACS development team, led by
5 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6 * others, as listed in the AUTHORS file in the top-level source
7 * 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
42 #include "runnercommon.h"
50 #include "gromacs/legacyheaders/oenv.h"
51 #include "gromacs/legacyheaders/rmpbc.h"
52 #include "gromacs/legacyheaders/smalloc.h"
53 #include "gromacs/legacyheaders/statutil.h"
54 #include "gromacs/legacyheaders/tpxio.h"
55 #include "gromacs/legacyheaders/vec.h"
57 #include "gromacs/options/basicoptions.h"
58 #include "gromacs/options/filenameoption.h"
59 #include "gromacs/options/options.h"
60 #include "gromacs/selection/indexutil.h"
61 #include "gromacs/selection/selectioncollection.h"
62 #include "gromacs/selection/selectionfileoption.h"
63 #include "gromacs/trajectoryanalysis/analysissettings.h"
64 #include "gromacs/utility/exceptions.h"
65 #include "gromacs/utility/gmxassert.h"
66 #include "gromacs/utility/stringutil.h"
68 #include "analysissettings-impl.h"
73 class TrajectoryAnalysisRunnerCommon::Impl
76 Impl(TrajectoryAnalysisSettings *settings);
79 void finishTrajectory();
81 TrajectoryAnalysisSettings &settings_;
82 TopologyInformation topInfo_;
84 //! Name of the trajectory file (empty if not provided).
86 //! Name of the topology file (empty if no topology provided).
88 //! Name of the index file (empty if no index file provided).
94 gmx_ana_indexgrps_t *grps_;
96 //! The current frame, or \p NULL if no frame loaded yet.
99 //! Used to store the status variable from read_first_frame().
100 t_trxstatus *status_;
105 TrajectoryAnalysisRunnerCommon::Impl::Impl(TrajectoryAnalysisSettings *settings)
106 : settings_(*settings),
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 common file name arguments.
173 options->addOption(FileNameOption("f")
174 .filetype(eftTrajectory).inputFile()
175 .store(&impl_->trjfile_)
176 .defaultBasename("traj")
177 .description("Input trajectory or single configuration"));
178 options->addOption(FileNameOption("s")
179 .filetype(eftTopology).inputFile()
180 .store(&impl_->topfile_)
181 .defaultBasename("topol")
182 .description("Input structure"));
183 options->addOption(FileNameOption("n")
184 .filetype(eftIndex).inputFile()
185 .store(&impl_->ndxfile_)
186 .defaultBasename("index")
187 .description("Extra index groups"));
188 options->addOption(SelectionFileOption("sf"));
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"));
219 TrajectoryAnalysisRunnerCommon::scaleTimeOptions(Options *options)
221 impl_->settings_.impl_->timeUnitManager.scaleTimeOptions(options);
226 TrajectoryAnalysisRunnerCommon::optionsFinished(Options *options)
228 impl_->settings_.impl_->plotSettings.setTimeUnit(
229 impl_->settings_.impl_->timeUnitManager.timeUnit());
231 if (impl_->trjfile_.empty() && impl_->topfile_.empty())
233 GMX_THROW(InconsistentInputError("No trajectory or topology provided, nothing to do!"));
236 if (options->isSet("b"))
238 setTimeValue(TBEGIN, impl_->startTime_);
240 if (options->isSet("e"))
242 setTimeValue(TEND, impl_->endTime_);
244 if (options->isSet("dt"))
246 setTimeValue(TDELTA, impl_->deltaTime_);
252 TrajectoryAnalysisRunnerCommon::initIndexGroups(SelectionCollection *selections)
254 if (impl_->ndxfile_.empty())
256 // TODO: Initialize default selections
257 selections->setIndexGroups(NULL);
261 gmx_ana_indexgrps_init(&impl_->grps_, NULL, impl_->ndxfile_.c_str());
262 selections->setIndexGroups(impl_->grps_);
268 TrajectoryAnalysisRunnerCommon::doneIndexGroups(SelectionCollection *selections)
270 if (impl_->grps_ != NULL)
272 selections->setIndexGroups(NULL);
273 gmx_ana_indexgrps_free(impl_->grps_);
280 TrajectoryAnalysisRunnerCommon::initTopology(SelectionCollection *selections)
282 const TrajectoryAnalysisSettings &settings = impl_->settings_;
284 = settings.hasFlag(TrajectoryAnalysisSettings::efRequireTop)
285 || selections->requiresTopology();
286 if (bRequireTop && impl_->topfile_.empty())
288 GMX_THROW(InconsistentInputError("No topology provided, but one is required for analysis"));
291 // Load the topology if requested.
292 if (!impl_->topfile_.empty())
296 snew(impl_->topInfo_.top_, 1);
297 impl_->topInfo_.bTop_ = read_tps_conf(impl_->topfile_.c_str(), title,
298 impl_->topInfo_.top_, &impl_->topInfo_.ePBC_,
299 &impl_->topInfo_.xtop_, NULL, impl_->topInfo_.boxtop_, TRUE);
301 && !settings.hasFlag(TrajectoryAnalysisSettings::efUseTopX))
303 sfree(impl_->topInfo_.xtop_);
304 impl_->topInfo_.xtop_ = NULL;
308 // Read the first frame if we don't know the maximum number of atoms
311 if (!impl_->topInfo_.hasTopology())
314 natoms = impl_->fr->natoms;
316 selections->setTopology(impl_->topInfo_.topology(), natoms);
321 gmx_ana_poscalc_coll_print_tree(stderr, impl_->pcc);
322 fprintf(stderr, "\n");
329 TrajectoryAnalysisRunnerCommon::initFirstFrame()
331 // Return if we have already initialized the trajectory.
336 time_unit_t time_unit
337 = static_cast<time_unit_t>(impl_->settings_.timeUnit() + 1);
338 output_env_init(&impl_->oenv_, 0, NULL, time_unit, FALSE, exvgNONE, 0, 0);
340 int frflags = impl_->settings_.frflags();
341 frflags |= TRX_NEED_X;
345 const TopologyInformation &top = impl_->topInfo_;
348 if (!read_first_frame(impl_->oenv_, &impl_->status_,
349 impl_->trjfile_.c_str(), impl_->fr, frflags))
351 GMX_THROW(FileIOError("Could not read coordinates from trajectory"));
353 impl_->bTrajOpen_ = true;
355 if (top.hasTopology() && impl_->fr->natoms > top.topology()->atoms.nr)
357 GMX_THROW(InconsistentInputError(formatString(
358 "Trajectory (%d atoms) does not match topology (%d atoms)",
359 impl_->fr->natoms, top.topology()->atoms.nr)));
361 // TODO: Check index groups if they have been initialized based on the topology.
365 for (int i = 0; i < impl_->sel->nr(); ++i)
367 gmx_ana_index_check(impl_->sel->sel(i)->indexGroup(),
375 // Prepare a frame from topology information.
376 // TODO: Initialize more of the fields.
377 if (frflags & (TRX_NEED_V))
379 GMX_THROW(NotImplementedError("Velocity reading from a topology not implemented"));
381 if (frflags & (TRX_NEED_F))
383 GMX_THROW(InvalidInputError("Forces cannot be read from a topology"));
385 impl_->fr->flags = frflags;
386 impl_->fr->natoms = top.topology()->atoms.nr;
387 impl_->fr->bX = TRUE;
388 snew(impl_->fr->x, impl_->fr->natoms);
389 memcpy(impl_->fr->x, top.xtop_,
390 sizeof(*impl_->fr->x) * impl_->fr->natoms);
391 impl_->fr->bBox = TRUE;
392 copy_mat(const_cast<rvec *>(top.boxtop_), impl_->fr->box);
395 set_trxframe_ePBC(impl_->fr, top.ePBC());
396 if (top.hasTopology() && impl_->settings_.hasRmPBC())
398 impl_->gpbc_ = gmx_rmpbc_init(&top.topology()->idef, top.ePBC(),
405 TrajectoryAnalysisRunnerCommon::readNextFrame()
407 bool bContinue = false;
410 bContinue = read_next_frame(impl_->oenv_, impl_->status_, impl_->fr);
414 impl_->finishTrajectory();
421 TrajectoryAnalysisRunnerCommon::initFrame()
423 if (impl_->gpbc_ != NULL)
425 gmx_rmpbc_trxfr(impl_->gpbc_, impl_->fr);
431 TrajectoryAnalysisRunnerCommon::hasTrajectory() const
433 return !impl_->trjfile_.empty();
437 const TopologyInformation &
438 TrajectoryAnalysisRunnerCommon::topologyInformation() const
440 return impl_->topInfo_;
445 TrajectoryAnalysisRunnerCommon::frame() const
447 GMX_RELEASE_ASSERT(impl_->fr != NULL, "Frame not available when accessed");