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