MOAB: Mesh Oriented datABase  (version 5.2.1)
gsets.cpp
Go to the documentation of this file.
00001 #include <iostream>
00002 #include <cstdlib>
00003 #include "moab/Range.hpp"
00004 #include "moab/Core.hpp"
00005 #include "MBTagConventions.hpp"
00006 #include "moab/GeomTopoTool.hpp"
00007 
00008 using namespace moab;
00009 
00010 Tag geomTag        = 0;
00011 Tag blockTag       = 0;
00012 Tag sideTag        = 0;
00013 Tag nodeTag        = 0;
00014 Tag nameTag        = 0;
00015 Tag idTag          = 0;
00016 bool printAnonSets = false;
00017 bool printSVSense  = false;
00018 
00019 Core mb;
00020 GeomTopoTool geomTool( &mb );
00021 
00022 static void usage( const char* name, bool brief = true )
00023 {
00024     std::ostream& str = brief ? std::cerr : std::cout;
00025     if( !brief )
00026         str << name << ": A tool to export entity set parent/child relations" << std::endl
00027             << "      for use as input to graphviz" << std::endl;
00028     str << "Usage: " << name << " [-a | [-g] [-m] [-n] ] <input_file>" << std::endl
00029         << "       " << name << " -h" << std::endl;
00030     if( brief ) exit( 1 );
00031     str << "  The default behavior is equivalent to \"-gmn\"." << std::endl
00032         << "  If any of the following options are used to specify which " << std::endl
00033         << "  sets to output, then there are no defaults.  Only the " << std::endl
00034         << "  indicated sets will be output." << std::endl
00035         << "  -a  : write all sets (default is only geom, mesh, and named)" << std::endl
00036         << "  -g  : write geometric topology sets" << std::endl
00037         << "  -m  : write material sets and boundary condition sets" << std::endl
00038         << "  -n  : write named sets" << std::endl
00039         << "  -s  : label surface-volume links with sense" << std::endl
00040         << "  The default link behavior is to both child links" << std::endl
00041         << "  and containment with solid lines." << std::endl
00042         << "  -P  : do not write child links" << std::endl
00043         << "  -p  : write child links with dashed lines" << std::endl
00044         << "  -C  : do not write containment links" << std::endl
00045         << "  -c  : write containment links with dashed lines" << std::endl;
00046     exit( 0 );
00047 }
00048 
00049 enum Link
00050 {
00051     NONE = 0,
00052     SOLID,
00053     DASHED
00054 };
00055 static void write_dot( Link contained, Link children );
00056 static void dot_nodes( std::ostream& s, Range& sets_out );
00057 static void dot_children( std::ostream& s, const Range& sets, bool dashed );
00058 static void dot_contained( std::ostream& s, const Range& sets, bool dashed );
00059 
00060 int main( int argc, char* argv[] )
00061 {
00062     Link children = SOLID, contained = SOLID;
00063     bool printGeomSets     = true;
00064     bool printMeshSets     = true;
00065     bool printNamedSets    = true;
00066     const char* input_file = 0;
00067     bool geom_flag = false, mesh_flag = false, name_flag = false, all_flag = false;
00068     bool no_more_flags = false;
00069     for( int i = 1; i < argc; ++i )
00070     {
00071         if( no_more_flags || argv[i][0] != '-' )
00072         {
00073             if( input_file ) usage( argv[0] );
00074             input_file = argv[i];
00075             continue;
00076         }
00077         for( int j = 1; argv[i][j]; ++j )
00078         {
00079             switch( argv[i][j] )
00080             {
00081                 case 'a':
00082                     all_flag = true;
00083                     break;
00084                 case 'g':
00085                     geom_flag = true;
00086                     break;
00087                 case 'm':
00088                     mesh_flag = true;
00089                     break;
00090                 case 'n':
00091                     name_flag = true;
00092                     break;
00093                 case 's':
00094                     printSVSense = true;
00095                     break;
00096                 case 'P':
00097                     children = NONE;
00098                     break;
00099                 case 'p':
00100                     children = DASHED;
00101                     break;
00102                 case 'C':
00103                     contained = NONE;
00104                     break;
00105                 case 'c':
00106                     contained = DASHED;
00107                     break;
00108                 case '-':
00109                     no_more_flags = true;
00110                     break;
00111                 case 'h':
00112                     usage( argv[0], false );
00113                 default:
00114                     std::cerr << "Unknown flag: '" << argv[i][j] << "'" << std::endl;
00115                     usage( argv[0] );
00116             }
00117         }
00118     }
00119 
00120     if( !input_file )
00121     {
00122         std::cerr << "No input file specified." << std::endl;
00123         usage( argv[0] );
00124     }
00125 
00126     if( all_flag ) { printGeomSets = printMeshSets = printNamedSets = printAnonSets = true; }
00127     else if( geom_flag || mesh_flag || name_flag )
00128     {
00129         printGeomSets  = geom_flag;
00130         printMeshSets  = mesh_flag;
00131         printNamedSets = name_flag;
00132     }
00133 
00134     if( MB_SUCCESS != mb.load_mesh( input_file ) )
00135     {
00136         std::cerr << input_file << ": file read failed." << std::endl;
00137         return 1;
00138     }
00139 
00140     Tag t;
00141     if( printGeomSets )
00142     {
00143         if( MB_SUCCESS == mb.tag_get_handle( GEOM_DIMENSION_TAG_NAME, 1, MB_TYPE_INTEGER, t ) ) { geomTag = t; }
00144     }
00145     if( printMeshSets )
00146     {
00147         if( MB_SUCCESS == mb.tag_get_handle( MATERIAL_SET_TAG_NAME, 1, MB_TYPE_INTEGER, t ) ) { blockTag = t; }
00148         if( MB_SUCCESS == mb.tag_get_handle( DIRICHLET_SET_TAG_NAME, 1, MB_TYPE_INTEGER, t ) ) { nodeTag = t; }
00149         if( MB_SUCCESS == mb.tag_get_handle( NEUMANN_SET_TAG_NAME, 1, MB_TYPE_INTEGER, t ) ) { sideTag = t; }
00150     }
00151     if( printNamedSets )
00152     {
00153         if( MB_SUCCESS == mb.tag_get_handle( NAME_TAG_NAME, NAME_TAG_SIZE, MB_TYPE_OPAQUE, t ) ) { nameTag = t; }
00154     }
00155     idTag = mb.globalId_tag();
00156 
00157     write_dot( contained, children );
00158     return 0;
00159 }
00160 
00161 void write_dot( Link contained, Link children )
00162 {
00163     Range sets;
00164     std::cout << "digraph {" << std::endl;
00165     dot_nodes( std::cout, sets );
00166     std::cout << std::endl;
00167     if( contained ) dot_contained( std::cout, sets, contained == DASHED );
00168     if( children ) dot_children( std::cout, sets, children == DASHED );
00169     std::cout << "}" << std::endl;
00170 }
00171 
00172 static void dot_get_sets( Range& curr_sets, Range& result_sets, Tag tag, void* tag_val = 0 )
00173 {
00174     if( !tag ) return;
00175 
00176     result_sets.clear();
00177     mb.get_entities_by_type_and_tag( 0, MBENTITYSET, &tag, &tag_val, 1, result_sets );
00178     result_sets = subtract( result_sets, curr_sets );
00179     curr_sets.merge( result_sets );
00180 }
00181 
00182 static void dot_write_node( std::ostream& s, EntityHandle h, const char* label, int* id = 0 )
00183 {
00184     s << 's' << mb.id_from_handle( h ) << " [label = \"" << label;
00185     if( id ) s << ' ' << *id;
00186     s << "\"];" << std::endl;
00187 }
00188 
00189 static void dot_write_id_nodes( std::ostream& s, const Range& entites, Tag id_tag, const char* type_name )
00190 {
00191     int id;
00192     for( Range::iterator i = entites.begin(); i != entites.end(); ++i )
00193         if( MB_SUCCESS == mb.tag_get_data( id_tag, &*i, 1, &id ) ) dot_write_node( s, *i, type_name, &id );
00194 }
00195 
00196 void dot_nodes( std::ostream& s, Range& sets )
00197 {
00198     Range vol_sets, surf_sets, curv_sets, vert_sets;
00199     Range block_sets, side_sets, node_sets;
00200     Range named_sets, other_sets;
00201 
00202     dot_get_sets( sets, named_sets, nameTag );
00203 
00204     int dim = 3;
00205     dot_get_sets( sets, vol_sets, geomTag, &dim );
00206     dim = 2;
00207     dot_get_sets( sets, surf_sets, geomTag, &dim );
00208     dim = 1;
00209     dot_get_sets( sets, curv_sets, geomTag, &dim );
00210     dim = 0;
00211     dot_get_sets( sets, vert_sets, geomTag, &dim );
00212 
00213     dot_get_sets( sets, block_sets, blockTag );
00214     dot_get_sets( sets, side_sets, sideTag );
00215     dot_get_sets( sets, node_sets, nodeTag );
00216 
00217     if( printAnonSets )
00218     {
00219         mb.get_entities_by_type( 0, MBENTITYSET, other_sets );
00220         Range xsect = subtract( other_sets, sets );
00221         sets.swap( other_sets );
00222         other_sets.swap( xsect );
00223     }
00224 
00225     dot_write_id_nodes( s, vol_sets, idTag, "Volume" );
00226     dot_write_id_nodes( s, surf_sets, idTag, "Surface" );
00227     dot_write_id_nodes( s, curv_sets, idTag, "Curve" );
00228     dot_write_id_nodes( s, vert_sets, idTag, "Vertex" );
00229     dot_write_id_nodes( s, block_sets, blockTag, "Block" );
00230     dot_write_id_nodes( s, side_sets, sideTag, "Neumann Set" );
00231     dot_write_id_nodes( s, node_sets, nodeTag, "Dirichlet Set" );
00232 
00233     Range::iterator i;
00234     char name[NAME_TAG_SIZE + 1];
00235     for( i = named_sets.begin(); i != named_sets.end(); ++i )
00236     {
00237         if( MB_SUCCESS == mb.tag_get_data( nameTag, &*i, 1, name ) )
00238         {
00239             name[NAME_TAG_SIZE] = '\0';
00240             dot_write_node( s, *i, name );
00241         }
00242     }
00243     for( i = other_sets.begin(); i != other_sets.end(); ++i )
00244     {
00245         int id = mb.id_from_handle( *i );
00246         dot_write_node( s, *i, "EntitySet ", &id );
00247     }
00248 }
00249 
00250 static void dot_down_link( std::ostream& s, EntityHandle parent, EntityHandle child, bool dashed,
00251                            const char* label = 0 )
00252 {
00253     s << 's' << mb.id_from_handle( parent ) << " -> " << 's' << mb.id_from_handle( child );
00254     if( dashed && label )
00255         s << " [style = dashed label = \"" << label << "\"]";
00256     else if( dashed )
00257         s << " [style = dashed]";
00258     else if( label )
00259         s << " [label = \"" << label << "\"]";
00260     s << ';' << std::endl;
00261 }
00262 
00263 void dot_children( std::ostream& s, const Range& sets, bool dashed )
00264 {
00265     int sense;
00266     const char *fstr = "forward", *rstr = "reverse";
00267     for( Range::iterator i = sets.begin(); i != sets.end(); ++i )
00268     {
00269         Range parents;
00270         mb.get_parent_meshsets( *i, parents );
00271         parents = intersect( parents, sets );
00272 
00273         for( Range::iterator j = parents.begin(); j != parents.end(); ++j )
00274         {
00275             const char* linklabel = 0;
00276             if( printSVSense && MB_SUCCESS == geomTool.get_sense( *i, *j, sense ) )
00277 
00278                 // check here only one sense, as before...
00279                 linklabel = ( sense == (int)SENSE_FORWARD ) ? fstr : rstr;
00280 
00281             dot_down_link( s, *j, *i, dashed, linklabel );
00282         }
00283     }
00284 }
00285 
00286 void dot_contained( std::ostream& s, const Range& sets, bool dashed )
00287 {
00288     for( Range::iterator i = sets.begin(); i != sets.end(); ++i )
00289     {
00290         Range contained;
00291         mb.get_entities_by_type( *i, MBENTITYSET, contained );
00292         contained = intersect( contained, sets );
00293 
00294         for( Range::iterator j = contained.begin(); j != contained.end(); ++j )
00295             dot_down_link( s, *i, *j, dashed );
00296     }
00297 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines