/*
 * CreateConstraintsDiagnosticHoriz.c:
 */

#undef __FUNCT__ 
#define __FUNCT__ "CreateConstraintsDiagnosticHoriz"

#include "../DataSet/DataSet.h"
#include "../toolkits/toolkits.h"
#include "../EnumDefinitions/EnumDefinitions.h"
#include "../objects/objects.h"
#include "../shared/shared.h"
#include "./ModelProcessorx.h"


int	CreateConstraintsDiagnosticHoriz(DataSet** pconstraints, Model* model,ConstDataHandle model_handle){


	int noerr=1;
	int i;
	int count;
	
	DataSet* constraints = NULL;
	Spc*    spc  = NULL;

	/*spc intermediary data: */
	int spc_sid;
	int spc_node;
	int spc_dof;
	double spc_value;
	
	double* dirichletvalues_diag=NULL;
	double* gridondirichlet_diag=NULL;
	
	/*Create constraints: */
	constraints = new DataSet(ConstraintsEnum());


	/*Fetch data: */
	ModelFetchData((void**)&gridondirichlet_diag,NULL,NULL,model_handle,"gridondirichlet_diag","Matrix","Mat");
	ModelFetchData((void**)&dirichletvalues_diag,NULL,NULL,model_handle,"dirichletvalues_diag","Matrix","Mat");

	count=0;

	/*Create spcs from x,y,z, as well as the spc values on those spcs: */
	for (i=0;i<model->numberofnodes;i++){
	#ifdef _PARALLEL_
	/*keep only this partition's nodes:*/
	if((model->my_grids[i]==1)){
	#endif

		if ((int)gridondirichlet_diag[i]){
	
			/*This grid needs to be spc'd to vx_obs and vy_obs:*/

			spc_sid=count;
			spc_node=i+1;
			spc_dof=1; //we enforce first x translation degree of freedom
			spc_value=*(dirichletvalues_diag+2*i+0)/model->yts;

			spc = new Spc(spc_sid,spc_node,spc_dof,spc_value);
			constraints->AddObject(spc);
			count++;

			spc_sid=count;
			spc_node=i+1;
			spc_dof=2; //we enforce first y translation degree of freedom
			spc_value=*(dirichletvalues_diag+2*i+1)/model->yts;
			
			spc = new Spc(spc_sid,spc_node,spc_dof,spc_value);
			constraints->AddObject(spc);
			count++;
		}

	#ifdef _PARALLEL_
	} //if((my_grids[i]==1))
	#endif
	}

	/*All our datasets are already order by ids. Set presort flag so that later on, when sorting is requested on these 
	 * datasets, it will not be redone: */
	constraints->Presort();

	/*Free data: */
	xfree((void**)&gridondirichlet_diag);
	xfree((void**)&dirichletvalues_diag);

	/*Assign output pointer: */
	*pconstraints=constraints;
	return noerr;
}
