1 2 static char help[] = "Tests VecView()/VecLoad() for DMDA vectors (this tests DMDAGlobalToNatural()).\n\n"; 3 4 #include <petscdm.h> 5 #include <petscdmda.h> 6 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; 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,"-P",&P,NULL)); 26 CHKERRQ(PetscOptionsGetInt(NULL,NULL,"-dof",&dof,NULL)); 27 CHKERRQ(PetscOptionsGetInt(NULL,NULL,"-stencil_width",&stencil_width,NULL)); 28 CHKERRQ(PetscOptionsGetInt(NULL,NULL,"-periodic",&pt,NULL)); 29 CHKERRQ(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; 34 35 CHKERRQ(PetscOptionsGetInt(NULL,NULL,"-stencil_type",&st,NULL)); 36 stencil_type = (DMDAStencilType) st; 37 38 CHKERRQ(PetscOptionsHasName(NULL,NULL,"-one",&flg2)); 39 CHKERRQ(PetscOptionsHasName(NULL,NULL,"-two",&flg2)); 40 CHKERRQ(PetscOptionsHasName(NULL,NULL,"-three",&flg3)); 41 if (flg2) { 42 CHKERRQ(DMDACreate2d(PETSC_COMM_WORLD,bx,by,stencil_type,M,N,m,n,dof,stencil_width,0,0,&da)); 43 } else if (flg3) { 44 CHKERRQ(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 CHKERRQ(DMDACreate1d(PETSC_COMM_WORLD,bx,M,dof,stencil_width,NULL,&da)); 47 } 48 CHKERRQ(DMSetFromOptions(da)); 49 CHKERRQ(DMSetUp(da)); 50 51 CHKERRQ(DMCreateGlobalVector(da,&global1)); 52 CHKERRQ(PetscRandomCreate(PETSC_COMM_WORLD,&rdm)); 53 CHKERRQ(PetscRandomSetFromOptions(rdm)); 54 CHKERRQ(DMCreateGlobalVector(da,&global2)); 55 CHKERRQ(DMCreateGlobalVector(da,&global3)); 56 CHKERRQ(DMCreateGlobalVector(da,&global4)); 57 58 CHKERRQ(PetscViewerBinaryOpen(PETSC_COMM_WORLD,"temp",FILE_MODE_WRITE,&viewer)); 59 if (native) CHKERRQ(PetscViewerPushFormat(viewer,PETSC_VIEWER_NATIVE)); 60 CHKERRQ(VecSetRandom(global1,rdm)); 61 CHKERRQ(VecView(global1,viewer)); 62 CHKERRQ(VecSetRandom(global3,rdm)); 63 CHKERRQ(VecView(global3,viewer)); 64 if (native) CHKERRQ(PetscViewerPopFormat(viewer)); 65 CHKERRQ(PetscViewerDestroy(&viewer)); 66 67 CHKERRQ(PetscViewerBinaryOpen(PETSC_COMM_WORLD,"temp",FILE_MODE_READ,&viewer)); 68 if (native) CHKERRQ(PetscViewerPushFormat(viewer,PETSC_VIEWER_NATIVE)); 69 CHKERRQ(VecLoad(global2,viewer)); 70 CHKERRQ(VecLoad(global4,viewer)); 71 if (native) CHKERRQ(PetscViewerPopFormat(viewer)); 72 CHKERRQ(PetscViewerDestroy(&viewer)); 73 74 if (native) { 75 Vec filenative; 76 PetscBool same; 77 CHKERRQ(PetscViewerBinaryOpen(PETSC_COMM_WORLD,"temp",FILE_MODE_READ,&viewer)); 78 CHKERRQ(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 CHKERRQ(VecLoad(filenative,viewer)); 83 CHKERRQ(VecEqual(global2,filenative,&same)); 84 if (!same) { 85 CHKERRQ(PetscPrintf(PETSC_COMM_WORLD,"ex23: global vector does not match contents of file\n")); 86 CHKERRQ(VecView(global2,0)); 87 CHKERRQ(VecView(filenative,0)); 88 } 89 CHKERRQ(PetscViewerDestroy(&viewer)); 90 CHKERRQ(VecDestroy(&filenative)); 91 } 92 93 CHKERRQ(VecAXPY(global2,mone,global1)); 94 CHKERRQ(VecNorm(global2,NORM_MAX,&norm)); 95 if (norm != 0.0) { 96 CHKERRMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size)); 97 CHKERRQ(PetscPrintf(PETSC_COMM_WORLD,"ex23: Norm of difference %g should be zero\n",(double)norm)); 98 CHKERRQ(PetscPrintf(PETSC_COMM_WORLD," Number of processors %d\n",size)); 99 CHKERRQ(PetscPrintf(PETSC_COMM_WORLD," M,N,P,dof %D %D %D %D\n",M,N,P,dof)); 100 CHKERRQ(PetscPrintf(PETSC_COMM_WORLD," stencil_width %D stencil_type %d periodic %d\n",stencil_width,(int)stencil_type,(int)bx)); 101 CHKERRQ(PetscPrintf(PETSC_COMM_WORLD," dimension %d\n",1 + (int) flg2 + (int) flg3)); 102 } 103 CHKERRQ(VecAXPY(global4,mone,global3)); 104 CHKERRQ(VecNorm(global4,NORM_MAX,&norm)); 105 if (norm != 0.0) { 106 CHKERRMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size)); 107 CHKERRQ(PetscPrintf(PETSC_COMM_WORLD,"ex23: Norm of difference %g should be zero\n",(double)norm)); 108 CHKERRQ(PetscPrintf(PETSC_COMM_WORLD," Number of processors %d\n",size)); 109 CHKERRQ(PetscPrintf(PETSC_COMM_WORLD," M,N,P,dof %D %D %D %D\n",M,N,P,dof)); 110 CHKERRQ(PetscPrintf(PETSC_COMM_WORLD," stencil_width %D stencil_type %d periodic %d\n",stencil_width,(int)stencil_type,(int)bx)); 111 CHKERRQ(PetscPrintf(PETSC_COMM_WORLD," dimension %d\n",1 + (int) flg2 + (int) flg3)); 112 } 113 114 CHKERRQ(PetscRandomDestroy(&rdm)); 115 CHKERRQ(DMDestroy(&da)); 116 CHKERRQ(VecDestroy(&global1)); 117 CHKERRQ(VecDestroy(&global2)); 118 CHKERRQ(VecDestroy(&global3)); 119 CHKERRQ(VecDestroy(&global4)); 120 ierr = PetscFinalize(); 121 return ierr; 122 } 123 124 /*TEST 125 126 test: 127 nsize: {{1 3}} 128 args: -one -dof {{1 2 3}} -stencil_type {{0 1}} 129 130 test: 131 suffix: 3 132 nsize: {{2 4}} 133 args: -two -dof {{1 3}} -stencil_type {{0 1}} 134 135 test: 136 suffix: 4 137 nsize: {{1 4}} 138 args: -three -dof {{2 3}} -stencil_type {{0 1}} 139 140 test: 141 suffix: 2 142 nsize: 2 143 args: -two -native 144 145 TEST*/ 146