Date: 2013-07-02T14:40:52+00:00

Actual source code: cmatlab.c
petsc-3.4.2 2013-07-02

      2: #include <../src/vec/pf/pfimpl.h>            /*I "petscpf.h" I*/
      3: #include <petscmatlab.h>   /*I  "petscmatlab.h"  I*/

      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;

     16: PetscErrorCode PFView_Matlab(void *value,PetscViewer viewer)
     17: {
     19:   PetscBool      iascii;
     20:   PF_Matlab      *matlab = (PF_Matlab*)value;

     23:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&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:   PetscFree(matlab->string);
     39:   PetscMatlabEngineDestroy(&matlab->mengine);
     40:   PetscFree(matlab);
     41:   return(0);
     42: }

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

     52:   if (!value) SETERRQ(PETSC_COMM_SELF,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:   PetscBool      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: }

     80: PETSC_EXTERN PetscErrorCode PFCreate_Matlab(PF pf,void *value)
     81: {
     83:   PF_Matlab      *matlab;

     86:   PetscNewLog(pf,PF_Matlab,&matlab);
     87:   matlab->dimin  = pf->dimin;
     88:   matlab->dimout = pf->dimout;

     90:   PetscMatlabEngineCreate(PetscObjectComm((PetscObject)pf),NULL,&matlab->mengine);

     92:   if (value) {
     93:     PetscStrallocpy((char*)value,&matlab->string);
     94:   }
     95:   PFSet(pf,PFApply_Matlab,NULL,PFView_Matlab,PFDestroy_Matlab,matlab);

     97:   pf->ops->setfromoptions = PFSetFromOptions_Matlab;
     98:   return(0);
     99: }