Actual source code: lg.c

  1: /*$Id: lg.c,v 1.76 2001/04/10 19:34:23 bsmith Exp $*/
  2: /*
  3:        Contains the data structure for plotting several line
  4:     graphs in a window with an axis. This is intended for line 
  5:     graphs that change dynamically by adding more points onto 
  6:     the end of the X axis.
  7: */

 9:  #include petsc.h

 11: int DRAWLG_COOKIE = 0;

 13: struct _p_DrawLG {
 14:   PETSCHEADER(int)
 15:   int           (*destroy)(PetscDrawLG);
 16:   int           (*view)(PetscDrawLG,PetscViewer);
 17:   int           len,loc;
 18:   PetscDraw     win;
 19:   PetscDrawAxis axis;
 20:   PetscReal     xmin,xmax,ymin,ymax,*x,*y;
 21:   int           nopts,dim;
 22:   PetscTruth    use_dots;
 23: };

 25: #define CHUNCKSIZE 100

 29: /*@C
 30:     PetscDrawLGCreate - Creates a line graph data structure.

 32:     Collective over PetscDraw

 34:     Input Parameters:
 35: +   draw - the window where the graph will be made.
 36: -   dim - the number of line cures which will be drawn

 38:     Output Parameters:
 39: .   outctx - the line graph context

 41:     Level: intermediate

 43:     Concepts: line graph^creating

 45: .seealso:  PetscDrawLGDestroy()
 46: @*/
 47: int PetscDrawLGCreate(PetscDraw draw,int dim,PetscDrawLG *outctx)
 48: {
 49:   int         ierr;
 50:   PetscTruth  isnull;
 51:   PetscObject obj = (PetscObject)draw;
 52:   PetscDrawLG lg;

 57:   PetscTypeCompare(obj,PETSC_DRAW_NULL,&isnull);
 58:   if (isnull) {
 59:     PetscDrawOpenNull(obj->comm,(PetscDraw*)outctx);
 60:     return(0);
 61:   }
 62:   PetscHeaderCreate(lg,_p_DrawLG,int,DRAWLG_COOKIE,0,"PetscDrawLG",obj->comm,PetscDrawLGDestroy,0);
 63:   lg->view    = 0;
 64:   lg->destroy = 0;
 65:   lg->nopts   = 0;
 66:   lg->win     = draw;
 67:   lg->dim     = dim;
 68:   lg->xmin    = 1.e20;
 69:   lg->ymin    = 1.e20;
 70:   lg->xmax    = -1.e20;
 71:   lg->ymax    = -1.e20;
 72:   PetscMalloc(2*dim*CHUNCKSIZE*sizeof(PetscReal),&lg->x);
 73:   PetscLogObjectMemory(lg,2*dim*CHUNCKSIZE*sizeof(PetscReal));
 74:   lg->y       = lg->x + dim*CHUNCKSIZE;
 75:   lg->len     = dim*CHUNCKSIZE;
 76:   lg->loc     = 0;
 77:   lg->use_dots= PETSC_FALSE;
 78:   PetscDrawAxisCreate(draw,&lg->axis);
 79:   PetscLogObjectParent(lg,lg->axis);
 80:   *outctx = lg;
 81:   return(0);
 82: }

 86: /*@
 87:    PetscDrawLGSetDimension - Change the number of lines that are to be drawn.

 89:    Collective over PetscDrawLG

 91:    Input Parameter:
 92: +  lg - the line graph context.
 93: -  dim - the number of curves.

 95:    Level: intermediate

 97:    Concepts: line graph^setting number of lines

 99: @*/
100: int PetscDrawLGSetDimension(PetscDrawLG lg,int dim)
101: {

105:   if (lg && lg->cookie == PETSC_DRAW_COOKIE) return(0);
107:   if (lg->dim == dim) return(0);

109:   PetscFree(lg->x);
110:   lg->dim = dim;
111:   PetscMalloc(2*dim*CHUNCKSIZE*sizeof(PetscReal),&lg->x);
112:   PetscLogObjectMemory(lg,2*dim*CHUNCKSIZE*sizeof(PetscReal));
113:   lg->y       = lg->x + dim*CHUNCKSIZE;
114:   lg->len     = dim*CHUNCKSIZE;
115:   return(0);
116: }

120: /*@
121:    PetscDrawLGReset - Clears line graph to allow for reuse with new data.

123:    Collective over PetscDrawLG

125:    Input Parameter:
126: .  lg - the line graph context.

128:    Level: intermediate

130:    Concepts: line graph^restarting

132: @*/
133: int PetscDrawLGReset(PetscDrawLG lg)
134: {
136:   if (lg && lg->cookie == PETSC_DRAW_COOKIE) return(0);
138:   lg->xmin  = 1.e20;
139:   lg->ymin  = 1.e20;
140:   lg->xmax  = -1.e20;
141:   lg->ymax  = -1.e20;
142:   lg->loc   = 0;
143:   lg->nopts = 0;
144:   return(0);
145: }

149: /*@C
150:    PetscDrawLGDestroy - Frees all space taken up by line graph data structure.

152:    Collective over PetscDrawLG

154:    Input Parameter:
155: .  lg - the line graph context

157:    Level: intermediate

159: .seealso:  PetscDrawLGCreate()
160: @*/
161: int PetscDrawLGDestroy(PetscDrawLG lg)
162: {

166:   if (!lg || lg->cookie != PETSC_DRAW_COOKIE) {
168:   }

170:   if (--lg->refct > 0) return(0);
171:   if (lg && lg->cookie == PETSC_DRAW_COOKIE) {
172:     PetscObjectDestroy((PetscObject)lg);
173:     return(0);
174:   }
175:   PetscDrawAxisDestroy(lg->axis);
176:   PetscFree(lg->x);
177:   PetscLogObjectDestroy(lg);
178:   PetscHeaderDestroy(lg);
179:   return(0);
180: }

184: /*@
185:    PetscDrawLGAddPoint - Adds another point to each of the line graphs. 
186:    The new point must have an X coordinate larger than the old points.

188:    Not Collective, but ignored by all processors except processor 0 in PetscDrawLG

190:    Input Parameters:
191: +  lg - the LineGraph data structure
192: -  x, y - the points to two vectors containing the new x and y 
193:           point for each curve.

195:    Level: intermediate

197:    Concepts: line graph^adding points

199: .seealso: PetscDrawLGAddPoints()
200: @*/
201: int PetscDrawLGAddPoint(PetscDrawLG lg,PetscReal *x,PetscReal *y)
202: {
203:   int i,ierr;

206:   if (lg && lg->cookie == PETSC_DRAW_COOKIE) return(0);

209:   if (lg->loc+lg->dim >= lg->len) { /* allocate more space */
210:     PetscReal *tmpx,*tmpy;
211:     PetscMalloc((2*lg->len+2*lg->dim*CHUNCKSIZE)*sizeof(PetscReal),&tmpx);
212:     PetscLogObjectMemory(lg,2*lg->dim*CHUNCKSIZE*sizeof(PetscReal));
213:     tmpy = tmpx + lg->len + lg->dim*CHUNCKSIZE;
214:     PetscMemcpy(tmpx,lg->x,lg->len*sizeof(PetscReal));
215:     PetscMemcpy(tmpy,lg->y,lg->len*sizeof(PetscReal));
216:     PetscFree(lg->x);
217:     lg->x = tmpx; lg->y = tmpy;
218:     lg->len += lg->dim*CHUNCKSIZE;
219:   }
220:   for (i=0; i<lg->dim; i++) {
221:     if (x[i] > lg->xmax) lg->xmax = x[i];
222:     if (x[i] < lg->xmin) lg->xmin = x[i];
223:     if (y[i] > lg->ymax) lg->ymax = y[i];
224:     if (y[i] < lg->ymin) lg->ymin = y[i];

226:     lg->x[lg->loc]   = x[i];
227:     lg->y[lg->loc++] = y[i];
228:   }
229:   lg->nopts++;
230:   return(0);
231: }

235: /*@
236:    PetscDrawLGIndicateDataPoints - Causes LG to draw a big dot for each data-point.

238:    Not Collective, but ignored by all processors except processor 0 in PetscDrawLG

240:    Input Parameters:
241: .  lg - the linegraph context

243:    Level: intermediate

245:    Concepts: line graph^showing points

247: @*/
248: int PetscDrawLGIndicateDataPoints(PetscDrawLG lg)
249: {
251:   if (lg && lg->cookie == PETSC_DRAW_COOKIE) return(0);

253:   lg->use_dots = PETSC_TRUE;
254:   return(0);
255: }

259: /*@C
260:    PetscDrawLGAddPoints - Adds several points to each of the line graphs.
261:    The new points must have an X coordinate larger than the old points.

263:    Not Collective, but ignored by all processors except processor 0 in PetscDrawLG

265:    Input Parameters:
266: +  lg - the LineGraph data structure
267: .  xx,yy - points to two arrays of pointers that point to arrays 
268:            containing the new x and y points for each curve.
269: -  n - number of points being added

271:    Level: intermediate


274:    Concepts: line graph^adding points

276: .seealso: PetscDrawLGAddPoint()
277: @*/
278: int PetscDrawLGAddPoints(PetscDrawLG lg,int n,PetscReal **xx,PetscReal **yy)
279: {
280:   int       i,j,k,ierr;
281:   PetscReal *x,*y;

284:   if (lg && lg->cookie == PETSC_DRAW_COOKIE) return(0);
286:   if (lg->loc+n*lg->dim >= lg->len) { /* allocate more space */
287:     PetscReal *tmpx,*tmpy;
288:     int    chunk = CHUNCKSIZE;

290:     if (n > chunk) chunk = n;
291:     PetscMalloc((2*lg->len+2*lg->dim*chunk)*sizeof(PetscReal),&tmpx);
292:     PetscLogObjectMemory(lg,2*lg->dim*chunk*sizeof(PetscReal));
293:     tmpy = tmpx + lg->len + lg->dim*chunk;
294:     PetscMemcpy(tmpx,lg->x,lg->len*sizeof(PetscReal));
295:     PetscMemcpy(tmpy,lg->y,lg->len*sizeof(PetscReal));
296:     PetscFree(lg->x);
297:     lg->x    = tmpx; lg->y = tmpy;
298:     lg->len += lg->dim*chunk;
299:   }
300:   for (j=0; j<lg->dim; j++) {
301:     x = xx[j]; y = yy[j];
302:     k = lg->loc + j;
303:     for (i=0; i<n; i++) {
304:       if (x[i] > lg->xmax) lg->xmax = x[i];
305:       if (x[i] < lg->xmin) lg->xmin = x[i];
306:       if (y[i] > lg->ymax) lg->ymax = y[i];
307:       if (y[i] < lg->ymin) lg->ymin = y[i];

309:       lg->x[k]   = x[i];
310:       lg->y[k] = y[i];
311:       k += lg->dim;
312:     }
313:   }
314:   lg->loc   += n*lg->dim;
315:   lg->nopts += n;
316:   return(0);
317: }

321: /*@
322:    PetscDrawLGDraw - Redraws a line graph.

324:    Not Collective,but ignored by all processors except processor 0 in PetscDrawLG

326:    Input Parameter:
327: .  lg - the line graph context

329:    Level: intermediate

331: .seealso: PetscDrawSPDraw(), PetscDrawLGSPDraw()

333: @*/
334: int PetscDrawLGDraw(PetscDrawLG lg)
335: {
336:   PetscReal xmin=lg->xmin,xmax=lg->xmax,ymin=lg->ymin,ymax=lg->ymax;
337:   int       i,j,dim = lg->dim,nopts = lg->nopts,rank,ierr;
338:   PetscDraw draw = lg->win;

341:   if (lg && lg->cookie == PETSC_DRAW_COOKIE) return(0);

344:   PetscDrawClear(draw);
345:   PetscDrawAxisSetLimits(lg->axis,xmin,xmax,ymin,ymax);
346:   PetscDrawAxisDraw(lg->axis);

348:   MPI_Comm_rank(lg->comm,&rank);
349:   if (!rank) {
350: 
351:     for (i=0; i<dim; i++) {
352:       for (j=1; j<nopts; j++) {
353:         PetscDrawLine(draw,lg->x[(j-1)*dim+i],lg->y[(j-1)*dim+i],lg->x[j*dim+i],lg->y[j*dim+i],PETSC_DRAW_BLACK+i);
354:         if (lg->use_dots) {
355:           PetscDrawString(draw,lg->x[j*dim+i],lg->y[j*dim+i],PETSC_DRAW_RED,"x");
356:         }
357:       }
358:     }


361:   }
362:   PetscDrawFlush(lg->win);
363:   PetscDrawPause(lg->win);
364:   return(0);
365: }

369: /*@
370:   PetscDrawLGPrint - Prints a line graph.

372:   Not collective

374:   Input Parameter:
375: . lg - the line graph context

377:   Level: beginner

379:   Contributed by Matthew Knepley

381: .keywords:  draw, line, graph
382: @*/
383: int PetscDrawLGPrint(PetscDrawLG lg)
384: {
385:   PetscReal xmin=lg->xmin, xmax=lg->xmax, ymin=lg->ymin, ymax=lg->ymax;
386:   int       i, j, dim = lg->dim, nopts = lg->nopts;

389:   if (lg && lg->cookie == PETSC_DRAW_COOKIE) return(0);
391:   if (nopts < 1)                  return(0);
392:   if (xmin > xmax || ymin > ymax) return(0);

394:   for(i = 0; i < dim; i++) {
395:     PetscPrintf(lg->comm, "Line %d\n", i);
396:     for(j = 0; j < nopts; j++) {
397:       PetscPrintf(lg->comm, "  X: %g Y: %g\n", lg->x[j*dim+i], lg->y[j*dim+i]);
398:     }
399:   }
400:   return(0);
401: }
402: 
405: /*@
406:    PetscDrawLGSetLimits - Sets the axis limits for a line graph. If more
407:    points are added after this call, the limits will be adjusted to
408:    include those additional points.

410:    Not Collective, but ignored by all processors except processor 0 in PetscDrawLG

412:    Input Parameters:
413: +  xlg - the line graph context
414: -  x_min,x_max,y_min,y_max - the limits

416:    Level: intermediate

418:    Concepts: line graph^setting axis

420: @*/
421: int PetscDrawLGSetLimits(PetscDrawLG lg,PetscReal x_min,PetscReal x_max,PetscReal y_min,PetscReal y_max)
422: {
424:   if (lg && lg->cookie == PETSC_DRAW_COOKIE) return(0);
426:   (lg)->xmin = x_min;
427:   (lg)->xmax = x_max;
428:   (lg)->ymin = y_min;
429:   (lg)->ymax = y_max;
430:   return(0);
431: }
432: 
435: /*@C
436:    PetscDrawLGGetAxis - Gets the axis context associated with a line graph.
437:    This is useful if one wants to change some axis property, such as
438:    labels, color, etc. The axis context should not be destroyed by the
439:    application code.

441:    Not Collective, if PetscDrawLG is parallel then PetscDrawAxis is parallel

443:    Input Parameter:
444: .  lg - the line graph context

446:    Output Parameter:
447: .  axis - the axis context

449:    Level: advanced

451: @*/
452: int PetscDrawLGGetAxis(PetscDrawLG lg,PetscDrawAxis *axis)
453: {
455:   if (lg && lg->cookie == PETSC_DRAW_COOKIE) {
456:     *axis = 0;
457:     return(0);
458:   }
461:   *axis = lg->axis;
462:   return(0);
463: }

467: /*@C
468:    PetscDrawLGGetDraw - Gets the draw context associated with a line graph.

470:    Not Collective, if PetscDrawLG is parallel then PetscDraw is parallel

472:    Input Parameter:
473: .  lg - the line graph context

475:    Output Parameter:
476: .  draw - the draw context

478:    Level: intermediate

480: @*/
481: int PetscDrawLGGetDraw(PetscDrawLG lg,PetscDraw *draw)
482: {
486:   if (lg->cookie == PETSC_DRAW_COOKIE) {
487:     *draw = (PetscDraw)lg;
488:   } else {
490:     *draw = lg->win;
491:   }
492:   return(0);
493: }


498: /*@
499:    PetscDrawLGSPDraw - Redraws a line graph.

501:    Not Collective,but ignored by all processors except processor 0 in PetscDrawLG

503:    Input Parameter:
504: .  lg - the line graph context

506:    Level: intermediate

508: .seealso: PetscDrawLGDraw(), PetscDrawSPDraw()

510:    Developer Notes: This code cheats and uses the fact that the LG and SP structs are the same

512: @*/
513: int PetscDrawLGSPDraw(PetscDrawLG lg,PetscDrawSP spin)
514: {
515:   PetscDrawLG sp = (PetscDrawLG)spin;
516:   PetscReal   xmin,xmax,ymin,ymax;
517:   int         i,j,dim,nopts,rank,ierr;
518:   PetscDraw   draw = lg->win;

521:   if (lg && lg->cookie == PETSC_DRAW_COOKIE) return(0);

525:   xmin = PetscMin(lg->xmin,sp->xmin);
526:   ymin = PetscMin(lg->ymin,sp->ymin);
527:   xmax = PetscMax(lg->xmax,sp->xmax);
528:   ymax = PetscMax(lg->ymax,sp->ymax);

530:   PetscDrawClear(draw);
531:   PetscDrawAxisSetLimits(lg->axis,xmin,xmax,ymin,ymax);
532:   PetscDrawAxisDraw(lg->axis);

534:   MPI_Comm_rank(lg->comm,&rank);
535:   if (!rank) {
536: 
537:     dim   = lg->dim;
538:     nopts = lg->nopts;
539:     for (i=0; i<dim; i++) {
540:       for (j=1; j<nopts; j++) {
541:         PetscDrawLine(draw,lg->x[(j-1)*dim+i],lg->y[(j-1)*dim+i],lg->x[j*dim+i],lg->y[j*dim+i],PETSC_DRAW_BLACK+i);
542:         if (lg->use_dots) {
543:           PetscDrawString(draw,lg->x[j*dim+i],lg->y[j*dim+i],PETSC_DRAW_RED,"x");
544:         }
545:       }
546:     }

548:     dim   = sp->dim;
549:     nopts = sp->nopts;
550:     for (i=0; i<dim; i++) {
551:       for (j=0; j<nopts; j++) {
552:         PetscDrawString(draw,sp->x[j*dim+i],sp->y[j*dim+i],PETSC_DRAW_RED,"x");
553:       }
554:     }
555:   }
556:   PetscDrawFlush(lg->win);
557:   PetscDrawPause(lg->win);
558:   return(0);
559: }