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

#if !defined(AFX_COMBUSTION_H_INCLUDED_)
#define AFX_COMBUSTION_H_INCLUDED_

#include <stdio.h>
#include "Create_bmp.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
#define BTM_T_CONST  0x0000		// bottom side, constant temperature 
#define BTM_PERIODIC 0x0100		// bottom side, zero heat flow
#define BTM_FLOW_CT  0x0200		// bottom side, constant heat flow
#define TOP_T_CONST  0x0000		// top side, constant temperature 
#define TOP_PERIODIC 0x1000		// top side, zero heat flow
#define TOP_FLOW_CT  0x2000		// top side, constant heat flow 

// 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 Radius_sample;	// Sample radius, m
 double Btm_wall_thk;	// Vessel bottom wall thickness, m
 double Radius_trans;	// Radius initially transformed, SHS combustion, m
 double Height_sample;	// Sample height , m
 double Rgt_wall_thk;	// Vessel right wall thickness , m
 double Height_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 Radius_sample_adim;		// Dimensionaless Sample radius
 double Rgt_wall_thk_adim;		// Dimensionaless vessel right wall thickness
 double Height_sample_adim;		// Dimensionaless Sample height 
 double Btm_wall_thk_adim;		// Dimensionaless vessel bottom wall thickness 
 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_z at the boundary

 // parametres initialization
 int n_r_0;				// number of elements initially transformed in the radial direction
 int n_z_0;				// number of elements initially transformed in the vertical direction
 double alfa_in;		// initial degree of transformation

 // parameters for data recording
 int col_z,col_r;	// columns chosen to saves profiles and to calculate the front propagation
 int N_Front_prof;		// number of temperature and degree of transformation profiles recorded
 int N_Front_bmp;		// number of temperature and degree of transformation images recorded
 int N_Point_s;		// 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_bmp;	// counter of number of images
 int k_front_prof;	// counter of number of profiles
 double Inc_Front_bmp,Val_Front_bmp;
 double Inc_Front_prof,Val_Front_prof;
 double Inc_N_Point_s,Val_N_Point_s;
 long size_RGB,size_BMP;	// image parameters
 BYTE *img_RGB,*img_BMP;	// image parameters

 // parameters finite differences
 int n_r_tot;			// total number of points of the radial grid
 int n_r_subs;			// number of points of the radial substrate grid
 int n_r_sample;		// number of points of the radial sample grid
 int n_z_tot;			// total number of points of the axial grid
 int n_z_subs;			// number of points of the axial substrate grid
 int n_z_sample;		// number of points of the axial sample grid
 double Inc_s;			// dimensionless grid spacing 
 double Inc_t;			// dimensionless time step 
 double Inc_t_2;		// half dimensionless time step 
 double gamma;			// gamma=Inc_t/Inc_z/Inc_z 
 double gamma_2;		// gamma=Inc_t_2/Inc_z/Inc_z 
 double gamma_subs;		// gamma_subs=dimensionless subs difusivity*Inc_t/Inc_z/Inc_z
 double gamma_subs_2;		// gamma_subs=dimensionless subs difusivity*Inc_t_2/Inc_z/Inc_z
 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 two temperature and degree of transformation profiles
 // one in the radial direction and the other on the axial direction
 void RegiFront(double **Temp,double **alfa,FILE *PF2,char *file_path);
 // creates an image from alpha or temeprature
 bool CreateImatge(double **values,double Max_Value,double Min_Value);
 // saves the image in a BMP file
 void RegBMP(double **valors,char *Name,char *file_path,double Max_Value,double Min_Value);
// Locates the position of the propagation fron if any
// Prop_Rgt=true searches a front propagating rightwards, false leftwards
// Prop_Down=true searches a front propagating downwards, false upwards
 void RegPosFront(FILE *PF,double **alfa,bool Prop_Rgt,bool Prop_Down);
// searches in the radial direction in file col_z
// Prop_Rgt=true searches a front propagating rightwards, false leftwards
 bool RegPosFront_r(FILE *PF,double **alfa,bool Prop_Rgt,double &pos_r,double &width_r);
// searches in the axial direction in column col_r
// Prop_Down=true searches a front propagating downwards, false upwards
 bool RegPosFront_z(FILE *PF,double **alfa,bool Prop_Down,double &pos_z,double &width_z);

 // 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 in the radial axe
 void CalculaImp_r(double **alfa_n,double **alfa_a,double **Temp_n,double **Temp_a);
 // implicit integration in the vertical axe
 void CalculaImp_z(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_
