Loading...
Searching...
No Matches
VTKRectilinear.cpp
1/*---------------------------------------------------------------------------*\
2 *
3 * bitpit
4 *
5 * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6 *
7 * -------------------------------------------------------------------------
8 * License
9 * This file is part of bitpit.
10 *
11 * bitpit is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License v3 (LGPL)
13 * as published by the Free Software Foundation.
14 *
15 * bitpit is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with bitpit. If not, see <http://www.gnu.org/licenses/>.
22 *
23\*---------------------------------------------------------------------------*/
24
25#include "VTK.hpp"
26
27#include "logger.hpp"
28
29namespace bitpit{
30
47
49
50 m_geometry.push_back( VTKField("x_Coord") ) ;
51 m_geometry.push_back( VTKField("y_Coord") ) ;
52 m_geometry.push_back( VTKField("z_Coord") ) ;
53
54 for( auto & field : m_geometry ){
55 field.setLocation( VTKLocation::POINT ) ;
56 field.setFieldType( VTKFieldType::SCALAR ) ;
57 field.setDataType( VTKDataType::Float64 ) ;
58 field.setCodification(m_geomCodex);
59 }
60
61 for (int k = 0; k < 3; ++k) {
62 m_localIndex[k] = {{ -1, -1 }} ;
63 m_globalIndex[k] = {{ -1, -1 }} ;
64 }
65
66 m_dimensions = -1 ;
67
68}
69
76VTKRectilinearGrid::VTKRectilinearGrid( const std::string &dir, const std::string &name ) :VTKRectilinearGrid( ) {
77
78 setNames( dir, name ) ;
79
80}
81
95VTKRectilinearGrid::VTKRectilinearGrid( const std::string &dir, const std::string &name, VTKFormat codex, int n1, int n2, int m1, int m2, int l1, int l2 ) :VTKRectilinearGrid( ) {
96
97 setNames( dir, name ) ;
98
99 setDimensions( n1, n2, m1, m2, l1, l2) ;
100 setGeomCodex( codex ) ;
101
102}
103
115VTKRectilinearGrid::VTKRectilinearGrid( const std::string &dir, const std::string &name, VTKFormat codex, int n, int m, int l ) : VTKRectilinearGrid( ) {
116
117 setNames( dir, name ) ;
118
119 setDimensions( 0, n-1, 0, m-1, 0, l-1) ;
120 setGeomCodex( codex ) ;
121
122}
123
135VTKRectilinearGrid::VTKRectilinearGrid( const std::string &dir, const std::string &name, VTKFormat codex, int n1, int n2, int m1, int m2 ) :VTKRectilinearGrid( ) {
136
137 setNames( dir, name ) ;
138
139 setDimensions( n1, n2, m1, m2, 0, 0) ;
140 setGeomCodex( codex ) ;
141
142}
143
153VTKRectilinearGrid::VTKRectilinearGrid( const std::string &dir, const std::string &name, VTKFormat codex, int n, int m ) : VTKRectilinearGrid( ) {
154
155 setNames( dir, name ) ;
156
157 setDimensions( 0, n-1, 0, m-1, 0, 0) ;
158 setGeomCodex( codex ) ;
159
160}
161
167
168 std::fstream str;
169 std::string line, temp;
170
171 std::fstream::pos_type position;
172 std::array<int,6> extensions ;
173
174
175 str.open( m_fh.getPath( ), std::ios::in ) ;
176
177 getline( str, line);
178 while( ! bitpit::utils::string::keywordInString( line, "<VTKFile")){
179 getline(str, line);
180 }
181
182 if( bitpit::utils::string::getAfterKeyword( line, "header_type", '\"', temp) ){
183 setHeaderType( temp) ;
184 }
185
186 while( ! bitpit::utils::string::keywordInString( line, "<Piece")){
187 getline(str, line);
188 }
189
190 bitpit::utils::string::getAfterKeyword( line, "Extent", '\"', temp) ;
191 bitpit::utils::string::convertString( temp, extensions );
192
193 m_localIndex[0][0] = extensions[0] ;
194 m_localIndex[0][1] = extensions[1] ;
195 m_localIndex[1][0] = extensions[2] ;
196 m_localIndex[1][1] = extensions[3] ;
197 m_localIndex[2][0] = extensions[4] ;
198 m_localIndex[2][1] = extensions[5] ;
199
200 position = str.tellg() ;
201
202 readDataHeader( str ) ;
203
204 // Read metadata information
205 for( auto &field : m_geometry ){ //int i=0; i<geometry.size(); ++i){
206 str.seekg( position) ;
207 if( ! readDataArray( str, field ) ) {
208#if BITPIT_ENABLE_DEBUG
209 log::cout() <<"Geometry field " << field.getName() << " not found, it will be disabled" << std::endl ;
210#endif
211 field.disable();
212 }
213 }
214
215
216 setDimensions( m_localIndex[0][0], m_localIndex[0][1], m_localIndex[1][0], m_localIndex[1][1], m_localIndex[2][0], m_localIndex[2][1] ) ;
217 str.close() ;
218
219}
220
225
226 std::fstream str;
227
228 str.open( m_fh.getPath( ), std::ios::out ) ;
229 if (!str.is_open()) {
230 throw std::runtime_error("Cannot create file \"" + m_fh.getName() + "\"" + " inside the directory \"" + m_fh.getDirectory() + "\"");
231 }
232
233 //Writing XML header
234 str << "<?xml version=\"1.0\"?>" << std::endl;
235
236 //Writing Piece Information
237 str << "<VTKFile type=\"RectilinearGrid\" version=\"0.1\" byte_order=\"LittleEndian\" header_type=\"" << m_headerType << "\">" << std::endl;
238 str << " <RectilinearGrid WholeExtent= \""
239 << m_globalIndex[0][0] << " " << m_globalIndex[0][1]<< " "
240 << m_globalIndex[1][0] << " " << m_globalIndex[1][1]<< " "
241 << m_globalIndex[2][0] << " " << m_globalIndex[2][1]<< " "
242 << "\" >" << std::endl;
243
244 str << " <Piece Extent= \" "
245 << m_localIndex[0][0] << " " << m_localIndex[0][1]<< " "
246 << m_localIndex[1][0] << " " << m_localIndex[1][1]<< " "
247 << m_localIndex[2][0] << " " << m_localIndex[2][1]<< " "
248 << "\" >" << std::endl;
249
250
251
252 //Header for Data
253 writeDataHeader( str, false ) ;
254
255 //Wring Geometry Information
256 str << " <Coordinates>" << std::endl;
257 writeDataArray( str, m_geometry[0] ) ;
258 writeDataArray( str, m_geometry[1] ) ;
259 writeDataArray( str, m_geometry[2] ) ;
260 str << " </Coordinates>" << std::endl;
261
262 //Closing Piece
263 str << " </Piece>" << std::endl;
264 str << " </RectilinearGrid>" << std::endl;
265
266 //Write Appended Section
267 if (isAppendedActive()) {
268 str << " <AppendedData encoding=\"raw\">" << std::endl;
269 str << "_" << std::endl;
270 }
271
272 //Closing XML
273 str << "</VTKFile>" << std::endl;
274
275 str.close() ;
276
277}
278
285void VTKRectilinearGrid::writeCollection( const std::string &outputName, const std::string &collectionName ) const {
286
287 // Only one process in a parallel output should write the collection
288 if (m_procs <= 1 || m_rank != 0) {
289 return;
290 }
291
292 // Create file handler
293 FileHandler fhp = createCollectionHandler(collectionName) ;
294
295 // Initialize outout stream
296 std::fstream str ;
297 str.open( fhp.getPath( ), std::ios::out ) ;
298 if (!str.is_open()) {
299 throw std::runtime_error("Cannot create file \"" + fhp.getName() + "\"" + " inside the directory \"" + fhp.getDirectory() + "\"");
300 }
301
302 //Writing XML header
303 str << "<?xml version=\"1.0\"?>" << std::endl;
304
305 //Writing Piece Information
306 str << "<VTKFile type=\"PRectilinearGrid\" version=\"0.1\" byte_order=\"LittleEndian\">" << std::endl;
307 str << " <PRectilinearGrid WholeExtent= \""
308 << m_globalIndex[0][0] << " " << m_globalIndex[0][1]<< " "
309 << m_globalIndex[1][0] << " " << m_globalIndex[1][1]<< " "
310 << m_globalIndex[2][0] << " " << m_globalIndex[2][1]<< " "
311 << "GhostLevel=\"0\">" << std::endl;
312
313
314
315 //Header for Data
316 writeDataHeader( str, true );
317
318 //Wring Geometry Information
319 str << " <PCoordinates>" << std::endl;
320 writePDataArray( str, m_geometry[0] ) ;
321 writePDataArray( str, m_geometry[1] ) ;
322 writePDataArray( str, m_geometry[2] ) ;
323 str << " </PCoordinates>" << std::endl;
324
325
326 FileHandler fho(m_fh) ;
327 fho.setSeries(false) ;
328 fho.setDirectory(".") ;
329 fho.setName(outputName) ;
330 for( int i=0; i<m_procs; i++){
331 fho.setBlock(i) ;
332 const extension3D_t &index = m_procIndex[i] ;
333
334 str << " <Piece Extent= \" "
335 << index[0][0] << " " << index[0][1] << " "
336 << index[1][0] << " " << index[1][1] << " "
337 << index[2][0] << " " << index[2][1] << " "
338 << "\" Source= \"" << fho.getPath() << "\"/>" << std::endl;
339 }
340
341 str << " </PRectilinearGrid>" << std::endl;
342 str << "</VTKFile>" << std::endl;
343
344 str.close() ;
345
346}
347
357void VTKRectilinearGrid::setDimensions( int n1, int n2, int m1, int m2, int l1, int l2 ){
358
359 m_dimensions = ( l1 == l2 ) ? 2 :3 ;
360
361 m_localIndex[0][0] = n1 ;
362 m_localIndex[0][1] = n2 ;
363 m_localIndex[1][0] = m1 ;
364 m_localIndex[1][1] = m2 ;
365 m_localIndex[2][0] = l1 ;
366 m_localIndex[2][1] = l2 ;
367
368 if(m_procs==1)
370
371 m_cells = 1 ;
372 m_points = 1 ;
373
374 for(int d=0; d<m_dimensions; ++d){
375 m_cells = m_cells * ( m_localIndex[d][1] -m_localIndex[d][0] ) ;
376 m_points = m_points * ( m_localIndex[d][1] -m_localIndex[d][0] +1 ) ;
377 }
378
379}
380
387void VTKRectilinearGrid::setDimensions( int n, int m, int l ){
388
389 this->setDimensions( 0, n-1, 0, m-1, 0, l-1 );
390
391}
392
400void VTKRectilinearGrid::setDimensions( int n1, int n2, int m1, int m2 ){
401
402 this->setDimensions( n1, n2, m1, m2, 0, 0 );
403
404}
405
412
413 this->setDimensions( 0, n-1, 0, m-1, 0, 0 );
414
415}
416
424void VTKRectilinearGrid::setGlobalDimensions( int I, int J, int K ){
425
426 m_globalIndex[0][0] = 0;
427 m_globalIndex[0][1] = I;
428 m_globalIndex[1][0] = 0;
429 m_globalIndex[1][1] = J;
430 m_globalIndex[2][0] = 0;
431 m_globalIndex[2][1] = K;
432
433}
434
441
442 int index = static_cast<int>(fieldEnum) ;
443 VTKField& field = m_geometry[index] ;
444
445 field.setStreamer( *streamer ) ;
446
447}
448
456
457 m_globalIndex[0][0] = 0;
458 m_globalIndex[0][1] = I;
459 m_globalIndex[1][0] = 0;
460 m_globalIndex[1][1] = J;
461 m_globalIndex[2][0] = 0;
462 m_globalIndex[2][1] = 0;
463
464}
465
471void VTKRectilinearGrid::setGlobalIndex( const std::vector<extension3D_t> &loc ){
472
473 if( loc.size() != m_procs )
474 log::cout() << "Size of loc_ in VTKRectilinearGrid::setParallelIndex does not fit m_procs " << std::endl ;
475
476 m_procIndex = loc ;
477
478}
479
485void VTKRectilinearGrid::setGlobalIndex( const std::vector<extension2D_t> &loc ){
486
487 if( loc.size() !=m_procs ) log::cout() << "Size of loc_ in VTKRectilinearGrid::setParallelIndex does not fit m_procs " << std::endl ;
488
489 m_procIndex.resize(m_procs) ;
490
491 for( int i=0; i< m_procs; ++i){
492 m_procIndex[i][0] = loc[i][0] ;
493 m_procIndex[i][1] = loc[i][1] ;
494 m_procIndex[i][2] = {{0,0}} ;
495 }
496
497}
498
504uint64_t VTKRectilinearGrid::calcFieldSize( const VTKField &field ) const {
505
506 uint64_t bytes = calcFieldEntries(field) ;
507 bytes *= VTKTypes::sizeOfType( field.getDataType() ) ;
508
509 return bytes ;
510
511}
512
518uint64_t VTKRectilinearGrid::calcFieldEntries( const VTKField &field ) const {
519
520 uint64_t entries(0) ;
521 const std::string &name = field.getName() ;
522
523 if( name == "x_Coord" ){
524 entries = m_localIndex[0][1] -m_localIndex[0][0] +1 ;
525
526 } else if( name == "y_Coord" ){
527 entries = m_localIndex[1][1] -m_localIndex[1][0] +1 ;
528
529 } else if( name == "z_Coord" ){
530 entries = m_localIndex[2][1] -m_localIndex[2][0] +1 ;
531
532 } else{
533
534 VTKLocation location( field.getLocation() ) ;
535 assert( location != VTKLocation::UNDEFINED) ;
536
537 if( location == VTKLocation::CELL ){
538 entries = m_cells ;
539
540 } else if( location == VTKLocation::POINT ){
541 entries = m_points ;
542
543 }
544
545 entries *= field.getComponentCount() ;
546
547 }
548
549 return entries ;
550
551}
552
559
560 uint8_t comp ;
561 const std::string &name = field.getName() ;
562
563 if( name == "x_Coord" || name == "y_Cooord" || name == "z_Coord" ){
564 comp = 1 ;
565
566 } else{
567 comp = field.getComponentCount();
568
569 }
570
571 return comp ;
572
573}
574
581 return "vtr";
582}
583
584}
Creates file names and checks status.
std::string getPath() const
const std::string & getDirectory() const
void setDirectory(const std::string &d_)
void setName(const std::string &n_)
const std::string & getName() const
void setBlock(int b_)
void setSeries(bool s_)
void setAppendix(const std::string &a_)
std::string getName() const
Definition logger.cpp:842
The base class to be used to derive VTK streamers form.
Definition VTK.hpp:209
VTKField handles geometry and data field information for the VTK format.
Definition VTK.hpp:247
const std::string & getName() const
Definition VTKField.cpp:167
VTKLocation getLocation() const
Definition VTKField.cpp:200
void setStreamer(VTKBaseStreamer &)
Definition VTKField.cpp:145
static unsigned getComponentCount(VTKFieldType fieldType)
Definition VTKField.cpp:40
VTKDataType getDataType() const
Definition VTKField.cpp:192
VTK input output for Rectilinear Meshes.
Definition VTK.hpp:495
std::vector< extension3D_t > m_procIndex
Definition VTK.hpp:504
void setGlobalDimensions(int, int, int)
void setDimensions(int, int, int, int, int, int)
uint8_t calcFieldComponents(const VTKField &) const override
extension3D_t m_globalIndex
Definition VTK.hpp:503
void setGeomData(VTKRectilinearField, std::vector< T > &)
Definition VTK.tpp:131
std::string getExtension() const override
void writeMetaInformation() const override
extension3D_t m_localIndex
Definition VTK.hpp:502
uint64_t calcFieldSize(const VTKField &) const override
void setGlobalIndex(const std::vector< extension3D_t > &)
uint64_t calcFieldEntries(const VTKField &) const override
void readMetaInformation() override
static uint8_t sizeOfType(VTKDataType type)
Definition VTKTypes.cpp:67
A base class for VTK input output.
Definition VTK.hpp:298
uint16_t m_rank
Definition VTK.hpp:305
uint64_t m_cells
Definition VTK.hpp:303
std::vector< VTKField > m_geometry
Definition VTK.hpp:309
void readDataHeader(std::fstream &)
Definition VTK.cpp:1370
void setNames(const std::string &, const std::string &)
Definition VTK.cpp:108
FileHandler m_fh
Definition VTK.hpp:301
void setHeaderType(const std::string &)
Definition VTK.cpp:83
std::string m_headerType
Definition VTK.hpp:307
bool readDataArray(std::fstream &, VTKField &) const
Definition VTK.cpp:1468
void writeCollection() const
Definition VTK.cpp:789
void setGeomCodex(VTKFormat)
Definition VTK.cpp:239
void writePDataArray(std::fstream &, const VTKField &) const
Definition VTK.cpp:1224
uint16_t m_procs
Definition VTK.hpp:304
void writeDataHeader(std::fstream &, bool parallel=false) const
Definition VTK.cpp:1146
bool isAppendedActive() const
Definition VTK.cpp:603
void writeDataArray(std::fstream &, const VTKField &) const
Definition VTK.cpp:1212
FileHandler createCollectionHandler(const std::string &collectionName) const
Definition VTK.cpp:934
uint64_t m_points
Definition VTK.hpp:302
VTKFormat m_geomCodex
Definition VTK.hpp:310
VTKFormat
Definition VTK.hpp:92
VTKLocation
Definition VTK.hpp:102
VTKRectilinearField
Definition VTK.hpp:150
bool keywordInString(const std::string &line, const std::string &key)
void convertString(std::string input, T &output)
bool getAfterKeyword(const std::string &line, const std::string &key, char del, std::string &result)
Logger & cout(log::Level defaultSeverity, log::Visibility defaultVisibility)
Definition logger.cpp:1705
--- layout: doxygen_footer ---