Actual source code: ex2.cxx
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;
21: PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options)
22: {
24: PetscBool flg;
26: options->debug = PETSC_FALSE;
27: options->dim = 2;
28: options->nele = 5;
29: options->nfields = 256;
30: options->simplex = PETSC_FALSE;
31: options->write_output = PETSC_FALSE;
32: options->interlace = PETSC_FALSE;
33: options->input_file[0] = '\0';
34: PetscStrcpy(options->output_file,"ex2.h5m");
36: PetscOptionsBegin(comm, "", "Meshing Problem Options", "DMMOAB");
37: PetscOptionsBool("-debug", "Enable debug messages", "ex2.cxx", options->debug, &options->debug, NULL);
38: PetscOptionsBool("-interlace", "Use interlaced arrangement for the field data", "ex2.cxx", options->interlace, &options->interlace, NULL);
39: PetscOptionsBool("-simplex", "Create simplices instead of tensor product elements", "ex2.cxx", options->simplex, &options->simplex, NULL);
40: PetscOptionsRangeInt("-dim", "The topological mesh dimension", "ex2.cxx", options->dim, &options->dim, NULL,1,3);
41: PetscOptionsBoundedInt("-n", "The number of elements in each dimension", "ex2.cxx", options->nele, &options->nele, NULL,1);
42: PetscOptionsString("-meshfile", "The input mesh file", "ex2.cxx", options->input_file, options->input_file, sizeof(options->input_file), NULL);
43: 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);
44: PetscOptionsStringArray("-fields", "The list of names of the field variables", "ex2.cxx", options->fieldnames,&options->nfields, &flg);
45: PetscOptionsEnd();
47: if (options->debug) PetscPrintf(comm, "Total number of fields: %D.\n",options->nfields);
48: if (!flg) { /* if no field names were given by user, assign a default */
49: options->nfields = 1;
50: PetscStrallocpy("TestEX2Var",&options->fieldnames[0]);
51: }
53: PetscLogEventRegister("CreateMesh", DM_CLASSID, &options->createMeshEvent);
54: return 0;
55: }
57: PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *user)
58: {
59: PetscInt i;
60: size_t len;
61: PetscMPIInt rank;
63: PetscLogEventBegin(user->createMeshEvent,0,0,0,0);
64: MPI_Comm_rank(comm, &rank);
65: PetscStrlen(user->input_file, &len);
66: if (len) {
67: if (user->debug) PetscPrintf(comm, "Loading mesh from file: %s and creating a DM object.\n",user->input_file);
68: DMMoabLoadFromFile(comm, user->dim, 1, user->input_file, "", &user->dm);
69: }
70: else {
71: if (user->debug) {
72: 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);
73: }
74: DMMoabCreateBoxMesh(comm, user->dim, user->simplex, NULL, user->nele, 1, &user->dm);
75: }
77: if (user->debug) {
78: PetscPrintf(comm, "Setting field names to DM: \n");
79: for (i=0; i<user->nfields; i++)
80: PetscPrintf(comm, "\t Field{%D} = %s.\n",i,user->fieldnames[i]);
81: }
82: DMMoabSetFieldNames(user->dm, user->nfields, (const char**)user->fieldnames);
83: PetscObjectSetName((PetscObject)user->dm, "Structured Mesh");
84: PetscLogEventEnd(user->createMeshEvent,0,0,0,0);
85: return 0;
86: }
88: int main(int argc, char **argv)
89: {
90: AppCtx user; /* user-defined work context */
91: PetscRandom rctx;
92: Vec solution;
93: Mat system;
94: MPI_Comm comm;
95: PetscInt i;
97: PetscInitialize(&argc, &argv, NULL, help);
98: comm = PETSC_COMM_WORLD;
99: ProcessOptions(comm, &user);
100: CreateMesh(comm, &user);
102: /* set block size */
103: DMMoabSetBlockSize(user.dm, (user.interlace?user.nfields:1));
104: DMSetMatType(user.dm,MATAIJ);
106: DMSetFromOptions(user.dm);
108: /* SetUp the data structures for DMMOAB */
109: DMSetUp(user.dm);
111: if (user.debug) PetscPrintf(comm, "Creating a global vector defined on DM and setting random data.\n");
112: DMCreateGlobalVector(user.dm,&solution);
113: PetscRandomCreate(comm,&rctx);
114: VecSetRandom(solution,rctx);
116: /* test if matrix allocation for the prescribed matrix type is done correctly */
117: if (user.debug) PetscPrintf(comm, "Creating a global matrix defined on DM with the right block structure.\n");
118: DMCreateMatrix(user.dm,&system);
120: if (user.write_output) {
121: DMMoabSetGlobalFieldVector(user.dm, solution);
122: if (user.debug) PetscPrintf(comm, "Output mesh and associated field data to file: %s.\n",user.output_file);
123: DMMoabOutput(user.dm,(const char*)user.output_file,"");
124: }
126: if (user.nfields) {
127: for (i=0; i<user.nfields; i++) PetscFree(user.fieldnames[i]);
128: }
129: PetscRandomDestroy(&rctx);
130: VecDestroy(&solution);
131: MatDestroy(&system);
132: DMDestroy(&user.dm);
133: PetscFinalize();
134: return 0;
135: }
137: /*TEST
139: build:
140: requires: moab
142: test:
143: args: -debug -fields v1,v2,v3
145: TEST*/