MOAB: Mesh Oriented datABase  (version 5.4.1)
mbex1.cpp
Go to the documentation of this file.
00001 /// \file mbex1.cpp
00002 ///
00003 /// \author Milad Fatenejad
00004 ///
00005 /// \brief beginner tutorial, example 1: Demonstrates
00006 ///        constructing/saving a simple 2x2x2 hex mesh
00007 ///
00008 /// This example creates a 2x2x2 mesh (uniform grid) and writes it out
00009 /// to a VTK file and an H5M file. Each cell is of size 1x1x1 and is
00010 /// axis aligned. This example demonstrates how to manually create a
00011 /// mesh using the unstructured mesh interface. The mesh is then
00012 /// written out to file.
00013 
00014 // The moab/Core.hpp header file is needed for all MOAB work...
00015 #include "moab/Core.hpp"
00016 
00017 // C++ includes
00018 #include <iostream>
00019 
00020 // ****************
00021 // *              *
00022 // *     main     *
00023 // *              *
00024 // ****************
00025 int main()
00026 {
00027     moab::ErrorCode rval;
00028     // MOAB functionality is accessed through an instance of the
00029     // moab::Interface class:
00030     moab::Core mbcore;
00031 
00032     // ***************************
00033     // *   Create the vertexes   *
00034     // ***************************
00035 
00036     // We are going to create 27 vertexes which will be used to define 8
00037     // hexahedron cells. First, we have to create an array to store the
00038     // coordinates of each vertex.
00039 
00040     const unsigned NUMVTX = 27;  // The number of vertexes
00041     const unsigned NUMHEX = 8;   // The number of hexahedrons
00042 
00043     // This double array stores the x, y, z coordinate of each vertex.
00044     const double vertex_coords[3 * NUMVTX] = {
00045         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,
00046 
00047         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,
00048 
00049         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 };
00050 
00051     // Create the vertexes and store their entity handles in the
00052     // vertex_handles range. In MOAB, entities are defined using Entity
00053     // Handles (type moab::EntityHandle). An entity handle is a unique
00054     // integer that is used to identify/refer to specific entities (such
00055     // as vertexes, edges, hexahedrons, etc...) on the mesh. MOAB
00056     // guarantees that when multiple entities are created at the same
00057     // time, as in the create_vertices call below, that those entities
00058     // get adjacent handles. Thus, the second of the 27 vertexes we are
00059     // creating will have an entity handle that is 1 greater than the
00060     // handle of the first vertex. A range (type moab::Range) is a
00061     // container which stores entity handles. Of course, sets of handles
00062     // can also be stored in vectors or arrays, but ranges are much more
00063     // memory efficient, so use them when possible!
00064     moab::Range vertex_handles;
00065     rval = mbcore.create_vertices( vertex_coords, NUMVTX, vertex_handles );MB_CHK_SET_ERR( rval, "create_vertices failed" );
00066 
00067     // You can print out a range to see what elements it contains:
00068     std::cout << "Created 27 vertex entities:" << vertex_handles;
00069 
00070     // ******************************
00071     // *   Create the Hexahedrons   *
00072     // ******************************
00073 
00074     // The conn array stores the connectivity for each hex. It defines
00075     // which 8 vertexes connect to define a single hexahedron. The loop
00076     // below adds the handle of the first vertex (stored in
00077     // first_vertex_handle) to conn thereby ensuring that the entries of
00078     // conn are actual entity handles. This only works because MOAB
00079     // guarantees that entities (such as our vertexes) created at once
00080     // have adjacent entity handles.
00081     moab::EntityHandle conn[NUMHEX][8] = { { 0, 1, 4, 3, 9, 10, 13, 12 },      { 1, 2, 5, 4, 10, 11, 14, 13 },
00082                                            { 3, 4, 7, 6, 12, 13, 16, 15 },     { 4, 5, 8, 7, 13, 14, 17, 16 },
00083                                            { 9, 10, 13, 12, 18, 19, 22, 21 },  { 10, 11, 14, 13, 19, 20, 23, 22 },
00084                                            { 12, 13, 16, 15, 21, 22, 25, 24 }, { 13, 14, 17, 16, 22, 23, 26, 25 } };
00085 
00086     // Lets get the handle for the first vertex. Note that we can use
00087     // the square brackets operator on ranges just like vectors or
00088     // arrays:
00089     moab::EntityHandle first_vertex_handle = vertex_handles[0];
00090 
00091     for( unsigned i = 0; i < NUMHEX; ++i )
00092     {
00093         for( unsigned j = 0; j < 8; ++j )
00094         {
00095             // Add first_vertex_handle to each element of conn. This ensures
00096             // that the handles are specified properly (i.e. when
00097             // first_vertex_handle > 0)
00098             conn[i][j] = conn[i][j] + first_vertex_handle;
00099         }
00100     }
00101 
00102     // Now that the connectivity of each hex has been defined, we can
00103     // create each hex using a call to the create_element method which
00104     // gives back an entity handle for the hex that was created. We'll
00105     // then insert that entity into a range:
00106     moab::Range hexahedron_handles;
00107     moab::EntityHandle element;
00108     for( unsigned i = 0; i < NUMHEX; ++i )
00109     {
00110         rval = mbcore.create_element( moab::MBHEX, conn[i], 8, element );MB_CHK_SET_ERR( rval, "create_element failed" );
00111 
00112         hexahedron_handles.insert( element );
00113     }
00114 
00115     // Let's see what entities we just created:
00116     std::cout << "Created HEX8 entities: " << hexahedron_handles;
00117 
00118     // ***************************
00119     // *   Write Mesh to Files   *
00120     // ***************************
00121 
00122     // Now that we've created this amazing mesh, we can write it out to
00123     // a file. Since MOAB can write out to a variety of standard file
00124     // formats, you can quickly visualize and manipulate your mesh using
00125     // standard tools, such as VisIt.
00126 
00127     // In these examples, we will stick to using the VTK file format
00128     // because it is text based and will work whether or not you've got
00129     // HDF5, NETCDF, etc... installed and is a fairly standard file
00130     // format so a lot of tools work with it out of the box.
00131     rval = mbcore.write_file( "mbex1.vtk" );MB_CHK_SET_ERR( rval, "write_file(mbex1.vtk) failed" );
00132 
00133     return 0;
00134 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines