xref: /petsc/src/vec/pf/interface/pf.c (revision 292f8084fb157dadf9a2ae26c5bd14368ed7ffcb)
1*292f8084SBarry Smith /*
2*292f8084SBarry Smith     The PF mathematical functions interface routines, callable by users.
3*292f8084SBarry Smith */
4*292f8084SBarry Smith #include "src/pf/pfimpl.h"            /*I "petscpf.h" I*/
5*292f8084SBarry Smith 
6*292f8084SBarry Smith /* Logging support */
7*292f8084SBarry Smith PetscCookie PF_COOKIE = 0;
8*292f8084SBarry Smith 
9*292f8084SBarry Smith PetscFList PPetscFList         = PETSC_NULL; /* list of all registered PD functions */
10*292f8084SBarry Smith PetscTruth PFRegisterAllCalled = PETSC_FALSE;
11*292f8084SBarry Smith 
12*292f8084SBarry Smith #undef __FUNCT__
13*292f8084SBarry Smith #define __FUNCT__ "PFSet"
14*292f8084SBarry Smith /*@C
15*292f8084SBarry Smith    PFSet - Sets the C/C++/Fortran functions to be used by the PF function
16*292f8084SBarry Smith 
17*292f8084SBarry Smith    Collective on PF
18*292f8084SBarry Smith 
19*292f8084SBarry Smith    Input Parameter:
20*292f8084SBarry Smith +  pf - the function context
21*292f8084SBarry Smith .  apply - function to apply to an array
22*292f8084SBarry Smith .  applyvec - function to apply to a Vec
23*292f8084SBarry Smith .  view - function that prints information about the PF
24*292f8084SBarry Smith .  destroy - function to free the private function context
25*292f8084SBarry Smith -  ctx - private function context
26*292f8084SBarry Smith 
27*292f8084SBarry Smith    Level: beginner
28*292f8084SBarry Smith 
29*292f8084SBarry Smith .keywords: PF, setting
30*292f8084SBarry Smith 
31*292f8084SBarry Smith .seealso: PFCreate(), PFDestroy(), PFSetType(), PFApply(), PFApplyVec()
32*292f8084SBarry Smith @*/
33*292f8084SBarry Smith PetscErrorCode PFSet(PF pf,PetscErrorCode (*apply)(void*,PetscInt,PetscScalar*,PetscScalar*),PetscErrorCode (*applyvec)(void*,Vec,Vec),PetscErrorCode (*view)(void*,PetscViewer),PetscErrorCode (*destroy)(void*),void*ctx)
34*292f8084SBarry Smith {
35*292f8084SBarry Smith   PetscFunctionBegin;
36*292f8084SBarry Smith   PetscValidHeaderSpecific(pf,PF_COOKIE,1);
37*292f8084SBarry Smith   pf->data             = ctx;
38*292f8084SBarry Smith 
39*292f8084SBarry Smith   pf->ops->destroy     = destroy;
40*292f8084SBarry Smith   pf->ops->apply       = apply;
41*292f8084SBarry Smith   pf->ops->applyvec    = applyvec;
42*292f8084SBarry Smith   pf->ops->view        = view;
43*292f8084SBarry Smith 
44*292f8084SBarry Smith   PetscFunctionReturn(0);
45*292f8084SBarry Smith }
46*292f8084SBarry Smith 
47*292f8084SBarry Smith #undef __FUNCT__
48*292f8084SBarry Smith #define __FUNCT__ "PFDestroy"
49*292f8084SBarry Smith /*@C
50*292f8084SBarry Smith    PFDestroy - Destroys PF context that was created with PFCreate().
51*292f8084SBarry Smith 
52*292f8084SBarry Smith    Collective on PF
53*292f8084SBarry Smith 
54*292f8084SBarry Smith    Input Parameter:
55*292f8084SBarry Smith .  pf - the function context
56*292f8084SBarry Smith 
57*292f8084SBarry Smith    Level: beginner
58*292f8084SBarry Smith 
59*292f8084SBarry Smith .keywords: PF, destroy
60*292f8084SBarry Smith 
61*292f8084SBarry Smith .seealso: PFCreate(), PFSet(), PFSetType()
62*292f8084SBarry Smith @*/
63*292f8084SBarry Smith PetscErrorCode PFDestroy(PF pf)
64*292f8084SBarry Smith {
65*292f8084SBarry Smith   PetscErrorCode ierr;
66*292f8084SBarry Smith   PetscTruth flg;
67*292f8084SBarry Smith 
68*292f8084SBarry Smith   PetscFunctionBegin;
69*292f8084SBarry Smith   PetscValidHeaderSpecific(pf,PF_COOKIE,1);
70*292f8084SBarry Smith   if (--pf->refct > 0) PetscFunctionReturn(0);
71*292f8084SBarry Smith 
72*292f8084SBarry Smith   ierr = PetscOptionsHasName(pf->prefix,"-pf_view",&flg);CHKERRQ(ierr);
73*292f8084SBarry Smith   if (flg) {
74*292f8084SBarry Smith     ierr = PFView(pf,PETSC_VIEWER_STDOUT_(pf->comm));CHKERRQ(ierr);
75*292f8084SBarry Smith   }
76*292f8084SBarry Smith 
77*292f8084SBarry Smith   /* if memory was published with AMS then destroy it */
78*292f8084SBarry Smith   ierr = PetscObjectDepublish(pf);CHKERRQ(ierr);
79*292f8084SBarry Smith 
80*292f8084SBarry Smith   if (pf->ops->destroy) {ierr =  (*pf->ops->destroy)(pf->data);CHKERRQ(ierr);}
81*292f8084SBarry Smith   PetscLogObjectDestroy(pf);
82*292f8084SBarry Smith   PetscHeaderDestroy(pf);
83*292f8084SBarry Smith   PetscFunctionReturn(0);
84*292f8084SBarry Smith }
85*292f8084SBarry Smith 
86*292f8084SBarry Smith #undef __FUNCT__
87*292f8084SBarry Smith #define __FUNCT__ "PFPublish_Petsc"
88*292f8084SBarry Smith static PetscErrorCode PFPublish_Petsc(PetscObject obj)
89*292f8084SBarry Smith {
90*292f8084SBarry Smith #if defined(PETSC_HAVE_AMS)
91*292f8084SBarry Smith   PF          v = (PF) obj;
92*292f8084SBarry Smith   PetscErrorCode ierr;
93*292f8084SBarry Smith #endif
94*292f8084SBarry Smith 
95*292f8084SBarry Smith   PetscFunctionBegin;
96*292f8084SBarry Smith 
97*292f8084SBarry Smith #if defined(PETSC_HAVE_AMS)
98*292f8084SBarry Smith   /* if it is already published then return */
99*292f8084SBarry Smith   if (v->amem >=0) PetscFunctionReturn(0);
100*292f8084SBarry Smith 
101*292f8084SBarry Smith   ierr = PetscObjectPublishBaseBegin(obj);CHKERRQ(ierr);
102*292f8084SBarry Smith   ierr = PetscObjectPublishBaseEnd(obj);CHKERRQ(ierr);
103*292f8084SBarry Smith #endif
104*292f8084SBarry Smith 
105*292f8084SBarry Smith   PetscFunctionReturn(0);
106*292f8084SBarry Smith }
107*292f8084SBarry Smith 
108*292f8084SBarry Smith #undef __FUNCT__
109*292f8084SBarry Smith #define __FUNCT__ "PFCreate"
110*292f8084SBarry Smith /*@C
111*292f8084SBarry Smith    PFCreate - Creates a mathematical function context.
112*292f8084SBarry Smith 
113*292f8084SBarry Smith    Collective on MPI_Comm
114*292f8084SBarry Smith 
115*292f8084SBarry Smith    Input Parameter:
116*292f8084SBarry Smith +  comm - MPI communicator
117*292f8084SBarry Smith .  dimin - dimension of the space you are mapping from
118*292f8084SBarry Smith -  dimout - dimension of the space you are mapping to
119*292f8084SBarry Smith 
120*292f8084SBarry Smith    Output Parameter:
121*292f8084SBarry Smith .  pf - the function context
122*292f8084SBarry Smith 
123*292f8084SBarry Smith    Level: developer
124*292f8084SBarry Smith 
125*292f8084SBarry Smith .keywords: PF, create, context
126*292f8084SBarry Smith 
127*292f8084SBarry Smith .seealso: PFSetUp(), PFApply(), PFDestroy(), PFApplyVec()
128*292f8084SBarry Smith @*/
129*292f8084SBarry Smith PetscErrorCode PFCreate(MPI_Comm comm,PetscInt dimin,PetscInt dimout,PF *pf)
130*292f8084SBarry Smith {
131*292f8084SBarry Smith   PF  newpf;
132*292f8084SBarry Smith   PetscErrorCode ierr;
133*292f8084SBarry Smith 
134*292f8084SBarry Smith   PetscFunctionBegin;
135*292f8084SBarry Smith   PetscValidPointer(pf,1);
136*292f8084SBarry Smith   *pf = PETSC_NULL;
137*292f8084SBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
138*292f8084SBarry Smith   ierr = VecInitializePackage(PETSC_NULL);CHKERRQ(ierr);
139*292f8084SBarry Smith #endif
140*292f8084SBarry Smith 
141*292f8084SBarry Smith   PetscHeaderCreate(newpf,_p_PF,struct _PFOps,PF_COOKIE,-1,"PF",comm,PFDestroy,PFView);
142*292f8084SBarry Smith   PetscLogObjectCreate(newpf);
143*292f8084SBarry Smith   newpf->bops->publish    = PFPublish_Petsc;
144*292f8084SBarry Smith   newpf->data             = 0;
145*292f8084SBarry Smith 
146*292f8084SBarry Smith   newpf->ops->destroy     = 0;
147*292f8084SBarry Smith   newpf->ops->apply       = 0;
148*292f8084SBarry Smith   newpf->ops->applyvec    = 0;
149*292f8084SBarry Smith   newpf->ops->view        = 0;
150*292f8084SBarry Smith   newpf->dimin            = dimin;
151*292f8084SBarry Smith   newpf->dimout           = dimout;
152*292f8084SBarry Smith 
153*292f8084SBarry Smith   *pf                     = newpf;
154*292f8084SBarry Smith   ierr = PetscPublishAll(pf);CHKERRQ(ierr);
155*292f8084SBarry Smith   PetscFunctionReturn(0);
156*292f8084SBarry Smith 
157*292f8084SBarry Smith }
158*292f8084SBarry Smith 
159*292f8084SBarry Smith /* -------------------------------------------------------------------------------*/
160*292f8084SBarry Smith 
161*292f8084SBarry Smith #undef __FUNCT__
162*292f8084SBarry Smith #define __FUNCT__ "PFApplyVec"
163*292f8084SBarry Smith /*@
164*292f8084SBarry Smith    PFApplyVec - Applies the mathematical function to a vector
165*292f8084SBarry Smith 
166*292f8084SBarry Smith    Collective on PF
167*292f8084SBarry Smith 
168*292f8084SBarry Smith    Input Parameters:
169*292f8084SBarry Smith +  pf - the function context
170*292f8084SBarry Smith -  x - input vector (or PETSC_NULL for the vector (0,1, .... N-1)
171*292f8084SBarry Smith 
172*292f8084SBarry Smith    Output Parameter:
173*292f8084SBarry Smith .  y - output vector
174*292f8084SBarry Smith 
175*292f8084SBarry Smith    Level: beginner
176*292f8084SBarry Smith 
177*292f8084SBarry Smith .keywords: PF, apply
178*292f8084SBarry Smith 
179*292f8084SBarry Smith .seealso: PFApply(), PFCreate(), PFDestroy(), PFSetType(), PFSet()
180*292f8084SBarry Smith @*/
181*292f8084SBarry Smith PetscErrorCode PFApplyVec(PF pf,Vec x,Vec y)
182*292f8084SBarry Smith {
183*292f8084SBarry Smith   PetscErrorCode ierr;
184*292f8084SBarry Smith   PetscInt i,rstart,rend,n,p;
185*292f8084SBarry Smith   PetscTruth nox = PETSC_FALSE;
186*292f8084SBarry Smith 
187*292f8084SBarry Smith   PetscFunctionBegin;
188*292f8084SBarry Smith   PetscValidHeaderSpecific(pf,PF_COOKIE,1);
189*292f8084SBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE,3);
190*292f8084SBarry Smith   if (x) {
191*292f8084SBarry Smith     PetscValidHeaderSpecific(x,VEC_COOKIE,2);
192*292f8084SBarry Smith     if (x == y) SETERRQ(PETSC_ERR_ARG_IDN,"x and y must be different vectors");
193*292f8084SBarry Smith   } else {
194*292f8084SBarry Smith     PetscScalar *xx;
195*292f8084SBarry Smith 
196*292f8084SBarry Smith     ierr = VecDuplicate(y,&x);CHKERRQ(ierr);
197*292f8084SBarry Smith     nox  = PETSC_TRUE;
198*292f8084SBarry Smith     ierr = VecGetOwnershipRange(x,&rstart,&rend);CHKERRQ(ierr);
199*292f8084SBarry Smith     ierr = VecGetArray(x,&xx);CHKERRQ(ierr);
200*292f8084SBarry Smith     for (i=rstart; i<rend; i++) {
201*292f8084SBarry Smith       xx[i-rstart] = (PetscScalar)i;
202*292f8084SBarry Smith     }
203*292f8084SBarry Smith     ierr = VecRestoreArray(x,&xx);CHKERRQ(ierr);
204*292f8084SBarry Smith   }
205*292f8084SBarry Smith 
206*292f8084SBarry Smith   ierr = VecGetLocalSize(x,&n);CHKERRQ(ierr);
207*292f8084SBarry Smith   ierr = VecGetLocalSize(y,&p);CHKERRQ(ierr);
208*292f8084SBarry Smith   if (pf->dimin*(n/pf->dimin) != n) SETERRQ2(PETSC_ERR_ARG_IDN,"Local input vector length %d not divisible by dimin %d of function",n,pf->dimin);
209*292f8084SBarry Smith   if (pf->dimout*(p/pf->dimout) != p) SETERRQ2(PETSC_ERR_ARG_IDN,"Local output vector length %d not divisible by dimout %d of function",p,pf->dimout);
210*292f8084SBarry Smith   if (n/pf->dimin != p/pf->dimout) SETERRQ4(PETSC_ERR_ARG_IDN,"Local vector lengths %d %d are wrong for dimin and dimout %d %d of function",n,p,pf->dimin,pf->dimout);
211*292f8084SBarry Smith 
212*292f8084SBarry Smith   if (pf->ops->applyvec) {
213*292f8084SBarry Smith     ierr = (*pf->ops->applyvec)(pf->data,x,y);CHKERRQ(ierr);
214*292f8084SBarry Smith   } else {
215*292f8084SBarry Smith     PetscScalar *xx,*yy;
216*292f8084SBarry Smith 
217*292f8084SBarry Smith     ierr = VecGetLocalSize(x,&n);CHKERRQ(ierr);
218*292f8084SBarry Smith     n    = n/pf->dimin;
219*292f8084SBarry Smith     ierr = VecGetArray(x,&xx);CHKERRQ(ierr);
220*292f8084SBarry Smith     ierr = VecGetArray(y,&yy);CHKERRQ(ierr);
221*292f8084SBarry Smith     if (!pf->ops->apply) SETERRQ(1,"No function has been provided for this PF");
222*292f8084SBarry Smith     ierr = (*pf->ops->apply)(pf->data,n,xx,yy);CHKERRQ(ierr);
223*292f8084SBarry Smith     ierr = VecRestoreArray(x,&xx);CHKERRQ(ierr);
224*292f8084SBarry Smith     ierr = VecRestoreArray(y,&yy);CHKERRQ(ierr);
225*292f8084SBarry Smith   }
226*292f8084SBarry Smith   if (nox) {
227*292f8084SBarry Smith     ierr = VecDestroy(x);CHKERRQ(ierr);
228*292f8084SBarry Smith   }
229*292f8084SBarry Smith   PetscFunctionReturn(0);
230*292f8084SBarry Smith }
231*292f8084SBarry Smith 
232*292f8084SBarry Smith #undef __FUNCT__
233*292f8084SBarry Smith #define __FUNCT__ "PFApply"
234*292f8084SBarry Smith /*@
235*292f8084SBarry Smith    PFApply - Applies the mathematical function to an array of values.
236*292f8084SBarry Smith 
237*292f8084SBarry Smith    Collective on PF
238*292f8084SBarry Smith 
239*292f8084SBarry Smith    Input Parameters:
240*292f8084SBarry Smith +  pf - the function context
241*292f8084SBarry Smith .  n - number of pointwise function evaluations to perform, each pointwise function evaluation
242*292f8084SBarry Smith        is a function of dimin variables and computes dimout variables where dimin and dimout are defined
243*292f8084SBarry Smith        in the call to PFCreate()
244*292f8084SBarry Smith -  x - input array
245*292f8084SBarry Smith 
246*292f8084SBarry Smith    Output Parameter:
247*292f8084SBarry Smith .  y - output array
248*292f8084SBarry Smith 
249*292f8084SBarry Smith    Level: beginner
250*292f8084SBarry Smith 
251*292f8084SBarry Smith    Notes:
252*292f8084SBarry Smith 
253*292f8084SBarry Smith .keywords: PF, apply
254*292f8084SBarry Smith 
255*292f8084SBarry Smith .seealso: PFApplyVec(), PFCreate(), PFDestroy(), PFSetType(), PFSet()
256*292f8084SBarry Smith @*/
257*292f8084SBarry Smith PetscErrorCode PFApply(PF pf,PetscInt n,PetscScalar* x,PetscScalar* y)
258*292f8084SBarry Smith {
259*292f8084SBarry Smith   PetscErrorCode ierr;
260*292f8084SBarry Smith 
261*292f8084SBarry Smith   PetscFunctionBegin;
262*292f8084SBarry Smith   PetscValidHeaderSpecific(pf,PF_COOKIE,1);
263*292f8084SBarry Smith   PetscValidScalarPointer(x,2);
264*292f8084SBarry Smith   PetscValidScalarPointer(y,3);
265*292f8084SBarry Smith   if (x == y) SETERRQ(PETSC_ERR_ARG_IDN,"x and y must be different arrays");
266*292f8084SBarry Smith   if (!pf->ops->apply) SETERRQ(1,"No function has been provided for this PF");
267*292f8084SBarry Smith 
268*292f8084SBarry Smith   ierr = (*pf->ops->apply)(pf->data,n,x,y);CHKERRQ(ierr);
269*292f8084SBarry Smith   PetscFunctionReturn(0);
270*292f8084SBarry Smith }
271*292f8084SBarry Smith 
272*292f8084SBarry Smith #undef __FUNCT__
273*292f8084SBarry Smith #define __FUNCT__ "PFView"
274*292f8084SBarry Smith /*@
275*292f8084SBarry Smith    PFView - Prints information about a mathematical function
276*292f8084SBarry Smith 
277*292f8084SBarry Smith    Collective on PF unless PetscViewer is PETSC_VIEWER_STDOUT_SELF
278*292f8084SBarry Smith 
279*292f8084SBarry Smith    Input Parameters:
280*292f8084SBarry Smith +  PF - the PF context
281*292f8084SBarry Smith -  viewer - optional visualization context
282*292f8084SBarry Smith 
283*292f8084SBarry Smith    Note:
284*292f8084SBarry Smith    The available visualization contexts include
285*292f8084SBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
286*292f8084SBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
287*292f8084SBarry Smith          output where only the first processor opens
288*292f8084SBarry Smith          the file.  All other processors send their
289*292f8084SBarry Smith          data to the first processor to print.
290*292f8084SBarry Smith 
291*292f8084SBarry Smith    The user can open an alternative visualization contexts with
292*292f8084SBarry Smith    PetscViewerASCIIOpen() (output to a specified file).
293*292f8084SBarry Smith 
294*292f8084SBarry Smith    Level: developer
295*292f8084SBarry Smith 
296*292f8084SBarry Smith .keywords: PF, view
297*292f8084SBarry Smith 
298*292f8084SBarry Smith .seealso: PetscViewerCreate(), PetscViewerASCIIOpen()
299*292f8084SBarry Smith @*/
300*292f8084SBarry Smith PetscErrorCode PFView(PF pf,PetscViewer viewer)
301*292f8084SBarry Smith {
302*292f8084SBarry Smith   PFType            cstr;
303*292f8084SBarry Smith   PetscErrorCode ierr;
304*292f8084SBarry Smith   PetscTruth        iascii;
305*292f8084SBarry Smith   PetscViewerFormat format;
306*292f8084SBarry Smith 
307*292f8084SBarry Smith   PetscFunctionBegin;
308*292f8084SBarry Smith   PetscValidHeaderSpecific(pf,PF_COOKIE,1);
309*292f8084SBarry Smith   if (!viewer) viewer = PETSC_VIEWER_STDOUT_(pf->comm);
310*292f8084SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE,2);
311*292f8084SBarry Smith   PetscCheckSameComm(pf,1,viewer,2);
312*292f8084SBarry Smith 
313*292f8084SBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
314*292f8084SBarry Smith   if (iascii) {
315*292f8084SBarry Smith     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
316*292f8084SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"PF Object:\n");CHKERRQ(ierr);
317*292f8084SBarry Smith     ierr = PFGetType(pf,&cstr);CHKERRQ(ierr);
318*292f8084SBarry Smith     if (cstr) {
319*292f8084SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: %s\n",cstr);CHKERRQ(ierr);
320*292f8084SBarry Smith     } else {
321*292f8084SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: not yet set\n");CHKERRQ(ierr);
322*292f8084SBarry Smith     }
323*292f8084SBarry Smith     if (pf->ops->view) {
324*292f8084SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
325*292f8084SBarry Smith       ierr = (*pf->ops->view)(pf->data,viewer);CHKERRQ(ierr);
326*292f8084SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
327*292f8084SBarry Smith     }
328*292f8084SBarry Smith   } else {
329*292f8084SBarry Smith     SETERRQ1(1,"Viewer type %s not supported by PF",((PetscObject)viewer)->type_name);
330*292f8084SBarry Smith   }
331*292f8084SBarry Smith   PetscFunctionReturn(0);
332*292f8084SBarry Smith }
333*292f8084SBarry Smith 
334*292f8084SBarry Smith /*MC
335*292f8084SBarry Smith    PFRegisterDynamic - Adds a method to the mathematical function package.
336*292f8084SBarry Smith 
337*292f8084SBarry Smith    Synopsis:
338*292f8084SBarry Smith    int PFRegisterDynamic(char *name_solver,char *path,char *name_create,PetscErrorCode (*routine_create)(PF))
339*292f8084SBarry Smith 
340*292f8084SBarry Smith    Not collective
341*292f8084SBarry Smith 
342*292f8084SBarry Smith    Input Parameters:
343*292f8084SBarry Smith +  name_solver - name of a new user-defined solver
344*292f8084SBarry Smith .  path - path (either absolute or relative) the library containing this solver
345*292f8084SBarry Smith .  name_create - name of routine to create method context
346*292f8084SBarry Smith -  routine_create - routine to create method context
347*292f8084SBarry Smith 
348*292f8084SBarry Smith    Notes:
349*292f8084SBarry Smith    PFRegisterDynamic() may be called multiple times to add several user-defined functions
350*292f8084SBarry Smith 
351*292f8084SBarry Smith    If dynamic libraries are used, then the fourth input argument (routine_create)
352*292f8084SBarry Smith    is ignored.
353*292f8084SBarry Smith 
354*292f8084SBarry Smith    Sample usage:
355*292f8084SBarry Smith .vb
356*292f8084SBarry Smith    PFRegisterDynamic("my_function","/home/username/my_lib/lib/libO/solaris/mylib",
357*292f8084SBarry Smith               "MyFunctionCreate",MyFunctionSetCreate);
358*292f8084SBarry Smith .ve
359*292f8084SBarry Smith 
360*292f8084SBarry Smith    Then, your solver can be chosen with the procedural interface via
361*292f8084SBarry Smith $     PFSetType(pf,"my_function")
362*292f8084SBarry Smith    or at runtime via the option
363*292f8084SBarry Smith $     -pf_type my_function
364*292f8084SBarry Smith 
365*292f8084SBarry Smith    Level: advanced
366*292f8084SBarry Smith 
367*292f8084SBarry Smith    ${PETSC_ARCH}, ${PETSC_DIR}, ${PETSC_LIB_DIR}, ${BOPT}, or ${any environmental variable}
368*292f8084SBarry Smith  occuring in pathname will be replaced with appropriate values.
369*292f8084SBarry Smith 
370*292f8084SBarry Smith .keywords: PF, register
371*292f8084SBarry Smith 
372*292f8084SBarry Smith .seealso: PFRegisterAll(), PFRegisterDestroy(), PFRegister()
373*292f8084SBarry Smith M*/
374*292f8084SBarry Smith 
375*292f8084SBarry Smith #undef __FUNCT__
376*292f8084SBarry Smith #define __FUNCT__ "PFRegister"
377*292f8084SBarry Smith PetscErrorCode PFRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(PF,void*))
378*292f8084SBarry Smith {
379*292f8084SBarry Smith   PetscErrorCode ierr;
380*292f8084SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
381*292f8084SBarry Smith 
382*292f8084SBarry Smith   PetscFunctionBegin;
383*292f8084SBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
384*292f8084SBarry Smith   ierr = PetscFListAdd(&PPetscFList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
385*292f8084SBarry Smith   PetscFunctionReturn(0);
386*292f8084SBarry Smith }
387*292f8084SBarry Smith 
388*292f8084SBarry Smith 
389*292f8084SBarry Smith 
390*292f8084SBarry Smith #undef __FUNCT__
391*292f8084SBarry Smith #define __FUNCT__ "PFGetType"
392*292f8084SBarry Smith /*@C
393*292f8084SBarry Smith    PFGetType - Gets the PF method type and name (as a string) from the PF
394*292f8084SBarry Smith    context.
395*292f8084SBarry Smith 
396*292f8084SBarry Smith    Not Collective
397*292f8084SBarry Smith 
398*292f8084SBarry Smith    Input Parameter:
399*292f8084SBarry Smith .  pf - the function context
400*292f8084SBarry Smith 
401*292f8084SBarry Smith    Output Parameter:
402*292f8084SBarry Smith .  name - name of function
403*292f8084SBarry Smith 
404*292f8084SBarry Smith    Level: intermediate
405*292f8084SBarry Smith 
406*292f8084SBarry Smith .keywords: PF, get, method, name, type
407*292f8084SBarry Smith 
408*292f8084SBarry Smith .seealso: PFSetType()
409*292f8084SBarry Smith 
410*292f8084SBarry Smith @*/
411*292f8084SBarry Smith PetscErrorCode PFGetType(PF pf,PFType *meth)
412*292f8084SBarry Smith {
413*292f8084SBarry Smith   PetscFunctionBegin;
414*292f8084SBarry Smith   *meth = (PFType) pf->type_name;
415*292f8084SBarry Smith   PetscFunctionReturn(0);
416*292f8084SBarry Smith }
417*292f8084SBarry Smith 
418*292f8084SBarry Smith 
419*292f8084SBarry Smith #undef __FUNCT__
420*292f8084SBarry Smith #define __FUNCT__ "PFSetType"
421*292f8084SBarry Smith /*@C
422*292f8084SBarry Smith    PFSetType - Builds PF for a particular function
423*292f8084SBarry Smith 
424*292f8084SBarry Smith    Collective on PF
425*292f8084SBarry Smith 
426*292f8084SBarry Smith    Input Parameter:
427*292f8084SBarry Smith +  pf - the function context.
428*292f8084SBarry Smith .  type - a known method
429*292f8084SBarry Smith -  ctx - optional type dependent context
430*292f8084SBarry Smith 
431*292f8084SBarry Smith    Options Database Key:
432*292f8084SBarry Smith .  -pf_type <type> - Sets PF type
433*292f8084SBarry Smith 
434*292f8084SBarry Smith 
435*292f8084SBarry Smith   Notes:
436*292f8084SBarry Smith   See "petsc/include/petscpf.h" for available methods (for instance,
437*292f8084SBarry Smith   PFCONSTANT)
438*292f8084SBarry Smith 
439*292f8084SBarry Smith   Level: intermediate
440*292f8084SBarry Smith 
441*292f8084SBarry Smith .keywords: PF, set, method, type
442*292f8084SBarry Smith 
443*292f8084SBarry Smith .seealso: PFSet(), PFRegisterDynamic(), PFCreate(), DACreatePF()
444*292f8084SBarry Smith 
445*292f8084SBarry Smith @*/
446*292f8084SBarry Smith PetscErrorCode PFSetType(PF pf,const PFType type,void *ctx)
447*292f8084SBarry Smith {
448*292f8084SBarry Smith   PetscErrorCode ierr,(*r)(PF,void*);
449*292f8084SBarry Smith   PetscTruth match;
450*292f8084SBarry Smith 
451*292f8084SBarry Smith   PetscFunctionBegin;
452*292f8084SBarry Smith   PetscValidHeaderSpecific(pf,PF_COOKIE,1);
453*292f8084SBarry Smith   PetscValidCharPointer(type,2);
454*292f8084SBarry Smith 
455*292f8084SBarry Smith   ierr = PetscTypeCompare((PetscObject)pf,type,&match);CHKERRQ(ierr);
456*292f8084SBarry Smith   if (match) PetscFunctionReturn(0);
457*292f8084SBarry Smith 
458*292f8084SBarry Smith   if (pf->ops->destroy) {ierr =  (*pf->ops->destroy)(pf);CHKERRQ(ierr);}
459*292f8084SBarry Smith   pf->data        = 0;
460*292f8084SBarry Smith 
461*292f8084SBarry Smith   /* Get the function pointers for the method requested */
462*292f8084SBarry Smith   if (!PFRegisterAllCalled) {ierr = PFRegisterAll(0);CHKERRQ(ierr);}
463*292f8084SBarry Smith   /* Determine the PFCreateXXX routine for a particular function */
464*292f8084SBarry Smith   ierr =  PetscFListFind(pf->comm,PPetscFList,type,(void (**)(void)) &r);CHKERRQ(ierr);
465*292f8084SBarry Smith   if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested PF type %s",type);
466*292f8084SBarry Smith   pf->ops->destroy             = 0;
467*292f8084SBarry Smith   pf->ops->view                = 0;
468*292f8084SBarry Smith   pf->ops->apply               = 0;
469*292f8084SBarry Smith   pf->ops->applyvec            = 0;
470*292f8084SBarry Smith 
471*292f8084SBarry Smith   /* Call the PFCreateXXX routine for this particular function */
472*292f8084SBarry Smith   ierr = (*r)(pf,ctx);CHKERRQ(ierr);
473*292f8084SBarry Smith 
474*292f8084SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)pf,type);CHKERRQ(ierr);
475*292f8084SBarry Smith   PetscFunctionReturn(0);
476*292f8084SBarry Smith }
477*292f8084SBarry Smith 
478*292f8084SBarry Smith #undef __FUNCT__
479*292f8084SBarry Smith #define __FUNCT__ "PFSetFromOptions"
480*292f8084SBarry Smith /*@
481*292f8084SBarry Smith    PFSetFromOptions - Sets PF options from the options database.
482*292f8084SBarry Smith 
483*292f8084SBarry Smith    Collective on PF
484*292f8084SBarry Smith 
485*292f8084SBarry Smith    Input Parameters:
486*292f8084SBarry Smith .  pf - the mathematical function context
487*292f8084SBarry Smith 
488*292f8084SBarry Smith    Options Database Keys:
489*292f8084SBarry Smith 
490*292f8084SBarry Smith    Notes:
491*292f8084SBarry Smith    To see all options, run your program with the -help option
492*292f8084SBarry Smith    or consult the users manual.
493*292f8084SBarry Smith 
494*292f8084SBarry Smith    Level: intermediate
495*292f8084SBarry Smith 
496*292f8084SBarry Smith .keywords: PF, set, from, options, database
497*292f8084SBarry Smith 
498*292f8084SBarry Smith .seealso:
499*292f8084SBarry Smith @*/
500*292f8084SBarry Smith PetscErrorCode PFSetFromOptions(PF pf)
501*292f8084SBarry Smith {
502*292f8084SBarry Smith   PetscErrorCode ierr;
503*292f8084SBarry Smith   char       type[256];
504*292f8084SBarry Smith   PetscTruth flg;
505*292f8084SBarry Smith 
506*292f8084SBarry Smith   PetscFunctionBegin;
507*292f8084SBarry Smith   PetscValidHeaderSpecific(pf,PF_COOKIE,1);
508*292f8084SBarry Smith 
509*292f8084SBarry Smith   if (!PFRegisterAllCalled) {ierr = PFRegisterAll(0);CHKERRQ(ierr);}
510*292f8084SBarry Smith   ierr = PetscOptionsBegin(pf->comm,pf->prefix,"Mathematical functions options","Vec");CHKERRQ(ierr);
511*292f8084SBarry Smith     ierr = PetscOptionsList("-pf_type","Type of function","PFSetType",PPetscFList,0,type,256,&flg);CHKERRQ(ierr);
512*292f8084SBarry Smith     if (flg) {
513*292f8084SBarry Smith       ierr = PFSetType(pf,type,PETSC_NULL);CHKERRQ(ierr);
514*292f8084SBarry Smith     }
515*292f8084SBarry Smith     if (pf->ops->setfromoptions) {
516*292f8084SBarry Smith       ierr = (*pf->ops->setfromoptions)(pf);CHKERRQ(ierr);
517*292f8084SBarry Smith     }
518*292f8084SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
519*292f8084SBarry Smith 
520*292f8084SBarry Smith   PetscFunctionReturn(0);
521*292f8084SBarry Smith }
522*292f8084SBarry Smith 
523*292f8084SBarry Smith 
524*292f8084SBarry Smith 
525*292f8084SBarry Smith 
526*292f8084SBarry Smith 
527*292f8084SBarry Smith 
528*292f8084SBarry Smith 
529*292f8084SBarry Smith 
530*292f8084SBarry Smith 
531*292f8084SBarry Smith 
532