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"
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/options/basicoptions.h"
58 #include "gromacs/analysisdata/dataframe.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"
66 static const char *const g_plotFormats[] = {
67 "none", "xmgrace", "xmgr", NULL
73 /********************************************************************
74 * AnalysisDataPlotSettings
77 AnalysisDataPlotSettings::AnalysisDataPlotSettings()
78 : selections_(NULL), timeUnit_(eTimeUnit_ps), plotFormat_(1)
83 AnalysisDataPlotSettings::setSelectionCollection(const SelectionCollection *selections)
85 selections_ = selections;
90 AnalysisDataPlotSettings::addOptions(Options *options)
92 options->addOption(StringOption("xvg").enumValue(g_plotFormats)
93 .defaultValue("xmgrace")
94 .storeEnumIndex(&plotFormat_)
95 .description("Plot formatting"));
99 /********************************************************************
100 * AbstractPlotModule::Impl
103 class AbstractPlotModule::Impl
106 explicit Impl(const AnalysisDataPlotSettings &settings);
111 AnalysisDataPlotSettings settings_;
112 std::string filename_;
118 std::string subtitle_;
121 std::vector<std::string> legend_;
127 AbstractPlotModule::Impl::Impl(const AnalysisDataPlotSettings &settings)
128 : settings_(settings), fp_(NULL), bPlain_(false), gOmitX_(false),
131 strcpy(xformat_, "%11.3f");
132 strcpy(yformat_, " %8.3f");
135 AbstractPlotModule::Impl::~Impl()
142 AbstractPlotModule::Impl::closeFile()
159 /********************************************************************
163 AbstractPlotModule::AbstractPlotModule()
164 : impl_(new Impl(AnalysisDataPlotSettings()))
168 AbstractPlotModule::AbstractPlotModule(const AnalysisDataPlotSettings &settings)
169 : impl_(new Impl(settings))
174 AbstractPlotModule::~AbstractPlotModule()
180 AbstractPlotModule::setSettings(const AnalysisDataPlotSettings &settings)
182 impl_->settings_ = settings;
187 AbstractPlotModule::setFileName(const std::string &filename)
189 impl_->filename_ = filename;
194 AbstractPlotModule::setPlainOutput(bool bPlain)
196 impl_->bPlain_ = bPlain;
201 AbstractPlotModule::setOmitX(bool bOmitX)
203 impl_->gOmitX_ = bOmitX;
208 AbstractPlotModule::setTitle(const char *title)
210 impl_->title_ = title;
215 AbstractPlotModule::setSubtitle(const char *subtitle)
217 impl_->subtitle_ = subtitle;
222 AbstractPlotModule::setXLabel(const char *label)
224 impl_->xlabel_ = label;
229 AbstractPlotModule::setXAxisIsTime()
231 TimeUnitManager manager(impl_->settings_.timeUnit());
232 impl_->xlabel_ = formatString("Time (%s)", manager.timeUnitAsString());
233 impl_->xscale_ = manager.inverseTimeScaleFactor();
238 AbstractPlotModule::setYLabel(const char *label)
240 impl_->ylabel_ = label;
245 AbstractPlotModule::setLegend(int nsets, const char * const *setname)
247 impl_->legend_.reserve(impl_->legend_.size() + nsets);
248 for (int i = 0; i < nsets; ++i)
250 appendLegend(setname[i]);
256 AbstractPlotModule::appendLegend(const char *setname)
258 impl_->legend_.push_back(setname);
263 AbstractPlotModule::setXFormat(int width, int precision, char format)
265 GMX_RELEASE_ASSERT(width >= 0 && precision >= 0
266 && width <= 99 && precision <= 99,
267 "Invalid width or precision");
268 GMX_RELEASE_ASSERT(strchr("eEfFgG", format) != NULL,
269 "Invalid format specifier");
270 sprintf(impl_->xformat_, "%%%d.%d%c", width, precision, format);
275 AbstractPlotModule::setYFormat(int width, int precision, char format)
277 GMX_RELEASE_ASSERT(width >= 0 && precision >= 0
278 && width <= 99 && precision <= 99,
279 "Invalid width or precision");
280 GMX_RELEASE_ASSERT(strchr("eEfFgG", format) != NULL,
281 "Invalid format specifier");
282 sprintf(impl_->yformat_, " %%%d.%d%c", width, precision, format);
287 AbstractPlotModule::flags() const
289 return efAllowMulticolumn | efAllowMultipoint;
294 AbstractPlotModule::dataStarted(AbstractAnalysisData *data)
296 if (!impl_->filename_.empty())
300 impl_->fp_ = gmx_fio_fopen(impl_->filename_.c_str(), "w");
304 time_unit_t time_unit
305 = static_cast<time_unit_t>(impl_->settings_.timeUnit() + 1);
306 xvg_format_t xvg_format
307 = (impl_->settings_.plotFormat() > 0
308 ? static_cast<xvg_format_t>(impl_->settings_.plotFormat())
311 output_env_init(&oenv, 0, NULL, time_unit, FALSE, xvg_format, 0, 0);
312 boost::shared_ptr<output_env> oenvGuard(oenv, &output_env_done);
313 impl_->fp_ = xvgropen(impl_->filename_.c_str(), impl_->title_.c_str(),
314 impl_->xlabel_.c_str(), impl_->ylabel_.c_str(),
316 const SelectionCollection *selections
317 = impl_->settings_.selectionCollection();
318 if (selections != NULL)
320 selections->printXvgrInfo(impl_->fp_, oenv);
322 if (!impl_->subtitle_.empty())
324 xvgr_subtitle(impl_->fp_, impl_->subtitle_.c_str(), oenv);
326 if (output_env_get_print_xvgr_codes(oenv)
327 && !impl_->legend_.empty())
329 std::vector<const char *> legend;
330 legend.reserve(impl_->legend_.size());
331 for (size_t i = 0; i < impl_->legend_.size(); ++i)
333 legend.push_back(impl_->legend_[i].c_str());
335 xvgr_legend(impl_->fp_, legend.size(), &legend[0], oenv);
343 AbstractPlotModule::frameStarted(const AnalysisDataFrameHeader &frame)
351 std::fprintf(impl_->fp_, impl_->xformat_, frame.x() * impl_->xscale_);
357 AbstractPlotModule::frameFinished(const AnalysisDataFrameHeader & /*header*/)
363 std::fprintf(impl_->fp_, "\n");
368 AbstractPlotModule::dataFinished()
375 AbstractPlotModule::isFileOpen() const
377 return impl_->fp_ != NULL;
382 AbstractPlotModule::writeValue(real value) const
384 GMX_ASSERT(isFileOpen(), "File not opened, but write attempted");
385 std::fprintf(impl_->fp_, impl_->yformat_, value);
389 /********************************************************************
393 AnalysisDataPlotModule::AnalysisDataPlotModule()
397 AnalysisDataPlotModule::AnalysisDataPlotModule(
398 const AnalysisDataPlotSettings &settings)
399 : AbstractPlotModule(settings)
405 AnalysisDataPlotModule::pointsAdded(const AnalysisDataPointSetRef &points)
411 for (int i = 0; i < points.columnCount(); ++i)
413 writeValue(points.y(i));
418 /********************************************************************
419 * DataVectorPlotModule
422 AnalysisDataVectorPlotModule::AnalysisDataVectorPlotModule()
424 for (int i = 0; i < DIM; ++i)
428 bWrite_[DIM] = false;
432 AnalysisDataVectorPlotModule::AnalysisDataVectorPlotModule(
433 const AnalysisDataPlotSettings &settings)
434 : AbstractPlotModule(settings)
436 for (int i = 0; i < DIM; ++i)
440 bWrite_[DIM] = false;
445 AnalysisDataVectorPlotModule::setWriteX(bool bWrite)
447 bWrite_[XX] = bWrite;
452 AnalysisDataVectorPlotModule::setWriteY(bool bWrite)
454 bWrite_[YY] = bWrite;
459 AnalysisDataVectorPlotModule::setWriteZ(bool bWrite)
461 bWrite_[ZZ] = bWrite;
466 AnalysisDataVectorPlotModule::setWriteNorm(bool bWrite)
468 bWrite_[DIM] = bWrite;
473 AnalysisDataVectorPlotModule::setWriteMask(bool bWrite[DIM + 1])
475 for (int i = 0; i < DIM + 1; ++i)
477 bWrite_[i] = bWrite[i];
483 AnalysisDataVectorPlotModule::pointsAdded(const AnalysisDataPointSetRef &points)
485 if (points.firstColumn() % DIM != 0)
487 GMX_THROW(APIError("Partial data points"));
493 for (int i = 0; i < points.columnCount(); i += 3)
495 for (int d = 0; d < DIM; ++d)
499 writeValue(points.y(i + d));
504 rvec y = { points.y(i), points.y(i + 1), points.y(i + 2) };