2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010-2018, The GROMACS development team.
5 * Copyright (c) 2019,2020,2021, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
38 * Implements classes in plot.h.
40 * \ingroup module_analysisdata
41 * \author Teemu Murtola <teemu.murtola@gmail.com>
53 #include "gromacs/analysisdata/dataframe.h"
54 #include "gromacs/fileio/gmxfio.h"
55 #include "gromacs/fileio/oenv.h"
56 #include "gromacs/fileio/xvgr.h"
57 #include "gromacs/math/vec.h"
58 #include "gromacs/options/basicoptions.h"
59 #include "gromacs/options/ioptionscontainer.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/programcontext.h"
65 #include "gromacs/utility/stringutil.h"
66 #include "gromacs/utility/unique_cptr.h"
71 /********************************************************************
72 * AnalysisDataPlotSettings
75 AnalysisDataPlotSettings::AnalysisDataPlotSettings() :
76 selections_(nullptr), timeUnit_(TimeUnit::Default), plotFormat_(XvgFormat::Xmgrace)
80 void AnalysisDataPlotSettings::setSelectionCollection(const SelectionCollection* selections)
82 selections_ = selections;
85 /*! \brief Names for XvgFormat
87 * Technically this duplicates a definition in pargs.cpp for legacy
88 * support code, but as the latter will go away and the alternatives
89 * are ugly, the duplication is acceptable. */
90 const gmx::EnumerationArray<XvgFormat, const char*> c_xvgFormatNames = {
91 { "xmgrace", "xmgr", "none" }
94 void AnalysisDataPlotSettings::initOptions(IOptionsContainer* options)
97 EnumOption<XvgFormat>("xvg").enumValue(c_xvgFormatNames).store(&plotFormat_).description("Plot formatting"));
101 /********************************************************************
102 * AbstractPlotModule::Impl
105 class AbstractPlotModule::Impl
108 explicit Impl(const AnalysisDataPlotSettings& settings);
113 AnalysisDataPlotSettings settings_;
114 std::string filename_;
119 bool bErrorsAsSeparateColumn_;
121 std::string subtitle_;
124 std::vector<std::string> legend_;
125 std::string xformat_;
126 std::string yformat_;
130 AbstractPlotModule::Impl::Impl(const AnalysisDataPlotSettings& settings) :
135 bErrorsAsSeparateColumn_(false),
142 AbstractPlotModule::Impl::~Impl()
148 void AbstractPlotModule::Impl::closeFile()
165 /********************************************************************
169 AbstractPlotModule::AbstractPlotModule() : impl_(new Impl(AnalysisDataPlotSettings())) {}
171 AbstractPlotModule::AbstractPlotModule(const AnalysisDataPlotSettings& settings) :
172 impl_(new Impl(settings))
177 AbstractPlotModule::~AbstractPlotModule() {}
180 void AbstractPlotModule::setSettings(const AnalysisDataPlotSettings& settings)
182 impl_->settings_ = settings;
186 void AbstractPlotModule::setFileName(const std::string& filename)
188 impl_->filename_ = filename;
192 void AbstractPlotModule::setPlainOutput(bool bPlain)
194 impl_->bPlain_ = bPlain;
198 void AbstractPlotModule::setErrorsAsSeparateColumn(bool bSeparate)
200 impl_->bErrorsAsSeparateColumn_ = bSeparate;
204 void AbstractPlotModule::setOmitX(bool bOmitX)
206 impl_->bOmitX_ = bOmitX;
210 void AbstractPlotModule::setTitle(const char* title)
212 impl_->title_ = title;
215 void AbstractPlotModule::setTitle(const std::string& title)
217 impl_->title_ = title;
221 void AbstractPlotModule::setSubtitle(const char* subtitle)
223 impl_->subtitle_ = subtitle;
227 void AbstractPlotModule::setSubtitle(const std::string& subtitle)
229 impl_->subtitle_ = subtitle;
233 void AbstractPlotModule::setXLabel(const char* label)
235 impl_->xlabel_ = label;
239 void AbstractPlotModule::setXAxisIsTime()
241 TimeUnitManager manager(impl_->settings_.timeUnit());
242 impl_->xlabel_ = formatString("Time (%s)", manager.timeUnitAsString());
243 impl_->xscale_ = manager.inverseTimeScaleFactor();
247 void AbstractPlotModule::setYLabel(const char* label)
249 impl_->ylabel_ = label;
253 void AbstractPlotModule::setLegend(int nsets, const char* const* setname)
255 impl_->legend_.reserve(impl_->legend_.size() + nsets);
256 for (int i = 0; i < nsets; ++i)
258 appendLegend(setname[i]);
263 void AbstractPlotModule::appendLegend(const char* setname)
265 impl_->legend_.emplace_back(setname);
269 void AbstractPlotModule::appendLegend(const std::string& setname)
271 impl_->legend_.push_back(setname);
275 void AbstractPlotModule::setXFormat(int width, int precision, char format)
277 GMX_RELEASE_ASSERT(width >= 0 && precision >= 0 && width <= 99 && precision <= 99,
278 "Invalid width or precision");
279 GMX_RELEASE_ASSERT(strchr("eEfFgG", format) != nullptr, "Invalid format specifier");
280 impl_->xformat_ = formatString("%%%d.%d%c", width, precision, format);
284 void AbstractPlotModule::setYFormat(int width, int precision, char format)
286 GMX_RELEASE_ASSERT(width >= 0 && precision >= 0 && width <= 99 && precision <= 99,
287 "Invalid width or precision");
288 GMX_RELEASE_ASSERT(strchr("eEfFgG", format) != nullptr, "Invalid format specifier");
289 impl_->yformat_ = formatString(" %%%d.%d%c", width, precision, format);
293 int AbstractPlotModule::flags() const
295 return efAllowMissing | efAllowMulticolumn | efAllowMultipoint | efAllowMultipleDataSets;
299 void AbstractPlotModule::dataStarted(AbstractAnalysisData* /* data */)
301 if (!impl_->filename_.empty())
305 impl_->fp_ = gmx_fio_fopen(impl_->filename_.c_str(), "w");
309 const TimeUnit timeUnit = impl_->settings_.timeUnit();
310 const XvgFormat xvgFormat = impl_->settings_.plotFormat();
311 gmx_output_env_t* oenv;
312 output_env_init(&oenv, getProgramContext(), timeUnit, FALSE, xvgFormat, 0);
313 const unique_cptr<gmx_output_env_t, output_env_done> oenvGuard(oenv);
314 impl_->fp_ = xvgropen(
315 impl_->filename_.c_str(), impl_->title_.c_str(), impl_->xlabel_, impl_->ylabel_, oenv);
316 const SelectionCollection* selections = impl_->settings_.selectionCollection();
317 if (selections != nullptr && output_env_get_xvg_format(oenv) != XvgFormat::None)
319 selections->printXvgrInfo(impl_->fp_);
321 if (!impl_->subtitle_.empty())
323 xvgr_subtitle(impl_->fp_, impl_->subtitle_.c_str(), oenv);
325 if (output_env_get_print_xvgr_codes(oenv) && !impl_->legend_.empty())
327 std::vector<const char*> legend;
328 legend.reserve(impl_->legend_.size());
329 for (size_t i = 0; i < impl_->legend_.size(); ++i)
331 legend.push_back(impl_->legend_[i].c_str());
333 xvgr_legend(impl_->fp_, legend.size(), legend.data(), oenv);
340 void AbstractPlotModule::frameStarted(const AnalysisDataFrameHeader& header)
348 std::fprintf(impl_->fp_, impl_->xformat_.c_str(), header.x() * impl_->xscale_);
353 void AbstractPlotModule::frameFinished(const AnalysisDataFrameHeader& /*header*/)
359 std::fprintf(impl_->fp_, "\n");
363 void AbstractPlotModule::dataFinished()
369 bool AbstractPlotModule::isFileOpen() const
371 return impl_->fp_ != nullptr;
375 void AbstractPlotModule::writeValue(const AnalysisDataValue& value) const
377 GMX_ASSERT(isFileOpen(), "File not opened, but write attempted");
378 const real y = value.isSet() ? value.value() : 0.0;
379 std::fprintf(impl_->fp_, impl_->yformat_.c_str(), y);
380 if (impl_->bErrorsAsSeparateColumn_)
382 const real dy = value.isSet() ? value.error() : 0.0;
383 std::fprintf(impl_->fp_, impl_->yformat_.c_str(), dy);
388 /********************************************************************
392 AnalysisDataPlotModule::AnalysisDataPlotModule() {}
394 AnalysisDataPlotModule::AnalysisDataPlotModule(const AnalysisDataPlotSettings& settings) :
395 AbstractPlotModule(settings)
400 void AnalysisDataPlotModule::pointsAdded(const AnalysisDataPointSetRef& points)
406 for (int i = 0; i < points.columnCount(); ++i)
408 writeValue(points.values()[i]);
413 /********************************************************************
414 * DataVectorPlotModule
417 AnalysisDataVectorPlotModule::AnalysisDataVectorPlotModule() : bWrite_{ true, true, true, false } {}
420 AnalysisDataVectorPlotModule::AnalysisDataVectorPlotModule(const AnalysisDataPlotSettings& settings) :
421 AbstractPlotModule(settings), bWrite_{ true, true, true, false }
426 void AnalysisDataVectorPlotModule::setWriteX(bool bWrite)
428 bWrite_[XX] = bWrite;
432 void AnalysisDataVectorPlotModule::setWriteY(bool bWrite)
434 bWrite_[YY] = bWrite;
438 void AnalysisDataVectorPlotModule::setWriteZ(bool bWrite)
440 bWrite_[ZZ] = bWrite;
444 void AnalysisDataVectorPlotModule::setWriteNorm(bool bWrite)
446 bWrite_[DIM] = bWrite;
450 void AnalysisDataVectorPlotModule::setWriteMask(const bool bWrite[DIM + 1])
452 for (int i = 0; i < DIM + 1; ++i)
454 bWrite_[i] = bWrite[i];
459 void AnalysisDataVectorPlotModule::pointsAdded(const AnalysisDataPointSetRef& points)
461 if (points.firstColumn() % DIM != 0 || points.columnCount() % DIM != 0)
463 GMX_THROW(APIError("Partial data points"));
469 for (int i = 0; i < points.columnCount(); i += 3)
471 for (int d = 0; d < DIM; ++d)
475 writeValue(points.values()[i + d]);
480 const rvec y = { points.y(i), points.y(i + 1), points.y(i + 2) };
481 AnalysisDataValue value(norm(y));