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 MOAB_DIR=<installdir> \n
21 *
22 * <b> To run: </b>
23 * -# TestExodusII <mesh-file> \n
24 * -# TestExodusII (This uses the default <mesh-file>: <MOAB_SRC_DIR>/MeshFiles/unittest/mbtest2.g)
25 */
26 #include <iostream>
27
28 // Include header for MOAB instance and range
29 #include "moab/Core.hpp"
30
31 using namespace moab;
32 using namespace std;
33
34 string test_file_name = string(MESH_DIR) + string("/mbtest2.g");
main(int argc,char ** argv)35 int main(int argc, char **argv)
36 {
37 #ifdef MOAB_HAVE_NETCDF
38 // Get MOAB instance
39 Interface* mb = new (std::nothrow) Core;
40 if (NULL == mb)
41 return 1;
42
43 // Get the material set tag handle
44 Tag mtag;
45 ErrorCode rval;
46 const char *tag_nms[] = {"MATERIAL_SET", "DIRICHLET_SET", "NEUMANN_SET"};
47 Range sets, set_ents;
48
49 // Load a file
50 if (argc == 1) {
51 cout << "Running default case, loading " << test_file_name << endl;
52 cout << "Usage: " << argv[0] << " <filename>\n" << endl;
53 rval = mb->load_file(test_file_name.c_str());MB_CHK_ERR(rval);
54 }
55 else {
56 rval = mb->load_file(argv[argc - 1]);MB_CHK_ERR(rval);
57 cout << "Loaded mesh file: " << argv[argc - 1] << endl;
58 }
59
60 // Loop over set types
61 for (int i = 0; i < 3; i++) {
62 rval = mb->tag_get_handle(tag_nms[i], 1, MB_TYPE_INTEGER, mtag);MB_CHK_ERR(rval);
63
64 // Get all the sets of that type in the mesh
65 sets.clear();
66 rval = mb->get_entities_by_type_and_tag(0, MBENTITYSET, &mtag, NULL, 1, sets);MB_CHK_ERR(rval);
67
68 // Iterate over each set, getting entities
69 Range::iterator set_it;
70 for (set_it = sets.begin(); set_it != sets.end(); ++set_it) {
71 EntityHandle this_set = *set_it;
72
73 // Get the id for this set
74 int set_id;
75 rval = mb->tag_get_data(mtag, &this_set, 1, &set_id);MB_CHK_ERR(rval);
76
77 // Get the entities in the set, recursively
78 rval = mb->get_entities_by_handle(this_set, set_ents, true);MB_CHK_ERR(rval);
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 delete mb;
89 #else
90 cout<< " This test needs moab configured with netcdf \n";
91 #endif
92
93 return 0;
94 }
95