Index: ../trunk-jpl/src/c/solutionsequences/solutionsequence_fct.cpp
===================================================================
--- ../trunk-jpl/src/c/solutionsequences/solutionsequence_fct.cpp	(revision 18354)
+++ ../trunk-jpl/src/c/solutionsequences/solutionsequence_fct.cpp	(revision 18355)
@@ -193,13 +193,11 @@
 	*pudot=udot;
 
 }/*}}}*/
-void CreateRis(IssmDouble** pRi_plus_serial,IssmDouble** pRi_minus_serial,Mat Mc,Mat D,IssmDouble* ml_serial,Vec uvec,IssmDouble* u,IssmDouble* udot,IssmDouble deltat){/*{{{*/
+void CreateRis(IssmDouble** pRi_plus_serial,IssmDouble** pRi_minus_serial,Mat Mc,Mat D,IssmDouble* ml_serial,Vec uvec,IssmDouble* u,IssmDouble* udot,IssmDouble* ulmin,IssmDouble* ulmax,IssmDouble deltat){/*{{{*/
 
 	/*Intermediaries*/
 	Vec         Ri_plus  = NULL;
 	Vec         Ri_minus = NULL;
-	IssmDouble  uiLmax   = 3.;
-	IssmDouble  uiLmin   = 2.;
 	int         ncols,ncols2,rstart,rend;
 	double      d;
 	int        *cols     = NULL;
@@ -233,8 +231,10 @@
 		}
 
 		/*Compute Qis and Ris*/
-		double Qi_plus  = ml_serial[row]/deltat*(uiLmax - u[row]);
-		double Qi_minus = ml_serial[row]/deltat*(uiLmin - u[row]);
+		double Qi_plus  = ml_serial[row]/deltat*(3. - u[row]);
+		double Qi_minus = ml_serial[row]/deltat*(2. - u[row]);
+		//double Qi_plus  = ml_serial[row]/deltat*(ulmax[row] - u[row]);
+		//double Qi_minus = ml_serial[row]/deltat*(ulmin[row] - u[row]);
 		d = 1.;
 		if(Pi_plus!=0.) d = min(1.,Qi_plus/Pi_plus);
 		VecSetValue(Ri_plus,row,(const double)d,INSERT_VALUES);
@@ -302,119 +302,124 @@
 	/*Assign output pointer*/
 	*pFbar = Fbar;
 }/*}}}*/
-#endif
-	void solutionsequence_fct(FemModel* femmodel){
+void UpdateSolution(Vec u,Vec udot,Vec Ml,Vec Fbar,IssmDouble deltat){/*{{{*/
 
-		/*intermediary: */
-		Vector<IssmDouble>*  Ml = NULL;
-		Matrix<IssmDouble>*  K  = NULL;
-		Matrix<IssmDouble>*  Mc = NULL;
-		Vector<IssmDouble>*  ug = NULL;
-		Vector<IssmDouble>*  uf = NULL;
+	/*Intermediary*/
+	Vec temp1 = NULL;
 
-		IssmDouble theta,deltat,dmax;
-		int        dof,ncols,ncols2,rstart,rend;
-		int        configuration_type;
-		double     d;
-		int*       cols  = NULL;
-		int*       cols2 = NULL;
-		double*    vals  = NULL;
-		double*    vals2 = NULL;
+	/*Compute solution u^n+1 = u_L + deltat Ml^-1 fbar*/
+	VecDuplicate(u,&temp1);
+	VecPointwiseDivide(temp1,Fbar,Ml); //temp1 = Ml^-1 temp2
+	VecAXPBY(udot,1.,1.,temp1);
+	VecAXPY(u,deltat,temp1);
 
-		/*Create analysis*/
-		MasstransportAnalysis* analysis = new MasstransportAnalysis();
+	/*CLean up and return*/
+	VecFree(&temp1);
+}/*}}}*/
+#endif
+void solutionsequence_fct(FemModel* femmodel){
 
-		/*Recover parameters: */
-		femmodel->parameters->FindParam(&deltat,TimesteppingTimeStepEnum);
-		femmodel->parameters->FindParam(&configuration_type,ConfigurationTypeEnum);
-		femmodel->UpdateConstraintsx();
-		theta = 0.5;
+	/*intermediary: */
+	IssmDouble           theta,deltat,dmax;
+	int                  configuration_type;
+	Vector<IssmDouble>*  Ml = NULL;
+	Matrix<IssmDouble>*  K  = NULL;
+	Matrix<IssmDouble>*  Mc = NULL;
+	Vector<IssmDouble>*  ug = NULL;
+	Vector<IssmDouble>*  uf = NULL;
 
-		/*Create lumped mass matrix*/
-		analysis->LumpedMassMatrix(&Ml,femmodel);
-		analysis->MassMatrix(&Mc,femmodel);
-		analysis->FctKMatrix(&K,NULL,femmodel);
+	/*Create analysis*/
+	MasstransportAnalysis* analysis = new MasstransportAnalysis();
 
-		/*Convert matrices to PETSc matrices*/
-		Mat D_petsc  = NULL;
-		Mat LHS      = NULL;
-		Vec RHS      = NULL;
-		Vec u        = NULL;
-		Vec udot     = NULL;
-		Mat K_petsc  = K->pmatrix->matrix;
-		Vec Ml_petsc = Ml->pvector->vector;
-		Mat Mc_petsc = Mc->pmatrix->matrix;
+	/*Recover parameters: */
+	femmodel->parameters->FindParam(&deltat,TimesteppingTimeStepEnum);
+	femmodel->parameters->FindParam(&configuration_type,ConfigurationTypeEnum);
+	femmodel->UpdateConstraintsx();
+	theta = 0.5;
 
-		/*Create D Matrix*/
-		#ifdef _HAVE_PETSC_
-		CreateDMatrix(&D_petsc,K_petsc);
+	/*Create lumped mass matrix*/
+	analysis->LumpedMassMatrix(&Ml,femmodel);
+	analysis->MassMatrix(&Mc,femmodel);
+	analysis->FctKMatrix(&K,NULL,femmodel);
+	delete analysis;
 
-		/*Create LHS: [ML − theta*detlat *(K+D)^n+1]*/
-		CreateLHS(&LHS,&dmax,K_petsc,D_petsc,Ml_petsc,theta,deltat,femmodel,configuration_type);
+	/*Convert matrices to PETSc matrices*/
+	Mat D_petsc  = NULL;
+	Mat LHS      = NULL;
+	Vec RHS      = NULL;
+	Vec u        = NULL;
+	Vec udot     = NULL;
+	Mat K_petsc  = K->pmatrix->matrix;
+	Vec Ml_petsc = Ml->pvector->vector;
+	Mat Mc_petsc = Mc->pmatrix->matrix;
 
-		/*Get previous solution u^n*/
-		GetSolutionFromInputsx(&ug,femmodel);
-		Reducevectorgtofx(&uf, ug, femmodel->nodes,femmodel->parameters);
-		delete ug;
+	/*Create D Matrix*/
+	#ifdef _HAVE_PETSC_
+	CreateDMatrix(&D_petsc,K_petsc);
 
-		/*Create RHS: [ML + (1 − theta) deltaT L^n] u^n */
-		CreateRHS(&RHS,K_petsc,D_petsc,Ml_petsc,uf->pvector->vector,theta,deltat,dmax,femmodel,configuration_type);
-		delete uf;
+	/*Create LHS: [ML − theta*detlat *(K+D)^n+1]*/
+	CreateLHS(&LHS,&dmax,K_petsc,D_petsc,Ml_petsc,theta,deltat,femmodel,configuration_type);
 
-		/*Go solve lower order solution*/
-		SolverxPetsc(&u,LHS,RHS,NULL,NULL, femmodel->parameters); 
-		MatFree(&LHS);
-		VecFree(&RHS);
+	/*Get previous solution u^n*/
+	GetSolutionFromInputsx(&ug,femmodel);
+	Reducevectorgtofx(&uf, ug, femmodel->nodes,femmodel->parameters);
+	delete ug;
 
-		/*Richardson to calculate udot*/
-		RichardsonUdot(&udot,u,Ml_petsc,K_petsc,Mc_petsc);
+	/*Create RHS: [ML + (1 − theta) deltaT L^n] u^n */
+	CreateRHS(&RHS,K_petsc,D_petsc,Ml_petsc,uf->pvector->vector,theta,deltat,dmax,femmodel,configuration_type);
+	delete uf;
 
-		/*Serialize u and udot*/
-		IssmDouble* udot_serial = NULL;
-		IssmDouble* u_serial    = NULL;
-		IssmDouble* ml_serial   = NULL;
-		VecToMPISerial(&udot_serial,udot    ,IssmComm::GetComm());
-		VecToMPISerial(&u_serial   ,u       ,IssmComm::GetComm());
-		VecToMPISerial(&ml_serial  ,Ml_petsc,IssmComm::GetComm());
+	/*Go solve lower order solution*/
+	SolverxPetsc(&u,LHS,RHS,NULL,NULL, femmodel->parameters); 
+	MatFree(&LHS);
+	VecFree(&RHS);
 
-		/*Anti diffusive fluxes*/
-		Vec         Fbar            = NULL;
-		IssmDouble* Ri_minus_serial = NULL;
-		IssmDouble* Ri_plus_serial  = NULL;
-		CreateRis(&Ri_plus_serial,&Ri_minus_serial,Mc_petsc,D_petsc,ml_serial,u,u_serial,udot_serial,deltat);
-		CreateFbar(&Fbar,Ri_plus_serial,Ri_minus_serial,Mc_petsc,D_petsc,udot_serial,u_serial,u);
+	/*Richardson to calculate udot*/
+	RichardsonUdot(&udot,u,Ml_petsc,K_petsc,Mc_petsc);
+	delete K;
 
-		/*Clean up*/
-		MatFree(&D_petsc);
-		delete Mc;
-		xDelete<IssmDouble>(udot_serial);
-		xDelete<IssmDouble>(u_serial);
-		xDelete<IssmDouble>(ml_serial);
-		xDelete<IssmDouble>(Ri_plus_serial);
-		xDelete<IssmDouble>(Ri_minus_serial);
+	/*Serialize u and udot*/
+	IssmDouble* udot_serial = NULL;
+	IssmDouble* u_serial    = NULL;
+	IssmDouble* ml_serial   = NULL;
+	VecToMPISerial(&udot_serial,udot    ,IssmComm::GetComm());
+	VecToMPISerial(&u_serial   ,u       ,IssmComm::GetComm());
+	VecToMPISerial(&ml_serial  ,Ml_petsc,IssmComm::GetComm());
 
-		/*Compute solution u^n+1 = u_L + deltat Ml^-1 fbar*/
-		Vec temp1 = NULL;
-		VecDuplicate(u,&temp1);
-		VecPointwiseDivide(temp1,Fbar,Ml_petsc); //temp1 = Ml^-1 temp2
-		VecAXPBY(udot,1.,1.,temp1);
-		VecAXPY(u,deltat,temp1);
-		VecFree(&Fbar);
-		VecFree(&udot);
-		VecFree(&temp1);
+	/*Anti diffusive fluxes*/
+	Vec         Fbar            = NULL;
+	IssmDouble *Ri_minus_serial = NULL;
+	IssmDouble *Ri_plus_serial  = NULL;
+	IssmDouble *ulmin           = NULL;
+	IssmDouble *ulmax           = NULL;
+	femmodel->GetInputLocalMinMaxOnNodesx(&ulmin,&ulmax,ThicknessEnum);
+	CreateRis(&Ri_plus_serial,&Ri_minus_serial,Mc_petsc,D_petsc,ml_serial,u,u_serial,udot_serial,ulmin,ulmax,deltat);
+	CreateFbar(&Fbar,Ri_plus_serial,Ri_minus_serial,Mc_petsc,D_petsc,udot_serial,u_serial,u);
+	xDelete<IssmDouble>(Ri_plus_serial);
+	xDelete<IssmDouble>(Ri_minus_serial);
+	xDelete<IssmDouble>(ulmin);
+	xDelete<IssmDouble>(ulmax);
 
-		uf =new Vector<IssmDouble>(u);
-		VecFree(&u);
+	/*Clean up*/
+	MatFree(&D_petsc);
+	delete Mc;
+	xDelete<IssmDouble>(udot_serial);
+	xDelete<IssmDouble>(u_serial);
+	xDelete<IssmDouble>(ml_serial);
 
-		InputUpdateFromSolutionx(femmodel,uf); 
-		delete uf;
+	/*Compute solution u^n+1 = u_L + deltat Ml^-1 fbar*/
+	UpdateSolution(u,udot,Ml_petsc,Fbar,deltat);
+	uf =new Vector<IssmDouble>(u);
+	VecFree(&u);
+	VecFree(&Fbar);
+	VecFree(&udot);
+	delete Ml;
 
-		#else
-		_error_("PETSc needs to be installed");
-		#endif
+	/*Update Element inputs*/
+	InputUpdateFromSolutionx(femmodel,uf); 
+	delete uf;
 
-		delete Ml;
-		delete K;
-		delete analysis;
-
-	}
+	#else
+	_error_("PETSc needs to be installed");
+	#endif
+}
