MOAB: Mesh Oriented datABase  (version 5.2.1)
VtkTest.cpp
Go to the documentation of this file.
00001 #include "moab/Core.hpp"
00002 #include "moab/Range.hpp"
00003 
00004 using namespace moab;
00005 
00006 #include <string.h>
00007 #include <stdio.h>
00008 #include <stdlib.h>
00009 #include <assert.h>
00010 #include <map>
00011 #include <vector>
00012 #include <algorithm>
00013 #include <sstream>
00014 
00015 #include "TestUtil.hpp"
00016 
00017 std::string poly_example      = TestDir + "/io/poly8-10.vtk";
00018 std::string polyhedra_example = TestDir + "/io/polyhedra.vtk";
00019 
00020 #define DECLARE_TEST( A ) \
00021     bool test_##A();      \
00022     int A##_reg_var = register_test( &test_##A, #A );
00023 
00024 typedef bool ( *test_ptr )();
00025 struct test_data
00026 {
00027     test_ptr test;
00028     const char* name;
00029     bool result;
00030 };
00031 size_t num_tests      = 0;
00032 test_data* test_array = 0;
00033 int register_test( test_ptr test, const char* name )
00034 {
00035     test_data* new_test_array = (test_data*)realloc( test_array, sizeof( test_data ) * ( num_tests + 1 ) );
00036     if( !new_test_array )
00037     {
00038         fprintf( stderr, "VtkTest.cpp::regeister_test(): reallocation of test array failed\n" );
00039         free( test_array );
00040         test_array = NULL;
00041         num_tests  = 0;
00042         return -1;
00043     }
00044     else
00045         test_array = new_test_array;
00046     test_array[num_tests].test   = test;
00047     test_array[num_tests].name   = name;
00048     test_array[num_tests].result = true;
00049     ++num_tests;
00050     return 0;
00051 }
00052 
00053 DECLARE_TEST( edge2 )
00054 DECLARE_TEST( edge3 )
00055 DECLARE_TEST( tri3 )
00056 DECLARE_TEST( tri6 )
00057 DECLARE_TEST( quad4 )
00058 DECLARE_TEST( quad8 )
00059 DECLARE_TEST( quad9 )
00060 DECLARE_TEST( polygon )
00061 DECLARE_TEST( polygon_mix )
00062 DECLARE_TEST( polyhedra )
00063 DECLARE_TEST( tet4 )
00064 DECLARE_TEST( tet10 )
00065 DECLARE_TEST( hex8 )
00066 DECLARE_TEST( hex20 )
00067 DECLARE_TEST( hex27 )
00068 DECLARE_TEST( wedge )
00069 DECLARE_TEST( wedge15 )
00070 DECLARE_TEST( pyramid )
00071 DECLARE_TEST( pyramid13 )
00072 
00073 DECLARE_TEST( structured_points_2d )
00074 DECLARE_TEST( free_nodes )
00075 // DECLARE_TEST(free_nodes_and_triangle)
00076 
00077 DECLARE_TEST( structured_grid_2d )
00078 DECLARE_TEST( rectilinear_grid_2d )
00079 DECLARE_TEST( structured_points_3d )
00080 DECLARE_TEST( structured_grid_3d )
00081 DECLARE_TEST( rectilinear_grid_3d )
00082 
00083 DECLARE_TEST( scalar_attrib_1_bit )
00084 DECLARE_TEST( scalar_attrib_1_uchar )
00085 DECLARE_TEST( scalar_attrib_1_char )
00086 DECLARE_TEST( scalar_attrib_1_ushort )
00087 DECLARE_TEST( scalar_attrib_1_short )
00088 DECLARE_TEST( scalar_attrib_1_uint )
00089 DECLARE_TEST( scalar_attrib_1_int )
00090 DECLARE_TEST( scalar_attrib_1_ulong )
00091 DECLARE_TEST( scalar_attrib_1_long )
00092 DECLARE_TEST( scalar_attrib_1_float )
00093 DECLARE_TEST( scalar_attrib_1_double )
00094 
00095 DECLARE_TEST( scalar_attrib_4_bit )
00096 DECLARE_TEST( scalar_attrib_4_uchar )
00097 DECLARE_TEST( scalar_attrib_4_char )
00098 DECLARE_TEST( scalar_attrib_4_ushort )
00099 DECLARE_TEST( scalar_attrib_4_short )
00100 DECLARE_TEST( scalar_attrib_4_uint )
00101 DECLARE_TEST( scalar_attrib_4_int )
00102 DECLARE_TEST( scalar_attrib_4_ulong )
00103 DECLARE_TEST( scalar_attrib_4_long )
00104 DECLARE_TEST( scalar_attrib_4_float )
00105 DECLARE_TEST( scalar_attrib_4_double )
00106 
00107 DECLARE_TEST( vector_attrib_bit )
00108 DECLARE_TEST( vector_attrib_uchar )
00109 DECLARE_TEST( vector_attrib_char )
00110 DECLARE_TEST( vector_attrib_ushort )
00111 DECLARE_TEST( vector_attrib_short )
00112 DECLARE_TEST( vector_attrib_uint )
00113 DECLARE_TEST( vector_attrib_int )
00114 DECLARE_TEST( vector_attrib_ulong )
00115 DECLARE_TEST( vector_attrib_long )
00116 DECLARE_TEST( vector_attrib_float )
00117 DECLARE_TEST( vector_attrib_double )
00118 
00119 DECLARE_TEST( tensor_attrib_uchar )
00120 DECLARE_TEST( tensor_attrib_char )
00121 DECLARE_TEST( tensor_attrib_ushort )
00122 DECLARE_TEST( tensor_attrib_short )
00123 DECLARE_TEST( tensor_attrib_uint )
00124 DECLARE_TEST( tensor_attrib_int )
00125 DECLARE_TEST( tensor_attrib_ulong )
00126 DECLARE_TEST( tensor_attrib_long )
00127 DECLARE_TEST( tensor_attrib_float )
00128 DECLARE_TEST( tensor_attrib_double )
00129 
00130 DECLARE_TEST( subset )
00131 DECLARE_TEST( write_free_nodes )
00132 
00133 DECLARE_TEST( unstructured_field )
00134 
00135 int main( int argc, char* argv[] )
00136 {
00137     int* test_indices = (int*)malloc( sizeof( int ) * num_tests );
00138     int test_count;
00139     // if no arguments, do all tests
00140     if( argc == 1 )
00141     {
00142         for( unsigned i = 0; i < num_tests; ++i )
00143             test_indices[i] = i;
00144         test_count = num_tests;
00145     }
00146     // otherwise run only specified tests
00147     else
00148     {
00149         test_count = 0;
00150         for( int i = 1; i < argc; ++i )
00151             for( unsigned j = 0; j < num_tests; ++j )
00152                 if( !strcmp( test_array[j].name, argv[i] ) ) test_indices[test_count++] = j;
00153     }
00154 
00155     int fail_count = 0;
00156     for( int i = 0; i < test_count; ++i )
00157     {
00158         test_data& test = test_array[test_indices[i]];
00159         printf( "Testing %s...\n", test.name );
00160         if( !( test.result = test.test() ) ) ++fail_count;
00161     }
00162 
00163     printf( "\n\n" );
00164     if( fail_count )
00165     {
00166         printf( "FAILED TESTS:\n" );
00167         for( int i = 0; i < test_count; ++i )
00168         {
00169             test_data& test = test_array[test_indices[i]];
00170             if( !test.result ) printf( "\t%s\n", test.name );
00171         }
00172     }
00173 
00174     if( test_count == 0 )
00175         printf( "0 VTK tests run\n" );
00176     else if( fail_count == 0 )
00177         printf( "%d tests passed\n", test_count );
00178     else
00179         printf( "%d of %d tests failed\n", fail_count, test_count );
00180     printf( "\n" );
00181 
00182     free( test_indices );
00183     free( test_array );
00184 
00185     return fail_count;
00186 }
00187 // CHECK is defined in TestUtil now
00188 #undef CHECK
00189 #define CHECK( A ) \
00190     if( is_error( ( A ) ) ) return do_error( #A, __LINE__ )
00191 static bool do_error( const char* string, int line )
00192 {
00193     fprintf( stderr, "Check failed at line %d: %s\n", line, string );
00194     return false;
00195 }
00196 static inline bool is_error( bool b )
00197 {
00198     return !b;
00199 }
00200 
00201 // static bool do_error( ErrorCode err, int line )
00202 //{
00203 //  Core tmp_core;
00204 //  fprintf(stderr, "API failed at line %d: %s (%d)\n",
00205 //    line, tmp_core.get_error_string(err).c_str(), (int)err );
00206 //  return false;
00207 //}
00208 static inline bool is_error( ErrorCode b )
00209 {
00210     return MB_SUCCESS != b;
00211 }
00212 
00213 bool read_file( Interface* iface, const char* file );
00214 bool write_and_read( Interface* iface1, Interface* iface2 );
00215 
00216 bool test_read_write_element( const double* coords, unsigned num_coords, const int* vtk_conn, const int* moab_conn,
00217                               unsigned num_conn, unsigned num_elem, unsigned vtk_type, EntityType moab_type );
00218 
00219 bool test_edge2()
00220 {
00221     const double coords[] = { 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1 };
00222     const int conn[]      = { 0, 1, 1, 2, 2, 3, 3, 4, 0, 4 };
00223 
00224     return test_read_write_element( coords, 5, conn, conn, 10, 5, 3, MBEDGE );
00225 }
00226 
00227 bool test_edge3()
00228 {
00229     const double coords[] = { -1,    -1,     2, 1,     -1,    2, 1,     1,     2, -1,     1,     2,
00230                               0.000, -0.707, 2, 0.707, 0.000, 2, 0.000, 0.707, 2, -0.707, 0.000, 2 };
00231     const int conn[]      = { 0, 1, 4, 1, 2, 5, 2, 3, 6, 3, 0, 7 };
00232 
00233     return test_read_write_element( coords, 8, conn, conn, 12, 4, 21, MBEDGE );
00234 }
00235 
00236 bool test_tri3()
00237 {
00238     const double coords[] = { 0, 0, 0, 5, 0, 0, 0, 5, 0, -5, 0, 0, 0, -5, 0 };
00239     const int conn[]      = { 0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 1 };
00240 
00241     return test_read_write_element( coords, 5, conn, conn, 12, 4, 5, MBTRI );
00242 }
00243 
00244 bool test_tri6()
00245 {
00246     const double coords[] = { 0, 2, 0, 0, 0, 2, 0, -2, 0, 0, 0, -2, 0, 1, 1, 0, -1, 1, 0, -1, -1, 0, 1, -1, 0, 0, 0 };
00247     const int conn[]      = { 0, 1, 3, 4, 5, 8, 1, 2, 3, 6, 7, 8 };
00248 
00249     return test_read_write_element( coords, 9, conn, conn, 12, 2, 22, MBTRI );
00250 }
00251 
00252 const double grid_3x3[] = { 0, 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 0, 1, 0, 1, 1, 0, 2, 1, 0, 3, 1, 0,
00253                             0, 2, 0, 1, 2, 0, 2, 2, 0, 3, 2, 0, 0, 3, 0, 1, 3, 0, 2, 3, 0, 3, 3, 0 };
00254 
00255 const int quad_structured_conn[] = { 0,  1, 5, 4, 1,  2,  6, 5, 2,  3,  7, 6,  4,  5,  9,  8,  5,  6,
00256                                      10, 9, 6, 7, 11, 10, 8, 9, 13, 12, 9, 10, 14, 13, 10, 11, 15, 14 };
00257 
00258 bool test_quad4()
00259 {
00260     // test read as quads
00261     bool rval1 = test_read_write_element( grid_3x3, 16, quad_structured_conn, quad_structured_conn, 36, 9, 9, MBQUAD );
00262 
00263     // test read as pixels
00264     const int conn2[] = { 0, 1,  4, 5, 1,  2,  5, 6, 2,  3,  6, 7,  4,  5,  8,  9,  5,  6,
00265                           9, 10, 6, 7, 10, 11, 8, 9, 12, 13, 9, 10, 13, 14, 10, 11, 14, 15 };
00266     bool rval2        = test_read_write_element( grid_3x3, 16, conn2, quad_structured_conn, 36, 9, 8, MBQUAD );
00267 
00268     return rval1 && rval2;
00269 }
00270 
00271 bool test_quad8()
00272 {
00273     const double coords[] = { 0, 0, 0, 0, 2, 0, 0, 4, 0, 0, 0, 4, 0, 2, 4, 0, 4, 4, 4, 0,
00274                               0, 4, 2, 0, 4, 4, 0, 2, 0, 0, 2, 4, 0, 0, 0, 2, 0, 4, 2 };
00275     const int conn[]      = { 0, 2, 5, 3, 1, 12, 4, 11, 2, 0, 6, 8, 1, 9, 7, 10 };
00276 
00277     return test_read_write_element( coords, 13, conn, conn, 16, 2, 23, MBQUAD );
00278 }
00279 
00280 bool test_quad9()
00281 {
00282     const double coords[] = { 0, 0, 0, 0, 2, 0, 0, 4, 0, 0, 0, 4, 0, 2, 4, 0, 4, 4, 4, 0, 0, 4, 2,
00283                               0, 4, 4, 0, 2, 0, 0, 2, 2, 0, 2, 4, 0, 0, 0, 2, 0, 2, 2, 0, 4, 2 };
00284     const int conn[]      = { 0, 2, 5, 3, 1, 14, 4, 12, 12, 2, 0, 6, 8, 1, 9, 7, 11, 10 };
00285 
00286     return test_read_write_element( coords, 15, conn, conn, 18, 2, 28, MBQUAD );
00287 }
00288 
00289 bool test_polygon()
00290 {
00291     const double coords[] = { 0, 0, 0, 0, 2, 0, 0, 4, 0, 0, 0, 4, 0, 2, 4, 0, 4, 4, 4, 0,
00292                               0, 4, 2, 0, 4, 4, 0, 2, 0, 0, 2, 4, 0, 0, 0, 2, 0, 4, 2 };
00293     const int conn[]      = { 0, 1, 2, 12, 5, 4, 3, 11, 2, 1, 0, 9, 6, 7, 8, 10 };
00294 
00295     return test_read_write_element( coords, 13, conn, conn, 16, 2, 7, MBPOLYGON );
00296 }
00297 
00298 bool test_polygon_mix()
00299 {
00300     // just read the polygon file with mixed sequences
00301     Core moab;
00302     Interface& mb = moab;
00303 
00304     ErrorCode rval = mb.load_file( poly_example.c_str() );
00305     if( MB_SUCCESS != rval ) return false;
00306 
00307     return true;
00308 }
00309 bool test_polyhedra()
00310 {
00311     // just read the polyhedra file
00312     Core moab;
00313     Interface& mb = moab;
00314 
00315     ErrorCode rval = mb.load_file( polyhedra_example.c_str() );
00316     if( MB_SUCCESS != rval ) return false;
00317     Range polyhedras;
00318     rval = mb.get_entities_by_type( 0, MBPOLYHEDRON, polyhedras );
00319     if( MB_SUCCESS != rval ) return false;
00320 
00321     if( 10 != polyhedras.size() ) return false;
00322     return true;
00323 }
00324 bool test_tet4()
00325 {
00326     const double coords[] = { 1, -1, 0, 1, 1, 0, -1, 1, 0, -1, -1, 0, 0, 0, -1, 0, 0, 1 };
00327     const int conn[]      = { 0, 1, 3, 5, 1, 2, 3, 5, 0, 1, 4, 3, 1, 2, 4, 3 };
00328 
00329     return test_read_write_element( coords, 6, conn, conn, 16, 4, 10, MBTET );
00330 }
00331 
00332 bool test_tet10()
00333 {
00334     const double coords[] = { 4, 0,  0, 0, 2, 0, 0, -2, 0, 0, 0,  -2, 0, 0, 2,  0, 1,  1,  2, 0, 1,
00335                               0, -1, 1, 0, 0, 0, 2, 1,  0, 2, -1, 0,  2, 0, -1, 0, -1, -1, 0, 1, -1 };
00336     const int conn[]      = { 0, 1, 2, 4, 9, 8, 10, 6, 5, 7, 2, 1, 0, 3, 8, 9, 10, 12, 13, 11 };
00337 
00338     return test_read_write_element( coords, 14, conn, conn, 20, 2, 24, MBTET );
00339 }
00340 
00341 const double grid_2x2x2[] = { 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 1, 0, 1, 1, 0, 2, 1, 0, 0, 2, 0, 1, 2, 0, 2, 2, 0,
00342                               0, 0, 1, 1, 0, 1, 2, 0, 1, 0, 1, 1, 1, 1, 1, 2, 1, 1, 0, 2, 1, 1, 2, 1, 2, 2, 1,
00343                               0, 0, 2, 1, 0, 2, 2, 0, 2, 0, 1, 2, 1, 1, 2, 2, 1, 2, 0, 2, 2, 1, 2, 2, 2, 2, 2 };
00344 
00345 const int hex_structured_conn[] = { 0,  1,  4,  3,  9,  10, 13, 12, 1,  2,  5,  4,  10, 11, 14, 13,
00346                                     3,  4,  7,  6,  12, 13, 16, 15, 4,  5,  8,  7,  13, 14, 17, 16,
00347                                     9,  10, 13, 12, 18, 19, 22, 21, 10, 11, 14, 13, 19, 20, 23, 22,
00348                                     12, 13, 16, 15, 21, 22, 25, 24, 13, 14, 17, 16, 22, 23, 26, 25 };
00349 
00350 bool test_hex8()
00351 {
00352     // check vtk hexes
00353     bool rval1 = test_read_write_element( grid_2x2x2, 27, hex_structured_conn, hex_structured_conn, 64, 8, 12, MBHEX );
00354     CHECK( rval1 );
00355 
00356     const int conn2[] = { 0,  1,  3,  4,  9,  10, 12, 13, 1,  2,  4,  5,  10, 11, 13, 14, 3,  4,  6,  7,  12, 13,
00357                           15, 16, 4,  5,  7,  8,  13, 14, 16, 17, 9,  10, 12, 13, 18, 19, 21, 22, 10, 11, 13, 14,
00358                           19, 20, 22, 23, 12, 13, 15, 16, 21, 22, 24, 25, 13, 14, 16, 17, 22, 23, 25, 26 };
00359 
00360     // check with vtk voxels
00361     bool rval2 = test_read_write_element( grid_2x2x2, 27, conn2, hex_structured_conn, 64, 8, 11, MBHEX );
00362     CHECK( rval2 );
00363 
00364     return true;
00365 }
00366 
00367 bool test_hex20()
00368 {
00369     const int vtk_conn[] = { 0, 2, 8, 6, 18, 20, 26, 24, 1, 5, 7, 3, 19, 23, 25, 21, 9, 11, 17, 15 };
00370     const int exo_conn[] = { 0, 2, 8, 6, 18, 20, 26, 24, 1, 5, 7, 3, 9, 11, 17, 15, 19, 23, 25, 21 };
00371 
00372     return test_read_write_element( grid_2x2x2, 27, vtk_conn, exo_conn, 20, 1, 25, MBHEX );
00373 }
00374 
00375 bool test_hex27()
00376 {
00377     const int vtk_conn[]  = { 0,  2,  8, 6,  18, 20, 26, 24, 1,  5,  7, 3,  19, 23,
00378                              25, 21, 9, 11, 17, 15, 10, 16, 14, 12, 4, 22, 13 };
00379     const int moab_conn[] = { 0,  2,  8,  6,  18, 20, 26, 24, 1,  5,  7, 3,  9, 11,
00380                               17, 15, 19, 23, 25, 21, 14, 16, 12, 10, 4, 22, 13 };
00381 
00382     return test_read_write_element( grid_2x2x2, 27, vtk_conn, moab_conn, 27, 1, 29, MBHEX );
00383 }
00384 
00385 bool test_wedge()
00386 {
00387     const double coords[] = { 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1 };
00388     const int exo_conn[]  = { 0, 1, 3, 4, 5, 7, 1, 2, 3, 5, 6, 7 };
00389     const int vtk_conn[]  = { 0, 3, 1, 4, 7, 5, 1, 3, 2, 5, 7, 6 };
00390     return test_read_write_element( coords, 8, vtk_conn, exo_conn, 12, 2, 13, MBPRISM );
00391 }
00392 
00393 bool test_wedge15()
00394 {
00395     const double coords[] = { 2, 0, 0, 2, 2, 0, 0, 2, 0, 0, 0, 0, 2, 0, 2, 2, 2, 2, 0, 2, 2, 0,
00396                               0, 2, 2, 1, 0, 1, 2, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 2, 1, 2, 1, 2,
00397                               2, 0, 1, 2, 1, 0, 2, 1, 1, 2, 2, 0, 1, 2, 2, 1, 0, 2, 1, 0, 0, 1 };
00398     const int exo_conn[]  = { 0, 1, 3, 4, 5, 7, 8, 12, 11, 18, 19, 21, 13, 17, 16,
00399                              1, 2, 3, 5, 6, 7, 9, 10, 12, 19, 20, 21, 14, 15, 17 };
00400     const int vtk_conn[]  = { 0, 3, 1, 4, 7, 5, 11, 12, 8, 16, 17, 13, 18, 21, 19,
00401                              1, 3, 2, 5, 7, 6, 12, 10, 9, 17, 15, 14, 19, 21, 20 };
00402     return test_read_write_element( coords, 22, vtk_conn, exo_conn, 30, 2, 26, MBPRISM );
00403 }
00404 
00405 bool test_pyramid()
00406 {
00407     const double coords[] = { 1, -1, 0, 1, 1, 0, -1, 1, 0, -1, -1, 0, 0, 0, -1, 0, 0, 1 };
00408     const int conn[]      = { 0, 1, 2, 3, 5, 3, 2, 1, 0, 4 };
00409 
00410     return test_read_write_element( coords, 6, conn, conn, 10, 2, 14, MBPYRAMID );
00411 }
00412 
00413 bool test_pyramid13()
00414 {
00415     const double coords[] = { 2,  -2, 0,  2,  2,  0,  -2, 2,  0, -2, -2, 0, 0,  0,  -2, 0,  0,  2,
00416                               2,  0,  0,  0,  2,  0,  -2, 0,  0, 0,  -2, 0, 1,  -1, -1, 1,  1,  -1,
00417                               -1, 1,  -1, -1, -1, -1, 1,  -1, 1, 1,  1,  1, -1, 1,  1,  -1, -1, 1 };
00418     const int conn[]      = { 0, 1, 2, 3, 5, 6, 7, 8, 9, 14, 15, 16, 17, 3, 2, 1, 0, 4, 8, 7, 6, 9, 13, 12, 11, 10 };
00419 
00420     return test_read_write_element( coords, 18, conn, conn, 26, 2, 27, MBPYRAMID );
00421 }
00422 
00423 bool test_structured_2d( const char* file );
00424 bool test_structured_3d( const char* file );
00425 
00426 bool test_structured_points_2d()
00427 {
00428     const char file[] = "# vtk DataFile Version 3.0\n"
00429                         "MOAB Version 1.00\n"
00430                         "ASCII\n"
00431                         "DATASET STRUCTURED_POINTS\n"
00432                         "DIMENSIONS 4 4 1\n"
00433                         "ORIGIN 0 0 0\n"
00434                         "SPACING 1 1 1\n";
00435     bool rval1 = test_structured_2d( file );
00436 
00437     // test again w/ old 1.0 ASPECT_RATIO keyword
00438     const char file2[] = "# vtk DataFile Version 3.0\n"
00439                          "MOAB Version 1.00\n"
00440                          "ASCII\n"
00441                          "DATASET STRUCTURED_POINTS\n"
00442                          "DIMENSIONS 4 4 1\n"
00443                          "ORIGIN 0 0 0\n"
00444                          "ASPECT_RATIO 1 1 1\n";
00445     bool rval2 = test_structured_2d( file2 );
00446 
00447     return rval1 && rval2;
00448 }
00449 bool test_free_vertices( const char* file )
00450 {
00451     // read VTK file
00452     Core instance;
00453     bool bval = read_file( &instance, file );
00454     CHECK( bval );
00455     return true;
00456 }
00457 
00458 bool test_free_nodes()
00459 {
00460     const char file1[] = "# vtk DataFile Version 3.0\n"
00461                          "MOAB Version 1.00\n"
00462                          "ASCII\n"
00463                          "DATASET UNSTRUCTURED_GRID\n"
00464                          "POINTS 2 double\n"
00465                          "10.0 0 0\n"
00466                          "-10.0 0 0\n"
00467                          "CELLS 2 4\n"
00468                          "1 0\n"
00469                          "1 1\n"
00470                          "CELL_TYPES 2\n"
00471                          "1\n"
00472                          "1\n";
00473 
00474     bool rval1 = test_free_vertices( file1 );
00475 
00476     const char file2[] = "# vtk DataFile Version 3.0\n"
00477                          "MOAB Version 1.00\n"
00478                          "ASCII\n"
00479                          "DATASET UNSTRUCTURED_GRID\n"
00480                          "POINTS 5 double\n"
00481                          "10.0 0 0\n"
00482                          "-10.0 0 0\n"
00483                          "-5 2. 2.\n"
00484                          "-5 2. 0.\n"
00485                          "-5 4. 2.\n"
00486                          "CELLS 3 8\n"
00487                          "1 0\n"
00488                          "1 1\n"
00489                          "3 2 3 4\n"
00490                          "CELL_TYPES 3\n"
00491                          "1\n"
00492                          "1\n"
00493                          "5\n";
00494 
00495     bool rval2 = test_free_vertices( file2 );
00496     return rval1 && rval2;
00497 }
00498 bool test_structured_grid_2d()
00499 {
00500     char file[4096] = "# vtk DataFile Version 3.0\n"
00501                       "MOAB Version 1.00\n"
00502                       "ASCII\n"
00503                       "DATASET STRUCTURED_GRID\n"
00504                       "DIMENSIONS 4 4 1\n"
00505                       "POINTS 16 double\n";
00506     int len = strlen( file );
00507     for( unsigned i = 0; i < 16; ++i )
00508         len += sprintf( file + len, "%f %f %f\n", grid_3x3[3 * i], grid_3x3[3 * i + 1], grid_3x3[3 * i + 2] );
00509 
00510     return test_structured_2d( file );
00511 }
00512 
00513 bool test_rectilinear_grid_2d()
00514 {
00515     const char file[] = "# vtk DataFile Version 3.0\n"
00516                         "MOAB Version 1.00\n"
00517                         "ASCII\n"
00518                         "DATASET RECTILINEAR_GRID\n"
00519                         "DIMENSIONS 4 4 1\n"
00520                         "X_COORDINATES 4 float 0 1 2 3\n"
00521                         "Y_COORDINATES 4 float 0 1 2 3\n"
00522                         "Z_COORDINATES 1 float 0\n";
00523 
00524     return test_structured_2d( file );
00525 }
00526 
00527 bool test_structured_points_3d()
00528 {
00529     const char file[] = "# vtk DataFile Version 3.0\n"
00530                         "MOAB Version 1.00\n"
00531                         "ASCII\n"
00532                         "DATASET STRUCTURED_POINTS\n"
00533                         "DIMENSIONS 3 3 3\n"
00534                         "ORIGIN 0 0 0\n"
00535                         "SPACING 1 1 1\n";
00536     return test_structured_3d( file );
00537 }
00538 
00539 bool test_structured_grid_3d()
00540 {
00541     char file[4096] = "# vtk DataFile Version 3.0\n"
00542                       "MOAB Version 1.00\n"
00543                       "ASCII\n"
00544                       "DATASET STRUCTURED_GRID\n"
00545                       "DIMENSIONS 3 3 3\n"
00546                       "POINTS 27 double\n";
00547 
00548     int len = strlen( file );
00549     for( unsigned i = 0; i < 27; ++i )
00550         len += sprintf( file + len, "%f %f %f\n", grid_2x2x2[3 * i], grid_2x2x2[3 * i + 1], grid_2x2x2[3 * i + 2] );
00551 
00552     return test_structured_3d( file );
00553 }
00554 
00555 bool test_rectilinear_grid_3d()
00556 {
00557     const char file[] = "# vtk DataFile Version 3.0\n"
00558                         "MOAB Version 1.00\n"
00559                         "ASCII\n"
00560                         "DATASET RECTILINEAR_GRID\n"
00561                         "DIMENSIONS 3 3 3\n"
00562                         "X_COORDINATES 3 float 0 1 2\n"
00563                         "Y_COORDINATES 3 float 0 1 2\n"
00564                         "Z_COORDINATES 3 float 0 1 2\n";
00565 
00566     return test_structured_3d( file );
00567 }
00568 
00569 bool test_scalar_attrib( const char* vtk_type, DataType mb_type, int count );
00570 bool test_vector_attrib( const char* vtk_type, DataType mb_type );
00571 bool test_tensor_attrib( const char* vtk_type, DataType mb_type );
00572 
00573 bool test_scalar_attrib_1_bit()
00574 {
00575     return test_scalar_attrib( "bit", MB_TYPE_BIT, 1 );
00576 }
00577 
00578 bool test_scalar_attrib_1_uchar()
00579 {
00580     return test_scalar_attrib( "unsigned_char", MB_TYPE_INTEGER, 1 );
00581 }
00582 
00583 bool test_scalar_attrib_1_char()
00584 {
00585     return test_scalar_attrib( "char", MB_TYPE_INTEGER, 1 );
00586 }
00587 
00588 bool test_scalar_attrib_1_ushort()
00589 {
00590     return test_scalar_attrib( "unsigned_short", MB_TYPE_INTEGER, 1 );
00591 }
00592 
00593 bool test_scalar_attrib_1_short()
00594 {
00595     return test_scalar_attrib( "short", MB_TYPE_INTEGER, 1 );
00596 }
00597 
00598 bool test_scalar_attrib_1_uint()
00599 {
00600     return test_scalar_attrib( "unsigned_int", MB_TYPE_INTEGER, 1 );
00601 }
00602 
00603 bool test_scalar_attrib_1_int()
00604 {
00605     return test_scalar_attrib( "int", MB_TYPE_INTEGER, 1 );
00606 }
00607 
00608 bool test_scalar_attrib_1_ulong()
00609 {
00610     return test_scalar_attrib( "unsigned_long", MB_TYPE_INTEGER, 1 );
00611 }
00612 
00613 bool test_scalar_attrib_1_long()
00614 {
00615     return test_scalar_attrib( "long", MB_TYPE_INTEGER, 1 );
00616 }
00617 
00618 bool test_scalar_attrib_1_float()
00619 {
00620     return test_scalar_attrib( "float", MB_TYPE_DOUBLE, 1 );
00621 }
00622 
00623 bool test_scalar_attrib_1_double()
00624 {
00625     return test_scalar_attrib( "double", MB_TYPE_DOUBLE, 1 );
00626 }
00627 
00628 bool test_scalar_attrib_4_bit()
00629 {
00630     return test_scalar_attrib( "bit", MB_TYPE_BIT, 4 );
00631 }
00632 
00633 bool test_scalar_attrib_4_uchar()
00634 {
00635     return test_scalar_attrib( "unsigned_char", MB_TYPE_INTEGER, 4 );
00636 }
00637 
00638 bool test_scalar_attrib_4_char()
00639 {
00640     return test_scalar_attrib( "char", MB_TYPE_INTEGER, 4 );
00641 }
00642 
00643 bool test_scalar_attrib_4_ushort()
00644 {
00645     return test_scalar_attrib( "unsigned_short", MB_TYPE_INTEGER, 4 );
00646 }
00647 
00648 bool test_scalar_attrib_4_short()
00649 {
00650     return test_scalar_attrib( "short", MB_TYPE_INTEGER, 4 );
00651 }
00652 
00653 bool test_scalar_attrib_4_uint()
00654 {
00655     return test_scalar_attrib( "unsigned_int", MB_TYPE_INTEGER, 4 );
00656 }
00657 
00658 bool test_scalar_attrib_4_int()
00659 {
00660     return test_scalar_attrib( "int", MB_TYPE_INTEGER, 4 );
00661 }
00662 
00663 bool test_scalar_attrib_4_ulong()
00664 {
00665     return test_scalar_attrib( "unsigned_long", MB_TYPE_INTEGER, 4 );
00666 }
00667 
00668 bool test_scalar_attrib_4_long()
00669 {
00670     return test_scalar_attrib( "long", MB_TYPE_INTEGER, 4 );
00671 }
00672 
00673 bool test_scalar_attrib_4_float()
00674 {
00675     return test_scalar_attrib( "float", MB_TYPE_DOUBLE, 4 );
00676 }
00677 
00678 bool test_scalar_attrib_4_double()
00679 {
00680     return test_scalar_attrib( "double", MB_TYPE_DOUBLE, 4 );
00681 }
00682 
00683 bool test_vector_attrib_bit()
00684 {
00685     return test_vector_attrib( "bit", MB_TYPE_BIT );
00686 }
00687 
00688 bool test_vector_attrib_uchar()
00689 {
00690     return test_vector_attrib( "unsigned_char", MB_TYPE_INTEGER );
00691 }
00692 
00693 bool test_vector_attrib_char()
00694 {
00695     return test_vector_attrib( "char", MB_TYPE_INTEGER );
00696 }
00697 
00698 bool test_vector_attrib_ushort()
00699 {
00700     return test_vector_attrib( "unsigned_short", MB_TYPE_INTEGER );
00701 }
00702 
00703 bool test_vector_attrib_short()
00704 {
00705     return test_vector_attrib( "short", MB_TYPE_INTEGER );
00706 }
00707 
00708 bool test_vector_attrib_uint()
00709 {
00710     return test_vector_attrib( "unsigned_int", MB_TYPE_INTEGER );
00711 }
00712 
00713 bool test_vector_attrib_int()
00714 {
00715     return test_vector_attrib( "int", MB_TYPE_INTEGER );
00716 }
00717 
00718 bool test_vector_attrib_ulong()
00719 {
00720     return test_vector_attrib( "unsigned_long", MB_TYPE_INTEGER );
00721 }
00722 
00723 bool test_vector_attrib_long()
00724 {
00725     return test_vector_attrib( "long", MB_TYPE_INTEGER );
00726 }
00727 
00728 bool test_vector_attrib_float()
00729 {
00730     return test_vector_attrib( "float", MB_TYPE_DOUBLE );
00731 }
00732 
00733 bool test_vector_attrib_double()
00734 {
00735     return test_vector_attrib( "double", MB_TYPE_DOUBLE );
00736 }
00737 
00738 bool test_tensor_attrib_uchar()
00739 {
00740     return test_tensor_attrib( "unsigned_char", MB_TYPE_INTEGER );
00741 }
00742 
00743 bool test_tensor_attrib_char()
00744 {
00745     return test_tensor_attrib( "char", MB_TYPE_INTEGER );
00746 }
00747 
00748 bool test_tensor_attrib_ushort()
00749 {
00750     return test_tensor_attrib( "unsigned_short", MB_TYPE_INTEGER );
00751 }
00752 
00753 bool test_tensor_attrib_short()
00754 {
00755     return test_tensor_attrib( "short", MB_TYPE_INTEGER );
00756 }
00757 
00758 bool test_tensor_attrib_uint()
00759 {
00760     return test_tensor_attrib( "unsigned_int", MB_TYPE_INTEGER );
00761 }
00762 
00763 bool test_tensor_attrib_int()
00764 {
00765     return test_tensor_attrib( "int", MB_TYPE_INTEGER );
00766 }
00767 
00768 bool test_tensor_attrib_ulong()
00769 {
00770     return test_tensor_attrib( "unsigned_long", MB_TYPE_INTEGER );
00771 }
00772 
00773 bool test_tensor_attrib_long()
00774 {
00775     return test_tensor_attrib( "long", MB_TYPE_INTEGER );
00776 }
00777 
00778 bool test_tensor_attrib_float()
00779 {
00780     return test_tensor_attrib( "float", MB_TYPE_DOUBLE );
00781 }
00782 
00783 bool test_tensor_attrib_double()
00784 {
00785     return test_tensor_attrib( "double", MB_TYPE_DOUBLE );
00786 }
00787 
00788 bool read_file( Interface* iface, const char* file )
00789 {
00790     char fname[] = "tmp_file.vtk";
00791     FILE* fptr   = fopen( fname, "w" );
00792     fputs( file, fptr );
00793     fclose( fptr );
00794 
00795     ErrorCode rval = iface->load_mesh( fname );
00796     remove( fname );
00797     CHECK( rval );
00798     return true;
00799 }
00800 
00801 bool write_and_read( Interface* iface1, Interface* iface2 )
00802 {
00803     const char fname[] = "tmp_file.vtk";
00804     ErrorCode rval1    = iface1->write_mesh( fname );
00805     ErrorCode rval2    = iface2->load_mesh( fname );
00806     remove( fname );
00807     CHECK( rval1 );
00808     CHECK( rval2 );
00809     return true;
00810 }
00811 
00812 bool compare_connectivity( EntityType, const int* conn1, const int* conn2, unsigned len )
00813 {
00814     for( unsigned i = 0; i < len; ++i )
00815         if( conn1[i] != conn2[i] ) return false;
00816     return true;
00817 }
00818 
00819 bool match_vertices_and_elements( Interface* iface, EntityType moab_type, unsigned num_vert, unsigned num_elem,
00820                                   unsigned vert_per_elem, const double* coords, const int* connectivity,
00821                                   EntityHandle* vert_handles, EntityHandle* elem_handles )
00822 {
00823     ErrorCode rval;
00824 
00825     // get vertices and check count
00826     Range verts;
00827     rval = iface->get_entities_by_type( 0, MBVERTEX, verts );
00828     CHECK( rval );
00829     CHECK( verts.size() == num_vert );
00830 
00831     // get elements and check count
00832     Range elems;
00833     rval = iface->get_entities_by_type( 0, moab_type, elems );
00834     CHECK( rval );
00835     CHECK( elems.size() == num_elem );
00836 
00837     // get vertex coordinates
00838     std::vector< EntityHandle > vert_array( num_vert );
00839     std::copy( verts.begin(), verts.end(), vert_array.begin() );
00840     std::vector< double > mb_coords( 3 * num_vert );
00841     rval = iface->get_coords( &vert_array[0], num_vert, &mb_coords[0] );
00842     CHECK( rval );
00843 
00844     // compare vertex coordinates to construct map from
00845     // EntityHandle to index in input coordinate list
00846     std::map< EntityHandle, int > vert_map;
00847     std::vector< bool > seen( num_vert, false );
00848     for( unsigned i = 0; i < num_vert; ++i )
00849     {
00850         double* vert_coords = &mb_coords[3 * i];
00851         bool found          = false;
00852         for( unsigned j = 0; j < num_vert; ++j )
00853         {
00854             const double* file_coords = &coords[3 * j];
00855             double dsqr               = 0;
00856             for( unsigned k = 0; k < 3; ++k )
00857             {
00858                 double diff = vert_coords[k] - file_coords[k];
00859                 dsqr += diff * diff;
00860             }
00861             if( dsqr < 1e-6 )
00862             {
00863                 CHECK( !seen[j] );  // duplicate vertex
00864                 seen[j] = found         = true;
00865                 vert_map[vert_array[i]] = j;
00866                 vert_handles[j]         = vert_array[i];
00867                 break;
00868             }
00869         }
00870         CHECK( found );  // not found?
00871     }
00872 
00873     // check element connectivity
00874     seen.clear();
00875     seen.resize( num_elem, false );
00876     Range::iterator iter = elems.begin();
00877     for( unsigned i = 0; i < num_elem; ++i )
00878     {
00879         // get element connectivity
00880         EntityHandle elem = *iter;
00881         ++iter;
00882         std::vector< EntityHandle > elem_conn;
00883         rval = iface->get_connectivity( &elem, 1, elem_conn );
00884         CHECK( rval );
00885         CHECK( elem_conn.size() == vert_per_elem );
00886 
00887         // convert to input vertex ordering
00888         std::vector< int > elem_conn2( vert_per_elem );
00889         for( unsigned j = 0; j < vert_per_elem; ++j )
00890         {
00891             std::map< EntityHandle, int >::iterator k = vert_map.find( elem_conn[j] );
00892             CHECK( k != vert_map.end() );
00893             elem_conn2[j] = k->second;
00894         }
00895 
00896         // search input list for matching element
00897         bool found = false;
00898         for( unsigned j = 0; j < num_elem; ++j )
00899         {
00900             const int* conn_arr = connectivity + j * vert_per_elem;
00901             if( !seen[j] && compare_connectivity( moab_type, conn_arr, &elem_conn2[0], vert_per_elem ) )
00902             {
00903                 seen[j] = found = true;
00904                 elem_handles[j] = elem;
00905                 break;
00906             }
00907         }
00908         CHECK( found );
00909     }
00910 
00911     return true;
00912 }
00913 
00914 bool check_elements( Interface* iface, EntityType moab_type, unsigned num_elem, unsigned vert_per_elem,
00915                      const double* coords, unsigned num_vert, const int* connectivity )
00916 {
00917     std::vector< EntityHandle > junk1( num_vert ), junk2( num_elem );
00918     bool rval = match_vertices_and_elements( iface, moab_type, num_vert, num_elem, vert_per_elem, coords, connectivity,
00919                                              &junk1[0], &junk2[0] );
00920     CHECK( rval );
00921     return true;
00922 }
00923 
00924 bool test_read_write_element( const double* coords, unsigned num_verts, const int* vtk_conn, const int* moab_conn,
00925                               unsigned num_conn, unsigned num_elem, unsigned vtk_type, EntityType moab_type )
00926 
00927 {
00928     // construct VTK file
00929     char file[4096] = "# vtk DataFile Version 3.0\n"
00930                       "MOAB Version 1.00\n"
00931                       "ASCII\n"
00932                       "DATASET UNSTRUCTURED_GRID\n";
00933     size_t len = strlen( file );
00934 
00935     len += sprintf( file + len, "POINTS %u double\n", num_verts );
00936     for( unsigned i = 0; i < num_verts; ++i )
00937         len += sprintf( file + len, "%f %f %f\n", coords[3 * i], coords[3 * i + 1], coords[3 * i + 2] );
00938 
00939     len += sprintf( file + len, "CELLS %u %u\n", num_elem, num_conn + num_elem );
00940     assert( num_conn % num_elem == 0 );
00941     unsigned conn_len = num_conn / num_elem;
00942     for( unsigned i = 0; i < num_elem; ++i )
00943     {
00944         len += sprintf( file + len, "%u", conn_len );
00945         for( unsigned j = 0; j < conn_len; ++j )
00946             len += sprintf( file + len, " %u", vtk_conn[conn_len * i + j] );
00947         len += sprintf( file + len, "\n" );
00948     }
00949 
00950     len += sprintf( file + len, "CELL_TYPES %u\n", num_elem );
00951     for( unsigned i = 0; i < num_elem; ++i )
00952         len += sprintf( file + len, "%u\n", vtk_type );
00953 
00954     // read VTK file and check results
00955     Core instance1, instance2;
00956     bool bval = read_file( &instance1, file );
00957     CHECK( bval );
00958     bval = check_elements( &instance1, moab_type, num_elem, conn_len, coords, num_verts, moab_conn );
00959     CHECK( bval );
00960 
00961     // write, re-read, and check results
00962     bval = write_and_read( &instance1, &instance2 );
00963     CHECK( bval );
00964     bval = check_elements( &instance2, moab_type, num_elem, conn_len, coords, num_verts, moab_conn );
00965     CHECK( bval );
00966 
00967     return true;
00968 }
00969 
00970 bool test_structured_2d( const char* file )
00971 {
00972     // read VTK file and check results
00973     Core instance;
00974     bool bval = read_file( &instance, file );
00975     CHECK( bval );
00976     bval = check_elements( &instance, MBQUAD, 9, 4, grid_3x3, 16, quad_structured_conn );
00977     CHECK( bval );
00978 
00979     return true;
00980 }
00981 
00982 bool test_structured_3d( const char* file )
00983 {
00984     // read VTK file and check results
00985     Core instance;
00986     bool bval = read_file( &instance, file );
00987     CHECK( bval );
00988     bval = check_elements( &instance, MBHEX, 8, 8, grid_2x2x2, 27, hex_structured_conn );
00989     CHECK( bval );
00990 
00991     return true;
00992 }
00993 
00994 const char two_quad_mesh[] = "# vtk DataFile Version 3.0\n"
00995                              "MOAB Version 1.00\n"
00996                              "ASCII\n"
00997                              "DATASET UNSTRUCTURED_GRID\n"
00998                              "POINTS 6 float\n"
00999                              "-1 0 0\n"
01000                              " 0 0 0\n"
01001                              " 1 0 0\n"
01002                              "-1 1 0\n"
01003                              " 0 1 0\n"
01004                              " 1 1 0\n"
01005                              "CELLS 2 10\n"
01006                              "4 0 1 4 3\n"
01007                              "4 1 2 5 4\n"
01008                              "CELL_TYPES 2\n"
01009                              "9 9\n";
01010 
01011 const double two_quad_mesh_coords[] = { -1, 0, 0, 0, 0, 0, 1, 0, 0, -1, 1, 0, 0, 1, 0, 1, 1, 0 };
01012 const int two_quad_mesh_conn[]      = { 0, 1, 4, 3, 1, 2, 5, 4 };
01013 
01014 const int vertex_values[]  = { 9, 3, 8, 2, 0, 6, 4, 1, 4, 1, 0, 3, 8, 6, 6, 4, 0, 2, 1, 2, 3, 4, 5, 6, 6, 5, 4,
01015                               3, 2, 1, 0, 6, 1, 5, 2, 4, 3, 6, 9, 2, 5, 8, 1, 3, 5, 7, 1, 3, 5, 8, 1, 9, 7, 4 };
01016 const int element_values[] = { 1001, 1002, 1004, 1003, 50, 60, 51, 61, 1, 2, 3, 4, 5, 6,
01017                                7,    8,    9,    0,    0,  9,  8,  7,  6, 5, 4, 3, 2, 1 };
01018 
01019 void write_data( char* file, size_t& len, DataType type, unsigned count, const int* vals )
01020 {
01021     switch( type )
01022     {
01023         case MB_TYPE_BIT:
01024             for( unsigned i = 0; i < count; ++i )
01025                 len += sprintf( file + len, "%d\n", abs( vals[i] ) % 2 );
01026             break;
01027         case MB_TYPE_INTEGER:
01028             for( unsigned i = 0; i < count; ++i )
01029                 len += sprintf( file + len, "%d\n", vals[i] );
01030             break;
01031         case MB_TYPE_DOUBLE:
01032             for( unsigned i = 0; i < count; ++i )
01033                 len += sprintf( file + len, "%f\n", (double)vals[i] );
01034             break;
01035         case MB_TYPE_OPAQUE:
01036             for( unsigned i = 0; i < count; ++i )
01037                 len += sprintf( file + len, "%d\n", abs( vals[i] % 256 ) );
01038             break;
01039         default:
01040             assert( false /* VTK files cannot handle this type */ );
01041     }
01042 }
01043 
01044 bool check_tag_values( Interface* iface, DataType tag_type, int tag_length, int num_entities,
01045                        const EntityHandle* entities, const int* values )
01046 {
01047     Tag tag;
01048     ErrorCode rval = iface->tag_get_handle( "data", tag_length, tag_type, tag );
01049     CHECK( rval );
01050 
01051     int size, *intptr;
01052     double* dblptr;
01053     rval = iface->tag_get_bytes( tag, size );
01054     CHECK( rval );
01055     std::vector< unsigned char > data( size * num_entities );
01056 
01057     switch( tag_type )
01058     {
01059         case MB_TYPE_BIT:
01060             rval = iface->tag_get_length( tag, size );
01061             CHECK( rval );
01062             CHECK( tag_length == size );
01063             for( int i = 0; i < num_entities; ++i )
01064             {
01065                 unsigned char val;
01066                 rval = iface->tag_get_data( tag, entities + i, 1, &val );
01067                 CHECK( rval );
01068                 for( int j = 0; j < tag_length; ++j )
01069                 {
01070                     int bitval = !!( val & ( 1 << j ) );
01071                     int expval = abs( *values ) % 2;
01072                     CHECK( bitval == expval );
01073                     ++values;
01074                 }
01075             }
01076             break;
01077         case MB_TYPE_OPAQUE:
01078             rval = iface->tag_get_data( tag, entities, num_entities, &data[0] );
01079             CHECK( rval );
01080             CHECK( tag_length == size );
01081             for( int i = 0; i < num_entities; ++i )
01082                 for( int j = 0; j < tag_length; ++j, ++values )
01083                     CHECK( (unsigned)( *values % 256 ) == data[i * tag_length + j] );
01084             break;
01085         case MB_TYPE_INTEGER:
01086             rval = iface->tag_get_data( tag, entities, num_entities, &data[0] );
01087             CHECK( rval );
01088             CHECK( tag_length * sizeof( int ) == (unsigned)size );
01089             intptr = reinterpret_cast< int* >( &data[0] );
01090             for( int i = 0; i < num_entities; ++i )
01091                 for( int j = 0; j < tag_length; ++j, ++values )
01092                     CHECK( *values == intptr[i * tag_length + j] );
01093             break;
01094         case MB_TYPE_DOUBLE:
01095             rval = iface->tag_get_data( tag, entities, num_entities, &data[0] );
01096             CHECK( rval );
01097             CHECK( tag_length * sizeof( double ) == (unsigned)size );
01098             dblptr = reinterpret_cast< double* >( &data[0] );
01099             for( int i = 0; i < num_entities; ++i )
01100                 for( int j = 0; j < tag_length; ++j, ++values )
01101                     CHECK( *values == dblptr[i * tag_length + j] );
01102             break;
01103         default:
01104             assert( false );
01105             return false;
01106     }
01107     return true;
01108 }
01109 
01110 bool check_tag_values( Interface* iface, DataType type, int vals_per_ent )
01111 {
01112     EntityHandle vert_handles[6], elem_handles[2];
01113     bool rval = match_vertices_and_elements( iface, MBQUAD, 6, 2, 4, two_quad_mesh_coords, two_quad_mesh_conn,
01114                                              vert_handles, elem_handles );
01115     CHECK( rval );
01116 
01117     rval = check_tag_values( iface, type, vals_per_ent, 6, vert_handles, vertex_values );
01118     CHECK( rval );
01119     rval = check_tag_values( iface, type, vals_per_ent, 2, elem_handles, element_values );
01120     CHECK( rval );
01121     return rval;
01122 }
01123 
01124 bool check_tag_data( const char* file, DataType type, int vals_per_ent )
01125 {
01126     bool bval;
01127     Core instance1, instance2;
01128 
01129     bval = read_file( &instance1, file );
01130     CHECK( bval );
01131     bval = check_tag_values( &instance1, type, vals_per_ent );
01132     CHECK( bval );
01133     bval = write_and_read( &instance1, &instance2 );
01134     CHECK( bval );
01135     bval = check_tag_values( &instance2, type, vals_per_ent );
01136     CHECK( bval );
01137     return true;
01138 }
01139 
01140 bool test_scalar_attrib( const char* vtk_type, DataType mb_type, int count )
01141 {
01142     char file[4096];
01143     strcpy( file, two_quad_mesh );
01144     size_t len = strlen( file );
01145     len += sprintf( file + len, "POINT_DATA 6\n" );
01146     len += sprintf( file + len, "SCALARS data %s %d\n", vtk_type, count );
01147     len += sprintf( file + len, "LOOKUP_TABLE default\n" );
01148     write_data( file, len, mb_type, 6 * count, vertex_values );
01149     len += sprintf( file + len, "CELL_DATA 2\n" );
01150     len += sprintf( file + len, "SCALARS data %s %d\n", vtk_type, count );
01151     len += sprintf( file + len, "LOOKUP_TABLE default\n" );
01152     write_data( file, len, mb_type, 2 * count, element_values );
01153 
01154     return check_tag_data( file, mb_type, count );
01155 }
01156 
01157 bool test_vector_attrib( const char* vtk_type, DataType mb_type )
01158 {
01159     char file[4096];
01160     strcpy( file, two_quad_mesh );
01161     size_t len = strlen( file );
01162     len += sprintf( file + len, "POINT_DATA 6\n" );
01163     len += sprintf( file + len, "VECTORS data %s\n", vtk_type );
01164     write_data( file, len, mb_type, 6 * 3, vertex_values );
01165     len += sprintf( file + len, "CELL_DATA 2\n" );
01166     len += sprintf( file + len, "VECTORS data %s\n", vtk_type );
01167     write_data( file, len, mb_type, 2 * 3, element_values );
01168 
01169     return check_tag_data( file, mb_type, 3 );
01170 }
01171 
01172 bool test_tensor_attrib( const char* vtk_type, DataType mb_type )
01173 {
01174     char file[4096];
01175     strcpy( file, two_quad_mesh );
01176     size_t len = strlen( file );
01177     len += sprintf( file + len, "POINT_DATA 6\n" );
01178     len += sprintf( file + len, "TENSORS data %s\n", vtk_type );
01179     write_data( file, len, mb_type, 6 * 9, vertex_values );
01180     len += sprintf( file + len, "CELL_DATA 2\n" );
01181     len += sprintf( file + len, "TENSORS data %s\n", vtk_type );
01182     write_data( file, len, mb_type, 2 * 9, element_values );
01183 
01184     return check_tag_data( file, mb_type, 9 );
01185 }
01186 
01187 bool test_subset()
01188 {
01189     Core moab_inst;
01190     Interface& moab = moab_inst;
01191     ErrorCode rval;
01192 
01193     // create 9 nodes in grid pattern
01194     EntityHandle verts[9];
01195     const double coords[][3] = { { 0, 0, 0 }, { 1, 0, 0 }, { 2, 0, 0 }, { 0, 1, 0 }, { 1, 1, 0 },
01196                                  { 2, 1, 0 }, { 0, 2, 0 }, { 1, 2, 0 }, { 2, 2, 0 } };
01197     for( unsigned i = 0; i < 9; ++i )
01198     {
01199         rval = moab.create_vertex( coords[i], verts[i] );
01200         assert( MB_SUCCESS == rval );
01201     }
01202 
01203     // create 4 quad elements in grid pattern
01204     const int conn[][4] = { { 0, 1, 4, 3 }, { 1, 2, 5, 4 }, { 3, 4, 7, 6 }, { 4, 5, 8, 7 } };
01205     EntityHandle econn[4], elems[4];
01206     for( unsigned i = 0; i < 4; ++i )
01207     {
01208         for( unsigned j = 0; j < 4; ++j )
01209             econn[j] = verts[conn[i][j]];
01210         rval = moab.create_element( MBQUAD, econn, 4, elems[i] );
01211         assert( MB_SUCCESS == rval );
01212     }
01213 
01214     // create 3 meshsets
01215     EntityHandle sets[3];
01216     for( unsigned i = 0; i < 3; ++i )
01217     {
01218         rval = moab.create_meshset( 0, sets[i] );
01219         assert( MB_SUCCESS == rval );
01220     }
01221 
01222     // add element 3 to set 0
01223     rval = moab.add_entities( sets[0], elems + 3, 1 );
01224     assert( MB_SUCCESS == rval );
01225     // add node 2 to set 1
01226     rval = moab.add_entities( sets[1], verts + 2, 1 );
01227     assert( MB_SUCCESS == rval );
01228     // add element 2 and 3 to set 2
01229     rval = moab.add_entities( sets[2], elems + 2, 2 );
01230     assert( MB_SUCCESS == rval );
01231 
01232     // make set 2 a child of set 1
01233     rval = moab.add_child_meshset( sets[1], sets[2] );
01234     assert( MB_SUCCESS == rval );
01235     // put set 1 in set 0
01236     rval = moab.add_entities( sets[0], sets + 1, 1 );
01237     assert( MB_SUCCESS == rval );
01238 
01239     // write sets[0] to vtk file
01240     rval = moab.write_mesh( "tmp_file.vtk", sets, 1 );
01241     CHECK( rval );
01242 
01243     // read data back in
01244     moab.delete_mesh();
01245     rval = moab.load_mesh( "tmp_file.vtk" );
01246     remove( "tmp_file.vtk" );
01247     CHECK( rval );
01248 
01249     // writer should have written all three sets,
01250     // so the resulting mesh should be elems[2], elems[3],
01251     // and verts[2]
01252     Range new_elems, new_verts;
01253     rval = moab.get_entities_by_type( 0, MBQUAD, new_elems );
01254     CHECK( rval );
01255     CHECK( new_elems.size() == 2 );
01256     rval = moab.get_entities_by_type( 0, MBVERTEX, new_verts );
01257     CHECK( rval );
01258     CHECK( new_verts.size() == 7 );
01259 
01260     // vertex not in element closure should have coords of 2,0,0
01261     Range elem_verts;
01262     rval = moab.get_adjacencies( new_elems, 0, false, elem_verts, Interface::UNION );
01263     CHECK( rval );
01264     CHECK( elem_verts.size() == 6 );
01265     Range free_verts( subtract( new_verts, elem_verts ) );
01266     CHECK( free_verts.size() == 1 );
01267     double vcoords[3];
01268     rval = moab.get_coords( free_verts, vcoords );
01269     CHECK( vcoords[0] == 2 );
01270     CHECK( vcoords[1] == 0 );
01271     CHECK( vcoords[2] == 0 );
01272 
01273     return true;
01274 }
01275 
01276 bool test_write_free_nodes()
01277 {
01278     Core moab_inst;
01279     Interface& moab = moab_inst;
01280     ErrorCode rval;
01281 
01282     // create 9 nodes in grid pattern
01283     EntityHandle verts[9];
01284     const double coords[][3] = { { 0, 0, 0 }, { 1, 0, 0 }, { 2, 0, 0 }, { 0, 1, 0 }, { 1, 1, 0 },
01285                                  { 2, 1, 0 }, { 0, 2, 0 }, { 1, 2, 0 }, { 2, 2, 0 } };
01286     for( unsigned i = 0; i < 9; ++i )
01287     {
01288         rval = moab.create_vertex( coords[i], verts[i] );
01289         assert( MB_SUCCESS == rval );
01290     }
01291 
01292     // create 3 quad elements, one node (8) not used
01293     const int conn[][4] = { { 0, 1, 4, 3 }, { 1, 2, 5, 4 }, { 3, 4, 7, 6 } };
01294 
01295     Tag gid;
01296     rval = moab.tag_get_handle( "GLOBAL_ID", 1, moab::MB_TYPE_INTEGER, gid );
01297     assert( MB_SUCCESS == rval );
01298     EntityHandle econn[4], elems[3];
01299     for( unsigned i = 0; i < 3; ++i )
01300     {
01301         for( unsigned j = 0; j < 4; ++j )
01302             econn[j] = verts[conn[i][j]];
01303         rval = moab.create_element( MBQUAD, econn, 4, elems[i] );
01304         assert( MB_SUCCESS == rval );
01305         int id = i + 1;
01306         rval   = moab.tag_set_data( gid, &elems[i], 1, &id );
01307         assert( MB_SUCCESS == rval );
01308     }
01309 
01310     rval = moab.write_file( "tmp_file.vtk" );
01311     CHECK( rval );
01312 
01313     rval = moab.write_file( "tmp_file2.vtk", 0, "CREATE_ONE_NODE_CELLS;" );
01314     CHECK( rval );
01315 
01316     // read data back in
01317     moab.delete_mesh();
01318     rval = moab.load_file( "tmp_file.vtk" );
01319     remove( "tmp_file.vtk" );
01320     remove( "tmp_file2.vtk" );
01321     CHECK( rval );
01322 
01323     return true;
01324 }
01325 
01326 // Test technically invalid but somewhat common insertion of
01327 // FIELD blocks within an UNSTRUCTURED_GRID dataset
01328 bool test_unstructured_field()
01329 {
01330     // Use existing file defined in 'two_quad_mesh', but
01331     // insert a few field data blocks
01332     std::istringstream base_data( two_quad_mesh );
01333     std::ostringstream file_data;
01334     std::string line;
01335     while( getline( base_data, line ) )
01336     {
01337         if( 0 == line.find( "POINTS" ) )
01338         {
01339             file_data << "FIELD FieldData 2" << std::endl
01340                       << "avtOriginalBounds 1 6 float" << std::endl
01341                       << "-10 10 -10 10 -10 10 " << std::endl
01342                       << "TIME 1 1 double" << std::endl
01343                       << "10.543" << std::endl;
01344         }
01345         else if( 0 == line.find( "CELLS" ) )
01346         {
01347             file_data << "FIELD more_data 2" << std::endl
01348                       << "first_array 3 2 int" << std::endl
01349                       << "0 1 2" << std::endl
01350                       << "3 4 5" << std::endl
01351                       << "second_array 4 3 bit" << std::endl
01352                       << "0 0 0 0" << std::endl
01353                       << "1 1 1 1" << std::endl
01354                       << "1 0 1 0" << std::endl;
01355         }
01356         file_data << line << std::endl;
01357     }
01358 
01359     Core core;
01360     Interface& mb = core;
01361     bool rval     = read_file( &mb, file_data.str().c_str() );
01362     CHECK( rval );
01363 
01364     EntityHandle vert_handles[6], elem_handles[2];
01365     rval = match_vertices_and_elements( &mb, MBQUAD, 6, 2, 4, two_quad_mesh_coords, two_quad_mesh_conn, vert_handles,
01366                                         elem_handles );
01367     CHECK( rval );
01368 
01369     return true;
01370 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines