/*
 * CreateConstraintsEnthalpy.c:
 */

#include "../../../Container/Container.h"
#include "../../../io/io.h"
#include "../../../toolkits/toolkits.h"
#include "../../../EnumDefinitions/EnumDefinitions.h"
#include "../../../objects/objects.h"
#include "../../../shared/shared.h"
#include "../ModelProcessorx.h"

void	CreateConstraintsEnthalpy(Constraints** pconstraints, IoModel* iomodel){

	/*Intermediary*/
	int i;
	int count;
	int    dim;
	int    numberofvertices;
	double heatcapacity;
	double referencetemperature;
	
	/*Output*/
	Constraints* constraints = NULL;
	SpcStatic*    spcstatic  = NULL;

	/*Fetch parameters: */
	iomodel->Constant(&dim,MeshDimensionEnum);
	iomodel->Constant(&numberofvertices,MeshNumberofverticesEnum);
	iomodel->Constant(&heatcapacity,MaterialsHeatcapacityEnum);
	iomodel->Constant(&referencetemperature,ConstantsReferencetemperatureEnum);

	/*Recover pointer: */
	constraints=*pconstraints;

	/*Create constraints if they do not exist yet*/
	if(!constraints) constraints = new Constraints();

	/*return if 2d mesh*/
	if (dim==2){
		*pconstraints=constraints;
		return;
	}

	/*Fetch data: */
	double *spctemperature=NULL;
	iomodel->FetchData(&spctemperature,NULL,NULL,ThermalSpctemperatureEnum);

	/*Initialize counter*/
	count=0;

	/*Create constraints from x,y,z: */
	for (i=0;i<numberofvertices;i++){
		/*keep only this partition's nodes:*/
		if((iomodel->my_vertices[i])){

			if (!isnan(spctemperature[i])){

				constraints->AddObject(new SpcStatic(iomodel->constraintcounter+count+1,iomodel->nodecounter+i+1,1,heatcapacity*(spctemperature[i]-referencetemperature),EnthalpyAnalysisEnum));
				count++;

			}
		}
	}

	/*Free data: */
	xfree((void**)&spctemperature);
	
	/*Assign output pointer: */
	*pconstraints=constraints;
}
