gnuplotinterface.hh 9.99 KB
Newer Older
Thomas Fetzer's avatar
Thomas Fetzer committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
/*****************************************************************************
 *   See the file COPYING for full copying permissions.                      *
 *                                                                           *
 *   This program is free software: you can redistribute it and/or modify    *
 *   it under the terms of the GNU General Public License as published by    *
 *   the Free Software Foundation, either version 2 of the License, or       *
 *   (at your option) any later version.                                     *
 *                                                                           *
 *   This program is distributed in the hope that it will be useful,         *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            *
 *   GNU General Public License for more details.                            *
 *                                                                           *
 *   You should have received a copy of the GNU General Public License       *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.   *
 *****************************************************************************/
/*!
 * \file
 *
 * \brief Interface for passing data sets to a file and plotting them, if gnuplot
 *        is installed.
 *
 * The data sets for a specific window have to be passed by the addDataSet function
 * and then plotted by using the plot function.
 */

#ifndef HAVE_GNUPLOT
Thomas Fetzer's avatar
Thomas Fetzer committed
30
31
32
#warning Gnuplot has not been found by CMake. Defaulting to /usr/bin/gnuplot
#define GNUPLOT_EXECUTABLE "/usr/bin/gnuplot"
#endif
Thomas Fetzer's avatar
Thomas Fetzer committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296

#ifndef DUMUX_GNUPLOT_INTERFACE_HH
#define DUMUX_GNUPLOT_INTERFACE_HH

#include <cassert>
#include <cmath>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

namespace Dumux
{
/*!
 * \brief Interface for passing data sets to a file and plotting them, if gnuplot
 *        is installed.
 */
template<class Scalar>
class GnuplotInterface
{
public:
    typedef std::vector<std::string> StringVector;
    enum PlotStyle
    {
        lines, points, linesPoints, impulses, dots
    };

    GnuplotInterface() :
        plotStyle_("lines"), pipe_(0),
        fileName_(0), plotName_(0),
        xRangeMin_(0), xRangeMax_(0),
        yRangeMin_(0), yRangeMax_(0),
        xLabel_(0), yLabel_(0),
        gnuplotPath_(GNUPLOT_EXECUTABLE),
        terminalType_("wxt"),
        numberOfGnuplotWindows_(8+1)
    {
        {
            pipe_ = popen((gnuplotPath_ + " -persist").c_str(), "w"); // "w" - writing
        }
        fileName_.resize(numberOfGnuplotWindows_);
        plotName_.resize(numberOfGnuplotWindows_);
        xRangeMin_.resize(numberOfGnuplotWindows_, 1e100);
        xRangeMax_.resize(numberOfGnuplotWindows_, -1e100);
        yRangeMin_.resize(numberOfGnuplotWindows_, 1e100);
        yRangeMax_.resize(numberOfGnuplotWindows_, -1e100);
        xLabel_.resize(numberOfGnuplotWindows_, "");
        yLabel_.resize(numberOfGnuplotWindows_, "");
    }

    ~GnuplotInterface()
    {
        if (pclose(pipe_) == -1)
            assert("Could not close pipe to Gnuplot!");
    }
    
    /*!
     * \brief Plots the files for a specific window number, writes a gnuplot and png file.
     *
     * \param title The name of the output file
     * \param plottingWindowNumber The ID of the specific plot
     */
    void plot(const std::string &title,
              int plottingWindowNumber,
              bool interaction)
    {
        // setting correct terminal
        std::string plot = "set term ";

        if (interaction)
            plot += "wxt " + numberToString(plottingWindowNumber) + "\n";
        else
            plot += " pngcairo size 800,600 \nset output \"" + title + ".png\"\n";

        plot += "set yrange [" + numberToString(yRangeMin_[plottingWindowNumber])
                + ":" + numberToString(yRangeMax_[plottingWindowNumber]) + "]" + "\n";
        plot += "set xrange [" + numberToString(xRangeMin_[plottingWindowNumber])
                + ":" + numberToString(xRangeMax_[plottingWindowNumber]) + "]" + "\n";

        plot += "set xlabel \"" + xLabel_[plottingWindowNumber] + "\"\n";
        plot += "set ylabel \"" + yLabel_[plottingWindowNumber] + "\"\n";
        
        // plot curves
        plot += "plot \"" + fileName_[plottingWindowNumber][0]
               + "\" title \"" + plotName_[plottingWindowNumber][0]
               + "\" with " + plotStyle_;

        for (unsigned int i = 1; i < fileName_[plottingWindowNumber].size(); ++i)
        {
            plot += ", \"" + fileName_[plottingWindowNumber][i]
                    + "\" title \"" + plotName_[plottingWindowNumber][i]
                    + "\" with " + plotStyle_;
        }
        plot += "\n";

        if (!interaction)
        {
            std::string fileName = title + ".gp";
            std::ofstream file;
            file.open(fileName);
            file << plot;
            file.close();
        }

        executeGnuplot(plot.c_str());
    }

    /*!
     * \brief Adds data set for specific window number and writes a data file
     *
     * \param x Vector containing the x-axis data points
     * \param y Vector containing the y-axis data points
     * \param plotName The name of the data set
     * \param plottingWindowNumber The ID of the specific plot
     */
    void addDataSet(const std::vector<Scalar>& x,
                    const std::vector<Scalar>& y,
                    const std::string &plotName,
                    int plottingWindowNumber)
    {
        assert(x.size() == y.size());

        //write data to file
        std::string fileName = numberToString(plottingWindowNumber) + "_" + plotName + ".dat";
        std::ofstream file;
        file.open(fileName);
        for (unsigned int i = 0; i < x.size(); i++)
        {
            checkNumber(x[i], "x[i] i=" + numberToString(i) + " in " + fileName);
            checkNumber(y[i], "y[i] i=" + numberToString(i) + " in " + fileName);
            file << x[i] << " " << y[i] << std::endl;
        }
        file.close();

        // adding file to list of plotted lines
        fileName_[plottingWindowNumber].push_back(fileName);
        plotName_[plottingWindowNumber].push_back(plotName);
    }

    /*!
     * \brief Sets the label for the x-axis
     *
     * \param label The label of the x-axis
     * \param plottingWindowNumber The ID of the specific plot
     */
    void setXlabel(const std::string& label,
                   unsigned int plottingWindowNumber)
    {
        xLabel_[plottingWindowNumber] = label;
    }

    /*!
     * \brief Sets the label for the y-axis
     *
     * \param label The label of the y-axis
     * \param plottingWindowNumber The ID of the specific plot
     */
    void setYlabel(const std::string& label,
                   unsigned int plottingWindowNumber)
    {
        yLabel_[plottingWindowNumber] = label;
    }

    /*!
     * \brief Sets the range for the x-axis
     *
     * \param lowerEnd The lowest plotted value for the x-axis
     * \param upperEnd The highest plotted value for the x-axis
     * \param plottingWindowNumber The ID of the specific plot
     */
    void setXRange(Scalar lowerEnd,
                   Scalar upperEnd,
                   unsigned int plottingWindowNumber)
    {
        xRangeMin_[plottingWindowNumber] = std::min(xRangeMin_[plottingWindowNumber], lowerEnd);
        xRangeMax_[plottingWindowNumber] = std::max(xRangeMax_[plottingWindowNumber], upperEnd);
    }

    /*!
     * \brief Sets the range for the y-axis
     *
     * \param lowerEnd The lowest plotted value for the y-axis
     * \param upperEnd The highest plotted value for the y-axis
     * \param plottingWindowNumber The ID of the specific plot
     */
    void setYRange(Scalar lowerEnd,
                   Scalar upperEnd,
                   unsigned int plottingWindowNumber)
    {
        yRangeMin_[plottingWindowNumber] = std::min(yRangeMin_[plottingWindowNumber], lowerEnd);
        yRangeMax_[plottingWindowNumber] = std::max(yRangeMax_[plottingWindowNumber], upperEnd);
    }

    /*!
     * \brief Sets the plotting style for the data sets
     *
     * \param style Plot style of the data sets
     */
    void setStyle(const PlotStyle& style)
    {
        switch (style)
        {
        case lines:
            plotStyle_ = "lines";
            break;
        case points:
            plotStyle_ = "points";
            break;
        case linesPoints:
            plotStyle_ = "linespoints";
            break;
        case impulses:
            plotStyle_ = "impulses";
            break;
        case dots:
            plotStyle_ = "dots";
            break;
        default:
            assert(!"Unknown plot style");
        }
    }

private:
    // Give plot command to gnuplot
    void executeGnuplot(const std::string& plotCommand) const
    {
        fputs((plotCommand + "\n").c_str(), pipe_);
        fflush(pipe_);
    }

    // Check validity of number
    void checkNumber(Scalar number, std::string text = "") const
    {
        if (std::isnan(number))
            std::cout << "warning: " << text << " is not a number, adjust your data range" << std::endl;
        if (std::isinf(number))
            std::cout << "warning: " << text << " is infinity, adjust your data range" << std::endl;
    }

    // Convert number to string
    template<class T> std::string numberToString(const T number) const
    {
        std::ostringstream stream;
        stream << number;
        return stream.str();
    }

    std::string plotStyle_;
    std::FILE * pipe_;
    std::vector<StringVector> fileName_;
    std::vector<StringVector> plotName_;
    std::vector<Scalar> xRangeMin_;
    std::vector<Scalar> xRangeMax_;
    std::vector<Scalar> yRangeMin_;
    std::vector<Scalar> yRangeMax_;
    StringVector xLabel_;
    StringVector yLabel_;
    std::string gnuplotPath_;
    std::string terminalType_;
    const unsigned int numberOfGnuplotWindows_;
};
} // end of namespace
#endif // DUMUX_GNUPLOT_INTERFACE_HH