Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 4 additions & 30 deletions docs/source/examples/cstr_reaction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,7 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"from pathsim import Simulation, Connection\n",
"from pathsim.blocks import Source, Scope\n",
"\n",
"from pathsim_chem.process import CSTR"
]
"source": "import matplotlib.pyplot as plt\n\nplt.style.use('../pathsim_docs.mplstyle')\n\nfrom pathsim import Simulation, Connection\nfrom pathsim.blocks import Source, Scope\n\nfrom pathsim_chem.process import CSTR"
},
{
"cell_type": "markdown",
Expand All @@ -48,7 +41,7 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "cstr = CSTR(\n V=1.0, # reactor volume [m³]\n F=0.05, # volumetric flow rate [m³/s] -> tau = 20 s\n k0=1e6, # pre-exponential factor [1/s]\n Ea=40000.0, # activation energy [J/mol]\n n=1.0, # first-order reaction\n dH_rxn=-40000.0, # exothermic [J/mol]\n rho=1000.0, # density [kg/m³]\n Cp=4184.0, # heat capacity [J/(kg·K)]\n UA=800.0, # cooling jacket [W/K]\n C_A0=0.0, # start empty\n T0=300.0, # initial temperature [K]\n)"
"source": "reactor = CSTR(\n V=1.0, # reactor volume [m³]\n F=0.05, # volumetric flow rate [m³/s] -> tau = 20 s\n k0=1e6, # pre-exponential factor [1/s]\n Ea=40000.0, # activation energy [J/mol]\n n=1.0, # first-order reaction\n dH_rxn=-40000.0, # exothermic [J/mol]\n rho=1000.0, # density [kg/m³]\n Cp=4184.0, # heat capacity [J/(kg·K)]\n UA=800.0, # cooling jacket [W/K]\n C_A0=0.0, # start empty\n T0=300.0, # initial temperature [K]\n)"
},
{
"cell_type": "markdown",
Expand All @@ -60,33 +53,14 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "# Constant feed and coolant conditions\nC_feed = Source(func=lambda t: 1000.0) # feed concentration [mol/m³]\nT_feed = Source(func=lambda t: 320.0) # feed temperature [K]\nT_cool = Source(func=lambda t: 290.0) # coolant temperature [K]\n\nscp = Scope(labels=[\"C_A [mol/m³]\", \"T [K]\"])\n\nsim = Simulation(\n blocks=[C_feed, T_feed, T_cool, cstr, scp],\n connections=[\n Connection(C_feed, cstr), # C_in -> port 0\n Connection(T_feed, cstr[1]), # T_in -> port 1\n Connection(T_cool, cstr[2]), # T_c -> port 2\n Connection(cstr, scp), # C_out -> scope port 0\n Connection(cstr[1], scp[1]), # T_out -> scope port 1\n ],\n dt=0.1,\n)\n\nsim.run(200)"
"source": "# Constant feed and coolant conditions\nsrc_c = Source(lambda t: 1000.0) # feed concentration [mol/m³]\nsrc_t = Source(lambda t: 320.0) # feed temperature [K]\nsrc_tc = Source(lambda t: 290.0) # coolant temperature [K]\n\nsco = Scope(labels=[\"C_A [mol/m³]\", \"T [K]\"])\n\nsim = Simulation(\n [src_c, src_t, src_tc, reactor, sco],\n [\n Connection(src_c, reactor), # C_in\n Connection(src_t, reactor[1]), # T_in\n Connection(src_tc, reactor[2]), # T_c\n Connection(reactor, sco), # C_out\n Connection(reactor[1], sco[1]), # T_out\n ],\n dt=0.1,\n log=True,\n)\n\nsim.run(200)"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"time, signals = scp.read()\n",
"\n",
"fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))\n",
"\n",
"ax1.plot(time, signals[0])\n",
"ax1.set_xlabel(\"Time [s]\")\n",
"ax1.set_ylabel(\"Concentration [mol/m³]\")\n",
"ax1.set_title(\"Outlet Concentration\")\n",
"ax1.grid(True, alpha=0.3)\n",
"\n",
"ax2.plot(time, signals[1])\n",
"ax2.set_xlabel(\"Time [s]\")\n",
"ax2.set_ylabel(\"Temperature [K]\")\n",
"ax2.set_title(\"Reactor Temperature\")\n",
"ax2.grid(True, alpha=0.3)\n",
"\n",
"plt.tight_layout()\n",
"plt.show()"
]
"source": "sco.plot(lw=1.5)\nplt.show()"
},
{
"cell_type": "markdown",
Expand Down
107 changes: 7 additions & 100 deletions docs/source/examples/equation_of_state.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,7 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"from pathsim import Simulation, Connection\n",
"from pathsim.blocks import Source, Constant, Scope\n",
"\n",
"from pathsim_chem.thermodynamics import PengRobinson, RedlichKwongSoave"
]
"source": "import matplotlib.pyplot as plt\n\nplt.style.use('../pathsim_docs.mplstyle')\n\nfrom pathsim import Simulation, Connection\nfrom pathsim.blocks import Source, Constant, Scope\n\nfrom pathsim_chem.thermodynamics import PengRobinson, RedlichKwongSoave"
},
{
"cell_type": "markdown",
Expand All @@ -46,23 +39,7 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Critical properties of methane\n",
"Tc, Pc, omega = 190.6, 4.6e6, 0.011\n",
"\n",
"# EoS blocks\n",
"pr = PengRobinson(Tc=Tc, Pc=Pc, omega=omega)\n",
"rks = RedlichKwongSoave(Tc=Tc, Pc=Pc, omega=omega)\n",
"\n",
"# Fixed temperature, logarithmic pressure sweep\n",
"import numpy as np\n",
"T_const = Constant(250) # 250 K (above Tc, supercritical)\n",
"P_src = Source(func=lambda t: 10**(4 + t * 0.035)) # 10 kPa to ~30 MPa over 100s\n",
"\n",
"# Scopes: record Z from both EoS (output port 1)\n",
"scp_pr = Scope(labels=[\"Z_PR\"])\n",
"scp_rks = Scope(labels=[\"Z_RKS\"])"
]
"source": "import numpy as np\n\n# Critical properties of methane\nTc, Pc, omega = 190.6, 4.6e6, 0.011\n\n# EoS blocks\npr = PengRobinson(Tc=Tc, Pc=Pc, omega=omega)\nrks = RedlichKwongSoave(Tc=Tc, Pc=Pc, omega=omega)\n\n# Fixed temperature, logarithmic pressure sweep\nsrc_t = Constant(250) # 250 K (above Tc, supercritical)\nsrc_p = Source(lambda t: 10**(4 + t * 0.035)) # 10 kPa to ~30 MPa over 100s\n\n# Scopes: record Z from both EoS (output port 1)\nsco_pr = Scope(labels=[\"Z_PR\"])\nsco_rks = Scope(labels=[\"Z_RKS\"])"
},
{
"cell_type": "markdown",
Expand All @@ -78,46 +55,14 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sim = Simulation(\n",
" blocks=[T_const, P_src, pr, rks, scp_pr, scp_rks],\n",
" connections=[\n",
" # Temperature -> both EoS (input port 0)\n",
" Connection(T_const, pr, rks),\n",
" # Pressure -> both EoS (input port 1)\n",
" Connection(P_src, pr[1], rks[1]),\n",
" # Z output (port 1) -> scopes\n",
" Connection(pr[1], scp_pr),\n",
" Connection(rks[1], scp_rks),\n",
" ],\n",
" dt=1.0,\n",
")\n",
"\n",
"sim.run(100)"
]
"source": "sim = Simulation(\n [src_t, src_p, pr, rks, sco_pr, sco_rks],\n [\n Connection(src_t, pr, rks), # T -> both EoS\n Connection(src_p, pr[1], rks[1]), # P -> both EoS\n Connection(pr[1], sco_pr), # Z_PR\n Connection(rks[1], sco_rks), # Z_RKS\n ],\n dt=1.0,\n log=True,\n)\n\nsim.run(100)"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"time, Z_pr = scp_pr.read()\n",
"_, Z_rks = scp_rks.read()\n",
"P_vals = 10**(4 + time * 0.035) / 1e6 # MPa\n",
"\n",
"fig, ax = plt.subplots(figsize=(7, 5))\n",
"ax.semilogx(P_vals, Z_pr[0], label=\"Peng-Robinson\")\n",
"ax.semilogx(P_vals, Z_rks[0], \"--\", label=\"Soave-Redlich-Kwong\")\n",
"ax.axhline(1.0, color=\"gray\", linestyle=\"-.\", alpha=0.5, label=\"Ideal gas\")\n",
"ax.set_xlabel(\"Pressure [MPa]\")\n",
"ax.set_ylabel(\"Compressibility Factor Z\")\n",
"ax.set_title(\"Methane at T = 250 K\")\n",
"ax.legend()\n",
"ax.grid(True, alpha=0.3)\n",
"plt.tight_layout()\n",
"plt.show()"
]
"source": "time, [Z_pr] = sco_pr.read()\n_, [Z_rks] = sco_rks.read()\nP_vals = 10**(4 + time * 0.035) / 1e6 # MPa\n\nfig, ax = plt.subplots(figsize=(7, 5))\nax.semilogx(P_vals, Z_pr, label=\"Peng-Robinson\")\nax.semilogx(P_vals, Z_rks, \"--\", label=\"Soave-Redlich-Kwong\")\nax.axhline(1.0, color=\"gray\", linestyle=\"-.\", alpha=0.5, label=\"Ideal gas\")\nax.set_xlabel(\"Pressure [MPa]\")\nax.set_ylabel(\"Compressibility Factor Z\")\nax.set_title(\"Methane at T = 250 K\")\nax.legend()\nax.grid(True, alpha=0.3)\nplt.tight_layout()\nplt.show()"
},
{
"cell_type": "markdown",
Expand All @@ -140,52 +85,14 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pr_mix = PengRobinson(\n",
" Tc=[190.6, 305.3],\n",
" Pc=[4.6e6, 4.872e6],\n",
" omega=[0.011, 0.099],\n",
" x=[0.7, 0.3],\n",
")\n",
"\n",
"T_const2 = Constant(300)\n",
"P_src2 = Source(func=lambda t: 10**(4 + t * 0.035))\n",
"scp_mix = Scope(labels=[\"Z_mixture\"])\n",
"\n",
"sim_mix = Simulation(\n",
" blocks=[T_const2, P_src2, pr_mix, scp_mix],\n",
" connections=[\n",
" Connection(T_const2, pr_mix),\n",
" Connection(P_src2, pr_mix[1]),\n",
" Connection(pr_mix[1], scp_mix),\n",
" ],\n",
" dt=1.0,\n",
")\n",
"\n",
"sim_mix.run(100)"
]
"source": "pr_mix = PengRobinson(\n Tc=[190.6, 305.3],\n Pc=[4.6e6, 4.872e6],\n omega=[0.011, 0.099],\n x=[0.7, 0.3],\n)\n\nsrc_t2 = Constant(300)\nsrc_p2 = Source(lambda t: 10**(4 + t * 0.035))\nsco_mix = Scope(labels=[\"Z_mixture\"])\n\nsim_mix = Simulation(\n [src_t2, src_p2, pr_mix, sco_mix],\n [\n Connection(src_t2, pr_mix),\n Connection(src_p2, pr_mix[1]),\n Connection(pr_mix[1], sco_mix),\n ],\n dt=1.0,\n log=True,\n)\n\nsim_mix.run(100)"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"time_m, Z_mix = scp_mix.read()\n",
"P_mix = 10**(4 + time_m * 0.035) / 1e6\n",
"\n",
"fig, ax = plt.subplots(figsize=(7, 5))\n",
"ax.semilogx(P_vals, Z_pr[0], label=\"Pure CH₄ (250 K)\")\n",
"ax.semilogx(P_mix, Z_mix[0], \"--\", label=\"70/30 CH₄-C₂H₆ (300 K)\")\n",
"ax.axhline(1.0, color=\"gray\", linestyle=\"-.\", alpha=0.5)\n",
"ax.set_xlabel(\"Pressure [MPa]\")\n",
"ax.set_ylabel(\"Compressibility Factor Z\")\n",
"ax.set_title(\"Peng-Robinson: Pure vs Mixture\")\n",
"ax.legend()\n",
"ax.grid(True, alpha=0.3)\n",
"plt.tight_layout()\n",
"plt.show()"
]
"source": "time_m, [Z_mix] = sco_mix.read()\nP_mix = 10**(4 + time_m * 0.035) / 1e6\n\nfig, ax = plt.subplots(figsize=(7, 5))\nax.semilogx(P_vals, Z_pr, label=\"Pure CH₄ (250 K)\")\nax.semilogx(P_mix, Z_mix, \"--\", label=\"70/30 CH₄-C₂H₆ (300 K)\")\nax.axhline(1.0, color=\"gray\", linestyle=\"-.\", alpha=0.5)\nax.set_xlabel(\"Pressure [MPa]\")\nax.set_ylabel(\"Compressibility Factor Z\")\nax.set_title(\"Peng-Robinson: Pure vs Mixture\")\nax.legend()\nax.grid(True, alpha=0.3)\nplt.tight_layout()\nplt.show()"
},
{
"cell_type": "markdown",
Expand All @@ -208,4 +115,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}
Loading