Actual source code: ex1.cxx

  1: static char help[] = "Simple MOAB example\n\n";

  3: #include <petscdmmoab.h>
  4: #include "moab/ScdInterface.hpp"

  6: typedef struct {
  7:   DM            dm; /* DM implementation using the MOAB interface */
  8:   PetscLogEvent createMeshEvent;
  9:   /* Domain and mesh definition */
 10:   PetscInt dim;
 11:   char     filename[PETSC_MAX_PATH_LEN];
 12:   char     tagname[PETSC_MAX_PATH_LEN];
 13: } AppCtx;

 15: PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options)
 16: {
 17:   PetscBool flg;

 19:   PetscFunctionBegin;
 20:   PetscCall(PetscStrncpy(options->filename, "", sizeof(options->filename)));
 21:   PetscCall(PetscStrncpy(options->tagname, "petsc_tag", sizeof(options->tagname)));
 22:   options->dim = -1;

 24:   PetscOptionsBegin(comm, "", "MOAB example options", "DMMOAB");
 25:   PetscCall(PetscOptionsRangeInt("-dim", "The topological mesh dimension", "ex1.cxx", options->dim, &options->dim, NULL, PETSC_DECIDE, 3));
 26:   PetscCall(PetscOptionsString("-filename", "The file containing the mesh", "ex1.cxx", options->filename, options->filename, sizeof(options->filename), NULL));
 27:   PetscCall(PetscOptionsString("-tagname", "The tag name from which to create a vector", "ex1.cxx", options->tagname, options->tagname, sizeof(options->tagname), &flg));
 28:   PetscOptionsEnd();

 30:   PetscCall(PetscLogEventRegister("CreateMesh", DM_CLASSID, &options->createMeshEvent));
 31:   PetscFunctionReturn(PETSC_SUCCESS);
 32: }

 34: PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *user, DM *dm)
 35: {
 36:   moab::Interface *iface    = NULL;
 37:   moab::Tag        tag      = NULL;
 38:   moab::Tag        ltog_tag = NULL;
 39:   moab::Range      range;
 40:   PetscInt         tagsize;
 41:   moab::ErrorCode  merr;

 43:   PetscFunctionBegin;
 44:   PetscCall(PetscLogEventBegin(user->createMeshEvent, 0, 0, 0, 0));
 45:   PetscCall(DMMoabCreateMoab(comm, iface, &ltog_tag, &range, dm));
 46:   std::cout << "Created DMMoab using DMMoabCreateMoab." << std::endl;
 47:   PetscCall(DMMoabGetInterface(*dm, &iface));

 49:   // load file and get entities of requested or max dimension
 50:   if (strlen(user->filename) > 0) {
 51:     merr = iface->load_file(user->filename);
 52:     MBERRNM(merr);
 53:     std::cout << "Read mesh from file " << user->filename << std::endl;
 54:   } else {
 55:     // make a simple structured mesh
 56:     moab::ScdInterface *scdi;
 57:     merr = iface->query_interface(scdi);

 59:     moab::ScdBox *box;
 60:     merr = scdi->construct_box(moab::HomCoord(0, 0, 0), moab::HomCoord(5, 5, 5), NULL, 0, box);
 61:     MBERRNM(merr);
 62:     user->dim = 3;
 63:     merr      = iface->set_dimension(user->dim);
 64:     MBERRNM(merr);
 65:     std::cout << "Created structured 5x5x5 mesh." << std::endl;
 66:   }
 67:   if (-1 == user->dim) {
 68:     moab::Range tmp_range;
 69:     merr = iface->get_entities_by_handle(0, tmp_range);
 70:     MBERRNM(merr);
 71:     if (tmp_range.empty()) MBERRNM(moab::MB_FAILURE);
 72:     user->dim = iface->dimension_from_handle(*tmp_range.rbegin());
 73:   }
 74:   merr = iface->get_entities_by_dimension(0, user->dim, range);
 75:   MBERRNM(merr);
 76:   PetscCall(DMMoabSetLocalVertices(*dm, &range));

 78:   // get the requested tag and create if necessary
 79:   std::cout << "Creating tag with name: " << user->tagname << ";\n";
 80:   merr = iface->tag_get_handle(user->tagname, 1, moab::MB_TYPE_DOUBLE, tag, moab::MB_TAG_CREAT | moab::MB_TAG_DENSE);
 81:   MBERRNM(merr);
 82:   {
 83:     // initialize new tag with gids
 84:     std::vector<double> tag_vals(range.size());
 85:     merr = iface->tag_get_data(ltog_tag, range, tag_vals.data());
 86:     MBERRNM(merr); // read them into ints
 87:     double *dval = tag_vals.data();
 88:     int    *ival = reinterpret_cast<int *>(dval); // "stretch" them into doubles, from the end
 89:     for (int i = tag_vals.size() - 1; i >= 0; i--) dval[i] = ival[i];
 90:     merr = iface->tag_set_data(tag, range, tag_vals.data());
 91:     MBERRNM(merr); // write them into doubles
 92:   }
 93:   merr = iface->tag_get_length(tag, tagsize);
 94:   MBERRNM(merr);

 96:   PetscCall(DMSetUp(*dm));

 98:   // create the dmmoab and initialize its data
 99:   PetscCall(PetscObjectSetName((PetscObject)*dm, "MOAB mesh"));
100:   PetscCall(PetscLogEventEnd(user->createMeshEvent, 0, 0, 0, 0));
101:   user->dm = *dm;
102:   PetscFunctionReturn(PETSC_SUCCESS);
103: }

105: int main(int argc, char **argv)
106: {
107:   AppCtx           user; /* user-defined work context */
108:   moab::ErrorCode  merr;
109:   Vec              vec;
110:   moab::Interface *mbImpl  = NULL;
111:   moab::Tag        datatag = NULL;

113:   PetscFunctionBeginUser;
114:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
115:   PetscCall(ProcessOptions(PETSC_COMM_WORLD, &user));

117:   PetscCall(CreateMesh(PETSC_COMM_WORLD, &user, &user.dm)); /* create the MOAB dm and the mesh */

119:   PetscCall(DMMoabGetInterface(user.dm, &mbImpl));
120:   merr = mbImpl->tag_get_handle(user.tagname, datatag);
121:   MBERRNM(merr);
122:   PetscCall(DMMoabCreateVector(user.dm, datatag, NULL, PETSC_TRUE, PETSC_FALSE, &vec)); /* create a vec from user-input tag */

124:   std::cout << "Created VecMoab from existing tag." << std::endl;
125:   PetscCall(VecDestroy(&vec));
126:   std::cout << "Destroyed VecMoab." << std::endl;
127:   PetscCall(DMDestroy(&user.dm));
128:   std::cout << "Destroyed DMMoab." << std::endl;
129:   PetscCall(PetscFinalize());
130:   return 0;
131: }

133: /*TEST

135:      build:
136:        requires: moab !complex

138:      test:

140: TEST*/