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