Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
mbex3.cpp
Go to the documentation of this file.
1 /// \file mbex3.cpp
2 ///
3 /// \author Milad Fatenejad
4 ///
5 /// \brief beginner tutorial, example 3: Demonstrates
6 /// constructing/saving a simple 2x2x2 hex mesh using the
7 /// structured mesh interface
8 ///
9 /// In this example, we create a 2x2x2 mesh that is identical to the
10 /// previous example. However, in this case we will use the structured
11 /// mesh interface since the mesh we created is logically
12 /// structured. There are many advantages to using the structured mesh
13 /// interface...such as memory savings, speed, ease-of-use...
14 ///
15 /// In the previous example, we had to create 27 vertexes manually,
16 /// define the connectivity, then manually create 8 hexahedrons. With
17 /// the structured mesh interface, we just have to create the 27
18 /// vertexes then tell MOAB that these define a 2x2x2 structured mesh
19 /// and everything else is taken care of for us!
20 
21 // The moab/Core.hpp header file is needed for all MOAB work...
22 #include "moab/Core.hpp"
23 
24 // The moab/ScdInterface.hpp contains code which defines the moab
25 // structured mesh interface
26 #include "moab/ScdInterface.hpp"
27 
28 #include <iostream>
29 
30 // ****************
31 // * *
32 // * main *
33 // * *
34 // ****************
35 int main()
36 {
37  moab::ErrorCode rval;
38  moab::Core mbint;
39 
40  // **********************************
41  // * Create the Structured Mesh *
42  // **********************************
43 
44  // As before, we have to create an array defining the coordinate of
45  // each vertex:
46  const unsigned NUMVTX = 27;
47  const double vertex_coords[3 * NUMVTX] = {
48  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,
49 
50  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,
51 
52  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 };
53 
54  // moab::ScdInterface is the structured mesh interface class for
55  // MOAB.
56  moab::ScdInterface* scdint;
57 
58  // The query_interface method will create a structured mesh instance
59  // for mbcore and will point scdint to it. This is how you tell moab
60  // that our moab::Core instance is going to represent a structured
61  // mesh.
62  rval = mbint.query_interface( scdint );MB_CHK_SET_ERR( rval, "mbint.query_interface failed" );
63 
64  // Structured meshes a divided into "boxes". Each box represents a
65  // little structured mesh. A single mesh, for example a
66  // block-structured mesh, can contain many individual boxes. In this
67  // example, we want to create a single, 2x2x2 box. The construct_box
68  // method will do this for us.
69  moab::ScdBox* scdbox = NULL;
70  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" );
71 
72  // moab::HomCoord is a little class that is used to represent a
73  // coordinate in logical space. Above, we told MOAB that we want
74  // indexes which extend from 0,0,0 to 2,2,2 for vertexes. Since the
75  // i,j,k start/end indexes are all different, MOAB knows that our
76  // mesh consists of hexes. If we had gone from 0,0,0 to 1,1,0 then
77  // MOAB would construct a 2x2x1 mesh of quadrilaterals.
78 
79  // ***************
80  // * Inspect *
81  // ***************
82 
83  // Our mesh now exists and contains a single box! Lets loop over
84  // that box and manually print out the handle of each vertex/hex:
85  unsigned i, j, k;
86 
87  // Print out the entity handle associated with each hex:
88  for( i = 0; i < 2; ++i )
89  for( j = 0; j < 2; ++j )
90  for( k = 0; k < 2; ++k )
91  {
92  std::cout << "Hex (" << i << "," << j << "," << k << ") "
93  << "has handle: " << scdbox->get_element( i, j, k ) << std::endl;
94  }
95 
96  // Print out the entity handle associated with each vertex:
97  for( i = 0; i < 3; ++i )
98  for( j = 0; j < 3; ++j )
99  for( k = 0; k < 3; ++k )
100  {
101  std::cout << "Vertex (" << i << "," << j << "," << k << ") "
102  << "has handle: " << scdbox->get_vertex( i, j, k ) << std::endl;
103  }
104 
105  // Of course, you can still use all of the functionality defined in
106  // mbint to get coordinates, connectivity, etc...
107 
108  // ***************************
109  // * Write Mesh to Files *
110  // ***************************
111  rval = mbint.write_file( "mbex3.vtk" );MB_CHK_SET_ERR( rval, "write_file(mbex3.vtk) failed" );
112 
113  return 0;
114 }