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

beginner tutorial, example 1: Demonstrates constructing/saving a simple 2x2x2 hex mesh More...

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

Go to the source code of this file.

Functions

int main ()
 

Detailed Description

beginner tutorial, example 1: Demonstrates constructing/saving a simple 2x2x2 hex mesh

Author
Milad Fatenejad

This example creates a 2x2x2 mesh (uniform grid) and writes it out to a VTK file and an H5M file. Each cell is of size 1x1x1 and is axis aligned. This example demonstrates how to manually create a mesh using the unstructured mesh interface. The mesh is then written out to file.

Definition in file mbex1.cpp.

Function Documentation

◆ main()

int main ( )

Definition at line 25 of file mbex1.cpp.

26 {
27  moab::ErrorCode rval;
28  // MOAB functionality is accessed through an instance of the
29  // moab::Interface class:
30  moab::Core mbcore;
31 
32  // ***************************
33  // * Create the vertexes *
34  // ***************************
35 
36  // We are going to create 27 vertexes which will be used to define 8
37  // hexahedron cells. First, we have to create an array to store the
38  // coordinates of each vertex.
39 
40  const unsigned NUMVTX = 27; // The number of vertexes
41  const unsigned NUMHEX = 8; // The number of hexahedrons
42 
43  // This double array stores the x, y, z coordinate of each vertex.
44  const double vertex_coords[3 * NUMVTX] = {
45  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,
46 
47  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,
48 
49  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 };
50 
51  // Create the vertexes and store their entity handles in the
52  // vertex_handles range. In MOAB, entities are defined using Entity
53  // Handles (type moab::EntityHandle). An entity handle is a unique
54  // integer that is used to identify/refer to specific entities (such
55  // as vertexes, edges, hexahedrons, etc...) on the mesh. MOAB
56  // guarantees that when multiple entities are created at the same
57  // time, as in the create_vertices call below, that those entities
58  // get adjacent handles. Thus, the second of the 27 vertexes we are
59  // creating will have an entity handle that is 1 greater than the
60  // handle of the first vertex. A range (type moab::Range) is a
61  // container which stores entity handles. Of course, sets of handles
62  // can also be stored in vectors or arrays, but ranges are much more
63  // memory efficient, so use them when possible!
64  moab::Range vertex_handles;
65  rval = mbcore.create_vertices( vertex_coords, NUMVTX, vertex_handles );MB_CHK_SET_ERR( rval, "create_vertices failed" );
66 
67  // You can print out a range to see what elements it contains:
68  std::cout << "Created 27 vertex entities:" << vertex_handles;
69 
70  // ******************************
71  // * Create the Hexahedrons *
72  // ******************************
73 
74  // The conn array stores the connectivity for each hex. It defines
75  // which 8 vertexes connect to define a single hexahedron. The loop
76  // below adds the handle of the first vertex (stored in
77  // first_vertex_handle) to conn thereby ensuring that the entries of
78  // conn are actual entity handles. This only works because MOAB
79  // guarantees that entities (such as our vertexes) created at once
80  // have adjacent entity handles.
81  moab::EntityHandle conn[NUMHEX][8] = { { 0, 1, 4, 3, 9, 10, 13, 12 }, { 1, 2, 5, 4, 10, 11, 14, 13 },
82  { 3, 4, 7, 6, 12, 13, 16, 15 }, { 4, 5, 8, 7, 13, 14, 17, 16 },
83  { 9, 10, 13, 12, 18, 19, 22, 21 }, { 10, 11, 14, 13, 19, 20, 23, 22 },
84  { 12, 13, 16, 15, 21, 22, 25, 24 }, { 13, 14, 17, 16, 22, 23, 26, 25 } };
85 
86  // Lets get the handle for the first vertex. Note that we can use
87  // the square brackets operator on ranges just like vectors or
88  // arrays:
89  moab::EntityHandle first_vertex_handle = vertex_handles[0];
90 
91  for( unsigned i = 0; i < NUMHEX; ++i )
92  {
93  for( unsigned j = 0; j < 8; ++j )
94  {
95  // Add first_vertex_handle to each element of conn. This ensures
96  // that the handles are specified properly (i.e. when
97  // first_vertex_handle > 0)
98  conn[i][j] = conn[i][j] + first_vertex_handle;
99  }
100  }
101 
102  // Now that the connectivity of each hex has been defined, we can
103  // create each hex using a call to the create_element method which
104  // gives back an entity handle for the hex that was created. We'll
105  // then insert that entity into a range:
106  moab::Range hexahedron_handles;
107  moab::EntityHandle element;
108  for( unsigned i = 0; i < NUMHEX; ++i )
109  {
110  rval = mbcore.create_element( moab::MBHEX, conn[i], 8, element );MB_CHK_SET_ERR( rval, "create_element failed" );
111 
112  hexahedron_handles.insert( element );
113  }
114 
115  // Let's see what entities we just created:
116  std::cout << "Created HEX8 entities: " << hexahedron_handles;
117 
118  // ***************************
119  // * Write Mesh to Files *
120  // ***************************
121 
122  // Now that we've created this amazing mesh, we can write it out to
123  // a file. Since MOAB can write out to a variety of standard file
124  // formats, you can quickly visualize and manipulate your mesh using
125  // standard tools, such as VisIt.
126 
127  // In these examples, we will stick to using the VTK file format
128  // because it is text based and will work whether or not you've got
129  // HDF5, NETCDF, etc... installed and is a fairly standard file
130  // format so a lot of tools work with it out of the box.
131  rval = mbcore.write_file( "mbex1.vtk" );MB_CHK_SET_ERR( rval, "write_file(mbex1.vtk) failed" );
132 
133  return 0;
134 }

References moab::Core::create_element(), moab::Core::create_vertices(), ErrorCode, moab::Range::insert(), MB_CHK_SET_ERR, MBHEX, and moab::Core::write_file().