casualguitar

fluid model

Feb 4th, 2022
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.70 KB | None | 0 0
  1. import numpy as np
  2. import scipy as sp
  3. from scipy.integrate import solve_ivp
  4. import matplotlib.pyplot as plt
  5. import math
  6. # from stream import Stream
  7. from thermo.mixture import Mixture
  8.  
  9. ################################################################
  10. #IGNORE THIS BLOCK OF CODE IT IS JUST THERE TO SET UP A MIXTURE OBJECT (that allows thermodynamic property methods to be called)
  11. ################################################################
  12. from chemicals import normalize
  13. from thermo import ChemicalConstantsPackage, CEOSGas, CEOSLiquid, PRMIXTranslatedConsistent, FlashVL
  14. from thermo.interaction_parameters import IPDB, SPDB
  15. constants, properties = ChemicalConstantsPackage.from_IDs(['nitrogen', 'oxygen', 'argon'])
  16. kijs = IPDB.get_ip_asymmetric_matrix('ChemSep PR', constants.CASs, 'kij')
  17. Ls = SPDB.get_parameter_vector(name='PRTwu_PinaMartinez', CASs=constants.CASs, parameter='TwuPRL')
  18. Ms = SPDB.get_parameter_vector(name='PRTwu_PinaMartinez', CASs=constants.CASs, parameter='TwuPRM')
  19. Ns = SPDB.get_parameter_vector(name='PRTwu_PinaMartinez', CASs=constants.CASs, parameter='TwuPRN')
  20. cs = SPDB.get_parameter_vector(name='PRTwu_PinaMartinez', CASs=constants.CASs, parameter='TwuPRc')
  21. alpha_coeffs = [(Ls[i], Ms[i], Ns[i]) for i in range(constants.N)]
  22. eos_kwargs = {'Pcs': constants.Pcs, 'Tcs': constants.Tcs, 'omegas': constants.omegas, 'kijs': kijs,
  23. 'cs': cs, 'alpha_coeffs':alpha_coeffs}
  24. gas = CEOSGas(PRMIXTranslatedConsistent, eos_kwargs=eos_kwargs, HeatCapacityGases=properties.HeatCapacityGases)
  25. liquid = CEOSLiquid(PRMIXTranslatedConsistent, eos_kwargs=eos_kwargs, HeatCapacityGases=properties.HeatCapacityGases)
  26. flasher = FlashVL(constants, properties, liquid=liquid, gas=gas)
  27. zs = normalize([78.08, 20.95, .93])
  28. ################################################################
  29.  
  30. n = 3  # number of elements in spatial array
  31.  
  32. volumeV = 0.01 / n  # m3
  33. area = 0.1 / n  # m2
  34. mS = 1 / n  # kg
  35.  
  36. P = 200000  # Paß
  37.  
  38.  
  39. mS = 57.75 / n  # kg
  40. cpS = 1.1  # kJ/kgK
  41. u = 1  # heat transfer coefficient between fluid and solid (W/m2.K)
  42. T0 = 80  # initial fluid temp (K)
  43.  
  44. Tin = 300  # Inlet fluid temperature (K)
  45. minL = 0.05  # inlet mass flow (kg/s)
  46.  
  47. ## Using thermo
  48. hIn = flasher.flash(T=Tin, P=P, zs=zs).H_mass()/1000  # Inlet enthalpy (J/kg)
  49. H0 =  flasher.flash(T=T0, P=P, zs=zs).H_mass() /1000 # fluid starting enthalpy (J/kg)
  50.  
  51. print("thermo hIn = ", hIn)
  52. print("thermo H0 = ", H0)
  53.  
  54. global equil_state
  55.  
  56. ## First equilibrium state to accelerate convergence
  57. equil_state = flasher.flash(H=H0, P=P, zs=zs, retry=True)
  58.  
  59. def temp_thermo(H,P,last_equil_state=None):
  60.     # returns temperature given enthalpy (after processing function)
  61.     equil_state = flasher.flash(H=H, P=P, zs=zs, retry=True,hot_start=last_equil_state)
  62.     T_n = equil_state.T
  63.     print("H = ",H)
  64.     print("T = ", T_n)
  65.     return T_n, equil_state
  66.  
  67.  
  68. def mass_thermo(H, P, last_equil_state=None):
  69.     # returns mass holdup given enthalpy
  70.     equil_state = flasher.flash(H=H, P=P, zs=zs, retry=True,hot_start=last_equil_state)
  71.     m = equil_state.rho()*volumeV
  72.     return m, equil_state
  73.  
  74.  
  75. def dpdh(H, P):
  76.  
  77.     ## Using thermo
  78.     res = flasher.flash(H=H, P=P, zs=zs, retry=True)
  79.     if res.phase_count == 1:
  80.         if res.phase == 'L':
  81.             print("liquid phase")
  82.             drho_dT = 0
  83.         else:
  84.             drho_dT = res.gas.drho_dT_H()
  85.             print("gas phase")
  86.     else:
  87.         drho_dT = res.bulk._equilibrium_derivative(of='rho', wrt='T', const='H')
  88.         print("two phase")
  89.  
  90.     dpdh = drho_dT/res.dH_dT_P() # using drho/dT for constante enthalpy, is this correct?
  91.     print("dpdh = ", dpdh)
  92.     print("drho_dT = ", drho_dT)
  93.  
  94.     return dpdh
  95.  
  96. # time steps
  97. t_max = 100
  98. t = (0, t_max)  # time range
  99. # t_eval = np.linspace(0, t_max, 1000 * t_max)  # optional
  100.  
  101.  
  102. def my_system(t, y, n, hIn, minL, mS, cpS, u, area):
  103.     dydt = np.zeros(3 * n) #setting up zeros array for solution (solving for [H0,Ts0,m0,H1,Ts1,m1,H2,Ts2,m2,..Hn,Tsn,mn])
  104.  
  105.     # y = [h_0, Ts_0, m_0, ... h_n, Ts_n, m_n]
  106.     # y[0] = hIn
  107.     # y[1] = Tin
  108.     # y[2] = minL
  109.  
  110.     i=0
  111.  
  112.     global equil_state
  113.  
  114.     ## Using thermo
  115.     T, equil_state = temp_thermo(y[i],P,last_equil_state=equil_state) #initial T
  116.     m, equil_state = mass_thermo(y[i],P,last_equil_state=equil_state) #initial m
  117.  
  118.     #initial values
  119.     dydt[i] = (minL * (hIn - y[i]) + (u * area * (y[i + 1] - T))) / m # dH/dt (eq. 2)
  120.     dydt[i + 1] = -(u * area * (y[i + 1] - T)) / (mS * cpS) # dTs/dt from eq.3
  121.     dmdt = dydt[i] * dpdh(y[i], P) * volumeV / n # dm/dt (holdup variation) eq. 4b
  122.     dydt[i + 2] = minL - dmdt # mass flow out (eq.4a)
  123.  
  124.  
  125.     for i in range(3, 3 * n, 3): #starting at index 3, and incrementing by 3 because we are solving for 'triplets' [h,Ts,m] in each loop
  126.         print("i = ", i)
  127.         ## Using thermo
  128.         T, equil_state = temp_thermo(y[i],P,last_equil_state=equil_state)
  129.         m, equil_state = mass_thermo(y[i],P,last_equil_state=equil_state)
  130.  
  131.         # [h, TS, m]
  132.         dydt[i] = (dydt[i-1] * (y[i - 3] - y[i]) + (u * area * (y[i + 1] - T))) /  m # dH/dt (eq.2), dydt[i-1] is the mass of the previous tank
  133.         dydt[i + 1] = -((u * area * (y[i + 1] - T))) / (mS * cpS) # dTs/dt eq. (3)
  134.         dmdt = dydt[i] * dpdh(y[i], P) * volumeV / n # Equation 4b
  135.         dydt[i + 2] = dydt[i-1] - dmdt # Equation 4a
  136.  
  137.     return dydt
  138.  
  139. Ts0 = 80  # solid starting temperature (K)
  140. y0 = np.copy(np.asarray([H0, Ts0, minL] * n)) #setting up initial condition array (H0,Ts0 and m0 at all points
  141. y0[0] = H0 # initial fluid bed enthalpy
  142. y0[1] = Ts0 # initial solid bed temperature
  143. y0[2] = minL #initial mass flow        #####MINL
  144.  
  145. parameters = (n, hIn, minL, mS, cpS, u, area)
  146.  
  147. solution = solve_ivp(my_system, t, y0, args=parameters, method='BDF')
  148.  
  149.  
Advertisement
Add Comment
Please, Sign In to add comment