Index: /issm/trunk/src/c/modules/ContourToNodesx/ContourToNodesx.cpp
===================================================================
--- /issm/trunk/src/c/modules/ContourToNodesx/ContourToNodesx.cpp	(revision 3902)
+++ /issm/trunk/src/c/modules/ContourToNodesx/ContourToNodesx.cpp	(revision 3902)
@@ -0,0 +1,39 @@
+/*! \file  ContourToNodesx.c
+ */
+
+#include "./ContourToNodesx.h"
+
+int ContourToNodesx( Vec* pflags,double* x, double* y, int nods, Contour** contours,int numcontours,int edgevalue){
+
+	int i;
+	int m,n;
+
+	/*Contour:*/
+	Contour* contouri=NULL;
+	int      numgrids;
+	double*  xc=NULL;
+	double*  yc=NULL;
+	double   value;
+
+	/*output: */
+	Vec flags=NULL;
+
+	flags=NewVec(nods);
+
+	/*Loop through all contours: */
+	for (i=0;i<numcontours;i++){
+		contouri=*(contours+i);
+		numgrids=contouri->nods;
+		xc=contouri->x;
+		yc=contouri->y;
+		IsInPoly(flags,xc,yc,numgrids,x,y,0,nods,edgevalue);
+	}
+
+	/*Assemble vector: */
+	VecAssemblyBegin(flags);
+	VecAssemblyEnd(flags);
+
+	/*Assign output pointers: */
+	*pflags=flags;
+
+}
Index: /issm/trunk/src/c/modules/ContourToNodesx/ContourToNodesx.h
===================================================================
--- /issm/trunk/src/c/modules/ContourToNodesx/ContourToNodesx.h	(revision 3902)
+++ /issm/trunk/src/c/modules/ContourToNodesx/ContourToNodesx.h	(revision 3902)
@@ -0,0 +1,16 @@
+/*
+	ContourToNodesx.h
+*/
+
+
+#ifndef _CONTOURTONODESX_H
+#define _CONTOURTONODESX_H
+
+#include "../shared/shared.h"
+#include "../objects/objects.h"
+
+/* local prototypes: */
+int ContourToNodesx( Vec* pflags,double* x, double* y, int nods, Contour** contours,int numcontours,int edgevalue);
+
+#endif /* _CONTOURTONODESX_H */
+
Index: /issm/trunk/src/c/modules/ControlConstrainx/ControlConstrainx.cpp
===================================================================
--- /issm/trunk/src/c/modules/ControlConstrainx/ControlConstrainx.cpp	(revision 3902)
+++ /issm/trunk/src/c/modules/ControlConstrainx/ControlConstrainx.cpp	(revision 3902)
@@ -0,0 +1,33 @@
+/*!\file ControlConstrainx
+ * \brief constrain optimization parameters during inverse method
+ */
+
+#include "./ControlConstrainx.h"
+
+#include "../shared/shared.h"
+#include "../include/include.h"
+#include "../toolkits/toolkits.h"
+#include "../EnumDefinitions/EnumDefinitions.h"
+
+void ControlConstrainx( double* p_g, int numdofnodes, double cm_min, double cm_max,int control_type){
+
+	int i;
+	
+	if(isnan(cm_min) & isnan(cm_max)){
+		/*do nothing*/
+	}
+	else{
+		for(i=0;i<numdofnodes;i++){
+			if(isnan(p_g[i])){
+				ISSMERROR("NaN found in parameter p_g[%i]",i);
+			}
+			if(!isnan(cm_min)){
+				if (p_g[i]<cm_min)p_g[i]=cm_min;
+			}
+			if(!isnan(cm_max)){
+				if (p_g[i]>cm_max)p_g[i]=cm_max;
+			}
+		}
+	}
+		
+}
Index: /issm/trunk/src/c/modules/ControlConstrainx/ControlConstrainx.h
===================================================================
--- /issm/trunk/src/c/modules/ControlConstrainx/ControlConstrainx.h	(revision 3902)
+++ /issm/trunk/src/c/modules/ControlConstrainx/ControlConstrainx.h	(revision 3902)
@@ -0,0 +1,14 @@
+/*!\file:  ControlConstrainx.h
+ * \brief constrain optimization parameters during inverse method
+ */ 
+
+#ifndef _CONTROLCONSTRAINX_H
+#define _CONTROLCONSTRAINX_H
+
+#include "../DataSet/DataSet.h"
+
+/* local prototypes: */
+void ControlConstrainx( double* p_g, int numberofnodes, double cm_min, double cm_max,int control_type);
+
+#endif  /* _CONTROLCONSTRAINX_H */
+
Index: /issm/trunk/src/c/modules/Dofx/Dofx.cpp
===================================================================
--- /issm/trunk/src/c/modules/Dofx/Dofx.cpp	(revision 3902)
+++ /issm/trunk/src/c/modules/Dofx/Dofx.cpp	(revision 3902)
@@ -0,0 +1,77 @@
+/*!\file Dofx
+ * \brief: establish degrees of freedom for all nodes
+ */
+
+#include "./Dofx.h"
+
+#include "../shared/shared.h"
+#include "../include/include.h"
+#include "../toolkits/toolkits.h"
+#include "../EnumDefinitions/EnumDefinitions.h"
+
+int Dofx( DofVec** ppartition, DofVec** ptpartition, DataSet* elements,DataSet* nodes, DataSet* vertices, Parameters* parameters) {
+
+	int noerr=1;
+	int i;
+
+	int  found=0;
+	extern int num_procs;
+	extern int my_rank;
+	
+	/*intermediary: */
+	int  numberofnodes;
+	int  numberofvertices;
+	int  numberofdofspernode;
+
+	/*output: */
+	DofVec* partition=NULL;
+	DofVec* tpartition=NULL;
+
+	/*Check that vertices and nodes are not empty*/
+	ISSMASSERT(nodes && vertices);
+
+	/*Initialize dofvecs: */
+	partition=new DofVec("partition");
+	tpartition=new DofVec("tpartition");
+
+	/*First, recover number of vertices and nodes from parameters: */
+	found=parameters->FindParam(&numberofvertices,NumberOfVerticesEnum);
+	if(!found)ISSMERROR(" could not find numberofvertices in parameters");
+	
+	found=parameters->FindParam(&numberofnodes,NumberOfNodesEnum);
+	if(!found)ISSMERROR(" could not find numberofnodes in parameters");
+
+	/*Recover number of dofs per node: */
+	found=parameters->FindParam(&numberofdofspernode,NumberOfDofsPerNodeEnum);
+	if(!found)ISSMERROR(" could not find numberofdofspernode in parameters");
+
+	/*Ensure that only for each cpu, the partition border nodes only will be taken into account once 
+	 * across the cluster. To do so, we flag all the clone nodes: */
+	vertices->FlagClones(numberofvertices);
+	nodes->FlagClones(numberofnodes);
+
+	/*Go through all vertices and nodes, and build degree of freedom lists. Each vertex/node gets a fixed number of dofs. When 
+	 *a  vertex/node has already been distributed dofs on one cpu, all other cpus with the same vertex/node cannot distribute it 
+	 *anymore. Use clone field to be sure of that: */
+	vertices->DistributeDofs(numberofvertices,1); //only 1 dof per vertex.
+	nodes->DistributeDofs(numberofnodes,numberofdofspernode);
+
+	/*Now that dofs have been distributed, create partitioning vector and its transpose: */
+	vertices->CreatePartitioningVector(&partition->vector,numberofvertices);
+
+	/*Transpose partition into tpartition: */
+	VecTranspose(&tpartition->vector,partition->vector);
+
+	/*Now, setup numdof and numentries for dofvec: */
+	partition->numdofs=1;
+	VecGetSize(partition->vector,&partition->numentries);
+
+	tpartition->numdofs=1;
+	VecGetSize(tpartition->vector,&tpartition->numentries);
+
+	/*Assign output pointers: */
+	*ppartition=partition;
+	*ptpartition=tpartition;
+	
+	return noerr;
+}
Index: /issm/trunk/src/c/modules/Dofx/Dofx.h
===================================================================
--- /issm/trunk/src/c/modules/Dofx/Dofx.h	(revision 3902)
+++ /issm/trunk/src/c/modules/Dofx/Dofx.h	(revision 3902)
@@ -0,0 +1,15 @@
+/*!\file:  Dofx.h
+ * \brief header file for degree of freedoms distribution routines.
+ */ 
+
+#ifndef _DOFX_H
+#define _DOFX_H
+
+#include "../DataSet/DataSet.h"
+#include "../objects/objects.h"
+
+/* local prototypes: */
+int		Dofx( DofVec** partition, DofVec** ptpartition,DataSet* elements,DataSet* nodesin, DataSet* verticesin, Parameters* parameters);
+
+#endif  /* _DOFX_H */
+
Index: /issm/trunk/src/c/modules/Dux/Dux.cpp
===================================================================
--- /issm/trunk/src/c/modules/Dux/Dux.cpp	(revision 3902)
+++ /issm/trunk/src/c/modules/Dux/Dux.cpp	(revision 3902)
@@ -0,0 +1,53 @@
+/*!\file Dux
+ * \brief: diff between observed and modeled velocity
+ */
+
+#include "./Dux.h"
+
+#include "../shared/shared.h"
+#include "../include/include.h"
+#include "../toolkits/toolkits.h"
+#include "../EnumDefinitions/EnumDefinitions.h"
+#include "../SurfaceAreax/SurfaceAreax.h"
+
+void Dux( Vec* pdu_g, DataSet* elements,DataSet* nodes, DataSet* vertices, DataSet* loads, DataSet* materials, Parameters* parameters,
+			int analysis_type,int sub_analysis_type){
+
+	/*Intermediary*/
+	int i;
+	int gsize;
+	int found;
+	double fit=-1;
+	double S;
+
+	/*output: */
+	Vec du_g=NULL;
+
+	/*First, get elements and loads configured: */
+	elements->Configure(elements,loads, nodes, vertices, materials,parameters);
+	nodes->Configure(elements,loads, nodes, vertices, materials,parameters);
+	parameters->Configure(elements,loads, nodes, vertices, materials,parameters);
+
+	/*Compute surface area: */
+	SurfaceAreax(&S,elements,nodes,vertices, loads,materials,parameters,analysis_type,sub_analysis_type);
+
+	/*add surface area to elements :*/
+	elements->UpdateInputsFromVector(&S,SurfaceAreaEnum,ConstantEnum);
+	
+	/*Get size of matrix: */
+	gsize=nodes->NumberOfDofs();
+
+	/*Allocate du_g: */
+	du_g=NewVec(gsize);
+
+	/*Compute velocity difference: */
+	elements->Du(du_g,analysis_type,sub_analysis_type);
+
+	/*Assemble vector: */
+	VecAssemblyBegin(du_g);
+	VecAssemblyEnd(du_g);
+
+	/*Assign output pointers: */
+	*pdu_g=du_g;
+	
+}
Index: /issm/trunk/src/c/modules/Dux/Dux.h
===================================================================
--- /issm/trunk/src/c/modules/Dux/Dux.h	(revision 3902)
+++ /issm/trunk/src/c/modules/Dux/Dux.h	(revision 3902)
@@ -0,0 +1,16 @@
+/*!\file:  Dux.h
+ * \brief header file for degree of freedoms distribution routines.
+ */ 
+
+#ifndef _DUX_H
+#define _DUX_H
+
+#include "../objects/objects.h"
+#include "../DataSet/DataSet.h"
+
+/* local prototypes: */
+void Dux( Vec* pdu_g, DataSet* elements,DataSet* nodes, DataSet* vertices, DataSet* loads, DataSet* materials, Parameters* parameters, 
+			int analysis_type,int sub_analysis_type);
+
+#endif  /* _DUX_H */
+
Index: /issm/trunk/src/c/modules/ElementConnectivityx/ElementConnectivityx.cpp
===================================================================
--- /issm/trunk/src/c/modules/ElementConnectivityx/ElementConnectivityx.cpp	(revision 3902)
+++ /issm/trunk/src/c/modules/ElementConnectivityx/ElementConnectivityx.cpp	(revision 3902)
@@ -0,0 +1,97 @@
+/*!\file ElementConnectivityx
+ * \brief: compute element connectivity table, using node connectivity table and elements.
+ *
+ * For each element, we want to know which neighbouring elements it connects to (fully, via an entire segment, not by a node).
+ * We use the nodeconnectivity to speed up the computation. The nodeconnectivity gives us for each node of the element, 
+ * all the neighbouring elements of this node, which are good candidates to be neighbours of the element itself.
+ * For now, only triangular elements, ie 3 neighbours max per element.
+ */
+
+#include "./ElementConnectivityx.h"
+
+#include "../shared/shared.h"
+#include "../include/include.h"
+#include "../toolkits/toolkits.h"
+#include "../EnumDefinitions/EnumDefinitions.h"
+
+int hascommondedge(double* element1,double* element2);
+
+void	ElementConnectivityx( double** pelementconnectivity, double* elements, int nel, double* nodeconnectivity, int nods, int width){
+
+	int i,j,k,n;
+
+	/*intermediary: */
+	int    maxels;
+	double element;
+	double connectedelement;
+	int    connectedelementindex;
+	int    node;
+	int    index;
+	int    num_elements;
+
+	/*output: */
+	double* elementconnectivity=NULL;
+
+	/*maxels: */
+	maxels=width-1;
+	/*Allocate connectivity: */
+	elementconnectivity=(double*)xcalloc(nel*3,sizeof(double));
+
+	/*Go through all elements, and for each element, go through its nodes, to get the neighbouring elements. 
+	 * Once we get the neighbouring elements, figure out if they share a segment with the current element. If so, 
+	 * plug them in the connectivity, unless they are already there.: */
+	
+	for(n=0;n<nel;n++){
+
+		element=(double)(n+1); //matlab indexing
+
+		for(i=0;i<3;i++){
+		
+			node=(int)*(elements+n*3+i); //already matlab indexed, elements comes directly from the workspace.
+			index=node-1;
+
+			num_elements=(int)*(nodeconnectivity+width*index+maxels); //retrieve number of elements already  plugged into the connectivity of this node.
+
+			for(j=0;j<num_elements;j++){
+
+				/*for each element connected to node, figure out if it has a commond edge with element: */
+				connectedelement=*(nodeconnectivity+width*index+j);
+				connectedelementindex=(int)(connectedelement-1); //go from matlab indexing to c indexing.
+				
+				if(hascommondedge(elements+n*3+0,elements+connectedelementindex*3+0)){
+					/*Ok, this connected element has a commond edge  with element, plug it into elementconnectivity, unless 
+					 *it is already there: */
+
+					for(k=0;k<3;k++){
+						if (*(elementconnectivity+3*n+k)==0){
+							*(elementconnectivity+3*n+k)=connectedelement;
+							break;
+						}
+						else{
+							if(connectedelement==*(elementconnectivity+3*n+k))break;
+						}
+					}
+				}
+			}
+		}
+	}
+
+	/*Assign output pointers: */
+	*pelementconnectivity=elementconnectivity;
+}
+				
+
+int hascommondedge(double* element1,double* element2){
+
+	int i,j;
+	int count;
+
+	count=0;
+	for(i=0;i<3;i++){
+		for(j=0;j<3;j++){
+			if (*(element1+i)==*(element2+j))count++;
+		}
+	}
+	if (count==2)return 1;
+	else return 0;
+}
Index: /issm/trunk/src/c/modules/ElementConnectivityx/ElementConnectivityx.h
===================================================================
--- /issm/trunk/src/c/modules/ElementConnectivityx/ElementConnectivityx.h	(revision 3902)
+++ /issm/trunk/src/c/modules/ElementConnectivityx/ElementConnectivityx.h	(revision 3902)
@@ -0,0 +1,12 @@
+/*!\file:  ElementConnectivityx.h
+ * \brief header file for element connectivity computation
+ */ 
+
+#ifndef _ELEMENTCONNECTIVITYX_H
+#define _ELEMENTCONNECTIVITYX_H
+
+/* local prototypes: */
+void	ElementConnectivityx( double** pelementconnectivity, double* elements, int nel, double* nodeconnectivity, int nods, int width);
+
+#endif  /* _ELEMENTCONNECTIVITYX_H */
+
