xref: /petsc/src/dm/impls/moab/tests/ex3.cxx (revision 63a3b9bc7a1f24f247904ccba9383635fe6abade)
1c4762a1bSJed Brown static char help[] = "Create a box mesh with DMMoab and test defining a tag on the mesh\n\n";
2c4762a1bSJed Brown 
3c4762a1bSJed Brown #include <petscdmmoab.h>
4c4762a1bSJed Brown 
5c4762a1bSJed Brown typedef struct {
6c4762a1bSJed Brown   DM            dm;                /* DM implementation using the MOAB interface */
7c4762a1bSJed Brown   PetscBool     debug;             /* The debugging level */
8c4762a1bSJed Brown   PetscLogEvent createMeshEvent;
9c4762a1bSJed Brown   /* Domain and mesh definition */
10c4762a1bSJed Brown   PetscInt      dim;                            /* The topological mesh dimension */
11c4762a1bSJed Brown   PetscInt      nele;                           /* Elements in each dimension */
12c4762a1bSJed Brown   PetscInt      degree;                         /* Degree of refinement */
13c4762a1bSJed Brown   PetscBool     simplex;                        /* Use simplex elements */
14c4762a1bSJed Brown   PetscInt      nlevels;                        /* Number of levels in mesh hierarchy */
15c4762a1bSJed Brown   PetscInt      nghost;                        /* Number of ghost layers in the mesh */
16c4762a1bSJed Brown   char          input_file[PETSC_MAX_PATH_LEN];   /* Import mesh from file */
17c4762a1bSJed Brown   char          output_file[PETSC_MAX_PATH_LEN];   /* Output mesh file name */
18c4762a1bSJed Brown   PetscBool     write_output;                        /* Write output mesh and data to file */
19c4762a1bSJed Brown } AppCtx;
20c4762a1bSJed Brown 
21c4762a1bSJed Brown PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options)
22c4762a1bSJed Brown {
23c4762a1bSJed Brown   PetscFunctionBegin;
24c4762a1bSJed Brown   options->debug             = PETSC_FALSE;
25c4762a1bSJed Brown   options->nlevels           = 1;
26c4762a1bSJed Brown   options->nghost            = 1;
27c4762a1bSJed Brown   options->dim               = 2;
28c4762a1bSJed Brown   options->nele              = 5;
29c4762a1bSJed Brown   options->degree            = 2;
30c4762a1bSJed Brown   options->simplex           = PETSC_FALSE;
31c4762a1bSJed Brown   options->write_output      = PETSC_FALSE;
32c4762a1bSJed Brown   options->input_file[0]     = '\0';
339566063dSJacob Faibussowitsch   PetscCall(PetscStrcpy(options->output_file,"ex3.h5m"));
34c4762a1bSJed Brown 
35d0609cedSBarry Smith   PetscOptionsBegin(comm, "", "Uniform Mesh Refinement Options", "DMMOAB");
369566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-debug", "Enable debug messages", "ex2.cxx", options->debug, &options->debug, NULL));
379566063dSJacob Faibussowitsch   PetscCall(PetscOptionsRangeInt("-dim", "The topological mesh dimension", "ex3.cxx", options->dim, &options->dim, NULL,0,3));
389566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-n", "The number of elements in each dimension", "ex3.cxx", options->nele, &options->nele, NULL,1));
399566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-levels", "Number of levels in the hierarchy", "ex3.cxx", options->nlevels, &options->nlevels, NULL,0));
409566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-degree", "Number of degrees at each level of refinement", "ex3.cxx", options->degree, &options->degree, NULL,0));
419566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-ghost", "Number of ghost layers in the mesh", "ex3.cxx", options->nghost, &options->nghost, NULL,0));
429566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-simplex", "Create simplices instead of tensor product elements", "ex3.cxx", options->simplex, &options->simplex, NULL));
439566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-input", "The input mesh file", "ex3.cxx", options->input_file, options->input_file, sizeof(options->input_file), NULL));
449566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-io", "Write out the mesh and solution that is defined on it (Default H5M format)", "ex3.cxx", options->output_file, options->output_file, sizeof(options->output_file), &options->write_output));
45d0609cedSBarry Smith   PetscOptionsEnd();
46c4762a1bSJed Brown 
479566063dSJacob Faibussowitsch   PetscCall(PetscLogEventRegister("CreateMesh",          DM_CLASSID,   &options->createMeshEvent));
48c4762a1bSJed Brown   PetscFunctionReturn(0);
49c4762a1bSJed Brown };
50c4762a1bSJed Brown 
51c4762a1bSJed Brown PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *user)
52c4762a1bSJed Brown {
53c4762a1bSJed Brown   size_t         len;
54c4762a1bSJed Brown   PetscMPIInt    rank;
55c4762a1bSJed Brown 
56c4762a1bSJed Brown   PetscFunctionBegin;
579566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(user->createMeshEvent,0,0,0,0));
589566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
599566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(user->input_file, &len));
60c4762a1bSJed Brown   if (len) {
619566063dSJacob Faibussowitsch     if (user->debug) PetscCall(PetscPrintf(comm, "Loading mesh from file: %s and creating the coarse level DM object.\n",user->input_file));
629566063dSJacob Faibussowitsch     PetscCall(DMMoabLoadFromFile(comm, user->dim, user->nghost, user->input_file, "", &user->dm));
635f80ce2aSJacob Faibussowitsch   } else {
64*63a3b9bcSJacob Faibussowitsch     if (user->debug) PetscCall(PetscPrintf(comm, "Creating a %" PetscInt_FMT "-dimensional structured %s mesh of %" PetscInt_FMT "x%" PetscInt_FMT "x%" PetscInt_FMT " in memory and creating a DM object.\n",user->dim,(user->simplex?"simplex":"regular"),user->nele,user->nele,user->nele));
659566063dSJacob Faibussowitsch     PetscCall(DMMoabCreateBoxMesh(comm, user->dim, user->simplex, NULL, user->nele, user->nghost, &user->dm));
66c4762a1bSJed Brown   }
679566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)user->dm, "Coarse Mesh"));
689566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(user->createMeshEvent,0,0,0,0));
69c4762a1bSJed Brown   PetscFunctionReturn(0);
70c4762a1bSJed Brown }
71c4762a1bSJed Brown 
72c4762a1bSJed Brown int main(int argc, char **argv)
73c4762a1bSJed Brown {
74c4762a1bSJed Brown   AppCtx         user;                 /* user-defined work context */
75c4762a1bSJed Brown   MPI_Comm       comm;
76c4762a1bSJed Brown   PetscInt       i;
77c4762a1bSJed Brown   Mat            R;
78c4762a1bSJed Brown   DM            *dmhierarchy;
79c4762a1bSJed Brown   PetscInt      *degrees;
80c4762a1bSJed Brown   PetscBool      createR;
81c4762a1bSJed Brown 
829566063dSJacob Faibussowitsch   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
83c4762a1bSJed Brown   comm = PETSC_COMM_WORLD;
84c4762a1bSJed Brown   createR = PETSC_FALSE;
85c4762a1bSJed Brown 
869566063dSJacob Faibussowitsch   PetscCall(ProcessOptions(comm, &user));
879566063dSJacob Faibussowitsch   PetscCall(CreateMesh(comm, &user));
889566063dSJacob Faibussowitsch   PetscCall(DMSetFromOptions(user.dm));
89c4762a1bSJed Brown 
90c4762a1bSJed Brown   /* SetUp the data structures for DMMOAB */
919566063dSJacob Faibussowitsch   PetscCall(DMSetUp(user.dm));
92c4762a1bSJed Brown 
939566063dSJacob Faibussowitsch   PetscCall(PetscMalloc(sizeof(DM)*(user.nlevels+1),&dmhierarchy));
94c4762a1bSJed Brown   for (i=0; i<=user.nlevels; i++) dmhierarchy[i] = NULL;
95c4762a1bSJed Brown 
96c4762a1bSJed Brown   // coarsest grid = 0
97c4762a1bSJed Brown   // finest grid = nlevels
98c4762a1bSJed Brown   dmhierarchy[0] = user.dm;
99c4762a1bSJed Brown   PetscObjectReference((PetscObject)user.dm);
100c4762a1bSJed Brown 
101c4762a1bSJed Brown   if (user.nlevels) {
1029566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(user.nlevels, &degrees));
103c4762a1bSJed Brown     for (i=0; i < user.nlevels; i++) degrees[i] = user.degree;
104*63a3b9bcSJacob Faibussowitsch     if (user.debug) PetscCall(PetscPrintf(comm, "Generate the MOAB mesh hierarchy with %" PetscInt_FMT " levels.\n", user.nlevels));
1059566063dSJacob Faibussowitsch     PetscCall(DMMoabGenerateHierarchy(user.dm,user.nlevels,degrees));
106c4762a1bSJed Brown 
107c4762a1bSJed Brown     PetscBool usehierarchy=PETSC_FALSE;
1089566063dSJacob Faibussowitsch     if (usehierarchy) PetscCall(DMRefineHierarchy(user.dm,user.nlevels,&dmhierarchy[1]));
109c4762a1bSJed Brown     else {
110c4762a1bSJed Brown       if (user.debug) {
111*63a3b9bcSJacob Faibussowitsch         PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Level %" PetscInt_FMT "\n", 0));
1129566063dSJacob Faibussowitsch         PetscCall(DMView(user.dm, 0));
113c4762a1bSJed Brown       }
114c4762a1bSJed Brown       for (i=1; i<=user.nlevels; i++) {
115*63a3b9bcSJacob Faibussowitsch         if (user.debug) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Level %" PetscInt_FMT "\n", i));
1169566063dSJacob Faibussowitsch         PetscCall(DMRefine(dmhierarchy[i-1],MPI_COMM_NULL,&dmhierarchy[i]));
1179566063dSJacob Faibussowitsch         if (createR) PetscCall(DMCreateInterpolation(dmhierarchy[i-1],dmhierarchy[i],&R,NULL));
118c4762a1bSJed Brown         if (user.debug) {
1199566063dSJacob Faibussowitsch           PetscCall(DMView(dmhierarchy[i], 0));
1209566063dSJacob Faibussowitsch           if (createR) PetscCall(MatView(R,0));
121c4762a1bSJed Brown         }
122c4762a1bSJed Brown         /* Solvers could now set operator "R" to the multigrid PC object for level i
123c4762a1bSJed Brown             PCMGSetInterpolation(pc,i,R)
124c4762a1bSJed Brown         */
1259566063dSJacob Faibussowitsch         if (createR) PetscCall(MatDestroy(&R));
126c4762a1bSJed Brown       }
127c4762a1bSJed Brown     }
128c4762a1bSJed Brown   }
129c4762a1bSJed Brown 
130c4762a1bSJed Brown   if (user.write_output) {
1319566063dSJacob Faibussowitsch     if (user.debug) PetscCall(PetscPrintf(comm, "Output mesh hierarchy to file: %s.\n",user.output_file));
1329566063dSJacob Faibussowitsch     PetscCall(DMMoabOutput(dmhierarchy[user.nlevels],(const char*)user.output_file,""));
133c4762a1bSJed Brown   }
134c4762a1bSJed Brown 
1359566063dSJacob Faibussowitsch   for (i=0; i<=user.nlevels; i++) PetscCall(DMDestroy(&dmhierarchy[i]));
1369566063dSJacob Faibussowitsch   PetscCall(PetscFree(degrees));
1379566063dSJacob Faibussowitsch   PetscCall(PetscFree(dmhierarchy));
1389566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&user.dm));
1399566063dSJacob Faibussowitsch   PetscCall(PetscFinalize());
140c4762a1bSJed Brown   return 0;
141c4762a1bSJed Brown }
142c4762a1bSJed Brown 
143c4762a1bSJed Brown /*TEST
144c4762a1bSJed Brown 
145c4762a1bSJed Brown      build:
146c4762a1bSJed Brown        requires: moab
147c4762a1bSJed Brown 
148c4762a1bSJed Brown      test:
149c4762a1bSJed Brown        args: -debug -n 2 -dim 2 -levels 2 -simplex
150c4762a1bSJed Brown        filter:  grep -v "DM_0x"
151c4762a1bSJed Brown 
152c4762a1bSJed Brown      test:
153c4762a1bSJed Brown        args: -debug -n 2 -dim 3 -levels 2
154c4762a1bSJed Brown        filter:  grep -v "DM_0x"
155c4762a1bSJed Brown        suffix: 1_2
156c4762a1bSJed Brown 
157c4762a1bSJed Brown      test:
158c4762a1bSJed Brown        args: -debug -n 2 -dim 3 -ghost 1 -levels 2
159c4762a1bSJed Brown        filter:  grep -v "DM_0x"
160c4762a1bSJed Brown        nsize: 2
161c4762a1bSJed Brown        suffix: 2_1
162c4762a1bSJed Brown 
163c4762a1bSJed Brown TEST*/
164