2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010-2018, The GROMACS development team.
5 * Copyright (c) 2019, 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 //! Enum values for plot formats.
72 const char* const g_plotFormats[] = { "none", "xmgrace", "xmgr" };
79 /********************************************************************
80 * AnalysisDataPlotSettings
83 AnalysisDataPlotSettings::AnalysisDataPlotSettings() :
85 timeUnit_(TimeUnit_Default),
90 void AnalysisDataPlotSettings::setSelectionCollection(const SelectionCollection* selections)
92 selections_ = selections;
96 void AnalysisDataPlotSettings::initOptions(IOptionsContainer* options)
99 EnumIntOption("xvg").enumValue(g_plotFormats).store(&plotFormat_).description("Plot formatting"));
103 /********************************************************************
104 * AbstractPlotModule::Impl
107 class AbstractPlotModule::Impl
110 explicit Impl(const AnalysisDataPlotSettings& settings);
115 AnalysisDataPlotSettings settings_;
116 std::string filename_;
121 bool bErrorsAsSeparateColumn_;
123 std::string subtitle_;
126 std::vector<std::string> legend_;
127 std::string xformat_;
128 std::string yformat_;
132 AbstractPlotModule::Impl::Impl(const AnalysisDataPlotSettings& settings) :
137 bErrorsAsSeparateColumn_(false),
144 AbstractPlotModule::Impl::~Impl()
150 void AbstractPlotModule::Impl::closeFile()
167 /********************************************************************
171 AbstractPlotModule::AbstractPlotModule() : impl_(new Impl(AnalysisDataPlotSettings())) {}
173 AbstractPlotModule::AbstractPlotModule(const AnalysisDataPlotSettings& settings) :
174 impl_(new Impl(settings))
179 AbstractPlotModule::~AbstractPlotModule() {}
182 void AbstractPlotModule::setSettings(const AnalysisDataPlotSettings& settings)
184 impl_->settings_ = settings;
188 void AbstractPlotModule::setFileName(const std::string& filename)
190 impl_->filename_ = filename;
194 void AbstractPlotModule::setPlainOutput(bool bPlain)
196 impl_->bPlain_ = bPlain;
200 void AbstractPlotModule::setErrorsAsSeparateColumn(bool bSeparate)
202 impl_->bErrorsAsSeparateColumn_ = bSeparate;
206 void AbstractPlotModule::setOmitX(bool bOmitX)
208 impl_->bOmitX_ = bOmitX;
212 void AbstractPlotModule::setTitle(const char* title)
214 impl_->title_ = title;
217 void AbstractPlotModule::setTitle(const std::string& title)
219 impl_->title_ = title;
223 void AbstractPlotModule::setSubtitle(const char* subtitle)
225 impl_->subtitle_ = subtitle;
229 void AbstractPlotModule::setSubtitle(const std::string& subtitle)
231 impl_->subtitle_ = subtitle;
235 void AbstractPlotModule::setXLabel(const char* label)
237 impl_->xlabel_ = label;
241 void AbstractPlotModule::setXAxisIsTime()
243 TimeUnitManager manager(impl_->settings_.timeUnit());
244 impl_->xlabel_ = formatString("Time (%s)", manager.timeUnitAsString());
245 impl_->xscale_ = manager.inverseTimeScaleFactor();
249 void AbstractPlotModule::setYLabel(const char* label)
251 impl_->ylabel_ = label;
255 void AbstractPlotModule::setLegend(int nsets, const char* const* setname)
257 impl_->legend_.reserve(impl_->legend_.size() + nsets);
258 for (int i = 0; i < nsets; ++i)
260 appendLegend(setname[i]);
265 void AbstractPlotModule::appendLegend(const char* setname)
267 impl_->legend_.emplace_back(setname);
271 void AbstractPlotModule::appendLegend(const std::string& setname)
273 impl_->legend_.push_back(setname);
277 void AbstractPlotModule::setXFormat(int width, int precision, char format)
279 GMX_RELEASE_ASSERT(width >= 0 && precision >= 0 && width <= 99 && precision <= 99,
280 "Invalid width or precision");
281 GMX_RELEASE_ASSERT(strchr("eEfFgG", format) != nullptr, "Invalid format specifier");
282 impl_->xformat_ = formatString("%%%d.%d%c", width, precision, format);
286 void AbstractPlotModule::setYFormat(int width, int precision, char format)
288 GMX_RELEASE_ASSERT(width >= 0 && precision >= 0 && width <= 99 && precision <= 99,
289 "Invalid width or precision");
290 GMX_RELEASE_ASSERT(strchr("eEfFgG", format) != nullptr, "Invalid format specifier");
291 impl_->yformat_ = formatString(" %%%d.%d%c", width, precision, format);
295 int AbstractPlotModule::flags() const
297 return efAllowMissing | efAllowMulticolumn | efAllowMultipoint | efAllowMultipleDataSets;
301 void AbstractPlotModule::dataStarted(AbstractAnalysisData* /* data */)
303 if (!impl_->filename_.empty())
307 impl_->fp_ = gmx_fio_fopen(impl_->filename_.c_str(), "w");
311 time_unit_t time_unit = static_cast<time_unit_t>(
312 impl_->settings_.timeUnit() + 1); // NOLINT(bugprone-misplaced-widening-cast)
313 xvg_format_t xvg_format = (impl_->settings_.plotFormat() > 0
314 ? static_cast<xvg_format_t>(impl_->settings_.plotFormat())
316 gmx_output_env_t* oenv;
317 output_env_init(&oenv, getProgramContext(), time_unit, FALSE, xvg_format, 0);
318 const unique_cptr<gmx_output_env_t, output_env_done> oenvGuard(oenv);
319 impl_->fp_ = xvgropen(impl_->filename_.c_str(), impl_->title_.c_str(), impl_->xlabel_,
320 impl_->ylabel_, oenv);
321 const SelectionCollection* selections = impl_->settings_.selectionCollection();
322 if (selections != nullptr && output_env_get_xvg_format(oenv) != exvgNONE)
324 selections->printXvgrInfo(impl_->fp_);
326 if (!impl_->subtitle_.empty())
328 xvgr_subtitle(impl_->fp_, impl_->subtitle_.c_str(), oenv);
330 if (output_env_get_print_xvgr_codes(oenv) && !impl_->legend_.empty())
332 std::vector<const char*> legend;
333 legend.reserve(impl_->legend_.size());
334 for (size_t i = 0; i < impl_->legend_.size(); ++i)
336 legend.push_back(impl_->legend_[i].c_str());
338 xvgr_legend(impl_->fp_, legend.size(), legend.data(), oenv);
345 void AbstractPlotModule::frameStarted(const AnalysisDataFrameHeader& header)
353 std::fprintf(impl_->fp_, impl_->xformat_.c_str(), header.x() * impl_->xscale_);
358 void AbstractPlotModule::frameFinished(const AnalysisDataFrameHeader& /*header*/)
364 std::fprintf(impl_->fp_, "\n");
368 void AbstractPlotModule::dataFinished()
374 bool AbstractPlotModule::isFileOpen() const
376 return impl_->fp_ != nullptr;
380 void AbstractPlotModule::writeValue(const AnalysisDataValue& value) const
382 GMX_ASSERT(isFileOpen(), "File not opened, but write attempted");
383 const real y = value.isSet() ? value.value() : 0.0;
384 std::fprintf(impl_->fp_, impl_->yformat_.c_str(), y);
385 if (impl_->bErrorsAsSeparateColumn_)
387 const real dy = value.isSet() ? value.error() : 0.0;
388 std::fprintf(impl_->fp_, impl_->yformat_.c_str(), dy);
393 /********************************************************************
397 AnalysisDataPlotModule::AnalysisDataPlotModule() {}
399 AnalysisDataPlotModule::AnalysisDataPlotModule(const AnalysisDataPlotSettings& settings) :
400 AbstractPlotModule(settings)
405 void AnalysisDataPlotModule::pointsAdded(const AnalysisDataPointSetRef& points)
411 for (int i = 0; i < points.columnCount(); ++i)
413 writeValue(points.values()[i]);
418 /********************************************************************
419 * DataVectorPlotModule
422 AnalysisDataVectorPlotModule::AnalysisDataVectorPlotModule() : bWrite_{ true, true, true, false } {}
425 AnalysisDataVectorPlotModule::AnalysisDataVectorPlotModule(const AnalysisDataPlotSettings& settings) :
426 AbstractPlotModule(settings),
427 bWrite_{ true, true, true, false }
432 void AnalysisDataVectorPlotModule::setWriteX(bool bWrite)
434 bWrite_[XX] = bWrite;
438 void AnalysisDataVectorPlotModule::setWriteY(bool bWrite)
440 bWrite_[YY] = bWrite;
444 void AnalysisDataVectorPlotModule::setWriteZ(bool bWrite)
446 bWrite_[ZZ] = bWrite;
450 void AnalysisDataVectorPlotModule::setWriteNorm(bool bWrite)
452 bWrite_[DIM] = bWrite;
456 void AnalysisDataVectorPlotModule::setWriteMask(const bool bWrite[DIM + 1])
458 for (int i = 0; i < DIM + 1; ++i)
460 bWrite_[i] = bWrite[i];
465 void AnalysisDataVectorPlotModule::pointsAdded(const AnalysisDataPointSetRef& points)
467 if (points.firstColumn() % DIM != 0 || points.columnCount() % DIM != 0)
469 GMX_THROW(APIError("Partial data points"));
475 for (int i = 0; i < points.columnCount(); i += 3)
477 for (int d = 0; d < DIM; ++d)
481 writeValue(points.values()[i + d]);
486 const rvec y = { points.y(i), points.y(i + 1), points.y(i + 2) };
487 AnalysisDataValue value(norm(y));