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
30
31
32
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
|
/* This file is part of the OpenLB library
*
* Copyright (C) 2016 Fabian Klemens
* E-mail contact: info@openlb.net
* The most recent release of OpenLB can be downloaded at
* <http://www.openlb.net/>
*
* 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, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef GNUPLOT_WRITER_HH
#define GNUPLOT_WRITER_HH
#include <fstream>
#include <iostream>
#include <unistd.h>
#include "core/singleton.h"
#include "io/fileName.h"
#include "gnuplotWriter.h"
#include "utilities/vectorHelpers.h"
namespace olb {
/// Constructor with name of outputFiles
/// boolean true for real-time plotting //WARNING: experimental!
template< typename T >
Gnuplot<T>::Gnuplot(std::string name, bool liveplot)
: _name(name),
_dataFile(singleton::directories().getGnuplotOutDir()+"data/"+_name+".dat"),
_dir(singleton::directories().getGnuplotOutDir())
{
_liveplot = liveplot;
if (singleton::mpi().getRank() == _rank) {
std::ofstream fout;
///add (new) data file
fout.open(_dataFile.c_str(), std::ios::trunc);
fout.close();
}
}
/// writes the data and plot file for two doubles (x and y)
/// plotType indicates whether you want a linegraph 'l' (default) or a scatterplot 'p' (default: 'l')
template< typename T >
void Gnuplot<T>::setData(T xValue, T yValue, std::string name, std::string key, char plotType)
{
if (_init) {
_dataSize = 1;
_key = key;
_names = {name};
_plotTypes = {plotType};
if (_liveplot) {
writePlotFile("plot");
}
}
writeDataFile(xValue, yValue);
if (_liveplot && _init) {
startGnuplot("plot");
}
_init = false;
return;
}
/// writes the data and plot file for two doubles (x and y), where x is increasing integer
template< typename T >
void Gnuplot<T>::setData(bool noXvalue, T yValue, std::string name, std::string key, char plotType)
{
T xValue = _time;
setData(xValue, yValue, name, key, {plotType});
_time++;
}
/// writes the data and plot file for a double and a vector of doubles (x and y1,y2,...)
/// plotType indicates whether you want a linegraph 'l' (default) or a scatterplot 'p': (default: {'l','l'})
/// The position in the vector 'plotType'{'l', 'p'} is linked to the rank of the y-axis (y1, y2) :
/// y1 is plotted in form of a line plot & y2 is plotted in form of a scatterplot
template< typename T >
void Gnuplot<T>::setData(T xValue, std::vector<T> yValues, std::vector<std::string> names, std::string key, std::vector<char> plotType)
{
if (_init) {
_dataSize = yValues.size();
_key = key;
_names = names;
_plotTypes = plotType;
if (_names.size() < _dataSize) {
for (unsigned int i = _names.size(); i < _dataSize; i++) {
_names.push_back("");
}
}
if (_plotTypes.size() < _dataSize) {
for (unsigned int i = _plotTypes.size(); i < _dataSize; i++) {
_plotTypes.push_back('l');
}
}
if (_liveplot) {
writePlotFile("plot");
}
}
writeDataFile(xValue,yValues);
if (_liveplot && _init) {
startGnuplot("plot");
}
_init = false;
return;
}
/// writes the data and plot file for a double and a vector of doubles (x and y1,y2,...), where x is increasing integer
template< typename T >
void Gnuplot<T>::setData(bool noXvalue, std::vector<T> yValues, std::vector<std::string> names, std::string key, std::vector<char> plotType)
{
T xValue = _time;
setData(xValue, yValues, names, key, plotType);
_time++;
}
/// writes an PDF
template< typename T >
void Gnuplot<T>::writePDF()
{
if (!_init) {
writePlotFile("pdf");
startGnuplot("plotPDF");
}
return;
}
/// writes PNGs
/// usage: first argument: numbering of png file
/// second argument: range for the x axis
/// thrid argument: specifies the name of the plot in case the user wants to
/// create more than one plot with the simulation results (default: plotName = "")
/// no arguments: writes consecutive numbers with adaptive xrange
template< typename T >
void Gnuplot<T>::writePNG(int iT, double xRange, std::string plotName)
{
if (!_init) {
_iT = iT;
_xRange = xRange;
/// initialize the writePlotFile for Gnuplot with the type and the name of the output data
writePlotFile("png", plotName);
startGnuplot("plotPNG", plotName);
}
return;
}
/// plotName specifies the name of the plot in case the user wants to create more than
/// one plot with the simulation results (default: plotName = "")
template< typename T >
void Gnuplot<T>::writePlotFile(std::string type, std::string plotName)
{
if (singleton::mpi().getRank() == _rank ) {
std::ofstream fout;
std::string plotFile;
if (_liveplot && type == "plot") {
plotFile = singleton::directories().getGnuplotOutDir()+"data/plot.p";
} else if (type == "pdf") {
plotFile = singleton::directories().getGnuplotOutDir()+"data/plotPDF.p";
} else if (type == "png") {
plotFile = singleton::directories().getGnuplotOutDir()+"data/plotPNG"+plotName+".p";
} else {
std::cout << "WARNING: invalid Gnuplot type={'', 'plot'; 'pdf', 'png'}" << std::endl;
exit(-1);
}
fout.open(plotFile.c_str(), std::ios::trunc);
fout << "set key " << _key << "\n";
if (type=="pdf") {
fout << "set terminal pdf enhanced" << "\n"
<< "set output '"<<_dir<<_name<<".pdf'" << "\n";
}
if (type=="png") {
if ( !util::nearZero(_xRange+1) ) {
fout << "set xr[0:"<< _xRange <<"]" << "\n";
}
fout << "set terminal png" << "\n"
<< "set output '"<<_dir<<_name;
if (_iT != -1) {
fout <<"_"<<_iT;
}
fout <<".png'" << "\n";
}
/// set the x and y label of the Plot
fout << "set xlabel '" << _xLabel << "'" << "\n";
fout << "set ylabel '" << _yLabel << "'" << "\n";
/// vector which holds the information about the plotType
/// (e.g. scatterplot 'p' or lineplot 'l': default {'l','l'})
fout << "plot '"<<_dataFile<<"' u 1:2 w " << _plotTypes[0] << " t '"<< _names[0] << "'";
for (unsigned int i = 0; i < _dataSize-1; ++i) {
fout << ", '"<<_dataFile<<"' u 1:" << i+3 << " w " << _plotTypes[i+1] << " t '" << _names[i+1] << "'";
}
fout << "\n";
if (_liveplot && type=="plot") {
fout << "pause -1" << "\n"
<< "reread" << "\n";
}
fout.close();
}
return;
}
/// writes the data file for two doubles (x and y)
template< typename T >
void Gnuplot<T>::writeDataFile(T xValue, T yValue)
{
if (singleton::mpi().getRank() == _rank) {
std::ofstream fout;
fout.precision(6);
fout.open(_dataFile.c_str(), std::ios::app);
fout << xValue
<< " "
<< yValue
<< std::endl;
fout.close();
}
return;
}
/// set Label of the gnuplotPlot; xLabel and yLabel
template< typename T >
void Gnuplot<T>::setLabel(std::string xLabel, std::string yLabel)
{
_xLabel = xLabel;
_yLabel = yLabel;
}
/// writes the data file for one double and a vector of doubles (x and y1,y2,...)
template< typename T >
void Gnuplot<T>::writeDataFile(T xValue, std::vector<T> yValues)
{
if (singleton:
|