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: {
17: PetscBool iascii;
18: PF_Matlab *matlab = (PF_Matlab*)value;
21: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
22: if (iascii) {
23: PetscViewerASCIIPrintf(viewer,"Matlab Matlab = %s\n",matlab->string);
24: }
25: return(0);
26: }
28: PetscErrorCode PFDestroy_Matlab(void *value)
29: {
31: PF_Matlab *matlab = (PF_Matlab*)value;
34: PetscFree(matlab->string);
35: PetscMatlabEngineDestroy(&matlab->mengine);
36: PetscFree(matlab);
37: return(0);
38: }
40: PetscErrorCode PFApply_Matlab(void *value,PetscInt n,const PetscScalar *in,PetscScalar *out)
41: {
42: PF_Matlab *matlab = (PF_Matlab*)value;
46: if (!value) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Need to set string for MATLAB function, via -pf_matlab string");
47: PetscMatlabEnginePutArray(matlab->mengine,matlab->dimin,n,in,"x");
48: PetscMatlabEngineEvaluate(matlab->mengine,matlab->string);
49: PetscMatlabEngineGetArray(matlab->mengine,matlab->dimout,n,out,"f");
50: return(0);
51: }
53: PetscErrorCode PFSetFromOptions_Matlab(PetscOptionItems *PetscOptionsObject,PF pf)
54: {
56: PetscBool flag;
57: char value[256];
58: PF_Matlab *matlab = (PF_Matlab*)pf->data;
61: PetscOptionsHead(PetscOptionsObject,"Matlab function options");
62: PetscOptionsString("-pf_matlab","Matlab function","None","",value,sizeof(value),&flag);
63: if (flag) {
64: PetscStrallocpy((char*)value,&matlab->string);
65: }
66: PetscOptionsTail();
67: return(0);
68: }
70: PETSC_EXTERN PetscErrorCode PFCreate_Matlab(PF pf,void *value)
71: {
73: PF_Matlab *matlab;
76: PetscNewLog(pf,&matlab);
77: matlab->dimin = pf->dimin;
78: matlab->dimout = pf->dimout;
80: PetscMatlabEngineCreate(PetscObjectComm((PetscObject)pf),NULL,&matlab->mengine);
82: if (value) {
83: PetscStrallocpy((char*)value,&matlab->string);
84: }
85: PFSet(pf,PFApply_Matlab,NULL,PFView_Matlab,PFDestroy_Matlab,matlab);
87: pf->ops->setfromoptions = PFSetFromOptions_Matlab;
88: return(0);
89: }