Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
TestExodusII.cpp
Go to the documentation of this file.
1 /** @example TestExodusII.cpp
2  * This example demonstrates how to retrieve material, dirichlet and neumann sets
3  * from an ExodusII file. \n
4  * Sets in MOAB contain entities and have a tag on them to give meaning to the entities.
5  * Tag names: MATERIAL_SET", "DIRICHLET_SET" and "NEUMANN_SET" are reserved and
6  * are associated with their corresponding entity sets.
7  * Sets are traversed to find out the type number of entities contained in each set. \n
8  *
9  * <b>Steps in this example </b>:
10  * -# Instantiate MOAB
11  * -# Get input mesh file name and load it.
12  * -# Loop over the three sets: material, dirichlet and neumann
13  * -# Get TagHandle and EntitySet(corresponding to the TagHandle)
14  * -# Loop thru all the EntitySet's
15  * -# Get the set id and entities in this set
16  * -# Destroy the MOAB instance
17  *
18  *
19  * <b> To compile: </b>
20  * make TestExodusII \n
21  *
22  * <b> To run: </b>
23  * -# TestExodusII <mesh-file> \n
24  * -# TestExodusII (This uses the default <mesh-file>: MeshFiles/unittest/mbtest2.g)
25  */
26 #include <iostream>
27 #include <memory>
28 
29 // Include header for MOAB instance and range
30 #include "moab/Core.hpp"
31 
32 using namespace moab;
33 using namespace std;
34 
35 int main( int argc, char** argv )
36 {
37 #ifdef MOAB_HAVE_NETCDF
38  // Get MOAB instance
39  std::unique_ptr< Interface > mb( new( std::nothrow ) Core );
40  if( NULL == mb ) return 1;
41 
42  // Get the material set tag handle
43  Tag mtag;
44  ErrorCode rval;
45  const char* tag_nms[] = { "MATERIAL_SET", "DIRICHLET_SET", "NEUMANN_SET" };
46  Range sets, set_ents;
47 
48  // Load a file
49  if( argc == 1 )
50  {
51  cout << "Running default case, loading " << test_file_name << endl;
52  cout << "Usage: " << argv[0] << " <filename>\n" << endl;
53  MB_CHK_ERR( mb->load_file( test_file_name.c_str() ) );
54  }
55  else
56  {
57  MB_CHK_ERR( mb->load_file( argv[argc - 1] ) );
58  cout << "Loaded mesh file: " << argv[argc - 1] << endl;
59  }
60 
61  // Loop over set types
62  for( int i = 0; i < 3; i++ )
63  {
64  MB_CHK_ERR( mb->tag_get_handle( tag_nms[i], 1, MB_TYPE_INTEGER, mtag ) );
65 
66  // Get all the sets of that type in the mesh
67  sets.clear();
68  MB_CHK_ERR( mb->get_entities_by_type_and_tag( 0, MBENTITYSET, &mtag, NULL, 1, sets ) );
69 
70  // Iterate over each set, getting entities
71  for( EntityHandle this_set : sets )
72  {
73  // Get the id for this set
74  int set_id;
75  MB_CHK_ERR( mb->tag_get_data( mtag, &this_set, 1, &set_id ) );
76 
77  // Get the entities in the set, recursively
78  MB_CHK_ERR( mb->get_entities_by_handle( this_set, set_ents, true ) );
79 
80  cout << tag_nms[i] << " " << set_id << " has " << set_ents.size() << " entities:" << endl;
81 
82  // Print the entities contained in this set
83  set_ents.print( " " );
84  set_ents.clear();
85  }
86  }
87 
88 #else
89  cout << " This test needs moab configured with netcdf \n";
90 #endif
91 
92  return 0;
93 }