Advertisement
phystota

kinetics

Dec 16th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.93 KB | None | 0 0
  1. import numpy as np
  2. import math
  3. from dataclasses import dataclass
  4.  
  5. # Physical constants
  6. R = 0.25  # O2 N2 ratio
  7. PRESSURE = 1  # pressure
  8. KB = 1.380649E-23  # Boltzmann constant
  9. H = 6.62607015E-34  # Planck constant
  10. C = 3.0E+10  # speed of light, cm/s
  11. DT = 1.0E-8  # T step for finite differences
  12. NA = 6.02214076E+23  # Avogadro number
  13. NU = 1.0  # number of moles
  14. P_ATM = 101325.0  # atmospheric pressure
  15. EV = 1.60218E-19  # eV -> J translation coefficient
  16.  
  17.  
  18. @dataclass
  19. class Species:
  20.     constants: np.ndarray
  21.  
  22. def calculate_thermodynamic_properties(temperature: float, species_idx: int, species_data: np.ndarray) -> dict:
  23.     """
  24.    Calculate thermodynamic properties for a given species at specified temperature.
  25.    
  26.    Args:
  27.        temperature: Temperature in Kelvin
  28.        species_idx: Species index (0 for N, 1 for N2)
  29.        species_data: Array containing species data
  30.        
  31.    Returns:
  32.        Dictionary containing:
  33.        - Q_in: Internal partition function
  34.        - Q_tr: Translational partition function
  35.        - Q_total: Total partition function
  36.        - S: Entropy (J/mol/K)
  37.        - H_H0: Enthalpy difference from 0K (kJ/mol)
  38.    """
  39.     # Create Species object for the selected species
  40.     species = Species(constants=species_data[species_idx])
  41.    
  42.     # Calculate Q_in
  43.     if species.constants[1] == 0:
  44.         Q_rot = 1.0
  45.     else:
  46.         Q_rot = (8.0 * math.pi**2 * species.constants[1] * KB * temperature) / (H**2 * species.constants[0])
  47.  
  48.     if species.constants[2] == 0:
  49.         Q_vib = 1.0
  50.     else:
  51.         Q_vib = 1.0 / (1.0 - math.exp((-H * C * species.constants[2]) / (KB * temperature)))
  52.  
  53.     if species.constants[3] == 0:
  54.         Q_el = 1.0
  55.     else:
  56.         Q_el = sum(
  57.             species.constants[3+i] * math.exp(-H * C * species.constants[22+i] / (KB * temperature))
  58.             for i in range(19)
  59.         )
  60.    
  61.     Q_in = Q_rot * Q_vib * Q_el
  62.    
  63.     # Calculate Q_tr
  64.     Q_tr = (NU * KB * NA * temperature / (PRESSURE * P_ATM)) * \
  65.            (2 * math.pi * species.constants[41] * KB * temperature / (H * H))**(1.5)
  66.    
  67.     # Calculate Q_total
  68.     Q_total = Q_in * Q_tr
  69.    
  70.     # Calculate Entropy [J/(mol*K)]
  71.     S = KB * NA * (
  72.         math.log(Q_in) + math.log(Q_tr)
  73.     ) + KB * NA * temperature * (
  74.         (Q_tr_calc(species, temperature + DT) - Q_tr_calc(species, temperature - DT)) /
  75.         (2.0 * DT * Q_tr) +
  76.         (Q_in_calc(species, temperature + DT) - Q_in_calc(species, temperature - DT)) /
  77.         (2.0 * DT * Q_in)
  78.     ) - KB * NA * math.log(NA)
  79.    
  80.     # Calculate H-H0 [J/mol]
  81.     H_H0 = KB * NA * temperature * (
  82.         (temperature / Q_in) *
  83.         (Q_in_calc(species, temperature + DT) - Q_in_calc(species, temperature - DT)) /
  84.         (2.0 * DT) + 2.5
  85.     )
  86.    
  87.     return {
  88.         'Q_in': Q_in,
  89.         'Q_tr': Q_tr,
  90.         'Q_total': Q_total,
  91.         'S': S,
  92.         'H_H0': H_H0
  93.     }
  94.  
  95. # Helper functions for finite difference calculations
  96. def Q_in_calc(species: Species, T: float) -> float:
  97.     """Calculate Q_in at a specific temperature (helper function for derivatives)"""
  98.     if species.constants[1] == 0:
  99.         Q_rot = 1.0
  100.     else:
  101.         Q_rot = (8.0 * math.pi**2 * species.constants[1] * KB * T) / (H**2 * species.constants[0])
  102.  
  103.     if species.constants[2] == 0:
  104.         Q_vib = 1.0
  105.     else:
  106.         Q_vib = 1.0 / (1.0 - math.exp((-H * C * species.constants[2]) / (KB * T)))
  107.  
  108.     if species.constants[3] == 0:
  109.         Q_el = 1.0
  110.     else:
  111.         Q_el = sum(
  112.             species.constants[3+i] * math.exp(-H * C * species.constants[22+i] / (KB * T))
  113.             for i in range(19)
  114.         )
  115.    
  116.     return Q_rot * Q_vib * Q_el
  117.  
  118. def Q_tr_calc(species: Species, T: float) -> float:
  119.     """Calculate Q_tr at a specific temperature (helper function for derivatives)"""
  120.     return (NU * KB * NA * T / (PRESSURE * P_ATM)) * \
  121.            (2 * math.pi * species.constants[41] * KB * T / (H * H))**(1.5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement