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