Actual source code: ex7.c
2: static char help[] = "Tests DALocalToLocalxxx().\n\n";
4: #include petscda.h
8: int main(int argc,char **argv)
9: {
10: PetscMPIInt rank;
11: 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;
13: PetscTruth flg = PETSC_FALSE,flg2,flg3;
14: DAPeriodicType periodic;
15: DAStencilType stencil_type;
16: DA da;
17: Vec local,global,local_copy;
18: PetscScalar value;
19: PetscReal norm,work;
20: PetscViewer viewer;
21: char filename[64];
22: FILE *file;
24: PetscInitialize(&argc,&argv,(char*)0,help);
26: PetscOptionsGetInt(PETSC_NULL,"-M",&M,PETSC_NULL);
27: PetscOptionsGetInt(PETSC_NULL,"-N",&N,PETSC_NULL);
28: PetscOptionsGetInt(PETSC_NULL,"-dof",&dof,PETSC_NULL);
29: PetscOptionsGetInt(PETSC_NULL,"-stencil_width",&stencil_width,PETSC_NULL);
30: PetscOptionsGetInt(PETSC_NULL,"-periodic",&pt,PETSC_NULL);
31: periodic = (DAPeriodicType) pt;
32: PetscOptionsGetInt(PETSC_NULL,"-stencil_type",&st,PETSC_NULL);
33: stencil_type = (DAStencilType) st;
35: PetscOptionsHasName(PETSC_NULL,"-2d",&flg2);
36: PetscOptionsHasName(PETSC_NULL,"-3d",&flg3);
37: if (flg2) {
38: DACreate2d(PETSC_COMM_WORLD,periodic,stencil_type,M,N,m,n,dof,stencil_width,
39: PETSC_NULL,PETSC_NULL,&da);
40: } else if (flg3) {
41: DACreate3d(PETSC_COMM_WORLD,periodic,stencil_type,M,N,P,m,n,p,dof,stencil_width,
42: PETSC_NULL,PETSC_NULL,PETSC_NULL,&da);
43: }
44: else {
45: DACreate1d(PETSC_COMM_WORLD,periodic,M,dof,stencil_width,PETSC_NULL,&da);
46: }
48: DACreateGlobalVector(da,&global);
49: DACreateLocalVector(da,&local);
50: VecDuplicate(local,&local_copy);
52:
53: /* zero out vectors so that ghostpoints are zero */
54: value = 0;
55: VecSet(local,value);
56: VecSet(local_copy,value);
58: VecGetOwnershipRange(global,&start,&end);
59: for (i=start; i<end; i++) {
60: value = i + 1;
61: VecSetValues(global,1,&i,&value,INSERT_VALUES);
62: }
63: VecAssemblyBegin(global);
64: VecAssemblyEnd(global);
66: DAGlobalToLocalBegin(da,global,INSERT_VALUES,local);
67: DAGlobalToLocalEnd(da,global,INSERT_VALUES,local);
70: DALocalToLocalBegin(da,local,INSERT_VALUES,local_copy);
71: DALocalToLocalEnd(da,local,INSERT_VALUES,local_copy);
73:
74: PetscOptionsGetTruth(PETSC_NULL,"-save",&flg,PETSC_NULL);
75: if (flg) {
76: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
77: sprintf(filename,"local.%d",rank);
78: PetscViewerASCIIOpen(PETSC_COMM_SELF,filename,&viewer);
79: PetscViewerASCIIGetPointer(viewer,&file);
80: VecView(local,viewer);
81: fprintf(file,"Vector with correct ghost points\n");
82: VecView(local_copy,viewer);
83: PetscViewerDestroy(viewer);
84: }
86: VecAXPY(local_copy,-1.0,local);
87: VecNorm(local_copy,NORM_MAX,&work);
88: MPI_Allreduce(&work,&norm,1,MPIU_REAL,MPI_MAX,PETSC_COMM_WORLD);
89: PetscPrintf(PETSC_COMM_WORLD,"Norm of difference %G should be zero\n",norm);
90:
91: VecDestroy(local_copy);
92: VecDestroy(local);
93: VecDestroy(global);
94: DADestroy(da);
95: PetscFinalize();
96: return 0;
97: }