Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- from scipy.integrate import solve_ivp
- import matplotlib.pyplot as plt
- import math
- def initialize_system(*constants):
- ##sets the appropriate initial conditions
- #which is actually just a vector of constants (initially all nodes have the same initial condition values)
- n, dz, y0CO2, y0H2O, Tg0, Tb, MCO2, MH2O = constants #unpack constants
- #Initial conditions values are the same at all positions except boundary/inlet at t=0
- x0 = np.asarray([[y0CO2, y0H2O, Tg0, Tb, MCO2, MH2O]]*n) #2D array of initial condition values (setting initial conditions at all values of n equal to the same values)
- #Initially, all initial values for internal nodes are the same at all points
- #i.e.
- # y0CO2 = y1CO2 = ... = ynCO2
- # y0H2O = y1H20 = ... = ynH20
- # etc
- #x0 is a 2D array that looks like this:
- # n=0: |y0CO2, y0H2O, Tg0, Tb0, M0CO2, M1H2O|
- # n=1: |y1CO2, y1H2O, Tg1, Tb1, M1CO2, M2H2O|
- # n=2: |y2CO2, y2H2O, Tg2, Tb2, M2CO2, M3H2O|
- # ... ..... ...... ... ... ..... .....
- # n=n: |ynCO2, ynH2O, Tgn, Tbn, MnCO2, MnH2O|
- #It is then reshaped to be a column vector in the form:
- # [y0CO2, y0H2O, Tg0, Tb0, M0CO2, M1H2O,y1CO2, y1H2O, Tg1, Tb1, M1CO2, M2H2O, y2CO2, y2H2O, Tg2, Tb2, M2CO2, M3H2O,...ynCO2, ynH2O, Tgn, Tbn, MnCO2, MnH2O ]
- # Meaning that we do not need the for loop setup in initialize_system (possibly wrong?)
- # because we have constants everywhere initially. So we dont need the for loop below unless we have different values everywhere initially?
- # #Initial condition setup (non boundary points)
- # for j in range(1, n):
- # # CO2 mole fraction (dyi/dt)
- # x0[j][0] = (mdot*(y0CO2 - x0[1][j]) - MDR_CO2*A_s + x0[1][j]*1)/m
- # # H2O mole fraction (dyi/dt)
- # x0[j][1] = (mdot*(y0H2O - x0[1][j]) - MDR_CO2*A_s + x0[1][j]*1)/m
- # # CO2 solid deposition (dMi/dt)
- # x0[j][2] = MDR_CO2*A_s/(A_c*dz)
- # # H2O solid deposition (or liq) (dMi/dt)
- # x0[j][3] = MDR_H2O*A_s/(A_c*dz)
- # # Gas phase heat balance (dTg/dt)
- # x0[j][4] = (mdot*Cp(Tg0 - x0[4][j]) - Q*A_s)/(m*Cp)
- # # Bed heat balance (dTb/dt)
- # x0[j][5] = (Q*A_s)/(M_s*Cp)
- # #mj
- # mdot = mdot + (rho/x0[4][j])*A_c*dz*x0[4][j] - (MDR_CO2*A_s)
- #reshapes 2D array as a1,b1,c1,d1,e1,a2,b2,c2,d2,e2 or a1,...an,b1...bn, c1...cn,
- return x0.reshape(-1)
- def df(t,x,*constants):
- ##the time derivative of x
- dz, number_of_species, number_of_nodes, A_s, Cp = constants ## unpack constants
- ##setting up solution array
- x = x.reshape(number_of_nodes,6)
- dxdt = np.zeros_like(x)
- ##Set up boundary conditions here (n=0, t=t i.e. at position 0 at all times)
- ##BCs here
- ##boundary condition values at all times (inlet values)
- mjin = 1 ##molar flow in
- TgIn = 1 ##temperature of flow in
- yInCO2 = 0.1 ##mole fraction of N2
- yInH2O = 0.1 ##mole fraction of H2O
- ##density of components (not needed)
- Cp = 1 ##heat capacity (constant for now)
- #boundary ODE setup
- # setting up ODE for inlet position (n=0, t=t). This uses the inlet values, hence
- # the need for both boundary and initial condition setup
- # note: the reason I set the boundary conditions here rather than in initialize_system
- # is because I need to reference the solution array to set up the ODEs (initialize_system does not take
- # x as an argument meaning I could not do this, but df does so I set them up there)
- #boundary/inlet ODEs
- # CO2 mole fraction (dyi/dt)
- dxdt[0][0] = (mjin * (yInCO2 - x[1][0]) - MDR_CO2 * A_s + x[1][0] * 1) / m
- # H2O mole fraction (dyi/dt)
- dxdt[0][1] = (mjin * (yInH2O - x[1][0]) - MDR_H2O * A_s + x[1][0] * 1) / m
- # CO2 solid deposition (dMi/dt)
- dxdt[0][2] = MDR_CO2 * A_s / (A_c * dz)
- # H2O solid deposition (dMi/dt)
- dxdt[0][3] = MDR_H2O * A_s / (A_c * dz)
- # Gas phase heat balance (dTg/dt)
- dxdt[0][4] = (mjin * Cp(TgIn - x0[4][0]) - Q * A_s) / (m * Cp)
- # Bed heat balance (dTb/dt)
- dxdt[0][5] = (Q * A_s) / (M_s * Cp)
- # mj
- mdot = mjin + (rho / x[4][0]) * A_c * dz * dxdt[0][4] - (MDR_CO2 * A_s)
- ## internal nodes (and end node which is the same as the internal nodes)
- for j in range(1,n): ##start at index 1 (to avoid BCs), go to n (last node)
- ## CO2 mole fraction (dyi/dt)
- dxdt[j][0] = (mdot*(x[j-1][0] - x[j][0]) - MDR_CO2*A_s + x[j][0]*1)/m
- ## H2O mole fraction (dyi/dt)
- dxdt[j][1] = (mdot*(x[j-1][1] - x[j][1]) - MDR_CO2*A_s + x[j][1]*1)/m
- ## CO2 solid deposition (dMi/dt)
- dxdt[j][2] = MDR_CO2*A_s/(A_c*dz)
- ## H2O solid deposition (or liq) (dMi/dt)
- dxdt[j][3] = MDR_H2O*A_s/(A_c*dz)
- ## Gas phase heat balance (dTg/dt)
- dxdt[j][4] = (mdot*Cp(x[j-1][4] - x[j][4]) - Q*A_s)/(m*Cp)
- ## Bed heat balance (dTb/dt)
- dxdt[j][5] = (Q*A_s)/(M_s*Cp)
- ## mj
- mdot = mdot + (rho/x[j][4])*A_c*dz*dxdt[j][4] - (MDR_CO2*A_s)
- return dxdt.reshape(-1)
- y0CO2 = 0.1
- y0H2O = 0.1
- Tg0 = 0.1
- Tb = 0.1
- MCO2 = 0.1
- MH2O = 0.1
- constants = y0CO2,y0H2O, Tg0, Tb, MCO2, MH2O
- ##other values here (all this can go as *constants for now but will all be correlations/expressions)
- MDR_CO2 = 1 #molar deposition rate
- MDR_H2O = 1 #molar deposition rate
- m = 1 #will be a correlation for rho and will solve for molar holdup from this
- A_c = 1 #cross sectional area
- dz = 1 #delta z
- Cp = 1 #will be a correlation
- Q = 1 #this is a function of T_g, T_b and the heat transfer coefficients U_g and U_b
- M_s = 1
- rho = 0 #will be a correlation
- n = 10
- A_s = 1
- epsilon = 0.3
- #create your initial conditions
- t0 = 0
- tf = 100 #how long you want to integrate for
- x0 = initialize_system(constants)
- #solve the system
- sol = solve_ivp(df, [t0,tf], x0, dense_output = True) #dense_output gives you an interpolant (3rd/4th order polynomial)
- #decide at which times you'd like to check your system
- t_eval = [t_i, t_i+1,...t_f]
- x_eval = [sol.sol(t) for t in t_eval]
- #These are the expresstions (currently mostly empty) that will replace some of the constants)
- def Ug():
- # U_g = (2.19Re^1/3 + 0.78*Re^0.619)*Sc^1/3 * k_g
- return 0
- def Ub():
- # U_b = (2.19Re^1/3 + 0.78*Re^0.619)*Sc^1/3 * k_g
- return 0
- def QgI():
- # QgI= -(U_g*qstar)/((U_g + U_b) + Ustar(T_g-T_b)
- return 0
- def QIb():
- # QIb = (U_b*qstar)/((U_g + U_b) + Ustar(T_g-T_b)
- return 0
- def qstar():
- #qstar = M_CO2*deltaH_CO2 + M_H2O*deltaH_H2O
- return 0
- def Ustar():
- #Ustar = 1/((1/U_g)+ (1/U_b))
- return 0
- def m():
- #gas phase density * element volume
- return 0
- def rho_CO2():
- #gas phase density
- #could use thermo here
- return 0
- def rho_H2O():
- #gas phase density
- #could use thermo here
- return 0
- def molar_deposition_rate_CO2():
- #
- return 0
- def molar_deposition_rate_H2O():
- return 0
- def cp_CO2():
- #thermo
- return 0
- def cp_H2O():
- #thermo
- return 0
- def Pr():
- # Pr = mu_g*Cp/k_g
- return 0
- def Re(m_molar,T, A_c, epsilon,d):
- #Re = 6G0/(a*mu_g*Psi)
- Psi = 1
- g0 = m_molar/A_c
- sa_specific = (1-epsilon)*6/d #surface area per unit volume
- mu = 1 #correlation here #Pascal seconds #Pa.s
- re = 6*g0/(sa_specific*mu)
- return re
- def Nu():
- #Nu = h_fs*d_p/k_g
- return 0
- def Sh():
- #Sh = k_g*D_p/((1-e_g)Di*Psi)
- #D_i is diffusion coefficient of component i
- return 0
- def Sc():
- #Sc = mu/rho*D_i
- return 0
- def k():
- #k_f = (2.19Re^1/3 + 0.78*Re^0.619)*Sc^1/3 * k_g
- return 0
- def psubCO2(T):
- # sublimation pressure equation
- # only applies up to the triple point pressure Pt
- # arguments: Temperature
- Tt = 216.592 # triple point temperature (K)
- Pt = 0.51795 # triple point pressure (MPa)
- a1 = -14.740846 # constant a1
- a2 = 2.4327015 # constant a2
- a3 = -5.3061778 # constant a3
- if T>Tt:
- return 0
- else:
- reduced_temperature = T / Tt
- term1 = a1 * (1 - reduced_temperature)
- term2 = a2 * (1 - reduced_temperature) ** 1.9
- term3 = a3 * (1 - reduced_temperature) ** 2.9
- term4 = (1 / reduced_temperature) * (term1 + term2 + term3)
- term5 = (math.e) ** term4
- psub = term5 * Pt
- return psub
- def pliqH2O(T):
- #liquefaction pressure equation
- #arguments: Temperature
- Pt = 611.657 #Pa
- Tt = 273.16 #K
- a1 = 0.119539337*10**7 #constant a1
- a2 = 0.808183159*10**5 #constant a2
- a3 = 0.333826860*10**4 #constant a3
- b1 = 0.300*10**1 #constant b1
- b2 = 0.2575*10**2 #constant b2
- b3 = 0.103750*10**3 #constant b3
- theta = T/Tt #reduced temperature
- term1 = a1*(1-theta**b1)
- term2 = a2*(1-theta**b2)
- term3 = a3*(1-theta**b3)
- term4 = 1 + term1 + term2 + term3
- pLiq = term4*Pt
- return pLiq
Add Comment
Please, Sign In to add comment