xref: /petsc/src/vec/pf/impls/matlab/cmatlab.c (revision 38f2d2fdb3b6f522a3102c6eb796cebecf3224c0)
10c735eedSKris Buschelman #define PETSCVEC_DLL
2292f8084SBarry Smith 
358d819adSvictorle #include "src/vec/pf/pfimpl.h"            /*I "petscpf.h" I*/
4292f8084SBarry Smith 
5292f8084SBarry Smith /*
6292f8084SBarry 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;
19292f8084SBarry Smith   PetscTruth     iascii;
20292f8084SBarry Smith   PF_Matlab      *matlab = (PF_Matlab*)value;
21292f8084SBarry Smith 
22292f8084SBarry Smith   PetscFunctionBegin;
23292f8084SBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&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;
38292f8084SBarry Smith   ierr = PetscStrfree(matlab->string);CHKERRQ(ierr);
39292f8084SBarry 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"
461e1716dcSBarry Smith PetscErrorCode PFApply_Matlab(void *value,PetscInt n,PetscScalar *in,PetscScalar *out)
47292f8084SBarry Smith {
48292f8084SBarry Smith   PF_Matlab      *matlab = (PF_Matlab*)value;
49292f8084SBarry Smith   PetscErrorCode ierr;
50292f8084SBarry Smith 
51292f8084SBarry Smith   PetscFunctionBegin;
52e005ede5SBarry Smith   if (!value) SETERRQ(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;
64292f8084SBarry Smith   PetscTruth     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"
820c735eedSKris Buschelman PetscErrorCode PETSCVEC_DLLEXPORT PFCreate_Matlab(PF pf,void *value)
83292f8084SBarry Smith {
84292f8084SBarry Smith   PetscErrorCode ierr;
85292f8084SBarry Smith   PF_Matlab      *matlab;
86292f8084SBarry Smith 
87292f8084SBarry Smith   PetscFunctionBegin;
88*38f2d2fdSLisandro Dalcin   ierr           = PetscNewLog(pf,PF_Matlab,&matlab);CHKERRQ(ierr);
89292f8084SBarry Smith   matlab->dimin  = pf->dimin;
90292f8084SBarry Smith   matlab->dimout = pf->dimout;
91292f8084SBarry Smith 
92292f8084SBarry Smith   ierr = PetscMatlabEngineCreate(pf->comm,PETSC_NULL,&matlab->mengine);CHKERRQ(ierr);
93292f8084SBarry Smith 
94292f8084SBarry Smith   if (value) {
95292f8084SBarry Smith     ierr = PetscStrallocpy((char*)value,&matlab->string);CHKERRQ(ierr);
96292f8084SBarry Smith   }
97292f8084SBarry Smith   ierr   = PFSet(pf,PFApply_Matlab,PETSC_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