Actual source code: gr1.c


  2: /*
  3:    Plots vectors obtained with DMDACreate1d()
  4: */

  6: #include <petsc/private/dmdaimpl.h>

  8: /*@
  9:     DMDASetUniformCoordinates - Sets a DMDA coordinates to be a uniform grid

 11:   Collective on da

 13:   Input Parameters:
 14: +  da - the distributed array object
 15: .  xmin,xmax - extremes in the x direction
 16: .  ymin,ymax - extremes in the y direction (value ignored for 1 dimensional problems)
 17: -  zmin,zmax - extremes in the z direction (value ignored for 1 or 2 dimensional problems)

 19:   Level: beginner

 21: .seealso: `DMSetCoordinates()`, `DMGetCoordinates()`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMStagSetUniformCoordinates()`

 23: @*/
 24: PetscErrorCode DMDASetUniformCoordinates(DM da, PetscReal xmin, PetscReal xmax, PetscReal ymin, PetscReal ymax, PetscReal zmin, PetscReal zmax)
 25: {
 26:   MPI_Comm       comm;
 27:   DM             cda;
 28:   DM_DA         *dd = (DM_DA *)da->data;
 29:   DMBoundaryType bx, by, bz;
 30:   Vec            xcoor;
 31:   PetscScalar   *coors;
 32:   PetscReal      hx, hy, hz_;
 33:   PetscInt       i, j, k, M, N, P, istart, isize, jstart, jsize, kstart, ksize, dim, cnt;

 37:   DMDAGetInfo(da, &dim, &M, &N, &P, NULL, NULL, NULL, NULL, NULL, &bx, &by, &bz, NULL);
 41:   PetscObjectGetComm((PetscObject)da, &comm);
 42:   DMDAGetCorners(da, &istart, &jstart, &kstart, &isize, &jsize, &ksize);
 43:   DMGetCoordinateDM(da, &cda);
 44:   DMCreateGlobalVector(cda, &xcoor);
 45:   if (dim == 1) {
 46:     if (bx == DM_BOUNDARY_PERIODIC) hx = (xmax - xmin) / M;
 47:     else hx = (xmax - xmin) / (M - 1);
 48:     VecGetArray(xcoor, &coors);
 49:     for (i = 0; i < isize; i++) coors[i] = xmin + hx * (i + istart);
 50:     VecRestoreArray(xcoor, &coors);
 51:   } else if (dim == 2) {
 52:     if (bx == DM_BOUNDARY_PERIODIC) hx = (xmax - xmin) / (M);
 53:     else hx = (xmax - xmin) / (M - 1);
 54:     if (by == DM_BOUNDARY_PERIODIC) hy = (ymax - ymin) / (N);
 55:     else hy = (ymax - ymin) / (N - 1);
 56:     VecGetArray(xcoor, &coors);
 57:     cnt = 0;
 58:     for (j = 0; j < jsize; j++) {
 59:       for (i = 0; i < isize; i++) {
 60:         coors[cnt++] = xmin + hx * (i + istart);
 61:         coors[cnt++] = ymin + hy * (j + jstart);
 62:       }
 63:     }
 64:     VecRestoreArray(xcoor, &coors);
 65:   } else if (dim == 3) {
 66:     if (bx == DM_BOUNDARY_PERIODIC) hx = (xmax - xmin) / (M);
 67:     else hx = (xmax - xmin) / (M - 1);
 68:     if (by == DM_BOUNDARY_PERIODIC) hy = (ymax - ymin) / (N);
 69:     else hy = (ymax - ymin) / (N - 1);
 70:     if (bz == DM_BOUNDARY_PERIODIC) hz_ = (zmax - zmin) / (P);
 71:     else hz_ = (zmax - zmin) / (P - 1);
 72:     VecGetArray(xcoor, &coors);
 73:     cnt = 0;
 74:     for (k = 0; k < ksize; k++) {
 75:       for (j = 0; j < jsize; j++) {
 76:         for (i = 0; i < isize; i++) {
 77:           coors[cnt++] = xmin + hx * (i + istart);
 78:           coors[cnt++] = ymin + hy * (j + jstart);
 79:           coors[cnt++] = zmin + hz_ * (k + kstart);
 80:         }
 81:       }
 82:     }
 83:     VecRestoreArray(xcoor, &coors);
 84:   } else SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_SUP, "Cannot create uniform coordinates for this dimension %" PetscInt_FMT, dim);
 85:   DMSetCoordinates(da, xcoor);
 86:   VecDestroy(&xcoor);
 87:   return 0;
 88: }

 90: /*
 91:     Allows a user to select a subset of the fields to be drawn by VecView() when the vector comes from a DMDA
 92: */
 93: PetscErrorCode DMDASelectFields(DM da, PetscInt *outfields, PetscInt **fields)
 94: {
 95:   PetscInt  step, ndisplayfields, *displayfields, k, j;
 96:   PetscBool flg;

 98:   DMDAGetInfo(da, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &step, NULL, NULL, NULL, NULL, NULL);
 99:   PetscMalloc1(step, &displayfields);
100:   for (k = 0; k < step; k++) displayfields[k] = k;
101:   ndisplayfields = step;
102:   PetscOptionsGetIntArray(NULL, NULL, "-draw_fields", displayfields, &ndisplayfields, &flg);
103:   if (!ndisplayfields) ndisplayfields = step;
104:   if (!flg) {
105:     char      **fields;
106:     const char *fieldname;
107:     PetscInt    nfields = step;
108:     PetscMalloc1(step, &fields);
109:     PetscOptionsGetStringArray(NULL, NULL, "-draw_fields_by_name", fields, &nfields, &flg);
110:     if (flg) {
111:       ndisplayfields = 0;
112:       for (k = 0; k < nfields; k++) {
113:         for (j = 0; j < step; j++) {
114:           DMDAGetFieldName(da, j, &fieldname);
115:           PetscStrcmp(fieldname, fields[k], &flg);
116:           if (flg) goto found;
117:         }
118:         SETERRQ(PetscObjectComm((PetscObject)da), PETSC_ERR_USER, "Unknown fieldname %s", fields[k]);
119:       found:
120:         displayfields[ndisplayfields++] = j;
121:       }
122:     }
123:     for (k = 0; k < nfields; k++) PetscFree(fields[k]);
124:     PetscFree(fields);
125:   }
126:   *fields    = displayfields;
127:   *outfields = ndisplayfields;
128:   return 0;
129: }

131: #include <petscdraw.h>

133: PetscErrorCode VecView_MPI_Draw_DA1d(Vec xin, PetscViewer v)
134: {
135:   DM                  da;
136:   PetscMPIInt         rank, size, tag;
137:   PetscInt            i, n, N, dof, istart, isize, j, nbounds;
138:   MPI_Status          status;
139:   PetscReal           min, max, xmin = 0.0, xmax = 0.0, tmp = 0.0, xgtmp = 0.0;
140:   const PetscScalar  *array, *xg;
141:   PetscDraw           draw;
142:   PetscBool           isnull, useports = PETSC_FALSE, showmarkers = PETSC_FALSE;
143:   MPI_Comm            comm;
144:   PetscDrawAxis       axis;
145:   Vec                 xcoor;
146:   DMBoundaryType      bx;
147:   const char         *tlabel = NULL, *xlabel = NULL;
148:   const PetscReal    *bounds;
149:   PetscInt           *displayfields;
150:   PetscInt            k, ndisplayfields;
151:   PetscBool           hold;
152:   PetscDrawViewPorts *ports = NULL;
153:   PetscViewerFormat   format;

155:   PetscViewerDrawGetDraw(v, 0, &draw);
156:   PetscDrawIsNull(draw, &isnull);
157:   if (isnull) return 0;
158:   PetscViewerDrawGetBounds(v, &nbounds, &bounds);

160:   VecGetDM(xin, &da);
162:   PetscObjectGetComm((PetscObject)xin, &comm);
163:   MPI_Comm_size(comm, &size);
164:   MPI_Comm_rank(comm, &rank);

166:   PetscOptionsGetBool(NULL, NULL, "-draw_vec_use_markers", &showmarkers, NULL);

168:   DMDAGetInfo(da, NULL, &N, NULL, NULL, NULL, NULL, NULL, &dof, NULL, &bx, NULL, NULL, NULL);
169:   DMDAGetCorners(da, &istart, NULL, NULL, &isize, NULL, NULL);
170:   VecGetArrayRead(xin, &array);
171:   VecGetLocalSize(xin, &n);
172:   n = n / dof;

174:   /* Get coordinates of nodes */
175:   DMGetCoordinates(da, &xcoor);
176:   if (!xcoor) {
177:     DMDASetUniformCoordinates(da, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0);
178:     DMGetCoordinates(da, &xcoor);
179:   }
180:   VecGetArrayRead(xcoor, &xg);
181:   DMDAGetCoordinateName(da, 0, &xlabel);

183:   /* Determine the min and max coordinate in plot */
184:   if (rank == 0) xmin = PetscRealPart(xg[0]);
185:   if (rank == size - 1) xmax = PetscRealPart(xg[n - 1]);
186:   MPI_Bcast(&xmin, 1, MPIU_REAL, 0, comm);
187:   MPI_Bcast(&xmax, 1, MPIU_REAL, size - 1, comm);

189:   DMDASelectFields(da, &ndisplayfields, &displayfields);
190:   PetscViewerGetFormat(v, &format);
191:   PetscOptionsGetBool(NULL, NULL, "-draw_ports", &useports, NULL);
192:   if (format == PETSC_VIEWER_DRAW_PORTS) useports = PETSC_TRUE;
193:   if (useports) {
194:     PetscViewerDrawGetDraw(v, 0, &draw);
195:     PetscViewerDrawGetDrawAxis(v, 0, &axis);
196:     PetscDrawCheckResizedWindow(draw);
197:     PetscDrawClear(draw);
198:     PetscDrawViewPortsCreate(draw, ndisplayfields, &ports);
199:   }

201:   /* Loop over each field; drawing each in a different window */
202:   for (k = 0; k < ndisplayfields; k++) {
203:     j = displayfields[k];

205:     /* determine the min and max value in plot */
206:     VecStrideMin(xin, j, NULL, &min);
207:     VecStrideMax(xin, j, NULL, &max);
208:     if (j < nbounds) {
209:       min = PetscMin(min, bounds[2 * j]);
210:       max = PetscMax(max, bounds[2 * j + 1]);
211:     }
212:     if (min == max) {
213:       min -= 1.e-5;
214:       max += 1.e-5;
215:     }

217:     if (useports) {
218:       PetscDrawViewPortsSet(ports, k);
219:       DMDAGetFieldName(da, j, &tlabel);
220:     } else {
221:       const char *title;
222:       PetscViewerDrawGetHold(v, &hold);
223:       PetscViewerDrawGetDraw(v, k, &draw);
224:       PetscViewerDrawGetDrawAxis(v, k, &axis);
225:       DMDAGetFieldName(da, j, &title);
226:       if (title) PetscDrawSetTitle(draw, title);
227:       PetscDrawCheckResizedWindow(draw);
228:       if (!hold) PetscDrawClear(draw);
229:     }
230:     PetscDrawAxisSetLabels(axis, tlabel, xlabel, NULL);
231:     PetscDrawAxisSetLimits(axis, xmin, xmax, min, max);
232:     PetscDrawAxisDraw(axis);

234:     /* draw local part of vector */
235:     PetscObjectGetNewTag((PetscObject)xin, &tag);
236:     if (rank < size - 1) { /*send value to right */
237:       MPI_Send((void *)&xg[n - 1], 1, MPIU_REAL, rank + 1, tag, comm);
238:       MPI_Send((void *)&array[j + (n - 1) * dof], 1, MPIU_REAL, rank + 1, tag, comm);
239:     }
240:     if (rank) { /* receive value from left */
241:       MPI_Recv(&xgtmp, 1, MPIU_REAL, rank - 1, tag, comm, &status);
242:       MPI_Recv(&tmp, 1, MPIU_REAL, rank - 1, tag, comm, &status);
243:     }
244:     PetscDrawCollectiveBegin(draw);
245:     if (rank) {
246:       PetscDrawLine(draw, xgtmp, tmp, PetscRealPart(xg[0]), PetscRealPart(array[j]), PETSC_DRAW_RED);
247:       if (showmarkers) PetscDrawPoint(draw, xgtmp, tmp, PETSC_DRAW_BLACK);
248:     }
249:     for (i = 1; i < n; i++) {
250:       PetscDrawLine(draw, PetscRealPart(xg[i - 1]), PetscRealPart(array[j + dof * (i - 1)]), PetscRealPart(xg[i]), PetscRealPart(array[j + dof * i]), PETSC_DRAW_RED);
251:       if (showmarkers) PetscDrawMarker(draw, PetscRealPart(xg[i - 1]), PetscRealPart(array[j + dof * (i - 1)]), PETSC_DRAW_BLACK);
252:     }
253:     if (rank == size - 1) {
254:       if (showmarkers) PetscDrawMarker(draw, PetscRealPart(xg[n - 1]), PetscRealPart(array[j + dof * (n - 1)]), PETSC_DRAW_BLACK);
255:     }
256:     PetscDrawCollectiveEnd(draw);
257:     PetscDrawFlush(draw);
258:     PetscDrawPause(draw);
259:     if (!useports) PetscDrawSave(draw);
260:   }
261:   if (useports) {
262:     PetscViewerDrawGetDraw(v, 0, &draw);
263:     PetscDrawSave(draw);
264:   }

266:   PetscDrawViewPortsDestroy(ports);
267:   PetscFree(displayfields);
268:   VecRestoreArrayRead(xcoor, &xg);
269:   VecRestoreArrayRead(xin, &array);
270:   return 0;
271: }