SYCL: Avoid using no_init read accessor in rocFFT
[alexxy/gromacs.git] / src / gromacs / analysisdata / modules / plot.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2010,2011,2012,2013,2014 by the GROMACS development team.
5  * Copyright (c) 2015,2018,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.
9  *
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.
14  *
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.
19  *
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.
24  *
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.
32  *
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.
35  */
36 /*! \file
37  * \brief
38  * Declares gmx::AnalysisDataPlotModule for plotting data (into a file).
39  *
40  * \inpublicapi
41  * \ingroup module_analysisdata
42  * \author Teemu Murtola <teemu.murtola@gmail.com>
43  */
44 #ifndef GMX_ANALYSISDATA_MODULES_PLOT_H
45 #define GMX_ANALYSISDATA_MODULES_PLOT_H
46
47 #include <memory>
48 #include <string>
49
50 #include "gromacs/analysisdata/datamodule.h"
51 #include "gromacs/options/timeunitmanager.h"
52
53 enum class XvgFormat : int;
54
55 namespace gmx
56 {
57
58 class AnalysisDataValue;
59 class IOptionsContainer;
60 class SelectionCollection;
61
62 /*! \brief
63  * Common settings for data plots.
64  *
65  * \inpublicapi
66  * \ingroup module_analysisdata
67  */
68 class AnalysisDataPlotSettings
69 {
70 public:
71     //! Constructs default analysis plot settings.
72     AnalysisDataPlotSettings();
73
74     //! Returns the selection collection set with setSelectionCollection().
75     const SelectionCollection* selectionCollection() const { return selections_; }
76     //! Returns the time unit set with setTimeUnit().
77     TimeUnit timeUnit() const { return timeUnit_; }
78     /*! \brief
79      * Returns the plot format.
80      */
81     XvgFormat plotFormat() const { return plotFormat_; }
82
83     /*! \brief
84      * Set selection collection to print as comments into the output.
85      *
86      * Formatted selection text from all selections in \p selections is
87      * printed as comments in the output file.
88      * If this method is not called, no selection information is written
89      * to the output.
90      */
91     void setSelectionCollection(const SelectionCollection* selections);
92     /*! \brief
93      * Sets the time unit for the plot.
94      *
95      * The value is used only if AbstractPlotModule::setXAxisIsTime() is
96      * called, in which case it is used to print the appropriate axis label
97      * and to scale the values.
98      * If not called, the default time unit is ps.
99      */
100     void setTimeUnit(TimeUnit timeUnit) { timeUnit_ = timeUnit; }
101
102
103     /*! \brief
104      * Adds common options for setting plot options.
105      *
106      * \param[in,out] options Options object to which options are added.
107      */
108     void initOptions(IOptionsContainer* options);
109
110 private:
111     const SelectionCollection* selections_;
112     TimeUnit                   timeUnit_;
113     XvgFormat                  plotFormat_;
114 };
115
116 /*! \brief
117  * Abstract data module for writing data into a file.
118  *
119  * Implements features common to all plotting modules.  Subclasses implement
120  * features specific to certain applications (AnalysisDataPlotModule implements
121  * straightforward plotting).
122  *
123  * By default, the data is written into an xvgr file, according to the
124  * options read from the AnalysisDataPlotSettings object given to the
125  * constructor.
126  * For non-xvgr data, it's possible to skip all headers by calling
127  * setPlainOutput().
128  *
129  * A single output line corresponds to a single frame.  In most cases with
130  * multipoint data, setPlainOutput() should be called since the output does not
131  * make sense as an xvgr file, but this is not enforced.
132  *
133  * Multipoint data and multiple data sets are both supported, in which case all
134  * the points are written to the output, in the order in which they are added
135  * to the data.
136  *
137  * \ingroup module_analysisdata
138  */
139 class AbstractPlotModule : public AnalysisDataModuleSerial
140 {
141 public:
142     ~AbstractPlotModule() override;
143
144     /*! \brief
145      * Set common settings for the plotting.
146      */
147     void setSettings(const AnalysisDataPlotSettings& settings);
148     /*! \brief
149      * Set the output file name.
150      *
151      * If no file name is set (or if \p filename is empty), no output occurs.
152      */
153     void setFileName(const std::string& filename);
154     /*! \brief
155      * Set plain output.
156      *
157      * If \p bPlain is true, no xvgr headers are written to the file.
158      * In this case, only setOmitX(), setXFormat(), and setYFormat()
159      * methods have any effect on the output.
160      */
161     void setPlainOutput(bool bPlain);
162     /*! \brief
163      * Plot errors as a separate output column after each value column.
164      */
165     void setErrorsAsSeparateColumn(bool bSeparate);
166     /*! \brief
167      * Omit the X coordinates from the output.
168      *
169      * This method only makes sense when combined with setPlainOutput().
170      */
171     void setOmitX(bool bOmitX);
172     /*! \brief
173      * Set plot title.
174      */
175     void setTitle(const char* title);
176     //! \copydoc setTitle(const char *)
177     void setTitle(const std::string& title);
178     /*! \brief
179      * Set plot subtitle.
180      */
181     void setSubtitle(const char* subtitle);
182     //! \copydoc setSubtitle(const char *)
183     void setSubtitle(const std::string& subtitle);
184     /*! \brief
185      * Set X axis label.
186      */
187     void setXLabel(const char* label);
188     /*! \brief
189      * Treat X axis as time.
190      *
191      * Sets the label for the axis accordingly and also scales output to
192      * take into account the correct time unit.
193      */
194     void setXAxisIsTime();
195     /*! \brief
196      * Set Y axis label.
197      */
198     void setYLabel(const char* label);
199     /*! \brief
200      * Add legend from an array of strings.
201      *
202      * Multiple calls to setLegend() and/or appendLegend() are added
203      * together.
204      */
205     void setLegend(int nsets, const char* const* setname);
206     /*! \brief
207      * Add a legend string for the next data set.
208      *
209      * Multiple calls to setLegend() and/or appendLegend() are added
210      * together.
211      */
212     void appendLegend(const char* setname);
213     //! \copydoc appendLegend(const char *)
214     void appendLegend(const std::string& setname);
215     /*! \brief
216      * Set field width and precision for X value output.
217      */
218     void setXFormat(int width, int precision, char format = 'f');
219     /*! \brief
220      * Set field width and precision for Y value output.
221      */
222     void setYFormat(int width, int precision, char format = 'f');
223
224     int flags() const override;
225
226     void dataStarted(AbstractAnalysisData* data) override;
227     void frameStarted(const AnalysisDataFrameHeader& header) override;
228     void pointsAdded(const AnalysisDataPointSetRef& points) override = 0;
229     void frameFinished(const AnalysisDataFrameHeader& header) override;
230     void dataFinished() override;
231
232 protected:
233     /*! \cond libapi */
234     AbstractPlotModule();
235     //! Creates AbstractPlotModule and assign common settings.
236     explicit AbstractPlotModule(const AnalysisDataPlotSettings& settings);
237
238     //! Whether an output file has been opened.
239     bool isFileOpen() const;
240     /*! \brief
241      * Appends a single value to the current output line.
242      *
243      * \param[in] value  Value to append.
244      *
245      * Should be used from pointsAdded() implementations in derived classes
246      * to write out individual y values to the output.
247      *
248      * Must not be called if isFileOpen() returns false.
249      */
250     void writeValue(const AnalysisDataValue& value) const;
251     //! \endcond
252
253 private:
254     class Impl;
255
256     std::unique_ptr<Impl> impl_;
257 };
258
259
260 /*! \brief
261  * Plotting module for straightforward plotting of data.
262  *
263  * See AbstractPlotModule for common plotting options.
264  *
265  * \inpublicapi
266  * \ingroup module_analysisdata
267  */
268 class AnalysisDataPlotModule : public AbstractPlotModule
269 {
270 public:
271     AnalysisDataPlotModule();
272     //! Creates AnalysisDataPlotModule and assign common settings.
273     explicit AnalysisDataPlotModule(const AnalysisDataPlotSettings& settings);
274
275     void pointsAdded(const AnalysisDataPointSetRef& points) override;
276
277     // Copy and assign disallowed by base.
278 };
279
280
281 /*! \brief
282  * Plotting module specifically for data consisting of vectors.
283  *
284  * See AbstractPlotModule for common plotting options.
285  *
286  * \inpublicapi
287  * \ingroup module_analysisdata
288  */
289 class AnalysisDataVectorPlotModule : public AbstractPlotModule
290 {
291 public:
292     AnalysisDataVectorPlotModule();
293     //! Creates AnalysisDataVectorPlotModule and assign common settings.
294     explicit AnalysisDataVectorPlotModule(const AnalysisDataPlotSettings& settings);
295
296     /*! \brief
297      * Set whether to write X component.
298      */
299     void setWriteX(bool bWrite);
300     /*! \brief
301      * Set whether to write Y component.
302      */
303     void setWriteY(bool bWrite);
304     /*! \brief
305      * Set whether to write Z component.
306      */
307     void setWriteZ(bool bWrite);
308     /*! \brief
309      * Set whether to write norm of the vector.
310      */
311     void setWriteNorm(bool bWrite);
312     /*! \brief
313      * Set mask for what to write.
314      */
315     void setWriteMask(const bool bWrite[4]);
316
317     void pointsAdded(const AnalysisDataPointSetRef& points) override;
318
319 private:
320     bool bWrite_[4];
321
322     // Copy and assign disallowed by base.
323 };
324
325 //! Smart pointer to manage an AnalysisDataPlotModule object.
326 typedef std::shared_ptr<AnalysisDataPlotModule> AnalysisDataPlotModulePointer;
327 //! Smart pointer to manage an AnalysisDataVectorPlotModule object.
328 typedef std::shared_ptr<AnalysisDataVectorPlotModule> AnalysisDataVectorPlotModulePointer;
329
330 } // namespace gmx
331
332 #endif