Index: /issm/trunk/src/c/objects/Elements/Penta.cpp
===================================================================
--- /issm/trunk/src/c/objects/Elements/Penta.cpp	(revision 5851)
+++ /issm/trunk/src/c/objects/Elements/Penta.cpp	(revision 5852)
@@ -2141,5 +2141,7 @@
 
 	Tria* tria=(Tria*)SpawnTria(0,1,2); //grids 0, 1 and 2 make the new tria.
-	tria->CreateKMatrixCouplingMacAyealPattynFriction(Kgg);
+	ElementMatrix* Ke=tria->CreateKMatrixCouplingMacAyealPattynFriction();
+	if(Ke) Ke->AddToGlobal(Kgg,NULL,NULL);
+	delete Ke;
 	delete tria->matice; delete tria;
 }
Index: /issm/trunk/src/c/objects/Elements/Tria.cpp
===================================================================
--- /issm/trunk/src/c/objects/Elements/Tria.cpp	(revision 5851)
+++ /issm/trunk/src/c/objects/Elements/Tria.cpp	(revision 5852)
@@ -2860,50 +2860,37 @@
 /*}}}*/
 /*FUNCTION Tria::CreateKMatrixCouplingMacAyealPattynFriction {{{1*/
-void  Tria::CreateKMatrixCouplingMacAyealPattynFriction(Mat Kgg){
-
-	/* local declarations */
+ElementMatrix* Tria::CreateKMatrixCouplingMacAyealPattynFriction(void){
+
+	const int numdof   = 2 *NUMVERTICES;
 	int       i,j;
+	int     ig;
 	int       analysis_type;
-
-	/* node data: */
-	const int numdof   = 2 *NUMVERTICES;
 	double    xyz_list[NUMVERTICES][3];
-	int*      doflistm=NULL;
-	int*      doflistp=NULL;
 	int       numberofdofspernode=2;
-
-	/* gaussian points: */
-	int     ig;
 	GaussTria *gauss=NULL;
-
-	/* matrices: */
 	double L[2][numdof];
 	double DL[2][2]={{ 0,0 },{0,0}}; //for basal drag
 	double DL_scalar;
-
-	/* local element matrices: */
 	double Ke_gg[numdof][numdof]={0.0};
 	double Ke_gg_gaussian[numdof][numdof]; //stiffness matrix contribution from drag
-	
 	double Jdet;
-	
-	/*slope: */
 	double  slope[2]={0.0,0.0};
 	double  slope_magnitude;
-
-	/*friction: */
 	Friction *friction = NULL;
 	double    alpha2;
-
 	double MAXSLOPE=.06; // 6 %
 	double MOUNTAINKEXPONENT=10;
-
-	/*inputs: */
 	int  drag_type;
 
-	/*retrive parameters: */
+	/*Initialize Element matrix and return if necessary*/
+	if(IsOnWater() || IsOnShelf()) return NULL;
+	ElementMatrix* Ke1=this->NewElementMatrix(MacAyealApproximationEnum);
+	ElementMatrix* Ke2=this->NewElementMatrix(PattynApproximationEnum);
+	ElementMatrix* Ke=new ElementMatrix(Ke1,Ke2);
+	delete Ke1; delete Ke2;
+
+	/*retrieve inputs :*/
+	GetVerticesCoordinates(&xyz_list[0][0], nodes, NUMVERTICES);
 	parameters->FindParam(&analysis_type,AnalysisTypeEnum);
-
-	/*retrieve inputs :*/
 	inputs->GetParameterValue(&drag_type,DragTypeEnum);
 	Input* surface_input=inputs->GetInput(SurfaceEnum); ISSMASSERT(surface_input);
@@ -2911,14 +2898,4 @@
 	Input* vy_input=inputs->GetInput(VyEnum);           ISSMASSERT(vy_input);
 	Input* vz_input=inputs->GetInput(VzEnum);           ISSMASSERT(vz_input);
-	
-	/* Get node coordinates and dof list: */
-	GetVerticesCoordinates(&xyz_list[0][0], nodes, NUMVERTICES);
-	GetDofList(&doflistm,MacAyealApproximationEnum,GsetEnum);
-	GetDofList(&doflistp,PattynApproximationEnum,GsetEnum);
-
-	if (IsOnShelf()){
-		/*no friction, do nothing*/
-		return;
-	}
 
 	/*build friction object, used later on: */
@@ -2962,17 +2939,15 @@
 					&Ke_gg_gaussian[0][0],0);
 
-		for( i=0; i<numdof; i++) for(j=0;j<numdof;j++) Ke_gg[i][j]+=Ke_gg_gaussian[i][j];
-
-	}
-
-	/*Add Ke_gg to global matrix Kgg: */
-	MatSetValues(Kgg,numdof,doflistm,numdof,doflistp,(const double*)Ke_gg,ADD_VALUES);
-	MatSetValues(Kgg,numdof,doflistp,numdof,doflistm,(const double*)Ke_gg,ADD_VALUES);
+		for(i=0;i<numdof;i++) for(j=0;j<numdof;j++) Ke_gg[i][j]+=Ke_gg_gaussian[i][j];
+	}
+
+
+	for(i=0;i<numdof;i++) for(j=0;j<numdof;j++) Ke->values[i*numdof+(numdof+j)]+=Ke_gg[i][j];
+	for(i=0;i<numdof;i++) for(j=0;j<numdof;j++) Ke->values[(i+numdof)*numdof+j]+=Ke_gg[i][j];
 
 	/*Clean up and return*/
 	delete gauss;
 	delete friction;
-	xfree((void**)&doflistm);
-	xfree((void**)&doflistp);
+	return Ke;
 }	
 /*}}}*/
Index: /issm/trunk/src/c/objects/Elements/Tria.h
===================================================================
--- /issm/trunk/src/c/objects/Elements/Tria.h	(revision 5851)
+++ /issm/trunk/src/c/objects/Elements/Tria.h	(revision 5852)
@@ -129,5 +129,5 @@
 		ElementMatrix* CreateKMatrixDiagnosticMacAyealFriction(void);
 		void    CreateKMatrixDiagnosticMacAyealFriction(Mat Kgg,Mat Kff, Mat Kfs);
-		void	  CreateKMatrixCouplingMacAyealPattynFriction(Mat Kgg);
+		ElementMatrix* CreateKMatrixCouplingMacAyealPattynFriction(void);
 		ElementMatrix* CreateKMatrixDiagnosticPattynFriction(void);
 		ElementMatrix* CreateKMatrixDiagnosticHutter(void);
