Actual source code: cmatlab.c
2: #include <../src/vec/pf/pfimpl.h>
3: #include <petscmatlab.h>
5: /*
6: This 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;
14: PetscErrorCode PFView_Matlab(void *value,PetscViewer viewer)
15: {
16: PetscBool iascii;
17: PF_Matlab *matlab = (PF_Matlab*)value;
19: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
20: if (iascii) PetscViewerASCIIPrintf(viewer,"Matlab Matlab = %s\n",matlab->string);
21: return 0;
22: }
24: PetscErrorCode PFDestroy_Matlab(void *value)
25: {
26: PF_Matlab *matlab = (PF_Matlab*)value;
28: PetscFree(matlab->string);
29: PetscMatlabEngineDestroy(&matlab->mengine);
30: PetscFree(matlab);
31: return 0;
32: }
34: PetscErrorCode PFApply_Matlab(void *value,PetscInt n,const PetscScalar *in,PetscScalar *out)
35: {
36: PF_Matlab *matlab = (PF_Matlab*)value;
39: PetscMatlabEnginePutArray(matlab->mengine,matlab->dimin,n,in,"x");
40: PetscMatlabEngineEvaluate(matlab->mengine,matlab->string);
41: PetscMatlabEngineGetArray(matlab->mengine,matlab->dimout,n,out,"f");
42: return 0;
43: }
45: PetscErrorCode PFSetFromOptions_Matlab(PetscOptionItems *PetscOptionsObject,PF pf)
46: {
47: PetscBool flag;
48: char value[256];
49: PF_Matlab *matlab = (PF_Matlab*)pf->data;
51: PetscOptionsHead(PetscOptionsObject,"Matlab function options");
52: PetscOptionsString("-pf_matlab","Matlab function","None","",value,sizeof(value),&flag);
53: if (flag) PetscStrallocpy((char*)value,&matlab->string);
54: PetscOptionsTail();
55: return 0;
56: }
58: PETSC_EXTERN PetscErrorCode PFCreate_Matlab(PF pf,void *value)
59: {
60: PF_Matlab *matlab;
62: PetscNewLog(pf,&matlab);
63: matlab->dimin = pf->dimin;
64: matlab->dimout = pf->dimout;
66: PetscMatlabEngineCreate(PetscObjectComm((PetscObject)pf),NULL,&matlab->mengine);
68: if (value) PetscStrallocpy((char*)value,&matlab->string);
69: PFSet(pf,PFApply_Matlab,NULL,PFView_Matlab,PFDestroy_Matlab,matlab);
71: pf->ops->setfromoptions = PFSetFromOptions_Matlab;
72: return 0;
73: }