MOAB: Mesh Oriented datABase  (version 5.2.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 
00052     // Create the vertexes and store their entity handles in the
00053     // vertex_handles range. In MOAB, entities are defined using Entity
00054     // Handles (type moab::EntityHandle). An entity handle is a unique
00055     // integer that is used to identify/refer to specific entities (such
00056     // as vertexes, edges, hexahedrons, etc...) on the mesh. MOAB
00057     // guarantees that when multiple entities are created at the same
00058     // time, as in the create_vertices call below, that those entities
00059     // get adjacent handles. Thus, the second of the 27 vertexes we are
00060     // creating will have an entity handle that is 1 greater than the
00061     // handle of the first vertex. A range (type moab::Range) is a
00062     // container which stores entity handles. Of course, sets of handles
00063     // can also be stored in vectors or arrays, but ranges are much more
00064     // memory efficient, so use them when possible!
00065     moab::Range vertex_handles;
00066     rval = mbcore.create_vertices( vertex_coords, NUMVTX, vertex_handles );MB_CHK_SET_ERR( rval, "create_vertices failed" );
00067 
00068     // You can print out a range to see what elements it contains:
00069     std::cout << "Created 27 vertex entities:" << vertex_handles;
00070 
00071     // ******************************
00072     // *   Create the Hexahedrons   *
00073     // ******************************
00074 
00075     // The conn array stores the connectivity for each hex. It defines
00076     // which 8 vertexes connect to define a single hexahedron. The loop
00077     // below adds the handle of the first vertex (stored in
00078     // first_vertex_handle) to conn thereby ensuring that the entries of
00079     // conn are actual entity handles. This only works because MOAB
00080     // guarantees that entities (such as our vertexes) created at once
00081     // have adjacent entity handles.
00082     moab::EntityHandle conn[NUMHEX][8] = { { 0, 1, 4, 3, 9, 10, 13, 12 },      { 1, 2, 5, 4, 10, 11, 14, 13 },
00083                                            { 3, 4, 7, 6, 12, 13, 16, 15 },     { 4, 5, 8, 7, 13, 14, 17, 16 },
00084                                            { 9, 10, 13, 12, 18, 19, 22, 21 },  { 10, 11, 14, 13, 19, 20, 23, 22 },
00085                                            { 12, 13, 16, 15, 21, 22, 25, 24 }, { 13, 14, 17, 16, 22, 23, 26, 25 } };
00086 
00087     // Lets get the handle for the first vertex. Note that we can use
00088     // the square brackets operator on ranges just like vectors or
00089     // arrays:
00090     moab::EntityHandle first_vertex_handle = vertex_handles[0];
00091 
00092     for( unsigned i = 0; i < NUMHEX; ++i )
00093     {
00094         for( unsigned j = 0; j < 8; ++j )
00095         {
00096             // Add first_vertex_handle to each element of conn. This ensures
00097             // that the handles are specified properly (i.e. when
00098             // first_vertex_handle > 0)
00099             conn[i][j] = conn[i][j] + first_vertex_handle;
00100         }
00101     }
00102 
00103     // Now that the connectivity of each hex has been defined, we can
00104     // create each hex using a call to the create_element method which
00105     // gives back an entity handle for the hex that was created. We'll
00106     // then insert that entity into a range:
00107     moab::Range hexahedron_handles;
00108     moab::EntityHandle element;
00109     for( unsigned i = 0; i < NUMHEX; ++i )
00110     {
00111         rval = mbcore.create_element( moab::MBHEX, conn[i], 8, element );MB_CHK_SET_ERR( rval, "create_element failed" );
00112 
00113         hexahedron_handles.insert( element );
00114     }
00115 
00116     // Let's see what entities we just created:
00117     std::cout << "Created HEX8 entities: " << hexahedron_handles;
00118 
00119     // ***************************
00120     // *   Write Mesh to Files   *
00121     // ***************************
00122 
00123     // Now that we've created this amazing mesh, we can write it out to
00124     // a file. Since MOAB can write out to a variety of standard file
00125     // formats, you can quickly visualize and manipulate your mesh using
00126     // standard tools, such as VisIt.
00127 
00128     // In these examples, we will stick to using the VTK file format
00129     // because it is text based and will work whether or not you've got
00130     // HDF5, NETCDF, etc... installed and is a fairly standard file
00131     // format so a lot of tools work with it out of the box.
00132     rval = mbcore.write_file( "mbex1.vtk" );MB_CHK_SET_ERR( rval, "write_file(mbex1.vtk) failed" );
00133 
00134     return 0;
00135 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines