casualguitar

For perse

May 27th, 2022
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.79 KB | None | 0 0
  1. import numpy as np
  2. from scipy.integrate import solve_ivp
  3. import matplotlib.pyplot as plt
  4. import math
  5.  
  6. from SYSTEMMODELS.Functions.system_properties import *
  7. from SYSTEMMODELS.Functions.h_desublimation_CO2 import *
  8. from SYSTEMMODELS.Functions.h_vaporisation_H2O import *
  9. from SYSTEMMODELS.Functions.pressure_liquefaction_H2O import *
  10. from SYSTEMMODELS.Functions.pressure_sublimation_CO2 import *
  11. from SYSTEMMODELS.Functions.molar_desublimation_rate_CO2 import *
  12. from SYSTEMMODELS.Functions.molar_liquefaction_rate_H2O import *
  13. from SYSTEMMODELS.Functions.q_Ib import *
  14. from SYSTEMMODELS.Functions.q_gI import *
  15. from SYSTEMMODELS.Functions.q_star_CO2 import *
  16. from SYSTEMMODELS.Functions.q_star_H2O import *
  17. from SYSTEMMODELS.Functions.U_star import *
  18.  
  19. #Model:
  20. #Flow of CO2/H2O/N2 through a cold packed bed with constant mass and heat transfer coefficients
  21.  
  22. #initial conditions
  23. rho0CO2 = 1.1
  24. rho0H2O = 1.1
  25. y0CO2 = 0.1
  26. y0H2O = 0.1
  27. Tg0 = 200
  28. Tb0 = 200
  29. M0CO2 = 0 #solid moles deposited
  30. M0H2O = 0
  31. n = 5
  32. initial_constants = n, rho0CO2, rho0H2O, y0CO2, y0H2O, Tg0, Tb0, M0CO2, M0H2O
  33.  
  34. ##global constants (can remove actual constants and rename as name in system_properties. The non real constants will just be deleted/replaced with the functions)
  35. A_C = AREA_CROSS_SECTION
  36. A_s = SURFACE_AREA_SPECIFIC
  37. h_vap_h2o = h_vaporisation_h2o
  38. h_desublimation_CO2 = h_desublimation_CO2
  39. k_s = k_alumina
  40. dz = dz
  41. U_b = 100
  42. U_g = 100
  43. cp_CO2 = 45
  44. cp_H2O = 45
  45. ki_co2 = 8 #mol/m2.s
  46. ki_h2o = 16 #mol/m2.s
  47. m_co2 = rho0CO2*dz*A_C
  48. m_h2o = rho0H2O*dz*A_C
  49. M_al = rho_alumina/n ##check this
  50.  
  51. #Boundary conditions
  52. mol_in = 0.5
  53. y_co2_in = 0.1
  54. y_h2o_in  = 0.01
  55. T_in = 293
  56.  
  57. system_constants = A_C, A_s, h_vap_h2o, h_desublimation_CO2, k_s, dz, U_b, U_g, cp_CO2, cp_H2O, ki_co2, ki_h2o,m_co2,m_h2o, M_al, mol_in,y_co2_in,y_h2o_in,T_in
  58.  
  59. def initialize_system(*initial_constants):
  60.     ##sets the appropriate initial conditions (column vector of constants)
  61.     n, rho0CO2, rho0H2O, y0CO2, y0H2O, Tg0, Tb0, M0CO2, M0H2O = initial_constants  # unpack initial conditions
  62.  
  63.     # Initial conditions values are the same at all positions except boundary at t=0
  64.     constants_array = np.asarray(initial_constants)
  65.     constants_array = np.delete(constants_array,0)
  66.     x0 = np.reshape(np.array([constants_array]*n),-1)
  67.     # [rho0CO2, rho0H2O, y0CO2, y0H2O, Tg0, Tb0, M0CO2,...rho0CO2, rho0H2O, ynCO2, ynH2O, Tgn, Tbn, MnCO2, MnH2O]
  68.     return x0
  69.  
  70.  
  71. def df(t,x,*system_constants):
  72.     ##the time derivative of x
  73.     A_C, A_s, h_vap_h2o, h_desublimation_CO2, k_s, dz, U_b, U_g, cp_CO2, cp_H2O, ki_co2, ki_h2o, m_co2,m_h2o, M_al, mol_in, y_co2_in, y_h2o_in, T_in = system_constants #unpack system constants
  74.     ##setting up solution array
  75.     x = x.reshape(n,6)
  76.     dxdt = np.zeros_like(x)
  77.  
  78.     ##Set up boundary conditions here (n=0, t=t i.e. at position 0, the inlet, at all times)
  79.     #note: the reason I set the boundary conditions here rather than in initialize_system
  80.     # is because I need to reference the solution array (initialize_system does not take
  81.     # x as an argument meaning I could not do this
  82.     MDR_CO2 = MDR(ki_co2, x[0][4], psubCO2(x[0][4]))
  83.     MLR_H2O = MLR(ki_h2o, x[0][4], pliqH2O(x[0][4]))
  84.  
  85.     qstar = (h_desublimation_CO2*MDR_CO2) + (MLR_H2O*h_vaporisation_h2o)
  86.     ustar = U_star(U_g,U_b)
  87.     QGI = (-U_g*qstar/(U_g + U_b)) + (ustar*(x[0][4]-x[0][5]))
  88.     QIB = (U_b*qstar/(U_g + U_b)) + (ustar*(x[0][4]-x[0][5]))
  89.  
  90.     # CO2 mole fraction (dyi/dt)
  91.     dxdt[0][0] = (mol_in * (y_co2_in - x[0][0]) - (MDR_CO2 * A_s) + (x[0][0]*A_s * (MDR_CO2+MLR_H2O))) / m_co2
  92.     # H2O mole fraction (dyi/dt)
  93.     dxdt[0][1] = (mol_in * (y_h2o_in - x[1][0]) - (MLR_H2O * A_s) + (x[1][0] * A_s*(MDR_CO2+MLR_H2O))) / m_h2o
  94.     # CO2 solid deposition (dMi/dt)
  95.     dxdt[0][2] = MDR_CO2 * A_s / (A_C * dz)
  96.     # H2O solid deposition (or liq) (dMi/dt)
  97.     dxdt[0][3] = MLR_H2O * A_s / (A_C * dz)
  98.     # Gas phase heat balance (dTg/dt)
  99.     dxdt[0][4] = (mol_in * cp_CO2*(T_in - x0[4][0]) - QGI * A_s) / (m_co2 * cp_CO2)
  100.     # Bed heat balance (dTb/dt)
  101.     dxdt[0][5] = (QIB * A_s) / (M_al * cp_alumina)
  102.     # mj
  103.     mdot = mol_in + (rho0CO2 / x[0][4]) * A_C * dz * dxdt[0][4] - (MDR_CO2 * A_s)
  104.  
  105.     ## internal nodes
  106.     for j in range(1,n): ##start at index 1 (to avoid BCs), go to n (last node)
  107.         ## CO2 mole fraction (dyi/dt)
  108.         MDR_CO2 = MDR(ki_co2, x[j][4], psubCO2(x[j][4]))
  109.         MLR_H2O = MLR(ki_h2o, x[j][4], pliqH2O(x[j][4]))
  110.  
  111.         qstar = MDR_CO2 + MLR_H2O
  112.         ustar = U_star(U_g, U_b)
  113.         QGI = (-U_g * qstar / (U_g + U_b)) + (ustar * (x[j][4] - x[j][5]))
  114.         QIB = (U_b * qstar / (U_g + U_b)) + (ustar * (x[j][4] - x[j][5]))
  115.  
  116.         dxdt[j][0] = (mdot*(x[j-1][0] - x[j][0]) - MDR_CO2*A_s + x[j][0]*1)/m_co2
  117.         ## H2O mole fraction (dyi/dt)
  118.         dxdt[j][1] = (mdot*(x[j-1][1] - x[j][1]) - MDR_CO2*A_s + x[j][1]*1)/m_co2
  119.         ## CO2 solid deposition (dMi/dt)
  120.         dxdt[j][2] = MDR_CO2*A_s/(A_C*dz)
  121.         ## H2O solid deposition (or liq) (dMi/dt)
  122.         dxdt[j][3] = MLR_H2O*A_s/(A_C*dz)
  123.         ## Gas phase heat balance (dTg/dt)
  124.         dxdt[j][4] = (mdot*cp_CO2*(x[j-1][4] - x[j][4]) - QGI * A_s)/(m_co2*cp_CO2)
  125.         ## Bed heat balance (dTb/dt)
  126.         dxdt[j][5] = (QIB * A_s)/(M_al*cp_alumina)
  127.         ## mj
  128.         mdot = mdot + (rho0CO2/x[j][4])*A_C*dz*dxdt[j][4] - (MDR_CO2*A_s)
  129.  
  130.     return dxdt.reshape(-1)
  131.  
  132. #create your initial conditions
  133. t0 = 0
  134. tf = 100 #how long you want to integrate for
  135. x0 = initialize_system(*initial_constants)
  136.  
  137. #solve the system
  138. sol = solve_ivp(df, [t0,tf], x0, dense_output = True) #dense_output gives you an interpolant I think (3rd/4th order polynomial)
  139.  
  140. #decide at which times you'd like to check your system
  141. t_eval = [0,10,20,30,40,50,60,70,80,90,100] ##better way to increment probably
  142.  
  143. x_eval = [sol.sol(t) for t in t_eval]
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
Advertisement
Add Comment
Please, Sign In to add comment