xref: /petsc/src/dm/tests/ex39.c (revision ebead697dbf761eb322f829370bbe90b3bd93fa3)
1 
2 static char help[] = "Tests mirror boundary conditions in 1-d.\n\n";
3 
4 #include <petscdm.h>
5 #include <petscdmda.h>
6 
7 int main(int argc,char **argv)
8 {
9   PetscInt       M = 6,stencil_width = 1, dof = 1,m,xstart,i,j;
10   DM             da;
11   Vec            global,local;
12   PetscScalar    **vglobal;
13   PetscViewer    sviewer;
14 
15   PetscFunctionBeginUser;
16   PetscCall(PetscInitialize(&argc,&argv,(char*)0,help));
17   PetscCall(PetscOptionsGetInt(NULL,0,"-stencil_width",&stencil_width,0));
18   PetscCall(PetscOptionsGetInt(NULL,0,"-dof",&dof,0));
19 
20   PetscCall(DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_MIRROR,M,dof,stencil_width,NULL,&da));
21   PetscCall(DMSetFromOptions(da));
22   PetscCall(DMSetUp(da));
23   PetscCall(DMDAGetCorners(da,&xstart,0,0,&m,0,0));
24 
25   PetscCall(DMCreateGlobalVector(da,&global));
26   PetscCall(DMDAVecGetArrayDOF(da,global,&vglobal));
27   for (i=xstart; i<xstart+m; i++) {
28     for (j=0; j<dof; j++) {
29       vglobal[i][j] = 100*(i+1) + j;
30     }
31   }
32   PetscCall(DMDAVecRestoreArrayDOF(da,global,&vglobal));
33 
34   PetscCall(DMCreateLocalVector(da,&local));
35   PetscCall(DMGlobalToLocalBegin(da,global,INSERT_VALUES,local));
36   PetscCall(DMGlobalToLocalEnd(da,global,INSERT_VALUES,local));
37 
38   PetscCall(PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer));
39   PetscCall(VecView(local,sviewer));
40   PetscCall(PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer));
41   PetscCall(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD));
42   PetscCall(VecView(global,PETSC_VIEWER_STDOUT_WORLD));
43 
44   PetscCall(DMDestroy(&da));
45   PetscCall(VecDestroy(&local));
46   PetscCall(VecDestroy(&global));
47 
48   PetscCall(PetscFinalize());
49   return 0;
50 }
51 
52 /*TEST
53 
54    test:
55 
56    test:
57       suffix: 2
58       nsize: 2
59       filter: grep -v "Vec Object"
60 
61 TEST*/
62