xref: /petsc/src/dm/tests/ex7.c (revision 40badf4fbc550ac1f60bd080eaff6de6d55b946d)
1 
2 static char help[] = "Tests DMLocalToLocalxxx() for DMDA.\n\n";
3 
4 #include <petscdmda.h>
5 
6 int main(int argc,char **argv)
7 {
8   PetscMPIInt      rank;
9   PetscInt         M=8,dof=1,stencil_width=1,i,start,end,P=5,N = 6,m=PETSC_DECIDE,n=PETSC_DECIDE,p=PETSC_DECIDE,pt = 0,st = 0;
10   PetscErrorCode   ierr;
11   PetscBool        flg = PETSC_FALSE,flg2,flg3;
12   DMBoundaryType   periodic;
13   DMDAStencilType  stencil_type;
14   DM               da;
15   Vec              local,global,local_copy;
16   PetscScalar      value;
17   PetscReal        norm,work;
18   PetscViewer      viewer;
19   char             filename[64];
20   FILE             *file;
21 
22   ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
23   CHKERRQ(PetscOptionsGetInt(NULL,NULL,"-M",&M,NULL));
24   CHKERRQ(PetscOptionsGetInt(NULL,NULL,"-N",&N,NULL));
25   CHKERRQ(PetscOptionsGetInt(NULL,NULL,"-dof",&dof,NULL));
26   CHKERRQ(PetscOptionsGetInt(NULL,NULL,"-stencil_width",&stencil_width,NULL));
27   CHKERRQ(PetscOptionsGetInt(NULL,NULL,"-periodic",&pt,NULL));
28 
29   periodic = (DMBoundaryType) pt;
30 
31   CHKERRQ(PetscOptionsGetInt(NULL,NULL,"-stencil_type",&st,NULL));
32 
33   stencil_type = (DMDAStencilType) st;
34 
35   CHKERRQ(PetscOptionsHasName(NULL,NULL,"-grid2d",&flg2));
36   CHKERRQ(PetscOptionsHasName(NULL,NULL,"-grid3d",&flg3));
37   if (flg2) {
38     CHKERRQ(DMDACreate2d(PETSC_COMM_WORLD,periodic,periodic,stencil_type,M,N,m,n,dof,stencil_width,NULL,NULL,&da));
39   } else if (flg3) {
40     CHKERRQ(DMDACreate3d(PETSC_COMM_WORLD,periodic,periodic,periodic,stencil_type,M,N,P,m,n,p,dof,stencil_width,NULL,NULL,NULL,&da));
41   } else {
42     CHKERRQ(DMDACreate1d(PETSC_COMM_WORLD,periodic,M,dof,stencil_width,NULL,&da));
43   }
44   CHKERRQ(DMSetFromOptions(da));
45   CHKERRQ(DMSetUp(da));
46 
47   CHKERRQ(DMCreateGlobalVector(da,&global));
48   CHKERRQ(DMCreateLocalVector(da,&local));
49   CHKERRQ(VecDuplicate(local,&local_copy));
50 
51   /* zero out vectors so that ghostpoints are zero */
52   value = 0;
53   CHKERRQ(VecSet(local,value));
54   CHKERRQ(VecSet(local_copy,value));
55 
56   CHKERRQ(VecGetOwnershipRange(global,&start,&end));
57   for (i=start; i<end; i++) {
58     value = i + 1;
59     CHKERRQ(VecSetValues(global,1,&i,&value,INSERT_VALUES));
60   }
61   CHKERRQ(VecAssemblyBegin(global));
62   CHKERRQ(VecAssemblyEnd(global));
63 
64   CHKERRQ(DMGlobalToLocalBegin(da,global,INSERT_VALUES,local));
65   CHKERRQ(DMGlobalToLocalEnd(da,global,INSERT_VALUES,local));
66 
67   CHKERRQ(DMLocalToLocalBegin(da,local,INSERT_VALUES,local_copy));
68   CHKERRQ(DMLocalToLocalEnd(da,local,INSERT_VALUES,local_copy));
69 
70   CHKERRQ(PetscOptionsGetBool(NULL,NULL,"-save",&flg,NULL));
71   if (flg) {
72     CHKERRMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
73     sprintf(filename,"local.%d",rank);
74     CHKERRQ(PetscViewerASCIIOpen(PETSC_COMM_SELF,filename,&viewer));
75     CHKERRQ(PetscViewerASCIIGetPointer(viewer,&file));
76     CHKERRQ(VecView(local,viewer));
77     fprintf(file,"Vector with correct ghost points\n");
78     CHKERRQ(VecView(local_copy,viewer));
79     CHKERRQ(PetscViewerDestroy(&viewer));
80   }
81 
82   CHKERRQ(VecAXPY(local_copy,-1.0,local));
83   CHKERRQ(VecNorm(local_copy,NORM_MAX,&work));
84   CHKERRMPI(MPI_Allreduce(&work,&norm,1,MPIU_REAL,MPIU_MAX,PETSC_COMM_WORLD));
85   CHKERRQ(PetscPrintf(PETSC_COMM_WORLD,"Norm of difference %g should be zero\n",(double)norm));
86 
87   CHKERRQ(VecDestroy(&local_copy));
88   CHKERRQ(VecDestroy(&local));
89   CHKERRQ(VecDestroy(&global));
90   CHKERRQ(DMDestroy(&da));
91   ierr = PetscFinalize();
92   return ierr;
93 }
94 
95 /*TEST
96 
97    test:
98       nsize: 8
99       args: -dof 3 -stencil_width 2 -M 50 -N 50 -periodic
100 
101    test:
102       suffix: 2
103       nsize: 8
104       args: -dof 3 -stencil_width 2 -M 50 -N 50 -periodic -grid2d
105       output_file: output/ex7_1.out
106 
107    test:
108       suffix: 3
109       nsize: 8
110       args: -dof 3 -stencil_width 2 -M 50 -N 50 -periodic -grid3d
111       output_file: output/ex7_1.out
112 
113 TEST*/
114