casualguitar

solve_ivp changes

Nov 29th, 2021
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.32 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.  
  7. minL = 0.05 #inlet mass flow (kg/s)
  8. Tsat = 105 #inlet temperature (K)
  9. cpL = 2 #heat capacity (kJ/kg.K)
  10. cpV = 1 #heat capacity (kJ/kg.K)
  11. rhoL = 800 #liquid density (kg/m3)
  12. volumeV = 0.01 #m3
  13. minitial = 800*0.01
  14. area = 0.1 #m2
  15. gasConstant = 287.058*28 #J/kgK
  16. hVap = 200 #kJ/kg
  17. P = 200000 #Pa
  18. mW = 28 #amu
  19. mS = 1 #kg
  20. cpS = 1.1 #kJ/kgK
  21. u = 1 #heat transfer coefficient between fluid and solid (W/m2.K)
  22. T0 = 80 #initial fluid temp (K)
  23.  
  24. Tin = 300 #Inlet fluid temperature (K)
  25.  
  26. hIn = 200 + cpV*(Tin-Tsat) #Inlet enthalpy (kJ/kg)
  27.  
  28. n = 3 # number of elements in spatial array (number of tanks)
  29.  
  30. def temp(H):
  31.     #returns temperature given enthalpy (after processing function)
  32.     if H<=0:
  33.         T = Tsat + (H/cpL)
  34.  
  35.     elif H >= hVap:
  36.         T = Tsat + ((H-hVap)/cpV)
  37.     else:
  38.         T = Tsat
  39.     return T
  40.  
  41. def mass(H,T):
  42.     #returns mass holdup given enthalpy
  43.     if H<=0:
  44.         m = rhoL*volumeV
  45.     elif H >= hVap:
  46.         m = P*mW*volumeV/(gasConstant*T)
  47.         #print("mass goldup = ", m)
  48.     else:
  49.         m = volumeV/((1/rhoL)+((gasConstant*Tsat/(P*mW)) - (1/rhoL))*(H/hVap))
  50.     return m
  51.  
  52. def dpdh(H,T):
  53.     # returns mass holdup given enthalpy
  54.     if H <= 0:
  55.         m = rhoL * volumeV
  56.         dpdh = 0
  57.     elif H >= hVap:
  58.         m = P * mW * volumeV / (gasConstant * T)
  59.         dpdh = -1*(m/(cpV*T*volumeV))
  60.         # print("mass goldup = ", m)
  61.     else:
  62.         m = volumeV / ((1 / rhoL) + ((gasConstant * Tsat / (P * mW)) - (1 / rhoL)) * (H / hVap))
  63.         dpdh = -1*(m**2/volumeV**2)*(((gasConstant*Tsat/(P*mW))-(1/rhoL))/hVap)
  64.  
  65.     return dpdh
  66.  
  67. def dhdtcalc(H,T,Ts,m):
  68.  
  69.     Dhdt = ((minL * (hIn - H)) - (u * area * (T - Ts))) / m
  70.  
  71.     return Dhdt
  72.  
  73. def dtsdtcalc(T,Ts):
  74.  
  75.     DTsdt = ((u * area * (T - Ts))) / (mS * cpS)
  76.  
  77.     return DTsdt
  78.  
  79. def dmdtcalc(dhdt,dpdh):
  80.     dmdt = volumeV * dhdt * dpdh/n
  81.     return dmdt
  82.  
  83. def my_system(t, y, minL, mS, cpS, hIn, u, area):
  84.     H, Ts = y
  85.     T = temp(H)
  86.     m = mass(H,T)
  87.     # could also insert any variables that are dependent on t here
  88.     DHdt = dhdtcalc(H,T,Ts,m)
  89.     DTsdt = dtsdtcalc(T,Ts)
  90.     dydt = [DHdt, DTsdt]  # solving for both derivatives manually
  91.     return dydt
  92.  
  93. # starting condition
  94. H0 = cpL*(T0-Tsat) #fluid starting enthalpy (kJ/kg)
  95. Ts0 = 80 #solid starting temperature (K)
  96. y0 = [H0, Ts0]
  97.  
  98. # time steps
  99. t = (0, 30) #time range
  100. t_eval = np.linspace(0, 30, 30000)  # optional
  101.  
  102. solution = solve_ivp(my_system, t, y0, args=(minL, mS, cpS, hIn, u, area), method='LSODA', t_eval=t_eval)
  103. #print(dhdtextract)
  104.  
  105. #converting fluid enthalpy array back to temperatures
  106. x = solution.y[0] #fluid enthalpy array
  107. x1 = np.array(x) #converting x to numpy array for computation below
  108.  
  109. xs = solution.y[1] #fluid enthalpy array
  110. x2 = np.array(xs) #converting x to numpy array for computation below
  111.  
  112. #print(len(x1))
  113. Temperatures = []
  114. for i in x1:
  115.     Temperatures.append(temp(i))
  116.  
  117. Temperatures = np.array(Temperatures)
  118. massarray = []
  119.  
  120. for h,t in zip(x1,Temperatures):
  121.     massarray.append(mass(h,t))
  122. massarray = np.array(massarray)
  123.  
  124. dpdharray = []
  125. for h,t in zip(x1,Temperatures):
  126.     dpdharray.append(dpdh(h,t))
  127.  
  128. dhdtarray = []
  129. for h,T,Ts,m in zip(x1,Temperatures,x2,massarray):
  130.     dhdtarray.append(dhdtcalc(h,T,Ts,m))
  131.  
  132.  
  133. dhdtarray = np.array(dhdtarray)
  134. #print(dpdharray)
  135. print("dpdh len: ",len(dpdharray))
  136. print("dhdt len: ", len(dhdtarray))
  137. print("mass array len: ",len(massarray))
  138.  
  139. dmdt = volumeV*(np.multiply(dhdtarray,dpdharray))
  140. mout = [minL - dmdt for dmdt in dmdt]
  141.  
  142.  
  143.  
  144. #Creating Semi-log arrays for fluid and solid
  145. solidTs = solution.y[1] #solid temperature array
  146. semilogsolidtemperatures = [Tin - solidTs for solidTs in solidTs] #semi log solid temperatures
  147. semilogfluidtemperatures = [Tin - Temperatures for Temperatures in Temperatures] #converting to semi log fluid temperatures
  148. semilogmassflowout = [minL - mout for mout in mout] #INCORRECT
  149. #print(semilogmassflowout)
  150.  
  151. #slope:
  152. slope = np.log(semilogfluidtemperatures[300]- semilogfluidtemperatures[350])/(350-400)
  153. #print("slope 200 = ", slope)
  154. #print("time constant = ", 1/abs(slope))
  155.  
  156. #plt.plot(solution.t, semilogsolidtemperatures, 'g', label='Tin - T_solid') #plotting solid solution t vs Ts
  157. #plt.plot(solution.t, semilogfluidtemperatures, 'b', label='Tin - T_Fluid') #plotting fluid solution t vs T
  158. #plt.yscale('log')
  159. # plot
  160. #plt.plot(solution.t, solution.y[0], 'm', label='H_fluid') #plotting fluid solution t vs T
  161. #plt.plot(solution.t, solution.y[1], 'g', label='T_solid') #plotting solid solution t vs Ts
  162. #plt.plot(solution.t, Temperatures, 'b', label='T_Fluid') #plotting fluid solution t vs T
  163. #plt.plot(solution.t, massarray, 'c', label='Mass Holdup') #plotting fluid solution t vs T
  164. plt.plot(solution.t, mout, 'g', label='m_out') #plotting fluid solution t vs T
  165. #plt.plot(solution.t, massarray, 'b', label='mass holdup', scaley = 'log') #plotting fluid solution t vs T
  166. #plt.plot(solution.t, dpdharray, 'm', label='dp/dh') #plotting fluid solution t vs T
  167. #print(dpdharray)
  168. plt.yscale('log')
  169.  
  170.  
  171.  
  172. #plt.plot(solution.t, semilogmassflowout, 'b', label='Semi-Log m_out') #plotting fluid solution t vs T
  173.  
  174. plt.legend(loc='best')
  175. plt.xlabel('time')
  176. plt.ylabel('Mass Flow Out (kg/s)')
  177. #plt.ylabel('Tin - T')
  178. plt.grid()
  179. plt.show()
  180.  
  181.  
  182.  
  183.  
Advertisement
Add Comment
Please, Sign In to add comment