Parth M&S
Parth M&S
Practical File
Modelling and Simulation Lab in Chemical Engineering
Pro. Page
Title Date Sign.
No. No.
ii
Program No.1
Title:
Write a program in C++ to simulate flash calculations.
Theory:
Flash calculations are a common operation in chemical engineering and thermodynamics,
particularly in the design and operation of processes involving phase equilibrium. The
primary purpose of flash calculations is to determine the equilibrium concentrations and/or
compositions of components in a mixture when it is subjected to a sudden change in
thermodynamic conditions, such as pressure or temperature.
Code:
#include<iostream>
#include<vector>
#include<cmath>
#include<conio.h>
using namespace std;
const double EPSILON = 1e-8;
const int MAX_ITERATIONS = 100;
double rachford_rice(const vector<double>& K_values, double beta)
{
double sum = 0.0;
int n = K_values.size();
for (int i = 0; i < n; ++i)
{
sum += (K_values[i] - 1.0) / (1.0 + beta * (K_values[i] - 1.0));
}
return sum;
}
double solve_flash_calculation(const vector<double>& K_values)
{
double beta = 0.5; // Initial guess for the liquid fraction
double f_beta = rachford_rice(K_values, beta);
1
DCRUST|CHE 20001005014
int iterations = 0;
while (abs(f_beta) > EPSILON && iterations < MAX_ITERATIONS)
{
double f_prime_beta = 0.0;
for (int i = 0; i < K_values.size(); ++i)
{
double denominator = 1.0 + beta * (K_values[i] - 1.0);
f_prime_beta += -pow(K_values[i] - 1.0, 2) / pow(denominator, 2);
}
beta -= f_beta / f_prime_beta;
f_beta = rachford_rice(K_values, beta);
++iterations;
}
return beta;
}
int main()
{
int n;
cout << "Enter the number of components: ";
cin >> n;
vector<double> K_values(n);
cout << "Enter the K-values for each component:\n";
for (int i = 0; i < n; ++i)
{
cout << "K" << i + 1 << ": ";
cin >> K_values[i];
}
double beta = solve_flash_calculation(K_values);
cout << "\nFlash Calculation Results:" << endl;
cout << "Liquid Fraction (Beta): " << beta << endl;
2
DCRUST|CHE 20001005014
cout << "Vapor Fraction: " << 1.0 - beta << endl;
cout << "Equilibrium Compositions:" << endl;
for (int i = 0; i < n; ++i)
{
double liquid_composition = beta * K_values[i] / (1.0 + beta * (K_values[i] - 1.0));
double vapor_composition = (K_values[i] - 1.0) * (1.0 - beta) / (1.0 + beta *
(K_values[i] - 1.0));
cout << "Component " << i + 1 << ": Liquid: " << liquid_composition << ", Vapor: " <<
vapor_composition << endl;
}
return 0;
int getch(void);
}
Output:
3
DCRUST|CHE 20001005014
Program No.2
Title:
Write a program in C++ to simulate a gravity flow tank.
Theory:
A gravity flow tank refers to a type of tank or reservoir that relies on gravitational forces to
facilitate the movement or flow of fluids within the system. The basic principle is that liquids
flow from a higher elevation to a lower elevation due to the influence of gravity. Gravity flow
tanks are commonly used in various applications, including water supply systems, irrigation,
and industrial processes.
Code:
#include<iostream>
#include<vector>
using namespace std;
const double g = 9.81;
const double dt = 0.1;
double update_tank_level(double current_level, double inflow_rate, double outflow_rate)
{
double new_level = current_level + (inflow_rate - outflow_rate) * dt;
return new_level;
}
int main()
{
double tank_level = 0.0;
double inflow_rate, outflow_rate;
cout<<"Enter the initial tank level (meters): ";
cin>>tank_level;
cout<<"Enter the inflow rate (m^3/s): ";
cin>>inflow_rate;
cout<<"Enter the outflow rate (m^3/s): ";
cin>>outflow_rate;
int simulation_steps;
4
DCRUST|CHE 20001005014
cout<<"Enter the number of simulation steps: ";
cin>>simulation_steps;
cout<<"\nSimulation Results:" << endl;
for (int i = 0; i < simulation_steps; ++i)
{
cout << "Step " << i + 1 << ": Tank level = " << tank_level << " meters" << endl;
tank_level = update_tank_level(tank_level, inflow_rate, outflow_rate);
if (tank_level < 0)
{
tank_level = 0;
}
}
return 0;
}
Output:
5
DCRUST|CHE 20001005014
Program No.3
Title:
Write a program in C++ to simulate an isothermal continuous stirred tank reactor in an open
loop.
Theory:
An isothermal continuous stirred tank reactor (CSTR) in an open-loop system refers to a
specific type of chemical reactor configuration where reactants are continuously fed into the
reactor, and products are continuously withdrawn. The term "isothermal" indicates that the
reactor is maintained at a constant temperature throughout the reaction.
Code:
#include <iostream>
using namespace std;
const double V = 10.0;
const double V0 = 5.0;
const double k = 0.1;
double update_reactor_concentration(double current_concentration, double inflow_rate)
{
double reaction_rate=k*current_concentration;
double outflow_rate=inflow_rate;
double dV_dt=inflow_rate-outflow_rate;
double dC_dt=(inflow_rate*V0-current_concentration*dV_dt)/V;
return current_concentration+dC_dt;
}
int main()
{
double concentration=1.0;
double inflow_rate;
cout<<"Enter the initial concentration of reactant (mol/m^3): ";
cin>>concentration;
cout<<"Enter the inflow rate (m^3/s): ";
cin>>inflow_rate;
6
DCRUST|CHE 20001005014
int simulation_steps;
cout<<"Enter the number of simulation steps: ";
cin>>simulation_steps;
cout<<"\nSimulation Results:" << endl;
for(int i=0;i<simulation_steps;i++)
{
cout<<"Step "<<i+1<<": Concentration = "<<concentration<<" mol/m^3"<< endl;
concentration=update_reactor_concentration(concentration,inflow_rate);
}
return 0;
}
Output:
7
DCRUST|CHE 20001005014
Program No.4
Title:
Write a program in C++ to simulate an isothermal continuous stirred tank reactor in a closed
loop.
Theory:
An isothermal continuous stirred tank reactor (CSTR) in a closed-loop system involves a
feedback control mechanism to maintain the reactor at a constant temperature. The closed-
loop system is designed to adjust and regulate various operating parameters based on
feedback from sensors, ensuring the reactor operates under isothermal conditions.
Code:
#include <iostream>
#include <vector>
using namespace std;
double derivative(double c, double k, double v, double q, double cin)
{
return (q / v) * (cin - c) - k * c;
}
int main()
{
const double k = 0.3;
const double v = 10.0;
const double q = 5.0;
const double cin = 2.0;
const double t_end = 5.0;
const double dt = 0.1;
double c = 0.0;
for (double t = 0; t <= t_end; t += dt)
{
double dc_dt = derivative(c, k, v, q, cin);
c += dc_dt * dt;
cout << "Time: " << t << " Concentration: " << c << endl;
8
DCRUST|CHE 20001005014
}
return 0;
}
Output:
9
DCRUST|CHE 20001005014
Program No.5
Title:
Write a program in C++ to simulate a non-isothermal continuous stirred tank reactor.
Theory:
A non-isothermal continuous stirred tank reactor (CSTR) is a type of chemical reactor where
the temperature of the reaction mixture is not held constant throughout the process. In
contrast to an isothermal reactor, where the temperature remains the same, a non-isothermal
reactor allows for temperature variations during the course of the reaction. This type of
reactor is commonly used when the reaction is exothermic or endothermic, meaning it either
releases or absorbs heat.
Code:
#include<iostream>
#include<vector>
#include<math.h>
using namespace std;
double concentration_derivative(double c, double k, double v, double q, double cin)
{
return (q / v) * (cin - c) - k * c;
}
double temperature_derivative(double t, double k0, double Ea, double R, double c, double cp,
double h, double Tc)
{
return (k0 * exp(-Ea / (R * t)) * c - h * (t - Tc)) / (cp);
}
int main()
{
const double k = 0.3;
const double v = 10.0;
const double q = 5.0;
const double cin = 2.0;
const double k0 = 1.0;
const double Ea = 50.0;
10
DCRUST|CHE 20001005014
const double R = 8.314;
const double cp = 4.184;
const double h = 0.2;
const double Tc = 300.0;
const double t_end = 5.0;
const double dt = 0.1;
double c = 0.0;
double t = 350.0;
for (double time = 0; time <= t_end; time += dt)
{
double dc_dt = concentration_derivative(c, k, v, q, cin);
double dt_dt = temperature_derivative(t, k0, Ea, R, c, cp, h, Tc);
c += dc_dt * dt;
t += dt_dt * dt;
cout << "Time: " << time << " Concentration: " << c << " Temperature: " << t << endl;
}
return 0;
}
11
DCRUST|CHE 20001005014
Output:
12
DCRUST|CHE 20001005014
13
DCRUST|CHE 20001005014
14
DCRUST|CHE 20001005014
Program No.6
Title:
Write a program in C++ to simulate an isothermal batch reactor.
Theory:
An isothermal batch reactor is a type of chemical reactor in which a fixed quantity of
reactants is processed in a closed system, and the temperature within the reactor is maintained
at a constant level throughout the reaction. The term "batch" implies that the reactants are
loaded into the reactor at the beginning of the process, and the reaction progresses until the
desired conversion is achieved or a specified time has elapsed.
Code:
#include <iostream>
#include <vector>
using namespace std;
double derivative(double c, double k)
{
return -k * c;
}
int main()
{
const double k = 0.3;
const double c0 = 2.0;
const double t_end = 5.0;
const double dt = 0.1;
double c = c0;
for (double t = 0; t <= t_end; t += dt)
{
double dc_dt = derivative(c, k);
c += dc_dt * dt;
cout << "Time: " << t << " Concentration: " << c << endl;
}
return 0;
15
DCRUST|CHE 20001005014
}
Output:
16
DCRUST|CHE 20001005014
17
DCRUST|CHE 20001005014
Program No.7
Title:
Write a program in C++ to simulate a non-isothermal batch reactor.
Theory:
A non-isothermal batch reactor is a type of chemical reactor in which the temperature of the
reaction mixture can vary during the course of the reaction. Unlike an isothermal batch
reactor, where the temperature is held constant, a non-isothermal reactor allows for
temperature changes due to heat generation or absorption associated with the chemical
reaction.
Code:
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
double concentration_derivative(double c, double k)
{
return -k * c;
}
double temperature_derivative(double t, double k0, double Ea, double R, double c, double cp,
double h, double Tc)
{
return (k0 * exp(-Ea / (R * t)) * c - h * (t - Tc)) / (cp);
}
int main()
{
const double k = 0.3;
const double k0 = 1.0;
const double Ea = 50.0;
const double R = 8.314;
const double cp = 4.184;
const double h = 0.2;
const double Tc = 300.0;
18
DCRUST|CHE 20001005014
const double c0 = 2.0;
const double t0 = 350.0;
const double t_end = 10.0;
const double dt = 0.1;
double c = c0;
double t = t0;
for (double time = 0; time <= t_end; time += dt)
{
double dc_dt = concentration_derivative(c, k);
double dt_dt = temperature_derivative(t, k0, Ea, R, c, cp, h, Tc);
c += dc_dt * dt;
t += dt_dt * dt;
cout << "Time: " << time << " Concentration: " << c << " Temperature: " << t << endl;
}
return 0;
}
Output:
19
DCRUST|CHE 20001005014
20
DCRUST|CHE 20001005014
Program No.8
Title:
Write a program in C++ to simulate an ideal binary distillation column.
Theory:
An ideal binary distillation column is a theoretical concept used in chemical engineering to
analyse and understand the separation of a mixture of two components (binary mixture)
through distillation. The term "ideal" implies that the column operates under certain
simplified assumptions, making the analysis more tractable for theoretical considerations.
Code:
#include <iostream>
#include <vector>
using namespace std;
double liquid_derivative(double L, double V, double F, double K, double x)
{
return (F * (K * (V / L) - 1.0) + L * x - V * K * x) / L;
}
double vapor_derivative(double L, double V, double F, double K, double x)
{
return (F * (K - 1.0) + V * (x - K * x)) / V;
}
int main()
{
const double L0 = 0.6;
const double V0 = 0.4;
const double F = 1.0;
const double K = 2.0;
const double x0 = 0.4;
const double t_end = 5.0;
const double dt = 0.1;
double L = L0;
double V = V0;
21
DCRUST|CHE 20001005014
double x = x0;
for (double t = 0; t <= t_end; t += dt) {
double dL_dt = liquid_derivative(L, V, F, K, x);
double dV_dt = vapor_derivative(L, V, F, K, x);
L += dL_dt * dt;
V += dV_dt * dt;
x = (L * x) / (L + V * K);
cout << "Time: " << t << " Liquid Flow Rate: " << L << " Vapor Flow Rate: " << V <<
" Liquid Composition: " << x << endl;
}
return 0;
}
Output:
22
DCRUST|CHE 20001005014
23
DCRUST|CHE 20001005014
Program No.9
Title:
Write a program in C++ to simulate an multi component distillation column.
Theory:
A multi-component distillation column, also known as a fractional distillation column, is a
type of chemical separation equipment used to separate a mixture of more than two
components into its individual constituents based on differences in boiling points. Unlike the
idealized binary distillation column, a multi-component column is designed to handle
mixtures containing three or more components.
Code:
#include <iostream>
#include <vector>
using namespace std;
vector<double> liquid_derivative(const vector<double>& L, const vector<double>& V,
const vector<double>& F,
const vector<vector<double>>& K, const vector<vector<double>>& x)
{
vector<double> dL_dt(L.size());
for (size_t i = 0; i < L.size(); ++i)
{
double sum = 0.0;
for (size_t j = 0; j < L.size(); ++j)
{
sum += F[j] * (K[j][i] * (V[i] / L[i]) - 1.0) + L[j] * x[j][i] - V[j] * K[j][i] * x[j][i];
}
dL_dt[i] = sum / L[i];
}
return dL_dt;
}
vector<double> vapor_derivative(const vector<double>& L, const vector<double>& V,
const vector<double>& F,
const vector<vector<double>>& K, const vector<vector<double>>& x)
24
DCRUST|CHE 20001005014
{
vector<double> dV_dt(V.size());
for (size_t i = 0; i < V.size(); ++i)
{
double sum = 0.0;
for (size_t j = 0; j < V.size(); ++j)
{
sum += F[j] * (K[j][i] - 1.0) + V[j] * (x[j][i] - K[j][i] * x[j][i]);
}
dV_dt[i] = sum / V[i];
}
return dV_dt;
}
int main()
{
const vector<double> L0 = {0.6, 0.4};
const vector<double> V0 = {0.4, 0.6};
const vector<double> F = {1.0, 1.0};
const vector<vector<double>> K = {{1.5, 2.5}, {2.0, 1.8}};
const vector<vector<double>> x0 = {{0.4, 0.3}, {0.6, 0.7}};
const double t_end = 1.0;
const double dt = 0.1;
vector<double> L = L0;
vector<double> V = V0;
vector<vector<double>> x = x0;
for (double t = 0; t <= t_end; t += dt)
{
vector<double> dL_dt = liquid_derivative(L, V, F, K, x);
vector<double> dV_dt = vapor_derivative(L, V, F, K, x);
for (size_t i = 0; i < L.size(); ++i)
25
DCRUST|CHE 20001005014
{
L[i] += dL_dt[i] * dt;
V[i] += dV_dt[i] * dt;
}
for (size_t i = 0; i < x.size(); ++i)
{
for (size_t j = 0; j < x[i].size(); ++j)
{
x[i][j] = (L[j] * x[j][i]) / (L[j] + V[j] * K[j][i]);
}
}
cout << "Time: " << t << endl;
cout << "Liquid Flow Rates: ";
for (const auto &l : L)
{
cout << l << " ";
}
cout << endl;
cout << "Vapor Flow Rates: ";
for (const auto &v : V)
{
cout << v << " ";
}
cout << endl;
cout << "Liquid Compositions: " << endl;
for (const auto &row : x)
{
for (const auto &val : row)
{
cout << val << " ";
26
DCRUST|CHE 20001005014
}
cout << endl;
}
cout << endl;
}
return 0;
}
Output:
27
DCRUST|CHE 20001005014
28
DCRUST|CHE 20001005014