24#ifndef GNUPLOT_WRITER_HH
25#define GNUPLOT_WRITER_HH
42 _dataFile(singleton::directories().getGnuplotOutDir()+
"data/"+_name+
".dat"),
43 _dir(singleton::directories().getGnuplotOutDir()),
44 _preCommand(preCommand),
46 _regressionType(regressionType),
50 _gnuplotInstalled = (! system(
"which gnuplot >/dev/null 2>/dev/null"));
53 std::cout <<
"We could not find a gnuplot distribution at your system." << std::endl;
54 std::cout <<
"We still write the data files s.t. you can plot the data yourself." << std::endl;
78 setData(xValue, std::vector<T>{yValue}, std::vector<std::string>{name}, key, std::vector<char>{plotType});
86 setData(xValue, yValue, name, key, std::vector<char>{plotType});
95void Gnuplot<T>::setData(T xValue, std::vector<T> yValues, std::vector<std::string> names, std::string key, std::vector<char> plotType)
98 _dataSize = yValues.size();
101 _plotTypes = plotType;
102 if (_names.size() < _dataSize) {
103 for (
unsigned int i = _names.size(); i < _dataSize; i++) {
104 _names.push_back(
"");
107 if (_plotTypes.size() < _dataSize) {
108 for (
unsigned int i = _plotTypes.size(); i < _dataSize; i++) {
109 _plotTypes.push_back(
'l');
112 if (_gnuplotInstalled && _liveplot) {
113 writePlotFile(
"plot");
116 csvWriter.writeDataFile(xValue,yValues);
118 if (_gnuplotInstalled && _liveplot && _init) {
119 startGnuplot(
"plot");
131 fout <<
"set fit quiet\n";
133 for (
unsigned int i = 0; i < _dataSize; ++i) {
134 fout <<
"f" << i+1 <<
"(x) = m" << i+1 <<
" * x + b"<< i+1 <<
"\n";
135 fout <<
"fit f" << i+1 <<
"(x) path using ("<< xAxisType <<
"$1)):(" << yAxisType <<
"$" << i+2 <<
")) via m" << i+1 <<
", b" << i+1 <<
"\n";
142 for (
unsigned int i = 0; i < _dataSize; ++i) {
143 fout <<
"f"<< i+1 <<
"(x) title sprintf('regr " << _names[i] <<
", gradient = %.3f', m" << i+1 <<
") lc " << i+1 <<
" axis x1y1,";
158 case(LOGLOGINVERTED):
160 fout <<
"set xtics nomirror" <<
"\n";
161 fout <<
"set ytics nomirror" <<
"\n";
163 fout <<
"set format y '10^{%.2f}'" <<
"\n";
164 fout <<
"set format x '10^{%.2f}'" <<
"\n";
178template<
typename T >
179void Gnuplot<T>::setData(
bool noXvalue, std::vector<T> yValues, std::vector<std::string> names, std::string key, std::vector<char> plotType)
182 setData(xValue, yValues, names, key, plotType);
188template<
typename T >
191 if (_gnuplotInstalled && (!_init)) {
192 writePlotFile(
"pdf", plotName);
193 startGnuplot(
"plotPDF", plotName);
205template<
typename T >
208 if (_gnuplotInstalled && (!_init)) {
213 writePlotFile(
"png", plotName);
214 startGnuplot(
"plotPNG", plotName);
222template<
typename T >
228 std::string plotFile;
229 if (_liveplot && type ==
"plot") {
232 else if (type ==
"pdf") {
235 else if (type ==
"png") {
239 std::cout <<
"WARNING: invalid Gnuplot type={'', 'plot'; 'pdf', 'png'}" << std::endl;
243 fout.open(plotFile.c_str(), std::ios::trunc);
244 fout <<
"set key " << _key <<
"\n";
247 fout <<
"set terminal pdf enhanced" <<
"\n"
248 <<
"set output '"<<_dir<<_name<<
".pdf'" <<
"\n";
252 fout <<
"set xr[0:"<< _xRange <<
"]" <<
"\n";
254 fout <<
"set terminal png" <<
"\n"
255 <<
"set output '"<<_dir<<_name;
259 fout <<
".png'" <<
"\n";
263 fout << _preCommand <<
"\n";
271 fout <<
"set xlabel '" << _xLabel <<
"'" <<
"\n";
272 fout <<
"set ylabel '" << _yLabel <<
"'" <<
"\n";
277 std::string xAxisType;
278 std::string yAxisType;
284 xAxisType =
"log10(";
285 yAxisType =
"log10(";
290 xAxisType =
"log10(1/";
291 yAxisType =
"log10(";
302 fout <<
"path = '" << _dir <<
"data/"<<_name <<
".dat'" <<
"\n";
312 switch (_regressionType)
316 linRegression(fout, xAxisType, yAxisType);
332 for ( ; i < _dataSize - 1; ++i) {
333 fout <<
"path u (" << xAxisType <<
"$1)):(" << yAxisType <<
"$" << i+2 <<
")) w " << _plotTypes[i] <<
" t '" << _names[i] <<
"' lc " << i+1 <<
" axis x1y1,";
335 fout <<
"path u (" << xAxisType <<
"$1)):(" << yAxisType <<
"$" << i+2 <<
")) w " << _plotTypes[i] <<
" t '" << _names[i] <<
"' lc " << i+1 <<
" axis x1y1";
338 if (_liveplot && type==
"plot") {
339 fout <<
"pause -1" <<
"\n"
351template<
typename T >
362template<
typename T >
367 if (!system(
nullptr)) {
370 const std::string command =
"gnuplot -persistent "+_dir+
"data/"+plotFile+plotName+
".p > /dev/null &";
371 if ( system(command.c_str()) ) {
372 std::cout <<
"Error at GnuplotWriter" << std::endl;
void startGnuplot(std::string plotFile, std::string plotName="")
system command to start gnuplot (LINUX ONLY!)
void setData(T xValue, T yValue, std::string name="", std::string key="", char plotType='l')
sets the data and plot file for two doubles (x and y) the plotType indicates whether the user want to...
void scaleAxes(std::ofstream &fout)
scales the axes if needed
Gnuplot(std::string name, bool liveplot, std::string preCommand, AxisType axisType, Regression regressionType)
type of Regression, off, LINear REGression, exponential Regression?,...
void writePNG(int iT=-1, double xRange=-1, std::string plotName="")
writes PNGs usage: first argument: numbering of png file (optional), second argument: range for the x...
Regression
different types of data usage and axes scaling
void linRegression(std::ofstream &fout, std::string x_axisType, std::string y_axisType)
creates the lin regression to the data
void writePDF(std::string plotName="")
writes an PDF
void setLabel(std::string xLabel="", std::string yLabel="")
set labels of the plot: xLabel and yLabel
std::string getGnuplotOutDir() const
int getRank() const
Returns the process ID.
These functions help you to create file names.
Directories & directories()
bool nearZero(const ADf< T, DIM > &a)
Top level namespace for all of OpenLB.
Definition of singletons: global, publicly available information.