OpenLB 1.7
Loading...
Searching...
No Matches
blockVtkWriter2D.hh
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2*
3* Copyright (C) 2014 Albert Mink, Mathias J. 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
29#ifndef BLOCK_VTK_WRITER_2D_HH
30#define BLOCK_VTK_WRITER_2D_HH
31
32#include <fstream>
33#include <iostream>
35#include "core/singleton.h"
36#include "io/blockVtkWriter2D.h"
37#include "io/base64.h"
38#include "io/fileName.h"
39
40namespace olb {
41
42
43template<typename T>
44BlockVTKwriter2D<T>::BlockVTKwriter2D( std::string name, bool binary )
45 : clout( std::cout,"BlockVTKwriter2D" ), _name(name), _binary(binary)
46{}
47
48template<typename T>
50{
51 clearAddedFunctors();
52}
53
54// iteration on _pointerVec is realized by function
55// dataArray() respective dataArrayBinary()
56template<typename T>
58{
59 if ( _pointerVec.empty() ) {
60 clout << "Error: Please add functor via addFunctor()";
61 }
62 else {
63 auto it = _pointerVec.cbegin();
64
65 T originX = 0;
66 T originY = 0;
67 T originZ = 0;
68 int nx = (**it).getBlockStructure().getNx() -1;
69 int ny = (**it).getBlockStructure().getNy() -1;
70
71 std::string fullNameVti = singleton::directories().getVtkOutDir()
72 + createFileName( _name, iT ) + ".vti";
73
74
75 preamble( fullNameVti, nx,ny, originX,originY,originZ );
76 if ( _binary ) {
77 for ( auto functor = _pointerVec.cbegin(); functor != _pointerVec.cend(); ++functor) {
78 writeRawDataBinary( fullNameVti, **functor, nx, ny);
79 }
80 }
81 else {
82 for ( auto functor = _pointerVec.cbegin(); functor != _pointerVec.cend(); ++functor) {
83 writeRawData( fullNameVti, **functor, nx, ny);
84 }
85 }
86 closePreamble( fullNameVti );
87 }
88}
89
90template<typename T>
92{
93 T originX = 0;
94 T originY = 0;
95 T originZ = 0;
96 int nx = f.getBlockStructure().getNx() -1;
97 int ny = f.getBlockStructure().getNy() -1;
98
99 std::string fullNameVti = singleton::directories().getVtkOutDir()
100 + createFileName( f.getName(), iT ) + ".vti";
101
102 preamble( fullNameVti, nx,ny, originX,originY,originZ );
103 if ( _binary ) {
104 writeRawDataBinary( fullNameVti, f, nx,ny );
105 }
106 else {
107 writeRawData( fullNameVti, f, nx,ny );
108 }
109 closePreamble( fullNameVti );
110}
111
112template<typename T>
114{
115 _pointerVec.push_back(&f);
116}
117
118template<typename T>
120{
121 _pointerVec.clear();
122}
123
124template<typename T>
125void BlockVTKwriter2D<T>::preamble(const std::string& fullName, int nx, int ny,
126 T originX, T originY, T originZ)
127{
128 if (singleton::mpi().getRank()==0) {
129 std::ofstream fout(fullName.c_str());
130 if (!fout) {
131 clout << "Error: could not open " << fullName << std::endl;
132 }
133
134 // spacing is not known for BlockF2D classes
135 // prone to error: spacing might correspond to extension in y direction
136 // at the end of the day, is can be fixed by apply a scaling in paraview
137 double spacing = double(1.0/nx);
138
139 fout << "<?xml version=\"1.0\"?>\n";
140 fout << "<VTKFile type=\"ImageData\" version=\"0.1\" "
141 << "byte_order=\"LittleEndian\">\n";
142 fout << "<ImageData WholeExtent=\""
143 << 0 <<" "<< nx <<" "
144 << 0 <<" "<< ny <<" "
145 << 0 <<" "<< 0
146 << "\" Origin=\"" << originX << " " << originY << " " << originZ
147 << "\" Spacing=\"" << spacing << " " << spacing << " " << spacing << "\">\n";
148
149 fout << "<Piece Extent=\""
150 << 0 <<" "<< nx <<" "
151 << 0 <<" "<< ny <<" "
152 << 0 <<" "<< 0 <<"\">\n";
153
154 fout << "<PointData>\n";
155 fout.close();
156 }
157}
158
159template<typename T>
160void BlockVTKwriter2D<T>::closePreamble(const std::string& fullNamePiece)
161{
162 if (singleton::mpi().getRank()==0) {
163 std::ofstream fout(fullNamePiece.c_str(), std::ios::app );
164 if (!fout) {
165 clout << "Error: could not open " << fullNamePiece << std::endl;
166 }
167 fout << "</PointData>\n";
168 fout << "</Piece>\n";
169 fout << "</ImageData>\n";
170 fout << "</VTKFile>\n";
171 fout.close();
172 }
173}
174
175template<typename T>
176void BlockVTKwriter2D<T>::writeRawData(const std::string& fullNameVti, BlockF2D<T>& f,
177 int nx, int ny)
178{
179 std::ofstream fout(fullNameVti.c_str(), std::ios::app);
180 if (!fout) {
181 clout << "Error: could not open " << fullNameVti << std::endl;
182 }
183
184 if (singleton::mpi().getRank()==0) {
185 fout << "<DataArray " ;
186 fout << "type=\"Float32\" Name=\"" << f.getName() << "\" "
187 << "NumberOfComponents=\"" << f.getTargetDim() <<"\">\n";
188 }
189
190 int i[2] = {int()};
191 T evaluated[f.getTargetDim()];
192 for (int iDim = 0; iDim < f.getTargetDim(); ++iDim) {
193 evaluated[iDim] = T();
194 }
195 for (i[1] = 0; i[1] < ny+1; ++i[1]) {
196 for (i[0] = 0; i[0] < nx+1; ++i[0]) {
197 f(evaluated,i);
198 for (int iDim = 0; iDim < f.getTargetDim(); ++iDim) {
199 if (singleton::mpi().getRank()==0) {
200 fout << evaluated[iDim] << " ";
201 }
202 }
203 }
204 }
205
206 if (singleton::mpi().getRank()==0) {
207 fout << "\n</DataArray>\n";
208 }
209
210 fout.close();
211}
212
213template<typename T>
214void BlockVTKwriter2D<T>::writeRawDataBinary(const std::string& fullNameVti,
215 BlockF2D<T>& f, int nx, int ny)
216{
217 const char* fileName = fullNameVti.c_str();
218 std::ofstream fout(fileName, std::ios::app);
219 if (!fout) {
220 clout << "Error: could not open " << fileName << std::endl;
221 }
222
223 if (singleton::mpi().getRank()==0) {
224 fout << "<DataArray " ;
225 if (f.getTargetDim() == 1) {
226 fout << "type=\"Float32\" Name=\"" << f.getName() << "\" "
227 << "format=\"binary\" encoding=\"base64\">\n";
228 }
229 else {
230 fout << "type=\"Float32\" Name=\"" << f.getName() << "\" "
231 << "format=\"binary\" encoding=\"base64\" "
232 << "NumberOfComponents=\"" << f.getTargetDim() <<"\">\n";
233 }
234 }
235 fout.close();
236
237 std::ofstream ofstr( fileName, std::ios::out | std::ios::app | std::ios::binary );
238 if (!ofstr) {
239 clout << "Error: could not open " << fileName << std::endl;
240 }
241
242 size_t fullSize = f.getTargetDim() * (1 + nx) * (1 + ny) * (1);
243 size_t binarySize = size_t( fullSize * sizeof(float) );
244 // writes first number, which have to be the size(byte) of the following data
245 Base64Encoder<unsigned int> sizeEncoder(ofstr, 1);
246 unsigned int uintBinarySize = (unsigned int)binarySize;
247 sizeEncoder.encode(&uintBinarySize, 1);
248 // write numbers from functor
249 Base64Encoder<float>* dataEncoder = nullptr;
250 dataEncoder = new Base64Encoder<float>( ofstr, fullSize );
251
252
253 int i[2] = {int()};
254 T evaluated[f.getTargetDim()];
255 for (int iDim = 0; iDim < f.getTargetDim(); ++iDim) {
256 evaluated[iDim] = T();
257 }
258 for (i[1] = 0; i[1] < ny+1; ++i[1]) {
259 for (i[0] = 0; i[0] < nx+1; ++i[0]) {
260 f(evaluated,i);
261 for (int iDim = 0; iDim < f.getTargetDim(); ++iDim) {
262 if (singleton::mpi().getRank()==0) {
263 const float evaluated2 = float( evaluated[iDim] );
264 dataEncoder->encode( &evaluated2, 1 );
265 }
266 }
267 }
268 }
269 ofstr.close();
270
271 if (singleton::mpi().getRank()==0) {
272 std::ofstream foutt(fileName, std::ios::out | std::ios::app);
273 if (!foutt) {
274 clout << "Error: could not open " << fileName << std::endl;
275 }
276 foutt << "\n</DataArray>\n";
277 foutt.close();
278 }
279 delete dataEncoder;
280}
281
282
283} // namespace olb
284
285#endif
A method to write vtk data for block geometries (only for uniform grids) – header file.
represents all functors that operate on a cuboid in general, mother class of BlockLatticeF,...
virtual BlockStructureD< 2 > & getBlockStructure()
virtual destructor for defined behaviour
int getNy() const
Read only access to block height.
int getNx() const
Read only access to block width.
BlockVTKwriter2D writes any BLockF2D to vtk-based output files.
void write(int iT=0)
method calls preamble(), pointData(), data() and coresponding closing methods.
void clearAddedFunctors()
to clear stored functors
BlockVTKwriter2D(std::string name, bool binary=true)
void addFunctor(BlockF2D< T > &f)
put functor to _pointerVec to simplify writing process of several functors
std::string & getName()
read and write access to name
Definition genericF.hh:51
std::string getVtkOutDir() const
Definition singleton.h:97
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.