xref: /petsc/src/dm/tests/ex39.c (revision b122ec5aa1bd4469eb4e0673542fb7de3f411254)
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   CHKERRQ(PetscInitialize(&argc,&argv,(char*)0,help));
16   CHKERRQ(PetscOptionsGetInt(NULL,0,"-stencil_width",&stencil_width,0));
17   CHKERRQ(PetscOptionsGetInt(NULL,0,"-dof",&dof,0));
18 
19   CHKERRQ(DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_MIRROR,M,dof,stencil_width,NULL,&da));
20   CHKERRQ(DMSetFromOptions(da));
21   CHKERRQ(DMSetUp(da));
22   CHKERRQ(DMDAGetCorners(da,&xstart,0,0,&m,0,0));
23 
24   CHKERRQ(DMCreateGlobalVector(da,&global));
25   CHKERRQ(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   CHKERRQ(DMDAVecRestoreArrayDOF(da,global,&vglobal));
32 
33   CHKERRQ(DMCreateLocalVector(da,&local));
34   CHKERRQ(DMGlobalToLocalBegin(da,global,INSERT_VALUES,local));
35   CHKERRQ(DMGlobalToLocalEnd(da,global,INSERT_VALUES,local));
36 
37   CHKERRQ(PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer));
38   CHKERRQ(VecView(local,sviewer));
39   CHKERRQ(PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer));
40   CHKERRQ(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD));
41   CHKERRQ(VecView(global,PETSC_VIEWER_STDOUT_WORLD));
42 
43   CHKERRQ(DMDestroy(&da));
44   CHKERRQ(VecDestroy(&local));
45   CHKERRQ(VecDestroy(&global));
46 
47   CHKERRQ(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