MOAB: Mesh Oriented datABase  (version 5.2.1)
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 
00055     // moab::ScdInterface is the structured mesh interface class for
00056     // MOAB.
00057     moab::ScdInterface* scdint;
00058 
00059     // The query_interface method will create a structured mesh instance
00060     // for mbcore and will point scdint to it. This is how you tell moab
00061     // that our moab::Core instance is going to represent a structured
00062     // mesh.
00063     rval = mbint.query_interface( scdint );MB_CHK_SET_ERR( rval, "mbint.query_interface failed" );
00064 
00065     // Structured meshes a divided into "boxes". Each box represents a
00066     // little structured mesh. A single mesh, for example a
00067     // block-structured mesh, can contain many individual boxes. In this
00068     // example, we want to create a single, 2x2x2 box. The construct_box
00069     // method will do this for us.
00070     moab::ScdBox* scdbox = NULL;
00071     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" );
00072 
00073     // moab::HomCoord is a little class that is used to represent a
00074     // coordinate in logical space. Above, we told MOAB that we want
00075     // indexes which extend from 0,0,0 to 2,2,2 for vertexes. Since the
00076     // i,j,k start/end indexes are all different, MOAB knows that our
00077     // mesh consists of hexes. If we had gone from 0,0,0 to 1,1,0 then
00078     // MOAB would construct a 2x2x1 mesh of quadrilaterals.
00079 
00080     // ***************
00081     // *   Inspect   *
00082     // ***************
00083 
00084     // Our mesh now exists and contains a single box! Lets loop over
00085     // that box and manually print out the handle of each vertex/hex:
00086     unsigned i, j, k;
00087 
00088     // Print out the entity handle associated with each hex:
00089     for( i = 0; i < 2; ++i )
00090         for( j = 0; j < 2; ++j )
00091             for( k = 0; k < 2; ++k )
00092             {
00093                 std::cout << "Hex (" << i << "," << j << "," << k << ") "
00094                           << "has handle: " << scdbox->get_element( i, j, k ) << std::endl;
00095             }
00096 
00097     // Print out the entity handle associated with each vertex:
00098     for( i = 0; i < 3; ++i )
00099         for( j = 0; j < 3; ++j )
00100             for( k = 0; k < 3; ++k )
00101             {
00102                 std::cout << "Vertex (" << i << "," << j << "," << k << ") "
00103                           << "has handle: " << scdbox->get_vertex( i, j, k ) << std::endl;
00104             }
00105 
00106     // Of course, you can still use all of the functionality defined in
00107     // mbint to get coordinates, connectivity, etc...
00108 
00109     // ***************************
00110     // *   Write Mesh to Files   *
00111     // ***************************
00112     rval = mbint.write_file( "mbex3.vtk" );MB_CHK_SET_ERR( rval, "write_file(mbex3.vtk) failed" );
00113 
00114     return 0;
00115 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines