xref: /petsc/src/vec/pf/interface/pf.c (revision d8d19677bbccf95218448bee62e6b87f4513e133)
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 
15*d8d19677SJose 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   PetscErrorCode ierr;
54292f8084SBarry Smith 
55292f8084SBarry Smith   PetscFunctionBegin;
566bf464f9SBarry Smith   if (!*pf) PetscFunctionReturn(0);
576bf464f9SBarry Smith   PetscValidHeaderSpecific((*pf),PF_CLASSID,1);
586bf464f9SBarry Smith   if (--((PetscObject)(*pf))->refct > 0) PetscFunctionReturn(0);
59292f8084SBarry Smith 
60ce1779c8SBarry Smith   ierr = PFViewFromOptions(*pf,NULL,"-pf_view");CHKERRQ(ierr);
61e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
62e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*pf);CHKERRQ(ierr);
63292f8084SBarry Smith 
646bf464f9SBarry Smith   if ((*pf)->ops->destroy) {ierr =  (*(*pf)->ops->destroy)((*pf)->data);CHKERRQ(ierr);}
65d38fa0fbSBarry Smith   ierr = PetscHeaderDestroy(pf);CHKERRQ(ierr);
66292f8084SBarry Smith   PetscFunctionReturn(0);
67292f8084SBarry Smith }
68292f8084SBarry Smith 
69292f8084SBarry Smith /*@C
70292f8084SBarry Smith    PFCreate - Creates a mathematical function context.
71292f8084SBarry Smith 
72d083f849SBarry Smith    Collective
73292f8084SBarry Smith 
74*d8d19677SJose E. Roman    Input Parameters:
75292f8084SBarry Smith +  comm - MPI communicator
76292f8084SBarry Smith .  dimin - dimension of the space you are mapping from
77292f8084SBarry Smith -  dimout - dimension of the space you are mapping to
78292f8084SBarry Smith 
79292f8084SBarry Smith    Output Parameter:
80292f8084SBarry Smith .  pf - the function context
81292f8084SBarry Smith 
82292f8084SBarry Smith    Level: developer
83292f8084SBarry Smith 
8403193ff8SBarry Smith .seealso: PFSet(), PFApply(), PFDestroy(), PFApplyVec()
85292f8084SBarry Smith @*/
867087cfbeSBarry Smith PetscErrorCode  PFCreate(MPI_Comm comm,PetscInt dimin,PetscInt dimout,PF *pf)
87292f8084SBarry Smith {
88292f8084SBarry Smith   PF             newpf;
89292f8084SBarry Smith   PetscErrorCode ierr;
90292f8084SBarry Smith 
91292f8084SBarry Smith   PetscFunctionBegin;
92064a246eSJacob Faibussowitsch   PetscValidPointer(pf,4);
930298fd71SBarry Smith   *pf = NULL;
94607a6623SBarry Smith   ierr = PFInitializePackage();CHKERRQ(ierr);
95292f8084SBarry Smith 
9673107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(newpf,PF_CLASSID,"PF","Mathematical functions","Vec",comm,PFDestroy,PFView);CHKERRQ(ierr);
974c8fdceaSLisandro Dalcin   newpf->data          = NULL;
984c8fdceaSLisandro Dalcin   newpf->ops->destroy  = NULL;
994c8fdceaSLisandro Dalcin   newpf->ops->apply    = NULL;
1004c8fdceaSLisandro Dalcin   newpf->ops->applyvec = NULL;
1014c8fdceaSLisandro Dalcin   newpf->ops->view     = NULL;
102292f8084SBarry Smith   newpf->dimin         = dimin;
103292f8084SBarry Smith   newpf->dimout        = dimout;
104292f8084SBarry Smith 
105292f8084SBarry Smith   *pf                  = newpf;
106292f8084SBarry Smith   PetscFunctionReturn(0);
107292f8084SBarry Smith 
108292f8084SBarry Smith }
109292f8084SBarry Smith 
110292f8084SBarry Smith /* -------------------------------------------------------------------------------*/
111292f8084SBarry Smith 
112292f8084SBarry Smith /*@
113292f8084SBarry Smith    PFApplyVec - Applies the mathematical function to a vector
114292f8084SBarry Smith 
115292f8084SBarry Smith    Collective on PF
116292f8084SBarry Smith 
117292f8084SBarry Smith    Input Parameters:
118292f8084SBarry Smith +  pf - the function context
1190298fd71SBarry Smith -  x - input vector (or NULL for the vector (0,1, .... N-1)
120292f8084SBarry Smith 
121292f8084SBarry Smith    Output Parameter:
122292f8084SBarry Smith .  y - output vector
123292f8084SBarry Smith 
124292f8084SBarry Smith    Level: beginner
125292f8084SBarry Smith 
126292f8084SBarry Smith .seealso: PFApply(), PFCreate(), PFDestroy(), PFSetType(), PFSet()
127292f8084SBarry Smith @*/
1287087cfbeSBarry Smith PetscErrorCode  PFApplyVec(PF pf,Vec x,Vec y)
129292f8084SBarry Smith {
130292f8084SBarry Smith   PetscErrorCode ierr;
131292f8084SBarry Smith   PetscInt       i,rstart,rend,n,p;
132ace3abfcSBarry Smith   PetscBool      nox = PETSC_FALSE;
133292f8084SBarry Smith 
134292f8084SBarry Smith   PetscFunctionBegin;
1350700a824SBarry Smith   PetscValidHeaderSpecific(pf,PF_CLASSID,1);
1360700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
137292f8084SBarry Smith   if (x) {
1380700a824SBarry Smith     PetscValidHeaderSpecific(x,VEC_CLASSID,2);
139e32f2f54SBarry Smith     if (x == y) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"x and y must be different vectors");
140292f8084SBarry Smith   } else {
141292f8084SBarry Smith     PetscScalar *xx;
14203193ff8SBarry Smith     PetscInt    lsize;
143292f8084SBarry Smith 
14403193ff8SBarry Smith     ierr  = VecGetLocalSize(y,&lsize);CHKERRQ(ierr);
14503193ff8SBarry Smith     lsize = pf->dimin*lsize/pf->dimout;
146ce94432eSBarry Smith     ierr  = VecCreateMPI(PetscObjectComm((PetscObject)y),lsize,PETSC_DETERMINE,&x);CHKERRQ(ierr);
147292f8084SBarry Smith     nox   = PETSC_TRUE;
148292f8084SBarry Smith     ierr  = VecGetOwnershipRange(x,&rstart,&rend);CHKERRQ(ierr);
149292f8084SBarry Smith     ierr  = VecGetArray(x,&xx);CHKERRQ(ierr);
150f6e5521dSKarl Rupp     for (i=rstart; i<rend; i++) xx[i-rstart] = (PetscScalar)i;
151292f8084SBarry Smith     ierr = VecRestoreArray(x,&xx);CHKERRQ(ierr);
152292f8084SBarry Smith   }
153292f8084SBarry Smith 
154292f8084SBarry Smith   ierr = VecGetLocalSize(x,&n);CHKERRQ(ierr);
155292f8084SBarry Smith   ierr = VecGetLocalSize(y,&p);CHKERRQ(ierr);
156e32f2f54SBarry 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);
157e32f2f54SBarry 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);
158e32f2f54SBarry 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);
159292f8084SBarry Smith 
160292f8084SBarry Smith   if (pf->ops->applyvec) {
161292f8084SBarry Smith     ierr = (*pf->ops->applyvec)(pf->data,x,y);CHKERRQ(ierr);
162292f8084SBarry Smith   } else {
163292f8084SBarry Smith     PetscScalar *xx,*yy;
164292f8084SBarry Smith 
165292f8084SBarry Smith     ierr = VecGetLocalSize(x,&n);CHKERRQ(ierr);
166292f8084SBarry Smith     n    = n/pf->dimin;
167292f8084SBarry Smith     ierr = VecGetArray(x,&xx);CHKERRQ(ierr);
168292f8084SBarry Smith     ierr = VecGetArray(y,&yy);CHKERRQ(ierr);
169e32f2f54SBarry Smith     if (!pf->ops->apply) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No function has been provided for this PF");
170292f8084SBarry Smith     ierr = (*pf->ops->apply)(pf->data,n,xx,yy);CHKERRQ(ierr);
171292f8084SBarry Smith     ierr = VecRestoreArray(x,&xx);CHKERRQ(ierr);
172292f8084SBarry Smith     ierr = VecRestoreArray(y,&yy);CHKERRQ(ierr);
173292f8084SBarry Smith   }
174292f8084SBarry Smith   if (nox) {
1756bf464f9SBarry Smith     ierr = VecDestroy(&x);CHKERRQ(ierr);
176292f8084SBarry Smith   }
177292f8084SBarry Smith   PetscFunctionReturn(0);
178292f8084SBarry Smith }
179292f8084SBarry Smith 
180292f8084SBarry Smith /*@
181292f8084SBarry Smith    PFApply - Applies the mathematical function to an array of values.
182292f8084SBarry Smith 
183292f8084SBarry Smith    Collective on PF
184292f8084SBarry Smith 
185292f8084SBarry Smith    Input Parameters:
186292f8084SBarry Smith +  pf - the function context
187292f8084SBarry Smith .  n - number of pointwise function evaluations to perform, each pointwise function evaluation
188292f8084SBarry Smith        is a function of dimin variables and computes dimout variables where dimin and dimout are defined
189292f8084SBarry Smith        in the call to PFCreate()
190292f8084SBarry Smith -  x - input array
191292f8084SBarry Smith 
192292f8084SBarry Smith    Output Parameter:
193292f8084SBarry Smith .  y - output array
194292f8084SBarry Smith 
195292f8084SBarry Smith    Level: beginner
196292f8084SBarry Smith 
197292f8084SBarry Smith    Notes:
198292f8084SBarry Smith 
199292f8084SBarry Smith .seealso: PFApplyVec(), PFCreate(), PFDestroy(), PFSetType(), PFSet()
200292f8084SBarry Smith @*/
2017087cfbeSBarry Smith PetscErrorCode  PFApply(PF pf,PetscInt n,const PetscScalar *x,PetscScalar *y)
202292f8084SBarry Smith {
203292f8084SBarry Smith   PetscErrorCode ierr;
204292f8084SBarry Smith 
205292f8084SBarry Smith   PetscFunctionBegin;
2060700a824SBarry Smith   PetscValidHeaderSpecific(pf,PF_CLASSID,1);
207064a246eSJacob Faibussowitsch   PetscValidScalarPointer(x,3);
208064a246eSJacob Faibussowitsch   PetscValidScalarPointer(y,4);
209e32f2f54SBarry Smith   if (x == y) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"x and y must be different arrays");
210e32f2f54SBarry Smith   if (!pf->ops->apply) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No function has been provided for this PF");
211292f8084SBarry Smith 
212292f8084SBarry Smith   ierr = (*pf->ops->apply)(pf->data,n,x,y);CHKERRQ(ierr);
213292f8084SBarry Smith   PetscFunctionReturn(0);
214292f8084SBarry Smith }
215292f8084SBarry Smith 
216fe2efc57SMark /*@C
217fe2efc57SMark    PFViewFromOptions - View from Options
218fe2efc57SMark 
219fe2efc57SMark    Collective on PF
220fe2efc57SMark 
221fe2efc57SMark    Input Parameters:
222fe2efc57SMark +  A - the PF context
223736c3998SJose E. Roman .  obj - Optional object
224736c3998SJose E. Roman -  name - command line option
225fe2efc57SMark 
226fe2efc57SMark    Level: intermediate
227fe2efc57SMark .seealso:  PF, PFView, PetscObjectViewFromOptions(), PFCreate()
228fe2efc57SMark @*/
229fe2efc57SMark PetscErrorCode  PFViewFromOptions(PF A,PetscObject obj,const char name[])
230fe2efc57SMark {
231fe2efc57SMark   PetscErrorCode ierr;
232fe2efc57SMark 
233fe2efc57SMark   PetscFunctionBegin;
234fe2efc57SMark   PetscValidHeaderSpecific(A,PF_CLASSID,1);
235fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)A,obj,name);CHKERRQ(ierr);
236fe2efc57SMark   PetscFunctionReturn(0);
237fe2efc57SMark }
238fe2efc57SMark 
239292f8084SBarry Smith /*@
240292f8084SBarry Smith    PFView - Prints information about a mathematical function
241292f8084SBarry Smith 
242292f8084SBarry Smith    Collective on PF unless PetscViewer is PETSC_VIEWER_STDOUT_SELF
243292f8084SBarry Smith 
244292f8084SBarry Smith    Input Parameters:
245292f8084SBarry Smith +  PF - the PF context
246292f8084SBarry Smith -  viewer - optional visualization context
247292f8084SBarry Smith 
248292f8084SBarry Smith    Note:
249292f8084SBarry Smith    The available visualization contexts include
250292f8084SBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
251292f8084SBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
252292f8084SBarry Smith          output where only the first processor opens
253292f8084SBarry Smith          the file.  All other processors send their
254292f8084SBarry Smith          data to the first processor to print.
255292f8084SBarry Smith 
256292f8084SBarry Smith    The user can open an alternative visualization contexts with
257292f8084SBarry Smith    PetscViewerASCIIOpen() (output to a specified file).
258292f8084SBarry Smith 
259292f8084SBarry Smith    Level: developer
260292f8084SBarry Smith 
261292f8084SBarry Smith .seealso: PetscViewerCreate(), PetscViewerASCIIOpen()
262292f8084SBarry Smith @*/
2637087cfbeSBarry Smith PetscErrorCode  PFView(PF pf,PetscViewer viewer)
264292f8084SBarry Smith {
265292f8084SBarry Smith   PetscErrorCode    ierr;
266ace3abfcSBarry Smith   PetscBool         iascii;
267292f8084SBarry Smith   PetscViewerFormat format;
268292f8084SBarry Smith 
269292f8084SBarry Smith   PetscFunctionBegin;
2700700a824SBarry Smith   PetscValidHeaderSpecific(pf,PF_CLASSID,1);
2713050cee2SBarry Smith   if (!viewer) {
272ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)pf),&viewer);CHKERRQ(ierr);
2733050cee2SBarry Smith   }
2740700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
275292f8084SBarry Smith   PetscCheckSameComm(pf,1,viewer,2);
276292f8084SBarry Smith 
277251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
278292f8084SBarry Smith   if (iascii) {
279292f8084SBarry Smith     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
280dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)pf,viewer);CHKERRQ(ierr);
281292f8084SBarry Smith     if (pf->ops->view) {
282292f8084SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
283292f8084SBarry Smith       ierr = (*pf->ops->view)(pf->data,viewer);CHKERRQ(ierr);
284292f8084SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
285292f8084SBarry Smith     }
286292f8084SBarry Smith   }
287292f8084SBarry Smith   PetscFunctionReturn(0);
288292f8084SBarry Smith }
289292f8084SBarry Smith 
290bdf89e91SBarry Smith /*@C
291bdf89e91SBarry Smith    PFRegister - Adds a method to the mathematical function package.
292292f8084SBarry Smith 
293292f8084SBarry Smith    Not collective
294292f8084SBarry Smith 
295292f8084SBarry Smith    Input Parameters:
296292f8084SBarry Smith +  name_solver - name of a new user-defined solver
297292f8084SBarry Smith -  routine_create - routine to create method context
298292f8084SBarry Smith 
299292f8084SBarry Smith    Notes:
3001c84c290SBarry Smith    PFRegister() may be called multiple times to add several user-defined functions
301292f8084SBarry Smith 
302292f8084SBarry Smith    Sample usage:
303292f8084SBarry Smith .vb
304bdf89e91SBarry Smith    PFRegister("my_function",MyFunctionSetCreate);
305292f8084SBarry Smith .ve
306292f8084SBarry Smith 
307292f8084SBarry Smith    Then, your solver can be chosen with the procedural interface via
308292f8084SBarry Smith $     PFSetType(pf,"my_function")
309292f8084SBarry Smith    or at runtime via the option
310292f8084SBarry Smith $     -pf_type my_function
311292f8084SBarry Smith 
312292f8084SBarry Smith    Level: advanced
313292f8084SBarry Smith 
314292f8084SBarry Smith .seealso: PFRegisterAll(), PFRegisterDestroy(), PFRegister()
315bdf89e91SBarry Smith @*/
316bdf89e91SBarry Smith PetscErrorCode  PFRegister(const char sname[],PetscErrorCode (*function)(PF,void*))
317292f8084SBarry Smith {
318292f8084SBarry Smith   PetscErrorCode ierr;
319292f8084SBarry Smith 
320292f8084SBarry Smith   PetscFunctionBegin;
3211d36bdfdSBarry Smith   ierr = PFInitializePackage();CHKERRQ(ierr);
32237e93019SBarry Smith   ierr = PetscFunctionListAdd(&PFList,sname,function);CHKERRQ(ierr);
323292f8084SBarry Smith   PetscFunctionReturn(0);
324292f8084SBarry Smith }
325292f8084SBarry Smith 
326292f8084SBarry Smith /*@C
327292f8084SBarry Smith    PFGetType - Gets the PF method type and name (as a string) from the PF
328292f8084SBarry Smith    context.
329292f8084SBarry Smith 
330292f8084SBarry Smith    Not Collective
331292f8084SBarry Smith 
332292f8084SBarry Smith    Input Parameter:
333292f8084SBarry Smith .  pf - the function context
334292f8084SBarry Smith 
335292f8084SBarry Smith    Output Parameter:
336c4e43342SLisandro Dalcin .  type - name of function
337292f8084SBarry Smith 
338292f8084SBarry Smith    Level: intermediate
339292f8084SBarry Smith 
340292f8084SBarry Smith .seealso: PFSetType()
341292f8084SBarry Smith 
342292f8084SBarry Smith @*/
34319fd82e9SBarry Smith PetscErrorCode  PFGetType(PF pf,PFType *type)
344292f8084SBarry Smith {
345292f8084SBarry Smith   PetscFunctionBegin;
3460700a824SBarry Smith   PetscValidHeaderSpecific(pf,PF_CLASSID,1);
347c4e43342SLisandro Dalcin   PetscValidPointer(type,2);
348c4e43342SLisandro Dalcin   *type = ((PetscObject)pf)->type_name;
349292f8084SBarry Smith   PetscFunctionReturn(0);
350292f8084SBarry Smith }
351292f8084SBarry Smith 
352292f8084SBarry Smith /*@C
353292f8084SBarry Smith    PFSetType - Builds PF for a particular function
354292f8084SBarry Smith 
355292f8084SBarry Smith    Collective on PF
356292f8084SBarry Smith 
357*d8d19677SJose E. Roman    Input Parameters:
358292f8084SBarry Smith +  pf - the function context.
359292f8084SBarry Smith .  type - a known method
360292f8084SBarry Smith -  ctx - optional type dependent context
361292f8084SBarry Smith 
362292f8084SBarry Smith    Options Database Key:
363292f8084SBarry Smith .  -pf_type <type> - Sets PF type
364292f8084SBarry Smith 
365292f8084SBarry Smith   Notes:
366292f8084SBarry Smith   See "petsc/include/petscpf.h" for available methods (for instance,
367292f8084SBarry Smith   PFCONSTANT)
368292f8084SBarry Smith 
369292f8084SBarry Smith   Level: intermediate
370292f8084SBarry Smith 
3711c84c290SBarry Smith .seealso: PFSet(), PFRegister(), PFCreate(), DMDACreatePF()
372292f8084SBarry Smith 
373292f8084SBarry Smith @*/
37419fd82e9SBarry Smith PetscErrorCode  PFSetType(PF pf,PFType type,void *ctx)
375292f8084SBarry Smith {
376292f8084SBarry Smith   PetscErrorCode ierr,(*r)(PF,void*);
377ace3abfcSBarry Smith   PetscBool      match;
378292f8084SBarry Smith 
379292f8084SBarry Smith   PetscFunctionBegin;
3800700a824SBarry Smith   PetscValidHeaderSpecific(pf,PF_CLASSID,1);
381292f8084SBarry Smith   PetscValidCharPointer(type,2);
382292f8084SBarry Smith 
383251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)pf,type,&match);CHKERRQ(ierr);
384292f8084SBarry Smith   if (match) PetscFunctionReturn(0);
385292f8084SBarry Smith 
386292f8084SBarry Smith   if (pf->ops->destroy) {ierr =  (*pf->ops->destroy)(pf);CHKERRQ(ierr);}
3874c8fdceaSLisandro Dalcin   pf->data = NULL;
388292f8084SBarry Smith 
389292f8084SBarry Smith   /* Determine the PFCreateXXX routine for a particular function */
39037e93019SBarry Smith   ierr = PetscFunctionListFind(PFList,type,&r);CHKERRQ(ierr);
391e32f2f54SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested PF type %s",type);
3924c8fdceaSLisandro Dalcin   pf->ops->destroy  = NULL;
3934c8fdceaSLisandro Dalcin   pf->ops->view     = NULL;
3944c8fdceaSLisandro Dalcin   pf->ops->apply    = NULL;
3954c8fdceaSLisandro Dalcin   pf->ops->applyvec = NULL;
396292f8084SBarry Smith 
397292f8084SBarry Smith   /* Call the PFCreateXXX routine for this particular function */
398292f8084SBarry Smith   ierr = (*r)(pf,ctx);CHKERRQ(ierr);
399292f8084SBarry Smith 
400292f8084SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)pf,type);CHKERRQ(ierr);
401292f8084SBarry Smith   PetscFunctionReturn(0);
402292f8084SBarry Smith }
403292f8084SBarry Smith 
404292f8084SBarry Smith /*@
405292f8084SBarry Smith    PFSetFromOptions - Sets PF options from the options database.
406292f8084SBarry Smith 
407292f8084SBarry Smith    Collective on PF
408292f8084SBarry Smith 
409292f8084SBarry Smith    Input Parameters:
410292f8084SBarry Smith .  pf - the mathematical function context
411292f8084SBarry Smith 
412292f8084SBarry Smith    Options Database Keys:
413292f8084SBarry Smith 
414292f8084SBarry Smith    Notes:
415292f8084SBarry Smith    To see all options, run your program with the -help option
416292f8084SBarry Smith    or consult the users manual.
417292f8084SBarry Smith 
418292f8084SBarry Smith    Level: intermediate
419292f8084SBarry Smith 
420292f8084SBarry Smith .seealso:
421292f8084SBarry Smith @*/
4227087cfbeSBarry Smith PetscErrorCode  PFSetFromOptions(PF pf)
423292f8084SBarry Smith {
424292f8084SBarry Smith   PetscErrorCode ierr;
425292f8084SBarry Smith   char           type[256];
426ace3abfcSBarry Smith   PetscBool      flg;
427292f8084SBarry Smith 
428292f8084SBarry Smith   PetscFunctionBegin;
4290700a824SBarry Smith   PetscValidHeaderSpecific(pf,PF_CLASSID,1);
430292f8084SBarry Smith 
4313194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)pf);CHKERRQ(ierr);
4324c8fdceaSLisandro Dalcin   ierr = PetscOptionsFList("-pf_type","Type of function","PFSetType",PFList,NULL,type,256,&flg);CHKERRQ(ierr);
433292f8084SBarry Smith   if (flg) {
4340298fd71SBarry Smith     ierr = PFSetType(pf,type,NULL);CHKERRQ(ierr);
435292f8084SBarry Smith   }
436292f8084SBarry Smith   if (pf->ops->setfromoptions) {
437e55864a3SBarry Smith     ierr = (*pf->ops->setfromoptions)(PetscOptionsObject,pf);CHKERRQ(ierr);
438292f8084SBarry Smith   }
4395d973c19SBarry Smith 
4405d973c19SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
4410633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)pf);CHKERRQ(ierr);
442292f8084SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
443292f8084SBarry Smith   PetscFunctionReturn(0);
444292f8084SBarry Smith }
445292f8084SBarry Smith 
446ace3abfcSBarry Smith static PetscBool PFPackageInitialized = PETSC_FALSE;
447b022a5c1SBarry Smith /*@C
448b022a5c1SBarry Smith   PFFinalizePackage - This function destroys everything in the Petsc interface to Mathematica. It is
449b022a5c1SBarry Smith   called from PetscFinalize().
450b022a5c1SBarry Smith 
451b022a5c1SBarry Smith   Level: developer
452b022a5c1SBarry Smith 
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
4688a690491SBarry Smith   from PetscDLLibraryRegister_petscvec() when using dynamic libraries, and on the first call to PFCreate()
4698a690491SBarry Smith   when using shared or static libraries.
4709877f0dbSBarry Smith 
4719877f0dbSBarry Smith   Level: developer
4729877f0dbSBarry Smith 
4739877f0dbSBarry Smith .seealso: PetscInitialize()
4749877f0dbSBarry Smith @*/
475607a6623SBarry Smith PetscErrorCode  PFInitializePackage(void)
4769877f0dbSBarry Smith {
4779877f0dbSBarry Smith   char           logList[256];
4788e81d068SLisandro Dalcin   PetscBool      opt,pkg;
4799877f0dbSBarry Smith   PetscErrorCode ierr;
4809877f0dbSBarry Smith 
4819877f0dbSBarry Smith   PetscFunctionBegin;
482b022a5c1SBarry Smith   if (PFPackageInitialized) PetscFunctionReturn(0);
483b022a5c1SBarry Smith   PFPackageInitialized = PETSC_TRUE;
4849877f0dbSBarry Smith   /* Register Classes */
4850700a824SBarry Smith   ierr = PetscClassIdRegister("PointFunction",&PF_CLASSID);CHKERRQ(ierr);
4869877f0dbSBarry Smith   /* Register Constructors */
487607a6623SBarry Smith   ierr = PFRegisterAll();CHKERRQ(ierr);
488e94e781bSJacob Faibussowitsch   /* Process Info */
489e94e781bSJacob Faibussowitsch   {
490e94e781bSJacob Faibussowitsch     PetscClassId  classids[1];
491e94e781bSJacob Faibussowitsch 
492e94e781bSJacob Faibussowitsch     classids[0] = PF_CLASSID;
493e94e781bSJacob Faibussowitsch     ierr = PetscInfoProcessClass("pf", 1, classids);CHKERRQ(ierr);
4949877f0dbSBarry Smith   }
4959877f0dbSBarry Smith   /* Process summary exclusions */
4968e81d068SLisandro Dalcin   ierr = PetscOptionsGetString(NULL,NULL,"-log_exclude",logList,sizeof(logList),&opt);CHKERRQ(ierr);
4979877f0dbSBarry Smith   if (opt) {
4988e81d068SLisandro Dalcin     ierr = PetscStrInList("pf",logList,',',&pkg);CHKERRQ(ierr);
499fa2bb9feSLisandro Dalcin     if (pkg) {ierr = PetscLogEventExcludeClass(PF_CLASSID);CHKERRQ(ierr);}
5009877f0dbSBarry Smith   }
5018e81d068SLisandro Dalcin   /* Register package finalizer */
502b022a5c1SBarry Smith   ierr = PetscRegisterFinalize(PFFinalizePackage);CHKERRQ(ierr);
5039877f0dbSBarry Smith   PetscFunctionReturn(0);
5049877f0dbSBarry Smith }
505292f8084SBarry Smith 
506