OpenLB 1.7
Loading...
Searching...
No Matches
blockGifWriter.hh
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2015 Albert Mink, Mathias Krause
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 BLOCK_GIF_WRITER_HH
25#define BLOCK_GIF_WRITER_HH
26
27#include <fstream>
28#include <string>
29#include <vector>
30
31#include "io/blockGifWriter.h"
32#include "io/fileName.h"
34#include "core/singleton.h"
36
37namespace olb {
38
39
40template< typename T >
41BlockGifWriter<T>::BlockGifWriter(std::string const& map)
42 : clout(std::cout, "BlockGifWriter"), _colorRange(1024), _numColors(1024),
43 _colorMap(graphics::mapGenerators::generateMap<T>(map)), _minValue(0),
44 _maxValue(1)
45{ }
46
47template< typename T >
48void BlockGifWriter<T>::write(BlockF2D<T>& f, T minValue, T maxValue, int iT,
49 std::string const& name)
50{
51 // [!] exeption image(f) != 1
52 if ( f.getTargetDim() != 1 ) {
53 clout << "Error: Functor targetDim is not 1. " << std::endl;
54 exit(-1);
55 }
56 else {
57 if ( singleton::mpi().getRank() == 0 ) {
58 std::string fullNamePpm;
59 if ( name == "emptyName") {
60 fullNamePpm = createFileName( singleton::directories().getImageOutDir(),
61 f.getName(), iT);
62 }
63 else {
64 fullNamePpm = createFileName( singleton::directories().getImageOutDir(),
65 name, iT);
66 }
67 fullNamePpm = fullNamePpm + ".ppm" ;
68 std::ofstream fout( fullNamePpm.c_str() );
69
70 // write header
71 fout << "P3\n";
72 // dimension of image
73 fout << f.getBlockStructure().getNx() << " "
74 << f.getBlockStructure().getNy() << "\n";
75 // dynamic range
76 fout << (_colorRange - 1) << "\n";
77
78 int i[2] = {0,0};
79 for (i[1] = f.getBlockStructure().getNy() - 1; i[1] >= 0; --i[1]) {
80 for (i[0] = 0; i[0] < f.getBlockStructure().getNx(); ++i[0]) {
81 T evaluated[1];
82 f(evaluated,i);
83 // scales evaluated in [getMinValue(),getMaxValue()] to [0,1]
84 evaluated[0] = (minValue - evaluated[0]) / (minValue - maxValue);
85 // sets evaluated notin [getMinValue(),getMaxValue()] to 1
86 if ( evaluated[0] >= T(1) ) {
87 evaluated[0] = 1;
88 }
89 graphics::rgb<T> color = _colorMap.get(evaluated[0]);
90 fout << (int)(color.r * (_colorRange - 1)) << " "
91 << (int)(color.g * (_colorRange - 1)) << " "
92 << (int)(color.b * (_colorRange - 1)) << "\n";
93 }
94 }
95 fout.close();
96 }
97 }
98}
99
100template< typename T >
101void BlockGifWriter<T>::write(BlockF2D<T>& f, int iT, std::string const& name)
102{
103 // determine util::min-/maxValue
104 int i[2] = {0,0};
105 // initialize util::min-/maxValue
106 T minValue[1];
107 T maxValue[1];
108 f(minValue,i);
109 f(maxValue,i);
110 for (i[0] = 1; i[0] < f.getBlockStructure().getNx(); i[0]++) {
111 for (i[1] = 1; i[1] < f.getBlockStructure().getNy(); i[1]++) {
112 T valueTmp[1];
113 f(valueTmp,i);
114 if (valueTmp[0] < minValue[0]) {
115 minValue[0] = valueTmp[0];
116 }
117 if (valueTmp[0] > maxValue[0]) {
118 maxValue[0] = valueTmp[0];
119 }
120 }
121 }
122 if (maxValue[0] <= minValue[0]) {
123 minValue[0] = T();
124 maxValue[0] = T(1);
125 }
126 // call write() with util::min-/maxValue
127 write(f, minValue[0], maxValue[0], iT, name);
128}
129
130// iteration on _pointerVec is realized by function
131// dataArray() respective dataArrayBinary()
132template< typename T >
134{
135 if ( _pointerVec.empty() ) {
136 // secure code. doesn't appear on console ??
137 clout << "Error: Please add functor via addFunctor()";
138 }
139 else {
140 int i = 0;
141 for ( auto it = _pointerVec.cbegin(); it != _pointerVec.cend(); ++it, ++i) {
142 if (_autoScale[i]) {
143 write(**it, iT, _name[i]);
144 }
145 else {
146 write(**it, _minValue[i], _maxValue[i], iT, _name[i]);
147 }
148 }
149 }
150}
151
152template< typename T >
153void BlockGifWriter<T>::addFunctor(BlockF2D<T>& f, std::string const& name)
154{
155 _pointerVec.push_back(&f);
156
157 if ( name == "emptyName") {
158 _name.push_back(f.getName() );
159 }
160 _name.push_back(name);
161
162 _autoScale.push_back(true);
163 _minValue.push_back(T());
164 _maxValue.push_back(T());
165}
166
167template< typename T >
168void BlockGifWriter<T>::addFunctor(BlockF2D<T>& f, T minValue, T maxValue,
169 std::string const& name)
170{
171 _pointerVec.push_back(&f);
172
173 if ( name == "emptyName") {
174 _name.push_back(f.getName() );
175 }
176 _name.push_back(name);
177
178 _autoScale.push_back(false);
179 _minValue.push_back(minValue);
180 _maxValue.push_back(maxValue);
181}
182
183
184
185
187//template<typename T>
188//void BlockGifWriter<T>::ppm2gif(const std::string& fullNamePpm,
189// const std::string& fullNameGif)
190//{
191// std::string consoleCommand = "convert " + fullNamePpm
192// + " -resize 600x800^ "
193// + fullNameGif;
194// system(consoleCommand.c_str());
195// consoleCommand = std::string("rm ") + fullNamePpm;
196// system(consoleCommand.c_str());
197//}
198
199
200
201
202
203
204} // namespace olb
205
206#endif
represents all functors that operate on a cuboid in general, mother class of BlockLatticeF,...
virtual BlockStructureD< 2 > & getBlockStructure()
virtual destructor for defined behaviour
void write(BlockF2D< T > &f, T minValue, T maxValue, int iT=0, std::string const &name="emptyName")
writes functor values normed to interval [0,1].
void addFunctor(BlockF2D< T > &f, std::string const &name="emptyName")
put functor to _pointerVec, to simplify writing process of several functors
BlockGifWriter(std::string const &map="leeloo")
int getNy() const
Read only access to block height.
int getNx() const
Read only access to block width.
int getTargetDim() const
read only access to member variable _n
Definition genericF.hh:45
std::string & getName()
read and write access to name
Definition genericF.hh:51
These functions help you to create file names.
Wrapper functions that simplify the use of MPI.
MpiManager & mpi()
Directories & directories()
Definition singleton.h:150
Top level namespace for all of OpenLB.
std::string createFileName(std::string name)
for .pvd masterFile
Definition fileName.hh:34
Definition of singletons: global, publicly available information.