/*\file ModelProcessor.c
 *\brief: processes model inputs from the matlab workspace, and transforms them into datasets or vectors 
 *        that will be used throughout the matlab solution sequences.
 */

#include "./ModelProcessor.h"

void mexFunction( int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]){ 

	/*diverse: */
	int   noerr=1;

	/* output datasets: */
	DataSet* elements=NULL;
	DataSet* nodes=NULL;
	DataSet* constraints=NULL;
	DataSet* loads=NULL;
	DataSet* materials=NULL;
	DataSet* parameters=NULL;

	/*input model: */
	Model* model=NULL;

	/*Boot module: */
	MODULEBOOT();

	/*checks on arguments on the matlab side: */
	CheckNumMatlabArguments(nlhs,NLHS,nrhs,NRHS,__FUNCT__,&ModelProcessorUsage);

	/*Fill model with matlab workspace data: */
	ModelInit(&model,MODEL); //this routine goes through the entire MODEL matlab class, and starts filling the corresponding "c" code model object.

	/*Create elements, nodes and materials: */
	ModelCreateElementsNodesAndMaterials(&elements,&nodes,&materials,model,MODEL);

	/*Create constraints: */
	ModelCreateConstraints(&constraints,model,MODEL);
	
	/*Create loads: */
	ModelCreateLoads(&loads,model,MODEL);
	
	/*Create parameters: */
	ModelCreateParameters(&parameters,model,MODEL);

	/*Write output data: */
	WriteData(ELEMENTS,elements,0,0,"DataSet",NULL);
	WriteData(NODES,nodes,0,0,"DataSet",NULL);
	WriteData(CONSTRAINTS,constraints,0,0,"DataSet",NULL);
	WriteData(LOADS,loads,0,0,"DataSet",NULL);
	WriteData(MATERIALS,materials,0,0,"DataSet",NULL);
	WriteData(PARAMETERS,parameters,0,0,"DataSet",NULL);

	/*Free ressources: */
	DeleteModel(&model);
	delete elements;
	delete nodes;
	delete constraints;
	delete loads;
	delete materials;
	delete parameters;

	/*end module: */
	MODULEEND();
}
	
void ModelProcessorUsage(void) {
	_printf_("\n");
	_printf_("   usage: [elements,grids,constraints,loads,materials,parameters,part,tpart]=ModelProcessor(model)\n");
	_printf_("   where: model is an instance of the matlab @model class.\n");
	_printf_("\n");
}
