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