Actual source code: pf.c

  1: /*$Id: pf.c,v 1.22 2001/04/10 19:37:43 bsmith Exp bsmith $*/
  2: /*
  3:     The PF mathematical functions interface routines, callable by users.
  4: */
 5:  #include src/pf/pfimpl.h

  7: PetscFList      PPetscFList = 0; /* list of all registered PD functions */
  8: PetscTruth PFRegisterAllCalled = PETSC_FALSE;

 10: /*@C
 11:    PFSet - Sets the C/C++/Fortran functions to be used by the PF function

 13:    Collective on PF

 15:    Input Parameter:
 16: +  pf - the function context
 17: .  apply - function to apply to an array
 18: .  applyvec - function to apply to a Vec
 19: .  view - function that prints information about the PF
 20: .  destroy - function to free the private function context
 21: -  ctx - private function context

 23:    Level: beginner

 25: .keywords: PF, setting

 27: .seealso: PFCreate(), PFDestroy(), PFSetType(), PFApply(), PFApplyVec()
 28: @*/
 29: int PFSet(PF pf,int(*apply)(void*,int,Scalar*,Scalar*),int(*applyvec)(void*,Vec,Vec),int(*view)(void*,PetscViewer),int(*destroy)(void*),void*ctx)
 30: {
 33:   pf->data             = ctx;

 35:   pf->ops->destroy     = destroy;
 36:   pf->ops->apply       = apply;
 37:   pf->ops->applyvec    = applyvec;
 38:   pf->ops->view        = view;

 40:   return(0);
 41: }

 43: /*@C
 44:    PFDestroy - Destroys PF context that was created with PFCreate().

 46:    Collective on PF

 48:    Input Parameter:
 49: .  pf - the function context

 51:    Level: beginner

 53: .keywords: PF, destroy

 55: .seealso: PFCreate(), PFSet(), PFSetType()
 56: @*/
 57: int PFDestroy(PF pf)
 58: {
 59:   int        ierr;
 60:   PetscTruth flg;

 64:   if (--pf->refct > 0) return(0);

 66:   PetscOptionsHasName(pf->prefix,"-pf_view",&flg);
 67:   if (flg) {
 68:     PFView(pf,PETSC_VIEWER_STDOUT_(pf->comm));
 69:   }

 71:   /* if memory was published with AMS then destroy it */
 72:   PetscObjectDepublish(pf);

 74:   if (pf->ops->destroy) { (*pf->ops->destroy)(pf->data);}
 75:   PetscLogObjectDestroy(pf);
 76:   PetscHeaderDestroy(pf);
 77:   return(0);
 78: }

 80: static int PFPublish_Petsc(PetscObject obj)
 81: {
 82: #if defined(PETSC_HAVE_AMS)
 83:   PF          v = (PF) obj;
 84:   int         ierr;
 85: #endif


 89: #if defined(PETSC_HAVE_AMS)
 90:   /* if it is already published then return */
 91:   if (v->amem >=0) return(0);

 93:   PetscObjectPublishBaseBegin(obj);
 94:   PetscObjectPublishBaseEnd(obj);
 95: #endif

 97:   return(0);
 98: }

100: /*@C
101:    PFCreate - Creates a mathematical function context.

103:    Collective on MPI_Comm

105:    Input Parameter:
106: +  comm - MPI communicator 
107: .  dimin - dimension of the space you are mapping from
108: -  dimout - dimension of the space you are mapping to

110:    Output Parameter:
111: .  pf - the function context

113:    Level: developer

115: .keywords: PF, create, context

117: .seealso: PFSetUp(), PFApply(), PFDestroy(), PFApplyVec()
118: @*/
119: int PFCreate(MPI_Comm comm,int dimin,int dimout,PF *pf)
120: {
121:   PF  newpf;

125:   *pf          = 0;

127:   PetscHeaderCreate(newpf,_p_PF,struct _PFOps,PF_COOKIE,-1,"PF",comm,PFDestroy,PFView);
128:   PetscLogObjectCreate(newpf);
129:   newpf->bops->publish    = PFPublish_Petsc;
130:   newpf->data             = 0;

132:   newpf->ops->destroy     = 0;
133:   newpf->ops->apply       = 0;
134:   newpf->ops->applyvec    = 0;
135:   newpf->ops->view        = 0;
136:   newpf->dimin            = dimin;
137:   newpf->dimout           = dimout;

139:   *pf                     = newpf;
140:   PetscPublishAll(pf);
141:   return(0);

143: }

145: /* -------------------------------------------------------------------------------*/

147: /*@
148:    PFApplyVec - Applies the mathematical function to a vector

150:    Collective on PF

152:    Input Parameters:
153: +  pf - the function context
154: -  x - input vector (or PETSC_NULL for the vector (0,1, .... N-1)

156:    Output Parameter:
157: .  y - output vector

159:    Level: beginner

161: .keywords: PF, apply

163: .seealso: PFApply(), PFCreate(), PFDestroy(), PFSetType(), PFSet()
164: @*/
165: int PFApplyVec(PF pf,Vec x,Vec y)
166: {
167:   int        ierr,i,rstart,rend;
168:   PetscTruth nox = PETSC_FALSE;

173:   if (x) {
175:     if (x == y) SETERRQ(PETSC_ERR_ARG_IDN,"x and y must be different vectors");
176:   } else {
177:     Scalar *xx;

179:     VecDuplicate(y,&x);
180:     nox  = PETSC_TRUE;
181:     VecGetOwnershipRange(x,&rstart,&rend);
182:     VecGetArray(x,&xx);
183:     for (i=rstart; i<rend; i++) {
184:       xx[i-rstart] = (Scalar)i;
185:     }
186:     VecRestoreArray(x,&xx);
187:   }

189:   if (pf->ops->applyvec) {
190:     (*pf->ops->applyvec)(pf->data,x,y);
191:   } else {
192:     Scalar *xx,*yy;
193:     int    n;

195:     VecGetLocalSize(x,&n);
196:     n    = n/pf->dimin;
197:     VecGetArray(x,&xx);
198:     VecGetArray(y,&yy);
199:     if (!pf->ops->apply) SETERRQ(1,"No function has been provided for this PF");
200:     (*pf->ops->apply)(pf->data,n,xx,yy);
201:     VecRestoreArray(x,&xx);
202:     VecRestoreArray(y,&yy);
203:   }
204:   if (nox) {
205:     VecDestroy(x);
206:   }
207:   return(0);
208: }

210: /*@
211:    PFApply - Applies the mathematical function to an array of values.

213:    Collective on PF

215:    Input Parameters:
216: +  pf - the function context
217: .  n - number of entries in input array
218: -  x - input array

220:    Output Parameter:
221: .  y - output array

223:    Level: beginner

225: .keywords: PF, apply

227: .seealso: PFApplyVec(), PFCreate(), PFDestroy(), PFSetType(), PFSet()
228: @*/
229: int PFApply(PF pf,int n,Scalar* x,Scalar* y)
230: {
231:   int        ierr;

235:   if (x == y) SETERRQ(PETSC_ERR_ARG_IDN,"x and y must be different arrays");
236:   if (!pf->ops->apply) SETERRQ(1,"No function has been provided for this PF");

238:   (*pf->ops->apply)(pf->data,n,x,y);
239:   return(0);
240: }

242: /*@ 
243:    PFView - Prints information about a mathematical function

245:    Collective on PF unless PetscViewer is PETSC_VIEWER_STDOUT_SELF  

247:    Input Parameters:
248: +  PF - the PF context
249: -  viewer - optional visualization context

251:    Note:
252:    The available visualization contexts include
253: +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
254: -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
255:          output where only the first processor opens
256:          the file.  All other processors send their 
257:          data to the first processor to print. 

259:    The user can open an alternative visualization contexts with
260:    PetscViewerASCIIOpen() (output to a specified file).

262:    Level: developer

264: .keywords: PF, view

266: .seealso: PetscViewerCreate(), PetscViewerASCIIOpen()
267: @*/
268: int PFView(PF pf,PetscViewer viewer)
269: {
270:   PFType            cstr;
271:   int               ierr;
272:   PetscTruth        isascii;
273:   PetscViewerFormat format;

277:   if (!viewer) viewer = PETSC_VIEWER_STDOUT_(pf->comm);

281:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);
282:   if (isascii) {
283:     PetscViewerGetFormat(viewer,&format);
284:     PetscViewerASCIIPrintf(viewer,"PF Object:n");
285:     PFGetType(pf,&cstr);
286:     if (cstr) {
287:       PetscViewerASCIIPrintf(viewer,"  type: %sn",cstr);
288:     } else {
289:       PetscViewerASCIIPrintf(viewer,"  type: not yet setn");
290:     }
291:     if (pf->ops->view) {
292:       PetscViewerASCIIPushTab(viewer);
293:       (*pf->ops->view)(pf->data,viewer);
294:       PetscViewerASCIIPopTab(viewer);
295:     }
296:   } else {
297:     SETERRQ1(1,"Viewer type %s not supported by PF",((PetscObject)viewer)->type_name);
298:   }
299:   return(0);
300: }

302: /*MC
303:    PFRegisterDynamic - Adds a method to the mathematical function package.

305:    Synopsis:
306:    int PFRegisterDynamic(char *name_solver,char *path,char *name_create,int (*routine_create)(PF))

308:    Not collective

310:    Input Parameters:
311: +  name_solver - name of a new user-defined solver
312: .  path - path (either absolute or relative) the library containing this solver
313: .  name_create - name of routine to create method context
314: -  routine_create - routine to create method context

316:    Notes:
317:    PFRegisterDynamic() may be called multiple times to add several user-defined functions

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

322:    Sample usage:
323: .vb
324:    PFRegisterDynamic("my_function","/home/username/my_lib/lib/libO/solaris/mylib",
325:               "MyFunctionCreate",MyFunctionSetCreate);
326: .ve

328:    Then, your solver can be chosen with the procedural interface via
329: $     PFSetType(pf,"my_function")
330:    or at runtime via the option
331: $     -pf_type my_function

333:    Level: advanced

335:    ${PETSC_ARCH}, ${PETSC_DIR}, ${PETSC_LIB_DIR}, ${BOPT}, or ${any environmental variable}
336:  occuring in pathname will be replaced with appropriate values.

338: .keywords: PF, register

340: .seealso: PFRegisterAll(), PFRegisterDestroy(), PFRegister()
341: M*/

343: int PFRegister(char *sname,char *path,char *name,int (*function)(PF,void*))
344: {
345:   int  ierr;
346:   char fullname[256];

349:   PetscFListConcat(path,name,fullname);
350:   PetscFListAdd(&PPetscFList,sname,fullname,(void (*)())function);
351:   return(0);
352: }



356: /*@C
357:    PFGetType - Gets the PF method type and name (as a string) from the PF
358:    context.

360:    Not Collective

362:    Input Parameter:
363: .  pf - the function context

365:    Output Parameter:
366: .  name - name of function 

368:    Level: intermediate

370: .keywords: PF, get, method, name, type

372: .seealso: PFSetType()

374: @*/
375: int PFGetType(PF pf,PFType *meth)
376: {
378:   *meth = (PFType) pf->type_name;
379:   return(0);
380: }


383: /*@C
384:    PFSetType - Builds PF for a particular function

386:    Collective on PF

388:    Input Parameter:
389: +  pf - the function context.
390: .  type - a known method
391: -  ctx - optional type dependent context

393:    Options Database Key:
394: .  -pf_type <type> - Sets PF type


397:   Notes:
398:   See "petsc/include/petscpf.h" for available methods (for instance,
399:   PFCONSTANT)

401:   Level: intermediate

403: .keywords: PF, set, method, type

405: .seealso: PFSet(), PFRegisterDynamic(), PFCreate(), DACreatePF()

407: @*/
408: int PFSetType(PF pf,PFType type,void *ctx)
409: {
410:   int        ierr,(*r)(PF,void*);
411:   PetscTruth match;


417:   PetscTypeCompare((PetscObject)pf,type,&match);
418:   if (match) return(0);

420:   if (pf->ops->destroy) { (*pf->ops->destroy)(pf);}
421:   pf->data        = 0;

423:   /* Get the function pointers for the method requested */
424:   if (!PFRegisterAllCalled) {PFRegisterAll(0);}

426:   /* Determine the PFCreateXXX routine for a particular function */
427:    PetscFListFind(pf->comm,PPetscFList,type,(void (**)(void)) &r);
428:   if (!r) SETERRQ1(1,"Unable to find requested PF type %s",type);

430:   pf->ops->destroy             = 0;
431:   pf->ops->view                = 0;
432:   pf->ops->apply               = 0;
433:   pf->ops->applyvec            = 0;

435:   /* Call the PFCreateXXX routine for this particular function */
436:   (*r)(pf,ctx);

438:   PetscObjectChangeTypeName((PetscObject)pf,type);
439:   return(0);
440: }

442: /*@
443:    PFSetFromOptions - Sets PF options from the options database.

445:    Collective on PF

447:    Input Parameters:
448: .  pf - the mathematical function context

450:    Options Database Keys:

452:    Notes:  
453:    To see all options, run your program with the -help option
454:    or consult the users manual.

456:    Level: intermediate

458: .keywords: PF, set, from, options, database

460: .seealso:
461: @*/
462: int PFSetFromOptions(PF pf)
463: {
464:   int        ierr;
465:   char       type[256];
466:   PetscTruth flg;


471:   if (!PFRegisterAllCalled) {PFRegisterAll(0);}
472:   PetscOptionsBegin(pf->comm,pf->prefix,"Mathematical functions options","Vec");
473:     PetscOptionsList("-pf_type","Type of function","PFSetType",PPetscFList,0,type,256,&flg);
474:     if (flg) {
475:       PFSetType(pf,type,PETSC_NULL);
476:     }
477:     if (pf->ops->setfromoptions) {
478:       (*pf->ops->setfromoptions)(pf);
479:     }
480:   PetscOptionsEnd();

482:   return(0);
483: }