xref: /petsc/src/vec/pf/interface/pf.c (revision 48a46eb9bd028bec07ec0f396b1a3abb43f14558)
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 
25db781477SPatrick Sanan .seealso: `PFCreate()`, `PFDestroy()`, `PFSetType()`, `PFApply()`, `PFApplyVec()`
26292f8084SBarry Smith @*/
279371c9d4SSatish Balay 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   PetscFunctionBegin;
290700a824SBarry Smith   PetscValidHeaderSpecific(pf, PF_CLASSID, 1);
30292f8084SBarry Smith   pf->data          = ctx;
31292f8084SBarry Smith   pf->ops->destroy  = destroy;
32292f8084SBarry Smith   pf->ops->apply    = apply;
33292f8084SBarry Smith   pf->ops->applyvec = applyvec;
34292f8084SBarry Smith   pf->ops->view     = view;
35292f8084SBarry Smith   PetscFunctionReturn(0);
36292f8084SBarry Smith }
37292f8084SBarry Smith 
38292f8084SBarry Smith /*@C
39292f8084SBarry Smith    PFDestroy - Destroys PF context that was created with PFCreate().
40292f8084SBarry Smith 
41292f8084SBarry Smith    Collective on PF
42292f8084SBarry Smith 
43292f8084SBarry Smith    Input Parameter:
44292f8084SBarry Smith .  pf - the function context
45292f8084SBarry Smith 
46292f8084SBarry Smith    Level: beginner
47292f8084SBarry Smith 
48db781477SPatrick Sanan .seealso: `PFCreate()`, `PFSet()`, `PFSetType()`
49292f8084SBarry Smith @*/
509371c9d4SSatish Balay PetscErrorCode PFDestroy(PF *pf) {
51292f8084SBarry Smith   PetscFunctionBegin;
526bf464f9SBarry Smith   if (!*pf) PetscFunctionReturn(0);
536bf464f9SBarry Smith   PetscValidHeaderSpecific((*pf), PF_CLASSID, 1);
546bf464f9SBarry Smith   if (--((PetscObject)(*pf))->refct > 0) PetscFunctionReturn(0);
55292f8084SBarry Smith 
569566063dSJacob Faibussowitsch   PetscCall(PFViewFromOptions(*pf, NULL, "-pf_view"));
57e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
589566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*pf));
59292f8084SBarry Smith 
609566063dSJacob Faibussowitsch   if ((*pf)->ops->destroy) PetscCall((*(*pf)->ops->destroy)((*pf)->data));
619566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(pf));
62292f8084SBarry Smith   PetscFunctionReturn(0);
63292f8084SBarry Smith }
64292f8084SBarry Smith 
65292f8084SBarry Smith /*@C
66292f8084SBarry Smith    PFCreate - Creates a mathematical function context.
67292f8084SBarry Smith 
68d083f849SBarry Smith    Collective
69292f8084SBarry Smith 
70d8d19677SJose E. Roman    Input Parameters:
71292f8084SBarry Smith +  comm - MPI communicator
72292f8084SBarry Smith .  dimin - dimension of the space you are mapping from
73292f8084SBarry Smith -  dimout - dimension of the space you are mapping to
74292f8084SBarry Smith 
75292f8084SBarry Smith    Output Parameter:
76292f8084SBarry Smith .  pf - the function context
77292f8084SBarry Smith 
78292f8084SBarry Smith    Level: developer
79292f8084SBarry Smith 
80db781477SPatrick Sanan .seealso: `PFSet()`, `PFApply()`, `PFDestroy()`, `PFApplyVec()`
81292f8084SBarry Smith @*/
829371c9d4SSatish Balay PetscErrorCode PFCreate(MPI_Comm comm, PetscInt dimin, PetscInt dimout, PF *pf) {
83292f8084SBarry Smith   PF newpf;
84292f8084SBarry Smith 
85292f8084SBarry Smith   PetscFunctionBegin;
86064a246eSJacob Faibussowitsch   PetscValidPointer(pf, 4);
870298fd71SBarry Smith   *pf = NULL;
889566063dSJacob Faibussowitsch   PetscCall(PFInitializePackage());
89292f8084SBarry Smith 
909566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(newpf, PF_CLASSID, "PF", "Mathematical functions", "Vec", comm, PFDestroy, PFView));
914c8fdceaSLisandro Dalcin   newpf->data          = NULL;
924c8fdceaSLisandro Dalcin   newpf->ops->destroy  = NULL;
934c8fdceaSLisandro Dalcin   newpf->ops->apply    = NULL;
944c8fdceaSLisandro Dalcin   newpf->ops->applyvec = NULL;
954c8fdceaSLisandro Dalcin   newpf->ops->view     = NULL;
96292f8084SBarry Smith   newpf->dimin         = dimin;
97292f8084SBarry Smith   newpf->dimout        = dimout;
98292f8084SBarry Smith 
99292f8084SBarry Smith   *pf = newpf;
100292f8084SBarry Smith   PetscFunctionReturn(0);
101292f8084SBarry Smith }
102292f8084SBarry Smith 
103292f8084SBarry Smith /* -------------------------------------------------------------------------------*/
104292f8084SBarry Smith 
105292f8084SBarry Smith /*@
106292f8084SBarry Smith    PFApplyVec - Applies the mathematical function to a vector
107292f8084SBarry Smith 
108292f8084SBarry Smith    Collective on PF
109292f8084SBarry Smith 
110292f8084SBarry Smith    Input Parameters:
111292f8084SBarry Smith +  pf - the function context
1120298fd71SBarry Smith -  x - input vector (or NULL for the vector (0,1, .... N-1)
113292f8084SBarry Smith 
114292f8084SBarry Smith    Output Parameter:
115292f8084SBarry Smith .  y - output vector
116292f8084SBarry Smith 
117292f8084SBarry Smith    Level: beginner
118292f8084SBarry Smith 
119db781477SPatrick Sanan .seealso: `PFApply()`, `PFCreate()`, `PFDestroy()`, `PFSetType()`, `PFSet()`
120292f8084SBarry Smith @*/
1219371c9d4SSatish Balay PetscErrorCode PFApplyVec(PF pf, Vec x, Vec y) {
122292f8084SBarry Smith   PetscInt  i, rstart, rend, n, p;
123ace3abfcSBarry Smith   PetscBool nox = PETSC_FALSE;
124292f8084SBarry Smith 
125292f8084SBarry Smith   PetscFunctionBegin;
1260700a824SBarry Smith   PetscValidHeaderSpecific(pf, PF_CLASSID, 1);
1270700a824SBarry Smith   PetscValidHeaderSpecific(y, VEC_CLASSID, 3);
128292f8084SBarry Smith   if (x) {
1290700a824SBarry Smith     PetscValidHeaderSpecific(x, VEC_CLASSID, 2);
13008401ef6SPierre Jolivet     PetscCheck(x != y, PETSC_COMM_SELF, PETSC_ERR_ARG_IDN, "x and y must be different vectors");
131292f8084SBarry Smith   } else {
132292f8084SBarry Smith     PetscScalar *xx;
13303193ff8SBarry Smith     PetscInt     lsize;
134292f8084SBarry Smith 
1359566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(y, &lsize));
13603193ff8SBarry Smith     lsize = pf->dimin * lsize / pf->dimout;
1379566063dSJacob Faibussowitsch     PetscCall(VecCreateMPI(PetscObjectComm((PetscObject)y), lsize, PETSC_DETERMINE, &x));
138292f8084SBarry Smith     nox = PETSC_TRUE;
1399566063dSJacob Faibussowitsch     PetscCall(VecGetOwnershipRange(x, &rstart, &rend));
1409566063dSJacob Faibussowitsch     PetscCall(VecGetArray(x, &xx));
141f6e5521dSKarl Rupp     for (i = rstart; i < rend; i++) xx[i - rstart] = (PetscScalar)i;
1429566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(x, &xx));
143292f8084SBarry Smith   }
144292f8084SBarry Smith 
1459566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(x, &n));
1469566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(y, &p));
147c9cc58a2SBarry Smith   PetscCheck((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);
148c9cc58a2SBarry Smith   PetscCheck((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);
14908401ef6SPierre 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);
150292f8084SBarry Smith 
151dbbe0bcdSBarry Smith   if (pf->ops->applyvec) PetscCall((*pf->ops->applyvec)(pf->data, x, y));
152dbbe0bcdSBarry Smith   else {
153292f8084SBarry Smith     PetscScalar *xx, *yy;
154292f8084SBarry Smith 
1559566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x, &n));
156292f8084SBarry Smith     n = n / pf->dimin;
1579566063dSJacob Faibussowitsch     PetscCall(VecGetArray(x, &xx));
1589566063dSJacob Faibussowitsch     PetscCall(VecGetArray(y, &yy));
1599566063dSJacob Faibussowitsch     PetscCall((*pf->ops->apply)(pf->data, n, xx, yy));
1609566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(x, &xx));
1619566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(y, &yy));
162292f8084SBarry Smith   }
163*48a46eb9SPierre Jolivet   if (nox) PetscCall(VecDestroy(&x));
164292f8084SBarry Smith   PetscFunctionReturn(0);
165292f8084SBarry Smith }
166292f8084SBarry Smith 
167292f8084SBarry Smith /*@
168292f8084SBarry Smith    PFApply - Applies the mathematical function to an array of values.
169292f8084SBarry Smith 
170292f8084SBarry Smith    Collective on PF
171292f8084SBarry Smith 
172292f8084SBarry Smith    Input Parameters:
173292f8084SBarry Smith +  pf - the function context
174292f8084SBarry Smith .  n - number of pointwise function evaluations to perform, each pointwise function evaluation
175292f8084SBarry Smith        is a function of dimin variables and computes dimout variables where dimin and dimout are defined
176292f8084SBarry Smith        in the call to PFCreate()
177292f8084SBarry Smith -  x - input array
178292f8084SBarry Smith 
179292f8084SBarry Smith    Output Parameter:
180292f8084SBarry Smith .  y - output array
181292f8084SBarry Smith 
182292f8084SBarry Smith    Level: beginner
183292f8084SBarry Smith 
184292f8084SBarry Smith    Notes:
185292f8084SBarry Smith 
186db781477SPatrick Sanan .seealso: `PFApplyVec()`, `PFCreate()`, `PFDestroy()`, `PFSetType()`, `PFSet()`
187292f8084SBarry Smith @*/
1889371c9d4SSatish Balay PetscErrorCode PFApply(PF pf, PetscInt n, const PetscScalar *x, PetscScalar *y) {
189292f8084SBarry Smith   PetscFunctionBegin;
1900700a824SBarry Smith   PetscValidHeaderSpecific(pf, PF_CLASSID, 1);
191064a246eSJacob Faibussowitsch   PetscValidScalarPointer(x, 3);
192064a246eSJacob Faibussowitsch   PetscValidScalarPointer(y, 4);
19308401ef6SPierre Jolivet   PetscCheck(x != y, PETSC_COMM_SELF, PETSC_ERR_ARG_IDN, "x and y must be different arrays");
194292f8084SBarry Smith 
1959566063dSJacob Faibussowitsch   PetscCall((*pf->ops->apply)(pf->data, n, x, y));
196292f8084SBarry Smith   PetscFunctionReturn(0);
197292f8084SBarry Smith }
198292f8084SBarry Smith 
199fe2efc57SMark /*@C
200fe2efc57SMark    PFViewFromOptions - View from Options
201fe2efc57SMark 
202fe2efc57SMark    Collective on PF
203fe2efc57SMark 
204fe2efc57SMark    Input Parameters:
205fe2efc57SMark +  A - the PF context
206736c3998SJose E. Roman .  obj - Optional object
207736c3998SJose E. Roman -  name - command line option
208fe2efc57SMark 
209fe2efc57SMark    Level: intermediate
210db781477SPatrick Sanan .seealso: `PF`, `PFView`, `PetscObjectViewFromOptions()`, `PFCreate()`
211fe2efc57SMark @*/
2129371c9d4SSatish Balay PetscErrorCode PFViewFromOptions(PF A, PetscObject obj, const char name[]) {
213fe2efc57SMark   PetscFunctionBegin;
214fe2efc57SMark   PetscValidHeaderSpecific(A, PF_CLASSID, 1);
2159566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
216fe2efc57SMark   PetscFunctionReturn(0);
217fe2efc57SMark }
218fe2efc57SMark 
219292f8084SBarry Smith /*@
220292f8084SBarry Smith    PFView - Prints information about a mathematical function
221292f8084SBarry Smith 
222292f8084SBarry Smith    Collective on PF unless PetscViewer is PETSC_VIEWER_STDOUT_SELF
223292f8084SBarry Smith 
224292f8084SBarry Smith    Input Parameters:
225292f8084SBarry Smith +  PF - the PF context
226292f8084SBarry Smith -  viewer - optional visualization context
227292f8084SBarry Smith 
228292f8084SBarry Smith    Note:
229292f8084SBarry Smith    The available visualization contexts include
230292f8084SBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
231292f8084SBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
232292f8084SBarry Smith          output where only the first processor opens
233292f8084SBarry Smith          the file.  All other processors send their
234292f8084SBarry Smith          data to the first processor to print.
235292f8084SBarry Smith 
236292f8084SBarry Smith    The user can open an alternative visualization contexts with
237292f8084SBarry Smith    PetscViewerASCIIOpen() (output to a specified file).
238292f8084SBarry Smith 
239292f8084SBarry Smith    Level: developer
240292f8084SBarry Smith 
241db781477SPatrick Sanan .seealso: `PetscViewerCreate()`, `PetscViewerASCIIOpen()`
242292f8084SBarry Smith @*/
2439371c9d4SSatish Balay PetscErrorCode PFView(PF pf, PetscViewer viewer) {
244ace3abfcSBarry Smith   PetscBool         iascii;
245292f8084SBarry Smith   PetscViewerFormat format;
246292f8084SBarry Smith 
247292f8084SBarry Smith   PetscFunctionBegin;
2480700a824SBarry Smith   PetscValidHeaderSpecific(pf, PF_CLASSID, 1);
249*48a46eb9SPierre Jolivet   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)pf), &viewer));
2500700a824SBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
251292f8084SBarry Smith   PetscCheckSameComm(pf, 1, viewer, 2);
252292f8084SBarry Smith 
2539566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
254292f8084SBarry Smith   if (iascii) {
2559566063dSJacob Faibussowitsch     PetscCall(PetscViewerGetFormat(viewer, &format));
2569566063dSJacob Faibussowitsch     PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)pf, viewer));
257292f8084SBarry Smith     if (pf->ops->view) {
2589566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPushTab(viewer));
2599566063dSJacob Faibussowitsch       PetscCall((*pf->ops->view)(pf->data, viewer));
2609566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPopTab(viewer));
261292f8084SBarry Smith     }
262292f8084SBarry Smith   }
263292f8084SBarry Smith   PetscFunctionReturn(0);
264292f8084SBarry Smith }
265292f8084SBarry Smith 
266bdf89e91SBarry Smith /*@C
267bdf89e91SBarry Smith    PFRegister - Adds a method to the mathematical function package.
268292f8084SBarry Smith 
269292f8084SBarry Smith    Not collective
270292f8084SBarry Smith 
271292f8084SBarry Smith    Input Parameters:
272292f8084SBarry Smith +  name_solver - name of a new user-defined solver
273292f8084SBarry Smith -  routine_create - routine to create method context
274292f8084SBarry Smith 
275292f8084SBarry Smith    Notes:
2761c84c290SBarry Smith    PFRegister() may be called multiple times to add several user-defined functions
277292f8084SBarry Smith 
278292f8084SBarry Smith    Sample usage:
279292f8084SBarry Smith .vb
280bdf89e91SBarry Smith    PFRegister("my_function",MyFunctionSetCreate);
281292f8084SBarry Smith .ve
282292f8084SBarry Smith 
283292f8084SBarry Smith    Then, your solver can be chosen with the procedural interface via
284292f8084SBarry Smith $     PFSetType(pf,"my_function")
285292f8084SBarry Smith    or at runtime via the option
286292f8084SBarry Smith $     -pf_type my_function
287292f8084SBarry Smith 
288292f8084SBarry Smith    Level: advanced
289292f8084SBarry Smith 
290db781477SPatrick Sanan .seealso: `PFRegisterAll()`, `PFRegisterDestroy()`, `PFRegister()`
291bdf89e91SBarry Smith @*/
2929371c9d4SSatish Balay PetscErrorCode PFRegister(const char sname[], PetscErrorCode (*function)(PF, void *)) {
293292f8084SBarry Smith   PetscFunctionBegin;
2949566063dSJacob Faibussowitsch   PetscCall(PFInitializePackage());
2959566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&PFList, sname, function));
296292f8084SBarry Smith   PetscFunctionReturn(0);
297292f8084SBarry Smith }
298292f8084SBarry Smith 
299292f8084SBarry Smith /*@C
300292f8084SBarry Smith    PFGetType - Gets the PF method type and name (as a string) from the PF
301292f8084SBarry Smith    context.
302292f8084SBarry Smith 
303292f8084SBarry Smith    Not Collective
304292f8084SBarry Smith 
305292f8084SBarry Smith    Input Parameter:
306292f8084SBarry Smith .  pf - the function context
307292f8084SBarry Smith 
308292f8084SBarry Smith    Output Parameter:
309c4e43342SLisandro Dalcin .  type - name of function
310292f8084SBarry Smith 
311292f8084SBarry Smith    Level: intermediate
312292f8084SBarry Smith 
313db781477SPatrick Sanan .seealso: `PFSetType()`
314292f8084SBarry Smith 
315292f8084SBarry Smith @*/
3169371c9d4SSatish Balay PetscErrorCode PFGetType(PF pf, PFType *type) {
317292f8084SBarry Smith   PetscFunctionBegin;
3180700a824SBarry Smith   PetscValidHeaderSpecific(pf, PF_CLASSID, 1);
319c4e43342SLisandro Dalcin   PetscValidPointer(type, 2);
320c4e43342SLisandro Dalcin   *type = ((PetscObject)pf)->type_name;
321292f8084SBarry Smith   PetscFunctionReturn(0);
322292f8084SBarry Smith }
323292f8084SBarry Smith 
324292f8084SBarry Smith /*@C
325292f8084SBarry Smith    PFSetType - Builds PF for a particular function
326292f8084SBarry Smith 
327292f8084SBarry Smith    Collective on PF
328292f8084SBarry Smith 
329d8d19677SJose E. Roman    Input Parameters:
330292f8084SBarry Smith +  pf - the function context.
331292f8084SBarry Smith .  type - a known method
332292f8084SBarry Smith -  ctx - optional type dependent context
333292f8084SBarry Smith 
334292f8084SBarry Smith    Options Database Key:
335292f8084SBarry Smith .  -pf_type <type> - Sets PF type
336292f8084SBarry Smith 
337292f8084SBarry Smith   Notes:
338292f8084SBarry Smith   See "petsc/include/petscpf.h" for available methods (for instance,
339292f8084SBarry Smith   PFCONSTANT)
340292f8084SBarry Smith 
341292f8084SBarry Smith   Level: intermediate
342292f8084SBarry Smith 
343db781477SPatrick Sanan .seealso: `PFSet()`, `PFRegister()`, `PFCreate()`, `DMDACreatePF()`
344292f8084SBarry Smith 
345292f8084SBarry Smith @*/
3469371c9d4SSatish Balay PetscErrorCode PFSetType(PF pf, PFType type, void *ctx) {
347ace3abfcSBarry Smith   PetscBool match;
3485f80ce2aSJacob Faibussowitsch   PetscErrorCode (*r)(PF, void *);
349292f8084SBarry Smith 
350292f8084SBarry Smith   PetscFunctionBegin;
3510700a824SBarry Smith   PetscValidHeaderSpecific(pf, PF_CLASSID, 1);
352292f8084SBarry Smith   PetscValidCharPointer(type, 2);
353292f8084SBarry Smith 
3549566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)pf, type, &match));
355292f8084SBarry Smith   if (match) PetscFunctionReturn(0);
356292f8084SBarry Smith 
357dbbe0bcdSBarry Smith   PetscTryTypeMethod(pf, destroy);
3584c8fdceaSLisandro Dalcin   pf->data = NULL;
359292f8084SBarry Smith 
360292f8084SBarry Smith   /* Determine the PFCreateXXX routine for a particular function */
3619566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(PFList, type, &r));
3625f80ce2aSJacob Faibussowitsch   PetscCheck(r, PETSC_COMM_SELF, PETSC_ERR_ARG_UNKNOWN_TYPE, "Unable to find requested PF type %s", type);
3634c8fdceaSLisandro Dalcin   pf->ops->destroy  = NULL;
3644c8fdceaSLisandro Dalcin   pf->ops->view     = NULL;
3654c8fdceaSLisandro Dalcin   pf->ops->apply    = NULL;
3664c8fdceaSLisandro Dalcin   pf->ops->applyvec = NULL;
367292f8084SBarry Smith 
368292f8084SBarry Smith   /* Call the PFCreateXXX routine for this particular function */
3699566063dSJacob Faibussowitsch   PetscCall((*r)(pf, ctx));
370292f8084SBarry Smith 
3719566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)pf, type));
372292f8084SBarry Smith   PetscFunctionReturn(0);
373292f8084SBarry Smith }
374292f8084SBarry Smith 
375292f8084SBarry Smith /*@
376292f8084SBarry Smith    PFSetFromOptions - Sets PF options from the options database.
377292f8084SBarry Smith 
378292f8084SBarry Smith    Collective on PF
379292f8084SBarry Smith 
380292f8084SBarry Smith    Input Parameters:
381292f8084SBarry Smith .  pf - the mathematical function context
382292f8084SBarry Smith 
383292f8084SBarry Smith    Options Database Keys:
384292f8084SBarry Smith 
385292f8084SBarry Smith    Notes:
386292f8084SBarry Smith    To see all options, run your program with the -help option
387292f8084SBarry Smith    or consult the users manual.
388292f8084SBarry Smith 
389292f8084SBarry Smith    Level: intermediate
390292f8084SBarry Smith 
391292f8084SBarry Smith .seealso:
392292f8084SBarry Smith @*/
3939371c9d4SSatish Balay PetscErrorCode PFSetFromOptions(PF pf) {
394292f8084SBarry Smith   char      type[256];
395ace3abfcSBarry Smith   PetscBool flg;
396292f8084SBarry Smith 
397292f8084SBarry Smith   PetscFunctionBegin;
3980700a824SBarry Smith   PetscValidHeaderSpecific(pf, PF_CLASSID, 1);
399292f8084SBarry Smith 
400d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)pf);
4019566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-pf_type", "Type of function", "PFSetType", PFList, NULL, type, 256, &flg));
4021baa6e33SBarry Smith   if (flg) PetscCall(PFSetType(pf, type, NULL));
403dbbe0bcdSBarry Smith   PetscTryTypeMethod(pf, setfromoptions, PetscOptionsObject);
4045d973c19SBarry Smith 
4055d973c19SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
406dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)pf, PetscOptionsObject));
407d0609cedSBarry Smith   PetscOptionsEnd();
408292f8084SBarry Smith   PetscFunctionReturn(0);
409292f8084SBarry Smith }
410292f8084SBarry Smith 
411ace3abfcSBarry Smith static PetscBool PFPackageInitialized = PETSC_FALSE;
412b022a5c1SBarry Smith /*@C
413b022a5c1SBarry Smith   PFFinalizePackage - This function destroys everything in the Petsc interface to Mathematica. It is
414b022a5c1SBarry Smith   called from PetscFinalize().
415b022a5c1SBarry Smith 
416b022a5c1SBarry Smith   Level: developer
417b022a5c1SBarry Smith 
418db781477SPatrick Sanan .seealso: `PetscFinalize()`
419b022a5c1SBarry Smith @*/
4209371c9d4SSatish Balay PetscErrorCode   PFFinalizePackage(void) {
421b022a5c1SBarry Smith     PetscFunctionBegin;
4229566063dSJacob Faibussowitsch     PetscCall(PetscFunctionListDestroy(&PFList));
423b022a5c1SBarry Smith     PFPackageInitialized = PETSC_FALSE;
424b022a5c1SBarry Smith     PFRegisterAllCalled  = PETSC_FALSE;
425b022a5c1SBarry Smith     PetscFunctionReturn(0);
426b022a5c1SBarry Smith }
427b022a5c1SBarry Smith 
4289877f0dbSBarry Smith /*@C
4299877f0dbSBarry Smith   PFInitializePackage - This function initializes everything in the PF package. It is called
4308a690491SBarry Smith   from PetscDLLibraryRegister_petscvec() when using dynamic libraries, and on the first call to PFCreate()
4318a690491SBarry Smith   when using shared or static libraries.
4329877f0dbSBarry Smith 
4339877f0dbSBarry Smith   Level: developer
4349877f0dbSBarry Smith 
435db781477SPatrick Sanan .seealso: `PetscInitialize()`
4369877f0dbSBarry Smith @*/
4379371c9d4SSatish Balay PetscErrorCode PFInitializePackage(void) {
4389877f0dbSBarry Smith   char      logList[256];
4398e81d068SLisandro Dalcin   PetscBool opt, pkg;
4409877f0dbSBarry Smith 
4419877f0dbSBarry Smith   PetscFunctionBegin;
442b022a5c1SBarry Smith   if (PFPackageInitialized) PetscFunctionReturn(0);
443b022a5c1SBarry Smith   PFPackageInitialized = PETSC_TRUE;
4449877f0dbSBarry Smith   /* Register Classes */
4459566063dSJacob Faibussowitsch   PetscCall(PetscClassIdRegister("PointFunction", &PF_CLASSID));
4469877f0dbSBarry Smith   /* Register Constructors */
4479566063dSJacob Faibussowitsch   PetscCall(PFRegisterAll());
448e94e781bSJacob Faibussowitsch   /* Process Info */
449e94e781bSJacob Faibussowitsch   {
450e94e781bSJacob Faibussowitsch     PetscClassId classids[1];
451e94e781bSJacob Faibussowitsch 
452e94e781bSJacob Faibussowitsch     classids[0] = PF_CLASSID;
4539566063dSJacob Faibussowitsch     PetscCall(PetscInfoProcessClass("pf", 1, classids));
4549877f0dbSBarry Smith   }
4559877f0dbSBarry Smith   /* Process summary exclusions */
4569566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetString(NULL, NULL, "-log_exclude", logList, sizeof(logList), &opt));
4579877f0dbSBarry Smith   if (opt) {
4589566063dSJacob Faibussowitsch     PetscCall(PetscStrInList("pf", logList, ',', &pkg));
4599566063dSJacob Faibussowitsch     if (pkg) PetscCall(PetscLogEventExcludeClass(PF_CLASSID));
4609877f0dbSBarry Smith   }
4618e81d068SLisandro Dalcin   /* Register package finalizer */
4629566063dSJacob Faibussowitsch   PetscCall(PetscRegisterFinalize(PFFinalizePackage));
4639877f0dbSBarry Smith   PetscFunctionReturn(0);
4649877f0dbSBarry Smith }
465