1 2 /* 3 Plots vectors obtained with DMDACreate1d() 4 */ 5 6 #include <petsc/private/dmdaimpl.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 (value ignored for 1 dimensional problems) 19 - zmin,zmax - extremes in the z direction (value ignored for 1 or 2 dimensional problems) 20 21 Level: beginner 22 23 .seealso: DMSetCoordinates(), DMGetCoordinates(), 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 PetscSection section; 30 DM cda; 31 DMBoundaryType bx,by,bz; 32 Vec xcoor; 33 PetscScalar *coors; 34 PetscReal hx,hy,hz_; 35 PetscInt i,j,k,M,N,P,istart,isize,jstart,jsize,kstart,ksize,dim,cnt; 36 PetscErrorCode ierr; 37 38 PetscFunctionBegin; 39 PetscValidHeaderSpecific(da,DM_CLASSID,1); 40 ierr = DMDAGetInfo(da,&dim,&M,&N,&P,0,0,0,0,0,&bx,&by,&bz,0);CHKERRQ(ierr); 41 if (xmax < xmin) SETERRQ2(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_INCOMP,"xmax must be larger than xmin %g %g",(double)xmin,(double)xmax); 42 if ((ymax < ymin) && (dim > 1)) SETERRQ2(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_INCOMP,"ymax must be larger than ymin %g %g",(double)ymin,(double)ymax); 43 if ((zmax < zmin) && (dim > 2)) SETERRQ2(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_INCOMP,"zmax must be larger than zmin %g %g",(double)zmin,(double)zmax); 44 ierr = PetscObjectGetComm((PetscObject)da,&comm);CHKERRQ(ierr); 45 ierr = DMGetDefaultSection(da,§ion);CHKERRQ(ierr); 46 ierr = DMDAGetCorners(da,&istart,&jstart,&kstart,&isize,&jsize,&ksize);CHKERRQ(ierr); 47 ierr = DMGetCoordinateDM(da, &cda);CHKERRQ(ierr); 48 if (section) { 49 /* This would be better as a vector, but this is compatible */ 50 PetscInt numComp[3] = {1, 1, 1}; 51 PetscInt numVertexDof[3] = {1, 1, 1}; 52 53 ierr = DMDASetFieldName(cda, 0, "x");CHKERRQ(ierr); 54 if (dim > 1) {ierr = DMDASetFieldName(cda, 1, "y");CHKERRQ(ierr);} 55 if (dim > 2) {ierr = DMDASetFieldName(cda, 2, "z");CHKERRQ(ierr);} 56 ierr = DMDACreateSection(cda, numComp, numVertexDof, NULL, NULL);CHKERRQ(ierr); 57 } 58 ierr = DMCreateGlobalVector(cda, &xcoor);CHKERRQ(ierr); 59 if (section) { 60 PetscSection csection; 61 PetscInt vStart, vEnd; 62 63 ierr = DMGetDefaultGlobalSection(cda,&csection);CHKERRQ(ierr); 64 ierr = VecGetArray(xcoor,&coors);CHKERRQ(ierr); 65 ierr = DMDAGetHeightStratum(da, dim, &vStart, &vEnd);CHKERRQ(ierr); 66 if (bx == DM_BOUNDARY_PERIODIC) hx = (xmax-xmin)/(M+1); 67 else hx = (xmax-xmin)/(M ? M : 1); 68 if (by == DM_BOUNDARY_PERIODIC) hy = (ymax-ymin)/(N+1); 69 else hy = (ymax-ymin)/(N ? N : 1); 70 if (bz == DM_BOUNDARY_PERIODIC) hz_ = (zmax-zmin)/(P+1); 71 else hz_ = (zmax-zmin)/(P ? P : 1); 72 switch (dim) { 73 case 1: 74 for (i = 0; i < isize+1; ++i) { 75 PetscInt v = i+vStart, dof, off; 76 77 ierr = PetscSectionGetDof(csection, v, &dof);CHKERRQ(ierr); 78 ierr = PetscSectionGetOffset(csection, v, &off);CHKERRQ(ierr); 79 if (off >= 0) { 80 coors[off] = xmin + hx*(i+istart); 81 } 82 } 83 break; 84 case 2: 85 for (j = 0; j < jsize+1; ++j) { 86 for (i = 0; i < isize+1; ++i) { 87 PetscInt v = j*(isize+1)+i+vStart, dof, off; 88 89 ierr = PetscSectionGetDof(csection, v, &dof);CHKERRQ(ierr); 90 ierr = PetscSectionGetOffset(csection, v, &off);CHKERRQ(ierr); 91 if (off >= 0) { 92 coors[off+0] = xmin + hx*(i+istart); 93 coors[off+1] = ymin + hy*(j+jstart); 94 } 95 } 96 } 97 break; 98 case 3: 99 for (k = 0; k < ksize+1; ++k) { 100 for (j = 0; j < jsize+1; ++j) { 101 for (i = 0; i < isize+1; ++i) { 102 PetscInt v = (k*(jsize+1)+j)*(isize+1)+i+vStart, dof, off; 103 104 ierr = PetscSectionGetDof(csection, v, &dof);CHKERRQ(ierr); 105 ierr = PetscSectionGetOffset(csection, v, &off);CHKERRQ(ierr); 106 if (off >= 0) { 107 coors[off+0] = xmin + hx*(i+istart); 108 coors[off+1] = ymin + hy*(j+jstart); 109 coors[off+2] = zmin + hz_*(k+kstart); 110 } 111 } 112 } 113 } 114 break; 115 default: 116 SETERRQ1(PetscObjectComm((PetscObject)da),PETSC_ERR_SUP,"Cannot create uniform coordinates for this dimension %D\n",dim); 117 } 118 ierr = VecRestoreArray(xcoor,&coors);CHKERRQ(ierr); 119 ierr = DMSetCoordinates(da,xcoor);CHKERRQ(ierr); 120 ierr = PetscLogObjectParent((PetscObject)da,(PetscObject)xcoor);CHKERRQ(ierr); 121 ierr = VecDestroy(&xcoor);CHKERRQ(ierr); 122 PetscFunctionReturn(0); 123 } 124 if (dim == 1) { 125 if (bx == DM_BOUNDARY_PERIODIC) hx = (xmax-xmin)/M; 126 else hx = (xmax-xmin)/(M-1); 127 ierr = VecGetArray(xcoor,&coors);CHKERRQ(ierr); 128 for (i=0; i<isize; i++) { 129 coors[i] = xmin + hx*(i+istart); 130 } 131 ierr = VecRestoreArray(xcoor,&coors);CHKERRQ(ierr); 132 } else if (dim == 2) { 133 if (bx == DM_BOUNDARY_PERIODIC) hx = (xmax-xmin)/(M); 134 else hx = (xmax-xmin)/(M-1); 135 if (by == DM_BOUNDARY_PERIODIC) hy = (ymax-ymin)/(N); 136 else hy = (ymax-ymin)/(N-1); 137 ierr = VecGetArray(xcoor,&coors);CHKERRQ(ierr); 138 cnt = 0; 139 for (j=0; j<jsize; j++) { 140 for (i=0; i<isize; i++) { 141 coors[cnt++] = xmin + hx*(i+istart); 142 coors[cnt++] = ymin + hy*(j+jstart); 143 } 144 } 145 ierr = VecRestoreArray(xcoor,&coors);CHKERRQ(ierr); 146 } else if (dim == 3) { 147 if (bx == DM_BOUNDARY_PERIODIC) hx = (xmax-xmin)/(M); 148 else hx = (xmax-xmin)/(M-1); 149 if (by == DM_BOUNDARY_PERIODIC) hy = (ymax-ymin)/(N); 150 else hy = (ymax-ymin)/(N-1); 151 if (bz == DM_BOUNDARY_PERIODIC) hz_ = (zmax-zmin)/(P); 152 else hz_ = (zmax-zmin)/(P-1); 153 ierr = VecGetArray(xcoor,&coors);CHKERRQ(ierr); 154 cnt = 0; 155 for (k=0; k<ksize; k++) { 156 for (j=0; j<jsize; j++) { 157 for (i=0; i<isize; i++) { 158 coors[cnt++] = xmin + hx*(i+istart); 159 coors[cnt++] = ymin + hy*(j+jstart); 160 coors[cnt++] = zmin + hz_*(k+kstart); 161 } 162 } 163 } 164 ierr = VecRestoreArray(xcoor,&coors);CHKERRQ(ierr); 165 } else SETERRQ1(PetscObjectComm((PetscObject)da),PETSC_ERR_SUP,"Cannot create uniform coordinates for this dimension %D\n",dim); 166 ierr = DMSetCoordinates(da,xcoor);CHKERRQ(ierr); 167 ierr = PetscLogObjectParent((PetscObject)da,(PetscObject)xcoor);CHKERRQ(ierr); 168 ierr = VecDestroy(&xcoor);CHKERRQ(ierr); 169 PetscFunctionReturn(0); 170 } 171 172 #undef __FUNCT__ 173 #define __FUNCT__ "DMDASelectFields" 174 /* 175 Allows a user to select a subset of the fields to be drawn by VecView() when the vector comes from a DMDA 176 */ 177 PetscErrorCode DMDASelectFields(DM da,PetscInt *outfields,PetscInt **fields) 178 { 179 PetscErrorCode ierr; 180 PetscInt step,ndisplayfields,*displayfields,k,j; 181 PetscBool flg; 182 183 PetscFunctionBegin; 184 ierr = DMDAGetInfo(da,0,0,0,0,0,0,0,&step,0,0,0,0,0);CHKERRQ(ierr); 185 ierr = PetscMalloc1(step,&displayfields);CHKERRQ(ierr); 186 for (k=0; k<step; k++) displayfields[k] = k; 187 ndisplayfields = step; 188 ierr = PetscOptionsGetIntArray(NULL,NULL,"-draw_fields",displayfields,&ndisplayfields,&flg);CHKERRQ(ierr); 189 if (!ndisplayfields) ndisplayfields = step; 190 if (!flg) { 191 char **fields; 192 const char *fieldname; 193 PetscInt nfields = step; 194 ierr = PetscMalloc1(step,&fields);CHKERRQ(ierr); 195 ierr = PetscOptionsGetStringArray(NULL,NULL,"-draw_fields_by_name",fields,&nfields,&flg);CHKERRQ(ierr); 196 if (flg) { 197 ndisplayfields = 0; 198 for (k=0; k<nfields;k++) { 199 for (j=0; j<step; j++) { 200 ierr = DMDAGetFieldName(da,j,&fieldname);CHKERRQ(ierr); 201 ierr = PetscStrcmp(fieldname,fields[k],&flg);CHKERRQ(ierr); 202 if (flg) { 203 goto found; 204 } 205 } 206 SETERRQ1(PetscObjectComm((PetscObject)da),PETSC_ERR_USER,"Unknown fieldname %s",fields[k]); 207 found: displayfields[ndisplayfields++] = j; 208 } 209 } 210 for (k=0; k<nfields; k++) { 211 ierr = PetscFree(fields[k]);CHKERRQ(ierr); 212 } 213 ierr = PetscFree(fields);CHKERRQ(ierr); 214 } 215 *fields = displayfields; 216 *outfields = ndisplayfields; 217 PetscFunctionReturn(0); 218 } 219 220 #include <petscdraw.h> 221 222 #undef __FUNCT__ 223 #define __FUNCT__ "VecView_MPI_Draw_DA1d" 224 PetscErrorCode VecView_MPI_Draw_DA1d(Vec xin,PetscViewer v) 225 { 226 DM da; 227 PetscErrorCode ierr; 228 PetscMPIInt rank,size,tag; 229 PetscInt i,n,N,dof,istart,isize,j,nbounds; 230 MPI_Status status; 231 PetscReal min,max,xmin = 0.0,xmax = 0.0,tmp = 0.0,xgtmp = 0.0; 232 const PetscScalar *array,*xg; 233 PetscDraw draw; 234 PetscBool isnull,useports = PETSC_FALSE,showmarkers = PETSC_FALSE; 235 MPI_Comm comm; 236 PetscDrawAxis axis; 237 Vec xcoor; 238 DMBoundaryType bx; 239 const char *tlabel = NULL,*xlabel = NULL; 240 const PetscReal *bounds; 241 PetscInt *displayfields; 242 PetscInt k,ndisplayfields; 243 PetscBool hold; 244 PetscDrawViewPorts *ports = NULL; 245 PetscViewerFormat format; 246 247 PetscFunctionBegin; 248 ierr = PetscViewerDrawGetDraw(v,0,&draw);CHKERRQ(ierr); 249 ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); 250 if (isnull) PetscFunctionReturn(0); 251 ierr = PetscViewerDrawGetBounds(v,&nbounds,&bounds);CHKERRQ(ierr); 252 253 ierr = VecGetDM(xin,&da);CHKERRQ(ierr); 254 if (!da) SETERRQ(PetscObjectComm((PetscObject)xin),PETSC_ERR_ARG_WRONG,"Vector not generated from a DMDA"); 255 ierr = PetscObjectGetComm((PetscObject)xin,&comm);CHKERRQ(ierr); 256 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 257 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 258 259 ierr = PetscOptionsGetBool(NULL,NULL,"-draw_vec_use_markers",&showmarkers,NULL);CHKERRQ(ierr); 260 261 ierr = DMDAGetInfo(da,NULL,&N,NULL,NULL,NULL,NULL,NULL,&dof,NULL,&bx,NULL,NULL,NULL);CHKERRQ(ierr); 262 ierr = DMDAGetCorners(da,&istart,NULL,NULL,&isize,NULL,NULL);CHKERRQ(ierr); 263 ierr = VecGetArrayRead(xin,&array);CHKERRQ(ierr); 264 ierr = VecGetLocalSize(xin,&n);CHKERRQ(ierr); 265 n = n/dof; 266 267 /* Get coordinates of nodes */ 268 ierr = DMGetCoordinates(da,&xcoor);CHKERRQ(ierr); 269 if (!xcoor) { 270 ierr = DMDASetUniformCoordinates(da,0.0,1.0,0.0,0.0,0.0,0.0);CHKERRQ(ierr); 271 ierr = DMGetCoordinates(da,&xcoor);CHKERRQ(ierr); 272 } 273 ierr = VecGetArrayRead(xcoor,&xg);CHKERRQ(ierr); 274 ierr = DMDAGetCoordinateName(da,0,&xlabel);CHKERRQ(ierr); 275 276 /* Determine the min and max coordinate in plot */ 277 if (!rank) xmin = PetscRealPart(xg[0]); 278 if (rank == size-1) xmax = PetscRealPart(xg[n-1]); 279 ierr = MPI_Bcast(&xmin,1,MPIU_REAL,0,comm);CHKERRQ(ierr); 280 ierr = MPI_Bcast(&xmax,1,MPIU_REAL,size-1,comm);CHKERRQ(ierr); 281 282 ierr = DMDASelectFields(da,&ndisplayfields,&displayfields);CHKERRQ(ierr); 283 ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr); 284 ierr = PetscOptionsGetBool(NULL,NULL,"-draw_ports",&useports,NULL);CHKERRQ(ierr); 285 if (format == PETSC_VIEWER_DRAW_PORTS) useports = PETSC_TRUE; 286 if (useports) { 287 ierr = PetscViewerDrawGetDraw(v,0,&draw);CHKERRQ(ierr); 288 ierr = PetscViewerDrawGetDrawAxis(v,0,&axis);CHKERRQ(ierr); 289 ierr = PetscDrawCheckResizedWindow(draw);CHKERRQ(ierr); 290 ierr = PetscDrawClear(draw);CHKERRQ(ierr); 291 ierr = PetscDrawViewPortsCreate(draw,ndisplayfields,&ports);CHKERRQ(ierr); 292 } 293 294 /* Loop over each field; drawing each in a different window */ 295 for (k=0; k<ndisplayfields; k++) { 296 j = displayfields[k]; 297 298 /* determine the min and max value in plot */ 299 ierr = VecStrideMin(xin,j,NULL,&min);CHKERRQ(ierr); 300 ierr = VecStrideMax(xin,j,NULL,&max);CHKERRQ(ierr); 301 if (j < nbounds) { 302 min = PetscMin(min,bounds[2*j]); 303 max = PetscMax(max,bounds[2*j+1]); 304 } 305 if (min == max) { 306 min -= 1.e-5; 307 max += 1.e-5; 308 } 309 310 if (useports) { 311 ierr = PetscDrawViewPortsSet(ports,k);CHKERRQ(ierr); 312 ierr = DMDAGetFieldName(da,j,&tlabel);CHKERRQ(ierr); 313 } else { 314 const char *title; 315 ierr = PetscViewerDrawGetHold(v,&hold);CHKERRQ(ierr); 316 ierr = PetscViewerDrawGetDraw(v,k,&draw);CHKERRQ(ierr); 317 ierr = PetscViewerDrawGetDrawAxis(v,k,&axis);CHKERRQ(ierr); 318 ierr = DMDAGetFieldName(da,j,&title);CHKERRQ(ierr); 319 if (title) {ierr = PetscDrawSetTitle(draw,title);CHKERRQ(ierr);} 320 ierr = PetscDrawCheckResizedWindow(draw);CHKERRQ(ierr); 321 if (!hold) {ierr = PetscDrawClear(draw);CHKERRQ(ierr);} 322 } 323 ierr = PetscDrawAxisSetLabels(axis,tlabel,xlabel,NULL);CHKERRQ(ierr); 324 ierr = PetscDrawAxisSetLimits(axis,xmin,xmax,min,max);CHKERRQ(ierr); 325 ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr); 326 327 /* draw local part of vector */ 328 ierr = PetscObjectGetNewTag((PetscObject)xin,&tag);CHKERRQ(ierr); 329 if (rank < size-1) { /*send value to right */ 330 ierr = MPI_Send((void*)&xg[n-1],1,MPIU_REAL,rank+1,tag,comm);CHKERRQ(ierr); 331 ierr = MPI_Send((void*)&array[j+(n-1)*dof],1,MPIU_REAL,rank+1,tag,comm);CHKERRQ(ierr); 332 } 333 if (rank) { /* receive value from left */ 334 ierr = MPI_Recv(&xgtmp,1,MPIU_REAL,rank-1,tag,comm,&status);CHKERRQ(ierr); 335 ierr = MPI_Recv(&tmp,1,MPIU_REAL,rank-1,tag,comm,&status);CHKERRQ(ierr); 336 } 337 ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr); 338 if (rank) { 339 ierr = PetscDrawLine(draw,xgtmp,tmp,PetscRealPart(xg[0]),PetscRealPart(array[j]),PETSC_DRAW_RED);CHKERRQ(ierr); 340 if (showmarkers) {ierr = PetscDrawPoint(draw,xgtmp,tmp,PETSC_DRAW_BLACK);CHKERRQ(ierr);} 341 } 342 for (i=1; i<n; i++) { 343 ierr = PetscDrawLine(draw,PetscRealPart(xg[i-1]),PetscRealPart(array[j+dof*(i-1)]),PetscRealPart(xg[i]),PetscRealPart(array[j+dof*i]),PETSC_DRAW_RED);CHKERRQ(ierr); 344 if (showmarkers) {ierr = PetscDrawMarker(draw,PetscRealPart(xg[i-1]),PetscRealPart(array[j+dof*(i-1)]),PETSC_DRAW_BLACK);CHKERRQ(ierr);} 345 } 346 if (rank == size-1) { 347 if (showmarkers) {ierr = PetscDrawMarker(draw,PetscRealPart(xg[n-1]),PetscRealPart(array[j+dof*(n-1)]),PETSC_DRAW_BLACK);CHKERRQ(ierr);} 348 } 349 ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr); 350 ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 351 ierr = PetscDrawPause(draw);CHKERRQ(ierr); 352 if (!useports) {ierr = PetscDrawSave(draw);CHKERRQ(ierr);} 353 } 354 if (useports) { 355 ierr = PetscViewerDrawGetDraw(v,0,&draw);CHKERRQ(ierr); 356 ierr = PetscDrawSave(draw);CHKERRQ(ierr); 357 } 358 359 ierr = PetscDrawViewPortsDestroy(ports);CHKERRQ(ierr); 360 ierr = PetscFree(displayfields);CHKERRQ(ierr); 361 ierr = VecRestoreArrayRead(xcoor,&xg);CHKERRQ(ierr); 362 ierr = VecRestoreArrayRead(xin,&array);CHKERRQ(ierr); 363 PetscFunctionReturn(0); 364 } 365 366