MOAB: Mesh Oriented datABase  (version 5.5.0)
coords_connect_iterate.cpp File Reference
#include "moab/Core.hpp"
#include "moab/Range.hpp"
#include "moab/ScdInterface.hpp"
#include "moab/HomXform.hpp"
#include "moab/ReadUtilIface.hpp"
#include "TestUtil.hpp"
#include <cstdlib>
#include <algorithm>
+ Include dependency graph for coords_connect_iterate.cpp:

Go to the source code of this file.

Functions

void test_coords_connect_iterate ()
 
void test_scd_invalid ()
 
void test_iterates ()
 
int main ()
 

Function Documentation

◆ main()

int main ( )

Definition at line 16 of file coords_connect_iterate.cpp.

17 {
18  int failures = 0;
19 
21  failures += RUN_TEST( test_scd_invalid );
22  failures += RUN_TEST( test_iterates );
23 
24  if( failures ) std::cerr << "<<<< " << failures << " TESTS FAILED >>>>" << std::endl;
25 
26  return failures;
27 }

References RUN_TEST, test_coords_connect_iterate(), test_iterates(), and test_scd_invalid().

◆ test_coords_connect_iterate()

void test_coords_connect_iterate ( )

Definition at line 29 of file coords_connect_iterate.cpp.

30 {
31  // create 1000 vertices
32  const unsigned int NUM_VTX = 1000;
33  Core moab;
34  Interface& mb = moab;
35  std::vector< double > coords( 3 * NUM_VTX );
36  for( unsigned int i = 0; i < NUM_VTX; i++ )
37  coords[3 * i] = coords[3 * i + 1] = coords[3 * i + 2] = i;
38 
39  Range verts, hexes, faces, edges, dead;
40  ErrorCode rval = mb.create_vertices( &coords[0], NUM_VTX, verts );CHECK_ERR( rval );
41 
42  // create a bunch of hexes from those
43  ReadUtilIface* rui;
44  EntityHandle *orig_connect, start_hex;
45  rval = mb.query_interface( rui );CHECK_ERR( rval );
46  rval = rui->get_element_connect( NUM_VTX / 8, 8, MBHEX, 1, start_hex, orig_connect );CHECK_ERR( rval );
47  std::copy( verts.begin(), verts.end(), orig_connect );
48  hexes.insert( start_hex, start_hex + NUM_VTX / 8 - 1 );
49 
50  // delete about 1% of vertices
51  const int step = 100;
52  int remaining = NUM_VTX;
53  Range::iterator vit = verts.begin();
55  for( int j = 0; j < remaining; j += step )
56  {
57  entities[0] = start_hex + j / 8;
58  entities[1] = *vit;
59  rval = mb.delete_entities( entities, 2 );CHECK_ERR( rval );
60  dead.insert( *vit );
61  dead.insert( start_hex + j / 8 );
62  vit = verts.erase( vit );
63  vit += step - 1;
64  hexes.erase( start_hex + j / 8 );
65  }
66 
67  // Remove some additional values from the range
68  // so that our handle blocks don't always align with
69  // sequences
70  verts.erase( verts.begin() + ( step - 5 ), verts.begin() + ( step + 5 ) );
71  hexes.erase( hexes.begin() + ( step / 8 - 5 ), hexes.begin() + ( step / 8 + 5 ) );
72 
73  // Check that we get back expected values
74  double *xcoord, *ycoord, *zcoord;
75  vit = verts.begin();
76  int count, total = 0;
77  while( vit != verts.end() )
78  {
79  rval = mb.coords_iterate( vit, verts.end(), xcoord, ycoord, zcoord, count );
80  if( MB_SUCCESS && ( !xcoord || !ycoord || !zcoord ) ) rval = MB_FAILURE;CHECK_ERR( rval );
81 
82  assert( total + count <= (int)verts.size() );
83  for( int i = 0; i < count; i++ )
84  {
85  // vertex handles start at 1, so need to subtract one
86  double val = *vit + (double)i - 1.0;
87  CHECK_REAL_EQUAL( val, xcoord[i], 1.0e-10 );
88  CHECK_REAL_EQUAL( val, ycoord[i], 1.0e-10 );
89  CHECK_REAL_EQUAL( val, zcoord[i], 1.0e-10 );
90  }
91 
92  // Check that we can set values and get the right values back
93  for( int i = 0; i < count; i++ )
94  {
95  xcoord[i] *= 2.0;
96  ycoord[i] *= 2.0;
97  zcoord[i] *= 2.0;
98  }
99 
100  std::vector< double > dum( 3 * count );
101  Range dum_verts( *vit, *vit + count - 1 );
102  rval = mb.get_coords( dum_verts, &dum[0] );CHECK_ERR( rval );
103  for( int i = 0; i < count; i++ )
104  {
105  // vertex handles start at 1, so need to subtract 1 from expected value
106  double val = 2.0 * ( *vit + (double)i - 1 );
107  CHECK_REAL_EQUAL( val, xcoord[i], 1.0e-10 );
108  CHECK_REAL_EQUAL( val, ycoord[i], 1.0e-10 );
109  CHECK_REAL_EQUAL( val, zcoord[i], 1.0e-10 );
110  }
111 
112  vit += count;
113  total += count;
114  }
115 
116  // now check connectivity
117  Range::iterator hit = hexes.begin();
118  EntityHandle* connect = NULL;
119  EntityHandle dum_connect[8];
120  int num_connect;
121  while( hit != hexes.end() )
122  {
123  rval = mb.connect_iterate( hit, hexes.end(), connect, num_connect, count );
124  if( MB_SUCCESS && !connect ) rval = MB_FAILURE;CHECK_ERR( rval );
125  CHECK_EQUAL( num_connect, 8 );
126 
127  // should be equal to initial connectivity
128  for( int i = 0; i < count; i++ )
129  {
130  EntityHandle first = 8 * ( *hit - start_hex + i ) + 1;
131  for( unsigned int j = 0; j < 8; j++ )
132  dum_connect[j] = first + j;
133  CHECK_ARRAYS_EQUAL( connect, 8, &dum_connect[0], 8 );
134  connect += 8;
135  }
136 
137  hit += count;
138  // total += count;
139  }
140 
141  // ok, done
142 }

References moab::Range::begin(), CHECK_ARRAYS_EQUAL, CHECK_EQUAL, CHECK_ERR, CHECK_REAL_EQUAL, moab::Core::connect_iterate(), moab::Core::coords_iterate(), moab::Core::create_vertices(), moab::Core::delete_entities(), moab::dum, moab::Range::end(), entities, moab::Range::erase(), ErrorCode, moab::GeomUtil::first(), moab::Core::get_coords(), moab::ReadUtilIface::get_element_connect(), moab::Range::insert(), mb, MB_SUCCESS, MBHEX, moab::Interface::query_interface(), and moab::Range::size().

Referenced by main().

◆ test_iterates()

void test_iterates ( )

Definition at line 177 of file coords_connect_iterate.cpp.

178 {
179  // create 400 vertices, in 2 separate sequences
180  // also, create 50 hexes, in 2 separate sequences
181  const int NUM_VTX = 400;
182  const int NUM_HEX = 50;
183  Core moab;
184  Interface& mb = moab;
185  std::vector< double > coords( 3 * NUM_VTX / 2 );
186  for( unsigned int i = 0; i < NUM_VTX / 2; i++ )
187  {
188  coords[3 * i] = i;
189  coords[3 * i + 1] = 10 * i;
190  coords[3 * i + 2] = 100 * i;
191  }
192 
193  Range verts, hexes;
194  ErrorCode rval = mb.create_vertices( &coords[0], NUM_VTX / 2, verts );CHECK_ERR( rval );
195 
196  // create more vertices, with same coordinates
197  Range v2;
198  rval = mb.create_vertices( &coords[0], NUM_VTX / 2, v2 );CHECK_ERR( rval );
199  Range ver = unite( verts, v2 );
200  // range of vertices is contiguous, but its memory for vertices is not!!
201  CHECK_EQUAL( (int)ver.psize(), 1 );
202  // create a bunch of hexes from those
203  ReadUtilIface* rui;
204  EntityHandle start_hex;
205  rval = mb.query_interface( rui );CHECK_ERR( rval );
206 
207  Range::iterator vertIter = ver.begin();
208  for( int i = 0; i < NUM_HEX; i += NUM_HEX / 2 )
209  {
210  EntityHandle *orig_connect, current_hex;
211  Range::iterator vertIterEnd = vertIter + ( 25 * 8 );
212  rval = rui->get_element_connect( 25, 8, MBHEX, 1, current_hex, orig_connect );CHECK_ERR( rval );
213  std::copy( vertIter, vertIterEnd, orig_connect );
214  hexes.insert( current_hex, current_hex + 24 );
215  vertIter += ( NUM_HEX / 2 * 8 );
216  }
217 
218  // make sure hexes range is contiguous
219  CHECK_EQUAL( (int)hexes.psize(), 1 );
220  start_hex = hexes.front();
221  Tag idtag = moab.globalId_tag();
222 
223  for( int i = 0; i < NUM_HEX; i++, start_hex++ )
224  {
225  rval = mb.tag_set_data( idtag, &start_hex, 1, &i );CHECK_ERR( rval );
226  }
227  int count = 0;
228  int total = 0;
229 
230  // now check connectivity
231  start_hex = hexes.front();
232  Range::iterator hit = hexes.begin();
233  EntityHandle* connect = NULL;
234  EntityHandle dum_connect[8];
235  int num_connect;
236  while( hit != hexes.end() )
237  {
238  rval = mb.connect_iterate( hit, hexes.end(), connect, num_connect, count );
239  if( MB_SUCCESS && !connect ) rval = MB_FAILURE;
240 
241  CHECK_ERR( rval );
242  CHECK_EQUAL( num_connect, 8 );
243  CHECK_EQUAL( count, NUM_HEX / 2 );
244  // should be equal to initial connectivity
245  for( int i = 0; i < count; i++ )
246  {
247  EntityHandle first = 8 * ( *hit - start_hex + i ) + 1;
248  for( unsigned int j = 0; j < 8; j++ )
249  dum_connect[j] = first + j;
250  CHECK_ARRAYS_EQUAL( connect, 8, &dum_connect[0], 8 );
251  connect += 8;
252  }
253 
254  hit += count;
255  total += count;
256  }
257 
258  hit = hexes.begin();
259  void* ptr;
260  while( hit != hexes.end() )
261  {
262  // get contiguous block of tag data, of size of the sequence allocated
263  rval = mb.tag_iterate( idtag, hit, hexes.end(), count, ptr );CHECK_ERR( rval );
264  CHECK_EQUAL( count, NUM_HEX / 2 );
265  hit += count;
266  }
267 
268  hit = ver.begin();
269  // coords_iterate
270  double* xc;
271  double* yc;
272  double* zc;
273  while( hit != ver.end() )
274  {
275  // get contiguous block of coords
276 
277  rval = mb.coords_iterate( hit, ver.end(), xc, yc, zc, count );CHECK_ERR( rval );
278  CHECK_EQUAL( count, NUM_VTX / 2 );
279  hit += count;
280  }
281 }

References moab::Range::begin(), CHECK_ARRAYS_EQUAL, CHECK_EQUAL, CHECK_ERR, moab::Core::connect_iterate(), moab::Core::coords_iterate(), moab::Core::create_vertices(), moab::Range::end(), ErrorCode, moab::GeomUtil::first(), moab::Range::front(), moab::ReadUtilIface::get_element_connect(), moab::Range::insert(), mb, MB_SUCCESS, MBHEX, moab::Range::psize(), moab::Interface::query_interface(), moab::Core::tag_iterate(), moab::Core::tag_set_data(), and moab::unite().

Referenced by main().

◆ test_scd_invalid()

void test_scd_invalid ( )

Definition at line 144 of file coords_connect_iterate.cpp.

145 {
146  // check that we get errors from structured mesh
147  Core moab;
148  Interface& mb = moab;
149  ScdInterface* scdi;
150  ErrorCode rval = mb.query_interface( scdi );CHECK_ERR( rval );
151 
152  // make an arbitrary structured mesh
153  const int NUM_DIMS = 10;
154  HomCoord low( 0, 0, 0 ), high( NUM_DIMS, NUM_DIMS, NUM_DIMS );
155  ScdBox* new_box = NULL;
156  rval = scdi->construct_box( low, high, NULL, 0, new_box );CHECK_ERR( rval );
157  CHECK( new_box != NULL );
158 
159  EntityHandle start_hex = new_box->start_element();
160  Range hexes( start_hex, start_hex + NUM_DIMS * NUM_DIMS * NUM_DIMS - 1 );
161 
162  // should be able to get vertices used by this box
163  Range verts;
164  rval = mb.get_adjacencies( hexes, 0, false, verts, Interface::UNION );CHECK_ERR( rval );
165  CHECK_EQUAL( (int)verts.size(), (int)( ( NUM_DIMS + 1 ) * ( NUM_DIMS + 1 ) * ( NUM_DIMS + 1 ) ) );
166 
167  // should NOT be able to get connect iterator
168  EntityHandle* connect;
169  int count, num_connect;
170  // expected failure
171  rval = mb.connect_iterate( hexes.begin(), hexes.end(), connect, num_connect, count );
172  CHECK_EQUAL( rval, MB_FAILURE );
173 }

References moab::Range::begin(), CHECK, CHECK_EQUAL, CHECK_ERR, moab::Core::connect_iterate(), moab::ScdInterface::construct_box(), moab::Range::end(), ErrorCode, moab::Core::get_adjacencies(), mb, moab::Interface::query_interface(), moab::Range::size(), moab::ScdBox::start_element(), and moab::Interface::UNION.

Referenced by main().