xref: /petsc/src/vec/pf/impls/constant/const.c (revision 3e08d2bebf9958b2a7617e5d4703c5f9c1d28ec4)
1 
2 #include <../src/vec/pf/pfimpl.h>            /*I "petscpf.h" I*/
3 
4 #undef __FUNCT__
5 #define __FUNCT__ "PFApply_Constant"
6 PetscErrorCode PFApply_Constant(void *value,PetscInt n,const PetscScalar *x,PetscScalar *y)
7 {
8   PetscInt    i;
9   PetscScalar v = ((PetscScalar*)value)[0];
10 
11   PetscFunctionBegin;
12   n *= (PetscInt) PetscRealPart(((PetscScalar*)value)[1]);
13   for (i=0; i<n; i++) {
14     y[i] = v;
15   }
16   PetscFunctionReturn(0);
17 }
18 
19 #undef __FUNCT__
20 #define __FUNCT__ "PFApplyVec_Constant"
21 PetscErrorCode PFApplyVec_Constant(void *value,Vec x,Vec y)
22 {
23   PetscErrorCode ierr;
24   PetscFunctionBegin;
25   ierr = VecSet(y,*((PetscScalar*)value));CHKERRQ(ierr);
26   PetscFunctionReturn(0);
27 }
28 #undef __FUNCT__
29 #define __FUNCT__ "PFView_Constant"
30 PetscErrorCode PFView_Constant(void *value,PetscViewer viewer)
31 {
32   PetscErrorCode ierr;
33   PetscBool      iascii;
34 
35   PetscFunctionBegin;
36   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
37   if (iascii) {
38 #if !defined(PETSC_USE_COMPLEX)
39     ierr = PetscViewerASCIIPrintf(viewer,"Constant = %g\n",*(double*)value);CHKERRQ(ierr);
40 #else
41     ierr = PetscViewerASCIIPrintf(viewer,"Constant = %g + %gi\n",PetscRealPart(*(PetscScalar*)value),PetscImaginaryPart(*(PetscScalar*)value));CHKERRQ(ierr);
42 #endif
43   }
44   PetscFunctionReturn(0);
45 }
46 #undef __FUNCT__
47 #define __FUNCT__ "PFDestroy_Constant"
48 PetscErrorCode PFDestroy_Constant(void *value)
49 {
50   PetscErrorCode ierr;
51   PetscFunctionBegin;
52   ierr = PetscFree(value);CHKERRQ(ierr);
53   PetscFunctionReturn(0);
54 }
55 
56 #undef __FUNCT__
57 #define __FUNCT__ "PFSetFromOptions_Constant"
58 PetscErrorCode PFSetFromOptions_Constant(PF pf)
59 {
60   PetscErrorCode ierr;
61   PetscScalar    *value = (PetscScalar *)pf->data;
62 
63   PetscFunctionBegin;
64   ierr = PetscOptionsHead("Constant function options");CHKERRQ(ierr);
65     ierr = PetscOptionsScalar("-pf_constant","The constant value","None",*value,value,0);CHKERRQ(ierr);
66   ierr = PetscOptionsTail();CHKERRQ(ierr);
67   PetscFunctionReturn(0);
68 }
69 
70 EXTERN_C_BEGIN
71 #undef __FUNCT__
72 #define __FUNCT__ "PFCreate_Constant"
73 PetscErrorCode  PFCreate_Constant(PF pf,void *value)
74 {
75   PetscErrorCode ierr;
76   PetscScalar    *loc;
77 
78   PetscFunctionBegin;
79   ierr = PetscMalloc(2*sizeof(PetscScalar),&loc);CHKERRQ(ierr);
80   if (value) loc[0] = *(PetscScalar*)value; else loc[0] = 0.0;
81   loc[1] = pf->dimout;
82   ierr   = PFSet(pf,PFApply_Constant,PFApplyVec_Constant,PFView_Constant,PFDestroy_Constant,loc);CHKERRQ(ierr);
83 
84   pf->ops->setfromoptions = PFSetFromOptions_Constant;
85   PetscFunctionReturn(0);
86 }
87 EXTERN_C_END
88 
89 
90 typedef PetscErrorCode (*FCN)(void*,PetscInt,const PetscScalar*,PetscScalar*); /* force argument to next function to not be extern C*/
91 EXTERN_C_BEGIN
92 #undef __FUNCT__
93 #define __FUNCT__ "PFCreate_Quick"
94 PetscErrorCode  PFCreate_Quick(PF pf,void *function)
95 {
96   PetscErrorCode ierr;
97 
98   PetscFunctionBegin;
99   ierr = PFSet(pf,(FCN)function,0,0,0,0);CHKERRQ(ierr);
100   PetscFunctionReturn(0);
101 }
102 EXTERN_C_END
103 
104 /* -------------------------------------------------------------------------------------------------------------------*/
105 #undef __FUNCT__
106 #define __FUNCT__ "PFApply_Identity"
107 PetscErrorCode PFApply_Identity(void *value,PetscInt n,const PetscScalar *x,PetscScalar *y)
108 {
109   PetscInt    i;
110 
111   PetscFunctionBegin;
112   n *= *(PetscInt*)value;
113   for (i=0; i<n; i++) {
114     y[i] = x[i];
115   }
116   PetscFunctionReturn(0);
117 }
118 
119 #undef __FUNCT__
120 #define __FUNCT__ "PFApplyVec_Identity"
121 PetscErrorCode PFApplyVec_Identity(void *value,Vec x,Vec y)
122 {
123   PetscErrorCode ierr;
124   PetscFunctionBegin;
125   ierr = VecCopy(x,y);CHKERRQ(ierr);
126   PetscFunctionReturn(0);
127 }
128 #undef __FUNCT__
129 #define __FUNCT__ "PFView_Identity"
130 PetscErrorCode PFView_Identity(void *value,PetscViewer viewer)
131 {
132   PetscErrorCode ierr;
133   PetscBool      iascii;
134 
135   PetscFunctionBegin;
136   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
137   if (iascii) {
138     ierr = PetscViewerASCIIPrintf(viewer,"Identity function\n");CHKERRQ(ierr);
139   }
140   PetscFunctionReturn(0);
141 }
142 #undef __FUNCT__
143 #define __FUNCT__ "PFDestroy_Identity"
144 PetscErrorCode PFDestroy_Identity(void *value)
145 {
146   PetscErrorCode ierr;
147   PetscFunctionBegin;
148   ierr = PetscFree(value);CHKERRQ(ierr);
149   PetscFunctionReturn(0);
150 }
151 
152 EXTERN_C_BEGIN
153 #undef __FUNCT__
154 #define __FUNCT__ "PFCreate_Identity"
155 PetscErrorCode  PFCreate_Identity(PF pf,void *value)
156 {
157   PetscErrorCode ierr;
158   PetscInt       *loc;
159 
160   PetscFunctionBegin;
161   if (pf->dimout != pf->dimin) {
162     SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Input dimension must match output dimension for Identity function, dimin = %D dimout = %D\n",pf->dimin,pf->dimout);
163   }
164   ierr = PetscMalloc(sizeof(PetscInt),&loc);CHKERRQ(ierr);
165   loc[0] = pf->dimout;
166   ierr   = PFSet(pf,PFApply_Identity,PFApplyVec_Identity,PFView_Identity,PFDestroy_Identity,loc);CHKERRQ(ierr);
167   PetscFunctionReturn(0);
168 }
169 EXTERN_C_END
170