Actual source code: pf.c
1: /*
2: The PF mathematical functions interface routines, callable by users.
3: */
4: #include <../src/vec/pf/pfimpl.h>
6: PetscClassId PF_CLASSID = 0;
7: PetscFunctionList PFList = NULL; /* list of all registered PD functions */
8: PetscBool 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 Parameters:
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: .seealso: PFCreate(), PFDestroy(), PFSetType(), PFApply(), PFApplyVec()
26: @*/
27: PetscErrorCode PFSet(PF pf,PetscErrorCode (*apply)(void*,PetscInt,const PetscScalar*,PetscScalar*),PetscErrorCode (*applyvec)(void*,Vec,Vec),PetscErrorCode (*view)(void*,PetscViewer),PetscErrorCode (*destroy)(void*),void*ctx)
28: {
31: pf->data = ctx;
32: pf->ops->destroy = destroy;
33: pf->ops->apply = apply;
34: pf->ops->applyvec = applyvec;
35: pf->ops->view = view;
36: return(0);
37: }
39: /*@C
40: PFDestroy - Destroys PF context that was created with PFCreate().
42: Collective on PF
44: Input Parameter:
45: . pf - the function context
47: Level: beginner
49: .seealso: PFCreate(), PFSet(), PFSetType()
50: @*/
51: PetscErrorCode PFDestroy(PF *pf)
52: {
56: if (!*pf) return(0);
58: if (--((PetscObject)(*pf))->refct > 0) return(0);
60: PFViewFromOptions(*pf,NULL,"-pf_view");
61: /* if memory was published with SAWs then destroy it */
62: PetscObjectSAWsViewOff((PetscObject)*pf);
64: if ((*pf)->ops->destroy) { (*(*pf)->ops->destroy)((*pf)->data);}
65: PetscHeaderDestroy(pf);
66: return(0);
67: }
69: /*@C
70: PFCreate - Creates a mathematical function context.
72: Collective
74: Input Parameters:
75: + comm - MPI communicator
76: . dimin - dimension of the space you are mapping from
77: - dimout - dimension of the space you are mapping to
79: Output Parameter:
80: . pf - the function context
82: Level: developer
84: .seealso: PFSet(), PFApply(), PFDestroy(), PFApplyVec()
85: @*/
86: PetscErrorCode PFCreate(MPI_Comm comm,PetscInt dimin,PetscInt dimout,PF *pf)
87: {
88: PF newpf;
93: *pf = NULL;
94: PFInitializePackage();
96: PetscHeaderCreate(newpf,PF_CLASSID,"PF","Mathematical functions","Vec",comm,PFDestroy,PFView);
97: newpf->data = NULL;
98: newpf->ops->destroy = NULL;
99: newpf->ops->apply = NULL;
100: newpf->ops->applyvec = NULL;
101: newpf->ops->view = NULL;
102: newpf->dimin = dimin;
103: newpf->dimout = dimout;
105: *pf = newpf;
106: return(0);
108: }
110: /* -------------------------------------------------------------------------------*/
112: /*@
113: PFApplyVec - Applies the mathematical function to a vector
115: Collective on PF
117: Input Parameters:
118: + pf - the function context
119: - x - input vector (or NULL for the vector (0,1, .... N-1)
121: Output Parameter:
122: . y - output vector
124: Level: beginner
126: .seealso: PFApply(), PFCreate(), PFDestroy(), PFSetType(), PFSet()
127: @*/
128: PetscErrorCode PFApplyVec(PF pf,Vec x,Vec y)
129: {
131: PetscInt i,rstart,rend,n,p;
132: PetscBool nox = PETSC_FALSE;
137: if (x) {
139: if (x == y) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"x and y must be different vectors");
140: } else {
141: PetscScalar *xx;
142: PetscInt lsize;
144: VecGetLocalSize(y,&lsize);
145: lsize = pf->dimin*lsize/pf->dimout;
146: VecCreateMPI(PetscObjectComm((PetscObject)y),lsize,PETSC_DETERMINE,&x);
147: nox = PETSC_TRUE;
148: VecGetOwnershipRange(x,&rstart,&rend);
149: VecGetArray(x,&xx);
150: for (i=rstart; i<rend; i++) xx[i-rstart] = (PetscScalar)i;
151: VecRestoreArray(x,&xx);
152: }
154: VecGetLocalSize(x,&n);
155: VecGetLocalSize(y,&p);
156: if ((pf->dimin*(n/pf->dimin)) != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local input vector length %D not divisible by dimin %D of function",n,pf->dimin);
157: if ((pf->dimout*(p/pf->dimout)) != p) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local output vector length %D not divisible by dimout %D of function",p,pf->dimout);
158: if ((n/pf->dimin) != (p/pf->dimout)) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local vector lengths %D %D are wrong for dimin and dimout %D %D of function",n,p,pf->dimin,pf->dimout);
160: if (pf->ops->applyvec) {
161: (*pf->ops->applyvec)(pf->data,x,y);
162: } else {
163: PetscScalar *xx,*yy;
165: VecGetLocalSize(x,&n);
166: n = n/pf->dimin;
167: VecGetArray(x,&xx);
168: VecGetArray(y,&yy);
169: if (!pf->ops->apply) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No function has been provided for this PF");
170: (*pf->ops->apply)(pf->data,n,xx,yy);
171: VecRestoreArray(x,&xx);
172: VecRestoreArray(y,&yy);
173: }
174: if (nox) {
175: VecDestroy(&x);
176: }
177: return(0);
178: }
180: /*@
181: PFApply - Applies the mathematical function to an array of values.
183: Collective on PF
185: Input Parameters:
186: + pf - the function context
187: . n - number of pointwise function evaluations to perform, each pointwise function evaluation
188: is a function of dimin variables and computes dimout variables where dimin and dimout are defined
189: in the call to PFCreate()
190: - x - input array
192: Output Parameter:
193: . y - output array
195: Level: beginner
197: Notes:
199: .seealso: PFApplyVec(), PFCreate(), PFDestroy(), PFSetType(), PFSet()
200: @*/
201: PetscErrorCode PFApply(PF pf,PetscInt n,const PetscScalar *x,PetscScalar *y)
202: {
209: if (x == y) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"x and y must be different arrays");
210: if (!pf->ops->apply) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No function has been provided for this PF");
212: (*pf->ops->apply)(pf->data,n,x,y);
213: return(0);
214: }
216: /*@C
217: PFViewFromOptions - View from Options
219: Collective on PF
221: Input Parameters:
222: + A - the PF context
223: . obj - Optional object
224: - name - command line option
226: Level: intermediate
227: .seealso: PF, PFView, PetscObjectViewFromOptions(), PFCreate()
228: @*/
229: PetscErrorCode PFViewFromOptions(PF A,PetscObject obj,const char name[])
230: {
235: PetscObjectViewFromOptions((PetscObject)A,obj,name);
236: return(0);
237: }
239: /*@
240: PFView - Prints information about a mathematical function
242: Collective on PF unless PetscViewer is PETSC_VIEWER_STDOUT_SELF
244: Input Parameters:
245: + PF - the PF context
246: - viewer - optional visualization context
248: Note:
249: The available visualization contexts include
250: + PETSC_VIEWER_STDOUT_SELF - standard output (default)
251: - PETSC_VIEWER_STDOUT_WORLD - synchronized standard
252: output where only the first processor opens
253: the file. All other processors send their
254: data to the first processor to print.
256: The user can open an alternative visualization contexts with
257: PetscViewerASCIIOpen() (output to a specified file).
259: Level: developer
261: .seealso: PetscViewerCreate(), PetscViewerASCIIOpen()
262: @*/
263: PetscErrorCode PFView(PF pf,PetscViewer viewer)
264: {
265: PetscErrorCode ierr;
266: PetscBool iascii;
267: PetscViewerFormat format;
271: if (!viewer) {
272: PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)pf),&viewer);
273: }
277: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
278: if (iascii) {
279: PetscViewerGetFormat(viewer,&format);
280: PetscObjectPrintClassNamePrefixType((PetscObject)pf,viewer);
281: if (pf->ops->view) {
282: PetscViewerASCIIPushTab(viewer);
283: (*pf->ops->view)(pf->data,viewer);
284: PetscViewerASCIIPopTab(viewer);
285: }
286: }
287: return(0);
288: }
290: /*@C
291: PFRegister - Adds a method to the mathematical function package.
293: Not collective
295: Input Parameters:
296: + name_solver - name of a new user-defined solver
297: - routine_create - routine to create method context
299: Notes:
300: PFRegister() may be called multiple times to add several user-defined functions
302: Sample usage:
303: .vb
304: PFRegister("my_function",MyFunctionSetCreate);
305: .ve
307: Then, your solver can be chosen with the procedural interface via
308: $ PFSetType(pf,"my_function")
309: or at runtime via the option
310: $ -pf_type my_function
312: Level: advanced
314: .seealso: PFRegisterAll(), PFRegisterDestroy(), PFRegister()
315: @*/
316: PetscErrorCode PFRegister(const char sname[],PetscErrorCode (*function)(PF,void*))
317: {
321: PFInitializePackage();
322: PetscFunctionListAdd(&PFList,sname,function);
323: return(0);
324: }
326: /*@C
327: PFGetType - Gets the PF method type and name (as a string) from the PF
328: context.
330: Not Collective
332: Input Parameter:
333: . pf - the function context
335: Output Parameter:
336: . type - name of function
338: Level: intermediate
340: .seealso: PFSetType()
342: @*/
343: PetscErrorCode PFGetType(PF pf,PFType *type)
344: {
348: *type = ((PetscObject)pf)->type_name;
349: return(0);
350: }
352: /*@C
353: PFSetType - Builds PF for a particular function
355: Collective on PF
357: Input Parameters:
358: + pf - the function context.
359: . type - a known method
360: - ctx - optional type dependent context
362: Options Database Key:
363: . -pf_type <type> - Sets PF type
365: Notes:
366: See "petsc/include/petscpf.h" for available methods (for instance,
367: PFCONSTANT)
369: Level: intermediate
371: .seealso: PFSet(), PFRegister(), PFCreate(), DMDACreatePF()
373: @*/
374: PetscErrorCode PFSetType(PF pf,PFType type,void *ctx)
375: {
376: PetscErrorCode ierr,(*r)(PF,void*);
377: PetscBool match;
383: PetscObjectTypeCompare((PetscObject)pf,type,&match);
384: if (match) return(0);
386: if (pf->ops->destroy) { (*pf->ops->destroy)(pf);}
387: pf->data = NULL;
389: /* Determine the PFCreateXXX routine for a particular function */
390: PetscFunctionListFind(PFList,type,&r);
391: if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested PF type %s",type);
392: pf->ops->destroy = NULL;
393: pf->ops->view = NULL;
394: pf->ops->apply = NULL;
395: pf->ops->applyvec = NULL;
397: /* Call the PFCreateXXX routine for this particular function */
398: (*r)(pf,ctx);
400: PetscObjectChangeTypeName((PetscObject)pf,type);
401: return(0);
402: }
404: /*@
405: PFSetFromOptions - Sets PF options from the options database.
407: Collective on PF
409: Input Parameters:
410: . pf - the mathematical function context
412: Options Database Keys:
414: Notes:
415: To see all options, run your program with the -help option
416: or consult the users manual.
418: Level: intermediate
420: .seealso:
421: @*/
422: PetscErrorCode PFSetFromOptions(PF pf)
423: {
425: char type[256];
426: PetscBool flg;
431: PetscObjectOptionsBegin((PetscObject)pf);
432: PetscOptionsFList("-pf_type","Type of function","PFSetType",PFList,NULL,type,256,&flg);
433: if (flg) {
434: PFSetType(pf,type,NULL);
435: }
436: if (pf->ops->setfromoptions) {
437: (*pf->ops->setfromoptions)(PetscOptionsObject,pf);
438: }
440: /* process any options handlers added with PetscObjectAddOptionsHandler() */
441: PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)pf);
442: PetscOptionsEnd();
443: return(0);
444: }
446: static PetscBool PFPackageInitialized = PETSC_FALSE;
447: /*@C
448: PFFinalizePackage - This function destroys everything in the Petsc interface to Mathematica. It is
449: called from PetscFinalize().
451: Level: developer
453: .seealso: PetscFinalize()
454: @*/
455: PetscErrorCode PFFinalizePackage(void)
456: {
460: PetscFunctionListDestroy(&PFList);
461: PFPackageInitialized = PETSC_FALSE;
462: PFRegisterAllCalled = PETSC_FALSE;
463: return(0);
464: }
466: /*@C
467: PFInitializePackage - This function initializes everything in the PF package. It is called
468: from PetscDLLibraryRegister_petscvec() when using dynamic libraries, and on the first call to PFCreate()
469: when using shared or static libraries.
471: Level: developer
473: .seealso: PetscInitialize()
474: @*/
475: PetscErrorCode PFInitializePackage(void)
476: {
477: char logList[256];
478: PetscBool opt,pkg;
482: if (PFPackageInitialized) return(0);
483: PFPackageInitialized = PETSC_TRUE;
484: /* Register Classes */
485: PetscClassIdRegister("PointFunction",&PF_CLASSID);
486: /* Register Constructors */
487: PFRegisterAll();
488: /* Process Info */
489: {
490: PetscClassId classids[1];
492: classids[0] = PF_CLASSID;
493: PetscInfoProcessClass("pf", 1, classids);
494: }
495: /* Process summary exclusions */
496: PetscOptionsGetString(NULL,NULL,"-log_exclude",logList,sizeof(logList),&opt);
497: if (opt) {
498: PetscStrInList("pf",logList,',',&pkg);
499: if (pkg) {PetscLogEventExcludeClass(PF_CLASSID);}
500: }
501: /* Register package finalizer */
502: PetscRegisterFinalize(PFFinalizePackage);
503: return(0);
504: }