MOAB: Mesh Oriented datABase  (version 5.2.1)
imoab_test.cpp
Go to the documentation of this file.
00001 #include "moab/MOABConfig.h"
00002 
00003 #ifdef MOAB_HAVE_MPI
00004 #include "moab_mpi.h"
00005 #endif
00006 
00007 #include "moab/iMOAB.h"
00008 #include "TestUtil.hpp"
00009 
00010 // for malloc, free:
00011 #include <stdlib.h>
00012 #include <stdio.h>
00013 #include <string.h>
00014 
00015 #define CHECKRC( rc, message )   \
00016     if( 0 != rc )                \
00017     {                            \
00018         printf( "%s", message ); \
00019         return 1;                \
00020     }
00021 
00022 int main( int argc, char* argv[] )
00023 {
00024     int nprocs = 1, rank = 0;
00025     std::string filen;
00026 #ifdef MOAB_HAVE_MPI
00027     MPI_Init( &argc, &argv );
00028 
00029     MPI_Comm comm = MPI_COMM_WORLD;
00030     MPI_Comm_size( comm, &nprocs );
00031     MPI_Comm_rank( comm, &rank );
00032 #endif
00033 
00034 #ifdef MOAB_HAVE_HDF5
00035     filen = TestDir + "/io/p8ex1.h5m";
00036 #endif
00037 
00038     if( argc > 1 ) filen = std::string( argv[1] );
00039 
00040     if( !filen.size() ) return 1;
00041     /*
00042      * MOAB needs to be initialized; A MOAB instance will be created, and will be used by each
00043      * application in this framework. There is no parallel context yet.
00044      */
00045     ErrCode rc = iMOAB_Initialize( argc, argv );
00046 
00047     CHECKRC( rc, "failed to initialize MOAB" );
00048     int num_global_vertices = 0, num_global_elements = 0, num_dimension = 0, num_parts = 0;
00049     /*
00050      * Header information is cheap to retrieve from an hdf5 file; it can be called by each process
00051      * or can be called by one master task and broadcasted. The hdf5 file has to be in MOAB native
00052      * format, and has to be partitioned in advance. There should be at least as many partitions in
00053      * the file as number of tasks in the communicator.
00054      */
00055     rc = iMOAB_ReadHeaderInfo( filen.c_str(), &num_global_vertices, &num_global_elements, &num_dimension, &num_parts,
00056                                (int)( filen.size() ) );
00057 
00058     CHECKRC( rc, "failed to read header info" );
00059 
00060     if( 0 == rank )
00061     {
00062         printf( "file %s has %d vertices, %d elements, %d parts in partition\n", filen.c_str(), num_global_vertices,
00063                 num_global_elements, num_parts );
00064     }
00065     int appID, appDupID;
00066     iMOAB_AppID pid    = &appID;
00067     iMOAB_AppID pidDup = &appDupID;
00068     /*
00069      * Each application has to be registered once. A mesh set and a parallel communicator will be
00070      * associated with each application. A unique application id will be returned, and will be used
00071      * for all future mesh operations/queries.
00072      */
00073     int compid = 11, compdupid = 12;
00074     rc = iMOAB_RegisterApplication( "MBAPP",
00075 #ifdef MOAB_HAVE_MPI
00076                                     &comm,
00077 #endif
00078                                     &compid, pid );
00079     CHECKRC( rc, "failed to register application" );
00080 
00081     rc = iMOAB_RegisterApplication( "MBDUPAPP",
00082 #ifdef MOAB_HAVE_MPI
00083                                     &comm,
00084 #endif
00085                                     &compdupid, pidDup );
00086     CHECKRC( rc, "failed to register duplicate application" );
00087 
00088 #ifdef MOAB_HAVE_MPI
00089     const char* read_opts = "PARALLEL=READ_PART;PARTITION=PARALLEL_PARTITION;PARALLEL_RESOLVE_SHARED_ENTS";
00090 #else
00091     const char* read_opts = "";
00092 #endif
00093     int num_ghost_layers = 1;
00094 
00095     /*
00096      * Loading the mesh is a parallel IO operation. Ghost layers can be exchanged too, and default
00097      * MOAB sets are augmented with ghost elements. By convention, blocks correspond to MATERIAL_SET
00098      * sets, side sets with NEUMANN_SET sets, node sets with DIRICHLET_SET sets. Each element and
00099      * vertex entity should have a GLOBAL ID tag in the file, which will be available for visible
00100      * entities
00101      */
00102     rc = iMOAB_LoadMesh( pid, filen.c_str(), read_opts, &num_ghost_layers, (int)( filen.size() ), strlen( read_opts ) );
00103     CHECKRC( rc, "failed to load mesh" );
00104 
00105     rc = iMOAB_LoadMesh( pidDup, filen.c_str(), read_opts, &num_ghost_layers, (int)( filen.size() ),
00106                          strlen( read_opts ) );
00107     CHECKRC( rc, "failed to load mesh" );
00108 
00109     rc = iMOAB_SetGlobalInfo( pid, &num_global_vertices, &num_global_elements );
00110     CHECKRC( rc, "failed to set global info" );
00111 
00112     int nverts[3], nelem[3], nblocks[3], nsbc[3], ndbc[3];
00113     /*
00114      * Each process in the communicator will have access to a local mesh instance, which will
00115      * contain the original cells in the local partition and ghost entities. Number of vertices,
00116      * primary cells, visible blocks, number of sidesets and nodesets boundary conditions will be
00117      * returned in size 3 arrays, for local, ghost and total numbers.
00118      */
00119     rc = iMOAB_GetMeshInfo( pid, nverts, nelem, nblocks, nsbc, ndbc );
00120     CHECKRC( rc, "failed to get mesh info" );
00121 
00122     iMOAB_GlobalID* vGlobalID = (iMOAB_GlobalID*)malloc( nverts[2] * sizeof( iMOAB_GlobalID ) );
00123     /*
00124      * All visible vertices have a unique global ID. All arrays are allocated by client.
00125      * Currently, global ID is a 32 bit integer, total number of visible vertices was returned by
00126      * iMOAB_GetMeshInfo
00127      */
00128     rc = iMOAB_GetVertexID( pid, &nverts[2], vGlobalID );
00129     CHECKRC( rc, "failed to get vertex id info" );
00130 
00131     int* vranks = (int*)malloc( nverts[2] * sizeof( int ) );
00132     /*
00133      * In MOAB terminology, a vertex can be owned, shared or ghost. An owned vertex will be
00134      * owned by the current process. A shared vertex appears at the interface between partitions, it
00135      * can be owned by another process, while a ghost vertex is owned by another process always. All
00136      * vertex arrays have a natural local order, in which the ghost vertices are at the end of the
00137      * arrays. The local index of the vertex will vary from 0 to number of visible vertices (in this
00138      * example nverts[2]-1)
00139      */
00140     rc = iMOAB_GetVertexOwnership( pid, &nverts[2], vranks );
00141     CHECKRC( rc, "failed to get vertex ranks" );
00142 
00143     double* coords  = (double*)malloc( 3 * nverts[2] * sizeof( double ) );
00144     int size_coords = 3 * nverts[2];
00145     /*
00146      * Coordinates are returned interleaved, for all visible vertices. Size is dimension times
00147      * number of visible vertices
00148      */
00149     rc = iMOAB_GetVisibleVerticesCoordinates( pid, &size_coords, coords );
00150     CHECKRC( rc, "failed to get coordinates" );
00151 
00152     /*
00153      * Block IDs correspond to MATERIAL_SET tag values in hdf5 file
00154      * They also correspond to block IDs in a Cubit model.
00155      * Local visible blocks might contain ghost cells, owned by other
00156      * processes.
00157      * Inside MOAB, a block will correspond to a meshset with a MATERIAL_SET tag,
00158      * with value the block ID.
00159      * A MATERIAL_SET tag in moab if SPARSE and type INTEGER.
00160      */
00161     iMOAB_GlobalID* gbIDs = (iMOAB_GlobalID*)malloc( nblocks[2] * sizeof( iMOAB_GlobalID ) );
00162     rc                    = iMOAB_GetBlockID( pid, &nblocks[2], gbIDs );
00163     CHECKRC( rc, "failed to get block info" );
00164 
00165     /*
00166      * The 2 tags used in this example exist in the file, already.
00167      * If a user needs a new tag, it can be defined with the same call as this one
00168      * this method, iMOAB_DefineTagStorage, will return a local index for the tag.
00169      * The name of the tag is case sensitive.
00170      * This method is collective.
00171      */
00172     int tagIndex[2];
00173     int entTypes[2]    = { 0, 1 }; /* first is on vertex, second is on elements; */
00174     int tagTypes[2]    = { DENSE_INTEGER, DENSE_DOUBLE };
00175     int num_components = 1;
00176     rc = iMOAB_DefineTagStorage( pid, "INTFIELD", &tagTypes[0], &num_components, &tagIndex[0], strlen( "INTFIELD" ) );
00177     CHECKRC( rc, "failed to get tag INTFIELD " );
00178 
00179     rc = iMOAB_DefineTagStorage( pid, "DFIELD", &tagTypes[1], &num_components, &tagIndex[1], strlen( "DFIELD" ) );
00180     CHECKRC( rc, "failed to get tag DFIELD " );
00181 
00182     // synchronize one of the tags only, just to see what happens
00183     int num_tags_to_sync = 1;
00184     /*
00185      * tags are not exchanged for ghost entities by default. The user has to explicitly
00186      * synchronize them with a call like this one. The tags to synchronize are referenced by the
00187      * index returned at their "definition" with iMOAB_DefineTagStorage method.
00188      */
00189     rc = iMOAB_SynchronizeTags( pid, &num_tags_to_sync, &tagIndex[0], &tagTypes[0] );
00190     CHECKRC( rc, "failed to sync tag INTFIELD " );
00191 
00192     for( int irank = 0; irank < nprocs; irank++ )
00193     {
00194         if( irank == rank )
00195         {
00196             /* printf some of the block info */
00197             printf( "on rank %d, there are \n"
00198                     "  %3d visible vertices of which  %3d local  %3d ghost \n"
00199                     "  %3d visible elements of which  %3d owned  %3d ghost \n"
00200                     "  %3d visible blocks\n"
00201                     "  %3d visible neumann BCs\n"
00202                     "  %3d visible dirichlet BCs\n",
00203                     rank, nverts[2], nverts[0], nverts[1], nelem[2], nelem[0], nelem[1], nblocks[2], nsbc[2], ndbc[2] );
00204             /* printf some of the vertex id infos */
00205             int numToPrint = nverts[2];
00206             printf( "on rank %d vertex info:\n", rank );
00207             for( int i = 0; i < numToPrint; i++ )
00208                 printf( " vertex local id: %3d, rank ID:%d  global ID: %3d  coords: %g, %g, %g\n", i, vranks[i],
00209                         vGlobalID[i], coords[3 * i], coords[3 * i + 1], coords[3 * i + 2] );
00210 
00211             iMOAB_GlobalID* element_global_IDs = (iMOAB_GlobalID*)malloc( nelem[2] * sizeof( iMOAB_GlobalID ) );
00212             iMOAB_GlobalID* block_IDs          = (iMOAB_GlobalID*)malloc( nelem[2] * sizeof( iMOAB_GlobalID ) );
00213             int* ranks                         = (int*)malloc( nelem[2] * sizeof( int ) );
00214             /*
00215              * visible elements info is available for all visible elements, with this call. Similar
00216              * information can be retrieved by looping over visible blocks
00217              */
00218             rc = iMOAB_GetVisibleElementsInfo( pid, &nelem[2], element_global_IDs, ranks, block_IDs );
00219             CHECKRC( rc, "failed to get all elem info" );
00220             for( int i = 0; i < nelem[2]; i++ )
00221                 printf( " element local id: %3d,  global ID: %3d  rank:%d  block ID: %2d \n", i, element_global_IDs[i],
00222                         ranks[i], block_IDs[i] );
00223             free( element_global_IDs );
00224             free( ranks );
00225             free( block_IDs );
00226 
00227             // get first element connectivity
00228             int conn[27];
00229             int nv     = 27;
00230             int eindex = 0;
00231             /*
00232              * element connectivity can be retrieved for a single element. Local vertex indices are
00233              * returned.
00234              */
00235             rc = iMOAB_GetElementConnectivity( pid, &eindex, &nv, conn );
00236             CHECKRC( rc, "failed to get first element connectivity" );
00237             printf( " conn for first element: \n" );
00238             for( int i = 0; i < nv; i++ )
00239                 printf( " %3d", conn[i] );
00240             printf( "\n" );
00241 
00242             // test neighbors
00243             int local_index           = 0;   // test element with local index 0
00244             int num_adjacent_elements = 10;  // we can have maximum 6 actually
00245             iMOAB_LocalID adjacent_element_IDs[10];
00246             /*
00247              * Neighbor elements that share a face with current element can be determined with this
00248              * call Elements are identified by their local index
00249              */
00250             rc = iMOAB_GetNeighborElements( pid, &local_index, &num_adjacent_elements, adjacent_element_IDs );
00251             CHECKRC( rc, "failed to get first element neighbors" );
00252             printf( "  neighbors for first element:\n" );
00253             for( int i = 0; i < num_adjacent_elements; i++ )
00254             {
00255                 printf( "  %4d", adjacent_element_IDs[i] );
00256             }
00257             printf( "\n" );
00258 
00259             for( int i = 0; i < nblocks[2]; i++ )
00260             {
00261                 printf( " block index: %3d, block ID: %3d \n", i, gbIDs[i] );
00262                 int vertices_per_element, num_elements_in_block;
00263                 /*
00264                  * blocks should have the same type of primary elements. Number of elements in block
00265                  * and number of vertices per element are found with this call. The method refers
00266                  * only to the visible elements on the process. The block is identified with its
00267                  * block ID. Should it be identified with its local index in the list of visible
00268                  * blocks?
00269                  */
00270                 rc = iMOAB_GetBlockInfo( pid, &gbIDs[i], &vertices_per_element, &num_elements_in_block );
00271                 CHECKRC( rc, "failed to elem block info" );
00272                 printf( "    has %4d elements with %d vertices per element\n", num_elements_in_block,
00273                         vertices_per_element );
00274                 int size_conn                       = num_elements_in_block * vertices_per_element;
00275                 iMOAB_LocalID* element_connectivity = (iMOAB_LocalID*)malloc( sizeof( iMOAB_LocalID ) * size_conn );
00276                 /*
00277                  * Connectivity for all elements in the block can be determined with this method.
00278                  * Vertices are identified by their local index (from 0 to number of visible
00279                  * vertices -1)
00280                  */
00281                 rc = iMOAB_GetBlockElementConnectivities( pid, &gbIDs[i], &size_conn, element_connectivity );
00282                 CHECKRC( rc, "failed to get block elem connectivity" );
00283                 int* element_ownership = (int*)malloc( sizeof( int ) * num_elements_in_block );
00284 
00285                 /*
00286                  * Each element is owned by a particular process. Ownership can be returned for all
00287                  * elements in a block with this call.
00288                  */
00289                 rc = iMOAB_GetElementOwnership( pid, &gbIDs[i], &num_elements_in_block, element_ownership );
00290                 CHECKRC( rc, "failed to get block elem ownership" );
00291                 iMOAB_GlobalID* global_element_ID =
00292                     (iMOAB_GlobalID*)malloc( sizeof( iMOAB_GlobalID ) * num_elements_in_block );
00293                 iMOAB_LocalID* local_element_ID =
00294                     (iMOAB_LocalID*)malloc( sizeof( iMOAB_LocalID ) * num_elements_in_block );
00295 
00296                 /*
00297                  * Global element IDs are determined with this call. Local indices within the
00298                  * process are returned too.
00299                  */
00300                 rc = iMOAB_GetElementID( pid, &gbIDs[i], &num_elements_in_block, global_element_ID, local_element_ID );
00301                 CHECKRC( rc, "failed to get block elem IDs" );
00302                 for( int j = 0; j < num_elements_in_block; j++ )
00303                 {
00304                     printf( "  elem %3d owned by %d gid: %4d lid: %4d  -- ", j, element_ownership[j],
00305                             global_element_ID[j], local_element_ID[j] );
00306                     for( int k = 0; k < vertices_per_element; k++ )
00307                         printf( " %5d", element_connectivity[j * vertices_per_element + k] );
00308                     printf( "\n" );
00309                 }
00310                 free( global_element_ID );
00311                 free( local_element_ID );
00312                 free( element_connectivity );
00313                 free( element_ownership );
00314             }
00315             /*
00316              * query integer tag values on vertices
00317              * this tag was synchronized (see iMOAB_SynchronizeTags above)
00318              * Being a dense tag, all visible vertices have a value.
00319              * As usual, the client allocates the returned array, which is filled with data with
00320              * this call (deep copy)
00321              */
00322             int* int_tag_vals = (int*)malloc( sizeof( int ) * nverts[2] );  // for all visible vertices on the rank
00323             rc =
00324                 iMOAB_GetIntTagStorage( pid, "INTFIELD", &nverts[2], &entTypes[0], int_tag_vals, strlen( "INTFIELD" ) );
00325             CHECKRC( rc, "failed to get INTFIELD tag" );
00326             printf( "INTFIELD tag values:\n" );
00327             for( int i = 0; i < nverts[2]; i++ )
00328             {
00329                 printf( " %4d", int_tag_vals[i] );
00330                 if( i % 20 == 19 ) printf( "\n" );
00331             }
00332             printf( "\n" );
00333             free( int_tag_vals );
00334 
00335             /*
00336              * query double tag values on elements
00337              * This tag was not synchronized, so ghost elements have a default value of 0.
00338              */
00339             double* double_tag_vals =
00340                 (double*)malloc( sizeof( double ) * nelem[2] );  // for all visible elements on the rank
00341             rc = iMOAB_GetDoubleTagStorage( pid, "DFIELD", &nelem[2], &entTypes[1], double_tag_vals,
00342                                             strlen( "DFIELD" ) );
00343             CHECKRC( rc, "failed to get DFIELD tag" );
00344             printf( "DFIELD tag values: (not exchanged) \n" );
00345             for( int i = 0; i < nelem[2]; i++ )
00346             {
00347                 printf( " %f", double_tag_vals[i] );
00348                 if( i % 8 == 7 ) printf( "\n" );
00349             }
00350             printf( "\n" );
00351             free( double_tag_vals );
00352 
00353             /* query surface BCs */
00354             iMOAB_LocalID* surfBC_ID = (iMOAB_LocalID*)malloc( sizeof( iMOAB_LocalID ) * nsbc[2] );
00355             int* ref_surf            = (int*)malloc( sizeof( int ) * nsbc[2] );
00356             int* bc_value            = (int*)malloc( sizeof( int ) * nsbc[2] );
00357             /*
00358              * Surface boundary condition information is returned for all visible Neumann conditions
00359              * the reference surfaces are indexed from 1 to 6 for hexahedrons, 1 to 4 to
00360              * tetrahedrons, in the MOAB canonical ordering convention
00361              */
00362             rc = iMOAB_GetPointerToSurfaceBC( pid, &nsbc[2], surfBC_ID, ref_surf, bc_value );
00363             CHECKRC( rc, "failed to get surf boundary conditions" );
00364             printf( " Surface boundary conditions:\n" );
00365             for( int i = 0; i < nsbc[2]; i++ )
00366             {
00367                 printf( "  elem_localID %4d  side:%d  BC:%2d\n", surfBC_ID[i], ref_surf[i], bc_value[i] );
00368             }
00369             free( surfBC_ID );
00370             free( ref_surf );
00371             free( bc_value );
00372             /* query vertex BCs */
00373             iMOAB_LocalID* vertBC_ID = (iMOAB_LocalID*)malloc( sizeof( iMOAB_LocalID ) * ndbc[2] );
00374             int* vertBC_value        = (int*)malloc( sizeof( int ) * ndbc[2] );
00375             rc                       = iMOAB_GetPointerToVertexBC( pid, &ndbc[2], vertBC_ID, vertBC_value );
00376             CHECKRC( rc, "failed to get vertex boundary conditions" );
00377             printf( "  Vertex boundary conditions:\n" );
00378             for( int i = 0; i < ndbc[2]; i++ )
00379             {
00380                 printf( "   vertex %4d   BC:%2d\n", vertBC_ID[i], vertBC_value[i] );
00381             }
00382             free( vertBC_ID );
00383             free( vertBC_value );
00384         }
00385 #ifdef MOAB_HAVE_MPI
00386         MPI_Barrier( comm );  // to avoid printing problems, as we write all procs data
00387 #endif
00388     }
00389 
00390     // free allocated data
00391     free( coords );
00392     free( vGlobalID );
00393     free( vranks );
00394     char outputFile[] = "fnew.h5m";
00395 #ifdef MOAB_HAVE_MPI
00396     char writeOptions[] = "PARALLEL=WRITE_PART";
00397 #else
00398     char writeOptions[]   = "";
00399 #endif
00400     /*
00401      * the file can be written in parallel, and it will contain additional tags defined by the user
00402      * we may extend the method to write only desired tags to the file
00403      */
00404     rc = iMOAB_WriteMesh( pid, outputFile, writeOptions, strlen( outputFile ), strlen( writeOptions ) );
00405 
00406     /*
00407      * deregistering application will delete all mesh entities associated with the application and
00408      * will free allocated tag storage.
00409      */
00410     rc = iMOAB_DeregisterApplication( pid );
00411     CHECKRC( rc, "failed to de-register application" );
00412 
00413     rc = iMOAB_DeregisterApplication( pidDup );
00414     CHECKRC( rc, "failed to de-register application" );
00415 
00416     /*
00417      * this method will delete MOAB instance
00418      */
00419     rc = iMOAB_Finalize();
00420     CHECKRC( rc, "failed to finalize MOAB" );
00421 #ifdef MOAB_HAVE_MPI
00422     MPI_Finalize();
00423 #endif
00424 
00425     return 0;
00426 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines