1 static char help[] = "Create a box mesh with DMMoab and test defining a tag on the mesh\n\n"; 2 3 #include <petscdmmoab.h> 4 5 typedef struct { 6 DM dm; /* DM implementation using the MOAB interface */ 7 PetscBool debug; /* The debugging level */ 8 PetscLogEvent createMeshEvent; 9 /* Domain and mesh definition */ 10 PetscInt dim; /* The topological mesh dimension */ 11 PetscInt nele; /* Elements in each dimension */ 12 PetscBool simplex; /* Use simplex elements */ 13 PetscBool interlace; 14 char input_file[PETSC_MAX_PATH_LEN]; /* Import mesh from file */ 15 char output_file[PETSC_MAX_PATH_LEN]; /* Output mesh file name */ 16 PetscBool write_output; /* Write output mesh and data to file */ 17 PetscInt nfields; /* Number of fields */ 18 char *fieldnames[PETSC_MAX_PATH_LEN]; /* Name of a defined field on the mesh */ 19 } AppCtx; 20 21 PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options) 22 { 23 PetscErrorCode ierr; 24 PetscBool flg; 25 26 PetscFunctionBegin; 27 options->debug = PETSC_FALSE; 28 options->dim = 2; 29 options->nele = 5; 30 options->nfields = 256; 31 options->simplex = PETSC_FALSE; 32 options->write_output = PETSC_FALSE; 33 options->interlace = PETSC_FALSE; 34 options->input_file[0] = '\0'; 35 PetscCall(PetscStrcpy(options->output_file,"ex2.h5m")); 36 37 ierr = PetscOptionsBegin(comm, "", "Meshing Problem Options", "DMMOAB");PetscCall(ierr); 38 PetscCall(PetscOptionsBool("-debug", "Enable debug messages", "ex2.cxx", options->debug, &options->debug, NULL)); 39 PetscCall(PetscOptionsBool("-interlace", "Use interlaced arrangement for the field data", "ex2.cxx", options->interlace, &options->interlace, NULL)); 40 PetscCall(PetscOptionsBool("-simplex", "Create simplices instead of tensor product elements", "ex2.cxx", options->simplex, &options->simplex, NULL)); 41 PetscCall(PetscOptionsRangeInt("-dim", "The topological mesh dimension", "ex2.cxx", options->dim, &options->dim, NULL,1,3)); 42 PetscCall(PetscOptionsBoundedInt("-n", "The number of elements in each dimension", "ex2.cxx", options->nele, &options->nele, NULL,1)); 43 PetscCall(PetscOptionsString("-meshfile", "The input mesh file", "ex2.cxx", options->input_file, options->input_file, sizeof(options->input_file), NULL)); 44 PetscCall(PetscOptionsString("-io", "Write out the mesh and solution that is defined on it (Default H5M format)", "ex2.cxx", options->output_file, options->output_file, sizeof(options->output_file), &options->write_output)); 45 PetscCall(PetscOptionsStringArray("-fields", "The list of names of the field variables", "ex2.cxx", options->fieldnames,&options->nfields, &flg)); 46 ierr = PetscOptionsEnd();PetscCall(ierr); 47 48 if (options->debug) PetscCall(PetscPrintf(comm, "Total number of fields: %D.\n",options->nfields)); 49 if (!flg) { /* if no field names were given by user, assign a default */ 50 options->nfields = 1; 51 PetscCall(PetscStrallocpy("TestEX2Var",&options->fieldnames[0])); 52 } 53 54 PetscCall(PetscLogEventRegister("CreateMesh", DM_CLASSID, &options->createMeshEvent)); 55 PetscFunctionReturn(0); 56 } 57 58 PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *user) 59 { 60 PetscInt i; 61 size_t len; 62 PetscMPIInt rank; 63 64 PetscFunctionBegin; 65 PetscCall(PetscLogEventBegin(user->createMeshEvent,0,0,0,0)); 66 PetscCallMPI(MPI_Comm_rank(comm, &rank)); 67 PetscCall(PetscStrlen(user->input_file, &len)); 68 if (len) { 69 if (user->debug) PetscCall(PetscPrintf(comm, "Loading mesh from file: %s and creating a DM object.\n",user->input_file)); 70 PetscCall(DMMoabLoadFromFile(comm, user->dim, 1, user->input_file, "", &user->dm)); 71 } 72 else { 73 if (user->debug) { 74 PetscCall(PetscPrintf(comm, "Creating a %D-dimensional structured %s mesh of %Dx%Dx%D in memory and creating a DM object.\n",user->dim,(user->simplex?"simplex":"regular"),user->nele,user->nele,user->nele)); 75 } 76 PetscCall(DMMoabCreateBoxMesh(comm, user->dim, user->simplex, NULL, user->nele, 1, &user->dm)); 77 } 78 79 if (user->debug) { 80 PetscPrintf(comm, "Setting field names to DM: \n"); 81 for (i=0; i<user->nfields; i++) 82 PetscPrintf(comm, "\t Field{%D} = %s.\n",i,user->fieldnames[i]); 83 } 84 PetscCall(DMMoabSetFieldNames(user->dm, user->nfields, (const char**)user->fieldnames)); 85 PetscCall(PetscObjectSetName((PetscObject)user->dm, "Structured Mesh")); 86 PetscCall(PetscLogEventEnd(user->createMeshEvent,0,0,0,0)); 87 PetscFunctionReturn(0); 88 } 89 90 int main(int argc, char **argv) 91 { 92 AppCtx user; /* user-defined work context */ 93 PetscRandom rctx; 94 Vec solution; 95 Mat system; 96 MPI_Comm comm; 97 PetscInt i; 98 99 PetscCall(PetscInitialize(&argc, &argv, NULL, help)); 100 comm = PETSC_COMM_WORLD; 101 PetscCall(ProcessOptions(comm, &user)); 102 PetscCall(CreateMesh(comm, &user)); 103 104 /* set block size */ 105 PetscCall(DMMoabSetBlockSize(user.dm, (user.interlace?user.nfields:1))); 106 PetscCall(DMSetMatType(user.dm,MATAIJ)); 107 108 PetscCall(DMSetFromOptions(user.dm)); 109 110 /* SetUp the data structures for DMMOAB */ 111 PetscCall(DMSetUp(user.dm)); 112 113 if (user.debug) PetscCall(PetscPrintf(comm, "Creating a global vector defined on DM and setting random data.\n")); 114 PetscCall(DMCreateGlobalVector(user.dm,&solution)); 115 PetscCall(PetscRandomCreate(comm,&rctx)); 116 PetscCall(VecSetRandom(solution,rctx)); 117 118 /* test if matrix allocation for the prescribed matrix type is done correctly */ 119 if (user.debug) PetscCall(PetscPrintf(comm, "Creating a global matrix defined on DM with the right block structure.\n")); 120 PetscCall(DMCreateMatrix(user.dm,&system)); 121 122 if (user.write_output) { 123 PetscCall(DMMoabSetGlobalFieldVector(user.dm, solution)); 124 if (user.debug) PetscPrintf(comm, "Output mesh and associated field data to file: %s.\n",user.output_file); 125 PetscCall(DMMoabOutput(user.dm,(const char*)user.output_file,"")); 126 } 127 128 if (user.nfields) { 129 for (i=0; i<user.nfields; i++) PetscCall(PetscFree(user.fieldnames[i])); 130 } 131 PetscCall(PetscRandomDestroy(&rctx)); 132 PetscCall(VecDestroy(&solution)); 133 PetscCall(MatDestroy(&system)); 134 PetscCall(DMDestroy(&user.dm)); 135 PetscCall(PetscFinalize()); 136 return 0; 137 } 138 139 /*TEST 140 141 build: 142 requires: moab 143 144 test: 145 args: -debug -fields v1,v2,v3 146 147 TEST*/ 148