OpenLB 1.7
Loading...
Searching...
No Matches
vtiWriter.hh
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2006, 2007, 2009, 2012, 2015 Mathias J. Krause, Benjamin Förster, Jonas Latt, Tim Dornieden
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 VTI_WRITER_HH
30#define VTI_WRITER_HH
31
32#include <fstream>
33#include <cstdlib>
34#include <iostream>
35#include <sstream>
36#include <string>
37#include <vector>
38
39#include "core/singleton.h"
40#include "geometry/cuboid3D.h"
42#include "core/blockData.h"
43#include "core/superData.h"
45#include "vtiWriter.h"
46
47namespace olb {
48
49// Initialize Output
50template<typename T, typename BaseType>
51OstreamManager VTIwriter3D<T,BaseType>::clout("VTIwriter3D");
52
53template<typename T, typename BaseType>
55
56template<typename T, typename BaseType>
57void VTIwriter3D<T,BaseType>::writeData( std::string const& fName,
58 std::string const& fieldName, BlockData<3,T,BaseType> const& blockData,
59 Cuboid3D<T> const& cuboid)
60{
61 // Generate full file path
62 std::string fullName = getFullName(fName);
63 writePreamble(fullName, cuboid);
64 writeBlockData(fullName, fieldName, blockData, cuboid);
65 writePostScript(fullName);
66}
67
68template<typename T, typename BaseType>
69void VTIwriter3D<T,BaseType>::writeData( std::string const& fName, std::string const& fieldName,
70 SuperData<3,T,BaseType> const& superData, CuboidGeometry3D<T> const& cGeometry,
71 LoadBalancer<T> const& loadBalancer)
72{
73 // Generate full file path
74 std::string fullName = getFullName(fName);
75
76 int rank = 0;
77 int size = 1;
78#ifdef PARALLEL_MODE_MPI
79 rank = singleton::mpi().getRank();
80 size = singleton::mpi().getSize();
81#endif
82
83 if ( rank == 0 ) {
84 // Write XML Preamble with geometry of surrounding cuboid
85 writePreamble(fullName, cGeometry.getMotherCuboid());
86 }
87#ifdef PARALLEL_MODE_MPI
89#endif
90
91 // Write VTI File in Order of MPI Rank
92 for (int iRank = 0; iRank < size; ++iRank) {
93 if (rank == iRank) {
94 for (int iC = 0; iC < loadBalancer.size(); ++iC) {
95 // Write data block by block
96 writeBlockData(fullName, fieldName, superData.getBlock(iC), cGeometry.get(loadBalancer.glob(iC)));
97 }
98 }
99#ifdef PARALLEL_MODE_MPI
100 // Wait for current writer process
102#endif
103 }
104 //
105 if (rank == 0) {
106 // Close All XML Tags
107 writePostScript(fullName);
108 }
109}
110
111template<typename T, typename BaseType>
112void VTIwriter3D<T,BaseType>::writeData( std::string const& fName,
113 std::string const& fieldName, SuperData<3,T,BaseType> const& superData)
114{
115 writeData(fName, fieldName, superData, superData.getCuboidGeometry(), superData.getLoadBalancer());
116}
117
118template<typename T, typename BaseType>
119void VTIwriter3D<T,BaseType>::writeBlockData(std::string& fullName, std::string const& fieldName,
120 BlockData<3,T,BaseType> const& blockData, Cuboid3D<T> const& cuboid)
121{
122 // Open File
123 std::ofstream fout(fullName.c_str(), std::ios::app);
124 if (!fout) {
125 std::cout << "Error: could not open file " << fullName << std::endl;
126 }
127
128 // Calculate extent info from cuboid
129 // NOTE: origin is actual coordinate, globX needs to be number of starting node
130 Vector<T,3> origin = cuboid.getOrigin();
131 T delta = cuboid.getDeltaR();
132 int globX = (int) (origin[0] / (T)delta);
133 int globY = (int) (origin[1] / (T)delta);
134 int globZ = (int) (origin[2] / (T)delta);
135 fout << "<Piece Extent=\""
136 << globX << " " << cuboid.getNx() - 1 + globX << " "
137 << globY << " " << cuboid.getNy() - 1 + globY << " "
138 << globZ << " " << cuboid.getNz() - 1 + globZ << "\">\n";
139
140 // Write Point Data
141 fout << "<PointData>\n";
142 // fout << "<DataArray type=\"Float32\" Name=\"" << blockData.getName()
143 // << "\" NumberOfComponents=\"" << blockData.getSize() << "\">\n";
144
145 fout << "<DataArray type=\"Float32\" Name=\"" << fieldName
146 << "\" NumberOfComponents=\"" << blockData.getSize() << "\">\n";
147
148 // NOTE: Respect ordering of VTI File!
149 for (int iZ = 0; iZ < cuboid.getNz(); ++iZ) {
150 for (int iY = 0; iY < cuboid.getNy(); ++iY) {
151 for (int iX = 0; iX < cuboid.getNx(); ++iX) {
152 for (unsigned iSize = 0; iSize < blockData.getSize(); ++iSize) {
153 fout << blockData.get({iX, iY, iZ}, iSize) << " ";
154 }
155 }
156 }
157 fout << "\n";
158 }
159
160 // Close All Tags
161 fout << "</DataArray>\n";
162 fout << "</PointData>\n";
163 fout << "</Piece>\n";
164
165 // Close File
166 fout.close();
167}
168
169template<typename T, typename BaseType>
170void VTIwriter3D<T,BaseType>::writePreamble(std::string& fullName, Cuboid3D<T> const& cuboid)
171{
172 Vector<T,3> origin = cuboid.getOrigin();
173 writePreamble(fullName,
174 cuboid.getNx() - 1, cuboid.getNy() - 1, cuboid.getNz() - 1,
175 cuboid.getDeltaR(),
176 origin[0], origin[1], origin[2]);
177}
178
179template<typename T, typename BaseType>
180void VTIwriter3D<T,BaseType>::writePreamble(std::string& fullName, int nx,
181 int ny, int nz, T delta, T originX, T originY, T originZ)
182{
183 // Open File
184 std::ofstream fout(fullName.c_str());
185 if (!fout) {
186 clout << "Error: could not open " << fullName << std::endl;
187 }
188
189 // Follow VTI standard
190 fout << "<?xml version=\"1.0\"?>\n";
191 fout << "<VTKFile type=\"ImageData\" version=\"0.1\" "
192 << "byte_order=\"LittleEndian\">\n";
193 fout << "<ImageData";
194 fout << " WholeExtent=\" 0 " << nx << " 0 " << ny << " 0 " << nz << " \"";
195 fout << " Origin=\"" << originX << " " << originY << " " << originZ << "\"";
196 fout << " Spacing=\"" << delta << " " << delta << " " << delta << "\">\n";
197
198 // Close File
199 fout.close();
200}
201
202template<typename T, typename BaseType>
203void VTIwriter3D<T,BaseType>::writePostScript(std::string& fullName)
204{
205 std::ofstream fout(fullName.c_str(), std::ios::app );
206 if (!fout) {
207 clout << "Error: could not open " << fullName << std::endl;
208 }
209
210 fout << "</ImageData>\n";
211 fout << "</VTKFile>\n";
212
213 fout.close();
214}
215
216template<typename T, typename BaseType>
217std::string VTIwriter3D<T,BaseType>::getFullName(std::string const& fName)
218{
219 return singleton::directories().getVtkOutDir() + fName + ".vti";
220}
221
222
223} // namespace olb
224
225#endif
unsigned getSize() const
Definition blockData.hh:118
U & get(std::size_t iCell, int iD=0)
Definition blockData.hh:94
A regular single 3D cuboid is the basic component of a 3D cuboid structure which defines the grid.
Definition cuboid3D.h:58
Vector< T, 3 > getOrigin() const
Read only access to left lower corner coordinates.
Definition cuboid3D.hh:135
T getDeltaR() const
Read only access to the distance of cuboid nodes.
Definition cuboid3D.hh:141
int getNz() const
Read access to cuboid depth.
Definition cuboid3D.hh:159
int getNy() const
Read access to cuboid height.
Definition cuboid3D.hh:153
int getNx() const
Read access to cuboid width.
Definition cuboid3D.hh:147
A cuboid geometry represents a voxel mesh.
Cuboid3D< T > & get(int iC)
Read and write access to a single cuboid.
Cuboid3D< T > getMotherCuboid()
Returns the smallest cuboid that includes all cuboids of the structure.
Base class for all LoadBalancer.
int glob(int loc) const
const BlockData< D, T, U > & getBlock(int iC) const
Definition superData.hh:88
CuboidGeometry< T, D > & getCuboidGeometry()
Read and write access to cuboid geometry.
LoadBalancer< T > & getLoadBalancer()
Read and write access to the load balancer.
static void writeData(std::string const &fName, std::string const &fieldName, BlockData< 3, T, BaseType > const &blockData, Cuboid3D< T > const &cuboid)
Write Single Block Data.
Definition vtiWriter.hh:57
std::string getVtkOutDir() const
Definition singleton.h:97
int getSize() const
Returns the number of processes.
int getRank() const
Returns the process ID.
void barrier(MPI_Comm comm=MPI_COMM_WORLD)
Synchronizes the processes.
The description of a single 3D cuboid – header file.
The description of a vector of 3D cuboid – header file.
MpiManager & mpi()
Directories & directories()
Definition singleton.h:150
Top level namespace for all of OpenLB.
Definition of singletons: global, publicly available information.
Dynamics for a generic 2D super data – header file.
A method to write vti data for cuboid geometries (only for uniform grids) – header file.