MOAB: Mesh Oriented datABase  (version 5.4.1)
AddFieldtoPG2.cpp
Go to the documentation of this file.
00001 #include "moab/ProgOptions.hpp"
00002 #include "moab/Core.hpp"
00003 #include <iostream>
00004 
00005 using namespace moab;
00006 using namespace std;
00007 
00008 int main( int argc, char* argv[] )
00009 {
00010 
00011     ProgOptions opts;
00012 
00013     std::string inputfile, outfile( "out.h5m" ), physgridfile, variable_name;
00014 
00015     opts.addOpt< std::string >( "input,i", "input mesh filename", &inputfile );
00016     opts.addOpt< std::string >( "output,o", "output mesh filename", &outfile );
00017     opts.addOpt< std::string >( "phys,p", "phys grid solution filename", &physgridfile );
00018     opts.addOpt< std::string >( "var,v", "variable to extract and add to output file", &variable_name );
00019 
00020     opts.parseCommandLine( argc, argv );
00021 
00022     if( inputfile.empty() )
00023     {
00024         opts.printHelp();
00025         return 0;
00026     }
00027     ErrorCode rval;
00028     Core* mb = new Core();
00029 
00030     EntityHandle fset1, fset2;
00031     rval = mb->create_meshset( MESHSET_SET, fset1 );MB_CHK_SET_ERR( rval, "can't create mesh set" );
00032     rval = mb->load_file( inputfile.c_str(), &fset1 );MB_CHK_SET_ERR( rval, "can't load input file" );
00033 
00034     cout << " opened " << inputfile << " with initial h5m data.\n";
00035 
00036     rval = mb->create_meshset( MESHSET_SET, fset2 );MB_CHK_SET_ERR( rval, "can't create mesh set" );
00037     rval = mb->load_file( physgridfile.c_str(), &fset2 );MB_CHK_SET_ERR( rval, "can't load phys grid file" );
00038 
00039     Tag tagv;
00040     rval = mb->tag_get_handle( variable_name.c_str(), tagv );MB_CHK_SET_ERR( rval, "can't get tag handle" );
00041 
00042     Tag gitag = mb->globalId_tag();
00043 
00044     Range verts;  // from phys grid
00045     rval = mb->get_entities_by_dimension( fset2, 0, verts );MB_CHK_SET_ERR( rval, "can't get vertices" );
00046     std::vector< int > gids;
00047     gids.resize( verts.size() );
00048     rval = mb->tag_get_data( gitag, verts, &gids[0] );MB_CHK_SET_ERR( rval, "can't get gi tag values" );
00049     std::vector< double > valsTag;
00050     valsTag.resize( verts.size() );
00051     rval = mb->tag_get_data( tagv, verts, &valsTag[0] );MB_CHK_SET_ERR( rval, "can't get tag vals" );
00052     Range cells;
00053 
00054     rval = mb->get_entities_by_dimension( fset1, 2, cells );MB_CHK_SET_ERR( rval, "can't get cells" );
00055 
00056     std::map< int, double > valsByID;
00057     for( int i = 0; i < (int)gids.size(); i++ )
00058         valsByID[gids[i]] = valsTag[i];
00059 
00060     // set now cells values
00061     std::vector< int > cellsIds;
00062     cellsIds.resize( cells.size() );
00063     rval = mb->tag_get_data( gitag, cells, &cellsIds[0] );MB_CHK_SET_ERR( rval, "can't get cells ids" );
00064     for( int i = 0; i < (int)cells.size(); i++ )
00065     {
00066         valsTag[i] = valsByID[cellsIds[i]];
00067     }
00068     rval = mb->tag_set_data( tagv, cells, &valsTag[0] );MB_CHK_SET_ERR( rval, "can't set  cells tags" );
00069 
00070     rval = mb->write_file( outfile.c_str(), 0, 0, &fset1, 1 );MB_CHK_SET_ERR( rval, "can't write file" );
00071 
00072     return 0;
00073 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines