Guest User

Untitled

a guest
Aug 8th, 2018
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.23 KB | None | 0 0
  1. #%% - Imports
  2. import numpy as np
  3. from numpy import *
  4. import matplotlib.pylab as plt
  5. from fipy import *
  6. import pandas as pd
  7.  
  8. #%% - Constants
  9. rho_b_MH = 589. # kg m^-3
  10. Cp_b = 181.170 #J (mol*K)^-1, Thermal conductivity and specific heat measurements of metal hydrides
  11.  
  12. # I'm using L in place of lambda
  13. L_eff = 8.4 # W m^-1 K^-1
  14.  
  15. E_c = 45.*1000 # J/mol
  16. Beta = 13.5 #%
  17. P = 100. #atm
  18. M = 2.02 #g/mol
  19. dH = 28.*1000. #J/mol
  20.  
  21. h = 150. # W/m^-2 K^-1
  22. T_c = 60.+273.15 #K, Coolant Temperature
  23. To = 60+273.15 # K, Bed Temperature
  24. R = 8.314 # J/mol/K
  25.  
  26. Peq = ((E_c/dH)/(1+E_c/dH))*P #atm
  27. A = 1.1319e25
  28.  
  29. #%% - Mesh
  30.  
  31. L = 1.
  32. nx = 10.
  33. dx = L/nx
  34.  
  35. # Create the mesh
  36. mesh = Grid1D(nx=nx,dx=dx)
  37. #mesh = CylindricalGrid1D(nx=nx,dx=dx)
  38.  
  39. ##### Sorbtion Setup #####
  40. X = CellVariable(name = "Sorbtion",mesh = mesh, value=0.1, hasOld=True)
  41. CC = mesh.cellCenters[0]
  42.  
  43. #### PDE Variable Setup #####
  44. #T = CellVariable(name = "Temperature", mesh = mesh,value = 60.+273.15)
  45. T = CellVariable(name = "Temperature", mesh = mesh, value = 60.+273.15, hasOld=True)
  46.  
  47. # I want to use the robin boundary condition but in either case, the constraint doesn't seem to be working
  48. #T.constrain(60.+273.15,mesh.facesLeft)
  49. constraint_value = FaceVariable(mesh=mesh)
  50. T.faceGrad.constrain(constraint_value,mesh.facesLeft)
  51.  
  52.  
  53. # I want to use the robin boundary condition
  54. #T.constrain(110.+273,mesh.facesRight)
  55. T.faceGrad.constrain(0.,mesh.facesRight)
  56. import ipdb; ipdb.set_trace()
  57. k_c = A*T**-9.73625
  58. rc = k_c*(1-X)*(P-Peq)
  59.  
  60. param = k_c * (P - Peq)
  61. maxX = 0.9
  62. largeValue = 1e9
  63. constraint = largeValue * (X > maxX)
  64.  
  65. big_value = 1e9
  66.  
  67. eqX = TransientTerm(var = X) == param - ImplicitSourceTerm(param, var=X) + maxX * constraint - ImplicitSourceTerm(constraint, var=X)
  68. eqT = TransientTerm(rho_b_MH * Cp_b, var=T) == DiffusionTerm(coeff = L_eff, var=T) + rho_b_MH*(dH/M)*rc
  69. eq = eqT & eqX
  70.  
  71. #%% - Equation Solution
  72. TSD = .005  # This seems to be a stable time step for this function
  73. sweeps = 150 # identify the number of sweeps to run, running 150 for now
  74. ts = np.linspace(0,TSD*sweeps,sweeps+1)  # ts is the number of time slots plotted for the ODE. create an array to plot for the ode
  75.  
  76. solver = LinearLUSolver() # Trying a different solver
  77.  
  78. #Prepare an array for storing calculated values of X
  79. Xsolution = np.zeros((sweeps+1,int(nx))) #Create a matrix a row for initial value + 1 row for each sweep. There will be nx columns per row.
  80. #Store the initial value of X at all points in the first row of sweeps
  81. Xsolution[0,:] = X.value
  82.  
  83. #Prepare an array for storing calculated values of T
  84. Tsolution = np.zeros((sweeps+1,int(nx))) #Create a matrix a row for initial value + 1 row for each sweep. There will be nx columns per row.
  85. #Store the initial value of T at all points in the first row of sweeps
  86. # This matrix has the form with the rows are T|t=0...T|t=t_max, and columns T|r=0....T|r=R
  87. Tsolution[0,:] = T.value
  88.  
  89. viewer  = Viewer(vars = T, datamin = 330., datamax = 450., title = "Temperature")
  90. viewer2 = Viewer(vars = X, datamin = 0., datamax = 1.05, title = "X")
  91.  
  92. for sweep in range(sweeps):
  93.     constraint_value[:] = h * ((60. + 273.15) - T.faceValue)
  94.     X.updateOld()
  95.     T.updateOld()
  96.     eq.sweep(dt=TSD,solver=solver)
  97.     # eqX.sweep(X, dt=TSD, solver=solver)
  98.     # eqT.sweep(T, dt=TSD, solver=solver)
  99.     viewer.plot()
  100.     viewer2.plot()
  101.     Xsolution[sweep+1] = X.value #Store the recently computed X value in the current sweep
  102.     Tsolution[sweep+1] = T.value #Store the recently computed T value in the current sweep
  103.     #print("X values",X.value[-1])
  104.     #print("T values",T.value[-1])
  105.     #print(rc[-1])
  106.     df=pd.DataFrame([X.value[-1],T.value[-1],k_c[-1],rc[-1]],index = ["X","T","k_c","rc"],columns=[""])
  107.     print(df.T)
  108.  
  109. #print(Xsolution[:,0])
  110. #plt.plot(CC.value,X.value,'ro') # plot the points where calculations are made in the fipy solver.
  111. fig,ax = plt.subplots()
  112. ax.plot(ts,Xsolution[:,0],'bo')
  113. ax.set_xlabel("time")
  114. ax.set_ylabel(X.name)
  115. ax.set_title("ODE Solution")
  116.  
  117. fig,ax = plt.subplots()
  118. ax.plot(ts,Tsolution[:,0],"bo")
  119. ax.set_xlabel("time")
  120. ax.set_ylabel(T.name)
  121. ax.set_title("Time vs. Temp")
  122.  
  123. """
  124. fig,ax = plt.subplots()
  125. ax.plot(ts,Tsolution[-1,:],"bo")
  126. ax.set_xlabel("time")
  127. ax.set_ylabel(T.name)
  128. ax.set_title("Time vs. Temp")
  129. """
Advertisement
Add Comment
Please, Sign In to add comment