xref: /petsc/src/vec/pf/impls/constant/const.c (revision f6e5521d3750b26fd5ddb3be4390fdb36f2b5fb2)
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++) y[i] = v;
14   PetscFunctionReturn(0);
15 }
16 
17 #undef __FUNCT__
18 #define __FUNCT__ "PFApplyVec_Constant"
19 PetscErrorCode PFApplyVec_Constant(void *value,Vec x,Vec y)
20 {
21   PetscErrorCode ierr;
22 
23   PetscFunctionBegin;
24   ierr = VecSet(y,*((PetscScalar*)value));CHKERRQ(ierr);
25   PetscFunctionReturn(0);
26 }
27 #undef __FUNCT__
28 #define __FUNCT__ "PFView_Constant"
29 PetscErrorCode PFView_Constant(void *value,PetscViewer viewer)
30 {
31   PetscErrorCode ierr;
32   PetscBool      iascii;
33 
34   PetscFunctionBegin;
35   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
36   if (iascii) {
37 #if !defined(PETSC_USE_COMPLEX)
38     ierr = PetscViewerASCIIPrintf(viewer,"Constant = %g\n",*(double*)value);CHKERRQ(ierr);
39 #else
40     ierr = PetscViewerASCIIPrintf(viewer,"Constant = %g + %gi\n",PetscRealPart(*(PetscScalar*)value),PetscImaginaryPart(*(PetscScalar*)value));CHKERRQ(ierr);
41 #endif
42   }
43   PetscFunctionReturn(0);
44 }
45 #undef __FUNCT__
46 #define __FUNCT__ "PFDestroy_Constant"
47 PetscErrorCode PFDestroy_Constant(void *value)
48 {
49   PetscErrorCode ierr;
50 
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;
81   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,PetscErrorCode (*function)(void*,PetscInt,const PetscScalar*,PetscScalar*))
96 {
97   PetscErrorCode ierr;
98 
99   PetscFunctionBegin;
100   ierr = PFSet(pf,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++) y[i] = x[i];
115   PetscFunctionReturn(0);
116 }
117 
118 #undef __FUNCT__
119 #define __FUNCT__ "PFApplyVec_Identity"
120 PetscErrorCode PFApplyVec_Identity(void *value,Vec x,Vec y)
121 {
122   PetscErrorCode ierr;
123 
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 
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) 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   ierr   = PetscMalloc(sizeof(PetscInt),&loc);CHKERRQ(ierr);
164   loc[0] = pf->dimout;
165   ierr   = PFSet(pf,PFApply_Identity,PFApplyVec_Identity,PFView_Identity,PFDestroy_Identity,loc);CHKERRQ(ierr);
166   PetscFunctionReturn(0);
167 }
168 EXTERN_C_END
169