// Combustion.h: interface for the Sample class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_COMBUSTION_H_INCLUDED_)
#define AFX_COMBUSTION_H_INCLUDED_

#include <stdio.h>

#define ZERO_KELVIN 273.15	// 0ºC in Kelvin
#ifndef R_G
#define R_G 8.31447215		// Universal gas cosntant J/K/mol
#endif

#define LIMIT 1.0e-6		// Maximum relative error 
#define MAXIT 200           //Maximum number of iterations allowed. 
#define FPMIN 2.2204460492503131e-016    // Close to smallest representable floating-point number.
#define EPS 6.0e-15		// Relative error, or absolute error near the zero 
#define _MAX_ITER_	20000000	// maximum number of time iterations allowed

// definitions of boundary condition
#define RGT_T_CONST  0x0000		// right side, constant temperature 
#define RGT_PERIODIC 0x0010		// right side, zero heat flow
#define RGT_FLOW_CT  0x0020		// right side, constant heat flow 
// due to symmetry, on the left side (axial axis) the heat flow is zero, dT/dr=0

// reaction models

#define __F1__
//#define __F2__
//#define __FN__
//#define __KJMA__
//#define __PN__
//#define __F0__
//#define __D1__
//#define __D3_J__

#define _cof_n 1		// coeficient of reaction models

#define __FRAC_MAX__ 0.999	// the program stops when alpha > __FRAC_MAX__ 

class Sample{	// class that contains all the information about the system

public:	// 

// dimensional parameters
 double H_cap_sample;	// Sample Heat capacity, Joules/(kg K)
 double H_cap_subs;		// Substrate Heat capacity, Joules/(kg K)
 double dens_sample;	// Sample density  (kg/m3)
 double dens_subs;		// Substrate density  (kg/m3)
 double Cond_sample;	// Sample thermal conductivity W/(m K)
 double Cond_subs;		// Substrate thermal conductivity W/(m K)
 double Ent;			// Reaction enthalpy, J/kg
 // Kinetic parameters, Arrhenius dependence of the reaction rate constant
 double K_0;  			// Preexponencial term of the Arrhenius dependence, s-1
 double E_A;			// Activation Energy, Joules/mol

 double Length_sample;	// Sample height (thickness in thin films), m
 double Length_subs;	// Substrate height (thickness in thin films), m
 double Length_trans;	// Height initially transformed, SHS combustion, m
 double f_time;			// time normalization factor, s 
 double f_Temp;			// Temperature normalization factor, K 
 double f_space;		// Space normalization factor, m 
 double Tot_time;		// Maximun time allowed for integration, s
 double Vel_esc;		// Constant temperature rise  K/s
 bool Reaction_exists;	// true: a chemical reaction exists, false no
 bool Com_Front_Created;// true: a combustion front has been formed
 double Avg_alpha;		// averaged degree of transformation
 double Dif_sample;		// Thermal difusivity sample, m2/s i substrat
 double Dif_subs;		// Thermal difusivity substrate, m2/s 
 double elap_time;		// elapsed time, s
 double T_max;			// Maximum sample temperature (adiabatic temperature if there is reaction), K
 double T_min;			// Initial sample temperature, K
 double T_Subs_Ini;		// Intital susbtrate temperature, K

 // dimensionaless parameters
 double H_cap_samp_adim;		// Dimensionaless heat capacity sample
 double Dif_subs_adim;			// Dimensionaless thermal difusivity of substrate
 double Vel_esc_adim;			// Dimensionaless constant temperature rise
 double elap_time_adim;			// Dimensionaless elapsed time
 double Tot_time_adim;			// Dimensionaless maximun time allowed for integration
 double Length_sample_adim;		// Dimensionaless sample length
 double Length_subs_adim;		// Dimensionaless substrate length
 double T_max_adim;				// Dimensionaless maximum temperature
 double T_min_adim;				// Dimensionaless initial temperature
 double T_furnace;				// Dimensionaless furnace temperature
 double T_furnace_in;			// Dimensionaless initial furnace temperature

  // parameters related to boundary conditions
 int Boundary_cond;		// parameter that controls the boundary conditions
 double T_room;			// sourrounding gas temperature, used for convection, K 
 double h_conve;		// convection coefficient W/m^2/K
 double h_div_k;		// convection coefficient / dimenisonless conductivity
 double f_const;		// heat flow*Inc_r at the boundary

 // parametres initialization
 int n_elem_0;			// number of elements initially transformed
 double alfa_in;		// initial degree of transformation

 // parameters for data recording
 int N_Front;		// number of temperature and degree of transformation profiles recorded
 int N_Point_r;		// number of points of the time evolution file 
 int N_Point_Calc;	// number of points between data displayed on the screen 
 bool Int_Fronts;	// true: saves profiles at constant degree of transformation intervals
					// false: saves profiles at constant tempeterature intervals
 int k_front;		// counter of number of fronts
 double Inc_Front,Val_Front;
 double Inc_N_Point_r,Val_N_Point_r;

 // parameters finite differences
 int n_elem_tot;		// total number of points of the space grid
 int n_elem_subs;		// number of points of the substrate grid
 int n_elem_sample;		// number of points of the sample grid
 double Inc_r;			// dimensionless grid spacing 
 double Inc_t;			// dimensionless time step 
 double gamma;			// gamma=Inc_t/Inc_r/Inc_r 
 double gamma_subs;		// gamma_subs=dimensionless subs difusivity*Inc_t/Inc_r/Inc_r
 double gamma_max;		// maximum value allowed for coeficient gamma
 double Err_Rel_Max;	// maximum relative error allowed between iterations
 double *A_1,*A_2;		// coeficients used in the resolution of the implicit equation

 //------------------------------- funtions
 
 // Functions in DataManip.cpp
 Sample();				// constructor 
 ~Sample();				// destructor
 int ImputData(char *File_Name);	// reads the data, 0 is returned if an error is detected 
 void ListData(FILE *PF2);			// list the data
 int Data_Initialization();			// data initialization, 0 is returned if an error is detected 
 // saves the temperature and degree of transformation profiles
 void RegiFront(double *Temp,double *alfa,FILE *PF2,char *file_path);	
 // saves the position and width of the propagatin front if any
 void RegPosFront(FILE *PF,double *alfa,bool Prop_Rgt);		

 // funcions in Integration.cpp
 // calulation of alpha for T=constant
 void CalculaAlfaIsoterm(double *alfa_n,double *alfa_a,double *Temp);
 // calulation of alpha for T not constant
 void CalculaAlfaNoIsoterm(double *alfa_n,double *alfa_a,double *Temp_n,double *Temp_a);
 // implicit integration
 void CalculaImp(double *alfa_n,double *alfa_a,double *Temp_n,double *Temp_a);
private:

};

void Help(void);
int SeparaNomExt(char *Fitxer,char *Nom);
int TreuPath(char *Fitxer,char *Nom);
// calculation of function p(x)
double Int_Temp_Apr(double x);

#endif // AFX_COMBUSTION_H_INCLUDED_
