1 2 #include <../src/vec/pf/pfimpl.h> /*I "petscpf.h" I*/ 3 4 static PetscErrorCode PFApply_Constant(void *value, PetscInt n, const PetscScalar *x, PetscScalar *y) 5 { 6 PetscInt i; 7 PetscScalar v = ((PetscScalar *)value)[0]; 8 9 PetscFunctionBegin; 10 n *= (PetscInt)PetscRealPart(((PetscScalar *)value)[1]); 11 for (i = 0; i < n; i++) y[i] = v; 12 PetscFunctionReturn(0); 13 } 14 15 static PetscErrorCode PFApplyVec_Constant(void *value, Vec x, Vec y) 16 { 17 PetscFunctionBegin; 18 PetscCall(VecSet(y, *((PetscScalar *)value))); 19 PetscFunctionReturn(0); 20 } 21 PetscErrorCode PFView_Constant(void *value, PetscViewer viewer) 22 { 23 PetscBool iascii; 24 25 PetscFunctionBegin; 26 PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii)); 27 if (iascii) { 28 #if !defined(PETSC_USE_COMPLEX) 29 PetscCall(PetscViewerASCIIPrintf(viewer, "Constant = %g\n", *(double *)value)); 30 #else 31 PetscCall(PetscViewerASCIIPrintf(viewer, "Constant = %g + %gi\n", (double)PetscRealPart(*(PetscScalar *)value), (double)PetscImaginaryPart(*(PetscScalar *)value))); 32 #endif 33 } 34 PetscFunctionReturn(0); 35 } 36 static PetscErrorCode PFDestroy_Constant(void *value) 37 { 38 PetscFunctionBegin; 39 PetscCall(PetscFree(value)); 40 PetscFunctionReturn(0); 41 } 42 43 static PetscErrorCode PFSetFromOptions_Constant(PF pf, PetscOptionItems *PetscOptionsObject) 44 { 45 PetscScalar *value = (PetscScalar *)pf->data; 46 47 PetscFunctionBegin; 48 PetscOptionsHeadBegin(PetscOptionsObject, "Constant function options"); 49 PetscCall(PetscOptionsScalar("-pf_constant", "The constant value", "None", *value, value, NULL)); 50 PetscOptionsHeadEnd(); 51 PetscFunctionReturn(0); 52 } 53 54 PETSC_EXTERN PetscErrorCode PFCreate_Constant(PF pf, void *value) 55 { 56 PetscScalar *loc; 57 58 PetscFunctionBegin; 59 PetscCall(PetscMalloc1(2, &loc)); 60 if (value) loc[0] = *(PetscScalar *)value; 61 else loc[0] = 0.0; 62 loc[1] = pf->dimout; 63 PetscCall(PFSet(pf, PFApply_Constant, PFApplyVec_Constant, PFView_Constant, PFDestroy_Constant, loc)); 64 65 pf->ops->setfromoptions = PFSetFromOptions_Constant; 66 PetscFunctionReturn(0); 67 } 68 69 /*typedef PetscErrorCode (*FCN)(void*,PetscInt,const PetscScalar*,PetscScalar*); force argument to next function to not be extern C*/ 70 71 PETSC_EXTERN PetscErrorCode PFCreate_Quick(PF pf, PetscErrorCode (*function)(void *, PetscInt, const PetscScalar *, PetscScalar *)) 72 { 73 PetscFunctionBegin; 74 PetscCall(PFSet(pf, function, NULL, NULL, NULL, NULL)); 75 PetscFunctionReturn(0); 76 } 77 78 /* -------------------------------------------------------------------------------------------------------------------*/ 79 static PetscErrorCode PFApply_Identity(void *value, PetscInt n, const PetscScalar *x, PetscScalar *y) 80 { 81 PetscInt i; 82 83 PetscFunctionBegin; 84 n *= *(PetscInt *)value; 85 for (i = 0; i < n; i++) y[i] = x[i]; 86 PetscFunctionReturn(0); 87 } 88 89 static PetscErrorCode PFApplyVec_Identity(void *value, Vec x, Vec y) 90 { 91 PetscFunctionBegin; 92 PetscCall(VecCopy(x, y)); 93 PetscFunctionReturn(0); 94 } 95 static PetscErrorCode PFView_Identity(void *value, PetscViewer viewer) 96 { 97 PetscBool iascii; 98 99 PetscFunctionBegin; 100 PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii)); 101 if (iascii) PetscCall(PetscViewerASCIIPrintf(viewer, "Identity function\n")); 102 PetscFunctionReturn(0); 103 } 104 static PetscErrorCode PFDestroy_Identity(void *value) 105 { 106 PetscFunctionBegin; 107 PetscCall(PetscFree(value)); 108 PetscFunctionReturn(0); 109 } 110 111 PETSC_EXTERN PetscErrorCode PFCreate_Identity(PF pf, void *value) 112 { 113 PetscInt *loc; 114 115 PetscFunctionBegin; 116 PetscCheck(pf->dimout == pf->dimin, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Input dimension must match output dimension for Identity function, dimin = %" PetscInt_FMT " dimout = %" PetscInt_FMT, pf->dimin, pf->dimout); 117 PetscCall(PetscNew(&loc)); 118 loc[0] = pf->dimout; 119 PetscCall(PFSet(pf, PFApply_Identity, PFApplyVec_Identity, PFView_Identity, PFDestroy_Identity, loc)); 120 PetscFunctionReturn(0); 121 } 122