xref: /petsc/src/vec/vec/utils/matlab/gcreatev.c (revision e8e8640d1cb9a3a2f50c0c0d7b26e5c4d521e2e4)
1 #include <petscvec.h> /*I "petscvec.h" I*/
2 #include <petsc/private/petscimpl.h>
3 
4 #include <engine.h> /* MATLAB include file */
5 #include <mex.h>    /* MATLAB include file */
6 
VecMatlabEnginePut_Default(PetscObject obj,void * mengine)7 PETSC_EXTERN PetscErrorCode VecMatlabEnginePut_Default(PetscObject obj, void *mengine)
8 {
9   PetscInt           n;
10   Vec                vec = (Vec)obj;
11   const PetscScalar *array;
12   mxArray           *mat;
13 
14   PetscFunctionBegin;
15   PetscCall(VecGetArrayRead(vec, &array));
16   PetscCall(VecGetLocalSize(vec, &n));
17 #if defined(PETSC_USE_COMPLEX)
18   mat = mxCreateDoubleMatrix(n, 1, mxCOMPLEX);
19 #else
20   mat = mxCreateDoubleMatrix(n, 1, mxREAL);
21 #endif
22   PetscCall(PetscArraycpy(mxGetPr(mat), array, n));
23   PetscCall(PetscObjectName(obj));
24   engPutVariable((Engine *)mengine, obj->name, mat);
25 
26   PetscCall(VecRestoreArrayRead(vec, &array));
27   PetscFunctionReturn(PETSC_SUCCESS);
28 }
29 
VecMatlabEngineGet_Default(PetscObject obj,void * mengine)30 PETSC_EXTERN PetscErrorCode VecMatlabEngineGet_Default(PetscObject obj, void *mengine)
31 {
32   PetscInt     n;
33   Vec          vec = (Vec)obj;
34   PetscScalar *array;
35   mxArray     *mat;
36 
37   PetscFunctionBegin;
38   PetscCall(VecGetArray(vec, &array));
39   PetscCall(VecGetLocalSize(vec, &n));
40   mat = engGetVariable((Engine *)mengine, obj->name);
41   PetscCheck(mat, PETSC_COMM_SELF, PETSC_ERR_LIB, "Unable to get object %s from matlab", obj->name);
42   PetscCall(PetscArraycpy(array, mxGetPr(mat), n));
43   PetscCall(VecRestoreArray(vec, &array));
44   PetscFunctionReturn(PETSC_SUCCESS);
45 }
46