Actual source code: pf.c
1: /*
2: The PF mathematical functions interface routines, callable by users.
3: */
4: #include <../src/vec/pf/pfimpl.h>
6: PetscClassId PF_CLASSID = 0;
7: PetscFunctionList PFList = NULL; /* list of all registered PD functions */
8: PetscBool PFRegisterAllCalled = PETSC_FALSE;
10: /*@
11: PFSet - Sets the C/C++/Fortran functions to be used by the PF function
13: Collective
15: Input Parameters:
16: + pf - the function context
17: . apply - function to apply to an array
18: . applyvec - function to apply to a Vec
19: . view - function that prints information about the `PF`
20: . destroy - function to free the private function context
21: - ctx - private function context
23: Level: beginner
25: .seealso: `PF`, `PFCreate()`, `PFDestroy()`, `PFSetType()`, `PFApply()`, `PFApplyVec()`
26: @*/
27: PetscErrorCode PFSet(PF pf, PetscErrorCode (*apply)(PetscCtx, PetscInt, const PetscScalar *, PetscScalar *), PetscErrorCode (*applyvec)(PetscCtx, Vec, Vec), PetscErrorCode (*view)(PetscCtx, PetscViewer), PetscErrorCode (*destroy)(PetscCtxRt), PetscCtx ctx)
28: {
29: PetscFunctionBegin;
31: pf->data = ctx;
32: pf->ops->destroy = destroy;
33: pf->ops->apply = apply;
34: pf->ops->applyvec = applyvec;
35: pf->ops->view = view;
36: PetscFunctionReturn(PETSC_SUCCESS);
37: }
39: /*@
40: PFDestroy - Destroys `PF` context that was created with `PFCreate()`.
42: Collective
44: Input Parameter:
45: . pf - the function context
47: Level: beginner
49: .seealso: `PF`, `PFCreate()`, `PFSet()`, `PFSetType()`
50: @*/
51: PetscErrorCode PFDestroy(PF *pf)
52: {
53: PetscFunctionBegin;
54: if (!*pf) PetscFunctionReturn(PETSC_SUCCESS);
56: if (--((PetscObject)*pf)->refct > 0) PetscFunctionReturn(PETSC_SUCCESS);
58: PetscCall(PFViewFromOptions(*pf, NULL, "-pf_view"));
59: /* if memory was published with SAWs then destroy it */
60: PetscCall(PetscObjectSAWsViewOff((PetscObject)*pf));
62: if ((*pf)->ops->destroy) PetscCall((*(*pf)->ops->destroy)((*pf)->data));
63: PetscCall(PetscHeaderDestroy(pf));
64: PetscFunctionReturn(PETSC_SUCCESS);
65: }
67: /*@
68: PFCreate - Creates a mathematical function context.
70: Collective
72: Input Parameters:
73: + comm - MPI communicator
74: . dimin - dimension of the space you are mapping from
75: - dimout - dimension of the space you are mapping to
77: Output Parameter:
78: . pf - the function context
80: Level: developer
82: .seealso: `PF`, `PFSet()`, `PFApply()`, `PFDestroy()`, `PFApplyVec()`
83: @*/
84: PetscErrorCode PFCreate(MPI_Comm comm, PetscInt dimin, PetscInt dimout, PF *pf)
85: {
86: PF newpf;
88: PetscFunctionBegin;
89: PetscAssertPointer(pf, 4);
90: *pf = NULL;
91: PetscCall(PFInitializePackage());
93: PetscCall(PetscHeaderCreate(newpf, PF_CLASSID, "PF", "Mathematical functions", "Vec", comm, PFDestroy, PFView));
94: newpf->data = NULL;
95: newpf->dimin = dimin;
96: newpf->dimout = dimout;
98: *pf = newpf;
99: PetscFunctionReturn(PETSC_SUCCESS);
100: }
102: /*@
103: PFApplyVec - Applies the mathematical function to a vector
105: Collective
107: Input Parameters:
108: + pf - the function context
109: - x - input vector (or `NULL` for the vector (0,1, .... N-1)
111: Output Parameter:
112: . y - output vector
114: Level: beginner
116: .seealso: `PF`, `PFApply()`, `PFCreate()`, `PFDestroy()`, `PFSetType()`, `PFSet()`
117: @*/
118: PetscErrorCode PFApplyVec(PF pf, Vec x, Vec y)
119: {
120: PetscInt i, rstart, rend, n, p;
121: PetscBool nox = PETSC_FALSE;
123: PetscFunctionBegin;
126: if (x) {
128: PetscCheck(x != y, PETSC_COMM_SELF, PETSC_ERR_ARG_IDN, "x and y must be different vectors");
129: } else {
130: PetscScalar *xx;
131: PetscInt lsize;
133: PetscCall(VecGetLocalSize(y, &lsize));
134: lsize = pf->dimin * lsize / pf->dimout;
135: PetscCall(VecCreateMPI(PetscObjectComm((PetscObject)y), lsize, PETSC_DETERMINE, &x));
136: nox = PETSC_TRUE;
137: PetscCall(VecGetOwnershipRange(x, &rstart, &rend));
138: PetscCall(VecGetArray(x, &xx));
139: for (i = rstart; i < rend; i++) xx[i - rstart] = (PetscScalar)i;
140: PetscCall(VecRestoreArray(x, &xx));
141: }
143: PetscCall(VecGetLocalSize(x, &n));
144: PetscCall(VecGetLocalSize(y, &p));
145: 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);
146: 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);
147: 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);
149: if (pf->ops->applyvec) PetscCallBack("PF callback apply to vector", (*pf->ops->applyvec)(pf->data, x, y));
150: else {
151: const PetscScalar *xx;
152: PetscScalar *yy;
154: PetscCall(VecGetLocalSize(x, &n));
155: n = n / pf->dimin;
156: PetscCall(VecGetArrayRead(x, &xx));
157: PetscCall(VecGetArray(y, &yy));
158: PetscCallBack("PF callback apply to array", (*pf->ops->apply)(pf->data, n, xx, yy));
159: PetscCall(VecRestoreArrayRead(x, &xx));
160: PetscCall(VecRestoreArray(y, &yy));
161: }
162: if (nox) PetscCall(VecDestroy(&x));
163: PetscFunctionReturn(PETSC_SUCCESS);
164: }
166: /*@
167: PFApply - Applies the mathematical function to an array of values.
169: Collective
171: Input Parameters:
172: + pf - the function context
173: . n - number of pointwise function evaluations to perform, each pointwise function evaluation
174: is a function of dimin variables and computes dimout variables where dimin and dimout are defined
175: in the call to `PFCreate()`
176: - x - input array
178: Output Parameter:
179: . y - output array
181: Level: beginner
183: .seealso: `PF`, `PFApplyVec()`, `PFCreate()`, `PFDestroy()`, `PFSetType()`, `PFSet()`
184: @*/
185: PetscErrorCode PFApply(PF pf, PetscInt n, const PetscScalar *x, PetscScalar *y)
186: {
187: PetscFunctionBegin;
189: PetscAssertPointer(x, 3);
190: PetscAssertPointer(y, 4);
191: PetscCheck(x != y, PETSC_COMM_SELF, PETSC_ERR_ARG_IDN, "x and y must be different arrays");
193: PetscCallBack("PF callback apply", (*pf->ops->apply)(pf->data, n, x, y));
194: PetscFunctionReturn(PETSC_SUCCESS);
195: }
197: /*@
198: PFViewFromOptions - View a `PF` based on options set in the options database
200: Collective
202: Input Parameters:
203: + A - the `PF` context
204: . obj - optional object that provides the prefix used to search the options database, pass `NULL` to use the options prefix of `A`
205: - name - command line option
207: Options Database Key:
208: . -name viewer_specification - See `PetscOptionsCreateViewer()` for the values of `viewer_specification`
210: Level: intermediate
212: Note:
213: This checks the options database, creates the viewer on-the-fly, uses it and then destroys it. Hence it should not be called in heavily used routines,
214: rather `PetscOptionsCreateViewer()` should be used to construct the viewer once which can then be utilized in the heavily used routine.
216: .seealso: `PF`, `PFView()`, `PetscObjectViewFromOptions()`, `PFCreate()`, `PetscOptionsCreateViewer()`
217: @*/
218: PetscErrorCode PFViewFromOptions(PF A, PetscObject obj, const char name[])
219: {
220: PetscFunctionBegin;
222: PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
223: PetscFunctionReturn(PETSC_SUCCESS);
224: }
226: /*@
227: PFView - Prints information about a mathematical function
229: Collective unless `viewer` is `PETSC_VIEWER_STDOUT_SELF`
231: Input Parameters:
232: + pf - the `PF` context
233: - viewer - optional visualization context
235: Level: developer
237: Note:
238: The available visualization contexts include
239: + `PETSC_VIEWER_STDOUT_SELF` - standard output (default)
240: - `PETSC_VIEWER_STDOUT_WORLD` - synchronized standard
241: output where only the first processor opens
242: the file. All other processors send their
243: data to the first processor to print.
245: The user can open an alternative visualization contexts with
246: `PetscViewerASCIIOpen()` (output to a specified file).
248: .seealso: `PF`, `PetscViewerCreate()`, `PetscViewerASCIIOpen()`
249: @*/
250: PetscErrorCode PFView(PF pf, PetscViewer viewer)
251: {
252: PetscBool isascii;
253: PetscViewerFormat format;
255: PetscFunctionBegin;
257: if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)pf), &viewer));
259: PetscCheckSameComm(pf, 1, viewer, 2);
261: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
262: if (isascii) {
263: PetscCall(PetscViewerGetFormat(viewer, &format));
264: PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)pf, viewer));
265: if (pf->ops->view) {
266: PetscCall(PetscViewerASCIIPushTab(viewer));
267: PetscCallBack("PF callback view", (*pf->ops->view)(pf->data, viewer));
268: PetscCall(PetscViewerASCIIPopTab(viewer));
269: }
270: }
271: PetscFunctionReturn(PETSC_SUCCESS);
272: }
274: /*@
275: PFRegister - Adds a method to the mathematical function package.
277: Not Collective
279: Input Parameters:
280: + sname - name of a new user-defined solver
281: - function - routine to create method context
283: Example Usage:
284: .vb
285: PFRegister("my_function", MyFunctionSetCreate);
286: .ve
288: Then, your solver can be chosen with the procedural interface via
289: .vb
290: PFSetType(pf, "my_function")
291: .ve
292: or at runtime via the option
293: .vb
294: -pf_type my_function
295: .ve
297: Level: advanced
299: Note:
300: `PFRegister()` may be called multiple times to add several user-defined functions
302: .seealso: `PF`, `PFRegisterAll()`, `PFRegisterDestroy()`
303: @*/
304: PetscErrorCode PFRegister(const char sname[], PetscErrorCode (*function)(PF, PetscCtx))
305: {
306: PetscFunctionBegin;
307: PetscCall(PFInitializePackage());
308: PetscCall(PetscFunctionListAdd(&PFList, sname, function));
309: PetscFunctionReturn(PETSC_SUCCESS);
310: }
312: /*@
313: PFGetType - Gets the `PFType` name (as a string) from the `PF`
314: context.
316: Not Collective
318: Input Parameter:
319: . pf - the function context
321: Output Parameter:
322: . type - name of function
324: Level: intermediate
326: Note:
327: `type` should not be retained for later use as it will be an invalid pointer if the `PFType` of `pf` is changed.
329: .seealso: `PF`, `PFSetType()`, `PFType`, `PetscObjectTypeCompare()`, `PetscObjectTypeCompareAny()`
330: @*/
331: PetscErrorCode PFGetType(PF pf, PFType *type)
332: {
333: PetscFunctionBegin;
335: PetscAssertPointer(type, 2);
336: *type = ((PetscObject)pf)->type_name;
337: PetscFunctionReturn(PETSC_SUCCESS);
338: }
340: /*@
341: PFSetType - Builds `PF` for a particular function
343: Collective
345: Input Parameters:
346: + pf - the function context.
347: . type - a known type, see `PFType` for available methods (for instance, `PFCONSTANT`)
348: - ctx - optional type dependent context
350: Options Database Key:
351: . -pf_type (constant|mat|string|quick|identity|matlab) - Set the `PFType`
353: Level: intermediate
355: .seealso: `PF`, `PFSet()`, `PFRegister()`, `PFCreate()`, `DMDACreatePF()`, `PFType`, `PFGetType()`
356: @*/
357: PetscErrorCode PFSetType(PF pf, PFType type, PetscCtx ctx)
358: {
359: PetscBool match;
360: PetscErrorCode (*r)(PF, PetscCtx);
362: PetscFunctionBegin;
364: PetscAssertPointer(type, 2);
366: PetscCall(PetscObjectTypeCompare((PetscObject)pf, type, &match));
367: if (match) PetscFunctionReturn(PETSC_SUCCESS);
369: PetscTryTypeMethod(pf, destroy);
370: pf->data = NULL;
372: /* Determine the PFCreateXXX routine for a particular function */
373: PetscCall(PetscFunctionListFind(PFList, type, &r));
374: PetscCheck(r, PetscObjectComm((PetscObject)pf), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unable to find requested PF type %s", type);
375: pf->ops->destroy = NULL;
376: pf->ops->view = NULL;
377: pf->ops->apply = NULL;
378: pf->ops->applyvec = NULL;
380: /* Call the PFCreateXXX routine for this particular function */
381: PetscCall((*r)(pf, ctx));
383: PetscCall(PetscObjectChangeTypeName((PetscObject)pf, type));
384: PetscFunctionReturn(PETSC_SUCCESS);
385: }
387: /*@
388: PFSetFromOptions - Sets `PF` options from the options database.
390: Collective
392: Input Parameters:
393: . pf - the mathematical function context
395: Level: intermediate
397: Notes:
398: To see all options, run your program with the -help option
399: or consult the users manual.
401: .seealso: `PF`
402: @*/
403: PetscErrorCode PFSetFromOptions(PF pf)
404: {
405: char type[256];
406: PetscBool flg;
408: PetscFunctionBegin;
411: PetscObjectOptionsBegin((PetscObject)pf);
412: PetscCall(PetscOptionsFList("-pf_type", "Type of function", "PFSetType", PFList, NULL, type, sizeof(type), &flg));
413: if (flg) PetscCall(PFSetType(pf, type, NULL));
414: PetscTryTypeMethod(pf, setfromoptions, PetscOptionsObject);
416: /* process any options handlers added with PetscObjectAddOptionsHandler() */
417: PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)pf, PetscOptionsObject));
418: PetscOptionsEnd();
419: PetscFunctionReturn(PETSC_SUCCESS);
420: }
422: static PetscBool PFPackageInitialized = PETSC_FALSE;
424: /*@
425: PFFinalizePackage - This function destroys everything in the PETSc `PF` package. It is
426: called from `PetscFinalize()`.
428: Level: developer
430: .seealso: `PF`, `PetscFinalize()`
431: @*/
432: PetscErrorCode PFFinalizePackage(void)
433: {
434: PetscFunctionBegin;
435: PetscCall(PetscFunctionListDestroy(&PFList));
436: PFPackageInitialized = PETSC_FALSE;
437: PFRegisterAllCalled = PETSC_FALSE;
438: PetscFunctionReturn(PETSC_SUCCESS);
439: }
441: /*@
442: PFInitializePackage - This function initializes everything in the `PF` package. It is called
443: from PetscDLLibraryRegister_petscvec() when using dynamic libraries, and on the first call to `PFCreate()`
444: when using shared or static libraries.
446: Level: developer
448: .seealso: `PF`, `PetscInitialize()`
449: @*/
450: PetscErrorCode PFInitializePackage(void)
451: {
452: char logList[256];
453: PetscBool opt, pkg;
455: PetscFunctionBegin;
456: if (PFPackageInitialized) PetscFunctionReturn(PETSC_SUCCESS);
457: PFPackageInitialized = PETSC_TRUE;
458: /* Register Classes */
459: PetscCall(PetscClassIdRegister("PointFunction", &PF_CLASSID));
460: /* Register Constructors */
461: PetscCall(PFRegisterAll());
462: /* Process Info */
463: {
464: PetscClassId classids[1];
466: classids[0] = PF_CLASSID;
467: PetscCall(PetscInfoProcessClass("pf", 1, classids));
468: }
469: /* Process summary exclusions */
470: PetscCall(PetscOptionsGetString(NULL, NULL, "-log_exclude", logList, sizeof(logList), &opt));
471: if (opt) {
472: PetscCall(PetscStrInList("pf", logList, ',', &pkg));
473: if (pkg) PetscCall(PetscLogEventExcludeClass(PF_CLASSID));
474: }
475: /* Register package finalizer */
476: PetscCall(PetscRegisterFinalize(PFFinalizePackage));
477: PetscFunctionReturn(PETSC_SUCCESS);
478: }
480: /*@
481: PFSetOptionsPrefix - Sets the prefix used for searching for all
482: `PF` options in the database.
484: Logically Collective
486: Input Parameters:
487: + pf - the `PF` context
488: - prefix - the prefix string to prepend to all `PF` option requests
490: Level: advanced
492: Note:
493: A hyphen (-) must NOT be given at the beginning of the prefix name.
494: The first character of all runtime options is AUTOMATICALLY the
495: hyphen.
497: .seealso: [](ch_ksp), `PF`, `PFSetFromOptions()`, `PFAppendOptionsPrefix()`, `PFGetOptionsPrefix()`
498: @*/
499: PetscErrorCode PFSetOptionsPrefix(PF pf, const char prefix[])
500: {
501: PetscFunctionBegin;
503: PetscCall(PetscObjectSetOptionsPrefix((PetscObject)pf, prefix));
504: PetscFunctionReturn(PETSC_SUCCESS);
505: }
507: /*@
508: PFAppendOptionsPrefix - Appends to the prefix used for searching for all
509: `PF` options in the database.
511: Logically Collective
513: Input Parameters:
514: + pf - the `PF`
515: - prefix - the prefix string to prepend to all `PF` option requests
517: Level: advanced
519: Note:
520: A hyphen (-) must NOT be given at the beginning of the prefix name.
521: The first character of all runtime options is AUTOMATICALLY the
522: hyphen.
524: .seealso: [](ch_ksp), `PF`, `PFSetFromOptions()`, `PFSetOptionsPrefix()`, `PFGetOptionsPrefix()`
525: @*/
526: PetscErrorCode PFAppendOptionsPrefix(PF pf, const char prefix[])
527: {
528: PetscFunctionBegin;
530: PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)pf, prefix));
531: PetscFunctionReturn(PETSC_SUCCESS);
532: }
534: /*@
535: PFGetOptionsPrefix - Gets the prefix used for searching for all
536: `PF` options in the database.
538: Not Collective
540: Input Parameter:
541: . pf - the `PF`
543: Output Parameter:
544: . prefix - pointer to the prefix string used, is returned
546: Level: advanced
548: .seealso: [](ch_ksp), `PF`, `PFSetFromOptions()`, `PFSetOptionsPrefix()`, `PFAppendOptionsPrefix()`
549: @*/
550: PetscErrorCode PFGetOptionsPrefix(PF pf, const char *prefix[])
551: {
552: PetscFunctionBegin;
554: PetscAssertPointer(prefix, 2);
555: PetscCall(PetscObjectGetOptionsPrefix((PetscObject)pf, prefix));
556: PetscFunctionReturn(PETSC_SUCCESS);
557: }