top of page

11. marts 2025

||

Humzaa

Python code for CSTR and PFR Simulation



The code for the test is the following.

import
import numpy as np
import matplotlib.pyplot as plt

# Given data
C_A0 = 1.0  # mol/L (inlet concentration of A)
k = 0.2  # min^-1 (rate constant)
F = 10  # L/min (flow rate)
V = 50  # L (reactor volume)

def cstr_concentration(C_A0, k, tau):
"""Calculate outlet concentration for a CSTR."""
return C_A0 / (1 + k * tau)

def pfr_concentration(C_A0, k, tau)
"""Calculate outlet concentration for a PFR."""
return C_A0 * np.exp(-k * tau)

# Calculate residence time (tau)
tau = V / F  # Space time = V/F

# Compute outlet concentration
C_A_CSTR = cstr_concentration(C_A0, k, tau)
C_A_PFR = pfr_concentration(C_A0, k, tau)

# Print results
print(f"CSTR Outlet Concentration: {C_A_CSTR:.4f} mol/L")
print(f"PFR Outlet Concentration: {C_A_PFR:.4f} mol/L")


# Visualization
volumes = np.linspace(0, V, 100)  # Reactor volume range for PFR
tau_values = volumes / F  # Space time values
C_A_PFR_curve = pfr_concentration(C_A0, k, tau_values)

plt.figure(figsize=(8,5))
plt.plot(volumes, C_A_PFR_curve, label='PFR Concentration', color='blue')
plt.axhline(C_A_CSTR, color='red', linestyle='--', label='CSTR Concentration')
plt.xlabel("Reactor Volume (L)")
plt.ylabel("Outlet Concentration (mol/L)")
plt.title("CSTR vs. PFR Outlet Concentrations")
plt.legend()
plt.grid()
plt.show()


We are always looking for feedback!
So if you wish to see something added or improved, then do reach out to us!

ChemEnggLife - Main Logo - white background.png

Chemical Engineering Life

ChemEnggLife

Discovering the interesting world of chemical engineering, as told by Chemical engineers themselves.

Quick Links

  • LinkedIn
  • Instagram
  • Facebook

Subscribe and Stay Updated!

©2023 by ChemEnggLife

bottom of page