xref: /petsc/src/vec/pf/impls/constant/const.c (revision 9371c9d470a9602b6d10a8bf50c9b2280a79e45a)
1 
2 #include <../src/vec/pf/pfimpl.h> /*I "petscpf.h" I*/
3 
4 static PetscErrorCode PFApply_Constant(void *value, PetscInt n, const PetscScalar *x, PetscScalar *y) {
5   PetscInt    i;
6   PetscScalar v = ((PetscScalar *)value)[0];
7 
8   PetscFunctionBegin;
9   n *= (PetscInt)PetscRealPart(((PetscScalar *)value)[1]);
10   for (i = 0; i < n; i++) y[i] = v;
11   PetscFunctionReturn(0);
12 }
13 
14 static PetscErrorCode PFApplyVec_Constant(void *value, Vec x, Vec y) {
15   PetscFunctionBegin;
16   PetscCall(VecSet(y, *((PetscScalar *)value)));
17   PetscFunctionReturn(0);
18 }
19 PetscErrorCode PFView_Constant(void *value, PetscViewer viewer) {
20   PetscBool iascii;
21 
22   PetscFunctionBegin;
23   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
24   if (iascii) {
25 #if !defined(PETSC_USE_COMPLEX)
26     PetscCall(PetscViewerASCIIPrintf(viewer, "Constant = %g\n", *(double *)value));
27 #else
28     PetscCall(PetscViewerASCIIPrintf(viewer, "Constant = %g + %gi\n", (double)PetscRealPart(*(PetscScalar *)value), (double)PetscImaginaryPart(*(PetscScalar *)value)));
29 #endif
30   }
31   PetscFunctionReturn(0);
32 }
33 static PetscErrorCode PFDestroy_Constant(void *value) {
34   PetscFunctionBegin;
35   PetscCall(PetscFree(value));
36   PetscFunctionReturn(0);
37 }
38 
39 static PetscErrorCode PFSetFromOptions_Constant(PF pf, PetscOptionItems *PetscOptionsObject) {
40   PetscScalar *value = (PetscScalar *)pf->data;
41 
42   PetscFunctionBegin;
43   PetscOptionsHeadBegin(PetscOptionsObject, "Constant function options");
44   PetscCall(PetscOptionsScalar("-pf_constant", "The constant value", "None", *value, value, NULL));
45   PetscOptionsHeadEnd();
46   PetscFunctionReturn(0);
47 }
48 
49 PETSC_EXTERN PetscErrorCode PFCreate_Constant(PF pf, void *value) {
50   PetscScalar *loc;
51 
52   PetscFunctionBegin;
53   PetscCall(PetscMalloc1(2, &loc));
54   if (value) loc[0] = *(PetscScalar *)value;
55   else loc[0] = 0.0;
56   loc[1] = pf->dimout;
57   PetscCall(PFSet(pf, PFApply_Constant, PFApplyVec_Constant, PFView_Constant, PFDestroy_Constant, loc));
58 
59   pf->ops->setfromoptions = PFSetFromOptions_Constant;
60   PetscFunctionReturn(0);
61 }
62 
63 /*typedef PetscErrorCode (*FCN)(void*,PetscInt,const PetscScalar*,PetscScalar*);  force argument to next function to not be extern C*/
64 
65 PETSC_EXTERN PetscErrorCode PFCreate_Quick(PF pf, PetscErrorCode (*function)(void *, PetscInt, const PetscScalar *, PetscScalar *)) {
66   PetscFunctionBegin;
67   PetscCall(PFSet(pf, function, NULL, NULL, NULL, NULL));
68   PetscFunctionReturn(0);
69 }
70 
71 /* -------------------------------------------------------------------------------------------------------------------*/
72 static PetscErrorCode PFApply_Identity(void *value, PetscInt n, const PetscScalar *x, PetscScalar *y) {
73   PetscInt i;
74 
75   PetscFunctionBegin;
76   n *= *(PetscInt *)value;
77   for (i = 0; i < n; i++) y[i] = x[i];
78   PetscFunctionReturn(0);
79 }
80 
81 static PetscErrorCode PFApplyVec_Identity(void *value, Vec x, Vec y) {
82   PetscFunctionBegin;
83   PetscCall(VecCopy(x, y));
84   PetscFunctionReturn(0);
85 }
86 static PetscErrorCode PFView_Identity(void *value, PetscViewer viewer) {
87   PetscBool iascii;
88 
89   PetscFunctionBegin;
90   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
91   if (iascii) { PetscCall(PetscViewerASCIIPrintf(viewer, "Identity function\n")); }
92   PetscFunctionReturn(0);
93 }
94 static PetscErrorCode PFDestroy_Identity(void *value) {
95   PetscFunctionBegin;
96   PetscCall(PetscFree(value));
97   PetscFunctionReturn(0);
98 }
99 
100 PETSC_EXTERN PetscErrorCode PFCreate_Identity(PF pf, void *value) {
101   PetscInt *loc;
102 
103   PetscFunctionBegin;
104   PetscCheck(pf->dimout == pf->dimin, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Input dimension must match output dimension for Identity function, dimin = %" PetscInt_FMT " dimout = %" PetscInt_FMT, pf->dimin, pf->dimout);
105   PetscCall(PetscNew(&loc));
106   loc[0] = pf->dimout;
107   PetscCall(PFSet(pf, PFApply_Identity, PFApplyVec_Identity, PFView_Identity, PFDestroy_Identity, loc));
108   PetscFunctionReturn(0);
109 }
110