1*292f8084SBarry Smith 2*292f8084SBarry Smith #include "src/pf/pfimpl.h" /*I "petscpf.h" I*/ 3*292f8084SBarry Smith 4*292f8084SBarry Smith /* 5*292f8084SBarry Smith Ths PF generates a Matlab function on the fly 6*292f8084SBarry Smith */ 7*292f8084SBarry Smith typedef struct { 8*292f8084SBarry Smith int dimin,dimout; 9*292f8084SBarry Smith PetscMatlabEngine mengine; 10*292f8084SBarry Smith char *string; 11*292f8084SBarry Smith } PF_Matlab; 12*292f8084SBarry Smith 13*292f8084SBarry Smith #undef __FUNCT__ 14*292f8084SBarry Smith #define __FUNCT__ "PFView_Matlab" 15*292f8084SBarry Smith PetscErrorCode PFView_Matlab(void *value,PetscViewer viewer) 16*292f8084SBarry Smith { 17*292f8084SBarry Smith PetscErrorCode ierr; 18*292f8084SBarry Smith PetscTruth iascii; 19*292f8084SBarry Smith PF_Matlab *matlab = (PF_Matlab*)value; 20*292f8084SBarry Smith 21*292f8084SBarry Smith PetscFunctionBegin; 22*292f8084SBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr); 23*292f8084SBarry Smith if (iascii) { 24*292f8084SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Matlab Matlab = %s\n",matlab->string);CHKERRQ(ierr); 25*292f8084SBarry Smith } 26*292f8084SBarry Smith PetscFunctionReturn(0); 27*292f8084SBarry Smith } 28*292f8084SBarry Smith 29*292f8084SBarry Smith #undef __FUNCT__ 30*292f8084SBarry Smith #define __FUNCT__ "PFDestroy_Matlab" 31*292f8084SBarry Smith PetscErrorCode PFDestroy_Matlab(void *value) 32*292f8084SBarry Smith { 33*292f8084SBarry Smith PetscErrorCode ierr; 34*292f8084SBarry Smith PF_Matlab *matlab = (PF_Matlab*)value; 35*292f8084SBarry Smith 36*292f8084SBarry Smith PetscFunctionBegin; 37*292f8084SBarry Smith ierr = PetscStrfree(matlab->string);CHKERRQ(ierr); 38*292f8084SBarry Smith ierr = PetscMatlabEngineDestroy(matlab->mengine);CHKERRQ(ierr); 39*292f8084SBarry Smith ierr = PetscFree(matlab);CHKERRQ(ierr); 40*292f8084SBarry Smith PetscFunctionReturn(0); 41*292f8084SBarry Smith } 42*292f8084SBarry Smith 43*292f8084SBarry Smith #undef __FUNCT__ 44*292f8084SBarry Smith #define __FUNCT__ "PFApply_Matlab" 45*292f8084SBarry Smith PetscErrorCode PFApply_Matlab(void *value,int n,PetscScalar *in,PetscScalar *out) 46*292f8084SBarry Smith { 47*292f8084SBarry Smith PF_Matlab *matlab = (PF_Matlab*)value; 48*292f8084SBarry Smith PetscErrorCode ierr; 49*292f8084SBarry Smith 50*292f8084SBarry Smith PetscFunctionBegin; 51*292f8084SBarry Smith if (!value) SETERRQ(1,"Need to set string for Matlab function, via -pf_matlab string"); 52*292f8084SBarry Smith ierr = PetscMatlabEnginePutArray(matlab->mengine,matlab->dimin,n,in,"x");CHKERRQ(ierr); 53*292f8084SBarry Smith ierr = PetscMatlabEngineEvaluate(matlab->mengine,matlab->string);CHKERRQ(ierr); 54*292f8084SBarry Smith ierr = PetscMatlabEngineGetArray(matlab->mengine,matlab->dimout,n,out,"f");CHKERRQ(ierr); 55*292f8084SBarry Smith PetscFunctionReturn(0); 56*292f8084SBarry Smith } 57*292f8084SBarry Smith 58*292f8084SBarry Smith #undef __FUNCT__ 59*292f8084SBarry Smith #define __FUNCT__ "PFSetFromOptions_Matlab" 60*292f8084SBarry Smith PetscErrorCode PFSetFromOptions_Matlab(PF pf) 61*292f8084SBarry Smith { 62*292f8084SBarry Smith PetscErrorCode ierr; 63*292f8084SBarry Smith PetscTruth flag; 64*292f8084SBarry Smith char value[256]; 65*292f8084SBarry Smith PF_Matlab *matlab = (PF_Matlab*)pf->data; 66*292f8084SBarry Smith 67*292f8084SBarry Smith PetscFunctionBegin; 68*292f8084SBarry Smith ierr = PetscOptionsHead("Matlab function options");CHKERRQ(ierr); 69*292f8084SBarry Smith ierr = PetscOptionsString("-pf_matlab","Matlab function","None","",value,256,&flag);CHKERRQ(ierr); 70*292f8084SBarry Smith if (flag) { 71*292f8084SBarry Smith ierr = PetscStrallocpy((char*)value,&matlab->string);CHKERRQ(ierr); 72*292f8084SBarry Smith } 73*292f8084SBarry Smith ierr = PetscOptionsTail();CHKERRQ(ierr); 74*292f8084SBarry Smith PetscFunctionReturn(0); 75*292f8084SBarry Smith } 76*292f8084SBarry Smith 77*292f8084SBarry Smith 78*292f8084SBarry Smith EXTERN_C_BEGIN 79*292f8084SBarry Smith #undef __FUNCT__ 80*292f8084SBarry Smith #define __FUNCT__ "PFCreate_Matlab" 81*292f8084SBarry Smith PetscErrorCode PFCreate_Matlab(PF pf,void *value) 82*292f8084SBarry Smith { 83*292f8084SBarry Smith PetscErrorCode ierr; 84*292f8084SBarry Smith PF_Matlab *matlab; 85*292f8084SBarry Smith 86*292f8084SBarry Smith PetscFunctionBegin; 87*292f8084SBarry Smith ierr = PetscNew(PF_Matlab,&matlab);CHKERRQ(ierr); 88*292f8084SBarry Smith matlab->dimin = pf->dimin; 89*292f8084SBarry Smith matlab->dimout = pf->dimout; 90*292f8084SBarry Smith 91*292f8084SBarry Smith ierr = PetscMatlabEngineCreate(pf->comm,PETSC_NULL,&matlab->mengine);CHKERRQ(ierr); 92*292f8084SBarry Smith 93*292f8084SBarry Smith if (value) { 94*292f8084SBarry Smith ierr = PetscStrallocpy((char*)value,&matlab->string);CHKERRQ(ierr); 95*292f8084SBarry Smith } 96*292f8084SBarry Smith ierr = PFSet(pf,PFApply_Matlab,PETSC_NULL,PFView_Matlab,PFDestroy_Matlab,matlab);CHKERRQ(ierr); 97*292f8084SBarry Smith 98*292f8084SBarry Smith pf->ops->setfromoptions = PFSetFromOptions_Matlab; 99*292f8084SBarry Smith PetscFunctionReturn(0); 100*292f8084SBarry Smith } 101*292f8084SBarry Smith EXTERN_C_END 102*292f8084SBarry Smith 103*292f8084SBarry Smith 104*292f8084SBarry Smith 105*292f8084SBarry Smith 106*292f8084SBarry Smith 107