Index: /issm/trunk/src/c/modules/AverageOntoPartitionx/AverageOntoPartitionx.cpp
===================================================================
--- /issm/trunk/src/c/modules/AverageOntoPartitionx/AverageOntoPartitionx.cpp	(revision 5517)
+++ /issm/trunk/src/c/modules/AverageOntoPartitionx/AverageOntoPartitionx.cpp	(revision 5518)
@@ -19,5 +19,5 @@
 
 			
-void AverageOntoPartitionx(double** average, double* vertex_response, Vertices* vertices, Parameters* parameters,Vec node_partition){
+void AverageOntoPartitionx(double** paverage, Elements* elements, Nodes* nodes, Vertices* vertices, Loads* loads, Materials* materials, Parameters* parameters,double* vertex_response,Vec vertex_cluster_partition){
 
 	int     i,j,k;
@@ -25,12 +25,18 @@
 
 	int     qmu_npart;
-	double *qmu_part_serial = NULL;
+	double *qmu_part_nocluster = NULL;
 	double *qmu_part  = NULL;
-	double* partition=NULL; //serial version of node_partition
+	double* partition=NULL; //serial version of vertex_cluster_partition
 	int     numberofvertices;
 
-	/*First, recover qmu partition of vertices. Careful, do not confuse with node_partition, which is just used to distribute vertices 
+	/*output: */
+	Vec partition_contributions=NULL;
+	Vec partition_areas=NULL;
+	Vec vec_average=NULL;
+	double* average=NULL;
+
+	/*First, recover qmu partition of vertices. Careful, do not confuse with vertex_cluster_partition, which is just used to distribute vertices 
 	 * onto cpus in a cluter: */
-	if(!parameters->FindParam(&qmu_part_serial,&dummy,QmuPartEnum))ISSMERROR(" could not find qmu partition vector");
+	if(!parameters->FindParam(&qmu_part_nocluster,&dummy,QmuPartEnum))ISSMERROR(" could not find qmu partition vector");
 
 	/*Some parameters: */
@@ -38,10 +44,10 @@
 	parameters->FindParam(&qmu_npart,QmuNPartEnum);
 
-	/*serialize nodal partition vector: */
-	VecToMPISerial(&partition,node_partition);
+	/*serialize vertex_cluster_partition: */
+	VecToMPISerial(&partition,vertex_cluster_partition);
 
-	/*Use partition vector to repartition qmu_part_serial, which is ordered in a serial way: */
+	/*Use partition vector to repartition qmu_part_nocluster, which is ordered for use on a serial machine, not a cluster: */
 	qmu_part=(double*)xmalloc(numberofvertices*sizeof(double));
-	for(k=0;k<numberofvertices;k++) qmu_part[(int)(partition[k])]=qmu_part_serial[k];
+	for(k=0;k<numberofvertices;k++) qmu_part[(int)(partition[k])]=qmu_part_nocluster[k];
 
 	/*Ok, now we have a qmu partition (into separate areas) that takes into account the parallelism of the cluster. 
@@ -49,6 +55,37 @@
 	 be a npart sized vector. */
 
-	ISSMERROR(" not supported yet!");
+	/*allocate: */
+	partition_contributions=NewVec(qmu_npart);
+	partition_areas=NewVec(qmu_npart);
+	vec_average=NewVec(qmu_npart);
 
+	/*loop on each element, and add contribution of the element to the partition (surface weighted average): */
+	for(i=0;i<elements->Size();i++){
+		Element* element=(Element*)elements->GetObjectByOffset(i);
+		element->AverageOntoPartition(partition_contributions,partition_areas,vertex_response,qmu_part);
+	}
 
+	/*Assemble: */
+	VecAssemblyBegin(partition_contributions);
+	VecAssemblyEnd(partition_contributions);
+
+	VecAssemblyBegin(partition_areas);
+	VecAssemblyEnd(partition_areas);
+
+	/*We have the partition_areas and the partition_contributions for each partition -> compute the surfae weighted average: */
+	VecPointwiseDivide(vec_average,partition_contributions,partition_areas);
+
+	/*serialize:*/
+	VecToMPISerial(&average,vec_average);
+
+	/*Free ressources:*/
+	xfree((void**)&partition);
+	xfree((void**)&qmu_part_nocluster);
+	xfree((void**)&qmu_part);
+	VecFree(&partition_contributions);
+	VecFree(&partition_areas);
+	VecFree(&vec_average);
+
+	/*Assign output pointers:*/
+	*paverage=average;
 }
Index: /issm/trunk/src/c/modules/AverageOntoPartitionx/AverageOntoPartitionx.h
===================================================================
--- /issm/trunk/src/c/modules/AverageOntoPartitionx/AverageOntoPartitionx.h	(revision 5517)
+++ /issm/trunk/src/c/modules/AverageOntoPartitionx/AverageOntoPartitionx.h	(revision 5518)
@@ -9,5 +9,5 @@
 #include "../../Container/Container.h"
 
-void AverageOntoPartitionx(double** average, double* vertex_response, Vertices* vertices,Parameters* parameters,Vec node_partition);
+void AverageOntoPartitionx(double** average, Elements* elements, Nodes* nodes, Vertices* vertices, Loads* loads, Materials* materials, Parameters* parameters,double* vertex_response,Vec vertex_cluster_partition);
 
 #endif  /* _AVERAGEONTOPARTITIONXX_H */
Index: /issm/trunk/src/c/modules/DakotaResponsesx/DakotaResponsesx.cpp
===================================================================
--- /issm/trunk/src/c/modules/DakotaResponsesx/DakotaResponsesx.cpp	(revision 5517)
+++ /issm/trunk/src/c/modules/DakotaResponsesx/DakotaResponsesx.cpp	(revision 5518)
@@ -18,5 +18,5 @@
 
 
-void DakotaResponsesx(double* d_responses,Elements* elements,Nodes* nodes, Vertices* vertices,Loads* loads,Materials* materials, Parameters* parameters,Vec node_partition,char** responses_descriptors,int numresponsedescriptors,int d_numresponses){
+void DakotaResponsesx(double* d_responses,Elements* elements,Nodes* nodes, Vertices* vertices,Loads* loads,Materials* materials, Parameters* parameters,Vec vertex_cluster_partition,char** responses_descriptors,int numresponsedescriptors,int d_numresponses){
 
 	int        i,j,k;
@@ -31,5 +31,5 @@
 	int    flag;
 	double* vertex_response=NULL;
-	double* partition_response=NULL;
+	double* qmu_response=NULL;
 
 	double* responses_pointer=NULL;
@@ -54,15 +54,18 @@
 
 			/*Now, average it onto the partition grids: */
-			AverageOntoPartitionx(&partition_response,vertex_response,vertices,parameters,node_partition);
+			AverageOntoPartitionx(&qmu_response,elements,nodes,vertices,loads,materials,parameters,vertex_response,vertex_cluster_partition);
 
 			/*Copy onto our dakota responses: */
-			for(i=0;i<npart;i++)responses_pointer[i]=partition_response[i];
+			if(my_rank==0){
+				/*plug response: */
+				for(i=0;i<npart;i++)responses_pointer[i]=qmu_response[i];
+
+				/*increment response_pointer :*/
+				responses_pointer+=npart;
+			}
 
 			/*Free ressources:*/
 			xfree((void**)&vertex_response);
-			xfree((void**)&partition_response);
-
-			/*increment response_pointer :*/
-			responses_pointer+=npart;
+			xfree((void**)&qmu_response);
 
 		}
Index: /issm/trunk/src/c/modules/DakotaResponsesx/DakotaResponsesx.h
===================================================================
--- /issm/trunk/src/c/modules/DakotaResponsesx/DakotaResponsesx.h	(revision 5517)
+++ /issm/trunk/src/c/modules/DakotaResponsesx/DakotaResponsesx.h	(revision 5518)
@@ -9,5 +9,5 @@
 #include "../../Container/Container.h"
 
-void DakotaResponsesx(double* d_responses,Elements* elements,Nodes* nodes, Vertices* vertices,Loads* loads,Materials* materials, Parameters* parameters,Vec node_partition,char** responses_descriptors,int numresponsedescriptors,int d_numresponses);
+void DakotaResponsesx(double* d_responses,Elements* elements,Nodes* nodes, Vertices* vertices,Loads* loads,Materials* materials, Parameters* parameters,Vec vertex_cluster_partition,char** responses_descriptors,int numresponsedescriptors,int d_numresponses);
 
 #endif  /* _DAKOTARESPONSESXX_H */
Index: /issm/trunk/src/c/objects/Elements/Element.h
===================================================================
--- /issm/trunk/src/c/objects/Elements/Element.h	(revision 5517)
+++ /issm/trunk/src/c/objects/Elements/Element.h	(revision 5518)
@@ -78,4 +78,5 @@
 		virtual void   InputControlUpdate(double scalar,bool save_parameter)=0;
 		virtual bool   InputConvergence(double* eps, int* enums,int num_enums,int* criterionenums,double* criterionvalues,int num_criterionenums)=0;
+		virtual void   AverageOntoPartition(Vec partition_contributions,Vec partition_areas,double* vertex_response,double* qmu_part)=0;
 
 		/*Implementation: */
Index: /issm/trunk/src/c/objects/Elements/Penta.cpp
===================================================================
--- /issm/trunk/src/c/objects/Elements/Penta.cpp	(revision 5517)
+++ /issm/trunk/src/c/objects/Elements/Penta.cpp	(revision 5518)
@@ -465,4 +465,9 @@
 
 /*Element virtual functions definitions: */
+/*FUNCTION Penta::AverageOntoPartition {{{1*/
+void  Penta::AverageOntoPartition(Vec partition_contributions,Vec partition_areas,double* vertex_response,double* qmu_part){
+	ISSMERROR("Not supported yet!");
+}
+/*}}}*/
 /*FUNCTION Penta::ComputeBasalStress {{{1*/
 void  Penta::ComputeBasalStress(Vec sigma_b){
Index: /issm/trunk/src/c/objects/Elements/Penta.h
===================================================================
--- /issm/trunk/src/c/objects/Elements/Penta.h	(revision 5517)
+++ /issm/trunk/src/c/objects/Elements/Penta.h	(revision 5518)
@@ -69,4 +69,5 @@
 		/*}}}*/
 		/*Element virtual functions definitions: {{{1*/
+		void   AverageOntoPartition(Vec partition_contributions,Vec partition_areas,double* vertex_response,double* qmu_part);
 		void   ComputeBasalStress(Vec sigma_b);
 		void   ComputePressure(Vec p_g);
Index: /issm/trunk/src/c/objects/Elements/Tria.cpp
===================================================================
--- /issm/trunk/src/c/objects/Elements/Tria.cpp	(revision 5517)
+++ /issm/trunk/src/c/objects/Elements/Tria.cpp	(revision 5518)
@@ -529,4 +529,43 @@
 
 /*Element virtual functions definitions: */
+/*FUNCTION Tria::AverageOntoPartition {{{1*/
+void  Tria::AverageOntoPartition(Vec partition_contributions,Vec partition_areas,double* vertex_response,double* qmu_part){
+
+	int       i,j;
+	const int numvertices=3;
+	double    area;
+	double    mean;
+	int       partition[numvertices];
+	int       offset[numvertices];
+	double    values[3];
+	bool      already=false;
+
+	/*First, get the area: */
+	area=this->GetArea();
+
+	/*Figure out the average for this element: */
+	this->GetDofList1(&offset[0]);
+	mean=0;
+	for(i=0;i<numvertices;i++){
+		partition[i]=(int)qmu_part[offset[i]];
+		mean=mean+1.0/numvertices*vertex_response[offset[i]];
+	}
+
+	/*Add contribution: */
+	for(i=0;i<numvertices;i++){
+		already=false;
+		for(j=0;j<i;j++){
+			if (partition[i]==partition[j]){
+				already=true;
+				break;
+			}
+		}
+		if(!already){
+			VecSetValue(partition_contributions,partition[i],mean*area,ADD_VALUES);
+			VecSetValue(partition_areas,partition[i],area,ADD_VALUES);
+		};
+	}
+}
+/*}}}*/
 /*FUNCTION Tria::ComputeBasalStress {{{1*/
 void  Tria::ComputeBasalStress(Vec eps){
@@ -6378,5 +6417,5 @@
 	x3=xyz_list[2][0]; y3=xyz_list[2][1];
  
-	return x2*y3 - y2*x3 + x1*y2 - y1*x2 + x3*y1 - y3*x1;
+	return (x2*y3 - y2*x3 + x1*y2 - y1*x2 + x3*y1 - y3*x1)/2;
 }
 /*}}}*/
Index: /issm/trunk/src/c/objects/Elements/Tria.h
===================================================================
--- /issm/trunk/src/c/objects/Elements/Tria.h	(revision 5517)
+++ /issm/trunk/src/c/objects/Elements/Tria.h	(revision 5518)
@@ -66,4 +66,5 @@
 		/*}}}*/
 		/*Element virtual functions definitions: {{{1*/
+		void   AverageOntoPartition(Vec partition_contributions,Vec partition_areas,double* vertex_response,double* qmu_part);
 		void   ComputeBasalStress(Vec sigma_b);
 		void   ComputePressure(Vec p_g);
