Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
mbex3.cpp File Reference

beginner tutorial, example 3: Demonstrates constructing/saving a simple 2x2x2 hex mesh using the structured mesh interface More...

#include "moab/Core.hpp"
#include "moab/ScdInterface.hpp"
#include <iostream>
+ Include dependency graph for mbex3.cpp:

Go to the source code of this file.

Functions

int main ()
 

Detailed Description

beginner tutorial, example 3: Demonstrates constructing/saving a simple 2x2x2 hex mesh using the structured mesh interface

Author
Milad Fatenejad

In this example, we create a 2x2x2 mesh that is identical to the previous example. However, in this case we will use the structured mesh interface since the mesh we created is logically structured. There are many advantages to using the structured mesh interface...such as memory savings, speed, ease-of-use...

In the previous example, we had to create 27 vertexes manually, define the connectivity, then manually create 8 hexahedrons. With the structured mesh interface, we just have to create the 27 vertexes then tell MOAB that these define a 2x2x2 structured mesh and everything else is taken care of for us!

Definition in file mbex3.cpp.

Function Documentation

◆ main()

int main ( )

Definition at line 35 of file mbex3.cpp.

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 }

References moab::ScdInterface::construct_box(), ErrorCode, moab::ScdBox::get_element(), moab::ScdBox::get_vertex(), MB_CHK_SET_ERR, moab::Interface::query_interface(), and moab::Core::write_file().