xref: /petsc/src/vec/pf/interface/pf.c (revision 0633abcb43573dee031ebfc4bd9ab19a34328c30)
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 #undef __FUNCT__
11292f8084SBarry Smith #define __FUNCT__ "PFSet"
12292f8084SBarry Smith /*@C
13292f8084SBarry Smith    PFSet - Sets the C/C++/Fortran functions to be used by the PF function
14292f8084SBarry Smith 
15292f8084SBarry Smith    Collective on PF
16292f8084SBarry Smith 
17292f8084SBarry Smith    Input Parameter:
18292f8084SBarry Smith +  pf - the function context
19292f8084SBarry Smith .  apply - function to apply to an array
20292f8084SBarry Smith .  applyvec - function to apply to a Vec
21292f8084SBarry Smith .  view - function that prints information about the PF
22292f8084SBarry Smith .  destroy - function to free the private function context
23292f8084SBarry Smith -  ctx - private function context
24292f8084SBarry Smith 
25292f8084SBarry Smith    Level: beginner
26292f8084SBarry Smith 
27292f8084SBarry Smith .keywords: PF, setting
28292f8084SBarry Smith 
29292f8084SBarry Smith .seealso: PFCreate(), PFDestroy(), PFSetType(), PFApply(), PFApplyVec()
30292f8084SBarry Smith @*/
317087cfbeSBarry 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)
32292f8084SBarry Smith {
33292f8084SBarry Smith   PetscFunctionBegin;
340700a824SBarry Smith   PetscValidHeaderSpecific(pf,PF_CLASSID,1);
35292f8084SBarry Smith   pf->data          = ctx;
36292f8084SBarry Smith   pf->ops->destroy  = destroy;
37292f8084SBarry Smith   pf->ops->apply    = apply;
38292f8084SBarry Smith   pf->ops->applyvec = applyvec;
39292f8084SBarry Smith   pf->ops->view     = view;
40292f8084SBarry Smith   PetscFunctionReturn(0);
41292f8084SBarry Smith }
42292f8084SBarry Smith 
43292f8084SBarry Smith #undef __FUNCT__
44292f8084SBarry Smith #define __FUNCT__ "PFDestroy"
45292f8084SBarry Smith /*@C
46292f8084SBarry Smith    PFDestroy - Destroys PF context that was created with PFCreate().
47292f8084SBarry Smith 
48292f8084SBarry Smith    Collective on PF
49292f8084SBarry Smith 
50292f8084SBarry Smith    Input Parameter:
51292f8084SBarry Smith .  pf - the function context
52292f8084SBarry Smith 
53292f8084SBarry Smith    Level: beginner
54292f8084SBarry Smith 
55292f8084SBarry Smith .keywords: PF, destroy
56292f8084SBarry Smith 
57292f8084SBarry Smith .seealso: PFCreate(), PFSet(), PFSetType()
58292f8084SBarry Smith @*/
596bf464f9SBarry Smith PetscErrorCode  PFDestroy(PF *pf)
60292f8084SBarry Smith {
61292f8084SBarry Smith   PetscErrorCode ierr;
62292f8084SBarry Smith 
63292f8084SBarry Smith   PetscFunctionBegin;
646bf464f9SBarry Smith   if (!*pf) PetscFunctionReturn(0);
656bf464f9SBarry Smith   PetscValidHeaderSpecific((*pf),PF_CLASSID,1);
666bf464f9SBarry Smith   if (--((PetscObject)(*pf))->refct > 0) PetscFunctionReturn(0);
67292f8084SBarry Smith 
68ce1779c8SBarry Smith   ierr = PFViewFromOptions(*pf,NULL,"-pf_view");CHKERRQ(ierr);
69e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
70e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*pf);CHKERRQ(ierr);
71292f8084SBarry Smith 
726bf464f9SBarry Smith   if ((*pf)->ops->destroy) {ierr =  (*(*pf)->ops->destroy)((*pf)->data);CHKERRQ(ierr);}
73d38fa0fbSBarry Smith   ierr = PetscHeaderDestroy(pf);CHKERRQ(ierr);
74292f8084SBarry Smith   PetscFunctionReturn(0);
75292f8084SBarry Smith }
76292f8084SBarry Smith 
77292f8084SBarry Smith #undef __FUNCT__
78292f8084SBarry Smith #define __FUNCT__ "PFCreate"
79292f8084SBarry Smith /*@C
80292f8084SBarry Smith    PFCreate - Creates a mathematical function context.
81292f8084SBarry Smith 
82292f8084SBarry Smith    Collective on MPI_Comm
83292f8084SBarry Smith 
84292f8084SBarry Smith    Input Parameter:
85292f8084SBarry Smith +  comm - MPI communicator
86292f8084SBarry Smith .  dimin - dimension of the space you are mapping from
87292f8084SBarry Smith -  dimout - dimension of the space you are mapping to
88292f8084SBarry Smith 
89292f8084SBarry Smith    Output Parameter:
90292f8084SBarry Smith .  pf - the function context
91292f8084SBarry Smith 
92292f8084SBarry Smith    Level: developer
93292f8084SBarry Smith 
94292f8084SBarry Smith .keywords: PF, create, context
95292f8084SBarry Smith 
9603193ff8SBarry Smith .seealso: PFSet(), PFApply(), PFDestroy(), PFApplyVec()
97292f8084SBarry Smith @*/
987087cfbeSBarry Smith PetscErrorCode  PFCreate(MPI_Comm comm,PetscInt dimin,PetscInt dimout,PF *pf)
99292f8084SBarry Smith {
100292f8084SBarry Smith   PF             newpf;
101292f8084SBarry Smith   PetscErrorCode ierr;
102292f8084SBarry Smith 
103292f8084SBarry Smith   PetscFunctionBegin;
104292f8084SBarry Smith   PetscValidPointer(pf,1);
1050298fd71SBarry Smith   *pf = NULL;
106607a6623SBarry Smith   ierr = PFInitializePackage();CHKERRQ(ierr);
107292f8084SBarry Smith 
10873107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(newpf,PF_CLASSID,"PF","Mathematical functions","Vec",comm,PFDestroy,PFView);CHKERRQ(ierr);
109292f8084SBarry Smith   newpf->data          = 0;
110292f8084SBarry Smith   newpf->ops->destroy  = 0;
111292f8084SBarry Smith   newpf->ops->apply    = 0;
112292f8084SBarry Smith   newpf->ops->applyvec = 0;
113292f8084SBarry Smith   newpf->ops->view     = 0;
114292f8084SBarry Smith   newpf->dimin         = dimin;
115292f8084SBarry Smith   newpf->dimout        = dimout;
116292f8084SBarry Smith 
117292f8084SBarry Smith   *pf                  = newpf;
118292f8084SBarry Smith   PetscFunctionReturn(0);
119292f8084SBarry Smith 
120292f8084SBarry Smith }
121292f8084SBarry Smith 
122292f8084SBarry Smith /* -------------------------------------------------------------------------------*/
123292f8084SBarry Smith 
124292f8084SBarry Smith #undef __FUNCT__
125292f8084SBarry Smith #define __FUNCT__ "PFApplyVec"
126292f8084SBarry Smith /*@
127292f8084SBarry Smith    PFApplyVec - Applies the mathematical function to a vector
128292f8084SBarry Smith 
129292f8084SBarry Smith    Collective on PF
130292f8084SBarry Smith 
131292f8084SBarry Smith    Input Parameters:
132292f8084SBarry Smith +  pf - the function context
1330298fd71SBarry Smith -  x - input vector (or NULL for the vector (0,1, .... N-1)
134292f8084SBarry Smith 
135292f8084SBarry Smith    Output Parameter:
136292f8084SBarry Smith .  y - output vector
137292f8084SBarry Smith 
138292f8084SBarry Smith    Level: beginner
139292f8084SBarry Smith 
140292f8084SBarry Smith .keywords: PF, apply
141292f8084SBarry Smith 
142292f8084SBarry Smith .seealso: PFApply(), PFCreate(), PFDestroy(), PFSetType(), PFSet()
143292f8084SBarry Smith @*/
1447087cfbeSBarry Smith PetscErrorCode  PFApplyVec(PF pf,Vec x,Vec y)
145292f8084SBarry Smith {
146292f8084SBarry Smith   PetscErrorCode ierr;
147292f8084SBarry Smith   PetscInt       i,rstart,rend,n,p;
148ace3abfcSBarry Smith   PetscBool      nox = PETSC_FALSE;
149292f8084SBarry Smith 
150292f8084SBarry Smith   PetscFunctionBegin;
1510700a824SBarry Smith   PetscValidHeaderSpecific(pf,PF_CLASSID,1);
1520700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
153292f8084SBarry Smith   if (x) {
1540700a824SBarry Smith     PetscValidHeaderSpecific(x,VEC_CLASSID,2);
155e32f2f54SBarry Smith     if (x == y) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"x and y must be different vectors");
156292f8084SBarry Smith   } else {
157292f8084SBarry Smith     PetscScalar *xx;
15803193ff8SBarry Smith     PetscInt    lsize;
159292f8084SBarry Smith 
16003193ff8SBarry Smith     ierr  = VecGetLocalSize(y,&lsize);CHKERRQ(ierr);
16103193ff8SBarry Smith     lsize = pf->dimin*lsize/pf->dimout;
162ce94432eSBarry Smith     ierr  = VecCreateMPI(PetscObjectComm((PetscObject)y),lsize,PETSC_DETERMINE,&x);CHKERRQ(ierr);
163292f8084SBarry Smith     nox   = PETSC_TRUE;
164292f8084SBarry Smith     ierr  = VecGetOwnershipRange(x,&rstart,&rend);CHKERRQ(ierr);
165292f8084SBarry Smith     ierr  = VecGetArray(x,&xx);CHKERRQ(ierr);
166f6e5521dSKarl Rupp     for (i=rstart; i<rend; i++) xx[i-rstart] = (PetscScalar)i;
167292f8084SBarry Smith     ierr = VecRestoreArray(x,&xx);CHKERRQ(ierr);
168292f8084SBarry Smith   }
169292f8084SBarry Smith 
170292f8084SBarry Smith   ierr = VecGetLocalSize(x,&n);CHKERRQ(ierr);
171292f8084SBarry Smith   ierr = VecGetLocalSize(y,&p);CHKERRQ(ierr);
172e32f2f54SBarry 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);
173e32f2f54SBarry 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);
174e32f2f54SBarry 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);
175292f8084SBarry Smith 
176292f8084SBarry Smith   if (pf->ops->applyvec) {
177292f8084SBarry Smith     ierr = (*pf->ops->applyvec)(pf->data,x,y);CHKERRQ(ierr);
178292f8084SBarry Smith   } else {
179292f8084SBarry Smith     PetscScalar *xx,*yy;
180292f8084SBarry Smith 
181292f8084SBarry Smith     ierr = VecGetLocalSize(x,&n);CHKERRQ(ierr);
182292f8084SBarry Smith     n    = n/pf->dimin;
183292f8084SBarry Smith     ierr = VecGetArray(x,&xx);CHKERRQ(ierr);
184292f8084SBarry Smith     ierr = VecGetArray(y,&yy);CHKERRQ(ierr);
185e32f2f54SBarry Smith     if (!pf->ops->apply) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No function has been provided for this PF");
186292f8084SBarry Smith     ierr = (*pf->ops->apply)(pf->data,n,xx,yy);CHKERRQ(ierr);
187292f8084SBarry Smith     ierr = VecRestoreArray(x,&xx);CHKERRQ(ierr);
188292f8084SBarry Smith     ierr = VecRestoreArray(y,&yy);CHKERRQ(ierr);
189292f8084SBarry Smith   }
190292f8084SBarry Smith   if (nox) {
1916bf464f9SBarry Smith     ierr = VecDestroy(&x);CHKERRQ(ierr);
192292f8084SBarry Smith   }
193292f8084SBarry Smith   PetscFunctionReturn(0);
194292f8084SBarry Smith }
195292f8084SBarry Smith 
196292f8084SBarry Smith #undef __FUNCT__
197292f8084SBarry Smith #define __FUNCT__ "PFApply"
198292f8084SBarry Smith /*@
199292f8084SBarry Smith    PFApply - Applies the mathematical function to an array of values.
200292f8084SBarry Smith 
201292f8084SBarry Smith    Collective on PF
202292f8084SBarry Smith 
203292f8084SBarry Smith    Input Parameters:
204292f8084SBarry Smith +  pf - the function context
205292f8084SBarry Smith .  n - number of pointwise function evaluations to perform, each pointwise function evaluation
206292f8084SBarry Smith        is a function of dimin variables and computes dimout variables where dimin and dimout are defined
207292f8084SBarry Smith        in the call to PFCreate()
208292f8084SBarry Smith -  x - input array
209292f8084SBarry Smith 
210292f8084SBarry Smith    Output Parameter:
211292f8084SBarry Smith .  y - output array
212292f8084SBarry Smith 
213292f8084SBarry Smith    Level: beginner
214292f8084SBarry Smith 
215292f8084SBarry Smith    Notes:
216292f8084SBarry Smith 
217292f8084SBarry Smith .keywords: PF, apply
218292f8084SBarry Smith 
219292f8084SBarry Smith .seealso: PFApplyVec(), PFCreate(), PFDestroy(), PFSetType(), PFSet()
220292f8084SBarry Smith @*/
2217087cfbeSBarry Smith PetscErrorCode  PFApply(PF pf,PetscInt n,const PetscScalar *x,PetscScalar *y)
222292f8084SBarry Smith {
223292f8084SBarry Smith   PetscErrorCode ierr;
224292f8084SBarry Smith 
225292f8084SBarry Smith   PetscFunctionBegin;
2260700a824SBarry Smith   PetscValidHeaderSpecific(pf,PF_CLASSID,1);
227292f8084SBarry Smith   PetscValidScalarPointer(x,2);
228292f8084SBarry Smith   PetscValidScalarPointer(y,3);
229e32f2f54SBarry Smith   if (x == y) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"x and y must be different arrays");
230e32f2f54SBarry Smith   if (!pf->ops->apply) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No function has been provided for this PF");
231292f8084SBarry Smith 
232292f8084SBarry Smith   ierr = (*pf->ops->apply)(pf->data,n,x,y);CHKERRQ(ierr);
233292f8084SBarry Smith   PetscFunctionReturn(0);
234292f8084SBarry Smith }
235292f8084SBarry Smith 
236292f8084SBarry Smith #undef __FUNCT__
237292f8084SBarry Smith #define __FUNCT__ "PFView"
238292f8084SBarry Smith /*@
239292f8084SBarry Smith    PFView - Prints information about a mathematical function
240292f8084SBarry Smith 
241292f8084SBarry Smith    Collective on PF unless PetscViewer is PETSC_VIEWER_STDOUT_SELF
242292f8084SBarry Smith 
243292f8084SBarry Smith    Input Parameters:
244292f8084SBarry Smith +  PF - the PF context
245292f8084SBarry Smith -  viewer - optional visualization context
246292f8084SBarry Smith 
247292f8084SBarry Smith    Note:
248292f8084SBarry Smith    The available visualization contexts include
249292f8084SBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
250292f8084SBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
251292f8084SBarry Smith          output where only the first processor opens
252292f8084SBarry Smith          the file.  All other processors send their
253292f8084SBarry Smith          data to the first processor to print.
254292f8084SBarry Smith 
255292f8084SBarry Smith    The user can open an alternative visualization contexts with
256292f8084SBarry Smith    PetscViewerASCIIOpen() (output to a specified file).
257292f8084SBarry Smith 
258292f8084SBarry Smith    Level: developer
259292f8084SBarry Smith 
260292f8084SBarry Smith .keywords: PF, view
261292f8084SBarry Smith 
262292f8084SBarry Smith .seealso: PetscViewerCreate(), PetscViewerASCIIOpen()
263292f8084SBarry Smith @*/
2647087cfbeSBarry Smith PetscErrorCode  PFView(PF pf,PetscViewer viewer)
265292f8084SBarry Smith {
266292f8084SBarry Smith   PetscErrorCode    ierr;
267ace3abfcSBarry Smith   PetscBool         iascii;
268292f8084SBarry Smith   PetscViewerFormat format;
269292f8084SBarry Smith 
270292f8084SBarry Smith   PetscFunctionBegin;
2710700a824SBarry Smith   PetscValidHeaderSpecific(pf,PF_CLASSID,1);
2723050cee2SBarry Smith   if (!viewer) {
273ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)pf),&viewer);CHKERRQ(ierr);
2743050cee2SBarry Smith   }
2750700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
276292f8084SBarry Smith   PetscCheckSameComm(pf,1,viewer,2);
277292f8084SBarry Smith 
278251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
279292f8084SBarry Smith   if (iascii) {
280292f8084SBarry Smith     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
281dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)pf,viewer);CHKERRQ(ierr);
282292f8084SBarry Smith     if (pf->ops->view) {
283292f8084SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
284292f8084SBarry Smith       ierr = (*pf->ops->view)(pf->data,viewer);CHKERRQ(ierr);
285292f8084SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
286292f8084SBarry Smith     }
287292f8084SBarry Smith   }
288292f8084SBarry Smith   PetscFunctionReturn(0);
289292f8084SBarry Smith }
290292f8084SBarry Smith 
291292f8084SBarry Smith 
292bdf89e91SBarry Smith #undef __FUNCT__
293bdf89e91SBarry Smith #define __FUNCT__ "PFRegister"
294bdf89e91SBarry Smith /*@C
295bdf89e91SBarry Smith    PFRegister - Adds a method to the mathematical function package.
296292f8084SBarry Smith 
297292f8084SBarry Smith    Not collective
298292f8084SBarry Smith 
299292f8084SBarry Smith    Input Parameters:
300292f8084SBarry Smith +  name_solver - name of a new user-defined solver
301292f8084SBarry Smith -  routine_create - routine to create method context
302292f8084SBarry Smith 
303292f8084SBarry Smith    Notes:
3041c84c290SBarry Smith    PFRegister() may be called multiple times to add several user-defined functions
305292f8084SBarry Smith 
306292f8084SBarry Smith    Sample usage:
307292f8084SBarry Smith .vb
308bdf89e91SBarry Smith    PFRegister("my_function",MyFunctionSetCreate);
309292f8084SBarry Smith .ve
310292f8084SBarry Smith 
311292f8084SBarry Smith    Then, your solver can be chosen with the procedural interface via
312292f8084SBarry Smith $     PFSetType(pf,"my_function")
313292f8084SBarry Smith    or at runtime via the option
314292f8084SBarry Smith $     -pf_type my_function
315292f8084SBarry Smith 
316292f8084SBarry Smith    Level: advanced
317292f8084SBarry Smith 
318292f8084SBarry Smith .keywords: PF, register
319292f8084SBarry Smith 
320292f8084SBarry Smith .seealso: PFRegisterAll(), PFRegisterDestroy(), PFRegister()
321bdf89e91SBarry Smith @*/
322bdf89e91SBarry Smith PetscErrorCode  PFRegister(const char sname[],PetscErrorCode (*function)(PF,void*))
323292f8084SBarry Smith {
324292f8084SBarry Smith   PetscErrorCode ierr;
325292f8084SBarry Smith 
326292f8084SBarry Smith   PetscFunctionBegin;
32737e93019SBarry Smith   ierr = PetscFunctionListAdd(&PFList,sname,function);CHKERRQ(ierr);
328292f8084SBarry Smith   PetscFunctionReturn(0);
329292f8084SBarry Smith }
330292f8084SBarry Smith 
331292f8084SBarry Smith #undef __FUNCT__
332292f8084SBarry Smith #define __FUNCT__ "PFGetType"
333292f8084SBarry Smith /*@C
334292f8084SBarry Smith    PFGetType - Gets the PF method type and name (as a string) from the PF
335292f8084SBarry Smith    context.
336292f8084SBarry Smith 
337292f8084SBarry Smith    Not Collective
338292f8084SBarry Smith 
339292f8084SBarry Smith    Input Parameter:
340292f8084SBarry Smith .  pf - the function context
341292f8084SBarry Smith 
342292f8084SBarry Smith    Output Parameter:
343c4e43342SLisandro Dalcin .  type - name of function
344292f8084SBarry Smith 
345292f8084SBarry Smith    Level: intermediate
346292f8084SBarry Smith 
347292f8084SBarry Smith .keywords: PF, get, method, name, type
348292f8084SBarry Smith 
349292f8084SBarry Smith .seealso: PFSetType()
350292f8084SBarry Smith 
351292f8084SBarry Smith @*/
35219fd82e9SBarry Smith PetscErrorCode  PFGetType(PF pf,PFType *type)
353292f8084SBarry Smith {
354292f8084SBarry Smith   PetscFunctionBegin;
3550700a824SBarry Smith   PetscValidHeaderSpecific(pf,PF_CLASSID,1);
356c4e43342SLisandro Dalcin   PetscValidPointer(type,2);
357c4e43342SLisandro Dalcin   *type = ((PetscObject)pf)->type_name;
358292f8084SBarry Smith   PetscFunctionReturn(0);
359292f8084SBarry Smith }
360292f8084SBarry Smith 
361292f8084SBarry Smith 
362292f8084SBarry Smith #undef __FUNCT__
363292f8084SBarry Smith #define __FUNCT__ "PFSetType"
364292f8084SBarry Smith /*@C
365292f8084SBarry Smith    PFSetType - Builds PF for a particular function
366292f8084SBarry Smith 
367292f8084SBarry Smith    Collective on PF
368292f8084SBarry Smith 
369292f8084SBarry Smith    Input Parameter:
370292f8084SBarry Smith +  pf - the function context.
371292f8084SBarry Smith .  type - a known method
372292f8084SBarry Smith -  ctx - optional type dependent context
373292f8084SBarry Smith 
374292f8084SBarry Smith    Options Database Key:
375292f8084SBarry Smith .  -pf_type <type> - Sets PF type
376292f8084SBarry Smith 
377292f8084SBarry Smith 
378292f8084SBarry Smith   Notes:
379292f8084SBarry Smith   See "petsc/include/petscpf.h" for available methods (for instance,
380292f8084SBarry Smith   PFCONSTANT)
381292f8084SBarry Smith 
382292f8084SBarry Smith   Level: intermediate
383292f8084SBarry Smith 
384292f8084SBarry Smith .keywords: PF, set, method, type
385292f8084SBarry Smith 
3861c84c290SBarry Smith .seealso: PFSet(), PFRegister(), PFCreate(), DMDACreatePF()
387292f8084SBarry Smith 
388292f8084SBarry Smith @*/
38919fd82e9SBarry Smith PetscErrorCode  PFSetType(PF pf,PFType type,void *ctx)
390292f8084SBarry Smith {
391292f8084SBarry Smith   PetscErrorCode ierr,(*r)(PF,void*);
392ace3abfcSBarry Smith   PetscBool      match;
393292f8084SBarry Smith 
394292f8084SBarry Smith   PetscFunctionBegin;
3950700a824SBarry Smith   PetscValidHeaderSpecific(pf,PF_CLASSID,1);
396292f8084SBarry Smith   PetscValidCharPointer(type,2);
397292f8084SBarry Smith 
398251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)pf,type,&match);CHKERRQ(ierr);
399292f8084SBarry Smith   if (match) PetscFunctionReturn(0);
400292f8084SBarry Smith 
401292f8084SBarry Smith   if (pf->ops->destroy) {ierr =  (*pf->ops->destroy)(pf);CHKERRQ(ierr);}
402292f8084SBarry Smith   pf->data = 0;
403292f8084SBarry Smith 
404292f8084SBarry Smith   /* Determine the PFCreateXXX routine for a particular function */
40537e93019SBarry Smith   ierr = PetscFunctionListFind(PFList,type,&r);CHKERRQ(ierr);
406e32f2f54SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested PF type %s",type);
407292f8084SBarry Smith   pf->ops->destroy  = 0;
408292f8084SBarry Smith   pf->ops->view     = 0;
409292f8084SBarry Smith   pf->ops->apply    = 0;
410292f8084SBarry Smith   pf->ops->applyvec = 0;
411292f8084SBarry Smith 
412292f8084SBarry Smith   /* Call the PFCreateXXX routine for this particular function */
413292f8084SBarry Smith   ierr = (*r)(pf,ctx);CHKERRQ(ierr);
414292f8084SBarry Smith 
415292f8084SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)pf,type);CHKERRQ(ierr);
416292f8084SBarry Smith   PetscFunctionReturn(0);
417292f8084SBarry Smith }
418292f8084SBarry Smith 
419292f8084SBarry Smith #undef __FUNCT__
420292f8084SBarry Smith #define __FUNCT__ "PFSetFromOptions"
421292f8084SBarry Smith /*@
422292f8084SBarry Smith    PFSetFromOptions - Sets PF options from the options database.
423292f8084SBarry Smith 
424292f8084SBarry Smith    Collective on PF
425292f8084SBarry Smith 
426292f8084SBarry Smith    Input Parameters:
427292f8084SBarry Smith .  pf - the mathematical function context
428292f8084SBarry Smith 
429292f8084SBarry Smith    Options Database Keys:
430292f8084SBarry Smith 
431292f8084SBarry Smith    Notes:
432292f8084SBarry Smith    To see all options, run your program with the -help option
433292f8084SBarry Smith    or consult the users manual.
434292f8084SBarry Smith 
435292f8084SBarry Smith    Level: intermediate
436292f8084SBarry Smith 
437292f8084SBarry Smith .keywords: PF, set, from, options, database
438292f8084SBarry Smith 
439292f8084SBarry Smith .seealso:
440292f8084SBarry Smith @*/
4417087cfbeSBarry Smith PetscErrorCode  PFSetFromOptions(PF pf)
442292f8084SBarry Smith {
443292f8084SBarry Smith   PetscErrorCode ierr;
444292f8084SBarry Smith   char           type[256];
445ace3abfcSBarry Smith   PetscBool      flg;
446292f8084SBarry Smith 
447292f8084SBarry Smith   PetscFunctionBegin;
4480700a824SBarry Smith   PetscValidHeaderSpecific(pf,PF_CLASSID,1);
449292f8084SBarry Smith 
4503194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)pf);CHKERRQ(ierr);
451a264d7a6SBarry Smith   ierr = PetscOptionsFList("-pf_type","Type of function","PFSetType",PFList,0,type,256,&flg);CHKERRQ(ierr);
452292f8084SBarry Smith   if (flg) {
4530298fd71SBarry Smith     ierr = PFSetType(pf,type,NULL);CHKERRQ(ierr);
454292f8084SBarry Smith   }
455292f8084SBarry Smith   if (pf->ops->setfromoptions) {
456e55864a3SBarry Smith     ierr = (*pf->ops->setfromoptions)(PetscOptionsObject,pf);CHKERRQ(ierr);
457292f8084SBarry Smith   }
4585d973c19SBarry Smith 
4595d973c19SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
460*0633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)pf);CHKERRQ(ierr);
461292f8084SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
462292f8084SBarry Smith   PetscFunctionReturn(0);
463292f8084SBarry Smith }
464292f8084SBarry Smith 
465ace3abfcSBarry Smith static PetscBool PFPackageInitialized = PETSC_FALSE;
466b022a5c1SBarry Smith #undef __FUNCT__
467b022a5c1SBarry Smith #define __FUNCT__ "PFFinalizePackage"
468b022a5c1SBarry Smith /*@C
469b022a5c1SBarry Smith   PFFinalizePackage - This function destroys everything in the Petsc interface to Mathematica. It is
470b022a5c1SBarry Smith   called from PetscFinalize().
471b022a5c1SBarry Smith 
472b022a5c1SBarry Smith   Level: developer
473b022a5c1SBarry Smith 
474b022a5c1SBarry Smith .keywords: Petsc, destroy, package, mathematica
475b022a5c1SBarry Smith .seealso: PetscFinalize()
476b022a5c1SBarry Smith @*/
4777087cfbeSBarry Smith PetscErrorCode  PFFinalizePackage(void)
478b022a5c1SBarry Smith {
47937e93019SBarry Smith   PetscErrorCode ierr;
48037e93019SBarry Smith 
481b022a5c1SBarry Smith   PetscFunctionBegin;
48237e93019SBarry Smith   ierr = PetscFunctionListDestroy(&PFList);CHKERRQ(ierr);
483b022a5c1SBarry Smith   PFPackageInitialized = PETSC_FALSE;
484b022a5c1SBarry Smith   PFRegisterAllCalled  = PETSC_FALSE;
485b022a5c1SBarry Smith   PetscFunctionReturn(0);
486b022a5c1SBarry Smith }
487b022a5c1SBarry Smith 
4889877f0dbSBarry Smith #undef __FUNCT__
4899877f0dbSBarry Smith #define __FUNCT__ "PFInitializePackage"
4909877f0dbSBarry Smith /*@C
4919877f0dbSBarry Smith   PFInitializePackage - This function initializes everything in the PF package. It is called
4929877f0dbSBarry Smith   from PetscDLLibraryRegister() when using dynamic libraries, and on the first call to PFCreate()
4939877f0dbSBarry Smith   when using static libraries.
4949877f0dbSBarry Smith 
4959877f0dbSBarry Smith   Level: developer
4969877f0dbSBarry Smith 
4979877f0dbSBarry Smith .keywords: Vec, initialize, package
4989877f0dbSBarry Smith .seealso: PetscInitialize()
4999877f0dbSBarry Smith @*/
500607a6623SBarry Smith PetscErrorCode  PFInitializePackage(void)
5019877f0dbSBarry Smith {
5029877f0dbSBarry Smith   char           logList[256];
5039877f0dbSBarry Smith   char           *className;
504ace3abfcSBarry Smith   PetscBool      opt;
5059877f0dbSBarry Smith   PetscErrorCode ierr;
5069877f0dbSBarry Smith 
5079877f0dbSBarry Smith   PetscFunctionBegin;
508b022a5c1SBarry Smith   if (PFPackageInitialized) PetscFunctionReturn(0);
509b022a5c1SBarry Smith   PFPackageInitialized = PETSC_TRUE;
5109877f0dbSBarry Smith   /* Register Classes */
5110700a824SBarry Smith   ierr = PetscClassIdRegister("PointFunction",&PF_CLASSID);CHKERRQ(ierr);
5129877f0dbSBarry Smith   /* Register Constructors */
513607a6623SBarry Smith   ierr = PFRegisterAll();CHKERRQ(ierr);
5149877f0dbSBarry Smith   /* Process info exclusions */
5150298fd71SBarry Smith   ierr = PetscOptionsGetString(NULL, "-info_exclude", logList, 256, &opt);CHKERRQ(ierr);
5169877f0dbSBarry Smith   if (opt) {
5179877f0dbSBarry Smith     ierr = PetscStrstr(logList, "pf", &className);CHKERRQ(ierr);
5189877f0dbSBarry Smith     if (className) {
5190700a824SBarry Smith       ierr = PetscInfoDeactivateClass(PF_CLASSID);CHKERRQ(ierr);
5209877f0dbSBarry Smith     }
5219877f0dbSBarry Smith   }
5229877f0dbSBarry Smith   /* Process summary exclusions */
5230298fd71SBarry Smith   ierr = PetscOptionsGetString(NULL, "-log_summary_exclude", logList, 256, &opt);CHKERRQ(ierr);
5249877f0dbSBarry Smith   if (opt) {
5259877f0dbSBarry Smith     ierr = PetscStrstr(logList, "pf", &className);CHKERRQ(ierr);
5269877f0dbSBarry Smith     if (className) {
5270700a824SBarry Smith       ierr = PetscLogEventDeactivateClass(PF_CLASSID);CHKERRQ(ierr);
5289877f0dbSBarry Smith     }
5299877f0dbSBarry Smith   }
530b022a5c1SBarry Smith   ierr = PetscRegisterFinalize(PFFinalizePackage);CHKERRQ(ierr);
5319877f0dbSBarry Smith   PetscFunctionReturn(0);
5329877f0dbSBarry Smith }
533292f8084SBarry Smith 
534292f8084SBarry Smith 
535292f8084SBarry Smith 
536292f8084SBarry Smith 
537292f8084SBarry Smith 
538292f8084SBarry Smith 
539292f8084SBarry Smith 
540292f8084SBarry Smith 
541292f8084SBarry Smith 
542