1292f8084SBarry Smith 2c6db04a5SJed Brown #include <../src/vec/pf/pfimpl.h> /*I "petscpf.h" I*/ 3292f8084SBarry Smith 4292f8084SBarry Smith #undef __FUNCT__ 5292f8084SBarry Smith #define __FUNCT__ "PFApply_Constant" 6dd394643SJed Brown PetscErrorCode PFApply_Constant(void *value,PetscInt n,const PetscScalar *x,PetscScalar *y) 7292f8084SBarry Smith { 8292f8084SBarry Smith PetscInt i; 9292f8084SBarry Smith PetscScalar v = ((PetscScalar*)value)[0]; 10292f8084SBarry Smith 11292f8084SBarry Smith PetscFunctionBegin; 12292f8084SBarry Smith n *= (PetscInt) PetscRealPart(((PetscScalar*)value)[1]); 13f6e5521dSKarl Rupp for (i=0; i<n; i++) y[i] = v; 14292f8084SBarry Smith PetscFunctionReturn(0); 15292f8084SBarry Smith } 16292f8084SBarry Smith 17292f8084SBarry Smith #undef __FUNCT__ 18292f8084SBarry Smith #define __FUNCT__ "PFApplyVec_Constant" 19292f8084SBarry Smith PetscErrorCode PFApplyVec_Constant(void *value,Vec x,Vec y) 20292f8084SBarry Smith { 21292f8084SBarry Smith PetscErrorCode ierr; 225fd66863SKarl Rupp 23292f8084SBarry Smith PetscFunctionBegin; 242dcb1b2aSMatthew Knepley ierr = VecSet(y,*((PetscScalar*)value));CHKERRQ(ierr); 25292f8084SBarry Smith PetscFunctionReturn(0); 26292f8084SBarry Smith } 27292f8084SBarry Smith #undef __FUNCT__ 28292f8084SBarry Smith #define __FUNCT__ "PFView_Constant" 29292f8084SBarry Smith PetscErrorCode PFView_Constant(void *value,PetscViewer viewer) 30292f8084SBarry Smith { 31292f8084SBarry Smith PetscErrorCode ierr; 32ace3abfcSBarry Smith PetscBool iascii; 33292f8084SBarry Smith 34292f8084SBarry Smith PetscFunctionBegin; 35251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 36292f8084SBarry Smith if (iascii) { 37292f8084SBarry Smith #if !defined(PETSC_USE_COMPLEX) 38292f8084SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Constant = %g\n",*(double*)value);CHKERRQ(ierr); 39292f8084SBarry Smith #else 40292f8084SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Constant = %g + %gi\n",PetscRealPart(*(PetscScalar*)value),PetscImaginaryPart(*(PetscScalar*)value));CHKERRQ(ierr); 41292f8084SBarry Smith #endif 42292f8084SBarry Smith } 43292f8084SBarry Smith PetscFunctionReturn(0); 44292f8084SBarry Smith } 45292f8084SBarry Smith #undef __FUNCT__ 46292f8084SBarry Smith #define __FUNCT__ "PFDestroy_Constant" 47292f8084SBarry Smith PetscErrorCode PFDestroy_Constant(void *value) 48292f8084SBarry Smith { 49292f8084SBarry Smith PetscErrorCode ierr; 505fd66863SKarl Rupp 51292f8084SBarry Smith PetscFunctionBegin; 52292f8084SBarry Smith ierr = PetscFree(value);CHKERRQ(ierr); 53292f8084SBarry Smith PetscFunctionReturn(0); 54292f8084SBarry Smith } 55292f8084SBarry Smith 56292f8084SBarry Smith #undef __FUNCT__ 57292f8084SBarry Smith #define __FUNCT__ "PFSetFromOptions_Constant" 58292f8084SBarry Smith PetscErrorCode PFSetFromOptions_Constant(PF pf) 59292f8084SBarry Smith { 60292f8084SBarry Smith PetscErrorCode ierr; 61292f8084SBarry Smith PetscScalar *value = (PetscScalar*)pf->data; 62292f8084SBarry Smith 63292f8084SBarry Smith PetscFunctionBegin; 64292f8084SBarry Smith ierr = PetscOptionsHead("Constant function options");CHKERRQ(ierr); 65292f8084SBarry Smith ierr = PetscOptionsScalar("-pf_constant","The constant value","None",*value,value,0);CHKERRQ(ierr); 66292f8084SBarry Smith ierr = PetscOptionsTail();CHKERRQ(ierr); 67292f8084SBarry Smith PetscFunctionReturn(0); 68292f8084SBarry Smith } 69292f8084SBarry Smith 70292f8084SBarry Smith #undef __FUNCT__ 71292f8084SBarry Smith #define __FUNCT__ "PFCreate_Constant" 72*6ac5842eSBarry Smith PETSC_EXTERN_C PetscErrorCode PFCreate_Constant(PF pf,void *value) 73292f8084SBarry Smith { 74292f8084SBarry Smith PetscErrorCode ierr; 75292f8084SBarry Smith PetscScalar *loc; 76292f8084SBarry Smith 77292f8084SBarry Smith PetscFunctionBegin; 78292f8084SBarry Smith ierr = PetscMalloc(2*sizeof(PetscScalar),&loc);CHKERRQ(ierr); 79f6e5521dSKarl Rupp if (value) loc[0] = *(PetscScalar*)value; 80f6e5521dSKarl Rupp else loc[0] = 0.0; 81292f8084SBarry Smith loc[1] = pf->dimout; 82292f8084SBarry Smith ierr = PFSet(pf,PFApply_Constant,PFApplyVec_Constant,PFView_Constant,PFDestroy_Constant,loc);CHKERRQ(ierr); 83292f8084SBarry Smith 84292f8084SBarry Smith pf->ops->setfromoptions = PFSetFromOptions_Constant; 85292f8084SBarry Smith PetscFunctionReturn(0); 86292f8084SBarry Smith } 87292f8084SBarry Smith 885c8f6a95SKarl Rupp /*typedef PetscErrorCode (*FCN)(void*,PetscInt,const PetscScalar*,PetscScalar*); force argument to next function to not be extern C*/ 89*6ac5842eSBarry Smith 90292f8084SBarry Smith #undef __FUNCT__ 91292f8084SBarry Smith #define __FUNCT__ "PFCreate_Quick" 92*6ac5842eSBarry Smith PETSC_EXTERN_C PetscErrorCode PFCreate_Quick(PF pf,PetscErrorCode (*function)(void*,PetscInt,const PetscScalar*,PetscScalar*)) 93292f8084SBarry Smith { 94292f8084SBarry Smith PetscErrorCode ierr; 95292f8084SBarry Smith 96292f8084SBarry Smith PetscFunctionBegin; 975c8f6a95SKarl Rupp ierr = PFSet(pf,function,0,0,0,0);CHKERRQ(ierr); 98292f8084SBarry Smith PetscFunctionReturn(0); 99292f8084SBarry Smith } 100292f8084SBarry Smith 101292f8084SBarry Smith /* -------------------------------------------------------------------------------------------------------------------*/ 102292f8084SBarry Smith #undef __FUNCT__ 103292f8084SBarry Smith #define __FUNCT__ "PFApply_Identity" 104dd394643SJed Brown PetscErrorCode PFApply_Identity(void *value,PetscInt n,const PetscScalar *x,PetscScalar *y) 105292f8084SBarry Smith { 106292f8084SBarry Smith PetscInt i; 107292f8084SBarry Smith 108292f8084SBarry Smith PetscFunctionBegin; 109292f8084SBarry Smith n *= *(PetscInt*)value; 110f6e5521dSKarl Rupp for (i=0; i<n; i++) y[i] = x[i]; 111292f8084SBarry Smith PetscFunctionReturn(0); 112292f8084SBarry Smith } 113292f8084SBarry Smith 114292f8084SBarry Smith #undef __FUNCT__ 115292f8084SBarry Smith #define __FUNCT__ "PFApplyVec_Identity" 116292f8084SBarry Smith PetscErrorCode PFApplyVec_Identity(void *value,Vec x,Vec y) 117292f8084SBarry Smith { 118292f8084SBarry Smith PetscErrorCode ierr; 1195fd66863SKarl Rupp 120292f8084SBarry Smith PetscFunctionBegin; 121292f8084SBarry Smith ierr = VecCopy(x,y);CHKERRQ(ierr); 122292f8084SBarry Smith PetscFunctionReturn(0); 123292f8084SBarry Smith } 124292f8084SBarry Smith #undef __FUNCT__ 125292f8084SBarry Smith #define __FUNCT__ "PFView_Identity" 126292f8084SBarry Smith PetscErrorCode PFView_Identity(void *value,PetscViewer viewer) 127292f8084SBarry Smith { 128292f8084SBarry Smith PetscErrorCode ierr; 129ace3abfcSBarry Smith PetscBool iascii; 130292f8084SBarry Smith 131292f8084SBarry Smith PetscFunctionBegin; 132251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 133292f8084SBarry Smith if (iascii) { 134292f8084SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Identity function\n");CHKERRQ(ierr); 135292f8084SBarry Smith } 136292f8084SBarry Smith PetscFunctionReturn(0); 137292f8084SBarry Smith } 138292f8084SBarry Smith #undef __FUNCT__ 139292f8084SBarry Smith #define __FUNCT__ "PFDestroy_Identity" 140292f8084SBarry Smith PetscErrorCode PFDestroy_Identity(void *value) 141292f8084SBarry Smith { 142292f8084SBarry Smith PetscErrorCode ierr; 1435fd66863SKarl Rupp 144292f8084SBarry Smith PetscFunctionBegin; 145292f8084SBarry Smith ierr = PetscFree(value);CHKERRQ(ierr); 146292f8084SBarry Smith PetscFunctionReturn(0); 147292f8084SBarry Smith } 148292f8084SBarry Smith 149292f8084SBarry Smith #undef __FUNCT__ 150292f8084SBarry Smith #define __FUNCT__ "PFCreate_Identity" 151*6ac5842eSBarry Smith PETSC_EXTERN_C PetscErrorCode PFCreate_Identity(PF pf,void *value) 152292f8084SBarry Smith { 153292f8084SBarry Smith PetscErrorCode ierr; 154292f8084SBarry Smith PetscInt *loc; 155292f8084SBarry Smith 156292f8084SBarry Smith PetscFunctionBegin; 157f6e5521dSKarl Rupp 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); 158292f8084SBarry Smith ierr = PetscMalloc(sizeof(PetscInt),&loc);CHKERRQ(ierr); 159292f8084SBarry Smith loc[0] = pf->dimout; 160292f8084SBarry Smith ierr = PFSet(pf,PFApply_Identity,PFApplyVec_Identity,PFView_Identity,PFDestroy_Identity,loc);CHKERRQ(ierr); 161292f8084SBarry Smith PetscFunctionReturn(0); 162292f8084SBarry Smith } 163