MOAB: Mesh Oriented datABase  (version 5.4.1)
nastran_test.cpp
Go to the documentation of this file.
00001 #include "TestUtil.hpp"
00002 #include "moab/Core.hpp"
00003 #include "MBTagConventions.hpp"
00004 #define IS_BUILDING_MB
00005 #include "moab/Range.hpp"
00006 #include <cmath>
00007 #include <algorithm>
00008 
00009 using namespace moab;
00010 
00011 #ifdef MESHDIR
00012 static const char example[] = STRINGIFY( MESHDIR ) "/io/test.nas";
00013 #else
00014 static const char example[] = "test.nas";
00015 #endif
00016 
00017 void read_file( Interface& moab, const char* input_file );
00018 void test_read_nodes();
00019 void test_read_tets();
00020 void test_read_prisms();
00021 void test_read_hexes();
00022 void test_read_material_set1();
00023 void test_read_material_set2();
00024 
00025 int main()
00026 {
00027     int result = 0;
00028 
00029     result += RUN_TEST( test_read_nodes );
00030     result += RUN_TEST( test_read_tets );
00031     result += RUN_TEST( test_read_prisms );
00032     result += RUN_TEST( test_read_hexes );
00033     result += RUN_TEST( test_read_material_set1 );
00034     result += RUN_TEST( test_read_material_set2 );
00035 
00036     return result;
00037 }
00038 
00039 void read_file( Interface& moab, const char* input_file )
00040 {
00041     ErrorCode rval = moab.load_file( input_file );CHECK_ERR( rval );
00042 }
00043 
00044 void test_read_nodes()
00045 {
00046     const double eps = 1e-100;
00047     ErrorCode rval;
00048     Core moab;
00049     Interface& mb = moab;
00050     read_file( moab, example );
00051 
00052     std::vector< EntityHandle > nodes;
00053     rval = mb.get_entities_by_type( 0, MBVERTEX, nodes );CHECK_ERR( rval );
00054     CHECK_EQUAL( (size_t)19, nodes.size() );
00055 
00056     Tag id_tag = mb.globalId_tag();
00057 
00058     std::vector< int > ids( nodes.size() );
00059     rval = mb.tag_get_data( id_tag, &nodes[0], nodes.size(), &ids[0] );CHECK_ERR( rval );
00060 
00061     std::vector< int > sorted_ids( ids );
00062     std::sort( sorted_ids.begin(), sorted_ids.end() );
00063 
00064     std::vector< double > coords( 3 * nodes.size() );
00065     rval = mb.get_coords( &nodes[0], nodes.size(), &coords[0] );CHECK_ERR( rval );
00066 
00067     int idx, pos = 0;
00068     // shared between 2 tets and 2 prisms
00069     CHECK_EQUAL( pos + 1, sorted_ids[pos] );
00070     idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
00071     CHECK_REAL_EQUAL( coords[3 * idx + 0], 0.0, eps );
00072     CHECK_REAL_EQUAL( coords[3 * idx + 1], 1.0, eps );
00073     CHECK_REAL_EQUAL( coords[3 * idx + 2], -2.0, eps );
00074 
00075     ++pos;
00076     CHECK_EQUAL( pos + 1, sorted_ids[pos] );
00077     idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
00078     CHECK_REAL_EQUAL( coords[3 * idx + 0], -1.0, eps );
00079     CHECK_REAL_EQUAL( coords[3 * idx + 1], 0.0, eps );
00080     CHECK_REAL_EQUAL( coords[3 * idx + 2], -1.0, eps );
00081 
00082     ++pos;
00083     CHECK_EQUAL( pos + 1, sorted_ids[pos] );
00084     idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
00085     CHECK_REAL_EQUAL( coords[3 * idx + 0], 1.0, eps );
00086     CHECK_REAL_EQUAL( coords[3 * idx + 1], 0.0, eps );
00087     CHECK_REAL_EQUAL( coords[3 * idx + 2], -1.0, eps );
00088 
00089     ++pos;
00090     CHECK_EQUAL( pos + 1, sorted_ids[pos] );
00091     idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
00092     CHECK_REAL_EQUAL( coords[3 * idx + 0], 0.0, eps );
00093     CHECK_REAL_EQUAL( coords[3 * idx + 1], 1.0, eps );
00094     CHECK_REAL_EQUAL( coords[3 * idx + 2], -1.0, eps );
00095 
00096     ++pos;
00097     CHECK_EQUAL( pos + 1, sorted_ids[pos] );
00098     idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
00099     CHECK_REAL_EQUAL( coords[3 * idx + 0], -1.0, eps );
00100     CHECK_REAL_EQUAL( coords[3 * idx + 1], 0.0, eps );
00101     CHECK_REAL_EQUAL( coords[3 * idx + 2], 0.0, eps );
00102 
00103     ++pos;
00104     CHECK_EQUAL( pos + 1, sorted_ids[pos] );
00105     idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
00106     CHECK_REAL_EQUAL( coords[3 * idx + 0], 1.0, eps );
00107     CHECK_REAL_EQUAL( coords[3 * idx + 1], 0.0, eps );
00108     CHECK_REAL_EQUAL( coords[3 * idx + 2], 0.0, eps );
00109 
00110     ++pos;
00111     CHECK_EQUAL( pos + 1, sorted_ids[pos] );
00112     idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
00113     CHECK_REAL_EQUAL( coords[3 * idx + 0], 0.0, eps );
00114     CHECK_REAL_EQUAL( coords[3 * idx + 1], 1.0, eps );
00115     CHECK_REAL_EQUAL( coords[3 * idx + 2], 0.0, eps );
00116 
00117     ++pos;
00118     CHECK_EQUAL( pos + 1, sorted_ids[pos] );
00119     idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
00120     CHECK_REAL_EQUAL( coords[3 * idx + 0], -1.0, eps );
00121     CHECK_REAL_EQUAL( coords[3 * idx + 1], 0.0, eps );
00122     CHECK_REAL_EQUAL( coords[3 * idx + 2], 1.0, eps );
00123 
00124     ++pos;
00125     CHECK_EQUAL( pos + 1, sorted_ids[pos] );
00126     idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
00127     CHECK_REAL_EQUAL( coords[3 * idx + 0], 1.0, eps );
00128     CHECK_REAL_EQUAL( coords[3 * idx + 1], 0.0, eps );
00129     CHECK_REAL_EQUAL( coords[3 * idx + 2], 1.0, eps );
00130 
00131     ++pos;
00132     CHECK_EQUAL( pos + 1, sorted_ids[pos] );
00133     idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
00134     CHECK_REAL_EQUAL( coords[3 * idx + 0], 0.0, eps );
00135     CHECK_REAL_EQUAL( coords[3 * idx + 1], 1.0, eps );
00136     CHECK_REAL_EQUAL( coords[3 * idx + 2], 1.0, eps );
00137 
00138     ++pos;
00139     CHECK_EQUAL( pos + 1, sorted_ids[pos] );
00140     idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
00141     CHECK_REAL_EQUAL( coords[3 * idx + 0], 0.0, eps );
00142     CHECK_REAL_EQUAL( coords[3 * idx + 1], 1.0, eps );
00143     CHECK_REAL_EQUAL( coords[3 * idx + 2], 2.0, eps );
00144     // hex element
00145     ++pos;
00146     CHECK_EQUAL( pos + 1, sorted_ids[pos] );
00147     idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
00148     CHECK_REAL_EQUAL( coords[3 * idx + 0], 5.0, eps );
00149     CHECK_REAL_EQUAL( coords[3 * idx + 1], 5.0, eps );
00150     CHECK_REAL_EQUAL( coords[3 * idx + 2], 5.0, eps );
00151 
00152     ++pos;
00153     CHECK_EQUAL( pos + 1, sorted_ids[pos] );
00154     idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
00155     CHECK_REAL_EQUAL( coords[3 * idx + 0], 10.0, eps );
00156     CHECK_REAL_EQUAL( coords[3 * idx + 1], 5.0, eps );
00157     CHECK_REAL_EQUAL( coords[3 * idx + 2], 5.0, eps );
00158 
00159     ++pos;
00160     CHECK_EQUAL( pos + 1, sorted_ids[pos] );
00161     idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
00162     CHECK_REAL_EQUAL( coords[3 * idx + 0], 10.0, eps );
00163     CHECK_REAL_EQUAL( coords[3 * idx + 1], 10.0, eps );
00164     CHECK_REAL_EQUAL( coords[3 * idx + 2], 5.0, eps );
00165 
00166     ++pos;
00167     CHECK_EQUAL( pos + 1, sorted_ids[pos] );
00168     idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
00169     CHECK_REAL_EQUAL( coords[3 * idx + 0], 5.0, eps );
00170     CHECK_REAL_EQUAL( coords[3 * idx + 1], 10.0, eps );
00171     CHECK_REAL_EQUAL( coords[3 * idx + 2], 5.0, eps );
00172 
00173     ++pos;
00174     CHECK_EQUAL( pos + 1, sorted_ids[pos] );
00175     idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
00176     CHECK_REAL_EQUAL( coords[3 * idx + 0], 5.0, eps );
00177     CHECK_REAL_EQUAL( coords[3 * idx + 1], 5.0, eps );
00178     CHECK_REAL_EQUAL( coords[3 * idx + 2], 10.0, eps );
00179 
00180     ++pos;
00181     CHECK_EQUAL( pos + 1, sorted_ids[pos] );
00182     idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
00183     CHECK_REAL_EQUAL( coords[3 * idx + 0], 10.0, eps );
00184     CHECK_REAL_EQUAL( coords[3 * idx + 1], 5.0, eps );
00185     CHECK_REAL_EQUAL( coords[3 * idx + 2], 10.0, eps );
00186 
00187     ++pos;
00188     CHECK_EQUAL( pos + 1, sorted_ids[pos] );
00189     idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
00190     CHECK_REAL_EQUAL( coords[3 * idx + 0], 10.0, eps );
00191     CHECK_REAL_EQUAL( coords[3 * idx + 1], 10.0, eps );
00192     CHECK_REAL_EQUAL( coords[3 * idx + 2], 10.0, eps );
00193 
00194     ++pos;
00195     CHECK_EQUAL( pos + 1, sorted_ids[pos] );
00196     idx = std::find( ids.begin(), ids.end(), pos + 1 ) - ids.begin();
00197     CHECK_REAL_EQUAL( coords[3 * idx + 0], 5.0, eps );
00198     CHECK_REAL_EQUAL( coords[3 * idx + 1], 10.0, eps );
00199     CHECK_REAL_EQUAL( coords[3 * idx + 2], 10.0, eps );
00200 }
00201 
00202 void test_read_tets()
00203 {
00204     ErrorCode rval;
00205     Core moab;
00206     Interface& mb = moab;
00207     read_file( moab, example );
00208 
00209     std::vector< EntityHandle > tets;
00210     rval = mb.get_entities_by_type( 0, MBTET, tets );CHECK_ERR( rval );
00211     CHECK_EQUAL( (size_t)2, tets.size() );
00212 
00213     Tag id_tag = mb.globalId_tag();
00214 
00215     std::vector< int > ids( tets.size() );
00216     rval = mb.tag_get_data( id_tag, &tets[0], tets.size(), &ids[0] );CHECK_ERR( rval );
00217 
00218     if( ids[0] != 1 )
00219     {
00220         std::swap( ids[0], ids[1] );
00221         std::swap( tets[0], tets[1] );
00222     }
00223 
00224     int vtx_ids[4];
00225     const EntityHandle* conn;
00226     int len;
00227 
00228     const int conn1[] = { 8, 9, 10, 11 };
00229     int pos           = 0;
00230     CHECK_EQUAL( pos + 1, ids[pos] );
00231     rval = mb.get_connectivity( tets[pos], conn, len );CHECK_ERR( rval );
00232     CHECK_EQUAL( 4, len );
00233     rval = mb.tag_get_data( id_tag, conn, len, vtx_ids );CHECK_ERR( rval );
00234     CHECK_ARRAYS_EQUAL( conn1, 4, vtx_ids, len );
00235 
00236     const int conn2[] = { 4, 3, 2, 1 };
00237     ++pos;
00238     CHECK_EQUAL( pos + 1, ids[pos] );
00239     rval = mb.get_connectivity( tets[pos], conn, len );CHECK_ERR( rval );
00240     CHECK_EQUAL( 4, len );
00241     rval = mb.tag_get_data( id_tag, conn, len, vtx_ids );CHECK_ERR( rval );
00242     CHECK_ARRAYS_EQUAL( conn2, 4, vtx_ids, len );
00243 }
00244 
00245 void test_read_prisms()
00246 {
00247     ErrorCode rval;
00248     Core moab;
00249     Interface& mb = moab;
00250     read_file( moab, example );
00251 
00252     std::vector< EntityHandle > prisms;
00253     rval = mb.get_entities_by_type( 0, MBPRISM, prisms );CHECK_ERR( rval );
00254     CHECK_EQUAL( (size_t)2, prisms.size() );
00255 
00256     Tag id_tag = mb.globalId_tag();
00257 
00258     std::vector< int > ids( prisms.size() );
00259     rval = mb.tag_get_data( id_tag, &prisms[0], prisms.size(), &ids[0] );CHECK_ERR( rval );
00260 
00261     if( ids[0] != 3 )
00262     {
00263         std::swap( ids[0], ids[1] );
00264         std::swap( prisms[0], prisms[1] );
00265     }
00266 
00267     int vtx_ids[6];
00268     const EntityHandle* conn;
00269     int len;
00270 
00271     const int conn1[] = { 2, 3, 4, 5, 6, 7 };
00272     int pos           = 0;
00273     // Element ids 1 and 2 are the two tet elements.
00274     // Element ids 3 and 4 are the two prism elements.
00275     CHECK_EQUAL( pos + 3, ids[pos] );
00276     rval = mb.get_connectivity( prisms[pos], conn, len );CHECK_ERR( rval );
00277     CHECK_EQUAL( 6, len );
00278     rval = mb.tag_get_data( id_tag, conn, len, vtx_ids );CHECK_ERR( rval );
00279     CHECK_ARRAYS_EQUAL( conn1, 6, vtx_ids, len );
00280 
00281     const int conn2[] = { 5, 6, 7, 8, 9, 10 };
00282     ++pos;
00283     CHECK_EQUAL( pos + 3, ids[pos] );
00284     rval = mb.get_connectivity( prisms[pos], conn, len );CHECK_ERR( rval );
00285     CHECK_EQUAL( 6, len );
00286     rval = mb.tag_get_data( id_tag, conn, len, vtx_ids );CHECK_ERR( rval );
00287     CHECK_ARRAYS_EQUAL( conn2, 6, vtx_ids, len );
00288 }
00289 
00290 void test_read_hexes()
00291 {
00292     ErrorCode rval;
00293     Core moab;
00294     Interface& mb = moab;
00295     read_file( moab, example );
00296 
00297     std::vector< EntityHandle > hexes;
00298     rval = mb.get_entities_by_type( 0, MBHEX, hexes );CHECK_ERR( rval );
00299     CHECK_EQUAL( (size_t)1, hexes.size() );
00300 
00301     Tag id_tag = mb.globalId_tag();
00302 
00303     std::vector< int > ids( hexes.size() );
00304     rval = mb.tag_get_data( id_tag, &hexes[0], hexes.size(), &ids[0] );CHECK_ERR( rval );
00305 
00306     int vtx_ids[8];
00307     const EntityHandle* conn;
00308     int len;
00309 
00310     const int conn1[] = { 12, 13, 14, 15, 16, 17, 18, 19 };
00311     int pos           = 0;
00312     // Element id 5 is the hex
00313     CHECK_EQUAL( pos + 5, ids[pos] );
00314     rval = mb.get_connectivity( hexes[pos], conn, len );CHECK_ERR( rval );
00315     CHECK_EQUAL( 8, len );
00316     rval = mb.tag_get_data( id_tag, conn, len, vtx_ids );CHECK_ERR( rval );
00317     CHECK_ARRAYS_EQUAL( conn1, 8, vtx_ids, len );
00318 }
00319 
00320 // The tets are in material set 1.
00321 void test_read_material_set1()
00322 {
00323     ErrorCode rval;
00324     Core moab;
00325     Interface& mb = moab;
00326     read_file( moab, example );
00327 
00328     Tag mat_tag;
00329     rval = mb.tag_get_handle( MATERIAL_SET_TAG_NAME, 1, MB_TYPE_INTEGER, mat_tag );CHECK_ERR( rval );
00330 
00331     Range mat_set_one;
00332     const int one               = 1;
00333     const void* const one_val[] = { &one };
00334     rval                        = mb.get_entities_by_type_and_tag( 0, MBENTITYSET, &mat_tag, one_val, 1, mat_set_one );CHECK_ERR( rval );
00335     CHECK_EQUAL( 1, (int)mat_set_one.size() );
00336 
00337     std::vector< EntityHandle > tets, contents;
00338     rval = mb.get_entities_by_type( 0, MBTET, tets );CHECK_ERR( rval );
00339     rval = mb.get_entities_by_handle( mat_set_one.front(), contents );CHECK_ERR( rval );
00340     std::sort( tets.begin(), tets.end() );
00341     std::sort( contents.begin(), contents.end() );
00342     CHECK_EQUAL( tets, contents );
00343 }
00344 
00345 // The prisms are in material set 2.
00346 void test_read_material_set2()
00347 {
00348     ErrorCode rval;
00349     Core moab;
00350     Interface& mb = moab;
00351     read_file( moab, example );
00352 
00353     Tag mat_tag;
00354     rval = mb.tag_get_handle( MATERIAL_SET_TAG_NAME, 1, MB_TYPE_INTEGER, mat_tag );CHECK_ERR( rval );
00355 
00356     Range mat_set_two;
00357     const int two               = 2;
00358     const void* const two_val[] = { &two };
00359     rval                        = mb.get_entities_by_type_and_tag( 0, MBENTITYSET, &mat_tag, two_val, 1, mat_set_two );CHECK_ERR( rval );
00360     CHECK_EQUAL( 1, (int)mat_set_two.size() );
00361 
00362     std::vector< EntityHandle > prisms, contents;
00363     rval = mb.get_entities_by_type( 0, MBPRISM, prisms );CHECK_ERR( rval );
00364     rval = mb.get_entities_by_handle( mat_set_two.front(), contents );CHECK_ERR( rval );
00365     std::sort( prisms.begin(), prisms.end() );
00366     std::sort( contents.begin(), contents.end() );
00367     CHECK_EQUAL( prisms, contents );
00368 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines