Actual source code: ex23.c
petsc-3.7.3 2016-08-01
2: static char help[] = "Tests VecView()/VecLoad() for DMDA vectors (this tests DMDAGlobalToNatural()).\n\n";
4: #include <petscdm.h>
5: #include <petscdmda.h>
9: int main(int argc,char **argv)
10: {
11: PetscMPIInt size;
12: PetscInt N = 6,m=PETSC_DECIDE,n=PETSC_DECIDE,p=PETSC_DECIDE,M=8,dof=1,stencil_width=1,P=5,pt = 0,st = 0;
13: PetscErrorCode ierr;
14: PetscBool flg2,flg3,native = PETSC_FALSE;
15: DMBoundaryType bx = DM_BOUNDARY_NONE,by = DM_BOUNDARY_NONE,bz = DM_BOUNDARY_NONE;
16: DMDAStencilType stencil_type = DMDA_STENCIL_STAR;
17: DM da;
18: Vec global1,global2,global3,global4;
19: PetscScalar mone = -1.0;
20: PetscReal norm;
21: PetscViewer viewer;
22: PetscRandom rdm;
24: PetscInitialize(&argc,&argv,(char*)0,help);
26: PetscOptionsGetInt(NULL,NULL,"-M",&M,NULL);
27: PetscOptionsGetInt(NULL,NULL,"-N",&N,NULL);
28: PetscOptionsGetInt(NULL,NULL,"-P",&P,NULL);
29: PetscOptionsGetInt(NULL,NULL,"-dof",&dof,NULL);
30: PetscOptionsGetInt(NULL,NULL,"-stencil_width",&stencil_width,NULL);
31: PetscOptionsGetInt(NULL,NULL,"-periodic",&pt,NULL);
32: PetscOptionsGetBool(NULL,NULL,"-native",&native,NULL);
33: if (pt == 1) bx = DM_BOUNDARY_PERIODIC;
34: if (pt == 2) by = DM_BOUNDARY_PERIODIC;
35: if (pt == 3) {bx = DM_BOUNDARY_PERIODIC; by = DM_BOUNDARY_PERIODIC;}
36: if (pt == 4) bz = DM_BOUNDARY_PERIODIC;
38: PetscOptionsGetInt(NULL,NULL,"-stencil_type",&st,NULL);
39: stencil_type = (DMDAStencilType) st;
41: PetscOptionsHasName(NULL,NULL,"-one",&flg2);
42: PetscOptionsHasName(NULL,NULL,"-two",&flg2);
43: PetscOptionsHasName(NULL,NULL,"-three",&flg3);
44: if (flg2) {
45: DMDACreate2d(PETSC_COMM_WORLD,bx,by,stencil_type,M,N,m,n,dof,stencil_width,0,0,&da);
46: } else if (flg3) {
47: DMDACreate3d(PETSC_COMM_WORLD,bx,by,bz,stencil_type,M,N,P,m,n,p,dof,stencil_width,0,0,0,&da);
48: } else {
49: DMDACreate1d(PETSC_COMM_WORLD,bx,M,dof,stencil_width,NULL,&da);
50: }
52: DMCreateGlobalVector(da,&global1);
53: PetscRandomCreate(PETSC_COMM_WORLD,&rdm);
54: PetscRandomSetFromOptions(rdm);
55: DMCreateGlobalVector(da,&global2);
56: DMCreateGlobalVector(da,&global3);
57: DMCreateGlobalVector(da,&global4);
59: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"temp",FILE_MODE_WRITE,&viewer);
60: if (native) {PetscViewerPushFormat(viewer,PETSC_VIEWER_NATIVE);}
61: VecSetRandom(global1,rdm);
62: VecView(global1,viewer);
63: VecSetRandom(global3,rdm);
64: VecView(global3,viewer);
65: if (native) {PetscViewerPopFormat(viewer);}
66: PetscViewerDestroy(&viewer);
68: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"temp",FILE_MODE_READ,&viewer);
69: if (native) {PetscViewerPushFormat(viewer,PETSC_VIEWER_NATIVE);}
70: VecLoad(global2,viewer);
71: VecLoad(global4,viewer);
72: if (native) {PetscViewerPopFormat(viewer);}
73: PetscViewerDestroy(&viewer);
75: if (native) {
76: Vec filenative;
77: PetscBool same;
78: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"temp",FILE_MODE_READ,&viewer);
79: DMDACreateNaturalVector(da,&filenative);
80: /* DMDA "natural" Vec does not commandeer VecLoad. The following load will only work when run on the same process
81: * layout, where as the standard VecView/VecLoad (using DMDA and not PETSC_VIEWER_NATIVE) can be read on a different
82: * number of processors. */
83: VecLoad(filenative,viewer);
84: VecEqual(global2,filenative,&same);
85: if (!same) {
86: PetscPrintf(PETSC_COMM_WORLD,"ex23: global vector does not match contents of file\n");
87: VecView(global2,0);
88: VecView(filenative,0);
89: }
90: PetscViewerDestroy(&viewer);
91: VecDestroy(&filenative);
92: }
94: VecAXPY(global2,mone,global1);
95: VecNorm(global2,NORM_MAX,&norm);
96: if (norm != 0.0) {
97: MPI_Comm_size(PETSC_COMM_WORLD,&size);
98: PetscPrintf(PETSC_COMM_WORLD,"ex23: Norm of difference %g should be zero\n",(double)norm);
99: PetscPrintf(PETSC_COMM_WORLD," Number of processors %d\n",size);
100: PetscPrintf(PETSC_COMM_WORLD," M,N,P,dof %D %D %D %D\n",M,N,P,dof);
101: PetscPrintf(PETSC_COMM_WORLD," stencil_width %D stencil_type %d periodic %d\n",stencil_width,(int)stencil_type,(int)bx);
102: PetscPrintf(PETSC_COMM_WORLD," dimension %d\n",1 + (int) flg2 + (int) flg3);
103: }
104: VecAXPY(global4,mone,global3);
105: VecNorm(global4,NORM_MAX,&norm);
106: if (norm != 0.0) {
107: MPI_Comm_size(PETSC_COMM_WORLD,&size);
108: PetscPrintf(PETSC_COMM_WORLD,"ex23: Norm of difference %g should be zero\n",(double)norm);
109: PetscPrintf(PETSC_COMM_WORLD," Number of processors %d\n",size);
110: PetscPrintf(PETSC_COMM_WORLD," M,N,P,dof %D %D %D %D\n",M,N,P,dof);
111: PetscPrintf(PETSC_COMM_WORLD," stencil_width %D stencil_type %d periodic %d\n",stencil_width,(int)stencil_type,(int)bx);
112: PetscPrintf(PETSC_COMM_WORLD," dimension %d\n",1 + (int) flg2 + (int) flg3);
113: }
116: PetscRandomDestroy(&rdm);
117: DMDestroy(&da);
118: VecDestroy(&global1);
119: VecDestroy(&global2);
120: VecDestroy(&global3);
121: VecDestroy(&global4);
122: PetscFinalize();
123: return 0;
124: }