{ "cells": [ { "cell_type": "markdown", "id": "024f7bb5", "metadata": {}, "source": [ "# Gas Mixing Calculation Using AGA8 GERG-2008\n", "\n", "This example demonstrates how to calculate the resulting gas composition when mixing multiple streams with different compositions, pressures, and temperatures.\n", "\n", "## Scenario\n", "\n", "Three gas streams are involved:\n", "- **Gas 1** and **Gas 2** are produced into a single mixing point\n", "- A reactor removes **Gas 3** from this mixture\n", "- **Stream 4** (output) = Gas 1 + Gas 2 - Gas 3\n", "\n", "Each stream is measured by an ultrasonic flowmeter (volume flowrate Q in m³/h). We'll use GERG-2008 to calculate density, then mass flowrate, and finally the composition of Stream 4." ] }, { "cell_type": "markdown", "id": "24724a19", "metadata": {}, "source": [ "## 1. Import Required Libraries" ] }, { "cell_type": "code", "execution_count": 1, "id": "60542b13", "metadata": {}, "outputs": [], "source": [ "import pvtlib\n", "import pandas as pd\n", "import numpy as np" ] }, { "cell_type": "markdown", "id": "bd49ffe2", "metadata": {}, "source": [ "## 2. Define Input Parameters\n", "\n", "Input data for the three gas streams:" ] }, { "cell_type": "code", "execution_count": 2, "id": "57d02fdf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Input Parameters:\n", " Gas 1 Gas 2 Gas 3\n", "Q [m³/h] 5000 3000 500\n", "P [bara] 150 130 30\n", "T [°C] 50 40 30\n", "\n", "Gas Compositions [mole %]:\n", "Gas 1: {'N2': 1, 'CO2': 2, 'C1': 90, 'C2': 6, 'C3': 0.9, 'iC4': 0.05, 'nC4': 0.05}\n", "Gas 2: {'N2': 10, 'CO2': 5, 'C1': 81, 'C2': 4}\n", "Gas 3: {'N2': 10, 'CO2': 60, 'C1': 30}\n" ] } ], "source": [ "# Volume flowrates [m3/h]\n", "Q_gas1 = 5000\n", "Q_gas2 = 3000\n", "Q_gas3 = 500\n", "\n", "# Pressures [bara]\n", "P_gas1 = 150\n", "P_gas2 = 130\n", "P_gas3 = 30\n", "\n", "# Temperatures [°C]\n", "T_gas1 = 50\n", "T_gas2 = 40\n", "T_gas3 = 30\n", "\n", "# Gas compositions [mole %]\n", "comp_gas1 = {\n", " 'N2': 1,\n", " 'CO2': 2,\n", " 'C1': 90,\n", " 'C2': 6,\n", " 'C3': 0.9,\n", " 'iC4': 0.05,\n", " 'nC4': 0.05\n", "}\n", "\n", "comp_gas2 = {\n", " 'N2': 10,\n", " 'CO2': 5,\n", " 'C1': 81,\n", " 'C2': 4\n", "}\n", "\n", "comp_gas3 = {\n", " 'N2': 10,\n", " 'CO2': 60,\n", " 'C1': 30\n", "}\n", "\n", "# Display input data\n", "input_df = pd.DataFrame({\n", " 'Gas 1': [Q_gas1, P_gas1, T_gas1],\n", " 'Gas 2': [Q_gas2, P_gas2, T_gas2],\n", " 'Gas 3': [Q_gas3, P_gas3, T_gas3]\n", "}, index=['Q [m³/h]', 'P [bara]', 'T [°C]'])\n", "\n", "print(\"Input Parameters:\")\n", "print(input_df)\n", "print(\"\\nGas Compositions [mole %]:\")\n", "print(f\"Gas 1: {comp_gas1}\")\n", "print(f\"Gas 2: {comp_gas2}\")\n", "print(f\"Gas 3: {comp_gas3}\")" ] }, { "cell_type": "markdown", "id": "8154f5df", "metadata": {}, "source": [ "## 3. Calculate Gas Properties Using GERG-2008\n", "\n", "Use the AGA8 GERG-2008 equation of state to calculate density and other properties for each stream." ] }, { "cell_type": "code", "execution_count": 3, "id": "af1f3729", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Calculated Densities using GERG-2008:\n", "Gas 1: ρ = 118.400 kg/m³\n", "Gas 2: ρ = 111.650 kg/m³\n", "Gas 3: ρ = 44.622 kg/m³\n" ] } ], "source": [ "# Initialize GERG-2008\n", "gerg = pvtlib.AGA8('GERG-2008')\n", "\n", "# Calculate properties for Gas 1\n", "gerg_gas1 = gerg.calculate_from_PT(\n", " composition=comp_gas1,\n", " pressure=P_gas1,\n", " temperature=T_gas1,\n", " pressure_unit='bara',\n", " temperature_unit='C'\n", ")\n", "\n", "# Calculate properties for Gas 2\n", "gerg_gas2 = gerg.calculate_from_PT(\n", " composition=comp_gas2,\n", " pressure=P_gas2,\n", " temperature=T_gas2,\n", " pressure_unit='bara',\n", " temperature_unit='C'\n", ")\n", "\n", "# Calculate properties for Gas 3\n", "gerg_gas3 = gerg.calculate_from_PT(\n", " composition=comp_gas3,\n", " pressure=P_gas3,\n", " temperature=T_gas3,\n", " pressure_unit='bara',\n", " temperature_unit='C'\n", ")\n", "\n", "# Display densities\n", "print(\"Calculated Densities using GERG-2008:\")\n", "print(f\"Gas 1: ρ = {gerg_gas1['rho']:.3f} kg/m³\")\n", "print(f\"Gas 2: ρ = {gerg_gas2['rho']:.3f} kg/m³\")\n", "print(f\"Gas 3: ρ = {gerg_gas3['rho']:.3f} kg/m³\")" ] }, { "cell_type": "markdown", "id": "74ce09b2", "metadata": {}, "source": [ "## 4. Calculate Mass Flowrates\n", "\n", "Calculate mass flowrate from volume flowrate and density:\n", "$$\\dot{m} = Q \\times \\rho$$\n", "\n", "where:\n", "- $\\dot{m}$ = mass flowrate [kg/h]\n", "- $Q$ = volumetric flowrate [m³/h]\n", "- $\\rho$ = density [kg/m³]" ] }, { "cell_type": "code", "execution_count": 4, "id": "18c3a33e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Mass Flowrate Calculations:\n", " Gas 1 Gas 2 Gas 3\n", "Q [m³/h] 5000.00 3000.00 500.00\n", "ρ [kg/m³] 118.40 111.65 44.62\n", "Mass Flow [kg/h] 592000.58 334948.81 22310.89\n" ] } ], "source": [ "# Calculate mass flowrates [kg/h]\n", "mass_flow_gas1 = Q_gas1 * gerg_gas1['rho']\n", "mass_flow_gas2 = Q_gas2 * gerg_gas2['rho']\n", "mass_flow_gas3 = Q_gas3 * gerg_gas3['rho']\n", "\n", "# Display results\n", "mass_flow_df = pd.DataFrame({\n", " 'Gas 1': [Q_gas1, gerg_gas1['rho'], mass_flow_gas1],\n", " 'Gas 2': [Q_gas2, gerg_gas2['rho'], mass_flow_gas2],\n", " 'Gas 3': [Q_gas3, gerg_gas3['rho'], mass_flow_gas3]\n", "}, index=['Q [m³/h]', 'ρ [kg/m³]', 'Mass Flow [kg/h]'])\n", "\n", "print(\"Mass Flowrate Calculations:\")\n", "print(mass_flow_df.round(2))" ] }, { "cell_type": "markdown", "id": "f0ee0583", "metadata": {}, "source": [ "## 5. Calculate Stream 4 Composition\n", "\n", "Use the AGA8 mix function to calculate the resulting composition:\n", "\n", "**Stream 4 = Gas 1 + Gas 2 - Gas 3**\n", "\n", "The mix function takes a list of compositions and their corresponding masses (negative mass for subtraction)." ] }, { "cell_type": "code", "execution_count": 5, "id": "5d9fb5ea", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Stream 4 Results:\n", "\n", "Total Mass Flowrate: 904638.50 kg/h\n", "\n", "Composition [mole %]:\n", " C1: 87.644%\n", " C2: 5.380%\n", " N2: 4.026%\n", " CO2: 2.286%\n", " C3: 0.597%\n", " iC4: 0.033%\n", " nC4: 0.033%\n" ] } ], "source": [ "# Mix the streams: Gas1 + Gas2 - Gas3\n", "# Note: Gas 3 has negative mass (subtraction)\n", "stream4 = gerg.mix(\n", " compositions=[comp_gas1, comp_gas2, comp_gas3],\n", " mix_weights=[mass_flow_gas1, mass_flow_gas2, -mass_flow_gas3]\n", ")\n", "\n", "# Display results\n", "print(\"Stream 4 Results:\")\n", "print(f\"\\nTotal Mass Flowrate: {stream4['total_mass']:.2f} kg/h\")\n", "print(f\"\\nComposition [mole %]:\")\n", "for component, mole_percent in sorted(stream4['composition'].items(), \n", " key=lambda x: x[1], reverse=True):\n", " print(f\" {component:>4s}: {mole_percent:6.3f}%\")" ] }, { "cell_type": "markdown", "id": "626cefea", "metadata": {}, "source": [ "## 5.1 Calculate Stream 4 Volume Flowrate and Velocity\n", "\n", "Calculate the volume flowrate and velocity of Stream 4 in a 20\" FC10A pipe (ID = 408 mm).\n", "\n", "**Note:** For Stream 4, we assume the gas is at Gas 1 conditions (P = 150 bara, T = 50°C)." ] }, { "cell_type": "code", "execution_count": 6, "id": "2a588ec1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Stream 4 Flow Calculations:\n", "------------------------------------------------------------\n", "Pipe: 20\" FC10A, ID = 408 mm\n", "Conditions: P = 150 bara, T = 50 °C\n", "Density: ρ = 118.378 kg/m³\n", "\n", "Volume Flowrate: Q = 7641.94 m³/h (2.1228 m³/s)\n", "Velocity: v = 16.24 m/s\n" ] } ], "source": [ "# Assume Stream 4 is at Gas 1 conditions\n", "P_stream4 = P_gas1 # bara\n", "T_stream4 = T_gas1 # °C\n", "\n", "# Calculate GERG properties for Stream 4 composition\n", "gerg_stream4 = gerg.calculate_from_PT(\n", " composition=stream4['composition'],\n", " pressure=P_stream4,\n", " temperature=T_stream4,\n", " pressure_unit='bara',\n", " temperature_unit='C'\n", ")\n", "\n", "# Calculate volume flowrate [m³/h]\n", "# Q = mass_flow / density\n", "Q_stream4 = stream4['total_mass'] / gerg_stream4['rho']\n", "\n", "# Pipe geometry\n", "pipe_ID = 408 # mm\n", "pipe_ID_m = pipe_ID / 1000 # Convert to meters\n", "\n", "# Calculate cross-sectional area [m²]\n", "pipe_area = np.pi * (pipe_ID_m / 2)**2\n", "\n", "# Calculate velocity [m/s]\n", "# v = Q / A, but Q is in m³/h, so convert to m³/s\n", "Q_stream4_m3s = Q_stream4 / 3600 # Convert m³/h to m³/s\n", "velocity_stream4 = Q_stream4_m3s / pipe_area\n", "\n", "# Display results\n", "print(\"Stream 4 Flow Calculations:\")\n", "print(\"-\" * 60)\n", "print(f\"Pipe: 20\\\" FC10A, ID = {pipe_ID} mm\")\n", "print(f\"Conditions: P = {P_stream4} bara, T = {T_stream4} °C\")\n", "print(f\"Density: ρ = {gerg_stream4['rho']:.3f} kg/m³\")\n", "print(f\"\\nVolume Flowrate: Q = {Q_stream4:.2f} m³/h ({Q_stream4_m3s:.4f} m³/s)\")\n", "print(f\"Velocity: v = {velocity_stream4:.2f} m/s\")" ] }, { "cell_type": "markdown", "id": "d0e72ba2", "metadata": {}, "source": [ "## 6. Summary\n", "\n", "Create a comprehensive summary table showing all streams and their properties." ] }, { "cell_type": "code", "execution_count": 7, "id": "e044616d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "======================================================================\n", "SUMMARY - GAS MIXING CALCULATION\n", "======================================================================\n", "\n", "Stream Properties:\n", " Gas 1 Gas 2 Gas 3 Stream 4\n", "Q [m³/h] 5000 3000 -500 7641.94\n", "P [bara] 150 130 30 150.00\n", "T [°C] 50 40 30 50.00\n", "ρ [kg/m³] 118.400116 111.649603 44.62178 118.38\n", "Mass Flow [kg/h] 592000.58238 334948.809611 -22310.889932 904638.50\n", "Velocity [m/s] - - - 16.24\n", "\n", "\n", "Stream 4 Composition (sorted by mole %):\n", "----------------------------------------\n", " C1: 87.644%\n", " C2: 5.380%\n", " N2: 4.026%\n", " CO2: 2.286%\n", " C3: 0.597%\n", " iC4: 0.033%\n", " nC4: 0.033%\n", "----------------------------------------\n", " Total: 100.000%\n", "\n", "======================================================================\n", "Stream 4 = Gas 1 + Gas 2 - Gas 3\n", " = 592000.58 + 334948.81 - 22310.89\n", " = 904638.50 kg/h\n", "\n", "Stream 4 Flow (20\" FC10A pipe, ID = 408 mm):\n", " Q = 7641.94 m³/h\n", " v = 16.24 m/s\n", "======================================================================\n" ] } ], "source": [ "# Create comprehensive summary\n", "summary_df = pd.DataFrame({\n", " 'Gas 1': [Q_gas1, P_gas1, T_gas1, gerg_gas1['rho'], mass_flow_gas1, '-'],\n", " 'Gas 2': [Q_gas2, P_gas2, T_gas2, gerg_gas2['rho'], mass_flow_gas2, '-'],\n", " 'Gas 3': [-Q_gas3, P_gas3, T_gas3, gerg_gas3['rho'], -mass_flow_gas3, '-'],\n", " 'Stream 4': [Q_stream4, P_stream4, T_stream4, gerg_stream4['rho'], stream4['total_mass'], velocity_stream4]\n", "}, index=['Q [m³/h]', 'P [bara]', 'T [°C]', 'ρ [kg/m³]', 'Mass Flow [kg/h]', 'Velocity [m/s]'])\n", "\n", "print(\"=\" * 70)\n", "print(\"SUMMARY - GAS MIXING CALCULATION\")\n", "print(\"=\" * 70)\n", "print(\"\\nStream Properties:\")\n", "print(summary_df.round(2))\n", "\n", "print(\"\\n\\nStream 4 Composition (sorted by mole %):\")\n", "print(\"-\" * 40)\n", "for component, mole_percent in sorted(stream4['composition'].items(), \n", " key=lambda x: x[1], reverse=True):\n", " print(f\" {component:>4s}: {mole_percent:7.3f}%\")\n", "print(\"-\" * 40)\n", "print(f\" Total: {sum(stream4['composition'].values()):7.3f}%\")\n", "\n", "print(\"\\n\" + \"=\" * 70)\n", "print(f\"Stream 4 = Gas 1 + Gas 2 - Gas 3\")\n", "print(f\" = {mass_flow_gas1:.2f} + {mass_flow_gas2:.2f} - {mass_flow_gas3:.2f}\")\n", "print(f\" = {stream4['total_mass']:.2f} kg/h\")\n", "print(f\"\\nStream 4 Flow (20\\\" FC10A pipe, ID = 408 mm):\")\n", "print(f\" Q = {Q_stream4:.2f} m³/h\")\n", "print(f\" v = {velocity_stream4:.2f} m/s\")\n", "print(\"=\" * 70)" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "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.11.7" } }, "nbformat": 4, "nbformat_minor": 5 }