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