#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MMW 18.0153 //Molar mass of water Grams per mol
#define MME 46.0684 //Molar mass of ethanol Grams per mol
#define GAL2L 3.7854 //Gallons to Liters conversion
#define W_DENS 0.998 //Water density at 20 degrees C
#define E_DENS 0.789 //Ethanol density at 20 degrees C
#define LHV_W 2257 //latent heat of vaporisation of water in joules\gram\K
#define LHV_E 855 //latent heat of vaporisation of ethanol in joules\gram\K
#define CPE 2.845 //Specific heat ethanol
#define CPW 4.184 //Specific heat of water
/***********************************************************/
//Antoine Eq coefficients
double eA = 7.68117;
double eB = 1332.04;
double eC = 199.2;
double wA = 8.07131;
double wB = 1730.63;
double wC = 233.426;
//CONVERSION COEFFICIENTS
static const double abw_abv[9] = /*Array of E Croissants coefficients for conversion of alcohol by weight to alcohol by volume*/
{
-0.000039705486746795932, 1.2709666849144778, -0.40926819348115739, 2.0463351302912738,
-7.8964816507513707, 15.009692673927390, -15.765836469736477, 8.8142267038252680, -2.0695760421183493
};
/**DECLARE FUNCTIONS********************************************************/
void get_user_input(double *pstart_vol, double *pstart_abv, int *start_temp, int *atm_press, int *watts);
//Converts alcohol by volume to alcohol by weight
double abv_to_abw(double abv);
//Calculates mass and mols of mixture*/
void initial_conditions(double start_vol, double abv, double (*abv_to_abw)(double abv),
double *peth_gram1, double *ph2o_gram1, double *peth_mol1);
//van Laar ctivity coefficient applied to liquid phase of Raoults Law
//x1 is molar fraction of ethanol. x2 is molar fraction of water (1-x1)
double calc_gamma_e(double x1, double x2);
double calc_gamma_w(double x1, double x2);
//Calculate saturated vapor pressure per Antoine equation by temperature
double calc_psat_t(double A, double B, double C, double *boil_pt1, double gamma, double x);
//Calculate boil point by iterating temperature
void bp_by_t(double atm_p, double *boil_pt1, double *eth_mol1,
double (*calc_gamma_e)(double x1, double x2),
double (*calc_gamma_w)(double x1, double x2),
double (*calc_psat_t)(double A, double B, double C, double *temp, double gamma, double x));
//Calculate time to boil
void time2boil(double start_temp, double boil_pt1, int watts, double eth_gram1, double h2o_gram1);
/*********************************************************/
int main()
{
//User input data
double start_vol, start_abv = 0;
int start_temp, atm_press, watts = 0;
double *pstart_vol = &start_vol;
double *pstart_abv = &start_abv;
int *pstart_temp = &start_temp;
int *patm_press = &atm_press;
int *pwatts = &watts;
//Initial conditions data
double eth_gram1, h2o_gram1 = 0;
double *ph2o_gram1 = &h2o_gram1;
double eth_mol1 = 0;
double *peth_mol1 = ð_mol1;
double boil_pt1 = 80; // Seed temp to saturated pressure calc Degrees C
double *pboil_pt1 = &boil_pt1;
get_user_input(&start_vol, &start_abv, &start_temp, &atm_press, &watts);
//Check user input
//printf("\n1 = %lf \n2 = %lf \n3 = %d \n4 = %d \n5 = %d", start_vol, start_abv, start_temp, atm_press, watts);
initial_conditions(start_vol, start_abv, abv_to_abw, ð_gram1, &h2o_gram1, ð_mol1);
//Check initial conditions
//printf("\neth mass = %lf grams \nh2o mass = %lf grams \neth mols = % lf mols ", eth_gram1, h2o_gram1, eth_mol1);
bp_by_t(atm_press, &boil_pt1, ð_mol1, calc_gamma_e, calc_gamma_w, calc_psat_t);
//Check boil point
//printf("\nfirst boil point = %lf", boil_pt1);
time2boil(start_temp, boil_pt1, watts, eth_gram1, h2o_gram1);
return 0;
}
/*********FUNCTION DEFINITIONS**********/
void get_user_input(double *pstart_vol, double *pstart_abv, int *pstart_temp, int *patm_press, int *pwatts)
{
printf("\nEnter Pot liquid volume (gal): ");
scanf("%lf", pstart_vol);
printf("\nEnter Pot liquid ABV (0 to 1): ");
scanf("%lf", pstart_abv);
printf("\nEnter Start Temperature C: ");
scanf("%d", pstart_temp);
printf("\nEnter Atmospheric Pressure Hg/mm: ");
scanf("%d", patm_press);
printf("\nEnter Heatup Power Watts: ");
scanf("%d", pwatts);
return;
}
/***************************************************/
/*Calculates alcohol by weight from alcohol by volume. Source: On the Conversion of Ethanol by Edwin Croissant 02-14-2016 */
/*Result between 0 and 1. Valid only for input between 0 and 1 and valid only at 20 degrees C*/
/*Initial polynomial y = a + b*x + c*x^2 + d*x^3 + f*x^4 + g*x^5 + h*x^6 + i*x^7 + j*x^8*/
double abv_to_abw(double abv)
{
const double a = 0.00018684999875047631;
const double b = 0.77602465132552556;
const double c = 0.41803095099103116;
const double d = -2.5221614925275091;
const double f = 9.5827123045656251;
const double g = -19.928886159385002;
const double h = 24.165120890385651;
const double i = -15.830262207383321;
const double j = 4.3390473620304988;
//In Horner form
double temp = j;
temp = temp * abv +i;
temp = temp * abv +h;
temp = temp * abv +g;
temp = temp * abv +f;
temp = temp * abv +d;
temp = temp * abv +c;
temp = temp * abv +b;
double abw = temp * abv +a;
return abw;
}
/*****************************************************************/
void initial_conditions(double start_vol, double abv, double (*abv_to_abw)(double abv),
double *peth_gram1, double *ph2o_gram1, double *peth_mol1)
{
//Convert alcohol by volume to alcohol by wight (wt%-wt%)
double abw = 0;
abw = abv_to_abw(abv);
//Calculate component mass of mixture
*peth_gram1 = start_vol * abw * GAL2L * 1000 * E_DENS;
*ph2o_gram1 = start_vol * (1-abw) * GAL2L * 1000 * W_DENS;
//Convert alcohol by weight to alcohol by mol (mol fraction)
*peth_mol1 = abw /(abw +MME /MMW *(1-abw));
return;
}
/************************************************/
/*Calculate Van Laar activity coefficient for ethanol*/
double calc_gamma_e(double x1, double x2)
{
double const a12 = 1.68811;/*a12 and a21 are Van Laar activity model constants*/
double const a21 = 0.95268;
double result = exp((a12 * pow(x2, 2)) / (pow(((a12 * x1 / a21) + x2), 2)));
return (result);
}
/***************************************************/
/*Calculate Van Laar activity coefficient for water*/
double calc_gamma_w(double x1, double x2)
{
double const a12 = 1.68811;
double const a21 = 0.95268;
double result = exp((a21 * pow(x1, 2)) / (pow(((a21 * x2 / a12) + x1), 2)));
return (result);
}
/***************************************************/
//Antoine Eq. to calculate saturated vapor pressure for water and ethanol
double calc_psat_t(double A, double B, double C, double *temp, double gamma, double x)
{
double psat = x*gamma*pow(10, (A-((B)/(C + *temp))));
return psat;
}
/***************************************************/
void bp_by_t(double atm_p, double *pboil_pt1, double *peth_mol1,
double (*calc_gamma_e)(double x1, double x2),
double (*calc_gamma_w)(double x1, double x2),
double (*calc_psat_t)(double A, double B, double C, double *temp, double gamma, double x))
{
double x1 = *peth_mol1; //ethanol component of mixture in mols
double x2 = 1-x1; //water component
double psat1 = 0;
double psat2 = 0;
double gamma_e = 0;
double gamma_w = 0;
gamma_e = calc_gamma_e(x1, x2);
gamma_w = calc_gamma_w(x1, x2);
do
{
psat1 = calc_psat_t(eA, eB, eC, pboil_pt1, gamma_e, x1); //Saturated vapor pressure ethanol
psat2 = calc_psat_t(wA, wB, wC, pboil_pt1, gamma_w, x2); //Saturated vapor pressure water
*pboil_pt1 += 0.001;
}
while (psat1 + psat2 < atm_p); //When sum of vapor pressures = or exceed atmospheric pressure the mixture is boiling
printf("\npressure = %lf", psat1 +psat2);
return;
}
/*****************************************************************/
void time2boil(double start_temp, double boil_pt1, int watts, double eth_gram1, double h2o_gram1)
{
double power_req, temp_rise, minutes = 0;
//Calc specific heat. CPE&W are global constants of specific heat of ethanol and water respectively
double pot_mass = eth_gram1 + h2o_gram1;
double spec_heat = (eth_gram1/pot_mass *CPE) + (h2o_gram1/pot_mass *CPW);
temp_rise = boil_pt1 - start_temp;
power_req = pot_mass *spec_heat *temp_rise;
minutes = power_req /watts /60;
printf("\ntime to boil = %.1lf", minutes);
return;
}