Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Case Study 2 - Solving sets of equations - Example separation train
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from scipy.optimize import fsolve"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Specify problem parameters \n",
"Problem specifies: \n",
" - feed rate to column 1, F \n",
" - mole fraction for feed to column 1, F_mf \n",
" - mole fraction of all product streams, D1_mf .. B2_mf"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"F = 70.0\n",
"F_mf = np.array([0.15, 0.25, 0.40, 0.20])\n",
"D1_mf = np.array([0.07, 0.04, 0.54, 0.35])\n",
"B1_mf = np.array([0.18, 0.24, 0.42, 0.16])\n",
"D2_mf = np.array([0.15, 0.10, 0.54, 0.21])\n",
"B2_mf = np.array([0.24, 0.65, 0.10, 0.01])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Initialise parameters to be solved\n",
"Initialise the variables for the unspecified molar flowrates and mole fractions."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"D,B,D1,B1,D2,B2 = [10.0]*6\n",
"D_mf = np.array([0.1]*4)\n",
"B_mf = np.array([0.1]*4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Equations describing the array of distillation columns \n",
"- Sum of mole fractions for each stream to sum to 1.0\n",
"- Mass balance for each of columns 1, 2 and 3"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def mass_balance(x):\n",
" D, B, D1, B1, D2, B2 = x[0:6]\n",
" D_mf = np.array(x[6:10])\n",
" B_mf = np.array(x[10:15])\n",
" \n",
" error_1 = 1 - sum(D_mf)\n",
" error_2 = 1 - sum(B_mf)\n",
" \n",
" error_mf_1 = D *D_mf + B *B_mf - F*F_mf\n",
" error_mf_2 = D1*D1_mf + B1*B1_mf - D*D_mf\n",
" error_mf_3 = D2*D2_mf + B2*B2_mf - B*B_mf\n",
" \n",
" return np.append([error_1, error_2], [error_mf_1, error_mf_2, error_mf_3])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Problem solution\n",
"\n",
"Solve the set of equations using fsolve from the numpy library.\n",
"Note that the variables to be solved are based through the fsolve method as a single array."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"solution = fsolve(mass_balance, np.append([D, B, D1, B1, D2, B2],[D_mf, B_mf]))\n",
"\n",
"D, B, D1, B1, D2, B2 = solution[0:6]\n",
"D_mf = solution[6:10]\n",
"B_mf = solution[10:15]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The molar flowrate and mole fractions for all streams are given below:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1mPrimary column\u001b[0m\n",
"stream D:\n",
"molar flow = 43.75 mol/min\n",
"xylene = 0.11\n",
"styrene = 0.12\n",
"toluene = 0.49\n",
"benzene = 0.27\n",
"\n",
"stream B:\n",
"molar flow = 26.25 mol/min\n",
"xylene = 0.21\n",
"styrene = 0.47\n",
"toluene = 0.25\n",
"benzene = 0.08\n",
"\n",
"\u001b[1mDistillate column\u001b[0m\n",
"stream D1:\n",
"molar flow = 26.25 mol/min\n",
"xylene = 0.07\n",
"styrene = 0.04\n",
"toluene = 0.54\n",
"benzene = 0.35\n",
"\n",
"stream B1:\n",
"molar flow = 17.50 mol/min\n",
"xylene = 0.18\n",
"styrene = 0.24\n",
"toluene = 0.42\n",
"benzene = 0.16\n",
"\n",
"\u001b[1mBottoms column\u001b[0m\n",
"stream D2:\n",
"molar flow = 8.75 mol/min\n",
"xylene = 0.15\n",
"styrene = 0.10\n",
"toluene = 0.54\n",
"benzene = 0.21\n",
"\n",
"stream B2:\n",
"molar flow = 17.50 mol/min\n",
"xylene = 0.24\n",
"styrene = 0.65\n",
"toluene = 0.10\n",
"benzene = 0.01\n"
]
}
],
"source": [
"class style:\n",
" BOLD = '\\033[1m'\n",
" END = '\\033[0m'\n",
"\n",
"print(style.BOLD + 'Primary column' + style.END)\n",
"print('stream D:')\n",
"print('molar flow = %.2f mol/min' %D)\n",
"print('xylene = %.2f' %D_mf[0])\n",
"print('styrene = %.2f' %D_mf[1])\n",
"print('toluene = %.2f' %D_mf[2])\n",
"print('benzene = %.2f' %D_mf[3])\n",
"print('')\n",
"print('stream B:')\n",
"print('molar flow = %.2f mol/min' %B)\n",
"print('xylene = %.2f' %B_mf[0])\n",
"print('styrene = %.2f' %B_mf[1])\n",
"print('toluene = %.2f' %B_mf[2])\n",
"print('benzene = %.2f' %B_mf[3])\n",
"\n",
"print('')\n",
"print(style.BOLD + 'Distillate column' + style.END)\n",
"print('stream D1:')\n",
"print('molar flow = %.2f mol/min' %D1)\n",
"print('xylene = %.2f' %D1_mf[0])\n",
"print('styrene = %.2f' %D1_mf[1])\n",
"print('toluene = %.2f' %D1_mf[2])\n",
"print('benzene = %.2f' %D1_mf[3])\n",
"print('')\n",
"print('stream B1:')\n",
"print('molar flow = %.2f mol/min' %B1)\n",
"print('xylene = %.2f' %B1_mf[0])\n",
"print('styrene = %.2f' %B1_mf[1])\n",
"print('toluene = %.2f' %B1_mf[2])\n",
"print('benzene = %.2f' %B1_mf[3])\n",
"\n",
"print('')\n",
"print(style.BOLD + 'Bottoms column' + style.END)\n",
"print('stream D2:')\n",
"print('molar flow = %.2f mol/min' %D2)\n",
"print('xylene = %.2f' %D2_mf[0])\n",
"print('styrene = %.2f' %D2_mf[1])\n",
"print('toluene = %.2f' %D2_mf[2])\n",
"print('benzene = %.2f' %D2_mf[3])\n",
"print('')\n",
"print('stream B2:')\n",
"print('molar flow = %.2f mol/min' %B2)\n",
"print('xylene = %.2f' %B2_mf[0])\n",
"print('styrene = %.2f' %B2_mf[1])\n",
"print('toluene = %.2f' %B2_mf[2])\n",
"print('benzene = %.2f' %B2_mf[3])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment