OpenLB 1.7
Loading...
Searching...
No Matches
gnuplotWriter.hh
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2016 Fabian Klemens
4 * E-mail contact: info@openlb.net
5 * The most recent release of OpenLB can be downloaded at
6 * <http://www.openlb.net/>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22*/
23
24#ifndef GNUPLOT_WRITER_HH
25#define GNUPLOT_WRITER_HH
26
27#include <fstream>
28#include <iostream>
29#include <unistd.h>
30#include "core/singleton.h"
31#include "io/fileName.h"
32#include "gnuplotWriter.h"
34
35namespace olb {
38template< typename T >
39Gnuplot<T>::Gnuplot(std::string name, bool liveplot, std::string preCommand, AxisType axisType, Regression regressionType)
40 : _name(name),
41 _liveplot(liveplot),
42 _dataFile(singleton::directories().getGnuplotOutDir()+"data/"+_name+".dat"),
43 _dir(singleton::directories().getGnuplotOutDir()),
44 _preCommand(preCommand),
45 _axisType(axisType),
46 _regressionType(regressionType),
47 csvWriter(name)
49#ifndef WIN32
50 _gnuplotInstalled = (! system("which gnuplot >/dev/null 2>/dev/null"));
51#endif
52 if ((! _gnuplotInstalled) && (singleton::mpi().getRank() == _rank )) {
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;
55 }
56}
60template <typename T>
61Gnuplot<T>::Gnuplot(std::string name) : Gnuplot(name, false, "", LINEAR, OFF) {}
62
63template< typename T >
64Gnuplot<T>::Gnuplot(std::string name, bool liveplot) : Gnuplot(name, liveplot,"", LINEAR, OFF) {}
66template <typename T>
67Gnuplot<T>::Gnuplot(std::string name, AxisType axisType) : Gnuplot(name, false, "", axisType, OFF) {}
69template< typename T >
70Gnuplot<T>::Gnuplot(std::string name, AxisType axisType, Regression regressionType) : Gnuplot(name, false,"", axisType, regressionType) {}
72
75template< typename T >
76void Gnuplot<T>::setData(T xValue, T yValue, std::string name, std::string key, char plotType)
77{
78 setData(xValue, std::vector<T>{yValue}, std::vector<std::string>{name}, key, std::vector<char>{plotType});
80
82template< typename T >
83void Gnuplot<T>::setData(bool noXvalue, T yValue, std::string name, std::string key, char plotType)
84{
85 T xValue = _time;
86 setData(xValue, yValue, name, key, std::vector<char>{plotType});
87 _time++;
88}
89
94template< typename T >
95void Gnuplot<T>::setData(T xValue, std::vector<T> yValues, std::vector<std::string> names, std::string key, std::vector<char> plotType)
96{
97 if (_init) {
98 _dataSize = yValues.size();
99 _key = key;
100 _names = names;
101 _plotTypes = plotType;
102 if (_names.size() < _dataSize) {
103 for (unsigned int i = _names.size(); i < _dataSize; i++) {
104 _names.push_back("");
105 }
106 }
107 if (_plotTypes.size() < _dataSize) {
108 for (unsigned int i = _plotTypes.size(); i < _dataSize; i++) {
109 _plotTypes.push_back('l');
110 }
111 }
112 if (_gnuplotInstalled && _liveplot) {
113 writePlotFile("plot");
114 }
115 }
116 csvWriter.writeDataFile(xValue,yValues);
118 if (_gnuplotInstalled && _liveplot && _init) {
119 startGnuplot("plot");
121
122 _init = false;
123 return;
124}
125
128template<typename T>
129void Gnuplot<T>::linRegression(std::ofstream& fout, std::string xAxisType, std::string yAxisType)
130{
131 fout << "set fit quiet\n";
132
133 for (unsigned int i = 0; i < _dataSize; ++i) {
134 fout << "f" << i+1 << "(x) = m" << i+1 << " * x + b"<< i+1 << "\n"; //Match f2 to the dataset 1:3 with f2(x) = m2 * x + b2 and so on
135 fout << "fit f" << i+1 << "(x) path using ("<< xAxisType << "$1)):(" << yAxisType << "$" << i+2 << ")) via m" << i+1 << ", b" << i+1 <<"\n";
136 }
137 fout << "\n";
138
141 fout << "plot ";
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,";
144 }
145
146 return;
147}
148
152template<typename T>
153void Gnuplot<T>::scaleAxes(std::ofstream& fout)
154{
155 switch (_axisType)
156 {
157 case (LOGLOG):
158 case(LOGLOGINVERTED):
159 {
160 fout << "set xtics nomirror" << "\n";
161 fout << "set ytics nomirror" << "\n";
162
163 fout << "set format y '10^{%.2f}'" << "\n";
164 fout << "set format x '10^{%.2f}'" << "\n";
165 } break;
166
167 case LINEAR:
168 default:
169 {} break;
170 }
171
172 fout << "\n";
173 return;
174}
175
176
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)
180{
181 T xValue = _time;
182 setData(xValue, yValues, names, key, plotType);
183 _time++;
184}
185
186
188template< typename T >
189void Gnuplot<T>::writePDF(std::string plotName)
190{
191 if (_gnuplotInstalled && (!_init)) {
192 writePlotFile("pdf", plotName);
193 startGnuplot("plotPDF", plotName);
194 }
195 return;
196}
197
198
205template< typename T >
206void Gnuplot<T>::writePNG(int iT, double xRange, std::string plotName)
207{
208 if (_gnuplotInstalled && (!_init)) {
209 _iT = iT;
210 _xRange = xRange;
211
213 writePlotFile("png", plotName);
214 startGnuplot("plotPNG", plotName);
215 }
216 return;
217}
218
222template< typename T >
223void Gnuplot<T>::writePlotFile(std::string type, std::string plotName)
224{
225 if (singleton::mpi().getRank() == _rank ) {
226 std::ofstream fout;
227
228 std::string plotFile;
229 if (_liveplot && type == "plot") {
230 plotFile = singleton::directories().getGnuplotOutDir()+"data/plot.p";
231 }
232 else if (type == "pdf") {
233 plotFile = singleton::directories().getGnuplotOutDir()+"data/plotPDF"+plotName+".p";
234 }
235 else if (type == "png") {
236 plotFile = singleton::directories().getGnuplotOutDir()+"data/plotPNG"+plotName+".p";
237 }
238 else {
239 std::cout << "WARNING: invalid Gnuplot type={'', 'plot'; 'pdf', 'png'}" << std::endl;
240 exit(-1);
241 }
242
243 fout.open(plotFile.c_str(), std::ios::trunc);
244 fout << "set key " << _key << "\n";
245
246 if (type=="pdf") {
247 fout << "set terminal pdf enhanced" << "\n"
248 << "set output '"<<_dir<<_name<<".pdf'" << "\n";
249 }
250 if (type=="png") {
251 if ( !util::nearZero(_xRange+1) ) {
252 fout << "set xr[0:"<< _xRange <<"]" << "\n";
253 }
254 fout << "set terminal png" << "\n"
255 << "set output '"<<_dir<<_name;
256 if (_iT != -1) {
257 fout <<"_"<<_iT;
258 }
259 fout <<".png'" << "\n";
260 }
261
263 fout << _preCommand << "\n";
264
265
267 scaleAxes(fout);
268
270
271 fout << "set xlabel '" << _xLabel << "'" << "\n";
272 fout << "set ylabel '" << _yLabel << "'" << "\n";
273
274
275
277 std::string xAxisType;
278 std::string yAxisType;
279
280 switch (_axisType)
281 {
282 case LOGLOG:
283 {
284 xAxisType = "log10(";
285 yAxisType = "log10(";
286 } break;
287
288 case LOGLOGINVERTED:
289 {
290 xAxisType = "log10(1/";
291 yAxisType = "log10(";
292 } break;
293
294 case LINEAR:
295 default:
296 {
297 xAxisType = "(";
298 yAxisType = "(";
299 }
300 }
301
302 fout << "path = '" << _dir << "data/"<<_name << ".dat'" << "\n";
303
304
305
309
312 switch (_regressionType)
313 {
314 case LINREG:
315 {
316 linRegression(fout, xAxisType, yAxisType);
317 } break;
318
319 case OFF:
320 default:
321 {
322 fout << "plot ";
323 } break;
324
325 }
328
330
331 unsigned int i = 0;
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,";
334 }
335 fout << "path u (" << xAxisType << "$1)):(" << yAxisType << "$" << i+2 << ")) w " << _plotTypes[i] << " t '" << _names[i] << "' lc " << i+1 << " axis x1y1";
336
337 fout << "\n";
338 if (_liveplot && type=="plot") {
339 fout << "pause -1" << "\n"
340 << "reread" << "\n";
341 }
342 fout.close();
343
344
345 }
346 return;
347}
348
349
351template< typename T >
352void Gnuplot<T>::setLabel(std::string xLabel, std::string yLabel)
353{
354 _xLabel = xLabel;
355 _yLabel = yLabel;
356}
357
358
362template< typename T >
363void Gnuplot<T>::startGnuplot(std::string plotFile, std::string plotName)
364{
365#ifndef WIN32
366 if (singleton::mpi().getRank() == _rank) {
367 if (!system(nullptr)) {
368 exit (EXIT_FAILURE);
369 }
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;
373 }
374 }
375 return;
376#endif
377}
378} // namespace olb
379
380#endif
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
Definition singleton.h:101
int getRank() const
Returns the process ID.
These functions help you to create file names.
void exit(int exitcode)
Definition singleton.h:165
MpiManager & mpi()
Directories & directories()
Definition singleton.h:150
bool nearZero(const ADf< T, DIM > &a)
Definition aDiff.h:1087
Top level namespace for all of OpenLB.
Definition of singletons: global, publicly available information.