xref: /petsc/src/dm/impls/plex/tutorials/ex8.c (revision d71ae5a4db6382e7f06317b8d368875286fe9008)
1 static char help[] = "Element closure restrictions in tensor/lexicographic/spectral-element ordering using DMPlex\n\n";
2 
3 #include <petscdmplex.h>
4 
5 static PetscErrorCode ViewOffsets(DM dm, Vec X)
6 {
7   PetscInt           num_elem, elem_size, num_comp, num_dof;
8   PetscInt          *elem_restr_offsets;
9   const PetscScalar *x = NULL;
10   const char        *name;
11 
12   PetscFunctionBegin;
13   PetscCall(PetscObjectGetName((PetscObject)dm, &name));
14   PetscCall(DMPlexGetLocalOffsets(dm, NULL, 0, 0, 0, &num_elem, &elem_size, &num_comp, &num_dof, &elem_restr_offsets));
15   PetscCall(PetscPrintf(PETSC_COMM_SELF, "DM %s offsets: num_elem %" PetscInt_FMT ", size %" PetscInt_FMT ", comp %" PetscInt_FMT ", dof %" PetscInt_FMT "\n", name, num_elem, elem_size, num_comp, num_dof));
16   if (X) PetscCall(VecGetArrayRead(X, &x));
17   for (PetscInt c = 0; c < num_elem; c++) {
18     PetscCall(PetscIntView(elem_size, &elem_restr_offsets[c * elem_size], PETSC_VIEWER_STDOUT_SELF));
19     if (x) {
20       for (PetscInt i = 0; i < elem_size; i++) PetscCall(PetscScalarView(num_comp, &x[elem_restr_offsets[c * elem_size + i]], PETSC_VIEWER_STDERR_SELF));
21     }
22   }
23   if (X) PetscCall(VecRestoreArrayRead(X, &x));
24   PetscCall(PetscFree(elem_restr_offsets));
25   PetscFunctionReturn(0);
26 }
27 
28 int main(int argc, char **argv)
29 {
30   DM           dm;
31   PetscSection section;
32   PetscFE      fe;
33   PetscInt     dim, c, cStart, cEnd;
34   PetscBool    view_coord = PETSC_FALSE, tensor = PETSC_TRUE, project = PETSC_FALSE;
35 
36   PetscFunctionBeginUser;
37   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
38   PetscOptionsBegin(PETSC_COMM_WORLD, NULL, "Tensor closure restrictions", "DMPLEX");
39   PetscCall(PetscOptionsBool("-closure_tensor", "Apply DMPlexSetClosurePermutationTensor", "ex8.c", tensor, &tensor, NULL));
40   PetscCall(PetscOptionsBool("-project_coordinates", "Call DMProjectCoordinates explicitly", "ex8.c", project, &project, NULL));
41   PetscCall(PetscOptionsBool("-view_coord", "View coordinates of element closures", "ex8.c", view_coord, &view_coord, NULL));
42   PetscOptionsEnd();
43 
44   PetscCall(DMCreate(PETSC_COMM_WORLD, &dm));
45   PetscCall(DMSetType(dm, DMPLEX));
46   PetscCall(DMSetFromOptions(dm));
47   if (project) {
48     PetscFE  fe_coords;
49     PetscInt cdim;
50     PetscCall(DMGetCoordinateDim(dm, &cdim));
51     PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, cdim, cdim, PETSC_FALSE, 1, 1, &fe_coords));
52     PetscCall(DMProjectCoordinates(dm, fe_coords));
53     PetscCall(PetscFEDestroy(&fe_coords));
54   }
55   PetscCall(DMViewFromOptions(dm, NULL, "-dm_view"));
56   PetscCall(DMGetDimension(dm, &dim));
57 
58   PetscCall(PetscFECreateDefault(PETSC_COMM_SELF, dim, 1, PETSC_FALSE, NULL, PETSC_DETERMINE, &fe));
59   PetscCall(DMAddField(dm, NULL, (PetscObject)fe));
60   PetscCall(DMCreateDS(dm));
61   if (tensor) PetscCall(DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL));
62   PetscCall(DMGetLocalSection(dm, &section));
63   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
64   for (c = cStart; c < cEnd; c++) {
65     PetscInt numindices, *indices;
66     PetscCall(DMPlexGetClosureIndices(dm, section, section, c, PETSC_TRUE, &numindices, &indices, NULL, NULL));
67     PetscCall(PetscPrintf(PETSC_COMM_SELF, "Element #%" PetscInt_FMT "\n", c - cStart));
68     PetscCall(PetscIntView(numindices, indices, PETSC_VIEWER_STDOUT_SELF));
69     PetscCall(DMPlexRestoreClosureIndices(dm, section, section, c, PETSC_TRUE, &numindices, &indices, NULL, NULL));
70   }
71   if (view_coord) {
72     DM       cdm;
73     Vec      X;
74     PetscInt cdim;
75 
76     PetscCall(DMGetCoordinateDim(dm, &cdim));
77     PetscCall(DMGetCoordinateDM(dm, &cdm));
78     PetscCall(PetscObjectSetName((PetscObject)cdm, "coords"));
79     if (tensor) PetscCall(DMPlexSetClosurePermutationTensor(cdm, PETSC_DETERMINE, NULL));
80     for (c = cStart; c < cEnd; ++c) {
81       const PetscScalar *array;
82       PetscScalar       *x = NULL;
83       PetscInt           ndof;
84       PetscBool          isDG;
85 
86       PetscCall(DMPlexGetCellCoordinates(dm, c, &isDG, &ndof, &array, &x));
87       PetscCheck(ndof % cdim == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "ndof not divisible by cdim");
88       PetscCall(PetscPrintf(PETSC_COMM_SELF, "Element #%" PetscInt_FMT " coordinates\n", c - cStart));
89       for (PetscInt i = 0; i < ndof; i += cdim) PetscCall(PetscScalarView(cdim, &x[i], PETSC_VIEWER_STDOUT_SELF));
90       PetscCall(DMPlexRestoreCellCoordinates(dm, c, &isDG, &ndof, &array, &x));
91     }
92     PetscCall(ViewOffsets(dm, NULL));
93     PetscCall(DMGetCoordinatesLocal(dm, &X));
94     PetscCall(ViewOffsets(cdm, X));
95   }
96   PetscCall(PetscFEDestroy(&fe));
97   PetscCall(DMDestroy(&dm));
98   PetscCall(PetscFinalize());
99   return 0;
100 }
101 
102 /*TEST
103 
104   test:
105     suffix: 1d_q2
106     args: -dm_plex_dim 1 -petscspace_degree 2 -dm_plex_simplex 0 -dm_plex_box_faces 2
107   test:
108     suffix: 2d_q1
109     args: -dm_plex_dim 2 -petscspace_degree 1 -dm_plex_simplex 0 -dm_plex_box_faces 2,2
110   test:
111     suffix: 2d_q2
112     args: -dm_plex_dim 2 -petscspace_degree 2 -dm_plex_simplex 0 -dm_plex_box_faces 2,2
113   test:
114     suffix: 2d_q3
115     args: -dm_plex_dim 2 -petscspace_degree 3 -dm_plex_simplex 0 -dm_plex_box_faces 1,1
116   test:
117     suffix: 3d_q1
118     args: -dm_plex_dim 3 -petscspace_degree 1 -dm_plex_simplex 0 -dm_plex_box_faces 1,1,1
119   test:
120     suffix: 1d_q1_periodic
121     requires: !complex
122     args: -dm_plex_dim 1 -petscspace_degree 1 -dm_plex_simplex 0 -dm_plex_box_faces 3 -dm_plex_box_bd periodic -dm_view -view_coord
123   test:
124     suffix: 2d_q1_periodic
125     requires: !complex
126     args: -dm_plex_dim 2 -petscspace_degree 1 -dm_plex_simplex 0 -dm_plex_box_faces 3,2 -dm_plex_box_bd periodic,none -dm_view -view_coord
127   test:
128     suffix: 3d_q1_periodic
129     requires: !complex
130     args: -dm_plex_dim 3 -petscspace_degree 1 -dm_plex_simplex 0 -dm_plex_box_faces 3,2,1 -dm_plex_box_bd periodic,none,none -dm_view -view_coord
131   test:
132     suffix: 3d_q1_periodic_project
133     requires: !complex
134     args: -dm_plex_dim 3 -petscspace_degree 1 -dm_plex_simplex 0 -dm_plex_box_faces 1,1,3 -dm_plex_box_bd none,none,periodic -dm_view -view_coord -project_coordinates
135 
136   test:
137     suffix: 3d_q2_periodic  # not actually periodic because only 2 cells
138     args: -dm_plex_dim 3 -petscspace_degree 2 -dm_plex_simplex 0 -dm_plex_box_faces 2,2,2 -dm_plex_box_bd periodic,none,periodic -dm_view
139 
140 TEST*/
141