xref: /petsc/src/vec/pf/impls/constant/const.c (revision e6e75211d226c622f451867f53ce5d558649ff4f)
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(PetscOptions *PetscOptionsObject,PF pf)
59 {
60   PetscErrorCode ierr;
61   PetscScalar    *value = (PetscScalar*)pf->data;
62 
63   PetscFunctionBegin;
64   ierr = PetscOptionsHead(PetscOptionsObject,"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 #undef __FUNCT__
71 #define __FUNCT__ "PFCreate_Constant"
72 PETSC_EXTERN PetscErrorCode PFCreate_Constant(PF pf,void *value)
73 {
74   PetscErrorCode ierr;
75   PetscScalar    *loc;
76 
77   PetscFunctionBegin;
78   ierr = PetscMalloc1(2,&loc);CHKERRQ(ierr);
79   if (value) loc[0] = *(PetscScalar*)value;
80   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 
88 /*typedef PetscErrorCode (*FCN)(void*,PetscInt,const PetscScalar*,PetscScalar*);  force argument to next function to not be extern C*/
89 
90 #undef __FUNCT__
91 #define __FUNCT__ "PFCreate_Quick"
92 PETSC_EXTERN PetscErrorCode PFCreate_Quick(PF pf,PetscErrorCode (*function)(void*,PetscInt,const PetscScalar*,PetscScalar*))
93 {
94   PetscErrorCode ierr;
95 
96   PetscFunctionBegin;
97   ierr = PFSet(pf,function,0,0,0,0);CHKERRQ(ierr);
98   PetscFunctionReturn(0);
99 }
100 
101 /* -------------------------------------------------------------------------------------------------------------------*/
102 #undef __FUNCT__
103 #define __FUNCT__ "PFApply_Identity"
104 PetscErrorCode PFApply_Identity(void *value,PetscInt n,const PetscScalar *x,PetscScalar *y)
105 {
106   PetscInt i;
107 
108   PetscFunctionBegin;
109   n *= *(PetscInt*)value;
110   for (i=0; i<n; i++) y[i] = x[i];
111   PetscFunctionReturn(0);
112 }
113 
114 #undef __FUNCT__
115 #define __FUNCT__ "PFApplyVec_Identity"
116 PetscErrorCode PFApplyVec_Identity(void *value,Vec x,Vec y)
117 {
118   PetscErrorCode ierr;
119 
120   PetscFunctionBegin;
121   ierr = VecCopy(x,y);CHKERRQ(ierr);
122   PetscFunctionReturn(0);
123 }
124 #undef __FUNCT__
125 #define __FUNCT__ "PFView_Identity"
126 PetscErrorCode PFView_Identity(void *value,PetscViewer viewer)
127 {
128   PetscErrorCode ierr;
129   PetscBool      iascii;
130 
131   PetscFunctionBegin;
132   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
133   if (iascii) {
134     ierr = PetscViewerASCIIPrintf(viewer,"Identity function\n");CHKERRQ(ierr);
135   }
136   PetscFunctionReturn(0);
137 }
138 #undef __FUNCT__
139 #define __FUNCT__ "PFDestroy_Identity"
140 PetscErrorCode PFDestroy_Identity(void *value)
141 {
142   PetscErrorCode ierr;
143 
144   PetscFunctionBegin;
145   ierr = PetscFree(value);CHKERRQ(ierr);
146   PetscFunctionReturn(0);
147 }
148 
149 #undef __FUNCT__
150 #define __FUNCT__ "PFCreate_Identity"
151 PETSC_EXTERN PetscErrorCode PFCreate_Identity(PF pf,void *value)
152 {
153   PetscErrorCode ierr;
154   PetscInt       *loc;
155 
156   PetscFunctionBegin;
157   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);
158   ierr   = PetscMalloc(sizeof(PetscInt),&loc);CHKERRQ(ierr);
159   loc[0] = pf->dimout;
160   ierr   = PFSet(pf,PFApply_Identity,PFApplyVec_Identity,PFView_Identity,PFDestroy_Identity,loc);CHKERRQ(ierr);
161   PetscFunctionReturn(0);
162 }
163