Actual source code: dviewp.c


  2: /*
  3:        Provides the calling sequences for all the basic PetscDraw routines.
  4: */
  5: #include <petsc/private/drawimpl.h>

  7: /*@
  8:    PetscDrawSetViewPort - Sets the portion of the window (page) to which draw
  9:    routines will write.

 11:    Collective on PetscDraw

 13:    Input Parameters:
 14: +  xl - the horizontal coordinate of the lower left corner of the subwindow.
 15: .  yl - the vertical coordinate of the lower left corner of the subwindow.
 16: .  xr - the horizontal coordinate of the upper right corner of the subwindow.
 17: .  yr - the vertical coordinate of the upper right corner of the subwindow.
 18: -  draw - the drawing context

 20:    Notes:
 21:    These numbers must always be between 0.0 and 1.0.
 22:    Lower left corner is (0,0).

 24:    Level: advanced

 26: @*/
 27: PetscErrorCode  PetscDrawSetViewPort(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr)
 28: {
 31:   draw->port_xl = xl; draw->port_yl = yl;
 32:   draw->port_xr = xr; draw->port_yr = yr;
 33:   if (draw->ops->setviewport) {
 34:     (*draw->ops->setviewport)(draw,xl,yl,xr,yr);
 35:   }
 36:   return 0;
 37: }

 39: /*@
 40:    PetscDrawGetViewPort - Gets the portion of the window (page) to which draw
 41:    routines will write.

 43:    Collective on PetscDraw

 45:    Input Parameter:
 46: .  draw - the drawing context

 48:    Output Parameters:
 49: +  xl - the horizontal coordinate of the lower left corner of the subwindow.
 50: .  yl - the vertical coordinate of the lower left corner of the subwindow.
 51: .  xr - the horizontal coordinate of the upper right corner of the subwindow.
 52: -  yr - the vertical coordinate of the upper right corner of the subwindow.

 54:    Notes:
 55:    These numbers must always be between 0.0 and 1.0.
 56:    Lower left corner is (0,0).

 58:    Level: advanced

 60: @*/
 61: PetscErrorCode  PetscDrawGetViewPort(PetscDraw draw,PetscReal *xl,PetscReal *yl,PetscReal *xr,PetscReal *yr)
 62: {
 68:   *xl = draw->port_xl;
 69:   *yl = draw->port_yl;
 70:   *xr = draw->port_xr;
 71:   *yr = draw->port_yr;
 72:   return 0;
 73: }

 75: /*@
 76:    PetscDrawSplitViewPort - Splits a window shared by several processes into smaller
 77:    view ports. One for each process.

 79:    Collective on PetscDraw

 81:    Input Parameter:
 82: .  draw - the drawing context

 84:    Level: advanced

 86: .seealso: PetscDrawDivideViewPort(), PetscDrawSetViewPort()

 88: @*/
 89: PetscErrorCode  PetscDrawSplitViewPort(PetscDraw draw)
 90: {
 91:   PetscMPIInt    rank,size;
 92:   PetscInt       n;
 93:   PetscBool      isnull;
 94:   PetscReal      xl,xr,yl,yr,h;

 98:   PetscDrawIsNull(draw,&isnull);
 99:   if (isnull) return 0;
100:   MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);
101:   MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size);

103:   n = (PetscInt)(.1 + PetscSqrtReal((PetscReal)size));
104:   while (n*n < size) n++;

106:   h  = 1.0/n;
107:   xl = (rank % n)*h;
108:   xr = xl + h;
109:   yl = (rank / n)*h;
110:   yr = yl + h;

112:   PetscDrawCollectiveBegin(draw);
113:   PetscDrawLine(draw,xl,yl,xl,yr,PETSC_DRAW_BLACK);
114:   PetscDrawLine(draw,xl,yr,xr,yr,PETSC_DRAW_BLACK);
115:   PetscDrawLine(draw,xr,yr,xr,yl,PETSC_DRAW_BLACK);
116:   PetscDrawLine(draw,xr,yl,xl,yl,PETSC_DRAW_BLACK);
117:   PetscDrawCollectiveEnd(draw);
118:   PetscDrawFlush(draw);

120:   draw->port_xl = xl + .05*h;
121:   draw->port_xr = xr - .05*h;
122:   draw->port_yl = yl + .05*h;
123:   draw->port_yr = yr - .05*h;

125:   if (draw->ops->setviewport) (*draw->ops->setviewport)(draw,xl,yl,xr,yr);
126:   return 0;
127: }

129: /*@C
130:    PetscDrawViewPortsCreate - Splits a window into smaller view ports. Each processor shares all the viewports.

132:    Collective on PetscDraw

134:    Input Parameters:
135: +  draw - the drawing context
136: -  nports - the number of ports

138:    Output Parameter:
139: .  ports - a PetscDrawViewPorts context (C structure)

141:    Options Database:
142: .  -draw_ports - display multiple fields in the same window with PetscDrawPorts() instead of in separate windows

144:    Level: advanced

146: .seealso: PetscDrawSplitViewPort(), PetscDrawSetViewPort(), PetscDrawViewPortsSet(), PetscDrawViewPortsDestroy()

148: @*/
149: PetscErrorCode  PetscDrawViewPortsCreate(PetscDraw draw,PetscInt nports,PetscDrawViewPorts **newports)
150: {
151:   PetscDrawViewPorts *ports;
152:   PetscInt           i,n;
153:   PetscBool          isnull;
154:   PetscMPIInt        rank;
155:   PetscReal          *xl,*xr,*yl,*yr,h;
156:   PetscErrorCode     ierr;

161:   PetscDrawIsNull(draw,&isnull);
162:   if (isnull) {*newports = NULL; return 0;}
163:   MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);

165:   PetscNew(&ports); *newports = ports;
166:   ports->draw = draw;
167:   ports->nports = nports;
168:   PetscObjectReference((PetscObject)draw);
169:   /* save previous drawport of window */
170:   PetscDrawGetViewPort(draw,&ports->port_xl,&ports->port_yl,&ports->port_xr,&ports->port_yr);

172:   n = (PetscInt)(.1 + PetscSqrtReal((PetscReal)nports));
173:   while (n*n < nports) n++;
174:   h = 1.0/n;

176:   PetscMalloc4(n*n,&xl,n*n,&xr,n*n,&yl,n*n,&yr);
177:   ports->xl = xl;
178:   ports->xr = xr;
179:   ports->yl = yl;
180:   ports->yr = yr;

182:   PetscDrawSetCoordinates(draw,0.0,0.0,1.0,1.0);
183:   PetscDrawCollectiveBegin(draw);
184:   for (i=0; i<n*n; i++) {
185:     xl[i] = (i % n)*h;
186:     xr[i] = xl[i] + h;
187:     yl[i] = (i / n)*h;
188:     yr[i] = yl[i] + h;

190:     if (rank == 0) {
191:       PetscDrawLine(draw,xl[i],yl[i],xl[i],yr[i],PETSC_DRAW_BLACK);
192:       PetscDrawLine(draw,xl[i],yr[i],xr[i],yr[i],PETSC_DRAW_BLACK);
193:       PetscDrawLine(draw,xr[i],yr[i],xr[i],yl[i],PETSC_DRAW_BLACK);
194:       PetscDrawLine(draw,xr[i],yl[i],xl[i],yl[i],PETSC_DRAW_BLACK);
195:     }

197:     xl[i] += .05*h;
198:     xr[i] -= .05*h;
199:     yl[i] += .05*h;
200:     yr[i] -= .05*h;
201:   }
202:   PetscDrawCollectiveEnd(draw);
203:   PetscDrawFlush(draw);
204:   return 0;
205: }

207: /*@C
208:    PetscDrawViewPortsCreateRect - Splits a window into smaller
209:        view ports. Each processor shares all the viewports. The number
210:        of views in the x- and y-directions is specified.

212:    Collective on PetscDraw

214:    Input Parameters:
215: +  draw - the drawing context
216: .  nx - the number of x divisions
217: -  ny - the number of y divisions

219:    Output Parameter:
220: .  ports - a PetscDrawViewPorts context (C structure)

222:    Level: advanced

224: .seealso: PetscDrawSplitViewPort(), PetscDrawSetViewPort(), PetscDrawViewPortsSet(), PetscDrawViewPortsDestroy()

226: @*/
227: PetscErrorCode  PetscDrawViewPortsCreateRect(PetscDraw draw,PetscInt nx,PetscInt ny,PetscDrawViewPorts **newports)
228: {
229:   PetscDrawViewPorts *ports;
230:   PetscReal          *xl,*xr,*yl,*yr,hx,hy;
231:   PetscInt           i,j,k,n;
232:   PetscBool          isnull;
233:   PetscMPIInt        rank;
234:   PetscErrorCode     ierr;

239:   PetscDrawIsNull(draw,&isnull);
240:   if (isnull) {*newports = NULL; return 0;}
241:   MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);

243:   n  = nx*ny;
244:   hx = 1.0/nx;
245:   hy = 1.0/ny;
246:   PetscNew(&ports); *newports = ports;
247:   ports->draw = draw;
248:   ports->nports = n;
249:   PetscObjectReference((PetscObject) draw);
250:   /* save previous drawport of window */
251:   PetscDrawGetViewPort(draw,&ports->port_xl,&ports->port_yl,&ports->port_xr,&ports->port_yr);

253:   PetscMalloc4(n,&xl,n,&xr,n,&yl,n,&yr);
254:   ports->xr = xr;
255:   ports->xl = xl;
256:   ports->yl = yl;
257:   ports->yr = yr;

259:   PetscDrawSetCoordinates(draw,0.0,0.0,1.0,1.0);
260:   PetscDrawCollectiveBegin(draw);
261:   for (i = 0; i < nx; i++) {
262:     for (j = 0; j < ny; j++) {
263:       k = j*nx+i;

265:       xl[k] = i*hx;
266:       xr[k] = xl[k] + hx;
267:       yl[k] = j*hy;
268:       yr[k] = yl[k] + hy;

270:       if (rank == 0) {
271:         PetscDrawLine(draw,xl[k],yl[k],xl[k],yr[k],PETSC_DRAW_BLACK);
272:         PetscDrawLine(draw,xl[k],yr[k],xr[k],yr[k],PETSC_DRAW_BLACK);
273:         PetscDrawLine(draw,xr[k],yr[k],xr[k],yl[k],PETSC_DRAW_BLACK);
274:         PetscDrawLine(draw,xr[k],yl[k],xl[k],yl[k],PETSC_DRAW_BLACK);
275:       }

277:       xl[k] += .05*hx;
278:       xr[k] -= .05*hx;
279:       yl[k] += .05*hy;
280:       yr[k] -= .05*hy;
281:     }
282:   }
283:   PetscDrawCollectiveEnd(draw);
284:   PetscDrawFlush(draw);
285:   return 0;
286: }

288: /*@C
289:    PetscDrawViewPortsDestroy - frees a PetscDrawViewPorts object

291:    Collective on PetscDraw inside PetscDrawViewPorts

293:    Input Parameter:
294: .  ports - the PetscDrawViewPorts object

296:    Level: advanced

298: .seealso: PetscDrawSplitViewPort(), PetscDrawSetViewPort(), PetscDrawViewPortsSet(), PetscDrawViewPortsCreate()

300: @*/
301: PetscErrorCode  PetscDrawViewPortsDestroy(PetscDrawViewPorts *ports)
302: {
303:   if (!ports) return 0;
305:   /* reset Drawport of Window back to previous value */
306:   PetscDrawSetViewPort(ports->draw,ports->port_xl,ports->port_yl,ports->port_xr,ports->port_yr);
307:   PetscDrawDestroy(&ports->draw);
308:   PetscFree4(ports->xl,ports->xr,ports->yl,ports->yr);
309:   PetscFree(ports);
310:   return 0;
311: }

313: /*@C
314:    PetscDrawViewPortsSet - sets a draw object to use a particular subport

316:    Logically Collective on PetscDraw inside PetscDrawViewPorts

318:    Input Parameters:
319: +  ports - the PetscDrawViewPorts object
320: -  port - the port number, from 0 to nports-1

322:    Level: advanced

324: .seealso: PetscDrawSplitViewPort(), PetscDrawSetViewPort(), PetscDrawViewPortsDestroy(), PetscDrawViewPortsCreate()

326: @*/
327: PetscErrorCode  PetscDrawViewPortsSet(PetscDrawViewPorts *ports,PetscInt port)
328: {
329:   if (!ports) return 0;
332:   PetscDrawSetViewPort(ports->draw,ports->xl[port],ports->yl[port],ports->xr[port],ports->yr[port]);
333:   return 0;
334: }