MOAB: Mesh Oriented datABase  (version 5.5.0)
VtkTest.cpp
Go to the documentation of this file.
1 #include "moab/Core.hpp"
2 #include "moab/Range.hpp"
3 
4 using namespace moab;
5 
6 #include <cstring>
7 #include <cstdio>
8 #include <cstdlib>
9 #include <cassert>
10 #include <map>
11 #include <vector>
12 #include <algorithm>
13 #include <sstream>
14 
15 #include "TestUtil.hpp"
16 
17 std::string poly_example = TestDir + "unittest/io/poly8-10.vtk";
18 std::string polyhedra_example = TestDir + "unittest/io/polyhedra.vtk";
19 
20 #define DECLARE_TEST( A ) \
21  bool test_##A(); \
22  int A##_reg_var = register_test( &test_##A, #A );
23 
24 typedef bool ( *test_ptr )();
25 struct test_data
26 {
28  const char* name;
29  bool result;
30 };
31 size_t num_tests = 0;
33 int register_test( test_ptr test, const char* name )
34 {
35  test_data* new_test_array = (test_data*)realloc( test_array, sizeof( test_data ) * ( num_tests + 1 ) );
36  if( !new_test_array )
37  {
38  fprintf( stderr, "VtkTest.cpp::regeister_test(): reallocation of test array failed\n" );
39  free( test_array );
40  test_array = NULL;
41  num_tests = 0;
42  return -1;
43  }
44  else
45  test_array = new_test_array;
47  test_array[num_tests].name = name;
48  test_array[num_tests].result = true;
49  ++num_tests;
50  return 0;
51 }
52 
53 DECLARE_TEST( edge2 )
54 DECLARE_TEST( edge3 )
55 DECLARE_TEST( tri3 )
56 DECLARE_TEST( tri6 )
57 DECLARE_TEST( quad4 )
58 DECLARE_TEST( quad8 )
59 DECLARE_TEST( quad9 )
60 DECLARE_TEST( polygon )
61 DECLARE_TEST( polygon_mix )
62 DECLARE_TEST( polyhedra )
63 DECLARE_TEST( tet4 )
64 DECLARE_TEST( tet10 )
65 DECLARE_TEST( hex8 )
66 DECLARE_TEST( hex20 )
67 DECLARE_TEST( hex27 )
69 DECLARE_TEST( wedge15 )
70 DECLARE_TEST( pyramid )
71 DECLARE_TEST( pyramid13 )
72 
73 DECLARE_TEST( structured_points_2d )
74 DECLARE_TEST( free_nodes )
75 // DECLARE_TEST(free_nodes_and_triangle)
76 
77 DECLARE_TEST( structured_grid_2d )
78 DECLARE_TEST( rectilinear_grid_2d )
79 DECLARE_TEST( structured_points_3d )
80 DECLARE_TEST( structured_grid_3d )
81 DECLARE_TEST( rectilinear_grid_3d )
82 
83 DECLARE_TEST( scalar_attrib_1_bit )
84 DECLARE_TEST( scalar_attrib_1_uchar )
85 DECLARE_TEST( scalar_attrib_1_char )
86 DECLARE_TEST( scalar_attrib_1_ushort )
87 DECLARE_TEST( scalar_attrib_1_short )
88 DECLARE_TEST( scalar_attrib_1_uint )
89 DECLARE_TEST( scalar_attrib_1_int )
90 DECLARE_TEST( scalar_attrib_1_ulong )
91 DECLARE_TEST( scalar_attrib_1_long )
92 DECLARE_TEST( scalar_attrib_1_float )
93 DECLARE_TEST( scalar_attrib_1_double )
94 
95 DECLARE_TEST( scalar_attrib_4_bit )
96 DECLARE_TEST( scalar_attrib_4_uchar )
97 DECLARE_TEST( scalar_attrib_4_char )
98 DECLARE_TEST( scalar_attrib_4_ushort )
99 DECLARE_TEST( scalar_attrib_4_short )
100 DECLARE_TEST( scalar_attrib_4_uint )
101 DECLARE_TEST( scalar_attrib_4_int )
102 DECLARE_TEST( scalar_attrib_4_ulong )
103 DECLARE_TEST( scalar_attrib_4_long )
104 DECLARE_TEST( scalar_attrib_4_float )
105 DECLARE_TEST( scalar_attrib_4_double )
106 
107 DECLARE_TEST( vector_attrib_bit )
108 DECLARE_TEST( vector_attrib_uchar )
109 DECLARE_TEST( vector_attrib_char )
110 DECLARE_TEST( vector_attrib_ushort )
111 DECLARE_TEST( vector_attrib_short )
112 DECLARE_TEST( vector_attrib_uint )
113 DECLARE_TEST( vector_attrib_int )
114 DECLARE_TEST( vector_attrib_ulong )
115 DECLARE_TEST( vector_attrib_long )
116 DECLARE_TEST( vector_attrib_float )
117 DECLARE_TEST( vector_attrib_double )
118 
119 DECLARE_TEST( tensor_attrib_uchar )
120 DECLARE_TEST( tensor_attrib_char )
121 DECLARE_TEST( tensor_attrib_ushort )
122 DECLARE_TEST( tensor_attrib_short )
123 DECLARE_TEST( tensor_attrib_uint )
124 DECLARE_TEST( tensor_attrib_int )
125 DECLARE_TEST( tensor_attrib_ulong )
126 DECLARE_TEST( tensor_attrib_long )
127 DECLARE_TEST( tensor_attrib_float )
128 DECLARE_TEST( tensor_attrib_double )
129 
130 DECLARE_TEST( subset )
131 DECLARE_TEST( write_free_nodes )
132 
133 DECLARE_TEST( unstructured_field )
134 
135 int main( int argc, char* argv[] )
136 {
137  int* test_indices = (int*)malloc( sizeof( int ) * num_tests );
138  int test_count;
139  // if no arguments, do all tests
140  if( argc == 1 )
141  {
142  for( unsigned i = 0; i < num_tests; ++i )
143  test_indices[i] = i;
144  test_count = num_tests;
145  }
146  // otherwise run only specified tests
147  else
148  {
149  test_count = 0;
150  for( int i = 1; i < argc; ++i )
151  for( unsigned j = 0; j < num_tests; ++j )
152  if( !strcmp( test_array[j].name, argv[i] ) ) test_indices[test_count++] = j;
153  }
154 
155  int fail_count = 0;
156  for( int i = 0; i < test_count; ++i )
157  {
158  test_data& test = test_array[test_indices[i]];
159  printf( "Testing %s...\n", test.name );
160  if( !( test.result = test.test() ) ) ++fail_count;
161  }
162 
163  printf( "\n\n" );
164  if( fail_count )
165  {
166  printf( "FAILED TESTS:\n" );
167  for( int i = 0; i < test_count; ++i )
168  {
169  test_data& test = test_array[test_indices[i]];
170  if( !test.result ) printf( "\t%s\n", test.name );
171  }
172  }
173 
174  if( test_count == 0 )
175  printf( "0 VTK tests run\n" );
176  else if( fail_count == 0 )
177  printf( "%d tests passed\n", test_count );
178  else
179  printf( "%d of %d tests failed\n", fail_count, test_count );
180  printf( "\n" );
181 
182  free( test_indices );
183  free( test_array );
184 
185  return fail_count;
186 }
187 // CHECK is defined in TestUtil now
188 #undef CHECK
189 #define CHECK( A ) \
190  if( is_error( ( A ) ) ) return do_error( #A, __LINE__ )
191 static bool do_error( const char* string, int line )
192 {
193  fprintf( stderr, "Check failed at line %d: %s\n", line, string );
194  return false;
195 }
196 static inline bool is_error( bool b )
197 {
198  return !b;
199 }
200 
201 // static bool do_error( ErrorCode err, int line )
202 //{
203 // Core tmp_core;
204 // fprintf(stderr, "API failed at line %d: %s (%d)\n",
205 // line, tmp_core.get_error_string(err).c_str(), (int)err );
206 // return false;
207 //}
208 static inline bool is_error( ErrorCode b )
209 {
210  return MB_SUCCESS != b;
211 }
212 
213 bool read_file( Interface* iface, const char* file );
214 bool write_and_read( Interface* iface1, Interface* iface2 );
215 
216 bool test_read_write_element( const double* coords,
217  unsigned num_coords,
218  const int* vtk_conn,
219  const int* moab_conn,
220  unsigned num_conn,
221  unsigned num_elem,
222  unsigned vtk_type,
223  EntityType moab_type );
224 
226 {
227  const double coords[] = { 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1 };
228  const int conn[] = { 0, 1, 1, 2, 2, 3, 3, 4, 0, 4 };
229 
230  return test_read_write_element( coords, 5, conn, conn, 10, 5, 3, MBEDGE );
231 }
232 
234 {
235  const double coords[] = { -1, -1, 2, 1, -1, 2, 1, 1, 2, -1, 1, 2,
236  0.000, -0.707, 2, 0.707, 0.000, 2, 0.000, 0.707, 2, -0.707, 0.000, 2 };
237  const int conn[] = { 0, 1, 4, 1, 2, 5, 2, 3, 6, 3, 0, 7 };
238 
239  return test_read_write_element( coords, 8, conn, conn, 12, 4, 21, MBEDGE );
240 }
241 
242 bool test_tri3()
243 {
244  const double coords[] = { 0, 0, 0, 5, 0, 0, 0, 5, 0, -5, 0, 0, 0, -5, 0 };
245  const int conn[] = { 0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 1 };
246 
247  return test_read_write_element( coords, 5, conn, conn, 12, 4, 5, MBTRI );
248 }
249 
250 bool test_tri6()
251 {
252  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 };
253  const int conn[] = { 0, 1, 3, 4, 5, 8, 1, 2, 3, 6, 7, 8 };
254 
255  return test_read_write_element( coords, 9, conn, conn, 12, 2, 22, MBTRI );
256 }
257 
258 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,
259  0, 2, 0, 1, 2, 0, 2, 2, 0, 3, 2, 0, 0, 3, 0, 1, 3, 0, 2, 3, 0, 3, 3, 0 };
260 
261 const int quad_structured_conn[] = { 0, 1, 5, 4, 1, 2, 6, 5, 2, 3, 7, 6, 4, 5, 9, 8, 5, 6,
262  10, 9, 6, 7, 11, 10, 8, 9, 13, 12, 9, 10, 14, 13, 10, 11, 15, 14 };
263 
265 {
266  // test read as quads
268 
269  // test read as pixels
270  const int conn2[] = { 0, 1, 4, 5, 1, 2, 5, 6, 2, 3, 6, 7, 4, 5, 8, 9, 5, 6,
271  9, 10, 6, 7, 10, 11, 8, 9, 12, 13, 9, 10, 13, 14, 10, 11, 14, 15 };
272  bool rval2 = test_read_write_element( grid_3x3, 16, conn2, quad_structured_conn, 36, 9, 8, MBQUAD );
273 
274  return rval1 && rval2;
275 }
276 
278 {
279  const double coords[] = { 0, 0, 0, 0, 2, 0, 0, 4, 0, 0, 0, 4, 0, 2, 4, 0, 4, 4, 4, 0,
280  0, 4, 2, 0, 4, 4, 0, 2, 0, 0, 2, 4, 0, 0, 0, 2, 0, 4, 2 };
281  const int conn[] = { 0, 2, 5, 3, 1, 12, 4, 11, 2, 0, 6, 8, 1, 9, 7, 10 };
282 
283  return test_read_write_element( coords, 13, conn, conn, 16, 2, 23, MBQUAD );
284 }
285 
287 {
288  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,
289  0, 4, 4, 0, 2, 0, 0, 2, 2, 0, 2, 4, 0, 0, 0, 2, 0, 2, 2, 0, 4, 2 };
290  const int conn[] = { 0, 2, 5, 3, 1, 14, 4, 12, 12, 2, 0, 6, 8, 1, 9, 7, 11, 10 };
291 
292  return test_read_write_element( coords, 15, conn, conn, 18, 2, 28, MBQUAD );
293 }
294 
296 {
297  const double coords[] = { 0, 0, 0, 0, 2, 0, 0, 4, 0, 0, 0, 4, 0, 2, 4, 0, 4, 4, 4, 0,
298  0, 4, 2, 0, 4, 4, 0, 2, 0, 0, 2, 4, 0, 0, 0, 2, 0, 4, 2 };
299  const int conn[] = { 0, 1, 2, 12, 5, 4, 3, 11, 2, 1, 0, 9, 6, 7, 8, 10 };
300 
301  return test_read_write_element( coords, 13, conn, conn, 16, 2, 7, MBPOLYGON );
302 }
303 
305 {
306  // just read the polygon file with mixed sequences
307  Core moab;
308  Interface& mb = moab;
309 
310  ErrorCode rval = mb.load_file( poly_example.c_str() );
311  if( MB_SUCCESS != rval ) return false;
312 
313  return true;
314 }
316 {
317  // just read the polyhedra file
318  Core moab;
319  Interface& mb = moab;
320 
321  ErrorCode rval = mb.load_file( polyhedra_example.c_str() );
322  if( MB_SUCCESS != rval ) return false;
323  Range polyhedras;
324  rval = mb.get_entities_by_type( 0, MBPOLYHEDRON, polyhedras );
325  if( MB_SUCCESS != rval ) return false;
326 
327  if( 10 != polyhedras.size() ) return false;
328  return true;
329 }
330 bool test_tet4()
331 {
332  const double coords[] = { 1, -1, 0, 1, 1, 0, -1, 1, 0, -1, -1, 0, 0, 0, -1, 0, 0, 1 };
333  const int conn[] = { 0, 1, 3, 5, 1, 2, 3, 5, 0, 1, 4, 3, 1, 2, 4, 3 };
334 
335  return test_read_write_element( coords, 6, conn, conn, 16, 4, 10, MBTET );
336 }
337 
339 {
340  const double coords[] = { 4, 0, 0, 0, 2, 0, 0, -2, 0, 0, 0, -2, 0, 0, 2, 0, 1, 1, 2, 0, 1,
341  0, -1, 1, 0, 0, 0, 2, 1, 0, 2, -1, 0, 2, 0, -1, 0, -1, -1, 0, 1, -1 };
342  const int conn[] = { 0, 1, 2, 4, 9, 8, 10, 6, 5, 7, 2, 1, 0, 3, 8, 9, 10, 12, 13, 11 };
343 
344  return test_read_write_element( coords, 14, conn, conn, 20, 2, 24, MBTET );
345 }
346 
347 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,
348  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,
349  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 };
350 
351 const int hex_structured_conn[] = { 0, 1, 4, 3, 9, 10, 13, 12, 1, 2, 5, 4, 10, 11, 14, 13,
352  3, 4, 7, 6, 12, 13, 16, 15, 4, 5, 8, 7, 13, 14, 17, 16,
353  9, 10, 13, 12, 18, 19, 22, 21, 10, 11, 14, 13, 19, 20, 23, 22,
354  12, 13, 16, 15, 21, 22, 25, 24, 13, 14, 17, 16, 22, 23, 26, 25 };
355 
356 bool test_hex8()
357 {
358  // check vtk hexes
360  CHECK( rval1 );
361 
362  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,
363  15, 16, 4, 5, 7, 8, 13, 14, 16, 17, 9, 10, 12, 13, 18, 19, 21, 22, 10, 11, 13, 14,
364  19, 20, 22, 23, 12, 13, 15, 16, 21, 22, 24, 25, 13, 14, 16, 17, 22, 23, 25, 26 };
365 
366  // check with vtk voxels
367  bool rval2 = test_read_write_element( grid_2x2x2, 27, conn2, hex_structured_conn, 64, 8, 11, MBHEX );
368  CHECK( rval2 );
369 
370  return true;
371 }
372 
374 {
375  const int vtk_conn[] = { 0, 2, 8, 6, 18, 20, 26, 24, 1, 5, 7, 3, 19, 23, 25, 21, 9, 11, 17, 15 };
376  const int exo_conn[] = { 0, 2, 8, 6, 18, 20, 26, 24, 1, 5, 7, 3, 9, 11, 17, 15, 19, 23, 25, 21 };
377 
378  return test_read_write_element( grid_2x2x2, 27, vtk_conn, exo_conn, 20, 1, 25, MBHEX );
379 }
380 
382 {
383  const int vtk_conn[] = { 0, 2, 8, 6, 18, 20, 26, 24, 1, 5, 7, 3, 19, 23,
384  25, 21, 9, 11, 17, 15, 10, 16, 14, 12, 4, 22, 13 };
385  const int moab_conn[] = { 0, 2, 8, 6, 18, 20, 26, 24, 1, 5, 7, 3, 9, 11,
386  17, 15, 19, 23, 25, 21, 14, 16, 12, 10, 4, 22, 13 };
387 
388  return test_read_write_element( grid_2x2x2, 27, vtk_conn, moab_conn, 27, 1, 29, MBHEX );
389 }
390 
392 {
393  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 };
394  const int exo_conn[] = { 0, 1, 3, 4, 5, 7, 1, 2, 3, 5, 6, 7 };
395  const int vtk_conn[] = { 0, 3, 1, 4, 7, 5, 1, 3, 2, 5, 7, 6 };
396  return test_read_write_element( coords, 8, vtk_conn, exo_conn, 12, 2, 13, MBPRISM );
397 }
398 
400 {
401  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,
402  0, 2, 2, 1, 0, 1, 2, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 2, 1, 2, 1, 2,
403  2, 0, 1, 2, 1, 0, 2, 1, 1, 2, 2, 0, 1, 2, 2, 1, 0, 2, 1, 0, 0, 1 };
404  const int exo_conn[] = { 0, 1, 3, 4, 5, 7, 8, 12, 11, 18, 19, 21, 13, 17, 16,
405  1, 2, 3, 5, 6, 7, 9, 10, 12, 19, 20, 21, 14, 15, 17 };
406  const int vtk_conn[] = { 0, 3, 1, 4, 7, 5, 11, 12, 8, 16, 17, 13, 18, 21, 19,
407  1, 3, 2, 5, 7, 6, 12, 10, 9, 17, 15, 14, 19, 21, 20 };
408  return test_read_write_element( coords, 22, vtk_conn, exo_conn, 30, 2, 26, MBPRISM );
409 }
410 
412 {
413  const double coords[] = { 1, -1, 0, 1, 1, 0, -1, 1, 0, -1, -1, 0, 0, 0, -1, 0, 0, 1 };
414  const int conn[] = { 0, 1, 2, 3, 5, 3, 2, 1, 0, 4 };
415 
416  return test_read_write_element( coords, 6, conn, conn, 10, 2, 14, MBPYRAMID );
417 }
418 
420 {
421  const double coords[] = { 2, -2, 0, 2, 2, 0, -2, 2, 0, -2, -2, 0, 0, 0, -2, 0, 0, 2,
422  2, 0, 0, 0, 2, 0, -2, 0, 0, 0, -2, 0, 1, -1, -1, 1, 1, -1,
423  -1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, -1, 1 };
424  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 };
425 
426  return test_read_write_element( coords, 18, conn, conn, 26, 2, 27, MBPYRAMID );
427 }
428 
429 bool test_structured_2d( const char* file );
430 bool test_structured_3d( const char* file );
431 
433 {
434  const char file[] = "# vtk DataFile Version 3.0\n"
435  "MOAB Version 1.00\n"
436  "ASCII\n"
437  "DATASET STRUCTURED_POINTS\n"
438  "DIMENSIONS 4 4 1\n"
439  "ORIGIN 0 0 0\n"
440  "SPACING 1 1 1\n";
441  bool rval1 = test_structured_2d( file );
442 
443  // test again w/ old 1.0 ASPECT_RATIO keyword
444  const char file2[] = "# vtk DataFile Version 3.0\n"
445  "MOAB Version 1.00\n"
446  "ASCII\n"
447  "DATASET STRUCTURED_POINTS\n"
448  "DIMENSIONS 4 4 1\n"
449  "ORIGIN 0 0 0\n"
450  "ASPECT_RATIO 1 1 1\n";
451  bool rval2 = test_structured_2d( file2 );
452 
453  return rval1 && rval2;
454 }
455 bool test_free_vertices( const char* file )
456 {
457  // read VTK file
458  Core instance;
459  bool bval = read_file( &instance, file );
460  CHECK( bval );
461  return true;
462 }
463 
465 {
466  const char file1[] = "# vtk DataFile Version 3.0\n"
467  "MOAB Version 1.00\n"
468  "ASCII\n"
469  "DATASET UNSTRUCTURED_GRID\n"
470  "POINTS 2 double\n"
471  "10.0 0 0\n"
472  "-10.0 0 0\n"
473  "CELLS 2 4\n"
474  "1 0\n"
475  "1 1\n"
476  "CELL_TYPES 2\n"
477  "1\n"
478  "1\n";
479 
480  bool rval1 = test_free_vertices( file1 );
481 
482  const char file2[] = "# vtk DataFile Version 3.0\n"
483  "MOAB Version 1.00\n"
484  "ASCII\n"
485  "DATASET UNSTRUCTURED_GRID\n"
486  "POINTS 5 double\n"
487  "10.0 0 0\n"
488  "-10.0 0 0\n"
489  "-5 2. 2.\n"
490  "-5 2. 0.\n"
491  "-5 4. 2.\n"
492  "CELLS 3 8\n"
493  "1 0\n"
494  "1 1\n"
495  "3 2 3 4\n"
496  "CELL_TYPES 3\n"
497  "1\n"
498  "1\n"
499  "5\n";
500 
501  bool rval2 = test_free_vertices( file2 );
502  return rval1 && rval2;
503 }
505 {
506  char file[4096] = "# vtk DataFile Version 3.0\n"
507  "MOAB Version 1.00\n"
508  "ASCII\n"
509  "DATASET STRUCTURED_GRID\n"
510  "DIMENSIONS 4 4 1\n"
511  "POINTS 16 double\n";
512  int len = strlen( file );
513  for( unsigned i = 0; i < 16; ++i )
514  len += sprintf( file + len, "%f %f %f\n", grid_3x3[3 * i], grid_3x3[3 * i + 1], grid_3x3[3 * i + 2] );
515 
516  return test_structured_2d( file );
517 }
518 
520 {
521  const char file[] = "# vtk DataFile Version 3.0\n"
522  "MOAB Version 1.00\n"
523  "ASCII\n"
524  "DATASET RECTILINEAR_GRID\n"
525  "DIMENSIONS 4 4 1\n"
526  "X_COORDINATES 4 float 0 1 2 3\n"
527  "Y_COORDINATES 4 float 0 1 2 3\n"
528  "Z_COORDINATES 1 float 0\n";
529 
530  return test_structured_2d( file );
531 }
532 
534 {
535  const char file[] = "# vtk DataFile Version 3.0\n"
536  "MOAB Version 1.00\n"
537  "ASCII\n"
538  "DATASET STRUCTURED_POINTS\n"
539  "DIMENSIONS 3 3 3\n"
540  "ORIGIN 0 0 0\n"
541  "SPACING 1 1 1\n";
542  return test_structured_3d( file );
543 }
544 
546 {
547  char file[4096] = "# vtk DataFile Version 3.0\n"
548  "MOAB Version 1.00\n"
549  "ASCII\n"
550  "DATASET STRUCTURED_GRID\n"
551  "DIMENSIONS 3 3 3\n"
552  "POINTS 27 double\n";
553 
554  int len = strlen( file );
555  for( unsigned i = 0; i < 27; ++i )
556  len += sprintf( file + len, "%f %f %f\n", grid_2x2x2[3 * i], grid_2x2x2[3 * i + 1], grid_2x2x2[3 * i + 2] );
557 
558  return test_structured_3d( file );
559 }
560 
562 {
563  const char file[] = "# vtk DataFile Version 3.0\n"
564  "MOAB Version 1.00\n"
565  "ASCII\n"
566  "DATASET RECTILINEAR_GRID\n"
567  "DIMENSIONS 3 3 3\n"
568  "X_COORDINATES 3 float 0 1 2\n"
569  "Y_COORDINATES 3 float 0 1 2\n"
570  "Z_COORDINATES 3 float 0 1 2\n";
571 
572  return test_structured_3d( file );
573 }
574 
575 bool test_scalar_attrib( const char* vtk_type, DataType mb_type, int count );
576 bool test_vector_attrib( const char* vtk_type, DataType mb_type );
577 bool test_tensor_attrib( const char* vtk_type, DataType mb_type );
578 
580 {
581  return test_scalar_attrib( "bit", MB_TYPE_BIT, 1 );
582 }
583 
585 {
586  return test_scalar_attrib( "unsigned_char", MB_TYPE_INTEGER, 1 );
587 }
588 
590 {
591  return test_scalar_attrib( "char", MB_TYPE_INTEGER, 1 );
592 }
593 
595 {
596  return test_scalar_attrib( "unsigned_short", MB_TYPE_INTEGER, 1 );
597 }
598 
600 {
601  return test_scalar_attrib( "short", MB_TYPE_INTEGER, 1 );
602 }
603 
605 {
606  return test_scalar_attrib( "unsigned_int", MB_TYPE_INTEGER, 1 );
607 }
608 
610 {
611  return test_scalar_attrib( "int", MB_TYPE_INTEGER, 1 );
612 }
613 
615 {
616  return test_scalar_attrib( "unsigned_long", MB_TYPE_INTEGER, 1 );
617 }
618 
620 {
621  return test_scalar_attrib( "long", MB_TYPE_INTEGER, 1 );
622 }
623 
625 {
626  return test_scalar_attrib( "float", MB_TYPE_DOUBLE, 1 );
627 }
628 
630 {
631  return test_scalar_attrib( "double", MB_TYPE_DOUBLE, 1 );
632 }
633 
635 {
636  return test_scalar_attrib( "bit", MB_TYPE_BIT, 4 );
637 }
638 
640 {
641  return test_scalar_attrib( "unsigned_char", MB_TYPE_INTEGER, 4 );
642 }
643 
645 {
646  return test_scalar_attrib( "char", MB_TYPE_INTEGER, 4 );
647 }
648 
650 {
651  return test_scalar_attrib( "unsigned_short", MB_TYPE_INTEGER, 4 );
652 }
653 
655 {
656  return test_scalar_attrib( "short", MB_TYPE_INTEGER, 4 );
657 }
658 
660 {
661  return test_scalar_attrib( "unsigned_int", MB_TYPE_INTEGER, 4 );
662 }
663 
665 {
666  return test_scalar_attrib( "int", MB_TYPE_INTEGER, 4 );
667 }
668 
670 {
671  return test_scalar_attrib( "unsigned_long", MB_TYPE_INTEGER, 4 );
672 }
673 
675 {
676  return test_scalar_attrib( "long", MB_TYPE_INTEGER, 4 );
677 }
678 
680 {
681  return test_scalar_attrib( "float", MB_TYPE_DOUBLE, 4 );
682 }
683 
685 {
686  return test_scalar_attrib( "double", MB_TYPE_DOUBLE, 4 );
687 }
688 
690 {
691  return test_vector_attrib( "bit", MB_TYPE_BIT );
692 }
693 
695 {
696  return test_vector_attrib( "unsigned_char", MB_TYPE_INTEGER );
697 }
698 
700 {
701  return test_vector_attrib( "char", MB_TYPE_INTEGER );
702 }
703 
705 {
706  return test_vector_attrib( "unsigned_short", MB_TYPE_INTEGER );
707 }
708 
710 {
711  return test_vector_attrib( "short", MB_TYPE_INTEGER );
712 }
713 
715 {
716  return test_vector_attrib( "unsigned_int", MB_TYPE_INTEGER );
717 }
718 
720 {
721  return test_vector_attrib( "int", MB_TYPE_INTEGER );
722 }
723 
725 {
726  return test_vector_attrib( "unsigned_long", MB_TYPE_INTEGER );
727 }
728 
730 {
731  return test_vector_attrib( "long", MB_TYPE_INTEGER );
732 }
733 
735 {
736  return test_vector_attrib( "float", MB_TYPE_DOUBLE );
737 }
738 
740 {
741  return test_vector_attrib( "double", MB_TYPE_DOUBLE );
742 }
743 
745 {
746  return test_tensor_attrib( "unsigned_char", MB_TYPE_INTEGER );
747 }
748 
750 {
751  return test_tensor_attrib( "char", MB_TYPE_INTEGER );
752 }
753 
755 {
756  return test_tensor_attrib( "unsigned_short", MB_TYPE_INTEGER );
757 }
758 
760 {
761  return test_tensor_attrib( "short", MB_TYPE_INTEGER );
762 }
763 
765 {
766  return test_tensor_attrib( "unsigned_int", MB_TYPE_INTEGER );
767 }
768 
770 {
771  return test_tensor_attrib( "int", MB_TYPE_INTEGER );
772 }
773 
775 {
776  return test_tensor_attrib( "unsigned_long", MB_TYPE_INTEGER );
777 }
778 
780 {
781  return test_tensor_attrib( "long", MB_TYPE_INTEGER );
782 }
783 
785 {
786  return test_tensor_attrib( "float", MB_TYPE_DOUBLE );
787 }
788 
790 {
791  return test_tensor_attrib( "double", MB_TYPE_DOUBLE );
792 }
793 
794 bool read_file( Interface* iface, const char* file )
795 {
796  char fname[] = "tmp_file.vtk";
797  FILE* fptr = fopen( fname, "w" );
798  fputs( file, fptr );
799  fclose( fptr );
800 
801  ErrorCode rval = iface->load_mesh( fname );
802  remove( fname );
803  CHECK( rval );
804  return true;
805 }
806 
807 bool write_and_read( Interface* iface1, Interface* iface2 )
808 {
809  const char fname[] = "tmp_file.vtk";
810  ErrorCode rval1 = iface1->write_mesh( fname );
811  ErrorCode rval2 = iface2->load_mesh( fname );
812  remove( fname );
813  CHECK( rval1 );
814  CHECK( rval2 );
815  return true;
816 }
817 
818 bool compare_connectivity( EntityType, const int* conn1, const int* conn2, unsigned len )
819 {
820  for( unsigned i = 0; i < len; ++i )
821  if( conn1[i] != conn2[i] ) return false;
822  return true;
823 }
824 
826  EntityType moab_type,
827  unsigned num_vert,
828  unsigned num_elem,
829  unsigned vert_per_elem,
830  const double* coords,
831  const int* connectivity,
832  EntityHandle* vert_handles,
833  EntityHandle* elem_handles )
834 {
835  ErrorCode rval;
836 
837  // get vertices and check count
838  Range verts;
839  rval = iface->get_entities_by_type( 0, MBVERTEX, verts );
840  CHECK( rval );
841  CHECK( verts.size() == num_vert );
842 
843  // get elements and check count
844  Range elems;
845  rval = iface->get_entities_by_type( 0, moab_type, elems );
846  CHECK( rval );
847  CHECK( elems.size() == num_elem );
848 
849  // get vertex coordinates
850  std::vector< EntityHandle > vert_array( num_vert );
851  std::copy( verts.begin(), verts.end(), vert_array.begin() );
852  std::vector< double > mb_coords( 3 * num_vert );
853  rval = iface->get_coords( &vert_array[0], num_vert, &mb_coords[0] );
854  CHECK( rval );
855 
856  // compare vertex coordinates to construct map from
857  // EntityHandle to index in input coordinate list
858  std::map< EntityHandle, int > vert_map;
859  std::vector< bool > seen( num_vert, false );
860  for( unsigned i = 0; i < num_vert; ++i )
861  {
862  double* vert_coords = &mb_coords[3 * i];
863  bool found = false;
864  for( unsigned j = 0; j < num_vert; ++j )
865  {
866  const double* file_coords = &coords[3 * j];
867  double dsqr = 0;
868  for( unsigned k = 0; k < 3; ++k )
869  {
870  double diff = vert_coords[k] - file_coords[k];
871  dsqr += diff * diff;
872  }
873  if( dsqr < 1e-6 )
874  {
875  CHECK( !seen[j] ); // duplicate vertex
876  seen[j] = found = true;
877  vert_map[vert_array[i]] = j;
878  vert_handles[j] = vert_array[i];
879  break;
880  }
881  }
882  CHECK( found ); // not found?
883  }
884 
885  // check element connectivity
886  seen.clear();
887  seen.resize( num_elem, false );
888  Range::iterator iter = elems.begin();
889  for( unsigned i = 0; i < num_elem; ++i )
890  {
891  // get element connectivity
892  EntityHandle elem = *iter;
893  ++iter;
894  std::vector< EntityHandle > elem_conn;
895  rval = iface->get_connectivity( &elem, 1, elem_conn );
896  CHECK( rval );
897  CHECK( elem_conn.size() == vert_per_elem );
898 
899  // convert to input vertex ordering
900  std::vector< int > elem_conn2( vert_per_elem );
901  for( unsigned j = 0; j < vert_per_elem; ++j )
902  {
903  std::map< EntityHandle, int >::iterator k = vert_map.find( elem_conn[j] );
904  CHECK( k != vert_map.end() );
905  elem_conn2[j] = k->second;
906  }
907 
908  // search input list for matching element
909  bool found = false;
910  for( unsigned j = 0; j < num_elem; ++j )
911  {
912  const int* conn_arr = connectivity + j * vert_per_elem;
913  if( !seen[j] && compare_connectivity( moab_type, conn_arr, &elem_conn2[0], vert_per_elem ) )
914  {
915  seen[j] = found = true;
916  elem_handles[j] = elem;
917  break;
918  }
919  }
920  CHECK( found );
921  }
922 
923  return true;
924 }
925 
927  EntityType moab_type,
928  unsigned num_elem,
929  unsigned vert_per_elem,
930  const double* coords,
931  unsigned num_vert,
932  const int* connectivity )
933 {
934  std::vector< EntityHandle > junk1( num_vert ), junk2( num_elem );
935  bool rval = match_vertices_and_elements( iface, moab_type, num_vert, num_elem, vert_per_elem, coords, connectivity,
936  &junk1[0], &junk2[0] );
937  CHECK( rval );
938  return true;
939 }
940 
941 bool test_read_write_element( const double* coords,
942  unsigned num_verts,
943  const int* vtk_conn,
944  const int* moab_conn,
945  unsigned num_conn,
946  unsigned num_elem,
947  unsigned vtk_type,
948  EntityType moab_type )
949 
950 {
951  // construct VTK file
952  char file[4096] = "# vtk DataFile Version 3.0\n"
953  "MOAB Version 1.00\n"
954  "ASCII\n"
955  "DATASET UNSTRUCTURED_GRID\n";
956  size_t len = strlen( file );
957 
958  len += sprintf( file + len, "POINTS %u double\n", num_verts );
959  for( unsigned i = 0; i < num_verts; ++i )
960  len += sprintf( file + len, "%f %f %f\n", coords[3 * i], coords[3 * i + 1], coords[3 * i + 2] );
961 
962  len += sprintf( file + len, "CELLS %u %u\n", num_elem, num_conn + num_elem );
963  assert( num_conn % num_elem == 0 );
964  unsigned conn_len = num_conn / num_elem;
965  for( unsigned i = 0; i < num_elem; ++i )
966  {
967  len += sprintf( file + len, "%u", conn_len );
968  for( unsigned j = 0; j < conn_len; ++j )
969  len += sprintf( file + len, " %u", vtk_conn[conn_len * i + j] );
970  len += sprintf( file + len, "\n" );
971  }
972 
973  len += sprintf( file + len, "CELL_TYPES %u\n", num_elem );
974  for( unsigned i = 0; i < num_elem; ++i )
975  len += sprintf( file + len, "%u\n", vtk_type );
976 
977  // read VTK file and check results
978  Core instance1, instance2;
979  bool bval = read_file( &instance1, file );
980  CHECK( bval );
981  bval = check_elements( &instance1, moab_type, num_elem, conn_len, coords, num_verts, moab_conn );
982  CHECK( bval );
983 
984  // write, re-read, and check results
985  bval = write_and_read( &instance1, &instance2 );
986  CHECK( bval );
987  bval = check_elements( &instance2, moab_type, num_elem, conn_len, coords, num_verts, moab_conn );
988  CHECK( bval );
989 
990  return true;
991 }
992 
993 bool test_structured_2d( const char* file )
994 {
995  // read VTK file and check results
996  Core instance;
997  bool bval = read_file( &instance, file );
998  CHECK( bval );
999  bval = check_elements( &instance, MBQUAD, 9, 4, grid_3x3, 16, quad_structured_conn );
1000  CHECK( bval );
1001 
1002  return true;
1003 }
1004 
1005 bool test_structured_3d( const char* file )
1006 {
1007  // read VTK file and check results
1008  Core instance;
1009  bool bval = read_file( &instance, file );
1010  CHECK( bval );
1011  bval = check_elements( &instance, MBHEX, 8, 8, grid_2x2x2, 27, hex_structured_conn );
1012  CHECK( bval );
1013 
1014  return true;
1015 }
1016 
1017 const char two_quad_mesh[] = "# vtk DataFile Version 3.0\n"
1018  "MOAB Version 1.00\n"
1019  "ASCII\n"
1020  "DATASET UNSTRUCTURED_GRID\n"
1021  "POINTS 6 float\n"
1022  "-1 0 0\n"
1023  " 0 0 0\n"
1024  " 1 0 0\n"
1025  "-1 1 0\n"
1026  " 0 1 0\n"
1027  " 1 1 0\n"
1028  "CELLS 2 10\n"
1029  "4 0 1 4 3\n"
1030  "4 1 2 5 4\n"
1031  "CELL_TYPES 2\n"
1032  "9 9\n";
1033 
1034 const double two_quad_mesh_coords[] = { -1, 0, 0, 0, 0, 0, 1, 0, 0, -1, 1, 0, 0, 1, 0, 1, 1, 0 };
1035 const int two_quad_mesh_conn[] = { 0, 1, 4, 3, 1, 2, 5, 4 };
1036 
1037 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,
1038  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 };
1039 const int element_values[] = { 1001, 1002, 1004, 1003, 50, 60, 51, 61, 1, 2, 3, 4, 5, 6,
1040  7, 8, 9, 0, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
1041 
1042 void write_data( char* file, size_t& len, DataType type, unsigned count, const int* vals )
1043 {
1044  switch( type )
1045  {
1046  case MB_TYPE_BIT:
1047  for( unsigned i = 0; i < count; ++i )
1048  len += sprintf( file + len, "%d\n", abs( vals[i] ) % 2 );
1049  break;
1050  case MB_TYPE_INTEGER:
1051  for( unsigned i = 0; i < count; ++i )
1052  len += sprintf( file + len, "%d\n", vals[i] );
1053  break;
1054  case MB_TYPE_DOUBLE:
1055  for( unsigned i = 0; i < count; ++i )
1056  len += sprintf( file + len, "%f\n", (double)vals[i] );
1057  break;
1058  case MB_TYPE_OPAQUE:
1059  for( unsigned i = 0; i < count; ++i )
1060  len += sprintf( file + len, "%d\n", abs( vals[i] % 256 ) );
1061  break;
1062  default:
1063  assert( false /* VTK files cannot handle this type */ );
1064  }
1065 }
1066 
1068  DataType tag_type,
1069  int tag_length,
1070  int num_entities,
1071  const EntityHandle* entities,
1072  const int* values )
1073 {
1074  Tag tag;
1075  ErrorCode rval = iface->tag_get_handle( "data", tag_length, tag_type, tag );
1076  CHECK( rval );
1077 
1078  int size, *intptr;
1079  double* dblptr;
1080  rval = iface->tag_get_bytes( tag, size );
1081  CHECK( rval );
1082  std::vector< unsigned char > data( size * num_entities );
1083 
1084  switch( tag_type )
1085  {
1086  case MB_TYPE_BIT:
1087  rval = iface->tag_get_length( tag, size );
1088  CHECK( rval );
1089  CHECK( tag_length == size );
1090  for( int i = 0; i < num_entities; ++i )
1091  {
1092  unsigned char val;
1093  rval = iface->tag_get_data( tag, entities + i, 1, &val );
1094  CHECK( rval );
1095  for( int j = 0; j < tag_length; ++j )
1096  {
1097  int bitval = !!( val & ( 1 << j ) );
1098  int expval = abs( *values ) % 2;
1099  CHECK( bitval == expval );
1100  ++values;
1101  }
1102  }
1103  break;
1104  case MB_TYPE_OPAQUE:
1105  rval = iface->tag_get_data( tag, entities, num_entities, &data[0] );
1106  CHECK( rval );
1107  CHECK( tag_length == size );
1108  for( int i = 0; i < num_entities; ++i )
1109  for( int j = 0; j < tag_length; ++j, ++values )
1110  CHECK( (unsigned)( *values % 256 ) == data[i * tag_length + j] );
1111  break;
1112  case MB_TYPE_INTEGER:
1113  rval = iface->tag_get_data( tag, entities, num_entities, &data[0] );
1114  CHECK( rval );
1115  CHECK( tag_length * sizeof( int ) == (unsigned)size );
1116  intptr = reinterpret_cast< int* >( &data[0] );
1117  for( int i = 0; i < num_entities; ++i )
1118  for( int j = 0; j < tag_length; ++j, ++values )
1119  CHECK( *values == intptr[i * tag_length + j] );
1120  break;
1121  case MB_TYPE_DOUBLE:
1122  rval = iface->tag_get_data( tag, entities, num_entities, &data[0] );
1123  CHECK( rval );
1124  CHECK( tag_length * sizeof( double ) == (unsigned)size );
1125  dblptr = reinterpret_cast< double* >( &data[0] );
1126  for( int i = 0; i < num_entities; ++i )
1127  for( int j = 0; j < tag_length; ++j, ++values )
1128  CHECK( *values == dblptr[i * tag_length + j] );
1129  break;
1130  default:
1131  assert( false );
1132  return false;
1133  }
1134  return true;
1135 }
1136 
1137 bool check_tag_values( Interface* iface, DataType type, int vals_per_ent )
1138 {
1139  EntityHandle vert_handles[6], elem_handles[2];
1141  vert_handles, elem_handles );
1142  CHECK( rval );
1143 
1144  rval = check_tag_values( iface, type, vals_per_ent, 6, vert_handles, vertex_values );
1145  CHECK( rval );
1146  rval = check_tag_values( iface, type, vals_per_ent, 2, elem_handles, element_values );
1147  CHECK( rval );
1148  return rval;
1149 }
1150 
1151 bool check_tag_data( const char* file, DataType type, int vals_per_ent )
1152 {
1153  bool bval;
1154  Core instance1, instance2;
1155 
1156  bval = read_file( &instance1, file );
1157  CHECK( bval );
1158  bval = check_tag_values( &instance1, type, vals_per_ent );
1159  CHECK( bval );
1160  bval = write_and_read( &instance1, &instance2 );
1161  CHECK( bval );
1162  bval = check_tag_values( &instance2, type, vals_per_ent );
1163  CHECK( bval );
1164  return true;
1165 }
1166 
1167 bool test_scalar_attrib( const char* vtk_type, DataType mb_type, int count )
1168 {
1169  char file[4096];
1170  strcpy( file, two_quad_mesh );
1171  size_t len = strlen( file );
1172  len += sprintf( file + len, "POINT_DATA 6\n" );
1173  len += sprintf( file + len, "SCALARS data %s %d\n", vtk_type, count );
1174  len += sprintf( file + len, "LOOKUP_TABLE default\n" );
1175  write_data( file, len, mb_type, 6 * count, vertex_values );
1176  len += sprintf( file + len, "CELL_DATA 2\n" );
1177  len += sprintf( file + len, "SCALARS data %s %d\n", vtk_type, count );
1178  len += sprintf( file + len, "LOOKUP_TABLE default\n" );
1179  write_data( file, len, mb_type, 2 * count, element_values );
1180 
1181  return check_tag_data( file, mb_type, count );
1182 }
1183 
1184 bool test_vector_attrib( const char* vtk_type, DataType mb_type )
1185 {
1186  char file[4096];
1187  strcpy( file, two_quad_mesh );
1188  size_t len = strlen( file );
1189  len += sprintf( file + len, "POINT_DATA 6\n" );
1190  len += sprintf( file + len, "VECTORS data %s\n", vtk_type );
1191  write_data( file, len, mb_type, 6 * 3, vertex_values );
1192  len += sprintf( file + len, "CELL_DATA 2\n" );
1193  len += sprintf( file + len, "VECTORS data %s\n", vtk_type );
1194  write_data( file, len, mb_type, 2 * 3, element_values );
1195 
1196  return check_tag_data( file, mb_type, 3 );
1197 }
1198 
1199 bool test_tensor_attrib( const char* vtk_type, DataType mb_type )
1200 {
1201  char file[4096];
1202  strcpy( file, two_quad_mesh );
1203  size_t len = strlen( file );
1204  len += sprintf( file + len, "POINT_DATA 6\n" );
1205  len += sprintf( file + len, "TENSORS data %s\n", vtk_type );
1206  write_data( file, len, mb_type, 6 * 9, vertex_values );
1207  len += sprintf( file + len, "CELL_DATA 2\n" );
1208  len += sprintf( file + len, "TENSORS data %s\n", vtk_type );
1209  write_data( file, len, mb_type, 2 * 9, element_values );
1210 
1211  return check_tag_data( file, mb_type, 9 );
1212 }
1213 
1215 {
1216  Core moab_inst;
1217  Interface& moab = moab_inst;
1218  ErrorCode rval;
1219 
1220  // create 9 nodes in grid pattern
1221  EntityHandle verts[9];
1222  const double coords[][3] = { { 0, 0, 0 }, { 1, 0, 0 }, { 2, 0, 0 }, { 0, 1, 0 }, { 1, 1, 0 },
1223  { 2, 1, 0 }, { 0, 2, 0 }, { 1, 2, 0 }, { 2, 2, 0 } };
1224  for( unsigned i = 0; i < 9; ++i )
1225  {
1226  rval = moab.create_vertex( coords[i], verts[i] );
1227  assert( MB_SUCCESS == rval );
1228  }
1229 
1230  // create 4 quad elements in grid pattern
1231  const int conn[][4] = { { 0, 1, 4, 3 }, { 1, 2, 5, 4 }, { 3, 4, 7, 6 }, { 4, 5, 8, 7 } };
1232  EntityHandle econn[4], elems[4];
1233  for( unsigned i = 0; i < 4; ++i )
1234  {
1235  for( unsigned j = 0; j < 4; ++j )
1236  econn[j] = verts[conn[i][j]];
1237  rval = moab.create_element( MBQUAD, econn, 4, elems[i] );
1238  assert( MB_SUCCESS == rval );
1239  }
1240 
1241  // create 3 meshsets
1242  EntityHandle sets[3];
1243  for( unsigned i = 0; i < 3; ++i )
1244  {
1245  rval = moab.create_meshset( 0, sets[i] );
1246  assert( MB_SUCCESS == rval );
1247  }
1248 
1249  // add element 3 to set 0
1250  rval = moab.add_entities( sets[0], elems + 3, 1 );
1251  assert( MB_SUCCESS == rval );
1252  // add node 2 to set 1
1253  rval = moab.add_entities( sets[1], verts + 2, 1 );
1254  assert( MB_SUCCESS == rval );
1255  // add element 2 and 3 to set 2
1256  rval = moab.add_entities( sets[2], elems + 2, 2 );
1257  assert( MB_SUCCESS == rval );
1258 
1259  // make set 2 a child of set 1
1260  rval = moab.add_child_meshset( sets[1], sets[2] );
1261  assert( MB_SUCCESS == rval );
1262  // put set 1 in set 0
1263  rval = moab.add_entities( sets[0], sets + 1, 1 );
1264  assert( MB_SUCCESS == rval );
1265 
1266  // write sets[0] to vtk file
1267  rval = moab.write_mesh( "tmp_file.vtk", sets, 1 );
1268  CHECK( rval );
1269 
1270  // read data back in
1271  moab.delete_mesh();
1272  rval = moab.load_mesh( "tmp_file.vtk" );
1273  remove( "tmp_file.vtk" );
1274  CHECK( rval );
1275 
1276  // writer should have written all three sets,
1277  // so the resulting mesh should be elems[2], elems[3],
1278  // and verts[2]
1279  Range new_elems, new_verts;
1280  rval = moab.get_entities_by_type( 0, MBQUAD, new_elems );
1281  CHECK( rval );
1282  CHECK( new_elems.size() == 2 );
1283  rval = moab.get_entities_by_type( 0, MBVERTEX, new_verts );
1284  CHECK( rval );
1285  CHECK( new_verts.size() == 7 );
1286 
1287  // vertex not in element closure should have coords of 2,0,0
1288  Range elem_verts;
1289  rval = moab.get_adjacencies( new_elems, 0, false, elem_verts, Interface::UNION );
1290  CHECK( rval );
1291  CHECK( elem_verts.size() == 6 );
1292  Range free_verts( subtract( new_verts, elem_verts ) );
1293  CHECK( free_verts.size() == 1 );
1294  double vcoords[3];
1295  rval = moab.get_coords( free_verts, vcoords );
1296  CHECK( vcoords[0] == 2 );
1297  CHECK( vcoords[1] == 0 );
1298  CHECK( vcoords[2] == 0 );
1299 
1300  return true;
1301 }
1302 
1304 {
1305  Core moab_inst;
1306  Interface& moab = moab_inst;
1307  ErrorCode rval;
1308 
1309  // create 9 nodes in grid pattern
1310  EntityHandle verts[9];
1311  const double coords[][3] = { { 0, 0, 0 }, { 1, 0, 0 }, { 2, 0, 0 }, { 0, 1, 0 }, { 1, 1, 0 },
1312  { 2, 1, 0 }, { 0, 2, 0 }, { 1, 2, 0 }, { 2, 2, 0 } };
1313  for( unsigned i = 0; i < 9; ++i )
1314  {
1315  rval = moab.create_vertex( coords[i], verts[i] );
1316  assert( MB_SUCCESS == rval );
1317  }
1318 
1319  // create 3 quad elements, one node (8) not used
1320  const int conn[][4] = { { 0, 1, 4, 3 }, { 1, 2, 5, 4 }, { 3, 4, 7, 6 } };
1321 
1322  Tag gid;
1323  rval = moab.tag_get_handle( "GLOBAL_ID", 1, moab::MB_TYPE_INTEGER, gid );
1324  assert( MB_SUCCESS == rval );
1325  EntityHandle econn[4], elems[3];
1326  for( unsigned i = 0; i < 3; ++i )
1327  {
1328  for( unsigned j = 0; j < 4; ++j )
1329  econn[j] = verts[conn[i][j]];
1330  rval = moab.create_element( MBQUAD, econn, 4, elems[i] );
1331  assert( MB_SUCCESS == rval );
1332  int id = i + 1;
1333  rval = moab.tag_set_data( gid, &elems[i], 1, &id );
1334  assert( MB_SUCCESS == rval );
1335  }
1336 
1337  rval = moab.write_file( "tmp_file.vtk" );
1338  CHECK( rval );
1339 
1340  rval = moab.write_file( "tmp_file2.vtk", 0, "CREATE_ONE_NODE_CELLS;" );
1341  CHECK( rval );
1342 
1343  // read data back in
1344  moab.delete_mesh();
1345  rval = moab.load_file( "tmp_file.vtk" );
1346  remove( "tmp_file.vtk" );
1347  remove( "tmp_file2.vtk" );
1348  CHECK( rval );
1349 
1350  return true;
1351 }
1352 
1353 // Test technically invalid but somewhat common insertion of
1354 // FIELD blocks within an UNSTRUCTURED_GRID dataset
1356 {
1357  // Use existing file defined in 'two_quad_mesh', but
1358  // insert a few field data blocks
1359  std::istringstream base_data( two_quad_mesh );
1360  std::ostringstream file_data;
1361  std::string line;
1362  while( getline( base_data, line ) )
1363  {
1364  if( 0 == line.find( "POINTS" ) )
1365  {
1366  file_data << "FIELD FieldData 2" << std::endl
1367  << "avtOriginalBounds 1 6 float" << std::endl
1368  << "-10 10 -10 10 -10 10 " << std::endl
1369  << "TIME 1 1 double" << std::endl
1370  << "10.543" << std::endl;
1371  }
1372  else if( 0 == line.find( "CELLS" ) )
1373  {
1374  file_data << "FIELD more_data 2" << std::endl
1375  << "first_array 3 2 int" << std::endl
1376  << "0 1 2" << std::endl
1377  << "3 4 5" << std::endl
1378  << "second_array 4 3 bit" << std::endl
1379  << "0 0 0 0" << std::endl
1380  << "1 1 1 1" << std::endl
1381  << "1 0 1 0" << std::endl;
1382  }
1383  file_data << line << std::endl;
1384  }
1385 
1386  Core core;
1387  Interface& mb = core;
1388  bool rval = read_file( &mb, file_data.str().c_str() );
1389  CHECK( rval );
1390 
1391  EntityHandle vert_handles[6], elem_handles[2];
1393  elem_handles );
1394  CHECK( rval );
1395 
1396  return true;
1397 }