Actual source code: ex1.cxx

petsc-3.11.4 2019-09-28
Report Typos and Errors
  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: {
 18:   PetscBool      flg;

 21:   PetscStrcpy(options->filename, "");
 22:   PetscStrcpy(options->tagname, "petsc_tag");
 23:   options->dim = -1;

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

 31:   PetscLogEventRegister("CreateMesh",          DM_CLASSID,   &options->createMeshEvent);
 32:   return(0);
 33: }

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

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

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

 61:     moab::ScdBox *box;
 62:     merr = scdi->construct_box (moab::HomCoord(0,0,0), moab::HomCoord(5,5,5), NULL, 0, box);MBERRNM(merr);
 63:     user->dim = 3;
 64:     merr = iface->set_dimension(user->dim);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);MBERRNM(merr);
 70:     if (tmp_range.empty()) {
 71:       MBERRNM(moab::MB_FAILURE);
 72:     }
 73:     user->dim = iface->dimension_from_handle(*tmp_range.rbegin());
 74:   }
 75:   merr = iface->get_entities_by_dimension(0, user->dim, range);MBERRNM(merr);
 76:   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);MBERRNM(merr);
 81:   {
 82:       // initialize new tag with gids
 83:     std::vector<double> tag_vals(range.size());
 84:     merr = iface->tag_get_data(ltog_tag, range, tag_vals.data());MBERRNM(merr); // read them into ints
 85:     double *dval = tag_vals.data(); int *ival = reinterpret_cast<int*>(dval); // "stretch" them into doubles, from the end
 86:     for (int i = tag_vals.size()-1; i >= 0; i--) dval[i] = ival[i];
 87:     merr = iface->tag_set_data(tag, range, tag_vals.data());MBERRNM(merr); // write them into doubles
 88:   }
 89:   merr = iface->tag_get_length(tag, tagsize);MBERRNM(merr);

 91:   DMSetUp(*dm);

 93:     // create the dmmoab and initialize its data
 94:   PetscObjectSetName((PetscObject) *dm, "MOAB mesh");
 95:   PetscLogEventEnd(user->createMeshEvent,0,0,0,0);
 96:   user->dm = *dm;
 97:   return(0);
 98: }

100: int main(int argc, char **argv)
101: {
102:   AppCtx            user;                 /* user-defined work context */
103:   moab::ErrorCode   merr;
104:   PetscErrorCode    ierr;
105:   Vec               vec;
106:   moab::Interface*  mbImpl=NULL;
107:   moab::Tag         datatag=NULL;

109:   PetscInitialize(&argc, &argv, NULL,help);if (ierr) return ierr;
110:   ProcessOptions(PETSC_COMM_WORLD, &user);

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

114:   DMMoabGetInterface(user.dm, &mbImpl);
115:   merr = mbImpl->tag_get_handle(user.tagname, datatag);MBERRNM(merr);
116:   DMMoabCreateVector(user.dm, datatag, NULL, PETSC_TRUE, PETSC_FALSE,&vec); /* create a vec from user-input tag */

118:   std::cout << "Created VecMoab from existing tag." << std::endl;
119:   VecDestroy(&vec);
120:   std::cout << "Destroyed VecMoab." << std::endl;
121:   DMDestroy(&user.dm);
122:   std::cout << "Destroyed DMMoab." << std::endl;
123:   PetscFinalize();
124:   return ierr;
125: }

127: /*TEST

129:      build:
130:        requires: moab

132:      test:

134: TEST*/