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