xref: /petsc/src/dm/impls/plex/tests/ex16.c (revision 6a98f8dc3f2c9149905a87dc2e9d0fedaf64e09a)
1 static char help[] = "Tests for creation of submeshes\n\n";
2 
3 #include <petscdmplex.h>
4 
5 typedef struct {
6   PetscInt  debug;       /* The debugging level */
7   PetscInt  dim;         /* The topological mesh dimension */
8   PetscBool cellSimplex; /* Use simplices or hexes */
9 } AppCtx;
10 
11 PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options)
12 {
13   PetscErrorCode ierr;
14 
15   PetscFunctionBegin;
16   options->debug       = 0;
17   options->dim         = 2;
18   options->cellSimplex = PETSC_TRUE;
19 
20   ierr = PetscOptionsBegin(comm, "", "Meshing Problem Options", "DMPLEX");CHKERRQ(ierr);
21   ierr = PetscOptionsBoundedInt("-debug", "The debugging level", "ex16.c", options->debug, &options->debug, NULL,0);CHKERRQ(ierr);
22   ierr = PetscOptionsRangeInt("-dim", "The topological mesh dimension", "ex16.c", options->dim, &options->dim, NULL,1,3);CHKERRQ(ierr);
23   ierr = PetscOptionsBool("-cell_simplex", "Use simplices if true, otherwise hexes", "ex16.c", options->cellSimplex, &options->cellSimplex, NULL);CHKERRQ(ierr);
24   ierr = PetscOptionsEnd();
25   PetscFunctionReturn(0);
26 }
27 
28 PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *user, DM *dm)
29 {
30   PetscInt       dim         = user->dim;
31   PetscBool      cellSimplex = user->cellSimplex;
32   PetscErrorCode ierr;
33 
34   PetscFunctionBegin;
35   ierr = DMPlexCreateBoxMesh(comm, dim, cellSimplex, NULL, NULL, NULL, NULL, PETSC_TRUE, dm);CHKERRQ(ierr);
36   {
37     DM pdm = NULL;
38 
39     /* Distribute mesh over processes */
40     ierr = DMPlexDistribute(*dm, 0, NULL, &pdm);CHKERRQ(ierr);
41     if (pdm) {
42       ierr = DMViewFromOptions(pdm, NULL, "-dm_view");CHKERRQ(ierr);
43       ierr = DMDestroy(dm);CHKERRQ(ierr);
44       *dm  = pdm;
45     }
46   }
47   ierr = PetscObjectSetName((PetscObject) *dm, "Mesh");CHKERRQ(ierr);
48   ierr = DMSetFromOptions(*dm);CHKERRQ(ierr);
49   ierr = DMViewFromOptions(*dm, NULL, "-dm_view");CHKERRQ(ierr);
50   PetscFunctionReturn(0);
51 }
52 
53 PetscErrorCode CreateSubmesh(DM dm, PetscBool start, DM *subdm)
54 {
55   DMLabel        label, map;
56   PetscInt       cStart, cEnd, cStartSub, cEndSub, c;
57   PetscErrorCode ierr;
58 
59   PetscFunctionBegin;
60   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
61   ierr = DMCreateLabel(dm, "cells");CHKERRQ(ierr);
62   ierr = DMGetLabel(dm, "cells", &label);CHKERRQ(ierr);
63   ierr = DMLabelClearStratum(label, 1);CHKERRQ(ierr);
64   if (start) {cStartSub = cStart; cEndSub = cEnd/2;}
65   else       {cStartSub = cEnd/2; cEndSub = cEnd;}
66   for (c = cStartSub; c < cEndSub; ++c) {ierr = DMLabelSetValue(label, c, 1);CHKERRQ(ierr);}
67   ierr = DMPlexFilter(dm, label, 1, subdm);CHKERRQ(ierr);
68   ierr = PetscObjectSetName((PetscObject) *subdm, "Submesh");CHKERRQ(ierr);
69   ierr = DMViewFromOptions(*subdm, NULL, "-dm_view");CHKERRQ(ierr);
70   ierr = DMPlexGetSubpointMap(*subdm, &map);CHKERRQ(ierr);
71   ierr = DMLabelView(map, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
72   PetscFunctionReturn(0);
73 }
74 
75 int main(int argc, char **argv)
76 {
77   DM             dm, subdm;
78   AppCtx         user;
79   PetscErrorCode ierr;
80 
81   ierr = PetscInitialize(&argc, &argv, NULL, help);if (ierr) return ierr;
82   ierr = ProcessOptions(PETSC_COMM_WORLD, &user);CHKERRQ(ierr);
83   ierr = CreateMesh(PETSC_COMM_WORLD, &user, &dm);CHKERRQ(ierr);
84   ierr = CreateSubmesh(dm, PETSC_TRUE, &subdm);CHKERRQ(ierr);
85   ierr = DMSetFromOptions(subdm);CHKERRQ(ierr);
86   ierr = DMDestroy(&subdm);CHKERRQ(ierr);
87   ierr = CreateSubmesh(dm, PETSC_FALSE, &subdm);CHKERRQ(ierr);
88   ierr = DMSetFromOptions(subdm);CHKERRQ(ierr);
89   ierr = DMDestroy(&subdm);CHKERRQ(ierr);
90   ierr = DMDestroy(&dm);CHKERRQ(ierr);
91   ierr = PetscFinalize();
92   return ierr;
93 }
94 
95 /*TEST
96 
97   test:
98     suffix: 0
99     requires: triangle
100     args: -dm_view ascii::ascii_info_detail -dm_plex_check_symmetry -dm_plex_check_skeleton -dm_plex_check_faces
101 
102 TEST*/
103