moab
StructuredMeshSimple.cpp
Go to the documentation of this file.
00001 
00024 #include "moab/Core.hpp"
00025 #include "moab/ScdInterface.hpp"
00026 #include "moab/ProgOptions.hpp"
00027 #include "moab/CN.hpp"
00028 #ifdef USE_MPI
00029 #include "moab_mpi.h"
00030 #endif
00031 #include <iostream>
00032 #include <vector>
00033 
00034 using namespace moab;
00035 
00036 int main(int argc, char **argv) 
00037 {
00038   int N = 10, dim = 3;
00039 
00040 #ifdef USE_MPI
00041   MPI_Init(&argc, &argv);
00042 #endif
00043 
00044   ProgOptions opts;
00045   opts.addOpt<int>(std::string("dim,d"), std::string("Dimension of mesh (default=3)"),
00046                    &dim);
00047   opts.addOpt<int>(std::string(",n"), std::string("Number of elements on a side (default=10)"),
00048                    &N);
00049   opts.parseCommandLine(argc, argv);
00050   
00051     // 0. Instantiate MOAB and get the structured mesh interface
00052   Interface *mb = new Core();
00053   ScdInterface *scdiface;
00054   ErrorCode rval = mb->query_interface(scdiface); // get a ScdInterface object through moab instance
00055   if (MB_SUCCESS != rval) return rval;
00056 
00057     // 1. Decide what the local parameters of the mesh will be, based on parallel/serial and rank.
00058   int ilow = 0, ihigh = N;
00059   int rank = 0, nprocs = 1;
00060 #ifdef USE_MPI
00061   MPI_Comm_size(MPI_COMM_WORLD, &nprocs); MPI_Comm_rank(MPI_COMM_WORLD, &rank);
00062   ilow = rank*N; ihigh = ilow + N;
00063 #endif  
00064 
00065     // 2. Create a N^d structured mesh, which includes (N+1)^d vertices and N^d elements.
00066   ScdBox *box;
00067   rval = scdiface->construct_box(HomCoord(ilow, (dim>1?0:-1), (dim>2?0:-1)), // use in-line logical tests to handle dimensionality
00068                                  HomCoord(ihigh, (dim>1?N:-1), (dim>2?N:-1)), 
00069                                  NULL, 0, // NULL coords vector and 0 coords (don't specify coords for now)
00070                                  box);    // box is the structured box object providing the parametric
00071                                           // structured mesh interface for this rectangle of elements
00072   if (MB_SUCCESS != rval) return rval;
00073 
00074     // 3. Get the vertices and elements from moab and check their numbers against (N+1)^d and N^d, resp.
00075   Range verts, elems;
00076   rval = mb->get_entities_by_dimension(0, 0, verts); // first '0' specifies "root set", or entire MOAB instance, second the entity dimension being requested
00077   if (MB_SUCCESS != rval) return rval;
00078   rval = mb->get_entities_by_dimension(0, dim, elems);
00079   if (MB_SUCCESS != rval) return rval;
00080 
00081 #define MYSTREAM(a) if (!rank) std::cout << a << std::endl
00082   
00083   if (pow(N,dim) == (int) elems.size() && pow(N+1,dim) == (int) verts.size()) { // expected #e and #v are N^d and (N+1)^d, resp.
00084 #ifdef USE_MPI
00085     MYSTREAM("Proc 0: ");
00086 #endif
00087     MYSTREAM("Created " << elems.size() << " " << CN::EntityTypeName(mb->type_from_handle(*elems.begin())) 
00088              << " elements and " << verts.size() << " vertices." << std::endl);
00089   }
00090   else
00091     std::cout << "Created the wrong number of vertices or hexes!" << std::endl;
00092   
00093     // 4. Loop over elements in 3 nested loops over i, j, k; for each (i,j,k):
00094   std::vector<double> coords(3*pow(N+1,dim));
00095   std::vector<EntityHandle> connect;
00096   for (int k = 0; k < (dim>2?N:1); k++) {
00097     for (int j = 0; j < (dim>1?N:1); j++) {
00098       for (int i = 0; i < N-1; i++) {
00099           // 4a. Get the element corresponding to (i,j,k)
00100         EntityHandle ehandle = box->get_element(i, j, k);
00101         if (0 == ehandle) return MB_FAILURE;
00102           // 4b. Get the connectivity of the element
00103         rval = mb->get_connectivity(&ehandle, 1, connect); // get the connectivity, in canonical order
00104         if (MB_SUCCESS != rval) return rval;
00105           // 4c. Get the coordinates of the vertices comprising that element
00106         rval = mb->get_coords(connect.data(), connect.size(), coords.data()); // get the coordinates of those vertices
00107         if (MB_SUCCESS != rval) return rval;
00108       }
00109     }
00110   }
00111 
00112     // 5. Release the structured mesh interface and destroy the MOAB instance
00113   mb->release_interface(scdiface); // tell MOAB we're done with the ScdInterface
00114   delete mb;
00115 
00116 #ifdef USE_MPI
00117   MPI_Finalize();
00118 #endif
00119 
00120   return 0;
00121 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines