xref: /petsc/src/dm/tests/ex2.c (revision 6ffe77eaecce1557e50d00ca5347a7f48e598865)
1 
2 static char help[] = "Tests various 1-dimensional DMDA routines.\n\n";
3 
4 #include <petscdm.h>
5 #include <petscdmda.h>
6 #include <petscdraw.h>
7 
8 int main(int argc,char **argv)
9 {
10   PetscMPIInt            rank;
11   PetscInt               M  = 13,s=1,dof=1;
12   DMBoundaryType         bx = DM_BOUNDARY_PERIODIC;
13   DM                     da;
14   PetscViewer            viewer;
15   Vec                    local,global;
16   PetscScalar            value;
17   PetscDraw              draw;
18   PetscBool              flg = PETSC_FALSE;
19   ISLocalToGlobalMapping is;
20 
21   PetscCall(PetscInitialize(&argc,&argv,(char*)0,help));
22   PetscCall(PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",280,480,600,200,&viewer));
23   PetscCall(PetscViewerDrawGetDraw(viewer,0,&draw));
24   PetscCall(PetscDrawSetDoubleBuffer(draw));
25 
26   /* Readoptions */
27   PetscCall(PetscOptionsGetInt(NULL,NULL,"-M",&M,NULL));
28   PetscCall(PetscOptionsGetEnum(NULL,NULL,"-wrap",DMBoundaryTypes,(PetscEnum*)&bx,NULL));
29   PetscCall(PetscOptionsGetInt(NULL,NULL,"-dof",&dof,NULL));
30   PetscCall(PetscOptionsGetInt(NULL,NULL,"-s",&s,NULL));
31 
32   /* Create distributed array and get vectors */
33   PetscCall(DMDACreate1d(PETSC_COMM_WORLD,bx,M,dof,s,NULL,&da));
34   PetscCall(DMSetFromOptions(da));
35   PetscCall(DMSetUp(da));
36   PetscCall(DMView(da,viewer));
37   PetscCall(DMCreateGlobalVector(da,&global));
38   PetscCall(DMCreateLocalVector(da,&local));
39 
40   value = 1;
41   PetscCall(VecSet(global,value));
42 
43   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
44   value = rank+1;
45   PetscCall(VecScale(global,value));
46 
47   PetscCall(VecView(global,viewer));
48   PetscCall(PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD,"\nGlobal Vector:\n"));
49   PetscCall(VecView(global,PETSC_VIEWER_STDOUT_WORLD));
50   PetscCall(PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD,"\n"));
51 
52   /* Send ghost points to local vectors */
53   PetscCall(DMGlobalToLocalBegin(da,global,INSERT_VALUES,local));
54   PetscCall(DMGlobalToLocalEnd(da,global,INSERT_VALUES,local));
55 
56   PetscCall(PetscOptionsGetBool(NULL,NULL,"-local_print",&flg,NULL));
57   if (flg) {
58     PetscViewer            sviewer;
59 
60     PetscCall(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD));
61     PetscCall(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"\nLocal Vector: processor %d\n",rank));
62     PetscCall(PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer));
63     PetscCall(VecView(local,sviewer));
64     PetscCall(PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer));
65     PetscCall(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD));
66     PetscCall(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD));
67   }
68   PetscCall(PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD,"\nLocal to global mapping\n"));
69   PetscCall(DMGetLocalToGlobalMapping(da,&is));
70   PetscCall(ISLocalToGlobalMappingView(is,PETSC_VIEWER_STDOUT_WORLD));
71 
72   /* Free memory */
73   PetscCall(PetscViewerDestroy(&viewer));
74   PetscCall(VecDestroy(&global));
75   PetscCall(VecDestroy(&local));
76   PetscCall(DMDestroy(&da));
77   PetscCall(PetscFinalize());
78   return 0;
79 }
80 
81 /*TEST
82 
83    test:
84       nsize: 2
85       args: -nox
86       filter: grep -v " MPI process"
87       output_file: output/ex2_1.out
88       requires: x
89 
90    test:
91       suffix: 2
92       nsize: 3
93       args: -wrap none -local_print -nox
94       filter: grep -v "Vec Object: Vec"
95       requires: x
96 
97    test:
98       suffix: 3
99       nsize: 3
100       args: -wrap ghosted -local_print -nox
101       filter: grep -v "Vec Object: Vec"
102       requires: x
103 
104 TEST*/
105