Mesh Oriented datABase
(version 5.4.1)
Array-based unstructured mesh datastructure
|
00001 #include "WriteCGNS.hpp" 00002 #include "moab/CN.hpp" 00003 #include "MBTagConventions.hpp" 00004 #include "MBParallelConventions.h" 00005 #include "moab/Interface.hpp" 00006 #include "moab/Range.hpp" 00007 #include "moab/WriteUtilIface.hpp" 00008 #include "moab/FileOptions.hpp" 00009 #include "GmshUtil.hpp" 00010 00011 #include <fstream> 00012 #include <map> 00013 #include <set> 00014 00015 #include <iostream> 00016 00017 namespace moab 00018 { 00019 00020 WriterIface* WriteCGNS::factory( Interface* iface ) 00021 { 00022 return new WriteCGNS( iface ); 00023 } 00024 00025 WriteCGNS::WriteCGNS( Interface* impl ) 00026 : mbImpl( impl ), fileName( NULL ), IndexFile( 0 ), BaseName( NULL ), IndexBase( 0 ), ZoneName( NULL ), 00027 IndexZone( 0 ), IndexSection( 0 ), celldim( 0 ), physdim( 0 ), VrtSize( 0 ), EdgeSize( 0 ), FaceSize( 0 ), 00028 CellSize( 0 ) 00029 { 00030 impl->query_interface( mWriteIface ); 00031 IndexCoord[0] = IndexCoord[1] = IndexCoord[2] = 0; 00032 isize[0] = isize[1] = isize[2] = 0; 00033 } 00034 00035 WriteCGNS::~WriteCGNS() 00036 { 00037 mbImpl->release_interface( mWriteIface ); 00038 } 00039 00040 //! writes out a file 00041 ErrorCode WriteCGNS::write_file( const char* file_name, 00042 const bool overwrite, 00043 const FileOptions& /*options*/, 00044 const EntityHandle* /*output_list*/, 00045 const int /*num_sets*/, 00046 const std::vector< std::string >&, 00047 const Tag*, 00048 int, 00049 int ) 00050 { 00051 ErrorCode rval; 00052 00053 if( !overwrite ) 00054 { 00055 rval = mWriteIface->check_doesnt_exist( file_name ); 00056 if( MB_SUCCESS != rval ) return rval; 00057 } 00058 std::cout << "THE CGNS CONVERSION ONLY WORKS FOR ENTITIES CITED BELOW:"; 00059 std::cout << "\n -MBVERTEX\n -MBEDGE\n -MBTRI\n -MBQUAD"; 00060 std::cout << "\n -MBTET\n -MBPYRAMID\n -MBHEX\n"; 00061 00062 // Get entities to write 00063 // Get and count vertex entities 00064 rval = get_vertex_entities( VrtSize, Nodes ); 00065 if( rval != MB_SUCCESS ) 00066 { 00067 return rval; 00068 } 00069 // Get and count edge entities 00070 rval = get_edge_entities( EdgeSize, Edges ); 00071 if( rval != MB_SUCCESS ) 00072 { 00073 return rval; 00074 } 00075 // Get and count face entities 00076 rval = get_face_entities( FaceSize, Faces ); 00077 if( rval != MB_SUCCESS ) 00078 { 00079 return rval; 00080 } 00081 // Get and count cell entities 00082 rval = get_cell_entities( CellSize, Cells ); 00083 if( rval != MB_SUCCESS ) 00084 { 00085 return rval; 00086 } 00087 std::cout << "\nThe Number of Vertex is " << VrtSize << ".\n"; 00088 std::cout << "The Number of Edges is " << EdgeSize << ".\n"; 00089 std::cout << "The Number of Faces is " << FaceSize << ".\n"; 00090 std::cout << "The Number of Cells is " << CellSize << ".\n\n"; 00091 00092 // save filename to member variable so we don't need to pass as an argument 00093 // to called functions 00094 fileName = file_name; 00095 std::cout << fileName << " file is a " << physdim << "-D mesh.\n"; 00096 00097 // Open file 00098 IndexFile = 0; 00099 00100 // open the cgns file 00101 // filename: (input) Name of the CGNS file, including path name if necessary. There is no 00102 // limit on the 00103 // length of this character variable. 00104 // CG_MODE_WRITE: (input) Mode used for opening the file. The modes currently supported are 00105 // CG_MODE_READ, 00106 // CG_MODE_WRITE, and CG_MODE_MODIFY. 00107 // filePtr: (output) CGNS file index number. 00108 if( cg_open( fileName, CG_MODE_WRITE, &IndexFile ) ) 00109 { 00110 std::cout << "Error opening file\n"; 00111 cg_error_exit(); 00112 } 00113 // Give a base name 00114 BaseName = "Cgns Base"; 00115 if( cg_base_write( IndexFile, BaseName, celldim, physdim, &IndexBase ) ) 00116 { 00117 std::cout << "Error creating CGNS base"; 00118 } 00119 // Give a zone name 00120 ZoneName = "Cgns Zone"; 00121 // isize array contains the total vertex size, cell size, and boundary 00122 // vertex size for the zone 00123 // Note that for unstructured zones, the index dimension is always 1 00124 isize[0] = VrtSize; // isize[0] contains the total vertex size 00125 isize[1] = CellSize; // isize[1] contains the total cell size 00126 isize[2] = 0; // isize[2] = 0 for unsorted elements 00127 // Create zone */ 00128 // ZoneType_t: Unstructured 00129 if( cg_zone_write( IndexFile, IndexBase, ZoneName, isize, Unstructured, &IndexZone ) ) 00130 { 00131 std::cout << "Error creating CGNS zone\n"; 00132 cg_error_exit(); 00133 } 00134 // Write the vertex coordinates 00135 rval = write_coord_cgns( Nodes ); 00136 if( rval != MB_SUCCESS ) 00137 { 00138 return rval; 00139 } 00140 00141 // Create a vector to hold the Tags 00142 std::vector< moab::Tag > TagHandles; 00143 // Get Tags 00144 rval = mbImpl->tag_get_tags( TagHandles ); 00145 if( rval != MB_SUCCESS ) 00146 { 00147 return rval; 00148 } 00149 // Get the number of Tags in the mesh 00150 int NbTags = TagHandles.size(); 00151 std::cout << "\nThe mesh has " << NbTags << " Tags.\n"; 00152 00153 // Create a vector of size NbTags 00154 // Sets have informations about the entity set 00155 std::vector< SetStruct > Sets; 00156 Sets.reserve( NbTags ); 00157 // Fill Sets with all information needed 00158 rval = set_tag_values( TagHandles, Edges, Faces, Cells, Sets ); 00159 if( rval != MB_SUCCESS ) 00160 { 00161 std::cout << "Problem to set tag values\n"; 00162 return rval; 00163 } 00164 00165 // Create a matrix to hold connectivity 00166 std::vector< std::vector< cgsize_t > > ConnTable; 00167 ConnTable.resize( NbTags ); 00168 00169 std::vector< int > Begin( NbTags, 0 ); 00170 std::vector< int > End( NbTags, 0 ); 00171 00172 // Take the connectivity of higher dimension entities 00173 cgsize_t BeginSetsIndex = 1; 00174 cgsize_t EndSetsIndex; 00175 switch( physdim ) 00176 { 00177 case 1: 00178 rval = get_conn_table( Edges, Begin, End, TagHandles, Sets, ConnTable ); 00179 if( rval != MB_SUCCESS ) 00180 { 00181 std::cout << "Problem to fill the connectivity table for 1-D entities\n"; 00182 return rval; 00183 } 00184 for( int i = 0; i < NbTags; ++i ) 00185 { 00186 if( Sets[i].IdSet != -1 ) 00187 { 00188 const char* SectionName = Sets[i].TagName.c_str(); 00189 EndSetsIndex = BeginSetsIndex + Sets[i].NbEdges - 1; 00190 // Write the section in CGNS file 00191 if( cg_section_write( IndexFile, IndexBase, IndexBase, SectionName, Sets[i].CGNSType, 00192 BeginSetsIndex, EndSetsIndex, 0, &ConnTable[i][0], &IndexSection ) ) 00193 { 00194 std::cout << "Issue on writing connectivity - 3-D\n"; 00195 cg_error_exit(); 00196 } 00197 BeginSetsIndex = EndSetsIndex + 1; 00198 } 00199 } 00200 break; 00201 case 2: 00202 rval = get_conn_table( Edges, Begin, End, TagHandles, Sets, ConnTable ); 00203 if( rval != MB_SUCCESS ) 00204 { 00205 std::cout << "Problem to fill the connectivity table for 1-D entities\n"; 00206 return rval; 00207 } 00208 rval = get_conn_table( Faces, Begin, End, TagHandles, Sets, ConnTable ); 00209 if( rval != MB_SUCCESS ) 00210 { 00211 std::cout << "Problem to fill the connectivity table for 2-D entities\n"; 00212 return rval; 00213 } 00214 for( int i = 0; i < NbTags; ++i ) 00215 { 00216 if( Sets[i].IdSet != -1 ) 00217 { 00218 const char* SectionName = Sets[i].TagName.c_str(); 00219 EndSetsIndex = BeginSetsIndex + Sets[i].NbEdges + Sets[i].NbFaces - 1; 00220 // Write the section in CGNS file 00221 if( cg_section_write( IndexFile, IndexBase, IndexBase, SectionName, Sets[i].CGNSType, 00222 BeginSetsIndex, EndSetsIndex, 0, &ConnTable[i][0], &IndexSection ) ) 00223 { 00224 std::cout << "Issue on writing connectivity -- 2-D\n"; 00225 cg_error_exit(); 00226 } 00227 BeginSetsIndex = EndSetsIndex + 1; 00228 } 00229 } 00230 break; 00231 case 3: 00232 rval = get_conn_table( Faces, Begin, End, TagHandles, Sets, ConnTable ); 00233 if( rval != MB_SUCCESS ) 00234 { 00235 std::cout << "Problem to fill the connectivity table for 2-D entities\n"; 00236 return rval; 00237 } 00238 rval = get_conn_table( Cells, Begin, End, TagHandles, Sets, ConnTable ); 00239 if( rval != MB_SUCCESS ) 00240 { 00241 std::cout << "Problem to fill the connectivity table for 3-D entities\n"; 00242 return rval; 00243 } 00244 for( int i = 0; i < NbTags; ++i ) 00245 { 00246 if( Sets[i].IdSet != -1 ) 00247 { 00248 const char* SectionName = Sets[i].TagName.c_str(); 00249 EndSetsIndex = BeginSetsIndex + Sets[i].NbFaces + Sets[i].NbCells - 1; 00250 std::cout << "BeginSetsIndex = " << BeginSetsIndex << "\tEndSetsIndex = " << EndSetsIndex << "\n"; 00251 // Write the section in CGNS file 00252 if( cg_section_write( IndexFile, IndexBase, IndexBase, SectionName, Sets[i].CGNSType, 00253 BeginSetsIndex, EndSetsIndex, 0, &ConnTable[i][0], &IndexSection ) ) 00254 { 00255 std::cout << "Issue on writing connectivity -- 3-D\n"; 00256 cg_error_exit(); 00257 } 00258 BeginSetsIndex = EndSetsIndex + 1; 00259 } 00260 } 00261 break; 00262 default: 00263 std::cout << "Issue on Physical dimension\n"; 00264 return MB_FAILURE; 00265 } 00266 00267 // Close the CGNS mesh file 00268 if( cg_close( IndexFile ) ) 00269 { 00270 std::cout << "Error closing file\n"; 00271 cg_error_exit(); 00272 } 00273 // done 00274 return MB_SUCCESS; 00275 } 00276 00277 // Get and count vertex entities 00278 ErrorCode WriteCGNS::get_vertex_entities( cgsize_t& VrtSize_, std::vector< moab::EntityHandle >& Nodes_ ) 00279 { 00280 ErrorCode rval; 00281 // Get vertex entities 00282 // The first input is 0 because one queries the entire mesh. 00283 // Retrieves all entities of dimension = 0 in "Nodes" 00284 rval = mbImpl->get_entities_by_dimension( 0, 0, Nodes_, false ); 00285 if( Nodes.size() > 0 ) 00286 { 00287 celldim = 0; 00288 physdim = 0; 00289 // get the amout of vertex 00290 VrtSize_ = Nodes_.size(); 00291 } 00292 else 00293 { 00294 std::cout << "The mesh has not node points.\n"; 00295 } 00296 // done 00297 return rval; 00298 } 00299 00300 // Get and count edge entities 00301 ErrorCode WriteCGNS::get_edge_entities( cgsize_t& EdgeSize_, std::vector< moab::EntityHandle >& Edges_ ) 00302 { 00303 ErrorCode rval; 00304 // The first input is 0 because one queries the entire mesh. 00305 // Get all entities of dimension = 1 in Edges 00306 rval = mbImpl->get_entities_by_dimension( 0, 1, Edges_, false ); 00307 if( Edges_.size() > 0 ) 00308 { 00309 celldim = 1; 00310 physdim = 1; 00311 // get the amout of edges 00312 EdgeSize_ = Edges_.size(); 00313 } 00314 // done 00315 return rval; 00316 } 00317 00318 // Get and count face entities 00319 ErrorCode WriteCGNS::get_face_entities( cgsize_t& FaceSize_, std::vector< moab::EntityHandle >& Faces_ ) 00320 { 00321 ErrorCode rval; 00322 // The first input is 0 because one queries the entire mesh. 00323 // Get all entities of dimension = 2 in Faces 00324 rval = mbImpl->get_entities_by_dimension( 0, 2, Faces_, false ); 00325 if( Faces_.size() ) 00326 { 00327 celldim = 2; 00328 physdim = 2; 00329 // get the amout of faces 00330 FaceSize_ = Faces_.size(); 00331 } 00332 // done 00333 return rval; 00334 } 00335 00336 // Get and count cell entities 00337 ErrorCode WriteCGNS::get_cell_entities( cgsize_t& CellSize_, std::vector< moab::EntityHandle >& Cells_ ) 00338 { 00339 ErrorCode rval; 00340 // The first input is 0 because one queries the entire mesh. 00341 // Get all entities of dimension = 3 in Cell 00342 rval = mbImpl->get_entities_by_dimension( 0, 3, Cells_, false ); 00343 if( Cells_.size() ) 00344 { 00345 celldim = 3; 00346 physdim = 3; 00347 // get the amout of volumes 00348 CellSize_ = Cells_.size(); 00349 } 00350 // done 00351 return rval; 00352 } 00353 00354 ErrorCode WriteCGNS::write_coord_cgns( std::vector< moab::EntityHandle >& Nodes_ ) 00355 { 00356 ErrorCode rval; 00357 00358 const int num_entities = (int)Nodes_.size(); 00359 00360 // Moab works with one vector for the threee coordinates 00361 std::vector< double > Coords( 3 * num_entities ); 00362 std::vector< double >::iterator c = Coords.begin(); 00363 00364 // CGNS uses one vector for each coordinate 00365 std::vector< double > CoordX; 00366 std::vector< double > CoordY; 00367 std::vector< double > CoordZ; 00368 00369 // Summ the values of all coordinates to be sure if it is not zero 00370 double SumX = 0; 00371 double SumY = 0; 00372 double SumZ = 0; 00373 00374 // Get the moab coordinates - Coords is the output 00375 rval = mbImpl->get_coords( &Nodes_[0], num_entities, &Coords[0] ); 00376 if( MB_SUCCESS != rval ) 00377 { 00378 std::cout << "Error getting coordinates from nodes.\n"; 00379 return rval; 00380 } 00381 00382 // Reserve the size of nodes 00383 CoordX.reserve( Nodes_.size() ); 00384 CoordY.reserve( Nodes_.size() ); 00385 CoordZ.reserve( Nodes_.size() ); 00386 00387 for( std::vector< moab::EntityHandle >::iterator i = Nodes_.begin(); i != Nodes_.end(); ++i ) 00388 { 00389 CoordX.push_back( *c ); // Put the X coordinate in CoordX vector 00390 SumX += abs( *c ); // Sum all X coordinates 00391 ++c; // Move to Y coordinate 00392 CoordY.push_back( *c ); // Put the Y coordinate in CoordY vector 00393 SumY += abs( *c ); // Sum all Y coordinates 00394 ++c; // Move to Z coordinate 00395 CoordZ.push_back( *c ); // Put the Z coordinate in CoordZ vector 00396 SumZ += abs( *c ); // Sum all Z coordinates 00397 ++c; // Move to X coordinate 00398 } 00399 00400 // If X coordinate is not empty then write CoordX (user must use SIDS-standard names here) 00401 if( SumX != 0 ) 00402 { 00403 if( cg_coord_write( IndexFile, IndexBase, IndexZone, RealDouble, "CoordinateX", &CoordX[0], &IndexCoord[0] ) ) 00404 { 00405 std::cout << " Error writing X coordinates.\n"; 00406 cg_error_exit(); 00407 } 00408 } 00409 // If Y coordinate is not empty then write CoordY (user must use SIDS-standard names here) 00410 if( SumY != 0 ) 00411 { 00412 if( cg_coord_write( IndexFile, IndexBase, IndexZone, RealDouble, "CoordinateY", &CoordY[0], &IndexCoord[1] ) ) 00413 { 00414 std::cout << " Error writing Y coordinates.\n"; 00415 cg_error_exit(); 00416 } 00417 } 00418 // If Z coordinate is not empty then write CoordZ (user must use SIDS-standard names here) 00419 if( SumZ != 0 ) 00420 { 00421 if( cg_coord_write( IndexFile, IndexBase, IndexZone, RealDouble, "CoordinateZ", &CoordZ[0], &IndexCoord[2] ) ) 00422 { 00423 std::cout << " Error writing Z coordinates.\n"; 00424 cg_error_exit(); 00425 } 00426 } 00427 00428 // Clear vectors 00429 Coords.clear(); 00430 CoordX.clear(); 00431 CoordY.clear(); 00432 CoordZ.clear(); 00433 00434 // done 00435 return MB_SUCCESS; 00436 } 00437 00438 ErrorCode WriteCGNS::set_tag_values( std::vector< Tag >& TagHandles, 00439 std::vector< moab::EntityHandle >& Edges_, 00440 std::vector< moab::EntityHandle >& Faces_, 00441 std::vector< moab::EntityHandle >& Cells_, 00442 std::vector< WriteCGNS::SetStruct >& Sets ) 00443 { 00444 ErrorCode rval; 00445 00446 // Get the number of Tags in the mesh 00447 int NbTags = TagHandles.size(); 00448 00449 // Loop over all Tags 00450 for( int i = 0; i < NbTags; ++i ) 00451 { 00452 00453 // Allocate another position in the vector of SetStruct using a default constructor 00454 Sets.push_back( SetStruct() ); 00455 00456 // Get the Tag name 00457 rval = mbImpl->tag_get_name( TagHandles[i], Sets[i].TagName ); 00458 if( rval != MB_SUCCESS ) 00459 { 00460 std::cout << "Problem to get Tag Name\n"; 00461 return rval; 00462 } 00463 std::cout << "Tag name= " << Sets[i].TagName << "\n"; 00464 00465 // Count all entities by type and put in Sets[i].NbEntities vector 00466 rval = get_set_entities( i, TagHandles, Sets ); 00467 if( rval != MB_SUCCESS ) 00468 { 00469 std::cout << "Problem to get Set entities\n"; 00470 return rval; 00471 } 00472 00473 // Get the CGNSTYpe of the Set 00474 rval = get_cgns_type( i, Sets ); 00475 if( rval != MB_SUCCESS ) 00476 { 00477 std::cout << "Problem to get CGNSType\n"; 00478 return rval; 00479 } 00480 std::cout << "\tSets[" << i << "].CGNSType= " << Sets[i].CGNSType << "\n"; 00481 00482 // int Number; 00483 00484 // Set a data index for Edges and TagHandles[i] 00485 if( Sets[i].CGNSType == BAR_2 || Sets[i].CGNSType == TRI_3 || Sets[i].CGNSType == QUAD_4 || 00486 Sets[i].CGNSType == TETRA_4 || Sets[i].CGNSType == PYRA_5 || Sets[i].CGNSType == PENTA_6 || 00487 Sets[i].CGNSType == HEXA_8 || Sets[i].CGNSType == MIXED ) 00488 { 00489 00490 if( Sets[i].NbEdges > 0 && physdim < 3 ) 00491 { 00492 // Set a data index for Edges and TagHandles[i] 00493 const std::vector< int > tag_values( Edges_.size(), i ); 00494 rval = mbImpl->tag_set_data( TagHandles[i], &Edges_[0], Edges_.size(), &tag_values[0] ); 00495 if( rval != MB_SUCCESS ) 00496 { 00497 std::cout << "Problem to set data for 1-D entities\n"; 00498 return rval; 00499 } 00500 } 00501 if( Sets[i].NbFaces > 0 && physdim > 1 ) 00502 { 00503 // Set a data index for Faces and TagHandles[i] 00504 const std::vector< int > tag_values( Faces.size(), i ); 00505 rval = mbImpl->tag_set_data( TagHandles[i], &Faces_[0], Faces_.size(), &tag_values[0] ); 00506 if( rval != MB_SUCCESS ) 00507 { 00508 std::cout << "Problem to set data for 2-D entities\n"; 00509 return rval; 00510 } 00511 } 00512 if( Sets[i].NbCells > 0 && physdim > 2 ) 00513 { 00514 // Set a data index for Cells and TagHandles[i] 00515 const std::vector< int > tag_values( Cells.size(), i ); 00516 rval = mbImpl->tag_set_data( TagHandles[i], &Cells_[0], Cells_.size(), &tag_values[0] ); 00517 if( rval != MB_SUCCESS ) 00518 { 00519 std::cout << "Problem to set data for 3-D entities\n"; 00520 return rval; 00521 } 00522 } 00523 // IdSet gets the Set Index indicating that it is not empty 00524 Sets[i].IdSet = i; 00525 } 00526 std::cout << "\tSets[" << i << "].IdSet = " << Sets[i].IdSet << "\n\n"; 00527 } 00528 return MB_SUCCESS; 00529 } 00530 00531 // Get Entities in the set 00532 ErrorCode WriteCGNS::get_set_entities( int i, 00533 std::vector< Tag >& TagHandles, 00534 std::vector< WriteCGNS::SetStruct >& Sets ) 00535 { 00536 ErrorCode rval; 00537 00538 // Get the number of MBEDGE entities 00539 // NbEntities[0] holds the number of MBEDGE in the "Sets" 00540 int Number = 0; 00541 rval = mbImpl->get_number_entities_by_type_and_tag( 0, MBEDGE, &TagHandles[i], 0, 1, Number ); 00542 if( rval != MB_SUCCESS ) 00543 { 00544 std::cout << "Problem to get the number of entities by type and tag\n"; 00545 return rval; 00546 } 00547 Sets[i].NbEntities.push_back( Number ); // MBEDGE == Sets[i].NbEntities[0] 00548 Sets[i].NbEdges += Number; 00549 std::cout << "\tNumber of MBEDGE = " << Number << "\n"; 00550 00551 // Get the number of MBTRI entities 00552 // NbEntities[1] holds the number of MBTRI in the "Sets" 00553 Number = 0; 00554 rval = mbImpl->get_number_entities_by_type_and_tag( 0, MBTRI, &TagHandles[i], 0, 1, Number ); 00555 if( rval != MB_SUCCESS ) 00556 { 00557 std::cout << "Problem to get the number of entities by type and tag\n"; 00558 return rval; 00559 } 00560 Sets[i].NbEntities.push_back( Number ); // MBTRI == Sets[i].NbEntities[1] 00561 Sets[i].NbFaces += Number; 00562 std::cout << "\tNumber of MBTRI = " << Number << "\n"; 00563 00564 // Get the number of MBQUAD entities 00565 // NbEntities[2] holds the number of MBQUAD in the "Sets" 00566 Number = 0; 00567 rval = mbImpl->get_number_entities_by_type_and_tag( 0, MBQUAD, &TagHandles[i], 0, 1, Number ); 00568 if( rval != MB_SUCCESS ) 00569 { 00570 std::cout << "Problem to get the number of entities by type and tag\n"; 00571 return rval; 00572 } 00573 Sets[i].NbEntities.push_back( Number ); // MBQUAD == Sets[i].NbEntities[2] 00574 Sets[i].NbFaces += Number; 00575 std::cout << "\tNumber of MBQUAD = " << Number << "\n"; 00576 00577 // Get the number of MBTET entities 00578 // NbEntities[3] holds the number of MBTET in the "Sets" 00579 Number = 0; 00580 rval = mbImpl->get_number_entities_by_type_and_tag( 0, MBTET, &TagHandles[i], 0, 1, Number ); 00581 if( rval != MB_SUCCESS ) 00582 { 00583 std::cout << "Problem to get the number of entities by type and tag\n"; 00584 return rval; 00585 } 00586 Sets[i].NbEntities.push_back( Number ); // MBTET == Sets[i].NbEntities[3] 00587 Sets[i].NbCells += Number; 00588 std::cout << "\tNumber of MBTET = " << Number << "\n"; 00589 00590 // Get the number of MBPYRAMID entities 00591 // NbEntities[4] holds the number of MBPYRAMID in the "Sets" 00592 Number = 0; 00593 rval = mbImpl->get_number_entities_by_type_and_tag( 0, MBPYRAMID, &TagHandles[i], 0, 1, Number ); 00594 if( rval != MB_SUCCESS ) 00595 { 00596 std::cout << "Problem to get the number of entities by type and tag\n"; 00597 return rval; 00598 } 00599 Sets[i].NbEntities.push_back( Number ); // MBPYRAMID == Sets[i].NbEntities[4] 00600 Sets[i].NbCells += Number; 00601 std::cout << "\tNumber of MBPYRAMID = " << Number << "\n"; 00602 00603 // Get the number of MBPRISM entities - MBPRISM == PENTA_6 00604 // NbEntities[5] holds the number of MBPRISM in the "Sets" 00605 Number = 0; 00606 rval = mbImpl->get_number_entities_by_type_and_tag( 0, MBPRISM, &TagHandles[i], 0, 1, Number ); 00607 if( rval != MB_SUCCESS ) 00608 { 00609 std::cout << "Problem to get the number of entities by type and tag\n"; 00610 return rval; 00611 } 00612 Sets[i].NbEntities.push_back( Number ); // MBPRISM == Sets[i].NbEntities[5] 00613 Sets[i].NbCells += Number; 00614 std::cout << "\tNumber of MBPRISM = " << Number << "\n"; 00615 00616 // Get the number of MBHEX entities 00617 // NbEntities[6] holds the number of MBHEX in the "Sets" 00618 Number = 0; 00619 rval = mbImpl->get_number_entities_by_type_and_tag( 0, MBHEX, &TagHandles[i], 0, 1, Number ); 00620 if( rval != MB_SUCCESS ) 00621 { 00622 std::cout << "Problem to get the number of entities by type and tag\n"; 00623 return rval; 00624 } 00625 Sets[i].NbEntities.push_back( Number ); // MBHEX == Sets[i].NbEntities[6] 00626 Sets[i].NbCells += Number; 00627 std::cout << "\tNumber of MBHEX = " << Number << "\n"; 00628 00629 std::cout << "\tTotal number of Edges = " << Sets[i].NbEdges << "\n"; 00630 std::cout << "\tTotal number of Faces = " << Sets[i].NbFaces << "\n"; 00631 std::cout << "\tTotal number of Cells = " << Sets[i].NbCells << "\n"; 00632 00633 return MB_SUCCESS; 00634 } 00635 00636 // Get CGNSType 00637 ErrorCode WriteCGNS::get_cgns_type( int i, std::vector< WriteCGNS::SetStruct >& Sets ) 00638 { 00639 std::vector< int > Test; 00640 int Sum = 0; 00641 00642 // NbEntities is a vector which has the number of entities of each type 00643 // 0-MBEDGE | 1-MBTRI | 2-MBQUAD | 3-MBTET | 4-MBPYRAMID | 5-MBPRISM | 6-MBHEX 00644 // if NbEntities[i]>0 then Test[i]=1 00645 // else then Test[i]=0 00646 for( int j = 0; j < (int)Sets[i].NbEntities.size(); ++j ) 00647 { 00648 if( Sets[i].NbEntities[j] > 0 ) 00649 { 00650 Test.push_back( 1 ); 00651 Sum++; 00652 } 00653 else 00654 { 00655 Test.push_back( 0 ); 00656 } 00657 } 00658 00659 // Test the Sum 00660 // if Sum > 1 then the Set is MIXED 00661 // if Sum = 0 then the Set is Homogeneous 00662 // else then the Set is empty 00663 if( Sum > 1 ) 00664 { 00665 Sets[i].CGNSType = MIXED; 00666 } 00667 else if( Sum == 1 ) 00668 { 00669 int j = 0; 00670 std::cout << "Homogeneous Type\n"; 00671 while( j < (int)Sets[i].NbEntities.size() && Sets[i].NbEntities[j] != 1 ) 00672 { 00673 ++j; 00674 } 00675 switch( j ) 00676 { 00677 case 0: 00678 Sets[i].CGNSType = BAR_2; 00679 break; 00680 case 1: 00681 Sets[i].CGNSType = TRI_3; 00682 break; 00683 case 2: 00684 Sets[i].CGNSType = QUAD_4; 00685 break; 00686 case 3: 00687 Sets[i].CGNSType = TETRA_4; 00688 break; 00689 case 4: 00690 Sets[i].CGNSType = PYRA_5; 00691 break; 00692 case 5: 00693 Sets[i].CGNSType = PENTA_6; 00694 break; 00695 case 6: 00696 Sets[i].CGNSType = HEXA_8; 00697 break; 00698 default: 00699 std::cout << "It was not possible to identify the CGNSType\n"; 00700 return MB_FAILURE; 00701 } 00702 } 00703 else 00704 { 00705 Sets[i].CGNSType = ElementTypeNull; 00706 } // NOT SURE IF THAT'S THE RIGHT WAY....... 00707 00708 // Clear the test vector 00709 Test.clear(); 00710 00711 return MB_SUCCESS; 00712 } 00713 00714 // Fill the connectivity table 00715 ErrorCode WriteCGNS::get_conn_table( std::vector< moab::EntityHandle >& Elements, 00716 std::vector< int >& Begin, 00717 std::vector< int >& End, 00718 std::vector< moab::Tag >& TagHandles, 00719 std::vector< WriteCGNS::SetStruct >& Sets, 00720 std::vector< std::vector< cgsize_t > >& ConnTable ) 00721 { 00722 ErrorCode rval; 00723 00724 // int Begin = 0; // GOT TO WORK ON THIS 00725 // int End; 00726 00727 // Get the number of Tags in the mesh 00728 int NbTags = TagHandles.size(); 00729 00730 // Test all Elements, get their ids and connectivity 00731 // to fill ConnTable 00732 for( std::vector< moab::EntityHandle >::iterator i = Elements.begin(); i != Elements.end(); ++i ) 00733 { 00734 int id; 00735 // Test all Tags 00736 for( int j = 0; j < NbTags; ++j ) 00737 { 00738 // Test if the Tag has data 00739 if( Sets[j].IdSet != -1 ) 00740 { 00741 // Try to get data from entity 00742 rval = mbImpl->tag_get_data( TagHandles[j], &*i, 1, &id ); 00743 if( MB_SUCCESS != rval ) 00744 { 00745 return rval; 00746 } 00747 // If successful id==j 00748 if( id == j ) 00749 { 00750 // Get the entity type of the EntityHandle wich points to Cells 00751 int num_vtx; // Number of MeshVertices in array connectivity. 00752 const EntityHandle* conn; // Array in which connectivity of entity_handle is returned. 00753 // Gets a pointer to constant connectivity data of entity_handle 00754 rval = mbImpl->get_connectivity( *i, conn, num_vtx ); 00755 if( MB_SUCCESS != rval ) 00756 { 00757 return rval; 00758 } 00759 // If the Set is MIXED type 00760 // push CGNS ENUM type of the entity 00761 // before the connectivity 00762 if( Sets[j].CGNSType == MIXED ) 00763 { 00764 ConnTable[j].push_back( moab_cgns_conv( *i ) ); // moab_cgns_conv return an int which 00765 // represents the CGNS type 00766 Begin[j]++; 00767 } 00768 End[j] = Begin[j] + num_vtx; 00769 // Push conn in ConnTable in which "j" is the Set Index 00770 for( int k = Begin[j]; k < End[j]; ++k ) 00771 { 00772 ConnTable[j].push_back( (cgsize_t)conn[k - Begin[j]] ); 00773 } 00774 Begin[j] = End[j]; 00775 } 00776 } 00777 } 00778 } 00779 return MB_SUCCESS; 00780 } 00781 00782 // Read the Moab type and return CGNS type 00783 int WriteCGNS::moab_cgns_conv( const EntityHandle handle ) 00784 { 00785 EntityType MoabType = mbImpl->type_from_handle( handle ); 00786 switch( MoabType ) 00787 { 00788 case MBEDGE: /**< Mesh Edge */ 00789 return BAR_2; 00790 case MBTRI: /**< Triangular element (including shells) */ 00791 return TRI_3; 00792 case MBQUAD: /**< Quadrilateral element (including shells) */ 00793 return QUAD_4; 00794 case MBTET: /**< Tetrahedral element */ 00795 return TETRA_4; 00796 case MBPYRAMID: /**< Pyramid element */ 00797 return PYRA_5; 00798 case MBPRISM: /**< Wedge element */ 00799 return PENTA_6; 00800 case MBHEX: /**< Hexahedral element */ 00801 return HEXA_8; 00802 default: 00803 std::cout << "It was not possible to identify the CGNSType\n"; 00804 return 0; 00805 } 00806 } 00807 00808 } // namespace moab