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 SETERRQ1(((PetscObject)da)->comm,PETSC_ERR_SUP,"Cannot create uniform coordinates for this dimension %D\n",dim); 90 ierr = DMDASetCoordinates(da,xcoor);CHKERRQ(ierr); 91 ierr = PetscLogObjectParent(da,xcoor);CHKERRQ(ierr); 92 ierr = VecDestroy(&xcoor);CHKERRQ(ierr); 93 PetscFunctionReturn(0); 94 } 95 96 #undef __FUNCT__ 97 #define __FUNCT__ "VecView_MPI_Draw_DA1d" 98 PetscErrorCode VecView_MPI_Draw_DA1d(Vec xin,PetscViewer v) 99 { 100 DM da; 101 PetscErrorCode ierr; 102 PetscMPIInt rank,size,tag1,tag2; 103 PetscInt i,n,N,step,istart,isize,j,nbounds; 104 MPI_Status status; 105 PetscReal coors[4],ymin,ymax,min,max,xmin,xmax,tmp,xgtmp; 106 const PetscScalar *array,*xg; 107 PetscDraw draw; 108 PetscBool isnull,showpoints = PETSC_FALSE; 109 MPI_Comm comm; 110 PetscDrawAxis axis; 111 Vec xcoor; 112 DMDABoundaryType bx; 113 const PetscReal *bounds; 114 115 PetscFunctionBegin; 116 ierr = PetscViewerDrawGetDraw(v,0,&draw);CHKERRQ(ierr); 117 ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); 118 ierr = PetscViewerDrawGetBounds(v,&nbounds,&bounds);CHKERRQ(ierr); 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 if (j < nbounds) { 177 min = PetscMin(min,bounds[2*j]); 178 max = PetscMax(max,bounds[2*j+1]); 179 } 180 181 ierr = MPI_Reduce(&min,&ymin,1,MPIU_REAL,MPIU_MIN,0,comm);CHKERRQ(ierr); 182 ierr = MPI_Reduce(&max,&ymax,1,MPIU_REAL,MPIU_MAX,0,comm);CHKERRQ(ierr); 183 184 ierr = PetscDrawSynchronizedClear(draw);CHKERRQ(ierr); 185 ierr = PetscViewerDrawGetDrawAxis(v,j,&axis);CHKERRQ(ierr); 186 ierr = PetscLogObjectParent(draw,axis);CHKERRQ(ierr); 187 if (!rank) { 188 const char *title; 189 190 ierr = PetscDrawAxisSetLimits(axis,xmin,xmax,ymin,ymax);CHKERRQ(ierr); 191 ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr); 192 ierr = PetscDrawGetCoordinates(draw,coors,coors+1,coors+2,coors+3);CHKERRQ(ierr); 193 ierr = DMDAGetFieldName(da,j,&title);CHKERRQ(ierr); 194 if (title) {ierr = PetscDrawSetTitle(draw,title);CHKERRQ(ierr);} 195 } 196 ierr = MPI_Bcast(coors,4,MPIU_REAL,0,comm);CHKERRQ(ierr); 197 if (rank) { 198 ierr = PetscDrawSetCoordinates(draw,coors[0],coors[1],coors[2],coors[3]);CHKERRQ(ierr); 199 } 200 201 /* draw local part of vector */ 202 PetscObjectGetNewTag((PetscObject)xin,&tag1);CHKERRQ(ierr); 203 PetscObjectGetNewTag((PetscObject)xin,&tag2);CHKERRQ(ierr); 204 if (rank < size-1) { /*send value to right */ 205 ierr = MPI_Send((void*)&array[j+(n-1)*step],1,MPIU_REAL,rank+1,tag1,comm);CHKERRQ(ierr); 206 ierr = MPI_Send((void*)&xg[n-1],1,MPIU_REAL,rank+1,tag1,comm);CHKERRQ(ierr); 207 } 208 if (!rank && bx == DMDA_BOUNDARY_PERIODIC && size > 1) { /* first processor sends first value to last */ 209 ierr = MPI_Send((void*)&array[j],1,MPIU_REAL,size-1,tag2,comm);CHKERRQ(ierr); 210 } 211 212 for (i=1; i<n; i++) { 213 #if !defined(PETSC_USE_COMPLEX) 214 ierr = PetscDrawLine(draw,xg[i-1],array[j+step*(i-1)],xg[i],array[j+step*i],PETSC_DRAW_RED);CHKERRQ(ierr); 215 #else 216 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); 217 #endif 218 if (showpoints) { 219 ierr = PetscDrawPoint(draw,PetscRealPart(xg[i-1]),PetscRealPart(array[j+step*(i-1)]),PETSC_DRAW_BLACK);CHKERRQ(ierr); 220 } 221 } 222 if (rank) { /* receive value from left */ 223 ierr = MPI_Recv(&tmp,1,MPIU_REAL,rank-1,tag1,comm,&status);CHKERRQ(ierr); 224 ierr = MPI_Recv(&xgtmp,1,MPIU_REAL,rank-1,tag1,comm,&status);CHKERRQ(ierr); 225 #if !defined(PETSC_USE_COMPLEX) 226 ierr = PetscDrawLine(draw,xgtmp,tmp,xg[0],array[j],PETSC_DRAW_RED);CHKERRQ(ierr); 227 #else 228 ierr = PetscDrawLine(draw,xgtmp,tmp,PetscRealPart(xg[0]),PetscRealPart(array[j]),PETSC_DRAW_RED);CHKERRQ(ierr); 229 #endif 230 if (showpoints) { 231 ierr = PetscDrawPoint(draw,xgtmp,tmp,PETSC_DRAW_BLACK);CHKERRQ(ierr); 232 } 233 } 234 if (rank == size-1 && bx == DMDA_BOUNDARY_PERIODIC && size > 1) { 235 ierr = MPI_Recv(&tmp,1,MPIU_REAL,0,tag2,comm,&status);CHKERRQ(ierr); 236 #if !defined(PETSC_USE_COMPLEX) 237 ierr = PetscDrawLine(draw,xg[n-2],array[j+step*(n-1)],xg[n-1],tmp,PETSC_DRAW_RED);CHKERRQ(ierr); 238 #else 239 ierr = PetscDrawLine(draw,PetscRealPart(xg[n-2]),PetscRealPart(array[j+step*(n-1)]), 240 PetscRealPart(xg[n-1]),tmp,PETSC_DRAW_RED);CHKERRQ(ierr); 241 #endif 242 if (showpoints) { 243 ierr = PetscDrawPoint(draw,PetscRealPart(xg[n-2]),PetscRealPart(array[j+step*(n-1)]),PETSC_DRAW_BLACK);CHKERRQ(ierr); 244 } 245 } 246 ierr = PetscDrawSynchronizedFlush(draw);CHKERRQ(ierr); 247 ierr = PetscDrawPause(draw);CHKERRQ(ierr); 248 } 249 ierr = VecRestoreArrayRead(xcoor,&xg);CHKERRQ(ierr); 250 ierr = VecRestoreArrayRead(xin,&array);CHKERRQ(ierr); 251 PetscFunctionReturn(0); 252 } 253 254