Actual source code: drawreg.c

  1: /*$Id: drawreg.c,v 1.45 2001/06/21 21:15:18 bsmith Exp $*/
  2: /*
  3:        Provides the registration process for PETSc PetscDraw routines
  4: */
 5:  #include src/sys/src/draw/drawimpl.h

  7: /*
  8:    Contains the list of registered PetscDraw routines
  9: */
 10: PetscFList PetscDrawList              = 0;

 12: /*@C
 13:    PetscDrawCreate - Creates a graphics context.

 15:    Collective on MPI_Comm

 17:    Input Parameter:
 18: +  comm - MPI communicator
 19: .  display - X display when using X windows
 20: .  title - optional title added to top of window
 21: .  x,y - coordinates of lower left corner of window or PETSC_DECIDE
 22: -  w, h - width and height of window or PETSC_DECIDE or PETSC_DRAW_HALF_SIZE, PETSC_DRAW_FULL_SIZE,
 23:           or PETSC_DRAW_THIRD_SIZE or PETSC_DRAW_QUARTER_SIZE

 25:    Output Parameter:
 26: .  draw - location to put the PetscDraw context

 28:    Level: beginner

 30:    Concepts: graphics^creating context
 31:    Concepts: drawing^creating context

 33: .seealso: PetscDrawSetFromOptions(), PetscDrawDestroy(), PetscDrawSetType()
 34: @*/
 35: int PetscDrawCreate(MPI_Comm comm,const char display[],const char title[],int x,int y,int w,int h,PetscDraw *indraw)
 36: {
 37:   PetscDraw draw;
 38:   int  ierr;

 41:   *indraw = 0;
 42:   PetscHeaderCreate(draw,_p_PetscDraw,struct _PetscDrawOps,PETSC_DRAW_COOKIE,-1,"Draw",comm,PetscDrawDestroy,0);
 43:   PetscLogObjectCreate(draw);
 44:   draw->type    = -1;
 45:   draw->data    = 0;
 46:   ierr          = PetscStrallocpy(title,&draw->title);
 47:   ierr          = PetscStrallocpy(display,&draw->display);
 48:   draw->x       = x;
 49:   draw->y       = y;
 50:   draw->w       = w;
 51:   draw->h       = h;
 52:   draw->pause   = 0;
 53:   draw->coor_xl = 0.0;
 54:   draw->coor_xr = 1.0;
 55:   draw->coor_yl = 0.0;
 56:   draw->coor_yr = 1.0;
 57:   draw->port_xl = 0.0;
 58:   draw->port_xr = 1.0;
 59:   draw->port_yl = 0.0;
 60:   draw->port_yr = 1.0;
 61:   draw->popup   = 0;
 62:   PetscOptionsGetInt(PETSC_NULL,"-draw_pause",&draw->pause,PETSC_NULL);
 63:   *indraw       = draw;
 64:   return(0);
 65: }
 66: 
 67: /*@C
 68:    PetscDrawSetType - Builds graphics object for a particular implementation 

 70:    Collective on PetscDraw

 72:    Input Parameter:
 73: +  draw      - the graphics context
 74: -  type      - for example, PETSC_DRAW_X

 76:    Options Database Command:
 77: .  -draw_type  <type> - Sets the type; use -help for a list 
 78:     of available methods (for instance, x)

 80:    Level: intermediate

 82:    Notes:  
 83:    See "petsc/include/petscdraw.h" for available methods (for instance,
 84:    PETSC_DRAW_X)

 86:    Concepts: drawing^X windows
 87:    Concepts: X windows^graphics
 88:    Concepts: drawing^postscript
 89:    Concepts: postscript^graphics
 90:    Concepts: drawing^Microsoft Windows

 92: .seealso: PetscDrawSetFromOptions(), PetscDrawCreate(), PetscDrawDestroy()
 93: @*/
 94: int PetscDrawSetType(PetscDraw draw,PetscDrawType type)
 95: {
 96:   int        ierr,(*r)(PetscDraw);
 97:   PetscTruth match;
 98:   PetscTruth flg=PETSC_FALSE;


104:   PetscTypeCompare((PetscObject)draw,type,&match);
105:   if (match) return(0);

107: #if defined(PETSC_HAVE_X11)
108:   /*  User requests no graphics */
109:   PetscOptionsHasName(PETSC_NULL,"-nox",&flg);
110: #endif

112:   /*
113:      This is not ideal, but it allows codes to continue to run if X graphics 
114:    was requested but is not installed on this machine. Mostly this is for
115:    testing.
116:    */
117: #if !defined(PETSC_HAVE_X11)
118:   {
119:     PetscStrcmp(type,PETSC_DRAW_X,&match);
120:     if (match) flg = PETSC_TRUE;
121:   }
122: #endif
123:   if (flg) {
124:     type = PETSC_DRAW_NULL;
125:   }

127:   if (draw->data) {
128:     /* destroy the old private PetscDraw context */
129:     ierr       = (*draw->ops->destroy)(draw);
130:     draw->data = 0;
131:   }

133:   /* Get the function pointers for the graphics method requested */
134:   if (!PetscDrawList) SETERRQ(1,"No draw implementations ierr");

136:    PetscFListFind(draw->comm,PetscDrawList,type,(void (**)(void)) &r);

138:   if (!r) SETERRQ1(1,"Unknown PetscDraw type given: %s",type);

140:   PetscObjectChangeTypeName((PetscObject)draw,type);

142:   draw->data        = 0;
143:   (*r)(draw);

145:   return(0);
146: }

148: /*@C
149:    PetscDrawRegisterDestroy - Frees the list of PetscDraw methods that were
150:    registered by PetscDrawRegisterDynamic().

152:    Not Collective

154:    Level: developer

156: .seealso: PetscDrawRegisterDynamic(), PetscDrawRegisterAll()
157: @*/
158: int PetscDrawRegisterDestroy(void)
159: {

163:   if (PetscDrawList) {
164:     PetscFListDestroy(&PetscDrawList);
165:     PetscDrawList = 0;
166:   }
167:   return(0);
168: }

170: /*@C
171:    PetscDrawGetType - Gets the PetscDraw type as a string from the PetscDraw object.

173:    Not Collective

175:    Input Parameter:
176: .  draw - Krylov context 

178:    Output Parameters:
179: .  name - name of PetscDraw method 

181:    Level: advanced

183: @*/
184: int PetscDrawGetType(PetscDraw draw,PetscDrawType *type)
185: {
187:   *type = draw->type_name;
188:   return(0);
189: }

191: /*MC
192:    PetscDrawRegisterDynamic - Adds a method to the Krylov subspace solver package.

194:    Synopsis:
195:    int PetscDrawRegisterDynamic(char *name_solver,char *path,char *name_create,int (*routine_create)(PetscDraw))

197:    Not Collective

199:    Input Parameters:
200: +  name_solver - name of a new user-defined solver
201: .  path - path (either absolute or relative) the library containing this solver
202: .  name_create - name of routine to create method context
203: -  routine_create - routine to create method context

205:    Level: developer

207:    Notes:
208:    PetscDrawRegisterDynamic() may be called multiple times to add several user-defined solvers.

210:    If dynamic libraries are used, then the fourth input argument (routine_create)
211:    is ignored.

213:    Sample usage:
214: .vb
215:    PetscDrawRegisterDynamic("my_draw_type",/home/username/my_lib/lib/libO/solaris/mylib.a,
216:                "MyDrawCreate",MyDrawCreate);
217: .ve

219:    Then, your solver can be chosen with the procedural interface via
220: $     PetscDrawSetType(ksp,"my_draw_type")
221:    or at runtime via the option
222: $     -draw_type my_draw_type

224:    Concepts: graphics^registering new draw classes
225:    Concepts: PetscDraw^registering new draw classes

227: .seealso: PetscDrawRegisterAll(), PetscDrawRegisterDestroy()
228: M*/

230: int PetscDrawRegister(char *sname,char *path,char *name,int (*function)(PetscDraw))
231: {
232:   int  ierr;
233:   char fullname[256];

236:   PetscFListConcat(path,name,fullname);
237:   PetscFListAdd(&PetscDrawList,sname,fullname,(void (*)(void))function);
238:   return(0);
239: }

241: /*@C
242:    PetscDrawSetFromOptions - Sets the graphics type from the options database.
243:       Defaults to a PETSc X windows graphics.

245:    Collective on PetscDraw

247:    Input Parameter:
248: .     draw - the graphics context

250:    Options Database:
251: +   -nox - do not use X graphics (ignore graphics calls, but run program correctly)
252: -   -nox_warning - when X windows support is not installed this prevents the warning message
253:                    from being printed

255:    Level: intermediate

257:    Notes: 
258:     Must be called after PetscDrawCreate() before the PetscDrawtor is used.

260:     Concepts: drawing^setting options
261:     Concepts: graphics^setting options

263: .seealso: PetscDrawCreate(), PetscDrawSetType()

265: @*/
266: int PetscDrawSetFromOptions(PetscDraw draw)
267: {
268:   int        ierr;
269:   PetscTruth flg,nox;
270:   char       vtype[256],*def;
271: #if !defined(PARCH_Win32) && !defined(PETSC_HAVE_X11)
272:   PetscTruth warn;
273: #endif


278:   if (!PetscDrawList) SETERRQ(1,"No draw implementations registered");
279:   if (draw->type_name) {
280:     def = draw->type_name;
281:   } else {
282:     PetscOptionsHasName(PETSC_NULL,"-nox",&nox);
283:     def  = PETSC_DRAW_NULL;
284: #if defined(PARCH_win32)
285:     if (!nox) def = PETSC_DRAW_WIN32;
286: #elif defined(PETSC_HAVE_X11)
287:     if (!nox) def = PETSC_DRAW_X;
288: #else
289:     PetscOptionsHasName(PETSC_NULL,"-nox_warning",&warn);
290:     if (!nox && !warn) {
291:       (*PetscErrorPrintf)("PETSc installed without X windows on this machinenproceeding without graphicsn");
292:     }
293: #endif
294:   }
295:   PetscOptionsBegin(draw->comm,draw->prefix,"Graphics (PetscDraw) Options","Draw");
296:     PetscOptionsList("-draw_type","Type of graphical output","PetscDrawSetType",PetscDrawList,def,vtype,256,&flg);
297:     if (flg) {
298:       PetscDrawSetType(draw,vtype);
299:     } else if (!draw->type_name) {
300:       PetscDrawSetType(draw,def);
301:     }
302:     PetscOptionsName("-nox","Run without graphics","None",&nox);
303:   PetscOptionsEnd();
304:   return(0);
305: }