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