Actual source code: const.c


  2: #include <../src/vec/pf/pfimpl.h>

  4: static PetscErrorCode PFApply_Constant(void *value,PetscInt n,const PetscScalar *x,PetscScalar *y)
  5: {
  6:   PetscInt    i;
  7:   PetscScalar v = ((PetscScalar*)value)[0];

  9:   n *= (PetscInt) PetscRealPart(((PetscScalar*)value)[1]);
 10:   for (i=0; i<n; i++) y[i] = v;
 11:   return 0;
 12: }

 14: static PetscErrorCode PFApplyVec_Constant(void *value,Vec x,Vec y)
 15: {
 16:   VecSet(y,*((PetscScalar*)value));
 17:   return 0;
 18: }
 19: PetscErrorCode PFView_Constant(void *value,PetscViewer viewer)
 20: {
 21:   PetscBool      iascii;

 23:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
 24:   if (iascii) {
 25: #if !defined(PETSC_USE_COMPLEX)
 26:     PetscViewerASCIIPrintf(viewer,"Constant = %g\n",*(double*)value);
 27: #else
 28:     PetscViewerASCIIPrintf(viewer,"Constant = %g + %gi\n",PetscRealPart(*(PetscScalar*)value),PetscImaginaryPart(*(PetscScalar*)value));
 29: #endif
 30:   }
 31:   return 0;
 32: }
 33: static PetscErrorCode PFDestroy_Constant(void *value)
 34: {
 35:   PetscFree(value);
 36:   return 0;
 37: }

 39: static PetscErrorCode PFSetFromOptions_Constant(PetscOptionItems *PetscOptionsObject,PF pf)
 40: {
 41:   PetscScalar    *value = (PetscScalar*)pf->data;

 43:   PetscOptionsHead(PetscOptionsObject,"Constant function options");
 44:   PetscOptionsScalar("-pf_constant","The constant value","None",*value,value,NULL);
 45:   PetscOptionsTail();
 46:   return 0;
 47: }

 49: PETSC_EXTERN PetscErrorCode PFCreate_Constant(PF pf,void *value)
 50: {
 51:   PetscScalar    *loc;

 53:   PetscMalloc1(2,&loc);
 54:   if (value) loc[0] = *(PetscScalar*)value;
 55:   else loc[0] = 0.0;
 56:   loc[1] = pf->dimout;
 57:   PFSet(pf,PFApply_Constant,PFApplyVec_Constant,PFView_Constant,PFDestroy_Constant,loc);

 59:   pf->ops->setfromoptions = PFSetFromOptions_Constant;
 60:   return 0;
 61: }

 63: /*typedef PetscErrorCode (*FCN)(void*,PetscInt,const PetscScalar*,PetscScalar*);  force argument to next function to not be extern C*/

 65: PETSC_EXTERN PetscErrorCode PFCreate_Quick(PF pf,PetscErrorCode (*function)(void*,PetscInt,const PetscScalar*,PetscScalar*))
 66: {
 67:   PFSet(pf,function,NULL,NULL,NULL,NULL);
 68:   return 0;
 69: }

 71: /* -------------------------------------------------------------------------------------------------------------------*/
 72: static PetscErrorCode PFApply_Identity(void *value,PetscInt n,const PetscScalar *x,PetscScalar *y)
 73: {
 74:   PetscInt i;

 76:   n *= *(PetscInt*)value;
 77:   for (i=0; i<n; i++) y[i] = x[i];
 78:   return 0;
 79: }

 81: static PetscErrorCode PFApplyVec_Identity(void *value,Vec x,Vec y)
 82: {
 83:   VecCopy(x,y);
 84:   return 0;
 85: }
 86: static PetscErrorCode PFView_Identity(void *value,PetscViewer viewer)
 87: {
 88:   PetscBool      iascii;

 90:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
 91:   if (iascii) {
 92:     PetscViewerASCIIPrintf(viewer,"Identity function\n");
 93:   }
 94:   return 0;
 95: }
 96: static PetscErrorCode PFDestroy_Identity(void *value)
 97: {
 98:   PetscFree(value);
 99:   return 0;
100: }

102: PETSC_EXTERN PetscErrorCode PFCreate_Identity(PF pf,void *value)
103: {
104:   PetscInt       *loc;

107:   PetscNew(&loc);
108:   loc[0] = pf->dimout;
109:   PFSet(pf,PFApply_Identity,PFApplyVec_Identity,PFView_Identity,PFDestroy_Identity,loc);
110:   return 0;
111: }