Actual source code: dviewp.c

petsc-3.3-p7 2013-05-11
  2: /*
  3:        Provides the calling sequences for all the basic PetscDraw routines.
  4: */
  5: #include <../src/sys/draw/drawimpl.h>  /*I "petscdraw.h" I*/

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

 13:    Collective on PetscDraw

 15:    Input Parameters:
 16: +  xl,yl,xr,yr - upper right and lower left corners of subwindow
 17:                  These numbers must always be between 0.0 and 1.0.
 18:                  Lower left corner is (0,0).
 19: -  draw - the drawing context

 21:    Level: advanced

 23:    Concepts: drawing^in subset of window
 24:    Concepts: graphics^in subset of window

 26: @*/
 27: PetscErrorCode  PetscDrawSetViewPort(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr)
 28: {
 32:   if (xl < 0.0 || xr > 1.0 || yl < 0.0 || yr > 1.0 || xr <= xl || yr <= yl) {
 33:     SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"ViewPort values must be >= 0 and <= 1: Instead %G %G %G %G",xl,yl,xr,yr);
 34:   }
 35:   draw->port_xl = xl; draw->port_yl = yl;
 36:   draw->port_xr = xr; draw->port_yr = yr;
 37:   if (draw->ops->setviewport) {
 38:     (*draw->ops->setviewport)(draw,xl,yl,xr,yr);
 39:   }
 40:   return(0);
 41: }

 45: /*@
 46:    PetscDrawSplitViewPort - Splits a window shared by several processes into smaller
 47:    view ports. One for each process. 

 49:    Collective on PetscDraw

 51:    Input Parameter:
 52: .  draw - the drawing context

 54:    Level: advanced

 56:    Concepts: drawing^in subset of window

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

 60: @*/
 61: PetscErrorCode  PetscDrawSplitViewPort(PetscDraw draw)
 62: {
 64:   PetscMPIInt    rank,size;
 65:   int            n;
 66:   PetscBool      isnull;
 67:   PetscReal      xl,xr,yl,yr,h;

 71:   PetscObjectTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);
 72:   if (isnull) return(0);

 74:   MPI_Comm_rank(((PetscObject)draw)->comm,&rank);
 75:   MPI_Comm_size(((PetscObject)draw)->comm,&size);

 77:   n = (int)(.1 + sqrt((double)size));
 78:   while (n*n < size) {n++;}

 80:   h  = 1.0/n;
 81:   xl = (rank % n)*h;
 82:   xr = xl + h;
 83:   yl = (rank/n)*h;
 84:   yr = yl + h;

 86:   PetscDrawLine(draw,xl,yl,xl,yr,PETSC_DRAW_BLACK);
 87:   PetscDrawLine(draw,xl,yr,xr,yr,PETSC_DRAW_BLACK);
 88:   PetscDrawLine(draw,xr,yr,xr,yl,PETSC_DRAW_BLACK);
 89:   PetscDrawLine(draw,xr,yl,xl,yl,PETSC_DRAW_BLACK);
 90:   PetscDrawSynchronizedFlush(draw);

 92:   draw->port_xl = xl + .1*h;
 93:   draw->port_xr = xr - .1*h;
 94:   draw->port_yl = yl + .1*h;
 95:   draw->port_yr = yr - .1*h;

 97:   if (draw->ops->setviewport) {
 98:      (*draw->ops->setviewport)(draw,xl,yl,xr,yr);
 99:   }
100:   return(0);
101: }

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

109:    Collective on PetscDraw

111:    Input Parameters:
112: +  draw - the drawing context
113: -  nports - the number of ports

115:    Output Parameter:
116: .  ports - a PetscDrawViewPorts context (C structure)

118:    Level: advanced

120:    Concepts: drawing^in subset of window

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

124: @*/
125: PetscErrorCode  PetscDrawViewPortsCreate(PetscDraw draw,PetscInt nports,PetscDrawViewPorts **ports)
126: {
127:   int        i,n;
129:   PetscBool  isnull;
130:   PetscReal  *xl,*xr,*yl,*yr,h;

135:   PetscObjectTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);
136:   if (isnull) {
137:     *ports = PETSC_NULL;
138:     return(0);
139:   }

141:   PetscNew(PetscDrawViewPorts,ports);
142:   (*ports)->draw   = draw;
143:   (*ports)->nports = nports;

145:   PetscObjectReference((PetscObject)draw);

147:   n = (int)(.1 + sqrt((double)nports));
148:   while (n*n < nports) {n++;}
149: 
150:   PetscMalloc(n*n*sizeof(PetscReal),&xl);(*ports)->xl = xl;
151:   PetscMalloc(n*n*sizeof(PetscReal),&xr);(*ports)->xr = xr;
152:   PetscMalloc(n*n*sizeof(PetscReal),&yl);(*ports)->yl = yl;
153:   PetscMalloc(n*n*sizeof(PetscReal),&yr);(*ports)->yr = yr;

155:   h  = 1.0/n;

157:   for (i=0; i<n*n; i++) {
158:     xl[i] = (i % n)*h;
159:     xr[i] = xl[i] + h;
160:     yl[i] = (i/n)*h;
161:     yr[i] = yl[i] + h;

163:     PetscDrawLine(draw,xl[i],yl[i],xl[i],yr[i],PETSC_DRAW_BLACK);
164:     PetscDrawLine(draw,xl[i],yr[i],xr[i],yr[i],PETSC_DRAW_BLACK);
165:     PetscDrawLine(draw,xr[i],yr[i],xr[i],yl[i],PETSC_DRAW_BLACK);
166:     PetscDrawLine(draw,xr[i],yl[i],xl[i],yl[i],PETSC_DRAW_BLACK);

168:     xl[i] += .1*h;
169:     xr[i] -= .1*h;
170:     yl[i] += .1*h;
171:     yr[i] -= .1*h;
172:   }
173:   PetscDrawSynchronizedFlush(draw);

175:   return(0);
176: }

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

185:    Collective on PetscDraw

187:    Input Parameters:
188: +  draw - the drawing context
189: .  nx - the number of x divisions
190: -  ny - the number of y divisions

192:    Output Parameter:
193: .  ports - a PetscDrawViewPorts context (C structure)

195:    Level: advanced

197:    Concepts: drawing^in subset of window

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

201: @*/
202: PetscErrorCode  PetscDrawViewPortsCreateRect(PetscDraw draw,PetscInt nx,PetscInt ny,PetscDrawViewPorts **ports)
203: {
204:   PetscReal     *xl, *xr, *yl, *yr, hx, hy;
205:   PetscBool      isnull;
206:   PetscInt       i, j, n;

211:   if ((nx < 1) || (ny < 1)) {
212:     SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Number of divisions must be positive: %d x %d", nx, ny);
213:   }
214:   PetscObjectTypeCompare((PetscObject) draw, PETSC_DRAW_NULL, &isnull);
215:   if (isnull) {
216:     *ports = PETSC_NULL;
217:     return(0);
218:   }
219:   n  = nx*ny;
220:   hx = 1.0/nx;
221:   hy = 1.0/ny;
222:   PetscNew(PetscDrawViewPorts, ports);
223:   (*ports)->draw   = draw;
224:   (*ports)->nports = n;
225:   PetscObjectReference((PetscObject) draw);
226:   PetscMalloc(n*sizeof(PetscReal), &xl);(*ports)->xl = xl;
227:   PetscMalloc(n*sizeof(PetscReal), &xr);(*ports)->xr = xr;
228:   PetscMalloc(n*sizeof(PetscReal), &yl);(*ports)->yl = yl;
229:   PetscMalloc(n*sizeof(PetscReal), &yr);(*ports)->yr = yr;
230:   for(i = 0; i < nx; i++) {
231:     for(j = 0; j < ny; j++) {
232:       PetscInt k = j*nx+i;

234:       xl[k] = i*hx;
235:       xr[k] = xl[k] + hx;
236:       yl[k] = j*hy;
237:       yr[k] = yl[k] + hy;

239:       PetscDrawLine(draw,xl[k],yl[k],xl[k],yr[k],PETSC_DRAW_BLACK);
240:       PetscDrawLine(draw,xl[k],yr[k],xr[k],yr[k],PETSC_DRAW_BLACK);
241:       PetscDrawLine(draw,xr[k],yr[k],xr[k],yl[k],PETSC_DRAW_BLACK);
242:       PetscDrawLine(draw,xr[k],yl[k],xl[k],yl[k],PETSC_DRAW_BLACK);

244:       xl[k] += .01*hx;
245:       xr[k] -= .01*hx;
246:       yl[k] += .01*hy;
247:       yr[k] -= .01*hy;
248:     }
249:   }
250:   PetscDrawSynchronizedFlush(draw);
251:   return(0);
252: }

256: /*@C
257:    PetscDrawViewPortsDestroy - frees a PetscDrawViewPorts object

259:    Collective on PetscDraw inside PetscDrawViewPorts

261:    Input Parameter:
262: .  ports - the PetscDrawViewPorts object

264:    Level: advanced

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

268: @*/
269: PetscErrorCode  PetscDrawViewPortsDestroy(PetscDrawViewPorts *ports)
270: {

274:   if (!ports) return(0);
275:   PetscDrawDestroy(&ports->draw);
276:   PetscFree(ports->xl);
277:   PetscFree(ports->xr);
278:   PetscFree(ports->yl);
279:   PetscFree(ports->yr);
280:   PetscFree(ports);
281:   return(0);
282: }

286: /*@C
287:    PetscDrawViewPortsSet - sets a draw object to use a particular subport

289:    Logically Collective on PetscDraw inside PetscDrawViewPorts

291:    Input Parameter:
292: +  ports - the PetscDrawViewPorts object
293: -  port - the port number, from 0 to nports-1

295:    Level: advanced

297:    Concepts: drawing^in subset of window

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

301: @*/
302: PetscErrorCode  PetscDrawViewPortsSet(PetscDrawViewPorts *ports,PetscInt port)
303: {

307:   if (ports) {
308:     if (port < 0 || port > ports->nports-1) {
309:       SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Port is out of range requested %d from 0 to %d\n",port,ports->nports);
310:     }
311:     PetscDrawSetViewPort(ports->draw,ports->xl[port],ports->yl[port],ports->xr[port],ports->yr[port]);
312:   }
313:   return(0);
314: }