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,flg;


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

106:   /*  User requests no graphics */
107:   PetscOptionsHasName(PETSC_NULL,"-nox",&flg);

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

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

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

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

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

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

139:   draw->data        = 0;
140:   (*r)(draw);

142:   return(0);
143: }

145: /*@C
146:    PetscDrawRegisterDestroy - Frees the list of PetscDraw methods that were
147:    registered by PetscDrawRegisterDynamic().

149:    Not Collective

151:    Level: developer

153: .seealso: PetscDrawRegisterDynamic(), PetscDrawRegisterAll()
154: @*/
155: int PetscDrawRegisterDestroy(void)
156: {

160:   if (PetscDrawList) {
161:     PetscFListDestroy(&PetscDrawList);
162:     PetscDrawList = 0;
163:   }
164:   return(0);
165: }

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

170:    Not Collective

172:    Input Parameter:
173: .  draw - Krylov context 

175:    Output Parameters:
176: .  name - name of PetscDraw method 

178:    Level: advanced

180: @*/
181: int PetscDrawGetType(PetscDraw draw,PetscDrawType *type)
182: {
184:   *type = draw->type_name;
185:   return(0);
186: }

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

191:    Synopsis:
192:    int PetscDrawRegisterDynamic(char *name_solver,char *path,char *name_create,int (*routine_create)(PetscDraw))

194:    Not Collective

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

202:    Level: developer

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

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

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

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

221:    Concepts: graphics^registering new draw classes
222:    Concepts: PetscDraw^registering new draw classes

224: .seealso: PetscDrawRegisterAll(), PetscDrawRegisterDestroy()
225: M*/

227: int PetscDrawRegister(char *sname,char *path,char *name,int (*function)(PetscDraw))
228: {
229:   int  ierr;
230:   char fullname[256];

233:   PetscFListConcat(path,name,fullname);
234:   PetscFListAdd(&PetscDrawList,sname,fullname,(void (*)(void))function);
235:   return(0);
236: }

238: /*@C
239:    PetscDrawSetFromOptions - Sets the graphics type from the options database.
240:       Defaults to a PETSc X windows graphics.

242:    Collective on PetscDraw

244:    Input Parameter:
245: .     draw - the graphics context

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

252:    Level: intermediate

254:    Notes: 
255:     Must be called after PetscDrawCreate() before the PetscDrawtor is used.

257:     Concepts: drawing^setting options
258:     Concepts: graphics^setting options

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

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


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