-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfractional.c
More file actions
executable file
·352 lines (339 loc) · 12.9 KB
/
Copy pathfractional.c
File metadata and controls
executable file
·352 lines (339 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/* This file does a multicomponent fractional
distillation calculation using the FUGK method */
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "main.h"
#include "fractional.h"
extern void azeotrope_search(void);
extern void convert_pressure(void);
long double x_distillate_light, x_distillate_heavy, x_bottom_light, x_bottom_heavy;
void fractional_user(void)
{
int i = 0;
long double q_line, reflux_ratio, r_min;
long double trace_k[300];
printf("What Is The Light Key? ");
scanf("%s", light_key);
while(user_batch_chemical(light_key) == -1)
{
printf("Chemical Was Not Originally Input Into The Model. Please Enter A Chemical Already Input Into The Model. ");
scanf("%s", light_key);
printf("\n");
if(i > 5)
{
printf("A Valid Chemical Was Not Input\n EXIT PROGRAM\n");
exit(9);
}
}
i = 0;
printf("What Fraction Of %s Is Recovered In The Distillate? ", light_key);
scanf("%Lf", &x_distillate_light);
x_distillate_light = pos_frac_check(x_distillate_light);
x_bottom_light = 1 - x_distillate_light;
printf("What Is The Heavy Key? ");
scanf("%s", heavy_key);
while(user_batch_chemical(heavy_key) == -1)
{
printf("Chemical Was Not Originally Input Into The Model. Please Enter A Chemical Already Input Into The Model. ");
scanf("%s", heavy_key);
printf("\n");
if(i > 5)
{
printf("A Valid Chemical Was Not Input\n EXIT PROGRAM\n");
exit(9);
}
}
i = 0;
printf("What Fraction Of %s Remains In The Bottoms? ", heavy_key);
scanf("%Lf", &x_bottom_heavy);
x_bottom_heavy = pos_frac_check(x_bottom_heavy);
x_distillate_heavy = 1 - x_bottom_heavy;
if(models_done > 0)
{
printf("Would You Like To Use The Same Total System Pressure Used In The Flash Calculation? ");
if(user_yes_no() == 0)
{
printf("What Is The Pressure Of Your System? ");
scanf("%Lf", &total_pressure);
printf("Is This Pressure In mmHg? ");
if(user_yes_no() == 0)
{
convert_pressure();
}
boiling_point_pure_substance();
}
}
else
{
printf("What Is The Pressure Of Your System? ");
scanf("%Lf", &total_pressure);
printf("Is This Pressure In mmHg? ");
if(user_yes_no() == 0)
{
convert_pressure();
}
boiling_point_pure_substance();
}
printf("What Is The Value Of q? ");
scanf("%Lf", &q_line);
fractional_distillation_array_creation();
alpha_gmean(trace_k);
fenske();
redistribution(trace_k);
r_min = underwood(q_line);
printf("\nYou Can Input A Reflux Ratio OR Use 1.4X The Minimium Reflux Ratio\n");
printf("Would You Like To Input A Reflux Ratio? ");
if(user_yes_no() == 1)
{
printf("What Is Your Reflux Ratio? ");
scanf("%Lf", &reflux_ratio);
while(reflux_ratio < r_min)
{
printf("The Reflux Ratio Must Be Equal To Or Greater Than The Minium Reflux Ratio : %Lf\n", r_min);
printf("Please Enter A Valid Reflux Ratio: ");
scanf("%Lf", &reflux_ratio);
printf("\n");
if(i > 20)
{
printf("A Valid Reflux Ratio Was Not Input\n EXIT PROGRAM\n");
exit(9);
}
}
}
else
{
reflux_ratio = r_min * 1.4;
}
gilliland(reflux_ratio, r_min);
kirkbride();
return;
}
void alpha_gmean(long double *trace_k)
{
int k;
long double a, b, c;
long double light_key_k = -999;
long double heavy_key_k = -999;
long double alpha_top;
long double alpha_bottom;
long double alpha;
boiling_equilibrium_temperature_calculation();
dew_equilibrium_temperature_calculation();
in_bounds_antoine_data(dew_temperature);
light_location = user_batch_chemical(light_key);
heavy_location = user_batch_chemical(heavy_key);
for(k = 0; k < species; k++)
{
a = in_bounds_temperature_data[k*5];
b = in_bounds_temperature_data[k*5+1];
c = in_bounds_temperature_data[k*5+2];
trace_k[k] = pow(10, a-b/(dew_temperature+c))/total_pressure;
if(k == light_location)
{
light_key_k = trace_k[k];
}
if(k == heavy_location)
{
heavy_key_k = trace_k[k];
}
}
alpha_top = light_key_k/heavy_key_k;
in_bounds_antoine_data(boiling_temperature);
for( ;k < species*2 ;k++)
{
a = in_bounds_temperature_data[(k-species)*5];
b = in_bounds_temperature_data[(k-species)*5+1];
c = in_bounds_temperature_data[(k-species)*5+2];
trace_k[k] = pow(10, a-b/(boiling_temperature+c))/total_pressure;
if((k - species) == light_location)
{
light_key_k = trace_k[k];
}
if((k - species) == heavy_location)
{
heavy_key_k = trace_k[k];
}
}
alpha_bottom = light_key_k/heavy_key_k;
alpha = sqrt(alpha_top*alpha_bottom);
for(k = 0; k < species; k++)
{
strcpy(f_array[k] -> fd_array[0] -> light_key, light_key);
strcpy(f_array[k] -> fd_array[0] -> heavy_key, heavy_key);
f_array[k] -> fd_array[0] -> total_pressure = total_pressure;
f_array[k] -> fd_array[0] -> total_distillate_mass = mass_distillate;
f_array[k] -> fd_array[0] -> total_bottom_mass = mass_bottom;
f_array[k] -> fd_array[0] -> boiling_temperature = boiling_temperature;
f_array[k] -> fd_array[0] -> dew_temperature = dew_temperature;
f_array[k] -> fd_array[0] -> alpha = alpha;
strcpy(f_array[k] -> fd_array[0] -> chemical, f_array[k] -> chemical);
f_array[k] -> fd_array[0] -> distillate_mass = f_array[k] -> fd_array[0] -> total_distillate_mass*f_array[k] -> fd_array[0]-> distillate_frac;
f_array[k] -> fd_array[0] -> bottoms_mass = f_array[k] -> fd_array[0] -> total_bottom_mass*f_array[k] -> fd_array[0] -> bottoms_frac;
}
return;
}
void fenske(void)
{
int i;
long double n_min;
n_min = log((x_distillate_light/x_bottom_light)*(x_bottom_heavy/x_distillate_heavy))/log(f_array[0] -> fd_array[0] -> alpha);
for(i = 0; i < species; i++)
{
f_array[i] -> fd_array[0] -> n_min = n_min;
}
return;
}
void redistribution(long double *trace_k)
{
int i;
long double alpha_trace;
long double bottom_heavy_key_mass = f_array[heavy_location] -> fd_array[0] -> bottoms_mass;
long double distillate_heavy_key_mass = f_array[heavy_location] -> fd_array[0] -> distillate_mass;
long double bottoms_trace;
long double distillate_trace;
long double bottoms_mass = 0;
long double distillate_mass = 0;
for(i = 0; i < species; i++)
{
if(i != heavy_location && i != light_location)
{
alpha_trace = (trace_k[i+species]/trace_k[heavy_location+species] + trace_k[i]/trace_k[heavy_location])/2;
bottoms_trace = f_array[i] -> feed_mass/(1+(distillate_heavy_key_mass/bottom_heavy_key_mass)*pow(alpha_trace,f_array[i] -> fd_array[0] -> n_min));
distillate_trace = (f_array[i] -> feed_mass*(distillate_heavy_key_mass/bottom_heavy_key_mass)*pow(alpha_trace,f_array[i] -> fd_array[0] -> n_min))/(1+(distillate_heavy_key_mass/bottom_heavy_key_mass)*pow(alpha_trace,f_array[i] -> fd_array[0] -> n_min));
if(bottoms_trace < distillate_trace)
{
distillate_trace = f_array[i]->feed_mass - bottoms_trace;
f_array[i] -> fd_array[0] -> distillate_mass = distillate_trace;
f_array[i] -> fd_array[0] -> bottoms_mass = bottoms_trace;
}
if(bottoms_trace > distillate_trace)
{
bottoms_trace = f_array[i]-> feed_mass - distillate_trace;
f_array[i] -> fd_array[0] -> distillate_mass = distillate_trace;
f_array[i] -> fd_array[0] -> bottoms_mass = bottoms_trace;
}
}
}
for(i = 0; i < species; i++)
{
distillate_mass += f_array[i] -> fd_array[0] -> distillate_mass;
bottoms_mass += f_array[i] -> fd_array[0] -> bottoms_mass;
}
for(i=0; i < species; i++)
{
f_array[i] -> fd_array[0] -> distillate_frac = f_array[i] -> fd_array[0] -> distillate_mass/ distillate_mass;
f_array[i] -> fd_array[0] -> total_distillate_mass = distillate_mass;
f_array[i] -> fd_array[0] -> bottoms_frac = f_array[i] -> fd_array[0] -> bottoms_mass/ bottoms_mass;
f_array[i] -> fd_array[0] -> total_bottom_mass = bottoms_mass;
}
return;
}
long double underwood(long double q_line)
{
int i;
int j = 0;
int one_below_location = 999;
long double theta_array[2] = {0};
long double guess = 0;
long double r_min;
long double a, b, c, k_heavy_key, k_light_key, k_i_key, i_hk_alpha;
long double one_below_hk = 999;
long double theta;
long double average_temperature = (dew_temperature+boiling_temperature)/2;
in_bounds_antoine_data(average_temperature);
a = in_bounds_temperature_data[heavy_location*5];
b = in_bounds_temperature_data[heavy_location*5+1];
c = in_bounds_temperature_data[heavy_location*5+2];
k_heavy_key = pow(10, a-b/(average_temperature+c))/total_pressure;
a = in_bounds_temperature_data[light_location*5];
b = in_bounds_temperature_data[light_location*5+1];
c = in_bounds_temperature_data[light_location*5+2];
k_light_key = pow(10, a-b/(average_temperature+c))/total_pressure;
for(i = 0; i < species; i++)
{
if(i != heavy_location)
{
if (f_array[heavy_location] -> pure_boiling_temperature - f_array[i] -> pure_boiling_temperature < 0)
{
if(fabsl(f_array[heavy_location] -> pure_boiling_temperature - f_array[i]->pure_boiling_temperature) < one_below_hk)
{
one_below_hk = f_array[i] -> pure_boiling_temperature;
one_below_location = i;
}
}
}
}
a = in_bounds_temperature_data[one_below_location*5];
b = in_bounds_temperature_data[one_below_location*5+1];
c = in_bounds_temperature_data[one_below_location*5+2];
one_below_hk = pow(10, a-b/(average_temperature+c))/total_pressure;
for(theta = one_below_hk/k_heavy_key; theta < k_light_key/k_heavy_key; theta = theta + 0.01)
{
guess = 0;
for(i = 0; i <species; i++)
{
a = in_bounds_temperature_data[i*5];
b = in_bounds_temperature_data[i*5+1];
c = in_bounds_temperature_data[i*5+2];
k_i_key = pow(10, a-b/(average_temperature+c))/total_pressure;
i_hk_alpha = k_i_key/k_heavy_key;
guess += (i_hk_alpha*f_array[i]->feed_frac)/(i_hk_alpha-theta);
}
if(fabsl(guess + q_line - 1) < 0.05)
{
theta_array[j] = theta;
j++;
}
}
for(j=0; j < 2; j++)
{
guess = 0;
for(i = 0; i < species; i++)
{
a = in_bounds_temperature_data[i*5];
b = in_bounds_temperature_data[i*5+1];
c = in_bounds_temperature_data[i*5+2];
k_i_key = pow(10, a-b/(average_temperature+c))/total_pressure;
i_hk_alpha = k_i_key/k_heavy_key;
guess += (i_hk_alpha*f_array[i] -> fd_array[0]-> distillate_frac)/(i_hk_alpha-theta_array[j]);
}
r_min = guess - 1;
if(guess - 1 > r_min)
{
r_min = guess - 1;
}
}
for(i=0;i<species;i++)
{
f_array[i] -> fd_array[0] -> r_min = r_min;
}
return r_min;
}
void gilliland(long double reflux_ratio, long double r_min)
{
int i;
long double right_hand_side, number_of_stages;
right_hand_side= 0.75*(1-pow((reflux_ratio-r_min)/(reflux_ratio-1),0.567));
number_of_stages = (f_array[0]->fd_array[0] -> n_min + right_hand_side)/(1-right_hand_side);
for(i = 0; i< species; i++)
{
f_array[i] -> fd_array[0] -> reflux_ratio = reflux_ratio;
f_array[i] -> fd_array[0] -> number_of_stages = number_of_stages;
}
return;
}
void kirkbride(void)
{
int i;
long double reflux_stripping_ratio, number_of_reflux_stages, number_of_stripping_stages;
reflux_stripping_ratio = pow((f_array[heavy_location] -> feed_frac/f_array[light_location] -> feed_frac)*pow((f_array[light_location] -> fd_array[0] -> bottoms_frac/f_array[heavy_location] -> fd_array[0] -> distillate_frac),2)*(f_array[0] -> fd_array[0] -> total_bottom_mass/f_array[0]->fd_array[0] -> total_distillate_mass), 0.206);
number_of_stripping_stages = f_array[0]->fd_array[0]->number_of_stages/(reflux_stripping_ratio+1);
number_of_reflux_stages = f_array[0]->fd_array[0]->number_of_stages - number_of_stripping_stages;
for(i = 0; i < species; i++)
{
f_array[i] -> fd_array[0] -> feed_locations = number_of_reflux_stages;
}
return;
}