Sunday, November 24, 2024

Understanding the Psychrometric Chart python code

The psychrometric calculator is an essential tool in HVAC (Heating, Ventilation, and Air Conditioning) and other thermal systems to analyze air-water vapor mixtures. This blog post explains the fundamental concepts, formulas, and algorithms used in the given HTML-based psychrometric calculator.


How to Create a Psychrometric Chart Using Python

A psychrometric chart is a powerful tool used in HVAC, thermodynamics, and atmospheric sciences to represent the properties of moist air. This article explains how to generate a comprehensive psychrometric chart using Python with numpy, matplotlib, and the psychrolib library.

Refer this link to see full Python code: https://github.com/chemenggcalc/Psychrometric-Chart-using-Python


Libraries Used

  • NumPy: For efficient numerical computations.
  • Matplotlib: For visualizing the psychrometric chart.
  • PsychroLib: A library for psychrometric calculations, supporting SI and IP unit systems.

Setting Up the Environment

Before starting, ensure you have the required libraries installed. Use the following command to install them:

pip install numpy matplotlib psychrolib

The Code Breakdown

1. Initialization

The psychrometric calculations are based on the SI unit system (Celsius, kg/kg, Pa). Use the SetUnitSystem function from psychrolib to set the unit system:

from psychrolib import SI, SetUnitSystem
SetUnitSystem(SI)

2. Define Parameters

Define the ranges for dry-bulb temperature, humidity ratio, and atmospheric pressure:

import numpy as np

temperature_range = np.linspace(0, 50, 100)  # Dry-bulb temperature range (°C)
humidity_ratio_range = np.linspace(0, 0.01, 30)  # Humidity ratio range (kg/kg dry air)
pressure = 101325  # Atmospheric pressure (Pa)

3. Plotting the Psychrometric Chart

A. Saturation Line

The saturation line represents air that holds the maximum amount of water vapor at a given temperature:

from psychrolib import GetSatHumRatio

sat_hum_ratios = [GetSatHumRatio(t, pressure) for t in temperature_range]
plt.plot(temperature_range, sat_hum_ratios, color="blue", linewidth=4, label="Saturation Line")

B. Constant Relative Humidity Lines

Relative humidity lines (10%, 20%, ..., 100%) are plotted using GetHumRatioFromRelHum:

from psychrolib import GetHumRatioFromRelHum

relative_humidities = np.linspace(0.1, 1, 10)
for rh in relative_humidities:
    hum_ratios = [GetHumRatioFromRelHum(t, rh, pressure) for t in temperature_range]
    plt.plot(temperature_range, hum_ratios, linestyle="--", color="#32CD32", linewidth=2)

C. Constant Enthalpy Lines

Enthalpy lines (10, 20, ..., 250 kJ/kg) are derived from the equation:

h=1.006T+W(2501+1.86T)h = 1.006 \cdot T + W \cdot (2501 + 1.86 \cdot T)

Rearranged to find WW (humidity ratio):

enthalpy_lines = range(10, 250, 15)
for h in enthalpy_lines:
    hum_ratios = [(h - 1.006 * t) / (2501 + 1.86 * t) for t in temperature_range]
    plt.plot(temperature_range, hum_ratios, linestyle="-.", color="#FF4500", linewidth=2)

D. Constant Wet-Bulb Temperature Lines

Wet-bulb temperature lines are approximated using GetHumRatioFromRelHum:

wet_bulb_temps = range(5, 36, 5)
for twb in wet_bulb_temps:
    hum_ratios = []
    for t in temperature_range:
        try:
            hum_ratio = GetHumRatioFromRelHum(t, twb / 100.0, pressure)
            hum_ratios.append(hum_ratio)
        except:
            hum_ratios.append(np.nan)
    plt.plot(temperature_range, hum_ratios, linestyle=":", color="#9400D3", linewidth=2)

E. Constant Specific Volume Lines

Specific volume lines are calculated using an approximation formula:

v=1ρ=RTPv = \frac{1}{\rho} = \frac{R \cdot T}{P}
specific_volumes = np.linspace(0.75, 25, 20)
for vol in specific_volumes:
    hum_ratios = [vol / (t + 273.15) for t in temperature_range]
    plt.plot(temperature_range, hum_ratios, linestyle="-", color="#FFA500", linewidth=2)

4. Enhancing the Chart

Grid Lines

Vertical and horizontal grid lines improve readability:

for t in temperature_range:
    plt.axvline(t, color="gray", linestyle="--", linewidth=1, alpha=0.5)
for hr in np.linspace(humidity_ratio_range[0], humidity_ratio_range[1], 10):
    plt.axhline(hr, color="gray", linestyle=":", linewidth=1, alpha=0.5)

Labels and Title

Add descriptive labels, a title, and a legend:

plt.xlabel("Dry-Bulb Temperature (°C)", fontsize=20, weight="bold")
plt.ylabel("Humidity Ratio (kg water / kg dry air)", fontsize=20, weight="bold")
plt.text(15, 0.075, "Psychrometric Chart", fontsize=50, ha="center", color="black", weight="bold")
plt.legend(loc="center left", fontsize=18, frameon=True, shadow=True, fancybox=True)

Customization

Customize the layout with grid styling and appropriate figure size:

plt.figure(figsize=(20, 15))
plt.grid(True, which='both', linestyle="--", linewidth=1.5, alpha=0.7)

5. Display the Chart

Finally, render the chart using:

plt.show()

Key Outputs

The psychrometric chart includes:

  1. Saturation line.
  2. Constant relative humidity, enthalpy, wet-bulb temperature, and specific volume lines.
  3. Interactive labels and enhanced readability through grid lines.

Applications

  • HVAC Design: Analyze air-conditioning systems.
  • Environmental Science: Study humidity and temperature changes.
  • Industrial Processes: Understand air-water mixture dynamics in drying and humidification.

By leveraging Python's capabilities, this tool simplifies complex psychrometric calculations and makes air-property analysis accessible to engineers and researchers.

No comments:

Post a Comment

Understanding the Psychrometric Chart python code

The psychrometric calculator is an essential tool in HVAC (Heating, Ventilation, and Air Conditioning) and other thermal systems to analyze ...