Mesh Oriented datABase  (version 5.4.1)
Array-based unstructured mesh datastructure
mbex3.cpp
Go to the documentation of this file.
00001 /// \file mbex3.cpp
00002 ///
00003 /// \author Milad Fatenejad
00004 ///
00005 /// \brief beginner tutorial, example 3: Demonstrates
00006 ///        constructing/saving a simple 2x2x2 hex mesh using the
00007 ///        structured mesh interface
00008 ///
00009 /// In this example, we create a 2x2x2 mesh that is identical to the
00010 /// previous example. However, in this case we will use the structured
00011 /// mesh interface since the mesh we created is logically
00012 /// structured. There are many advantages to using the structured mesh
00013 /// interface...such as memory savings, speed, ease-of-use...
00014 ///
00015 /// In the previous example, we had to create 27 vertexes manually,
00016 /// define the connectivity, then manually create 8 hexahedrons. With
00017 /// the structured mesh interface, we just have to create the 27
00018 /// vertexes then tell MOAB that these define a 2x2x2 structured mesh
00019 /// and everything else is taken care of for us!
00020 
00021 // The moab/Core.hpp header file is needed for all MOAB work...
00022 #include "moab/Core.hpp"
00023 
00024 // The moab/ScdInterface.hpp contains code which defines the moab
00025 // structured mesh interface
00026 #include "moab/ScdInterface.hpp"
00027 
00028 #include <iostream>
00029 
00030 // ****************
00031 // *              *
00032 // *     main     *
00033 // *              *
00034 // ****************
00035 int main()
00036 {
00037     moab::ErrorCode rval;
00038     moab::Core mbint;
00039 
00040     // **********************************
00041     // *   Create the Structured Mesh   *
00042     // **********************************
00043 
00044     // As before, we have to create an array defining the coordinate of
00045     // each vertex:
00046     const unsigned NUMVTX                  = 27;
00047     const double vertex_coords[3 * NUMVTX] = {
00048         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,
00049 
00050         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,
00051 
00052         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 };
00053 
00054     // moab::ScdInterface is the structured mesh interface class for
00055     // MOAB.
00056     moab::ScdInterface* scdint;
00057 
00058     // The query_interface method will create a structured mesh instance
00059     // for mbcore and will point scdint to it. This is how you tell moab
00060     // that our moab::Core instance is going to represent a structured
00061     // mesh.
00062     rval = mbint.query_interface( scdint );MB_CHK_SET_ERR( rval, "mbint.query_interface failed" );
00063 
00064     // Structured meshes a divided into "boxes". Each box represents a
00065     // little structured mesh. A single mesh, for example a
00066     // block-structured mesh, can contain many individual boxes. In this
00067     // example, we want to create a single, 2x2x2 box. The construct_box
00068     // method will do this for us.
00069     moab::ScdBox* scdbox = NULL;
00070     rval = scdint->construct_box( moab::HomCoord( 0, 0, 0 ), moab::HomCoord( 2, 2, 2 ), vertex_coords, NUMVTX, scdbox );MB_CHK_SET_ERR( rval, "scdint->construct_box failed" );
00071 
00072     // moab::HomCoord is a little class that is used to represent a
00073     // coordinate in logical space. Above, we told MOAB that we want
00074     // indexes which extend from 0,0,0 to 2,2,2 for vertexes. Since the
00075     // i,j,k start/end indexes are all different, MOAB knows that our
00076     // mesh consists of hexes. If we had gone from 0,0,0 to 1,1,0 then
00077     // MOAB would construct a 2x2x1 mesh of quadrilaterals.
00078 
00079     // ***************
00080     // *   Inspect   *
00081     // ***************
00082 
00083     // Our mesh now exists and contains a single box! Lets loop over
00084     // that box and manually print out the handle of each vertex/hex:
00085     unsigned i, j, k;
00086 
00087     // Print out the entity handle associated with each hex:
00088     for( i = 0; i < 2; ++i )
00089         for( j = 0; j < 2; ++j )
00090             for( k = 0; k < 2; ++k )
00091             {
00092                 std::cout << "Hex (" << i << "," << j << "," << k << ") "
00093                           << "has handle: " << scdbox->get_element( i, j, k ) << std::endl;
00094             }
00095 
00096     // Print out the entity handle associated with each vertex:
00097     for( i = 0; i < 3; ++i )
00098         for( j = 0; j < 3; ++j )
00099             for( k = 0; k < 3; ++k )
00100             {
00101                 std::cout << "Vertex (" << i << "," << j << "," << k << ") "
00102                           << "has handle: " << scdbox->get_vertex( i, j, k ) << std::endl;
00103             }
00104 
00105     // Of course, you can still use all of the functionality defined in
00106     // mbint to get coordinates, connectivity, etc...
00107 
00108     // ***************************
00109     // *   Write Mesh to Files   *
00110     // ***************************
00111     rval = mbint.write_file( "mbex3.vtk" );MB_CHK_SET_ERR( rval, "write_file(mbex3.vtk) failed" );
00112 
00113     return 0;
00114 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines