xref: /petsc/src/vec/pf/interface/pf.c (revision 20f4b53cbb5e9bd9ef12b76a8697d60d197cda17)
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 
13c3339decSBarry Smith    Collective
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 
25*20f4b53cSBarry Smith .seealso: `PF`, `PFCreate()`, `PFDestroy()`, `PFSetType()`, `PFApply()`, `PFApplyVec()`
26292f8084SBarry Smith @*/
27d71ae5a4SJacob Faibussowitsch 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)
28d71ae5a4SJacob Faibussowitsch {
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;
363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
37292f8084SBarry Smith }
38292f8084SBarry Smith 
39292f8084SBarry Smith /*@C
40cd05f99aSJacob Faibussowitsch    PFDestroy - Destroys `PF` context that was created with `PFCreate()`.
41292f8084SBarry Smith 
42c3339decSBarry Smith    Collective
43292f8084SBarry Smith 
44292f8084SBarry Smith    Input Parameter:
45292f8084SBarry Smith .  pf - the function context
46292f8084SBarry Smith 
47292f8084SBarry Smith    Level: beginner
48292f8084SBarry Smith 
49*20f4b53cSBarry Smith .seealso: `PF`, `PFCreate()`, `PFSet()`, `PFSetType()`
50292f8084SBarry Smith @*/
51d71ae5a4SJacob Faibussowitsch PetscErrorCode PFDestroy(PF *pf)
52d71ae5a4SJacob Faibussowitsch {
53292f8084SBarry Smith   PetscFunctionBegin;
543ba16761SJacob Faibussowitsch   if (!*pf) PetscFunctionReturn(PETSC_SUCCESS);
556bf464f9SBarry Smith   PetscValidHeaderSpecific((*pf), PF_CLASSID, 1);
563ba16761SJacob Faibussowitsch   if (--((PetscObject)(*pf))->refct > 0) PetscFunctionReturn(PETSC_SUCCESS);
57292f8084SBarry Smith 
589566063dSJacob Faibussowitsch   PetscCall(PFViewFromOptions(*pf, NULL, "-pf_view"));
59e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
609566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*pf));
61292f8084SBarry Smith 
629566063dSJacob Faibussowitsch   if ((*pf)->ops->destroy) PetscCall((*(*pf)->ops->destroy)((*pf)->data));
639566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(pf));
643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
65292f8084SBarry Smith }
66292f8084SBarry Smith 
67292f8084SBarry Smith /*@C
68292f8084SBarry Smith    PFCreate - Creates a mathematical function context.
69292f8084SBarry Smith 
70d083f849SBarry Smith    Collective
71292f8084SBarry Smith 
72d8d19677SJose E. Roman    Input Parameters:
73292f8084SBarry Smith +  comm - MPI communicator
74292f8084SBarry Smith .  dimin - dimension of the space you are mapping from
75292f8084SBarry Smith -  dimout - dimension of the space you are mapping to
76292f8084SBarry Smith 
77292f8084SBarry Smith    Output Parameter:
78292f8084SBarry Smith .  pf - the function context
79292f8084SBarry Smith 
80292f8084SBarry Smith    Level: developer
81292f8084SBarry Smith 
82*20f4b53cSBarry Smith .seealso: `PF`, `PFSet()`, `PFApply()`, `PFDestroy()`, `PFApplyVec()`
83292f8084SBarry Smith @*/
84d71ae5a4SJacob Faibussowitsch PetscErrorCode PFCreate(MPI_Comm comm, PetscInt dimin, PetscInt dimout, PF *pf)
85d71ae5a4SJacob Faibussowitsch {
86292f8084SBarry Smith   PF newpf;
87292f8084SBarry Smith 
88292f8084SBarry Smith   PetscFunctionBegin;
89064a246eSJacob Faibussowitsch   PetscValidPointer(pf, 4);
900298fd71SBarry Smith   *pf = NULL;
919566063dSJacob Faibussowitsch   PetscCall(PFInitializePackage());
92292f8084SBarry Smith 
939566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(newpf, PF_CLASSID, "PF", "Mathematical functions", "Vec", comm, PFDestroy, PFView));
944c8fdceaSLisandro Dalcin   newpf->data          = NULL;
954c8fdceaSLisandro Dalcin   newpf->ops->destroy  = NULL;
964c8fdceaSLisandro Dalcin   newpf->ops->apply    = NULL;
974c8fdceaSLisandro Dalcin   newpf->ops->applyvec = NULL;
984c8fdceaSLisandro Dalcin   newpf->ops->view     = NULL;
99292f8084SBarry Smith   newpf->dimin         = dimin;
100292f8084SBarry Smith   newpf->dimout        = dimout;
101292f8084SBarry Smith 
102292f8084SBarry Smith   *pf = newpf;
1033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
104292f8084SBarry Smith }
105292f8084SBarry Smith 
106292f8084SBarry Smith /* -------------------------------------------------------------------------------*/
107292f8084SBarry Smith 
108292f8084SBarry Smith /*@
109292f8084SBarry Smith    PFApplyVec - Applies the mathematical function to a vector
110292f8084SBarry Smith 
111c3339decSBarry Smith    Collective
112292f8084SBarry Smith 
113292f8084SBarry Smith    Input Parameters:
114292f8084SBarry Smith +  pf - the function context
1150298fd71SBarry Smith -  x - input vector (or NULL for the vector (0,1, .... N-1)
116292f8084SBarry Smith 
117292f8084SBarry Smith    Output Parameter:
118292f8084SBarry Smith .  y - output vector
119292f8084SBarry Smith 
120292f8084SBarry Smith    Level: beginner
121292f8084SBarry Smith 
122*20f4b53cSBarry Smith .seealso: `PF`, `PFApply()`, `PFCreate()`, `PFDestroy()`, `PFSetType()`, `PFSet()`
123292f8084SBarry Smith @*/
124d71ae5a4SJacob Faibussowitsch PetscErrorCode PFApplyVec(PF pf, Vec x, Vec y)
125d71ae5a4SJacob Faibussowitsch {
126292f8084SBarry Smith   PetscInt  i, rstart, rend, n, p;
127ace3abfcSBarry Smith   PetscBool nox = PETSC_FALSE;
128292f8084SBarry Smith 
129292f8084SBarry Smith   PetscFunctionBegin;
1300700a824SBarry Smith   PetscValidHeaderSpecific(pf, PF_CLASSID, 1);
1310700a824SBarry Smith   PetscValidHeaderSpecific(y, VEC_CLASSID, 3);
132292f8084SBarry Smith   if (x) {
1330700a824SBarry Smith     PetscValidHeaderSpecific(x, VEC_CLASSID, 2);
13408401ef6SPierre Jolivet     PetscCheck(x != y, PETSC_COMM_SELF, PETSC_ERR_ARG_IDN, "x and y must be different vectors");
135292f8084SBarry Smith   } else {
136292f8084SBarry Smith     PetscScalar *xx;
13703193ff8SBarry Smith     PetscInt     lsize;
138292f8084SBarry Smith 
1399566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(y, &lsize));
14003193ff8SBarry Smith     lsize = pf->dimin * lsize / pf->dimout;
1419566063dSJacob Faibussowitsch     PetscCall(VecCreateMPI(PetscObjectComm((PetscObject)y), lsize, PETSC_DETERMINE, &x));
142292f8084SBarry Smith     nox = PETSC_TRUE;
1439566063dSJacob Faibussowitsch     PetscCall(VecGetOwnershipRange(x, &rstart, &rend));
1449566063dSJacob Faibussowitsch     PetscCall(VecGetArray(x, &xx));
145f6e5521dSKarl Rupp     for (i = rstart; i < rend; i++) xx[i - rstart] = (PetscScalar)i;
1469566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(x, &xx));
147292f8084SBarry Smith   }
148292f8084SBarry Smith 
1499566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(x, &n));
1509566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(y, &p));
151c9cc58a2SBarry 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);
152c9cc58a2SBarry 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);
15308401ef6SPierre 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);
154292f8084SBarry Smith 
155dbbe0bcdSBarry Smith   if (pf->ops->applyvec) PetscCall((*pf->ops->applyvec)(pf->data, x, y));
156dbbe0bcdSBarry Smith   else {
157292f8084SBarry Smith     PetscScalar *xx, *yy;
158292f8084SBarry Smith 
1599566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x, &n));
160292f8084SBarry Smith     n = n / pf->dimin;
1619566063dSJacob Faibussowitsch     PetscCall(VecGetArray(x, &xx));
1629566063dSJacob Faibussowitsch     PetscCall(VecGetArray(y, &yy));
1639566063dSJacob Faibussowitsch     PetscCall((*pf->ops->apply)(pf->data, n, xx, yy));
1649566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(x, &xx));
1659566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(y, &yy));
166292f8084SBarry Smith   }
16748a46eb9SPierre Jolivet   if (nox) PetscCall(VecDestroy(&x));
1683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
169292f8084SBarry Smith }
170292f8084SBarry Smith 
171292f8084SBarry Smith /*@
172292f8084SBarry Smith    PFApply - Applies the mathematical function to an array of values.
173292f8084SBarry Smith 
174c3339decSBarry Smith    Collective
175292f8084SBarry Smith 
176292f8084SBarry Smith    Input Parameters:
177292f8084SBarry Smith +  pf - the function context
178292f8084SBarry Smith .  n - number of pointwise function evaluations to perform, each pointwise function evaluation
179292f8084SBarry Smith        is a function of dimin variables and computes dimout variables where dimin and dimout are defined
180292f8084SBarry Smith        in the call to PFCreate()
181292f8084SBarry Smith -  x - input array
182292f8084SBarry Smith 
183292f8084SBarry Smith    Output Parameter:
184292f8084SBarry Smith .  y - output array
185292f8084SBarry Smith 
186292f8084SBarry Smith    Level: beginner
187292f8084SBarry Smith 
188292f8084SBarry Smith    Notes:
189292f8084SBarry Smith 
190*20f4b53cSBarry Smith .seealso: `PF`, `PFApplyVec()`, `PFCreate()`, `PFDestroy()`, `PFSetType()`, `PFSet()`
191292f8084SBarry Smith @*/
192d71ae5a4SJacob Faibussowitsch PetscErrorCode PFApply(PF pf, PetscInt n, const PetscScalar *x, PetscScalar *y)
193d71ae5a4SJacob Faibussowitsch {
194292f8084SBarry Smith   PetscFunctionBegin;
1950700a824SBarry Smith   PetscValidHeaderSpecific(pf, PF_CLASSID, 1);
196064a246eSJacob Faibussowitsch   PetscValidScalarPointer(x, 3);
197064a246eSJacob Faibussowitsch   PetscValidScalarPointer(y, 4);
19808401ef6SPierre Jolivet   PetscCheck(x != y, PETSC_COMM_SELF, PETSC_ERR_ARG_IDN, "x and y must be different arrays");
199292f8084SBarry Smith 
2009566063dSJacob Faibussowitsch   PetscCall((*pf->ops->apply)(pf->data, n, x, y));
2013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
202292f8084SBarry Smith }
203292f8084SBarry Smith 
204fe2efc57SMark /*@C
205*20f4b53cSBarry Smith    PFViewFromOptions - View a `PF` based on options set in the options database
206fe2efc57SMark 
207c3339decSBarry Smith    Collective
208fe2efc57SMark 
209fe2efc57SMark    Input Parameters:
210*20f4b53cSBarry Smith +  A - the `PF` context
211*20f4b53cSBarry Smith .  obj - Optional object that provides the prefix used to search the options database
212736c3998SJose E. Roman -  name - command line option
213fe2efc57SMark 
214fe2efc57SMark    Level: intermediate
215*20f4b53cSBarry Smith 
216*20f4b53cSBarry Smith    Note:
217*20f4b53cSBarry Smith   See `PetscObjectViewFromOptions()` for the variety of viewer options available
218*20f4b53cSBarry Smith 
219db781477SPatrick Sanan .seealso: `PF`, `PFView`, `PetscObjectViewFromOptions()`, `PFCreate()`
220fe2efc57SMark @*/
221d71ae5a4SJacob Faibussowitsch PetscErrorCode PFViewFromOptions(PF A, PetscObject obj, const char name[])
222d71ae5a4SJacob Faibussowitsch {
223fe2efc57SMark   PetscFunctionBegin;
224fe2efc57SMark   PetscValidHeaderSpecific(A, PF_CLASSID, 1);
2259566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
2263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
227fe2efc57SMark }
228fe2efc57SMark 
229292f8084SBarry Smith /*@
230292f8084SBarry Smith    PFView - Prints information about a mathematical function
231292f8084SBarry Smith 
232*20f4b53cSBarry Smith    Collective unless `viewer` is `PETSC_VIEWER_STDOUT_SELF`
233292f8084SBarry Smith 
234292f8084SBarry Smith    Input Parameters:
235*20f4b53cSBarry Smith +  PF - the `PF` context
236292f8084SBarry Smith -  viewer - optional visualization context
237292f8084SBarry Smith 
238*20f4b53cSBarry Smith    Level: developer
239*20f4b53cSBarry Smith 
240292f8084SBarry Smith    Note:
241292f8084SBarry Smith    The available visualization contexts include
242*20f4b53cSBarry Smith +     `PETSC_VIEWER_STDOUT_SELF` - standard output (default)
243*20f4b53cSBarry Smith -     `PETSC_VIEWER_STDOUT_WORLD` - synchronized standard
244292f8084SBarry Smith          output where only the first processor opens
245292f8084SBarry Smith          the file.  All other processors send their
246292f8084SBarry Smith          data to the first processor to print.
247292f8084SBarry Smith 
248292f8084SBarry Smith    The user can open an alternative visualization contexts with
249*20f4b53cSBarry Smith    `PetscViewerASCIIOpen()` (output to a specified file).
250292f8084SBarry Smith 
251*20f4b53cSBarry Smith .seealso: `PF`, `PetscViewerCreate()`, `PetscViewerASCIIOpen()`
252292f8084SBarry Smith @*/
253d71ae5a4SJacob Faibussowitsch PetscErrorCode PFView(PF pf, PetscViewer viewer)
254d71ae5a4SJacob Faibussowitsch {
255ace3abfcSBarry Smith   PetscBool         iascii;
256292f8084SBarry Smith   PetscViewerFormat format;
257292f8084SBarry Smith 
258292f8084SBarry Smith   PetscFunctionBegin;
2590700a824SBarry Smith   PetscValidHeaderSpecific(pf, PF_CLASSID, 1);
26048a46eb9SPierre Jolivet   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)pf), &viewer));
2610700a824SBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
262292f8084SBarry Smith   PetscCheckSameComm(pf, 1, viewer, 2);
263292f8084SBarry Smith 
2649566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
265292f8084SBarry Smith   if (iascii) {
2669566063dSJacob Faibussowitsch     PetscCall(PetscViewerGetFormat(viewer, &format));
2679566063dSJacob Faibussowitsch     PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)pf, viewer));
268292f8084SBarry Smith     if (pf->ops->view) {
2699566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPushTab(viewer));
2709566063dSJacob Faibussowitsch       PetscCall((*pf->ops->view)(pf->data, viewer));
2719566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPopTab(viewer));
272292f8084SBarry Smith     }
273292f8084SBarry Smith   }
2743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
275292f8084SBarry Smith }
276292f8084SBarry Smith 
277bdf89e91SBarry Smith /*@C
278bdf89e91SBarry Smith    PFRegister - Adds a method to the mathematical function package.
279292f8084SBarry Smith 
280*20f4b53cSBarry Smith    Not Collective
281292f8084SBarry Smith 
282292f8084SBarry Smith    Input Parameters:
283*20f4b53cSBarry Smith +  sname - name of a new user-defined solver
284*20f4b53cSBarry Smith -  function - routine to create method context
285292f8084SBarry Smith 
286292f8084SBarry Smith    Sample usage:
287292f8084SBarry Smith .vb
288bdf89e91SBarry Smith    PFRegister("my_function",MyFunctionSetCreate);
289292f8084SBarry Smith .ve
290292f8084SBarry Smith 
291292f8084SBarry Smith    Then, your solver can be chosen with the procedural interface via
292292f8084SBarry Smith $     PFSetType(pf,"my_function")
293292f8084SBarry Smith    or at runtime via the option
294292f8084SBarry Smith $     -pf_type my_function
295292f8084SBarry Smith 
296292f8084SBarry Smith    Level: advanced
297292f8084SBarry Smith 
298*20f4b53cSBarry Smith    Note:
299*20f4b53cSBarry Smith    `PFRegister()` may be called multiple times to add several user-defined functions
300*20f4b53cSBarry Smith 
301*20f4b53cSBarry Smith .seealso: `PF`, `PFRegisterAll()`, `PFRegisterDestroy()`, `PFRegister()`
302bdf89e91SBarry Smith @*/
303d71ae5a4SJacob Faibussowitsch PetscErrorCode PFRegister(const char sname[], PetscErrorCode (*function)(PF, void *))
304d71ae5a4SJacob Faibussowitsch {
305292f8084SBarry Smith   PetscFunctionBegin;
3069566063dSJacob Faibussowitsch   PetscCall(PFInitializePackage());
3079566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&PFList, sname, function));
3083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
309292f8084SBarry Smith }
310292f8084SBarry Smith 
311292f8084SBarry Smith /*@C
312*20f4b53cSBarry Smith    PFGetType - Gets the `PFType` name (as a string) from the `PF`
313292f8084SBarry Smith    context.
314292f8084SBarry Smith 
315292f8084SBarry Smith    Not Collective
316292f8084SBarry Smith 
317292f8084SBarry Smith    Input Parameter:
318292f8084SBarry Smith .  pf - the function context
319292f8084SBarry Smith 
320292f8084SBarry Smith    Output Parameter:
321c4e43342SLisandro Dalcin .  type - name of function
322292f8084SBarry Smith 
323292f8084SBarry Smith    Level: intermediate
324292f8084SBarry Smith 
325*20f4b53cSBarry Smith .seealso: `PF`, `PFSetType()`
326292f8084SBarry Smith @*/
327d71ae5a4SJacob Faibussowitsch PetscErrorCode PFGetType(PF pf, PFType *type)
328d71ae5a4SJacob Faibussowitsch {
329292f8084SBarry Smith   PetscFunctionBegin;
3300700a824SBarry Smith   PetscValidHeaderSpecific(pf, PF_CLASSID, 1);
331c4e43342SLisandro Dalcin   PetscValidPointer(type, 2);
332c4e43342SLisandro Dalcin   *type = ((PetscObject)pf)->type_name;
3333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
334292f8084SBarry Smith }
335292f8084SBarry Smith 
336292f8084SBarry Smith /*@C
337*20f4b53cSBarry Smith    PFSetType - Builds `PF` for a particular function
338292f8084SBarry Smith 
339c3339decSBarry Smith    Collective
340292f8084SBarry Smith 
341d8d19677SJose E. Roman    Input Parameters:
342292f8084SBarry Smith +  pf - the function context.
343292f8084SBarry Smith .  type - a known method
344292f8084SBarry Smith -  ctx - optional type dependent context
345292f8084SBarry Smith 
346292f8084SBarry Smith    Options Database Key:
347292f8084SBarry Smith .  -pf_type <type> - Sets PF type
348292f8084SBarry Smith 
349292f8084SBarry Smith   Level: intermediate
350292f8084SBarry Smith 
351*20f4b53cSBarry Smith   Note:
352*20f4b53cSBarry Smith   See "petsc/include/petscpf.h" for available methods (for instance, `PFCONSTANT`)
353292f8084SBarry Smith 
354*20f4b53cSBarry Smith .seealso: `PF`, `PFSet()`, `PFRegister()`, `PFCreate()`, `DMDACreatePF()`
355292f8084SBarry Smith @*/
356d71ae5a4SJacob Faibussowitsch PetscErrorCode PFSetType(PF pf, PFType type, void *ctx)
357d71ae5a4SJacob Faibussowitsch {
358ace3abfcSBarry Smith   PetscBool match;
3595f80ce2aSJacob Faibussowitsch   PetscErrorCode (*r)(PF, void *);
360292f8084SBarry Smith 
361292f8084SBarry Smith   PetscFunctionBegin;
3620700a824SBarry Smith   PetscValidHeaderSpecific(pf, PF_CLASSID, 1);
363292f8084SBarry Smith   PetscValidCharPointer(type, 2);
364292f8084SBarry Smith 
3659566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)pf, type, &match));
3663ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
367292f8084SBarry Smith 
368dbbe0bcdSBarry Smith   PetscTryTypeMethod(pf, destroy);
3694c8fdceaSLisandro Dalcin   pf->data = NULL;
370292f8084SBarry Smith 
371292f8084SBarry Smith   /* Determine the PFCreateXXX routine for a particular function */
3729566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(PFList, type, &r));
3735f80ce2aSJacob Faibussowitsch   PetscCheck(r, PETSC_COMM_SELF, PETSC_ERR_ARG_UNKNOWN_TYPE, "Unable to find requested PF type %s", type);
3744c8fdceaSLisandro Dalcin   pf->ops->destroy  = NULL;
3754c8fdceaSLisandro Dalcin   pf->ops->view     = NULL;
3764c8fdceaSLisandro Dalcin   pf->ops->apply    = NULL;
3774c8fdceaSLisandro Dalcin   pf->ops->applyvec = NULL;
378292f8084SBarry Smith 
379292f8084SBarry Smith   /* Call the PFCreateXXX routine for this particular function */
3809566063dSJacob Faibussowitsch   PetscCall((*r)(pf, ctx));
381292f8084SBarry Smith 
3829566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)pf, type));
3833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
384292f8084SBarry Smith }
385292f8084SBarry Smith 
386292f8084SBarry Smith /*@
387*20f4b53cSBarry Smith    PFSetFromOptions - Sets `PF` options from the options database.
388292f8084SBarry Smith 
389c3339decSBarry Smith    Collective
390292f8084SBarry Smith 
391292f8084SBarry Smith    Input Parameters:
392292f8084SBarry Smith .  pf - the mathematical function context
393292f8084SBarry Smith 
394*20f4b53cSBarry Smith    Level: intermediate
395292f8084SBarry Smith 
396292f8084SBarry Smith    Notes:
397292f8084SBarry Smith    To see all options, run your program with the -help option
398292f8084SBarry Smith    or consult the users manual.
399292f8084SBarry Smith 
400*20f4b53cSBarry Smith .seealso: `PF`
401292f8084SBarry Smith @*/
402d71ae5a4SJacob Faibussowitsch PetscErrorCode PFSetFromOptions(PF pf)
403d71ae5a4SJacob Faibussowitsch {
404292f8084SBarry Smith   char      type[256];
405ace3abfcSBarry Smith   PetscBool flg;
406292f8084SBarry Smith 
407292f8084SBarry Smith   PetscFunctionBegin;
4080700a824SBarry Smith   PetscValidHeaderSpecific(pf, PF_CLASSID, 1);
409292f8084SBarry Smith 
410d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)pf);
4119566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-pf_type", "Type of function", "PFSetType", PFList, NULL, type, 256, &flg));
4121baa6e33SBarry Smith   if (flg) PetscCall(PFSetType(pf, type, NULL));
413dbbe0bcdSBarry Smith   PetscTryTypeMethod(pf, setfromoptions, PetscOptionsObject);
4145d973c19SBarry Smith 
4155d973c19SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
416dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)pf, PetscOptionsObject));
417d0609cedSBarry Smith   PetscOptionsEnd();
4183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
419292f8084SBarry Smith }
420292f8084SBarry Smith 
421ace3abfcSBarry Smith static PetscBool PFPackageInitialized = PETSC_FALSE;
422b022a5c1SBarry Smith /*@C
423*20f4b53cSBarry Smith   PFFinalizePackage - This function destroys everything in the PETSc `PF` package. It is
424*20f4b53cSBarry Smith   called from `PetscFinalize()`.
425b022a5c1SBarry Smith 
426b022a5c1SBarry Smith   Level: developer
427b022a5c1SBarry Smith 
428*20f4b53cSBarry Smith .seealso: `PF`, `PetscFinalize()`
429b022a5c1SBarry Smith @*/
430d71ae5a4SJacob Faibussowitsch PetscErrorCode PFFinalizePackage(void)
431d71ae5a4SJacob Faibussowitsch {
432b022a5c1SBarry Smith   PetscFunctionBegin;
4339566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListDestroy(&PFList));
434b022a5c1SBarry Smith   PFPackageInitialized = PETSC_FALSE;
435b022a5c1SBarry Smith   PFRegisterAllCalled  = PETSC_FALSE;
4363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
437b022a5c1SBarry Smith }
438b022a5c1SBarry Smith 
4399877f0dbSBarry Smith /*@C
440*20f4b53cSBarry Smith   PFInitializePackage - This function initializes everything in the `PF` package. It is called
441*20f4b53cSBarry Smith   from PetscDLLibraryRegister_petscvec() when using dynamic libraries, and on the first call to `PFCreate()`
4428a690491SBarry Smith   when using shared or static libraries.
4439877f0dbSBarry Smith 
4449877f0dbSBarry Smith   Level: developer
4459877f0dbSBarry Smith 
446*20f4b53cSBarry Smith .seealso: `PF`, `PetscInitialize()`
4479877f0dbSBarry Smith @*/
448d71ae5a4SJacob Faibussowitsch PetscErrorCode PFInitializePackage(void)
449d71ae5a4SJacob Faibussowitsch {
4509877f0dbSBarry Smith   char      logList[256];
4518e81d068SLisandro Dalcin   PetscBool opt, pkg;
4529877f0dbSBarry Smith 
4539877f0dbSBarry Smith   PetscFunctionBegin;
4543ba16761SJacob Faibussowitsch   if (PFPackageInitialized) PetscFunctionReturn(PETSC_SUCCESS);
455b022a5c1SBarry Smith   PFPackageInitialized = PETSC_TRUE;
4569877f0dbSBarry Smith   /* Register Classes */
4579566063dSJacob Faibussowitsch   PetscCall(PetscClassIdRegister("PointFunction", &PF_CLASSID));
4589877f0dbSBarry Smith   /* Register Constructors */
4599566063dSJacob Faibussowitsch   PetscCall(PFRegisterAll());
460e94e781bSJacob Faibussowitsch   /* Process Info */
461e94e781bSJacob Faibussowitsch   {
462e94e781bSJacob Faibussowitsch     PetscClassId classids[1];
463e94e781bSJacob Faibussowitsch 
464e94e781bSJacob Faibussowitsch     classids[0] = PF_CLASSID;
4659566063dSJacob Faibussowitsch     PetscCall(PetscInfoProcessClass("pf", 1, classids));
4669877f0dbSBarry Smith   }
4679877f0dbSBarry Smith   /* Process summary exclusions */
4689566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetString(NULL, NULL, "-log_exclude", logList, sizeof(logList), &opt));
4699877f0dbSBarry Smith   if (opt) {
4709566063dSJacob Faibussowitsch     PetscCall(PetscStrInList("pf", logList, ',', &pkg));
4719566063dSJacob Faibussowitsch     if (pkg) PetscCall(PetscLogEventExcludeClass(PF_CLASSID));
4729877f0dbSBarry Smith   }
4738e81d068SLisandro Dalcin   /* Register package finalizer */
4749566063dSJacob Faibussowitsch   PetscCall(PetscRegisterFinalize(PFFinalizePackage));
4753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4769877f0dbSBarry Smith }
477