Index: /issm/trunk-jpl/src/c/toolkits/petsc/patches/PetscMatrixToDoubleMatrix.cpp
===================================================================
--- /issm/trunk-jpl/src/c/toolkits/petsc/patches/PetscMatrixToDoubleMatrix.cpp	(revision 12101)
+++ /issm/trunk-jpl/src/c/toolkits/petsc/patches/PetscMatrixToDoubleMatrix.cpp	(revision 12101)
@@ -0,0 +1,53 @@
+/* \file PetscMatrixToDoubleMatrix.cpp
+ * \brief: convert a sparse or dense Petsc matrix into a matlab matrix
+ */
+
+
+#ifdef HAVE_CONFIG_H
+	#include <config.h>
+#else
+#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
+#endif
+
+
+/*Petsc includes: */
+#include "petscmat.h"
+#include "petscvec.h"
+#include "petscksp.h"
+
+/*Petsc includes: */
+#include "mex.h"
+
+#include "../../shared/shared.h"
+#include <string>
+
+
+void PetscMatrixToDoubleMatrix(double** pmatrix, int* prows, int* pcols,Mat petsc_matrix){
+
+	int i,j,k;
+
+	/*output: */
+	double* matrix=NULL;
+	int     rows,cols;
+
+	/*intermediary: */
+	int*    idxm=NULL;
+	int*    idxn=NULL;
+
+	/*Some needed information: */
+	MatGetSize(petsc_matrix,&rows,&cols);
+
+	idxm=(int*)xmalloc(rows*sizeof(int));
+	idxn=(int*)xmalloc(cols*sizeof(int));
+
+	for(i=0;i<rows;i++)idxm[i]=i;
+	for(i=0;i<cols;i++)idxn[i]=i;
+
+	matrix=(double*)xmalloc(rows*cols*sizeof(double));
+	MatGetValues(petsc_matrix,rows,idxm,cols,idxn,matrix);
+
+	/*Assign output pointers: */
+	*pmatrix=matrix;
+	*prows=rows;
+	*pcols=cols;
+}
Index: /issm/trunk-jpl/src/c/toolkits/petsc/patches/PetscVectorToDoubleVector.cpp
===================================================================
--- /issm/trunk-jpl/src/c/toolkits/petsc/patches/PetscVectorToDoubleVector.cpp	(revision 12101)
+++ /issm/trunk-jpl/src/c/toolkits/petsc/patches/PetscVectorToDoubleVector.cpp	(revision 12101)
@@ -0,0 +1,47 @@
+/* \file PetscVectorToDoubleVector.cpp
+ */
+
+#ifdef HAVE_CONFIG_H
+	#include <config.h>
+#else
+#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
+#endif
+
+/*Petsc includes: */
+#include "petscmat.h"
+#include "petscvec.h"
+#include "petscksp.h"
+
+#include "mex.h"
+#include "../../shared/shared.h"
+#include <string>
+
+void PetscVectorToDoubleVector(double** pvector, int* prows, Vec petsc_vector){
+
+	int     i;
+	int     rows;
+	int    *idxm   = NULL;
+	double *vector = NULL;
+
+	/*output: */
+	mxArray* dataref=NULL;
+
+	/*Get size of vector: */
+	if(petsc_vector){
+		VecGetSize(petsc_vector,&rows);
+		if(rows){
+			idxm=(int*)xmalloc(rows*sizeof(int));
+			vector=(double*)xmalloc(rows*sizeof(double));
+			for(i=0;i<rows;i++)idxm[i]=i;
+
+			VecGetValues(petsc_vector,rows,idxm,vector);
+		}
+	}
+	else{
+		rows=0;
+	}
+
+	/*Assign output pointers: */
+	*pvector=vector;
+	*prows=rows;
+}
Index: /issm/trunk-jpl/src/c/toolkits/petsc/patches/petscpatches.h
===================================================================
--- /issm/trunk-jpl/src/c/toolkits/petsc/patches/petscpatches.h	(revision 12100)
+++ /issm/trunk-jpl/src/c/toolkits/petsc/patches/petscpatches.h	(revision 12101)
@@ -43,3 +43,6 @@
 MatType ISSMToPetscMatrixType(MatrixType type);
 
+void PetscMatrixToDoubleMatrix(double** pmatrix, int* prows, int* pcols,Mat matrix);
+void PetscVectorToDoubleVector(double** pvector, int* prows, Vec vector);
+
 #endif
