Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
TestErrorHandling.cpp
Go to the documentation of this file.
1 /**
2  * @file TestErrorHandling.cpp
3  * @brief Example demonstrating MOAB's trace back error handler in serial
4  *
5  * This example shows how to:
6  * - Initialize and finalize MOAB's error handler
7  * - Simulate different types of errors in serial execution
8  * - Handle various MOAB error codes and error propagation
9  * - Test error handling for file loading, tag creation, and tag iteration
10  *
11  * The example demonstrates four test cases:
12  * - Test case 1: Error MB_NOT_IMPLEMENTED (unsupported variable on edges)
13  * - Test case 2: Error MB_TYPE_OUT_OF_RANGE (invalid GATHER_SET option)
14  * - Test case 3: Error MB_FAILURE (NOMESH option with NULL file set)
15  * - Test case 4: Error MB_VARIABLE_DATA_LENGTH (variable-length tag iteration)
16  *
17  * @author MOAB Development Team
18  * @date 2024
19  *
20 
21  * Description: This example tests MOAB's trace back error handler in serial. \n
22  *
23  * <b>To run</b>: ./TestErrorHandling <test_case_num(1 to 4)> \n
24  *
25  * @param argc Number of command line arguments
26  * @param argv Command line arguments array
27  * @return 0 on success, 1 on failure
28  */
29 
30 #include "moab/Core.hpp"
31 #ifdef MOAB_HAVE_MPI
32 #include "moab_mpi.h"
33 #endif
34 
35 #include <iostream>
36 #include <memory>
37 
38 using namespace moab;
39 using namespace std;
40 
41 // In this test case, an error MB_NOT_IMPLEMENTED is returned by MOAB
43 {
44  Core moab;
45  Interface& mb = moab;
46 
47  // Load a CAM-FV file and read a variable on edges (not supported yet)
48  string test_file = string( MESH_DIR ) + string( "/io/fv3x46x72.t.3.nc" );
49  MB_CHK_ERR( mb.load_file( test_file.c_str(), NULL, "VARIABLE=US" ) );
50 
51  return MB_SUCCESS;
52 }
53 
54 // In this test case, an error MB_TYPE_OUT_OF_RANGE is returned by MOAB
56 {
57  Core moab;
58  Interface& mb = moab;
59 
60  // Load a HOMME file with an invalid GATHER_SET option
61  string test_file = string( MESH_DIR ) + string( "/io/homme3x3458.t.3.nc" );
62  MB_CHK_ERR( mb.load_file( test_file.c_str(), NULL, "VARIABLE=T;GATHER_SET=0.1" ) );
63 
64  return MB_SUCCESS;
65 }
66 
67 // In this test case, an error MB_FAILURE is returned by MOAB
69 {
70  Core moab;
71  Interface& mb = moab;
72 
73  // Load a CAM-FV file with NOMESH option and a NULL file set
74  string test_file = string( MESH_DIR ) + string( "/io/fv3x46x72.t.3.nc" );
75  MB_CHK_ERR( mb.load_file( test_file.c_str(), NULL, "NOMESH;VARIABLE=" ) );
76 
77  return MB_SUCCESS;
78 }
79 
80 // In this test case, an error MB_VARIABLE_DATA_LENGTH is returned by MOAB
82 {
83  Core moab;
84  Interface& mb = moab;
85 
86  // Create 100 vertices
87  const int NUM_VTX = 100;
88  vector< double > coords( 3 * NUM_VTX );
89  Range verts;
90  MB_CHK_SET_ERR( mb.create_vertices( &coords[0], NUM_VTX, verts ), "Failed to create vertices" );
91 
92  // Create a variable-length dense tag
93  Tag tag;
94  MB_CHK_SET_ERR( mb.tag_get_handle( "var_len_den", 1, MB_TYPE_INTEGER, tag,
96  "Failed to create a tag" );
97 
98  // Attempt to iterate over a variable-length tag, which will never be possible
99  void* ptr = NULL;
100  int count = 0;
101  MB_CHK_SET_ERR( mb.tag_iterate( tag, verts.begin(), verts.end(), count, ptr ),
102  "Failed to iterate over tag on " << NUM_VTX << " vertices" );
103 
104  return MB_SUCCESS;
105 }
106 
107 int main( int argc, char** argv )
108 {
109  if( argc < 2 )
110  {
111  cout << "Usage: " << argv[0] << " <test_case_num(1 to 4)>" << endl;
112  return 0;
113  }
114 
115 #ifdef MOAB_HAVE_MPI
116  MPI_Init( &argc, &argv );
117 #endif
118 
119  // Initialize error handler, optional for this example (using moab instances)
121 
122  ErrorCode rval = MB_SUCCESS;
123 
124  int test_case_num = atoi( argv[1] );
125  switch( test_case_num )
126  {
127  case 1:
129  break;
130  case 2:
132  break;
133  case 3:
135  break;
136  case 4:
138  break;
139  default:
140  break;
141  }
142 
143  // Finalize error handler, optional for this example (using moab instances)
145 
146 #ifdef MOAB_HAVE_MPI
147  MPI_Finalize();
148 #endif
149 
150  return 0;
151 }