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