xref: /petsc/src/dm/tests/ex39.c (revision 503c0ea9b45bcfbcebbb1ea5341243bbc69f0bea)
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   PetscCall(PetscInitialize(&argc,&argv,(char*)0,help));
16   PetscCall(PetscOptionsGetInt(NULL,0,"-stencil_width",&stencil_width,0));
17   PetscCall(PetscOptionsGetInt(NULL,0,"-dof",&dof,0));
18 
19   PetscCall(DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_MIRROR,M,dof,stencil_width,NULL,&da));
20   PetscCall(DMSetFromOptions(da));
21   PetscCall(DMSetUp(da));
22   PetscCall(DMDAGetCorners(da,&xstart,0,0,&m,0,0));
23 
24   PetscCall(DMCreateGlobalVector(da,&global));
25   PetscCall(DMDAVecGetArrayDOF(da,global,&vglobal));
26   for (i=xstart; i<xstart+m; i++) {
27     for (j=0; j<dof; j++) {
28       vglobal[i][j] = 100*(i+1) + j;
29     }
30   }
31   PetscCall(DMDAVecRestoreArrayDOF(da,global,&vglobal));
32 
33   PetscCall(DMCreateLocalVector(da,&local));
34   PetscCall(DMGlobalToLocalBegin(da,global,INSERT_VALUES,local));
35   PetscCall(DMGlobalToLocalEnd(da,global,INSERT_VALUES,local));
36 
37   PetscCall(PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer));
38   PetscCall(VecView(local,sviewer));
39   PetscCall(PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer));
40   PetscCall(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD));
41   PetscCall(VecView(global,PETSC_VIEWER_STDOUT_WORLD));
42 
43   PetscCall(DMDestroy(&da));
44   PetscCall(VecDestroy(&local));
45   PetscCall(VecDestroy(&global));
46 
47   PetscCall(PetscFinalize());
48   return 0;
49 }
50 
51 /*TEST
52 
53    test:
54 
55    test:
56       suffix: 2
57       nsize: 2
58       filter: grep -v "Vec Object"
59 
60 TEST*/
61