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