1 2 /* 3 Plots vectors obtained with DMDACreate1d() 4 */ 5 6 #include <petscdmda.h> /*I "petscdmda.h" I*/ 7 8 #undef __FUNCT__ 9 #define __FUNCT__ "DMDASetUniformCoordinates" 10 /*@ 11 DMDASetUniformCoordinates - Sets a DMDA coordinates to be a uniform grid 12 13 Collective on DMDA 14 15 Input Parameters: 16 + da - the distributed array object 17 . xmin,xmax - extremes in the x direction 18 . ymin,ymax - extremes in the y direction (use PETSC_NULL for 1 dimensional problems) 19 - zmin,zmax - extremes in the z direction (use PETSC_NULL for 1 or 2 dimensional problems) 20 21 Level: beginner 22 23 .seealso: DMDASetCoordinates(), DMDAGetCoordinates(), DMDACreate1d(), DMDACreate2d(), DMDACreate3d() 24 25 @*/ 26 PetscErrorCode DMDASetUniformCoordinates(DM da,PetscReal xmin,PetscReal xmax,PetscReal ymin,PetscReal ymax,PetscReal zmin,PetscReal zmax) 27 { 28 MPI_Comm comm; 29 DM cda; 30 DMDABoundaryType bx,by,bz; 31 Vec xcoor; 32 PetscScalar *coors; 33 PetscReal hx,hy,hz_; 34 PetscInt i,j,k,M,N,P,istart,isize,jstart,jsize,kstart,ksize,dim,cnt; 35 PetscErrorCode ierr; 36 37 PetscFunctionBegin; 38 if (xmax <= xmin) SETERRQ2(((PetscObject)da)->comm,PETSC_ERR_ARG_INCOMP,"xmax must be larger than xmin %G %G",xmin,xmax); 39 40 ierr = PetscObjectGetComm((PetscObject)da,&comm);CHKERRQ(ierr); 41 ierr = DMDAGetInfo(da,&dim,&M,&N,&P,0,0,0,0,0,&bx,&by,&bz,0);CHKERRQ(ierr); 42 ierr = DMDAGetCorners(da,&istart,&jstart,&kstart,&isize,&jsize,&ksize);CHKERRQ(ierr); 43 ierr = DMDAGetCoordinateDA(da, &cda);CHKERRQ(ierr); 44 ierr = DMCreateGlobalVector(cda, &xcoor);CHKERRQ(ierr); 45 if (dim == 1) { 46 if (bx == DMDA_BOUNDARY_PERIODIC) hx = (xmax-xmin)/M; 47 else hx = (xmax-xmin)/(M-1); 48 ierr = VecGetArray(xcoor,&coors);CHKERRQ(ierr); 49 for (i=0; i<isize; i++) { 50 coors[i] = xmin + hx*(i+istart); 51 } 52 ierr = VecRestoreArray(xcoor,&coors);CHKERRQ(ierr); 53 } else if (dim == 2) { 54 if (ymax <= ymin) SETERRQ2(((PetscObject)da)->comm,PETSC_ERR_ARG_INCOMP,"ymax must be larger than ymin %G %G",ymin,ymax); 55 if (bx == DMDA_BOUNDARY_PERIODIC) hx = (xmax-xmin)/(M); 56 else hx = (xmax-xmin)/(M-1); 57 if (by == DMDA_BOUNDARY_PERIODIC) hy = (ymax-ymin)/(N); 58 else hy = (ymax-ymin)/(N-1); 59 ierr = VecGetArray(xcoor,&coors);CHKERRQ(ierr); 60 cnt = 0; 61 for (j=0; j<jsize; j++) { 62 for (i=0; i<isize; i++) { 63 coors[cnt++] = xmin + hx*(i+istart); 64 coors[cnt++] = ymin + hy*(j+jstart); 65 } 66 } 67 ierr = VecRestoreArray(xcoor,&coors);CHKERRQ(ierr); 68 } else if (dim == 3) { 69 if (ymax <= ymin) SETERRQ2(((PetscObject)da)->comm,PETSC_ERR_ARG_INCOMP,"ymax must be larger than ymin %G %G",ymin,ymax); 70 if (zmax <= zmin) SETERRQ2(((PetscObject)da)->comm,PETSC_ERR_ARG_INCOMP,"zmax must be larger than zmin %G %G",zmin,zmax); 71 if (bx == DMDA_BOUNDARY_PERIODIC) hx = (xmax-xmin)/(M); 72 else hx = (xmax-xmin)/(M-1); 73 if (by == DMDA_BOUNDARY_PERIODIC) hy = (ymax-ymin)/(N); 74 else hy = (ymax-ymin)/(N-1); 75 if (bz == DMDA_BOUNDARY_PERIODIC) hz_ = (zmax-zmin)/(P); 76 else hz_ = (zmax-zmin)/(P-1); 77 ierr = VecGetArray(xcoor,&coors);CHKERRQ(ierr); 78 cnt = 0; 79 for (k=0; k<ksize; k++) { 80 for (j=0; j<jsize; j++) { 81 for (i=0; i<isize; i++) { 82 coors[cnt++] = xmin + hx*(i+istart); 83 coors[cnt++] = ymin + hy*(j+jstart); 84 coors[cnt++] = zmin + hz_*(k+kstart); 85 } 86 } 87 } 88 ierr = VecRestoreArray(xcoor,&coors);CHKERRQ(ierr); 89 } else { 90 SETERRQ1(((PetscObject)da)->comm,PETSC_ERR_SUP,"Cannot create uniform coordinates for this dimension %D\n",dim); 91 } 92 ierr = DMDASetCoordinates(da,xcoor);CHKERRQ(ierr); 93 ierr = PetscLogObjectParent(da,xcoor);CHKERRQ(ierr); 94 ierr = VecDestroy(&xcoor);CHKERRQ(ierr); 95 PetscFunctionReturn(0); 96 } 97 98 #undef __FUNCT__ 99 #define __FUNCT__ "VecView_MPI_Draw_DA1d" 100 PetscErrorCode VecView_MPI_Draw_DA1d(Vec xin,PetscViewer v) 101 { 102 DM da; 103 PetscErrorCode ierr; 104 PetscMPIInt rank,size,tag1,tag2; 105 PetscInt i,n,N,step,istart,isize,j; 106 MPI_Status status; 107 PetscReal coors[4],ymin,ymax,min,max,xmin,xmax,tmp,xgtmp; 108 const PetscScalar *array,*xg; 109 PetscDraw draw; 110 PetscBool isnull,showpoints = PETSC_FALSE; 111 MPI_Comm comm; 112 PetscDrawAxis axis; 113 Vec xcoor; 114 DMDABoundaryType bx; 115 116 PetscFunctionBegin; 117 ierr = PetscViewerDrawGetDraw(v,0,&draw);CHKERRQ(ierr); 118 ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); 119 120 ierr = PetscObjectQuery((PetscObject)xin,"DM",(PetscObject*)&da);CHKERRQ(ierr); 121 if (!da) SETERRQ(((PetscObject)xin)->comm,PETSC_ERR_ARG_WRONG,"Vector not generated from a DMDA"); 122 123 ierr = PetscOptionsGetBool(PETSC_NULL,"-draw_vec_mark_points",&showpoints,PETSC_NULL);CHKERRQ(ierr); 124 125 ierr = DMDAGetInfo(da,0,&N,0,0,0,0,0,&step,0,&bx,0,0,0);CHKERRQ(ierr); 126 ierr = DMDAGetCorners(da,&istart,0,0,&isize,0,0);CHKERRQ(ierr); 127 ierr = VecGetArrayRead(xin,&array);CHKERRQ(ierr); 128 ierr = VecGetLocalSize(xin,&n);CHKERRQ(ierr); 129 n = n/step; 130 131 /* get coordinates of nodes */ 132 ierr = DMDAGetCoordinates(da,&xcoor);CHKERRQ(ierr); 133 if (!xcoor) { 134 ierr = DMDASetUniformCoordinates(da,0.0,1.0,0.0,0.0,0.0,0.0);CHKERRQ(ierr); 135 ierr = DMDAGetCoordinates(da,&xcoor);CHKERRQ(ierr); 136 } 137 ierr = VecGetArrayRead(xcoor,&xg);CHKERRQ(ierr); 138 139 ierr = PetscObjectGetComm((PetscObject)xin,&comm);CHKERRQ(ierr); 140 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 141 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 142 143 /* 144 Determine the min and max x coordinate in plot 145 */ 146 if (!rank) { 147 xmin = PetscRealPart(xg[0]); 148 } 149 if (rank == size-1) { 150 xmax = PetscRealPart(xg[n-1]); 151 } 152 ierr = MPI_Bcast(&xmin,1,MPIU_REAL,0,comm);CHKERRQ(ierr); 153 ierr = MPI_Bcast(&xmax,1,MPIU_REAL,size-1,comm);CHKERRQ(ierr); 154 155 for (j=0; j<step; j++) { 156 ierr = PetscViewerDrawGetDraw(v,j,&draw);CHKERRQ(ierr); 157 ierr = PetscDrawCheckResizedWindow(draw);CHKERRQ(ierr); 158 159 /* 160 Determine the min and max y coordinate in plot 161 */ 162 min = 1.e20; max = -1.e20; 163 for (i=0; i<n; i++) { 164 #if defined(PETSC_USE_COMPLEX) 165 if (PetscRealPart(array[j+i*step]) < min) min = PetscRealPart(array[j+i*step]); 166 if (PetscRealPart(array[j+i*step]) > max) max = PetscRealPart(array[j+i*step]); 167 #else 168 if (array[j+i*step] < min) min = array[j+i*step]; 169 if (array[j+i*step] > max) max = array[j+i*step]; 170 #endif 171 } 172 if (min + 1.e-10 > max) { 173 min -= 1.e-5; 174 max += 1.e-5; 175 } 176 ierr = MPI_Reduce(&min,&ymin,1,MPIU_REAL,MPIU_MIN,0,comm);CHKERRQ(ierr); 177 ierr = MPI_Reduce(&max,&ymax,1,MPIU_REAL,MPIU_MAX,0,comm);CHKERRQ(ierr); 178 179 ierr = PetscDrawSynchronizedClear(draw);CHKERRQ(ierr); 180 ierr = PetscViewerDrawGetDrawAxis(v,j,&axis);CHKERRQ(ierr); 181 ierr = PetscLogObjectParent(draw,axis);CHKERRQ(ierr); 182 if (!rank) { 183 const char *title; 184 185 ierr = PetscDrawAxisSetLimits(axis,xmin,xmax,ymin,ymax);CHKERRQ(ierr); 186 ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr); 187 ierr = PetscDrawGetCoordinates(draw,coors,coors+1,coors+2,coors+3);CHKERRQ(ierr); 188 ierr = DMDAGetFieldName(da,j,&title);CHKERRQ(ierr); 189 if (title) {ierr = PetscDrawSetTitle(draw,title);CHKERRQ(ierr);} 190 } 191 ierr = MPI_Bcast(coors,4,MPIU_REAL,0,comm);CHKERRQ(ierr); 192 if (rank) { 193 ierr = PetscDrawSetCoordinates(draw,coors[0],coors[1],coors[2],coors[3]);CHKERRQ(ierr); 194 } 195 196 /* draw local part of vector */ 197 PetscObjectGetNewTag((PetscObject)xin,&tag1);CHKERRQ(ierr); 198 PetscObjectGetNewTag((PetscObject)xin,&tag2);CHKERRQ(ierr); 199 if (rank < size-1) { /*send value to right */ 200 ierr = MPI_Send((void*)&array[j+(n-1)*step],1,MPIU_REAL,rank+1,tag1,comm);CHKERRQ(ierr); 201 ierr = MPI_Send((void*)&xg[n-1],1,MPIU_REAL,rank+1,tag1,comm);CHKERRQ(ierr); 202 } 203 if (!rank && bx == DMDA_BOUNDARY_PERIODIC && size > 1) { /* first processor sends first value to last */ 204 ierr = MPI_Send((void*)&array[j],1,MPIU_REAL,size-1,tag2,comm);CHKERRQ(ierr); 205 } 206 207 for (i=1; i<n; i++) { 208 #if !defined(PETSC_USE_COMPLEX) 209 ierr = PetscDrawLine(draw,xg[i-1],array[j+step*(i-1)],xg[i],array[j+step*i],PETSC_DRAW_RED);CHKERRQ(ierr); 210 #else 211 ierr = PetscDrawLine(draw,PetscRealPart(xg[i-1]),PetscRealPart(array[j+step*(i-1)]),PetscRealPart(xg[i]),PetscRealPart(array[j+step*i]),PETSC_DRAW_RED);CHKERRQ(ierr); 212 #endif 213 if (showpoints) { 214 ierr = PetscDrawPoint(draw,PetscRealPart(xg[i-1]),PetscRealPart(array[j+step*(i-1)]),PETSC_DRAW_BLACK);CHKERRQ(ierr); 215 } 216 } 217 if (rank) { /* receive value from left */ 218 ierr = MPI_Recv(&tmp,1,MPIU_REAL,rank-1,tag1,comm,&status);CHKERRQ(ierr); 219 ierr = MPI_Recv(&xgtmp,1,MPIU_REAL,rank-1,tag1,comm,&status);CHKERRQ(ierr); 220 #if !defined(PETSC_USE_COMPLEX) 221 ierr = PetscDrawLine(draw,xgtmp,tmp,xg[0],array[j],PETSC_DRAW_RED);CHKERRQ(ierr); 222 #else 223 ierr = PetscDrawLine(draw,xgtmp,tmp,PetscRealPart(xg[0]),PetscRealPart(array[j]),PETSC_DRAW_RED);CHKERRQ(ierr); 224 #endif 225 if (showpoints) { 226 ierr = PetscDrawPoint(draw,xgtmp,tmp,PETSC_DRAW_BLACK);CHKERRQ(ierr); 227 } 228 } 229 if (rank == size-1 && bx == DMDA_BOUNDARY_PERIODIC && size > 1) { 230 ierr = MPI_Recv(&tmp,1,MPIU_REAL,0,tag2,comm,&status);CHKERRQ(ierr); 231 #if !defined(PETSC_USE_COMPLEX) 232 ierr = PetscDrawLine(draw,xg[n-2],array[j+step*(n-1)],xg[n-1],tmp,PETSC_DRAW_RED);CHKERRQ(ierr); 233 #else 234 ierr = PetscDrawLine(draw,PetscRealPart(xg[n-2]),PetscRealPart(array[j+step*(n-1)]), 235 PetscRealPart(xg[n-1]),tmp,PETSC_DRAW_RED);CHKERRQ(ierr); 236 #endif 237 if (showpoints) { 238 ierr = PetscDrawPoint(draw,PetscRealPart(xg[n-2]),PetscRealPart(array[j+step*(n-1)]),PETSC_DRAW_BLACK);CHKERRQ(ierr); 239 } 240 } 241 ierr = PetscDrawSynchronizedFlush(draw);CHKERRQ(ierr); 242 ierr = PetscDrawPause(draw);CHKERRQ(ierr); 243 } 244 ierr = VecRestoreArrayRead(xcoor,&xg);CHKERRQ(ierr); 245 ierr = VecRestoreArrayRead(xin,&array);CHKERRQ(ierr); 246 PetscFunctionReturn(0); 247 } 248 249