1292f8084SBarry Smith 2c6db04a5SJed Brown #include <../src/vec/pf/pfimpl.h> /*I "petscpf.h" I*/ 3*82c86c8fSBarry Smith #include <petscmatlab.h> /*I "petscmatlab.h" I*/ 4292f8084SBarry Smith 5292f8084SBarry Smith /* 6e3c5b3baSBarry Smith Ths PF generates a MATLAB function on the fly 7292f8084SBarry Smith */ 8292f8084SBarry Smith typedef struct { 91e1716dcSBarry Smith PetscInt dimin,dimout; 10292f8084SBarry Smith PetscMatlabEngine mengine; 11292f8084SBarry Smith char *string; 12292f8084SBarry Smith } PF_Matlab; 13292f8084SBarry Smith 14292f8084SBarry Smith #undef __FUNCT__ 15292f8084SBarry Smith #define __FUNCT__ "PFView_Matlab" 16292f8084SBarry Smith PetscErrorCode PFView_Matlab(void *value,PetscViewer viewer) 17292f8084SBarry Smith { 18292f8084SBarry Smith PetscErrorCode ierr; 19ace3abfcSBarry Smith PetscBool iascii; 20292f8084SBarry Smith PF_Matlab *matlab = (PF_Matlab*)value; 21292f8084SBarry Smith 22292f8084SBarry Smith PetscFunctionBegin; 23251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 24292f8084SBarry Smith if (iascii) { 25292f8084SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Matlab Matlab = %s\n",matlab->string);CHKERRQ(ierr); 26292f8084SBarry Smith } 27292f8084SBarry Smith PetscFunctionReturn(0); 28292f8084SBarry Smith } 29292f8084SBarry Smith 30292f8084SBarry Smith #undef __FUNCT__ 31292f8084SBarry Smith #define __FUNCT__ "PFDestroy_Matlab" 32292f8084SBarry Smith PetscErrorCode PFDestroy_Matlab(void *value) 33292f8084SBarry Smith { 34292f8084SBarry Smith PetscErrorCode ierr; 35292f8084SBarry Smith PF_Matlab *matlab = (PF_Matlab*)value; 36292f8084SBarry Smith 37292f8084SBarry Smith PetscFunctionBegin; 38503cfb0cSBarry Smith ierr = PetscFree(matlab->string);CHKERRQ(ierr); 39a32ab379SBarry Smith ierr = PetscMatlabEngineDestroy(&matlab->mengine);CHKERRQ(ierr); 40292f8084SBarry Smith ierr = PetscFree(matlab);CHKERRQ(ierr); 41292f8084SBarry Smith PetscFunctionReturn(0); 42292f8084SBarry Smith } 43292f8084SBarry Smith 44292f8084SBarry Smith #undef __FUNCT__ 45292f8084SBarry Smith #define __FUNCT__ "PFApply_Matlab" 4669b4f73cSBarry Smith PetscErrorCode PFApply_Matlab(void *value,PetscInt n,const PetscScalar *in,PetscScalar *out) 47292f8084SBarry Smith { 48292f8084SBarry Smith PF_Matlab *matlab = (PF_Matlab*)value; 49292f8084SBarry Smith PetscErrorCode ierr; 50292f8084SBarry Smith 51292f8084SBarry Smith PetscFunctionBegin; 52e3c5b3baSBarry Smith if (!value) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Need to set string for MATLAB function, via -pf_matlab string"); 53292f8084SBarry Smith ierr = PetscMatlabEnginePutArray(matlab->mengine,matlab->dimin,n,in,"x");CHKERRQ(ierr); 54292f8084SBarry Smith ierr = PetscMatlabEngineEvaluate(matlab->mengine,matlab->string);CHKERRQ(ierr); 55292f8084SBarry Smith ierr = PetscMatlabEngineGetArray(matlab->mengine,matlab->dimout,n,out,"f");CHKERRQ(ierr); 56292f8084SBarry Smith PetscFunctionReturn(0); 57292f8084SBarry Smith } 58292f8084SBarry Smith 59292f8084SBarry Smith #undef __FUNCT__ 60292f8084SBarry Smith #define __FUNCT__ "PFSetFromOptions_Matlab" 61292f8084SBarry Smith PetscErrorCode PFSetFromOptions_Matlab(PF pf) 62292f8084SBarry Smith { 63292f8084SBarry Smith PetscErrorCode ierr; 64ace3abfcSBarry Smith PetscBool flag; 65292f8084SBarry Smith char value[256]; 66292f8084SBarry Smith PF_Matlab *matlab = (PF_Matlab*)pf->data; 67292f8084SBarry Smith 68292f8084SBarry Smith PetscFunctionBegin; 69292f8084SBarry Smith ierr = PetscOptionsHead("Matlab function options");CHKERRQ(ierr); 70292f8084SBarry Smith ierr = PetscOptionsString("-pf_matlab","Matlab function","None","",value,256,&flag);CHKERRQ(ierr); 71292f8084SBarry Smith if (flag) { 72292f8084SBarry Smith ierr = PetscStrallocpy((char*)value,&matlab->string);CHKERRQ(ierr); 73292f8084SBarry Smith } 74292f8084SBarry Smith ierr = PetscOptionsTail();CHKERRQ(ierr); 75292f8084SBarry Smith PetscFunctionReturn(0); 76292f8084SBarry Smith } 77292f8084SBarry Smith 78292f8084SBarry Smith 79292f8084SBarry Smith EXTERN_C_BEGIN 80292f8084SBarry Smith #undef __FUNCT__ 81292f8084SBarry Smith #define __FUNCT__ "PFCreate_Matlab" 827087cfbeSBarry Smith PetscErrorCode PFCreate_Matlab(PF pf,void *value) 83292f8084SBarry Smith { 84292f8084SBarry Smith PetscErrorCode ierr; 85292f8084SBarry Smith PF_Matlab *matlab; 86292f8084SBarry Smith 87292f8084SBarry Smith PetscFunctionBegin; 8838f2d2fdSLisandro Dalcin ierr = PetscNewLog(pf,PF_Matlab,&matlab);CHKERRQ(ierr); 89292f8084SBarry Smith matlab->dimin = pf->dimin; 90292f8084SBarry Smith matlab->dimout = pf->dimout; 91292f8084SBarry Smith 92ce94432eSBarry Smith ierr = PetscMatlabEngineCreate(PetscObjectComm((PetscObject)pf),NULL,&matlab->mengine);CHKERRQ(ierr); 93292f8084SBarry Smith 94292f8084SBarry Smith if (value) { 95292f8084SBarry Smith ierr = PetscStrallocpy((char*)value,&matlab->string);CHKERRQ(ierr); 96292f8084SBarry Smith } 970298fd71SBarry Smith ierr = PFSet(pf,PFApply_Matlab,NULL,PFView_Matlab,PFDestroy_Matlab,matlab);CHKERRQ(ierr); 98292f8084SBarry Smith 99292f8084SBarry Smith pf->ops->setfromoptions = PFSetFromOptions_Matlab; 100292f8084SBarry Smith PetscFunctionReturn(0); 101292f8084SBarry Smith } 102292f8084SBarry Smith EXTERN_C_END 103292f8084SBarry Smith 104292f8084SBarry Smith 105292f8084SBarry Smith 106292f8084SBarry Smith 107292f8084SBarry Smith 108