1292f8084SBarry Smith 2c6db04a5SJed Brown #include <../src/vec/pf/pfimpl.h> /*I "petscpf.h" I*/ 3292f8084SBarry Smith 4e0877f53SBarry Smith static PetscErrorCode PFApply_Constant(void *value,PetscInt n,const PetscScalar *x,PetscScalar *y) 5292f8084SBarry Smith { 6292f8084SBarry Smith PetscInt i; 7292f8084SBarry Smith PetscScalar v = ((PetscScalar*)value)[0]; 8292f8084SBarry Smith 9292f8084SBarry Smith PetscFunctionBegin; 10292f8084SBarry Smith n *= (PetscInt) PetscRealPart(((PetscScalar*)value)[1]); 11f6e5521dSKarl Rupp for (i=0; i<n; i++) y[i] = v; 12292f8084SBarry Smith PetscFunctionReturn(0); 13292f8084SBarry Smith } 14292f8084SBarry Smith 15e0877f53SBarry Smith static PetscErrorCode PFApplyVec_Constant(void *value,Vec x,Vec y) 16292f8084SBarry Smith { 17292f8084SBarry Smith PetscFunctionBegin; 189566063dSJacob Faibussowitsch PetscCall(VecSet(y,*((PetscScalar*)value))); 19292f8084SBarry Smith PetscFunctionReturn(0); 20292f8084SBarry Smith } 21292f8084SBarry Smith PetscErrorCode PFView_Constant(void *value,PetscViewer viewer) 22292f8084SBarry Smith { 23ace3abfcSBarry Smith PetscBool iascii; 24292f8084SBarry Smith 25292f8084SBarry Smith PetscFunctionBegin; 269566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii)); 27292f8084SBarry Smith if (iascii) { 28292f8084SBarry Smith #if !defined(PETSC_USE_COMPLEX) 299566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer,"Constant = %g\n",*(double*)value)); 30292f8084SBarry Smith #else 319566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer,"Constant = %g + %gi\n",PetscRealPart(*(PetscScalar*)value),PetscImaginaryPart(*(PetscScalar*)value))); 32292f8084SBarry Smith #endif 33292f8084SBarry Smith } 34292f8084SBarry Smith PetscFunctionReturn(0); 35292f8084SBarry Smith } 36e0877f53SBarry Smith static PetscErrorCode PFDestroy_Constant(void *value) 37292f8084SBarry Smith { 38292f8084SBarry Smith PetscFunctionBegin; 399566063dSJacob Faibussowitsch PetscCall(PetscFree(value)); 40292f8084SBarry Smith PetscFunctionReturn(0); 41292f8084SBarry Smith } 42292f8084SBarry Smith 43e0877f53SBarry Smith static PetscErrorCode PFSetFromOptions_Constant(PetscOptionItems *PetscOptionsObject,PF pf) 44292f8084SBarry Smith { 45292f8084SBarry Smith PetscScalar *value = (PetscScalar*)pf->data; 46292f8084SBarry Smith 47292f8084SBarry Smith PetscFunctionBegin; 489566063dSJacob Faibussowitsch PetscCall(PetscOptionsHead(PetscOptionsObject,"Constant function options")); 499566063dSJacob Faibussowitsch PetscCall(PetscOptionsScalar("-pf_constant","The constant value","None",*value,value,NULL)); 509566063dSJacob Faibussowitsch PetscCall(PetscOptionsTail()); 51292f8084SBarry Smith PetscFunctionReturn(0); 52292f8084SBarry Smith } 53292f8084SBarry Smith 548cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PFCreate_Constant(PF pf,void *value) 55292f8084SBarry Smith { 56292f8084SBarry Smith PetscScalar *loc; 57292f8084SBarry Smith 58292f8084SBarry Smith PetscFunctionBegin; 599566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(2,&loc)); 60f6e5521dSKarl Rupp if (value) loc[0] = *(PetscScalar*)value; 61f6e5521dSKarl Rupp else loc[0] = 0.0; 62292f8084SBarry Smith loc[1] = pf->dimout; 639566063dSJacob Faibussowitsch PetscCall(PFSet(pf,PFApply_Constant,PFApplyVec_Constant,PFView_Constant,PFDestroy_Constant,loc)); 64292f8084SBarry Smith 65292f8084SBarry Smith pf->ops->setfromoptions = PFSetFromOptions_Constant; 66292f8084SBarry Smith PetscFunctionReturn(0); 67292f8084SBarry Smith } 68292f8084SBarry Smith 695c8f6a95SKarl Rupp /*typedef PetscErrorCode (*FCN)(void*,PetscInt,const PetscScalar*,PetscScalar*); force argument to next function to not be extern C*/ 706ac5842eSBarry Smith 718cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PFCreate_Quick(PF pf,PetscErrorCode (*function)(void*,PetscInt,const PetscScalar*,PetscScalar*)) 72292f8084SBarry Smith { 73292f8084SBarry Smith PetscFunctionBegin; 749566063dSJacob Faibussowitsch PetscCall(PFSet(pf,function,NULL,NULL,NULL,NULL)); 75292f8084SBarry Smith PetscFunctionReturn(0); 76292f8084SBarry Smith } 77292f8084SBarry Smith 78292f8084SBarry Smith /* -------------------------------------------------------------------------------------------------------------------*/ 79e0877f53SBarry Smith static PetscErrorCode PFApply_Identity(void *value,PetscInt n,const PetscScalar *x,PetscScalar *y) 80292f8084SBarry Smith { 81292f8084SBarry Smith PetscInt i; 82292f8084SBarry Smith 83292f8084SBarry Smith PetscFunctionBegin; 84292f8084SBarry Smith n *= *(PetscInt*)value; 85f6e5521dSKarl Rupp for (i=0; i<n; i++) y[i] = x[i]; 86292f8084SBarry Smith PetscFunctionReturn(0); 87292f8084SBarry Smith } 88292f8084SBarry Smith 89e0877f53SBarry Smith static PetscErrorCode PFApplyVec_Identity(void *value,Vec x,Vec y) 90292f8084SBarry Smith { 91292f8084SBarry Smith PetscFunctionBegin; 929566063dSJacob Faibussowitsch PetscCall(VecCopy(x,y)); 93292f8084SBarry Smith PetscFunctionReturn(0); 94292f8084SBarry Smith } 95e0877f53SBarry Smith static PetscErrorCode PFView_Identity(void *value,PetscViewer viewer) 96292f8084SBarry Smith { 97ace3abfcSBarry Smith PetscBool iascii; 98292f8084SBarry Smith 99292f8084SBarry Smith PetscFunctionBegin; 1009566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii)); 101292f8084SBarry Smith if (iascii) { 1029566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer,"Identity function\n")); 103292f8084SBarry Smith } 104292f8084SBarry Smith PetscFunctionReturn(0); 105292f8084SBarry Smith } 106e0877f53SBarry Smith static PetscErrorCode PFDestroy_Identity(void *value) 107292f8084SBarry Smith { 108292f8084SBarry Smith PetscFunctionBegin; 1099566063dSJacob Faibussowitsch PetscCall(PetscFree(value)); 110292f8084SBarry Smith PetscFunctionReturn(0); 111292f8084SBarry Smith } 112292f8084SBarry Smith 1138cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PFCreate_Identity(PF pf,void *value) 114292f8084SBarry Smith { 115292f8084SBarry Smith PetscInt *loc; 116292f8084SBarry Smith 117292f8084SBarry Smith PetscFunctionBegin; 118*08401ef6SPierre 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); 1199566063dSJacob Faibussowitsch PetscCall(PetscNew(&loc)); 120292f8084SBarry Smith loc[0] = pf->dimout; 1219566063dSJacob Faibussowitsch PetscCall(PFSet(pf,PFApply_Identity,PFApplyVec_Identity,PFView_Identity,PFDestroy_Identity,loc)); 122292f8084SBarry Smith PetscFunctionReturn(0); 123292f8084SBarry Smith } 124