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