Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
SetsNTags.cpp File Reference

Example demonstrating entity sets and tag operations. More...

#include "moab/Core.hpp"
#include "moab/Range.hpp"
#include "MBTagConventions.hpp"
#include "moab/CN.hpp"
#include <iostream>
#include <memory>
#include <vector>
#include <string>
+ Include dependency graph for SetsNTags.cpp:

Go to the source code of this file.

Namespaces

 anonymous_namespace{SetsNTags.cpp}
 

Functions

void anonymous_namespace{SetsNTags.cpp}::print_usage (const char *program_name)
 
int main (int argc, char **argv)
 

Variables

const char *const anonymous_namespace{SetsNTags.cpp}::DEFAULT_MESH_FILE = "hex01.vtk"
 
const std::vector< const char * > anonymous_namespace{SetsNTags.cpp}::TAG_NAMES = { MATERIAL_SET_TAG_NAME, DIRICHLET_SET_TAG_NAME, NEUMANN_SET_TAG_NAME }
 

Detailed Description

Example demonstrating entity sets and tag operations.

This example shows how to:

  • Work with entity sets (material sets, boundary condition sets)
  • Access conventional tags from MBTagConventions.hpp
  • Query entities by type and tag
  • Retrieve set contents recursively
  • Access tag data on sets

The program reads a mesh file and identifies material sets, Dirichlet boundary condition sets, and Neumann boundary condition sets, then reports their contents.

Author
MOAB Team
Date
2024
Parameters
[in]argcNumber of command line arguments
[in]argvCommand line arguments array
[in]argv[1]Optional mesh file path (default: hex01.vtk)
Returns
0 on success, 1 on failure
Usage:
./SetsNTags [meshfile]
Example:
./SetsNTags my_mesh.vtk
See also
Core, Interface, Range, Tag, MBTagConventions

Definition in file SetsNTags.cpp.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 69 of file SetsNTags.cpp.

70 {
71  try
72  {
73  // Parse command line arguments
74  if( argc > 1 && ( std::string( argv[1] ) == "-h" || std::string( argv[1] ) == "--help" ) )
75  {
76  print_usage( argv[0] );
77  return 0;
78  }
79 
80  const std::string mesh_file = ( argc > 1 ) ? argv[1] : std::string( DEFAULT_MESH_FILE );
81 
82  // Create MOAB instance with smart pointer for automatic cleanup
83  auto moab = std::make_unique< Core >();
84  if( !moab )
85  {
86  std::cerr << "Error: Failed to create MOAB instance\n";
87  return 1;
88  }
89 
90  // Load the mesh file
91  MB_CHK_SET_ERR( moab->load_file( mesh_file.c_str() ), "Failed to load mesh file: " << mesh_file );
92 
93  Range sets, set_entities;
94  Tag tag_handle;
95 
96  // Process each tag type
97  for( const auto& tag_name : TAG_NAMES )
98  {
99  // Get the tag handle for this tag name
100  MB_CHK_SET_ERR( moab->tag_get_handle( tag_name, 1, moab::MB_TYPE_INTEGER, tag_handle ),
101  "Failed to get tag handle for: " << tag_name );
102 
103  // Get all sets with this tag
104  sets.clear();
105  MB_CHK_SET_ERR( moab->get_entities_by_type_and_tag( 0, moab::MBENTITYSET, &tag_handle, nullptr, 1, sets ),
106  "Failed to get entities for tag: " << tag_name );
107 
108  std::cout << "\nFound " << sets.size() << " sets with tag: " << tag_name << "\n";
109 
110  // Process each set
111  for( const auto& set_handle : sets )
112  {
113  // Get the set ID
114  int set_id;
115  MB_CHK_SET_ERR( moab->tag_get_data( tag_handle, &set_handle, 1, &set_id ),
116  "Failed to get tag data for set" );
117 
118  // Get all entities in the set (recursively)
119  set_entities.clear();
120  MB_CHK_SET_ERR( moab->get_entities_by_handle( set_handle, set_entities, true ),
121  "Failed to get entities in set" );
122 
123  std::cout << " " << tag_name << " " << set_id << " contains " << set_entities.size() << " entities\n";
124 
125  // Print entity information
126  set_entities.print( " " );
127  set_entities.clear();
128  }
129  }
130 
131  return 0;
132  }
133  catch( const std::exception& e )
134  {
135  std::cerr << "\nError: " << e.what() << "\n";
136  return 1;
137  }
138  catch( ... )
139  {
140  std::cerr << "\nError: Unknown exception occurred\n";
141  return 1;
142  }
143 }

References moab::Range::clear(), anonymous_namespace{SetsNTags.cpp}::DEFAULT_MESH_FILE, MB_CHK_SET_ERR, MB_TYPE_INTEGER, MBENTITYSET, moab::Range::print(), anonymous_namespace{SetsNTags.cpp}::print_usage(), moab::Range::size(), and anonymous_namespace{SetsNTags.cpp}::TAG_NAMES.