Index: /issm/trunk-jpl/src/c/EnumDefinitions/EnumDefinitions.h
===================================================================
--- /issm/trunk-jpl/src/c/EnumDefinitions/EnumDefinitions.h	(revision 13282)
+++ /issm/trunk-jpl/src/c/EnumDefinitions/EnumDefinitions.h	(revision 13283)
@@ -17,4 +17,6 @@
 	AutodiffIndependentsEnum,
 	AutodiffNumIndependentsEnum,
+	AutodiffJacobianEnum,
+	AutodiffXpEnum,
 	BalancethicknessSpcthicknessEnum,
 	BalancethicknessStabilizationEnum,
Index: /issm/trunk-jpl/src/c/Makefile.am
===================================================================
--- /issm/trunk-jpl/src/c/Makefile.am	(revision 13282)
+++ /issm/trunk-jpl/src/c/Makefile.am	(revision 13283)
@@ -329,4 +329,8 @@
 					./modules/RequestedOutputsx/RequestedOutputsx.h\
 					./modules/RequestedOutputsx/RequestedOutputsx.cpp\
+					./modules/RequestedDependentsx/RequestedDependentsx.h\
+					./modules/RequestedDependentsx/RequestedDependentsx.cpp\
+					./modules/AutodiffDriversx/AutodiffDriversx.h\
+					./modules/AutodiffDriversx/AutodiffDriversx.cpp\
 					./modules/ResetConstraintsx/ResetConstraintsx.h\
 					./modules/ResetConstraintsx/ResetConstraintsx.cpp\
Index: /issm/trunk-jpl/src/c/classes/IoModel.cpp
===================================================================
--- /issm/trunk-jpl/src/c/classes/IoModel.cpp	(revision 13282)
+++ /issm/trunk-jpl/src/c/classes/IoModel.cpp	(revision 13283)
@@ -26,4 +26,5 @@
 	this->fid=NULL;
 	this->data=NULL;
+	this->independents=NULL;
 	this->constants=NULL;
 	
@@ -55,4 +56,8 @@
 	this->data=xNew<IssmDouble*>(MaximumNumberOfEnums);
 	for(int i=0;i<MaximumNumberOfEnums;i++) this->data[i]=NULL;
+
+	/*Initialize array detecting whether data[i] is an independent AD mode variable: */
+	this->independents=xNew<bool>(MaximumNumberOfEnums);
+	for(int i=0;i<MaximumNumberOfEnums;i++) this->independents[i]=false;
 	
 	/*Initialize permanent data: */
@@ -85,4 +90,5 @@
 
 	xDelete<IssmDouble*>(this->data);
+	xDelete<bool>(this->independents);
 	xDelete<bool>(this->my_elements);
 	xDelete<bool>(this->my_nodes);
@@ -191,5 +197,5 @@
 }
 /*}}}*/
-/*FUNCTION IoModel::DeleteData{{{*/
+/*FUNCTION IoModel::DeleteData(int num,...){{{*/
 void  IoModel::DeleteData(int num,...){
 
@@ -205,7 +211,16 @@
 		dataenum=va_arg(ap, int);
 		_assert_(dataenum<MaximumNumberOfEnums);
-		xDelete<IssmDouble>(this->data[dataenum]);
+		
+		/*do not erase independent variables for the AD mode computations!: */
+		if (!this->independents[dataenum]) xDelete<IssmDouble>(this->data[dataenum]);
 	}
 	va_end(ap);
+} /*}}}*/
+/*FUNCTION IoModel::DeleteData(IssmDouble* {{{*/
+void  IoModel::DeleteData(IssmDouble* vector, int dataenum){
+
+	/*do not erase independent variables for the AD mode computations!: */
+	if(vector)if (!this->independents[dataenum]) xDelete<IssmDouble>(vector);
+
 } /*}}}*/
 /*FUNCTION IoModel::FetchConstants{{{*/
@@ -508,5 +523,5 @@
 	IssmPDouble   scalar;
 	int      code;
-	
+
 	/*Set file pointer to beginning of the data: */
 	fid=this->SetFilePointerToData(&code,NULL,data_enum);
@@ -538,5 +553,5 @@
 	int   string_size;
 	int code=0;
-	
+
 	/*Set file pointer to beginning of the data: */
 	fid=this->SetFilePointerToData(&code,NULL,data_enum);
@@ -591,6 +606,6 @@
 	int code=0;
 	int vector_type=0;
-	
-	
+
+
 	/*Set file pointer to beginning of the data: */
 	fid=this->SetFilePointerToData(&code,&vector_type,data_enum);
@@ -664,5 +679,5 @@
 	int code=0;
 	int vector_type=0;
-	
+
 	/*Set file pointer to beginning of the data: */
 	fid=this->SetFilePointerToData(&code,&vector_type,data_enum);
@@ -698,6 +713,14 @@
 		MPI_Bcast(matrix,M*N,MPI_DOUBLE,0,MPI_COMM_WORLD); 
 		#endif
-		*pmatrix=xNew<IssmDouble>(M*N);
-		for (int i=0;i<M*N;++i) (*pmatrix)[i]=matrix[i];
+
+		if (this->independents[data_enum]){
+			/*this data has already been checked out! So cancel all that we've done here, and return 
+			 * the data[data_enum] directly: */
+			*pmatrix=this->data[data_enum];
+		}
+		else{
+			*pmatrix=xNew<IssmDouble>(M*N);
+			for (int i=0;i<M*N;++i) (*pmatrix)[i]=matrix[i];
+		}
 		xDelete<IssmPDouble>(matrix);
 	}
@@ -936,4 +959,9 @@
 		
 		dataenum=va_arg(ap, int);
+		
+		if (this->independents[dataenum]){
+			/*this data has already been checked out! Continue: */
+			continue;
+		}
 
 		/*Some checks in debugging mode*/
@@ -1146,8 +1174,9 @@
 			/*}}}*/
 	}
-	/*Free ressources:*/
-	xDelete<IssmDouble>(IssmDoublevector);
+	/*Free ressources. Pay attention to not freeing an AD mode independent variable though!:*/
+	if (!this->independents[vector_enum] && !this->independents[default_vector_enum]) xDelete<IssmDouble>(IssmDoublevector);
 	xDelete<char>(string);
 }
+/*}}}*/
 /*FUNCTION IoModel::LastIndex{{{*/
 void IoModel::LastIndex(int *pindex){
@@ -1185,4 +1214,5 @@
 	*pindex=lastindex;
 }
+/*}}}*/
 /*FUNCTION IoModel::SetFilePointerToData{{{*/
 FILE* IoModel::SetFilePointerToData(int* pcode,int* pvector_type, int data_enum){
@@ -1250,2 +1280,95 @@
 }
 /*}}}*/
+/*FUNCTION IoModel::DeclareIndependents{{{*/
+void IoModel::DeclareIndependents(void){
+
+	bool autodiff=false;
+	int  dummy;
+	int i;
+	int  num_independents;
+	int* independents=NULL;
+
+	#ifdef _HAVE_ADOLC_
+	/*recover independent enums: */
+	this->Constant(&num_independents,AutodiffNumIndependentsEnum);
+	if(num_independents){
+		this->FetchData(&independents,&dummy,&dummy,AutodiffIndependentsEnum);
+
+		/*now go fetch the independent variables for each independent enum: */
+		for(i=0;i<num_independents;i++){
+			this->FetchIndependent(independents[i]);
+		}
+		xDelete<int>(independents);
+	}
+	#else
+	/*if we asked for AD computations, we have a problem!: */
+	this->Constant(&autodiff,AutodiffIsautodiffEnum);
+	if(autodiff)_error_("Cannot carry out AD mode computations without support of ADOLC compiled in!");
+	#endif
+
+}
+/*}}}*/
+/*FUNCTION IoModel::FetchIndependent{{{*/
+void IoModel::FetchIndependent(int independent_enum){
+
+	#ifdef _HAVE_ADOLC_ //cannot come here unless you are running AD mode, from DeclaredIndependents:
+	extern int my_rank;
+	extern int num_procs;
+
+	/*output: */
+	int M,N;
+	IssmPDouble* buffer=NULL; //a buffer to read the data from disk
+	IssmDouble* matrix=NULL; //our independent variable
+	int code=0;
+	int vector_type=0;
+	
+	/*Set file pointer to beginning of the data: */
+	fid=this->SetFilePointerToData(&code,&vector_type,independent_enum);
+	if((code!=5) && (code!=6) && (code!=7))_error_("expecting a IssmDouble, integer or boolean matrix for enum " << EnumToStringx(independent_enum));
+	
+	/*Now fetch: */
+
+	/*We have to read a matrix from disk. First read the dimensions of the matrix, then the whole matrix: */
+	/*numberofelements: */
+	if(my_rank==0){  
+		if(fread(&M,sizeof(int),1,fid)!=1) _error_("could not read number of rows for matrix ");
+	}
+	#ifdef _HAVE_MPI_
+	MPI_Bcast(&M,1,MPI_INT,0,MPI_COMM_WORLD); 
+	#endif
+
+	if(my_rank==0){  
+		if(fread(&N,sizeof(int),1,fid)!=1) _error_("could not read number of columns for matrix ");
+	}
+	#ifdef _HAVE_MPI_
+	MPI_Bcast(&N,1,MPI_INT,0,MPI_COMM_WORLD); 
+	#endif
+
+	/*Now allocate matrix: */
+	if(M*N){
+		buffer=xNew<IssmPDouble>(M*N);
+		matrix=xNew<IssmDouble>(M*N);
+
+		/*Read matrix on node 0, then broadcast: */
+		if(my_rank==0){  
+			if(fread(buffer,M*N*sizeof(IssmPDouble),1,fid)!=1) _error_("could not read matrix ");
+			
+			/*Now, before we even broadcast this to other nodes, declare the whole matrix as a independent variable!: */
+			for (int i=0;i<M*N;++i) matrix[i]<<=buffer[i];  /*we use the <<= ADOLC overloaded operator to declare the independency*/
+		}
+		#ifdef _HAVE_MPI_
+		MPI_Bcast(matrix,M*N,MPI_DOUBLE,0,MPI_COMM_WORLD); 
+		#endif
+		
+		xDelete<IssmPDouble>(buffer);
+	}
+	else _error_("cannot declare the independent variable " << EnumToStringx(independent_enum) <<  "if it's empty!");
+
+	/*Ok, we are almost done. Matrix is now a independent matrix. We don't want this matrix to be fetched again in the 
+	 *future, which would effectively write over the independency in the ADOLC tape! So we are going to keep track of this 
+	 independent matrix inthe iomodel->data[independent_enum] data slot: */
+	this->data[independent_enum]=matrix;
+	this->independents[independent_enum]=true;
+ 	#endif
+}
+/*}}}*/
Index: /issm/trunk-jpl/src/c/classes/IoModel.h
===================================================================
--- /issm/trunk-jpl/src/c/classes/IoModel.h	(revision 13282)
+++ /issm/trunk-jpl/src/c/classes/IoModel.h	(revision 13283)
@@ -19,8 +19,9 @@
 
 	private: 
+		Parameters  *constants;   //this dataset holds all IssmDouble, int, bool and char *parameters read in from the input file.*
+	
+	public:
 		IssmDouble **data;        //this dataset holds temporary data, memory intensive.
-		Parameters  *constants;   //this dataset holds all IssmDouble, int, bool and char *parameters read in from the input file.*
-
-	public:
+		
 		/*This data needs to stay memory resident at all time, even if it's memory intensive: */
 		FILE *fid;         //pointer to input file
@@ -31,8 +32,12 @@
 		int  *numbernodetoelementconnectivity;
 
+
 		/*Data to synchronize through low level object drivers: */
 		int nodecounter;         //keep track of how many nodes are being created in each analysis type
 		int loadcounter;         //keep track of how many loads are being created in each analysis type
 		int constraintcounter;   //keep track of how many constraints are being created in each analysis type
+		
+		/*for AD mode: to keep track of our independent variables we fetch:*/
+		bool* independents;
 
 		/*Methods*/
@@ -50,4 +55,5 @@
 		IssmDouble *Data(int dataenum);
 		void        DeleteData(int num,...);
+		void        DeleteData(IssmDouble* vector, int dataenum);
 		void        FetchConstants(void);
 		void        FetchData(bool* pboolean,int data_enum);
@@ -64,7 +70,6 @@
 		void        LastIndex(int *pindex);
 		FILE*       SetFilePointerToData(int* pcode,int* pvector_type, int data_enum);
-		#ifdef _HAVE_ADOLC_
-		void        FetchIndependentVariable(IssmDouble**  pscalarmatrix,int* pM,int* pN,int data_enum);
-		#endif
+		void        DeclareIndependents(void);
+		void        FetchIndependent(int dependent_enum);
 };
 
Index: /issm/trunk-jpl/src/c/modules/AutodiffDriversx/AutodiffDriversx.cpp
===================================================================
--- /issm/trunk-jpl/src/c/modules/AutodiffDriversx/AutodiffDriversx.cpp	(revision 13283)
+++ /issm/trunk-jpl/src/c/modules/AutodiffDriversx/AutodiffDriversx.cpp	(revision 13283)
@@ -0,0 +1,71 @@
+/*!\file AutodiffDriversx
+ * \brief: compute outputs from the AD mode,  using our dependents and independents, and drivers available in Adolc.
+ */
+
+#include "../../modules/modules.h"
+#include "../../shared/shared.h"
+#include "../../include/include.h"
+#include "../../toolkits/toolkits.h"
+#include "../../EnumDefinitions/EnumDefinitions.h"
+
+void AutodiffDriversx(Elements* elements,Nodes* nodes,Vertices* vertices,Loads* loads,Materials* materials,Parameters* parameters,Results* results){
+
+
+
+
+
+	int          i,j;
+	bool         isautodiff       = false;
+
+	int          num_dependents;
+	int          num_independents;
+	int          numberofvertices;
+
+	/*outputs: */
+	IssmDouble  *matJ             = NULL;
+	IssmDouble  *axp                = NULL;
+
+	double     **J                = NULL;
+	double      *xp                = NULL;
+
+
+	/*AD mode on?: */
+	parameters->FindParam(&isautodiff,AutodiffIsautodiffEnum);
+
+	if(isautodiff){
+		parameters->FindParam(&num_dependents,AutodiffNumDependentsEnum);
+		parameters->FindParam(&axp,&num_independents,AutodiffXpEnum);
+		parameters->FindParam(&numberofvertices,MeshNumberofverticesEnum);
+
+		if(!num_dependents*num_independents)return;
+
+		/*allocate driver results: */
+		J=xNew<double*>(num_independents);
+		xp=xNew<double>(num_independents); 
+
+		for(i=0;i<num_independents;i++){
+			J[i]=xNew<double>(num_dependents);
+			xp[i]=reCast<double,IssmDouble>(axp[i]);
+		}
+
+		/*Call AD driver: */
+		jacobian(1,num_dependents,num_independents,xp,J);
+
+		/*Add jacobian matrix to results: */
+		matJ=xNew<IssmDouble>(num_dependents*num_independents);
+		for (i=0;i<num_independents;++i){
+			double* rowi=J[i];
+			for (j=0;j<num_dependents;++j){
+				*(matJ+num_dependents*i+j)=rowi[j];
+			}
+		}
+					
+		results->AddObject(new DoubleMatExternalResult(results->Size()+1,AutodiffJacobianEnum,matJ,num_independents,num_dependents,1,0.0));
+
+		/*Free ressources: */
+		xDelete<double*>(J);
+		xDelete<double>(xp);
+		xDelete<IssmDouble>(matJ);
+		xDelete<IssmDouble>(axp);
+	}
+}
Index: /issm/trunk-jpl/src/c/modules/AutodiffDriversx/AutodiffDriversx.h
===================================================================
--- /issm/trunk-jpl/src/c/modules/AutodiffDriversx/AutodiffDriversx.h	(revision 13283)
+++ /issm/trunk-jpl/src/c/modules/AutodiffDriversx/AutodiffDriversx.h	(revision 13283)
@@ -0,0 +1,14 @@
+/*!\file:  AutodiffDriversx.h
+ * \brief header file for  requesting AD results (gradient, jacobian, etc ...)
+ */ 
+
+#ifndef _AUTODIFF_DRIVERSX_H_
+#define _AUTODIFF_DRIVERSX_H_
+
+#include "../../Container/Container.h"
+
+/* local prototypes: */
+void AutodiffDriversx(Elements* elements,Nodes* nodes,Vertices* vertices,Loads* loads,Materials* materials,Parameters* parameters,Results* results);
+
+#endif  /* _INPUTTORESULTX_H */
+
Index: /issm/trunk-jpl/src/c/modules/EnumToStringx/EnumToStringx.cpp
===================================================================
--- /issm/trunk-jpl/src/c/modules/EnumToStringx/EnumToStringx.cpp	(revision 13282)
+++ /issm/trunk-jpl/src/c/modules/EnumToStringx/EnumToStringx.cpp	(revision 13283)
@@ -22,4 +22,6 @@
 		case AutodiffIndependentsEnum : return "AutodiffIndependents";
 		case AutodiffNumIndependentsEnum : return "AutodiffNumIndependents";
+		case AutodiffJacobianEnum : return "AutodiffJacobian";
+		case AutodiffXpEnum : return "AutodiffXp";
 		case BalancethicknessSpcthicknessEnum : return "BalancethicknessSpcthickness";
 		case BalancethicknessStabilizationEnum : return "BalancethicknessStabilization";
Index: /issm/trunk-jpl/src/c/modules/ModelProcessorx/Autodiff/CreateParametersAutodiff.cpp
===================================================================
--- /issm/trunk-jpl/src/c/modules/ModelProcessorx/Autodiff/CreateParametersAutodiff.cpp	(revision 13282)
+++ /issm/trunk-jpl/src/c/modules/ModelProcessorx/Autodiff/CreateParametersAutodiff.cpp	(revision 13283)
@@ -14,5 +14,5 @@
 void CreateParametersAutodiff(Parameters** pparameters,IoModel* iomodel,int solution_type,int analysis_type){
 	
-	int         i;
+	int         i,j;
 	Parameters *parameters       = NULL;
 	bool        autodiff_analysis;
@@ -21,4 +21,6 @@
 	int*        independents       = NULL;
 	int         num_independents;
+	int         numberofvertices;
+	IssmDouble* xp=NULL;
 	
 	/*Get parameters: */
@@ -27,20 +29,37 @@
 	/*retrieve some parameters: */
 	iomodel->Constant(&autodiff_analysis,AutodiffIsautodiffEnum);
+	
+	if(autodiff_analysis){
 
-	if(autodiff_analysis){
+		iomodel->Constant(&num_independents,AutodiffNumIndependentsEnum);
+		iomodel->Constant(&num_dependents,AutodiffNumDependentsEnum);
+		iomodel->Constant(&numberofvertices,MeshNumberofverticesEnum);
 
 		/*recover dependents: */
 		parameters->AddObject(iomodel->CopyConstantObject(AutodiffNumDependentsEnum));
-		iomodel->FetchData(&dependents,NULL,&num_dependents,AutodiffDependentsEnum);
-		parameters->AddObject(new IntVecParam(AutodiffDependentsEnum,dependents,num_dependents));
-	
+		if(num_dependents){
+			iomodel->FetchData(&dependents,NULL,&num_dependents,AutodiffDependentsEnum);
+			parameters->AddObject(new IntVecParam(AutodiffDependentsEnum,dependents,num_dependents));
+		}
+
 		/*recover independents: */
 		parameters->AddObject(iomodel->CopyConstantObject(AutodiffNumIndependentsEnum));
-		iomodel->FetchData(&independents,NULL,&num_independents,AutodiffIndependentsEnum);
-		parameters->AddObject(new IntVecParam(AutodiffIndependentsEnum,independents,num_independents));
+		if(num_independents){
+			iomodel->FetchData(&independents,NULL,&num_independents,AutodiffIndependentsEnum);
+			parameters->AddObject(new IntVecParam(AutodiffIndependentsEnum,independents,num_independents));
 
+			/*Build state vector, value at which we compute our gradients of dependent variables in adolc: the xp vector  */
+			xp=xNew<IssmDouble>(num_independents*numberofvertices);
+			for(i=0;i<num_independents;i++){
+				IssmDouble* values=iomodel->data[independents[i]];
+				for(j=0;j<numberofvertices;j++){
+					xp[i*numberofvertices+j]=values[j];
+				}
+			}
+			parameters->AddObject(new DoubleVecParam(AutodiffXpEnum,xp,num_independents*numberofvertices));
+		}
+
+		/*Assign output pointer: */
+		*pparameters=parameters;
 	}
-
-	/*Assign output pointer: */
-	*pparameters=parameters;
 }
Index: /issm/trunk-jpl/src/c/modules/ModelProcessorx/Control/CreateParametersControl.cpp
===================================================================
--- /issm/trunk-jpl/src/c/modules/ModelProcessorx/Control/CreateParametersControl.cpp	(revision 13282)
+++ /issm/trunk-jpl/src/c/modules/ModelProcessorx/Control/CreateParametersControl.cpp	(revision 13283)
@@ -64,8 +64,8 @@
 
 		xDelete<int>(control_type);
-		xDelete<IssmDouble>(cm_responses);
-		xDelete<IssmDouble>(cm_jump);
-		xDelete<IssmDouble>(optscal);
-		xDelete<IssmDouble>(maxiter);
+		iomodel->DeleteData(cm_responses,InversionCostFunctionsEnum);
+		iomodel->DeleteData(cm_jump,InversionStepThresholdEnum);
+		iomodel->DeleteData(optscal,InversionGradientScalingEnum);
+		iomodel->DeleteData(maxiter,InversionMaxiterPerStepEnum);
 	}
 
Index: /issm/trunk-jpl/src/c/modules/ModelProcessorx/CreateParameters.cpp
===================================================================
--- /issm/trunk-jpl/src/c/modules/ModelProcessorx/CreateParameters.cpp	(revision 13282)
+++ /issm/trunk-jpl/src/c/modules/ModelProcessorx/CreateParameters.cpp	(revision 13283)
@@ -92,5 +92,4 @@
 	parameters->AddObject(iomodel->CopyConstantObject(SurfaceforcingsIsdelta18oEnum));
 	parameters->AddObject(iomodel->CopyConstantObject(SurfaceforcingsIssmbgradientsEnum));
-	parameters->AddObject(iomodel->CopyConstantObject(AutodiffIsautodiffEnum));
 
 	iomodel->Constant(&isdelta18o,SurfaceforcingsIsdelta18oEnum);
@@ -105,10 +104,10 @@
 		for(i=0;i<M;i++) temp[M+i]=yts*temp[M+i];
 		parameters->AddObject(new TransientParam(SurfaceforcingsDelta18oEnum,&temp[0],&temp[M],M));
-		xDelete<IssmDouble>(temp);
+		iomodel->DeleteData(temp,SurfaceforcingsDelta18oEnum);
 
 		iomodel->FetchData(&temp,&N,&M,SurfaceforcingsDelta18oSurfaceEnum); _assert_(N==2);
 		for(i=0;i<M;i++) temp[M+i]=yts*temp[M+i];
 		parameters->AddObject(new TransientParam(SurfaceforcingsDelta18oSurfaceEnum,&temp[0],&temp[M],M));
-		xDelete<IssmDouble>(temp);
+		iomodel->DeleteData(temp,SurfaceforcingsDelta18oSurfaceEnum);
 	}
 
@@ -127,13 +126,13 @@
 	parameters->AddObject(new IntParam(DiagnosticNumRequestedOutputsEnum,numoutputs));
 	if(numoutputs)parameters->AddObject(new IntVecParam(DiagnosticRequestedOutputsEnum,requestedoutputs,numoutputs));
-	xDelete<IssmDouble>(requestedoutputs);
+	iomodel->DeleteData(requestedoutputs,DiagnosticRequestedOutputsEnum);
 	iomodel->FetchData(&requestedoutputs,&numoutputs,NULL,TransientRequestedOutputsEnum);
 	parameters->AddObject(new IntParam(TransientNumRequestedOutputsEnum,numoutputs));
 	if(numoutputs)parameters->AddObject(new IntVecParam(TransientRequestedOutputsEnum,requestedoutputs,numoutputs));
-	xDelete<IssmDouble>(requestedoutputs);
+	iomodel->DeleteData(requestedoutputs,TransientRequestedOutputsEnum);
 	iomodel->FetchData(&requestedoutputs,&numoutputs,NULL,SteadystateRequestedOutputsEnum);
 	parameters->AddObject(new IntParam(SteadystateNumRequestedOutputsEnum,numoutputs));
 	if(numoutputs)parameters->AddObject(new IntVecParam(SteadystateRequestedOutputsEnum,requestedoutputs,numoutputs));
-	xDelete<IssmDouble>(requestedoutputs);
+	iomodel->DeleteData(requestedoutputs,SteadystateRequestedOutputsEnum);
 	
 	/*Before returning, create parameters in case we are running Qmu or control types runs: */
Index: /issm/trunk-jpl/src/c/modules/ModelProcessorx/DiagnosticHoriz/CreateConstraintsDiagnosticHoriz.cpp
===================================================================
--- /issm/trunk-jpl/src/c/modules/ModelProcessorx/DiagnosticHoriz/CreateConstraintsDiagnosticHoriz.cpp	(revision 13282)
+++ /issm/trunk-jpl/src/c/modules/ModelProcessorx/DiagnosticHoriz/CreateConstraintsDiagnosticHoriz.cpp	(revision 13283)
@@ -326,15 +326,15 @@
 	  
 	/*Free data: */
-	xDelete<IssmDouble>(spcvx);
-	xDelete<IssmDouble>(spcvy);
-	xDelete<IssmDouble>(spcvz);
-	xDelete<IssmDouble>(nodeonmacayeal);
-	xDelete<IssmDouble>(nodeonpattyn);
-	xDelete<IssmDouble>(nodeonstokes);
-	xDelete<IssmDouble>(nodeonicesheet);
-	xDelete<IssmDouble>(nodeonbed);
-	xDelete<IssmDouble>(vertices_type);
-	xDelete<IssmDouble>(surface);
-	xDelete<IssmDouble>(z);
+	iomodel->DeleteData(spcvx,DiagnosticSpcvxEnum);
+	iomodel->DeleteData(spcvy,DiagnosticSpcvyEnum);
+	iomodel->DeleteData(spcvz,DiagnosticSpcvzEnum);
+	iomodel->DeleteData(nodeonmacayeal,FlowequationBordermacayealEnum);
+	if(dim==3)iomodel->DeleteData(nodeonpattyn,FlowequationBorderpattynEnum);
+	if(dim==3)iomodel->DeleteData(nodeonstokes,FlowequationBorderstokesEnum);
+	if(dim==3)iomodel->DeleteData(nodeonbed,MeshVertexonbedEnum);
+	if(dim==3)iomodel->DeleteData(nodeonicesheet,MaskVertexongroundediceEnum);
+	iomodel->DeleteData(vertices_type,FlowequationVertexEquationEnum);
+	iomodel->DeleteData(surface,SurfaceEnum);
+	iomodel->DeleteData(z,MeshZEnum);
 
 	/*Free resources:*/
Index: /issm/trunk-jpl/src/c/modules/ModelProcessorx/DiagnosticHoriz/CreateLoadsDiagnosticHoriz.cpp
===================================================================
--- /issm/trunk-jpl/src/c/modules/ModelProcessorx/DiagnosticHoriz/CreateLoadsDiagnosticHoriz.cpp	(revision 13282)
+++ /issm/trunk-jpl/src/c/modules/ModelProcessorx/DiagnosticHoriz/CreateLoadsDiagnosticHoriz.cpp	(revision 13283)
@@ -129,6 +129,7 @@
 	/*Free data: */
 	iomodel->DeleteData(3,DiagnosticIcefrontEnum,ThicknessEnum,BedEnum);
-	xDelete<IssmDouble>(elements_type);
-	xDelete<IssmDouble>(pressureload);
+	iomodel->DeleteData(elements_type,FlowequationElementEquationEnum);
+	iomodel->DeleteData(pressureload,DiagnosticIcefrontEnum);
+
 
 	/*Create Penpair for penalties: */
@@ -153,5 +154,5 @@
 
 	/*free ressources: */
-	xDelete<IssmDouble>(penalties);
+	iomodel->DeleteData(penalties,DiagnosticVertexPairingEnum);
 
 	/*Create Riffront loads for rifts: */
Index: /issm/trunk-jpl/src/c/modules/ModelProcessorx/ElementsAndVerticesPartitioning.cpp
===================================================================
--- /issm/trunk-jpl/src/c/modules/ModelProcessorx/ElementsAndVerticesPartitioning.cpp	(revision 13282)
+++ /issm/trunk-jpl/src/c/modules/ModelProcessorx/ElementsAndVerticesPartitioning.cpp	(revision 13283)
@@ -76,6 +76,6 @@
 
 	/*Free elements and elements2d: */
-	xDelete<IssmDouble>(elements);
-	xDelete<IssmDouble>(elements2d);
+	iomodel->DeleteData(elements,MeshElementsEnum);
+	iomodel->DeleteData(elements2d,MeshElements2dEnum);
 
 	/*Deal with rifts, they have to be included into one partition only, not several: */
@@ -87,5 +87,5 @@
 			epart[el2]=epart[el1]; //ensures that this pair of elements will be in the same partition, as well as the corresponding vertices;
 		}
-		xDelete<IssmDouble>(riftinfo); 
+		iomodel->DeleteData(riftinfo,RiftsRiftstructEnum);
 	}
 
@@ -119,5 +119,5 @@
 	}//for (i=0;i<numberofelements;i++)
 	/*Free data : */
-	xDelete<IssmDouble>(elements);
+	iomodel->DeleteData(elements,MeshElementsEnum);
 
 	/*We might have vertex_pairing in which case, some vertices have to be cloned:
@@ -130,5 +130,5 @@
 		}
 	}
-	xDelete<IssmDouble>(vertex_pairing);
+	iomodel->DeleteData(vertex_pairing,DiagnosticVertexPairingEnum);
 	iomodel->FetchData(&vertex_pairing,&numvertex_pairing,NULL,PrognosticVertexPairingEnum);
 	for(i=0;i<numvertex_pairing;i++){
@@ -137,5 +137,5 @@
 		}
 	}
-	xDelete<IssmDouble>(vertex_pairing);
+	iomodel->DeleteData(vertex_pairing,PrognosticVertexPairingEnum);
 
 	/*Free ressources:*/
Index: /issm/trunk-jpl/src/c/modules/ModelProcessorx/Enthalpy/CreateConstraintsEnthalpy.cpp
===================================================================
--- /issm/trunk-jpl/src/c/modules/ModelProcessorx/Enthalpy/CreateConstraintsEnthalpy.cpp	(revision 13282)
+++ /issm/trunk-jpl/src/c/modules/ModelProcessorx/Enthalpy/CreateConstraintsEnthalpy.cpp	(revision 13283)
@@ -110,5 +110,5 @@
 
 	/*Free ressources:*/
-	xDelete<IssmDouble>(spcvector);
+	iomodel->DeleteData(spcvector,ThermalSpctemperatureEnum);
 	xDelete<IssmDouble>(times);
 	xDelete<IssmDouble>(values);
Index: /issm/trunk-jpl/src/c/modules/ModelProcessorx/ModelProcessorx.cpp
===================================================================
--- /issm/trunk-jpl/src/c/modules/ModelProcessorx/ModelProcessorx.cpp	(revision 13282)
+++ /issm/trunk-jpl/src/c/modules/ModelProcessorx/ModelProcessorx.cpp	(revision 13283)
@@ -20,5 +20,5 @@
 
 	int   i,analysis_type,dim,verbose;
-	bool  isthermal,isprognostic,isdiagnostic,isgroundingline,isenthalpy;
+	bool  isthermal,isprognostic,isdiagnostic,isgroundingline,isenthalpy,autodiff;
 	
 	/*output: */
@@ -43,5 +43,11 @@
 	iomodel->Constant(&isdiagnostic,TransientIsdiagnosticEnum);
 	iomodel->Constant(&isgroundingline,TransientIsgroundinglineEnum);
-	
+	iomodel->Constant(&autodiff,AutodiffIsautodiffEnum);
+
+	/*If we are running in AD mode, we need to declare our independent variables now, before 
+	 *and prevent them from being erased during successive calls to iomodel->FetchData and 
+	 iomodel->DeleteData:*/
+	if(autodiff)iomodel->DeclareIndependents();
+
 	SetVerbosityLevel(verbose);
 
Index: /issm/trunk-jpl/src/c/modules/ModelProcessorx/Prognostic/CreateLoadsPrognostic.cpp
===================================================================
--- /issm/trunk-jpl/src/c/modules/ModelProcessorx/Prognostic/CreateLoadsPrognostic.cpp	(revision 13282)
+++ /issm/trunk-jpl/src/c/modules/ModelProcessorx/Prognostic/CreateLoadsPrognostic.cpp	(revision 13283)
@@ -90,6 +90,6 @@
 
 	/*free ressources: */
-	xDelete<IssmDouble>(vertex_pairing);
-	xDelete<IssmDouble>(nodeonbed);
+	iomodel->DeleteData(vertex_pairing,PrognosticVertexPairingEnum);
+	iomodel->DeleteData(nodeonbed,MeshVertexonbedEnum);
 
 	/*Assign output pointer: */
Index: /issm/trunk-jpl/src/c/modules/RequestedDependentsx/RequestedDependentsx.cpp
===================================================================
--- /issm/trunk-jpl/src/c/modules/RequestedDependentsx/RequestedDependentsx.cpp	(revision 13283)
+++ /issm/trunk-jpl/src/c/modules/RequestedDependentsx/RequestedDependentsx.cpp	(revision 13283)
@@ -0,0 +1,41 @@
+/*!\file RequestedDependentsx
+ * \brief: compute outputs that were requested specifically for this solution, such as BasalStress, StrainHeating, etc ...
+ */
+
+#include "../../modules/modules.h"
+#include "../../shared/shared.h"
+#include "../../include/include.h"
+#include "../../toolkits/toolkits.h"
+#include "../../EnumDefinitions/EnumDefinitions.h"
+
+void RequestedDependentsx(Results* results,Elements* elements,Nodes* nodes,Vertices* vertices,Loads* loads,Materials* materials,Parameters* parameters){
+
+
+
+	int         i;
+	int         output_enum;
+	bool        isautodiff      = false;
+	IssmDouble  output_value;
+	Element    *element         = NULL;
+	int        *dependent_enums = NULL;
+
+	int         num_dependents;
+	IssmPDouble *dependents;
+
+	/*AD mode on?: */
+	parameters->FindParam(&isautodiff,AutodiffIsautodiffEnum);
+
+	if(isautodiff){
+		parameters->FindParam(&num_dependents,AutodiffNumDependentsEnum);
+		if(num_dependents){
+			dependents=xNew<IssmPDouble>(num_dependents);
+			parameters->FindParam(&dependent_enums,&num_dependents,AutodiffDependentsEnum);
+
+			/*Go through our dependent variables, and compute the response:*/
+			for(i=0;i<num_dependents;i++){
+				Responsex(&output_value,elements,nodes,vertices,loads,materials,parameters,dependent_enums[i],false,0);
+				output_value>>=dependents[i];
+			}
+		}
+	}
+}
Index: /issm/trunk-jpl/src/c/modules/RequestedDependentsx/RequestedDependentsx.h
===================================================================
--- /issm/trunk-jpl/src/c/modules/RequestedDependentsx/RequestedDependentsx.h	(revision 13283)
+++ /issm/trunk-jpl/src/c/modules/RequestedDependentsx/RequestedDependentsx.h	(revision 13283)
@@ -0,0 +1,14 @@
+/*!\file:  RequestedDependentsx.h
+ * \brief header file for  requesting AD dependents
+ */ 
+
+#ifndef _REQUESTED_DEPENDENTSX_H_
+#define _REQUESTED_DEPENDENTSX_H_
+
+#include "../../Container/Container.h"
+
+/* local prototypes: */
+void RequestedDependentsx(Results* results,Elements* elements,Nodes* nodes,Vertices* vertices,Loads* loads,Materials* materials,Parameters* parameters);
+
+#endif  /* _INPUTTORESULTX_H */
+
Index: /issm/trunk-jpl/src/c/modules/StringToEnumx/StringToEnumx.cpp
===================================================================
--- /issm/trunk-jpl/src/c/modules/StringToEnumx/StringToEnumx.cpp	(revision 13282)
+++ /issm/trunk-jpl/src/c/modules/StringToEnumx/StringToEnumx.cpp	(revision 13283)
@@ -23,4 +23,6 @@
 	      else if (strcmp(name,"AutodiffIndependents")==0) return AutodiffIndependentsEnum;
 	      else if (strcmp(name,"AutodiffNumIndependents")==0) return AutodiffNumIndependentsEnum;
+	      else if (strcmp(name,"AutodiffJacobian")==0) return AutodiffJacobianEnum;
+	      else if (strcmp(name,"AutodiffXp")==0) return AutodiffXpEnum;
 	      else if (strcmp(name,"BalancethicknessSpcthickness")==0) return BalancethicknessSpcthicknessEnum;
 	      else if (strcmp(name,"BalancethicknessStabilization")==0) return BalancethicknessStabilizationEnum;
@@ -136,10 +138,10 @@
 	      else if (strcmp(name,"MeshVertexonbed")==0) return MeshVertexonbedEnum;
 	      else if (strcmp(name,"MeshVertexonsurface")==0) return MeshVertexonsurfaceEnum;
-	      else if (strcmp(name,"MeshX")==0) return MeshXEnum;
-	      else if (strcmp(name,"MeshY")==0) return MeshYEnum;
          else stage=2;
    }
    if(stage==2){
-	      if (strcmp(name,"MeshZ")==0) return MeshZEnum;
+	      if (strcmp(name,"MeshX")==0) return MeshXEnum;
+	      else if (strcmp(name,"MeshY")==0) return MeshYEnum;
+	      else if (strcmp(name,"MeshZ")==0) return MeshZEnum;
 	      else if (strcmp(name,"MiscellaneousName")==0) return MiscellaneousNameEnum;
 	      else if (strcmp(name,"PrognosticHydrostaticAdjustment")==0) return PrognosticHydrostaticAdjustmentEnum;
@@ -259,10 +261,10 @@
 	      else if (strcmp(name,"Materials")==0) return MaterialsEnum;
 	      else if (strcmp(name,"Nodes")==0) return NodesEnum;
-	      else if (strcmp(name,"Parameters")==0) return ParametersEnum;
-	      else if (strcmp(name,"Vertices")==0) return VerticesEnum;
          else stage=3;
    }
    if(stage==3){
-	      if (strcmp(name,"Results")==0) return ResultsEnum;
+	      if (strcmp(name,"Parameters")==0) return ParametersEnum;
+	      else if (strcmp(name,"Vertices")==0) return VerticesEnum;
+	      else if (strcmp(name,"Results")==0) return ResultsEnum;
 	      else if (strcmp(name,"AdolcParam")==0) return AdolcParamEnum;
 	      else if (strcmp(name,"BoolInput")==0) return BoolInputEnum;
@@ -382,10 +384,10 @@
 	      else if (strcmp(name,"VzMacAyeal")==0) return VzMacAyealEnum;
 	      else if (strcmp(name,"VzPattyn")==0) return VzPattynEnum;
-	      else if (strcmp(name,"VzPicard")==0) return VzPicardEnum;
-	      else if (strcmp(name,"VzStokes")==0) return VzStokesEnum;
          else stage=4;
    }
    if(stage==4){
-	      if (strcmp(name,"VxMesh")==0) return VxMeshEnum;
+	      if (strcmp(name,"VzPicard")==0) return VzPicardEnum;
+	      else if (strcmp(name,"VzStokes")==0) return VzStokesEnum;
+	      else if (strcmp(name,"VxMesh")==0) return VxMeshEnum;
 	      else if (strcmp(name,"VyMesh")==0) return VyMeshEnum;
 	      else if (strcmp(name,"VzMesh")==0) return VzMeshEnum;
Index: /issm/trunk-jpl/src/c/modules/modules.h
===================================================================
--- /issm/trunk-jpl/src/c/modules/modules.h	(revision 13282)
+++ /issm/trunk-jpl/src/c/modules/modules.h	(revision 13283)
@@ -8,4 +8,5 @@
 /*Modules: */
 #include "./AddExternalResultx/AddExternalResultx.h"
+#include "./AutodiffDriversx/AutodiffDriversx.h"
 #include "./AverageFilterx/AverageFilterx.h"
 #include "./AverageOntoPartitionx/AverageOntoPartitionx.h"
@@ -103,4 +104,5 @@
 #include "./Reducevectorgtofx/Reducevectorgtofx.h"
 #include "./RequestedOutputsx/RequestedOutputsx.h"
+#include "./RequestedDependentsx/RequestedDependentsx.h"
 #include "./ResetConstraintsx/ResetConstraintsx.h"
 #include "./ResetCoordinateSystemx/ResetCoordinateSystemx.h"
Index: /issm/trunk-jpl/src/c/solutions/issm.cpp
===================================================================
--- /issm/trunk-jpl/src/c/solutions/issm.cpp	(revision 13282)
+++ /issm/trunk-jpl/src/c/solutions/issm.cpp	(revision 13283)
@@ -84,5 +84,10 @@
 	/*out of solution_type, figure out types of analyses needed in the femmodel: */
 	AnalysisConfiguration(&analyses,&numanalyses,solution_type);
-	
+
+	/*before we create the model, start the trace on for AD mode: */
+	#ifdef _HAVE_ADOLC_
+	trace_on(1);
+	#endif
+
 	/*Create femmodel, using input file: */
 	#ifdef _HAVE_MPI_
@@ -124,5 +129,4 @@
 	theAdolcEDF_p->GetParameterValue().myEDF_for_solverx_p=reg_ext_fct(EDF_for_solverx);
 	femmodel->parameters->AddObject(theAdolcEDF_p);
-	if(autodiff) trace_on(1);
 	#else
 	if(autodiff) _error_("ISSM was not compiled with ADOLC support, cannot carry out autodiff analysis!");
@@ -166,4 +170,9 @@
 	}
 
+	if(autodiff){
+		trace_off();
+		AutodiffDriversx(femmodel->elements, femmodel->nodes, femmodel->vertices, femmodel->loads, femmodel->materials, femmodel->parameters,femmodel->results);
+	}
+
 
 	#ifdef _HAVE_MPI_
@@ -172,5 +181,5 @@
 	finish_core=(IssmPDouble)clock();
 	#endif
-
+	
 	_pprintLine_("write results to disk:");
 	OutputResultsx(femmodel->elements, femmodel->nodes, femmodel->vertices, femmodel->loads, femmodel->materials, femmodel->parameters,femmodel->results);
@@ -179,5 +188,4 @@
 	#ifdef _HAVE_ADOLC_
 	if(autodiff){
-		trace_off();
 		tapestats(1,tape_stats); //reading of tape statistics
 		_pprintLine_("   ADOLC statistics: ");
Index: /issm/trunk-jpl/src/c/solutions/transient_core.cpp
===================================================================
--- /issm/trunk-jpl/src/c/solutions/transient_core.cpp	(revision 13282)
+++ /issm/trunk-jpl/src/c/solutions/transient_core.cpp	(revision 13283)
@@ -152,4 +152,6 @@
 	}
 
+	RequestedDependentsx(femmodel->results,femmodel->elements,femmodel->nodes,femmodel->vertices,femmodel->loads,femmodel->materials,femmodel->parameters);
+	
 	/*Free ressources:*/
 	xDelete<int>(requested_outputs);
Index: /issm/trunk-jpl/src/m/classes/autodiff.m
===================================================================
--- /issm/trunk-jpl/src/m/classes/autodiff.m	(revision 13282)
+++ /issm/trunk-jpl/src/m/classes/autodiff.m	(revision 13283)
@@ -7,6 +7,6 @@
 	properties (SetAccess=public) 
 		isautodiff = false;
-		dependents = {''};
-		independents = {''};
+		dependents = {};
+		independents = {};
 	end
 	methods
@@ -23,4 +23,8 @@
 		end % }}}
 		function md = checkconsistency(obj,md,solution,analyses) % {{{
+
+		%Early return 
+		if ~obj.isautodiff, return; end
+
 
 		end % }}}
@@ -40,19 +44,25 @@
 			%process dependent variables
 			num_dependents=numel(obj.dependents);
-			data=zeros(1,num_dependents);
-			for i=1:num_dependents,
-				data(i)=StringToEnum(obj.dependents{i});
+			WriteData(fid,'data',num_dependents,'enum',AutodiffNumDependentsEnum(),'format','Integer');
+			
+			if(num_dependents),
+				data=zeros(1,num_dependents);
+				for i=1:num_dependents,
+					data(i)=StringToEnum(obj.dependents{i});
+				end
+				WriteData(fid,'data',data,'enum',AutodiffDependentsEnum(),'format','DoubleMat','mattype',3);
 			end
-			WriteData(fid,'data',data,'enum',AutodiffDependentsEnum(),'format','DoubleMat','mattype',3);
-			WriteData(fid,'data',num_dependents,'enum',AutodiffNumDependentsEnum(),'format','Integer');
 			
 			%process independent variables
 			num_independents=numel(obj.independents);
-			data=zeros(1,num_independents);
-			for i=1:num_independents,
-				data(i)=StringToEnum(obj.independents{i});
+			WriteData(fid,'data',num_independents,'enum',AutodiffNumIndependentsEnum(),'format','Integer');
+			
+			if(num_independents)
+				data=zeros(1,num_independents);
+				for i=1:num_independents,
+					data(i)=StringToEnum(obj.independents{i});
+				end
+				WriteData(fid,'data',data,'enum',AutodiffIndependentsEnum(),'format','DoubleMat','mattype',3);
 			end
-			WriteData(fid,'data',data,'enum',AutodiffIndependentsEnum(),'format','DoubleMat','mattype',3);
-			WriteData(fid,'data',num_independents,'enum',AutodiffNumIndependentsEnum(),'format','Integer');
 
 		end % }}}
Index: /issm/trunk-jpl/src/m/enum/AutodiffJacobianEnum.m
===================================================================
--- /issm/trunk-jpl/src/m/enum/AutodiffJacobianEnum.m	(revision 13283)
+++ /issm/trunk-jpl/src/m/enum/AutodiffJacobianEnum.m	(revision 13283)
@@ -0,0 +1,11 @@
+function macro=AutodiffJacobianEnum()
+%AUTODIFFJACOBIANENUM - Enum of AutodiffJacobian
+%
+%   WARNING: DO NOT MODIFY THIS FILE
+%            this file has been automatically generated by src/c/EnumDefinitions/Synchronize.sh
+%            Please read src/c/EnumDefinitions/README for more information
+%
+%   Usage:
+%      macro=AutodiffJacobianEnum()
+
+macro=StringToEnum('AutodiffJacobian');
Index: /issm/trunk-jpl/src/m/enum/AutodiffXpEnum.m
===================================================================
--- /issm/trunk-jpl/src/m/enum/AutodiffXpEnum.m	(revision 13283)
+++ /issm/trunk-jpl/src/m/enum/AutodiffXpEnum.m	(revision 13283)
@@ -0,0 +1,11 @@
+function macro=AutodiffXpEnum()
+%AUTODIFFXPENUM - Enum of AutodiffXp
+%
+%   WARNING: DO NOT MODIFY THIS FILE
+%            this file has been automatically generated by src/c/EnumDefinitions/Synchronize.sh
+%            Please read src/c/EnumDefinitions/README for more information
+%
+%   Usage:
+%      macro=AutodiffXpEnum()
+
+macro=StringToEnum('AutodiffXp');
Index: /issm/trunk-jpl/src/m/enum/EnumDefinitions.py
===================================================================
--- /issm/trunk-jpl/src/m/enum/EnumDefinitions.py	(revision 13282)
+++ /issm/trunk-jpl/src/m/enum/EnumDefinitions.py	(revision 13283)
@@ -59,4 +59,24 @@
 	return StringToEnum('AutodiffNumIndependents')[0]
 
+def AutodiffJacobianEnum():
+	"""
+	AUTODIFFJACOBIANENUM - Enum of AutodiffJacobian
+
+	   Usage:
+	      macro=AutodiffJacobianEnum()
+	"""
+
+	return StringToEnum('AutodiffJacobian')[0]
+
+def AutodiffXpEnum():
+	"""
+	AUTODIFFXPENUM - Enum of AutodiffXp
+
+	   Usage:
+	      macro=AutodiffXpEnum()
+	"""
+
+	return StringToEnum('AutodiffXp')[0]
+
 def BalancethicknessSpcthicknessEnum():
 	"""
@@ -4717,4 +4737,4 @@
 	"""
 
-	return 470
-
+	return 472
+
Index: /issm/trunk-jpl/src/m/enum/MaximumNumberOfEnums.m
===================================================================
--- /issm/trunk-jpl/src/m/enum/MaximumNumberOfEnums.m	(revision 13282)
+++ /issm/trunk-jpl/src/m/enum/MaximumNumberOfEnums.m	(revision 13283)
@@ -9,3 +9,3 @@
 %      macro=MaximumNumberOfEnums()
 
-macro=470;
+macro=472;
