Mesh Oriented datABase  (version 5.4.1)
Array-based unstructured mesh datastructure
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                     break;
00114                 default:
00115                     std::cerr << "Unknown flag: '" << argv[i][j] << "'" << std::endl;
00116                     usage( argv[0] );
00117             }
00118         }
00119     }
00120 
00121     if( !input_file )
00122     {
00123         std::cerr << "No input file specified." << std::endl;
00124         usage( argv[0] );
00125     }
00126 
00127     if( all_flag )
00128     {
00129         printGeomSets = printMeshSets = printNamedSets = printAnonSets = true;
00130     }
00131     else if( geom_flag || mesh_flag || name_flag )
00132     {
00133         printGeomSets  = geom_flag;
00134         printMeshSets  = mesh_flag;
00135         printNamedSets = name_flag;
00136     }
00137 
00138     if( MB_SUCCESS != mb.load_mesh( input_file ) )
00139     {
00140         std::cerr << input_file << ": file read failed." << std::endl;
00141         return 1;
00142     }
00143 
00144     Tag t;
00145     if( printGeomSets )
00146     {
00147         if( MB_SUCCESS == mb.tag_get_handle( GEOM_DIMENSION_TAG_NAME, 1, MB_TYPE_INTEGER, t ) )
00148         {
00149             geomTag = t;
00150         }
00151     }
00152     if( printMeshSets )
00153     {
00154         if( MB_SUCCESS == mb.tag_get_handle( MATERIAL_SET_TAG_NAME, 1, MB_TYPE_INTEGER, t ) )
00155         {
00156             blockTag = t;
00157         }
00158         if( MB_SUCCESS == mb.tag_get_handle( DIRICHLET_SET_TAG_NAME, 1, MB_TYPE_INTEGER, t ) )
00159         {
00160             nodeTag = t;
00161         }
00162         if( MB_SUCCESS == mb.tag_get_handle( NEUMANN_SET_TAG_NAME, 1, MB_TYPE_INTEGER, t ) )
00163         {
00164             sideTag = t;
00165         }
00166     }
00167     if( printNamedSets )
00168     {
00169         if( MB_SUCCESS == mb.tag_get_handle( NAME_TAG_NAME, NAME_TAG_SIZE, MB_TYPE_OPAQUE, t ) )
00170         {
00171             nameTag = t;
00172         }
00173     }
00174     idTag = mb.globalId_tag();
00175 
00176     write_dot( contained, children );
00177     return 0;
00178 }
00179 
00180 void write_dot( Link contained, Link children )
00181 {
00182     Range sets;
00183     std::cout << "digraph {" << std::endl;
00184     dot_nodes( std::cout, sets );
00185     std::cout << std::endl;
00186     if( contained ) dot_contained( std::cout, sets, contained == DASHED );
00187     if( children ) dot_children( std::cout, sets, children == DASHED );
00188     std::cout << "}" << std::endl;
00189 }
00190 
00191 static void dot_get_sets( Range& curr_sets, Range& result_sets, Tag tag, void* tag_val = 0 )
00192 {
00193     if( !tag ) return;
00194 
00195     result_sets.clear();
00196     mb.get_entities_by_type_and_tag( 0, MBENTITYSET, &tag, &tag_val, 1, result_sets );
00197     result_sets = subtract( result_sets, curr_sets );
00198     curr_sets.merge( result_sets );
00199 }
00200 
00201 static void dot_write_node( std::ostream& s, EntityHandle h, const char* label, int* id = 0 )
00202 {
00203     s << 's' << mb.id_from_handle( h ) << " [label = \"" << label;
00204     if( id ) s << ' ' << *id;
00205     s << "\"];" << std::endl;
00206 }
00207 
00208 static void dot_write_id_nodes( std::ostream& s, const Range& entites, Tag id_tag, const char* type_name )
00209 {
00210     int id;
00211     for( Range::iterator i = entites.begin(); i != entites.end(); ++i )
00212         if( MB_SUCCESS == mb.tag_get_data( id_tag, &*i, 1, &id ) ) dot_write_node( s, *i, type_name, &id );
00213 }
00214 
00215 void dot_nodes( std::ostream& s, Range& sets )
00216 {
00217     Range vol_sets, surf_sets, curv_sets, vert_sets;
00218     Range block_sets, side_sets, node_sets;
00219     Range named_sets, other_sets;
00220 
00221     dot_get_sets( sets, named_sets, nameTag );
00222 
00223     int dim = 3;
00224     dot_get_sets( sets, vol_sets, geomTag, &dim );
00225     dim = 2;
00226     dot_get_sets( sets, surf_sets, geomTag, &dim );
00227     dim = 1;
00228     dot_get_sets( sets, curv_sets, geomTag, &dim );
00229     dim = 0;
00230     dot_get_sets( sets, vert_sets, geomTag, &dim );
00231 
00232     dot_get_sets( sets, block_sets, blockTag );
00233     dot_get_sets( sets, side_sets, sideTag );
00234     dot_get_sets( sets, node_sets, nodeTag );
00235 
00236     if( printAnonSets )
00237     {
00238         mb.get_entities_by_type( 0, MBENTITYSET, other_sets );
00239         Range xsect = subtract( other_sets, sets );
00240         sets.swap( other_sets );
00241         other_sets.swap( xsect );
00242     }
00243 
00244     dot_write_id_nodes( s, vol_sets, idTag, "Volume" );
00245     dot_write_id_nodes( s, surf_sets, idTag, "Surface" );
00246     dot_write_id_nodes( s, curv_sets, idTag, "Curve" );
00247     dot_write_id_nodes( s, vert_sets, idTag, "Vertex" );
00248     dot_write_id_nodes( s, block_sets, blockTag, "Block" );
00249     dot_write_id_nodes( s, side_sets, sideTag, "Neumann Set" );
00250     dot_write_id_nodes( s, node_sets, nodeTag, "Dirichlet Set" );
00251 
00252     Range::iterator i;
00253     char name[NAME_TAG_SIZE + 1];
00254     for( i = named_sets.begin(); i != named_sets.end(); ++i )
00255     {
00256         if( MB_SUCCESS == mb.tag_get_data( nameTag, &*i, 1, name ) )
00257         {
00258             name[NAME_TAG_SIZE] = '\0';
00259             dot_write_node( s, *i, name );
00260         }
00261     }
00262     for( i = other_sets.begin(); i != other_sets.end(); ++i )
00263     {
00264         int id = mb.id_from_handle( *i );
00265         dot_write_node( s, *i, "EntitySet ", &id );
00266     }
00267 }
00268 
00269 static void dot_down_link( std::ostream& s,
00270                            EntityHandle parent,
00271                            EntityHandle child,
00272                            bool dashed,
00273                            const char* label = 0 )
00274 {
00275     s << 's' << mb.id_from_handle( parent ) << " -> " << 's' << mb.id_from_handle( child );
00276     if( dashed && label )
00277         s << " [style = dashed label = \"" << label << "\"]";
00278     else if( dashed )
00279         s << " [style = dashed]";
00280     else if( label )
00281         s << " [label = \"" << label << "\"]";
00282     s << ';' << std::endl;
00283 }
00284 
00285 void dot_children( std::ostream& s, const Range& sets, bool dashed )
00286 {
00287     int sense;
00288     const char *fstr = "forward", *rstr = "reverse";
00289     for( Range::iterator i = sets.begin(); i != sets.end(); ++i )
00290     {
00291         Range parents;
00292         mb.get_parent_meshsets( *i, parents );
00293         parents = intersect( parents, sets );
00294 
00295         for( Range::iterator j = parents.begin(); j != parents.end(); ++j )
00296         {
00297             const char* linklabel = 0;
00298             if( printSVSense && MB_SUCCESS == geomTool.get_sense( *i, *j, sense ) )
00299 
00300                 // check here only one sense, as before...
00301                 linklabel = ( sense == (int)SENSE_FORWARD ) ? fstr : rstr;
00302 
00303             dot_down_link( s, *j, *i, dashed, linklabel );
00304         }
00305     }
00306 }
00307 
00308 void dot_contained( std::ostream& s, const Range& sets, bool dashed )
00309 {
00310     for( Range::iterator i = sets.begin(); i != sets.end(); ++i )
00311     {
00312         Range contained;
00313         mb.get_entities_by_type( *i, MBENTITYSET, contained );
00314         contained = intersect( contained, sets );
00315 
00316         for( Range::iterator j = contained.begin(); j != contained.end(); ++j )
00317             dot_down_link( s, *i, *j, dashed );
00318     }
00319 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines