MOAB: Mesh Oriented datABase  (version 5.4.1)
coords_connect_iterate.cpp
Go to the documentation of this file.
00001 #include "moab/Core.hpp"
00002 #include "moab/Range.hpp"
00003 #include "moab/ScdInterface.hpp"
00004 #include "moab/HomXform.hpp"
00005 #include "moab/ReadUtilIface.hpp"
00006 #include "TestUtil.hpp"
00007 #include <cstdlib>
00008 #include <algorithm>
00009 
00010 void test_coords_connect_iterate();
00011 void test_scd_invalid();
00012 void test_iterates();
00013 
00014 using namespace moab;
00015 
00016 int main()
00017 {
00018     int failures = 0;
00019 
00020     failures += RUN_TEST( test_coords_connect_iterate );
00021     failures += RUN_TEST( test_scd_invalid );
00022     failures += RUN_TEST( test_iterates );
00023 
00024     if( failures ) std::cerr << "<<<< " << failures << " TESTS FAILED >>>>" << std::endl;
00025 
00026     return failures;
00027 }
00028 
00029 void test_coords_connect_iterate()
00030 {
00031     // create 1000 vertices
00032     const unsigned int NUM_VTX = 1000;
00033     Core moab;
00034     Interface& mb = moab;
00035     std::vector< double > coords( 3 * NUM_VTX );
00036     for( unsigned int i = 0; i < NUM_VTX; i++ )
00037         coords[3 * i] = coords[3 * i + 1] = coords[3 * i + 2] = i;
00038 
00039     Range verts, hexes, faces, edges, dead;
00040     ErrorCode rval = mb.create_vertices( &coords[0], NUM_VTX, verts );CHECK_ERR( rval );
00041 
00042     // create a bunch of hexes from those
00043     ReadUtilIface* rui;
00044     EntityHandle *orig_connect, start_hex;
00045     rval = mb.query_interface( rui );CHECK_ERR( rval );
00046     rval = rui->get_element_connect( NUM_VTX / 8, 8, MBHEX, 1, start_hex, orig_connect );CHECK_ERR( rval );
00047     std::copy( verts.begin(), verts.end(), orig_connect );
00048     hexes.insert( start_hex, start_hex + NUM_VTX / 8 - 1 );
00049 
00050     // delete about 1% of vertices
00051     const int step      = 100;
00052     int remaining       = NUM_VTX;
00053     Range::iterator vit = verts.begin();
00054     EntityHandle entities[2];
00055     for( int j = 0; j < remaining; j += step )
00056     {
00057         entities[0] = start_hex + j / 8;
00058         entities[1] = *vit;
00059         rval        = mb.delete_entities( entities, 2 );CHECK_ERR( rval );
00060         dead.insert( *vit );
00061         dead.insert( start_hex + j / 8 );
00062         vit = verts.erase( vit );
00063         vit += step - 1;
00064         hexes.erase( start_hex + j / 8 );
00065     }
00066 
00067     // Remove some additional values from the range
00068     // so that our handle blocks don't always align with
00069     // sequences
00070     verts.erase( verts.begin() + ( step - 5 ), verts.begin() + ( step + 5 ) );
00071     hexes.erase( hexes.begin() + ( step / 8 - 5 ), hexes.begin() + ( step / 8 + 5 ) );
00072 
00073     // Check that we get back expected values
00074     double *xcoord, *ycoord, *zcoord;
00075     vit = verts.begin();
00076     int count, total = 0;
00077     while( vit != verts.end() )
00078     {
00079         rval = mb.coords_iterate( vit, verts.end(), xcoord, ycoord, zcoord, count );
00080         if( MB_SUCCESS && ( !xcoord || !ycoord || !zcoord ) ) rval = MB_FAILURE;CHECK_ERR( rval );
00081 
00082         assert( total + count <= (int)verts.size() );
00083         for( int i = 0; i < count; i++ )
00084         {
00085             // vertex handles start at 1, so need to subtract one
00086             double val = *vit + (double)i - 1.0;
00087             CHECK_REAL_EQUAL( val, xcoord[i], 1.0e-10 );
00088             CHECK_REAL_EQUAL( val, ycoord[i], 1.0e-10 );
00089             CHECK_REAL_EQUAL( val, zcoord[i], 1.0e-10 );
00090         }
00091 
00092         // Check that we can set values and get the right values back
00093         for( int i = 0; i < count; i++ )
00094         {
00095             xcoord[i] *= 2.0;
00096             ycoord[i] *= 2.0;
00097             zcoord[i] *= 2.0;
00098         }
00099 
00100         std::vector< double > dum( 3 * count );
00101         Range dum_verts( *vit, *vit + count - 1 );
00102         rval = mb.get_coords( dum_verts, &dum[0] );CHECK_ERR( rval );
00103         for( int i = 0; i < count; i++ )
00104         {
00105             // vertex handles start at 1, so need to subtract 1 from expected value
00106             double val = 2.0 * ( *vit + (double)i - 1 );
00107             CHECK_REAL_EQUAL( val, xcoord[i], 1.0e-10 );
00108             CHECK_REAL_EQUAL( val, ycoord[i], 1.0e-10 );
00109             CHECK_REAL_EQUAL( val, zcoord[i], 1.0e-10 );
00110         }
00111 
00112         vit += count;
00113         total += count;
00114     }
00115 
00116     // now check connectivity
00117     Range::iterator hit   = hexes.begin();
00118     EntityHandle* connect = NULL;
00119     EntityHandle dum_connect[8];
00120     int num_connect;
00121     while( hit != hexes.end() )
00122     {
00123         rval = mb.connect_iterate( hit, hexes.end(), connect, num_connect, count );
00124         if( MB_SUCCESS && !connect ) rval = MB_FAILURE;CHECK_ERR( rval );
00125         CHECK_EQUAL( num_connect, 8 );
00126 
00127         // should be equal to initial connectivity
00128         for( int i = 0; i < count; i++ )
00129         {
00130             EntityHandle first = 8 * ( *hit - start_hex + i ) + 1;
00131             for( unsigned int j = 0; j < 8; j++ )
00132                 dum_connect[j] = first + j;
00133             CHECK_ARRAYS_EQUAL( connect, 8, &dum_connect[0], 8 );
00134             connect += 8;
00135         }
00136 
00137         hit += count;
00138         // total += count;
00139     }
00140 
00141     // ok, done
00142 }
00143 
00144 void test_scd_invalid()
00145 {
00146     // check that we get errors from structured mesh
00147     Core moab;
00148     Interface& mb = moab;
00149     ScdInterface* scdi;
00150     ErrorCode rval = mb.query_interface( scdi );CHECK_ERR( rval );
00151 
00152     // make an arbitrary structured mesh
00153     const int NUM_DIMS = 10;
00154     HomCoord low( 0, 0, 0 ), high( NUM_DIMS, NUM_DIMS, NUM_DIMS );
00155     ScdBox* new_box = NULL;
00156     rval            = scdi->construct_box( low, high, NULL, 0, new_box );CHECK_ERR( rval );
00157     CHECK( new_box != NULL );
00158 
00159     EntityHandle start_hex = new_box->start_element();
00160     Range hexes( start_hex, start_hex + NUM_DIMS * NUM_DIMS * NUM_DIMS - 1 );
00161 
00162     // should be able to get vertices used by this box
00163     Range verts;
00164     rval = mb.get_adjacencies( hexes, 0, false, verts, Interface::UNION );CHECK_ERR( rval );
00165     CHECK_EQUAL( (int)verts.size(), (int)( ( NUM_DIMS + 1 ) * ( NUM_DIMS + 1 ) * ( NUM_DIMS + 1 ) ) );
00166 
00167     // should NOT be able to get connect iterator
00168     EntityHandle* connect;
00169     int count, num_connect;
00170     // expected failure
00171     rval = mb.connect_iterate( hexes.begin(), hexes.end(), connect, num_connect, count );
00172     CHECK_EQUAL( rval, MB_FAILURE );
00173 }
00174 
00175 // these tests are for sequences that result in contiguous entity ranges
00176 // sequences are still broken in memory
00177 void test_iterates()
00178 {
00179     // create 400 vertices, in 2 separate sequences
00180     // also, create 50 hexes, in 2 separate sequences
00181     const int NUM_VTX = 400;
00182     const int NUM_HEX = 50;
00183     Core moab;
00184     Interface& mb = moab;
00185     std::vector< double > coords( 3 * NUM_VTX / 2 );
00186     for( unsigned int i = 0; i < NUM_VTX / 2; i++ )
00187     {
00188         coords[3 * i]     = i;
00189         coords[3 * i + 1] = 10 * i;
00190         coords[3 * i + 2] = 100 * i;
00191     }
00192 
00193     Range verts, hexes;
00194     ErrorCode rval = mb.create_vertices( &coords[0], NUM_VTX / 2, verts );CHECK_ERR( rval );
00195 
00196     // create more vertices, with same coordinates
00197     Range v2;
00198     rval = mb.create_vertices( &coords[0], NUM_VTX / 2, v2 );CHECK_ERR( rval );
00199     Range ver = unite( verts, v2 );
00200     // range of vertices is contiguous, but its memory for vertices is not!!
00201     CHECK_EQUAL( (int)ver.psize(), 1 );
00202     // create a bunch of hexes from those
00203     ReadUtilIface* rui;
00204     EntityHandle start_hex;
00205     rval = mb.query_interface( rui );CHECK_ERR( rval );
00206 
00207     Range::iterator vertIter = ver.begin();
00208     for( int i = 0; i < NUM_HEX; i += NUM_HEX / 2 )
00209     {
00210         EntityHandle *orig_connect, current_hex;
00211         Range::iterator vertIterEnd = vertIter + ( 25 * 8 );
00212         rval                        = rui->get_element_connect( 25, 8, MBHEX, 1, current_hex, orig_connect );CHECK_ERR( rval );
00213         std::copy( vertIter, vertIterEnd, orig_connect );
00214         hexes.insert( current_hex, current_hex + 24 );
00215         vertIter += ( NUM_HEX / 2 * 8 );
00216     }
00217 
00218     // make sure hexes range is contiguous
00219     CHECK_EQUAL( (int)hexes.psize(), 1 );
00220     start_hex = hexes.front();
00221     Tag idtag = moab.globalId_tag();
00222 
00223     for( int i = 0; i < NUM_HEX; i++, start_hex++ )
00224     {
00225         rval = mb.tag_set_data( idtag, &start_hex, 1, &i );CHECK_ERR( rval );
00226     }
00227     int count = 0;
00228     int total = 0;
00229 
00230     // now check connectivity
00231     start_hex             = hexes.front();
00232     Range::iterator hit   = hexes.begin();
00233     EntityHandle* connect = NULL;
00234     EntityHandle dum_connect[8];
00235     int num_connect;
00236     while( hit != hexes.end() )
00237     {
00238         rval = mb.connect_iterate( hit, hexes.end(), connect, num_connect, count );
00239         if( MB_SUCCESS && !connect ) rval = MB_FAILURE;
00240 
00241         CHECK_ERR( rval );
00242         CHECK_EQUAL( num_connect, 8 );
00243         CHECK_EQUAL( count, NUM_HEX / 2 );
00244         // should be equal to initial connectivity
00245         for( int i = 0; i < count; i++ )
00246         {
00247             EntityHandle first = 8 * ( *hit - start_hex + i ) + 1;
00248             for( unsigned int j = 0; j < 8; j++ )
00249                 dum_connect[j] = first + j;
00250             CHECK_ARRAYS_EQUAL( connect, 8, &dum_connect[0], 8 );
00251             connect += 8;
00252         }
00253 
00254         hit += count;
00255         total += count;
00256     }
00257 
00258     hit = hexes.begin();
00259     void* ptr;
00260     while( hit != hexes.end() )
00261     {
00262         // get contiguous block of tag data, of size of the sequence allocated
00263         rval = mb.tag_iterate( idtag, hit, hexes.end(), count, ptr );CHECK_ERR( rval );
00264         CHECK_EQUAL( count, NUM_HEX / 2 );
00265         hit += count;
00266     }
00267 
00268     hit = ver.begin();
00269     // coords_iterate
00270     double* xc;
00271     double* yc;
00272     double* zc;
00273     while( hit != ver.end() )
00274     {
00275         // get contiguous block of coords
00276 
00277         rval = mb.coords_iterate( hit, ver.end(), xc, yc, zc, count );CHECK_ERR( rval );
00278         CHECK_EQUAL( count, NUM_VTX / 2 );
00279         hit += count;
00280     }
00281 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines