summaryrefslogtreecommitdiff
path: root/src/io/gnuplotHeatMapWriter.hh
blob: 82250d8891fceb7b613f28ff491dc1dc06564332 (plain)
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
/*  This file is part of the OpenLB library
 *
 *  Copyright (C) 2017 Marc Haussmann
 *  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_HEATMAP_WRITER_HH
#define GNUPLOT_HEATMAP_WRITER_HH

#include <fstream>
#include <iostream>
#include <unistd.h>

#include "core/singleton.h"
#include "io/fileName.h"
#include "gnuplotHeatMapWriter.h"
#include "utilities/vectorHelpers.h"

namespace olb {

namespace heatmap {

template <typename T>
void write(BlockReduction3D2D<T>& blockReduction, int iT, const plotParam<T> param) {
  detail::genericHeatMapInterface(blockReduction, blockReduction, iT, param);
}

template <typename T>
void write(BlockReduction2D2D<T>& blockReduction, int iT, const plotParam<T> param) {
  detail::genericHeatMapInterface(blockReduction.getPlaneDiscretizationIn3D(), blockReduction, iT,
                                  param);
}

namespace detail {

template <typename T>
void genericHeatMapInterface(const HyperplaneLattice3D<T>& hyperPlane, BlockF2D<T>& blockData, int iT,
                             const plotParam<T>& plot) {


  if ( blockData.getTargetDim() != 1 ) {
    std::cout << "HeatMap Error: Functor targetDim is not 1. " << std::endl;
    exit(-1);
  }
  else {
    if ( singleton::mpi().getRank() == 0 ) {
      detail::detailParam<T> param;
      param.plot = &plot;
      param.blockData = &blockData;
      param.hyperPlane = &hyperPlane;
      param.dir = singleton::directories().getImageOutDir();
      param.quantityname = param.blockData->getName();
      param.quantityname.erase (param.quantityname.begin(), param.quantityname.begin()+15);
      param.quantityname.pop_back();
      std::string name;
      if (plot.name == "") {
        name = param.quantityname;
      } else {
        name = plot.name;
      }
      param.matrixPath = param.dir + "data/" + name  + ".matrix";
      param.csvPath = createFileName( param.dir + "data/", name, iT) + ".csv";
      param.jpegPath = createFileName( param.dir, name, iT) + ".jpeg";
      param.pngPath = createFileName( param.dir, name, iT) + ".png";
      param.plotFilePath = param.dir + "data/heatMap"+name+".p";

      for (size_t pos = 0; pos < param.plotFilePath.length(); pos++) {
        if (param.plotFilePath.at(pos) == '(' || param.plotFilePath.at(pos) == ')') {
          param.plotFilePath.replace(pos,  1,  "");
        }
      }

      param.nx = param.hyperPlane->getNx();
      param.ny = param.hyperPlane->getNy();
      param.spacing = param.hyperPlane->getPhysSpacing();
      param.origin = param.hyperPlane->getPhysOrigin();
      param.normal = crossProduct3D(param.hyperPlane->getVectorU(),param.hyperPlane->getVectorV());
      param.zoomMin = plot.zoomOrigin;
      param.zoomMax = plot.zoomOrigin + plot.zoomExtend;
      param.iT = iT;

      writeHeatMapDataFile(param);
      writeHeatMapPlotFile(param);
      executeGnuplot(param);
    }
  }
  return;
}

template <typename T>
void writeHeatMapDataFile(detailParam<T>& param) {


  std::ofstream foutMatrix( param.matrixPath.c_str() );

  int i[2] = {0,0};
  for (i[1] = param.ny * param.zoomMin[1]; i[1] < param.ny * param.zoomMax[1]; i[1]++) {
    for (i[0] = param.nx * param.zoomMin[0]; i[0] < param.nx * param.zoomMax[0]; i[0]++) {
      T evaluated[1];
      (*param.blockData)(evaluated,i);
      foutMatrix << evaluated[0] << " ";
      if (i[0] == int(param.nx * param.zoomMax[0]) - 1) {
        foutMatrix  << "\n";
      }
    }
  }
  foutMatrix.close();


  if (param.plot->writeCSV) {
    std::ofstream foutCSV( param.csvPath.c_str() );
    for (i[1] = 0; i[1] < param.ny; i[1]++) {
      for (i[0] = 0; i[0] < param.nx; i[0]++) {
      T evaluated[1];
      Vector<T,3> physPoint = param.hyperPlane->getPhysR(i[0], i[1]);
      (*param.blockData)(evaluated,i);
      foutCSV << physPoint[0] << " " << physPoint[1] << " " << physPoint[2] << " " << evaluated[0] << "\n";
      }
    }
    foutCSV.close();
  }

  return;
}

template <typename T>
void writeHeatMapPlotFile(detailParam<T>& param) {
  std::ofstream fout;

  fout.open(param.plotFilePath.c_str(), std::ios::trunc);

  fout << "if (strstrt(GPVAL_TERMINALS, 'jpeg') > 0) {";
  fout << "set terminal jpeg " << "size " << 1920  << "," << 1080 << "font \",25\"" << "\n";
  fout << "set output '"<< param.jpegPath << "'"<< "\n";
  fout << "} else {";
  fout << "set terminal png " << "size " << 1920  << "," << 1080 << "font \",25\"" << "\n";
  fout << "set output '"<< param.pngPath << "'"<< "\n";
  fout << "}" << "\n";
  fout << "set pm3d map" << "\n";
  fout << "unset key" << "\n";
  fout << "set size ratio -1" << "\n";
  fout << "set size 0.925,1.0" << "\n";
  fout << "set xtics out" << "\n";
  fout << "set ytics out" << "\n";
  fout << "set xtics nomirror" << "\n";
  fout << "set ytics nomirror" << "\n";
  fout << "set pm3d interpolate 0,0" << "\n";
  fout << "set colorbox vertical user origin 0.85,0.1 size 0.025 ,0.8" << "\n";

  if ( util::nearZero(param.normal[0]) && util::nearZero(param.normal[1]) ) {
    fout << "set xlabel \"x-axis in m \"" << "\n"
         << "set ylabel \"y-axis in m \"" << "\n";
  } else if ( util::nearZero(param.normal[0]) && util::nearZero(param.normal[2]) ) {
    fout << "set xlabel \"x-axis in m \"" << "\n"
         << "set ylabel \"z-axis in m \"" << "\n";
    param.origin[1] = param.origin[2];
  } else if ( util::nearZero(param.normal[1]) && util::nearZero(param.normal[2]) ) {
    fout << "set xlabel \"y-axis in m \"" << "\n"
         << "set ylabel \"z-axis in m \"" << "\n";
    param.origin[0] = param.origin[1];
    param.origin[1] = param.origin[2];
  } else