Actual source code: cmatlab.c

      1: #define PETSCVEC_DLL

     3:  #include src/vec/pf/pfimpl.h

      5: /*
      6:         Ths PF generates a Matlab function on the fly
      7: */
      8: typedef struct {
      9:   PetscInt          dimin,dimout;
     10:   PetscMatlabEngine mengine;
     11:   char              *string;
     12: } PF_Matlab;
     13:
     16: PetscErrorCode PFView_Matlab(void *value,PetscViewer viewer)
     17: {
     19:   PetscTruth     iascii;
     20:   PF_Matlab      *matlab = (PF_Matlab*)value;

     23:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
     24:   if (iascii) {
     25:     PetscViewerASCIIPrintf(viewer,"Matlab Matlab = %s\n",matlab->string);
     26:   }
     27:   return(0);
     28: }

     32: PetscErrorCode PFDestroy_Matlab(void *value)
     33: {
     35:   PF_Matlab      *matlab = (PF_Matlab*)value;

     38:   PetscStrfree(matlab->string);
     39:   PetscMatlabEngineDestroy(matlab->mengine);
     40:   PetscFree(matlab);
     41:   return(0);
     42: }

     46: PetscErrorCode PFApply_Matlab(void *value,PetscInt n,PetscScalar *in,PetscScalar *out)
     47: {
     48:   PF_Matlab      *matlab = (PF_Matlab*)value;

     52:   if (!value) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Need to set string for Matlab function, via -pf_matlab string");
     53:   PetscMatlabEnginePutArray(matlab->mengine,matlab->dimin,n,in,"x");
     54:   PetscMatlabEngineEvaluate(matlab->mengine,matlab->string);
     55:   PetscMatlabEngineGetArray(matlab->mengine,matlab->dimout,n,out,"f");
     56:   return(0);
     57: }

     61: PetscErrorCode PFSetFromOptions_Matlab(PF pf)
     62: {
     64:   PetscTruth     flag;
     65:   char           value[256];
     66:   PF_Matlab      *matlab = (PF_Matlab*)pf->data;

     69:   PetscOptionsHead("Matlab function options");
     70:     PetscOptionsString("-pf_matlab","Matlab function","None","",value,256,&flag);
     71:     if (flag) {
     72:       PetscStrallocpy((char*)value,&matlab->string);
     73:     }
     74:   PetscOptionsTail();
     75:   return(0);
     76: }

     82: PetscErrorCode PETSCVEC_DLLEXPORT PFCreate_Matlab(PF pf,void *value)
     83: {
     85:   PF_Matlab      *matlab;

     88:   PetscNew(PF_Matlab,&matlab);
     89:   matlab->dimin  = pf->dimin;
     90:   matlab->dimout = pf->dimout;

     92:   PetscMatlabEngineCreate(pf->comm,PETSC_NULL,&matlab->mengine);
     93:
     94:   if (value) {
     95:     PetscStrallocpy((char*)value,&matlab->string);
     96:   }
     97:   PFSet(pf,PFApply_Matlab,PETSC_NULL,PFView_Matlab,PFDestroy_Matlab,matlab);

     99:   pf->ops->setfromoptions = PFSetFromOptions_Matlab;
    100:   return(0);
    101: }