xref: /petsc/src/vec/pf/interface/pf.c (revision 08401ef684002a709c6d3db98a0c9f54a8bcf1ec)
1292f8084SBarry Smith /*
2292f8084SBarry Smith     The PF mathematical functions interface routines, callable by users.
3292f8084SBarry Smith */
4c6db04a5SJed Brown #include <../src/vec/pf/pfimpl.h>            /*I "petscpf.h" I*/
5292f8084SBarry Smith 
60700a824SBarry Smith PetscClassId      PF_CLASSID          = 0;
737e93019SBarry Smith PetscFunctionList PFList              = NULL;   /* list of all registered PD functions */
8ace3abfcSBarry Smith PetscBool         PFRegisterAllCalled = PETSC_FALSE;
9292f8084SBarry Smith 
10292f8084SBarry Smith /*@C
11292f8084SBarry Smith    PFSet - Sets the C/C++/Fortran functions to be used by the PF function
12292f8084SBarry Smith 
13292f8084SBarry Smith    Collective on PF
14292f8084SBarry Smith 
15d8d19677SJose E. Roman    Input Parameters:
16292f8084SBarry Smith +  pf - the function context
17292f8084SBarry Smith .  apply - function to apply to an array
18292f8084SBarry Smith .  applyvec - function to apply to a Vec
19292f8084SBarry Smith .  view - function that prints information about the PF
20292f8084SBarry Smith .  destroy - function to free the private function context
21292f8084SBarry Smith -  ctx - private function context
22292f8084SBarry Smith 
23292f8084SBarry Smith    Level: beginner
24292f8084SBarry Smith 
25292f8084SBarry Smith .seealso: PFCreate(), PFDestroy(), PFSetType(), PFApply(), PFApplyVec()
26292f8084SBarry Smith @*/
277087cfbeSBarry Smith 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)
28292f8084SBarry Smith {
29292f8084SBarry Smith   PetscFunctionBegin;
300700a824SBarry Smith   PetscValidHeaderSpecific(pf,PF_CLASSID,1);
31292f8084SBarry Smith   pf->data          = ctx;
32292f8084SBarry Smith   pf->ops->destroy  = destroy;
33292f8084SBarry Smith   pf->ops->apply    = apply;
34292f8084SBarry Smith   pf->ops->applyvec = applyvec;
35292f8084SBarry Smith   pf->ops->view     = view;
36292f8084SBarry Smith   PetscFunctionReturn(0);
37292f8084SBarry Smith }
38292f8084SBarry Smith 
39292f8084SBarry Smith /*@C
40292f8084SBarry Smith    PFDestroy - Destroys PF context that was created with PFCreate().
41292f8084SBarry Smith 
42292f8084SBarry Smith    Collective on PF
43292f8084SBarry Smith 
44292f8084SBarry Smith    Input Parameter:
45292f8084SBarry Smith .  pf - the function context
46292f8084SBarry Smith 
47292f8084SBarry Smith    Level: beginner
48292f8084SBarry Smith 
49292f8084SBarry Smith .seealso: PFCreate(), PFSet(), PFSetType()
50292f8084SBarry Smith @*/
516bf464f9SBarry Smith PetscErrorCode  PFDestroy(PF *pf)
52292f8084SBarry Smith {
53292f8084SBarry Smith   PetscFunctionBegin;
546bf464f9SBarry Smith   if (!*pf) PetscFunctionReturn(0);
556bf464f9SBarry Smith   PetscValidHeaderSpecific((*pf),PF_CLASSID,1);
566bf464f9SBarry Smith   if (--((PetscObject)(*pf))->refct > 0) PetscFunctionReturn(0);
57292f8084SBarry Smith 
589566063dSJacob Faibussowitsch   PetscCall(PFViewFromOptions(*pf,NULL,"-pf_view"));
59e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
609566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*pf));
61292f8084SBarry Smith 
629566063dSJacob Faibussowitsch   if ((*pf)->ops->destroy) PetscCall((*(*pf)->ops->destroy)((*pf)->data));
639566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(pf));
64292f8084SBarry Smith   PetscFunctionReturn(0);
65292f8084SBarry Smith }
66292f8084SBarry Smith 
67292f8084SBarry Smith /*@C
68292f8084SBarry Smith    PFCreate - Creates a mathematical function context.
69292f8084SBarry Smith 
70d083f849SBarry Smith    Collective
71292f8084SBarry Smith 
72d8d19677SJose E. Roman    Input Parameters:
73292f8084SBarry Smith +  comm - MPI communicator
74292f8084SBarry Smith .  dimin - dimension of the space you are mapping from
75292f8084SBarry Smith -  dimout - dimension of the space you are mapping to
76292f8084SBarry Smith 
77292f8084SBarry Smith    Output Parameter:
78292f8084SBarry Smith .  pf - the function context
79292f8084SBarry Smith 
80292f8084SBarry Smith    Level: developer
81292f8084SBarry Smith 
8203193ff8SBarry Smith .seealso: PFSet(), PFApply(), PFDestroy(), PFApplyVec()
83292f8084SBarry Smith @*/
847087cfbeSBarry Smith PetscErrorCode  PFCreate(MPI_Comm comm,PetscInt dimin,PetscInt dimout,PF *pf)
85292f8084SBarry Smith {
86292f8084SBarry Smith   PF             newpf;
87292f8084SBarry Smith 
88292f8084SBarry Smith   PetscFunctionBegin;
89064a246eSJacob Faibussowitsch   PetscValidPointer(pf,4);
900298fd71SBarry Smith   *pf = NULL;
919566063dSJacob Faibussowitsch   PetscCall(PFInitializePackage());
92292f8084SBarry Smith 
939566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(newpf,PF_CLASSID,"PF","Mathematical functions","Vec",comm,PFDestroy,PFView));
944c8fdceaSLisandro Dalcin   newpf->data          = NULL;
954c8fdceaSLisandro Dalcin   newpf->ops->destroy  = NULL;
964c8fdceaSLisandro Dalcin   newpf->ops->apply    = NULL;
974c8fdceaSLisandro Dalcin   newpf->ops->applyvec = NULL;
984c8fdceaSLisandro Dalcin   newpf->ops->view     = NULL;
99292f8084SBarry Smith   newpf->dimin         = dimin;
100292f8084SBarry Smith   newpf->dimout        = dimout;
101292f8084SBarry Smith 
102292f8084SBarry Smith   *pf                  = newpf;
103292f8084SBarry Smith   PetscFunctionReturn(0);
104292f8084SBarry Smith 
105292f8084SBarry Smith }
106292f8084SBarry Smith 
107292f8084SBarry Smith /* -------------------------------------------------------------------------------*/
108292f8084SBarry Smith 
109292f8084SBarry Smith /*@
110292f8084SBarry Smith    PFApplyVec - Applies the mathematical function to a vector
111292f8084SBarry Smith 
112292f8084SBarry Smith    Collective on PF
113292f8084SBarry Smith 
114292f8084SBarry Smith    Input Parameters:
115292f8084SBarry Smith +  pf - the function context
1160298fd71SBarry Smith -  x - input vector (or NULL for the vector (0,1, .... N-1)
117292f8084SBarry Smith 
118292f8084SBarry Smith    Output Parameter:
119292f8084SBarry Smith .  y - output vector
120292f8084SBarry Smith 
121292f8084SBarry Smith    Level: beginner
122292f8084SBarry Smith 
123292f8084SBarry Smith .seealso: PFApply(), PFCreate(), PFDestroy(), PFSetType(), PFSet()
124292f8084SBarry Smith @*/
1257087cfbeSBarry Smith PetscErrorCode  PFApplyVec(PF pf,Vec x,Vec y)
126292f8084SBarry Smith {
127292f8084SBarry Smith   PetscInt       i,rstart,rend,n,p;
128ace3abfcSBarry Smith   PetscBool      nox = PETSC_FALSE;
129292f8084SBarry Smith 
130292f8084SBarry Smith   PetscFunctionBegin;
1310700a824SBarry Smith   PetscValidHeaderSpecific(pf,PF_CLASSID,1);
1320700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
133292f8084SBarry Smith   if (x) {
1340700a824SBarry Smith     PetscValidHeaderSpecific(x,VEC_CLASSID,2);
135*08401ef6SPierre Jolivet     PetscCheck(x != y,PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"x and y must be different vectors");
136292f8084SBarry Smith   } else {
137292f8084SBarry Smith     PetscScalar *xx;
13803193ff8SBarry Smith     PetscInt    lsize;
139292f8084SBarry Smith 
1409566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(y,&lsize));
14103193ff8SBarry Smith     lsize = pf->dimin*lsize/pf->dimout;
1429566063dSJacob Faibussowitsch     PetscCall(VecCreateMPI(PetscObjectComm((PetscObject)y),lsize,PETSC_DETERMINE,&x));
143292f8084SBarry Smith     nox   = PETSC_TRUE;
1449566063dSJacob Faibussowitsch     PetscCall(VecGetOwnershipRange(x,&rstart,&rend));
1459566063dSJacob Faibussowitsch     PetscCall(VecGetArray(x,&xx));
146f6e5521dSKarl Rupp     for (i=rstart; i<rend; i++) xx[i-rstart] = (PetscScalar)i;
1479566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(x,&xx));
148292f8084SBarry Smith   }
149292f8084SBarry Smith 
1509566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(x,&n));
1519566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(y,&p));
1522c71b3e2SJacob Faibussowitsch   PetscCheckFalse((pf->dimin*(n/pf->dimin)) != n,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local input vector length %" PetscInt_FMT " not divisible by dimin %" PetscInt_FMT " of function",n,pf->dimin);
1532c71b3e2SJacob Faibussowitsch   PetscCheckFalse((pf->dimout*(p/pf->dimout)) != p,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local output vector length %" PetscInt_FMT " not divisible by dimout %" PetscInt_FMT " of function",p,pf->dimout);
154*08401ef6SPierre Jolivet   PetscCheck((n/pf->dimin) == (p/pf->dimout),PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local vector lengths %" PetscInt_FMT " %" PetscInt_FMT " are wrong for dimin and dimout %" PetscInt_FMT " %" PetscInt_FMT " of function",n,p,pf->dimin,pf->dimout);
155292f8084SBarry Smith 
156292f8084SBarry Smith   if (pf->ops->applyvec) {
1579566063dSJacob Faibussowitsch     PetscCall((*pf->ops->applyvec)(pf->data,x,y));
158292f8084SBarry Smith   } else {
159292f8084SBarry Smith     PetscScalar *xx,*yy;
160292f8084SBarry Smith 
1619566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x,&n));
162292f8084SBarry Smith     n    = n/pf->dimin;
1639566063dSJacob Faibussowitsch     PetscCall(VecGetArray(x,&xx));
1649566063dSJacob Faibussowitsch     PetscCall(VecGetArray(y,&yy));
16528b400f6SJacob Faibussowitsch     PetscCheck(pf->ops->apply,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No function has been provided for this PF");
1669566063dSJacob Faibussowitsch     PetscCall((*pf->ops->apply)(pf->data,n,xx,yy));
1679566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(x,&xx));
1689566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(y,&yy));
169292f8084SBarry Smith   }
170292f8084SBarry Smith   if (nox) {
1719566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&x));
172292f8084SBarry Smith   }
173292f8084SBarry Smith   PetscFunctionReturn(0);
174292f8084SBarry Smith }
175292f8084SBarry Smith 
176292f8084SBarry Smith /*@
177292f8084SBarry Smith    PFApply - Applies the mathematical function to an array of values.
178292f8084SBarry Smith 
179292f8084SBarry Smith    Collective on PF
180292f8084SBarry Smith 
181292f8084SBarry Smith    Input Parameters:
182292f8084SBarry Smith +  pf - the function context
183292f8084SBarry Smith .  n - number of pointwise function evaluations to perform, each pointwise function evaluation
184292f8084SBarry Smith        is a function of dimin variables and computes dimout variables where dimin and dimout are defined
185292f8084SBarry Smith        in the call to PFCreate()
186292f8084SBarry Smith -  x - input array
187292f8084SBarry Smith 
188292f8084SBarry Smith    Output Parameter:
189292f8084SBarry Smith .  y - output array
190292f8084SBarry Smith 
191292f8084SBarry Smith    Level: beginner
192292f8084SBarry Smith 
193292f8084SBarry Smith    Notes:
194292f8084SBarry Smith 
195292f8084SBarry Smith .seealso: PFApplyVec(), PFCreate(), PFDestroy(), PFSetType(), PFSet()
196292f8084SBarry Smith @*/
1977087cfbeSBarry Smith PetscErrorCode  PFApply(PF pf,PetscInt n,const PetscScalar *x,PetscScalar *y)
198292f8084SBarry Smith {
199292f8084SBarry Smith   PetscFunctionBegin;
2000700a824SBarry Smith   PetscValidHeaderSpecific(pf,PF_CLASSID,1);
201064a246eSJacob Faibussowitsch   PetscValidScalarPointer(x,3);
202064a246eSJacob Faibussowitsch   PetscValidScalarPointer(y,4);
203*08401ef6SPierre Jolivet   PetscCheck(x != y,PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"x and y must be different arrays");
20428b400f6SJacob Faibussowitsch   PetscCheck(pf->ops->apply,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No function has been provided for this PF");
205292f8084SBarry Smith 
2069566063dSJacob Faibussowitsch   PetscCall((*pf->ops->apply)(pf->data,n,x,y));
207292f8084SBarry Smith   PetscFunctionReturn(0);
208292f8084SBarry Smith }
209292f8084SBarry Smith 
210fe2efc57SMark /*@C
211fe2efc57SMark    PFViewFromOptions - View from Options
212fe2efc57SMark 
213fe2efc57SMark    Collective on PF
214fe2efc57SMark 
215fe2efc57SMark    Input Parameters:
216fe2efc57SMark +  A - the PF context
217736c3998SJose E. Roman .  obj - Optional object
218736c3998SJose E. Roman -  name - command line option
219fe2efc57SMark 
220fe2efc57SMark    Level: intermediate
221fe2efc57SMark .seealso:  PF, PFView, PetscObjectViewFromOptions(), PFCreate()
222fe2efc57SMark @*/
223fe2efc57SMark PetscErrorCode  PFViewFromOptions(PF A,PetscObject obj,const char name[])
224fe2efc57SMark {
225fe2efc57SMark   PetscFunctionBegin;
226fe2efc57SMark   PetscValidHeaderSpecific(A,PF_CLASSID,1);
2279566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)A,obj,name));
228fe2efc57SMark   PetscFunctionReturn(0);
229fe2efc57SMark }
230fe2efc57SMark 
231292f8084SBarry Smith /*@
232292f8084SBarry Smith    PFView - Prints information about a mathematical function
233292f8084SBarry Smith 
234292f8084SBarry Smith    Collective on PF unless PetscViewer is PETSC_VIEWER_STDOUT_SELF
235292f8084SBarry Smith 
236292f8084SBarry Smith    Input Parameters:
237292f8084SBarry Smith +  PF - the PF context
238292f8084SBarry Smith -  viewer - optional visualization context
239292f8084SBarry Smith 
240292f8084SBarry Smith    Note:
241292f8084SBarry Smith    The available visualization contexts include
242292f8084SBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
243292f8084SBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
244292f8084SBarry Smith          output where only the first processor opens
245292f8084SBarry Smith          the file.  All other processors send their
246292f8084SBarry Smith          data to the first processor to print.
247292f8084SBarry Smith 
248292f8084SBarry Smith    The user can open an alternative visualization contexts with
249292f8084SBarry Smith    PetscViewerASCIIOpen() (output to a specified file).
250292f8084SBarry Smith 
251292f8084SBarry Smith    Level: developer
252292f8084SBarry Smith 
253292f8084SBarry Smith .seealso: PetscViewerCreate(), PetscViewerASCIIOpen()
254292f8084SBarry Smith @*/
2557087cfbeSBarry Smith PetscErrorCode  PFView(PF pf,PetscViewer viewer)
256292f8084SBarry Smith {
257ace3abfcSBarry Smith   PetscBool         iascii;
258292f8084SBarry Smith   PetscViewerFormat format;
259292f8084SBarry Smith 
260292f8084SBarry Smith   PetscFunctionBegin;
2610700a824SBarry Smith   PetscValidHeaderSpecific(pf,PF_CLASSID,1);
2623050cee2SBarry Smith   if (!viewer) {
2639566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)pf),&viewer));
2643050cee2SBarry Smith   }
2650700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
266292f8084SBarry Smith   PetscCheckSameComm(pf,1,viewer,2);
267292f8084SBarry Smith 
2689566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii));
269292f8084SBarry Smith   if (iascii) {
2709566063dSJacob Faibussowitsch     PetscCall(PetscViewerGetFormat(viewer,&format));
2719566063dSJacob Faibussowitsch     PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)pf,viewer));
272292f8084SBarry Smith     if (pf->ops->view) {
2739566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPushTab(viewer));
2749566063dSJacob Faibussowitsch       PetscCall((*pf->ops->view)(pf->data,viewer));
2759566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPopTab(viewer));
276292f8084SBarry Smith     }
277292f8084SBarry Smith   }
278292f8084SBarry Smith   PetscFunctionReturn(0);
279292f8084SBarry Smith }
280292f8084SBarry Smith 
281bdf89e91SBarry Smith /*@C
282bdf89e91SBarry Smith    PFRegister - Adds a method to the mathematical function package.
283292f8084SBarry Smith 
284292f8084SBarry Smith    Not collective
285292f8084SBarry Smith 
286292f8084SBarry Smith    Input Parameters:
287292f8084SBarry Smith +  name_solver - name of a new user-defined solver
288292f8084SBarry Smith -  routine_create - routine to create method context
289292f8084SBarry Smith 
290292f8084SBarry Smith    Notes:
2911c84c290SBarry Smith    PFRegister() may be called multiple times to add several user-defined functions
292292f8084SBarry Smith 
293292f8084SBarry Smith    Sample usage:
294292f8084SBarry Smith .vb
295bdf89e91SBarry Smith    PFRegister("my_function",MyFunctionSetCreate);
296292f8084SBarry Smith .ve
297292f8084SBarry Smith 
298292f8084SBarry Smith    Then, your solver can be chosen with the procedural interface via
299292f8084SBarry Smith $     PFSetType(pf,"my_function")
300292f8084SBarry Smith    or at runtime via the option
301292f8084SBarry Smith $     -pf_type my_function
302292f8084SBarry Smith 
303292f8084SBarry Smith    Level: advanced
304292f8084SBarry Smith 
305292f8084SBarry Smith .seealso: PFRegisterAll(), PFRegisterDestroy(), PFRegister()
306bdf89e91SBarry Smith @*/
307bdf89e91SBarry Smith PetscErrorCode  PFRegister(const char sname[],PetscErrorCode (*function)(PF,void*))
308292f8084SBarry Smith {
309292f8084SBarry Smith   PetscFunctionBegin;
3109566063dSJacob Faibussowitsch   PetscCall(PFInitializePackage());
3119566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&PFList,sname,function));
312292f8084SBarry Smith   PetscFunctionReturn(0);
313292f8084SBarry Smith }
314292f8084SBarry Smith 
315292f8084SBarry Smith /*@C
316292f8084SBarry Smith    PFGetType - Gets the PF method type and name (as a string) from the PF
317292f8084SBarry Smith    context.
318292f8084SBarry Smith 
319292f8084SBarry Smith    Not Collective
320292f8084SBarry Smith 
321292f8084SBarry Smith    Input Parameter:
322292f8084SBarry Smith .  pf - the function context
323292f8084SBarry Smith 
324292f8084SBarry Smith    Output Parameter:
325c4e43342SLisandro Dalcin .  type - name of function
326292f8084SBarry Smith 
327292f8084SBarry Smith    Level: intermediate
328292f8084SBarry Smith 
329292f8084SBarry Smith .seealso: PFSetType()
330292f8084SBarry Smith 
331292f8084SBarry Smith @*/
33219fd82e9SBarry Smith PetscErrorCode  PFGetType(PF pf,PFType *type)
333292f8084SBarry Smith {
334292f8084SBarry Smith   PetscFunctionBegin;
3350700a824SBarry Smith   PetscValidHeaderSpecific(pf,PF_CLASSID,1);
336c4e43342SLisandro Dalcin   PetscValidPointer(type,2);
337c4e43342SLisandro Dalcin   *type = ((PetscObject)pf)->type_name;
338292f8084SBarry Smith   PetscFunctionReturn(0);
339292f8084SBarry Smith }
340292f8084SBarry Smith 
341292f8084SBarry Smith /*@C
342292f8084SBarry Smith    PFSetType - Builds PF for a particular function
343292f8084SBarry Smith 
344292f8084SBarry Smith    Collective on PF
345292f8084SBarry Smith 
346d8d19677SJose E. Roman    Input Parameters:
347292f8084SBarry Smith +  pf - the function context.
348292f8084SBarry Smith .  type - a known method
349292f8084SBarry Smith -  ctx - optional type dependent context
350292f8084SBarry Smith 
351292f8084SBarry Smith    Options Database Key:
352292f8084SBarry Smith .  -pf_type <type> - Sets PF type
353292f8084SBarry Smith 
354292f8084SBarry Smith   Notes:
355292f8084SBarry Smith   See "petsc/include/petscpf.h" for available methods (for instance,
356292f8084SBarry Smith   PFCONSTANT)
357292f8084SBarry Smith 
358292f8084SBarry Smith   Level: intermediate
359292f8084SBarry Smith 
3601c84c290SBarry Smith .seealso: PFSet(), PFRegister(), PFCreate(), DMDACreatePF()
361292f8084SBarry Smith 
362292f8084SBarry Smith @*/
36319fd82e9SBarry Smith PetscErrorCode  PFSetType(PF pf,PFType type,void *ctx)
364292f8084SBarry Smith {
365ace3abfcSBarry Smith   PetscBool      match;
3665f80ce2aSJacob Faibussowitsch   PetscErrorCode (*r)(PF,void*);
367292f8084SBarry Smith 
368292f8084SBarry Smith   PetscFunctionBegin;
3690700a824SBarry Smith   PetscValidHeaderSpecific(pf,PF_CLASSID,1);
370292f8084SBarry Smith   PetscValidCharPointer(type,2);
371292f8084SBarry Smith 
3729566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)pf,type,&match));
373292f8084SBarry Smith   if (match) PetscFunctionReturn(0);
374292f8084SBarry Smith 
3759566063dSJacob Faibussowitsch   if (pf->ops->destroy) PetscCall((*pf->ops->destroy)(pf));
3764c8fdceaSLisandro Dalcin   pf->data = NULL;
377292f8084SBarry Smith 
378292f8084SBarry Smith   /* Determine the PFCreateXXX routine for a particular function */
3799566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(PFList,type,&r));
3805f80ce2aSJacob Faibussowitsch   PetscCheck(r,PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested PF type %s",type);
3814c8fdceaSLisandro Dalcin   pf->ops->destroy  = NULL;
3824c8fdceaSLisandro Dalcin   pf->ops->view     = NULL;
3834c8fdceaSLisandro Dalcin   pf->ops->apply    = NULL;
3844c8fdceaSLisandro Dalcin   pf->ops->applyvec = NULL;
385292f8084SBarry Smith 
386292f8084SBarry Smith   /* Call the PFCreateXXX routine for this particular function */
3879566063dSJacob Faibussowitsch   PetscCall((*r)(pf,ctx));
388292f8084SBarry Smith 
3899566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)pf,type));
390292f8084SBarry Smith   PetscFunctionReturn(0);
391292f8084SBarry Smith }
392292f8084SBarry Smith 
393292f8084SBarry Smith /*@
394292f8084SBarry Smith    PFSetFromOptions - Sets PF options from the options database.
395292f8084SBarry Smith 
396292f8084SBarry Smith    Collective on PF
397292f8084SBarry Smith 
398292f8084SBarry Smith    Input Parameters:
399292f8084SBarry Smith .  pf - the mathematical function context
400292f8084SBarry Smith 
401292f8084SBarry Smith    Options Database Keys:
402292f8084SBarry Smith 
403292f8084SBarry Smith    Notes:
404292f8084SBarry Smith    To see all options, run your program with the -help option
405292f8084SBarry Smith    or consult the users manual.
406292f8084SBarry Smith 
407292f8084SBarry Smith    Level: intermediate
408292f8084SBarry Smith 
409292f8084SBarry Smith .seealso:
410292f8084SBarry Smith @*/
4117087cfbeSBarry Smith PetscErrorCode  PFSetFromOptions(PF pf)
412292f8084SBarry Smith {
413292f8084SBarry Smith   PetscErrorCode ierr;
414292f8084SBarry Smith   char           type[256];
415ace3abfcSBarry Smith   PetscBool      flg;
416292f8084SBarry Smith 
417292f8084SBarry Smith   PetscFunctionBegin;
4180700a824SBarry Smith   PetscValidHeaderSpecific(pf,PF_CLASSID,1);
419292f8084SBarry Smith 
4209566063dSJacob Faibussowitsch   ierr = PetscObjectOptionsBegin((PetscObject)pf);PetscCall(ierr);
4219566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-pf_type","Type of function","PFSetType",PFList,NULL,type,256,&flg));
422292f8084SBarry Smith   if (flg) {
4239566063dSJacob Faibussowitsch     PetscCall(PFSetType(pf,type,NULL));
424292f8084SBarry Smith   }
425292f8084SBarry Smith   if (pf->ops->setfromoptions) {
4269566063dSJacob Faibussowitsch     PetscCall((*pf->ops->setfromoptions)(PetscOptionsObject,pf));
427292f8084SBarry Smith   }
4285d973c19SBarry Smith 
4295d973c19SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
4309566063dSJacob Faibussowitsch   PetscCall(PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)pf));
4319566063dSJacob Faibussowitsch   ierr = PetscOptionsEnd();PetscCall(ierr);
432292f8084SBarry Smith   PetscFunctionReturn(0);
433292f8084SBarry Smith }
434292f8084SBarry Smith 
435ace3abfcSBarry Smith static PetscBool PFPackageInitialized = PETSC_FALSE;
436b022a5c1SBarry Smith /*@C
437b022a5c1SBarry Smith   PFFinalizePackage - This function destroys everything in the Petsc interface to Mathematica. It is
438b022a5c1SBarry Smith   called from PetscFinalize().
439b022a5c1SBarry Smith 
440b022a5c1SBarry Smith   Level: developer
441b022a5c1SBarry Smith 
442b022a5c1SBarry Smith .seealso: PetscFinalize()
443b022a5c1SBarry Smith @*/
4447087cfbeSBarry Smith PetscErrorCode  PFFinalizePackage(void)
445b022a5c1SBarry Smith {
446b022a5c1SBarry Smith   PetscFunctionBegin;
4479566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListDestroy(&PFList));
448b022a5c1SBarry Smith   PFPackageInitialized = PETSC_FALSE;
449b022a5c1SBarry Smith   PFRegisterAllCalled  = PETSC_FALSE;
450b022a5c1SBarry Smith   PetscFunctionReturn(0);
451b022a5c1SBarry Smith }
452b022a5c1SBarry Smith 
4539877f0dbSBarry Smith /*@C
4549877f0dbSBarry Smith   PFInitializePackage - This function initializes everything in the PF package. It is called
4558a690491SBarry Smith   from PetscDLLibraryRegister_petscvec() when using dynamic libraries, and on the first call to PFCreate()
4568a690491SBarry Smith   when using shared or static libraries.
4579877f0dbSBarry Smith 
4589877f0dbSBarry Smith   Level: developer
4599877f0dbSBarry Smith 
4609877f0dbSBarry Smith .seealso: PetscInitialize()
4619877f0dbSBarry Smith @*/
462607a6623SBarry Smith PetscErrorCode  PFInitializePackage(void)
4639877f0dbSBarry Smith {
4649877f0dbSBarry Smith   char           logList[256];
4658e81d068SLisandro Dalcin   PetscBool      opt,pkg;
4669877f0dbSBarry Smith 
4679877f0dbSBarry Smith   PetscFunctionBegin;
468b022a5c1SBarry Smith   if (PFPackageInitialized) PetscFunctionReturn(0);
469b022a5c1SBarry Smith   PFPackageInitialized = PETSC_TRUE;
4709877f0dbSBarry Smith   /* Register Classes */
4719566063dSJacob Faibussowitsch   PetscCall(PetscClassIdRegister("PointFunction",&PF_CLASSID));
4729877f0dbSBarry Smith   /* Register Constructors */
4739566063dSJacob Faibussowitsch   PetscCall(PFRegisterAll());
474e94e781bSJacob Faibussowitsch   /* Process Info */
475e94e781bSJacob Faibussowitsch   {
476e94e781bSJacob Faibussowitsch     PetscClassId  classids[1];
477e94e781bSJacob Faibussowitsch 
478e94e781bSJacob Faibussowitsch     classids[0] = PF_CLASSID;
4799566063dSJacob Faibussowitsch     PetscCall(PetscInfoProcessClass("pf", 1, classids));
4809877f0dbSBarry Smith   }
4819877f0dbSBarry Smith   /* Process summary exclusions */
4829566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetString(NULL,NULL,"-log_exclude",logList,sizeof(logList),&opt));
4839877f0dbSBarry Smith   if (opt) {
4849566063dSJacob Faibussowitsch     PetscCall(PetscStrInList("pf",logList,',',&pkg));
4859566063dSJacob Faibussowitsch     if (pkg) PetscCall(PetscLogEventExcludeClass(PF_CLASSID));
4869877f0dbSBarry Smith   }
4878e81d068SLisandro Dalcin   /* Register package finalizer */
4889566063dSJacob Faibussowitsch   PetscCall(PetscRegisterFinalize(PFFinalizePackage));
4899877f0dbSBarry Smith   PetscFunctionReturn(0);
4909877f0dbSBarry Smith }
491