Actual source code: ex2.cxx
petsc-3.7.3 2016-08-01
1: static char help[] = "Create a box mesh with DMMoab and test defining a tag on the mesh\n\n";
3: #include <petscdmmoab.h>
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;
23: PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options)
24: {
26: PetscBool flg;
29: options->debug = PETSC_FALSE;
30: options->dim = 2;
31: options->nele = 5;
32: options->nfields = 256;
33: options->simplex = PETSC_FALSE;
34: options->write_output = PETSC_FALSE;
35: options->interlace = PETSC_FALSE;
36: options->input_file[0] = '\0';
37: PetscStrcpy(options->output_file,"ex2.h5m");
39: PetscOptionsBegin(comm, "", "Meshing Problem Options", "DMMOAB");
40: PetscOptionsBool("-debug", "Enable debug messages", "ex2.c", options->debug, &options->debug, NULL);
41: PetscOptionsBool("-interlace", "Use interlaced arrangement for the field data", "ex2.c", options->interlace, &options->interlace, NULL);
42: PetscOptionsBool("-simplex", "Create simplices instead of tensor product elements", "ex2.c", options->simplex, &options->simplex, NULL);
43: PetscOptionsInt("-dim", "The topological mesh dimension", "ex2.c", options->dim, &options->dim, NULL);
44: PetscOptionsInt("-n", "The number of elements in each dimension", "ex2.c", options->nele, &options->nele, NULL);
45: PetscOptionsString("-meshfile", "The input mesh file", "ex2.c", options->input_file, options->input_file, PETSC_MAX_PATH_LEN, NULL);
46: PetscOptionsString("-out", "Write out the mesh and solution that is defined on it (Default H5M format)", "ex2.c", options->output_file, options->output_file, PETSC_MAX_PATH_LEN, &options->write_output);
47: PetscOptionsStringArray("-fields", "The list of names of the field variables", "ex2.c", options->fieldnames,&options->nfields, &flg);
48: PetscOptionsEnd();
50: if (options->debug) PetscPrintf(comm, "Total number of fields: %D.\n",options->nfields);
51: if (!flg) { /* if no field names were given by user, assign a default */
52: options->nfields = 1;
53: PetscStrallocpy("TestEX2Var",&options->fieldnames[0]);
54: }
56: PetscLogEventRegister("CreateMesh", DM_CLASSID, &options->createMeshEvent);
57: return(0);
58: }
62: PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *user)
63: {
64: PetscInt i;
65: size_t len;
66: PetscMPIInt rank;
70: PetscLogEventBegin(user->createMeshEvent,0,0,0,0);
71: MPI_Comm_rank(comm, &rank);
72: PetscStrlen(user->input_file, &len);
73: if (len) {
74: if (user->debug) PetscPrintf(comm, "Loading mesh from file: %s and creating a DM object.\n",user->input_file);
75: DMMoabLoadFromFile(comm, user->dim, user->input_file, "", &user->dm);
76: }
77: else {
78: if (user->debug) {
79: 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);
80: }
81: DMMoabCreateBoxMesh(comm, user->dim, user->simplex, NULL, user->nele, 1, &user->dm);
82: }
84: if (user->debug) {
85: PetscPrintf(comm, "Setting field names to DM: \n");
86: for (i=0; i<user->nfields; i++)
87: PetscPrintf(comm, "\t Field{%D} = %s.\n",i,user->fieldnames[i]);
88: }
89: DMMoabSetFieldNames(user->dm, user->nfields, (const char**)user->fieldnames);
90: PetscObjectSetName((PetscObject)user->dm, "Structured Mesh");
91: PetscLogEventEnd(user->createMeshEvent,0,0,0,0);
92: return(0);
93: }
97: int main(int argc, char **argv)
98: {
99: AppCtx user; /* user-defined work context */
100: PetscRandom rctx;
101: Vec solution;
102: Mat system;
103: MPI_Comm comm;
104: PetscInt i;
107: PetscInitialize(&argc, &argv, NULL, help);
108: comm = PETSC_COMM_WORLD;
109: ProcessOptions(comm, &user);
110: CreateMesh(comm, &user);
112: /* set block size */
113: DMMoabSetBlockSize(user.dm, (user.interlace?user.nfields:1));
114: DMSetMatType(user.dm,MATAIJ);
116: DMSetFromOptions(user.dm);
118: /* SetUp the data structures for DMMOAB */
119: DMSetUp(user.dm);
121: if (user.debug) PetscPrintf(comm, "Creating a global vector defined on DM and setting random data.\n");
122: DMCreateGlobalVector(user.dm,&solution);
123: PetscRandomCreate(comm,&rctx);
124: VecSetRandom(solution,rctx);
126: /* test if matrix allocation for the prescribed matrix type is done correctly */
127: if (user.debug) PetscPrintf(comm, "Creating a global matrix defined on DM with the right block structure.\n");
128: DMCreateMatrix(user.dm,&system);
130: if (user.write_output) {
131: DMMoabSetGlobalFieldVector(user.dm, solution);
132: if (user.debug) PetscPrintf(comm, "Output mesh and associated field data to file: %s.\n",user.output_file);
133: DMMoabOutput(user.dm,(const char*)user.output_file,"");
134: }
136: if (user.nfields) {
137: for(i=0; i<user.nfields; i++) {
138: PetscFree(user.fieldnames[i]);
139: }
140: }
141: PetscRandomDestroy(&rctx);
142: VecDestroy(&solution);
143: MatDestroy(&system);
144: DMDestroy(&user.dm);
145: PetscFinalize();
146: return 0;
147: }