Index: /issm/trunk-jpl/externalpackages/boost/install.sh
===================================================================
--- /issm/trunk-jpl/externalpackages/boost/install.sh	(revision 12010)
+++ /issm/trunk-jpl/externalpackages/boost/install.sh	(revision 12011)
@@ -26,4 +26,6 @@
 	--with-python-root="$ISSM_TIER/externalpackages/python/install" 
 
+exit
+
 #Compile boost
 ./bjam install
Index: /issm/trunk-jpl/src/c/Container/Options.h
===================================================================
--- /issm/trunk-jpl/src/c/Container/Options.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/Container/Options.h	(revision 12011)
@@ -15,7 +15,5 @@
 		/*constructors, destructors*/
 		Options();
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		Options(int istart, int nrhs, const mxArray* prhs[]);
-		#endif
+		Options(int istart, int nrhs, void* module_references);
 		~Options();
 
Index: /issm/trunk-jpl/src/c/Container/Results.cpp
===================================================================
--- /issm/trunk-jpl/src/c/Container/Results.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/Container/Results.cpp	(revision 12011)
@@ -65,88 +65,4 @@
 /*}}}*/
 /*FUNCTION Results::Write{{{1*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void Results::Write(mxArray** pdataref){
-
-	int i,j;
-	int count;
-
-	/*output: */
-	mxArray* dataref=NULL;
-	mxArray* processeddataref=NULL;
-	mwSize nfields;
-	mwSize maxfields;
-	mwSize nsteps;
-	mwSize step;
-	const char **fnames      = NULL;
-	int         *enums       = NULL;
-	int          baseenum;
-	mwSize       onebyone[2] = {1,1};
-	mwSize       ndim        = 2;
-
-	/*How many time steps do we have? : */
-	nsteps=0;
-	for(i=0;i<this->Size();i++){
-		ExternalResult* result=(ExternalResult*)this->GetObjectByOffset(i);
-		step=result->GetStep();
-		if(step>nsteps)nsteps=step;
-	}
-	onebyone[0]=nsteps;
-
-	/*How many field names do we have. First, figure out how many result types we have: */
-	maxfields=(mwSize)this->Size();
-	enums=(int*)xmalloc(maxfields*sizeof(int));
-	for(i=0;i<maxfields;i++){
-		ExternalResult* result=(ExternalResult*)this->GetObjectByOffset(i);
-		enums[i]=result->InstanceEnum();
-	}
-	/*Now, make result types unique: */
-	for(i=0;i<maxfields;i++){
-		if(enums[i]>=0){//if <0, it means this enum was found to replicate another one previously
-			baseenum=enums[i]; 		
-			/*is the baseenum repeated later on?:*/
-			for(j=i+1;j<maxfields;j++){
-				if (enums[j]==baseenum)enums[j]=-1;
-			}
-		}
-		else continue;
-	}
-
-	/*Now, go through enums, and whatever is not null is a non repeated field name: */
-	nfields=0;
-	for(i=0;i<maxfields;i++)if(enums[i]>0)nfields++;
-
-	/*Add 2 fields for time and step: */
-	nfields=nfields+2;
-	
-	/*Fill the names of the structure field: */
-	fnames=(const char**)xmalloc(nfields*sizeof(char*));
-	count=0;
-	for(i=0;i<maxfields;i++){
-		if (enums[i]>0){
-			fnames[count]=EnumToStringx(enums[i]);
-			count++;
-		}
-	}
-	/*don't forget the extra fields "time" and "step":*/
-	fnames[nfields-2]="time";
-	fnames[nfields-1]="step";
-
-	/*Initialize structure: */
-	dataref=mxCreateStructArray( ndim,onebyone,nfields,fnames);
-
-	/*Fill each field: */
-	for(i=0;i<this->Size();i++){ //do not include the last one used for time
-		ExternalResult* result=(ExternalResult*)this->GetObjectByOffset(i);
-		result->SetMatlabField(dataref);
-	}
-
-	/*Now, process the patch in the dataref structure, by calling MatlabProcessPatch.m 
-	 *on the current dataref structure: */
-	mexCallMATLAB(1,&processeddataref,1,&dataref, "MatlabProcessPatch");
-
-	/*Assign output pointers:*/
-	*pdataref=processeddataref;
-}
-#else 
 void Results::Write(Parameters* parameters){
 	
@@ -167,4 +83,3 @@
 
 }
-#endif
 /*}}}*/
Index: /issm/trunk-jpl/src/c/Container/Results.h
===================================================================
--- /issm/trunk-jpl/src/c/Container/Results.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/Container/Results.h	(revision 12011)
@@ -26,9 +26,5 @@
 		/*numerics: {{{1*/
 		Results* SpawnTriaResults(int* indices);
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		void Write(mxArray** pdataref);
-		#else 
 		void Write(Parameters* parameters);
-		#endif
 		/*}}}*/
 };
Index: /issm/trunk-jpl/src/c/Makefile.am
===================================================================
--- /issm/trunk-jpl/src/c/Makefile.am	(revision 12010)
+++ /issm/trunk-jpl/src/c/Makefile.am	(revision 12011)
@@ -4,17 +4,10 @@
 
 #Library declaration {{{1
-#Compile serial library, and then try and compile parallel library
-if NOSERIAL
-if NOPARALLEL
-lib_LIBRARIES = 
-else
-lib_LIBRARIES = libpISSM.a libOverload.a
-endif
-else
-if NOPARALLEL
-lib_LIBRARIES = libISSM.a libOverload.a
-else
-lib_LIBRARIES = libISSM.a libpISSM.a libOverload.a
-endif
+lib_LIBRARIES = libISSMCore.a libISSMModules.a libISSMOverload.a
+if PYTHON
+lib_LIBRARIES += libISSMPython.a 
+endif
+if MATLAB
+lib_LIBRARIES += libISSMMatlab.a 
 endif
 #}}}
@@ -362,6 +355,6 @@
 					  ./modules/AverageOntoPartitionx/AverageOntoPartitionx.cpp\
 					  ./modules/ModelProcessorx/Dakota/CreateParametersDakota.cpp\
-					  ./modules/AverageOntoPartitionx/AverageOntoPartitionx.h
-dakota_psources= ./modules/Dakotax/SpawnCoreParallel.cpp
+					  ./modules/AverageOntoPartitionx/AverageOntoPartitionx.h\
+					  ./modules/Dakotax/SpawnCoreParallel.cpp
 #}}}
 #Transient sources  {{{1
@@ -443,6 +436,5 @@
 					  ./objects/Inputs/ControlInput.cpp\
 					  ./shared/Numerics/BrentSearch.cpp\
-					  ./shared/Numerics/OptimalSearch.cpp\
-					  ./shared/Numerics/OptFunc.cpp
+					  ./shared/Numerics/OptimalSearch.cpp
 
 control_psources=./solutions/control_core.cpp\
@@ -694,23 +686,4 @@
 			     ./objects/KML/KMLFileReadUtils.h
 #}}}
-#Matlab sources  {{{1
-matlab_sources= ./toolkits/matlab/matlabincludes.h\
-				    ./toolkits/matlab/MatlabNArrayToNArray.cpp\
-				    ./toolkits/double/MatlabVectorToDoubleVector.cpp\
-				    ./toolkits/double/double.h\
-				    ./toolkits/double/MatlabMatrixToDoubleMatrix.cpp\
-				    ./io/Matlab/matlabio.h\
-				    ./io/Matlab/CheckNumMatlabArguments.cpp\
-				    ./io/Matlab/mxGetAssignedField.cpp\
-				    ./io/Matlab/WriteMatlabData.cpp\
-				    ./io/Matlab/FetchMatlabData.cpp\
-				    ./io/Matlab/OptionParse.cpp
-#}}}
-#Python sources  {{{1
-python_sources=     ./io/Python/pythonio.h\
-				    ./io/Python/WritePythonData.cpp\
-				    ./io/Python/CheckNumPythonArguments.cpp\
-				    ./io/Python/FetchPythonData.cpp
-#}}}
 #Petsc sources  {{{1
 petsc_sources= ./toolkits/petsc\
@@ -718,8 +691,4 @@
 					./toolkits/petsc/patches/SolverEnum.h\
 					./toolkits/petsc/patches/petscpatches.h\
-					./toolkits/petsc/patches/MatlabMatrixToPetscMatrix.cpp\
-					./toolkits/petsc/patches/MatlabVectorToPetscVector.cpp\
-					./toolkits/petsc/patches/PetscMatrixToMatlabMatrix.cpp\
-					./toolkits/petsc/patches/PetscVectorToMatlabVector.cpp\
 					./toolkits/petsc/patches/VecTranspose.cpp\
 					./toolkits/petsc/patches/VecToMPISerial.cpp\
@@ -754,6 +723,46 @@
 
 #}}}
-#Serialsources  {{{1
-serial_sources= ./objects/Options/Option.cpp\
+#Python sources  {{{1
+python_sources=     ./python/io/pythonio.h\
+				    ./python/io/WritePythonData.cpp\
+				    ./python/io/CheckNumPythonArguments.cpp\
+				    ./python/io/FetchPythonData.cpp\
+					./python/objects/Bamg/BamgGeom.h\
+					./python/objects/Bamg/BamgGeom.cpp\
+					./python/objects/Bamg/BamgMesh.h\
+					./python/objects/Bamg/BamgMesh.cpp\
+					./python/objects/Bamg/BamgOpts.h\
+					./python/objects/Bamg/BamgOpts.cpp
+
+#}}}
+#Matlab sources  {{{1
+matlab_sources= ./toolkits/matlab/matlabincludes.h\
+				    ./matlab/io/matlabio.h\
+				    ./matlab/io/MatlabNArrayToNArray.cpp\
+				    ./matlab/io/CheckNumMatlabArguments.cpp\
+				    ./matlab/io/mxGetAssignedField.cpp\
+				    ./matlab/io/WriteMatlabData.cpp\
+				    ./matlab/io/FetchMatlabData.cpp\
+				    ./matlab/io/OptionParse.cpp\
+				    ./matlab/io/MatlabMatrixToMatrix.cpp\
+				    ./matlab/io/MatlabVectorToVector.cpp\
+					./matlab/io/MatlabVectorToDoubleVector.cpp\
+				    ./matlab/io/MatlabMatrixToDoubleMatrix.cpp\
+					./matlab/io/Container/Options.cpp\
+					./matlab/objects/Bamg/BamgGeom.h\
+					./matlab/objects/Bamg/BamgGeom.cpp\
+					./matlab/objects/Bamg/BamgMesh.h\
+					./matlab/objects/Bamg/BamgMesh.cpp\
+					./matlab/objects/Bamg/BamgOpts.h\
+					./matlab/objects/Bamg/BamgOpts.cpp\
+					./matlab/io/MatlabMatrixToPetscMatrix.cpp\
+					./matlab/io/MatlabVectorToPetscVector.cpp\
+					./matlab/io/PetscMatrixToDoubleMatrix.cpp\
+					./matlab/io/PetscVectorToDoubleVector.cpp\
+					./matlab/io/MatlabMatrixToSeqMat.cpp\
+					./matlab/io/MatlabVectorToSeqVec.cpp
+#}}}
+#Modules sources{{{1
+module_sources= ./objects/Options/Option.cpp\
 			./objects/Options/Option.h\
 			./objects/Options/OptionDouble.cpp\
@@ -769,4 +778,6 @@
 			./objects/Options/OptionUtilities.cpp\
 			./objects/Options/OptionUtilities.h\
+			./shared/Alloc/alloc_module.h\
+			./shared/Alloc/alloc_module.cpp\
 			./shared/Threads/issm_threads.h\
 			./shared/Threads/LaunchThread.cpp\
@@ -790,5 +801,4 @@
 			./modules/Chacox/chaco_seconds.cpp\
 			./modules/Chacox/user_params.cpp\
-			./modules/Dakotax/SpawnCoreSerial.cpp\
 			./modules/TriaSearchx/TriaSearchx.h\
 			./modules/TriaSearchx/TriaSearchx.cpp\
@@ -841,67 +851,55 @@
 #}}}
 
+#{{{1 Conditional build-up of sources
 #ISSM sources are a combination of core sources and sources related to specific capabilities (which can 
 #be activated by autotools conditionals 
-#{{{1
+
 
 #First the core
 issm_sources  =  $(core_sources)
-issm_psources =  
 
 #Now the optional source
 if DAKOTA
 issm_sources  +=  $(dakota_sources)
-issm_psources +=  $(dakota_psources)
 endif
 
 if PETSC
 issm_sources  +=  $(petsc_sources)
-issm_psources +=  $(petsc_psources)
 endif
 
 if GSL
 issm_sources  +=  $(gsl_sources)
-issm_psources +=  $(gsl_psources)
-endif
-
+endif
 
 if TRANSIENT
 issm_sources  +=  $(transient_sources)
-issm_psources +=  $(transient_psources)
 endif
 
 if STEADYSTATE
 issm_sources  +=  $(steadystate_sources)
-issm_psources +=  $(steadystate_psources)
 endif
 
 if PROGNOSTIC
 issm_sources  +=  $(prognostic_sources)
-issm_psources +=  $(prognostic_psources)
 endif
 
 if THERMAL
 issm_sources  +=  $(thermal_sources)
-issm_psources +=  $(thermal_psources)
 endif
 
 if CONTROL
 issm_sources  +=  $(control_sources)
-issm_psources +=  $(control_psources)
 endif
 
 if HYDROLOGY
 issm_sources  +=  $(hydrology_sources)
-issm_psources +=  $(hydrology_psources)
 endif
 
 if DIAGNOSTIC
 issm_sources  +=  $(diagnostic_sources)
-issm_psources +=  $(diagnostic_psources)
 endif
 
 if BALANCED
 issm_sources  +=  $(balanced_sources)
-issm_psources +=  $(balanced_psources)
 endif
 
@@ -912,5 +910,4 @@
 if SLOPE
 issm_sources  +=  $(slope_sources)
-issm_psources +=  $(slope_psources)
 endif
 
@@ -927,46 +924,37 @@
 endif
 #}}}
-
-#ISSM serial library {{{1
-if SERIAL
-libISSM_a_SOURCES  = $(issm_sources)
-libISSM_a_SOURCES += $(serial_sources)
-libISSM_a_SOURCES += $(bamg_sources)
-libISSM_a_SOURCES += $(kml_sources)
-libISSM_a_CXXFLAGS = -fPIC -D_SERIAL_ -D_GNU_SOURCE -fno-omit-frame-pointer -pthread -D_CPP_  $(CXXFLAGS) $(CXXOPTFLAGS) 
-#libISSM_a_CXXFLAGS = -D_SERIAL_ -DTRILIBRARY -DANSI_DECLARATORS -DNO_TIMER   $(CXXFLAGS) $(CXXOPTFLAGS) 
+#Library flags and sources {{{1
+ALLCXXFLAGS= -fPIC -D_GNU_SOURCE -fno-omit-frame-pointer -pthread -D_CPP_  $(CXXFLAGS) $(CXXOPTFLAGS) 
+
+libISSMCore_a_SOURCES  = $(issm_sources)
+libISSMCore_a_CXXFLAGS = $(ALLCXXFLAGS)
+
+libISSMModules_a_SOURCES = $(module_sources)
+libISSMModules_a_SOURCES += $(bamg_sources)
+libISSMModules_a_SOURCES += $(kml_sources)
+libISSMModules_a_CXXFLAGS = $(ALLCXXFLAGS)
 
 if PYTHON
-libISSM_a_CXXFLAGS+=  -DNPY_NO_DEPRECATED_API 
-libISSM_a_SOURCES += $(python_sources)
+libISSMPython_a_SOURCES = $(python_sources)
+libISSMPython_a_CXXFLAGS= $(ALLCXXFLAGS)
+libISSMPython_a_CXXFLAGS+=  -DNPY_NO_DEPRECATED_API 
 endif
 
 if MATLAB
-libISSM_a_SOURCES += $(matlab_sources)
-endif
-
-endif
-#}}}
-#ISSM parallel library {{{1
-if PARALLEL
-libpISSM_a_SOURCES  = $(issm_sources)
-libpISSM_a_SOURCES += $(issm_psources)
-libpISSM_a_CXXFLAGS = -fPIC -D_PARALLEL_   -D_C_ $(CXXFLAGS) $(CXXOPTFLAGS) 
-endif
+libISSMMatlab_a_SOURCES = $(matlab_sources)
+libISSMMatlab_a_CXXFLAGS= $(ALLCXXFLAGS)
+endif
+
 #}}}
 #Overload library, to overload any non-standard symbols. {{{1
-libOverload_a_SOURCES = ./shared/String/stricmp.c
-libOverload_a_CFLAGS  = -fPIC -D_PARALLEL_   -D_C_ $(COPTFLAGS) $(CFLAGS)
+libISSMOverload_a_SOURCES = ./shared/String/stricmp.c
+libISSMOverload_a_CFLAGS  = -fPIC -D_C_ $(COPTFLAGS) $(CFLAGS)
 #}}}
 
 #Executable {{{1
-if NOPARALLEL
-bin_PROGRAMS = 
-else 
 bin_PROGRAMS = issm 
-endif
 
 #Standard libraries
-LDADD = ./libpISSM.a ./libOverload.a
+LDADD = ./libISSMCore.a ./libISSMOverload.a
 
 #External packages
@@ -974,15 +962,14 @@
 
 issm_SOURCES = solutions/issm.cpp
-issm_CXXFLAGS= -fPIC -D_PARALLEL_ $(CXXFLAGS) $(CXXOPTFLAGS) $(COPTFLAGS) 
+issm_CXXFLAGS= -fPIC $(CXXFLAGS) $(CXXOPTFLAGS) $(COPTFLAGS) 
 #}}}
 #Automatic differentiation: append this fold to the end of the src/c/Makefile.am to get this Makefile.am {{{
 if ADIC2 
-lib_LIBRARIES += libAD.a libpISSMRose.a
+lib_LIBRARIES += libAD.a libISSMRose.a
 
 #ADIC2 library, for automatic differentiation 
 #libAD_a_SOURCES = ./mini1.ad.c
 libAD_a_SOURCES = 
-libAD_a_CFLAGS = -fPIC -D_PARALLEL_   -D_C_ $(COPTFLAGS)
-
+libAD_a_CFLAGS = -fPIC -D_C_ $(COPTFLAGS)
 
 
@@ -990,6 +977,6 @@
 %.r2cpp.cpp : %.cpp
 	testTranslator -rose:o $@ -rose:skipfinalCompileStep -DHAVE_CONFIG_H -D_PARALLEL_ -D_C_ -I. -I../.. $(INCLUDES) $<
-libpISSMRose_a_SOURCES = $(libpISSM_a_SOURCES:.cpp=.r2cpp.cpp)
-libpISSMRose_a_CXXFLAGS= -fPIC -D_PARALLEL_ -D_C_ $(CXXOPTFLAGS)
+libISSMRose_a_SOURCES = $(libISSMCore_a_SOURCES:.cpp=.r2cpp.cpp)
+libISSMRose_a_CXXFLAGS= -fPIC -D_C_ $(CXXOPTFLAGS)
 
 
@@ -1003,7 +990,7 @@
 #Executable
 bin_PROGRAMS +=  issmRose.exe
-issmRose_exe_LDADD = ./libpISSMRose.a $(LDADD)
+issmRose_exe_LDADD = ./libISSMRose.a $(LDADD)
 issmRose_exe_SOURCES = solutions/issm.cpp
-issmRose_exe_CXXFLAGS= -fPIC -D_PARALLEL_  $(CXXOPTFLAGS) $(COPTFLAGS) 
+issmRose_exe_CXXFLAGS= -fPIC $(CXXOPTFLAGS) $(COPTFLAGS) 
 LDADD +=  $(ADIC2LIB) 
 
Index: /issm/trunk-jpl/src/c/include/macros.h
===================================================================
--- /issm/trunk-jpl/src/c/include/macros.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/include/macros.h	(revision 12011)
@@ -48,18 +48,4 @@
 
 #ifdef _SERIAL_
-#ifdef _HAVE_MATLAB_ //{{{2
-#define MODULEBOOT(); ModuleBoot(); \
-	try{
-
-#define MODULEEND(); ModuleEnd(); }\
-	catch(ErrorException &exception){\
-		exception.Report(); \
-		mexErrMsgTxt(""); \
-	}\
-	catch (exception& e) {\
-		_printf_(true,"Standard exception: %s\n",e.what());\
-		mexErrMsgTxt(" ");\
-	}
-#endif //}}}
 #ifdef _HAVE_PYTHON_ //{{{2
 #define MODULEBOOT(); ModuleBoot();  \
@@ -87,7 +73,4 @@
 /*}}}*/
 /* WRAPPER {{{1*/
-#ifdef _HAVE_MATLAB_
-#define WRAPPER(modulename,...) void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) 
-#endif
 #ifdef _HAVE_PYTHON_
 #define WRAPPER(modulename,...)  \
@@ -120,7 +103,4 @@
 /*}}}*/
 /* CHECKARGUMENTS {{{1*/
-#ifdef _HAVE_MATLAB_
-#define CHECKARGUMENTS(NLHS,NRHS,functionpointer) CheckNumMatlabArguments(nlhs,NLHS,nrhs,NRHS,__FUNCT__,functionpointer)
-#endif
 #ifdef _HAVE_PYTHON_
 #define CHECKARGUMENTS(NLHS,NRHS,functionpointer) CheckNumPythonArguments(args, NRHS,functionpointer)
@@ -128,4 +108,3 @@
 /*}}}*/
 
-
 #endif
Index: /issm/trunk-jpl/src/c/include/types.h
===================================================================
--- /issm/trunk-jpl/src/c/include/types.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/include/types.h	(revision 12011)
@@ -16,12 +16,4 @@
 
 /*Define abstract type for I/O: */
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-#include <mex.h>
-typedef const mxArray* ConstDataHandle;  //serially, we are reading data from a matlab array.
-typedef mxArray* DataHandle;  
-#else 
-typedef FILE* ConstDataHandle; //in parallel, we are reading data from a file.
-typedef FILE* DataHandle; 
-#endif
 enum param_type { STRING, INTEGER, STRINGARRAY, DOUBLE, DOUBLEVEC, DOUBLEMAT, PETSCVEC, PETSCMAT };
 
Index: /issm/trunk-jpl/src/c/matlab/Container/Options.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/Container/Options.cpp	(revision 12011)
+++ /issm/trunk-jpl/src/c/matlab/Container/Options.cpp	(revision 12011)
@@ -0,0 +1,55 @@
+/*
+ * \file Options.c
+ * \brief: implementation of the Options class methods that depend on Matlab
+ */
+
+/*Headers: {{{1*/
+#ifdef HAVE_CONFIG_H
+	#include <config.h>
+#else
+#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
+#endif
+
+#include <vector>
+#include <algorithm>
+
+#include "./DataSet.h"
+#include "../shared/shared.h"
+#include "../io/io.h"
+#include "../include/include.h"
+#include "../shared/shared.h"
+#include "../EnumDefinitions/EnumDefinitions.h"
+#if _SERIAL_
+#include "../io/io.h"
+#endif
+/*}}}*/
+
+/*Object constructors and destructor*/
+/*FUNCTION Options::Options(int istart, int nrhs, const mxArray* prhs[]){{{1*/
+Options::Options(int istart, int nrhs, void* module_references){
+
+	const mxArray** prhs = NULL;
+	int            i;
+	char          *name    = NULL;
+	Option *option = NULL;
+
+	/*Recover matlab references: */
+	prhs=(const mxArray**)module_references;
+
+	/*loop over each name and value*/
+	for (i=istart; i<nrhs; i=i+2){
+		if (!mxIsClass(prhs[i],"char")) _error_("Argument %d must be name of option.",i+1);
+
+		FetchData(&name,prhs[i]);
+		if (i+1 == nrhs) _error_("Argument %d must exist and be value of option \"%s\".",i+2,name);
+
+		//_printf_(true,"  Processing option \"%s\" of class \"%s\".\n",name,mxGetClassName(prhs[i+1]));
+		option=(Option*)OptionParse(name,&prhs[i+1]);
+		this->AddOption(option);
+		option=NULL;
+	}
+
+	/*echo the dataset  */
+	//if (this->Size()) for(i=0;i<this->Size();i++) ((Option*)this->GetObjectByOffset(i))->Echo();
+}
+/*}}}*/
Index: /issm/trunk-jpl/src/c/matlab/include/matlab_macros.h
===================================================================
--- /issm/trunk-jpl/src/c/matlab/include/matlab_macros.h	(revision 12011)
+++ /issm/trunk-jpl/src/c/matlab/include/matlab_macros.h	(revision 12011)
@@ -0,0 +1,50 @@
+/* \file macros.h
+ * \brief: global macros used in the whole code
+ */
+
+/*Header {{{1*/
+#ifndef _MATLAB_MACROS_H_
+#define _MATLAB_MACROS_H_
+
+#include "./typedefs.h"
+
+#ifdef HAVE_CONFIG_H
+	#include <config.h>
+#else
+#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
+#endif
+/*}}}*/
+
+/* MODULEBOOT/MODULEEND {{{1*/
+
+/*The following macros hide the error exception handling in a matlab module. Just put 
+ * MODULEBOOT(); and MODULEEND(); at the beginning and end of a module, and c++ exceptions 
+ * will be trapped. Really nifty!*/
+
+//Module Boot //{{{
+#define MODULEBOOT(); ModuleBoot(); \
+	try{
+
+#define MODULEEND(); ModuleEnd(); }\
+	catch(ErrorException &exception){\
+		exception.Report(); \
+		mexErrMsgTxt(""); \
+	}\
+	catch (exception& e) {\
+		_printf_(true,"Standard exception: %s\n",e.what());\
+		mexErrMsgTxt(" ");\
+	}
+//}}}
+/* WRAPPER {{{1*/
+#ifdef _HAVE_MATLAB_
+#define WRAPPER(modulename,...) void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) 
+#endif
+
+/*}}}*/
+/* CHECKARGUMENTS {{{1*/
+#ifdef _HAVE_MATLAB_
+#define CHECKARGUMENTS(NLHS,NRHS,functionpointer) CheckNumMatlabArguments(nlhs,NLHS,nrhs,NRHS,__FUNCT__,functionpointer)
+#endif
+/*}}}*/
+
+#endif
Index: /issm/trunk-jpl/src/c/matlab/io/CheckNumMatlabArguments.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/CheckNumMatlabArguments.cpp	(revision 12011)
+++ /issm/trunk-jpl/src/c/matlab/io/CheckNumMatlabArguments.cpp	(revision 12011)
@@ -0,0 +1,35 @@
+/*!\file CheckNumMatlabArguments.cpp:
+ * \brief: check number of arguments and report an usage error message.
+ */
+
+#ifdef HAVE_CONFIG_H
+	#include <config.h>
+#else
+#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
+#endif
+
+
+#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
+
+#include "../../shared/Exceptions/exceptions.h"
+#include "../../include/include.h"
+#include "mex.h"
+
+int CheckNumMatlabArguments(int nlhs,int NLHS, int nrhs,int NRHS, const char* __FUNCT__, void (*function)( void )){
+	
+	/*checks on arguments on the matlab side: */
+	if (nrhs==0 && nlhs==0) {
+		/*unless NLHS=0 and NRHS=0, we are just asking for documentation: */
+		if (NRHS==0 && NLHS==0)return 1;
+		/* special case: */
+		function();
+		_error_("usage: see above");
+	}
+	else if (nlhs!=NLHS || nrhs!=NRHS ) {
+		function(); 
+		_error_("usage error.");
+	}
+	return 1;
+}
+
+#endif
Index: /issm/trunk-jpl/src/c/matlab/io/FetchMatlabData.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/FetchMatlabData.cpp	(revision 12011)
+++ /issm/trunk-jpl/src/c/matlab/io/FetchMatlabData.cpp	(revision 12011)
@@ -0,0 +1,744 @@
+/*\file FetchData.cpp:
+ * \brief: general I/O interface to fetch data in matlab
+ */
+
+#ifdef HAVE_CONFIG_H
+	#include <config.h>
+#else
+#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
+#endif
+
+#include "../../shared/shared.h"
+#include "../../include/include.h"
+
+#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
+#include <mex.h>
+/*FUNCTION FetchData(DataSet** pdataset,const mxArray* dataref){{{1*/
+void FetchData(DataSet** pdataset,const mxArray* dataref){
+
+	/*output*/
+	DataSet* outdataset=NULL;
+	char*    outdataset_buffer=NULL;
+	int      outdataset_size;
+
+	/*First, check that our reference is a double, otherwise, error out: */
+	if (mxIsClass(dataref,"double")){
+			
+		/*We need to copy the data pointed by dataref, so that our dataset is not actually a pointer!:*/
+		if (!mxIsEmpty(dataref)){
+			outdataset_buffer=(char*)mxGetPr(dataref);
+			outdataset_size=mxGetM(dataref)*mxGetN(dataref);
+			if(outdataset_size)outdataset=DataSetDemarshall(outdataset_buffer);
+		}
+	}
+	else{
+		if (mxIsEmpty(dataref)){
+			/*Nothing to pick up. Just initialize pointer to NULL, and warn the server we are not uploading anything: */
+			outdataset_size=0;
+			outdataset=NULL;
+		}
+		else{
+			/*This is an error: we don't have the correct input!: */
+			_error_("Input parameter of class %s not supported yet",mxGetClassName(dataref));
+		}
+	}
+
+	/*Assign output pointers:*/
+	*pdataset=outdataset;
+}
+/*}}}*/
+/*FUNCTION FetchData(double** pmatrix,int* pM,int *pN,const mxArray* dataref){{{1*/
+void FetchData(double** pmatrix,int* pM,int *pN,const mxArray* dataref){
+
+	double*  outmatrix=NULL;
+	int      outmatrix_rows,outmatrix_cols;
+
+	if(mxIsEmpty(dataref) ){
+		/*Nothing to pick up. Just initialize matrix pointer to NULL: */
+		outmatrix_rows=0;
+		outmatrix_cols=0;
+		outmatrix=NULL;
+	}
+	else if(mxIsClass(dataref,"double") || mxIsClass(dataref,"single")){
+		/*Check dataref is not pointing to NaN: */
+		if ( mxIsNaN(*(mxGetPr(dataref))) && (mxGetM(dataref)==1) && (mxGetN(dataref)==1) ){
+			outmatrix_rows=0;
+			outmatrix_cols=0;
+			outmatrix=NULL;
+		}
+		else{
+			/*Convert matlab matrix to double* matrix: */
+			MatlabMatrixToDoubleMatrix(&outmatrix,&outmatrix_rows,&outmatrix_cols,dataref);
+		}
+	}
+	else{
+		/*This is an error: we don't have the correct input!: */
+		_error_("Input parameter of class %s not supported yet",mxGetClassName(dataref));
+	}
+			
+	/*Assign output pointers:*/
+	*pmatrix=outmatrix;
+	if (pM)*pM=outmatrix_rows;
+	if (pN)*pN=outmatrix_cols;
+
+}
+/*}}}*/
+/*FUNCTION FetchData(double** pmatrix,int* pnumel,int* pndims,int** psize,const mxArray* dataref){{{1*/
+void FetchData(double** pmatrix,int* pnumel,int* pndims,int** psize,const mxArray* dataref){
+
+	double*  outmatrix=NULL;
+	int      outmatrix_numel,outmatrix_ndims;
+	int*     outmatrix_size=NULL;
+
+	if(mxIsEmpty(dataref) ){
+		/*Nothing to pick up. Just initialize matrix pointer to NULL: */
+		outmatrix_numel=0;
+		outmatrix_ndims=0;
+		outmatrix_size =NULL;
+		outmatrix=NULL;
+	}
+	else if (mxIsClass(dataref,"double") ){
+
+		/*Check dataref is not pointing to NaN: */
+		if ( mxIsNaN(*(mxGetPr(dataref))) && (mxGetNumberOfElements(dataref)==1) ){
+			outmatrix_numel=0;
+			outmatrix_ndims=0;
+			outmatrix_size =NULL;
+			outmatrix=NULL;
+		}
+		else{
+
+			/*Convert matlab n-dim array to double* matrix: */
+			MatlabNArrayToNArray(&outmatrix,&outmatrix_numel,&outmatrix_ndims,&outmatrix_size,dataref);
+		}
+	}
+	else{
+		/*This is an error: we don't have the correct input!: */
+		_error_("Input parameter of class %s not supported yet",mxGetClassName(dataref));
+	}
+			
+	/*Assign output pointers:*/
+	*pmatrix=outmatrix;
+	if (pnumel)*pnumel=outmatrix_numel;
+	if (pndims)*pndims=outmatrix_ndims;
+	if (psize )*psize =outmatrix_size;
+	else xfree((void**)&outmatrix_size);
+
+}
+/*}}}*/
+/*FUNCTION FetchData(int** pmatrix,int* pM,int *pN,const mxArray* dataref){{{1*/
+void FetchData(int** pmatrix,int* pM,int *pN,const mxArray* dataref){
+
+	int     i,outmatrix_rows,outmatrix_cols;
+	double *doublematrix=NULL;
+	int    *outmatrix=NULL;
+
+	if(mxIsEmpty(dataref) ){
+		/*Nothing to pick up. Just initialize matrix pointer to NULL: */
+		outmatrix_rows=0;
+		outmatrix_cols=0;
+		outmatrix=NULL;
+	}
+	else if (mxIsClass(dataref,"double") ){
+
+		/*Check dataref is not pointing to NaN: */
+		if ( mxIsNaN(*(mxGetPr(dataref))) && (mxGetM(dataref)==1) && (mxGetN(dataref)==1) ){
+			outmatrix_rows=0;
+			outmatrix_cols=0;
+			outmatrix=NULL;
+		}
+		else{
+
+			/*Convert matlab matrix to double* matrix: */
+			MatlabMatrixToDoubleMatrix(&doublematrix,&outmatrix_rows,&outmatrix_cols,dataref);
+
+			/*Convert double matrix into integer matrix: */
+			outmatrix=(int*)xmalloc(outmatrix_rows*outmatrix_cols*sizeof(int));
+			for(i=0;i<outmatrix_rows*outmatrix_cols;i++)outmatrix[i]=(int)doublematrix[i];
+		}
+	}
+	else{
+		/*This is an error: we don't have the correct input!: */
+		_error_("Input parameter of class %s not supported yet",mxGetClassName(dataref));
+	}
+
+	/*Assign output pointers:*/
+	*pmatrix=outmatrix;
+	if (pM)*pM=outmatrix_rows;
+	if (pN)*pN=outmatrix_cols;
+}
+/*}}}*/
+/*FUNCTION FetchData(bool** pmatrix,int* pM,int *pN,const mxArray* dataref){{{1*/
+void FetchData(bool** pmatrix,int* pM,int *pN,const mxArray* dataref){
+
+	int     i,outmatrix_rows,outmatrix_cols;
+	double *doublematrix=NULL;
+	bool   *outmatrix=NULL;
+
+	if(mxIsEmpty(dataref) ){
+		/*Nothing to pick up. Just initialize matrix pointer to NULL: */
+		outmatrix_rows=0;
+		outmatrix_cols=0;
+		outmatrix=NULL;
+	}
+	else if (mxIsClass(dataref,"double") ){
+
+		/*Check dataref is not pointing to NaN: */
+		if ( mxIsNaN(*(mxGetPr(dataref))) && (mxGetM(dataref)==1) && (mxGetN(dataref)==1) ){
+			outmatrix_rows=0;
+			outmatrix_cols=0;
+			outmatrix=NULL;
+		}
+		else{
+
+			/*Convert matlab matrix to double* matrix: */
+			MatlabMatrixToDoubleMatrix(&doublematrix,&outmatrix_rows,&outmatrix_cols,dataref);
+
+			/*Convert double matrix into integer matrix: */
+			outmatrix=(bool*)xmalloc(outmatrix_rows*outmatrix_cols*sizeof(bool));
+			for(i=0;i<outmatrix_rows;i++)outmatrix[i]=(bool)doublematrix[i];
+		}
+	}
+	else{
+		/*This is an error: we don't have the correct input!: */
+		_error_("Input parameter of class %s not supported yet",mxGetClassName(dataref));
+	}
+
+	/*Assign output pointers:*/
+	*pmatrix=outmatrix;
+	if (pM)*pM=outmatrix_rows;
+	if (pN)*pN=outmatrix_cols;
+}
+/*}}}*/
+/*FUNCTION FetchData(bool** pmatrix,int* pnumel,int* pndims,int** psize,const mxArray* dataref){{{1*/
+void FetchData(bool** pmatrix,int* pnumel,int* pndims,int** psize,const mxArray* dataref){
+
+	int      i;
+	int      outmatrix_numel,outmatrix_ndims;
+	int*     outmatrix_size=NULL;
+	double*  doublematrix=NULL;
+	bool*    outmatrix=NULL;
+
+	if(mxIsEmpty(dataref) ){
+		/*Nothing to pick up. Just initialize matrix pointer to NULL: */
+		outmatrix_numel=0;
+		outmatrix_ndims=0;
+		outmatrix_size =NULL;
+		outmatrix=NULL;
+	}
+	else if (mxIsClass(dataref,"logical") ){
+
+		/*Check dataref is not pointing to NaN: */
+		if ( mxIsNaN(*((bool*)mxGetData(dataref))) && (mxGetNumberOfElements(dataref)==1) ){
+			outmatrix_numel=0;
+			outmatrix_ndims=0;
+			outmatrix_size =NULL;
+			outmatrix=NULL;
+		}
+		else{
+
+			/*Convert matlab n-dim array to bool* matrix: */
+			MatlabNArrayToNArray(&outmatrix,&outmatrix_numel,&outmatrix_ndims,&outmatrix_size,dataref);
+		}
+	}
+	else if (mxIsClass(dataref,"double") ){
+
+		/*Check dataref is not pointing to NaN: */
+		if ( mxIsNaN(*(mxGetPr(dataref))) && (mxGetNumberOfElements(dataref)==1) ){
+			outmatrix_numel=0;
+			outmatrix_ndims=0;
+			outmatrix_size =NULL;
+			outmatrix=NULL;
+		}
+		else{
+
+			/*Convert matlab n-dim array to double* matrix: */
+			MatlabNArrayToNArray(&doublematrix,&outmatrix_numel,&outmatrix_ndims,&outmatrix_size,dataref);
+
+			/*Convert double matrix into bool matrix: */
+			outmatrix=(bool*)xmalloc(outmatrix_numel*sizeof(bool));
+			for(i=0;i<outmatrix_numel;i++)outmatrix[i]=(bool)doublematrix[i];
+			xfree((void**)&doublematrix);
+		}
+	}
+	else{
+		/*This is an error: we don't have the correct input!: */
+		_error_("Input parameter of class %s not supported yet",mxGetClassName(dataref));
+	}
+			
+	/*Assign output pointers:*/
+	*pmatrix=outmatrix;
+	if (pnumel)*pnumel=outmatrix_numel;
+	if (pndims)*pndims=outmatrix_ndims;
+	if (psize )*psize =outmatrix_size;
+	else xfree((void**)&outmatrix_size);
+
+}
+/*}}}*/
+/*FUNCTION FetchData(Matrix** pmatrix,const mxArray* dataref){{{1*/
+void FetchData(Matrix** pmatrix,const mxArray* dataref){
+	
+	Matrix* outmatrix=NULL;
+	int dummy=0;
+
+	if (mxIsClass(dataref,"double") ){
+			
+		/*Convert matlab matrix to matrix: */
+		outmatrix=MatlabMatrixToMatrix(dataref);
+
+	}
+	else{
+		/*This is an error: we don't have the correct input!: */
+		_error_("Input parameter of class %s not supported yet",mxGetClassName(dataref));
+	}
+
+	/*Assign output pointers:*/
+	*pmatrix=outmatrix;
+}
+/*}}}*/
+/*FUNCTION FetchData(double** pvector,int* pM,const mxArray* dataref){{{1*/
+void FetchData(double** pvector,int* pM,const mxArray* dataref){
+
+	double* outvector=NULL;
+	int outvector_rows;
+
+	if(mxIsEmpty(dataref)){
+		/*Nothing to pick up. Just initialize matrix pointer to NULL: */
+		outvector_rows=0;
+		outvector=NULL;
+	}
+	else if (mxIsClass(dataref,"double") ){
+
+		/*Convert matlab vector to double*  vector: */
+		MatlabVectorToDoubleVector(&outvector,&outvector_rows,dataref);
+
+	}
+	else{
+		/*This is an error: we don't have the correct input!: */
+		_error_("Input parameter of class %s not supported yet",mxGetClassName(dataref));
+	}
+
+	/*Assign output pointers:*/
+	*pvector=outvector;
+	if (pM)*pM=outvector_rows;
+}
+/*}}}*/
+/*FUNCTION FetchData(int** pvector,int* pM,const mxArray* dataref){{{1*/
+void FetchData(int** pvector,int* pM,const mxArray* dataref){
+
+	int    i;
+	double *doublevector   = NULL;
+	int    *outvector      = NULL;
+	int     outvector_rows;
+
+	if(mxIsEmpty(dataref)){
+		/*Nothing to pick up. Just initialize matrix pointer to NULL: */
+		outvector_rows=0;
+		outvector=NULL;
+	}
+	else if (mxIsClass(dataref,"double") ){
+
+		/*Convert matlab vector to double*  vector: */
+		MatlabVectorToDoubleVector(&doublevector,&outvector_rows,dataref);
+
+		/*Convert double vector into integer vector: */
+		outvector=(int*)xmalloc(outvector_rows*sizeof(int));
+		for(i=0;i<outvector_rows;i++)outvector[i]=(int)doublevector[i];
+	}
+	else{
+		/*This is an error: we don't have the correct input!: */
+		_error_("Input parameter of class %s not supported yet",mxGetClassName(dataref));
+	}
+
+	/*Assign output pointers:*/
+	*pvector=outvector;
+	if (pM)*pM=outvector_rows;
+}
+/*}}}*/
+/*FUNCTION FetchData(bool** pvector,int* pM,const mxArray* dataref){{{1*/
+void FetchData(bool** pvector,int* pM,const mxArray* dataref){
+
+	int    i;
+	double *doublevector   = NULL;
+	bool   *outvector      = NULL;
+	int     outvector_rows;
+
+	if(mxIsEmpty(dataref)){
+		/*Nothing to pick up. Just initialize matrix pointer to NULL: */
+		outvector_rows=0;
+		outvector=NULL;
+	}
+	else if (mxIsClass(dataref,"double") ){
+
+		/*Convert matlab vector to double*  vector: */
+		MatlabVectorToDoubleVector(&doublevector,&outvector_rows,dataref);
+
+		/*Convert double vector into integer vector: */
+		outvector=(bool*)xmalloc(outvector_rows*sizeof(bool));
+		for(i=0;i<outvector_rows;i++)outvector[i]=(bool)doublevector[i];
+	}
+	else{
+		/*This is an error: we don't have the correct input!: */
+		_error_("Input parameter of class %s not supported yet",mxGetClassName(dataref));
+	}
+
+	/*Assign output pointers:*/
+	*pvector=outvector;
+	if (pM)*pM=outvector_rows;
+}
+/*}}}*/
+/*FUNCTION FetchData(float** pvector,int* pM,const mxArray* dataref){{{1*/
+void FetchData(float** pvector,int* pM,const mxArray* dataref){
+
+	int    i;
+	double *doublevector   = NULL;
+	float  *outvector      = NULL;
+	int     outvector_rows;
+
+	if(mxIsEmpty(dataref)){
+		/*Nothing to pick up. Just initialize matrix pointer to NULL: */
+		outvector_rows=0;
+		outvector=NULL;
+	}
+	else if (mxIsClass(dataref,"double") ){
+
+		/*Convert matlab vector to double*  vector: */
+		MatlabVectorToDoubleVector(&doublevector,&outvector_rows,dataref);
+
+		/*Convert double vector into float vector: */
+		outvector=(float*)xmalloc(outvector_rows*sizeof(float));
+		for(i=0;i<outvector_rows;i++)outvector[i]=(float)doublevector[i];
+	}
+	else{
+		/*This is an error: we don't have the correct input!: */
+		_error_("Input parameter of class %s not supported yet",mxGetClassName(dataref));
+	}
+
+	/*Assign output pointers:*/
+	*pvector=outvector;
+	if (pM)*pM=outvector_rows;
+}
+/*}}}*/
+/*FUNCTION FetchData(Vector** pvector,const mxArray* dataref){{{1*/
+void FetchData(Vector** pvector,const mxArray* dataref){
+	
+	Vector* vector=NULL;
+	int dummy;
+
+	if(mxIsEmpty(dataref)){
+		/*Nothing to pick up. Just initialize matrix pointer to NULL: */
+		vector=new Vector(0);
+	}
+	else if (mxIsClass(dataref,"double") ){
+
+		/*Convert matlab vector to petsc vector: */
+		vector=MatlabVectorToVector(dataref);
+	}
+	else{
+		/*This is an error: we don't have the correct input!: */
+		_error_("Input parameter of class %s not supported yet",mxGetClassName(dataref));
+	}
+
+	/*Assign output pointers:*/
+	*pvector=vector;
+}
+/*}}}*/
+/*FUNCTION FetchData(char** pstring,const mxArray* dataref){{{1*/
+void FetchData(char** pstring,const mxArray* dataref){
+
+	char* outstring=NULL;
+
+
+	/*Ok, the string should be coming directly from the matlab workspace: */
+	if (!mxIsClass(dataref,"char")){
+		_error_("input data_type is not a string!");
+	}
+	else{
+		/*Recover the string:*/
+		int stringlen;
+		
+		stringlen = mxGetM(dataref)*mxGetN(dataref)+1;
+		outstring = (char*)xmalloc(sizeof(mxChar)*stringlen);
+		mxGetString(dataref,outstring,stringlen);
+	}
+
+	/*Assign output pointers:*/
+	*pstring=outstring;
+}
+/*FUNCTION FetchData(char** pmatrix,int* pnumel,int* pndims,int** psize,const mxArray* dataref){{{1*/
+void FetchData(char** pmatrix,int* pnumel,int* pndims,int** psize,const mxArray* dataref){
+
+	int      outmatrix_numel,outmatrix_ndims;
+	int*     outmatrix_size=NULL;
+	char*    outmatrix=NULL;
+
+	if(mxIsEmpty(dataref) ){
+		/*Nothing to pick up. Just initialize matrix pointer to NULL: */
+		outmatrix_numel=0;
+		outmatrix_ndims=0;
+		outmatrix_size =NULL;
+		outmatrix=NULL;
+	}
+	else if (mxIsClass(dataref,"char") ){
+
+		/*Check dataref is not pointing to NaN: */
+		if ( mxIsNaN(*(mxGetPr(dataref))) && (mxGetNumberOfElements(dataref)==1) ){
+			outmatrix_numel=0;
+			outmatrix_ndims=0;
+			outmatrix_size =NULL;
+			outmatrix=NULL;
+		}
+		else{
+
+			/*Convert matlab n-dim array to char* matrix: */
+			MatlabNArrayToNArray(&outmatrix,&outmatrix_numel,&outmatrix_ndims,&outmatrix_size,dataref);
+		}
+	}
+	else{
+		/*This is an error: we don't have the correct input!: */
+		_error_("Input parameter of class %s not supported yet",mxGetClassName(dataref));
+	}
+			
+	/*Assign output pointers:*/
+	*pmatrix=outmatrix;
+	if (pnumel)*pnumel=outmatrix_numel;
+	if (pndims)*pndims=outmatrix_ndims;
+	if (psize )*psize =outmatrix_size;
+	else xfree((void**)&outmatrix_size);
+
+}
+/*}}}*/
+/*FUNCTION FetchData(double* pscalar,const mxArray* dataref){{{1*/
+void FetchData(double* pscalar,const mxArray* dataref){
+
+	double scalar;
+
+	if (!mxIsClass(dataref,"double")){
+		_error_("input data_type is not a double!");
+	}
+	else{
+		/*Recover the double: */
+		scalar=mxGetScalar(dataref);
+	}
+
+	/*Assign output pointers:*/
+	*pscalar=scalar;
+}
+/*}}}*/
+/*FUNCTION FetchData(int* pinteger,const mxArray* dataref){{{1*/
+void FetchData(int* pinteger,const mxArray* dataref){
+
+	int integer;
+
+	if (!mxIsClass(dataref,"double")){
+		_error_("input data_type is not a scalar!");
+	}
+	else{
+		/*Recover the double: */
+		integer=(int)mxGetScalar(dataref);
+	}
+
+	/*Assign output pointers:*/
+	*pinteger=integer;
+}
+/*}}}*/
+/*FUNCTION FetchData(bool* pboolean,const mxArray* dataref){{{1*/
+void FetchData(bool* pboolean,const mxArray* dataref){
+
+	bool* mxbool_ptr=NULL;
+
+	if (mxIsClass(dataref,"logical")){
+		if(mxGetM(dataref)!=1) _error_("input data is not of size 1x1");
+		if(mxGetN(dataref)!=1) _error_("input data is not of size 1x1");
+		mxbool_ptr=mxGetLogicals(dataref);
+	}
+	else{
+		_error_("input data_type is not a bool!");
+	}
+
+	*pboolean=*mxbool_ptr;
+}
+/*}}}*/
+/*FUNCTION FetchData(Parameters** pparameters, DataHandle dataref){{{1*/
+void FetchData(Parameters** pparameters, DataHandle dataref){
+
+	int i,j;
+	int count;
+
+	/*output: */
+	Param* param=NULL;
+	Parameters* parameters=NULL;
+
+	/*intermediary: */
+	int M,N;
+	double* tmatrix=NULL;
+	double* matrix=NULL;
+	char**  stringarray=NULL;
+	double** array=NULL;
+	int*     mdims_array=NULL;
+	int*     ndims_array=NULL;
+	int nfields;
+	char* name=NULL;
+	mxArray* pfield=NULL;
+	mxArray* pfield2=NULL;
+	int enum_type;
+
+
+	/*First, create parameters : */
+	parameters=new Parameters();
+
+	/*go through matlab params structure, and create Param object for each field: */
+
+	nfields=mxGetNumberOfFields(dataref);
+
+	for(count=0;count<nfields;count++){
+
+		/*Get i'th field: */
+		name=(char*)mxGetFieldNameByNumber(dataref,count);
+		enum_type=StringToEnumx(name);
+		pfield=mxGetFieldByNumber(dataref,0,count);
+		_assert_(pfield);
+		
+		/*Check type of field: */
+		if (mxIsClass(pfield,"double")){
+			
+			M=mxGetM(pfield);
+			N=mxGetN(pfield);
+
+			if (M==0 | N==0){
+				_error_("array in parameters structure field %s is of size 0",name);
+			}
+			if (M==1 && N==1){
+				/*we have a simple scalar: */
+				param= new DoubleParam(enum_type,*mxGetPr(pfield));
+				parameters->AddObject(param);
+
+			}
+			else{
+				if (N==1){
+					
+					/*vector: */
+					param= new DoubleVecParam(enum_type,mxGetPr(pfield),M);
+					parameters->AddObject(param);
+
+				}
+				else{
+					/*matrix: first, transpose, then plug into Param */
+					matrix=mxGetPr(pfield);
+					tmatrix=(double*)xmalloc(M*N*sizeof(double));
+					for (i=0;i<M;i++){
+						for(j=0;j<N;j++){
+							*(tmatrix+N*i+j)=*(matrix+M*j+i);
+						}
+					}
+
+					param= new DoubleMatParam(enum_type,tmatrix,M,N);
+					parameters->AddObject(param);
+	
+					/*Free ressources:*/
+					xfree((void**)&tmatrix);
+				}
+			}
+
+		}
+		else if (mxIsClass(pfield,"logical")){
+
+			M=mxGetM(pfield);
+			N=mxGetN(pfield);
+
+			if (M==0 | N==0){
+				_error_("array in parameters structure field %s is of size 0",name);
+			}
+			if (M==1 && N==1){
+				/*we have a simple bool: */
+				param= new BoolParam(enum_type,*mxGetLogicals(pfield));
+				parameters->AddObject(param);
+
+			}
+			else{
+				_error_("Matrices of Booleans not supported yet in parameters");
+			}
+		}
+		else if (mxIsClass(pfield,"char")){
+			/* we have a string parameter:*/
+			
+			int stringlen;
+			char* string=NULL;
+			
+			stringlen = mxGetM(pfield)*mxGetN(pfield)+1;
+			string = (char*)xmalloc(sizeof(mxChar)*stringlen);
+			mxGetString(pfield,string,stringlen);
+
+			param= new StringParam(enum_type,string);
+			parameters->AddObject(param);
+
+			xfree((void**)&string);
+		}
+		else if (mxIsClass(pfield,"cell")){
+
+			/*This can be a string array, or a matrix array. Check the first 
+			 *element type to decide: */
+			pfield2=mxGetCell(pfield,0);
+			if (mxIsClass(pfield2,"char")){
+				
+				/*string array: */
+				M=mxGetM(pfield);
+				stringarray=(char**)xmalloc(M*sizeof(char*));
+
+				for(i=0;i<M;i++){
+					char* descriptor=NULL;
+					pfield2=mxGetCell(pfield,i);
+					FetchData(&descriptor,pfield2);
+					stringarray[i]=descriptor;
+				}
+
+				param= new StringArrayParam(enum_type,stringarray,M);
+				parameters->AddObject(param);
+
+				/*Free ressources:*/
+				for(i=0;i<M;i++){
+					char* descriptor=stringarray[i];
+					xfree((void**)&descriptor);
+				}
+				xfree((void**)&stringarray);
+
+			}
+			else{
+				
+				/*matrix array: */
+				M=mxGetM(pfield);
+				array=(double**)xmalloc(M*sizeof(double*));
+				mdims_array=(int*)xmalloc(M*sizeof(int));
+				ndims_array=(int*)xmalloc(M*sizeof(int));
+
+				for(i=0;i<M;i++){
+					double* matrix=NULL;
+					int     m,n;
+					pfield2=mxGetCell(pfield,i);
+					FetchData(&matrix,&m,&n,pfield2);
+					array[i]=matrix;
+					mdims_array[i]=m;
+					ndims_array[i]=n;
+				}
+
+				param= new DoubleMatArrayParam(enum_type,array,M,mdims_array,ndims_array);
+				parameters->AddObject(param);
+
+				/*Free ressources:*/
+				for(i=0;i<M;i++){
+					double* matrix=array[i];
+					xfree((void**)&matrix);
+				}
+				xfree((void**)&array);
+				xfree((void**)&mdims_array);
+				xfree((void**)&ndims_array);
+			}
+		}
+		else _error_("Parameters structure field %s has a format that is not supported: %s",name,mxGetClassName(pfield));
+	}
+
+	/*Assign output pointers:*/
+	*pparameters=parameters;
+}
+/*}}}*/
+#endif
Index: /issm/trunk-jpl/src/c/matlab/io/MatlabMatrixToDoubleMatrix.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/MatlabMatrixToDoubleMatrix.cpp	(revision 12011)
+++ /issm/trunk-jpl/src/c/matlab/io/MatlabMatrixToDoubleMatrix.cpp	(revision 12011)
@@ -0,0 +1,101 @@
+/* \file MatlabMatrixToDoubleMatrix.cpp
+ * \brief: convert a sparse or dense matlab matrix to a double* pointer
+ */
+
+
+#ifdef HAVE_CONFIG_H
+	#include <config.h>
+#else
+#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
+#endif
+
+
+/*Matlab includes: */
+#include "mex.h"
+
+#include "../../shared/shared.h"
+
+int MatlabMatrixToDoubleMatrix(double** pmatrix,int* pmatrix_rows,int* pmatrix_cols,const mxArray* mxmatrix){
+
+	int     i,j,count,rows,cols;
+	double *pmxdoublematrix = NULL;
+	float  *pmxsinglematrix = NULL;
+
+	/*output: */
+	double* matrix=NULL;
+
+	/*matlab indices: */
+	mwIndex*    ir=NULL;
+	mwIndex*    jc=NULL;
+
+	/*Ok, first check if we are dealing with a sparse or full matrix: */
+	if (mxIsSparse(mxmatrix)){
+
+		/*Dealing with sparse matrix: recover size first: */
+		pmxdoublematrix=(double*)mxGetPr(mxmatrix);
+		rows=mxGetM(mxmatrix);
+		cols=mxGetN(mxmatrix);
+		
+		if(rows*cols){
+			matrix=(double*)xcalloc(rows*cols,sizeof(double));
+
+			/*Now, get ir,jc and pr: */
+			ir=mxGetIr(mxmatrix);
+			jc=mxGetJc(mxmatrix);
+
+			/*Now, start inserting data into double* matrix: */
+			count=0;
+			for(i=0;i<cols;i++){
+				for(j=0;j<(jc[i+1]-jc[i]);j++){
+					matrix[rows*ir[count]+i]=pmxdoublematrix[count];
+					count++;
+				}
+			}
+		}
+
+	}
+	else if(mxIsClass(mxmatrix,"double")){
+		/*Dealing with dense matrix: recover pointer and size: */
+		pmxdoublematrix=(double*)mxGetPr(mxmatrix);
+		rows=mxGetM(mxmatrix);
+		cols=mxGetN(mxmatrix);
+		
+		/*Create serial matrix: */
+		if(rows*cols){
+			matrix=(double*)xcalloc(rows*cols,sizeof(double));
+
+			for(i=0;i<rows;i++){
+				for(j=0;j<cols;j++){
+					matrix[cols*i+j]=(double)pmxdoublematrix[rows*j+i];
+				}
+			}
+		}
+	}
+	else if(mxIsClass(mxmatrix,"single")){
+		/*Dealing with dense matrix: recover pointer and size: */
+		pmxsinglematrix=(float*)mxGetPr(mxmatrix);
+		rows=mxGetM(mxmatrix);
+		cols=mxGetN(mxmatrix);
+
+		/*Create serial matrix: */
+		if(rows*cols){
+			matrix=(double*)xcalloc(rows*cols,sizeof(double));
+
+			for(i=0;i<rows;i++){
+				for(j=0;j<cols;j++){
+					matrix[cols*i+j]=(double)pmxsinglematrix[rows*j+i];
+				}
+			}
+		}
+	}
+	else{
+		_error_("Matlab matrix type Not implemented yet");
+	}
+
+	/*Assign output pointer: */
+	*pmatrix=matrix;
+	*pmatrix_rows=rows;
+	*pmatrix_cols=cols;
+
+	return 1;
+}
Index: /issm/trunk-jpl/src/c/matlab/io/MatlabMatrixToMatrix.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/MatlabMatrixToMatrix.cpp	(revision 12011)
+++ /issm/trunk-jpl/src/c/matlab/io/MatlabMatrixToMatrix.cpp	(revision 12011)
@@ -0,0 +1,16 @@
+Matrix* MatlabMatrixToMatrix(const mxArray* mxmatrix){
+
+	int dummy;
+	Matrix* matrix=NULL;
+
+	/*allocate matrix object: */
+	matrix=new Matrix();
+
+	#ifdef _HAVE_PETSC_
+	MatlabMatrixToPetscMatrix(&matrix->matrix,NULL,NULL,mxmatrix);
+	#else
+	matrix->matrix=MatlabMatrixToSeqMat(mxmatrix);
+	#endif
+	
+	return matrix;
+}
Index: /issm/trunk-jpl/src/c/matlab/io/MatlabMatrixToPetscMatrix.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/MatlabMatrixToPetscMatrix.cpp	(revision 12011)
+++ /issm/trunk-jpl/src/c/matlab/io/MatlabMatrixToPetscMatrix.cpp	(revision 12011)
@@ -0,0 +1,123 @@
+/* \file MatlabMatrixToPetscMatrix.cpp
+ * \brief: convert a sparse or dense matlab matrix to a serial Petsc matrix:
+ */
+
+
+#ifdef HAVE_CONFIG_H
+	#include <config.h>
+#else
+#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
+#endif
+
+
+#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
+
+/*Petsc includes: */
+#include "petscmat.h"
+#include "petscvec.h"
+#include "petscksp.h"
+
+/*Matlab includes: */
+#include "mex.h"
+
+#include "../../../shared/shared.h"
+
+int MatlabMatrixToPetscMatrix(Mat* pmatrix,int* pmatrix_rows,int* pmatrix_cols,const mxArray* mxmatrix){
+
+	int rows, cols;
+	double* mxmatrix_ptr=NULL;
+	double* tmatrix=NULL;
+	int ierr;
+	int i,j;
+
+	/*output: */
+	Mat matrix=NULL;
+
+	/*matlab indices: */
+	mwIndex*    ir=NULL;
+	mwIndex*    jc=NULL;
+	double* pr=NULL;
+	int     count;
+	int     nnz;
+	int     nz;
+
+	/*petsc indices: */
+	int* idxm=NULL;
+	int* idxn=NULL;
+	
+	/*Ok, first check if we are dealing with a sparse or full matrix: */
+	if (mxIsSparse(mxmatrix)){
+
+		/*Dealing with sparse matrix: recover size first: */
+		mxmatrix_ptr=(double*)mxGetPr(mxmatrix);
+		rows=mxGetM(mxmatrix);
+		cols=mxGetN(mxmatrix);
+		nnz=mxGetNzmax(mxmatrix);
+		if(rows){
+			nz=(int)((double)nnz/(double)rows);
+		}
+		else{
+			nz=0;
+		}
+
+		ierr=MatCreateSeqAIJ(PETSC_COMM_SELF,rows,cols,nz,PETSC_NULL,&matrix);CHKERRQ(ierr);
+
+		/*Now, get ir,jc and pr: */
+		pr=mxGetPr(mxmatrix);
+		ir=mxGetIr(mxmatrix);
+		jc=mxGetJc(mxmatrix);
+
+		/*Now, start inserting data into sparse matrix: */
+		count=0;
+		for(i=0;i<cols;i++){
+			for(j=0;j<(jc[i+1]-jc[i]);j++){
+				MatSetValue(matrix,ir[count],i,pr[count],INSERT_VALUES);
+				count++;
+			}
+		}
+
+	}
+	else{
+
+		/*Dealing with dense matrix: recover pointer and size: */
+		mxmatrix_ptr=(double*)mxGetPr(mxmatrix);
+		rows=mxGetM(mxmatrix);
+		cols=mxGetN(mxmatrix);
+
+		/*transpose, as Petsc now does not allows MAT_COLUMN_ORIENTED matrices in MatSetValues: */
+		tmatrix=(double*)xmalloc(rows*cols*sizeof(double));
+		for(i=0;i<cols;i++){
+			for(j=0;j<rows;j++){
+				*(tmatrix+rows*i+j)=*(mxmatrix_ptr+cols*j+i);
+			}
+		}
+
+		/*Create serial matrix: */
+		ierr=MatCreateSeqDense(PETSC_COMM_SELF,rows,cols,NULL,&matrix);CHKERRQ(ierr);
+
+		/*Insert mxmatrix_ptr values into petsc matrix: */
+		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;
+
+		ierr=MatSetValues(matrix,rows,idxm,cols,idxn,tmatrix,INSERT_VALUES); CHKERRQ(ierr);
+
+		xfree((void**)&tmatrix);
+
+	}
+
+	/*Assemble matrix: */
+	MatAssemblyBegin(matrix,MAT_FINAL_ASSEMBLY); 
+	MatAssemblyEnd(matrix,MAT_FINAL_ASSEMBLY);
+
+
+	/*Assign output pointer: */
+	*pmatrix=matrix;
+	if(pmatrix_rows) *pmatrix_rows=rows;
+	if(pmatrix_cols) *pmatrix_cols=cols;
+
+	return 1;
+}
+#endif
Index: /issm/trunk-jpl/src/c/matlab/io/MatlabMatrixToSeqMat.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/MatlabMatrixToSeqMat.cpp	(revision 12011)
+++ /issm/trunk-jpl/src/c/matlab/io/MatlabMatrixToSeqMat.cpp	(revision 12011)
@@ -0,0 +1,9 @@
+SeqMat* MatlabMatrixToSeqMat(const mxArray* dataref){
+
+	SeqMat* output=NULL;
+
+	output=new SeqMat();
+	MatlabMatrixToDoubleMatrix(&output->matrix,&output->M,&output->N,dataref);
+	return output;
+
+}
Index: /issm/trunk-jpl/src/c/matlab/io/MatlabNArrayToNArray.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/MatlabNArrayToNArray.cpp	(revision 12011)
+++ /issm/trunk-jpl/src/c/matlab/io/MatlabNArrayToNArray.cpp	(revision 12011)
@@ -0,0 +1,274 @@
+/* \file MatlabNArrayToNArray.cpp
+ * \brief: convert a sparse or dense matlab n-dimensional array to cpp n-dimensional array
+ */
+
+
+#ifdef HAVE_CONFIG_H
+	#include <config.h>
+#else
+#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
+#endif
+
+#include "../../shared/shared.h"
+#include "../../include/include.h"
+
+#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
+#include <mex.h>
+
+/*FUNCTION MatlabNArrayToNArray(double** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){{{1*/
+int MatlabNArrayToNArray(double** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){
+
+	int  i,j,rows,cols;
+	int  numel,ndims;
+	int *size,*dims;
+	double* mxmatrix_ptr=NULL;
+	const mwSize* ipt=NULL;
+
+	/*output: */
+	double* matrix=NULL;
+
+	/*matlab indices: */
+	mwIndex*    ir=NULL;
+	mwIndex*    jc=NULL;
+	double* pr=NULL;
+	int     count;
+	int     nnz;
+	int     nz;
+
+	/*get Matlab matrix information: */
+	numel=mxGetNumberOfElements(mxmatrix);
+	ndims=mxGetNumberOfDimensions(mxmatrix);
+	ipt  =mxGetDimensions(mxmatrix);
+	size =(int *) xcalloc(ndims,sizeof(int));
+	for (i=0; i<ndims; i++) size[i]=(int)ipt[i];
+
+	/*Ok, first check if we are dealing with a sparse or full matrix: */
+	if (mxIsSparse(mxmatrix)){
+
+		/*Dealing with sparse matrix: recover size first: */
+		rows=mxGetM(mxmatrix);
+		cols=mxGetN(mxmatrix);
+		nnz=mxGetNzmax(mxmatrix);
+		nz=(int)((double)nnz/(double)rows);
+
+		matrix=(double*)xcalloc(rows*cols,sizeof(double));
+
+		/*Now, get ir,jc and pr: */
+		ir=mxGetIr(mxmatrix);
+		jc=mxGetJc(mxmatrix);
+		pr=mxGetPr(mxmatrix);
+
+		/*Now, start inserting data into double* matrix: */
+		count=0;
+		for(i=0;i<cols;i++){
+			for(j=0;j<(jc[i+1]-jc[i]);j++){
+				*(matrix+rows*ir[count]+i)=pr[count];
+				count++;
+			}
+		}
+
+	}
+	else{
+
+		/*Dealing with dense matrix: recover pointer and size: */
+		mxmatrix_ptr=(double*)mxGetPr(mxmatrix);
+		
+		/*Create serial matrix: */
+		matrix=(double*)xcalloc(numel,sizeof(double));
+
+		dims=(int *) xcalloc(ndims,sizeof(int));
+		for(i=0;i<numel;i++){
+			ColumnWiseDimsFromIndex(dims,i,size,ndims);
+			j=IndexFromRowWiseDims(dims,size,ndims);
+			*(matrix+j)=*(mxmatrix_ptr+i);
+		}
+		xfree((void**)&dims);
+		
+	}
+
+	/*Assign output pointer: */
+	*pmatrix=matrix;
+	*pmatrix_numel=numel;
+	*pmatrix_ndims=ndims;
+	*pmatrix_size=size;
+
+	return 1;
+}
+/*}}}*/
+/*FUNCTION MatlabNArrayToNArray(bool** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){{{1*/
+int MatlabNArrayToNArray(bool** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){
+
+	int  i,j,rows,cols;
+	int  numel,ndims;
+	int *size,*dims;
+	bool* mxmatrix_ptr=NULL;
+	const mwSize* ipt=NULL;
+
+	/*output: */
+	bool* matrix=NULL;
+
+	/*matlab indices: */
+	mwIndex*    ir=NULL;
+	mwIndex*    jc=NULL;
+	bool*   pm=NULL;
+	int     count;
+	int     nnz;
+	int     nz;
+
+	/*get Matlab matrix information: */
+	numel=mxGetNumberOfElements(mxmatrix);
+	ndims=mxGetNumberOfDimensions(mxmatrix);
+	ipt  =mxGetDimensions(mxmatrix);
+	size =(int *) xcalloc(ndims,sizeof(int));
+	for (i=0; i<ndims; i++) size[i]=(int)ipt[i];
+
+	/*Ok, first check if we are dealing with a sparse or full matrix: */
+	if (mxIsSparse(mxmatrix)){
+
+		/*Dealing with sparse matrix: recover size first: */
+		rows=mxGetM(mxmatrix);
+		cols=mxGetN(mxmatrix);
+		nnz=mxGetNzmax(mxmatrix);
+		nz=(int)((double)nnz/(double)rows);
+
+		matrix=(bool*)xcalloc(rows*cols,sizeof(bool));
+
+		/*Now, get ir,jc and pm: */
+		ir=mxGetIr(mxmatrix);
+		jc=mxGetJc(mxmatrix);
+		pm=(bool*)mxGetData(mxmatrix);
+
+		/*Now, start inserting data into bool* matrix: */
+		count=0;
+		for(i=0;i<cols;i++){
+			for(j=0;j<(jc[i+1]-jc[i]);j++){
+				*(matrix+rows*ir[count]+i)=pm[count];
+				count++;
+			}
+		}
+
+	}
+	else{
+
+		/*Dealing with dense matrix: recover pointer and size: */
+		mxmatrix_ptr=(bool*)mxGetData(mxmatrix);
+		
+		/*Create serial matrix: */
+		matrix=(bool*)xcalloc(numel,sizeof(bool));
+
+		dims=(int *) xcalloc(ndims,sizeof(int));
+		for(i=0;i<numel;i++){
+			ColumnWiseDimsFromIndex(dims,i,size,ndims);
+			j=IndexFromRowWiseDims(dims,size,ndims);
+			*(matrix+j)=(bool)*(mxmatrix_ptr+i);
+		}
+		xfree((void**)&dims);
+		
+	}
+
+	/*Assign output pointer: */
+	*pmatrix=matrix;
+	*pmatrix_numel=numel;
+	*pmatrix_ndims=ndims;
+	*pmatrix_size=size;
+
+	return 1;
+}
+/*}}}*/
+/*FUNCTION MatlabNArrayToNArray(char** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){{{1*/
+int MatlabNArrayToNArray(char** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){
+
+	int  i,j,rows,cols;
+	int  numel,ndims;
+	int *size,*dims;
+	mxChar* mxmatrix_ptr=NULL;
+	const mwSize* ipt=NULL;
+
+	/*output: */
+	char* matrix=NULL;
+
+	/*matlab indices: */
+	mwIndex*    ir=NULL;
+	mwIndex*    jc=NULL;
+	char*   pm=NULL;
+	int     count;
+	int     nnz;
+	int     nz;
+
+	/*get Matlab matrix information: */
+	numel=mxGetNumberOfElements(mxmatrix);
+	ndims=mxGetNumberOfDimensions(mxmatrix);
+	ipt  =mxGetDimensions(mxmatrix);
+	size =(int *) xcalloc(ndims,sizeof(int));
+	for (i=0; i<ndims; i++) size[i]=(int)ipt[i];
+
+	/*Ok, first check if we are dealing with a sparse or full matrix: */
+	if (mxIsSparse(mxmatrix)){
+
+		/*Dealing with sparse matrix: recover size first: */
+		rows=mxGetM(mxmatrix);
+		cols=mxGetN(mxmatrix);
+		nnz=mxGetNzmax(mxmatrix);
+		nz=(int)((double)nnz/(double)rows);
+
+		matrix=(char*)xcalloc(rows*cols,sizeof(double));
+
+		/*Now, get ir,jc and pm: */
+		ir=mxGetIr(mxmatrix);
+		jc=mxGetJc(mxmatrix);
+		pm=(char*)mxGetData(mxmatrix);
+
+		/*Now, start inserting data into char* matrix: */
+		count=0;
+		for(i=0;i<cols;i++){
+			for(j=0;j<(jc[i+1]-jc[i]);j++){
+				*(matrix+rows*ir[count]+i)=(char)pm[count];
+				count++;
+			}
+		}
+
+	}
+	else{
+
+		/*Dealing with dense matrix: recover pointer and size: */
+		mxmatrix_ptr=mxGetChars(mxmatrix);
+		
+		/*Create serial matrix: */
+		matrix=(char*)xcalloc(numel+1,sizeof(mxChar));
+
+		/*looping code adapted from Matlab example explore.c: */
+		int elements_per_page = size[0] * size[1];
+		/* total_number_of_pages = size[2] x size[3] x ... x size[N-1] */
+		int total_number_of_pages = 1;
+		for (i=2; i<ndims; i++) {
+			total_number_of_pages *= size[i];
+		}
+
+		i=0;
+		for (int page=0; page < total_number_of_pages; page++) {
+			int row;
+			/* On each page, walk through each row. */
+			for (row=0; row<size[0]; row++)  {
+				int column;
+				j = (page * elements_per_page) + row;
+
+				/* Walk along each column in the current row. */
+				for (column=0; column<size[1]; column++) {
+					*(matrix+i++)=(char)*(mxmatrix_ptr+j);
+					j += size[0];
+				}
+			}
+		}
+
+	}
+
+	/*Assign output pointer: */
+	*pmatrix=matrix;
+	*pmatrix_numel=numel;
+	*pmatrix_ndims=ndims;
+	*pmatrix_size=size;
+
+	return 1;
+}
+/*}}}*/
+#endif
Index: /issm/trunk-jpl/src/c/matlab/io/MatlabVectorToDoubleVector.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/MatlabVectorToDoubleVector.cpp	(revision 12011)
+++ /issm/trunk-jpl/src/c/matlab/io/MatlabVectorToDoubleVector.cpp	(revision 12011)
@@ -0,0 +1,96 @@
+/* \file MatlabVectorToDoubleVector.cpp
+ * \brief: convert a sparse or dense matlab vector to a serial vector:
+ */
+
+
+#ifdef HAVE_CONFIG_H
+	#include <config.h>
+#else
+#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
+#endif
+
+
+#include <string.h>
+
+/*Matlab includes: */
+#include "mex.h"
+
+#include "../../shared/shared.h"
+
+int MatlabVectorToDoubleVector(double** pvector,int* pvector_rows,const mxArray* mxvector){
+
+	int rows,cols;
+	double* mxvector_ptr=NULL;
+	int ierr;
+	int i,j;
+
+	/*output: */
+	double* vector=NULL;
+
+	/*matlab indices: */
+	mwIndex*    ir=NULL;
+	mwIndex*    jc=NULL;
+	double* pr=NULL;
+	int     count;
+	int     nnz;
+	int     nz;
+
+	/*Ok, first check if we are dealing with a sparse or full vector: */
+	if (mxIsSparse(mxvector)){
+
+		/*Dealing with sparse vector: recover size first: */
+		mxvector_ptr=(double*)mxGetPr(mxvector);
+		rows=mxGetM(mxvector);
+		cols=mxGetN(mxvector);
+		nnz=mxGetNzmax(mxvector);
+		
+		/*Check that input is actualy a vector*/
+		if (cols!=1) _error_("input vector of size %ix%i should have only one column",rows,cols);
+
+		nz=(int)((double)nnz/(double)rows);
+
+		if(rows){
+			vector=(double*)xcalloc(rows,sizeof(double));
+
+			/*Now, get ir,jc and pr: */
+			pr=mxGetPr(mxvector);
+			ir=mxGetIr(mxvector);
+			jc=mxGetJc(mxvector);
+
+			/*Now, start inserting data into sparse vector: */
+			count=0;
+			for(i=0;i<cols;i++){
+				for(j=0;j<(jc[i+1]-jc[i]);j++){
+					vector[ir[count]]=pr[count];
+					count++;
+				}
+			}
+		}
+
+	}
+	else{
+
+		/*Dealing with dense vector: recover pointer and size: */
+		mxvector_ptr=(double*)mxGetPr(mxvector);
+		rows=mxGetM(mxvector);
+		cols=mxGetN(mxvector);
+
+		/*Check that input is actualy a vector*/
+		if (cols!=1) _error_("input vector of size %ix%i should have only one column",rows,cols);
+
+		/*allocate and memcpy*/
+		if(rows){
+			vector=(double*)xmalloc(rows*sizeof(double));
+			memcpy(vector,mxvector_ptr,rows*sizeof(double));
+		}
+		else{
+			vector=NULL;
+		}
+	}
+
+	/*Assign output pointer: */
+	*pvector=vector;
+	*pvector_rows=rows;
+
+	return 1;
+}
Index: /issm/trunk-jpl/src/c/matlab/io/MatlabVectorToPetscVector.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/MatlabVectorToPetscVector.cpp	(revision 12011)
+++ /issm/trunk-jpl/src/c/matlab/io/MatlabVectorToPetscVector.cpp	(revision 12011)
@@ -0,0 +1,102 @@
+/* \file MatlabVectorToPetscVector.cpp
+ * \brief: convert a sparse or dense matlab vector to a serial Petsc vector:
+ */
+
+
+#ifdef HAVE_CONFIG_H
+	#include <config.h>
+#else
+#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
+#endif
+
+
+#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
+
+/*Petsc includes: */
+#include "petscmat.h"
+#include "petscvec.h"
+#include "petscksp.h"
+
+/*Matlab includes: */
+#include "mex.h"
+
+#include "../../../shared/shared.h"
+
+int MatlabVectorToPetscVector(Vec* pvector,int* pvector_rows,const mxArray* mxvector){
+
+	int rows, cols;
+	double* mxvector_ptr=NULL;
+	int ierr;
+	int i,j;
+
+	/*output: */
+	Vec vector=NULL;
+
+	/*matlab indices: */
+	mwIndex*    ir=NULL;
+	mwIndex*    jc=NULL;
+	double* pr=NULL;
+	int     count;
+	int     nnz;
+	int     nz;
+
+	/*petsc indices: */
+	int* idxm=NULL;
+	
+	/*Ok, first check if we are dealing with a sparse or full vector: */
+	if (mxIsSparse(mxvector)){
+
+		/*Dealing with sparse vector: recover size first: */
+		mxvector_ptr=(double*)mxGetPr(mxvector);
+		rows=mxGetM(mxvector);
+		cols=mxGetN(mxvector);
+		nnz=mxGetNzmax(mxvector);
+		nz=(int)((double)nnz/(double)rows);
+
+		ierr=VecCreateSeq(PETSC_COMM_SELF,rows,&vector);CHKERRQ(ierr);
+
+		/*Now, get ir,jc and pr: */
+		pr=mxGetPr(mxvector);
+		ir=mxGetIr(mxvector);
+		jc=mxGetJc(mxvector);
+
+		/*Now, start inserting data into sparse vector: */
+		count=0;
+		for(i=0;i<cols;i++){
+			for(j=0;j<(jc[i+1]-jc[i]);j++){
+				VecSetValue(vector,ir[count],pr[count],INSERT_VALUES);
+				count++;
+			}
+		}
+
+	}
+	else{
+
+		/*Dealing with dense vector: recover pointer and size: */
+		mxvector_ptr=(double*)mxGetPr(mxvector);
+		rows=mxGetM(mxvector);
+		cols=mxGetN(mxvector);
+
+		/*Create serial vector: */
+		ierr=VecCreateSeq(PETSC_COMM_SELF,rows,&vector);CHKERRQ(ierr);
+
+		/*Insert mxvector_ptr values into petsc vector: */
+		idxm=(int*)xmalloc(rows*sizeof(int));
+
+		for(i=0;i<rows;i++)idxm[i]=i;
+
+		ierr=VecSetValues(vector,rows,idxm,mxvector_ptr,INSERT_VALUES);CHKERRQ(ierr);
+
+	}
+
+	/*Assemble vector: */
+	VecAssemblyBegin(vector);
+	VecAssemblyEnd(vector);
+
+	/*Assign output pointer: */
+	*pvector=vector;
+	*pvector_rows=rows;
+
+	return 1;
+}
+#endif
Index: /issm/trunk-jpl/src/c/matlab/io/MatlabVectorToSeqVec.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/MatlabVectorToSeqVec.cpp	(revision 12011)
+++ /issm/trunk-jpl/src/c/matlab/io/MatlabVectorToSeqVec.cpp	(revision 12011)
@@ -0,0 +1,9 @@
+SeqVec* MatlabVectorToSeqVec(const mxArray* dataref){
+
+	SeqVec* output=NULL;
+
+	output=new SeqVec();
+	MatlabVectorToDoubleVector(&output->vector,&output->M,dataref);
+	return output;
+
+}
Index: /issm/trunk-jpl/src/c/matlab/io/MatlabVectorToVector.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/MatlabVectorToVector.cpp	(revision 12011)
+++ /issm/trunk-jpl/src/c/matlab/io/MatlabVectorToVector.cpp	(revision 12011)
@@ -0,0 +1,17 @@
+
+Vector* MatlabVectorToVector(const mxArray* mxvector){
+
+	int dummy;
+	Vector* vector=NULL;
+
+	/*allocate vector object: */
+	vector=new Vector();
+
+	#ifdef _HAVE_PETSC_
+	MatlabVectorToPetscVector(&vector->vector,&dummy,mxvector);
+	#else
+	vector->vector=MatlabVectorToSeqVec(mxvector);
+	#endif
+	
+	return vector;
+}
Index: /issm/trunk-jpl/src/c/matlab/io/OptionParse.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/OptionParse.cpp	(revision 12011)
+++ /issm/trunk-jpl/src/c/matlab/io/OptionParse.cpp	(revision 12011)
@@ -0,0 +1,194 @@
+/*\file OptionParse.c
+ *\brief: functions to parse the mex options.
+ */
+#ifdef HAVE_CONFIG_H
+    #include <config.h>
+#else
+#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
+#endif
+
+#include "../../shared/shared.h"
+#include "../../io/io.h"
+#include "../../include/include.h"
+#include "./matlabio.h"
+
+#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
+#include <mex.h>
+
+/*FUNCTION OptionDoubleParse {{{1*/
+OptionDouble* OptionDoubleParse( char* name, const mxArray* prhs[]){
+
+	OptionDouble *odouble = NULL;
+
+	/*check and parse the name  */
+	odouble=new OptionDouble;
+	odouble->name =(char *) xmalloc((strlen(name)+1)*sizeof(char));
+	memcpy(odouble->name,name,(strlen(name)+1)*sizeof(char));
+
+	/*check and parse the value  */
+	if (!mxIsClass(prhs[0],"double")){
+		_error_("Value of option \"%s\" must be class \"%s\", not class \"%s\".",odouble->name,"double",odouble->name,mxGetClassName(prhs[0]));
+	}
+
+	FetchData(&odouble->values,&odouble->numel,&odouble->ndims,&odouble->size,prhs[0]);
+
+	return(odouble);
+}/*}}}*/
+/*FUNCTION OptionLogicalParse {{{1*/
+OptionLogical* OptionLogicalParse( char* name, const mxArray* prhs[]){
+
+	OptionLogical *ological = NULL;
+
+	/*check and parse the name  */
+	ological=new OptionLogical;
+	ological->name =(char *) xmalloc((strlen(name)+1)*sizeof(char));
+	memcpy(ological->name,name,(strlen(name)+1)*sizeof(char));
+
+	/*check and parse the value  */
+	if (!mxIsClass(prhs[0],"logical")){
+		_error_("Value of option \"%s\" must be class \"%s\", not class \"%s\".",ological->name,"logical",ological->name,mxGetClassName(prhs[0]));
+	}
+
+	FetchData(&ological->values,&ological->numel,&ological->ndims,&ological->size,prhs[0]);
+
+	return(ological);
+}/*}}}*/
+/*FUNCTION OptionCharParse {{{1*/
+OptionChar* OptionCharParse( char* name, const mxArray* prhs[]){
+
+	OptionChar  *ochar = NULL;
+
+	/*check and parse the name  */
+	ochar=new OptionChar;
+	ochar->name =(char *) xmalloc((strlen(name)+1)*sizeof(char));
+	memcpy(ochar->name,name,(strlen(name)+1)*sizeof(char));
+
+	/*check and parse the value  */
+	if (!mxIsClass(prhs[0],"char")){
+		_error_("Value of option \"%s\" must be class \"%s\", not class \"%s\".",ochar->name,"char",ochar->name,mxGetClassName(prhs[0]));
+	}
+
+	FetchData(&ochar->values,&ochar->numel,&ochar->ndims,&ochar->size,prhs[0]);
+
+	return(ochar);
+}/*}}}*/
+/*FUNCTION OptionStructParse {{{1*/
+OptionStruct* OptionStructParse( char* name, const mxArray* prhs[]){
+
+	int            i;
+	char           namei[161];
+	OptionStruct  *ostruct    = NULL;
+	Option        *option     = NULL;
+	const mwSize  *ipt        = NULL;
+	const mxArray *structi;
+	mwIndex        sindex;
+
+	/*check and parse the name  */
+	ostruct=new OptionStruct;
+	ostruct->name =(char*)xmalloc((strlen(name)+1)*sizeof(char));
+	memcpy(ostruct->name,name,(strlen(name)+1)*sizeof(char));
+
+	/*check and parse the value  */
+	if (!mxIsClass(prhs[0],"struct")){
+		_error_("Value of option \"%s\" must be class \"%s\", not class \"%s\".",ostruct->name,"struct",ostruct->name,mxGetClassName(prhs[0]));
+	}
+
+	ostruct->numel=mxGetNumberOfElements(prhs[0]);
+	ostruct->ndims=mxGetNumberOfDimensions(prhs[0]);
+	ipt           =mxGetDimensions(prhs[0]);
+	ostruct->size =(int *) xmalloc(ostruct->ndims*sizeof(int));
+	for (i=0; i<ostruct->ndims; i++) ostruct->size[i]=(int)ipt[i];
+	if (ostruct->numel) ostruct->values=(Options**) xmalloc(ostruct->numel*sizeof(Options *));
+
+	/*loop through and process each element of the struct array  */
+	for (sindex=0; sindex<ostruct->numel; sindex++) {
+		ostruct->values[sindex]=new Options;
+
+		/*loop through and process each field for the element  */
+		for (i=0; i<mxGetNumberOfFields(prhs[0]); i++) {
+			sprintf(namei,"%s.%s",name,mxGetFieldNameByNumber(prhs[0],i));
+			structi=mxGetFieldByNumber(prhs[0],sindex,i);
+
+			option=(Option*)OptionParse(namei,&structi);
+			ostruct->values[sindex]->AddObject((Object*)option);
+			option=NULL;
+		}
+	}
+
+	return(ostruct);
+}/*}}}*/
+/*FUNCTION OptionCellParse {{{1*/
+OptionCell* OptionCellParse( char* name, const mxArray* prhs[]){
+
+	int            i;
+	int           *dims;
+	char           namei[161];
+	char           cstr[81];
+	OptionCell    *ocell      = NULL;
+	Option        *option     = NULL;
+	const mwSize  *ipt        = NULL;
+	const mxArray *celli;
+	mwIndex        cindex;
+
+	/*check and parse the name  */
+	ocell=new OptionCell;
+	ocell->name =(char *) xmalloc((strlen(name)+1)*sizeof(char));
+	memcpy(ocell->name,name,(strlen(name)+1)*sizeof(char));
+
+	/*check and parse the value  */
+	if (!mxIsClass(prhs[0],"cell")){
+		_error_("Value of option \"%s\" must be class \"%s\", not class \"%s\".",ocell->name,"cell",ocell->name,mxGetClassName(prhs[0]));
+	}
+
+	ocell->numel=mxGetNumberOfElements(prhs[0]);
+	ocell->ndims=mxGetNumberOfDimensions(prhs[0]);
+	ipt         =mxGetDimensions(prhs[0]);
+	ocell->size =(int *) xmalloc(ocell->ndims*sizeof(int));
+	for (i=0; i<ocell->ndims; i++) ocell->size[i]=(int)ipt[i];
+	ocell->values=new Options;
+
+	/*loop through and process each element of the cell array  */
+	dims=(int *) xmalloc(ocell->ndims*sizeof(int));
+	for (cindex=0; cindex<ocell->numel; cindex++) {
+		ColumnWiseDimsFromIndex(dims,(int)cindex,ocell->size,ocell->ndims);
+		StringFromDims(cstr,dims,ocell->ndims);
+		#ifdef _INTEL_WIN_
+			_snprintf(namei,161,"%s%s",name,cstr);
+		#else
+			snprintf(namei,161,"%s%s",name,cstr);
+		#endif
+		celli=mxGetCell(prhs[0],cindex);
+
+		option=(Option*)OptionParse(namei,&celli);
+		ocell->values->AddObject((Object*)option);
+		option=NULL;
+	}
+	xfree((void**)&dims);
+
+	return(ocell);
+}/*}}}*/
+/*FUNCTION OptionParse{{{1*/
+Option* OptionParse(char* name, const mxArray* prhs[]){
+
+	Option *option = NULL;
+	mxArray       *lhs[1];
+
+	/*parse the value according to the matlab data type  */
+	if     (mxIsClass(prhs[0],"double"))  option=(Option*)OptionDoubleParse(name,prhs);
+	else if(mxIsClass(prhs[0],"logical")) option=(Option*)OptionLogicalParse(name,prhs);
+	else if(mxIsClass(prhs[0],"char"))    option=(Option*)OptionCharParse(name,prhs);
+	else if(mxIsClass(prhs[0],"struct"))  option=(Option*)OptionStructParse(name,prhs);
+	else if(mxIsClass(prhs[0],"cell"))    option=(Option*)OptionCellParse(name,prhs);
+	else {
+		_printf_(true,"  Converting value of option \"%s\" from unrecognized class \"%s\" to class \"%s\".\n",name,mxGetClassName(prhs[0]),"struct");
+		if (!mexCallMATLAB(1,lhs,1,(mxArray**)prhs,"struct")) {
+			option=(Option*)OptionStructParse(name,(const mxArray**)lhs);
+			mxDestroyArray(lhs[0]);
+		}
+		else _error_("Second argument value of option \"%s\" is of unrecognized class \"%s\".",name,mxGetClassName(prhs[0]));
+	}
+
+	return(option);
+}/*}}}*/
+
+#endif
Index: /issm/trunk-jpl/src/c/matlab/io/PetscMatrixToDoubleMatrix.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/PetscMatrixToDoubleMatrix.cpp	(revision 12011)
+++ /issm/trunk-jpl/src/c/matlab/io/PetscMatrixToDoubleMatrix.cpp	(revision 12011)
@@ -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/matlab/io/PetscVectorToDoubleVector.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/PetscVectorToDoubleVector.cpp	(revision 12011)
+++ /issm/trunk-jpl/src/c/matlab/io/PetscVectorToDoubleVector.cpp	(revision 12011)
@@ -0,0 +1,52 @@
+/* \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"
+
+/*Petsc includes: */
+#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(vector){
+		VecGetSize(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(vector,rows,idxm,vector);
+		}
+	}
+	else{
+		rows=0;
+	}
+
+	/*Assign output pointers: */
+	*pvector=vector;
+	*prows=rows;
+}
Index: /issm/trunk-jpl/src/c/matlab/io/PrintfFunction.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/PrintfFunction.cpp	(revision 12011)
+++ /issm/trunk-jpl/src/c/matlab/io/PrintfFunction.cpp	(revision 12011)
@@ -0,0 +1,64 @@
+/*\file PrintfFunction.c
+ *\brief: this function is used by the _printf_ macro, to take into account the 
+ *fact we may be running on a cluster. 
+ */
+
+#include <stdarg.h>
+#include <stdio.h>
+#include "../../shared/shared.h"
+#include "../../include/include.h"
+
+#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
+#include "mex.h"
+#endif
+
+int PrintfFunction(char* format,...){
+	/*http://linux.die.net/man/3/vsnprintf*/
+
+	/*string to be printed: */
+	char *buffer = NULL;
+	int   n,size = 100;
+	int         string_size;
+	extern int  my_rank;
+	extern int  num_procs;
+                                                                                                                                                                                                     
+	//variable list of arguments
+	va_list args;
+
+	while(true){
+
+		/*allocate buffer for given string size*/
+		buffer=(char*)xmalloc(size*sizeof(char));
+
+		/* Try to print in the allocated space. */
+		va_start(args, format);
+#ifndef WIN32
+		n=vsnprintf(buffer,size,format,args);
+#else
+		n=vsnprintf(buffer,size,format,args);
+#endif
+		va_end(args);
+
+		/* If that worked, return the string. */
+		if(n>-1 && n<size) break;
+
+		/* Else try again with more space. */
+		if(n>-1)   /* glibc 2.1 */
+		 size=n+1; /* precisely what is needed */
+		else       /* glibc 2.0 */
+		 size*=2;  /* twice the old size */
+
+		xfree((void**)&buffer);
+	}
+
+	/*Ok, if we are running in parallel, get node 0 to print*/
+#if defined(_PARALLEL_)
+	if(my_rank==0)printf(buffer);
+#else
+	mexPrintf(buffer);
+#endif
+
+	/*Clean up and return*/
+	xfree((void**)&buffer);
+	return 1;
+}
Index: /issm/trunk-jpl/src/c/matlab/io/WriteMatlabData.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/WriteMatlabData.cpp	(revision 12011)
+++ /issm/trunk-jpl/src/c/matlab/io/WriteMatlabData.cpp	(revision 12011)
@@ -0,0 +1,249 @@
+/* \file WriteData.c:
+ * \brief: general interface for writing data
+ */
+
+#ifdef HAVE_CONFIG_H
+	#include <config.h>
+#else
+#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
+#endif
+
+#include "../../include/include.h"
+#include "../../shared/shared.h"
+
+#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
+#include <mex.h>
+
+/*FUNCTION WriteData(mxArray** pdataref,DataSet* dataset){{{1*/
+void WriteData(mxArray** pdataref,DataSet* dataset){
+
+	mxArray* dataref=NULL;
+	char* marshalled_dataset=NULL;
+	int   marshalled_dataset_size;
+
+	/*Write a dataset: */
+	if(dataset){
+			/* marshall the dataset: */
+			marshalled_dataset=dataset->Marshall();
+			marshalled_dataset_size=dataset->MarshallSize();
+			
+			dataref = mxCreateDoubleMatrix(0,0,mxREAL);
+			mxSetM(dataref,(mwSize)(marshalled_dataset_size/sizeof(double)));
+			mxSetN(dataref,(mwSize)1);
+			mxSetPr(dataref,(double*)marshalled_dataset);	
+	}
+	else{
+		/* return empty matrix: */
+		dataref=mxCreateDoubleMatrix(0,0,mxREAL);
+	}
+	*pdataref=dataref;
+	
+}
+/*}}}*/
+/*FUNCTION WriteData(mxArray** pdataref,Matrix* matrix){{{1*/
+void WriteData(mxArray** pdataref,Matrix* matrix){
+		
+	mxArray* dataref=NULL;
+	double*  matrix_ptr=NULL;
+	int      rows,cols;
+	double*  tmatrix_ptr=NULL;
+	
+	if(matrix){
+		
+		#ifdef _HAVE_PETSC_
+		PetscMatrixToDoubleMatrix(&tmatrix_ptr,&rows,&cols);
+		#else
+		matrix_ptr=matrix->matrix->ToSerial();
+		matrix->matrix->GetSize(&rows,cols);
+		#endif
+
+		/*Now transpose the matrix: */
+		tmatrix_ptr=(double*)mxMalloc(rows*cols*sizeof(double));
+		for(i=0;i<cols;i++){
+			for(j=0;j<rows;j++){
+				tmatrix_ptr[i*rows+j]=matrix_ptr[j*cols+i];
+			}
+		}
+		
+		/*create matlab matrix: */
+		dataref=mxCreateDoubleMatrix(0,0,mxREAL);
+		mxSetM(dataref,rows); 
+		mxSetN(dataref,cols);
+		mxSetPr(dataref,tmatrix_ptr);
+
+		/*Free ressources:*/
+		xfree((void**)&matrix_ptr);
+
+	}
+	else{
+		dataref = mxCreateDoubleMatrix(0,0,mxREAL);
+	}
+
+	*pdataref=dataref;
+}
+/*}}}*/
+/*FUNCTION WriteData(mxArray** pdataref,Vector* vector){{{1*/
+void WriteData(mxArray** pdataref,Vector* vector){
+	
+	mxArray* dataref=NULL;
+	double*  vector_ptr=NULL;
+	int      rows;
+	
+	if(vector){
+		/*call toolkit routine: */
+		#ifdef _HAVE_PETSC_
+		PetscVectorToDoubleVector(&vector_ptr,&rows,vector->vector);
+		#else
+		vector_ptr=vector->vector->ToMPISerial();
+		vector->vector->GetSize(&rows);
+		#endif
+		
+		/*now create the matlab vector: */
+		dataref = mxCreateDoubleMatrix(0,0,mxREAL);                         
+		mxSetM(dataref,rows);
+		mxSetN(dataref,1);                                                                                          
+		mxSetPr(dataref,vector_ptr);           
+	}
+	else{
+		dataref = mxCreateDoubleMatrix(0,0,mxREAL);
+	}
+	*pdataref=dataref;
+
+}
+/*}}}*/
+/*FUNCTION WriteData(mxArray** pdataref,double* matrix, int M,int N){{{1*/
+void WriteData(mxArray** pdataref,double* matrix, int M,int N){
+	
+	mxArray* dataref=NULL;
+	mxArray* tdataref=NULL;
+		
+	if(matrix){
+		
+		/*data is a double* pointer. Copy into a matrix: */
+		tdataref = mxCreateDoubleMatrix(0,0,mxREAL);
+		mxSetM(tdataref,(mwSize)N);
+		mxSetN(tdataref,(mwSize)M);
+		mxSetPr(tdataref,(double*)matrix);
+
+		//transpose
+		mexCallMATLAB(1,&dataref,1,&tdataref, "transpose");
+	}
+	else{
+		dataref = mxCreateDoubleMatrix(0,0,mxREAL);
+	}
+	*pdataref=dataref;
+}
+/*}}}*/
+/*FUNCTION WriteData(mxArray** pdataref,int* matrix, int M,int N){{{1*/
+void WriteData(mxArray** pdataref,int* matrix, int M,int N){
+
+	mxArray* dataref=NULL;
+	mxArray* tdataref=NULL;
+
+	if(matrix){
+
+		/*convert to double matrix*/
+		double* doublematrix=(double*)mxMalloc(M*N*sizeof(double));
+		for(int i=0;i<M*N;i++) doublematrix[i]=(double)matrix[i];
+
+		/*data is a double* pointer. Copy into a matrix: */
+		tdataref = mxCreateDoubleMatrix(0,0,mxREAL);
+		mxSetM(tdataref,(mwSize)N);
+		mxSetN(tdataref,(mwSize)M);
+		mxSetPr(tdataref,(double*)doublematrix);
+
+		//transpose
+		mexCallMATLAB(1,&dataref,1,&tdataref, "transpose");
+	}
+	else{
+		dataref = mxCreateDoubleMatrix(0,0,mxREAL);
+	}
+	*pdataref=dataref;
+}
+/*}}}*/
+/*FUNCTION WriteData(mxArray** pdataref,double* vector, int M){{{1*/
+void WriteData(mxArray** pdataref,double* vector, int M){
+	
+	mxArray* dataref=NULL;
+
+	if(vector){
+
+		/*data is a double* pointer. Copy into a vector: */
+		dataref = mxCreateDoubleMatrix(0,0,mxREAL);
+		mxSetM(dataref,(mwSize)M);
+		mxSetN(dataref,(mwSize)1);
+		mxSetPr(dataref,vector);
+	}
+	else{
+		dataref = mxCreateDoubleMatrix(0,0,mxREAL);
+	}
+
+	*pdataref=dataref;
+}
+/*}}}*/
+/*FUNCTION WriteData(mxArray** pdataref,double scalar){{{1*/
+void WriteData(mxArray** pdataref,double scalar){
+
+	*pdataref=mxCreateDoubleScalar(scalar);
+}
+/*}}}*/
+/*FUNCTION WriteData(mxArray** pdataref,int integer){{{1*/
+void WriteData(mxArray** pdataref,int integer){
+
+		*pdataref=mxCreateDoubleScalar((double)integer);
+
+}
+/*}}}*/
+/*FUNCTION WriteData(mxArray** pdataref,int boolean){{{1*/
+void WriteData(mxArray** pdataref,bool boolean){
+
+	*pdataref=mxCreateDoubleScalar((double)boolean);
+
+}
+/*}}}*/
+/*FUNCTION WriteData(mxArray** pdataref,char* string){{{1*/
+void WriteData(mxArray** pdataref,char* string){
+
+		*pdataref=mxCreateString(string);
+}
+/*}}}*/
+/*FUNCTION WriteData(mxArray** pdataref,Parameters* parameters){{{1*/
+void WriteData(mxArray** pdataref,Parameters* parameters){
+
+	int i;
+
+	/*output: */
+	mxArray  *dataref     = NULL;
+	mwSize    nfields;
+	char    **fnames      = NULL;
+	mwSize    onebyone[2] = {1,1};
+	mwSize    ndim        = 2;
+
+	/*intermediary: */
+	Param*      param=NULL;
+
+	/*Recover data from the parameters dataset: */
+	nfields=(mwSize)parameters->Size();
+	fnames=(char**)mxMalloc(nfields*sizeof(char*));
+	
+	/*Build structure in matlab workspace with all the parameter fields: */
+	for(i=0;i<nfields;i++){
+		param=(Param*)parameters->GetObjectByOffset(i);
+		param->GetParameterName(&fnames[i]);
+	}
+	/*Initialize structure: */
+	dataref=mxCreateStructArray( ndim,onebyone,nfields,(const char**)fnames);
+
+	/*Fill each field: */
+	for(i=0;i<nfields;i++){
+
+		param=(Param*)parameters->GetObjectByOffset(i);
+		param->SetMatlabField(dataref);
+	}
+		
+	/*Assign output pointers:*/
+	*pdataref=dataref;
+
+}
+/*}}}*/
+#endif
Index: /issm/trunk-jpl/src/c/matlab/io/matlabio.h
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/matlabio.h	(revision 12011)
+++ /issm/trunk-jpl/src/c/matlab/io/matlabio.h	(revision 12011)
@@ -0,0 +1,85 @@
+/*\file matlabio.h
+ *\brief: I/O for ISSM in matlab mode
+ */
+
+#ifndef _MATLAB_IO_H_
+#define _MATLAB_IO_H_
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#else
+#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
+#endif 
+
+#include "../../objects/objects.h"
+#include "../../Container/Container.h"
+#include "../../include/include.h"
+
+class DataSet;
+class Parameters;
+
+#include <mex.h>
+
+void WriteData(mxArray** pdataref,DataSet* dataset);
+void WriteData(mxArray** pdataref,Matrix* matrix);
+void WriteData(mxArray** pdataref,double* matrix, int M,int N);
+void WriteData(mxArray** pdataref,int*    matrix, int M,int N);
+void WriteData(mxArray** pdataref,Vector* vector);
+void WriteData(mxArray** pdataref,double* vector, int M);
+void WriteData(mxArray** pdataref,int integer);
+void WriteData(mxArray** pdataref,bool boolean);
+void WriteData(mxArray** pdataref,double scalar);
+void WriteData(mxArray** pdataref,char* string);
+void WriteData(DataHandle* pdataref,Parameters* parameters);
+
+void FetchData(DataSet** pdataset,const mxArray* dataref);
+void FetchData(double** pmatrix,int* pM,int *pN,const mxArray* dataref);
+void FetchData(double** pmatrix,int* pnumel,int* pndims,int** psize,const mxArray* dataref);
+void FetchData(int** pmatrix,int* pM,int *pN,const mxArray* dataref);
+void FetchData(bool** pmatrix,int* pM,int *pN,const mxArray* dataref);
+void FetchData(bool** pmatrix,int* pnumel,int* pndims,int** psize,const mxArray* dataref);
+void FetchData(Matrix** pmatrix,const mxArray* dataref);
+void FetchData(int** pvector,int* pM,const mxArray* dataref);
+void FetchData(float** pvector,int* pM,const mxArray* dataref);
+void FetchData(double** pvector,int* pM,const mxArray* dataref);
+void FetchData(bool** pvector,int* pM,const mxArray* dataref);
+void FetchData(Vector** pvector,const mxArray* dataref);
+void FetchData(char** pstring,const mxArray* dataref);
+void FetchData(char** pmatrix,int* pnumel,int* pndims,int** psize,const mxArray* dataref);
+void FetchData(double* pscalar,const mxArray* dataref);
+void FetchData(int* pinteger,const mxArray* dataref);
+void FetchData(bool* pbool,const mxArray* dataref);
+void FetchData(Parameters** pparameters, DataHandle dataref);
+
+Option* OptionParse(char* name, const mxArray* prhs[]);
+OptionDouble*   OptionDoubleParse( char* name, const mxArray* prhs[]);
+OptionLogical*  OptionLogicalParse( char* name, const mxArray* prhs[]);
+OptionChar*     OptionCharParse( char* name, const mxArray* prhs[]);
+OptionStruct*   OptionStructParse( char* name, const mxArray* prhs[]);
+OptionCell*     OptionCellParse( char* name, const mxArray* prhs[]);
+
+mxArray* mxGetAssignedField(const mxArray* pmxa_array,int number, const char* field);
+int CheckNumMatlabArguments(int nlhs,int NLHS, int nrhs,int NRHS, const char* THISFUNCTION, void (*function)( void ));
+
+/*Matlab to Matrix routines: */
+Matrix* MatlabMatrixToMatrix(const mxArray* mxmatrix);
+Vector* MatlabVectorToVector(const mxArray* mxvector);
+
+/*Matlab to double* routines: */
+int MatlabVectorToDoubleVector(double** pvector,int* pvector_rows,const mxArray* mxvector);
+int MatlabMatrixToDoubleMatrix(double** pmatrix,int* pmatrix_rows,int* pmatrix_cols,const mxArray* mxmatrix);
+int MatlabNArrayToNArray(double** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix);
+int MatlabNArrayToNArray(bool** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix);
+int MatlabNArrayToNArray(char** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix);
+int MatlabMatrixToPetscMatrix(Mat* matrix,int* prows,int* pcols, const mxArray* mxmatrix);
+
+/*Matlab to Petsc routines: */
+int MatlabVectorToPetscVector(Vec* pvector,int* pvector_rows,const mxArray* mxvector);
+void PetscMatrixToDoubleMatrix(double** pmatrix, int* prows, int* pcols,Mat matrix);
+void PetscVectorToDoubleVector(double** pvector, int* prows, Vec vector);
+
+/*Matlab to SeqMat routines: */
+SeqMat* MatlabMatrixToSeqMat(const mxArray* dataref);
+SeqVec* MatlabVectorToSeqVec(const mxArray* dataref);
+
+#endif	/* _IO_H_ */
Index: /issm/trunk-jpl/src/c/matlab/io/mxGetAssignedField.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/mxGetAssignedField.cpp	(revision 12011)
+++ /issm/trunk-jpl/src/c/matlab/io/mxGetAssignedField.cpp	(revision 12011)
@@ -0,0 +1,47 @@
+/*!\file: mxGetAssignedField.c: 
+ * \brief: abstract interface on parallel side for i/o, so it ressembles the serial i/o.
+ *
+ * In serial mode, this routine takes care of returning the field coming 
+ * from the model. If largesize is 1, we are running out of core models in 
+ * matlab, and we need to call the subsref private method from the model object
+ * in order to correctly load the data from disk.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#else
+#error "Cannot compile without HAVE_CONFIG_H symbol! run configure first!"
+#endif
+
+#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
+#include "mex.h"
+
+mxArray* mxGetAssignedField(const mxArray* pmxa_array,int number,const char* field){
+
+	//output
+	mxArray* mxfield=NULL;
+	
+	//input
+	mxArray    *inputs[2];
+	mxArray    *pindex      = NULL;
+	const char *fnames[2];
+	mwSize      ndim        = 2;
+	mwSize      onebyone[2] = {1,1};
+
+	//We want to call the subsasgn method, and get the returned array.This ensures that if we are running 
+	//large sized problems, the data is truly loaded from disk by the model subsasgn class method.
+	inputs[0]=(mxArray*)pmxa_array; //this is the model
+
+	//create index structure used in the assignment (index.type='.' and index.subs='x' for field x for ex)
+	fnames[0] = "type";
+	fnames[1] = "subs";
+	pindex=mxCreateStructArray( ndim,onebyone,2,fnames);
+	mxSetField( pindex, 0, "type",mxCreateString("."));
+	mxSetField( pindex, 0, "subs",mxCreateString(field));
+	inputs[1]=pindex;
+
+	mexCallMATLAB( 1, &mxfield, 2, (mxArray**)inputs, "subsref");
+
+	return mxfield;
+}
+#endif
Index: /issm/trunk-jpl/src/c/matlab/objects/Bamg/BamgGeom.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/objects/Bamg/BamgGeom.cpp	(revision 12011)
+++ /issm/trunk-jpl/src/c/matlab/objects/Bamg/BamgGeom.cpp	(revision 12011)
@@ -0,0 +1,105 @@
+#include <stdio.h>
+#include "../objects.h"
+#include "../../io/io.h"
+#include "../../shared/shared.h"
+
+/*Constructors/Destructors*/
+/*FUNCTION BamgGeom::BamgGeom(void* module_struct){{{1*/
+BamgGeom::BamgGeom(void* module_struct){
+
+	mxArray* matlab_struct=NULL;
+
+	/*recover our pointer: */
+	matlab_struct=(mxArray*)module_struct;
+
+	FetchData(&this->Vertices,        &this->VerticesSize[0],        &this->VerticesSize[1],        mxGetAssignedField(matlab_struct,0,"Vertices"));
+	FetchData(&this->Edges,           &this->EdgesSize[0],           &this->EdgesSize[1],           mxGetAssignedField(matlab_struct,0,"Edges"));
+	this->TangentAtEdgesSize[0]=0,    this->TangentAtEdgesSize[1]=0;    this->TangentAtEdges=NULL;
+	FetchData(&this->Corners,         &this->CornersSize[0],         &this->CornersSize[1],         mxGetAssignedField(matlab_struct,0,"Corners"));
+	FetchData(&this->RequiredVertices,&this->RequiredVerticesSize[0],&this->RequiredVerticesSize[1],mxGetAssignedField(matlab_struct,0,"RequiredVertices"));
+	FetchData(&this->RequiredEdges,   &this->RequiredEdgesSize[0],   &this->RequiredEdgesSize[1],   mxGetAssignedField(matlab_struct,0,"RequiredEdges"));
+	FetchData(&this->CrackedEdges,    &this->CrackedEdgesSize[0],    &this->CrackedEdgesSize[1],    mxGetAssignedField(matlab_struct,0,"CrackedEdges"));
+	FetchData(&this->SubDomains,      &this->SubDomainsSize[0],      &this->SubDomainsSize[1],      mxGetAssignedField(matlab_struct,0,"SubDomains"));
+
+}
+/*}}}*/
+
+/*Methods*/
+/*FUNCTION BamgGeom::SetStructureFields{{{1*/
+void BamgGeom::SetStructureFields(void* module_struct){
+
+	mxArray** matlab_struct=NULL;
+
+	/*Intermediary*/
+	int         i;
+	mxArray*    output=NULL;
+	const int         numfields=7;
+	const char* fnames[numfields];
+	mwSize      ndim=2;
+	mwSize      dimensions[2]={1,1};
+	
+	/*recover pointer: */
+	matlab_struct=(mxArray**)module_struct;
+
+	/*Initialize field names*/
+	i=0;
+	fnames[i++] = "Vertices";
+	fnames[i++] = "Edges";
+	fnames[i++] = "TangentAtEdges";
+	fnames[i++] = "RequiredVertices";
+	fnames[i++] = "RequiredEdges";
+	fnames[i++] = "CrackedEdges";
+	fnames[i++] = "SubDomains";
+	_assert_(i==numfields);
+
+	/*Initialize Matlab structure*/
+	output=mxCreateStructArray(ndim,dimensions,numfields,fnames);
+
+	/*set each matlab each field*/
+	i=0;
+	i++; SetStructureField(output,"Vertices",        this->VerticesSize[0],        this->VerticesSize[1],        this->Vertices);
+	i++; SetStructureField(output,"Edges",           this->EdgesSize[0],           this->EdgesSize[1],           this->Edges);
+	i++; SetStructureField(output,"TangentAtEdges",  this->TangentAtEdgesSize[0],  this->TangentAtEdgesSize[1],  this->TangentAtEdges);
+	i++; SetStructureField(output,"RequiredVertices",this->RequiredVerticesSize[0],this->RequiredVerticesSize[1],this->RequiredVertices);
+	i++; SetStructureField(output,"RequiredEdges",   this->RequiredEdgesSize[0],   this->RequiredEdgesSize[1],   this->RequiredEdges);
+	i++; SetStructureField(output,"CrackedEdges",    this->CrackedEdgesSize[0],    this->CrackedEdgesSize[1],    this->CrackedEdges);
+	i++; SetStructureField(output,"SubDomains",      this->SubDomainsSize[0],      this->SubDomainsSize[1],      this->SubDomains);
+	_assert_(i==numfields);
+
+	/*Assign output*/
+	*matlab_struct=output;
+
+}
+/*}}}*/
+/*FUNCTION BamgGeom::SetStructureField{{{1*/
+void BamgGeom::SetStructureField(void* module_struct,const char* fieldname,int fieldrows,int fieldcols,double* fieldpointer){
+
+	/*Intermediary*/
+	mxArray*    matlab_struct=NULL;
+	int         i1,i2;
+	mxArray*    pfield=NULL;
+	mxArray*    pfield2=NULL;
+
+	/*recover pointer: */
+	matlab_struct=(mxArray*)module_struct;
+
+	/*Copy field*/
+	double*  fieldcopy=NULL;
+	if (fieldrows*fieldcols){
+		fieldcopy=(double*)xmalloc(fieldrows*fieldcols*sizeof(double));
+		for(i1=0;i1<fieldrows;i1++){
+			for(i2=0;i2<fieldcols;i2++){
+				fieldcopy[fieldcols*i1+i2]=fieldpointer[fieldcols*i1+i2];
+			}
+		}
+	}
+
+	/*Set matlab field*/
+	pfield=mxCreateDoubleMatrix(0,0,mxREAL);
+	mxSetM(pfield,fieldcols);
+	mxSetN(pfield,fieldrows);
+	mxSetPr(pfield,fieldcopy);
+	mexCallMATLAB(1,&pfield2,1,&pfield,"transpose");//transpose
+	mxSetField(matlab_struct,0,fieldname,pfield2);
+}
+/*}}}*/
Index: /issm/trunk-jpl/src/c/matlab/objects/Bamg/BamgMesh.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/objects/Bamg/BamgMesh.cpp	(revision 12011)
+++ /issm/trunk-jpl/src/c/matlab/objects/Bamg/BamgMesh.cpp	(revision 12011)
@@ -0,0 +1,137 @@
+#include <stdio.h>
+#include "../objects.h"
+#include "../../io/io.h"
+#include "../../shared/shared.h"
+
+/*Constructors/Destructors*/
+/*FUNCTION BamgMesh::BamgMesh(void* module_struct){{{1*/
+BamgMesh::BamgMesh(void* module_struct){
+
+	mxArray* matlab_struct=NULL;
+	int lines,cols;
+
+	/*recover our pointer: */
+	matlab_struct=(mxArray*)module_struct;
+
+
+	FetchData(&this->Vertices,            &this->VerticesSize[0],            &this->VerticesSize[1],            mxGetAssignedField(matlab_struct,0,"Vertices"));
+	FetchData(&this->Edges,               &this->EdgesSize[0],               &this->EdgesSize[1],               mxGetAssignedField(matlab_struct,0,"Edges"));
+	FetchData(&this->Triangles,           &this->TrianglesSize[0],           &this->TrianglesSize[1],           mxGetAssignedField(matlab_struct,0,"Triangles"));
+	this->QuadrilateralsSize[0]=0,        this->QuadrilateralsSize[1]=0;     this->Quadrilaterals=NULL;
+
+	this->SubDomainsSize[0]=0,            this->SubDomainsSize[1]=0;         this->SubDomains=NULL;
+	this->SubDomainsFromGeomSize[0]=0,    this->SubDomainsFromGeomSize[1]=0; this->SubDomainsFromGeom=NULL;
+	this->CrackedVerticesSize[0]=0,       this->CrackedVerticesSize[1]=0;    this->CrackedVertices=NULL;
+	FetchData(&this->CrackedEdges,        &this->CrackedEdgesSize[0],        &this->CrackedEdgesSize[1],        mxGetAssignedField(matlab_struct,0,"CrackedEdges"));
+
+	FetchData(&this->VerticesOnGeomEdge,  &this->VerticesOnGeomEdgeSize[0],  &this->VerticesOnGeomEdgeSize[1],  mxGetAssignedField(matlab_struct,0,"VerticesOnGeomEdge"));
+	FetchData(&this->VerticesOnGeomVertex,&this->VerticesOnGeomVertexSize[0],&this->VerticesOnGeomVertexSize[1],mxGetAssignedField(matlab_struct,0,"VerticesOnGeomVertex"));
+	FetchData(&this->EdgesOnGeomEdge,     &this->EdgesOnGeomEdgeSize[0],     &this->EdgesOnGeomEdgeSize[1],     mxGetAssignedField(matlab_struct,0,"EdgesOnGeomEdge"));
+
+	this->IssmEdgesSize[0]=0,             this->IssmEdgesSize[1]=0;          this->IssmEdges=NULL;
+	FetchData(&this->IssmSegments,        &this->IssmSegmentsSize[0],        &this->IssmSegmentsSize[1],        mxGetAssignedField(matlab_struct,0,"IssmSegments"));
+
+	this->ElementConnectivitySize[0]=0,      this->ElementConnectivitySize[1]=0;      this->ElementConnectivity=NULL;
+	this->NodalConnectivitySize[0]=0,        this->NodalConnectivitySize[1]=0;        this->NodalConnectivity=NULL;
+	this->NodalElementConnectivitySize[0]=0, this->NodalElementConnectivitySize[1]=0; this->NodalElementConnectivity=NULL;
+
+}
+/*}}}*/
+
+/*Methods*/
+/*FUNCTION BamgMesh::SetStructureFields{{{1*/
+void BamgMesh::SetStructureFields(void* module_struct){
+	
+	mxArray** matlab_struct=NULL;
+
+	/*Intermediary*/
+	int         i;
+	mxArray*    output=NULL;
+	const int         numfields=16;
+	const char* fnames[numfields];
+	mwSize      ndim=2;
+	mwSize      dimensions[2]={1,1};
+
+	/*recover pointer: */
+	matlab_struct=(mxArray**)module_struct;
+
+	/*Initialize field names*/
+	i=0;
+	fnames[i++] = "Triangles";
+	fnames[i++] = "Vertices";
+	fnames[i++] = "Edges";
+	fnames[i++] = "IssmSegments";
+	fnames[i++] = "IssmEdges";
+	fnames[i++] = "Quadrilaterals";
+	fnames[i++] = "VerticesOnGeomVertex";
+	fnames[i++] = "VerticesOnGeomEdge";
+	fnames[i++] = "EdgesOnGeomEdge";
+	fnames[i++] = "SubDomains";
+	fnames[i++] = "SubDomainsFromGeom";
+	fnames[i++] = "ElementConnectivity";
+	fnames[i++] = "NodalConnectivity";
+	fnames[i++] = "NodalElementConnectivity";
+	fnames[i++] = "CrackedVertices";
+	fnames[i++] = "CrackedEdges";
+	_assert_(i==numfields);
+
+	/*Initialize Matlab structure*/
+	output=mxCreateStructArray(ndim,dimensions,numfields,fnames);
+
+	/*set each matlab each field*/
+	i=0;
+	i++; SetStructureField(output,"Triangles",                this->TrianglesSize[0],                this->TrianglesSize[1],                 this->Triangles);
+	i++; SetStructureField(output,"Vertices",                 this->VerticesSize[0],                 this->VerticesSize[1],                  this->Vertices);
+	i++; SetStructureField(output,"Edges",                    this->EdgesSize[0],                    this->EdgesSize[1],                     this->Edges);
+	i++; SetStructureField(output,"IssmSegments",             this->IssmSegmentsSize[0],             this->IssmSegmentsSize[1],              this->IssmSegments);
+	i++; SetStructureField(output,"IssmEdges",                this->IssmEdgesSize[0],                this->IssmEdgesSize[1],                 this->IssmEdges);
+	i++; SetStructureField(output,"Quadrilaterals",           this->QuadrilateralsSize[0],           this->QuadrilateralsSize[1],            this->Quadrilaterals);
+	i++; SetStructureField(output,"VerticesOnGeomVertex",this->VerticesOnGeomVertexSize[0],this->VerticesOnGeomVertexSize[1], this->VerticesOnGeomVertex);
+	i++; SetStructureField(output,"VerticesOnGeomEdge",  this->VerticesOnGeomEdgeSize[0],  this->VerticesOnGeomEdgeSize[1],   this->VerticesOnGeomEdge);
+	i++; SetStructureField(output,"EdgesOnGeomEdge",     this->EdgesOnGeomEdgeSize[0],     this->EdgesOnGeomEdgeSize[1],      this->EdgesOnGeomEdge);
+	i++; SetStructureField(output,"SubDomains",               this->SubDomainsSize[0],               this->SubDomainsSize[1],                this->SubDomains);
+	i++; SetStructureField(output,"SubDomainsFromGeom",       this->SubDomainsFromGeomSize[0],       this->SubDomainsFromGeomSize[1],        this->SubDomainsFromGeom);
+	i++; SetStructureField(output,"ElementConnectivity",      this->ElementConnectivitySize[0],      this->ElementConnectivitySize[1],       this->ElementConnectivity);
+	i++; SetStructureField(output,"NodalConnectivity",        this->NodalConnectivitySize[0],        this->NodalConnectivitySize[1],         this->NodalConnectivity);
+	i++; SetStructureField(output,"NodalElementConnectivity", this->NodalElementConnectivitySize[0], this->NodalElementConnectivitySize[1],  this->NodalElementConnectivity);
+	i++; SetStructureField(output,"CrackedVertices",          this->CrackedVerticesSize[0],          this->CrackedVerticesSize[1],           this->CrackedVertices);
+	i++; SetStructureField(output,"CrackedEdges",             this->CrackedEdgesSize[0],             this->CrackedEdgesSize[1],              this->CrackedEdges);
+	_assert_(i==numfields);
+
+	/*Assign output*/
+	*matlab_struct=output;
+
+}
+/*}}}*/
+/*FUNCTION BamgMesh::SetStructureField{{{1*/
+void BamgMesh::SetStructureField(void* module_struct,const char* fieldname,int fieldrows,int fieldcols,double* fieldpointer){
+
+	/*Intermediary*/
+	mxArray*    matlab_struct=NULL;
+	int         i1,i2;
+	mxArray*    pfield=NULL;
+	mxArray*    pfield2=NULL;
+
+	/*recover pointer: */
+	matlab_struct=(mxArray*)module_struct;
+
+	/*Copy field*/
+	double*  fieldcopy=NULL;
+	if (fieldrows*fieldcols){
+		fieldcopy=(double*)xmalloc(fieldrows*fieldcols*sizeof(double));
+		for(i1=0;i1<fieldrows;i1++){
+			for(i2=0;i2<fieldcols;i2++){
+				fieldcopy[fieldcols*i1+i2]=fieldpointer[fieldcols*i1+i2];
+			}
+		}
+	}
+
+	/*Set matlab field*/
+	pfield=mxCreateDoubleMatrix(0,0,mxREAL);
+	mxSetM(pfield,fieldcols);
+	mxSetN(pfield,fieldrows);
+	mxSetPr(pfield,fieldcopy);
+	mexCallMATLAB(1,&pfield2,1,&pfield,"transpose");//transpose
+	mxSetField(matlab_struct,0,fieldname,pfield2);
+}
+/*}}}*/
Index: /issm/trunk-jpl/src/c/matlab/objects/Bamg/BamgOpts.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/objects/Bamg/BamgOpts.cpp	(revision 12011)
+++ /issm/trunk-jpl/src/c/matlab/objects/Bamg/BamgOpts.cpp	(revision 12011)
@@ -0,0 +1,50 @@
+#include <stdio.h>
+#include "../../shared/shared.h"
+#include "../../include/include.h"
+#include "../objects.h"
+#include "../../io/io.h"
+
+/*Constructors/Destructors*/
+/*FUNCTION BamgOpts::BamgOpts(void* module_struct){{{1*/
+BamgOpts::BamgOpts(void* module_struct){
+	
+	mxArray*    matlab_struct=NULL;
+
+	/*recover pointer: */
+	matlab_struct=(mxArray*)module_struct;
+
+	FetchData(&this->anisomax,mxGetField(matlab_struct,0,"anisomax"));
+	FetchData(&this->cutoff,mxGetField(matlab_struct,0,"cutoff"));
+	FetchData(&this->coeff,mxGetField(matlab_struct,0,"coeff"));
+	FetchData(&this->errg,mxGetField(matlab_struct,0,"errg"));
+	FetchData(&this->gradation,mxGetField(matlab_struct,0,"gradation"));
+	FetchData(&this->Hessiantype,mxGetField(matlab_struct,0,"Hessiantype"));
+	FetchData(&this->MaxCornerAngle,mxGetField(matlab_struct,0,"MaxCornerAngle"));
+	FetchData(&this->maxnbv,mxGetField(matlab_struct,0,"maxnbv"));
+	FetchData(&this->maxsubdiv,mxGetField(matlab_struct,0,"maxsubdiv"));
+	FetchData(&this->Metrictype,mxGetField(matlab_struct,0,"Metrictype"));
+	FetchData(&this->nbjacobi,mxGetField(matlab_struct,0,"nbjacobi"));
+	FetchData(&this->nbsmooth,mxGetField(matlab_struct,0,"nbsmooth"));
+	FetchData(&this->omega,mxGetField(matlab_struct,0,"omega"));
+	FetchData(&this->power,mxGetField(matlab_struct,0,"power"));
+	FetchData(&this->verbose,mxGetField(matlab_struct,0,"verbose"));
+
+	FetchData(&this->Crack,mxGetField(matlab_struct,0,"Crack"));
+	FetchData(&this->geometricalmetric,mxGetField(matlab_struct,0,"geometricalmetric"));
+	FetchData(&this->KeepVertices,mxGetField(matlab_struct,0,"KeepVertices"));
+	FetchData(&this->splitcorners,mxGetField(matlab_struct,0,"splitcorners"));
+
+	FetchData(&this->hmin,mxGetField(matlab_struct,0,"hmin"));
+	FetchData(&this->hmax,mxGetField(matlab_struct,0,"hmax"));
+	FetchData(&this->hminVertices,&this->hminVerticesSize[0],&this->hminVerticesSize[1],mxGetField(matlab_struct,0,"hminVertices"));
+	FetchData(&this->hmaxVertices,&this->hmaxVerticesSize[0],&this->hmaxVerticesSize[1],mxGetField(matlab_struct,0,"hmaxVertices"));
+	FetchData(&this->hVertices,&this->hVerticesSize[0],&this->hVerticesSize[1],mxGetField(matlab_struct,0,"hVertices"));
+	FetchData(&this->metric,&this->metricSize[0],&this->metricSize[1],mxGetField(matlab_struct,0,"metric"));
+	FetchData(&this->field,&this->fieldSize[0],&this->fieldSize[1],mxGetField(matlab_struct,0,"field"));
+	FetchData(&this->err,&this->errSize[0],&this->errSize[1],mxGetField(matlab_struct,0,"err"));
+
+	/*Additional checks*/
+	this->Check();
+
+}
+/*}}}*/
Index: /issm/trunk-jpl/src/c/modules/Dakotax/Dakotax.cpp
===================================================================
--- /issm/trunk-jpl/src/c/modules/Dakotax/Dakotax.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/modules/Dakotax/Dakotax.cpp	(revision 12011)
@@ -51,9 +51,5 @@
 #endif
 
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void Dakotax(mxArray* femmodel){ 
-#else
 void Dakotax(FemModel* femmodel){ 
-#endif
 
 
@@ -69,9 +65,5 @@
 
 	/*Retrieve parameters: */
-	#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-	FetchData((Parameters**)&parameters,mxGetField((mxArray*)femmodel,0,"parameters"));
-	#else
 	parameters=femmodel->parameters;
-	#endif
 
 	/*Recover dakota_input_file, dakota_output_file and dakota_error_file, in the parameters dataset in parallel */
@@ -86,9 +78,5 @@
 		// Instantiate/initialize the parallel library and problem description
 		// database objects.
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-			Dakota::ParallelLibrary parallel_lib; //use Dakota's standard library mode constructor
-		#else
-			Dakota::ParallelLibrary parallel_lib("serial"); //use our own ISSM Dakota library mode constructor, which only fires up Dakota on CPU 0. 
-		#endif
+		Dakota::ParallelLibrary parallel_lib("serial"); //use our own ISSM Dakota library mode constructor, which only fires up Dakota on CPU 0. 
 		Dakota::ProblemDescDB problem_db(parallel_lib); 
 
@@ -143,8 +131,4 @@
 	xfree((void**)&dakota_output_file);
 
-	#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-	delete parameters;
-	#endif
-
 	#endif //#ifdef _HAVE_DAKOTA_
 }
Index: /issm/trunk-jpl/src/c/modules/Dakotax/Dakotax.h
===================================================================
--- /issm/trunk-jpl/src/c/modules/Dakotax/Dakotax.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/modules/Dakotax/Dakotax.h	(revision 12011)
@@ -10,14 +10,9 @@
 
 /* local prototypes: */
-int SpawnCore(double* responses, int numresponses, double* variables, char** variables_descriptors,int numvariables, void* femmodel,int counter);
+int  SpawnCore(double* responses, int numresponses, double* variables, char** variables_descriptors,int numvariables, void* femmodel,int counter);
 int  DescriptorIndex(char* root, int* pindex,char* descriptor);
 
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void Dakotax(mxArray* femmodel);
-void SpawnCoreSerial(double* responses, int numresponses, double* variables, char** variables_descriptors,int numvariables, mxArray* femmodel,int counter);
-#else
 void Dakotax(FemModel* femmodel);
 void SpawnCoreParallel(double* responses, int numresponses, double* variables, char** variables_descriptors,int numvariables, FemModel* femmodel,int counter);
-#endif
 void DakotaResponses(double* responses,char** responses_descriptors,int numresponses,FemModel* femmodel);
 void DakotaMPI_Bcast(double** pvariables, char*** pvariables_descriptors,int* pnumvariables, int* pnumresponses);
Index: /issm/trunk-jpl/src/c/modules/Dakotax/SpawnCore.cpp
===================================================================
--- /issm/trunk-jpl/src/c/modules/Dakotax/SpawnCore.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/modules/Dakotax/SpawnCore.cpp	(revision 12011)
@@ -21,12 +21,9 @@
 	/*Branch into a serial SpawnCore and a parallel SpawnCore: */
 
-	#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		SpawnCoreSerial(responses, numresponses, variables, variables_descriptors,numvariables, (mxArray*)femmodel, counter);
-	#else
-		/*Call SpawnCoreParallel unless counter=-1 on cpu0, in which case, bail out and return 0: */
-		MPI_Bcast(&counter,1,MPI_INT,0,MPI_COMM_WORLD); 
-		if(counter==-1)return 0;
-		SpawnCoreParallel(responses, numresponses, variables, variables_descriptors,numvariables, (FemModel*)femmodel,counter);
-	#endif
+	/*Call SpawnCoreParallel unless counter=-1 on cpu0, in which case, bail out and return 0: */
+	MPI_Bcast(&counter,1,MPI_INT,0,MPI_COMM_WORLD); 
+	if(counter==-1)return 0;
+
+	SpawnCoreParallel(responses, numresponses, variables, variables_descriptors,numvariables, (FemModel*)femmodel,counter);
 	return 1;
 }
Index: sm/trunk-jpl/src/c/modules/Dakotax/SpawnCoreSerial.cpp
===================================================================
--- /issm/trunk-jpl/src/c/modules/Dakotax/SpawnCoreSerial.cpp	(revision 12010)
+++ 	(revision )
@@ -1,71 +1,0 @@
-/*!\file:  SpawnCoreSerial.cpp
- * \brief: run core ISSM solution using Dakota inputs. Call the Serial core solution, using mexCallMATLAB
- * \sa SpawnCore.cpp SpawnCoreParallel.cpp
- *
- */ 
-
-#ifdef HAVE_CONFIG_H
-	#include <config.h>
-#else
-#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
-#endif
-
-#include "../../objects/objects.h"
-#include "../../io/io.h"
-#include "../../EnumDefinitions/EnumDefinitions.h"
-#include "../../shared/shared.h"
-#include "./Dakotax.h"
-#include "../../include/include.h"
-
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void SpawnCoreSerial(double* responses, int numresponses, double* variables, char** variables_descriptors,int numvariables, mxArray* femmodel, int counter){
-
-	int i;
-
-	//inputs to matlab routine
-	mxArray* mxvariables=NULL;
-	double*  variables_copy=NULL;
-	mxArray* mxvariabledescriptors=NULL;
-	mxArray* mxcounter=NULL;
-	mwSize   dims[2]={0};
-
-	//mexCallMATLAB arrays
-	mxArray* array[4];
-
-	//output from SpawnCore in matlab routine.
-	mxArray* mxresponses=NULL;
-
-	/*Create variables and variable descriptors mxArrays that we will feed to the core solution for update of the inputs: */
-	mxvariables=mxCreateDoubleMatrix(numvariables,1,mxREAL);
-	variables_copy=(double*)xmalloc(numvariables*sizeof(double));
-	memcpy(variables_copy,variables,numvariables*sizeof(double));
-	mxSetPr(mxvariables,variables_copy);
-
-	dims[0]=numvariables;
-	dims[1]=1;
-	mxvariabledescriptors=mxCreateCellArray(2,dims);
-	for(i=0;i<numvariables;i++){
-		mxSetCell(mxvariabledescriptors,i,mxCreateString(variables_descriptors[i]));
-	}
-
-	mxcounter=mxCreateDoubleScalar((double)counter);
-
-	//call SpwanCore matlab routine.
-	array[0]=femmodel;
-	array[1]=mxvariables;
-	array[2]=mxvariabledescriptors;
-	array[3]=mxcounter;
-
-	mexCallMATLAB(1,&mxresponses,4,array,"SpawnCore");
-
-	/*copy responses back to dakota: */
-	memcpy(responses,mxGetPr(mxresponses),numresponses*sizeof(double));
-
-	//destroy constructed arrays: 
-	mxDestroyArray(mxvariables);
-	mxDestroyArray(mxvariabledescriptors);
-	mxDestroyArray(mxresponses);
-	mxDestroyArray(mxcounter);
-
-}
-#endif
Index: /issm/trunk-jpl/src/c/modules/OutputResultsx/OutputResultsx.cpp
===================================================================
--- /issm/trunk-jpl/src/c/modules/OutputResultsx/OutputResultsx.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/modules/OutputResultsx/OutputResultsx.cpp	(revision 12011)
@@ -16,9 +16,5 @@
 #include "../../objects/objects.h"
 		
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void OutputResultsx(mxArray** pdataref, Elements* elements, Nodes* nodes, Vertices* vertices, Loads* loads, Materials* materials, Parameters* parameters,Results* results){
-#else 
 void OutputResultsx(                    Elements* elements, Nodes* nodes, Vertices* vertices, Loads* loads, Materials* materials, Parameters* parameters,Results* results){
-#endif
 
 	extern int  my_rank;
@@ -31,11 +27,4 @@
 	bool        dakota_analysis         = false;
 	
-	#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-	const char **fnames      = NULL;
-	mwSize       onebyone[2] = {0,0};
-	mwSize       ndim        = 2;
-	int          nfields=0;
-	#endif
-
 	/*retrieve parameters: */
 	parameters->FindParam(&dakota_analysis,QmuIsdakotaEnum);
@@ -43,7 +32,4 @@
 	if(dakota_analysis){
 		//no need to output anything, Dakota analysis has different outputs
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		*pdataref=mxCreateStructArray( ndim,onebyone,nfields,fnames);
-		#endif
 		return; 
 	}
@@ -56,5 +42,4 @@
 	/*Results do not include the type of solution being run	. In parallel, we output results to a filename, 
 	 *therefore, we need to include the solutiontype into the filename: */
-	#ifdef _PARALLEL_
 	if(my_rank==0){
 		parameters->FindParam(&solutiontype,SolutionTypeEnum);
@@ -88,26 +73,19 @@
 		parameters->SetParam(fid,OutputFilePointerEnum);
 	}
-	#endif
 
-	/*Write results to disk (in parallel), or to memory (in serial mode): */
-	#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		results->Write(pdataref);
-	#else
-		results->Write(parameters);
-	#endif
+	/*Write results to disk: */
+	results->Write(parameters);
 
 	/*Delete and reinitialize results, in parallel: */
-	#ifdef _PARALLEL_
-		results->clear();
+	results->clear();
 
-		/*Close output file? :*/
-		/*WARNING: issm.cpp is taking care of it for now (quick fix)
-		if((step==1) && (time==0)){
-			if(io_gather){
-				if(my_rank==0) pfclose(fid,outputfilename);
-			}
-			else pfclose(fid,cpu_outputfilename);
-		}
-		*/
-	#endif
+	/*Close output file? :*/
+	/*WARNING: issm.cpp is taking care of it for now (quick fix)
+	  if((step==1) && (time==0)){
+	  if(io_gather){
+	  if(my_rank==0) pfclose(fid,outputfilename);
+	  }
+	  else pfclose(fid,cpu_outputfilename);
+	  }
+	*/
 }
Index: /issm/trunk-jpl/src/c/modules/OutputResultsx/OutputResultsx.h
===================================================================
--- /issm/trunk-jpl/src/c/modules/OutputResultsx/OutputResultsx.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/modules/OutputResultsx/OutputResultsx.h	(revision 12011)
@@ -14,10 +14,5 @@
 #include "../../Container/Container.h"
 
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-#include <mex.h>
-void OutputResultsx(mxArray** pdataref, Elements* elements, Nodes* nodes, Vertices* vertices, Loads* loads,  Materials* materials, Parameters* parameters, Results* results);
-#else
 void OutputResultsx(Elements* elements, Nodes* nodes, Vertices* vertices, Loads* loads,  Materials* materials, Parameters* parameters, Results* results);
-#endif
 
 #endif  /* _OUTPUTRESULTS_H */
Index: /issm/trunk-jpl/src/c/objects/Bamg/BamgGeom.cpp
===================================================================
--- /issm/trunk-jpl/src/c/objects/Bamg/BamgGeom.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Bamg/BamgGeom.cpp	(revision 12011)
@@ -19,20 +19,4 @@
 }
 /*}}}*/
-/*FUNCTION BamgGeom::BamgGeom(mxArray* matlab_struct){{{1*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-BamgGeom::BamgGeom(mxArray* matlab_struct){
-
-	FetchData(&this->Vertices,        &this->VerticesSize[0],        &this->VerticesSize[1],        mxGetAssignedField(matlab_struct,0,"Vertices"));
-	FetchData(&this->Edges,           &this->EdgesSize[0],           &this->EdgesSize[1],           mxGetAssignedField(matlab_struct,0,"Edges"));
-	this->TangentAtEdgesSize[0]=0,    this->TangentAtEdgesSize[1]=0;    this->TangentAtEdges=NULL;
-	FetchData(&this->Corners,         &this->CornersSize[0],         &this->CornersSize[1],         mxGetAssignedField(matlab_struct,0,"Corners"));
-	FetchData(&this->RequiredVertices,&this->RequiredVerticesSize[0],&this->RequiredVerticesSize[1],mxGetAssignedField(matlab_struct,0,"RequiredVertices"));
-	FetchData(&this->RequiredEdges,   &this->RequiredEdgesSize[0],   &this->RequiredEdgesSize[1],   mxGetAssignedField(matlab_struct,0,"RequiredEdges"));
-	FetchData(&this->CrackedEdges,    &this->CrackedEdgesSize[0],    &this->CrackedEdgesSize[1],    mxGetAssignedField(matlab_struct,0,"CrackedEdges"));
-	FetchData(&this->SubDomains,      &this->SubDomainsSize[0],      &this->SubDomainsSize[1],      mxGetAssignedField(matlab_struct,0,"SubDomains"));
-
-}
-#endif
-/*}}}*/
 /*FUNCTION BamgGeom::~BamgGeom(){{{1*/
 BamgGeom::~BamgGeom(){
@@ -49,77 +33,2 @@
 }
 /*}}}*/
-
-/*Methods*/
-/*FUNCTION BamgGeom::SetMatlabStructureFields{{{1*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void BamgGeom::SetMatlabStructureFields(mxArray** matlab_struct){
-
-	/*Intermediary*/
-	int         i;
-	mxArray*    output=NULL;
-	const int         numfields=7;
-	const char* fnames[numfields];
-	mwSize      ndim=2;
-	mwSize      dimensions[2]={1,1};
-
-	/*Initialize field names*/
-	i=0;
-	fnames[i++] = "Vertices";
-	fnames[i++] = "Edges";
-	fnames[i++] = "TangentAtEdges";
-	fnames[i++] = "RequiredVertices";
-	fnames[i++] = "RequiredEdges";
-	fnames[i++] = "CrackedEdges";
-	fnames[i++] = "SubDomains";
-	_assert_(i==numfields);
-
-	/*Initialize Matlab structure*/
-	output=mxCreateStructArray(ndim,dimensions,numfields,fnames);
-
-	/*set each matlab each field*/
-	i=0;
-	i++; SetMatlabStructureField(output,"Vertices",        this->VerticesSize[0],        this->VerticesSize[1],        this->Vertices);
-	i++; SetMatlabStructureField(output,"Edges",           this->EdgesSize[0],           this->EdgesSize[1],           this->Edges);
-	i++; SetMatlabStructureField(output,"TangentAtEdges",  this->TangentAtEdgesSize[0],  this->TangentAtEdgesSize[1],  this->TangentAtEdges);
-	i++; SetMatlabStructureField(output,"RequiredVertices",this->RequiredVerticesSize[0],this->RequiredVerticesSize[1],this->RequiredVertices);
-	i++; SetMatlabStructureField(output,"RequiredEdges",   this->RequiredEdgesSize[0],   this->RequiredEdgesSize[1],   this->RequiredEdges);
-	i++; SetMatlabStructureField(output,"CrackedEdges",    this->CrackedEdgesSize[0],    this->CrackedEdgesSize[1],    this->CrackedEdges);
-	i++; SetMatlabStructureField(output,"SubDomains",      this->SubDomainsSize[0],      this->SubDomainsSize[1],      this->SubDomains);
-	_assert_(i==numfields);
-
-	/*Assign output*/
-	*matlab_struct=output;
-
-}
-#endif
-/*}}}*/
-/*FUNCTION BamgGeom::SetMatlabStructureField{{{1*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void BamgGeom::SetMatlabStructureField(mxArray* matlab_struct,const char* fieldname,int fieldrows,int fieldcols,double* fieldpointer){
-
-	/*Intermediary*/
-	int         i1,i2;
-	mxArray*    pfield=NULL;
-	mxArray*    pfield2=NULL;
-
-	/*Copy field*/
-	double*  fieldcopy=NULL;
-	if (fieldrows*fieldcols){
-		fieldcopy=(double*)xmalloc(fieldrows*fieldcols*sizeof(double));
-		for(i1=0;i1<fieldrows;i1++){
-			for(i2=0;i2<fieldcols;i2++){
-				fieldcopy[fieldcols*i1+i2]=fieldpointer[fieldcols*i1+i2];
-			}
-		}
-	}
-
-	/*Set matlab field*/
-	pfield=mxCreateDoubleMatrix(0,0,mxREAL);
-	mxSetM(pfield,fieldcols);
-	mxSetN(pfield,fieldrows);
-	mxSetPr(pfield,fieldcopy);
-	mexCallMATLAB(1,&pfield2,1,&pfield,"transpose");//transpose
-	mxSetField(matlab_struct,0,fieldname,pfield2);
-}
-#endif
-/*}}}*/
Index: /issm/trunk-jpl/src/c/objects/Bamg/BamgGeom.h
===================================================================
--- /issm/trunk-jpl/src/c/objects/Bamg/BamgGeom.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Bamg/BamgGeom.h	(revision 12011)
@@ -4,8 +4,4 @@
 #ifndef _BAMGGEOM_H_
 #define _BAMGGEOM_H_
-
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-#include <mex.h>
-#endif
 
 class BamgGeom{
@@ -30,13 +26,9 @@
 
 		BamgGeom();
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		BamgGeom(mxArray* matlab_struct);
-		#endif
+		BamgGeom(void* module_struct);
 		~BamgGeom();
 
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		void SetMatlabStructureFields(mxArray** matlab_struct);
-		void SetMatlabStructureField(mxArray* matlab_struct,const char* fieldname,int fieldrows,int fieldcols,double* fieldpointer);
-		#endif
+		void SetStructureFields(void* matlab_struct);
+		void SetStructureField(void* matlab_struct,const char* fieldname,int fieldrows,int fieldcols,double* fieldpointer);
 };
 
Index: /issm/trunk-jpl/src/c/objects/Bamg/BamgMesh.cpp
===================================================================
--- /issm/trunk-jpl/src/c/objects/Bamg/BamgMesh.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Bamg/BamgMesh.cpp	(revision 12011)
@@ -32,34 +32,4 @@
 }
 /*}}}*/
-/*FUNCTION BamgMesh::BamgMesh(mxArray* matlab_struct){{{1*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-BamgMesh::BamgMesh(mxArray* matlab_struct){
-
-	int lines,cols;
-
-	FetchData(&this->Vertices,            &this->VerticesSize[0],            &this->VerticesSize[1],            mxGetAssignedField(matlab_struct,0,"Vertices"));
-	FetchData(&this->Edges,               &this->EdgesSize[0],               &this->EdgesSize[1],               mxGetAssignedField(matlab_struct,0,"Edges"));
-	FetchData(&this->Triangles,           &this->TrianglesSize[0],           &this->TrianglesSize[1],           mxGetAssignedField(matlab_struct,0,"Triangles"));
-	this->QuadrilateralsSize[0]=0,        this->QuadrilateralsSize[1]=0;     this->Quadrilaterals=NULL;
-
-	this->SubDomainsSize[0]=0,            this->SubDomainsSize[1]=0;         this->SubDomains=NULL;
-	this->SubDomainsFromGeomSize[0]=0,    this->SubDomainsFromGeomSize[1]=0; this->SubDomainsFromGeom=NULL;
-	this->CrackedVerticesSize[0]=0,       this->CrackedVerticesSize[1]=0;    this->CrackedVertices=NULL;
-	FetchData(&this->CrackedEdges,        &this->CrackedEdgesSize[0],        &this->CrackedEdgesSize[1],        mxGetAssignedField(matlab_struct,0,"CrackedEdges"));
-
-	FetchData(&this->VerticesOnGeomEdge,  &this->VerticesOnGeomEdgeSize[0],  &this->VerticesOnGeomEdgeSize[1],  mxGetAssignedField(matlab_struct,0,"VerticesOnGeomEdge"));
-	FetchData(&this->VerticesOnGeomVertex,&this->VerticesOnGeomVertexSize[0],&this->VerticesOnGeomVertexSize[1],mxGetAssignedField(matlab_struct,0,"VerticesOnGeomVertex"));
-	FetchData(&this->EdgesOnGeomEdge,     &this->EdgesOnGeomEdgeSize[0],     &this->EdgesOnGeomEdgeSize[1],     mxGetAssignedField(matlab_struct,0,"EdgesOnGeomEdge"));
-
-	this->IssmEdgesSize[0]=0,             this->IssmEdgesSize[1]=0;          this->IssmEdges=NULL;
-	FetchData(&this->IssmSegments,        &this->IssmSegmentsSize[0],        &this->IssmSegmentsSize[1],        mxGetAssignedField(matlab_struct,0,"IssmSegments"));
-
-	this->ElementConnectivitySize[0]=0,      this->ElementConnectivitySize[1]=0;      this->ElementConnectivity=NULL;
-	this->NodalConnectivitySize[0]=0,        this->NodalConnectivitySize[1]=0;        this->NodalConnectivity=NULL;
-	this->NodalElementConnectivitySize[0]=0, this->NodalElementConnectivitySize[1]=0; this->NodalElementConnectivity=NULL;
-
-}
-#endif
-/*}}}*/
 /*FUNCTION BamgMesh::~BamgMesh(){{{1*/
 BamgMesh::~BamgMesh(){
@@ -89,95 +59,2 @@
 }
 /*}}}*/
-
-/*Methods*/
-/*FUNCTION BamgMesh::SetMatlabStructureFields{{{1*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void BamgMesh::SetMatlabStructureFields(mxArray** matlab_struct){
-
-	/*Intermediary*/
-	int         i;
-	mxArray*    output=NULL;
-	const int         numfields=16;
-	const char* fnames[numfields];
-	mwSize      ndim=2;
-	mwSize      dimensions[2]={1,1};
-
-	/*Initialize field names*/
-	i=0;
-	fnames[i++] = "Triangles";
-	fnames[i++] = "Vertices";
-	fnames[i++] = "Edges";
-	fnames[i++] = "IssmSegments";
-	fnames[i++] = "IssmEdges";
-	fnames[i++] = "Quadrilaterals";
-	fnames[i++] = "VerticesOnGeomVertex";
-	fnames[i++] = "VerticesOnGeomEdge";
-	fnames[i++] = "EdgesOnGeomEdge";
-	fnames[i++] = "SubDomains";
-	fnames[i++] = "SubDomainsFromGeom";
-	fnames[i++] = "ElementConnectivity";
-	fnames[i++] = "NodalConnectivity";
-	fnames[i++] = "NodalElementConnectivity";
-	fnames[i++] = "CrackedVertices";
-	fnames[i++] = "CrackedEdges";
-	_assert_(i==numfields);
-
-	/*Initialize Matlab structure*/
-	output=mxCreateStructArray(ndim,dimensions,numfields,fnames);
-
-	/*set each matlab each field*/
-	i=0;
-	i++; SetMatlabStructureField(output,"Triangles",                this->TrianglesSize[0],                this->TrianglesSize[1],                 this->Triangles);
-	i++; SetMatlabStructureField(output,"Vertices",                 this->VerticesSize[0],                 this->VerticesSize[1],                  this->Vertices);
-	i++; SetMatlabStructureField(output,"Edges",                    this->EdgesSize[0],                    this->EdgesSize[1],                     this->Edges);
-	i++; SetMatlabStructureField(output,"IssmSegments",             this->IssmSegmentsSize[0],             this->IssmSegmentsSize[1],              this->IssmSegments);
-	i++; SetMatlabStructureField(output,"IssmEdges",                this->IssmEdgesSize[0],                this->IssmEdgesSize[1],                 this->IssmEdges);
-	i++; SetMatlabStructureField(output,"Quadrilaterals",           this->QuadrilateralsSize[0],           this->QuadrilateralsSize[1],            this->Quadrilaterals);
-	i++; SetMatlabStructureField(output,"VerticesOnGeomVertex",this->VerticesOnGeomVertexSize[0],this->VerticesOnGeomVertexSize[1], this->VerticesOnGeomVertex);
-	i++; SetMatlabStructureField(output,"VerticesOnGeomEdge",  this->VerticesOnGeomEdgeSize[0],  this->VerticesOnGeomEdgeSize[1],   this->VerticesOnGeomEdge);
-	i++; SetMatlabStructureField(output,"EdgesOnGeomEdge",     this->EdgesOnGeomEdgeSize[0],     this->EdgesOnGeomEdgeSize[1],      this->EdgesOnGeomEdge);
-	i++; SetMatlabStructureField(output,"SubDomains",               this->SubDomainsSize[0],               this->SubDomainsSize[1],                this->SubDomains);
-	i++; SetMatlabStructureField(output,"SubDomainsFromGeom",       this->SubDomainsFromGeomSize[0],       this->SubDomainsFromGeomSize[1],        this->SubDomainsFromGeom);
-	i++; SetMatlabStructureField(output,"ElementConnectivity",      this->ElementConnectivitySize[0],      this->ElementConnectivitySize[1],       this->ElementConnectivity);
-	i++; SetMatlabStructureField(output,"NodalConnectivity",        this->NodalConnectivitySize[0],        this->NodalConnectivitySize[1],         this->NodalConnectivity);
-	i++; SetMatlabStructureField(output,"NodalElementConnectivity", this->NodalElementConnectivitySize[0], this->NodalElementConnectivitySize[1],  this->NodalElementConnectivity);
-	i++; SetMatlabStructureField(output,"CrackedVertices",          this->CrackedVerticesSize[0],          this->CrackedVerticesSize[1],           this->CrackedVertices);
-	i++; SetMatlabStructureField(output,"CrackedEdges",             this->CrackedEdgesSize[0],             this->CrackedEdgesSize[1],              this->CrackedEdges);
-	_assert_(i==numfields);
-
-	/*Assign output*/
-	*matlab_struct=output;
-
-}
-#endif
-/*}}}*/
-/*FUNCTION BamgMesh::SetMatlabStructureField{{{1*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void BamgMesh::SetMatlabStructureField(mxArray* matlab_struct,const char* fieldname,int fieldrows,int fieldcols,double* fieldpointer){
-
-	/*Intermediary*/
-	int         i1,i2;
-	mxArray*    pfield=NULL;
-	mxArray*    pfield2=NULL;
-
-	/*Copy field*/
-	double*  fieldcopy=NULL;
-	if (fieldrows*fieldcols){
-		fieldcopy=(double*)xmalloc(fieldrows*fieldcols*sizeof(double));
-		for(i1=0;i1<fieldrows;i1++){
-			for(i2=0;i2<fieldcols;i2++){
-				fieldcopy[fieldcols*i1+i2]=fieldpointer[fieldcols*i1+i2];
-			}
-		}
-	}
-
-	/*Set matlab field*/
-	pfield=mxCreateDoubleMatrix(0,0,mxREAL);
-	mxSetM(pfield,fieldcols);
-	mxSetN(pfield,fieldrows);
-	mxSetPr(pfield,fieldcopy);
-	mexCallMATLAB(1,&pfield2,1,&pfield,"transpose");//transpose
-	mxSetField(matlab_struct,0,fieldname,pfield2);
-}
-#endif
-/*}}}*/
Index: /issm/trunk-jpl/src/c/objects/Bamg/BamgMesh.h
===================================================================
--- /issm/trunk-jpl/src/c/objects/Bamg/BamgMesh.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Bamg/BamgMesh.h	(revision 12011)
@@ -4,8 +4,4 @@
 #ifndef _BAMGMESH_H_
 #define _BAMGMESH_H_
-
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-#include <mex.h>
-#endif
 
 class BamgMesh{
@@ -51,13 +47,9 @@
 
 		BamgMesh();
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		BamgMesh(mxArray* matlab_struct);
-		#endif
+		BamgMesh(void* module_struct);
 		~BamgMesh();
 
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		void SetMatlabStructureFields(mxArray** matlab_struct);
-		void SetMatlabStructureField(mxArray* matlab_struct,const char* fieldname,int fieldrows,int fieldcols,double* fieldpointer);
-		#endif
+		void SetStructureFields(void* module_struct);
+		void SetStructureField(void* module_struct,const char* fieldname,int fieldrows,int fieldcols,double* fieldpointer);
 
 };
Index: /issm/trunk-jpl/src/c/objects/Bamg/BamgOpts.cpp
===================================================================
--- /issm/trunk-jpl/src/c/objects/Bamg/BamgOpts.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Bamg/BamgOpts.cpp	(revision 12011)
@@ -40,44 +40,4 @@
 
 }
-/*}}}*/
-/*FUNCTION BamgOpts::BamgOpts(mxArray* matlab_struct){{{1*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-BamgOpts::BamgOpts(mxArray* matlab_struct){
-
-	FetchData(&this->anisomax,mxGetField(matlab_struct,0,"anisomax"));
-	FetchData(&this->cutoff,mxGetField(matlab_struct,0,"cutoff"));
-	FetchData(&this->coeff,mxGetField(matlab_struct,0,"coeff"));
-	FetchData(&this->errg,mxGetField(matlab_struct,0,"errg"));
-	FetchData(&this->gradation,mxGetField(matlab_struct,0,"gradation"));
-	FetchData(&this->Hessiantype,mxGetField(matlab_struct,0,"Hessiantype"));
-	FetchData(&this->MaxCornerAngle,mxGetField(matlab_struct,0,"MaxCornerAngle"));
-	FetchData(&this->maxnbv,mxGetField(matlab_struct,0,"maxnbv"));
-	FetchData(&this->maxsubdiv,mxGetField(matlab_struct,0,"maxsubdiv"));
-	FetchData(&this->Metrictype,mxGetField(matlab_struct,0,"Metrictype"));
-	FetchData(&this->nbjacobi,mxGetField(matlab_struct,0,"nbjacobi"));
-	FetchData(&this->nbsmooth,mxGetField(matlab_struct,0,"nbsmooth"));
-	FetchData(&this->omega,mxGetField(matlab_struct,0,"omega"));
-	FetchData(&this->power,mxGetField(matlab_struct,0,"power"));
-	FetchData(&this->verbose,mxGetField(matlab_struct,0,"verbose"));
-
-	FetchData(&this->Crack,mxGetField(matlab_struct,0,"Crack"));
-	FetchData(&this->geometricalmetric,mxGetField(matlab_struct,0,"geometricalmetric"));
-	FetchData(&this->KeepVertices,mxGetField(matlab_struct,0,"KeepVertices"));
-	FetchData(&this->splitcorners,mxGetField(matlab_struct,0,"splitcorners"));
-
-	FetchData(&this->hmin,mxGetField(matlab_struct,0,"hmin"));
-	FetchData(&this->hmax,mxGetField(matlab_struct,0,"hmax"));
-	FetchData(&this->hminVertices,&this->hminVerticesSize[0],&this->hminVerticesSize[1],mxGetField(matlab_struct,0,"hminVertices"));
-	FetchData(&this->hmaxVertices,&this->hmaxVerticesSize[0],&this->hmaxVerticesSize[1],mxGetField(matlab_struct,0,"hmaxVertices"));
-	FetchData(&this->hVertices,&this->hVerticesSize[0],&this->hVerticesSize[1],mxGetField(matlab_struct,0,"hVertices"));
-	FetchData(&this->metric,&this->metricSize[0],&this->metricSize[1],mxGetField(matlab_struct,0,"metric"));
-	FetchData(&this->field,&this->fieldSize[0],&this->fieldSize[1],mxGetField(matlab_struct,0,"field"));
-	FetchData(&this->err,&this->errSize[0],&this->errSize[1],mxGetField(matlab_struct,0,"err"));
-
-	/*Additional checks*/
-	this->Check();
-
-}
-#endif
 /*}}}*/
 /*FUNCTION BamgOpts::~BamgOpts() {{{1*/
Index: /issm/trunk-jpl/src/c/objects/Bamg/BamgOpts.h
===================================================================
--- /issm/trunk-jpl/src/c/objects/Bamg/BamgOpts.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Bamg/BamgOpts.h	(revision 12011)
@@ -5,8 +5,4 @@
 #ifndef _BAMGOPTS_H_
 #define _BAMGOPTS_H_
-
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-#include <mex.h>
-#endif
 
 class BamgOpts{
@@ -54,7 +50,5 @@
 
 		BamgOpts();
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		BamgOpts(mxArray* matlab_struct);
-		#endif
+		BamgOpts(void* module_struct);
 		~BamgOpts();
 
Index: /issm/trunk-jpl/src/c/objects/ExternalResults/BoolExternalResult.cpp
===================================================================
--- /issm/trunk-jpl/src/c/objects/ExternalResults/BoolExternalResult.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/ExternalResults/BoolExternalResult.cpp	(revision 12011)
@@ -185,17 +185,4 @@
 }
 /*}}}*/
-/*FUNCTION BoolExternalResult::SetMatlabField{{{1*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void BoolExternalResult::SetMatlabField(mxArray* dataref){
-
-	char* name=NULL;
-	this->GetResultName(&name);
-	
-	mxSetField( dataref, this->step-1, name,mxCreateDoubleScalar((double)value));
-	mxSetField( dataref, this->step-1, "time",mxCreateDoubleScalar((double)this->time)); 
-	mxSetField( dataref, this->step-1, "step",mxCreateDoubleScalar((double)this->step)); 
-}
-#endif
-/*}}}*/
 /*FUNCTION BoolExternalResult::GetStep{{{1*/
 int BoolExternalResult::GetStep(void){
Index: /issm/trunk-jpl/src/c/objects/ExternalResults/BoolExternalResult.h
===================================================================
--- /issm/trunk-jpl/src/c/objects/ExternalResults/BoolExternalResult.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/ExternalResults/BoolExternalResult.h	(revision 12011)
@@ -59,7 +59,4 @@
 		void  WriteData(FILE* fid,bool io_gather);
 		void  GetResultName(char**);
-	    #if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		void  SetMatlabField(mxArray* dataref);
-		#endif
 		int   GetStep(void);
 		/*}}}*/
Index: /issm/trunk-jpl/src/c/objects/ExternalResults/DoubleExternalResult.cpp
===================================================================
--- /issm/trunk-jpl/src/c/objects/ExternalResults/DoubleExternalResult.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/ExternalResults/DoubleExternalResult.cpp	(revision 12011)
@@ -181,17 +181,4 @@
 }
 /*}}}*/
-/*FUNCTION DoubleExternalResult::SetMatlabField{{{1*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void DoubleExternalResult::SetMatlabField(mxArray* dataref){
-
-	char* name=NULL;
-	this->GetResultName(&name);
-	mxSetField( dataref,this->step-1, name,mxCreateDoubleScalar(value));
-	mxSetField( dataref, this->step-1, "time",mxCreateDoubleScalar((double)this->time)); 
-	mxSetField( dataref, this->step-1, "step",mxCreateDoubleScalar((double)this->step)); 
-
-}
-#endif
-/*}}}*/
 /*FUNCTION DoubleExternalResult::GetStep{{{1*/
 int DoubleExternalResult::GetStep(void){
Index: /issm/trunk-jpl/src/c/objects/ExternalResults/DoubleExternalResult.h
===================================================================
--- /issm/trunk-jpl/src/c/objects/ExternalResults/DoubleExternalResult.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/ExternalResults/DoubleExternalResult.h	(revision 12011)
@@ -60,7 +60,4 @@
 		void  WriteData(FILE* fid,bool io_gather);
 		void  GetResultName(char**);
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		void  SetMatlabField(mxArray* dataref);
-		#endif
 		int   GetStep(void);
 		/*}}}*/
Index: /issm/trunk-jpl/src/c/objects/ExternalResults/DoubleMatExternalResult.cpp
===================================================================
--- /issm/trunk-jpl/src/c/objects/ExternalResults/DoubleMatExternalResult.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/ExternalResults/DoubleMatExternalResult.cpp	(revision 12011)
@@ -222,37 +222,4 @@
 }
 /*}}}*/
-/*FUNCTION DoubleMatExternalResult::SetMatlabField{{{1*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void DoubleMatExternalResult::SetMatlabField(mxArray* dataref){
-
-	mxArray* pfield=NULL;
-	mxArray* pfield2=NULL;
-	char* name=NULL;
-	double* doublemat=NULL;
-
-	/*Make a copy of the value, to be used by matlab: */
-	doublemat=(double*)xmalloc(M*N*sizeof(double));
-	memcpy(doublemat,values,M*N*sizeof(double));
-
-	/*recover name: */
-	this->GetResultName(&name);
-				
-	/*create matlab matrix: */
-	pfield=mxCreateDoubleMatrix(0,0,mxREAL);
-	mxSetM(pfield,N);
-	mxSetN(pfield,M);
-	mxSetPr(pfield,doublemat);
-	
-	/*transpose the matrix, from c to matlab format */
-	mexCallMATLAB(1,&pfield2, 1, &pfield, "transpose");
-
-	/*set tranpose matrix inside the dataref structure: */
-	mxSetField( dataref, this->step-1, name,pfield2);
-	mxSetField( dataref, this->step-1, "time",mxCreateDoubleScalar((double)this->time));
-	mxSetField( dataref, this->step-1, "step",mxCreateDoubleScalar((double)this->step));
-
-}
-#endif
-/*}}}*/
 /*FUNCTION DoubleMatExternalResult::GetStep{{{1*/
 int DoubleMatExternalResult::GetStep(void){
Index: /issm/trunk-jpl/src/c/objects/ExternalResults/DoubleMatExternalResult.h
===================================================================
--- /issm/trunk-jpl/src/c/objects/ExternalResults/DoubleMatExternalResult.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/ExternalResults/DoubleMatExternalResult.h	(revision 12011)
@@ -61,7 +61,4 @@
 		void  WriteData(FILE* fid,bool io_gather);
 		void  GetResultName(char**);
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		void  SetMatlabField(mxArray* dataref);
-		#endif
 		int   GetStep(void);
 		/*}}}*/
Index: /issm/trunk-jpl/src/c/objects/ExternalResults/DoubleVecExternalResult.cpp
===================================================================
--- /issm/trunk-jpl/src/c/objects/ExternalResults/DoubleVecExternalResult.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/ExternalResults/DoubleVecExternalResult.cpp	(revision 12011)
@@ -206,32 +206,4 @@
 }
 /*}}}*/
-/*FUNCTION DoubleVecExternalResult::SetMatlabField{{{1*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void DoubleVecExternalResult::SetMatlabField(mxArray* dataref){
-
-	mxArray *pfield    = NULL;
-	double  *doublemat = NULL;
-	char    *name      = NULL;
-	double  *doublevec = NULL;
-
-	/*Make a copy of the value, to be used by matlab: */
-	doublevec=(double*)xmalloc(M*sizeof(double));
-	memcpy(doublevec,values,M*sizeof(double));
-
-	/*recover name: */
-	this->GetResultName(&name);
-				
-	/*create matlab matrix: */
-	pfield=mxCreateDoubleMatrix(0,0,mxREAL);
-	mxSetM(pfield,M);
-	mxSetN(pfield,1);
-	mxSetPr(pfield,doublevec);
-
-	mxSetField( dataref, this->step-1, name,pfield);
-	mxSetField( dataref, this->step-1, "time",mxCreateDoubleScalar((double)this->time)); 
-	mxSetField( dataref, this->step-1, "step",mxCreateDoubleScalar((double)this->step)); 
-}
-#endif
-/*}}}*/
 /*FUNCTION DoubleVecExternalResult::GetStep{{{1*/
 int DoubleVecExternalResult::GetStep(void){
Index: /issm/trunk-jpl/src/c/objects/ExternalResults/DoubleVecExternalResult.h
===================================================================
--- /issm/trunk-jpl/src/c/objects/ExternalResults/DoubleVecExternalResult.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/ExternalResults/DoubleVecExternalResult.h	(revision 12011)
@@ -60,7 +60,4 @@
 		void  WriteData(FILE* fid,bool io_gather);
 		void  GetResultName(char**);
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		void  SetMatlabField(mxArray* dataref);
-		#endif
 		int   GetStep(void);
 		/*}}}*/
Index: /issm/trunk-jpl/src/c/objects/ExternalResults/ExternalResult.h
===================================================================
--- /issm/trunk-jpl/src/c/objects/ExternalResults/ExternalResult.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/ExternalResults/ExternalResult.h	(revision 12011)
@@ -33,7 +33,4 @@
 		virtual void  WriteData(FILE* fid,bool io_gather)=0;
 		virtual void  GetResultName(char**)=0;
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		virtual void  SetMatlabField(mxArray* dataref)=0;
-		#endif
 		virtual int   GetStep(void)=0;
 		/*}}}*/
Index: /issm/trunk-jpl/src/c/objects/ExternalResults/IntExternalResult.cpp
===================================================================
--- /issm/trunk-jpl/src/c/objects/ExternalResults/IntExternalResult.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/ExternalResults/IntExternalResult.cpp	(revision 12011)
@@ -185,18 +185,4 @@
 }
 /*}}}*/
-/*FUNCTION IntExternalResult::SetMatlabField{{{1*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void IntExternalResult::SetMatlabField(mxArray* dataref){
-
-	char* name=NULL;
-	this->GetResultName(&name);
-
-	mxSetField( dataref, this->step-1, name,mxCreateDoubleScalar(value));
-	mxSetField( dataref, this->step-1, "time",mxCreateDoubleScalar((double)this->time)); 
-	mxSetField( dataref, this->step-1, "step",mxCreateDoubleScalar((double)this->step)); 
-
-}
-#endif
-/*}}}*/
 /*FUNCTION IntExternalResult::GetStep{{{1*/
 int IntExternalResult::GetStep(void){
Index: /issm/trunk-jpl/src/c/objects/ExternalResults/IntExternalResult.h
===================================================================
--- /issm/trunk-jpl/src/c/objects/ExternalResults/IntExternalResult.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/ExternalResults/IntExternalResult.h	(revision 12011)
@@ -58,7 +58,4 @@
 		void  WriteData(FILE* fid,bool io_gather);
 		void  GetResultName(char**);
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		void  SetMatlabField(mxArray* dataref);
-		#endif
 		int   GetStep(void);
 		/*}}}*/
Index: /issm/trunk-jpl/src/c/objects/ExternalResults/PetscVecExternalResult.cpp
===================================================================
--- /issm/trunk-jpl/src/c/objects/ExternalResults/PetscVecExternalResult.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/ExternalResults/PetscVecExternalResult.cpp	(revision 12011)
@@ -244,29 +244,4 @@
 }
 /*}}}*/
-/*FUNCTION PetscVecExternalResult::SetMatlabField{{{1*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void  PetscVecExternalResult::SetMatlabField(mxArray* dataref){
-
-	mxArray* pfield=NULL;
-	char* name=NULL;
-	double* doublevec=NULL;
-	int M;
-	
-	VecToMPISerial(&doublevec,value);
-	VecGetSize(value,&M);
-	this->GetResultName(&name);
-	
-	pfield=mxCreateDoubleMatrix(0,0,mxREAL);
-	mxSetM(pfield,M);
-	mxSetN(pfield,1);
-	mxSetPr(pfield,doublevec);
-	
-	mxSetField( dataref, this->step-1, name, pfield);
-	mxSetField( dataref, this->step-1, "time",mxCreateDoubleScalar((double)this->time)); 
-	mxSetField( dataref, this->step-1, "step",mxCreateDoubleScalar((double)this->step)); 
-
-}
-#endif
-/*}}}*/
 /*FUNCTION PetscVecExternalResult::GetStep{{{1*/
 int PetscVecExternalResult::GetStep(void){
Index: /issm/trunk-jpl/src/c/objects/ExternalResults/PetscVecExternalResult.h
===================================================================
--- /issm/trunk-jpl/src/c/objects/ExternalResults/PetscVecExternalResult.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/ExternalResults/PetscVecExternalResult.h	(revision 12011)
@@ -60,7 +60,4 @@
 		void  WriteData(FILE* fid,bool io_gather);
 		void  GetResultName(char**);
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		void  SetMatlabField(mxArray* dataref);
-		#endif
 		int   GetStep(void);
 		/*}}}*/
Index: /issm/trunk-jpl/src/c/objects/ExternalResults/StringExternalResult.cpp
===================================================================
--- /issm/trunk-jpl/src/c/objects/ExternalResults/StringExternalResult.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/ExternalResults/StringExternalResult.cpp	(revision 12011)
@@ -197,19 +197,4 @@
 }
 /*}}}*/
-/*FUNCTION StringExternalResult::SetMatlabField{{{1*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void  StringExternalResult::SetMatlabField(mxArray* dataref){
-	
-	char* name=NULL;
-
-	this->GetResultName(&name);
-
-	mxSetField( dataref, this->step-1, name, mxCreateString(value));
-	mxSetField( dataref, this->step-1, "time",mxCreateDoubleScalar((double)this->time)); 
-	mxSetField( dataref, this->step-1, "step",mxCreateDoubleScalar((double)this->step)); 
-
-}
-#endif
-/*}}}*/
 /*FUNCTION StringExternalResult::GetStep{{{1*/
 int StringExternalResult::GetStep(void){
Index: /issm/trunk-jpl/src/c/objects/ExternalResults/StringExternalResult.h
===================================================================
--- /issm/trunk-jpl/src/c/objects/ExternalResults/StringExternalResult.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/ExternalResults/StringExternalResult.h	(revision 12011)
@@ -60,7 +60,4 @@
 		void  WriteData(FILE* fid,bool io_gather);
 		void  GetResultName(char**);
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		void  SetMatlabField(mxArray* dataref);
-		#endif
 		int   GetStep(void);
 		/*}}}*/
Index: /issm/trunk-jpl/src/c/objects/Numerics/Matrix.cpp
===================================================================
--- /issm/trunk-jpl/src/c/objects/Numerics/Matrix.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Numerics/Matrix.cpp	(revision 12011)
@@ -140,38 +140,4 @@
 }
 /*}}}*/
-
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-/*FUNCTION Matrix::ToMatlabMatrix{{{1*/
-mxArray* Matrix::ToMatlabMatrix(void){
-
-	mxArray* dataref=NULL;
-	#ifdef _HAVE_PETSC_
-	PetscMatrixToMatlabMatrix(&dataref,this->matrix);
-	#else
-	dataref=this->matrix->ToMatlabMatrix();
-	#endif
-	return dataref;
-
-}
-/*}}}*/
-/*FUNCTION MatlabMatrixToMatrix{{{1*/
-Matrix* MatlabMatrixToMatrix(const mxArray* mxmatrix){
-
-	int dummy;
-	Matrix* matrix=NULL;
-
-	/*allocate matrix object: */
-	matrix=new Matrix();
-
-	#ifdef _HAVE_PETSC_
-	MatlabMatrixToPetscMatrix(&matrix->matrix,NULL,NULL,mxmatrix);
-	#else
-	matrix->matrix=MatlabMatrixToSeqMat(mxmatrix);
-	#endif
-	
-	return matrix;
-}
-/*}}}*/
-#endif
 /*FUNCTION Matrix::Assemble{{{1*/
 void Matrix::Assemble(void){
Index: /issm/trunk-jpl/src/c/objects/Numerics/Matrix.h
===================================================================
--- /issm/trunk-jpl/src/c/objects/Numerics/Matrix.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Numerics/Matrix.h	(revision 12011)
@@ -51,7 +51,4 @@
 		/*Matrix specific routines {{{1*/
 		void Echo(void);
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		mxArray* ToMatlabMatrix(void);
-		#endif
 		void Assemble(void);
 		double Norm(NormMode norm_type);
@@ -66,8 +63,4 @@
 
 };
-/*API: */
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-Matrix* MatlabMatrixToMatrix(const mxArray* mxmatrix);
-#endif
 
 #endif //#ifndef _MATRIX_H_
Index: /issm/trunk-jpl/src/c/objects/Numerics/Vector.cpp
===================================================================
--- /issm/trunk-jpl/src/c/objects/Numerics/Vector.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Numerics/Vector.cpp	(revision 12011)
@@ -132,38 +132,4 @@
 }
 /*}}}*/
-
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-/*FUNCTION Vector::ToMatlabVector{{{1*/
-mxArray* Vector::ToMatlabVector(void){
-
-	mxArray* dataref=NULL;
-	#ifdef _HAVE_PETSC_
-	PetscVectorToMatlabVector(&dataref,this->vector);
-	#else
-	dataref=this->vector->ToMatlabVector();
-	#endif
-	return dataref;
-
-}
-/*}}}*/
-/*FUNCTION MatlabVectorToVector{{{1*/
-Vector* MatlabVectorToVector(const mxArray* mxvector){
-
-	int dummy;
-	Vector* vector=NULL;
-
-	/*allocate vector object: */
-	vector=new Vector();
-
-	#ifdef _HAVE_PETSC_
-	MatlabVectorToPetscVector(&vector->vector,&dummy,mxvector);
-	#else
-	vector->vector=MatlabVectorToSeqVec(mxvector);
-	#endif
-	
-	return vector;
-}
-/*}}}*/
-#endif
 /*FUNCTION Vector::Assemble{{{1*/
 void Vector::Assemble(void){
Index: /issm/trunk-jpl/src/c/objects/Numerics/Vector.h
===================================================================
--- /issm/trunk-jpl/src/c/objects/Numerics/Vector.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Numerics/Vector.h	(revision 12011)
@@ -54,7 +54,4 @@
 		/*Vector specific routines {{{1*/
 		void Echo(void);
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		mxArray* ToMatlabVector(void);
-		#endif
 		void    AXPY(Vector *X, double a);
 		void    AYPX(Vector *X, double a);
@@ -77,8 +74,4 @@
 };
 
-/*API: */
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-Vector* MatlabVectorToVector(const mxArray* mxvector);
-#endif
 
 #endif //#ifndef _VECTOR_H_
Index: /issm/trunk-jpl/src/c/objects/OptArgs.h
===================================================================
--- /issm/trunk-jpl/src/c/objects/OptArgs.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/OptArgs.h	(revision 12011)
@@ -6,19 +6,8 @@
 #define _OPTARGS_H_
 
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-
-#include "mex.h"
-struct OptArgs{
-	char* function_name;
-	mxArray* femmodel;
-};
-
-#else
-
 class Model;
 struct OptArgs{
 	FemModel* femmodel;
 };
-#endif
 
 #endif
Index: /issm/trunk-jpl/src/c/objects/Params/BoolParam.cpp
===================================================================
--- /issm/trunk-jpl/src/c/objects/Params/BoolParam.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Params/BoolParam.cpp	(revision 12011)
@@ -134,13 +134,4 @@
 }
 /*}}}*/
-/*FUNCTION BoolParam::SetMatlabField{{{1*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void  BoolParam::SetMatlabField(mxArray* dataref){
-	char* name=NULL;
-	this->GetParameterName(&name);
-	mxSetField( dataref, 0, name,mxCreateDoubleScalar((double)value));
-}
-#endif
-/*}}}*/
 /*FUNCTION BoolParam::UnitConversion{{{1*/
 void  BoolParam::UnitConversion(int direction_enum){
Index: /issm/trunk-jpl/src/c/objects/Params/BoolParam.h
===================================================================
--- /issm/trunk-jpl/src/c/objects/Params/BoolParam.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Params/BoolParam.h	(revision 12011)
@@ -81,7 +81,4 @@
 		
 		void GetParameterName(char**pname);
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		void  SetMatlabField(mxArray* dataref);
-		#endif
 		/*}}}*/
 };
Index: /issm/trunk-jpl/src/c/objects/Params/DoubleMatArrayParam.cpp
===================================================================
--- /issm/trunk-jpl/src/c/objects/Params/DoubleMatArrayParam.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Params/DoubleMatArrayParam.cpp	(revision 12011)
@@ -308,44 +308,4 @@
 }
 /*}}}*/
-/*FUNCTION StringArrayParam::SetMatlabField{{{1*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void  DoubleMatArrayParam::SetMatlabField(mxArray* dataref){
-	
-	int      i,m,n;
-	double*  matrix=NULL;
-	double*  outmatrix=NULL;
-	char*    name=NULL;
-	mwSize   dims[2]={0};
-	mxArray* pfield=NULL;
-	mxArray* pfield2=NULL;
-	mxArray* pfield3=NULL;
-	
-	this->GetParameterName(&name);
-	dims[0]=this->M;
-	dims[1]=1;
-	pfield=mxCreateCellArray(2,dims);
-
-	for(i=0;i<this->M;i++){
-		matrix=this->array[i];
-		m=this->mdim_array[i];
-		n=this->ndim_array[i];
-		outmatrix=(double*)xmalloc(m*n*sizeof(double));
-		memcpy(outmatrix,matrix,m*n*sizeof(double));
-	
-		pfield2=mxCreateDoubleMatrix(0,0,mxREAL);
-		mxSetM(pfield2,n);
-		mxSetN(pfield2,m);
-		mxSetPr(pfield2,outmatrix);
-
-		//transpose the outmatrix, written directly to matlab! from C to matlab.
-		mexCallMATLAB(1,&pfield3, 1, &pfield2, "transpose");
-	
-		mxSetCell(pfield,i,pfield3);
-	}
-	
-	mxSetField( dataref, 0, name,pfield);
-}
-#endif
-/*}}}*/
 /*FUNCTION DoubleMatArrayParam::SetValue(double** array, int M, int* mdim_array, int* ndim_array){{{1*/
 void  DoubleMatArrayParam::SetValue(double** in_array, int in_M, int* in_mdim_array, int* in_ndim_array){
Index: /issm/trunk-jpl/src/c/objects/Params/DoubleMatArrayParam.h
===================================================================
--- /issm/trunk-jpl/src/c/objects/Params/DoubleMatArrayParam.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Params/DoubleMatArrayParam.h	(revision 12011)
@@ -84,7 +84,4 @@
 
 		void GetParameterName(char**pname);
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		void  SetMatlabField(mxArray* dataref);
-		#endif
 
 		/*}}}*/
Index: /issm/trunk-jpl/src/c/objects/Params/DoubleMatParam.cpp
===================================================================
--- /issm/trunk-jpl/src/c/objects/Params/DoubleMatParam.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Params/DoubleMatParam.cpp	(revision 12011)
@@ -190,27 +190,4 @@
 }
 /*}}}*/
-/*FUNCTION DoubleMatParam::SetMatlabField{{{1*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void  DoubleMatParam::SetMatlabField(mxArray* dataref){
-
-	mxArray* pfield=NULL;
-	mxArray* pfield2=NULL;
-	double* doublemat=NULL;
-	char* name=NULL;
-	
-	this->GetParameterName(&name);
-	this->GetParameterValue(&doublemat,NULL,NULL);
-				
-	pfield=mxCreateDoubleMatrix(0,0,mxREAL);
-	mxSetM(pfield,N);
-	mxSetN(pfield,M);
-	mxSetPr(pfield,doublemat);
-	
-	//transpose the matrix, written directly to matlab! from C to matlab.
-	mexCallMATLAB(1,&pfield2, 1, &pfield, "transpose");
-	mxSetField( dataref, 0, name,pfield2);
-}
-#endif
-/*}}}*/
 /*FUNCTION DoubleMatParam::SetValue{{{1*/
 void  DoubleMatParam::SetValue(double* doublearray,int in_M,int in_N){
Index: /issm/trunk-jpl/src/c/objects/Params/DoubleMatParam.h
===================================================================
--- /issm/trunk-jpl/src/c/objects/Params/DoubleMatParam.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Params/DoubleMatParam.h	(revision 12011)
@@ -84,7 +84,4 @@
 
 		void GetParameterName(char**pname);
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		void  SetMatlabField(mxArray* dataref);
-		#endif
 
 		/*}}}*/
Index: /issm/trunk-jpl/src/c/objects/Params/DoubleParam.cpp
===================================================================
--- /issm/trunk-jpl/src/c/objects/Params/DoubleParam.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Params/DoubleParam.cpp	(revision 12011)
@@ -219,14 +219,4 @@
 }
 /*}}}*/
-/*FUNCTION DoubleParam::SetMatlabField{{{1*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void  DoubleParam::SetMatlabField(mxArray* dataref){
-
-	char* name=NULL;
-	this->GetParameterName(&name);
-	mxSetField( dataref, 0, name,mxCreateDoubleScalar(value));
-}
-#endif
-/*}}}*/
 /*FUNCTION DoubleParam::UnitConversion{{{1*/
 void  DoubleParam::UnitConversion(int direction_enum){
Index: /issm/trunk-jpl/src/c/objects/Params/DoubleParam.h
===================================================================
--- /issm/trunk-jpl/src/c/objects/Params/DoubleParam.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Params/DoubleParam.h	(revision 12011)
@@ -82,7 +82,4 @@
 
 		void GetParameterName(char**pname);
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		void  SetMatlabField(mxArray* dataref);
-		#endif
 
 		/*}}}*/
Index: /issm/trunk-jpl/src/c/objects/Params/DoubleVecParam.cpp
===================================================================
--- /issm/trunk-jpl/src/c/objects/Params/DoubleVecParam.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Params/DoubleVecParam.cpp	(revision 12011)
@@ -202,24 +202,4 @@
 }
 /*}}}*/
-/*FUNCTION DoubleVecParam::SetMatlabField{{{1*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void  DoubleVecParam::SetMatlabField(mxArray* dataref){
-
-	char* name=NULL;
-	double* doublevec=NULL;
-	mxArray* pfield=NULL;
-
-	this->GetParameterValue(&doublevec,NULL);
-	this->GetParameterName(&name);
-				
-	pfield=mxCreateDoubleMatrix(0,0,mxREAL);
-	mxSetM(pfield,M);
-	mxSetN(pfield,1);
-	mxSetPr(pfield,doublevec);
-	
-	mxSetField( dataref, 0, name, pfield);
-}
-#endif
-/*}}}*/
 /*FUNCTION DoubleVecParam::SetValue{{{1*/
 void  DoubleVecParam::SetValue(double* doublearray,int in_M){
Index: /issm/trunk-jpl/src/c/objects/Params/DoubleVecParam.h
===================================================================
--- /issm/trunk-jpl/src/c/objects/Params/DoubleVecParam.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Params/DoubleVecParam.h	(revision 12011)
@@ -82,7 +82,4 @@
 		
 		void GetParameterName(char**pname);
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		void  SetMatlabField(mxArray* dataref);
-		#endif
 		/*}}}*/
 };
Index: /issm/trunk-jpl/src/c/objects/Params/FileParam.cpp
===================================================================
--- /issm/trunk-jpl/src/c/objects/Params/FileParam.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Params/FileParam.cpp	(revision 12011)
@@ -101,12 +101,4 @@
 }
 /*}}}*/
-/*FUNCTION FileParam::SetMatlabField{{{1*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void  FileParam::SetMatlabField(mxArray* dataref){
-	
-	_error_("FileParam is a pointer and cannot be converted into a matlab object");
-}
-#endif
-/*}}}*/
 /*FUNCTION FileParam::UnitConversion{{{1*/
 void  FileParam::UnitConversion(int direction_enum){
Index: /issm/trunk-jpl/src/c/objects/Params/FileParam.h
===================================================================
--- /issm/trunk-jpl/src/c/objects/Params/FileParam.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Params/FileParam.h	(revision 12011)
@@ -81,7 +81,4 @@
 
 		void GetParameterName(char**pname);
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		void  SetMatlabField(mxArray* dataref);
-		#endif
 
 		/*}}}*/
Index: /issm/trunk-jpl/src/c/objects/Params/IntMatParam.cpp
===================================================================
--- /issm/trunk-jpl/src/c/objects/Params/IntMatParam.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Params/IntMatParam.cpp	(revision 12011)
@@ -172,33 +172,4 @@
 }
 /*}}}*/
-/*FUNCTION IntMatParam::SetMatlabField{{{1*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void  IntMatParam::SetMatlabField(mxArray* dataref){
-
-	char    *name        = NULL;
-	double  *doublearray = NULL;
-	int     *intarray    = NULL;
-	mxArray *pfield      = NULL;
-	mxArray *pfield2     = NULL;
-
-	this->GetParameterValue(&intarray,NULL,NULL);
-	this->GetParameterName(&name);
-
-	/*cast intarray into doublearray for Matlab*/
-	doublearray=(double*)xmalloc(M*N*sizeof(double));
-	for(int i=0;i<M*N;i++)doublearray[i]=(double)intarray[i];
-	xfree((void**)&intarray);
-
-	pfield=mxCreateDoubleMatrix(0,0,mxREAL);
-	mxSetM(pfield,M);
-	mxSetN(pfield,N);
-	mxSetPr(pfield,doublearray);
-
-	//transpose the matrix, written directly to matlab! from C to matlab.
-	mexCallMATLAB(1,&pfield2, 1, &pfield, "transpose");
-	mxSetField( dataref, 0, name,pfield2);
-}
-#endif
-/*}}}*/
 /*FUNCTION IntMatParam::SetValue{{{1*/
 void  IntMatParam::SetValue(int* intarray,int in_M,int in_N){
Index: /issm/trunk-jpl/src/c/objects/Params/IntMatParam.h
===================================================================
--- /issm/trunk-jpl/src/c/objects/Params/IntMatParam.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Params/IntMatParam.h	(revision 12011)
@@ -83,7 +83,4 @@
 
 		void GetParameterName(char**pname);
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		void  SetMatlabField(mxArray* dataref);
-		#endif
 
 		/*}}}*/
Index: /issm/trunk-jpl/src/c/objects/Params/IntParam.cpp
===================================================================
--- /issm/trunk-jpl/src/c/objects/Params/IntParam.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Params/IntParam.cpp	(revision 12011)
@@ -134,14 +134,4 @@
 }
 /*}}}*/
-/*FUNCTION IntParam::SetMatlabField{{{1*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void  IntParam::SetMatlabField(mxArray* dataref){
-	
-	char* name=NULL;
-	this->GetParameterName(&name);
-	mxSetField( dataref, 0, name,mxCreateDoubleScalar((double)value));
-}
-#endif
-/*}}}*/
 /*FUNCTION IntParam::UnitConversion{{{1*/
 void  IntParam::UnitConversion(int direction_enum){
Index: /issm/trunk-jpl/src/c/objects/Params/IntParam.h
===================================================================
--- /issm/trunk-jpl/src/c/objects/Params/IntParam.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Params/IntParam.h	(revision 12011)
@@ -82,7 +82,4 @@
 
 		void GetParameterName(char**pname);
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		void  SetMatlabField(mxArray* dataref);
-		#endif
 
 		/*}}}*/
Index: /issm/trunk-jpl/src/c/objects/Params/IntVecParam.cpp
===================================================================
--- /issm/trunk-jpl/src/c/objects/Params/IntVecParam.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Params/IntVecParam.cpp	(revision 12011)
@@ -185,33 +185,4 @@
 }
 /*}}}*/
-/*FUNCTION IntVecParam::SetMatlabField{{{1*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void  IntVecParam::SetMatlabField(mxArray* dataref){
-
-	char    *name      = NULL;
-	double  *doublevec = NULL;
-	int     *intvec    = NULL;
-	mxArray *pfield    = NULL;
-
-	this->GetParameterValue(&intvec,NULL);
-	this->GetParameterName(&name);
-
-	/*cast intvec into doublevec for Matlab*/
-	if(M){
-		doublevec=(double*)xmalloc(M*sizeof(double));
-		for(int i=0;i<M;i++)doublevec[i]=(double)intvec[i];
-	}
-	else doublevec=NULL;
-	xfree((void**)&intvec);
-				
-	pfield=mxCreateDoubleMatrix(0,0,mxREAL);
-	mxSetM(pfield,M);
-	mxSetN(pfield,1);
-	mxSetPr(pfield,doublevec);
-	
-	mxSetField(dataref, 0, name, pfield);
-}
-#endif
-/*}}}*/
 /*FUNCTION IntVecParam::SetValue{{{1*/
 void  IntVecParam::SetValue(int* intarray,int in_M){
Index: /issm/trunk-jpl/src/c/objects/Params/IntVecParam.h
===================================================================
--- /issm/trunk-jpl/src/c/objects/Params/IntVecParam.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Params/IntVecParam.h	(revision 12011)
@@ -83,7 +83,4 @@
 		
 		void GetParameterName(char**pname);
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		void  SetMatlabField(mxArray* dataref);
-		#endif
 		/*}}}*/
 };
Index: /issm/trunk-jpl/src/c/objects/Params/MatrixParam.cpp
===================================================================
--- /issm/trunk-jpl/src/c/objects/Params/MatrixParam.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Params/MatrixParam.cpp	(revision 12011)
@@ -190,25 +190,4 @@
 }
 /*}}}*/
-/*FUNCTION MatrixParam::SetMatlabField{{{1*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void  MatrixParam::SetMatlabField(mxArray* dataref){
-	
-	char* name=NULL;
-	int   M,N;
-	double* doublemat=NULL;
-	mxArray* pfield=NULL;
-
-	doublemat=value->ToSerial();
-	value->GetSize(&M,&N);
-	this->GetParameterName(&name);
-				
-	pfield=mxCreateDoubleMatrix(0,0,mxREAL);
-	mxSetM(pfield,M);
-	mxSetN(pfield,N);
-	mxSetPr(pfield,doublemat);
-	mxSetField( dataref, 0, name, pfield);
-}
-#endif
-/*}}}*/
 /*FUNCTION MatrixParam::SetValue{{{1*/
 void  MatrixParam::SetValue(Matrix* matrix){
Index: /issm/trunk-jpl/src/c/objects/Params/MatrixParam.h
===================================================================
--- /issm/trunk-jpl/src/c/objects/Params/MatrixParam.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Params/MatrixParam.h	(revision 12011)
@@ -82,7 +82,4 @@
 
 		void GetParameterName(char**pname);
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		void  SetMatlabField(mxArray* dataref);
-		#endif
 
 		/*}}}*/
Index: /issm/trunk-jpl/src/c/objects/Params/Param.h
===================================================================
--- /issm/trunk-jpl/src/c/objects/Params/Param.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Params/Param.h	(revision 12011)
@@ -60,7 +60,4 @@
 		virtual void  UnitConversion(int direction_enum)=0;
 		virtual void  GetParameterName(char**pname)=0;
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		virtual void  SetMatlabField(mxArray* dataref)=0;
-		#endif
 };
 #endif
Index: /issm/trunk-jpl/src/c/objects/Params/StringArrayParam.cpp
===================================================================
--- /issm/trunk-jpl/src/c/objects/Params/StringArrayParam.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Params/StringArrayParam.cpp	(revision 12011)
@@ -236,26 +236,4 @@
 }
 /*}}}*/
-/*FUNCTION StringArrayParam::SetMatlabField{{{1*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void  StringArrayParam::SetMatlabField(mxArray* dataref){
-	
-	int      i;
-	char*    name=NULL;
-	mwSize   dims[2]={0};
-	mxArray* pfield=NULL;
-	
-	this->GetParameterName(&name);
-
-	dims[0]=this->numstrings;
-	dims[1]=1;
-	pfield=mxCreateCellArray(2,dims);
-	for(i=0;i<this->numstrings;i++){
-		char* string=value[i];
-		mxSetCell(pfield,i,mxCreateString(string));
-	}
-	mxSetField( dataref, 0, name,pfield);
-}
-#endif
-/*}}}*/
 /*FUNCTION StringArrayParam::SetValue{{{1*/
 void  StringArrayParam::SetValue(char** stringarray,int M){
Index: /issm/trunk-jpl/src/c/objects/Params/StringArrayParam.h
===================================================================
--- /issm/trunk-jpl/src/c/objects/Params/StringArrayParam.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Params/StringArrayParam.h	(revision 12011)
@@ -84,7 +84,4 @@
 
 		void GetParameterName(char**pname);
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		void  SetMatlabField(mxArray* dataref);
-		#endif
 		/*}}}*/
 };
Index: /issm/trunk-jpl/src/c/objects/Params/StringParam.cpp
===================================================================
--- /issm/trunk-jpl/src/c/objects/Params/StringParam.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Params/StringParam.cpp	(revision 12011)
@@ -164,15 +164,4 @@
 }
 /*}}}*/
-/*FUNCTION StringParam::SetMatlabField{{{1*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void  StringParam::SetMatlabField(mxArray* dataref){
-	
-	char* name=NULL;
-
-	this->GetParameterName(&name);
-	mxSetField( dataref, 0, name, mxCreateString(value));
-}
-#endif
-/*}}}*/
 /*FUNCTION StringParam::SetValue{{{1*/
 void  StringParam::SetValue(char* string){
Index: /issm/trunk-jpl/src/c/objects/Params/StringParam.h
===================================================================
--- /issm/trunk-jpl/src/c/objects/Params/StringParam.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Params/StringParam.h	(revision 12011)
@@ -82,7 +82,4 @@
 
 		void GetParameterName(char**pname);
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		void  SetMatlabField(mxArray* dataref);
-		#endif
 
 		/*}}}*/
Index: /issm/trunk-jpl/src/c/objects/Params/VectorParam.cpp
===================================================================
--- /issm/trunk-jpl/src/c/objects/Params/VectorParam.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Params/VectorParam.cpp	(revision 12011)
@@ -188,26 +188,4 @@
 }
 /*}}}*/
-/*FUNCTION VectorParam::SetMatlabField{{{1*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-void  VectorParam::SetMatlabField(mxArray* dataref){
-
-	mxArray* pfield=NULL;
-	char* name=NULL;
-	double* doublevec=NULL;
-	int M;
-	
-	doublevec=value->ToMPISerial();
-	value->GetSize(&M);
-	this->GetParameterName(&name);
-	
-	pfield=mxCreateDoubleMatrix(0,0,mxREAL);
-	mxSetM(pfield,M);
-	mxSetN(pfield,1);
-	mxSetPr(pfield,doublevec);
-	
-	mxSetField( dataref, 0, name, pfield);
-}
-#endif
-/*}}}*/
 /*FUNCTION VectorParam::SetValue{{{1*/
 void  VectorParam::SetValue(Vector* vector){
Index: /issm/trunk-jpl/src/c/objects/Params/VectorParam.h
===================================================================
--- /issm/trunk-jpl/src/c/objects/Params/VectorParam.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/objects/Params/VectorParam.h	(revision 12011)
@@ -82,7 +82,4 @@
 
 		void GetParameterName(char**pname);
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		void  SetMatlabField(mxArray* dataref);
-		#endif
 
 		/*}}}*/
Index: /issm/trunk-jpl/src/c/python/io/CheckNumPythonArguments.cpp
===================================================================
--- /issm/trunk-jpl/src/c/python/io/CheckNumPythonArguments.cpp	(revision 12011)
+++ /issm/trunk-jpl/src/c/python/io/CheckNumPythonArguments.cpp	(revision 12011)
@@ -0,0 +1,40 @@
+/*!\file CheckNumPythonArguments.cpp:
+ * \brief: check number of arguments and report an usage error message.
+ */
+
+#ifdef HAVE_CONFIG_H
+	#include <config.h>
+#else
+#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
+#endif
+
+
+#if defined(_HAVE_PYTHON_) && defined(_SERIAL_)
+
+#define PY_ARRAY_UNIQUE_SYMBOL PythonIOSymbol
+#define NO_IMPORT
+
+#include "../../toolkits/toolkits.h"
+#include "../../shared/Exceptions/exceptions.h"
+#include "../../include/include.h"
+
+int CheckNumPythonArguments(PyObject* inputs,int NRHS, void (*function)( void )){
+
+	Py_ssize_t size=0;
+
+	/*figure out size of tuple in input: */
+	size=PyTuple_Size(inputs);
+	
+	/*check on requested size: */
+	if (size==0){
+		function();
+		_error_("usage: see above");
+	}
+	else if (size!=NRHS ) {
+		function(); 
+		_error_("usage error.");
+	}
+	return 1;
+}
+
+#endif
Index: /issm/trunk-jpl/src/c/python/io/FetchPythonData.cpp
===================================================================
--- /issm/trunk-jpl/src/c/python/io/FetchPythonData.cpp	(revision 12011)
+++ /issm/trunk-jpl/src/c/python/io/FetchPythonData.cpp	(revision 12011)
@@ -0,0 +1,74 @@
+/*\file FetchData.cpp:
+ * \brief: general I/O interface to fetch data in python
+ */
+
+#ifdef HAVE_CONFIG_H
+	#include <config.h>
+#else
+#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
+#endif
+
+#define PY_ARRAY_UNIQUE_SYMBOL PythonIOSymbol
+#define NO_IMPORT
+
+#include "../../toolkits/toolkits.h"
+#include "../../include/include.h"
+#include "../../shared/shared.h"
+
+#if defined(_HAVE_PYTHON_) && defined(_SERIAL_)
+/*FUNCTION FetchData(char** pstring,PyObject* py_unicode){{{1*/
+void FetchData(char** pstring,PyObject* py_unicode){
+
+	PyObject* py_bytes;
+	char* string=NULL;
+
+	
+	/*convert to bytes format: */
+	PyUnicode_FSConverter(py_unicode,&py_bytes);
+
+	/*convert from bytes to string: */
+	string=PyBytes_AS_STRING(py_bytes);
+	
+	*pstring=string;
+}
+/*FUNCTION FetchData(double* pscalar,PyObject* py_float){{{1*/
+void FetchData(double* pscalar,PyObject* py_float){
+
+	double scalar;
+
+	/*return internal value: */
+	scalar=PyFloat_AsDouble(py_float);
+
+	/*output: */
+	*pscalar=scalar;
+}
+/*}}}*/
+/*FUNCTION FetchData(int* pinteger,PyObject* py_long){{{1*/
+void FetchData(int* pinteger, PyObject* py_long){
+
+	int integer;
+
+	/*return internal value: */
+	integer=(int)PyLong_AsLong(py_long);
+
+	/*output: */
+	*pinteger=integer;
+}
+/*}}}*/
+/*FUNCTION FetchData(bool* pboolean,PyObject* py_boolean){{{1*/
+void FetchData(bool* pboolean,PyObject* py_boolean){
+
+	bool boolean;
+	
+	/*check this is indeed a subtype of long type: */
+	if(!PyBool_Check(py_boolean))_error_("expecting a boolean in input!");
+
+	/*extract boolean: */
+	boolean=(bool)PyLong_AsLong(py_boolean);
+
+	/*simple copy: */
+	*pboolean=boolean;
+	
+}
+/*}}}*/
+#endif
Index: /issm/trunk-jpl/src/c/python/io/WritePythonData.cpp
===================================================================
--- /issm/trunk-jpl/src/c/python/io/WritePythonData.cpp	(revision 12011)
+++ /issm/trunk-jpl/src/c/python/io/WritePythonData.cpp	(revision 12011)
@@ -0,0 +1,69 @@
+/* \file WriteData.c:
+ * \brief: general interface for writing data
+ */
+
+#ifdef HAVE_CONFIG_H
+	#include <config.h>
+#else
+#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
+#endif
+
+#if defined(_HAVE_PYTHON_) && defined(_SERIAL_)
+
+#define PY_ARRAY_UNIQUE_SYMBOL PythonIOSymbol
+#define NO_IMPORT
+
+#include "../../toolkits/toolkits.h"
+#include "../../include/include.h"
+#include "../../modules/modules.h"
+#include "../../Container/Container.h"
+#include "../../shared/shared.h"
+#include "../../io/io.h"
+#include "../../EnumDefinitions/EnumDefinitions.h"
+
+
+/*FUNCTION WriteData(PyObject* py_tuple,int index,char* string){{{1*/
+void WriteData(PyObject* py_tuple, int index, char* string){
+	
+	PyTuple_SetItem(py_tuple, index, PyUnicode_FromString(string));
+
+}
+/*}}}*/
+/*FUNCTION WriteData(PyObject* tuple,int index,Matrix* matrix){{{1*/
+void WriteData(PyObject* tuple,int index,Matrix* matrix){
+	
+	int M,N;
+	double* buffer=NULL;
+	npy_intp dims[2]={0,0};
+	PyObject* array=NULL;
+	
+	buffer=matrix->ToSerial();
+	matrix->GetSize(&M,&N);
+	dims[0]=(npy_intp)M;
+	dims[1]=(npy_intp)N;
+	array=PyArray_SimpleNewFromData(2,dims,NPY_DOUBLE,buffer);
+	
+	PyTuple_SetItem(tuple, index, array);
+
+
+}
+/*FUNCTION WriteData(PyObject* py_tuple,int index,Vector* vector){{{1*/
+void WriteData(PyObject* tuple,int index,Vector* vector){
+	
+	int M;
+	double* buffer=NULL;
+	npy_intp dim=10;
+	PyObject* array=NULL;
+	
+	buffer=vector->ToMPISerial();
+	vector->GetSize(&M);
+	dim=(npy_intp)M;
+	array=PyArray_SimpleNewFromData(1,&dim,NPY_DOUBLE,buffer);
+	
+	PyTuple_SetItem(tuple, index, array);
+
+
+}
+/*}}}*/
+
+#endif
Index: /issm/trunk-jpl/src/c/python/io/pythonio.h
===================================================================
--- /issm/trunk-jpl/src/c/python/io/pythonio.h	(revision 12011)
+++ /issm/trunk-jpl/src/c/python/io/pythonio.h	(revision 12011)
@@ -0,0 +1,56 @@
+/*\file pythonio.h
+ *\brief: I/O for ISSM in python mode
+ */
+
+#ifndef _PYTHON_IO_H_
+#define _PYTHON_IO_H_
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#else
+#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
+#endif 
+
+
+#include "../../objects/objects.h"
+#include "../../Container/Container.h"
+#include "../../include/include.h"
+
+class DataSet;
+class Parameters;
+
+//void WriteData(PyObject* py_tuple,DataSet* dataset);
+//void WriteData(PyObject* py_tuple,double* matrix, int M,int N);
+//void WriteData(PyObject* py_tuple,int*    matrix, int M,int N);
+//void WriteData(PyObject* py_tuple,double* vector, int M);
+//void WriteData(PyObject* py_tuple,int integer);
+//void WriteData(PyObject* py_tuple,bool boolean);
+//void WriteData(PyObject* py_tuple,double scalar);
+//void WriteData(DataHandle* py_tuple,Parameters* parameters);
+void WriteData(PyObject* py_tuple, int index, char* string);
+void WriteData(PyObject* py_tuple, int index, Matrix* matrix);
+void WriteData(PyObject* py_tuple, int index, Vector* vector);
+
+
+//void FetchData(DataSet** pdataset,PyObject* py_ref);
+//void FetchData(double** pmatrix,int* pM,int *pN,PyObject* py_ref);
+//void FetchData(double** pmatrix,int* pnumel,int* pndims,int** psize,PyObject* py_ref);
+//void FetchData(int** pmatrix,int* pM,int *pN,PyObject* py_ref);
+//void FetchData(bool** pmatrix,int* pM,int *pN,PyObject* py_ref);
+//void FetchData(bool** pmatrix,int* pnumel,int* pndims,int** psize,PyObject* py_ref);
+//void FetchData(Matrix** pmatrix,PyObject* py_ref);
+//void FetchData(int** pvector,int* pM,PyObject* py_ref);
+//void FetchData(float** pvector,int* pM,PyObject* py_ref);
+//void FetchData(double** pvector,int* pM,PyObject* py_ref);
+//void FetchData(bool** pvector,int* pM,PyObject* py_ref);
+//void FetchData(Vector** pvector,PyObject* py_ref);
+//void FetchData(char** pmatrix,int* pnumel,int* pndims,int** psize,PyObject* py_ref);
+//void FetchData(Parameters** pparameters, DataHandle py_ref);
+void FetchData(char** pstring,PyObject* py_unicode);
+void FetchData(double* pscalar,PyObject* py_float);
+void FetchData(int* pinteger,PyObject* py_long);
+void FetchData(bool* pbool,PyObject* py_boolean);
+
+int CheckNumPythonArguments(PyObject* inputs,int NRHS, void (*function)( void ));
+
+#endif	/* _IO_H_ */
Index: /issm/trunk-jpl/src/c/python/objects/Bamg/BamgGeom.cpp
===================================================================
--- /issm/trunk-jpl/src/c/python/objects/Bamg/BamgGeom.cpp	(revision 12011)
+++ /issm/trunk-jpl/src/c/python/objects/Bamg/BamgGeom.cpp	(revision 12011)
@@ -0,0 +1,27 @@
+#include <stdio.h>
+#include "../objects.h"
+#include "../../io/io.h"
+#include "../../shared/shared.h"
+
+/*Constructors/Destructors*/
+/*FUNCTION BamgGeom::BamgGeom(void* module_struct){{{1*/
+BamgGeom::BamgGeom(void* module_struct){
+
+	_error_("not implemented yet!");
+
+}
+/*}}}*/
+
+/*Methods*/
+/*FUNCTION BamgGeom::SetStructureFields{{{1*/
+void BamgGeom::SetStructureFields(void* module_struct){
+	_error_("not implemented yet!");
+}
+
+/*}}}*/
+/*FUNCTION BamgGeom::SetStructureField{{{1*/
+void BamgGeom::SetStructureField(void* module_struct,const char* fieldname,int fieldrows,int fieldcols,double* fieldpointer){
+	_error_("not implemented yet!");
+}
+
+/*}}}*/
Index: /issm/trunk-jpl/src/c/python/objects/Bamg/BamgMesh.cpp
===================================================================
--- /issm/trunk-jpl/src/c/python/objects/Bamg/BamgMesh.cpp	(revision 12011)
+++ /issm/trunk-jpl/src/c/python/objects/Bamg/BamgMesh.cpp	(revision 12011)
@@ -0,0 +1,23 @@
+#include <stdio.h>
+#include "../objects.h"
+#include "../../io/io.h"
+#include "../../shared/shared.h"
+
+/*Constructors/Destructors*/
+/*FUNCTION BamgMesh::BamgMesh(void* module_struct){{{1*/
+BamgMesh::BamgMesh(void* module_struct){
+	_error_("not implemented yet!");
+}
+/*}}}*/
+
+/*Methods*/
+/*FUNCTION BamgMesh::SetStructureFields{{{1*/
+void BamgMesh::SetStructureFields(void* module_struct){
+	_error_("not implemented yet!");
+}
+/*}}}*/
+/*FUNCTION BamgMesh::SetStructureField{{{1*/
+void BamgMesh::SetStructureField(void* module_struct,const char* fieldname,int fieldrows,int fieldcols,double* fieldpointer){
+	_error_("not implemented yet!");
+}
+/*}}}*/
Index: /issm/trunk-jpl/src/c/python/objects/Bamg/BamgOpts.cpp
===================================================================
--- /issm/trunk-jpl/src/c/python/objects/Bamg/BamgOpts.cpp	(revision 12011)
+++ /issm/trunk-jpl/src/c/python/objects/Bamg/BamgOpts.cpp	(revision 12011)
@@ -0,0 +1,12 @@
+#include <stdio.h>
+#include "../../shared/shared.h"
+#include "../../include/include.h"
+#include "../objects.h"
+#include "../../io/io.h"
+
+/*Constructors/Destructors*/
+/*FUNCTION BamgOpts::BamgOpts(void* module_struct){{{1*/
+BamgOpts::BamgOpts(void* module_struct){
+	_error_("not implemented yet!");
+}
+/*}}}*/
Index: /issm/trunk-jpl/src/c/shared/Alloc/alloc.cpp
===================================================================
--- /issm/trunk-jpl/src/c/shared/Alloc/alloc.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/shared/Alloc/alloc.cpp	(revision 12011)
@@ -19,8 +19,4 @@
 #endif
 
-#if defined(_SERIAL_) && defined(_HAVE_MATLAB_)
-#include "mex.h"
-#endif
-
 #include <stdio.h>
 #include <stdlib.h>
@@ -37,11 +33,6 @@
 	if(!size)_error_(" attempting to 0 size allocation!");
 
-	#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-	/* Use the matlab api to do the allocation: */
-	memptr=mxMalloc(size);
-	#else
 	/* Use the c library to do the allocation: */
 	memptr=malloc(size);
-	#endif
 	if(!memptr) _error_("memory allocation failed!");
 
@@ -55,11 +46,6 @@
 	if(!size)_error_("attempting to 0 size allocation!");
 
-	#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-	/* Use the matlab api to do the allocation: */
-	memptr=mxCalloc(n,size);
-	#else
 	/* Use the c library to do the allocation: */
 	memptr=calloc(n,size);
-	#endif
 	if(!memptr) _error_("memory allocation failed!");
 
@@ -70,9 +56,5 @@
 
 	if (pv && *pv){
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-			mxFree(*pv);
-		#else
-			free(*pv);
-		#endif
+		free(*pv);
 
 		*pv=NULL;
@@ -83,16 +65,8 @@
 	
 	if (pv && *pv){
-		#ifdef _PARALLEL_ 
 		/*There is no mxDelete in the Matlab API -> using delete trips up Matlab. So we 
 		 * don't actually free memory in Matlab, we let the memory manager do that. We only
 		 * free in parallel: */
 		delete *pv;
-		#else
-		/*Actually, still get rid of internal Petsc matrix. Quick fix until Matlab handles C++ 
-		 * correctly: */
-		#ifdef _HAVE_PETSC_
-			MatFree(&(*pv)->matrix);
-		#endif
-		#endif
 		*pv=NULL;
 	}
@@ -102,16 +76,8 @@
 
 	if (pv && *pv){
-		#ifdef _PARALLEL_ 
 		/*There is no mxDelete in the Matlab API -> using delete trips up Matlab. So we 
 		 * don't actually free memory in Matlab, we let the memory manager do that. We only
 		 * free in parallel: */
 		delete *pv;
-		#else
-		/*Actually, still get rid of internal Petsc vector. Quick fix until Matlab handles C++ 
-		 * correctly: */
-		#ifdef _HAVE_PETSC_
-			VecFree(&(*pv)->vector);
-		#endif
-		#endif
 		*pv=NULL;
 	}
@@ -124,10 +90,5 @@
 	
 	if(!size)_error_("attempting to realloc to zero");
-
-	#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-	value = (void*)mxRealloc(pv,size);
-	#else
 	value = (void*)realloc(pv,size);
-	#endif
 
 	if (value == NULL) {
Index: /issm/trunk-jpl/src/c/shared/Alloc/alloc_module.cpp
===================================================================
--- /issm/trunk-jpl/src/c/shared/Alloc/alloc_module.cpp	(revision 12011)
+++ /issm/trunk-jpl/src/c/shared/Alloc/alloc_module.cpp	(revision 12011)
@@ -0,0 +1,41 @@
+/* \file alloc_module.h
+ */
+
+
+#ifdef HAVE_CONFIG_H
+	#include <config.h>
+#else
+#error "Cannot compile without HAVE_CONFIG_H symbol! run configure first!"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include  "./allocmodule.h"
+#include "../Exceptions/exceptions.h"
+#include "../../include/include.h"
+#include "../../objects/objects.h"
+
+void xdelete_module(Matrix** pv){
+	
+	if (pv && *pv){
+		/*Actually, still get rid of internal Petsc matrix. Quick fix until Matlab handles C++ 
+		 * correctly: */
+		#ifdef _HAVE_PETSC_
+			MatFree(&(*pv)->matrix);
+		#endif
+		*pv=NULL;
+	}
+}
+
+void xdelete_module(Vector** pv){
+
+	if (pv && *pv){
+		/*Actually, still get rid of internal Petsc vector. Quick fix until Matlab handles C++ 
+		 * correctly: */
+		#ifdef _HAVE_PETSC_
+			VecFree(&(*pv)->vector);
+		#endif
+		*pv=NULL;
+	}
+}
Index: /issm/trunk-jpl/src/c/shared/Alloc/alloc_module.h
===================================================================
--- /issm/trunk-jpl/src/c/shared/Alloc/alloc_module.h	(revision 12011)
+++ /issm/trunk-jpl/src/c/shared/Alloc/alloc_module.h	(revision 12011)
@@ -0,0 +1,11 @@
+/* \file alloc.h
+ */
+
+#ifndef _ALLOC_MODULE_H_
+#define _ALLOC_MODULE_H_
+class Matrix;
+class Vector;
+void xdelete_module(Matrix** pvptr);
+void xdelete_module(Vector** pvptr);
+
+#endif
Index: sm/trunk-jpl/src/c/shared/Numerics/OptFunc.cpp
===================================================================
--- /issm/trunk-jpl/src/c/shared/Numerics/OptFunc.cpp	(revision 12010)
+++ 	(revision )
@@ -1,41 +1,0 @@
-/*!\file:  OptFunc.cpp
- * \brief: "C" code wrapper to matlab objectivefunctionC call
- */ 
-
-#ifdef HAVE_CONFIG_H
-	#include <config.h>
-#else
-#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
-#endif
-
-#include "../../objects/objects.h"
-#include "../../shared/shared.h"
-#include "../../include/include.h"
-
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-#include "mex.h"
-double OptFunc(double scalar, OptArgs* optargs){
-	
-	/*output: */
-	double J;
-
-	mxArray*   inputs[2];
-	mxArray*   psearch_scalar=NULL;
-	mxArray*   mxJ=NULL;
-
-	psearch_scalar=mxCreateDoubleScalar(scalar);
-	inputs[0]=psearch_scalar;
-	inputs[1]=optargs->femmodel;
-
-	mexCallMATLAB(1,&mxJ,2,(mxArray**)inputs, optargs->function_name);
-
-	/*extract misfit from mxArray*/
-	J=mxGetScalar(mxJ);
-
-	return J;
-}
-#else
-double OptFunc(double scalar, OptArgs* optargs){
-	_error_(" not implemented yet");
-}
-#endif
Index: /issm/trunk-jpl/src/c/shared/Numerics/Verbosity.cpp
===================================================================
--- /issm/trunk-jpl/src/c/shared/Numerics/Verbosity.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/shared/Numerics/Verbosity.cpp	(revision 12011)
@@ -39,36 +39,12 @@
 	if(level<0) _error_("vebosity level should be a positive integer (user provided %i)",level);
 
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-
-	mxArray* output=NULL;
-	mxArray* input=NULL;
-	input=mxCreateDoubleScalar((double)level);
-
-	mexCallMATLAB(0,&output,1,&input,"SetVerbosityLevel");
-#else
-
 	verbositylevel = level;
 
-#endif
 }/*}}}*/
 /*FUNCTION GetVerbosityLevel {{{*/
 int  GetVerbosityLevel(void){
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-
-	mxArray* output=NULL;
-	mxArray* input=NULL;
-	double   level;
-
-	mexCallMATLAB(1,&output,0,&input,"GetVerbosityLevel");
-	level=mxGetScalar(output);
-
-	verbositylevel = (int)level;
-	return verbositylevel;
-
-#else
 
 	_assert_(verbositylevel>=0);
 	return verbositylevel;
 
-#endif
 }/*}}}*/
Index: /issm/trunk-jpl/src/c/toolkits/issm/SeqMat.cpp
===================================================================
--- /issm/trunk-jpl/src/c/toolkits/issm/SeqMat.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/toolkits/issm/SeqMat.cpp	(revision 12011)
@@ -92,52 +92,4 @@
 }
 /*}}}*/
-
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-/*FUNCTION SeqMat::ToMatlabMatrix{{{1*/
-mxArray* SeqMat::ToMatlabMatrix(void){
-
-	/*Intermediary: */
-	double* buffer=NULL;
-	mxArray* pfield=NULL;
-	
-	/*output: */
-	mxArray* dataref=NULL;
-
-	/*copy vector into a new buffer: */
-	if(this->M*this->N){
-		buffer=(double*)xmalloc(this->M*this->N*sizeof(double));
-		memcpy(buffer,this->matrix,M*N*sizeof(double));
-
-		pfield=mxCreateDoubleMatrix(0,0,mxREAL);
-		mxSetM(pfield,this->N);
-		mxSetN(pfield,this->M);
-		mxSetPr(pfield,buffer);
-		
-		//transpose the matrix, written directly to matlab! from C to matlab.
-		mexCallMATLAB(1,&dataref, 1, &pfield, "transpose");
-	}
-	else dataref=mxCreateDoubleMatrix(0,0,mxREAL);
-
-	/*do not erase buffer!: */
-	return dataref;
-
-}
-
-
-	
-	
-/*}}}*/
-/*FUNCTION MatlabMatrixToSeqMat{{{1*/
-SeqMat* MatlabMatrixToSeqMat(const mxArray* dataref){
-
-	SeqMat* output=NULL;
-
-	output=new SeqMat();
-	MatlabMatrixToDoubleMatrix(&output->matrix,&output->M,&output->N,dataref);
-	return output;
-
-}
-/*}}}*/
-#endif
 /*FUNCTION SeqMat::Assemble{{{1*/
 void SeqMat::Assemble(void){
Index: /issm/trunk-jpl/src/c/toolkits/issm/SeqMat.h
===================================================================
--- /issm/trunk-jpl/src/c/toolkits/issm/SeqMat.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/toolkits/issm/SeqMat.h	(revision 12011)
@@ -40,7 +40,4 @@
 		/*SeqMat specific routines {{{1*/
 		void Echo(void);
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		mxArray* ToMatlabMatrix(void);
-		#endif
 		void Assemble(void);
 		double Norm(NormMode norm_type);
@@ -56,8 +53,3 @@
 };
 		
-/*API :*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-SeqMat*  MatlabMatrixToSeqMat(const mxArray* dataref);
-#endif
-
 #endif //#ifndef _SEQMAT_H_
Index: /issm/trunk-jpl/src/c/toolkits/issm/SeqVec.cpp
===================================================================
--- /issm/trunk-jpl/src/c/toolkits/issm/SeqVec.cpp	(revision 12010)
+++ /issm/trunk-jpl/src/c/toolkits/issm/SeqVec.cpp	(revision 12011)
@@ -66,42 +66,4 @@
 /*}}}*/
 
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-/*FUNCTION SeqVec::ToMatlabVector{{{1*/
-mxArray* SeqVec::ToMatlabVector(void){
-
-	double* buffer=NULL;
-	
-	mxArray* dataref=NULL;
-
-	/*copy vector into a new buffer: */
-	if(this->M){
-		buffer=(double*)xmalloc(this->M*sizeof(double));
-		memcpy(buffer,vector,M*sizeof(double));
-
-		dataref = mxCreateDoubleMatrix(0,0,mxREAL);
-		mxSetM(dataref,this->M);
-		mxSetN(dataref,1);
-		mxSetPr(dataref,buffer);	
-	}
-	else dataref = mxCreateDoubleMatrix(0,0,mxREAL);
-
-
-	/*do not erase buffer!: */
-	return dataref;
-
-}
-/*}}}*/
-/*FUNCTION MatlabVectorToSeqVec{{{1*/
-SeqVec* MatlabVectorToSeqVec(const mxArray* dataref){
-
-	SeqVec* output=NULL;
-
-	output=new SeqVec();
-	MatlabVectorToDoubleVector(&output->vector,&output->M,dataref);
-	return output;
-
-}
-/*}}}*/
-#endif
 /*FUNCTION SeqVec::Assemble{{{1*/
 void SeqVec::Assemble(void){
Index: /issm/trunk-jpl/src/c/toolkits/issm/SeqVec.h
===================================================================
--- /issm/trunk-jpl/src/c/toolkits/issm/SeqVec.h	(revision 12010)
+++ /issm/trunk-jpl/src/c/toolkits/issm/SeqVec.h	(revision 12011)
@@ -37,7 +37,4 @@
 		/*SeqVec specific routines {{{1*/
 		void Echo(void);
-		#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-		mxArray* ToMatlabVector(void);
-		#endif
 		void Assemble(void);
 		void SetValues(int ssize, int* list, double* values, InsMode mode);
@@ -59,9 +56,3 @@
 };
 
-
-/*API :*/
-#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
-SeqVec*  MatlabVectorToSeqVec(const mxArray* dataref);
-#endif
-
 #endif //#ifndef _SEQVEC_H_
Index: /issm/trunk-jpl/startup.py
===================================================================
--- /issm/trunk-jpl/startup.py	(revision 12010)
+++ /issm/trunk-jpl/startup.py	(revision 12011)
@@ -79,5 +79,4 @@
 from miscellaneous import *
 from private import *
-from TriMesh import *
 
 #}}}
Index: /issm/trunk-jpl/test/NightlyRun/test101.py
===================================================================
--- /issm/trunk-jpl/test/NightlyRun/test101.py	(revision 12010)
+++ /issm/trunk-jpl/test/NightlyRun/test101.py	(revision 12011)
@@ -1,6 +1,8 @@
 from model import *
-from TriMesh import *
+from TriMesh.Python import *
 
-md=model()
+#md=model()
+#md=triangle(md,'../Exp/Square.exp',150000);
+
 [a,b,c,d,e]=TriMesh('../Exp/Square.exp',15000.0**2.0,True)
 print(a,b,c,d,e)
