28#ifndef DUMUX_GNUPLOT_INTERFACE_HH
29#define DUMUX_GNUPLOT_INTERFACE_HH
33#define GNUPLOT_EXECUTABLE "/usr/bin/gnuplot"
45#include <dune/common/exceptions.hh>
46#include <dune/common/stdstreams.hh>
61 { function, file, data };
66 pipeInteractive_(0), pipeImage_(0),
67 openPlotWindow_(true), persist_(persist), createImage_(true),
68 terminalType_(
"x11"), outputDirectory_(
"./"),
69 datafileSeparator_(
' '), linetype_(
"solid"),
70 xRangeIsSet_(false), yRangeIsSet_(false),
71 xLabel_(
""), yLabel_(
""),
89 void plot(
const std::string &filename =
"")
92 std::string
plot =
"set datafile separator \'" + std::string(1, datafileSeparator_) +
"\'\n";
95 plot +=
"set xlabel \"" + xLabel_ +
"\"\n";
96 plot +=
"set ylabel \"" + yLabel_ +
"\"\n";
98 plot +=
"set xrange [" + toStringWithPrecision(xRangeMin_) +
":" + toStringWithPrecision(xRangeMax_) +
"]" +
"\n";
100 plot +=
"set yrange [" + toStringWithPrecision(yRangeMin_) +
":" + toStringWithPrecision(yRangeMax_) +
"]" +
"\n";
103 plot += plotOptions_ +
"\n";
107 std::string plotCommandForFile(
plot);
108 for (
unsigned int i = 0; i < curve_.size(); ++i)
112 plot += +
" " + curve_[i] +
" " + curveOptions_[i];
113 plotCommandForFile += +
" " + curve_[i] +
" " + curveOptions_[i];
117 plot += +
" '" + outputDirectory_ + curve_[i] +
"' " + curveOptions_[i];
118 plotCommandForFile += +
" '" + curve_[i] +
"' " + curveOptions_[i];
121 if (i < curve_.size()-1)
124 plotCommandForFile +=
",\\";
127 plotCommandForFile +=
"\n";
131 std::string interactivePlot =
"reset\n";
134 if (terminalType_.compare(
"x11") != 0 || linetype_.compare(
"solid") != 0)
135 interactivePlot +=
"set term " + terminalType_ +
" " + linetype_ +
" " +
" \n";
138 interactivePlot +=
plot;
140 executeGnuplot(interactivePlot, pipeInteractive_);
143 if (filename.compare(
"") != 0)
145 std::string filePlot =
"reset\n";
146 filePlot +=
"set term pngcairo size 800,600 " + linetype_ +
" \n";
147 filePlot +=
"set output \"" + filename +
".png\"\n";
149 std::string gnuplotFileName = outputDirectory_ + filename +
".gp";
151 file.open(gnuplotFileName);
157 executeGnuplot(filePlot, pipeImage_);
177 curveOptions_.clear();
184 void open(
const bool persist =
true)
187 pipeInteractive_ = popen((gnuplotPath_ +
" -persist").c_str(),
"w");
189 pipeInteractive_ = popen((gnuplotPath_).c_str(),
"w");
192 pipeImage_ = popen((gnuplotPath_).c_str(),
"w");
200 if (pclose(pipeInteractive_) == -1 || pclose(pipeImage_) == -1)
201 assert(
"Could not close pipe to Gnuplot!");
211 const std::string& options =
"with lines")
213 curve_.push_back(function);
214 curveOptions_.push_back(options);
225 const std::string& options =
"with lines")
227 curve_.push_back(fileName);
228 curveOptions_.push_back(options);
243 const std::vector<Scalar>& y,
244 const std::string& fileName,
245 const std::string& options =
"with lines")
247 if (x.empty() || y.empty())
248 DUNE_THROW(Dune::InvalidStateException,
"Data vectors have to contain data!");
250 if (x.size() > y.size())
251 DUNE_THROW(Dune::InvalidStateException,
"Non-matching data field sizes!");
253 if (x.size() != y.size())
254 std::cout <<
"GnuplotInterface warning: Added data fields of different size! "
255 <<
"Only plotting the first " << x.size() <<
" elements.\n";
259 file.open(outputDirectory_ + fileName);
260 for (
unsigned int i = 0; i < x.size(); i++)
262 checkNumber(x[i],
"x[i] i=" + std::to_string(i) +
" in " + fileName);
263 checkNumber(y[i],
"y[i] i=" + std::to_string(i) +
" in " + fileName);
264 file << x[i] << datafileSeparator_ << y[i] << std::endl;
269 curve_.push_back(fileName);
270 curveOptions_.push_back(options);
327 plotOptions_ += option +
"\n";
337 openPlotWindow_ = openPlotWindow;
347 createImage_ = createImage;
357 datafileSeparator_ = separator;
367 terminalType_ = terminal;
377 outputDirectory_ = outputDirectory +
"/";
387 linetype_ = dashed ?
"dashed" :
"solid";
392 void executeGnuplot(
const std::string& plotCommand, std::FILE * pipe)
const
395 fputs((plotCommand +
"\n").c_str(), pipe);
398 std::cerr <<
"Warning: Gnuplot has not been found by CMake, no image generation or interactive display possible." << std::endl;
399 std::cerr <<
"Note: The data and the gnuplot instruction file will still be created." << std::endl;
404 void checkNumber(Scalar number,
const std::string& text =
"")
const
409 Dune::dwarn <<
"warning: " << text <<
" is not a number, adjust your data range" << std::endl;
411 Dune::dwarn <<
"warning: " << text <<
" is infinity, adjust your data range" << std::endl;
415 template <
typename T>
416 std::string toStringWithPrecision(
const T value,
const int n = 8)
418 std::ostringstream out;
419 out << std::setprecision(n) << value;
423 std::FILE * pipeInteractive_;
424 std::FILE * pipeImage_;
425 bool openPlotWindow_;
428 std::string terminalType_;
429 std::string outputDirectory_;
430 char datafileSeparator_;
431 std::string linetype_;
443 std::string plotOptions_;
444 std::string gnuplotPath_;
#define GNUPLOT_EXECUTABLE
Definition: gnuplotinterface.hh:33
Adaption of the non-isothermal two-phase two-component flow model to problems with CO2.
Definition: adapt.hh:29
Interface for passing data sets to a file and plotting them, if gnuplot is installed.
Definition: gnuplotinterface.hh:57
void setXlabel(const std::string &label)
Sets the label for the x-axis.
Definition: gnuplotinterface.hh:279
std::vector< CurveType > CurveTypeVector
Definition: gnuplotinterface.hh:62
void resetPlot()
Deletes all plots from a plotting window and resets user-defined options.
Definition: gnuplotinterface.hh:174
void addFileToPlot(const std::string &fileName, const std::string &options="with lines")
Adds a file to list of plots.
Definition: gnuplotinterface.hh:224
void addDataSetToPlot(const std::vector< Scalar > &x, const std::vector< Scalar > &y, const std::string &fileName, const std::string &options="with lines")
Adds a data set and writes a data file.
Definition: gnuplotinterface.hh:242
void plot(const std::string &filename="")
Plots the files for a specific window number, writes a gnuplot and png file.
Definition: gnuplotinterface.hh:89
void resetAll(const bool persist=true)
Resets all gnuplot parameters.
Definition: gnuplotinterface.hh:164
~GnuplotInterface()
The destructor.
Definition: gnuplotinterface.hh:79
void setTerminalType(std::string terminal)
Sets the terminal used for interactive output.
Definition: gnuplotinterface.hh:365
void setOption(const std::string &option)
Sets additional user-defined options.
Definition: gnuplotinterface.hh:325
void setOpenPlotWindow(bool openPlotWindow)
Define whether the gnuplot window should be opened.
Definition: gnuplotinterface.hh:335
void setCreateImage(bool createImage)
Define whether gnuplot should create .png files.
Definition: gnuplotinterface.hh:345
void addFunctionToPlot(const std::string &function, const std::string &options="with lines")
Adds a function to list of plots.
Definition: gnuplotinterface.hh:210
void setXRange(Scalar min, Scalar max)
Sets the range for the x-axis.
Definition: gnuplotinterface.hh:300
GnuplotInterface(bool persist=true)
The constructor.
Definition: gnuplotinterface.hh:65
void close()
Closes gnuplot.
Definition: gnuplotinterface.hh:198
void setDatafileSeparator(char separator)
Sets the datafile separator.
Definition: gnuplotinterface.hh:355
void setOutputDirectory(const std::string &outputDirectory)
Sets the output directory for data and gnuplot files.
Definition: gnuplotinterface.hh:375
void useDashedLines(bool dashed)
Use dashed (true) or solid (false) lines.
Definition: gnuplotinterface.hh:385
void open(const bool persist=true)
Opens gnuplot.
Definition: gnuplotinterface.hh:184
std::vector< std::string > StringVector
Definition: gnuplotinterface.hh:59
void setYRange(Scalar min, Scalar max)
Sets the range for the y-axis.
Definition: gnuplotinterface.hh:313
void setYlabel(const std::string &label)
Sets the label for the y-axis.
Definition: gnuplotinterface.hh:289
CurveType
Definition: gnuplotinterface.hh:61