1 static char help[] = "Simple MOAB example\n\n"; 2 3 #include <petscdmmoab.h> 4 #include "moab/ScdInterface.hpp" 5 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; 14 15 PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options) 16 { 17 PetscErrorCode ierr; 18 PetscBool flg; 19 20 PetscFunctionBegin; 21 PetscCall(PetscStrcpy(options->filename, "")); 22 PetscCall(PetscStrcpy(options->tagname, "petsc_tag")); 23 options->dim = -1; 24 25 ierr = PetscOptionsBegin(comm, "", "MOAB example options", "DMMOAB");PetscCall(ierr); 26 PetscCall(PetscOptionsRangeInt("-dim", "The topological mesh dimension", "ex1.cxx", options->dim, &options->dim, NULL,PETSC_DECIDE,3)); 27 PetscCall(PetscOptionsString("-filename", "The file containing the mesh", "ex1.cxx", options->filename, options->filename, sizeof(options->filename), NULL)); 28 PetscCall(PetscOptionsString("-tagname", "The tag name from which to create a vector", "ex1.cxx", options->tagname, options->tagname, sizeof(options->tagname), &flg)); 29 ierr = PetscOptionsEnd();PetscCall(ierr); 30 31 PetscCall(PetscLogEventRegister("CreateMesh", DM_CLASSID, &options->createMeshEvent)); 32 PetscFunctionReturn(0); 33 } 34 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; 43 44 PetscFunctionBegin; 45 PetscCall(PetscLogEventBegin(user->createMeshEvent,0,0,0,0)); 46 PetscCall(DMMoabCreateMoab(comm, iface, <og_tag, &range, dm)); 47 std::cout << "Created DMMoab using DMMoabCreateMoab." << std::endl; 48 PetscCall(DMMoabGetInterface(*dm, &iface)); 49 50 // load file and get entities of requested or max dimension 51 if (strlen(user->filename) > 0) { 52 merr = iface->load_file(user->filename);MBERRNM(merr); 53 std::cout << "Read mesh from file " << user->filename << std::endl; 54 } 55 else { 56 // make a simple structured mesh 57 moab::ScdInterface *scdi; 58 merr = iface->query_interface(scdi); 59 60 moab::ScdBox *box; 61 merr = scdi->construct_box (moab::HomCoord(0,0,0), moab::HomCoord(5,5,5), NULL, 0, box);MBERRNM(merr); 62 user->dim = 3; 63 merr = iface->set_dimension(user->dim);MBERRNM(merr); 64 std::cout << "Created structured 5x5x5 mesh." << std::endl; 65 } 66 if (-1 == user->dim) { 67 moab::Range tmp_range; 68 merr = iface->get_entities_by_handle(0, tmp_range);MBERRNM(merr); 69 if (tmp_range.empty()) { 70 MBERRNM(moab::MB_FAILURE); 71 } 72 user->dim = iface->dimension_from_handle(*tmp_range.rbegin()); 73 } 74 merr = iface->get_entities_by_dimension(0, user->dim, range);MBERRNM(merr); 75 PetscCall(DMMoabSetLocalVertices(*dm, &range)); 76 77 // get the requested tag and create if necessary 78 std::cout << "Creating tag with name: " << user->tagname << ";\n"; 79 merr = iface->tag_get_handle(user->tagname, 1, moab::MB_TYPE_DOUBLE, tag, moab::MB_TAG_CREAT | moab::MB_TAG_DENSE);MBERRNM(merr); 80 { 81 // initialize new tag with gids 82 std::vector<double> tag_vals(range.size()); 83 merr = iface->tag_get_data(ltog_tag, range, tag_vals.data());MBERRNM(merr); // read them into ints 84 double *dval = tag_vals.data(); int *ival = reinterpret_cast<int*>(dval); // "stretch" them into doubles, from the end 85 for (int i = tag_vals.size()-1; i >= 0; i--) dval[i] = ival[i]; 86 merr = iface->tag_set_data(tag, range, tag_vals.data());MBERRNM(merr); // write them into doubles 87 } 88 merr = iface->tag_get_length(tag, tagsize);MBERRNM(merr); 89 90 PetscCall(DMSetUp(*dm)); 91 92 // create the dmmoab and initialize its data 93 PetscCall(PetscObjectSetName((PetscObject) *dm, "MOAB mesh")); 94 PetscCall(PetscLogEventEnd(user->createMeshEvent,0,0,0,0)); 95 user->dm = *dm; 96 PetscFunctionReturn(0); 97 } 98 99 int main(int argc, char **argv) 100 { 101 AppCtx user; /* user-defined work context */ 102 moab::ErrorCode merr; 103 Vec vec; 104 moab::Interface* mbImpl=NULL; 105 moab::Tag datatag=NULL; 106 107 PetscCall(PetscInitialize(&argc, &argv, NULL,help)); 108 PetscCall(ProcessOptions(PETSC_COMM_WORLD, &user)); 109 110 PetscCall(CreateMesh(PETSC_COMM_WORLD, &user, &user.dm)); /* create the MOAB dm and the mesh */ 111 112 PetscCall(DMMoabGetInterface(user.dm, &mbImpl)); 113 merr = mbImpl->tag_get_handle(user.tagname, datatag);MBERRNM(merr); 114 PetscCall(DMMoabCreateVector(user.dm, datatag, NULL, PETSC_TRUE, PETSC_FALSE,&vec)); /* create a vec from user-input tag */ 115 116 std::cout << "Created VecMoab from existing tag." << std::endl; 117 PetscCall(VecDestroy(&vec)); 118 std::cout << "Destroyed VecMoab." << std::endl; 119 PetscCall(DMDestroy(&user.dm)); 120 std::cout << "Destroyed DMMoab." << std::endl; 121 PetscCall(PetscFinalize()); 122 return 0; 123 } 124 125 /*TEST 126 127 build: 128 requires: moab 129 130 test: 131 132 TEST*/ 133