xref: /petsc/src/vec/pf/impls/matlab/cmatlab.c (revision e005ede52eafe2fed2813abb7a7eb3df04d5f886)
1292f8084SBarry Smith 
258d819adSvictorle #include "src/vec/pf/pfimpl.h"            /*I "petscpf.h" I*/
3292f8084SBarry Smith 
4292f8084SBarry Smith /*
5292f8084SBarry 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;
18292f8084SBarry Smith   PetscTruth     iascii;
19292f8084SBarry Smith   PF_Matlab      *matlab = (PF_Matlab*)value;
20292f8084SBarry Smith 
21292f8084SBarry Smith   PetscFunctionBegin;
22292f8084SBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&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;
37292f8084SBarry Smith   ierr = PetscStrfree(matlab->string);CHKERRQ(ierr);
38292f8084SBarry 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"
451e1716dcSBarry Smith PetscErrorCode PFApply_Matlab(void *value,PetscInt n,PetscScalar *in,PetscScalar *out)
46292f8084SBarry Smith {
47292f8084SBarry Smith   PF_Matlab      *matlab = (PF_Matlab*)value;
48292f8084SBarry Smith   PetscErrorCode ierr;
49292f8084SBarry Smith 
50292f8084SBarry Smith   PetscFunctionBegin;
51*e005ede5SBarry Smith   if (!value) SETERRQ(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;
63292f8084SBarry Smith   PetscTruth     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"
81292f8084SBarry Smith PetscErrorCode PFCreate_Matlab(PF pf,void *value)
82292f8084SBarry Smith {
83292f8084SBarry Smith   PetscErrorCode ierr;
84292f8084SBarry Smith   PF_Matlab      *matlab;
85292f8084SBarry Smith 
86292f8084SBarry Smith   PetscFunctionBegin;
87292f8084SBarry Smith   ierr           = PetscNew(PF_Matlab,&matlab);CHKERRQ(ierr);
88292f8084SBarry Smith   matlab->dimin  = pf->dimin;
89292f8084SBarry Smith   matlab->dimout = pf->dimout;
90292f8084SBarry Smith 
91292f8084SBarry Smith   ierr = PetscMatlabEngineCreate(pf->comm,PETSC_NULL,&matlab->mengine);CHKERRQ(ierr);
92292f8084SBarry Smith 
93292f8084SBarry Smith   if (value) {
94292f8084SBarry Smith     ierr = PetscStrallocpy((char*)value,&matlab->string);CHKERRQ(ierr);
95292f8084SBarry Smith   }
96292f8084SBarry Smith   ierr   = PFSet(pf,PFApply_Matlab,PETSC_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