xref: /petsc/src/vec/pf/interface/pf.c (revision 03047865b8d8757cf1cf9cda45785c1537b01dc1) !
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
192fe279fdSBarry 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 
2520f4b53cSBarry Smith .seealso: `PF`, `PFCreate()`, `PFDestroy()`, `PFSetType()`, `PFApply()`, `PFApplyVec()`
26292f8084SBarry Smith @*/
PFSet(PF pf,PetscErrorCode (* apply)(void *,PetscInt,const PetscScalar *,PetscScalar *),PetscErrorCode (* applyvec)(void *,Vec,Vec),PetscErrorCode (* view)(void *,PetscViewer),PetscErrorCode (* destroy)(PetscCtxRt),PetscCtx ctx)27*2a8381b2SBarry Smith PetscErrorCode PFSet(PF pf, PetscErrorCode (*apply)(void *, PetscInt, const PetscScalar *, PetscScalar *), PetscErrorCode (*applyvec)(void *, Vec, Vec), PetscErrorCode (*view)(void *, PetscViewer), PetscErrorCode (*destroy)(PetscCtxRt), PetscCtx 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 
4920f4b53cSBarry Smith .seealso: `PF`, `PFCreate()`, `PFSet()`, `PFSetType()`
50292f8084SBarry Smith @*/
PFDestroy(PF * pf)51d71ae5a4SJacob Faibussowitsch PetscErrorCode PFDestroy(PF *pf)
52d71ae5a4SJacob Faibussowitsch {
53292f8084SBarry Smith   PetscFunctionBegin;
543ba16761SJacob Faibussowitsch   if (!*pf) PetscFunctionReturn(PETSC_SUCCESS);
55f4f49eeaSPierre Jolivet   PetscValidHeaderSpecific(*pf, PF_CLASSID, 1);
56f4f49eeaSPierre Jolivet   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 
8220f4b53cSBarry Smith .seealso: `PF`, `PFSet()`, `PFApply()`, `PFDestroy()`, `PFApplyVec()`
83292f8084SBarry Smith @*/
PFCreate(MPI_Comm comm,PetscInt dimin,PetscInt dimout,PF * pf)84d71ae5a4SJacob Faibussowitsch PetscErrorCode PFCreate(MPI_Comm comm, PetscInt dimin, PetscInt dimout, PF *pf)
85d71ae5a4SJacob Faibussowitsch {
86292f8084SBarry Smith   PF newpf;
87292f8084SBarry Smith 
88292f8084SBarry Smith   PetscFunctionBegin;
894f572ea9SToby Isaac   PetscAssertPointer(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   PFApplyVec - Applies the mathematical function to a vector
108292f8084SBarry Smith 
109c3339decSBarry Smith   Collective
110292f8084SBarry Smith 
111292f8084SBarry Smith   Input Parameters:
112292f8084SBarry Smith + pf - the function context
1132fe279fdSBarry Smith - x  - input vector (or `NULL` for the vector (0,1, .... N-1)
114292f8084SBarry Smith 
115292f8084SBarry Smith   Output Parameter:
116292f8084SBarry Smith . y - output vector
117292f8084SBarry Smith 
118292f8084SBarry Smith   Level: beginner
119292f8084SBarry Smith 
12020f4b53cSBarry Smith .seealso: `PF`, `PFApply()`, `PFCreate()`, `PFDestroy()`, `PFSetType()`, `PFSet()`
121292f8084SBarry Smith @*/
PFApplyVec(PF pf,Vec x,Vec y)122d71ae5a4SJacob Faibussowitsch PetscErrorCode PFApplyVec(PF pf, Vec x, Vec y)
123d71ae5a4SJacob Faibussowitsch {
124292f8084SBarry Smith   PetscInt  i, rstart, rend, n, p;
125ace3abfcSBarry Smith   PetscBool nox = PETSC_FALSE;
126292f8084SBarry Smith 
127292f8084SBarry Smith   PetscFunctionBegin;
1280700a824SBarry Smith   PetscValidHeaderSpecific(pf, PF_CLASSID, 1);
1290700a824SBarry Smith   PetscValidHeaderSpecific(y, VEC_CLASSID, 3);
130292f8084SBarry Smith   if (x) {
1310700a824SBarry Smith     PetscValidHeaderSpecific(x, VEC_CLASSID, 2);
13208401ef6SPierre Jolivet     PetscCheck(x != y, PETSC_COMM_SELF, PETSC_ERR_ARG_IDN, "x and y must be different vectors");
133292f8084SBarry Smith   } else {
134292f8084SBarry Smith     PetscScalar *xx;
13503193ff8SBarry Smith     PetscInt     lsize;
136292f8084SBarry Smith 
1379566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(y, &lsize));
13803193ff8SBarry Smith     lsize = pf->dimin * lsize / pf->dimout;
1399566063dSJacob Faibussowitsch     PetscCall(VecCreateMPI(PetscObjectComm((PetscObject)y), lsize, PETSC_DETERMINE, &x));
140292f8084SBarry Smith     nox = PETSC_TRUE;
1419566063dSJacob Faibussowitsch     PetscCall(VecGetOwnershipRange(x, &rstart, &rend));
1429566063dSJacob Faibussowitsch     PetscCall(VecGetArray(x, &xx));
143f6e5521dSKarl Rupp     for (i = rstart; i < rend; i++) xx[i - rstart] = (PetscScalar)i;
1449566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(x, &xx));
145292f8084SBarry Smith   }
146292f8084SBarry Smith 
1479566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(x, &n));
1489566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(y, &p));
149c9cc58a2SBarry 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);
150c9cc58a2SBarry 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);
15108401ef6SPierre 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);
152292f8084SBarry Smith 
1539927e4dfSBarry Smith   if (pf->ops->applyvec) PetscCallBack("PF callback apply to vector", (*pf->ops->applyvec)(pf->data, x, y));
154dbbe0bcdSBarry Smith   else {
1559927e4dfSBarry Smith     const PetscScalar *xx;
1569927e4dfSBarry Smith     PetscScalar       *yy;
157292f8084SBarry Smith 
1589566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x, &n));
159292f8084SBarry Smith     n = n / pf->dimin;
1609927e4dfSBarry Smith     PetscCall(VecGetArrayRead(x, &xx));
1619566063dSJacob Faibussowitsch     PetscCall(VecGetArray(y, &yy));
1629927e4dfSBarry Smith     PetscCallBack("PF callback apply to array", (*pf->ops->apply)(pf->data, n, xx, yy));
1639927e4dfSBarry Smith     PetscCall(VecRestoreArrayRead(x, &xx));
1649566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(y, &yy));
165292f8084SBarry Smith   }
16648a46eb9SPierre Jolivet   if (nox) PetscCall(VecDestroy(&x));
1673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
168292f8084SBarry Smith }
169292f8084SBarry Smith 
170292f8084SBarry Smith /*@
171292f8084SBarry Smith   PFApply - Applies the mathematical function to an array of values.
172292f8084SBarry Smith 
173c3339decSBarry Smith   Collective
174292f8084SBarry Smith 
175292f8084SBarry Smith   Input Parameters:
176292f8084SBarry Smith + pf - the function context
177292f8084SBarry Smith . n  - number of pointwise function evaluations to perform, each pointwise function evaluation
178292f8084SBarry Smith        is a function of dimin variables and computes dimout variables where dimin and dimout are defined
1792fe279fdSBarry Smith        in the call to `PFCreate()`
180292f8084SBarry Smith - x  - input array
181292f8084SBarry Smith 
182292f8084SBarry Smith   Output Parameter:
183292f8084SBarry Smith . y - output array
184292f8084SBarry Smith 
185292f8084SBarry Smith   Level: beginner
186292f8084SBarry Smith 
18720f4b53cSBarry Smith .seealso: `PF`, `PFApplyVec()`, `PFCreate()`, `PFDestroy()`, `PFSetType()`, `PFSet()`
188292f8084SBarry Smith @*/
PFApply(PF pf,PetscInt n,const PetscScalar * x,PetscScalar * y)189d71ae5a4SJacob Faibussowitsch PetscErrorCode PFApply(PF pf, PetscInt n, const PetscScalar *x, PetscScalar *y)
190d71ae5a4SJacob Faibussowitsch {
191292f8084SBarry Smith   PetscFunctionBegin;
1920700a824SBarry Smith   PetscValidHeaderSpecific(pf, PF_CLASSID, 1);
1934f572ea9SToby Isaac   PetscAssertPointer(x, 3);
1944f572ea9SToby Isaac   PetscAssertPointer(y, 4);
19508401ef6SPierre Jolivet   PetscCheck(x != y, PETSC_COMM_SELF, PETSC_ERR_ARG_IDN, "x and y must be different arrays");
196292f8084SBarry Smith 
1979927e4dfSBarry Smith   PetscCallBack("PF callback apply", (*pf->ops->apply)(pf->data, n, x, y));
1983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
199292f8084SBarry Smith }
200292f8084SBarry Smith 
201ffeef943SBarry Smith /*@
20220f4b53cSBarry Smith   PFViewFromOptions - View a `PF` based on options set in the options database
203fe2efc57SMark 
204c3339decSBarry Smith   Collective
205fe2efc57SMark 
206fe2efc57SMark   Input Parameters:
20720f4b53cSBarry Smith + A    - the `PF` context
20820f4b53cSBarry Smith . obj  - Optional object that provides the prefix used to search the options database
209736c3998SJose E. Roman - name - command line option
210fe2efc57SMark 
211fe2efc57SMark   Level: intermediate
21220f4b53cSBarry Smith 
21320f4b53cSBarry Smith   Note:
21420f4b53cSBarry Smith   See `PetscObjectViewFromOptions()` for the variety of viewer options available
21520f4b53cSBarry Smith 
216db781477SPatrick Sanan .seealso: `PF`, `PFView`, `PetscObjectViewFromOptions()`, `PFCreate()`
217fe2efc57SMark @*/
PFViewFromOptions(PF A,PetscObject obj,const char name[])218d71ae5a4SJacob Faibussowitsch PetscErrorCode PFViewFromOptions(PF A, PetscObject obj, const char name[])
219d71ae5a4SJacob Faibussowitsch {
220fe2efc57SMark   PetscFunctionBegin;
221fe2efc57SMark   PetscValidHeaderSpecific(A, PF_CLASSID, 1);
2229566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
2233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
224fe2efc57SMark }
225fe2efc57SMark 
226292f8084SBarry Smith /*@
227292f8084SBarry Smith   PFView - Prints information about a mathematical function
228292f8084SBarry Smith 
22920f4b53cSBarry Smith   Collective unless `viewer` is `PETSC_VIEWER_STDOUT_SELF`
230292f8084SBarry Smith 
231292f8084SBarry Smith   Input Parameters:
23238b5cf2dSJacob Faibussowitsch + pf     - the `PF` context
233292f8084SBarry Smith - viewer - optional visualization context
234292f8084SBarry Smith 
23520f4b53cSBarry Smith   Level: developer
23620f4b53cSBarry Smith 
237292f8084SBarry Smith   Note:
238292f8084SBarry Smith   The available visualization contexts include
23920f4b53cSBarry Smith +     `PETSC_VIEWER_STDOUT_SELF` - standard output (default)
24020f4b53cSBarry Smith -     `PETSC_VIEWER_STDOUT_WORLD` - synchronized standard
241292f8084SBarry Smith   output where only the first processor opens
242292f8084SBarry Smith   the file.  All other processors send their
243292f8084SBarry Smith   data to the first processor to print.
244292f8084SBarry Smith 
245292f8084SBarry Smith   The user can open an alternative visualization contexts with
24620f4b53cSBarry Smith   `PetscViewerASCIIOpen()` (output to a specified file).
247292f8084SBarry Smith 
24820f4b53cSBarry Smith .seealso: `PF`, `PetscViewerCreate()`, `PetscViewerASCIIOpen()`
249292f8084SBarry Smith @*/
PFView(PF pf,PetscViewer viewer)250d71ae5a4SJacob Faibussowitsch PetscErrorCode PFView(PF pf, PetscViewer viewer)
251d71ae5a4SJacob Faibussowitsch {
2529f196a02SMartin Diehl   PetscBool         isascii;
253292f8084SBarry Smith   PetscViewerFormat format;
254292f8084SBarry Smith 
255292f8084SBarry Smith   PetscFunctionBegin;
2560700a824SBarry Smith   PetscValidHeaderSpecific(pf, PF_CLASSID, 1);
25748a46eb9SPierre Jolivet   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)pf), &viewer));
2580700a824SBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
259292f8084SBarry Smith   PetscCheckSameComm(pf, 1, viewer, 2);
260292f8084SBarry Smith 
2619f196a02SMartin Diehl   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
2629f196a02SMartin Diehl   if (isascii) {
2639566063dSJacob Faibussowitsch     PetscCall(PetscViewerGetFormat(viewer, &format));
2649566063dSJacob Faibussowitsch     PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)pf, viewer));
265292f8084SBarry Smith     if (pf->ops->view) {
2669566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPushTab(viewer));
2679927e4dfSBarry Smith       PetscCallBack("PF callback view", (*pf->ops->view)(pf->data, viewer));
2689566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPopTab(viewer));
269292f8084SBarry Smith     }
270292f8084SBarry Smith   }
2713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
272292f8084SBarry Smith }
273292f8084SBarry Smith 
274bdf89e91SBarry Smith /*@C
275bdf89e91SBarry Smith   PFRegister - Adds a method to the mathematical function package.
276292f8084SBarry Smith 
27720f4b53cSBarry Smith   Not Collective
278292f8084SBarry Smith 
279292f8084SBarry Smith   Input Parameters:
28020f4b53cSBarry Smith + sname    - name of a new user-defined solver
28120f4b53cSBarry Smith - function - routine to create method context
282292f8084SBarry Smith 
28338b5cf2dSJacob Faibussowitsch   Example Usage:
284292f8084SBarry Smith .vb
285bdf89e91SBarry Smith    PFRegister("my_function", MyFunctionSetCreate);
286292f8084SBarry Smith .ve
287292f8084SBarry Smith 
288292f8084SBarry Smith   Then, your solver can be chosen with the procedural interface via
289b44f4de4SBarry Smith .vb
290b44f4de4SBarry Smith   PFSetType(pf, "my_function")
291b44f4de4SBarry Smith .ve
292292f8084SBarry Smith   or at runtime via the option
293b44f4de4SBarry Smith .vb
294b44f4de4SBarry Smith   -pf_type my_function
295b44f4de4SBarry Smith .ve
296292f8084SBarry Smith 
297292f8084SBarry Smith   Level: advanced
298292f8084SBarry Smith 
29920f4b53cSBarry Smith   Note:
30020f4b53cSBarry Smith   `PFRegister()` may be called multiple times to add several user-defined functions
30120f4b53cSBarry Smith 
30242747ad1SJacob Faibussowitsch .seealso: `PF`, `PFRegisterAll()`, `PFRegisterDestroy()`
303bdf89e91SBarry Smith @*/
PFRegister(const char sname[],PetscErrorCode (* function)(PF,void *))304d71ae5a4SJacob Faibussowitsch PetscErrorCode PFRegister(const char sname[], PetscErrorCode (*function)(PF, void *))
305d71ae5a4SJacob Faibussowitsch {
306292f8084SBarry Smith   PetscFunctionBegin;
3079566063dSJacob Faibussowitsch   PetscCall(PFInitializePackage());
3089566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&PFList, sname, function));
3093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
310292f8084SBarry Smith }
311292f8084SBarry Smith 
312cc4c1da9SBarry Smith /*@
31320f4b53cSBarry Smith   PFGetType - Gets the `PFType` name (as a string) from the `PF`
314292f8084SBarry Smith   context.
315292f8084SBarry Smith 
316292f8084SBarry Smith   Not Collective
317292f8084SBarry Smith 
318292f8084SBarry Smith   Input Parameter:
319292f8084SBarry Smith . pf - the function context
320292f8084SBarry Smith 
321292f8084SBarry Smith   Output Parameter:
322c4e43342SLisandro Dalcin . type - name of function
323292f8084SBarry Smith 
324292f8084SBarry Smith   Level: intermediate
325292f8084SBarry Smith 
32620f4b53cSBarry Smith .seealso: `PF`, `PFSetType()`
327292f8084SBarry Smith @*/
PFGetType(PF pf,PFType * type)328d71ae5a4SJacob Faibussowitsch PetscErrorCode PFGetType(PF pf, PFType *type)
329d71ae5a4SJacob Faibussowitsch {
330292f8084SBarry Smith   PetscFunctionBegin;
3310700a824SBarry Smith   PetscValidHeaderSpecific(pf, PF_CLASSID, 1);
3324f572ea9SToby Isaac   PetscAssertPointer(type, 2);
333c4e43342SLisandro Dalcin   *type = ((PetscObject)pf)->type_name;
3343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
335292f8084SBarry Smith }
336292f8084SBarry Smith 
337cc4c1da9SBarry Smith /*@
33820f4b53cSBarry Smith   PFSetType - Builds `PF` for a particular function
339292f8084SBarry Smith 
340c3339decSBarry Smith   Collective
341292f8084SBarry Smith 
342d8d19677SJose E. Roman   Input Parameters:
343292f8084SBarry Smith + pf   - the function context.
344292f8084SBarry Smith . type - a known method
345292f8084SBarry Smith - ctx  - optional type dependent context
346292f8084SBarry Smith 
347292f8084SBarry Smith   Options Database Key:
348292f8084SBarry Smith . -pf_type <type> - Sets PF type
349292f8084SBarry Smith 
350292f8084SBarry Smith   Level: intermediate
351292f8084SBarry Smith 
35220f4b53cSBarry Smith   Note:
35320f4b53cSBarry Smith   See "petsc/include/petscpf.h" for available methods (for instance, `PFCONSTANT`)
354292f8084SBarry Smith 
35520f4b53cSBarry Smith .seealso: `PF`, `PFSet()`, `PFRegister()`, `PFCreate()`, `DMDACreatePF()`
356292f8084SBarry Smith @*/
PFSetType(PF pf,PFType type,PetscCtx ctx)357*2a8381b2SBarry Smith PetscErrorCode PFSetType(PF pf, PFType type, PetscCtx ctx)
358d71ae5a4SJacob Faibussowitsch {
359ace3abfcSBarry Smith   PetscBool match;
3605f80ce2aSJacob Faibussowitsch   PetscErrorCode (*r)(PF, void *);
361292f8084SBarry Smith 
362292f8084SBarry Smith   PetscFunctionBegin;
3630700a824SBarry Smith   PetscValidHeaderSpecific(pf, PF_CLASSID, 1);
3644f572ea9SToby Isaac   PetscAssertPointer(type, 2);
365292f8084SBarry Smith 
3669566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)pf, type, &match));
3673ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
368292f8084SBarry Smith 
369dbbe0bcdSBarry Smith   PetscTryTypeMethod(pf, destroy);
3704c8fdceaSLisandro Dalcin   pf->data = NULL;
371292f8084SBarry Smith 
372292f8084SBarry Smith   /* Determine the PFCreateXXX routine for a particular function */
3739566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(PFList, type, &r));
3746adde796SStefano Zampini   PetscCheck(r, PetscObjectComm((PetscObject)pf), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unable to find requested PF type %s", type);
3754c8fdceaSLisandro Dalcin   pf->ops->destroy  = NULL;
3764c8fdceaSLisandro Dalcin   pf->ops->view     = NULL;
3774c8fdceaSLisandro Dalcin   pf->ops->apply    = NULL;
3784c8fdceaSLisandro Dalcin   pf->ops->applyvec = NULL;
379292f8084SBarry Smith 
380292f8084SBarry Smith   /* Call the PFCreateXXX routine for this particular function */
3819566063dSJacob Faibussowitsch   PetscCall((*r)(pf, ctx));
382292f8084SBarry Smith 
3839566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)pf, type));
3843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
385292f8084SBarry Smith }
386292f8084SBarry Smith 
387292f8084SBarry Smith /*@
38820f4b53cSBarry Smith   PFSetFromOptions - Sets `PF` options from the options database.
389292f8084SBarry Smith 
390c3339decSBarry Smith   Collective
391292f8084SBarry Smith 
392292f8084SBarry Smith   Input Parameters:
393292f8084SBarry Smith . pf - the mathematical function context
394292f8084SBarry Smith 
39520f4b53cSBarry Smith   Level: intermediate
396292f8084SBarry Smith 
397292f8084SBarry Smith   Notes:
398292f8084SBarry Smith   To see all options, run your program with the -help option
399292f8084SBarry Smith   or consult the users manual.
400292f8084SBarry Smith 
40120f4b53cSBarry Smith .seealso: `PF`
402292f8084SBarry Smith @*/
PFSetFromOptions(PF pf)403d71ae5a4SJacob Faibussowitsch PetscErrorCode PFSetFromOptions(PF pf)
404d71ae5a4SJacob Faibussowitsch {
405292f8084SBarry Smith   char      type[256];
406ace3abfcSBarry Smith   PetscBool flg;
407292f8084SBarry Smith 
408292f8084SBarry Smith   PetscFunctionBegin;
4090700a824SBarry Smith   PetscValidHeaderSpecific(pf, PF_CLASSID, 1);
410292f8084SBarry Smith 
411d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)pf);
4129566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-pf_type", "Type of function", "PFSetType", PFList, NULL, type, 256, &flg));
4131baa6e33SBarry Smith   if (flg) PetscCall(PFSetType(pf, type, NULL));
414dbbe0bcdSBarry Smith   PetscTryTypeMethod(pf, setfromoptions, PetscOptionsObject);
4155d973c19SBarry Smith 
4165d973c19SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
417dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)pf, PetscOptionsObject));
418d0609cedSBarry Smith   PetscOptionsEnd();
4193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
420292f8084SBarry Smith }
421292f8084SBarry Smith 
422ace3abfcSBarry Smith static PetscBool PFPackageInitialized = PETSC_FALSE;
42366976f2fSJacob Faibussowitsch 
424b022a5c1SBarry Smith /*@C
42520f4b53cSBarry Smith   PFFinalizePackage - This function destroys everything in the PETSc `PF` package. It is
42620f4b53cSBarry Smith   called from `PetscFinalize()`.
427b022a5c1SBarry Smith 
428b022a5c1SBarry Smith   Level: developer
429b022a5c1SBarry Smith 
43020f4b53cSBarry Smith .seealso: `PF`, `PetscFinalize()`
431b022a5c1SBarry Smith @*/
PFFinalizePackage(void)432d71ae5a4SJacob Faibussowitsch PetscErrorCode PFFinalizePackage(void)
433d71ae5a4SJacob Faibussowitsch {
434b022a5c1SBarry Smith   PetscFunctionBegin;
4359566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListDestroy(&PFList));
436b022a5c1SBarry Smith   PFPackageInitialized = PETSC_FALSE;
437b022a5c1SBarry Smith   PFRegisterAllCalled  = PETSC_FALSE;
4383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
439b022a5c1SBarry Smith }
440b022a5c1SBarry Smith 
4419877f0dbSBarry Smith /*@C
44220f4b53cSBarry Smith   PFInitializePackage - This function initializes everything in the `PF` package. It is called
44320f4b53cSBarry Smith   from PetscDLLibraryRegister_petscvec() when using dynamic libraries, and on the first call to `PFCreate()`
4448a690491SBarry Smith   when using shared or static libraries.
4459877f0dbSBarry Smith 
4469877f0dbSBarry Smith   Level: developer
4479877f0dbSBarry Smith 
44820f4b53cSBarry Smith .seealso: `PF`, `PetscInitialize()`
4499877f0dbSBarry Smith @*/
PFInitializePackage(void)450d71ae5a4SJacob Faibussowitsch PetscErrorCode PFInitializePackage(void)
451d71ae5a4SJacob Faibussowitsch {
4529877f0dbSBarry Smith   char      logList[256];
4538e81d068SLisandro Dalcin   PetscBool opt, pkg;
4549877f0dbSBarry Smith 
4559877f0dbSBarry Smith   PetscFunctionBegin;
4563ba16761SJacob Faibussowitsch   if (PFPackageInitialized) PetscFunctionReturn(PETSC_SUCCESS);
457b022a5c1SBarry Smith   PFPackageInitialized = PETSC_TRUE;
4589877f0dbSBarry Smith   /* Register Classes */
4599566063dSJacob Faibussowitsch   PetscCall(PetscClassIdRegister("PointFunction", &PF_CLASSID));
4609877f0dbSBarry Smith   /* Register Constructors */
4619566063dSJacob Faibussowitsch   PetscCall(PFRegisterAll());
462e94e781bSJacob Faibussowitsch   /* Process Info */
463e94e781bSJacob Faibussowitsch   {
464e94e781bSJacob Faibussowitsch     PetscClassId classids[1];
465e94e781bSJacob Faibussowitsch 
466e94e781bSJacob Faibussowitsch     classids[0] = PF_CLASSID;
4679566063dSJacob Faibussowitsch     PetscCall(PetscInfoProcessClass("pf", 1, classids));
4689877f0dbSBarry Smith   }
4699877f0dbSBarry Smith   /* Process summary exclusions */
4709566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetString(NULL, NULL, "-log_exclude", logList, sizeof(logList), &opt));
4719877f0dbSBarry Smith   if (opt) {
4729566063dSJacob Faibussowitsch     PetscCall(PetscStrInList("pf", logList, ',', &pkg));
4739566063dSJacob Faibussowitsch     if (pkg) PetscCall(PetscLogEventExcludeClass(PF_CLASSID));
4749877f0dbSBarry Smith   }
4758e81d068SLisandro Dalcin   /* Register package finalizer */
4769566063dSJacob Faibussowitsch   PetscCall(PetscRegisterFinalize(PFFinalizePackage));
4773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4789877f0dbSBarry Smith }
479