Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
depth.cpp
Go to the documentation of this file.
1 #include "moab/Range.hpp"
2 #include "moab/Core.hpp"
3 #include "moab/Skinner.hpp"
4 #include <iostream>
5 #include <cstdlib>
6 
7 using namespace moab;
8 
9 enum
10 {
11  NO_ERROR = 0,
14  INTERNAL_ERROR = 3
15 };
16 
17 const char* DEFAULT_TAG_NAME = "depth";
18 
19 static void usage( const char* argv0 )
20 {
21  std::cerr << "Usage: " << argv0 << "[-t <tag name] <input_file> <output_file>" << std::endl
22  << argv0 << "-h" << std::endl;
23  exit( SYNTAX_ERROR );
24 }
25 
26 static void check( ErrorCode rval )
27 {
28  if( MB_SUCCESS != rval )
29  {
30  std::cerr << "Internal error. Aborting." << std::endl;
31  exit( INTERNAL_ERROR );
32  }
33 }
34 
35 static void tag_depth( Interface& moab, Tag tag );
36 
37 int main( int argc, char* argv[] )
38 {
39  const char *input = 0, *output = 0, *tagname = DEFAULT_TAG_NAME;
40  bool expect_tag_name = false;
41  for( int i = 1; i < argc; ++i )
42  {
43  if( expect_tag_name )
44  {
45  tagname = argv[i];
46  expect_tag_name = false;
47  }
48  else if( !strcmp( "-t", argv[i] ) )
49  expect_tag_name = true;
50  else if( input == 0 )
51  input = argv[i];
52  else if( output == 0 )
53  output = argv[i];
54  else
55  {
56  std::cerr << "Unexpected argument: '" << argv[i] << "'" << std::endl;
57  usage( argv[0] );
58  }
59  }
60 
61  if( expect_tag_name )
62  {
63  std::cerr << "Expected argument following '-t'" << std::endl;
64  usage( argv[0] );
65  }
66  if( !input )
67  {
68  std::cerr << "No input file" << std::endl;
69  usage( argv[0] );
70  }
71  if( !output )
72  {
73  std::cerr << "No output file" << std::endl;
74  usage( argv[0] );
75  }
76 
77  Core moab;
78  Interface& mb = moab;
79 
80  EntityHandle file;
81  ErrorCode rval;
82  rval = mb.create_meshset( MESHSET_SET, file );
83  check( rval );
84  rval = mb.load_file( input, &file );
85  if( MB_SUCCESS != rval )
86  {
87  std::cerr << "Failed to load file: " << input << std::endl;
88  return FILE_IO_ERROR;
89  }
90 
91  int init_val = -1;
92  Tag tag;
93  bool created;
94  rval = mb.tag_get_handle( tagname, 1, MB_TYPE_INTEGER, tag, MB_TAG_DENSE | MB_TAG_CREAT, &init_val, &created );
95  if( !created )
96  {
97  rval = mb.tag_delete( tag );
98  check( rval );
99  rval = mb.tag_get_handle( tagname, 1, MB_TYPE_INTEGER, tag, MB_TAG_DENSE | MB_TAG_CREAT, &init_val, &created );
100  check( rval );
101  }
102 
103  tag_depth( mb, tag );
104 
105  rval = mb.write_file( output, 0, 0, &file, 1 );
106  if( rval == MB_SUCCESS )
107  std::cout << "Wrote file: " << output << std::endl;
108  else
109  {
110  std::cerr << "Failed to write file: " << output << std::endl;
111  return FILE_IO_ERROR;
112  }
113 
114  return NO_ERROR;
115 }
116 
117 static ErrorCode get_adjacent_elems( Interface& mb, const Range& verts, Range& elems )
118 {
119  elems.clear();
120  ErrorCode rval;
121  for( int dim = 3; dim > 0; --dim )
122  {
123  rval = mb.get_adjacencies( verts, dim, false, elems, Interface::UNION );
124  if( MB_SUCCESS != rval ) break;
125  }
126  return rval;
127 }
128 
129 void tag_depth( Interface& mb, Tag tag )
130 {
131  ErrorCode rval;
132  int dim;
133 
134  Skinner tool( &mb );
135  Range verts, elems;
136  dim = 3;
137  while( elems.empty() )
138  {
139  rval = mb.get_entities_by_dimension( 0, dim, elems );
140  check( rval );
141  if( --dim == 0 ) return; // no elements
142  }
143  rval = tool.find_skin( 0, elems, 0, verts );
144  check( rval );
145  rval = get_adjacent_elems( mb, verts, elems );
146  check( rval );
147 
148  std::vector< int > data;
149  int val, depth = 0;
150  while( !elems.empty() )
151  {
152  data.clear();
153  data.resize( elems.size(), depth++ );
154  rval = mb.tag_set_data( tag, elems, &data[0] );
155  check( rval );
156 
157  verts.clear();
158  rval = mb.get_adjacencies( elems, 0, false, verts, Interface::UNION );
159  check( rval );
160 
161  Range tmp;
162  rval = get_adjacent_elems( mb, verts, tmp );
163  check( rval );
164  elems.clear();
165  for( Range::reverse_iterator i = tmp.rbegin(); i != tmp.rend(); ++i )
166  {
167  rval = mb.tag_get_data( tag, &*i, 1, &val );
168  check( rval );
169  if( val == -1 ) elems.insert( *i );
170  }
171  }
172 
173  std::cout << "Maximum depth: " << depth << std::endl;
174 }