xref: /petsc/src/vec/pf/interface/pf.c (revision 96fc60bc8467e312da823a9b847d0ecaf36ab221)
10c735eedSKris Buschelman #define PETSCVEC_DLL
2292f8084SBarry Smith /*
3292f8084SBarry Smith     The PF mathematical functions interface routines, callable by users.
4292f8084SBarry Smith */
558d819adSvictorle #include "src/vec/pf/pfimpl.h"            /*I "petscpf.h" I*/
6292f8084SBarry Smith 
7292f8084SBarry Smith /* Logging support */
8292f8084SBarry Smith PetscCookie PF_COOKIE = 0;
9292f8084SBarry Smith 
10292f8084SBarry Smith PetscFList PPetscFList         = PETSC_NULL; /* list of all registered PD functions */
11292f8084SBarry Smith PetscTruth PFRegisterAllCalled = PETSC_FALSE;
12292f8084SBarry Smith 
13292f8084SBarry Smith #undef __FUNCT__
14292f8084SBarry Smith #define __FUNCT__ "PFSet"
15292f8084SBarry Smith /*@C
16292f8084SBarry Smith    PFSet - Sets the C/C++/Fortran functions to be used by the PF function
17292f8084SBarry Smith 
18292f8084SBarry Smith    Collective on PF
19292f8084SBarry Smith 
20292f8084SBarry Smith    Input Parameter:
21292f8084SBarry Smith +  pf - the function context
22292f8084SBarry Smith .  apply - function to apply to an array
23292f8084SBarry Smith .  applyvec - function to apply to a Vec
24292f8084SBarry Smith .  view - function that prints information about the PF
25292f8084SBarry Smith .  destroy - function to free the private function context
26292f8084SBarry Smith -  ctx - private function context
27292f8084SBarry Smith 
28292f8084SBarry Smith    Level: beginner
29292f8084SBarry Smith 
30292f8084SBarry Smith .keywords: PF, setting
31292f8084SBarry Smith 
32292f8084SBarry Smith .seealso: PFCreate(), PFDestroy(), PFSetType(), PFApply(), PFApplyVec()
33292f8084SBarry Smith @*/
340c735eedSKris Buschelman PetscErrorCode PETSCVEC_DLLEXPORT PFSet(PF pf,PetscErrorCode (*apply)(void*,PetscInt,PetscScalar*,PetscScalar*),PetscErrorCode (*applyvec)(void*,Vec,Vec),PetscErrorCode (*view)(void*,PetscViewer),PetscErrorCode (*destroy)(void*),void*ctx)
35292f8084SBarry Smith {
36292f8084SBarry Smith   PetscFunctionBegin;
37292f8084SBarry Smith   PetscValidHeaderSpecific(pf,PF_COOKIE,1);
38292f8084SBarry Smith   pf->data             = ctx;
39292f8084SBarry Smith 
40292f8084SBarry Smith   pf->ops->destroy     = destroy;
41292f8084SBarry Smith   pf->ops->apply       = apply;
42292f8084SBarry Smith   pf->ops->applyvec    = applyvec;
43292f8084SBarry Smith   pf->ops->view        = view;
44292f8084SBarry Smith 
45292f8084SBarry Smith   PetscFunctionReturn(0);
46292f8084SBarry Smith }
47292f8084SBarry Smith 
48292f8084SBarry Smith #undef __FUNCT__
49292f8084SBarry Smith #define __FUNCT__ "PFDestroy"
50292f8084SBarry Smith /*@C
51292f8084SBarry Smith    PFDestroy - Destroys PF context that was created with PFCreate().
52292f8084SBarry Smith 
53292f8084SBarry Smith    Collective on PF
54292f8084SBarry Smith 
55292f8084SBarry Smith    Input Parameter:
56292f8084SBarry Smith .  pf - the function context
57292f8084SBarry Smith 
58292f8084SBarry Smith    Level: beginner
59292f8084SBarry Smith 
60292f8084SBarry Smith .keywords: PF, destroy
61292f8084SBarry Smith 
62292f8084SBarry Smith .seealso: PFCreate(), PFSet(), PFSetType()
63292f8084SBarry Smith @*/
640c735eedSKris Buschelman PetscErrorCode PETSCVEC_DLLEXPORT PFDestroy(PF pf)
65292f8084SBarry Smith {
66292f8084SBarry Smith   PetscErrorCode ierr;
67292f8084SBarry Smith   PetscTruth flg;
68292f8084SBarry Smith 
69292f8084SBarry Smith   PetscFunctionBegin;
70292f8084SBarry Smith   PetscValidHeaderSpecific(pf,PF_COOKIE,1);
71292f8084SBarry Smith   if (--pf->refct > 0) PetscFunctionReturn(0);
72292f8084SBarry Smith 
73292f8084SBarry Smith   ierr = PetscOptionsHasName(pf->prefix,"-pf_view",&flg);CHKERRQ(ierr);
74292f8084SBarry Smith   if (flg) {
75292f8084SBarry Smith     ierr = PFView(pf,PETSC_VIEWER_STDOUT_(pf->comm));CHKERRQ(ierr);
76292f8084SBarry Smith   }
77292f8084SBarry Smith 
78292f8084SBarry Smith   /* if memory was published with AMS then destroy it */
79292f8084SBarry Smith   ierr = PetscObjectDepublish(pf);CHKERRQ(ierr);
80292f8084SBarry Smith 
81292f8084SBarry Smith   if (pf->ops->destroy) {ierr =  (*pf->ops->destroy)(pf->data);CHKERRQ(ierr);}
82d38fa0fbSBarry Smith   ierr = PetscHeaderDestroy(pf);CHKERRQ(ierr);
83292f8084SBarry Smith   PetscFunctionReturn(0);
84292f8084SBarry Smith }
85292f8084SBarry Smith 
86292f8084SBarry Smith #undef __FUNCT__
87292f8084SBarry Smith #define __FUNCT__ "PFPublish_Petsc"
88292f8084SBarry Smith static PetscErrorCode PFPublish_Petsc(PetscObject obj)
89292f8084SBarry Smith {
90292f8084SBarry Smith #if defined(PETSC_HAVE_AMS)
91292f8084SBarry Smith   PF          v = (PF) obj;
92292f8084SBarry Smith   PetscErrorCode ierr;
93292f8084SBarry Smith #endif
94292f8084SBarry Smith 
95292f8084SBarry Smith   PetscFunctionBegin;
96292f8084SBarry Smith 
97292f8084SBarry Smith #if defined(PETSC_HAVE_AMS)
98292f8084SBarry Smith   /* if it is already published then return */
99292f8084SBarry Smith   if (v->amem >=0) PetscFunctionReturn(0);
100292f8084SBarry Smith 
101292f8084SBarry Smith   ierr = PetscObjectPublishBaseBegin(obj);CHKERRQ(ierr);
102292f8084SBarry Smith   ierr = PetscObjectPublishBaseEnd(obj);CHKERRQ(ierr);
103292f8084SBarry Smith #endif
104292f8084SBarry Smith 
105292f8084SBarry Smith   PetscFunctionReturn(0);
106292f8084SBarry Smith }
107292f8084SBarry Smith 
108292f8084SBarry Smith #undef __FUNCT__
109292f8084SBarry Smith #define __FUNCT__ "PFCreate"
110292f8084SBarry Smith /*@C
111292f8084SBarry Smith    PFCreate - Creates a mathematical function context.
112292f8084SBarry Smith 
113292f8084SBarry Smith    Collective on MPI_Comm
114292f8084SBarry Smith 
115292f8084SBarry Smith    Input Parameter:
116292f8084SBarry Smith +  comm - MPI communicator
117292f8084SBarry Smith .  dimin - dimension of the space you are mapping from
118292f8084SBarry Smith -  dimout - dimension of the space you are mapping to
119292f8084SBarry Smith 
120292f8084SBarry Smith    Output Parameter:
121292f8084SBarry Smith .  pf - the function context
122292f8084SBarry Smith 
123292f8084SBarry Smith    Level: developer
124292f8084SBarry Smith 
125292f8084SBarry Smith .keywords: PF, create, context
126292f8084SBarry Smith 
127292f8084SBarry Smith .seealso: PFSetUp(), PFApply(), PFDestroy(), PFApplyVec()
128292f8084SBarry Smith @*/
1290c735eedSKris Buschelman PetscErrorCode PETSCVEC_DLLEXPORT PFCreate(MPI_Comm comm,PetscInt dimin,PetscInt dimout,PF *pf)
130292f8084SBarry Smith {
131292f8084SBarry Smith   PF  newpf;
132292f8084SBarry Smith   PetscErrorCode ierr;
133292f8084SBarry Smith 
134292f8084SBarry Smith   PetscFunctionBegin;
135292f8084SBarry Smith   PetscValidPointer(pf,1);
136292f8084SBarry Smith   *pf = PETSC_NULL;
137292f8084SBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
138292f8084SBarry Smith   ierr = VecInitializePackage(PETSC_NULL);CHKERRQ(ierr);
139292f8084SBarry Smith #endif
140292f8084SBarry Smith 
14152e6d16bSBarry Smith   ierr = PetscHeaderCreate(newpf,_p_PF,struct _PFOps,PF_COOKIE,-1,"PF",comm,PFDestroy,PFView);CHKERRQ(ierr);
142292f8084SBarry Smith   newpf->bops->publish    = PFPublish_Petsc;
143292f8084SBarry Smith   newpf->data             = 0;
144292f8084SBarry Smith 
145292f8084SBarry Smith   newpf->ops->destroy     = 0;
146292f8084SBarry Smith   newpf->ops->apply       = 0;
147292f8084SBarry Smith   newpf->ops->applyvec    = 0;
148292f8084SBarry Smith   newpf->ops->view        = 0;
149292f8084SBarry Smith   newpf->dimin            = dimin;
150292f8084SBarry Smith   newpf->dimout           = dimout;
151292f8084SBarry Smith 
152292f8084SBarry Smith   *pf                     = newpf;
153292f8084SBarry Smith   ierr = PetscPublishAll(pf);CHKERRQ(ierr);
154292f8084SBarry Smith   PetscFunctionReturn(0);
155292f8084SBarry Smith 
156292f8084SBarry Smith }
157292f8084SBarry Smith 
158292f8084SBarry Smith /* -------------------------------------------------------------------------------*/
159292f8084SBarry Smith 
160292f8084SBarry Smith #undef __FUNCT__
161292f8084SBarry Smith #define __FUNCT__ "PFApplyVec"
162292f8084SBarry Smith /*@
163292f8084SBarry Smith    PFApplyVec - Applies the mathematical function to a vector
164292f8084SBarry Smith 
165292f8084SBarry Smith    Collective on PF
166292f8084SBarry Smith 
167292f8084SBarry Smith    Input Parameters:
168292f8084SBarry Smith +  pf - the function context
169292f8084SBarry Smith -  x - input vector (or PETSC_NULL for the vector (0,1, .... N-1)
170292f8084SBarry Smith 
171292f8084SBarry Smith    Output Parameter:
172292f8084SBarry Smith .  y - output vector
173292f8084SBarry Smith 
174292f8084SBarry Smith    Level: beginner
175292f8084SBarry Smith 
176292f8084SBarry Smith .keywords: PF, apply
177292f8084SBarry Smith 
178292f8084SBarry Smith .seealso: PFApply(), PFCreate(), PFDestroy(), PFSetType(), PFSet()
179292f8084SBarry Smith @*/
1800c735eedSKris Buschelman PetscErrorCode PETSCVEC_DLLEXPORT PFApplyVec(PF pf,Vec x,Vec y)
181292f8084SBarry Smith {
182292f8084SBarry Smith   PetscErrorCode ierr;
183292f8084SBarry Smith   PetscInt       i,rstart,rend,n,p;
184292f8084SBarry Smith   PetscTruth     nox = PETSC_FALSE;
185292f8084SBarry Smith 
186292f8084SBarry Smith   PetscFunctionBegin;
187292f8084SBarry Smith   PetscValidHeaderSpecific(pf,PF_COOKIE,1);
188292f8084SBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE,3);
189292f8084SBarry Smith   if (x) {
190292f8084SBarry Smith     PetscValidHeaderSpecific(x,VEC_COOKIE,2);
191292f8084SBarry Smith     if (x == y) SETERRQ(PETSC_ERR_ARG_IDN,"x and y must be different vectors");
192292f8084SBarry Smith   } else {
193292f8084SBarry Smith     PetscScalar *xx;
194292f8084SBarry Smith 
195292f8084SBarry Smith     ierr = VecDuplicate(y,&x);CHKERRQ(ierr);
196292f8084SBarry Smith     nox  = PETSC_TRUE;
197292f8084SBarry Smith     ierr = VecGetOwnershipRange(x,&rstart,&rend);CHKERRQ(ierr);
198292f8084SBarry Smith     ierr = VecGetArray(x,&xx);CHKERRQ(ierr);
199292f8084SBarry Smith     for (i=rstart; i<rend; i++) {
200292f8084SBarry Smith       xx[i-rstart] = (PetscScalar)i;
201292f8084SBarry Smith     }
202292f8084SBarry Smith     ierr = VecRestoreArray(x,&xx);CHKERRQ(ierr);
203292f8084SBarry Smith   }
204292f8084SBarry Smith 
205292f8084SBarry Smith   ierr = VecGetLocalSize(x,&n);CHKERRQ(ierr);
206292f8084SBarry Smith   ierr = VecGetLocalSize(y,&p);CHKERRQ(ierr);
207*96fc60bcSBarry Smith   if ((pf->dimin*(n/pf->dimin)) != n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Local input vector length %D not divisible by dimin %D of function",n,pf->dimin);
208*96fc60bcSBarry Smith   if ((pf->dimout*(p/pf->dimout)) != p) SETERRQ2(PETSC_ERR_ARG_SIZ,"Local output vector length %D not divisible by dimout %D of function",p,pf->dimout);
209*96fc60bcSBarry Smith   if ((n/pf->dimin) != (p/pf->dimout)) SETERRQ4(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);
210292f8084SBarry Smith 
211292f8084SBarry Smith   if (pf->ops->applyvec) {
212292f8084SBarry Smith     ierr = (*pf->ops->applyvec)(pf->data,x,y);CHKERRQ(ierr);
213292f8084SBarry Smith   } else {
214292f8084SBarry Smith     PetscScalar *xx,*yy;
215292f8084SBarry Smith 
216292f8084SBarry Smith     ierr = VecGetLocalSize(x,&n);CHKERRQ(ierr);
217292f8084SBarry Smith     n    = n/pf->dimin;
218292f8084SBarry Smith     ierr = VecGetArray(x,&xx);CHKERRQ(ierr);
219292f8084SBarry Smith     ierr = VecGetArray(y,&yy);CHKERRQ(ierr);
220e005ede5SBarry Smith     if (!pf->ops->apply) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No function has been provided for this PF");
221292f8084SBarry Smith     ierr = (*pf->ops->apply)(pf->data,n,xx,yy);CHKERRQ(ierr);
222292f8084SBarry Smith     ierr = VecRestoreArray(x,&xx);CHKERRQ(ierr);
223292f8084SBarry Smith     ierr = VecRestoreArray(y,&yy);CHKERRQ(ierr);
224292f8084SBarry Smith   }
225292f8084SBarry Smith   if (nox) {
226292f8084SBarry Smith     ierr = VecDestroy(x);CHKERRQ(ierr);
227292f8084SBarry Smith   }
228292f8084SBarry Smith   PetscFunctionReturn(0);
229292f8084SBarry Smith }
230292f8084SBarry Smith 
231292f8084SBarry Smith #undef __FUNCT__
232292f8084SBarry Smith #define __FUNCT__ "PFApply"
233292f8084SBarry Smith /*@
234292f8084SBarry Smith    PFApply - Applies the mathematical function to an array of values.
235292f8084SBarry Smith 
236292f8084SBarry Smith    Collective on PF
237292f8084SBarry Smith 
238292f8084SBarry Smith    Input Parameters:
239292f8084SBarry Smith +  pf - the function context
240292f8084SBarry Smith .  n - number of pointwise function evaluations to perform, each pointwise function evaluation
241292f8084SBarry Smith        is a function of dimin variables and computes dimout variables where dimin and dimout are defined
242292f8084SBarry Smith        in the call to PFCreate()
243292f8084SBarry Smith -  x - input array
244292f8084SBarry Smith 
245292f8084SBarry Smith    Output Parameter:
246292f8084SBarry Smith .  y - output array
247292f8084SBarry Smith 
248292f8084SBarry Smith    Level: beginner
249292f8084SBarry Smith 
250292f8084SBarry Smith    Notes:
251292f8084SBarry Smith 
252292f8084SBarry Smith .keywords: PF, apply
253292f8084SBarry Smith 
254292f8084SBarry Smith .seealso: PFApplyVec(), PFCreate(), PFDestroy(), PFSetType(), PFSet()
255292f8084SBarry Smith @*/
2560c735eedSKris Buschelman PetscErrorCode PETSCVEC_DLLEXPORT PFApply(PF pf,PetscInt n,PetscScalar* x,PetscScalar* y)
257292f8084SBarry Smith {
258292f8084SBarry Smith   PetscErrorCode ierr;
259292f8084SBarry Smith 
260292f8084SBarry Smith   PetscFunctionBegin;
261292f8084SBarry Smith   PetscValidHeaderSpecific(pf,PF_COOKIE,1);
262292f8084SBarry Smith   PetscValidScalarPointer(x,2);
263292f8084SBarry Smith   PetscValidScalarPointer(y,3);
264292f8084SBarry Smith   if (x == y) SETERRQ(PETSC_ERR_ARG_IDN,"x and y must be different arrays");
265e005ede5SBarry Smith   if (!pf->ops->apply) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No function has been provided for this PF");
266292f8084SBarry Smith 
267292f8084SBarry Smith   ierr = (*pf->ops->apply)(pf->data,n,x,y);CHKERRQ(ierr);
268292f8084SBarry Smith   PetscFunctionReturn(0);
269292f8084SBarry Smith }
270292f8084SBarry Smith 
271292f8084SBarry Smith #undef __FUNCT__
272292f8084SBarry Smith #define __FUNCT__ "PFView"
273292f8084SBarry Smith /*@
274292f8084SBarry Smith    PFView - Prints information about a mathematical function
275292f8084SBarry Smith 
276292f8084SBarry Smith    Collective on PF unless PetscViewer is PETSC_VIEWER_STDOUT_SELF
277292f8084SBarry Smith 
278292f8084SBarry Smith    Input Parameters:
279292f8084SBarry Smith +  PF - the PF context
280292f8084SBarry Smith -  viewer - optional visualization context
281292f8084SBarry Smith 
282292f8084SBarry Smith    Note:
283292f8084SBarry Smith    The available visualization contexts include
284292f8084SBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
285292f8084SBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
286292f8084SBarry Smith          output where only the first processor opens
287292f8084SBarry Smith          the file.  All other processors send their
288292f8084SBarry Smith          data to the first processor to print.
289292f8084SBarry Smith 
290292f8084SBarry Smith    The user can open an alternative visualization contexts with
291292f8084SBarry Smith    PetscViewerASCIIOpen() (output to a specified file).
292292f8084SBarry Smith 
293292f8084SBarry Smith    Level: developer
294292f8084SBarry Smith 
295292f8084SBarry Smith .keywords: PF, view
296292f8084SBarry Smith 
297292f8084SBarry Smith .seealso: PetscViewerCreate(), PetscViewerASCIIOpen()
298292f8084SBarry Smith @*/
2990c735eedSKris Buschelman PetscErrorCode PETSCVEC_DLLEXPORT PFView(PF pf,PetscViewer viewer)
300292f8084SBarry Smith {
301292f8084SBarry Smith   PFType            cstr;
302292f8084SBarry Smith   PetscErrorCode ierr;
303292f8084SBarry Smith   PetscTruth        iascii;
304292f8084SBarry Smith   PetscViewerFormat format;
305292f8084SBarry Smith 
306292f8084SBarry Smith   PetscFunctionBegin;
307292f8084SBarry Smith   PetscValidHeaderSpecific(pf,PF_COOKIE,1);
308292f8084SBarry Smith   if (!viewer) viewer = PETSC_VIEWER_STDOUT_(pf->comm);
309292f8084SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE,2);
310292f8084SBarry Smith   PetscCheckSameComm(pf,1,viewer,2);
311292f8084SBarry Smith 
312292f8084SBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
313292f8084SBarry Smith   if (iascii) {
314292f8084SBarry Smith     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
315292f8084SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"PF Object:\n");CHKERRQ(ierr);
316292f8084SBarry Smith     ierr = PFGetType(pf,&cstr);CHKERRQ(ierr);
317292f8084SBarry Smith     if (cstr) {
318292f8084SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: %s\n",cstr);CHKERRQ(ierr);
319292f8084SBarry Smith     } else {
320292f8084SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: not yet set\n");CHKERRQ(ierr);
321292f8084SBarry Smith     }
322292f8084SBarry Smith     if (pf->ops->view) {
323292f8084SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
324292f8084SBarry Smith       ierr = (*pf->ops->view)(pf->data,viewer);CHKERRQ(ierr);
325292f8084SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
326292f8084SBarry Smith     }
327292f8084SBarry Smith   } else {
32879a5c55eSBarry Smith     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported by PF",((PetscObject)viewer)->type_name);
329292f8084SBarry Smith   }
330292f8084SBarry Smith   PetscFunctionReturn(0);
331292f8084SBarry Smith }
332292f8084SBarry Smith 
333292f8084SBarry Smith /*MC
334292f8084SBarry Smith    PFRegisterDynamic - Adds a method to the mathematical function package.
335292f8084SBarry Smith 
336292f8084SBarry Smith    Synopsis:
337d360dc6fSBarry Smith    PetscErrorCode PFRegisterDynamic(char *name_solver,char *path,char *name_create,PetscErrorCode (*routine_create)(PF))
338292f8084SBarry Smith 
339292f8084SBarry Smith    Not collective
340292f8084SBarry Smith 
341292f8084SBarry Smith    Input Parameters:
342292f8084SBarry Smith +  name_solver - name of a new user-defined solver
343292f8084SBarry Smith .  path - path (either absolute or relative) the library containing this solver
344292f8084SBarry Smith .  name_create - name of routine to create method context
345292f8084SBarry Smith -  routine_create - routine to create method context
346292f8084SBarry Smith 
347292f8084SBarry Smith    Notes:
348292f8084SBarry Smith    PFRegisterDynamic() may be called multiple times to add several user-defined functions
349292f8084SBarry Smith 
350292f8084SBarry Smith    If dynamic libraries are used, then the fourth input argument (routine_create)
351292f8084SBarry Smith    is ignored.
352292f8084SBarry Smith 
353292f8084SBarry Smith    Sample usage:
354292f8084SBarry Smith .vb
355292f8084SBarry Smith    PFRegisterDynamic("my_function","/home/username/my_lib/lib/libO/solaris/mylib",
356292f8084SBarry Smith               "MyFunctionCreate",MyFunctionSetCreate);
357292f8084SBarry Smith .ve
358292f8084SBarry Smith 
359292f8084SBarry Smith    Then, your solver can be chosen with the procedural interface via
360292f8084SBarry Smith $     PFSetType(pf,"my_function")
361292f8084SBarry Smith    or at runtime via the option
362292f8084SBarry Smith $     -pf_type my_function
363292f8084SBarry Smith 
364292f8084SBarry Smith    Level: advanced
365292f8084SBarry Smith 
366ab901514SBarry Smith    ${PETSC_ARCH}, ${PETSC_DIR}, ${PETSC_LIB_DIR}, or ${any environmental variable}
367292f8084SBarry Smith  occuring in pathname will be replaced with appropriate values.
368292f8084SBarry Smith 
369292f8084SBarry Smith .keywords: PF, register
370292f8084SBarry Smith 
371292f8084SBarry Smith .seealso: PFRegisterAll(), PFRegisterDestroy(), PFRegister()
372292f8084SBarry Smith M*/
373292f8084SBarry Smith 
374292f8084SBarry Smith #undef __FUNCT__
375292f8084SBarry Smith #define __FUNCT__ "PFRegister"
3760c735eedSKris Buschelman PetscErrorCode PETSCVEC_DLLEXPORT PFRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(PF,void*))
377292f8084SBarry Smith {
378292f8084SBarry Smith   PetscErrorCode ierr;
379292f8084SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
380292f8084SBarry Smith 
381292f8084SBarry Smith   PetscFunctionBegin;
382292f8084SBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
383292f8084SBarry Smith   ierr = PetscFListAdd(&PPetscFList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
384292f8084SBarry Smith   PetscFunctionReturn(0);
385292f8084SBarry Smith }
386292f8084SBarry Smith 
387292f8084SBarry Smith 
388292f8084SBarry Smith 
389292f8084SBarry Smith #undef __FUNCT__
390292f8084SBarry Smith #define __FUNCT__ "PFGetType"
391292f8084SBarry Smith /*@C
392292f8084SBarry Smith    PFGetType - Gets the PF method type and name (as a string) from the PF
393292f8084SBarry Smith    context.
394292f8084SBarry Smith 
395292f8084SBarry Smith    Not Collective
396292f8084SBarry Smith 
397292f8084SBarry Smith    Input Parameter:
398292f8084SBarry Smith .  pf - the function context
399292f8084SBarry Smith 
400292f8084SBarry Smith    Output Parameter:
401292f8084SBarry Smith .  name - name of function
402292f8084SBarry Smith 
403292f8084SBarry Smith    Level: intermediate
404292f8084SBarry Smith 
405292f8084SBarry Smith .keywords: PF, get, method, name, type
406292f8084SBarry Smith 
407292f8084SBarry Smith .seealso: PFSetType()
408292f8084SBarry Smith 
409292f8084SBarry Smith @*/
4100c735eedSKris Buschelman PetscErrorCode PETSCVEC_DLLEXPORT PFGetType(PF pf,PFType *meth)
411292f8084SBarry Smith {
412292f8084SBarry Smith   PetscFunctionBegin;
413292f8084SBarry Smith   *meth = (PFType) pf->type_name;
414292f8084SBarry Smith   PetscFunctionReturn(0);
415292f8084SBarry Smith }
416292f8084SBarry Smith 
417292f8084SBarry Smith 
418292f8084SBarry Smith #undef __FUNCT__
419292f8084SBarry Smith #define __FUNCT__ "PFSetType"
420292f8084SBarry Smith /*@C
421292f8084SBarry Smith    PFSetType - Builds PF for a particular function
422292f8084SBarry Smith 
423292f8084SBarry Smith    Collective on PF
424292f8084SBarry Smith 
425292f8084SBarry Smith    Input Parameter:
426292f8084SBarry Smith +  pf - the function context.
427292f8084SBarry Smith .  type - a known method
428292f8084SBarry Smith -  ctx - optional type dependent context
429292f8084SBarry Smith 
430292f8084SBarry Smith    Options Database Key:
431292f8084SBarry Smith .  -pf_type <type> - Sets PF type
432292f8084SBarry Smith 
433292f8084SBarry Smith 
434292f8084SBarry Smith   Notes:
435292f8084SBarry Smith   See "petsc/include/petscpf.h" for available methods (for instance,
436292f8084SBarry Smith   PFCONSTANT)
437292f8084SBarry Smith 
438292f8084SBarry Smith   Level: intermediate
439292f8084SBarry Smith 
440292f8084SBarry Smith .keywords: PF, set, method, type
441292f8084SBarry Smith 
442292f8084SBarry Smith .seealso: PFSet(), PFRegisterDynamic(), PFCreate(), DACreatePF()
443292f8084SBarry Smith 
444292f8084SBarry Smith @*/
4450c735eedSKris Buschelman PetscErrorCode PETSCVEC_DLLEXPORT PFSetType(PF pf,const PFType type,void *ctx)
446292f8084SBarry Smith {
447292f8084SBarry Smith   PetscErrorCode ierr,(*r)(PF,void*);
448292f8084SBarry Smith   PetscTruth match;
449292f8084SBarry Smith 
450292f8084SBarry Smith   PetscFunctionBegin;
451292f8084SBarry Smith   PetscValidHeaderSpecific(pf,PF_COOKIE,1);
452292f8084SBarry Smith   PetscValidCharPointer(type,2);
453292f8084SBarry Smith 
454292f8084SBarry Smith   ierr = PetscTypeCompare((PetscObject)pf,type,&match);CHKERRQ(ierr);
455292f8084SBarry Smith   if (match) PetscFunctionReturn(0);
456292f8084SBarry Smith 
457292f8084SBarry Smith   if (pf->ops->destroy) {ierr =  (*pf->ops->destroy)(pf);CHKERRQ(ierr);}
458292f8084SBarry Smith   pf->data        = 0;
459292f8084SBarry Smith 
460292f8084SBarry Smith   /* Get the function pointers for the method requested */
461292f8084SBarry Smith   if (!PFRegisterAllCalled) {ierr = PFRegisterAll(0);CHKERRQ(ierr);}
462292f8084SBarry Smith   /* Determine the PFCreateXXX routine for a particular function */
463292f8084SBarry Smith   ierr =  PetscFListFind(pf->comm,PPetscFList,type,(void (**)(void)) &r);CHKERRQ(ierr);
464292f8084SBarry Smith   if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested PF type %s",type);
465292f8084SBarry Smith   pf->ops->destroy             = 0;
466292f8084SBarry Smith   pf->ops->view                = 0;
467292f8084SBarry Smith   pf->ops->apply               = 0;
468292f8084SBarry Smith   pf->ops->applyvec            = 0;
469292f8084SBarry Smith 
470292f8084SBarry Smith   /* Call the PFCreateXXX routine for this particular function */
471292f8084SBarry Smith   ierr = (*r)(pf,ctx);CHKERRQ(ierr);
472292f8084SBarry Smith 
473292f8084SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)pf,type);CHKERRQ(ierr);
474292f8084SBarry Smith   PetscFunctionReturn(0);
475292f8084SBarry Smith }
476292f8084SBarry Smith 
477292f8084SBarry Smith #undef __FUNCT__
478292f8084SBarry Smith #define __FUNCT__ "PFSetFromOptions"
479292f8084SBarry Smith /*@
480292f8084SBarry Smith    PFSetFromOptions - Sets PF options from the options database.
481292f8084SBarry Smith 
482292f8084SBarry Smith    Collective on PF
483292f8084SBarry Smith 
484292f8084SBarry Smith    Input Parameters:
485292f8084SBarry Smith .  pf - the mathematical function context
486292f8084SBarry Smith 
487292f8084SBarry Smith    Options Database Keys:
488292f8084SBarry Smith 
489292f8084SBarry Smith    Notes:
490292f8084SBarry Smith    To see all options, run your program with the -help option
491292f8084SBarry Smith    or consult the users manual.
492292f8084SBarry Smith 
493292f8084SBarry Smith    Level: intermediate
494292f8084SBarry Smith 
495292f8084SBarry Smith .keywords: PF, set, from, options, database
496292f8084SBarry Smith 
497292f8084SBarry Smith .seealso:
498292f8084SBarry Smith @*/
4990c735eedSKris Buschelman PetscErrorCode PETSCVEC_DLLEXPORT PFSetFromOptions(PF pf)
500292f8084SBarry Smith {
501292f8084SBarry Smith   PetscErrorCode ierr;
502292f8084SBarry Smith   char       type[256];
503292f8084SBarry Smith   PetscTruth flg;
504292f8084SBarry Smith 
505292f8084SBarry Smith   PetscFunctionBegin;
506292f8084SBarry Smith   PetscValidHeaderSpecific(pf,PF_COOKIE,1);
507292f8084SBarry Smith 
508292f8084SBarry Smith   if (!PFRegisterAllCalled) {ierr = PFRegisterAll(0);CHKERRQ(ierr);}
509292f8084SBarry Smith   ierr = PetscOptionsBegin(pf->comm,pf->prefix,"Mathematical functions options","Vec");CHKERRQ(ierr);
510292f8084SBarry Smith     ierr = PetscOptionsList("-pf_type","Type of function","PFSetType",PPetscFList,0,type,256,&flg);CHKERRQ(ierr);
511292f8084SBarry Smith     if (flg) {
512292f8084SBarry Smith       ierr = PFSetType(pf,type,PETSC_NULL);CHKERRQ(ierr);
513292f8084SBarry Smith     }
514292f8084SBarry Smith     if (pf->ops->setfromoptions) {
515292f8084SBarry Smith       ierr = (*pf->ops->setfromoptions)(pf);CHKERRQ(ierr);
516292f8084SBarry Smith     }
517292f8084SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
518292f8084SBarry Smith 
519292f8084SBarry Smith   PetscFunctionReturn(0);
520292f8084SBarry Smith }
521292f8084SBarry Smith 
522292f8084SBarry Smith 
523292f8084SBarry Smith 
524292f8084SBarry Smith 
525292f8084SBarry Smith 
526292f8084SBarry Smith 
527292f8084SBarry Smith 
528292f8084SBarry Smith 
529292f8084SBarry Smith 
530292f8084SBarry Smith 
531