MOAB: Mesh Oriented datABase  (version 5.2.1)
copyPartition.cpp
Go to the documentation of this file.
00001 /** @example copyPartition.cpp  copy partition info on a mesh file from a point cloud
00002  * this tool will take an existing h5m  phys grid partition file (point cloud) and copy the
00003  * partition information on a pg2 mesh file, for better viewing with VisIt
00004  *
00005  * example of usage:
00006  * ./copyPartition -p AtmPhys.h5m -g wholeATM_PG2.h5m -o atm_PG2_part.h5m
00007  *
00008  * the *PG2" style atm mesh is available in E3SM only for pg2 runs, something like
00009  *  --res ne30pg2_r05_oECv3_ICG --compset A_WCYCL1850S_CMIP6
00010  *  or
00011  *  --res ne4pg2_ne4pg2 --compset FC5AV1C-L
00012  */
00013 #include "moab/ProgOptions.hpp"
00014 #include "moab/Core.hpp"
00015 
00016 #include <math.h>
00017 #include <sstream>
00018 
00019 using namespace moab;
00020 
00021 int main( int argc, char* argv[] )
00022 {
00023 
00024     ProgOptions opts;
00025 
00026     std::string physfile, pg2file, outfile;
00027 
00028     opts.addOpt< std::string >( "physgridfile,p", "phys grid filename", &physfile );
00029     opts.addOpt< std::string >( "pg2file,g", "pg2 mesh file", &pg2file );
00030     opts.addOpt< std::string >( "output,o", "output mesh filename", &outfile );
00031 
00032     opts.parseCommandLine( argc, argv );
00033 
00034 
00035     std::cout << "phys grid cloud file: " << physfile << "\n";
00036     std::cout << "pg2 mesh file: " << pg2file << "\n";
00037     std::cout << "output file: " << outfile << "\n";
00038 
00039     if( physfile.empty() )
00040     {
00041         opts.printHelp();
00042         return 0;
00043     }
00044     ErrorCode rval;
00045     Core* mb = new Core();
00046 
00047     rval = mb->load_file( physfile.c_str() );MB_CHK_SET_ERR( rval, "can't load phys grid file" );
00048 
00049     Core* mb2 = new Core();
00050     rval      = mb2->load_file( pg2file.c_str() );MB_CHK_SET_ERR( rval, "can't load pg2 mesh file" );
00051 
00052     Tag globalIDTag1 = mb->globalId_tag();
00053     Tag parti;
00054     rval = mb->tag_get_handle( "partition", parti );MB_CHK_SET_ERR( rval, "can't get partition tag phys grid mesh " );
00055 
00056     Tag globalIDTag2 = mb2->globalId_tag();
00057 
00058     Range verts1;
00059     rval = mb->get_entities_by_dimension( 0, 0, verts1 );MB_CHK_SET_ERR( rval, "can't get vertices " );
00060 
00061     std::vector< int > partValues;
00062     partValues.resize( verts1.size() );
00063     rval = mb->tag_get_data( parti, verts1, &partValues[0] );MB_CHK_SET_ERR( rval, "can't get parts values on vertices " );
00064 
00065     Range cells;
00066     rval = mb2->get_entities_by_dimension( 0, 2, cells );MB_CHK_SET_ERR( rval, "can't get 2d cells " );
00067     std::vector< int > globalIdsCells;
00068     globalIdsCells.resize( cells.size() );
00069     rval = mb2->tag_get_data( globalIDTag2, cells, &globalIdsCells[0] );MB_CHK_SET_ERR( rval, "can't get global ids cells " );
00070 
00071     std::vector< int > globalIdsVerts;
00072     globalIdsVerts.resize( verts1.size() );
00073     rval = mb->tag_get_data( globalIDTag1, verts1, &globalIdsVerts[0] );MB_CHK_SET_ERR( rval, "can't get global ids cells " );
00074 
00075     Tag partTag;
00076     rval = mb2->tag_get_handle( "PARALLEL_PARTITION", partTag );MB_CHK_SET_ERR( rval, "can't partition tag " );
00077 
00078     Range sets;
00079     rval = mb2->get_entities_by_type_and_tag( 0, MBENTITYSET, &partTag, NULL, 1, sets );MB_CHK_ERR( rval );
00080 
00081     std::vector< int > setValues;
00082     setValues.resize( sets.size() );
00083     rval = mb2->tag_get_data( partTag, sets, &setValues[0] );MB_CHK_ERR( rval );
00084 
00085     std::map< int, EntityHandle > valToSet;
00086     int i = 0;
00087     for( Range::iterator st = sets.begin(); st != sets.end(); ++st, i++ )
00088     {
00089         valToSet[setValues[i]] = *st;
00090     }
00091     // now, every cell will be put into one set, by looking at the global id of cell
00092 
00093     std::map< int, EntityHandle > gidToCell;
00094     i = 0;
00095     for( Range::iterator it = cells.begin(); it != cells.end(); ++it, i++ )
00096     {
00097         gidToCell[globalIdsCells[i]] = *it;
00098     }
00099     // empty all sets
00100     rval = mb2->clear_meshset( sets );MB_CHK_ERR( rval );
00101 
00102     // look now at parti values for vertices, and their global ids
00103     for( i = 0; i < (int)verts1.size(); i++ )
00104     {
00105         int part          = partValues[i];
00106         int gid           = globalIdsVerts[i];
00107         EntityHandle set1 = valToSet[part];
00108         EntityHandle cell = gidToCell[gid];
00109         rval              = mb2->add_entities( set1, &cell, 1 );MB_CHK_ERR( rval );
00110     }
00111 
00112     rval = mb2->write_file( outfile.c_str() );MB_CHK_SET_ERR( rval, "can't write file" );
00113 
00114     delete mb;
00115     delete mb2;
00116 
00117     return 0;
00118 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines