/*!\file FemModel.c
 * \brief: implementation of the FemModel object
 */


#ifdef HAVE_CONFIG_H
	#include <config.h>
#else
#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
#endif

#include <stdio.h>
#include "../Container/Container.h"
#include "../modules/ModelProcessorx/ModelProcessorx.h"
#include "../io/io.h"
#include "./objects.h"
#include "../include/include.h"
#include "../EnumDefinitions/EnumDefinitions.h"
#include "../modules/modules.h"

/*Object constructors and destructor*/
/*FUNCTION FemModel::constructor {{{1*/
FemModel::FemModel(char* inputfilename, char* outputfilename, const int in_solution_type,const int* analyses,const int nummodels){

	/*intermediary*/
	int i;
	int analysis_type;
	FILE* IOMODEL;
	extern int my_rank;

	/*Open input file on cpu 0: */
	if(my_rank==0) IOMODEL= pfopen(inputfilename ,"rb");

	/*Initialize internal data: */
	this->nummodels=nummodels;
	this->solution_type=in_solution_type;
	this->analysis_counter=nummodels-1; //point to last analysis_type carried out.
	this->results=new Results(); //not initialized by CreateDataSets
	
	/*Dynamically allocate whatever is a list of length nummodels: */
	analysis_type_list=(int*)xmalloc(nummodels*sizeof(int));

	/*Initialize: */
	for(i=0;i<nummodels;i++)analysis_type_list[i]=analyses[i];

	/*create datasets for all analyses*/
	ModelProcessorx(&this->elements,&this->nodes,&this->vertices,&this->materials,&this->constraints,&this->loads,&this->parameters,IOMODEL,this->solution_type,nummodels,analyses);

	/*do the post-processing of the datasets to get an FemModel that can actually run analyses: */
	for(i=0;i<nummodels;i++){

		_printf_(VerboseMProcessor(),"   Processing finite element model of analysis %s:\n",EnumToStringx(analysis_type_list[i]));
		analysis_type=analysis_type_list[i];
		this->SetCurrentConfiguration(analysis_type);
	
		if(i==0){
			_printf_(VerboseMProcessor(),"      creating vertex degrees of freedom\n");
			VerticesDofx(vertices,parameters); //only call once, we only have one set of vertices
		}

		_printf_(VerboseMProcessor(),"      resolving node constraints\n");
		SpcNodesx(nodes,constraints,parameters,analysis_type); 

		_printf_(VerboseMProcessor(),"      creating nodal degrees of freedom\n");
		NodesDofx(nodes,parameters,analysis_type);
	
		_printf_(VerboseMProcessor(),"      configuring element and loads\n");
		ConfigureObjectsx(elements, loads, nodes, vertices, materials,parameters);
	}
	
	/*Close input file descriptors: */
	if(my_rank==0) pfclose(IOMODEL,inputfilename);

	/*Add output file name to parameters: */
	this->parameters->AddObject(new StringParam(OutputfilenameEnum,outputfilename));

}

/*}}}1*/
/*FUNCTION FemModel::destructor {{{1*/
FemModel::~FemModel(){

	/*Intermediary*/
	int i;

	/*Delete all the datasets: */
	xfree((void**)&analysis_type_list);
	delete elements;
	delete nodes;
	delete vertices;
	delete constraints;
	delete loads;
	delete materials;
	delete parameters;
	delete results;

}
/*}}}1*/

/*Object management*/
/*FUNCTION FemModel::Echo {{{1*/
void FemModel::Echo(void){

	printf("FemModel echo: \n");
	printf("   number of fem models: %i\n",nummodels);
	printf("   analysis_type_list: \n");
	for(int i=0;i<nummodels;i++)printf("     %i: %s\n",i,EnumToStringx(analysis_type_list[i]));
	printf("   current analysis_type: \n");
	printf("     %i: %s\n",analysis_counter,EnumToStringx(analysis_type_list[analysis_counter]));

}
/*}}}*/

/*Numerics: */
/*FUNCTION FemModel::SetCurrentConfiguration(int configuration_type,int analysis_type){{{1*/
void FemModel::SetCurrentConfiguration(int configuration_type,int analysis_type){

	/*Use configuration_type to setup the analysis counter, the configurations of objects etc ... but use 
	 * analysis_type to drive the element numerics. This allows for use of 1 configuration_type for several 
	 * analyses. For example: do a SurfaceSlopeX, SurfaceSlopeY, BedSlopeX and BedSlopeY analysis using the 
	 * Slope configuration.*/

	int found=-1;
	for(int i=0;i<nummodels;i++){
		if (analysis_type_list[i]==configuration_type){
			found=i;
			break;
		}
	}
	if(found!=-1) analysis_counter=found;
	else _error_("Could not find alias for analysis_type %s in list of FemModel analyses",EnumToStringx(configuration_type));

	/*Now, plug analysis_counter and analysis_type inside the parameters: */
	this->parameters->SetParam(analysis_counter,AnalysisCounterEnum);
	this->parameters->SetParam(analysis_type,AnalysisTypeEnum);
	this->parameters->SetParam(configuration_type,ConfigurationTypeEnum);

	/*configure elements, loads and nodes, for this new analysis: */
	this->elements->SetCurrentConfiguration(elements,loads, nodes,vertices, materials,parameters);
	this->nodes->SetCurrentConfiguration(elements,loads, nodes,vertices, materials,parameters);
	this->loads->SetCurrentConfiguration(elements, loads, nodes,vertices, materials,parameters);

	#ifdef _HAVE_PETSC_
	/*take care of petsc options, that depend on this analysis type (present only after model processor)*/
	if(this->parameters->Exist(PetscOptionsStringsEnum)){
		PetscOptionsFromAnalysis(this->parameters,analysis_type);
		_printf_(VerboseSolver(),"      petsc Options set for analysis type: %s\n",EnumToStringx(analysis_type));
	}
	#endif

}
/*}}}1*/
/*FUNCTION FemModel::SetCurrentConfiguration(int configuration_type){{{1*/
void FemModel::SetCurrentConfiguration(int configuration_type){

	/*overload: analysis_type = configuration_type: */
	this->SetCurrentConfiguration(configuration_type,configuration_type);
}
/*}}}1*/
