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 classes in plot.h.
35 * \ingroup module_analysisdata
36 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
38 #include "gromacs/analysisdata/modules/plot.h"
46 #include <boost/shared_ptr.hpp>
48 #include "gromacs/legacyheaders/gmxfio.h"
49 #include "gromacs/legacyheaders/oenv.h"
50 #include "gromacs/legacyheaders/vec.h"
51 #include "gromacs/legacyheaders/xvgr.h"
53 #include "gromacs/options/basicoptions.h"
54 #include "gromacs/analysisdata/dataframe.h"
55 #include "gromacs/options/options.h"
56 #include "gromacs/options/timeunitmanager.h"
57 #include "gromacs/selection/selectioncollection.h"
58 #include "gromacs/utility/exceptions.h"
59 #include "gromacs/utility/gmxassert.h"
60 #include "gromacs/utility/stringutil.h"
65 //! Enum values for plot formats.
66 const char *const g_plotFormats[] = {
67 "none", "xmgrace", "xmgr", NULL
75 /********************************************************************
76 * AnalysisDataPlotSettings
79 AnalysisDataPlotSettings::AnalysisDataPlotSettings()
80 : selections_(NULL), timeUnit_(eTimeUnit_ps), plotFormat_(1)
85 AnalysisDataPlotSettings::setSelectionCollection(const SelectionCollection *selections)
87 selections_ = selections;
92 AnalysisDataPlotSettings::addOptions(Options *options)
94 options->addOption(StringOption("xvg").enumValue(g_plotFormats)
95 .defaultValue("xmgrace")
96 .storeEnumIndex(&plotFormat_)
97 .description("Plot formatting"));
101 /********************************************************************
102 * AbstractPlotModule::Impl
105 class AbstractPlotModule::Impl
108 explicit Impl(const AnalysisDataPlotSettings &settings);
113 AnalysisDataPlotSettings settings_;
114 std::string filename_;
120 std::string subtitle_;
123 std::vector<std::string> legend_;
129 AbstractPlotModule::Impl::Impl(const AnalysisDataPlotSettings &settings)
130 : settings_(settings), fp_(NULL), bPlain_(false), gOmitX_(false),
133 strcpy(xformat_, "%11.3f");
134 strcpy(yformat_, " %8.3f");
137 AbstractPlotModule::Impl::~Impl()
144 AbstractPlotModule::Impl::closeFile()
161 /********************************************************************
165 AbstractPlotModule::AbstractPlotModule()
166 : impl_(new Impl(AnalysisDataPlotSettings()))
170 AbstractPlotModule::AbstractPlotModule(const AnalysisDataPlotSettings &settings)
171 : impl_(new Impl(settings))
176 AbstractPlotModule::~AbstractPlotModule()
182 AbstractPlotModule::setSettings(const AnalysisDataPlotSettings &settings)
184 impl_->settings_ = settings;
189 AbstractPlotModule::setFileName(const std::string &filename)
191 impl_->filename_ = filename;
196 AbstractPlotModule::setPlainOutput(bool bPlain)
198 impl_->bPlain_ = bPlain;
203 AbstractPlotModule::setOmitX(bool bOmitX)
205 impl_->gOmitX_ = bOmitX;
210 AbstractPlotModule::setTitle(const char *title)
212 impl_->title_ = title;
217 AbstractPlotModule::setSubtitle(const char *subtitle)
219 impl_->subtitle_ = subtitle;
224 AbstractPlotModule::setXLabel(const char *label)
226 impl_->xlabel_ = label;
231 AbstractPlotModule::setXAxisIsTime()
233 TimeUnitManager manager(impl_->settings_.timeUnit());
234 impl_->xlabel_ = formatString("Time (%s)", manager.timeUnitAsString());
235 impl_->xscale_ = manager.inverseTimeScaleFactor();
240 AbstractPlotModule::setYLabel(const char *label)
242 impl_->ylabel_ = label;
247 AbstractPlotModule::setLegend(int nsets, const char * const *setname)
249 impl_->legend_.reserve(impl_->legend_.size() + nsets);
250 for (int i = 0; i < nsets; ++i)
252 appendLegend(setname[i]);
258 AbstractPlotModule::appendLegend(const char *setname)
260 impl_->legend_.push_back(setname);
265 AbstractPlotModule::setXFormat(int width, int precision, char format)
267 GMX_RELEASE_ASSERT(width >= 0 && precision >= 0
268 && width <= 99 && precision <= 99,
269 "Invalid width or precision");
270 GMX_RELEASE_ASSERT(strchr("eEfFgG", format) != NULL,
271 "Invalid format specifier");
272 sprintf(impl_->xformat_, "%%%d.%d%c", width, precision, format);
277 AbstractPlotModule::setYFormat(int width, int precision, char format)
279 GMX_RELEASE_ASSERT(width >= 0 && precision >= 0
280 && width <= 99 && precision <= 99,
281 "Invalid width or precision");
282 GMX_RELEASE_ASSERT(strchr("eEfFgG", format) != NULL,
283 "Invalid format specifier");
284 sprintf(impl_->yformat_, " %%%d.%d%c", width, precision, format);
289 AbstractPlotModule::flags() const
291 return efAllowMulticolumn | efAllowMultipoint;
296 AbstractPlotModule::dataStarted(AbstractAnalysisData *data)
298 if (!impl_->filename_.empty())
302 impl_->fp_ = gmx_fio_fopen(impl_->filename_.c_str(), "w");
306 time_unit_t time_unit
307 = static_cast<time_unit_t>(impl_->settings_.timeUnit() + 1);
308 xvg_format_t xvg_format
309 = (impl_->settings_.plotFormat() > 0
310 ? static_cast<xvg_format_t>(impl_->settings_.plotFormat())
313 output_env_init(&oenv, 0, NULL, time_unit, FALSE, xvg_format, 0, 0);
314 boost::shared_ptr<output_env> oenvGuard(oenv, &output_env_done);
315 impl_->fp_ = xvgropen(impl_->filename_.c_str(), impl_->title_.c_str(),
316 impl_->xlabel_.c_str(), impl_->ylabel_.c_str(),
318 const SelectionCollection *selections
319 = impl_->settings_.selectionCollection();
320 if (selections != NULL)
322 selections->printXvgrInfo(impl_->fp_, oenv);
324 if (!impl_->subtitle_.empty())
326 xvgr_subtitle(impl_->fp_, impl_->subtitle_.c_str(), oenv);
328 if (output_env_get_print_xvgr_codes(oenv)
329 && !impl_->legend_.empty())
331 std::vector<const char *> legend;
332 legend.reserve(impl_->legend_.size());
333 for (size_t i = 0; i < impl_->legend_.size(); ++i)
335 legend.push_back(impl_->legend_[i].c_str());
337 xvgr_legend(impl_->fp_, legend.size(), &legend[0], oenv);
345 AbstractPlotModule::frameStarted(const AnalysisDataFrameHeader &frame)
353 std::fprintf(impl_->fp_, impl_->xformat_, frame.x() * impl_->xscale_);
359 AbstractPlotModule::frameFinished(const AnalysisDataFrameHeader & /*header*/)
365 std::fprintf(impl_->fp_, "\n");
370 AbstractPlotModule::dataFinished()
377 AbstractPlotModule::isFileOpen() const
379 return impl_->fp_ != NULL;
384 AbstractPlotModule::writeValue(real value) const
386 GMX_ASSERT(isFileOpen(), "File not opened, but write attempted");
387 std::fprintf(impl_->fp_, impl_->yformat_, value);
391 /********************************************************************
395 AnalysisDataPlotModule::AnalysisDataPlotModule()
399 AnalysisDataPlotModule::AnalysisDataPlotModule(
400 const AnalysisDataPlotSettings &settings)
401 : AbstractPlotModule(settings)
407 AnalysisDataPlotModule::pointsAdded(const AnalysisDataPointSetRef &points)
413 for (int i = 0; i < points.columnCount(); ++i)
415 writeValue(points.y(i));
420 /********************************************************************
421 * DataVectorPlotModule
424 AnalysisDataVectorPlotModule::AnalysisDataVectorPlotModule()
426 for (int i = 0; i < DIM; ++i)
430 bWrite_[DIM] = false;
434 AnalysisDataVectorPlotModule::AnalysisDataVectorPlotModule(
435 const AnalysisDataPlotSettings &settings)
436 : AbstractPlotModule(settings)
438 for (int i = 0; i < DIM; ++i)
442 bWrite_[DIM] = false;
447 AnalysisDataVectorPlotModule::setWriteX(bool bWrite)
449 bWrite_[XX] = bWrite;
454 AnalysisDataVectorPlotModule::setWriteY(bool bWrite)
456 bWrite_[YY] = bWrite;
461 AnalysisDataVectorPlotModule::setWriteZ(bool bWrite)
463 bWrite_[ZZ] = bWrite;
468 AnalysisDataVectorPlotModule::setWriteNorm(bool bWrite)
470 bWrite_[DIM] = bWrite;
475 AnalysisDataVectorPlotModule::setWriteMask(bool bWrite[DIM + 1])
477 for (int i = 0; i < DIM + 1; ++i)
479 bWrite_[i] = bWrite[i];
485 AnalysisDataVectorPlotModule::pointsAdded(const AnalysisDataPointSetRef &points)
487 if (points.firstColumn() % DIM != 0)
489 GMX_THROW(APIError("Partial data points"));
495 for (int i = 0; i < points.columnCount(); i += 3)
497 for (int d = 0; d < DIM; ++d)
501 writeValue(points.y(i + d));
506 rvec y = { points.y(i), points.y(i + 1), points.y(i + 2) };