1292f8084SBarry Smith 2c6db04a5SJed Brown #include <../src/vec/pf/pfimpl.h> /*I "petscpf.h" I*/ 3292f8084SBarry Smith 4292f8084SBarry Smith /* 5e3c5b3baSBarry Smith Ths PF generates a MATLAB function on the fly 6292f8084SBarry Smith */ 7292f8084SBarry Smith typedef struct { 81e1716dcSBarry Smith PetscInt dimin,dimout; 9292f8084SBarry Smith PetscMatlabEngine mengine; 10292f8084SBarry Smith char *string; 11292f8084SBarry Smith } PF_Matlab; 12292f8084SBarry Smith 13292f8084SBarry Smith #undef __FUNCT__ 14292f8084SBarry Smith #define __FUNCT__ "PFView_Matlab" 15292f8084SBarry Smith PetscErrorCode PFView_Matlab(void *value,PetscViewer viewer) 16292f8084SBarry Smith { 17292f8084SBarry Smith PetscErrorCode ierr; 18ace3abfcSBarry Smith PetscBool iascii; 19292f8084SBarry Smith PF_Matlab *matlab = (PF_Matlab*)value; 20292f8084SBarry Smith 21292f8084SBarry Smith PetscFunctionBegin; 22251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 23292f8084SBarry Smith if (iascii) { 24292f8084SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Matlab Matlab = %s\n",matlab->string);CHKERRQ(ierr); 25292f8084SBarry Smith } 26292f8084SBarry Smith PetscFunctionReturn(0); 27292f8084SBarry Smith } 28292f8084SBarry Smith 29292f8084SBarry Smith #undef __FUNCT__ 30292f8084SBarry Smith #define __FUNCT__ "PFDestroy_Matlab" 31292f8084SBarry Smith PetscErrorCode PFDestroy_Matlab(void *value) 32292f8084SBarry Smith { 33292f8084SBarry Smith PetscErrorCode ierr; 34292f8084SBarry Smith PF_Matlab *matlab = (PF_Matlab*)value; 35292f8084SBarry Smith 36292f8084SBarry Smith PetscFunctionBegin; 37503cfb0cSBarry Smith ierr = PetscFree(matlab->string);CHKERRQ(ierr); 38a32ab379SBarry Smith ierr = PetscMatlabEngineDestroy(&matlab->mengine);CHKERRQ(ierr); 39292f8084SBarry Smith ierr = PetscFree(matlab);CHKERRQ(ierr); 40292f8084SBarry Smith PetscFunctionReturn(0); 41292f8084SBarry Smith } 42292f8084SBarry Smith 43292f8084SBarry Smith #undef __FUNCT__ 44292f8084SBarry Smith #define __FUNCT__ "PFApply_Matlab" 4569b4f73cSBarry Smith PetscErrorCode PFApply_Matlab(void *value,PetscInt n,const PetscScalar *in,PetscScalar *out) 46292f8084SBarry Smith { 47292f8084SBarry Smith PF_Matlab *matlab = (PF_Matlab*)value; 48292f8084SBarry Smith PetscErrorCode ierr; 49292f8084SBarry Smith 50292f8084SBarry Smith PetscFunctionBegin; 51e3c5b3baSBarry Smith if (!value) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Need to set string for MATLAB function, via -pf_matlab string"); 52292f8084SBarry Smith ierr = PetscMatlabEnginePutArray(matlab->mengine,matlab->dimin,n,in,"x");CHKERRQ(ierr); 53292f8084SBarry Smith ierr = PetscMatlabEngineEvaluate(matlab->mengine,matlab->string);CHKERRQ(ierr); 54292f8084SBarry Smith ierr = PetscMatlabEngineGetArray(matlab->mengine,matlab->dimout,n,out,"f");CHKERRQ(ierr); 55292f8084SBarry Smith PetscFunctionReturn(0); 56292f8084SBarry Smith } 57292f8084SBarry Smith 58292f8084SBarry Smith #undef __FUNCT__ 59292f8084SBarry Smith #define __FUNCT__ "PFSetFromOptions_Matlab" 60292f8084SBarry Smith PetscErrorCode PFSetFromOptions_Matlab(PF pf) 61292f8084SBarry Smith { 62292f8084SBarry Smith PetscErrorCode ierr; 63ace3abfcSBarry Smith PetscBool flag; 64292f8084SBarry Smith char value[256]; 65292f8084SBarry Smith PF_Matlab *matlab = (PF_Matlab*)pf->data; 66292f8084SBarry Smith 67292f8084SBarry Smith PetscFunctionBegin; 68292f8084SBarry Smith ierr = PetscOptionsHead("Matlab function options");CHKERRQ(ierr); 69292f8084SBarry Smith ierr = PetscOptionsString("-pf_matlab","Matlab function","None","",value,256,&flag);CHKERRQ(ierr); 70292f8084SBarry Smith if (flag) { 71292f8084SBarry Smith ierr = PetscStrallocpy((char*)value,&matlab->string);CHKERRQ(ierr); 72292f8084SBarry Smith } 73292f8084SBarry Smith ierr = PetscOptionsTail();CHKERRQ(ierr); 74292f8084SBarry Smith PetscFunctionReturn(0); 75292f8084SBarry Smith } 76292f8084SBarry Smith 77292f8084SBarry Smith 78292f8084SBarry Smith EXTERN_C_BEGIN 79292f8084SBarry Smith #undef __FUNCT__ 80292f8084SBarry Smith #define __FUNCT__ "PFCreate_Matlab" 817087cfbeSBarry Smith PetscErrorCode PFCreate_Matlab(PF pf,void *value) 82292f8084SBarry Smith { 83292f8084SBarry Smith PetscErrorCode ierr; 84292f8084SBarry Smith PF_Matlab *matlab; 85292f8084SBarry Smith 86292f8084SBarry Smith PetscFunctionBegin; 8738f2d2fdSLisandro Dalcin ierr = PetscNewLog(pf,PF_Matlab,&matlab);CHKERRQ(ierr); 88292f8084SBarry Smith matlab->dimin = pf->dimin; 89292f8084SBarry Smith matlab->dimout = pf->dimout; 90292f8084SBarry Smith 91*ce94432eSBarry Smith ierr = PetscMatlabEngineCreate(PetscObjectComm((PetscObject)pf),NULL,&matlab->mengine);CHKERRQ(ierr); 92292f8084SBarry Smith 93292f8084SBarry Smith if (value) { 94292f8084SBarry Smith ierr = PetscStrallocpy((char*)value,&matlab->string);CHKERRQ(ierr); 95292f8084SBarry Smith } 960298fd71SBarry Smith ierr = PFSet(pf,PFApply_Matlab,NULL,PFView_Matlab,PFDestroy_Matlab,matlab);CHKERRQ(ierr); 97292f8084SBarry Smith 98292f8084SBarry Smith pf->ops->setfromoptions = PFSetFromOptions_Matlab; 99292f8084SBarry Smith PetscFunctionReturn(0); 100292f8084SBarry Smith } 101292f8084SBarry Smith EXTERN_C_END 102292f8084SBarry Smith 103292f8084SBarry Smith 104292f8084SBarry Smith 105292f8084SBarry Smith 106292f8084SBarry Smith 107