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 CHKERRQ(PetscStrcpy(options->output_file,"ex2.h5m")); 36 37 ierr = PetscOptionsBegin(comm, "", "Meshing Problem Options", "DMMOAB");CHKERRQ(ierr); 38 CHKERRQ(PetscOptionsBool("-debug", "Enable debug messages", "ex2.cxx", options->debug, &options->debug, NULL)); 39 CHKERRQ(PetscOptionsBool("-interlace", "Use interlaced arrangement for the field data", "ex2.cxx", options->interlace, &options->interlace, NULL)); 40 CHKERRQ(PetscOptionsBool("-simplex", "Create simplices instead of tensor product elements", "ex2.cxx", options->simplex, &options->simplex, NULL)); 41 CHKERRQ(PetscOptionsRangeInt("-dim", "The topological mesh dimension", "ex2.cxx", options->dim, &options->dim, NULL,1,3)); 42 CHKERRQ(PetscOptionsBoundedInt("-n", "The number of elements in each dimension", "ex2.cxx", options->nele, &options->nele, NULL,1)); 43 CHKERRQ(PetscOptionsString("-meshfile", "The input mesh file", "ex2.cxx", options->input_file, options->input_file, sizeof(options->input_file), NULL)); 44 CHKERRQ(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 CHKERRQ(PetscOptionsStringArray("-fields", "The list of names of the field variables", "ex2.cxx", options->fieldnames,&options->nfields, &flg)); 46 ierr = PetscOptionsEnd();CHKERRQ(ierr); 47 48 if (options->debug) CHKERRQ(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 CHKERRQ(PetscStrallocpy("TestEX2Var",&options->fieldnames[0])); 52 } 53 54 CHKERRQ(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 CHKERRQ(PetscLogEventBegin(user->createMeshEvent,0,0,0,0)); 66 CHKERRMPI(MPI_Comm_rank(comm, &rank)); 67 CHKERRQ(PetscStrlen(user->input_file, &len)); 68 if (len) { 69 if (user->debug) CHKERRQ(PetscPrintf(comm, "Loading mesh from file: %s and creating a DM object.\n",user->input_file)); 70 CHKERRQ(DMMoabLoadFromFile(comm, user->dim, 1, user->input_file, "", &user->dm)); 71 } 72 else { 73 if (user->debug) { 74 CHKERRQ(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 CHKERRQ(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 CHKERRQ(DMMoabSetFieldNames(user->dm, user->nfields, (const char**)user->fieldnames)); 85 CHKERRQ(PetscObjectSetName((PetscObject)user->dm, "Structured Mesh")); 86 CHKERRQ(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 PetscErrorCode ierr; 99 100 ierr = PetscInitialize(&argc, &argv, NULL, help);if (ierr) return ierr; 101 comm = PETSC_COMM_WORLD; 102 CHKERRQ(ProcessOptions(comm, &user)); 103 CHKERRQ(CreateMesh(comm, &user)); 104 105 /* set block size */ 106 CHKERRQ(DMMoabSetBlockSize(user.dm, (user.interlace?user.nfields:1))); 107 CHKERRQ(DMSetMatType(user.dm,MATAIJ)); 108 109 CHKERRQ(DMSetFromOptions(user.dm)); 110 111 /* SetUp the data structures for DMMOAB */ 112 CHKERRQ(DMSetUp(user.dm)); 113 114 if (user.debug) CHKERRQ(PetscPrintf(comm, "Creating a global vector defined on DM and setting random data.\n")); 115 CHKERRQ(DMCreateGlobalVector(user.dm,&solution)); 116 CHKERRQ(PetscRandomCreate(comm,&rctx)); 117 CHKERRQ(VecSetRandom(solution,rctx)); 118 119 /* test if matrix allocation for the prescribed matrix type is done correctly */ 120 if (user.debug) CHKERRQ(PetscPrintf(comm, "Creating a global matrix defined on DM with the right block structure.\n")); 121 CHKERRQ(DMCreateMatrix(user.dm,&system)); 122 123 if (user.write_output) { 124 CHKERRQ(DMMoabSetGlobalFieldVector(user.dm, solution)); 125 if (user.debug) PetscPrintf(comm, "Output mesh and associated field data to file: %s.\n",user.output_file); 126 CHKERRQ(DMMoabOutput(user.dm,(const char*)user.output_file,"")); 127 } 128 129 if (user.nfields) { 130 for (i=0; i<user.nfields; i++) CHKERRQ(PetscFree(user.fieldnames[i])); 131 } 132 CHKERRQ(PetscRandomDestroy(&rctx)); 133 CHKERRQ(VecDestroy(&solution)); 134 CHKERRQ(MatDestroy(&system)); 135 CHKERRQ(DMDestroy(&user.dm)); 136 ierr = PetscFinalize(); 137 return ierr; 138 } 139 140 /*TEST 141 142 build: 143 requires: moab 144 145 test: 146 args: -debug -fields v1,v2,v3 147 148 TEST*/ 149