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

Example demonstrating MOAB's trace back error handler in serial. More...

#include "moab/Core.hpp"
#include <iostream>
#include <memory>
+ Include dependency graph for TestErrorHandling.cpp:

Go to the source code of this file.

Functions

ErrorCode TestErrorHandling_1 ()
 
ErrorCode TestErrorHandling_2 ()
 
ErrorCode TestErrorHandling_3 ()
 
ErrorCode TestErrorHandling_4 ()
 
int main (int argc, char **argv)
 

Detailed Description

Example demonstrating MOAB's trace back error handler in serial.

This example shows how to:

  • Initialize and finalize MOAB's error handler
  • Simulate different types of errors in serial execution
  • Handle various MOAB error codes and error propagation
  • Test error handling for file loading, tag creation, and tag iteration

The example demonstrates four test cases:

  • Test case 1: Error MB_NOT_IMPLEMENTED (unsupported variable on edges)
  • Test case 2: Error MB_TYPE_OUT_OF_RANGE (invalid GATHER_SET option)
  • Test case 3: Error MB_FAILURE (NOMESH option with NULL file set)
  • Test case 4: Error MB_VARIABLE_DATA_LENGTH (variable-length tag iteration)
Author
MOAB Development Team
Date
2024

Description: This example tests MOAB's trace back error handler in serial.
To run: ./TestErrorHandling <test_case_num(1 to 4)>

Parameters
argcNumber of command line arguments
argvCommand line arguments array
Returns
0 on success, 1 on failure

Definition in file TestErrorHandling.cpp.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 107 of file TestErrorHandling.cpp.

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 }

References ErrorCode, MB_CHK_ERR, MB_SUCCESS, moab::MBErrorHandler_Finalize(), moab::MBErrorHandler_Init(), TestErrorHandling_1(), TestErrorHandling_2(), TestErrorHandling_3(), and TestErrorHandling_4().

◆ TestErrorHandling_1()

ErrorCode TestErrorHandling_1 ( )

Definition at line 42 of file TestErrorHandling.cpp.

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 }

References moab::Core::load_file(), mb, MB_CHK_ERR, MB_SUCCESS, and MESH_DIR.

Referenced by main().

◆ TestErrorHandling_2()

ErrorCode TestErrorHandling_2 ( )

Definition at line 55 of file TestErrorHandling.cpp.

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 }

References moab::Core::load_file(), mb, MB_CHK_ERR, MB_SUCCESS, and MESH_DIR.

Referenced by main().

◆ TestErrorHandling_3()

ErrorCode TestErrorHandling_3 ( )

Definition at line 68 of file TestErrorHandling.cpp.

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 }

References moab::Core::load_file(), mb, MB_CHK_ERR, MB_SUCCESS, and MESH_DIR.

Referenced by main().

◆ TestErrorHandling_4()

ErrorCode TestErrorHandling_4 ( )

Definition at line 81 of file TestErrorHandling.cpp.

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 }

References moab::Range::begin(), moab::Core::create_vertices(), moab::Range::end(), mb, MB_CHK_SET_ERR, MB_SUCCESS, MB_TAG_CREAT, MB_TAG_DENSE, MB_TAG_VARLEN, MB_TYPE_INTEGER, moab::Core::tag_get_handle(), and moab::Core::tag_iterate().

Referenced by main().