Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
GetEntities.cpp File Reference
#include "moab/Core.hpp"
#include "moab/Range.hpp"
#include "moab/CN.hpp"
#include <iostream>
#include <memory>
#include <string>
#include <string_view>
+ Include dependency graph for GetEntities.cpp:

Go to the source code of this file.

Namespaces

 anonymous_namespace{GetEntities.cpp}
 

Functions

void print_usage (const char *program_name)
 
int main (int argc, char **argv)
 

Variables

const char *const anonymous_namespace{GetEntities.cpp}::DEFAULT_MESH_FILE = "hex01.vtk"
 
const int anonymous_namespace{GetEntities.cpp}::DIMENSION = 3
 

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)
Examples
GetEntities.cpp.

Definition at line 44 of file GetEntities.cpp.

45 {
46  try
47  {
48  // Parse command line arguments
49  if( argc > 1 && ( std::string( argv[1] ) == "-h" || std::string( argv[1] ) == "--help" ) )
50  {
51  print_usage( argv[0] );
52  return 0;
53  }
54 
55  // Get mesh file path
56  const std::string mesh_file =
57  ( argc > 1 ) ? argv[1] : std::string( MESH_DIR ) + "/" + std::string( DEFAULT_MESH_FILE );
58 
59  // Initialize MOAB
60  auto moab = std::make_unique< Core >();
61  if( !moab )
62  {
63  std::cerr << "Error: Failed to create MOAB instance\n";
64  return 1;
65  }
66 
67  // Load the mesh
68  MB_CHK_SET_ERR( moab->load_mesh( mesh_file.c_str() ), "Failed to load mesh file: " << mesh_file );
69 
70  // Get all entities in the mesh
71  Range entities;
72  MB_CHK_SET_ERR( moab->get_entities_by_handle( 0, entities ), "Failed to retrieve entities from mesh" );
73 
74  // Process each entity
75  for( const auto& entity : entities )
76  {
77  const auto entity_type = moab->type_from_handle( entity );
78  const auto entity_id = moab->id_from_handle( entity );
79 
80  if( entity_type == moab::MBVERTEX )
81  {
82  // For vertices, get adjacent elements
83  Range adjacent_elements;
84  MB_CHK_SET_ERR( moab->get_adjacencies( &entity, 1, DIMENSION, false, adjacent_elements ),
85  "Failed to get adjacencies for vertex " << entity_id );
86 
87  std::cout << "Vertex " << entity_id << " is connected to " << adjacent_elements.size() << " elements\n";
88  if( !adjacent_elements.empty() )
89  {
90  std::cout << " Adjacent elements: ";
91  for( const auto& elem : adjacent_elements )
92  {
93  std::cout << moab->id_from_handle( elem ) << " ";
94  }
95  std::cout << "\n";
96  }
97  }
98  else if( entity_type < moab::MBENTITYSET )
99  {
100  // For elements, get connectivity
101  const EntityHandle* connectivity = nullptr;
102  int num_vertices = 0;
103 
104  MB_CHK_SET_ERR( moab->get_connectivity( entity, connectivity, num_vertices ),
105  "Failed to get connectivity for " << moab::CN::EntityTypeName( entity_type ) << " "
106  << entity_id );
107 
108  std::cout << moab::CN::EntityTypeName( entity_type ) << " " << entity_id << " has " << num_vertices
109  << " vertices: ";
110 
111  for( int i = 0; i < num_vertices; ++i )
112  {
113  std::cout << moab->id_from_handle( connectivity[i] );
114  if( i < num_vertices - 1 ) std::cout << ", ";
115  }
116  std::cout << "\n";
117  }
118  }
119 
120  return 0;
121  }
122  catch( const std::exception& e )
123  {
124  std::cerr << "Error: " << e.what() << "\n";
125  return 1;
126  }
127  catch( ... )
128  {
129  std::cerr << "Error: Unknown exception occurred\n";
130  return 1;
131  }
132 }

References anonymous_namespace{GetEntities.cpp}::DEFAULT_MESH_FILE, anonymous_namespace{GetEntities.cpp}::DIMENSION, moab::Range::empty(), moab::CN::EntityTypeName(), MB_CHK_SET_ERR, MBENTITYSET, MBVERTEX, MESH_DIR, print_usage(), and moab::Range::size().

◆ print_usage()

void print_usage ( const char *  program_name)
Examples
GetEntities.cpp.

Definition at line 38 of file GetEntities.cpp.

39 {
40  std::cout << "Usage: " << program_name << " [meshfile]\n"
41  << " meshfile - Path to the mesh file (default: " << MESH_DIR << "/" << DEFAULT_MESH_FILE << ")\n";
42 }

References anonymous_namespace{GetEntities.cpp}::DEFAULT_MESH_FILE, and MESH_DIR.

Referenced by main().