xref: /petsc/src/vec/pf/interface/pf.c (revision 8e81d0682c925ac65a16095f60c998f9f3b7b642)
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 
15292f8084SBarry Smith    Input Parameter:
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 .keywords: PF, setting
26292f8084SBarry Smith 
27292f8084SBarry Smith .seealso: PFCreate(), PFDestroy(), PFSetType(), PFApply(), PFApplyVec()
28292f8084SBarry Smith @*/
297087cfbeSBarry 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)
30292f8084SBarry Smith {
31292f8084SBarry Smith   PetscFunctionBegin;
320700a824SBarry Smith   PetscValidHeaderSpecific(pf,PF_CLASSID,1);
33292f8084SBarry Smith   pf->data          = ctx;
34292f8084SBarry Smith   pf->ops->destroy  = destroy;
35292f8084SBarry Smith   pf->ops->apply    = apply;
36292f8084SBarry Smith   pf->ops->applyvec = applyvec;
37292f8084SBarry Smith   pf->ops->view     = view;
38292f8084SBarry Smith   PetscFunctionReturn(0);
39292f8084SBarry Smith }
40292f8084SBarry Smith 
41292f8084SBarry Smith /*@C
42292f8084SBarry Smith    PFDestroy - Destroys PF context that was created with PFCreate().
43292f8084SBarry Smith 
44292f8084SBarry Smith    Collective on PF
45292f8084SBarry Smith 
46292f8084SBarry Smith    Input Parameter:
47292f8084SBarry Smith .  pf - the function context
48292f8084SBarry Smith 
49292f8084SBarry Smith    Level: beginner
50292f8084SBarry Smith 
51292f8084SBarry Smith .keywords: PF, destroy
52292f8084SBarry Smith 
53292f8084SBarry Smith .seealso: PFCreate(), PFSet(), PFSetType()
54292f8084SBarry Smith @*/
556bf464f9SBarry Smith PetscErrorCode  PFDestroy(PF *pf)
56292f8084SBarry Smith {
57292f8084SBarry Smith   PetscErrorCode ierr;
58292f8084SBarry Smith 
59292f8084SBarry Smith   PetscFunctionBegin;
606bf464f9SBarry Smith   if (!*pf) PetscFunctionReturn(0);
616bf464f9SBarry Smith   PetscValidHeaderSpecific((*pf),PF_CLASSID,1);
626bf464f9SBarry Smith   if (--((PetscObject)(*pf))->refct > 0) PetscFunctionReturn(0);
63292f8084SBarry Smith 
64ce1779c8SBarry Smith   ierr = PFViewFromOptions(*pf,NULL,"-pf_view");CHKERRQ(ierr);
65e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
66e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*pf);CHKERRQ(ierr);
67292f8084SBarry Smith 
686bf464f9SBarry Smith   if ((*pf)->ops->destroy) {ierr =  (*(*pf)->ops->destroy)((*pf)->data);CHKERRQ(ierr);}
69d38fa0fbSBarry Smith   ierr = PetscHeaderDestroy(pf);CHKERRQ(ierr);
70292f8084SBarry Smith   PetscFunctionReturn(0);
71292f8084SBarry Smith }
72292f8084SBarry Smith 
73292f8084SBarry Smith /*@C
74292f8084SBarry Smith    PFCreate - Creates a mathematical function context.
75292f8084SBarry Smith 
76292f8084SBarry Smith    Collective on MPI_Comm
77292f8084SBarry Smith 
78292f8084SBarry Smith    Input Parameter:
79292f8084SBarry Smith +  comm - MPI communicator
80292f8084SBarry Smith .  dimin - dimension of the space you are mapping from
81292f8084SBarry Smith -  dimout - dimension of the space you are mapping to
82292f8084SBarry Smith 
83292f8084SBarry Smith    Output Parameter:
84292f8084SBarry Smith .  pf - the function context
85292f8084SBarry Smith 
86292f8084SBarry Smith    Level: developer
87292f8084SBarry Smith 
88292f8084SBarry Smith .keywords: PF, create, context
89292f8084SBarry Smith 
9003193ff8SBarry Smith .seealso: PFSet(), PFApply(), PFDestroy(), PFApplyVec()
91292f8084SBarry Smith @*/
927087cfbeSBarry Smith PetscErrorCode  PFCreate(MPI_Comm comm,PetscInt dimin,PetscInt dimout,PF *pf)
93292f8084SBarry Smith {
94292f8084SBarry Smith   PF             newpf;
95292f8084SBarry Smith   PetscErrorCode ierr;
96292f8084SBarry Smith 
97292f8084SBarry Smith   PetscFunctionBegin;
98292f8084SBarry Smith   PetscValidPointer(pf,1);
990298fd71SBarry Smith   *pf = NULL;
100607a6623SBarry Smith   ierr = PFInitializePackage();CHKERRQ(ierr);
101292f8084SBarry Smith 
10273107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(newpf,PF_CLASSID,"PF","Mathematical functions","Vec",comm,PFDestroy,PFView);CHKERRQ(ierr);
103292f8084SBarry Smith   newpf->data          = 0;
104292f8084SBarry Smith   newpf->ops->destroy  = 0;
105292f8084SBarry Smith   newpf->ops->apply    = 0;
106292f8084SBarry Smith   newpf->ops->applyvec = 0;
107292f8084SBarry Smith   newpf->ops->view     = 0;
108292f8084SBarry Smith   newpf->dimin         = dimin;
109292f8084SBarry Smith   newpf->dimout        = dimout;
110292f8084SBarry Smith 
111292f8084SBarry Smith   *pf                  = newpf;
112292f8084SBarry Smith   PetscFunctionReturn(0);
113292f8084SBarry Smith 
114292f8084SBarry Smith }
115292f8084SBarry Smith 
116292f8084SBarry Smith /* -------------------------------------------------------------------------------*/
117292f8084SBarry Smith 
118292f8084SBarry Smith /*@
119292f8084SBarry Smith    PFApplyVec - Applies the mathematical function to a vector
120292f8084SBarry Smith 
121292f8084SBarry Smith    Collective on PF
122292f8084SBarry Smith 
123292f8084SBarry Smith    Input Parameters:
124292f8084SBarry Smith +  pf - the function context
1250298fd71SBarry Smith -  x - input vector (or NULL for the vector (0,1, .... N-1)
126292f8084SBarry Smith 
127292f8084SBarry Smith    Output Parameter:
128292f8084SBarry Smith .  y - output vector
129292f8084SBarry Smith 
130292f8084SBarry Smith    Level: beginner
131292f8084SBarry Smith 
132292f8084SBarry Smith .keywords: PF, apply
133292f8084SBarry Smith 
134292f8084SBarry Smith .seealso: PFApply(), PFCreate(), PFDestroy(), PFSetType(), PFSet()
135292f8084SBarry Smith @*/
1367087cfbeSBarry Smith PetscErrorCode  PFApplyVec(PF pf,Vec x,Vec y)
137292f8084SBarry Smith {
138292f8084SBarry Smith   PetscErrorCode ierr;
139292f8084SBarry Smith   PetscInt       i,rstart,rend,n,p;
140ace3abfcSBarry Smith   PetscBool      nox = PETSC_FALSE;
141292f8084SBarry Smith 
142292f8084SBarry Smith   PetscFunctionBegin;
1430700a824SBarry Smith   PetscValidHeaderSpecific(pf,PF_CLASSID,1);
1440700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
145292f8084SBarry Smith   if (x) {
1460700a824SBarry Smith     PetscValidHeaderSpecific(x,VEC_CLASSID,2);
147e32f2f54SBarry Smith     if (x == y) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"x and y must be different vectors");
148292f8084SBarry Smith   } else {
149292f8084SBarry Smith     PetscScalar *xx;
15003193ff8SBarry Smith     PetscInt    lsize;
151292f8084SBarry Smith 
15203193ff8SBarry Smith     ierr  = VecGetLocalSize(y,&lsize);CHKERRQ(ierr);
15303193ff8SBarry Smith     lsize = pf->dimin*lsize/pf->dimout;
154ce94432eSBarry Smith     ierr  = VecCreateMPI(PetscObjectComm((PetscObject)y),lsize,PETSC_DETERMINE,&x);CHKERRQ(ierr);
155292f8084SBarry Smith     nox   = PETSC_TRUE;
156292f8084SBarry Smith     ierr  = VecGetOwnershipRange(x,&rstart,&rend);CHKERRQ(ierr);
157292f8084SBarry Smith     ierr  = VecGetArray(x,&xx);CHKERRQ(ierr);
158f6e5521dSKarl Rupp     for (i=rstart; i<rend; i++) xx[i-rstart] = (PetscScalar)i;
159292f8084SBarry Smith     ierr = VecRestoreArray(x,&xx);CHKERRQ(ierr);
160292f8084SBarry Smith   }
161292f8084SBarry Smith 
162292f8084SBarry Smith   ierr = VecGetLocalSize(x,&n);CHKERRQ(ierr);
163292f8084SBarry Smith   ierr = VecGetLocalSize(y,&p);CHKERRQ(ierr);
164e32f2f54SBarry Smith   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);
165e32f2f54SBarry Smith   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);
166e32f2f54SBarry Smith   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);
167292f8084SBarry Smith 
168292f8084SBarry Smith   if (pf->ops->applyvec) {
169292f8084SBarry Smith     ierr = (*pf->ops->applyvec)(pf->data,x,y);CHKERRQ(ierr);
170292f8084SBarry Smith   } else {
171292f8084SBarry Smith     PetscScalar *xx,*yy;
172292f8084SBarry Smith 
173292f8084SBarry Smith     ierr = VecGetLocalSize(x,&n);CHKERRQ(ierr);
174292f8084SBarry Smith     n    = n/pf->dimin;
175292f8084SBarry Smith     ierr = VecGetArray(x,&xx);CHKERRQ(ierr);
176292f8084SBarry Smith     ierr = VecGetArray(y,&yy);CHKERRQ(ierr);
177e32f2f54SBarry Smith     if (!pf->ops->apply) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No function has been provided for this PF");
178292f8084SBarry Smith     ierr = (*pf->ops->apply)(pf->data,n,xx,yy);CHKERRQ(ierr);
179292f8084SBarry Smith     ierr = VecRestoreArray(x,&xx);CHKERRQ(ierr);
180292f8084SBarry Smith     ierr = VecRestoreArray(y,&yy);CHKERRQ(ierr);
181292f8084SBarry Smith   }
182292f8084SBarry Smith   if (nox) {
1836bf464f9SBarry Smith     ierr = VecDestroy(&x);CHKERRQ(ierr);
184292f8084SBarry Smith   }
185292f8084SBarry Smith   PetscFunctionReturn(0);
186292f8084SBarry Smith }
187292f8084SBarry Smith 
188292f8084SBarry Smith /*@
189292f8084SBarry Smith    PFApply - Applies the mathematical function to an array of values.
190292f8084SBarry Smith 
191292f8084SBarry Smith    Collective on PF
192292f8084SBarry Smith 
193292f8084SBarry Smith    Input Parameters:
194292f8084SBarry Smith +  pf - the function context
195292f8084SBarry Smith .  n - number of pointwise function evaluations to perform, each pointwise function evaluation
196292f8084SBarry Smith        is a function of dimin variables and computes dimout variables where dimin and dimout are defined
197292f8084SBarry Smith        in the call to PFCreate()
198292f8084SBarry Smith -  x - input array
199292f8084SBarry Smith 
200292f8084SBarry Smith    Output Parameter:
201292f8084SBarry Smith .  y - output array
202292f8084SBarry Smith 
203292f8084SBarry Smith    Level: beginner
204292f8084SBarry Smith 
205292f8084SBarry Smith    Notes:
206292f8084SBarry Smith 
207292f8084SBarry Smith .keywords: PF, apply
208292f8084SBarry Smith 
209292f8084SBarry Smith .seealso: PFApplyVec(), PFCreate(), PFDestroy(), PFSetType(), PFSet()
210292f8084SBarry Smith @*/
2117087cfbeSBarry Smith PetscErrorCode  PFApply(PF pf,PetscInt n,const PetscScalar *x,PetscScalar *y)
212292f8084SBarry Smith {
213292f8084SBarry Smith   PetscErrorCode ierr;
214292f8084SBarry Smith 
215292f8084SBarry Smith   PetscFunctionBegin;
2160700a824SBarry Smith   PetscValidHeaderSpecific(pf,PF_CLASSID,1);
217292f8084SBarry Smith   PetscValidScalarPointer(x,2);
218292f8084SBarry Smith   PetscValidScalarPointer(y,3);
219e32f2f54SBarry Smith   if (x == y) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"x and y must be different arrays");
220e32f2f54SBarry Smith   if (!pf->ops->apply) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No function has been provided for this PF");
221292f8084SBarry Smith 
222292f8084SBarry Smith   ierr = (*pf->ops->apply)(pf->data,n,x,y);CHKERRQ(ierr);
223292f8084SBarry Smith   PetscFunctionReturn(0);
224292f8084SBarry Smith }
225292f8084SBarry Smith 
226292f8084SBarry Smith /*@
227292f8084SBarry Smith    PFView - Prints information about a mathematical function
228292f8084SBarry Smith 
229292f8084SBarry Smith    Collective on PF unless PetscViewer is PETSC_VIEWER_STDOUT_SELF
230292f8084SBarry Smith 
231292f8084SBarry Smith    Input Parameters:
232292f8084SBarry Smith +  PF - the PF context
233292f8084SBarry Smith -  viewer - optional visualization context
234292f8084SBarry Smith 
235292f8084SBarry Smith    Note:
236292f8084SBarry Smith    The available visualization contexts include
237292f8084SBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
238292f8084SBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
239292f8084SBarry Smith          output where only the first processor opens
240292f8084SBarry Smith          the file.  All other processors send their
241292f8084SBarry Smith          data to the first processor to print.
242292f8084SBarry Smith 
243292f8084SBarry Smith    The user can open an alternative visualization contexts with
244292f8084SBarry Smith    PetscViewerASCIIOpen() (output to a specified file).
245292f8084SBarry Smith 
246292f8084SBarry Smith    Level: developer
247292f8084SBarry Smith 
248292f8084SBarry Smith .keywords: PF, view
249292f8084SBarry Smith 
250292f8084SBarry Smith .seealso: PetscViewerCreate(), PetscViewerASCIIOpen()
251292f8084SBarry Smith @*/
2527087cfbeSBarry Smith PetscErrorCode  PFView(PF pf,PetscViewer viewer)
253292f8084SBarry Smith {
254292f8084SBarry Smith   PetscErrorCode    ierr;
255ace3abfcSBarry Smith   PetscBool         iascii;
256292f8084SBarry Smith   PetscViewerFormat format;
257292f8084SBarry Smith 
258292f8084SBarry Smith   PetscFunctionBegin;
2590700a824SBarry Smith   PetscValidHeaderSpecific(pf,PF_CLASSID,1);
2603050cee2SBarry Smith   if (!viewer) {
261ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)pf),&viewer);CHKERRQ(ierr);
2623050cee2SBarry Smith   }
2630700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
264292f8084SBarry Smith   PetscCheckSameComm(pf,1,viewer,2);
265292f8084SBarry Smith 
266251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
267292f8084SBarry Smith   if (iascii) {
268292f8084SBarry Smith     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
269dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)pf,viewer);CHKERRQ(ierr);
270292f8084SBarry Smith     if (pf->ops->view) {
271292f8084SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
272292f8084SBarry Smith       ierr = (*pf->ops->view)(pf->data,viewer);CHKERRQ(ierr);
273292f8084SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
274292f8084SBarry Smith     }
275292f8084SBarry Smith   }
276292f8084SBarry Smith   PetscFunctionReturn(0);
277292f8084SBarry Smith }
278292f8084SBarry Smith 
279292f8084SBarry Smith 
280bdf89e91SBarry Smith /*@C
281bdf89e91SBarry Smith    PFRegister - Adds a method to the mathematical function package.
282292f8084SBarry Smith 
283292f8084SBarry Smith    Not collective
284292f8084SBarry Smith 
285292f8084SBarry Smith    Input Parameters:
286292f8084SBarry Smith +  name_solver - name of a new user-defined solver
287292f8084SBarry Smith -  routine_create - routine to create method context
288292f8084SBarry Smith 
289292f8084SBarry Smith    Notes:
2901c84c290SBarry Smith    PFRegister() may be called multiple times to add several user-defined functions
291292f8084SBarry Smith 
292292f8084SBarry Smith    Sample usage:
293292f8084SBarry Smith .vb
294bdf89e91SBarry Smith    PFRegister("my_function",MyFunctionSetCreate);
295292f8084SBarry Smith .ve
296292f8084SBarry Smith 
297292f8084SBarry Smith    Then, your solver can be chosen with the procedural interface via
298292f8084SBarry Smith $     PFSetType(pf,"my_function")
299292f8084SBarry Smith    or at runtime via the option
300292f8084SBarry Smith $     -pf_type my_function
301292f8084SBarry Smith 
302292f8084SBarry Smith    Level: advanced
303292f8084SBarry Smith 
304292f8084SBarry Smith .keywords: PF, register
305292f8084SBarry Smith 
306292f8084SBarry Smith .seealso: PFRegisterAll(), PFRegisterDestroy(), PFRegister()
307bdf89e91SBarry Smith @*/
308bdf89e91SBarry Smith PetscErrorCode  PFRegister(const char sname[],PetscErrorCode (*function)(PF,void*))
309292f8084SBarry Smith {
310292f8084SBarry Smith   PetscErrorCode ierr;
311292f8084SBarry Smith 
312292f8084SBarry Smith   PetscFunctionBegin;
31337e93019SBarry Smith   ierr = PetscFunctionListAdd(&PFList,sname,function);CHKERRQ(ierr);
314292f8084SBarry Smith   PetscFunctionReturn(0);
315292f8084SBarry Smith }
316292f8084SBarry Smith 
317292f8084SBarry Smith /*@C
318292f8084SBarry Smith    PFGetType - Gets the PF method type and name (as a string) from the PF
319292f8084SBarry Smith    context.
320292f8084SBarry Smith 
321292f8084SBarry Smith    Not Collective
322292f8084SBarry Smith 
323292f8084SBarry Smith    Input Parameter:
324292f8084SBarry Smith .  pf - the function context
325292f8084SBarry Smith 
326292f8084SBarry Smith    Output Parameter:
327c4e43342SLisandro Dalcin .  type - name of function
328292f8084SBarry Smith 
329292f8084SBarry Smith    Level: intermediate
330292f8084SBarry Smith 
331292f8084SBarry Smith .keywords: PF, get, method, name, type
332292f8084SBarry Smith 
333292f8084SBarry Smith .seealso: PFSetType()
334292f8084SBarry Smith 
335292f8084SBarry Smith @*/
33619fd82e9SBarry Smith PetscErrorCode  PFGetType(PF pf,PFType *type)
337292f8084SBarry Smith {
338292f8084SBarry Smith   PetscFunctionBegin;
3390700a824SBarry Smith   PetscValidHeaderSpecific(pf,PF_CLASSID,1);
340c4e43342SLisandro Dalcin   PetscValidPointer(type,2);
341c4e43342SLisandro Dalcin   *type = ((PetscObject)pf)->type_name;
342292f8084SBarry Smith   PetscFunctionReturn(0);
343292f8084SBarry Smith }
344292f8084SBarry Smith 
345292f8084SBarry Smith 
346292f8084SBarry Smith /*@C
347292f8084SBarry Smith    PFSetType - Builds PF for a particular function
348292f8084SBarry Smith 
349292f8084SBarry Smith    Collective on PF
350292f8084SBarry Smith 
351292f8084SBarry Smith    Input Parameter:
352292f8084SBarry Smith +  pf - the function context.
353292f8084SBarry Smith .  type - a known method
354292f8084SBarry Smith -  ctx - optional type dependent context
355292f8084SBarry Smith 
356292f8084SBarry Smith    Options Database Key:
357292f8084SBarry Smith .  -pf_type <type> - Sets PF type
358292f8084SBarry Smith 
359292f8084SBarry Smith 
360292f8084SBarry Smith   Notes:
361292f8084SBarry Smith   See "petsc/include/petscpf.h" for available methods (for instance,
362292f8084SBarry Smith   PFCONSTANT)
363292f8084SBarry Smith 
364292f8084SBarry Smith   Level: intermediate
365292f8084SBarry Smith 
366292f8084SBarry Smith .keywords: PF, set, method, type
367292f8084SBarry Smith 
3681c84c290SBarry Smith .seealso: PFSet(), PFRegister(), PFCreate(), DMDACreatePF()
369292f8084SBarry Smith 
370292f8084SBarry Smith @*/
37119fd82e9SBarry Smith PetscErrorCode  PFSetType(PF pf,PFType type,void *ctx)
372292f8084SBarry Smith {
373292f8084SBarry Smith   PetscErrorCode ierr,(*r)(PF,void*);
374ace3abfcSBarry Smith   PetscBool      match;
375292f8084SBarry Smith 
376292f8084SBarry Smith   PetscFunctionBegin;
3770700a824SBarry Smith   PetscValidHeaderSpecific(pf,PF_CLASSID,1);
378292f8084SBarry Smith   PetscValidCharPointer(type,2);
379292f8084SBarry Smith 
380251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)pf,type,&match);CHKERRQ(ierr);
381292f8084SBarry Smith   if (match) PetscFunctionReturn(0);
382292f8084SBarry Smith 
383292f8084SBarry Smith   if (pf->ops->destroy) {ierr =  (*pf->ops->destroy)(pf);CHKERRQ(ierr);}
384292f8084SBarry Smith   pf->data = 0;
385292f8084SBarry Smith 
386292f8084SBarry Smith   /* Determine the PFCreateXXX routine for a particular function */
38737e93019SBarry Smith   ierr = PetscFunctionListFind(PFList,type,&r);CHKERRQ(ierr);
388e32f2f54SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested PF type %s",type);
389292f8084SBarry Smith   pf->ops->destroy  = 0;
390292f8084SBarry Smith   pf->ops->view     = 0;
391292f8084SBarry Smith   pf->ops->apply    = 0;
392292f8084SBarry Smith   pf->ops->applyvec = 0;
393292f8084SBarry Smith 
394292f8084SBarry Smith   /* Call the PFCreateXXX routine for this particular function */
395292f8084SBarry Smith   ierr = (*r)(pf,ctx);CHKERRQ(ierr);
396292f8084SBarry Smith 
397292f8084SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)pf,type);CHKERRQ(ierr);
398292f8084SBarry Smith   PetscFunctionReturn(0);
399292f8084SBarry Smith }
400292f8084SBarry Smith 
401292f8084SBarry Smith /*@
402292f8084SBarry Smith    PFSetFromOptions - Sets PF options from the options database.
403292f8084SBarry Smith 
404292f8084SBarry Smith    Collective on PF
405292f8084SBarry Smith 
406292f8084SBarry Smith    Input Parameters:
407292f8084SBarry Smith .  pf - the mathematical function context
408292f8084SBarry Smith 
409292f8084SBarry Smith    Options Database Keys:
410292f8084SBarry Smith 
411292f8084SBarry Smith    Notes:
412292f8084SBarry Smith    To see all options, run your program with the -help option
413292f8084SBarry Smith    or consult the users manual.
414292f8084SBarry Smith 
415292f8084SBarry Smith    Level: intermediate
416292f8084SBarry Smith 
417292f8084SBarry Smith .keywords: PF, set, from, options, database
418292f8084SBarry Smith 
419292f8084SBarry Smith .seealso:
420292f8084SBarry Smith @*/
4217087cfbeSBarry Smith PetscErrorCode  PFSetFromOptions(PF pf)
422292f8084SBarry Smith {
423292f8084SBarry Smith   PetscErrorCode ierr;
424292f8084SBarry Smith   char           type[256];
425ace3abfcSBarry Smith   PetscBool      flg;
426292f8084SBarry Smith 
427292f8084SBarry Smith   PetscFunctionBegin;
4280700a824SBarry Smith   PetscValidHeaderSpecific(pf,PF_CLASSID,1);
429292f8084SBarry Smith 
4303194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)pf);CHKERRQ(ierr);
431a264d7a6SBarry Smith   ierr = PetscOptionsFList("-pf_type","Type of function","PFSetType",PFList,0,type,256,&flg);CHKERRQ(ierr);
432292f8084SBarry Smith   if (flg) {
4330298fd71SBarry Smith     ierr = PFSetType(pf,type,NULL);CHKERRQ(ierr);
434292f8084SBarry Smith   }
435292f8084SBarry Smith   if (pf->ops->setfromoptions) {
436e55864a3SBarry Smith     ierr = (*pf->ops->setfromoptions)(PetscOptionsObject,pf);CHKERRQ(ierr);
437292f8084SBarry Smith   }
4385d973c19SBarry Smith 
4395d973c19SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
4400633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)pf);CHKERRQ(ierr);
441292f8084SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
442292f8084SBarry Smith   PetscFunctionReturn(0);
443292f8084SBarry Smith }
444292f8084SBarry Smith 
445ace3abfcSBarry Smith static PetscBool PFPackageInitialized = PETSC_FALSE;
446b022a5c1SBarry Smith /*@C
447b022a5c1SBarry Smith   PFFinalizePackage - This function destroys everything in the Petsc interface to Mathematica. It is
448b022a5c1SBarry Smith   called from PetscFinalize().
449b022a5c1SBarry Smith 
450b022a5c1SBarry Smith   Level: developer
451b022a5c1SBarry Smith 
452b022a5c1SBarry Smith .keywords: Petsc, destroy, package, mathematica
453b022a5c1SBarry Smith .seealso: PetscFinalize()
454b022a5c1SBarry Smith @*/
4557087cfbeSBarry Smith PetscErrorCode  PFFinalizePackage(void)
456b022a5c1SBarry Smith {
45737e93019SBarry Smith   PetscErrorCode ierr;
45837e93019SBarry Smith 
459b022a5c1SBarry Smith   PetscFunctionBegin;
46037e93019SBarry Smith   ierr = PetscFunctionListDestroy(&PFList);CHKERRQ(ierr);
461b022a5c1SBarry Smith   PFPackageInitialized = PETSC_FALSE;
462b022a5c1SBarry Smith   PFRegisterAllCalled  = PETSC_FALSE;
463b022a5c1SBarry Smith   PetscFunctionReturn(0);
464b022a5c1SBarry Smith }
465b022a5c1SBarry Smith 
4669877f0dbSBarry Smith /*@C
4679877f0dbSBarry Smith   PFInitializePackage - This function initializes everything in the PF package. It is called
4689877f0dbSBarry Smith   from PetscDLLibraryRegister() when using dynamic libraries, and on the first call to PFCreate()
4699877f0dbSBarry Smith   when using static libraries.
4709877f0dbSBarry Smith 
4719877f0dbSBarry Smith   Level: developer
4729877f0dbSBarry Smith 
4739877f0dbSBarry Smith .keywords: Vec, initialize, package
4749877f0dbSBarry Smith .seealso: PetscInitialize()
4759877f0dbSBarry Smith @*/
476607a6623SBarry Smith PetscErrorCode  PFInitializePackage(void)
4779877f0dbSBarry Smith {
4789877f0dbSBarry Smith   char           logList[256];
479*8e81d068SLisandro Dalcin   PetscBool      opt,pkg;
4809877f0dbSBarry Smith   PetscErrorCode ierr;
4819877f0dbSBarry Smith 
4829877f0dbSBarry Smith   PetscFunctionBegin;
483b022a5c1SBarry Smith   if (PFPackageInitialized) PetscFunctionReturn(0);
484b022a5c1SBarry Smith   PFPackageInitialized = PETSC_TRUE;
4859877f0dbSBarry Smith   /* Register Classes */
4860700a824SBarry Smith   ierr = PetscClassIdRegister("PointFunction",&PF_CLASSID);CHKERRQ(ierr);
4879877f0dbSBarry Smith   /* Register Constructors */
488607a6623SBarry Smith   ierr = PFRegisterAll();CHKERRQ(ierr);
4899877f0dbSBarry Smith   /* Process info exclusions */
490*8e81d068SLisandro Dalcin   ierr = PetscOptionsGetString(NULL,NULL,"-info_exclude",logList,sizeof(logList),&opt);CHKERRQ(ierr);
4919877f0dbSBarry Smith   if (opt) {
492*8e81d068SLisandro Dalcin     ierr = PetscStrInList("pf",logList,',',&pkg);CHKERRQ(ierr);
493*8e81d068SLisandro Dalcin     if (pkg) {ierr = PetscInfoDeactivateClass(PF_CLASSID);CHKERRQ(ierr);}
4949877f0dbSBarry Smith   }
4959877f0dbSBarry Smith   /* Process summary exclusions */
496*8e81d068SLisandro Dalcin   ierr = PetscOptionsGetString(NULL,NULL,"-log_exclude",logList,sizeof(logList),&opt);CHKERRQ(ierr);
4979877f0dbSBarry Smith   if (opt) {
498*8e81d068SLisandro Dalcin     ierr = PetscStrInList("pf",logList,',',&pkg);CHKERRQ(ierr);
499*8e81d068SLisandro Dalcin     if (pkg) {ierr = PetscLogEventDeactivateClass(PF_CLASSID);CHKERRQ(ierr);}
5009877f0dbSBarry Smith   }
501*8e81d068SLisandro Dalcin   /* Register package finalizer */
502b022a5c1SBarry Smith   ierr = PetscRegisterFinalize(PFFinalizePackage);CHKERRQ(ierr);
5039877f0dbSBarry Smith   PetscFunctionReturn(0);
5049877f0dbSBarry Smith }
505292f8084SBarry Smith 
506292f8084SBarry Smith 
507292f8084SBarry Smith 
508292f8084SBarry Smith 
509292f8084SBarry Smith 
510292f8084SBarry Smith 
511292f8084SBarry Smith 
512292f8084SBarry Smith 
513292f8084SBarry Smith 
514