MOAB: Mesh Oriented datABase  (version 5.2.1)
ReadPartFile.cpp
Go to the documentation of this file.
00001 /** @example ReadPartFile.cpp
00002  *
00003  * read partition file created by a Zoltan process, that used the global ids for
00004  * identification of entities
00005  */
00006 
00007 #include "moab/Core.hpp"
00008 #include <iostream>
00009 #include <fstream>
00010 
00011 using namespace moab;
00012 using namespace std;
00013 
00014 #ifndef MESH_DIR
00015 #define MESH_DIR "."
00016 #endif
00017 
00018 // Note: change the file name below to test a trivial "No such file or directory" error
00019 string test_file_name = string( MESH_DIR ) + string( "/3k-tri-sphere.vtk" );
00020 string part_file_name;
00021 int nparts;
00022 int main( int argc, char** argv )
00023 {
00024     // Get MOAB instance
00025     Interface* mb = new( std::nothrow ) Core;
00026     if( NULL == mb ) return 1;
00027 
00028     // Need option handling here for input filename
00029     if( argc > 4 )
00030     {
00031         // User has input a mesh file
00032         test_file_name = argv[1];
00033         part_file_name = argv[2];
00034         nparts         = atoi( argv[3] );
00035     }
00036     else
00037     {
00038         cerr << " usage is " << argv[0] << " <input file> <part file> <#parts> <output file> \n";
00039         exit( 0 );
00040     }
00041     ifstream inFile;
00042     inFile.open( part_file_name.c_str() );
00043     if( !inFile )
00044     {
00045         cerr << "Unable to open file " << part_file_name << "\n";
00046         exit( 1 );  // call system to stop
00047     }
00048     // Load the mesh from file
00049     ErrorCode rval = mb->load_mesh( test_file_name.c_str() );MB_CHK_ERR( rval );
00050 
00051     // Get sets entities, by type
00052     Range sets;
00053     rval = mb->get_entities_by_type( 0, MBENTITYSET, sets );MB_CHK_ERR( rval );
00054 
00055     // Output the number of sets
00056     cout << "Number of sets is " << sets.size() << endl;
00057     // remove the sets that have a PARALLEL_PARTITION tag
00058 
00059     Tag tag;
00060     rval = mb->tag_get_handle( "PARALLEL_PARTITION", tag );MB_CHK_ERR( rval );
00061 
00062     int i                = 0;
00063     int num_deleted_sets = 0;
00064     for( Range::iterator it = sets.begin(); it != sets.end(); it++, i++ )
00065     {
00066         EntityHandle eh = *it;
00067         // cout << " set :" << mb->id_from_handle(eh) <<"\n";
00068         int val = -1;
00069         rval    = mb->tag_get_data( tag, &eh, 1, &val );
00070         if( val != -1 )
00071         {
00072             num_deleted_sets++;
00073             rval = mb->delete_entities( &eh, 1 );  // delete the set, we will have a new partition soon
00074         }
00075     }
00076     if( num_deleted_sets ) cout << "delete " << num_deleted_sets << " existing  partition sets, and create new ones \n";
00077     Range cells;  // get them by dimension 2!
00078     rval = mb->get_entities_by_dimension( 0, 2, cells );MB_CHK_ERR( rval );
00079     EntityHandle* psets = new EntityHandle[nparts];
00080     for( int i = 0; i < nparts; i++ )
00081     {
00082         rval = mb->create_meshset( MESHSET_SET, psets[i] );MB_CHK_ERR( rval );
00083         rval = mb->tag_set_data( tag, &( psets[i] ), 1, &i );MB_CHK_ERR( rval );
00084     }
00085 
00086     for( Range::iterator it = cells.begin(); it != cells.end(); it++ )
00087     {
00088         int part;
00089         EntityHandle eh = *it;
00090         inFile >> part;
00091         rval = mb->add_entities( psets[part], &eh, 1 );MB_CHK_ERR( rval );
00092     }
00093     mb->write_file( argv[4] );
00094 
00095     delete mb;
00096 
00097     return 0;
00098 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines