16#ifndef DUMUX_GNUPLOT_INTERFACE_HH
17#define DUMUX_GNUPLOT_INTERFACE_HH
19#if !DUMUX_HAVE_GNUPLOT
21#define GNUPLOT_EXECUTABLE "/usr/bin/gnuplot"
33#include <dune/common/exceptions.hh>
34#include <dune/common/stdstreams.hh>
49 { function, file, data };
54 pipeInteractive_(0), pipeImage_(0),
55 openPlotWindow_(true), persist_(persist), createImage_(true),
56 terminalType_(
"x11"), outputDirectory_(
"./"),
57 datafileSeparator_(
' '), linetype_(
"solid"),
58 xRangeIsSet_(false), yRangeIsSet_(false),
59 xLabel_(
""), yLabel_(
""),
77 void plot(
const std::string &filename =
"")
80 std::string
plot =
"set datafile separator \'" + std::string(1, datafileSeparator_) +
"\'\n";
83 plot +=
"set xlabel \"" + xLabel_ +
"\"\n";
84 plot +=
"set ylabel \"" + yLabel_ +
"\"\n";
86 plot +=
"set xrange [" + toStringWithPrecision(xRangeMin_) +
":" + toStringWithPrecision(xRangeMax_) +
"]" +
"\n";
88 plot +=
"set yrange [" + toStringWithPrecision(yRangeMin_) +
":" + toStringWithPrecision(yRangeMax_) +
"]" +
"\n";
91 plot += plotOptions_ +
"\n";
95 std::string plotCommandForFile(
plot);
96 for (
unsigned int i = 0; i < curve_.size(); ++i)
100 plot += +
" " + curve_[i] +
" " + curveOptions_[i];
101 plotCommandForFile += +
" " + curve_[i] +
" " + curveOptions_[i];
105 plot += +
" '" + outputDirectory_ + curve_[i] +
"' " + curveOptions_[i];
106 plotCommandForFile += +
" '" + curve_[i] +
"' " + curveOptions_[i];
109 if (i < curve_.size()-1)
112 plotCommandForFile +=
",\\";
115 plotCommandForFile +=
"\n";
119 std::string interactivePlot =
"reset\n";
122 if (terminalType_.compare(
"x11") != 0 || linetype_.compare(
"solid") != 0)
123 interactivePlot +=
"set term " + terminalType_ +
" " + linetype_ +
" " +
" \n";
126 interactivePlot +=
plot;
128 executeGnuplot(interactivePlot, pipeInteractive_);
131 if (filename.compare(
"") != 0)
133 std::string filePlot =
"reset\n";
134 filePlot +=
"set term pngcairo size 800,600 " + linetype_ +
" \n";
135 filePlot +=
"set output \"" + filename +
".png\"\n";
137 std::string gnuplotFileName = outputDirectory_ + filename +
".gp";
139 file.open(gnuplotFileName);
145 executeGnuplot(filePlot, pipeImage_);
165 curveOptions_.clear();
172 void open(
const bool persist =
true)
175 pipeInteractive_ = popen((gnuplotPath_ +
" -persist").c_str(),
"w");
177 pipeInteractive_ = popen((gnuplotPath_).c_str(),
"w");
180 pipeImage_ = popen((gnuplotPath_).c_str(),
"w");
188 if (pclose(pipeInteractive_) == -1 || pclose(pipeImage_) == -1)
189 assert(
"Could not close pipe to Gnuplot!");
199 const std::string& options =
"with lines")
201 curve_.push_back(function);
202 curveOptions_.push_back(options);
213 const std::string& options =
"with lines")
215 curve_.push_back(fileName);
216 curveOptions_.push_back(options);
230 template<
class DataX,
class DataY>
232 const std::string& fileName,
233 const std::string& options =
"with lines")
235 if (x.size() == 0 || y.size() == 0)
236 DUNE_THROW(Dune::InvalidStateException,
"Data vectors have to contain data!");
238 if (x.size() > y.size())
239 DUNE_THROW(Dune::InvalidStateException,
"Non-matching data field sizes!");
241 if (x.size() != y.size())
242 std::cout <<
"GnuplotInterface warning: Added data fields of different size! "
243 <<
"Only plotting the first " << x.size() <<
" elements.\n";
247 file.open(outputDirectory_ + fileName);
248 for (
unsigned int i = 0; i < x.size(); i++)
250 checkNumber(x[i],
"x[i] i=" + std::to_string(i) +
" in " + fileName);
251 checkNumber(y[i],
"y[i] i=" + std::to_string(i) +
" in " + fileName);
252 file << x[i] << datafileSeparator_ << y[i] << std::endl;
257 curve_.push_back(fileName);
258 curveOptions_.push_back(options);
315 plotOptions_ += option +
"\n";
325 openPlotWindow_ = openPlotWindow;
335 createImage_ = createImage;
345 datafileSeparator_ = separator;
355 terminalType_ = terminal;
365 outputDirectory_ = outputDirectory +
"/";
375 linetype_ = dashed ?
"dashed" :
"solid";
380 void executeGnuplot(
const std::string& plotCommand, std::FILE * pipe)
const
382#ifdef DUMUX_HAVE_GNUPLOT
383 fputs((plotCommand +
"\n").c_str(), pipe);
386 std::cerr <<
"Warning: Gnuplot has not been found by CMake, no image generation or interactive display possible." << std::endl;
387 std::cerr <<
"Note: The data and the gnuplot instruction file will still be created." << std::endl;
392 void checkNumber(Scalar number,
const std::string& text =
"")
const
397 Dune::dwarn <<
"warning: " << text <<
" is not a number, adjust your data range" << std::endl;
399 Dune::dwarn <<
"warning: " << text <<
" is infinity, adjust your data range" << std::endl;
403 template <
typename T>
404 std::string toStringWithPrecision(
const T value,
const int n = 8)
406 std::ostringstream out;
407 out << std::setprecision(n) << value;
411 std::FILE * pipeInteractive_;
412 std::FILE * pipeImage_;
413 bool openPlotWindow_;
416 std::string terminalType_;
417 std::string outputDirectory_;
418 char datafileSeparator_;
419 std::string linetype_;
431 std::string plotOptions_;
432 std::string gnuplotPath_;
Interface for passing data sets to a file and plotting them, if gnuplot is installed.
Definition: gnuplotinterface.hh:45
void setXlabel(const std::string &label)
Sets the label for the x-axis.
Definition: gnuplotinterface.hh:267
std::vector< CurveType > CurveTypeVector
Definition: gnuplotinterface.hh:50
void resetPlot()
Deletes all plots from a plotting window and resets user-defined options.
Definition: gnuplotinterface.hh:162
void addFileToPlot(const std::string &fileName, const std::string &options="with lines")
Adds a file to list of plots.
Definition: gnuplotinterface.hh:212
void addDataSetToPlot(const DataX &x, const DataY &y, const std::string &fileName, const std::string &options="with lines")
Adds a data set and writes a data file.
Definition: gnuplotinterface.hh:231
void plot(const std::string &filename="")
Plots the files for a specific window number, writes a gnuplot and png file.
Definition: gnuplotinterface.hh:77
void resetAll(const bool persist=true)
Resets all gnuplot parameters.
Definition: gnuplotinterface.hh:152
~GnuplotInterface()
The destructor.
Definition: gnuplotinterface.hh:67
void setTerminalType(std::string terminal)
Sets the terminal used for interactive output.
Definition: gnuplotinterface.hh:353
void setOption(const std::string &option)
Sets additional user-defined options.
Definition: gnuplotinterface.hh:313
void setOpenPlotWindow(bool openPlotWindow)
Define whether the gnuplot window should be opened.
Definition: gnuplotinterface.hh:323
void setCreateImage(bool createImage)
Define whether gnuplot should create .png files.
Definition: gnuplotinterface.hh:333
void addFunctionToPlot(const std::string &function, const std::string &options="with lines")
Adds a function to list of plots.
Definition: gnuplotinterface.hh:198
void setXRange(Scalar min, Scalar max)
Sets the range for the x-axis.
Definition: gnuplotinterface.hh:288
GnuplotInterface(bool persist=true)
The constructor.
Definition: gnuplotinterface.hh:53
void close()
Closes gnuplot.
Definition: gnuplotinterface.hh:186
void setDatafileSeparator(char separator)
Sets the datafile separator.
Definition: gnuplotinterface.hh:343
void setOutputDirectory(const std::string &outputDirectory)
Sets the output directory for data and gnuplot files.
Definition: gnuplotinterface.hh:363
void useDashedLines(bool dashed)
Use dashed (true) or solid (false) lines.
Definition: gnuplotinterface.hh:373
void open(const bool persist=true)
Opens gnuplot.
Definition: gnuplotinterface.hh:172
std::vector< std::string > StringVector
Definition: gnuplotinterface.hh:47
void setYRange(Scalar min, Scalar max)
Sets the range for the y-axis.
Definition: gnuplotinterface.hh:301
void setYlabel(const std::string &label)
Sets the label for the y-axis.
Definition: gnuplotinterface.hh:277
CurveType
Definition: gnuplotinterface.hh:49
#define GNUPLOT_EXECUTABLE
Definition: gnuplotinterface.hh:21