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