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 classes in plot.h.
39 * \ingroup module_analysisdata
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
42 #include "gromacs/analysisdata/modules/plot.h"
50 #include <boost/shared_ptr.hpp>
52 #include "gromacs/legacyheaders/gmxfio.h"
53 #include "gromacs/legacyheaders/oenv.h"
54 #include "gromacs/legacyheaders/vec.h"
55 #include "gromacs/legacyheaders/xvgr.h"
57 #include "gromacs/analysisdata/dataframe.h"
58 #include "gromacs/options/basicoptions.h"
59 #include "gromacs/options/options.h"
60 #include "gromacs/options/timeunitmanager.h"
61 #include "gromacs/selection/selectioncollection.h"
62 #include "gromacs/utility/exceptions.h"
63 #include "gromacs/utility/gmxassert.h"
64 #include "gromacs/utility/stringutil.h"
69 //! Enum values for plot formats.
70 const char *const g_plotFormats[] = {
71 "none", "xmgrace", "xmgr"
79 /********************************************************************
80 * AnalysisDataPlotSettings
83 AnalysisDataPlotSettings::AnalysisDataPlotSettings()
84 : selections_(NULL), timeUnit_(eTimeUnit_ps), plotFormat_(1)
89 AnalysisDataPlotSettings::setSelectionCollection(const SelectionCollection *selections)
91 selections_ = selections;
96 AnalysisDataPlotSettings::addOptions(Options *options)
98 options->addOption(StringOption("xvg").enumValue(g_plotFormats)
99 .defaultValue("xmgrace")
100 .storeEnumIndex(&plotFormat_)
101 .description("Plot formatting"));
105 /********************************************************************
106 * AbstractPlotModule::Impl
109 class AbstractPlotModule::Impl
112 explicit Impl(const AnalysisDataPlotSettings &settings);
117 AnalysisDataPlotSettings settings_;
118 std::string filename_;
123 bool bErrorsAsSeparateColumn_;
125 std::string subtitle_;
128 std::vector<std::string> legend_;
134 AbstractPlotModule::Impl::Impl(const AnalysisDataPlotSettings &settings)
135 : settings_(settings), fp_(NULL), bPlain_(false), bOmitX_(false),
136 bErrorsAsSeparateColumn_(false), xscale_(1.0)
138 strcpy(xformat_, "%11.3f");
139 strcpy(yformat_, " %8.3f");
142 AbstractPlotModule::Impl::~Impl()
149 AbstractPlotModule::Impl::closeFile()
166 /********************************************************************
170 AbstractPlotModule::AbstractPlotModule()
171 : impl_(new Impl(AnalysisDataPlotSettings()))
175 AbstractPlotModule::AbstractPlotModule(const AnalysisDataPlotSettings &settings)
176 : impl_(new Impl(settings))
181 AbstractPlotModule::~AbstractPlotModule()
187 AbstractPlotModule::setSettings(const AnalysisDataPlotSettings &settings)
189 impl_->settings_ = settings;
194 AbstractPlotModule::setFileName(const std::string &filename)
196 impl_->filename_ = filename;
201 AbstractPlotModule::setPlainOutput(bool bPlain)
203 impl_->bPlain_ = bPlain;
208 AbstractPlotModule::setErrorsAsSeparateColumn(bool bSeparate)
210 impl_->bErrorsAsSeparateColumn_ = bSeparate;
215 AbstractPlotModule::setOmitX(bool bOmitX)
217 impl_->bOmitX_ = bOmitX;
222 AbstractPlotModule::setTitle(const char *title)
224 impl_->title_ = title;
229 AbstractPlotModule::setSubtitle(const char *subtitle)
231 impl_->subtitle_ = subtitle;
236 AbstractPlotModule::setXLabel(const char *label)
238 impl_->xlabel_ = label;
243 AbstractPlotModule::setXAxisIsTime()
245 TimeUnitManager manager(impl_->settings_.timeUnit());
246 impl_->xlabel_ = formatString("Time (%s)", manager.timeUnitAsString());
247 impl_->xscale_ = manager.inverseTimeScaleFactor();
252 AbstractPlotModule::setYLabel(const char *label)
254 impl_->ylabel_ = label;
259 AbstractPlotModule::setLegend(int nsets, const char * const *setname)
261 impl_->legend_.reserve(impl_->legend_.size() + nsets);
262 for (int i = 0; i < nsets; ++i)
264 appendLegend(setname[i]);
270 AbstractPlotModule::appendLegend(const char *setname)
272 impl_->legend_.push_back(setname);
277 AbstractPlotModule::setXFormat(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_->xformat_, "%%%d.%d%c", width, precision, format);
289 AbstractPlotModule::setYFormat(int width, int precision, char format)
291 GMX_RELEASE_ASSERT(width >= 0 && precision >= 0
292 && width <= 99 && precision <= 99,
293 "Invalid width or precision");
294 GMX_RELEASE_ASSERT(strchr("eEfFgG", format) != NULL,
295 "Invalid format specifier");
296 sprintf(impl_->yformat_, " %%%d.%d%c", width, precision, format);
301 AbstractPlotModule::flags() const
303 return efAllowMissing | efAllowMulticolumn | efAllowMultipoint
304 | efAllowMultipleDataSets;
309 AbstractPlotModule::dataStarted(AbstractAnalysisData * /*data*/)
311 if (!impl_->filename_.empty())
315 impl_->fp_ = gmx_fio_fopen(impl_->filename_.c_str(), "w");
319 time_unit_t time_unit
320 = static_cast<time_unit_t>(impl_->settings_.timeUnit() + 1);
321 xvg_format_t xvg_format
322 = (impl_->settings_.plotFormat() > 0
323 ? static_cast<xvg_format_t>(impl_->settings_.plotFormat())
326 output_env_init(&oenv, 0, NULL, time_unit, FALSE, xvg_format, 0, 0);
327 boost::shared_ptr<output_env> oenvGuard(oenv, &output_env_done);
328 impl_->fp_ = xvgropen(impl_->filename_.c_str(), impl_->title_.c_str(),
329 impl_->xlabel_.c_str(), impl_->ylabel_.c_str(),
331 const SelectionCollection *selections
332 = impl_->settings_.selectionCollection();
333 if (selections != NULL)
335 selections->printXvgrInfo(impl_->fp_, oenv);
337 if (!impl_->subtitle_.empty())
339 xvgr_subtitle(impl_->fp_, impl_->subtitle_.c_str(), oenv);
341 if (output_env_get_print_xvgr_codes(oenv)
342 && !impl_->legend_.empty())
344 std::vector<const char *> legend;
345 legend.reserve(impl_->legend_.size());
346 for (size_t i = 0; i < impl_->legend_.size(); ++i)
348 legend.push_back(impl_->legend_[i].c_str());
350 xvgr_legend(impl_->fp_, legend.size(), &legend[0], oenv);
358 AbstractPlotModule::frameStarted(const AnalysisDataFrameHeader &frame)
366 std::fprintf(impl_->fp_, impl_->xformat_, frame.x() * impl_->xscale_);
372 AbstractPlotModule::frameFinished(const AnalysisDataFrameHeader & /*header*/)
378 std::fprintf(impl_->fp_, "\n");
383 AbstractPlotModule::dataFinished()
390 AbstractPlotModule::isFileOpen() const
392 return impl_->fp_ != NULL;
397 AbstractPlotModule::writeValue(const AnalysisDataValue &value) const
399 GMX_ASSERT(isFileOpen(), "File not opened, but write attempted");
400 const real y = value.isSet() ? value.value() : 0.0;
401 std::fprintf(impl_->fp_, impl_->yformat_, y);
402 if (impl_->bErrorsAsSeparateColumn_)
404 const real dy = value.isSet() ? value.error() : 0.0;
405 std::fprintf(impl_->fp_, impl_->yformat_, dy);
410 /********************************************************************
414 AnalysisDataPlotModule::AnalysisDataPlotModule()
418 AnalysisDataPlotModule::AnalysisDataPlotModule(
419 const AnalysisDataPlotSettings &settings)
420 : AbstractPlotModule(settings)
426 AnalysisDataPlotModule::pointsAdded(const AnalysisDataPointSetRef &points)
432 for (int i = 0; i < points.columnCount(); ++i)
434 writeValue(points.values()[i]);
439 /********************************************************************
440 * DataVectorPlotModule
443 AnalysisDataVectorPlotModule::AnalysisDataVectorPlotModule()
445 for (int i = 0; i < DIM; ++i)
449 bWrite_[DIM] = false;
453 AnalysisDataVectorPlotModule::AnalysisDataVectorPlotModule(
454 const AnalysisDataPlotSettings &settings)
455 : AbstractPlotModule(settings)
457 for (int i = 0; i < DIM; ++i)
461 bWrite_[DIM] = false;
466 AnalysisDataVectorPlotModule::setWriteX(bool bWrite)
468 bWrite_[XX] = bWrite;
473 AnalysisDataVectorPlotModule::setWriteY(bool bWrite)
475 bWrite_[YY] = bWrite;
480 AnalysisDataVectorPlotModule::setWriteZ(bool bWrite)
482 bWrite_[ZZ] = bWrite;
487 AnalysisDataVectorPlotModule::setWriteNorm(bool bWrite)
489 bWrite_[DIM] = bWrite;
494 AnalysisDataVectorPlotModule::setWriteMask(bool bWrite[DIM + 1])
496 for (int i = 0; i < DIM + 1; ++i)
498 bWrite_[i] = bWrite[i];
504 AnalysisDataVectorPlotModule::pointsAdded(const AnalysisDataPointSetRef &points)
506 if (points.firstColumn() % DIM != 0 || points.columnCount() % DIM != 0)
508 GMX_THROW(APIError("Partial data points"));
514 for (int i = 0; i < points.columnCount(); i += 3)
516 for (int d = 0; d < DIM; ++d)
520 writeValue(points.values()[i + d]);
525 const rvec y = { points.y(i), points.y(i + 1), points.y(i + 2) };
526 AnalysisDataValue value(norm(y));