Actual source code: gcreatev.c

      1: #include <petscvec.h>
      2: #include <petsc/private/petscimpl.h>

      4: #include <engine.h> /* MATLAB include file */
      5: #include <mex.h>    /* MATLAB include file */

      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;

     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);

     26:   PetscCall(VecRestoreArrayRead(vec, &array));
     27:   PetscFunctionReturn(PETSC_SUCCESS);
     28: }

     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;

     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: }