xref: /petsc/src/vec/pf/impls/constant/const.c (revision 77431f27ef6a796c710ee53da3b02f01a7247aeb)
1292f8084SBarry Smith 
258d819adSvictorle #include "src/vec/pf/pfimpl.h"            /*I "petscpf.h" I*/
3292f8084SBarry Smith 
4292f8084SBarry Smith #undef __FUNCT__
5292f8084SBarry Smith #define __FUNCT__ "PFApply_Constant"
6292f8084SBarry Smith PetscErrorCode PFApply_Constant(void *value,PetscInt n,PetscScalar *x,PetscScalar *y)
7292f8084SBarry Smith {
8292f8084SBarry Smith   PetscInt    i;
9292f8084SBarry Smith   PetscScalar v = ((PetscScalar*)value)[0];
10292f8084SBarry Smith 
11292f8084SBarry Smith   PetscFunctionBegin;
12292f8084SBarry Smith   n *= (PetscInt) PetscRealPart(((PetscScalar*)value)[1]);
13292f8084SBarry Smith   for (i=0; i<n; i++) {
14292f8084SBarry Smith     y[i] = v;
15292f8084SBarry Smith   }
16292f8084SBarry Smith   PetscFunctionReturn(0);
17292f8084SBarry Smith }
18292f8084SBarry Smith 
19292f8084SBarry Smith #undef __FUNCT__
20292f8084SBarry Smith #define __FUNCT__ "PFApplyVec_Constant"
21292f8084SBarry Smith PetscErrorCode PFApplyVec_Constant(void *value,Vec x,Vec y)
22292f8084SBarry Smith {
23292f8084SBarry Smith   PetscErrorCode ierr;
24292f8084SBarry Smith   PetscFunctionBegin;
25292f8084SBarry Smith   ierr = VecSet((PetscScalar*)value,y);CHKERRQ(ierr);
26292f8084SBarry Smith   PetscFunctionReturn(0);
27292f8084SBarry Smith }
28292f8084SBarry Smith #undef __FUNCT__
29292f8084SBarry Smith #define __FUNCT__ "PFView_Constant"
30292f8084SBarry Smith PetscErrorCode PFView_Constant(void *value,PetscViewer viewer)
31292f8084SBarry Smith {
32292f8084SBarry Smith   PetscErrorCode ierr;
33292f8084SBarry Smith   PetscTruth     iascii;
34292f8084SBarry Smith 
35292f8084SBarry Smith   PetscFunctionBegin;
36292f8084SBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
37292f8084SBarry Smith   if (iascii) {
38292f8084SBarry Smith #if !defined(PETSC_USE_COMPLEX)
39292f8084SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"Constant = %g\n",*(double*)value);CHKERRQ(ierr);
40292f8084SBarry Smith #else
41292f8084SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"Constant = %g + %gi\n",PetscRealPart(*(PetscScalar*)value),PetscImaginaryPart(*(PetscScalar*)value));CHKERRQ(ierr);
42292f8084SBarry Smith #endif
43292f8084SBarry Smith   }
44292f8084SBarry Smith   PetscFunctionReturn(0);
45292f8084SBarry Smith }
46292f8084SBarry Smith #undef __FUNCT__
47292f8084SBarry Smith #define __FUNCT__ "PFDestroy_Constant"
48292f8084SBarry Smith PetscErrorCode PFDestroy_Constant(void *value)
49292f8084SBarry Smith {
50292f8084SBarry Smith   PetscErrorCode ierr;
51292f8084SBarry Smith   PetscFunctionBegin;
52292f8084SBarry Smith   ierr = PetscFree(value);CHKERRQ(ierr);
53292f8084SBarry Smith   PetscFunctionReturn(0);
54292f8084SBarry Smith }
55292f8084SBarry Smith 
56292f8084SBarry Smith #undef __FUNCT__
57292f8084SBarry Smith #define __FUNCT__ "PFSetFromOptions_Constant"
58292f8084SBarry Smith PetscErrorCode PFSetFromOptions_Constant(PF pf)
59292f8084SBarry Smith {
60292f8084SBarry Smith   PetscErrorCode ierr;
61292f8084SBarry Smith   PetscScalar    *value = (PetscScalar *)pf->data;
62292f8084SBarry Smith 
63292f8084SBarry Smith   PetscFunctionBegin;
64292f8084SBarry Smith   ierr = PetscOptionsHead("Constant function options");CHKERRQ(ierr);
65292f8084SBarry Smith     ierr = PetscOptionsScalar("-pf_constant","The constant value","None",*value,value,0);CHKERRQ(ierr);
66292f8084SBarry Smith   ierr = PetscOptionsTail();CHKERRQ(ierr);
67292f8084SBarry Smith   PetscFunctionReturn(0);
68292f8084SBarry Smith }
69292f8084SBarry Smith 
70292f8084SBarry Smith EXTERN_C_BEGIN
71292f8084SBarry Smith #undef __FUNCT__
72292f8084SBarry Smith #define __FUNCT__ "PFCreate_Constant"
73292f8084SBarry Smith PetscErrorCode PFCreate_Constant(PF pf,void *value)
74292f8084SBarry Smith {
75292f8084SBarry Smith   PetscErrorCode ierr;
76292f8084SBarry Smith   PetscScalar    *loc;
77292f8084SBarry Smith 
78292f8084SBarry Smith   PetscFunctionBegin;
79292f8084SBarry Smith   ierr = PetscMalloc(2*sizeof(PetscScalar),&loc);CHKERRQ(ierr);
80292f8084SBarry Smith   if (value) loc[0] = *(PetscScalar*)value; else loc[0] = 0.0;
81292f8084SBarry Smith   loc[1] = pf->dimout;
82292f8084SBarry Smith   ierr   = PFSet(pf,PFApply_Constant,PFApplyVec_Constant,PFView_Constant,PFDestroy_Constant,loc);CHKERRQ(ierr);
83292f8084SBarry Smith 
84292f8084SBarry Smith   pf->ops->setfromoptions = PFSetFromOptions_Constant;
85292f8084SBarry Smith   PetscFunctionReturn(0);
86292f8084SBarry Smith }
87292f8084SBarry Smith EXTERN_C_END
88292f8084SBarry Smith 
89292f8084SBarry Smith 
90292f8084SBarry Smith typedef PetscErrorCode (*FCN)(void*,PetscInt,PetscScalar*,PetscScalar*); /* force argument to next function to not be extern C*/
91292f8084SBarry Smith EXTERN_C_BEGIN
92292f8084SBarry Smith #undef __FUNCT__
93292f8084SBarry Smith #define __FUNCT__ "PFCreate_Quick"
94292f8084SBarry Smith PetscErrorCode PFCreate_Quick(PF pf,void *function)
95292f8084SBarry Smith {
96292f8084SBarry Smith   PetscErrorCode ierr;
97292f8084SBarry Smith 
98292f8084SBarry Smith   PetscFunctionBegin;
99292f8084SBarry Smith   ierr = PFSet(pf,(FCN)function,0,0,0,0);CHKERRQ(ierr);
100292f8084SBarry Smith   PetscFunctionReturn(0);
101292f8084SBarry Smith }
102292f8084SBarry Smith EXTERN_C_END
103292f8084SBarry Smith 
104292f8084SBarry Smith /* -------------------------------------------------------------------------------------------------------------------*/
105292f8084SBarry Smith #undef __FUNCT__
106292f8084SBarry Smith #define __FUNCT__ "PFApply_Identity"
107292f8084SBarry Smith PetscErrorCode PFApply_Identity(void *value,PetscInt n,PetscScalar *x,PetscScalar *y)
108292f8084SBarry Smith {
109292f8084SBarry Smith   PetscInt    i;
110292f8084SBarry Smith 
111292f8084SBarry Smith   PetscFunctionBegin;
112292f8084SBarry Smith   n *= *(PetscInt*)value;
113292f8084SBarry Smith   for (i=0; i<n; i++) {
114292f8084SBarry Smith     y[i] = x[i];
115292f8084SBarry Smith   }
116292f8084SBarry Smith   PetscFunctionReturn(0);
117292f8084SBarry Smith }
118292f8084SBarry Smith 
119292f8084SBarry Smith #undef __FUNCT__
120292f8084SBarry Smith #define __FUNCT__ "PFApplyVec_Identity"
121292f8084SBarry Smith PetscErrorCode PFApplyVec_Identity(void *value,Vec x,Vec y)
122292f8084SBarry Smith {
123292f8084SBarry Smith   PetscErrorCode ierr;
124292f8084SBarry Smith   PetscFunctionBegin;
125292f8084SBarry Smith   ierr = VecCopy(x,y);CHKERRQ(ierr);
126292f8084SBarry Smith   PetscFunctionReturn(0);
127292f8084SBarry Smith }
128292f8084SBarry Smith #undef __FUNCT__
129292f8084SBarry Smith #define __FUNCT__ "PFView_Identity"
130292f8084SBarry Smith PetscErrorCode PFView_Identity(void *value,PetscViewer viewer)
131292f8084SBarry Smith {
132292f8084SBarry Smith   PetscErrorCode ierr;
133292f8084SBarry Smith   PetscTruth     iascii;
134292f8084SBarry Smith 
135292f8084SBarry Smith   PetscFunctionBegin;
136292f8084SBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
137292f8084SBarry Smith   if (iascii) {
138292f8084SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"Identity function\n");CHKERRQ(ierr);
139292f8084SBarry Smith   }
140292f8084SBarry Smith   PetscFunctionReturn(0);
141292f8084SBarry Smith }
142292f8084SBarry Smith #undef __FUNCT__
143292f8084SBarry Smith #define __FUNCT__ "PFDestroy_Identity"
144292f8084SBarry Smith PetscErrorCode PFDestroy_Identity(void *value)
145292f8084SBarry Smith {
146292f8084SBarry Smith   PetscErrorCode ierr;
147292f8084SBarry Smith   PetscFunctionBegin;
148292f8084SBarry Smith   ierr = PetscFree(value);CHKERRQ(ierr);
149292f8084SBarry Smith   PetscFunctionReturn(0);
150292f8084SBarry Smith }
151292f8084SBarry Smith 
152292f8084SBarry Smith EXTERN_C_BEGIN
153292f8084SBarry Smith #undef __FUNCT__
154292f8084SBarry Smith #define __FUNCT__ "PFCreate_Identity"
155292f8084SBarry Smith PetscErrorCode PFCreate_Identity(PF pf,void *value)
156292f8084SBarry Smith {
157292f8084SBarry Smith   PetscErrorCode ierr;
158292f8084SBarry Smith   PetscInt       *loc;
159292f8084SBarry Smith 
160292f8084SBarry Smith   PetscFunctionBegin;
161292f8084SBarry Smith   if (pf->dimout != pf->dimin) {
162*77431f27SBarry Smith     SETERRQ2(PETSC_ERR_ARG_SIZ,"Input dimension must match output dimension for Identity function, dimin = %D dimout = %D\n",pf->dimin,pf->dimout);
163292f8084SBarry Smith   }
164292f8084SBarry Smith   ierr = PetscMalloc(sizeof(PetscInt),&loc);CHKERRQ(ierr);
165292f8084SBarry Smith   loc[0] = pf->dimout;
166292f8084SBarry Smith   ierr   = PFSet(pf,PFApply_Identity,PFApplyVec_Identity,PFView_Identity,PFDestroy_Identity,loc);CHKERRQ(ierr);
167292f8084SBarry Smith   PetscFunctionReturn(0);
168292f8084SBarry Smith }
169292f8084SBarry Smith EXTERN_C_END
170