Advertisement
Guest User

moren din hahah

a guest
Feb 26th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.57 KB | None | 0 0
  1. warnings.filterwarnings('ignore')
  2. from ipywidgets import widgets, Output
  3. from IPython.display import display
  4. from IPython.html.widgets import *
  5. from IPython.display import clear_output
  6. import numpy as np
  7. import matplotlib.pyplot as plt
  8. from matplotlib.pyplot import figure as fig
  9. %matplotlib inline
  10. sub = ''
  11.  
  12. mode = ''
  13.  
  14.  
  15. D0 = 20 #Initial grain size [μm]
  16. K0 = 10**10 #Kinetic cosntant [μm^2/s]
  17. n = 0.5 #Time exponent
  18. Q = 221000 #Activation energy for grain growth [J/mol]
  19. R = 8.314 #Ideal gas constant [J/(mol*K)]
  20.  
  21. HR = 0.1 #float(input('What is the heating rate?')) #Heating rate [K/s]
  22. Tiso = 1473 #float(input('What is the isothermal heat treating temperature? (in Kelvin)')) #Isothermal heat treatment temperature [K]
  23. tiso = 50 #float(input('How long was the material keept at this temperature? (in seconds)')) #The time for the isothermal heat treatment temperature [s]
  24. CR = 1 #float(input('What is the cooling rate?'))#Cooling rate [K/s]
  25.  
  26. N = 100 #The number of steps the integration will be done in
  27. t1 = (Tiso-298)/HR #The first time value for reaching the isothermal heat treatment temperature [s]
  28. dt1 = t1/N #The time increment for t1 [s]
  29. T_hr = [] #List of temperatures during the heating phase [K]
  30. th=0 #The time values for T_hr
  31. T_cr=[] #List of temperatures during the cooling phase [K]
  32. for i in range(N): #This for loop makes the list of temperatures during the heating phase
  33. T_hr.append(298 + HR*th)
  34. th += dt1
  35.  
  36.  
  37.  
  38.  
  39. tc = 0 #Time valyes for T_cr
  40. tcool = (Tiso-298)/CR #The time it takes for the material to cool down [s]
  41.  
  42. dt2 = tcool/N #The time increment for tcool
  43.  
  44. for i in range(N): #This loop makes the list of temperatures during the cooling phase
  45. T_cr.append(Tiso -tc*CR)
  46. tc += dt2
  47.  
  48.  
  49.  
  50.  
  51. sum1 = 0 #Sum of the integral during the heating phase [s]
  52. for i in range(N): #This loop calculates the integral numerically
  53. sum1 += np.exp(-Q/(R*T_hr[i]))*dt1 - np.exp(-Q/(R*298))*dt1
  54.  
  55. #print(sum1)
  56.  
  57. sum2 = 0 #Sum of the integral during the cooling phase [s]
  58. for i in range(N): #This loop calculates the integral numerically
  59. sum2 += np.exp(-Q/(R*T_cr[i]))*dt2 - np.exp(-Q/(R*298))*dt2
  60.  
  61. D = (D0**(1/n) + (K0*sum1) + (K0*np.exp(-Q/(R*Tiso))*tiso) + (K0*sum2))**n #The grain size [μm]
  62.  
  63. print(K0*sum1)
  64. #print(K0*np.exp(-Q/(R*Tiso))*tiso)
  65. #print(K0*sum2)
  66.  
  67. print(D)
  68.  
  69. def main(mode): #Function for plotting different cases of grain growth
  70.  
  71. D0 = 20
  72. K0 = 10**10
  73. n = 0.5
  74. Q = 221000
  75. R = 8.314
  76.  
  77. HR = 0.1 #float(input('What is the heating rate?'))
  78. Tiso = 1473 #float(input('What is the isothermal heat treating temperature? (in Kelvin)'))
  79. tiso = 50 #float(input('How long was the material keept at this temperature? (in seconds)'))
  80. CR = 1 #float(input('What is the cooling rate?'))
  81.  
  82. N = 100
  83. t1 = (Tiso-298)/HR
  84. dt1 = t1/N
  85. T_hr = []
  86. th=0
  87.  
  88.  
  89. # Varying heating rate
  90.  
  91. if mode == 'Varying HR':
  92. x1 = [] #List of x-values in the plot, which will be the heating rate
  93. y1 = [] #List of y-values in the plot, which will be the grain size
  94. for HR in range(10,200): #For loop which makes the list of heating rates and grain sizes
  95. x1.append(HR*0.01)
  96. t1 = (Tiso-298)/(HR)
  97. dt1 = t1/100
  98. th=0
  99.  
  100. for i in range(100):
  101. T_hr.append(298 + HR*th)
  102. th += dt1
  103.  
  104.  
  105. sum1 = 0
  106. for i in range(100):
  107.  
  108. sum1 += np.exp(-Q/(R*T_hr[i]))*dt1
  109. tc = 0
  110. tcool = (Tiso-298)/CR
  111. sum2 = 0
  112. dt2 = tcool/N
  113. for i in range(N):
  114. T_cr.append(Tiso -tc*CR)
  115. tc += dt2
  116.  
  117.  
  118.  
  119. for i in range(N):
  120. sum2 += np.exp(-Q/(R*T_cr[i]))*dt2 - np.exp(-Q/(R*298))*dt2
  121.  
  122. D = (D0**(1/n) + K0*sum1 + K0*np.exp(-Q/(R*Tiso))*tiso + K0*sum2)**n
  123. y1.append(D)
  124.  
  125. plt.plot(x1,y1) #Plotting of the values
  126. plt.title('Varying heating rate')
  127. plt.xlabel('Heating rate [K/s]')
  128. plt.ylabel('Grain size [micrometer]')
  129. plt.show()
  130.  
  131. #Varying heating rate
  132. elif mode == 'Varying CR':
  133. x2 = [] #List of cooling rates
  134. y2 = [] #List of grain sizes
  135. for CR in range(10,200):
  136. x2.append(CR*0.01)
  137. tc = 0
  138. tcool = (Tiso-298)/CR
  139.  
  140. dt2 = tcool/N
  141.  
  142. for i in range(N):
  143. T_cr.append(Tiso -tc*CR)
  144. tc += dt2
  145. sum2 = 0
  146.  
  147. t1 = (Tiso-298)/(HR)
  148. dt1 = t1/100
  149. T_hr = []
  150. th=0
  151.  
  152. for i in range(100):
  153. T_hr.append(298 + HR*th)
  154. th += dt1
  155.  
  156.  
  157.  
  158. sum1 = 0
  159. for i in range(100):
  160.  
  161. sum1 += np.exp(-Q/(R*T_hr[i]))*dt1
  162.  
  163. for i in range(N):
  164. sum2 += np.exp(-Q/(R*T_cr[i]))*dt2 - np.exp(-Q/(R*298))*dt2
  165. D = (D0**(1/n) + K0*sum1 + K0*np.exp(-Q/(R*Tiso))*tiso + K0*sum2)**n
  166. y2.append(D)
  167.  
  168. plt.plot(x2,y2)
  169. plt.title('Varying cooling rate')
  170. plt.xlabel('Cooling rate [K/s]')
  171. plt.ylabel('Grain size [micrometer]')
  172. plt.show()
  173.  
  174.  
  175. #Varying Tiso
  176. elif mode == 'Varying Tiso':
  177. x3 = [] #List of isothermal temperatures
  178. y3 = [] #List of grain sizes
  179. for Tiso in range(1173,1673):
  180. x3.append(Tiso)
  181.  
  182.  
  183. N = 100
  184. t1 = (Tiso-298)/HR
  185. dt1 = t1/N
  186. T_hr = []
  187. th=0
  188.  
  189. for i in range(N):
  190. T_hr.append(298 + HR*th)
  191. th += dt1
  192.  
  193.  
  194.  
  195.  
  196. tc = 0
  197. tcool = (Tiso-298)/CR
  198.  
  199. dt2 = tcool/N
  200.  
  201. for i in range(N):
  202. T_cr.append(Tiso -tc*CR)
  203. tc += dt2
  204.  
  205. sum2 = 0
  206. for i in range(N):
  207. sum2 += np.exp(-Q/(R*T_cr[i]))*dt2 - np.exp(-Q/(R*298))*dt2
  208.  
  209.  
  210.  
  211. sum1 = 0
  212. for i in range(N):
  213. sum1 += np.exp(-Q/(R*T_hr[i]))*dt1 - np.exp(-Q/(R*298))*dt1
  214.  
  215. D = (D0**(1/n) + K0*sum1 + K0*np.exp(-Q/(R*Tiso))*tiso + K0*sum2)**n
  216. y3.append(D)
  217.  
  218. plt.plot(x3,y3)
  219. plt.title('Varying Tiso')
  220. plt.xlabel('Tiso')
  221. plt.ylabel('Grain size [micrometer]')
  222. plt.show()
  223.  
  224.  
  225. #HR = 0.1 #float(input('What is the heating rate?'))
  226. #Tiso = 1473 #float(input('What is the isothermal heat treating temperature? (in Kelvin)'))
  227. #tiso = 50 #float(input('How long was the material keept at this temperature? (in seconds)'))
  228. #CR = 1
  229. #varying tiso
  230. elif mode == 'Varying tiso':
  231. x4= [] #List of isothermal time values
  232. y4 = [] #List of grain sizes
  233.  
  234. for tiso in range(1,100):
  235. x4.append(tiso)
  236.  
  237. for i in range(N):
  238. T_hr.append(298 + HR*th)
  239. th += dt1
  240.  
  241. tc = 0
  242. tcool = (Tiso-298)/CR
  243.  
  244. dt2 = tcool/N
  245.  
  246. for i in range(N):
  247. T_cr.append(Tiso -tc*CR)
  248. tc += dt2
  249.  
  250. sum1 = 0
  251. for i in range(N):
  252. sum1 += np.exp(-Q/(R*T_hr[i]))*dt1 - np.exp(-Q/(R*298))*dt1
  253.  
  254. #print(sum1)
  255.  
  256. sum2 = 0
  257. for i in range(N):
  258. sum2 += np.exp(-Q/(R*T_cr[i]))*dt2 - np.exp(-Q/(R*298))*dt2
  259.  
  260.  
  261. D = (D0**(1/n) + K0*sum1 + K0*np.exp(-Q/(R*Tiso))*tiso + K0*sum2)**n
  262. y4.append(D)
  263.  
  264.  
  265. plt.plot(x4,y4)
  266. plt.title('Varying tiso')
  267. plt.xlabel('tiso')
  268. plt.ylabel('Grain size [micrometer]')
  269. plt.show()
  270. else:
  271. print('Unknown mode') #If the mode input isn't recognised, this will be printed
  272.  
  273. def sub_task_chooser(sub): #This is a part of the code for making buttons
  274. #The values for each button is assigned here
  275. if sub == 1:
  276. mode = 'Varying HR'
  277. if sub == 2:
  278. mode = 'Varying CR'
  279. if sub == 3:
  280. mode = 'Varying Tiso'
  281. if sub == 4:
  282. mode = 'Varying tiso'
  283. main(mode)
  284.  
  285. sub_menu = widgets.ToggleButtons(
  286. # {'names':corresponding values,}
  287. options={'Varying HR':1, 'Varying CR':2, 'Varying Tiso':3, 'Varying tiso':4},
  288. value = 2, #default value
  289. description='Choose case:', #name
  290. button_style='info', #blue buttons
  291. )
  292.  
  293. interact(sub_task_chooser, sub = sub_menu)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement