1292f8084SBarry Smith 2c6db04a5SJed Brown #include <../src/vec/pf/pfimpl.h> /*I "petscpf.h" I*/ 3292f8084SBarry Smith 49371c9d4SSatish Balay static PetscErrorCode PFApply_Constant(void *value, PetscInt n, const PetscScalar *x, PetscScalar *y) { 5292f8084SBarry Smith PetscInt i; 6292f8084SBarry Smith PetscScalar v = ((PetscScalar *)value)[0]; 7292f8084SBarry Smith 8292f8084SBarry Smith PetscFunctionBegin; 9292f8084SBarry Smith n *= (PetscInt)PetscRealPart(((PetscScalar *)value)[1]); 10f6e5521dSKarl Rupp for (i = 0; i < n; i++) y[i] = v; 11292f8084SBarry Smith PetscFunctionReturn(0); 12292f8084SBarry Smith } 13292f8084SBarry Smith 149371c9d4SSatish Balay static PetscErrorCode PFApplyVec_Constant(void *value, Vec x, Vec y) { 15292f8084SBarry Smith PetscFunctionBegin; 169566063dSJacob Faibussowitsch PetscCall(VecSet(y, *((PetscScalar *)value))); 17292f8084SBarry Smith PetscFunctionReturn(0); 18292f8084SBarry Smith } 199371c9d4SSatish Balay PetscErrorCode PFView_Constant(void *value, PetscViewer viewer) { 20ace3abfcSBarry Smith PetscBool iascii; 21292f8084SBarry Smith 22292f8084SBarry Smith PetscFunctionBegin; 239566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii)); 24292f8084SBarry Smith if (iascii) { 25292f8084SBarry Smith #if !defined(PETSC_USE_COMPLEX) 269566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Constant = %g\n", *(double *)value)); 27292f8084SBarry Smith #else 282a3335c0SJose E. Roman PetscCall(PetscViewerASCIIPrintf(viewer, "Constant = %g + %gi\n", (double)PetscRealPart(*(PetscScalar *)value), (double)PetscImaginaryPart(*(PetscScalar *)value))); 29292f8084SBarry Smith #endif 30292f8084SBarry Smith } 31292f8084SBarry Smith PetscFunctionReturn(0); 32292f8084SBarry Smith } 339371c9d4SSatish Balay static PetscErrorCode PFDestroy_Constant(void *value) { 34292f8084SBarry Smith PetscFunctionBegin; 359566063dSJacob Faibussowitsch PetscCall(PetscFree(value)); 36292f8084SBarry Smith PetscFunctionReturn(0); 37292f8084SBarry Smith } 38292f8084SBarry Smith 399371c9d4SSatish Balay static PetscErrorCode PFSetFromOptions_Constant(PF pf, PetscOptionItems *PetscOptionsObject) { 40292f8084SBarry Smith PetscScalar *value = (PetscScalar *)pf->data; 41292f8084SBarry Smith 42292f8084SBarry Smith PetscFunctionBegin; 43d0609cedSBarry Smith PetscOptionsHeadBegin(PetscOptionsObject, "Constant function options"); 449566063dSJacob Faibussowitsch PetscCall(PetscOptionsScalar("-pf_constant", "The constant value", "None", *value, value, NULL)); 45d0609cedSBarry Smith PetscOptionsHeadEnd(); 46292f8084SBarry Smith PetscFunctionReturn(0); 47292f8084SBarry Smith } 48292f8084SBarry Smith 499371c9d4SSatish Balay PETSC_EXTERN PetscErrorCode PFCreate_Constant(PF pf, void *value) { 50292f8084SBarry Smith PetscScalar *loc; 51292f8084SBarry Smith 52292f8084SBarry Smith PetscFunctionBegin; 539566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(2, &loc)); 54f6e5521dSKarl Rupp if (value) loc[0] = *(PetscScalar *)value; 55f6e5521dSKarl Rupp else loc[0] = 0.0; 56292f8084SBarry Smith loc[1] = pf->dimout; 579566063dSJacob Faibussowitsch PetscCall(PFSet(pf, PFApply_Constant, PFApplyVec_Constant, PFView_Constant, PFDestroy_Constant, loc)); 58292f8084SBarry Smith 59292f8084SBarry Smith pf->ops->setfromoptions = PFSetFromOptions_Constant; 60292f8084SBarry Smith PetscFunctionReturn(0); 61292f8084SBarry Smith } 62292f8084SBarry Smith 635c8f6a95SKarl Rupp /*typedef PetscErrorCode (*FCN)(void*,PetscInt,const PetscScalar*,PetscScalar*); force argument to next function to not be extern C*/ 646ac5842eSBarry Smith 659371c9d4SSatish Balay PETSC_EXTERN PetscErrorCode PFCreate_Quick(PF pf, PetscErrorCode (*function)(void *, PetscInt, const PetscScalar *, PetscScalar *)) { 66292f8084SBarry Smith PetscFunctionBegin; 679566063dSJacob Faibussowitsch PetscCall(PFSet(pf, function, NULL, NULL, NULL, NULL)); 68292f8084SBarry Smith PetscFunctionReturn(0); 69292f8084SBarry Smith } 70292f8084SBarry Smith 71292f8084SBarry Smith /* -------------------------------------------------------------------------------------------------------------------*/ 729371c9d4SSatish Balay static PetscErrorCode PFApply_Identity(void *value, PetscInt n, const PetscScalar *x, PetscScalar *y) { 73292f8084SBarry Smith PetscInt i; 74292f8084SBarry Smith 75292f8084SBarry Smith PetscFunctionBegin; 76292f8084SBarry Smith n *= *(PetscInt *)value; 77f6e5521dSKarl Rupp for (i = 0; i < n; i++) y[i] = x[i]; 78292f8084SBarry Smith PetscFunctionReturn(0); 79292f8084SBarry Smith } 80292f8084SBarry Smith 819371c9d4SSatish Balay static PetscErrorCode PFApplyVec_Identity(void *value, Vec x, Vec y) { 82292f8084SBarry Smith PetscFunctionBegin; 839566063dSJacob Faibussowitsch PetscCall(VecCopy(x, y)); 84292f8084SBarry Smith PetscFunctionReturn(0); 85292f8084SBarry Smith } 869371c9d4SSatish Balay static PetscErrorCode PFView_Identity(void *value, PetscViewer viewer) { 87ace3abfcSBarry Smith PetscBool iascii; 88292f8084SBarry Smith 89292f8084SBarry Smith PetscFunctionBegin; 909566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii)); 91*48a46eb9SPierre Jolivet if (iascii) PetscCall(PetscViewerASCIIPrintf(viewer, "Identity function\n")); 92292f8084SBarry Smith PetscFunctionReturn(0); 93292f8084SBarry Smith } 949371c9d4SSatish Balay static PetscErrorCode PFDestroy_Identity(void *value) { 95292f8084SBarry Smith PetscFunctionBegin; 969566063dSJacob Faibussowitsch PetscCall(PetscFree(value)); 97292f8084SBarry Smith PetscFunctionReturn(0); 98292f8084SBarry Smith } 99292f8084SBarry Smith 1009371c9d4SSatish Balay PETSC_EXTERN PetscErrorCode PFCreate_Identity(PF pf, void *value) { 101292f8084SBarry Smith PetscInt *loc; 102292f8084SBarry Smith 103292f8084SBarry Smith PetscFunctionBegin; 10408401ef6SPierre Jolivet 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); 1059566063dSJacob Faibussowitsch PetscCall(PetscNew(&loc)); 106292f8084SBarry Smith loc[0] = pf->dimout; 1079566063dSJacob Faibussowitsch PetscCall(PFSet(pf, PFApply_Identity, PFApplyVec_Identity, PFView_Identity, PFDestroy_Identity, loc)); 108292f8084SBarry Smith PetscFunctionReturn(0); 109292f8084SBarry Smith } 110