OpenLB 1.8.1
Loading...
Searching...
No Matches
gnuplotHeatMapWriter.hh
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2017 Marc Haussmann
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_HEATMAP_WRITER_HH
25#define GNUPLOT_HEATMAP_WRITER_HH
26
27#include <fstream>
28#include <iostream>
29#include <unistd.h>
30
31#include "core/singleton.h"
32#include "io/fileName.hh"
35
36namespace olb {
37
38namespace heatmap {
39
40namespace detail {
41
42template <typename T>
43void genericHeatMapInterface(const HyperplaneLattice3D<T>& hyperPlane, BlockF2D<T>& blockData, int iT,
44 const std::vector<T>& valueArea, const plotParam<T>& plot)
45{
46
47
48 if ( blockData.getTargetDim() != 1 ) {
49 std::cout << "HeatMap Error: Functor targetDim is not 1. " << std::endl;
50 exit(-1);
51 }
52 else {
53 if ( singleton::mpi().getRank() == 0 ) {
55 param.plot = &plot;
56 param.blockData = &blockData;
57 param.hyperPlane = &hyperPlane;
59 param.quantityname = param.blockData->getName();
60 param.quantityname.erase (param.quantityname.begin(), param.quantityname.begin()+15);
61 param.quantityname.pop_back();
62 std::string name;
63 if (plot.name == "") {
64 name = param.quantityname;
65 }
66 else {
67 name = plot.name;
68 }
69 //std::stringstream fNameStream;
70 //fNameStream << param.dir << "data/"<< name << "iT" << std::setw(7) << std::setfill('0') << iT;
71
72 param.matrixPath = createFileName( param.dir + "data/", name, iT) + ".matrix";
73 param.csvPath = createFileName( param.dir + "data/", name, iT) + ".csv";
74 param.jpegPath = createFileName( param.dir, name, iT) + ".jpeg";
75 param.pngPath = createFileName( param.dir, name, iT) + ".png";
76 param.plotFilePath = createFileName( param.dir + "data/", name, iT) +".p";
77
78 for (std::size_t pos = 0; pos < param.plotFilePath.length(); pos++) {
79 if (param.plotFilePath.at(pos) == '(' || param.plotFilePath.at(pos) == ')') {
80 param.plotFilePath.replace(pos, 1, "");
81 }
82 }
83
84 param.nx = param.hyperPlane->getNx();
85 param.ny = param.hyperPlane->getNy();
86 param.spacing = param.hyperPlane->getPhysSpacing();
87 param.origin = param.hyperPlane->getPhysOrigin();
88 param.normal = crossProduct3D(param.hyperPlane->getVectorU(),param.hyperPlane->getVectorV());
89 param.zoomMin = plot.zoomOrigin;
90 param.zoomMax = plot.zoomOrigin + plot.zoomExtend;
91 param.iT = iT;
92 //aspect ratio declared here
93 param.aspect = param.nx/(double)param.ny;
94 if (plot.fullScreenPlot) {
95 T base = std::min(param.nx, param.ny);
96 param.canvasX = param.aspect > 1. ? param.aspect*base : base;
97 param.canvasY = param.aspect < 1. ? base/param.aspect : base;
98 } else {
99 param.canvasX = param.aspect > 1. ? param.aspect*1000 : T(1000);
100 param.canvasY = param.aspect < 1. ? 1000/param.aspect : T(1000);
101 }
102 // avoid exceeding jpeg pixel range
103 if (param.canvasX > 65500)
104 param.canvasX = 65499;
105 if (param.canvasY > 65500)
106 param.canvasY = 65499;
107 param.cbXscaling = param.canvasX/(double)1000;
109 writeHeatMapPlotFile(param, valueArea);
110 executeGnuplot(param);
111 }
112 }
113 return;
114}
115
116template <typename T>
118{
119
120
121 std::ofstream foutMatrix( param.matrixPath.c_str() );
122
123 int i[2] = {0,0};
124 for (i[1] = param.ny * param.zoomMin[1]; i[1] < param.ny * param.zoomMax[1]; i[1]++) {
125 for (i[0] = param.nx * param.zoomMin[0]; i[0] < param.nx * param.zoomMax[0]; i[0]++) {
126 T evaluated[1];
127 (*param.blockData)(evaluated,i);
128 foutMatrix << BaseType<T>(evaluated[0]) << " ";
129 if (i[0] == int(param.nx * param.zoomMax[0]) - 1) {
130 foutMatrix << "\n";
131 }
132 }
133 }
134 foutMatrix.close();
135
136
137 if (param.plot->writeCSV) {
138 std::ofstream foutCSV( param.csvPath.c_str() );
139 for (i[1] = 0; i[1] < param.ny; i[1]++) {
140 for (i[0] = 0; i[0] < param.nx; i[0]++) {
141 T evaluated[1];
142 Vector<T,3> physPoint = param.hyperPlane->getPhysR(i[0], i[1]);
143 (*param.blockData)(evaluated,i);
144 foutCSV << physPoint[0] << " " << physPoint[1] << " " << physPoint[2] << " " << evaluated[0] << "\n";
145 }
146 }
147 foutCSV.close();
148 }
149
150 return;
151}
152
153template <typename T>
154void writeHeatMapPlotFile(detailParam<T>& param, const std::vector<T>& valueArea)
155{
156 std::ofstream fout;
157
158 fout.open(param.plotFilePath.c_str(), std::ios::trunc);
159
160 fout << "if (strstrt(GPVAL_TERMINALS, 'jpeg') > 0) {";
161 fout << "set terminal jpeg " << "size " << param.canvasX << "," << param.canvasY << "font \",25\"" << "\n";
162 fout << "set output '"<< param.jpegPath << "'"<< "\n";
163 fout << "} else {";
164 fout << "set terminal png " << "size " << param.canvasX << "," << param.canvasY << "font \",25\"" << "\n";
165 fout << "set output '"<< param.pngPath << "'"<< "\n";
166 fout << "}" << "\n";
167 fout << "set pm3d map" << "\n";
168 fout << "unset key" << "\n";
169
170 if (param.plot->fullScreenPlot) {
171 fout << "unset xtics" << "\n";
172 fout << "unset ytics" << "\n";
173 fout << "unset border"<< "\n";
174 } else {
175 fout << "set xtics out" << "\n";
176 fout << "set ytics out" << "\n";
177 fout << "set xtics nomirror" << "\n";
178 fout << "set ytics nomirror" << "\n";
179 }
180
181
182
183 fout << "set pm3d interpolate 0,0" << "\n";
184
185 if (param.plot->fullScreenPlot) {
186 //plot without margins if fullscreenmode enabled
187 fout << "set lmargin at screen 0" << "\n";
188 fout << "set rmargin at screen 1" << "\n";
189 fout << "set tmargin at screen 0" << "\n";
190 fout << "set bmargin at screen 1" << "\n";
191 } else {
192 //set size ratio for non-fullscreenmode plots
193 fout << "set size ratio -1" << "\n";
194 fout << "set size 0.925,1.0" << "\n";
195 }
196
197 //enable colorbox
198 if( param.plot->fullScreenPlot == false || param.plot->activateFullScreenPlotColorBox == true ) {
199 fout << "set colorbox vertical user origin 0.85,0.1 size " << 0.025/BaseType<T>(param.cbXscaling) << " ,0.8" << "\n";
200 }
201
202 if ( util::nearZero(param.normal[0]) && util::nearZero(param.normal[1]) ) {
203 fout << "set xlabel \"x-axis in m \"" << "\n"
204 << "set ylabel \"y-axis in m \"" << "\n";
205 }
206 else if ( util::nearZero(param.normal[0]) && util::nearZero(param.normal[2]) ) {
207 fout << "set xlabel \"x-axis in m \"" << "\n"
208 << "set ylabel \"z-axis in m \"" << "\n";
209 param.origin[1] = param.origin[2];
210 }
211 else if ( util::nearZero(param.normal[1]) && util::nearZero(param.normal[2]) ) {
212 fout << "set xlabel \"y-axis in m \"" << "\n"
213 << "set ylabel \"z-axis in m \"" << "\n";
214 param.origin[0] = param.origin[1];
215 param.origin[1] = param.origin[2];
216 }
217 else {
218 fout << "set xlabel \"width in m \"" << "\n"
219 << "set ylabel \"height in m \"" << "\n";
220
221 }
222
223 if (param.plot->contourlevel > 0) {
224 fout << "set contour base" << "\n";
225 fout << "set cntrparam levels " << param.plot->contourlevel << "\n";
226 fout << "set cntrparam bspline" << "\n";
227 fout << "do for [i=1:"<< param.plot->contourlevel <<"] {" << "\n";
228 fout << "set linetype i lc rgb \"black\""<< "\n";
229 fout << "}" << "\n";
230 }
231
232 fout << "set cblabel offset 0.5 \"" <<param.quantityname;
233 if (param.quantityname == "l2(physVelocity)" || param.quantityname == "EuklidNorm(physVelocity)") {
234 fout << " in m/s\"" << "\n";
235 }
236 else if (param.quantityname == "physPressure") {
237 fout << " in Pa\"" << "\n";
238 }
239 else {
240 fout << "\"\n";
241 }
242
243 if (!util::nearZero(param.plot->maxValue - param.plot->minValue)) {
244 fout << "set cbrange [" << BaseType<T>(param.plot ->minValue) << ":" << BaseType<T>(param.plot->maxValue) <<"]" << "\n";
245 }
246
247 if (valueArea.empty()) {
248 fout << "set autoscale fix" << "\n";
249 }
250 else if (valueArea[0] < valueArea[1]) {
251 fout << "set cbrange [" << BaseType<T>(valueArea[0]) << ":" << BaseType<T>(valueArea[1]) << "]" << "\n";
252 }
253 else {
254 fout << "set cbrange [" << BaseType<T>(valueArea[1]) << ":" << BaseType<T>(valueArea[0]) << "]" << "\n";
255 }
256
257 if (param.plot->colour == "grey") {
258 fout << "set palette grey" << "\n";
259 }
260 else if (param.plot->colour == "pm3d") {
261
262 }
263 else if (param.plot->colour == "blackbody") {
264 fout << "set palette defined ( 0 \"black\", 1 \"red\", 2 \"yellow\")" << "\n";
265 }
266 else {
267 fout << "set palette defined ( 0 \"blue\", 1 \"green\", 2 \"yellow\", 3 \"orange\", 4 \"red\" )" << "\n";
268 }
269 fout << "splot '" << param.matrixPath << "' u ($1*" << BaseType<T>(param.spacing) << "+" << BaseType<T>(param.origin[0]) + int(param.nx * BaseType<T>(param.zoomMin[0]))*BaseType<T>(param.spacing) << "):"
270 << "($2*" << BaseType<T>(param.spacing) << "+" << BaseType<T>(param.origin[1]) + int(param.ny * BaseType<T>(param.zoomMin[1]))*BaseType<T>(param.spacing) <<"):3 matrix with pm3d" << "\n";
271 fout.close();
272 return;
273}
274
275template< typename T >
277{
278 if (! gnuplotInstalled()) {
279 std::cout << "We could not find a gnuplot distribution at your system." << std::endl;
280 std::cout << "We still write the data files s.t. you can plot the data yourself." << std::endl;
281 return;
282 }
283#ifndef WIN32
284 if (!system(nullptr)) {
285 exit (EXIT_FAILURE);
286 }
287 const std::string command = "gnuplot "+ param.plotFilePath +" > /dev/null &";
288 if ( system(command.c_str()) ) {
289 std::cout << "Error at GnuplotWriter" << std::endl;
290 }
291 return;
292#endif
293}
294
296{
297#ifdef WIN32
298 return false;
299#else
300 return (! system("which gnuplot >/dev/null 2>/dev/null"));
301#endif
302}
303
304} // namespace detail
305
306} // namespace heatmap
307
308} // namespace olb
309
310#endif
represents all functors that operate on a cuboid in general, mother class of BlockLatticeF,...
Definition aliases.h:173
int getTargetDim() const
read only access to member variable _n
Definition genericF.hh:45
Parametrization of a hyperplane lattice.
Plain old scalar vector.
std::string getImageOutDir() const
Definition singleton.h:99
void writeHeatMapDataFile(detailParam< T > &param)
void genericHeatMapInterface(const HyperplaneLattice3D< T > &hyperPlane, BlockF2D< T > &blockData, int iT, const std::vector< T > &valueArea, const plotParam< T > &param)
void executeGnuplot(detailParam< T > &param)
void writeHeatMapPlotFile(detailParam< T > &param, const std::vector< T > &valueArea)
MpiManager & mpi()
Directories & directories()
Definition singleton.h:162
bool nearZero(T a) any_platform
return true if a is close to zero
Definition util.h:402
Top level namespace for all of OpenLB.
std::string createFileName(std::string name)
for .pvd masterFile
Definition fileName.hh:34
typename util::BaseTypeHelper< T >::type BaseType
Definition baseType.h:59
constexpr Vector< T, 3 > crossProduct3D(const ScalarVector< T, 3, IMPL > &a, const ScalarVector< T, 3, IMPL_ > &b) any_platform
Definition vector.h:263
Definition of singletons: global, publicly available information.
const HyperplaneLattice3D< T > * hyperPlane