Index: ../trunk-jpl/src/c/classes/IoModel.h
===================================================================
--- ../trunk-jpl/src/c/classes/IoModel.h	(revision 18878)
+++ ../trunk-jpl/src/c/classes/IoModel.h	(revision 18879)
@@ -58,7 +58,7 @@
 		/*Methods*/
 		~IoModel();
 		IoModel();
-		IoModel(FILE* iomodel_handle);
+		IoModel(FILE* iomodel_handle,bool trace,IssmPDouble* X);
 
 		/*Input/Output*/
 		void        CheckEnumSync(void);
@@ -92,8 +92,8 @@
 		void        LastIndex(int *pindex);
 		FILE*       SetFilePointerToData(int* pcode,int* pvector_type, int data_enum);
 		fpos_t*     SetFilePointersToData(int** pcodes,int** pvector_types, int* pnum_instances, int data_enum);
-		void        DeclareIndependents(void);
-		void        StartTrace(void);
+		void        DeclareIndependents(bool trace,IssmPDouble* X);
+		void        StartTrace(bool trace);
 		void        FetchIndependent(int dependent_enum);
 };
 
Index: ../trunk-jpl/src/c/classes/IoModel.cpp
===================================================================
--- ../trunk-jpl/src/c/classes/IoModel.cpp	(revision 18878)
+++ ../trunk-jpl/src/c/classes/IoModel.cpp	(revision 18879)
@@ -49,8 +49,11 @@
 	this->constraintcounter=0;
 }
 /*}}}*/
-IoModel::IoModel(FILE* iomodel_handle){/*{{{*/
+IoModel::IoModel(FILE* iomodel_handle,bool trace,IssmPDouble* X){/*{{{*/
 
+	bool autodiff=false;
+	bool iscontrol=false;
+	
 	/*First, keep track of the file handle: */
 	this->fid=iomodel_handle;
 
@@ -64,13 +67,28 @@
 	/*If we are running in AD mode, we need to start the trace and declare our independent variables now, 
 	 *and prevent them from being erased during successive calls to iomodel->FetchConstants, iomodel->FetchData and 
 	 iomodel->DeleteData:*/
-	this->StartTrace();
-	this->DeclareIndependents();
+	this->StartTrace(trace);
+	this->DeclareIndependents(trace,X);
 
 	/*Initialize and read constants:*/
 	this->constants=new Parameters();
 	this->FetchConstants(); /*this routine goes through the input file, and fetches bools, ints, IssmDoubles and strings only, nothing memory intensive*/
 
+	/*Now some very specific piece of logic. We want to know whether we are going to carry out an autodiff run. There are 
+	 *several cases: either trace is true (which bypasses the isautodiff choice), or trace is false but isautodiff is on, in which 
+	 *case two cases are possible: are we running a control method or not? To make things simpler, we are going to pin down everyting 
+	 *on isautodiff. By the way, we could not do this before we had the constants dataset initialized! {{{*/
+	this->constants->FindParam(&autodiff,AutodiffIsautodiffEnum);
+	this->constants->FindParam(&iscontrol,InversionIscontrolEnum);
+
+	if(trace)autodiff=true;
+	else{
+		if(autodiff && !iscontrol) autodiff=true;
+		else autodiff=false;
+	}
+	this->constants->SetParam(autodiff,AutodiffIsautodiffEnum);
+	/*}}}*/
+
 	/*Initialize permanent data: */
 	this->my_elements = NULL;
 	this->my_vertices = NULL;
@@ -107,7 +125,7 @@
 	}
 
 	/*checks in debugging mode*/
-	#ifdef _ISSM_DEBUG_
+	#if defined(_ISSM_DEBUG_) && !defined(_HAVE_ADOLC_)
 	if(this->data){
 		for(int i=0;i<MaximumNumberOfDefinitionsEnum;i++){
 			if(this->data[i]){
@@ -272,9 +290,10 @@
 	return this->data[data_enum];
 }
 /*}}}*/
-void IoModel::StartTrace(void){/*{{{*/
+void IoModel::StartTrace(bool trace){/*{{{*/
 
 	bool autodiff = false;
+	bool iscontrol = false;
 	bool keep=false;
 	IssmDouble gcTriggerRatio;
 	IssmDouble gcTriggerMaxSize;
@@ -286,8 +305,9 @@
 	int my_rank=IssmComm::GetRank();
 
 	this->FetchData(&autodiff,AutodiffIsautodiffEnum);
-	if(autodiff){
+	this->FetchData(&iscontrol,InversionIscontrolEnum);
 
+	if(trace || (autodiff && !iscontrol)){
 		#ifdef _HAVE_ADOLC_
 		/*Retrieve parameters: */
 		this->FetchData(&keep,AutodiffKeepEnum);
@@ -310,11 +330,13 @@
 
 }
 /*}}}*/
-void IoModel::DeclareIndependents(void){/*{{{*/
+void IoModel::DeclareIndependents(bool trace,IssmPDouble* X){/*{{{*/
 
 	int  i;
 	bool autodiff = false;
+	bool iscontrol = false;
 	int  num_independent_objects;
+	int  Xcount=0;
 
 	int *names = NULL;
 	int *types = NULL;
@@ -326,7 +348,9 @@
 	for(i=0;i<MaximumNumberOfDefinitionsEnum;i++) this->independents[i]=false;
 
 	this->FetchData(&autodiff,AutodiffIsautodiffEnum);
-	if(autodiff){
+	this->FetchData(&iscontrol,InversionIscontrolEnum);
+	
+	if(trace || (autodiff && !iscontrol)){
 
 		#ifdef _HAVE_ADOLC_
 		/*build dataset made of independent objects:*/
@@ -347,7 +371,7 @@
 				this->independent_objects->AddObject(independent_object);
 
 				/*now go fetch the independent variable: */
-				independent_object->FetchIndependent(this); //supply the pointer to iomodel.
+				independent_object->FetchIndependent(this,&Xcount,X); //supply the pointer to iomodel.
 			}
 			xDelete<int>(names);
 			xDelete<int>(types);
